2 * Copyright (C) 2004-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file signal.c Signal handling functions. */
15 static int signal_pipe[2];
18 * Initialize the paraslash signal subsystem.
20 * This function creates a pipe, the signal pipe, to deliver pending
21 * signals to the application (Bernstein's trick). It should be called
22 * during the application's startup part, followed by subsequent calls
23 * to para_install_sighandler() for each signal that should be caught.
25 * A generic signal handler is used for all signals simultaneously. When a
26 * signal arrives, the signal handler writes the number of the signal received
27 * to one end of the signal pipe. The application can test for pending signals
28 * by checking if the file descriptor of the other end of the signal pipe is
29 * ready for reading, see select(2).
31 * \return This function either succeeds or calls exit(2) to terminate the
32 * current process. On success, the file descriptor of the read end of the
33 * signal pipe is returned.
35 int para_signal_init(void)
38 if (pipe(signal_pipe) < 0) {
39 ret = -ERRNO_TO_PARA_ERROR(errno);
42 ret = mark_fd_nonblocking(signal_pipe[0]);
45 ret = mark_fd_nonblocking(signal_pipe[1]);
48 return signal_pipe[0];
50 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
54 /* Write the signal number to signal pipe. */
55 static void generic_signal_handler(int s)
58 * Signal handlers that make system calls must save a copy of errno on
59 * entry to the handler and restore it on exit, to prevent the
60 * possibility of overwriting a errno value that had previously been
61 * set in the main program.
63 int save_errno = errno;
64 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
66 if (ret == sizeof(int)) {
71 PARA_EMERG_LOG("%s\n", strerror(errno));
73 PARA_EMERG_LOG("short write to signal pipe\n");
80 * \param pid In case a child died, its pid is returned here.
82 * Call waitpid() and print a log message containing the pid and the cause of
85 * \return A (negative) paraslash error code on errors, zero, if no child died,
86 * one otherwise. If and only if the function returns one, the content of \a
91 int para_reap_child(pid_t *pid)
94 *pid = waitpid(-1, &status, WNOHANG);
99 return -ERRNO_TO_PARA_ERROR(errno);
100 if (WIFEXITED(status))
101 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
102 WEXITSTATUS(status));
103 else if (WIFSIGNALED(status))
104 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
107 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
112 * Install the given handler for the given signal.
114 * \param sig The number of the signal to catch.
115 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
117 * This either succeeds or calls exit(EXIT_FAILURE).
121 void para_sigaction(int sig, void (*handler)(int))
123 struct sigaction act;
125 PARA_DEBUG_LOG("catching signal %d\n", sig);
126 act.sa_handler = handler;
127 sigemptyset(&act.sa_mask);
129 if (sig == SIGALRM) {
130 #ifdef SA_INTERRUPT /* SunOS */
131 act.sa_flags |= SA_INTERRUPT;
134 #ifdef SA_RESTART /* BSD */
135 act.sa_flags |= SA_RESTART;
138 if (sigaction(sig, &act, NULL) >= 0)
140 PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
146 * Install the generic signal handler for the given signal number.
148 * \param sig The number of the signal to catch.
150 * \sa signal(2), sigaction(2).
152 void para_install_sighandler(int sig)
154 para_sigaction(sig, &generic_signal_handler);
158 * Block a signal for the caller.
160 * \param sig The signal to block.
162 * This sets the given signal in the current signal mask of the calling process
163 * to prevent this signal from delivery.
165 * \sa \ref para_unblock_signal(), sigprocmask(2), sigaddset(3).
167 void para_block_signal(int sig)
171 PARA_DEBUG_LOG("blocking signal %d\n", sig);
173 sigaddset(&set, sig);
174 sigprocmask(SIG_BLOCK, &set, NULL);
180 * \param sig The signal to unblock.
182 * This function removes the given signal from the current set of blocked
185 * \sa \ref para_block_signal(), sigprocmask(2), sigaddset(3).
187 void para_unblock_signal(int sig)
191 PARA_DEBUG_LOG("unblocking signal %d\n", sig);
193 sigaddset(&set, sig);
194 sigprocmask(SIG_UNBLOCK, &set, NULL);
198 * Return the number of the next pending signal.
200 * \param rfds The fd_set containing the signal pipe.
202 * \return On success, the number of the received signal is returned. If there
203 * is no signal currently pending, the function returns zero. On read errors
204 * from the signal pipe, the process is terminated.
206 int para_next_signal(fd_set *rfds)
209 int s, ret = read_nonblock(signal_pipe[0], &s, sizeof(s), rfds, &n);
212 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
217 assert(n == sizeof(s));
218 PARA_DEBUG_LOG("next signal: %d\n", s);
223 * Close the write end of the signal pipe.
225 void para_signal_shutdown(void)
227 close(signal_pipe[1]);