2 * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file signal.c Signal handling functions. */
15 static int signal_pipe
[2];
18 * Initialize the paraslash signal subsystem.
20 * This function creates a pipe, the signal pipe, to deliver pending
21 * signals to the application (Bernstein's trick). It should be called
22 * during the application's startup part, followed by subsequent calls
23 * to para_install_sighandler() for each signal that should be caught.
25 * para_signal_init() installs a generic signal handler which is used for all
26 * signals simultaneously. When a signal arrives, this generic signal handler
27 * writes the corresponding signal number to the signal pipe so that the
28 * application can test for pending signals simply by checking the signal pipe
29 * for reading, e.g. by using the select(2) system call.
31 * \return This function either succeeds or calls exit(2) to terminate
32 * the current process. On success, the file descriptor of the signal pipe is
35 int para_signal_init(void)
38 if (pipe(signal_pipe
) < 0) {
39 ret
= -ERRNO_TO_PARA_ERROR(errno
);
42 ret
= mark_fd_nonblocking(signal_pipe
[0]);
45 ret
= mark_fd_nonblocking(signal_pipe
[1]);
48 return signal_pipe
[0];
50 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
55 * just write one integer to signal pipe
57 static void generic_signal_handler(int s
)
59 ssize_t ret
= write(signal_pipe
[1], &s
, sizeof(int));
61 if (ret
== sizeof(int))
64 PARA_EMERG_LOG("%s\n", strerror(errno
));
66 PARA_EMERG_LOG("short write to signal pipe\n");
73 * \param pid In case a child died, its pid is returned here.
75 * Call waitpid() and print a log message containing the pid and the cause of
78 * \return A (negative) paraslash error code on errors, zero, if no child died,
79 * one otherwise. If and only if the function returns one, the content of \a
84 int para_reap_child(pid_t
*pid
)
87 *pid
= waitpid(-1, &status
, WNOHANG
);
92 return -ERRNO_TO_PARA_ERROR(errno
);
93 if (WIFEXITED(status
))
94 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid
,
96 else if (WIFSIGNALED(status
))
97 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid
,
100 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid
);
105 * Install the given handler for the given signal.
107 * \param sig The number of the signal to catch.
108 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
110 * This either succeeds or calls exit(EXIT_FAILURE).
114 void para_sigaction(int sig
, void (*handler
)(int))
116 struct sigaction act
;
118 PARA_DEBUG_LOG("catching signal %d\n", sig
);
119 act
.sa_handler
= handler
;
120 sigemptyset(&act
.sa_mask
);
122 if (sig
== SIGALRM
) {
123 #ifdef SA_INTERRUPT /* SunOS */
124 act
.sa_flags
|= SA_INTERRUPT
;
127 #ifdef SA_RESTART /* BSD */
128 act
.sa_flags
|= SA_RESTART
;
131 if (sigaction(sig
, &act
, NULL
) >= 0)
133 PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
139 * Install the generic signal handler for the given signal number.
141 * \param sig The number of the signal to catch.
143 * \sa signal(2), sigaction(2).
145 void para_install_sighandler(int sig
)
147 para_sigaction(sig
, &generic_signal_handler
);
151 * Return the number of the next pending signal.
153 * \param rfds The fd_set containing the signal pipe.
155 * \return On success, the number of the received signal is returned. If there
156 * is no signal currently pending, the function returns zero. On read errors
157 * from the signal pipe, the process is terminated.
159 int para_next_signal(fd_set
*rfds
)
162 int s
, ret
= read_nonblock(signal_pipe
[0], &s
, sizeof(s
), rfds
, &n
);
165 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
170 assert(n
== sizeof(s
));
171 PARA_DEBUG_LOG("next signal: %d\n", s
);
176 * Close the write end of the signal pipe.
178 void para_signal_shutdown(void)
180 close(signal_pipe
[1]);