Doxyfy ircd_signal.c.
[ircu2.10.12-pk.git] / ircd / ircd_signal.c
1 /*
2  * IRC - Internet Relay Chat, ircd/ircd_signal.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
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)
9  * any later version.
10  *
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.
15  *
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.
19  */
20 /** @file
21  * @brief Signal handlers for ircu.
22  * @version $Id$
23  */
24 #include "config.h"
25
26 #include "ircd.h"
27 #include "ircd_events.h"
28 #include "ircd_signal.h"
29 #include "s_conf.h"
30
31 #include <assert.h>
32 #include <signal.h>
33
34 /** Counts various types of signals that we receive. */
35 static struct tag_SignalCounter {
36   unsigned int alrm; /**< Received SIGALRM count. */
37   unsigned int hup;  /**< Received SIGHUP count. */
38 } SignalCounter;
39
40 /** Event generator for SIGHUP. */
41 static struct Signal sig_hup;
42 /** Event generator for SIGINT. */
43 static struct Signal sig_int;
44 /** Event generator for SIGTERM. */
45 static struct Signal sig_term;
46
47 /** Signal handler for SIGALRM.
48  * @param[in] sig Signal number (ignored).
49  */
50 static void sigalrm_handler(int sig)
51 {
52   ++SignalCounter.alrm;
53 }
54
55 /** Signal callback for SIGTERM.
56  * @param[in] ev Signal event descriptor.
57  */
58 static void sigterm_callback(struct Event* ev)
59 {
60   assert(0 != ev_signal(ev));
61   assert(ET_SIGNAL == ev_type(ev));
62   assert(SIGTERM == sig_signal(ev_signal(ev)));
63   assert(SIGTERM == ev_data(ev));
64
65   server_die("received signal SIGTERM");
66 }
67
68 /** Signal callback for SIGHUP.
69  * @param[in] ev Signal event descriptor.
70  */
71 static void sighup_callback(struct Event* ev)
72 {
73   assert(0 != ev_signal(ev));
74   assert(ET_SIGNAL == ev_type(ev));
75   assert(SIGHUP == sig_signal(ev_signal(ev)));
76   assert(SIGHUP == ev_data(ev));
77
78   ++SignalCounter.hup;
79   rehash(&me, 1);
80 }
81
82 /** Signal callback for SIGINT.
83  * @param[in] ev Signal event descriptor.
84  */
85 static void sigint_callback(struct Event* ev)
86 {
87   assert(0 != ev_signal(ev));
88   assert(ET_SIGNAL == ev_type(ev));
89   assert(SIGINT == sig_signal(ev_signal(ev)));
90   assert(SIGINT == ev_data(ev));
91
92   server_restart("caught signal: SIGINT");
93 }
94
95 /** Register all necessary signal handlers. */
96 void setup_signals(void)
97 {
98   struct sigaction act;
99
100   act.sa_handler = SIG_IGN;
101   act.sa_flags = 0;
102   sigemptyset(&act.sa_mask);
103   sigaddset(&act.sa_mask, SIGPIPE);
104   sigaddset(&act.sa_mask, SIGALRM);
105 #ifdef  SIGWINCH
106   sigaddset(&act.sa_mask, SIGWINCH);
107   sigaction(SIGWINCH, &act, 0);
108 #endif
109   sigaction(SIGPIPE, &act, 0);
110
111   act.sa_handler = sigalrm_handler;
112   sigaction(SIGALRM, &act, 0);
113
114   signal_add(&sig_hup, sighup_callback, 0, SIGHUP);
115   signal_add(&sig_int, sigint_callback, 0, SIGINT);
116   signal_add(&sig_term, sigterm_callback, 0, SIGTERM);
117
118 #ifdef HAVE_RESTARTABLE_SYSCALLS
119   /*
120    * At least on Apollo sr10.1 it seems continuing system calls
121    * after signal is the default. The following 'siginterrupt'
122    * should change that default to interrupting calls.
123    */
124   siginterrupt(SIGALRM, 1);
125 #endif
126 }
127