]> jfr.im git - irc/rqf/shadowircd.git/blob - src/ircd_signal.c
Improve SIGCHLD handler
[irc/rqf/shadowircd.git] / src / ircd_signal.c
1 /************************************************************************
2 * IRC - Internet Relay Chat, src/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: ircd_signal.c 6 2005-09-10 01:02:21Z nenolod $
21 */
22
23 #include "stdinc.h"
24 #include "ircd_signal.h"
25 #include "ircd.h" /* dorehash */
26 #include "restart.h" /* server_reboot */
27 #include "logger.h"
28 #include "s_conf.h"
29 #include "client.h"
30 #include "send.h"
31
32 /*
33 * dummy_handler - don't know if this is really needed but if alarm is still
34 * being used we probably will
35 */
36 static void
37 dummy_handler(int sig)
38 {
39 /* Empty */
40 }
41
42
43 static void
44 sigchld_handler(int sig)
45 {
46 int status, olderrno;
47
48 olderrno = errno;
49 while (waitpid(-1, &status, WNOHANG) > 0)
50 ;
51 errno = olderrno;
52 }
53
54 /*
55 * sigterm_handler - exit the server
56 */
57 static void
58 sigterm_handler(int sig)
59 {
60 /* XXX we had a flush_connections() here - we should close all the
61 * connections and flush data. read server_reboot() for my explanation.
62 * -- adrian
63 */
64 ilog(L_MAIN, "Server killed By SIGTERM");
65 exit(-1);
66 }
67
68 /*
69 * sighup_handler - reread the server configuration
70 */
71 static void
72 sighup_handler(int sig)
73 {
74 dorehash = 1;
75 }
76
77 /*
78 * sigusr1_handler - reread the motd file
79 */
80 static void
81 sigusr1_handler(int sig)
82 {
83 doremotd = 1;
84 }
85
86 static void
87 sigusr2_handler(int sig)
88 {
89 dorehashbans = 1;
90 }
91
92 /*
93 * sigint_handler - restart the server
94 */
95 static void
96 sigint_handler(int sig)
97 {
98 static int restarting = 0;
99
100 if(server_state_foreground)
101 {
102 ilog(L_MAIN, "Server exiting on SIGINT");
103 exit(0);
104 }
105 else
106 {
107 ilog(L_MAIN, "Server Restarting on SIGINT");
108 if(restarting == 0)
109 {
110 restarting = 1;
111 server_reboot();
112 }
113 }
114 }
115
116 /*
117 * setup_signals - initialize signal handlers for server
118 */
119 void
120 setup_signals()
121 {
122 struct sigaction act;
123
124 act.sa_flags = 0;
125 act.sa_handler = SIG_IGN;
126 sigemptyset(&act.sa_mask);
127 sigaddset(&act.sa_mask, SIGPIPE);
128 sigaddset(&act.sa_mask, SIGALRM);
129 #ifdef SIGTRAP
130 sigaddset(&act.sa_mask, SIGTRAP);
131 #endif
132
133 # ifdef SIGWINCH
134 sigaddset(&act.sa_mask, SIGWINCH);
135 sigaction(SIGWINCH, &act, 0);
136 # endif
137 sigaction(SIGPIPE, &act, 0);
138 #ifdef SIGTRAP
139 sigaction(SIGTRAP, &act, 0);
140 #endif
141
142 act.sa_handler = dummy_handler;
143 sigaction(SIGALRM, &act, 0);
144
145 act.sa_handler = sighup_handler;
146 sigemptyset(&act.sa_mask);
147 sigaddset(&act.sa_mask, SIGHUP);
148 sigaction(SIGHUP, &act, 0);
149
150 act.sa_handler = sigint_handler;
151 sigaddset(&act.sa_mask, SIGINT);
152 sigaction(SIGINT, &act, 0);
153
154 act.sa_handler = sigterm_handler;
155 sigaddset(&act.sa_mask, SIGTERM);
156 sigaction(SIGTERM, &act, 0);
157
158 act.sa_handler = sigusr1_handler;
159 sigaddset(&act.sa_mask, SIGUSR1);
160 sigaction(SIGUSR1, &act, 0);
161
162 act.sa_handler = sigusr2_handler;
163 sigaddset(&act.sa_mask, SIGUSR2);
164 sigaction(SIGUSR2, &act, 0);
165
166 act.sa_handler = sigchld_handler;
167 sigaddset(&act.sa_mask, SIGCHLD);
168 sigaction(SIGCHLD, &act, 0);
169
170 }