2 * Copyright (C) 2004-2010 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>
19 #include <sys/select.h>
22 #include "gcc-compat.h"
29 static int signal_pipe[2];
32 * Initialize the signal subsystem.
34 * This function creates a pipe, the signal pipe, to deliver pending signals to
35 * the application (Bernstein's trick). It should be called during the
36 * application's startup part, followed by subsequent calls to
37 * install_sighandler() for each signal that should be caught.
39 * signal_init() installs a generic signal handler which is used for all
40 * signals simultaneously. When a signal arrives, this generic signal handler
41 * writes the corresponding signal number to the signal pipe so that the
42 * application can test for pending signals simply by checking the signal pipe
43 * for reading, e.g. by using the select(2) system call.
45 * \return This function either succeeds or calls exit(2) to terminate
46 * the current process. On success, the file descriptor of the signal pipe is
52 if (pipe(signal_pipe) < 0) {
53 ret = -ERRNO_TO_DSS_ERROR(errno);
56 ret = mark_fd_nonblocking(signal_pipe[0]);
59 ret = mark_fd_nonblocking(signal_pipe[1]);
62 return signal_pipe[0];
64 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
69 * just write one integer to signal pipe
71 static void generic_signal_handler(int s)
73 write(signal_pipe[1], &s, sizeof(int));
79 * \param pid In case a child died, its pid is returned here.
81 * Call waitpid() and print a log message containing the pid and the cause of
84 * \return A (negative) error code on errors, zero, if no child died, one
85 * otherwise. If and only if the function returns one, the content of \a pid is
90 int reap_child(pid_t *pid, int *status)
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)
112 * \param sig The number of the signal to catch.
114 * This installs the generic signal handler for the given signal.
116 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
119 int install_sighandler(int sig)
121 DSS_DEBUG_LOG(("catching signal %d\n", sig));
122 if (signal(sig, &generic_signal_handler) != SIG_ERR)
124 return -E_SIGNAL_SIG_ERR;
128 * Return number of next pending signal.
130 * This should be called if the fd for the signal pipe is ready for reading.
132 * \return On success, the number of the received signal is returned.
133 * If the read was interrupted by another signal the function returns 0.
134 * Otherwise a negative error code is returned.
136 int next_signal(void)
141 r = read(signal_pipe[0], &s, sizeof(s));
142 if (r == sizeof(s)) {
143 DSS_DEBUG_LOG(("next signal: %d\n", s));
150 DSS_ERROR_LOG(("failed to read from signal pipe\n"));
151 return -ERRNO_TO_DSS_ERROR(err);
155 * Close the signal pipe.
157 void signal_shutdown(void)
159 close(signal_pipe[1]);