Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Communication of heap_area_to_ignore to the remote MCer
[simgrid.git] / src / mc / mc_client.c
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <error.h>
10
11 #include <sys/types.h>
12 #include <sys/socket.h>
13
14 #include <xbt/log.h>
15 #include <xbt/sysdep.h>
16
17 #include "mc_protocol.h"
18 #include "mc_client.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
21
22 mc_client_t mc_client;
23
24 void MC_client_init(void)
25 {
26   if (mc_client) {
27     XBT_WARN("MC_client_init called more than once.");
28     return;
29   }
30
31   char* fd_env = getenv(MC_ENV_SOCKET_FD);
32   if (!fd_env)
33     xbt_die("MC socket not found");
34
35   int fd = atoi(fd_env);
36   XBT_DEBUG("Model-checked application found socket FD %i", fd);
37
38   int type;
39   socklen_t socklen = sizeof(type);
40   if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
41     xbt_die("Could not check socket type: %s", strerror(errno));
42   if (type != SOCK_DGRAM)
43     xbt_die("Unexpected socket type %i", type);
44   XBT_DEBUG("Model-checked application found expected socket type");
45
46   mc_client = xbt_new0(s_mc_client_t, 1);
47   mc_client->fd = fd;
48   mc_client->active = 1;
49 }
50
51 void MC_client_hello(void)
52 {
53   XBT_DEBUG("Greeting the MC server");
54   if (MC_protocol_hello(mc_client->fd) != 0)
55     xbt_die("Could not say hello the MC server");
56   XBT_DEBUG("Greeted the MC server");
57 }
58
59 void MC_client_handle_messages(void)
60 {
61   while (1) {
62     XBT_DEBUG("Waiting messages from model-checker");
63
64     char message_buffer[MC_MESSAGE_LENGTH];
65     size_t s;
66     if ((s = recv(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
67       xbt_die("Could not receive commands from the model-checker: %s",
68         strerror(errno));
69
70     XBT_DEBUG("Receive message from model-checker");
71     s_mc_message_t message;
72     if (s < sizeof(message))
73       xbt_die("Message is too short");
74     memcpy(&message, message_buffer, sizeof(message));
75     switch (message.type) {
76     case MC_MESSAGE_CONTINUE:
77       return;
78     default:
79       xbt_die("Unexpected message from model-checker %i", message.type);
80     }
81   }
82 }