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