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 static int signalpipe[2];
38 static const int signals_catch[] = {
39 SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2
47 Log(LOG_DEBUG, "--- Internal server state: %s ---",
48 Client_ID(Client_ThisServer()));
49 Log(LOG_DEBUG, "time()=%ld", time(NULL));
53 Log(LOG_DEBUG, "--- End of state dump ---");
61 #ifdef HAVE_SIGPROCMASK
67 sigprocmask(SIG_BLOCK, &set, NULL);
74 Signal_Unblock(int sig)
76 #ifdef HAVE_SIGPROCMASK
82 sigprocmask(SIG_UNBLOCK, &set, NULL);
84 int old = sigblock(0) & ~sig;
90 * Reload the server configuration file.
95 char old_name[CLIENT_ID_LEN];
98 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
100 /* Remember old server name and nickname length */
101 strlcpy( old_name, Conf_ServerName, sizeof old_name );
102 old_nicklen = Conf_MaxNickLength;
104 /* Re-read configuration ... */
108 /* Close down all listening sockets */
109 Conn_ExitListeners( );
111 /* Recover old server name and nickname length: these values can't
112 * be changed during run-time */
113 if (strcmp(old_name, Conf_ServerName) != 0 ) {
114 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
116 "Can't change \"ServerName\" on runtime! Ignored new name.");
118 if (old_nicklen != Conf_MaxNickLength) {
119 Conf_MaxNickLength = old_nicklen;
121 "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
124 /* Create new pre-defined channels */
125 Channel_InitPredefined( );
127 if (!ConnSSL_InitLibrary())
129 "Re-Initializing of SSL failed, using old keys!");
131 /* Start listening on sockets */
132 Conn_InitListeners( );
134 /* Sync configuration with established connections */
135 Conn_SyncServerStruct( );
137 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
141 * Signal handler of ngIRCd.
142 * This function is called whenever ngIRCd catches a signal sent by the
143 * user and/or the system to it. For example SIGTERM and SIGHUP.
145 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
146 * @param Signal Number of the signal to handle.
149 Signal_Handler(int Signal)
155 /* shut down sever */
156 NGIRCd_SignalQuit = true;
159 /* child-process exited, avoid zombies */
160 while (waitpid( -1, NULL, WNOHANG) > 0)
165 if (! NGIRCd_Debug) {
166 Log(LOG_INFO|LOG_snotice,
167 "Got SIGUSR1, debug mode activated.");
169 strcpy(NGIRCd_DebugLevel, "2");
171 NGIRCd_Sniffer = true;
173 strcpy(NGIRCd_DebugLevel, "1");
177 Log(LOG_INFO|LOG_snotice,
178 "Got SIGUSR1, debug mode deactivated.");
179 strcpy(NGIRCd_DebugLevel, "");
180 NGIRCd_Debug = false;
182 NGIRCd_Sniffer = false;
190 * other signal: queue for later execution.
191 * This has the advantage that we are not restricted
192 * to functions that can be called safely from signal handlers.
194 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
195 Signal_Block(Signal);
196 } /* Signal_Handler */
199 * Signal processing handler of ngIRCd.
200 * This function is called from the main conn event loop in (io_dispatch)
201 * whenever ngIRCd has queued a signal.
203 * This function runs in normal context, not from the real signal handler,
204 * thus its not necessary to only use functions that are signal safe.
205 * @param Signal Number of the signal that was queued.
208 Signal_Handler_BH(int Signal)
212 /* re-read configuration */
218 Log(LOG_INFO|LOG_snotice,
219 "Got SIGUSR2, dumping internal state ...");
224 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
227 Signal_Unblock(Signal);
231 Signal_Callback(int fd, short UNUSED what)
237 ret = (int)read(fd, &sig, sizeof(sig));
238 if (ret == sizeof(int))
239 Signal_Handler_BH(sig);
240 } while (ret == sizeof(int));
243 if (errno == EAGAIN || errno == EINTR)
246 Log(LOG_EMERG, "Read from signal pipe: %s - Exiting!",
251 Log(LOG_EMERG, "EOF on signal pipe!? - Exiting!");
256 * Initialize the signal handlers, catch
257 * those signals we are interested in and sets SIGPIPE to be ignored.
258 * @return true if initialization was successful.
264 #ifdef HAVE_SIGACTION
265 struct sigaction saction;
267 if (signalpipe[0] > 0 || signalpipe[1] > 0)
270 if (pipe(signalpipe))
273 if (!io_setnonblock(signalpipe[0]) ||
274 !io_setnonblock(signalpipe[1]))
276 if (!io_setcloexec(signalpipe[0]) ||
277 !io_setcloexec(signalpipe[1]))
279 #ifdef HAVE_SIGACTION
280 memset( &saction, 0, sizeof( saction ));
281 saction.sa_handler = Signal_Handler;
283 saction.sa_flags |= SA_RESTART;
286 saction.sa_flags |= SA_NOCLDWAIT;
289 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
290 sigaction(signals_catch[i], &saction, NULL);
292 /* we handle write errors properly; ignore SIGPIPE */
293 saction.sa_handler = SIG_IGN;
294 sigaction(SIGPIPE, &saction, NULL);
296 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
297 signal(signals_catch[i], Signal_Handler);
299 signal(SIGPIPE, SIG_IGN);
301 return io_event_create(signalpipe[0], IO_WANTREAD, Signal_Callback);
305 * Restores signals to their default behavior.
307 * This should be called after a fork() in the new
308 * child prodcess, especially when we are about to call
309 * 3rd party code (e.g. PAM).
315 #ifdef HAVE_SIGACTION
316 struct sigaction saction;
318 memset(&saction, 0, sizeof(saction));
319 saction.sa_handler = SIG_DFL;
321 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
322 sigaction(signals_catch[i], &saction, NULL);
323 sigaction(SIGPIPE, &saction, NULL);
325 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
326 signal(signals_catch[i], SIG_DFL);
327 signal(SIGPIPE, SIG_DFL);
329 close(signalpipe[1]);
330 close(signalpipe[0]);
331 signalpipe[0] = signalpipe[1] = 0;