]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/ircd_signal.c
2 * IRC - Internet Relay Chat, ircd/ircd_signal.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 1, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief Signal handlers for ircu.
22 * @version $Id: ircd_signal.c,v 1.6.2.2 2006/03/25 03:46:56 entrope Exp $
27 #include "ircd_alloc.h"
28 #include "ircd_events.h"
30 #include "ircd_signal.h"
33 /* #include <assert.h> -- Now using assert in ircd_log.h */
39 /** Records a function to be called when a child process terminates. */
41 struct ChildRecord
*next
;
47 /** Counts various types of signals that we receive. */
48 static struct tag_SignalCounter
{
49 unsigned int alrm
; /**< Received SIGALRM count. */
50 unsigned int hup
; /**< Received SIGHUP count. */
51 unsigned int chld
; /**< Received SIGCHLD count. */
54 /** Event generator for SIGHUP. */
55 static struct Signal sig_hup
;
56 /** Event generator for SIGINT. */
57 static struct Signal sig_int
;
58 /** Event generator for SIGTERM. */
59 static struct Signal sig_term
;
60 /** Event generator for SIGCHLD. */
61 static struct Signal sig_chld
;
62 /** List of active child process callback requests. */
63 static struct ChildRecord
*children
;
64 /** List of inactive (free) child records. */
65 static struct ChildRecord
*crec_freelist
;
67 /* Make sure we have a definition for SIGCHLD. */
69 # define SIGCHLD SIGCLD
72 /** Signal handler for SIGALRM.
73 * @param[in] sig Signal number (ignored).
75 static void sigalrm_handler(int sig
)
80 /** Signal callback for SIGTERM.
81 * @param[in] ev Signal event descriptor.
83 static void sigterm_callback(struct Event
* ev
)
85 assert(0 != ev_signal(ev
));
86 assert(ET_SIGNAL
== ev_type(ev
));
87 assert(SIGTERM
== sig_signal(ev_signal(ev
)));
88 assert(SIGTERM
== ev_data(ev
));
90 server_die("received signal SIGTERM");
93 /** Signal callback for SIGHUP.
94 * @param[in] ev Signal event descriptor.
96 static void sighup_callback(struct Event
* ev
)
98 assert(0 != ev_signal(ev
));
99 assert(ET_SIGNAL
== ev_type(ev
));
100 assert(SIGHUP
== sig_signal(ev_signal(ev
)));
101 assert(SIGHUP
== ev_data(ev
));
107 /** Signal callback for SIGINT.
108 * @param[in] ev Signal event descriptor.
110 static void sigint_callback(struct Event
* ev
)
112 assert(0 != ev_signal(ev
));
113 assert(ET_SIGNAL
== ev_type(ev
));
114 assert(SIGINT
== sig_signal(ev_signal(ev
)));
115 assert(SIGINT
== ev_data(ev
));
117 server_restart("caught signal: SIGINT");
120 /** Allocate a child callback record.
121 * @return Newly allocated callback record.
123 static struct ChildRecord
*alloc_crec(void)
125 struct ChildRecord
*crec
;
129 crec
= crec_freelist
;
130 crec_freelist
= crec
->next
;
134 crec
= MyCalloc(1, sizeof(*crec
));
137 memset(crec
, 0, sizeof(*crec
));
142 /** Release \a crec, which is after \a prev.
143 * @param[in] crec Child process callback record to release.
145 static void release_crec(struct ChildRecord
*crec
)
147 memset(crec
, 0, sizeof(*crec
));
148 crec
->next
= crec_freelist
;
149 crec_freelist
= crec
;
152 /** Register a function to be called when a child process terminates.
153 * @param[in] child Child process ID.
154 * @param[in] call Function to call when process \a child terminates.
155 * @param[in] datum Additional data parameter to pass to \a call.
157 void register_child(pid_t child
, SigChldCallBack call
, void *datum
)
159 struct ChildRecord
*crec
;
162 /* Link into #children list. */
163 crec
->next
= children
;
165 /* Fill in user fields. */
171 /** Unregister all callbacks for a child process, optionally calling
173 * @param[in] child Child process ID to unregister.
174 * @param[in] do_call If non-zero, make the callbacks.
175 * @param[in] status If \a do_call is non-zero, the child's exit status.
177 static void do_unregister_child(pid_t child
, int do_call
, int status
)
179 struct ChildRecord
*crec
= children
;
180 struct ChildRecord
*prev
= NULL
;
184 if (crec
->cpid
== child
)
187 crec
->call(child
, crec
->datum
, status
);
190 prev
->next
= crec
->next
;
192 children
= crec
->next
;
198 crec
= prev
? prev
->next
: children
;
202 /** Unregister all callbacks for a child process.
203 * @param[in] child Child process ID to unregister.
205 void unregister_child(pid_t child
)
207 do_unregister_child(child
, 0, 0);
210 /** Signal handler for SIGCHLD.
211 * @param[in] ev Signal event descriptor.
213 static void sigchld_callback(struct Event
*ev
)
218 ++SignalCounter
.chld
;
220 cpid
= waitpid(-1, &status
, WNOHANG
);
222 do_unregister_child(cpid
, 1, status
);
226 /** Register all necessary signal handlers. */
227 void setup_signals(void)
229 struct sigaction act
;
231 act
.sa_handler
= SIG_IGN
;
233 sigemptyset(&act
.sa_mask
);
234 sigaddset(&act
.sa_mask
, SIGPIPE
);
235 sigaddset(&act
.sa_mask
, SIGALRM
);
237 sigaddset(&act
.sa_mask
, SIGWINCH
);
238 sigaction(SIGWINCH
, &act
, 0);
240 sigaction(SIGPIPE
, &act
, 0);
242 act
.sa_handler
= sigalrm_handler
;
243 sigaction(SIGALRM
, &act
, 0);
245 signal_add(&sig_hup
, sighup_callback
, 0, SIGHUP
);
246 signal_add(&sig_int
, sigint_callback
, 0, SIGINT
);
247 signal_add(&sig_term
, sigterm_callback
, 0, SIGTERM
);
248 signal_add(&sig_chld
, sigchld_callback
, 0, SIGCHLD
);
250 #ifdef HAVE_RESTARTABLE_SYSCALLS
252 * At least on Apollo sr10.1 it seems continuing system calls
253 * after signal is the default. The following 'siginterrupt'
254 * should change that default to interrupting calls.
256 siginterrupt(SIGALRM
, 1);
260 /** Kill and clean up all child processes. */
261 void reap_children(void)
263 /* Send SIGTERM to all children in process group. Sleep for a
264 * second to let them exit before we try to clean them up.
268 sigchld_callback(NULL
);