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"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_server, mc, "MC server logic");
25 // HArdcoded index for now:
26 #define SOCKET_FD_INDEX 0
27 #define SIGNAL_FD_INDEX 1
29 mc_server_t mc_server;
31 s_mc_server::s_mc_server(pid_t pid, int socket)
34 this->socket = socket;
37 void s_mc_server::start()
39 /* Wait for the target process to initialize and exchange a HELLO messages
40 * before trying to look at its memory map.
42 XBT_DEBUG("Greeting the MC client");
43 int res = MC_protocol_hello(socket);
45 throw std::system_error(res, std::system_category());
46 XBT_DEBUG("Greeted the MC client");
48 MC_init_pid(pid, socket);
50 // Block SIGCHLD (this will be handled with accept/signalfd):
53 sigaddset(&set, SIGCHLD);
54 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
55 throw std::system_error(errno, std::system_category());
58 sigfillset(&full_set);
60 // Prepare data for poll:
62 struct pollfd* socket_pollfd = &fds[SOCKET_FD_INDEX];
63 socket_pollfd->fd = socket;
64 socket_pollfd->events = POLLIN;
65 socket_pollfd->revents = 0;
67 int signal_fd = signalfd(-1, &set, 0);
69 throw std::system_error(errno, std::system_category());
71 struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
72 signalfd_pollfd->fd = signal_fd;
73 signalfd_pollfd->events = POLLIN;
74 signalfd_pollfd->revents = 0;
77 void s_mc_server::shutdown()
79 XBT_DEBUG("Shuting down model-checker");
81 mc_process_t process = &mc_model_checker->process;
82 int status = process->status;
83 if (process->running) {
84 XBT_DEBUG("Killing process");
85 kill(process->pid, SIGTERM);
86 if (waitpid(process->pid, &status, 0) == -1)
87 throw std::system_error(errno, std::system_category());
88 // TODO, handle the case when the process does not want to die with a timeout
89 process->status = status;
93 void s_mc_server::exit()
96 int status = mc_model_checker->process.status;
97 if (WIFEXITED(status))
98 ::exit(WEXITSTATUS(status));
99 else if (WIFSIGNALED(status)) {
100 // Try to uplicate the signal of the model-checked process.
101 // This is a temporary hack so we don't try too hard.
102 kill(mc_model_checker->process.pid, WTERMSIG(status));
105 xbt_die("Unexpected status from model-checked process");
109 void s_mc_server::resume(mc_process_t process)
111 int socket = process->socket;
112 int res = MC_protocol_send_simple_message(socket, MC_MESSAGE_CONTINUE);
114 throw std::system_error(res, std::system_category());
118 void throw_socket_error(int fd)
121 socklen_t errlen = sizeof(error);
122 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == -1)
124 throw std::system_error(error, std::system_category());
127 void s_mc_server::handle_events()
129 s_mc_message_t message;
130 struct pollfd* socket_pollfd = &fds[SOCKET_FD_INDEX];
131 struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
133 while(poll(fds, 2, -1) == -1) {
138 throw std::system_error(errno, std::system_category());
142 if (socket_pollfd->revents) {
143 if (socket_pollfd->revents & POLLIN) {
144 ssize_t size = recv(socket_pollfd->fd, &message, sizeof(message), MSG_DONTWAIT);
145 if (size == -1 && errno != EAGAIN)
146 throw std::system_error(errno, std::system_category());
147 else switch(message.type) {
149 xbt_die("Unexpected message from model-checked application");
153 if (socket_pollfd->revents & POLLERR) {
154 throw_socket_error(socket_pollfd->fd);
156 if (socket_pollfd->revents & POLLHUP)
157 xbt_die("Socket hang up?");
160 if (signalfd_pollfd->revents) {
161 if (signalfd_pollfd->revents & POLLIN) {
162 this->handle_signals();
165 if (signalfd_pollfd->revents & POLLERR) {
166 throw_socket_error(signalfd_pollfd->fd);
168 if (signalfd_pollfd->revents & POLLHUP)
169 xbt_die("Signalfd hang up?");
173 void s_mc_server::loop()
175 while (mc_model_checker->process.running)
176 this->handle_events();
179 void s_mc_server::handle_signals()
181 struct signalfd_siginfo info;
182 struct pollfd* signalfd_pollfd = &fds[SIGNAL_FD_INDEX];
184 ssize_t size = read(signalfd_pollfd->fd, &info, sizeof(info));
189 throw std::system_error(errno, std::system_category());
190 } else if (size != sizeof(info))
191 return throw std::runtime_error(
192 "Bad communication with model-checked application");
196 this->on_signal(&info);
199 void s_mc_server::handle_waitpid()
201 XBT_DEBUG("Check for wait event");
204 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
206 if (errno == ECHILD) {
208 if (mc_model_checker->process.running)
209 xbt_die("Inconsistent state");
213 XBT_ERROR("Could not wait for pid: %s", strerror(errno));
214 throw std::system_error(errno, std::system_category());
218 if (pid == mc_model_checker->process.pid) {
219 if (WIFEXITED(status) || WIFSIGNALED(status)) {
220 XBT_DEBUG("Child process is over");
221 mc_model_checker->process.status = status;
222 mc_model_checker->process.running = false;
228 void s_mc_server::on_signal(const struct signalfd_siginfo* info)
230 switch(info->ssi_signo) {
232 this->handle_waitpid();