2 * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file signal.c Signal handling functions. */
11 #include <sys/types.h>
21 #include "gcc-compat.h"
27 static int signal_pipe[2];
30 * Initialize the signal subsystem.
32 * This function creates a pipe, the signal pipe, to deliver pending signals to
33 * the application (Bernstein's trick). It should be called during the
34 * application's startup part, followed by subsequent calls to
35 * install_sighandler() for each signal that should be caught.
37 * signal_init() installs a generic signal handler which is used for all
38 * signals simultaneously. When a signal arrives, this generic signal handler
39 * writes the corresponding signal number to the signal pipe so that the
40 * application can test for pending signals simply by checking the signal pipe
41 * for reading, e.g. by using the select(2) system call.
43 * \return This function either succeeds or calls exit(2) to terminate
44 * the current process. On success, the file descriptor of the signal pipe is
50 if (pipe(signal_pipe) < 0) {
51 ret = -ERRNO_TO_DSS_ERROR(errno);
54 ret = mark_fd_nonblocking(signal_pipe[0]);
57 ret = mark_fd_nonblocking(signal_pipe[1]);
60 return signal_pipe[0];
62 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
67 * just write one integer to signal pipe
69 static void generic_signal_handler(int s)
71 write(signal_pipe[1], &s, sizeof(int));
72 //fprintf(stderr, "got sig %i, write returned %d\n", s, ret);
78 * \param pid In case a child died, its pid is returned here.
80 * Call waitpid() and print a log message containing the pid and the cause of
83 * \return A (negative) error code on errors, zero, if no child died, one
84 * otherwise. If and only if the function returns one, the content of \a pid is
89 int reap_child(pid_t *pid)
92 *pid = waitpid(-1, &status, WNOHANG);
97 return -ERRNO_TO_DSS_ERROR(errno);
98 if (WIFEXITED(status))
99 DSS_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
100 WEXITSTATUS(status));
101 else if (WIFSIGNALED(status))
102 DSS_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
105 DSS_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
110 * wrapper around signal(2)
111 * \param sig the number of the signal to catch
113 * This installs the generic signal handler for the given signal.
114 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
117 int install_sighandler(int sig)
119 DSS_DEBUG_LOG("catching signal %d\n", sig);
120 return signal(sig, &generic_signal_handler) == SIG_ERR? -E_SIGNAL_SIG_ERR : 1;
124 * return number of next pending signal
126 * This should be called if the fd for the signal pipe is ready for reading.
128 * \return On success, the number of the received signal is returned. \p
129 * -E_SIGNAL_READ is returned if a read error occurred while reading the signal
130 * pipe. If the read was interrupted by another signal the function returns 0.
132 int next_signal(void)
137 r = read(signal_pipe[0], &s, sizeof(s));
138 if (r == sizeof(s)) {
139 DSS_DEBUG_LOG("next signal: %d\n", s);
142 return r < 0 && (errno != EAGAIN)? 0 : -ERRNO_TO_DSS_ERROR(errno);
146 * Close the signal pipe.
148 void signal_shutdown(void)
150 close(signal_pipe[1]);