Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Implement remote support for MC_ignore
[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_REGION:
156         XBT_DEBUG("Received ignored region");
157         if (size != sizeof(s_mc_ignore_region_message_t))
158           xbt_die("Broken messsage");
159         // TODO, handle the message
160         break;
161
162       case MC_MESSAGE_IGNORE_MEMORY:
163         {
164           XBT_DEBUG("Received ignored memory");
165           if (size != sizeof(s_mc_ignore_memory_message_t))
166             xbt_die("Broken messsage");
167           s_mc_ignore_memory_message_t message;
168           memcpy(&message, buffer, sizeof(message));
169           MC_process_ignore_memory(&mc_model_checker->process,
170             message.addr, message.size);
171         }
172         break;
173
174       default:
175         xbt_die("Unexpected message from model-checked application");
176
177       }
178       return;
179     }
180     if (socket_pollfd->revents & POLLERR) {
181       throw_socket_error(socket_pollfd->fd);
182     }
183     if (socket_pollfd->revents & POLLHUP)
184       xbt_die("Socket hang up?");
185   }
186
187   if (signalfd_pollfd->revents) {
188     if (signalfd_pollfd->revents & POLLIN) {
189       this->handle_signals();
190       return;
191     }
192     if (signalfd_pollfd->revents & POLLERR) {
193       throw_socket_error(signalfd_pollfd->fd);
194     }
195     if (signalfd_pollfd->revents & POLLHUP)
196       xbt_die("Signalfd hang up?");
197   }
198 }
199
200 void s_mc_server::loop()
201 {
202   while (mc_model_checker->process.running)
203     this->handle_events();
204 }
205
206 void s_mc_server::handle_signals()
207 {
208   struct signalfd_siginfo info;
209   struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
210   while (1) {
211     ssize_t size = read(signalfd_pollfd->fd, &info, sizeof(info));
212     if (size == -1) {
213       if (errno == EINTR)
214         continue;
215       else
216         throw std::system_error(errno, std::system_category());
217     } else if (size != sizeof(info))
218         return throw std::runtime_error(
219           "Bad communication with model-checked application");
220     else
221       break;
222   }
223   this->on_signal(&info);
224 }
225
226 void s_mc_server::handle_waitpid()
227 {
228   XBT_DEBUG("Check for wait event");
229   int status;
230   pid_t pid;
231   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
232     if (pid == -1) {
233       if (errno == ECHILD) {
234         // No more children:
235         if (mc_model_checker->process.running)
236           xbt_die("Inconsistent state");
237         else
238           break;
239       } else {
240         XBT_ERROR("Could not wait for pid: %s", strerror(errno));
241         throw std::system_error(errno, std::system_category());
242       }
243     }
244
245     if (pid == mc_model_checker->process.pid) {
246       if (WIFEXITED(status) || WIFSIGNALED(status)) {
247         XBT_DEBUG("Child process is over");
248         mc_model_checker->process.status = status;
249         mc_model_checker->process.running = false;
250       }
251     }
252   }
253 }
254
255 void s_mc_server::on_signal(const struct signalfd_siginfo* info)
256 {
257   switch(info->ssi_signo) {
258   case SIGCHLD:
259     this->handle_waitpid();
260     break;
261   default:
262     break;
263   }
264 }