doc: Change email address to maan@tuebingen.mpg.de
[paraslash.git] / signal.c
1 /*
2 * Copyright (C) 2004-2014 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file signal.c Signal handling functions. */
7
8 #include <signal.h>
9 #include <sys/types.h>
10
11 #include "para.h"
12 #include "error.h"
13 #include "fd.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "signal.h"
17
18 static int signal_pipe[2];
19
20 /**
21 * Initialize the paraslash signal subsystem.
22 *
23 * This function creates a pipe, the signal pipe, to deliver pending
24 * signals to the application (Bernstein's trick). It should be called
25 * during the application's startup part, followed by subsequent calls
26 * to para_install_sighandler() for each signal that should be caught.
27 *
28 * A generic signal handler is used for all signals simultaneously. When a
29 * signal arrives, the signal handler writes the number of the signal received
30 * to one end of the signal pipe. The application can test for pending signals
31 * by checking if the file descriptor of the other end of the signal pipe is
32 * ready for reading, see select(2).
33 *
34 * \return This function either succeeds or calls exit(2) to terminate the
35 * current process. On success, the file descriptor of the read end of the
36 * signal pipe is returned.
37 */
38 int para_signal_init(void)
39 {
40 int ret;
41 if (pipe(signal_pipe) < 0) {
42 ret = -ERRNO_TO_PARA_ERROR(errno);
43 goto err_out;
44 }
45 ret = mark_fd_nonblocking(signal_pipe[0]);
46 if (ret < 0)
47 goto err_out;
48 ret = mark_fd_nonblocking(signal_pipe[1]);
49 if (ret < 0)
50 goto err_out;
51 return signal_pipe[0];
52 err_out:
53 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
54 exit(EXIT_FAILURE);
55 }
56
57 /* Write the signal number to signal pipe. */
58 static void generic_signal_handler(int s)
59 {
60 /*
61 * Signal handlers that make system calls must save a copy of errno on
62 * entry to the handler and restore it on exit, to prevent the
63 * possibility of overwriting a errno value that had previously been
64 * set in the main program.
65 */
66 int save_errno = errno;
67 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
68
69 if (ret == sizeof(int)) {
70 errno = save_errno;
71 return;
72 }
73 if (ret < 0)
74 PARA_EMERG_LOG("%s\n", strerror(errno));
75 else
76 PARA_EMERG_LOG("short write to signal pipe\n");
77 exit(EXIT_FAILURE);
78 }
79
80 /**
81 * Reap one child.
82 *
83 * \param pid In case a child died, its pid is returned here.
84 *
85 * Call waitpid() and print a log message containing the pid and the cause of
86 * the child's death.
87 *
88 * \return A (negative) paraslash error code on errors, zero, if no child died,
89 * one otherwise. If and only if the function returns one, the content of \a
90 * pid is meaningful.
91 *
92 * \sa waitpid(2).
93 */
94 int para_reap_child(pid_t *pid)
95 {
96 int status;
97 *pid = waitpid(-1, &status, WNOHANG);
98
99 if (!*pid)
100 return 0;
101 if (*pid < 0)
102 return -ERRNO_TO_PARA_ERROR(errno);
103 if (WIFEXITED(status))
104 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
105 WEXITSTATUS(status));
106 else if (WIFSIGNALED(status))
107 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
108 WTERMSIG(status));
109 else
110 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
111 return 1;
112 }
113
114 /**
115 * Install the given handler for the given signal.
116 *
117 * \param sig The number of the signal to catch.
118 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
119 *
120 * This either succeeds or calls exit(EXIT_FAILURE).
121 *
122 * \sa sigaction(2).
123 */
124 void para_sigaction(int sig, void (*handler)(int))
125 {
126 struct sigaction act;
127
128 PARA_DEBUG_LOG("catching signal %d\n", sig);
129 act.sa_handler = handler;
130 sigemptyset(&act.sa_mask);
131 act.sa_flags = 0;
132 if (sig == SIGALRM) {
133 #ifdef SA_INTERRUPT /* SunOS */
134 act.sa_flags |= SA_INTERRUPT;
135 #endif
136 } else {
137 #ifdef SA_RESTART /* BSD */
138 act.sa_flags |= SA_RESTART;
139 #endif
140 }
141 if (sigaction(sig, &act, NULL) >= 0)
142 return;
143 PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
144 sig);
145 exit(EXIT_FAILURE);
146 }
147
148 /**
149 * Install the generic signal handler for the given signal number.
150 *
151 * \param sig The number of the signal to catch.
152 *
153 * \sa signal(2), sigaction(2).
154 */
155 void para_install_sighandler(int sig)
156 {
157 para_sigaction(sig, &generic_signal_handler);
158 }
159
160 /**
161 * Block a signal for the caller.
162 *
163 * \param sig The signal to block.
164 *
165 * This sets the given signal in the current signal mask of the calling process
166 * to prevent this signal from delivery.
167 *
168 * \sa \ref para_unblock_signal(), sigprocmask(2), sigaddset(3).
169 */
170 void para_block_signal(int sig)
171 {
172 sigset_t set;
173
174 PARA_DEBUG_LOG("blocking signal %d\n", sig);
175 sigemptyset(&set);
176 sigaddset(&set, sig);
177 sigprocmask(SIG_BLOCK, &set, NULL);
178 }
179
180 /**
181 * Unblock a signal.
182 *
183 * \param sig The signal to unblock.
184 *
185 * This function removes the given signal from the current set of blocked
186 * signals.
187 *
188 * \sa \ref para_block_signal(), sigprocmask(2), sigaddset(3).
189 */
190 void para_unblock_signal(int sig)
191 {
192 sigset_t set;
193
194 PARA_DEBUG_LOG("unblocking signal %d\n", sig);
195 sigemptyset(&set);
196 sigaddset(&set, sig);
197 sigprocmask(SIG_UNBLOCK, &set, NULL);
198 }
199
200 /**
201 * Return the number of the next pending signal.
202 *
203 * \param rfds The fd_set containing the signal pipe.
204 *
205 * \return On success, the number of the received signal is returned. If there
206 * is no signal currently pending, the function returns zero. On read errors
207 * from the signal pipe, the process is terminated.
208 */
209 int para_next_signal(fd_set *rfds)
210 {
211 size_t n;
212 int s, ret = read_nonblock(signal_pipe[0], &s, sizeof(s), rfds, &n);
213
214 if (ret < 0) {
215 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
216 exit(EXIT_FAILURE);
217 }
218 if (n == 0)
219 return 0;
220 assert(n == sizeof(s));
221 PARA_DEBUG_LOG("next signal: %d\n", s);
222 return s;
223 }
224
225 /**
226 * Close the write end of the signal pipe.
227 */
228 void para_signal_shutdown(void)
229 {
230 close(signal_pipe[1]);
231 }