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. */
8 #include <system_error>
11 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/signalfd.h>
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"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_server, mc, "MC server logic");
26 // HArdcoded index for now:
27 #define SOCKET_FD_INDEX 0
28 #define SIGNAL_FD_INDEX 1
30 mc_server_t mc_server;
32 s_mc_server::s_mc_server(pid_t pid, int socket)
35 this->socket = socket;
38 void s_mc_server::start()
40 /* Wait for the target process to initialize and exchange a HELLO messages
41 * before trying to look at its memory map.
43 XBT_DEBUG("Greeting the MC client");
44 int res = MC_protocol_hello(socket);
46 throw std::system_error(res, std::system_category());
47 XBT_DEBUG("Greeted the MC client");
49 // Block SIGCHLD (this will be handled with accept/signalfd):
52 sigaddset(&set, SIGCHLD);
53 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
54 throw std::system_error(errno, std::system_category());
57 sigfillset(&full_set);
59 // Prepare data for poll:
61 struct pollfd* socket_pollfd = &fds[SOCKET_FD_INDEX];
62 socket_pollfd->fd = socket;
63 socket_pollfd->events = POLLIN;
64 socket_pollfd->revents = 0;
66 int signal_fd = signalfd(-1, &set, 0);
68 throw std::system_error(errno, std::system_category());
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;
76 void s_mc_server::shutdown()
78 XBT_DEBUG("Shuting down model-checker");
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;
92 void s_mc_server::exit()
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));
104 xbt_die("Unexpected status from model-checked process");
108 void s_mc_server::resume(mc_process_t process)
110 int socket = process->socket;
111 int res = MC_protocol_send_simple_message(socket, MC_MESSAGE_CONTINUE);
113 throw std::system_error(res, std::system_category());
117 void throw_socket_error(int fd)
120 socklen_t errlen = sizeof(error);
121 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == -1)
123 throw std::system_error(error, std::system_category());
126 void s_mc_server::handle_events()
128 char buffer[MC_MESSAGE_LENGTH];
129 struct pollfd* socket_pollfd = &fds[SOCKET_FD_INDEX];
130 struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
132 while(poll(fds, 2, -1) == -1) {
137 throw std::system_error(errno, std::system_category());
141 if (socket_pollfd->revents) {
142 if (socket_pollfd->revents & POLLIN) {
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());
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));
153 switch(base_message.type) {
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
162 case MC_MESSAGE_IGNORE_MEMORY:
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);
175 xbt_die("Unexpected message from model-checked application");
180 if (socket_pollfd->revents & POLLERR) {
181 throw_socket_error(socket_pollfd->fd);
183 if (socket_pollfd->revents & POLLHUP)
184 xbt_die("Socket hang up?");
187 if (signalfd_pollfd->revents) {
188 if (signalfd_pollfd->revents & POLLIN) {
189 this->handle_signals();
192 if (signalfd_pollfd->revents & POLLERR) {
193 throw_socket_error(signalfd_pollfd->fd);
195 if (signalfd_pollfd->revents & POLLHUP)
196 xbt_die("Signalfd hang up?");
200 void s_mc_server::loop()
202 while (mc_model_checker->process.running)
203 this->handle_events();
206 void s_mc_server::handle_signals()
208 struct signalfd_siginfo info;
209 struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
211 ssize_t size = read(signalfd_pollfd->fd, &info, sizeof(info));
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");
223 this->on_signal(&info);
226 void s_mc_server::handle_waitpid()
228 XBT_DEBUG("Check for wait event");
231 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
233 if (errno == ECHILD) {
235 if (mc_model_checker->process.running)
236 xbt_die("Inconsistent state");
240 XBT_ERROR("Could not wait for pid: %s", strerror(errno));
241 throw std::system_error(errno, std::system_category());
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;
255 void s_mc_server::on_signal(const struct signalfd_siginfo* info)
257 switch(info->ssi_signo) {
259 this->handle_waitpid();