1 /* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2 /** \file signal.c Signal handling functions. */
16 static int signal_pipe[2];
19 * Initialize the paraslash signal subsystem.
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.
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
32 * \return This function either succeeds or calls exit(3) to terminate the
33 * current process. On success, a signal task structure is returned.
35 struct signal_task *signal_init_or_die(void)
37 struct signal_task *st;
40 PARA_NOTICE_LOG("setting up signal handling\n");
41 if (pipe(signal_pipe) < 0) {
42 ret = -ERRNO_TO_PARA_ERROR(errno);
45 ret = mark_fd_nonblocking(signal_pipe[0]);
48 ret = mark_fd_nonblocking(signal_pipe[1]);
51 st = zalloc(sizeof(*st));
52 st->fd = signal_pipe[0];
55 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
59 /* Write the signal number to signal pipe. */
60 static void generic_signal_handler(int s)
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.
68 int save_errno = errno;
69 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
71 if (ret == sizeof(int)) {
76 * This is a fatal error which should never happen. We must not call
77 * PARA_XXX_LOG() here because this might acquire the log mutex which
78 * is already taken by the main program if the interrupt occurs while a
79 * log message is being printed. The mutex will not be released as long
80 * as this signal handler is running, so a deadlock ensues.
88 * \param pid In case a child died, its pid is returned here.
90 * Call waitpid() and print a log message containing the pid and the cause of
93 * \return A (negative) paraslash error code on errors, zero, if no child died,
94 * one otherwise. If and only if the function returns one, the content of \a
99 int para_reap_child(pid_t *pid)
102 *pid = waitpid(-1, &status, WNOHANG);
107 return -ERRNO_TO_PARA_ERROR(errno);
108 if (WIFEXITED(status))
109 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
110 WEXITSTATUS(status));
111 else if (WIFSIGNALED(status))
112 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
115 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
120 * Install the given handler for the given signal.
122 * \param sig The number of the signal to catch.
123 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
125 * This either succeeds or calls exit(EXIT_FAILURE).
129 void para_sigaction(int sig, void (*handler)(int))
131 struct sigaction act;
133 PARA_DEBUG_LOG("catching signal %d\n", sig);
134 act.sa_handler = handler;
135 sigemptyset(&act.sa_mask);
137 if (sig == SIGALRM) {
138 #ifdef SA_INTERRUPT /* SunOS */
139 act.sa_flags |= SA_INTERRUPT;
142 #ifdef SA_RESTART /* BSD */
143 act.sa_flags |= SA_RESTART;
146 if (sigaction(sig, &act, NULL) >= 0)
148 PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
154 * Install the generic signal handler for the given signal number.
156 * \param sig The number of the signal to catch.
158 * \sa signal(2), sigaction(2).
160 void para_install_sighandler(int sig)
162 para_sigaction(sig, &generic_signal_handler);
166 * Block a signal for the caller.
168 * \param sig The signal to block.
170 * This sets the given signal in the current signal mask of the calling process
171 * to prevent this signal from delivery.
173 * \sa \ref para_unblock_signal(), sigprocmask(2), sigaddset(3).
175 void para_block_signal(int sig)
179 PARA_DEBUG_LOG("blocking signal %d\n", sig);
181 sigaddset(&set, sig);
182 sigprocmask(SIG_BLOCK, &set, NULL);
188 * \param sig The signal to unblock.
190 * This function removes the given signal from the current set of blocked
193 * \sa \ref para_block_signal(), sigprocmask(2), sigaddset(3).
195 void para_unblock_signal(int sig)
199 PARA_DEBUG_LOG("unblocking signal %d\n", sig);
201 sigaddset(&set, sig);
202 sigprocmask(SIG_UNBLOCK, &set, NULL);
206 * Return the number of the next pending signal.
208 * \return On success, the number of the received signal is returned. If there
209 * is no signal currently pending, the function returns zero. On read errors
210 * from the signal pipe, the process is terminated.
212 int para_next_signal(void)
215 int s, ret = read_nonblock(signal_pipe[0], &s, sizeof(s), &n);
218 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
223 assert(n == sizeof(s));
224 PARA_DEBUG_LOG("next signal: %d\n", s);
229 * Close the write end of the signal pipe, deallocate resources.
231 * \param st The pointer obtained earlier from signal_init_or_die().
233 void signal_shutdown(struct signal_task *st)
235 close(signal_pipe[1]);