Author: Kev <klmitch@mit.edu>
[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  * $Id$
21  */
22 #include "config.h"
23
24 #include "ircd.h"
25 #include "ircd_events.h"
26 #include "ircd_signal.h"
27 #include "s_conf.h"
28
29 #include <assert.h>
30 #include <signal.h>
31
32 static struct tag_SignalCounter {
33   unsigned int alrm;
34   unsigned int hup;
35 } SignalCounter;
36
37 static struct Signal sig_hup;
38 static struct Signal sig_int;
39 static struct Signal sig_term;
40
41 static void sigalrm_handler(int sig)
42 {
43   ++SignalCounter.alrm;
44 }
45
46 static void sigterm_callback(struct Event* ev)
47 {
48   assert(0 != ev_signal(ev));
49   assert(ET_SIGNAL == ev_type(ev));
50   assert(SIGTERM == sig_signal(ev_signal(ev)));
51   assert(SIGTERM == ev_data(ev));
52
53   server_die("received signal SIGTERM");
54 }
55
56 static void sighup_callback(struct Event* ev)
57 {
58   assert(0 != ev_signal(ev));
59   assert(ET_SIGNAL == ev_type(ev));
60   assert(SIGHUP == sig_signal(ev_signal(ev)));
61   assert(SIGHUP == ev_data(ev));
62
63   ++SignalCounter.hup;
64   rehash(&me, 1);
65 }
66
67 static void sigint_callback(struct Event* ev)
68 {
69   assert(0 != ev_signal(ev));
70   assert(ET_SIGNAL == ev_type(ev));
71   assert(SIGINT == sig_signal(ev_signal(ev)));
72   assert(SIGINT == ev_data(ev));
73
74   server_restart("caught signal: SIGINT");
75 }
76
77 void setup_signals(void)
78 {
79   struct sigaction act;
80
81   act.sa_handler = SIG_IGN;
82   act.sa_flags = 0;
83   sigemptyset(&act.sa_mask);
84   sigaddset(&act.sa_mask, SIGPIPE);
85   sigaddset(&act.sa_mask, SIGALRM);
86 #ifdef  SIGWINCH
87   sigaddset(&act.sa_mask, SIGWINCH);
88   sigaction(SIGWINCH, &act, 0);
89 #endif
90   sigaction(SIGPIPE, &act, 0);
91
92   act.sa_handler = sigalrm_handler;
93   sigaction(SIGALRM, &act, 0);
94
95   signal_add(&sig_hup, sighup_callback, 0, SIGHUP);
96   signal_add(&sig_int, sigint_callback, 0, SIGINT);
97   signal_add(&sig_term, sigterm_callback, 0, SIGTERM);
98
99 #ifdef HAVE_RESTARTABLE_SYSCALLS
100   /*
101    * At least on Apollo sr10.1 it seems continuing system calls
102    * after signal is the default. The following 'siginterrupt'
103    * should change that default to interrupting calls.
104    */
105   siginterrupt(SIGALRM, 1);
106 #endif
107 }
108