1db008ca89a92a23f3c08017a74be04e3e06f1d5
2 * Copyright (C) 2004-2006 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_nonblock(signal_pipe
[0]);
46 ret
= mark_fd_nonblock(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 write(signal_pipe
[1], &s
, sizeof(int));
61 //fprintf(stderr, "got sig %i, write returned %d\n", s, ret);
67 * call waitpid() and print a log message containing the pid
68 * and the cause of the child's death.
70 * \return Like \p waitpid(), this function returns the process ID of the
71 * terminated child; on error, \p -E_WAITPID is returned.
74 pid_t
para_reap_child(void)
77 pid_t pid
= waitpid(-1, &status
, WNOHANG
);
84 if (WIFEXITED(status
))
85 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", pid
,
87 else if (WIFSIGNALED(status
))
88 PARA_DEBUG_LOG("child %i was killed by signal %i\n", pid
,
91 PARA_WARNING_LOG("child %i terminated abormally\n", pid
);
96 * paraslash's zombie killer
98 * It just calls \p para_reap_child() until there are no more children left to
101 void para_reap_children(void)
103 while (para_reap_child() > 0)
108 * wrapper around signal(2)
109 * \param sig the number of the signal to catch
111 * This installs the generic signal handler for the given signal.
112 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
115 int para_install_sighandler(int sig
)
117 PARA_DEBUG_LOG("catching signal %d\n", sig
);
118 return signal(sig
, &generic_signal_handler
) == SIG_ERR
? -E_SIGNAL_SIG_ERR
: 1;
122 * return number of next pending signal
124 * This should be called if the fd for the signal pipe is ready for reading.
126 * \return On success, the number of the received signal is returned. \p
127 * -E_SIGNAL_READ is returned if a read error occurred while reading the signal
128 * pipe. If the read was interrupted by another signal the function returns 0.
130 int para_next_signal(void)
135 if ((r
= read(signal_pipe
[0], &s
, sizeof(s
)) == sizeof(s
)) > 0) {
136 PARA_DEBUG_LOG("next signal: %d\n", s
);
139 return r
< 0 && (errno
!= EAGAIN
)? 0 : -E_SIGNAL_READ
;