Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
93cf397e2b9aace7787d207a9c250d6c5b1cbb27
[simgrid.git] / src / mc / simgrid_mc.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <exception>
8
9 #include <cstdlib>
10 #include <cstdio>
11 #include <cstring>
12
13 #include <signal.h>
14 #include <poll.h>
15
16 #include <unistd.h>
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/wait.h>
21 #include <sys/ptrace.h>
22
23 #ifdef __linux__
24 #include <sys/prctl.h>
25 #endif
26
27 #include <xbt/log.h>
28
29 #include "simgrid/sg_config.h"
30 #include "src/xbt_modinter.h"
31
32 #include "mc_base.h"
33 #include "mc_private.h"
34 #include "mc_protocol.h"
35 #include "mc_server.h"
36 #include "mc_safety.h"
37 #include "mc_comm_pattern.h"
38 #include "mc_liveness.h"
39
40 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
41
42 static int do_child(int socket, char** argv)
43 {
44   XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
45
46 #ifdef __linux__
47   // Make sure we do not outlive our parent:
48   if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
49     std::perror("simgrid-mc");
50     return MC_SERVER_ERROR;
51   }
52 #endif
53
54   int res;
55
56   // Remove CLOEXEC in order to pass the socket to the exec-ed program:
57   int fdflags = fcntl(socket, F_GETFD, 0);
58   if (fdflags == -1) {
59     std::perror("simgrid-mc");
60     return MC_SERVER_ERROR;
61   }
62   if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
63     std::perror("simgrid-mc");
64     return MC_SERVER_ERROR;
65   }
66
67   XBT_DEBUG("CLOEXEC removed on socket %i", socket);
68
69   // Set environment:
70   setenv(MC_ENV_VARIABLE, "1", 1);
71
72   // Disable lazy relocation in the model-ched process.
73   // We don't want the model-checked process to modify its .got.plt during
74   // snapshot.
75   setenv("LC_BIND_NOW", "1", 1);
76
77   char buffer[64];
78   res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
79   if ((size_t) res >= sizeof(buffer) || res == -1)
80     return MC_SERVER_ERROR;
81   setenv(MC_ENV_SOCKET_FD, buffer, 1);
82
83   execvp(argv[1], argv+1);
84   XBT_ERROR("Could not execute the child process");
85   return MC_SERVER_ERROR;
86 }
87
88 static int do_parent(int socket, pid_t child)
89 {
90   XBT_DEBUG("Inside the parent process");
91   if (mc_server)
92     xbt_die("MC server already present");
93   try {
94     mc_mode = MC_MODE_SERVER;
95     mc_server = new s_mc_server(child, socket);
96     mc_server->start();
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();
101     else
102       MC_modelcheck_liveness();
103     mc_server->shutdown();
104     mc_server->exit();
105   }
106   catch(std::exception& e) {
107     XBT_ERROR("Exception: %s", e.what());
108   }
109   exit(MC_SERVER_ERROR);
110 }
111
112 static char** argvdup(int argc, char** argv)
113 {
114   char** argv_copy = xbt_new(char*, argc+1);
115   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
116   argv_copy[argc] = NULL;
117   return argv_copy;
118 }
119
120 int main(int argc, char** argv)
121 {
122   _sg_do_model_check = 1;
123
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);
130
131   if (argc < 2)
132     xbt_die("Missing arguments.\n");
133
134   // Create a AF_LOCAL socketpair:
135   int res;
136
137   int sockets[2];
138   res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
139   if (res == -1) {
140     perror("simgrid-mc");
141     return MC_SERVER_ERROR;
142   }
143
144   XBT_DEBUG("Created socketpair");
145
146   pid_t pid = fork();
147   if (pid < 0) {
148     perror("simgrid-mc");
149     return MC_SERVER_ERROR;
150   } else if (pid == 0) {
151     close(sockets[1]);
152     int res = do_child(sockets[0], argv);
153     XBT_DEBUG("Error in the child process creation");
154     return res;
155   } else {
156     close(sockets[0]);
157     return do_parent(sockets[1], pid);
158   }
159
160   return 0;
161 }