Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove useless declarations
[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
22 #ifdef __linux__
23 #include <sys/prctl.h>
24 #endif
25
26 #include <xbt/log.h>
27
28 #include "simgrid/sg_config.h"
29 #include "xbt_modinter.h"
30
31 #include "mc_base.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"
38
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
40
41 static int do_child(int socket, char** argv)
42 {
43   XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
44
45 #ifdef __linux__
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;
50   }
51 #endif
52
53   int res;
54
55   // Remove CLOEXEC in order to pass the socket to the exec-ed program:
56   int fdflags = fcntl(socket, F_GETFD, 0);
57   if (fdflags == -1) {
58     std::perror("simgrid-mc");
59     return MC_SERVER_ERROR;
60   }
61   if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
62     std::perror("simgrid-mc");
63     return MC_SERVER_ERROR;
64   }
65
66   XBT_DEBUG("CLOEXEC removed on socket %i", socket);
67
68   // Set environment:
69   setenv(MC_ENV_VARIABLE, "1", 1);
70
71   // Disable lazy relocation in the model-ched process.
72   // We don't want the model-checked process to modify its .got.plt during
73   // snapshot.
74   setenv("LC_BIND_NOW", "1", 1);
75
76   char buffer[64];
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);
81
82   execvp(argv[1], argv+1);
83   std::perror("simgrid-mc");
84   return MC_SERVER_ERROR;
85 }
86
87 static int do_parent(int socket, pid_t child)
88 {
89   XBT_DEBUG("Inside the parent process");
90   if (mc_server)
91     xbt_die("MC server already present");
92   try {
93     mc_mode = MC_MODE_SERVER;
94     mc_server = new s_mc_server(child, socket);
95     mc_server->start();
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();
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     return do_child(sockets[0], argv);
153   } else {
154     close(sockets[0]);
155     return do_parent(sockets[1], pid);
156   }
157
158   return 0;
159 }