Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fb123fa7505807496ec413137c162786b56b12aa
[simgrid.git] / src / mc / mc_server.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 <memory>
8 #include <system_error>
9
10 #include <poll.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <sys/socket.h>
14 #include <sys/signalfd.h>
15
16 #include <xbt/log.h>
17
18 #include "mc_model_checker.h"
19 #include "mc_protocol.h"
20 #include "mc_server.h"
21 #include "mc_private.h"
22 #include "mc_ignore.h"
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_server, mc, "MC server logic");
25
26 // HArdcoded index for now:
27 #define SOCKET_FD_INDEX 0
28 #define SIGNAL_FD_INDEX 1
29
30 mc_server_t mc_server;
31
32 s_mc_server::s_mc_server(pid_t pid, int socket)
33 {
34   this->pid = pid;
35   this->socket = socket;
36 }
37
38 void s_mc_server::start()
39 {
40   /* Wait for the target process to initialize and exchange a HELLO messages
41    * before trying to look at its memory map.
42    */
43   XBT_DEBUG("Greeting the MC client");
44   int res = MC_protocol_hello(socket);
45   if (res != 0)
46     throw std::system_error(res, std::system_category());
47   XBT_DEBUG("Greeted the MC client");
48
49   // Block SIGCHLD (this will be handled with accept/signalfd):
50   sigset_t set;
51   sigemptyset(&set);
52   sigaddset(&set, SIGCHLD);
53   if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
54     throw std::system_error(errno, std::system_category());
55
56   sigset_t full_set;
57   sigfillset(&full_set);
58
59   // Prepare data for poll:
60
61   struct pollfd* socket_pollfd = &fds[SOCKET_FD_INDEX];
62   socket_pollfd->fd = socket;
63   socket_pollfd->events = POLLIN;
64   socket_pollfd->revents = 0;
65
66   int signal_fd = signalfd(-1, &set, 0);
67   if (signal_fd == -1)
68     throw std::system_error(errno, std::system_category());
69
70   struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
71   signalfd_pollfd->fd = signal_fd;
72   signalfd_pollfd->events = POLLIN;
73   signalfd_pollfd->revents = 0;
74 }
75
76 void s_mc_server::shutdown()
77 {
78   XBT_DEBUG("Shuting down model-checker");
79
80   mc_process_t process = &mc_model_checker->process;
81   int status = process->status;
82   if (process->running) {
83     XBT_DEBUG("Killing process");
84     kill(process->pid, SIGTERM);
85     if (waitpid(process->pid, &status, 0) == -1)
86       throw std::system_error(errno, std::system_category());
87     // TODO, handle the case when the process does not want to die with a timeout
88     process->status = status;
89   }
90 }
91
92 void s_mc_server::exit()
93 {
94   // Finished:
95   int status = mc_model_checker->process.status;
96   if (WIFEXITED(status))
97     ::exit(WEXITSTATUS(status));
98   else if (WIFSIGNALED(status)) {
99     // Try to uplicate the signal of the model-checked process.
100     // This is a temporary hack so we don't try too hard.
101     kill(mc_model_checker->process.pid, WTERMSIG(status));
102     abort();
103   } else {
104     xbt_die("Unexpected status from model-checked process");
105   }
106 }
107
108 void s_mc_server::resume(mc_process_t process)
109 {
110   int socket = process->socket;
111   int res = MC_protocol_send_simple_message(socket, MC_MESSAGE_CONTINUE);
112   if (res)
113     throw std::system_error(res, std::system_category());
114 }
115
116 static
117 void throw_socket_error(int fd)
118 {
119   int error = 0;
120   socklen_t errlen = sizeof(error);
121   if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == -1)
122     error = errno;
123   throw std::system_error(error, std::system_category());
124 }
125
126 void s_mc_server::handle_events()
127 {
128   char buffer[MC_MESSAGE_LENGTH];
129   struct pollfd* socket_pollfd = &fds[SOCKET_FD_INDEX];
130   struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
131
132   while(poll(fds, 2, -1) == -1) {
133     switch(errno) {
134     case EINTR:
135       continue;
136     default:
137       throw std::system_error(errno, std::system_category());
138     }
139   }
140
141   if (socket_pollfd->revents) {
142     if (socket_pollfd->revents & POLLIN) {
143
144       ssize_t size = recv(socket_pollfd->fd, buffer, sizeof(buffer), MSG_DONTWAIT);
145       if (size == -1 && errno != EAGAIN)
146         throw std::system_error(errno, std::system_category());
147
148       s_mc_message_t base_message;
149       if (size < (ssize_t) sizeof(base_message))
150         xbt_die("Broken message");
151       memcpy(&base_message, buffer, sizeof(base_message));
152
153       switch(base_message.type) {
154
155       case MC_MESSAGE_IGNORE_HEAP:
156         XBT_DEBUG("Received ignored region");
157         if (size != sizeof(s_mc_ignore_heap_message_t))
158           xbt_die("Broken messsage");
159         // TODO, handle the message
160         break;
161
162       case MC_MESSAGE_UNIGNORE_HEAP:
163         XBT_DEBUG("Received unignored region");
164         if (size != sizeof(s_mc_ignore_memory_message_t))
165           xbt_die("Broken messsage");
166         // TODO, handle the message
167         break;
168
169       case MC_MESSAGE_IGNORE_MEMORY:
170         {
171           XBT_DEBUG("Received ignored memory");
172           if (size != sizeof(s_mc_ignore_memory_message_t))
173             xbt_die("Broken messsage");
174           s_mc_ignore_memory_message_t message;
175           memcpy(&message, buffer, sizeof(message));
176           MC_process_ignore_memory(&mc_model_checker->process,
177             message.addr, message.size);
178         }
179         break;
180
181       default:
182         xbt_die("Unexpected message from model-checked application");
183
184       }
185       return;
186     }
187     if (socket_pollfd->revents & POLLERR) {
188       throw_socket_error(socket_pollfd->fd);
189     }
190     if (socket_pollfd->revents & POLLHUP)
191       xbt_die("Socket hang up?");
192   }
193
194   if (signalfd_pollfd->revents) {
195     if (signalfd_pollfd->revents & POLLIN) {
196       this->handle_signals();
197       return;
198     }
199     if (signalfd_pollfd->revents & POLLERR) {
200       throw_socket_error(signalfd_pollfd->fd);
201     }
202     if (signalfd_pollfd->revents & POLLHUP)
203       xbt_die("Signalfd hang up?");
204   }
205 }
206
207 void s_mc_server::loop()
208 {
209   while (mc_model_checker->process.running)
210     this->handle_events();
211 }
212
213 void s_mc_server::handle_signals()
214 {
215   struct signalfd_siginfo info;
216   struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
217   while (1) {
218     ssize_t size = read(signalfd_pollfd->fd, &info, sizeof(info));
219     if (size == -1) {
220       if (errno == EINTR)
221         continue;
222       else
223         throw std::system_error(errno, std::system_category());
224     } else if (size != sizeof(info))
225         return throw std::runtime_error(
226           "Bad communication with model-checked application");
227     else
228       break;
229   }
230   this->on_signal(&info);
231 }
232
233 void s_mc_server::handle_waitpid()
234 {
235   XBT_DEBUG("Check for wait event");
236   int status;
237   pid_t pid;
238   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
239     if (pid == -1) {
240       if (errno == ECHILD) {
241         // No more children:
242         if (mc_model_checker->process.running)
243           xbt_die("Inconsistent state");
244         else
245           break;
246       } else {
247         XBT_ERROR("Could not wait for pid: %s", strerror(errno));
248         throw std::system_error(errno, std::system_category());
249       }
250     }
251
252     if (pid == mc_model_checker->process.pid) {
253       if (WIFEXITED(status) || WIFSIGNALED(status)) {
254         XBT_DEBUG("Child process is over");
255         mc_model_checker->process.status = status;
256         mc_model_checker->process.running = false;
257       }
258     }
259   }
260 }
261
262 void s_mc_server::on_signal(const struct signalfd_siginfo* info)
263 {
264   switch(info->ssi_signo) {
265   case SIGCHLD:
266     this->handle_waitpid();
267     break;
268   default:
269     break;
270   }
271 }