bf0cd5ccff28286852858e1b4f22bf00f9436f7b
[paraslash.git] / signal.c
1 /*
2 * Copyright (C) 2004-2006 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 = -E_SIGNAL_PIPE;
39 if (pipe(signal_pipe))
40 goto err_out;
41 ret = mark_fd_nonblock(signal_pipe[0]);
42 if (ret < 0)
43 goto err_out;
44 ret = mark_fd_nonblock(signal_pipe[1]);
45 if (ret < 0)
46 goto err_out;
47 return signal_pipe[0];
48 err_out:
49 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
50 exit(EXIT_FAILURE);
51 }
52
53 /*
54 * just write one integer to signal pipe
55 */
56 static void generic_signal_handler(int s)
57 {
58 write(signal_pipe[1], &s, sizeof(int));
59 //fprintf(stderr, "got sig %i, write returned %d\n", s, ret);
60 }
61
62 /**
63 * reap one child
64 *
65 * call waitpid() and print a log message containing the pid
66 * and the cause of the child's death.
67 *
68 * \return Like \p waitpid(), this function returns the process ID of the
69 * terminated child; on error, \p -E_WAITPID is returned.
70 * \sa waitpid(2)
71 */
72 pid_t para_reap_child(void)
73 {
74 int status;
75 pid_t pid = waitpid(-1, &status, WNOHANG);
76
77 if (pid <= 0) {
78 if (pid < 0)
79 pid = -E_WAITPID;
80 return pid;
81 }
82 if (WIFEXITED(status))
83 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", pid,
84 WEXITSTATUS(status));
85 else if (WIFSIGNALED(status))
86 PARA_DEBUG_LOG("child %i was killed by signal %i\n", pid,
87 WTERMSIG(status));
88 else
89 PARA_WARNING_LOG("child %i terminated abormally\n", pid);
90 return pid;
91 }
92
93 /**
94 * paraslash's zombie killer
95 *
96 * It just calls \p para_reap_child() until there are no more children left to
97 * reap.
98 */
99 void para_reap_children(void)
100 {
101 while (para_reap_child() > 0)
102 ; /* nothing */
103 }
104
105 /**
106 * wrapper around signal(2)
107 * \param sig the number of the signal to catch
108 *
109 * This installs the generic signal handler for the given signal.
110 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
111 * \sa signal(2)
112 */
113 int para_install_sighandler(int sig)
114 {
115 PARA_DEBUG_LOG("catching signal %d\n", sig);
116 return signal(sig, &generic_signal_handler) == SIG_ERR? -E_SIGNAL_SIG_ERR : 1;
117 }
118
119 /**
120 * return number of next pending signal
121 *
122 * This should be called if the fd for the signal pipe is ready for reading.
123 *
124 * \return On success, the number of the received signal is returned. \p
125 * -E_SIGNAL_READ is returned if a read error occured while reading the signal
126 * pipe. If the read was interrupted by another signal the function returns 0.
127 */
128 int para_next_signal(void)
129 {
130 int s;
131 ssize_t r;
132
133 if ((r = read(signal_pipe[0], &s, sizeof(s)) == sizeof(s)) > 0) {
134 PARA_DEBUG_LOG("next signal: %d\n", s);
135 return s;
136 }
137 return r < 0 && (errno != EAGAIN)? 0 : -E_SIGNAL_READ;
138 }