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/ptrace.h>
24 #include <sys/prctl.h>
29 #include "simgrid/sg_config.h"
30 #include "src/xbt_modinter.h"
32 #include "src/mc/mc_base.h"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_protocol.h"
35 #include "src/mc/mc_safety.h"
36 #include "src/mc/mc_comm_pattern.h"
37 #include "src/mc/mc_liveness.h"
38 #include "src/mc/mc_exit.h"
40 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
42 static int do_child(int socket, char** argv)
44 XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
47 // Make sure we do not outlive our parent:
50 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0) {
51 std::perror ("sigprocmask");
52 return SIMGRID_MC_EXIT_ERROR;
55 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
56 std::perror("simgrid-mc");
57 return SIMGRID_MC_EXIT_ERROR;
63 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
64 int fdflags = fcntl(socket, F_GETFD, 0);
66 std::perror("simgrid-mc");
67 return SIMGRID_MC_EXIT_ERROR;
69 if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
70 std::perror("simgrid-mc");
71 return SIMGRID_MC_EXIT_ERROR;
74 XBT_DEBUG("CLOEXEC removed on socket %i", socket);
77 setenv(MC_ENV_VARIABLE, "1", 1);
79 // Disable lazy relocation in the model-ched process.
80 // We don't want the model-checked process to modify its .got.plt during
82 setenv("LC_BIND_NOW", "1", 1);
85 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
86 if ((size_t) res >= sizeof(buffer) || res == -1)
87 return SIMGRID_MC_EXIT_ERROR;
88 setenv(MC_ENV_SOCKET_FD, buffer, 1);
90 execvp(argv[1], argv+1);
91 XBT_ERROR("Could not execute the child process");
92 return SIMGRID_MC_EXIT_ERROR;
95 static int do_parent(int socket, pid_t child)
97 XBT_DEBUG("Inside the parent process");
99 xbt_die("MC server already present");
101 mc_mode = MC_MODE_SERVER;
102 mc_model_checker = new simgrid::mc::ModelChecker(child, socket);
103 mc_model_checker->start();
105 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
106 res = MC_modelcheck_comm_determinism();
107 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
108 res = MC_modelcheck_safety();
110 res = MC_modelcheck_liveness();
111 mc_model_checker->shutdown();
114 catch(std::exception& e) {
115 XBT_ERROR("Exception: %s", e.what());
116 return SIMGRID_MC_EXIT_ERROR;
120 static char** argvdup(int argc, char** argv)
122 char** argv_copy = xbt_new(char*, argc+1);
123 std::memcpy(argv_copy, argv, sizeof(char*) * argc);
124 argv_copy[argc] = NULL;
128 int main(int argc, char** argv)
130 _sg_do_model_check = 1;
132 // We need to keep the original parameters in order to pass them to the
133 // model-checked process:
134 int argc_copy = argc;
135 char** argv_copy = argvdup(argc, argv);
136 xbt_log_init(&argc_copy, argv_copy);
137 sg_config_init(&argc_copy, argv_copy);
140 xbt_die("Missing arguments.\n");
142 // Create a AF_LOCAL socketpair:
146 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
148 perror("simgrid-mc");
149 return SIMGRID_MC_EXIT_ERROR;
152 XBT_DEBUG("Created socketpair");
156 perror("simgrid-mc");
157 return SIMGRID_MC_EXIT_ERROR;
158 } else if (pid == 0) {
160 int res = do_child(sockets[0], argv);
161 XBT_DEBUG("Error in the child process creation");
165 return do_parent(sockets[1], pid);