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>
35 #include "sighandlers.h"
37 static int signalpipe[2];
39 static const int signals_catch[] = {
40 SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2
48 Log(LOG_DEBUG, "--- Internal server state: %s ---",
49 Client_ID(Client_ThisServer()));
50 Log(LOG_DEBUG, "time()=%ld", time(NULL));
54 Log(LOG_DEBUG, "--- End of state dump ---");
62 #ifdef HAVE_SIGPROCMASK
68 sigprocmask(SIG_BLOCK, &set, NULL);
75 Signal_Unblock(int sig)
77 #ifdef HAVE_SIGPROCMASK
83 sigprocmask(SIG_UNBLOCK, &set, NULL);
85 int old = sigblock(0) & ~sig;
91 * Reload the server configuration file.
96 char old_name[CLIENT_ID_LEN];
99 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
101 /* Remember old server name and nickname length */
102 strlcpy( old_name, Conf_ServerName, sizeof old_name );
103 old_nicklen = Conf_MaxNickLength;
105 /* Re-read configuration ... */
109 /* Close down all listening sockets */
110 Conn_ExitListeners( );
112 /* Recover old server name and nickname length: these values can't
113 * be changed during run-time */
114 if (strcmp(old_name, Conf_ServerName) != 0 ) {
115 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
117 "Can't change \"ServerName\" on runtime! Ignored new name.");
119 if (old_nicklen != Conf_MaxNickLength) {
120 Conf_MaxNickLength = old_nicklen;
122 "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
125 /* Create new pre-defined channels */
126 Channel_InitPredefined( );
128 if (!ConnSSL_InitLibrary())
130 "Re-Initializing of SSL failed, using old keys!");
132 /* Start listening on sockets */
133 Conn_InitListeners( );
135 /* Sync configuration with established connections */
136 Conn_SyncServerStruct( );
138 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
142 * Signal handler of ngIRCd.
143 * This function is called whenever ngIRCd catches a signal sent by the
144 * user and/or the system to it. For example SIGTERM and SIGHUP.
146 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
147 * @param Signal Number of the signal to handle.
150 Signal_Handler(int Signal)
156 /* shut down sever */
157 NGIRCd_SignalQuit = true;
160 /* child-process exited, avoid zombies */
161 while (waitpid( -1, NULL, WNOHANG) > 0)
166 if (! NGIRCd_Debug) {
167 Log(LOG_INFO|LOG_snotice,
168 "Got SIGUSR1, debug mode activated.");
170 strcpy(NGIRCd_DebugLevel, "2");
172 NGIRCd_Sniffer = true;
174 strcpy(NGIRCd_DebugLevel, "1");
178 Log(LOG_INFO|LOG_snotice,
179 "Got SIGUSR1, debug mode deactivated.");
180 strcpy(NGIRCd_DebugLevel, "");
181 NGIRCd_Debug = false;
183 NGIRCd_Sniffer = false;
191 * other signal: queue for later execution.
192 * This has the advantage that we are not restricted
193 * to functions that can be called safely from signal handlers.
195 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
196 Signal_Block(Signal);
197 } /* Signal_Handler */
200 * Signal processing handler of ngIRCd.
201 * This function is called from the main conn event loop in (io_dispatch)
202 * whenever ngIRCd has queued a signal.
204 * This function runs in normal context, not from the real signal handler,
205 * thus its not necessary to only use functions that are signal safe.
206 * @param Signal Number of the signal that was queued.
209 Signal_Handler_BH(int Signal)
213 /* re-read configuration */
219 Log(LOG_INFO|LOG_snotice,
220 "Got SIGUSR2, dumping internal state ...");
225 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
228 Signal_Unblock(Signal);
232 Signal_Callback(int fd, short UNUSED what)
238 ret = (int)read(fd, &sig, sizeof(sig));
239 if (ret == sizeof(int))
240 Signal_Handler_BH(sig);
241 } while (ret == sizeof(int));
244 if (errno == EAGAIN || errno == EINTR)
247 Log(LOG_EMERG, "Read from signal pipe: %s - Exiting!",
252 Log(LOG_EMERG, "EOF on signal pipe!? - Exiting!");
257 * Initialize the signal handlers, catch
258 * those signals we are interested in and sets SIGPIPE to be ignored.
259 * @return true if initialization was successful.
265 #ifdef HAVE_SIGACTION
266 struct sigaction saction;
268 if (signalpipe[0] > 0 || signalpipe[1] > 0)
271 if (pipe(signalpipe))
274 if (!io_setnonblock(signalpipe[0]) ||
275 !io_setnonblock(signalpipe[1]))
277 if (!io_setcloexec(signalpipe[0]) ||
278 !io_setcloexec(signalpipe[1]))
280 #ifdef HAVE_SIGACTION
281 memset( &saction, 0, sizeof( saction ));
282 saction.sa_handler = Signal_Handler;
284 saction.sa_flags |= SA_RESTART;
287 saction.sa_flags |= SA_NOCLDWAIT;
290 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
291 sigaction(signals_catch[i], &saction, NULL);
293 /* we handle write errors properly; ignore SIGPIPE */
294 saction.sa_handler = SIG_IGN;
295 sigaction(SIGPIPE, &saction, NULL);
297 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
298 signal(signals_catch[i], Signal_Handler);
300 signal(SIGPIPE, SIG_IGN);
302 return io_event_create(signalpipe[0], IO_WANTREAD, Signal_Callback);
306 * Restores signals to their default behavior.
308 * This should be called after a fork() in the new
309 * child prodcess, especially when we are about to call
310 * 3rd party code (e.g. PAM).
316 #ifdef HAVE_SIGACTION
317 struct sigaction saction;
319 memset(&saction, 0, sizeof(saction));
320 saction.sa_handler = SIG_DFL;
322 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
323 sigaction(signals_catch[i], &saction, NULL);
324 sigaction(SIGPIPE, &saction, NULL);
326 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
327 signal(signals_catch[i], SIG_DFL);
328 signal(SIGPIPE, SIG_DFL);
330 close(signalpipe[1]);
331 close(signalpipe[0]);
332 signalpipe[0] = signalpipe[1] = 0;