2 * Copyright (C) 2004-2010 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
10 #include <sys/types.h>
18 #include <sys/select.h>
21 #include "gcc-compat.h"
28 static int signal_pipe[2];
31 * Initialize the signal subsystem.
33 * This function creates a pipe, the signal pipe, to deliver pending signals to
34 * the application (Bernstein's trick). It should be called during the
35 * application's startup part, followed by subsequent calls to
36 * install_sighandler() for each signal that should be caught.
38 * signal_init() installs a generic signal handler which is used for all
39 * signals simultaneously. When a signal arrives, this generic signal handler
40 * writes the corresponding signal number to the signal pipe so that the
41 * application can test for pending signals simply by checking the signal pipe
42 * for reading, e.g. by using the select(2) system call.
44 * \return This function either succeeds or calls exit(2) to terminate
45 * the current process. On success, the file descriptor of the signal pipe is
51 if (pipe(signal_pipe) < 0) {
52 ret = -ERRNO_TO_DSS_ERROR(errno);
55 ret = mark_fd_nonblocking(signal_pipe[0]);
58 ret = mark_fd_nonblocking(signal_pipe[1]);
61 return signal_pipe[0];
63 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
67 /* Write the signal number to the signal pipe, abort on errors. */
68 static void generic_signal_handler(int s)
71 * Signal handlers that make system calls must save a copy of errno on
72 * entry to the handler and restore it on exit, to prevent the
73 * possibility of overwriting a errno value that had previously been
74 * set in the main program.
76 int save_errno = errno;
77 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
79 if (ret == sizeof(int)) {
84 DSS_EMERG_LOG(("%s\n", strerror(errno)));
86 DSS_EMERG_LOG(("short write to signal pipe\n"));
93 * \param pid In case a child died, its pid is returned here.
95 * Call waitpid() and print a log message containing the pid and the cause of
98 * \return A (negative) error code on errors, zero, if no child died, one
99 * otherwise. If and only if the function returns one, the content of \a pid is
104 int reap_child(pid_t *pid, int *status)
106 *pid = waitpid(-1, status, WNOHANG);
111 return -ERRNO_TO_DSS_ERROR(errno);
112 if (WIFEXITED(*status))
113 DSS_DEBUG_LOG(("child %i exited. Exit status: %i\n", (int)*pid,
114 WEXITSTATUS(*status)));
115 else if (WIFSIGNALED(*status))
116 DSS_DEBUG_LOG(("child %i was killed by signal %i\n", (int)*pid,
119 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)*pid));
124 * Wrapper around signal(2)
126 * \param sig The number of the signal to catch.
128 * This installs the generic signal handler for the given signal.
130 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
133 int install_sighandler(int sig)
135 DSS_DEBUG_LOG(("catching signal %d\n", sig));
136 if (signal(sig, &generic_signal_handler) != SIG_ERR)
138 return -E_SIGNAL_SIG_ERR;
142 * Return number of next pending signal.
144 * This should be called if the fd for the signal pipe is ready for reading.
146 * \return On success, the number of the received signal is returned.
147 * If the read was interrupted by another signal the function returns 0.
148 * Otherwise a negative error code is returned.
150 int next_signal(void)
155 r = read(signal_pipe[0], &s, sizeof(s));
156 if (r == sizeof(s)) {
157 DSS_DEBUG_LOG(("next signal: %d\n", s));
164 DSS_ERROR_LOG(("failed to read from signal pipe\n"));
165 return -ERRNO_TO_DSS_ERROR(err);
169 * Close the signal pipe.
171 void signal_shutdown(void)
173 close(signal_pipe[1]);