2 * Copyright (C) 2004-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file signal.c Signal handling functions. */
18 static int signal_pipe[2];
21 * Initialize the paraslash signal subsystem.
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.
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).
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.
38 int para_signal_init(void)
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 return signal_pipe[0];
53 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
57 /* Write the signal number to signal pipe. */
58 static void generic_signal_handler(int s)
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.
66 int save_errno = errno;
67 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
69 if (ret == sizeof(int)) {
74 PARA_EMERG_LOG("%s\n", strerror(errno));
76 PARA_EMERG_LOG("short write to signal pipe\n");
83 * \param pid In case a child died, its pid is returned here.
85 * Call waitpid() and print a log message containing the pid and the cause of
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
94 int para_reap_child(pid_t *pid)
97 *pid = waitpid(-1, &status, WNOHANG);
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,
110 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
115 * Install the given handler for the given signal.
117 * \param sig The number of the signal to catch.
118 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
120 * This either succeeds or calls exit(EXIT_FAILURE).
124 void para_sigaction(int sig, void (*handler)(int))
126 struct sigaction act;
128 PARA_DEBUG_LOG("catching signal %d\n", sig);
129 act.sa_handler = handler;
130 sigemptyset(&act.sa_mask);
132 if (sig == SIGALRM) {
133 #ifdef SA_INTERRUPT /* SunOS */
134 act.sa_flags |= SA_INTERRUPT;
137 #ifdef SA_RESTART /* BSD */
138 act.sa_flags |= SA_RESTART;
141 if (sigaction(sig, &act, NULL) >= 0)
143 PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
149 * Install the generic signal handler for the given signal number.
151 * \param sig The number of the signal to catch.
153 * \sa signal(2), sigaction(2).
155 void para_install_sighandler(int sig)
157 para_sigaction(sig, &generic_signal_handler);
161 * Block a signal for the caller.
163 * \param sig The signal to block.
165 * This sets the given signal in the current signal mask of the calling process
166 * to prevent this signal from delivery.
168 * \sa \ref para_unblock_signal(), sigprocmask(2), sigaddset(3).
170 void para_block_signal(int sig)
174 PARA_DEBUG_LOG("blocking signal %d\n", sig);
176 sigaddset(&set, sig);
177 sigprocmask(SIG_BLOCK, &set, NULL);
183 * \param sig The signal to unblock.
185 * This function removes the given signal from the current set of blocked
188 * \sa \ref para_block_signal(), sigprocmask(2), sigaddset(3).
190 void para_unblock_signal(int sig)
194 PARA_DEBUG_LOG("unblocking signal %d\n", sig);
196 sigaddset(&set, sig);
197 sigprocmask(SIG_UNBLOCK, &set, NULL);
201 * Return the number of the next pending signal.
203 * \param rfds The fd_set containing the signal pipe.
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.
209 int para_next_signal(fd_set *rfds)
212 int s, ret = read_nonblock(signal_pipe[0], &s, sizeof(s), rfds, &n);
215 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
220 assert(n == sizeof(s));
221 PARA_DEBUG_LOG("next signal: %d\n", s);
226 * Close the write end of the signal pipe.
228 void para_signal_shutdown(void)
230 close(signal_pipe[1]);