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
index 2f30581..55cc2ca 100644 (file)
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
 
-typedef struct s_mc_client {
-  int active;
-  int fd;
-} s_mc_client_t, mc_client_t;
-
-static s_mc_client_t mc_client;
+mc_client_t mc_client;
 
 void MC_client_init(void)
 {
-  mc_client.fd = -1;
-  mc_client.active = 1;
+  if (mc_client) {
+    XBT_WARN("MC_client_init called more than once.");
+    return;
+  }
+
   char* fd_env = getenv(MC_ENV_SOCKET_FD);
   if (!fd_env)
     xbt_die("MC socket not found");
@@ -45,13 +43,15 @@ void MC_client_init(void)
     xbt_die("Unexpected socket type %i", type);
   XBT_DEBUG("Model-checked application found expected socket type");
 
-  mc_client.fd = fd;
+  mc_client = xbt_new0(s_mc_client_t, 1);
+  mc_client->fd = fd;
+  mc_client->active = 1;
 }
 
 void MC_client_hello(void)
 {
   XBT_DEBUG("Greeting the MC server");
-  if (MC_protocol_hello(mc_client.fd) != 0)
+  if (MC_protocol_hello(mc_client->fd) != 0)
     xbt_die("Could not say hello the MC server");
   XBT_DEBUG("Greeted the MC server");
 }
@@ -60,11 +60,18 @@ void MC_client_handle_messages(void)
 {
   while (1) {
     XBT_DEBUG("Waiting messages from model-checker");
-    s_mc_message_t message;
-    if (recv(mc_client.fd, &message, sizeof(message), 0) == -1)
+
+    char message_buffer[MC_MESSAGE_LENGTH];
+    size_t s;
+    if ((s = recv(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
       xbt_die("Could not receive commands from the model-checker: %s",
         strerror(errno));
+
     XBT_DEBUG("Receive message from model-checker");
+    s_mc_message_t message;
+    if (s < sizeof(message))
+      xbt_die("Message is too short");
+    memcpy(&message, message_buffer, sizeof(message));
     switch (message.type) {
     case MC_MESSAGE_CONTINUE:
       return;