2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Signal Handlers: Actions to be performed when the program
25 #include <sys/types.h>
36 #include "sighandlers.h"
38 static int signalpipe[2];
40 static const int signals_catch[] = {
41 SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2
49 Log(LOG_DEBUG, "--- Internal server state: %s ---",
50 Client_ID(Client_ThisServer()));
51 Log(LOG_DEBUG, "time()=%ld", time(NULL));
55 Log(LOG_DEBUG, "--- End of state dump ---");
63 #ifdef HAVE_SIGPROCMASK
69 sigprocmask(SIG_BLOCK, &set, NULL);
76 Signal_Unblock(int sig)
78 #ifdef HAVE_SIGPROCMASK
84 sigprocmask(SIG_UNBLOCK, &set, NULL);
86 int old = sigblock(0) & ~sig;
92 * Reload the server configuration file.
97 char old_name[CLIENT_ID_LEN];
100 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
102 /* Remember old server name and nickname length */
103 strlcpy( old_name, Conf_ServerName, sizeof old_name );
104 old_nicklen = Conf_MaxNickLength;
106 /* Re-read configuration ... */
110 /* Close down all listening sockets */
111 Conn_ExitListeners( );
113 /* Recover old server name and nickname length: these values can't
114 * be changed during run-time */
115 if (strcmp(old_name, Conf_ServerName) != 0 ) {
116 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
118 "Can't change \"ServerName\" on runtime! Ignored new name.");
120 if (old_nicklen != Conf_MaxNickLength) {
121 Conf_MaxNickLength = old_nicklen;
123 "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
126 /* Create new pre-defined channels */
127 Channel_InitPredefined( );
129 if (!ConnSSL_InitLibrary())
131 "Re-Initializing of SSL failed, using old keys!");
133 /* Start listening on sockets */
134 Conn_InitListeners( );
136 /* Sync configuration with established connections */
137 Conn_SyncServerStruct( );
139 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
143 * Signal handler of ngIRCd.
144 * This function is called whenever ngIRCd catches a signal sent by the
145 * user and/or the system to it. For example SIGTERM and SIGHUP.
147 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
148 * @param Signal Number of the signal to handle.
151 Signal_Handler(int Signal)
157 /* shut down sever */
158 NGIRCd_SignalQuit = true;
161 /* child-process exited, avoid zombies */
162 while (waitpid( -1, NULL, WNOHANG) > 0)
167 if (! NGIRCd_Debug) {
168 Log(LOG_INFO|LOG_snotice,
169 "Got SIGUSR1, debug mode activated.");
171 strcpy(NGIRCd_DebugLevel, "2");
173 NGIRCd_Sniffer = true;
175 strcpy(NGIRCd_DebugLevel, "1");
179 Log(LOG_INFO|LOG_snotice,
180 "Got SIGUSR1, debug mode deactivated.");
181 strcpy(NGIRCd_DebugLevel, "");
182 NGIRCd_Debug = false;
184 NGIRCd_Sniffer = false;
192 * other signal: queue for later execution.
193 * This has the advantage that we are not restricted
194 * to functions that can be called safely from signal handlers.
196 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
197 Signal_Block(Signal);
198 } /* Signal_Handler */
201 * Signal processing handler of ngIRCd.
202 * This function is called from the main conn event loop in (io_dispatch)
203 * whenever ngIRCd has queued a signal.
205 * This function runs in normal context, not from the real signal handler,
206 * thus its not necessary to only use functions that are signal safe.
207 * @param Signal Number of the signal that was queued.
210 Signal_Handler_BH(int Signal)
214 /* re-read configuration */
220 Log(LOG_INFO|LOG_snotice,
221 "Got SIGUSR2, dumping internal state ...");
226 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
229 Signal_Unblock(Signal);
233 Signal_Callback(int fd, short UNUSED what)
239 ret = (int)read(fd, &sig, sizeof(sig));
240 if (ret == sizeof(int))
241 Signal_Handler_BH(sig);
242 } while (ret == sizeof(int));
245 if (errno == EAGAIN || errno == EINTR)
248 Log(LOG_EMERG, "Read from signal pipe: %s - Exiting!",
253 Log(LOG_EMERG, "EOF on signal pipe!? - Exiting!");
258 * Initialize the signal handlers, catch
259 * those signals we are interested in and sets SIGPIPE to be ignored.
260 * @return true if initialization was successful.
266 #ifdef HAVE_SIGACTION
267 struct sigaction saction;
269 if (signalpipe[0] > 0 || signalpipe[1] > 0)
272 if (pipe(signalpipe))
275 if (!io_setnonblock(signalpipe[0]) ||
276 !io_setnonblock(signalpipe[1]))
278 if (!io_setcloexec(signalpipe[0]) ||
279 !io_setcloexec(signalpipe[1]))
281 #ifdef HAVE_SIGACTION
282 memset( &saction, 0, sizeof( saction ));
283 saction.sa_handler = Signal_Handler;
285 saction.sa_flags |= SA_RESTART;
288 saction.sa_flags |= SA_NOCLDWAIT;
291 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
292 sigaction(signals_catch[i], &saction, NULL);
294 /* we handle write errors properly; ignore SIGPIPE */
295 saction.sa_handler = SIG_IGN;
296 sigaction(SIGPIPE, &saction, NULL);
298 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
299 signal(signals_catch[i], Signal_Handler);
301 signal(SIGPIPE, SIG_IGN);
303 return io_event_create(signalpipe[0], IO_WANTREAD, Signal_Callback);
307 * Restores signals to their default behavior.
309 * This should be called after a fork() in the new
310 * child prodcess, especially when we are about to call
311 * 3rd party code (e.g. PAM).
317 #ifdef HAVE_SIGACTION
318 struct sigaction saction;
320 memset(&saction, 0, sizeof(saction));
321 saction.sa_handler = SIG_DFL;
323 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
324 sigaction(signals_catch[i], &saction, NULL);
325 sigaction(SIGPIPE, &saction, NULL);
327 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
328 signal(signals_catch[i], SIG_DFL);
329 signal(SIGPIPE, SIG_DFL);
331 close(signalpipe[1]);
332 close(signalpipe[0]);
333 signalpipe[0] = signalpipe[1] = 0;