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"
33 #include "mc_private.h"
34 #include "mc_protocol.h"
35 #include "src/mc/Server.hpp"
36 #include "mc_safety.h"
37 #include "mc_comm_pattern.h"
38 #include "mc_liveness.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:
48 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
49 std::perror("simgrid-mc");
50 return SIMGRID_MC_EXIT_ERROR;
56 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
57 int fdflags = fcntl(socket, F_GETFD, 0);
59 std::perror("simgrid-mc");
60 return SIMGRID_MC_EXIT_ERROR;
62 if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
63 std::perror("simgrid-mc");
64 return SIMGRID_MC_EXIT_ERROR;
67 XBT_DEBUG("CLOEXEC removed on socket %i", socket);
70 setenv(MC_ENV_VARIABLE, "1", 1);
72 // Disable lazy relocation in the model-ched process.
73 // We don't want the model-checked process to modify its .got.plt during
75 setenv("LC_BIND_NOW", "1", 1);
78 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
79 if ((size_t) res >= sizeof(buffer) || res == -1)
80 return SIMGRID_MC_EXIT_ERROR;
81 setenv(MC_ENV_SOCKET_FD, buffer, 1);
83 execvp(argv[1], argv+1);
84 XBT_ERROR("Could not execute the child process");
85 return SIMGRID_MC_EXIT_ERROR;
88 static int do_parent(int socket, pid_t child)
90 XBT_DEBUG("Inside the parent process");
91 if (simgrid::mc::server)
92 xbt_die("MC server already present");
94 mc_mode = MC_MODE_SERVER;
95 simgrid::mc::server = new simgrid::mc::Server(child, socket);
96 simgrid::mc::server->start();
98 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
99 res = MC_modelcheck_comm_determinism();
100 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
101 res = MC_modelcheck_safety();
103 res = MC_modelcheck_liveness();
104 simgrid::mc::server->shutdown();
107 catch(std::exception& e) {
108 XBT_ERROR("Exception: %s", e.what());
109 return SIMGRID_MC_EXIT_ERROR;
113 static char** argvdup(int argc, char** argv)
115 char** argv_copy = xbt_new(char*, argc+1);
116 std::memcpy(argv_copy, argv, sizeof(char*) * argc);
117 argv_copy[argc] = NULL;
121 int main(int argc, char** argv)
123 _sg_do_model_check = 1;
125 // We need to keep the original parameters in order to pass them to the
126 // model-checked process:
127 int argc_copy = argc;
128 char** argv_copy = argvdup(argc, argv);
129 xbt_log_init(&argc_copy, argv_copy);
130 sg_config_init(&argc_copy, argv_copy);
133 xbt_die("Missing arguments.\n");
135 // Create a AF_LOCAL socketpair:
139 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
141 perror("simgrid-mc");
142 return SIMGRID_MC_EXIT_ERROR;
145 XBT_DEBUG("Created socketpair");
149 perror("simgrid-mc");
150 return SIMGRID_MC_EXIT_ERROR;
151 } else if (pid == 0) {
153 int res = do_child(sockets[0], argv);
154 XBT_DEBUG("Error in the child process creation");
158 return do_parent(sockets[1], pid);