Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remote xbt_dynar_foreach() in MC_request_testany_fail()
[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 #include <xbt/mmalloc.h>
17
18 #include "mc_protocol.h"
19 #include "mc_client.h"
20
21 // We won't need those once the separation MCer/MCed is complete:
22 #include "mc_mmalloc.h"
23 #include "mc_ignore.h"
24 #include "mc_model_checker.h"
25 #include "mc_private.h" // MC_deadlock_check()
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
28
29 mc_client_t mc_client;
30
31 void MC_client_init(void)
32 {
33   if (mc_client) {
34     XBT_WARN("MC_client_init called more than once.");
35     return;
36   }
37
38   char* fd_env = getenv(MC_ENV_SOCKET_FD);
39   if (!fd_env)
40     xbt_die("MC socket not found");
41
42   int fd = atoi(fd_env);
43   XBT_DEBUG("Model-checked application found socket FD %i", fd);
44
45   int type;
46   socklen_t socklen = sizeof(type);
47   if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
48     xbt_die("Could not check socket type");
49   if (type != SOCK_DGRAM)
50     xbt_die("Unexpected socket type %i", type);
51   XBT_DEBUG("Model-checked application found expected socket type");
52
53   mc_client = xbt_new0(s_mc_client_t, 1);
54   mc_client->fd = fd;
55   mc_client->active = 1;
56 }
57
58 void MC_client_hello(void)
59 {
60   XBT_DEBUG("Greeting the MC server");
61   if (MC_protocol_hello(mc_client->fd) != 0)
62     xbt_die("Could not say hello the MC server");
63   XBT_DEBUG("Greeted the MC server");
64 }
65
66 void MC_client_send_message(void* message, size_t size)
67 {
68   if (MC_protocol_send(mc_client->fd, message, size))
69     xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type);
70 }
71
72 void MC_client_handle_messages(void)
73 {
74   while (1) {
75     XBT_DEBUG("Waiting messages from model-checker");
76
77     char message_buffer[MC_MESSAGE_LENGTH];
78     size_t s;
79     if ((s = recv(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
80       xbt_die("Could not receive commands from the model-checker");
81
82     XBT_DEBUG("Receive message from model-checker");
83     s_mc_message_t message;
84     if (s < sizeof(message))
85       xbt_die("Message is too short");
86     memcpy(&message, message_buffer, sizeof(message));
87     switch (message.type) {
88
89     case MC_MESSAGE_DEADLOCK_CHECK:
90       {
91         int result = MC_deadlock_check();
92         s_mc_int_message_t answer;
93         answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
94         answer.value = result;
95         if (MC_protocol_send(mc_client->fd, &answer, sizeof(answer)))
96           xbt_die("Could nor send response");
97       }
98       break;
99
100     case MC_MESSAGE_CONTINUE:
101       return;
102
103     default:
104       xbt_die("Unexpected message from model-checker %i", message.type);
105     }
106   }
107 }