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. */
18 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <sys/prctl.h>
25 #include "simgrid/sg_config.h"
26 #include "xbt_modinter.h"
29 #include "mc_private.h"
30 #include "mc_protocol.h"
31 #include "mc_server.h"
32 #include "mc_safety.h"
33 #include "mc_comm_pattern.h"
34 #include "mc_liveness.h"
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
38 static int do_child(int socket, char** argv)
40 XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
41 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
42 std::perror("simgrid-mc");
43 return MC_SERVER_ERROR;
47 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
48 int fdflags = fcntl(socket, F_GETFD, 0);
50 std::perror("simgrid-mc");
51 return MC_SERVER_ERROR;
53 if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
54 std::perror("simgrid-mc");
55 return MC_SERVER_ERROR;
58 XBT_DEBUG("CLOEXEC removed on socket %i", socket);
61 setenv(MC_ENV_VARIABLE, "1", 1);
64 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
65 if ((size_t) res >= sizeof(buffer) || res == -1)
66 return MC_SERVER_ERROR;
67 setenv(MC_ENV_SOCKET_FD, buffer, 1);
69 execvp(argv[1], argv+1);
70 std::perror("simgrid-mc");
71 return MC_SERVER_ERROR;
74 static int do_parent(int socket, pid_t child)
76 XBT_DEBUG("Inside the parent process");
78 xbt_die("MC server already present");
80 mc_mode = MC_MODE_SERVER;
81 mc_server = new s_mc_server(child, socket);
83 MC_init_model_checker(child, socket);
84 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
85 MC_modelcheck_comm_determinism();
86 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
87 MC_modelcheck_safety();
89 MC_modelcheck_liveness();
90 mc_server->shutdown();
93 catch(std::exception& e) {
94 XBT_ERROR("Exception: %s", e.what());
96 exit(MC_SERVER_ERROR);
99 static char** argvdup(int argc, char** argv)
101 char** argv_copy = xbt_new(char*, argc+1);
102 std::memcpy(argv_copy, argv, sizeof(char*) * argc);
103 argv_copy[argc] = NULL;
107 int main(int argc, char** argv)
109 // We need to keep the original parameters in order to pass them to the
110 // model-checked process:
111 int argc_copy = argc;
112 char** argv_copy = argvdup(argc, argv);
113 xbt_log_init(&argc_copy, argv_copy);
114 sg_config_init(&argc_copy, argv_copy);
117 xbt_die("Missing arguments.\n");
119 bool server_mode = true;
120 char* env = std::getenv("SIMGRID_MC_MODE");
122 if (std::strcmp(env, "server") == 0)
124 else if (std::strcmp(env, "standalone") == 0)
127 xbt_die("Unrecognised value for SIMGRID_MC_MODE (server/standalone)");
131 setenv(MC_ENV_VARIABLE, "1", 1);
132 execvp(argv[1], argv+1);
134 std::perror("simgrid-mc");
138 // Create a AF_LOCAL socketpair:
142 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
144 perror("simgrid-mc");
145 return MC_SERVER_ERROR;
148 XBT_DEBUG("Created socketpair");
152 perror("simgrid-mc");
153 return MC_SERVER_ERROR;
154 } else if (pid == 0) {
156 return do_child(sockets[0], argv);
159 return do_parent(sockets[1], pid);