2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2015 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
48 LogDebug("--- Internal server state: %s ---",
49 Client_ID(Client_ThisServer()));
51 LogDebug("time()=%llu", (unsigned long long)time(NULL));
53 LogDebug("time()=%lu", (unsigned long)time(NULL));
58 LogDebug("--- End of state dump ---");
65 #ifdef HAVE_SIGPROCMASK
71 sigprocmask(SIG_BLOCK, &set, NULL);
78 Signal_Unblock(int sig)
80 #ifdef HAVE_SIGPROCMASK
86 sigprocmask(SIG_UNBLOCK, &set, NULL);
88 int old = sigblock(0) & ~sig;
94 * Reload the server configuration file.
99 char old_name[CLIENT_ID_LEN];
100 unsigned old_nicklen;
102 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
104 /* Remember old server name and nickname length */
105 strlcpy( old_name, Conf_ServerName, sizeof old_name );
106 old_nicklen = Conf_MaxNickLength;
108 /* Re-read configuration ... */
112 /* Close down all listening sockets */
113 Conn_ExitListeners( );
115 /* Recover old server name and nickname length: these values can't
116 * be changed during run-time */
117 if (strcmp(old_name, Conf_ServerName) != 0 ) {
118 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
120 "Can't change server name (\"Name\") on runtime! Ignored new name.");
122 if (old_nicklen != Conf_MaxNickLength) {
123 Conf_MaxNickLength = old_nicklen;
125 "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
128 /* Create new pre-defined channels */
129 Channel_InitPredefined( );
131 if (!ConnSSL_InitLibrary())
133 "Re-Initializing of SSL failed!");
135 /* Start listening on sockets */
136 Conn_InitListeners( );
138 /* Sync configuration with established connections */
139 Conn_SyncServerStruct( );
141 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
145 * Signal handler of ngIRCd.
146 * This function is called whenever ngIRCd catches a signal sent by the
147 * user and/or the system to it. For example SIGTERM and SIGHUP.
149 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
150 * @param Signal Number of the signal to handle.
153 Signal_Handler(int Signal)
155 if (Signal != SIGCHLD) {
156 #ifdef HAVE_STRSIGNAL
157 Log(LOG_INFO, "Got signal \"%s\" ...", strsignal(Signal));
159 Log(LOG_INFO, "Got signal %d ...", Signal);
167 /* shut down sever */
168 NGIRCd_SignalQuit = true;
171 /* child-process exited, avoid zombies */
172 while (waitpid( -1, NULL, WNOHANG) > 0)
176 if (! NGIRCd_Debug) {
177 Log(LOG_INFO|LOG_snotice,
178 "Got SIGUSR1, debug mode activated.");
180 strcpy(NGIRCd_DebugLevel, "2");
182 NGIRCd_Sniffer = true;
184 strcpy(NGIRCd_DebugLevel, "1");
188 Log(LOG_INFO|LOG_snotice,
189 "Got SIGUSR1, debug mode deactivated.");
190 strcpy(NGIRCd_DebugLevel, "");
191 NGIRCd_Debug = false;
193 NGIRCd_Sniffer = false;
200 * other signal: queue for later execution.
201 * This has the advantage that we are not restricted
202 * to functions that can be called safely from signal handlers.
204 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
205 Signal_Block(Signal);
206 } /* Signal_Handler */
209 * Signal processing handler of ngIRCd.
210 * This function is called from the main conn event loop in (io_dispatch)
211 * whenever ngIRCd has queued a signal.
213 * This function runs in normal context, not from the real signal handler,
214 * thus its not necessary to only use functions that are signal safe.
215 * @param Signal Number of the signal that was queued.
218 Signal_Handler_BH(int Signal)
222 /* re-read configuration */
227 Log(LOG_INFO|LOG_snotice,
228 "Got SIGUSR2, dumping internal state ...");
233 LogDebug("Got signal %d! Ignored.", Signal);
235 Signal_Unblock(Signal);
239 Signal_Callback(int fd, short UNUSED what)
245 ret = (int)read(fd, &sig, sizeof(sig));
246 if (ret == sizeof(int))
247 Signal_Handler_BH(sig);
248 } while (ret == sizeof(int));
251 if (errno == EAGAIN || errno == EINTR)
254 Log(LOG_EMERG, "Read from signal pipe: %s - Exiting!",
259 Log(LOG_EMERG, "EOF on signal pipe!? - Exiting!");
264 * Initialize the signal handlers, catch
265 * those signals we are interested in and sets SIGPIPE to be ignored.
266 * @return true if initialization was successful.
272 #ifdef HAVE_SIGACTION
273 struct sigaction saction;
275 if (signalpipe[0] > 0 || signalpipe[1] > 0)
278 if (pipe(signalpipe))
281 if (!io_setnonblock(signalpipe[0]) ||
282 !io_setnonblock(signalpipe[1]))
284 if (!io_setcloexec(signalpipe[0]) ||
285 !io_setcloexec(signalpipe[1]))
287 #ifdef HAVE_SIGACTION
288 memset( &saction, 0, sizeof( saction ));
289 saction.sa_handler = Signal_Handler;
291 saction.sa_flags |= SA_RESTART;
294 saction.sa_flags |= SA_NOCLDWAIT;
297 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
298 sigaction(signals_catch[i], &saction, NULL);
300 /* we handle write errors properly; ignore SIGPIPE */
301 saction.sa_handler = SIG_IGN;
302 sigaction(SIGPIPE, &saction, NULL);
304 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
305 signal(signals_catch[i], Signal_Handler);
307 signal(SIGPIPE, SIG_IGN);
309 return io_event_create(signalpipe[0], IO_WANTREAD, Signal_Callback);
313 * Restores signals to their default behavior.
315 * This should be called after a fork() in the new
316 * child prodcess, especially when we are about to call
317 * 3rd party code (e.g. PAM).
323 #ifdef HAVE_SIGACTION
324 struct sigaction saction;
326 memset(&saction, 0, sizeof(saction));
327 saction.sa_handler = SIG_DFL;
329 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
330 sigaction(signals_catch[i], &saction, NULL);
331 sigaction(SIGPIPE, &saction, NULL);
333 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
334 signal(signals_catch[i], SIG_DFL);
335 signal(SIGPIPE, SIG_DFL);
337 close(signalpipe[1]);
338 close(signalpipe[0]);
339 signalpipe[0] = signalpipe[1] = 0;