signal: Switch from signal() to sigaction.
[paraslash.git] / signal.c
1 /*
2 * Copyright (C) 2004-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file signal.c Signal handling functions. */
7
8 #include <signal.h>
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "fd.h"
15
16 static int signal_pipe[2];
17
18 /**
19 * Initialize the paraslash signal subsystem.
20 *
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.
25 *
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.
31 *
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
34 * returned.
35 */
36 int para_signal_init(void)
37 {
38 int ret;
39 if (pipe(signal_pipe) < 0) {
40 ret = -ERRNO_TO_PARA_ERROR(errno);
41 goto err_out;
42 }
43 ret = mark_fd_nonblocking(signal_pipe[0]);
44 if (ret < 0)
45 goto err_out;
46 ret = mark_fd_nonblocking(signal_pipe[1]);
47 if (ret < 0)
48 goto err_out;
49 return signal_pipe[0];
50 err_out:
51 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
52 exit(EXIT_FAILURE);
53 }
54
55 /*
56 * just write one integer to signal pipe
57 */
58 static void generic_signal_handler(int s)
59 {
60 ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
61
62 if (ret == sizeof(int))
63 return;
64 if (ret < 0)
65 PARA_EMERG_LOG("%s\n", strerror(errno));
66 else
67 PARA_EMERG_LOG("short write to signal pipe\n");
68 exit(EXIT_FAILURE);
69 }
70
71 /**
72 * Reap one child.
73 *
74 * \param pid In case a child died, its pid is returned here.
75 *
76 * Call waitpid() and print a log message containing the pid and the cause of
77 * the child's death.
78 *
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
81 * pid is meaningful.
82 *
83 * \sa waitpid(2).
84 */
85 int para_reap_child(pid_t *pid)
86 {
87 int status;
88 *pid = waitpid(-1, &status, WNOHANG);
89
90 if (!*pid)
91 return 0;
92 if (*pid < 0)
93 return -ERRNO_TO_PARA_ERROR(errno);
94 if (WIFEXITED(status))
95 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
96 WEXITSTATUS(status));
97 else if (WIFSIGNALED(status))
98 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
99 WTERMSIG(status));
100 else
101 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
102 return 1;
103 }
104
105 /**
106 * Paraslash's zombie killer.
107 *
108 * It just calls \p para_reap_child() until there are no more children left to
109 * reap.
110 */
111 void para_reap_children(void)
112 {
113 pid_t pid;
114
115 while (para_reap_child(&pid) > 0)
116 ; /* nothing */
117 }
118
119 /**
120 * Install the generic signal handler for the given signal number.
121 *
122 * \param sig The number of the signal to catch.
123 *
124 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
125 *
126 * \sa signal(2), sigaction(2).
127 */
128 int para_install_sighandler(int sig)
129 {
130 struct sigaction act;
131
132 PARA_DEBUG_LOG("catching signal %d\n", sig);
133 act.sa_handler = &generic_signal_handler;
134 sigemptyset(&act.sa_mask);
135 act.sa_flags = 0;
136 if (sig == SIGALRM) {
137 #ifdef SA_INTERRUPT /* SunOS */
138 act.sa_flags |= SA_INTERRUPT;
139 #endif
140 } else {
141 #ifdef SA_RESTART /* BSD */
142 act.sa_flags |= SA_RESTART;
143 #endif
144 }
145 if (sigaction(sig, &act, NULL) < 0)
146 return -E_SIGNAL_SIG_ERR;
147 return 1;
148 }
149
150 /**
151 * Return the number of next pending signal.
152 *
153 * This should be called if the fd for the signal pipe is ready for reading.
154 *
155 * \return On success, the number of the received signal is returned. If the
156 * read returned zero or was interrupted by another signal the function returns
157 * 0. Otherwise, a negative error value is returned.
158 */
159 int para_next_signal(void)
160 {
161 int s;
162 ssize_t r = read(signal_pipe[0], &s, sizeof(s));
163
164 if (!r) {
165 PARA_CRIT_LOG("read from signal pipe returned zero\n");
166 return 0;
167 }
168 if (r < 0) {
169 if (errno == EAGAIN || errno == EINTR)
170 return 0;
171 return -ERRNO_TO_PARA_ERROR(errno);
172 }
173 assert(r == sizeof(s));
174 PARA_DEBUG_LOG("next signal: %d\n", s);
175 return s;
176 }
177
178 /**
179 * Close the write end of the signal pipe.
180 */
181 void para_signal_shutdown(void)
182 {
183 close(signal_pipe[1]);
184 }