1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <sys/types.h>
12 #include <sys/socket.h>
15 #include <xbt/sysdep.h>
16 #include <xbt/mmalloc.h>
18 #include "mc_protocol.h"
19 #include "mc_client.h"
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"
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
28 mc_client_t mc_client;
30 void MC_client_init(void)
33 XBT_WARN("MC_client_init called more than once.");
37 char* fd_env = getenv(MC_ENV_SOCKET_FD);
39 xbt_die("MC socket not found");
41 int fd = atoi(fd_env);
42 XBT_DEBUG("Model-checked application found socket FD %i", fd);
45 socklen_t socklen = sizeof(type);
46 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
47 xbt_die("Could not check socket type: %s", strerror(errno));
48 if (type != SOCK_DGRAM)
49 xbt_die("Unexpected socket type %i", type);
50 XBT_DEBUG("Model-checked application found expected socket type");
52 mc_client = xbt_new0(s_mc_client_t, 1);
54 mc_client->active = 1;
57 void MC_client_hello(void)
59 XBT_DEBUG("Greeting the MC server");
60 if (MC_protocol_hello(mc_client->fd) != 0)
61 xbt_die("Could not say hello the MC server");
62 XBT_DEBUG("Greeted the MC server");
65 void MC_client_send_message(void* message, size_t size)
67 if (MC_protocol_send(mc_client->fd, message, size))
68 xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type);
71 void MC_client_handle_messages(void)
74 XBT_DEBUG("Waiting messages from model-checker");
76 char message_buffer[MC_MESSAGE_LENGTH];
78 if ((s = recv(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
79 xbt_die("Could not receive commands from the model-checker: %s",
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 case MC_MESSAGE_CONTINUE:
91 xbt_die("Unexpected message from model-checker %i", message.type);