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>
23 #include <sys/prctl.h>
28 #include "simgrid/sg_config.h"
29 #include "xbt_modinter.h"
32 #include "mc_private.h"
33 #include "mc_protocol.h"
34 #include "mc_server.h"
35 #include "mc_safety.h"
36 #include "mc_comm_pattern.h"
37 #include "mc_liveness.h"
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
41 static int do_child(int socket, char** argv)
43 XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
46 // Make sure we do not outlive our parent:
47 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
48 std::perror("simgrid-mc");
49 return MC_SERVER_ERROR;
55 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
56 int fdflags = fcntl(socket, F_GETFD, 0);
58 std::perror("simgrid-mc");
59 return MC_SERVER_ERROR;
61 if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
62 std::perror("simgrid-mc");
63 return MC_SERVER_ERROR;
66 XBT_DEBUG("CLOEXEC removed on socket %i", socket);
69 setenv(MC_ENV_VARIABLE, "1", 1);
71 // Disable lazy relocation in the model-ched process.
72 // We don't want the model-checked process to modify its .got.plt during
74 setenv("LC_BIND_NOW", "1", 1);
77 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
78 if ((size_t) res >= sizeof(buffer) || res == -1)
79 return MC_SERVER_ERROR;
80 setenv(MC_ENV_SOCKET_FD, buffer, 1);
82 execvp(argv[1], argv+1);
83 std::perror("simgrid-mc");
84 return MC_SERVER_ERROR;
87 static int do_parent(int socket, pid_t child)
89 XBT_DEBUG("Inside the parent process");
91 xbt_die("MC server already present");
93 mc_mode = MC_MODE_SERVER;
94 mc_server = new s_mc_server(child, socket);
96 MC_init_model_checker(child, socket);
97 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
98 MC_modelcheck_comm_determinism();
99 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
100 MC_modelcheck_safety();
102 MC_modelcheck_liveness();
103 mc_server->shutdown();
106 catch(std::exception& e) {
107 XBT_ERROR("Exception: %s", e.what());
109 exit(MC_SERVER_ERROR);
112 static char** argvdup(int argc, char** argv)
114 char** argv_copy = xbt_new(char*, argc+1);
115 std::memcpy(argv_copy, argv, sizeof(char*) * argc);
116 argv_copy[argc] = NULL;
120 int main(int argc, char** argv)
122 _sg_do_model_check = 1;
124 // We need to keep the original parameters in order to pass them to the
125 // model-checked process:
126 int argc_copy = argc;
127 char** argv_copy = argvdup(argc, argv);
128 xbt_log_init(&argc_copy, argv_copy);
129 sg_config_init(&argc_copy, argv_copy);
132 xbt_die("Missing arguments.\n");
134 // Create a AF_LOCAL socketpair:
138 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
140 perror("simgrid-mc");
141 return MC_SERVER_ERROR;
144 XBT_DEBUG("Created socketpair");
148 perror("simgrid-mc");
149 return MC_SERVER_ERROR;
150 } else if (pid == 0) {
152 return do_child(sockets[0], argv);
155 return do_parent(sockets[1], pid);