fix typo in FEATURES
[paraslash.git] / signal.c
1 /*
2  * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18 /** \file signal.c signal handling functions */
19
20
21
22 #include "para.h"
23 #include "fd.h"
24
25 #include <signal.h>
26
27 #include "error.h"
28 static int signal_pipe[2];
29
30 /**
31  * initialize the paraslash signal subsystem
32  *
33  * This function creates a pipe, the signal pipe, to deliver pending
34  * signals to the application (Bernstein's trick). It should be called
35  * during the application's startup part, followed by subsequent calls
36  * to para_install_sighandler() for each signal that should be caught.
37  *
38  * para_signal_init() installs a generic signal handler which is used for all
39  * signals simultaneously. When a signal arrives, this generic signal handler
40  * writes the corresponding signal number to the signal pipe so that the
41  * application can test for pending signals simply by checking the signal pipe
42  * for reading, e.g. by using the select(2) system call.
43  *
44  * \return This function either succeeds or calls exit(2) to terminate
45  * the current process. On success, the file descriptor of the signal pipe is
46  * returned.
47  */
48 int para_signal_init(void)
49 {
50         int ret = -E_SIGNAL_PIPE;
51         if (pipe(signal_pipe))
52                 goto err_out;
53         ret = mark_fd_nonblock(signal_pipe[0]);
54         if (ret < 0)
55                 goto err_out;
56         ret = mark_fd_nonblock(signal_pipe[1]);
57         if (ret < 0)
58                 goto err_out;
59         return signal_pipe[0];
60 err_out:
61         PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
62         exit(EXIT_FAILURE);
63 }
64
65 /*
66  * just write one integer to signal pipe
67  */
68 static void generic_signal_handler(int s)
69 {
70         write(signal_pipe[1], &s, sizeof(int));
71         //fprintf(stderr, "got sig %i, write returned %d\n", s, ret);
72 }
73
74 /**
75  * reap one child
76  *
77  * call waitpid() and print a log message containing the pid
78  * and the cause of the child's death.
79  *
80  * \return Like \p waitpid(), this function returns the process ID of the
81  * terminated child; on error, \p -E_WAITPID is returned.
82  * \sa waitpid(2)
83  */
84 pid_t para_reap_child(void)
85 {
86         int status;
87         pid_t pid = waitpid(-1, &status, WNOHANG);
88
89         if (pid <= 0) {
90                 if (pid < 0)
91                         pid = -E_WAITPID;
92                 return pid;
93         }
94         if (WIFEXITED(status))
95                 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", pid,
96                         WEXITSTATUS(status));
97         else if (WIFSIGNALED(status))
98                 PARA_DEBUG_LOG("child %i was killed by signal %i\n", pid,
99                         WTERMSIG(status));
100         else
101                 PARA_WARNING_LOG("child %i terminated abormally\n", pid);
102         return pid;
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         while (para_reap_child() > 0)
114                 ; /* nothing */
115 }
116
117 /**
118  * wrapper around signal(2)
119  * \param sig the number of the signal to catch
120  *
121  * This installs the generic signal handler for the given signal.
122  * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
123  * \sa signal(2)
124  */
125 int para_install_sighandler(int sig)
126 {
127         PARA_DEBUG_LOG("catching signal %d\n", sig);
128         return signal(sig, &generic_signal_handler) == SIG_ERR?  -E_SIGNAL_SIG_ERR : 1;
129 }
130
131 /**
132  * return number of next pending signal
133  *
134  * This should be called if the fd for the signal pipe is ready for reading.
135  *
136  * \return On success, the number of the received signal is returned. \p
137  * -E_SIGNAL_READ is returned if a read error occured while reading the signal
138  * pipe.  If the read was interrupted by another signal the function returns 0.
139  */
140 int para_next_signal(void)
141 {
142         int s;
143         ssize_t r;
144
145         if ((r = read(signal_pipe[0], &s, sizeof(s)) == sizeof(s)) > 0) {
146                 PARA_DEBUG_LOG("next signal: %d\n", s);
147                 return s;
148         }
149         return r < 0 && (errno != EAGAIN)? 0 : -E_SIGNAL_READ;
150 }