2 * Copyright (C) 2004-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file signal.c Signal handling functions. */
16 static int signal_pipe[2];
19 * Initialize the paraslash signal subsystem.
21 * This function creates a pipe, the signal pipe, to deliver pending
22 * signals to the application (Bernstein's trick). It should be called
23 * during the application's startup part, followed by subsequent calls
24 * to para_install_sighandler() for each signal that should be caught.
26 * para_signal_init() installs a generic signal handler which is used for all
27 * signals simultaneously. When a signal arrives, this generic signal handler
28 * writes the corresponding signal number to the signal pipe so that the
29 * application can test for pending signals simply by checking the signal pipe
30 * for reading, e.g. by using the select(2) system call.
32 * \return This function either succeeds or calls exit(2) to terminate
33 * the current process. On success, the file descriptor of the signal pipe is
36 int para_signal_init(void)
39 if (pipe(signal_pipe) < 0) {
40 ret = -ERRNO_TO_PARA_ERROR(errno);
43 ret = mark_fd_nonblocking(signal_pipe[0]);
46 ret = mark_fd_nonblocking(signal_pipe[1]);
49 return signal_pipe[0];
51 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
56 * just write one integer to signal pipe
58 static void generic_signal_handler(int s)
60 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
62 if (ret == sizeof(int))
65 PARA_EMERG_LOG("%s\n", strerror(errno));
67 PARA_EMERG_LOG("short write to signal pipe\n");
74 * \param pid In case a child died, its pid is returned here.
76 * Call waitpid() and print a log message containing the pid and the cause of
79 * \return A (negative) paraslash error code on errors, zero, if no child died,
80 * one otherwise. If and only if the function returns one, the content of \a
85 int para_reap_child(pid_t *pid)
88 *pid = waitpid(-1, &status, WNOHANG);
93 return -ERRNO_TO_PARA_ERROR(errno);
94 if (WIFEXITED(status))
95 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
97 else if (WIFSIGNALED(status))
98 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
101 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
106 * Paraslash's zombie killer.
108 * It just calls \p para_reap_child() until there are no more children left to
111 void para_reap_children(void)
115 while (para_reap_child(&pid) > 0)
120 * Install the given handler for the given signal.
122 * \param sig The number of the signal to catch.
123 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
125 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on
130 int para_sigaction(int sig, void (*handler)(int))
132 struct sigaction act;
134 PARA_DEBUG_LOG("catching signal %d\n", sig);
135 act.sa_handler = handler;
136 sigemptyset(&act.sa_mask);
138 if (sig == SIGALRM) {
139 #ifdef SA_INTERRUPT /* SunOS */
140 act.sa_flags |= SA_INTERRUPT;
143 #ifdef SA_RESTART /* BSD */
144 act.sa_flags |= SA_RESTART;
147 if (sigaction(sig, &act, NULL) < 0)
148 return -E_SIGNAL_SIG_ERR;
153 * Install the generic signal handler for the given signal number.
155 * \param sig The number of the signal to catch.
157 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on
160 * \sa signal(2), sigaction(2).
162 int para_install_sighandler(int sig)
164 return para_sigaction(sig, &generic_signal_handler);
168 * Return the number of next pending signal.
170 * This should be called if the fd for the signal pipe is ready for reading.
172 * \return On success, the number of the received signal is returned. If the
173 * read returned zero or was interrupted by another signal the function returns
174 * 0. Otherwise, a negative error value is returned.
176 int para_next_signal(void)
179 ssize_t r = read(signal_pipe[0], &s, sizeof(s));
182 PARA_CRIT_LOG("read from signal pipe returned zero\n");
186 if (errno == EAGAIN || errno == EINTR)
188 return -ERRNO_TO_PARA_ERROR(errno);
190 assert(r == sizeof(s));
191 PARA_DEBUG_LOG("next signal: %d\n", s);
196 * Close the write end of the signal pipe.
198 void para_signal_shutdown(void)
200 close(signal_pipe[1]);