2 * ngIRCd -- The Next Generation IRC Daemon
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 * Please read the file COPYING, README and AUTHORS for more information.
15 * Signal Handlers: Actions to be performed when the program
24 #include <sys/types.h>
35 #include "sighandlers.h"
37 static int signalpipe[2];
45 Log(LOG_DEBUG, "--- Internal server state: ---");
46 Log(LOG_DEBUG, "time()=%ld", time(NULL));
49 Log(LOG_DEBUG, "--- End of state dump ---");
55 static void Signal_Block(int sig)
57 #ifdef HAVE_SIGPROCMASK
63 sigprocmask(SIG_BLOCK, &set, NULL);
70 static void Signal_Unblock(int sig)
72 #ifdef HAVE_SIGPROCMASK
78 sigprocmask(SIG_UNBLOCK, &set, NULL);
80 int old = sigblock(0) & ~sig;
86 * Reload the server configuration file.
91 char old_name[CLIENT_ID_LEN];
94 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
96 /* Remember old server name and nick name length */
97 strlcpy( old_name, Conf_ServerName, sizeof old_name );
98 old_nicklen = Conf_MaxNickLength;
100 /* Re-read configuration ... */
104 /* Close down all listening sockets */
105 Conn_ExitListeners( );
107 /* Recover old server name and nick name length: these values can't
108 * be changed during run-time */
109 if (strcmp(old_name, Conf_ServerName) != 0 ) {
110 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
111 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
113 if (old_nicklen != Conf_MaxNickLength) {
114 Conf_MaxNickLength = old_nicklen;
115 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
118 /* Create new pre-defined channels */
119 Channel_InitPredefined( );
121 if (!ConnSSL_InitLibrary())
122 Log(LOG_WARNING, "Re-Initializing SSL failed, using old keys");
124 /* Start listening on sockets */
125 Conn_InitListeners( );
127 /* Sync configuration with established connections */
128 Conn_SyncServerStruct( );
130 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
131 } /* NGIRCd_Rehash */
136 * Signal handler of ngIRCd.
137 * This function is called whenever ngIRCd catches a signal sent by the
138 * user and/or the system to it. For example SIGTERM and SIGHUP.
140 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
141 * @param Signal Number of the signal to handle.
143 static void Signal_Handler(int Signal)
149 /* shut down sever */
150 NGIRCd_SignalQuit = true;
155 /* child-process exited, avoid zombies */
156 while (waitpid( -1, NULL, WNOHANG) > 0)
161 if (! NGIRCd_Debug) {
162 Log(LOG_INFO|LOG_snotice,
163 "Got SIGUSR1, debug mode activated.");
165 strcpy(NGIRCd_DebugLevel, "2");
167 NGIRCd_Sniffer = true;
169 strcpy(NGIRCd_DebugLevel, "1");
173 Log(LOG_INFO|LOG_snotice,
174 "Got SIGUSR1, debug mode deactivated.");
175 strcpy(NGIRCd_DebugLevel, "");
176 NGIRCd_Debug = false;
178 NGIRCd_Sniffer = false;
186 * other signal: queue for later execution.
187 * This has the advantage that we are not restricted
188 * to functions that can be called safely from signal handlers.
190 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
191 Signal_Block(Signal);
192 } /* Signal_Handler */
196 * Signal processing handler of ngIRCd.
197 * This function is called from the main conn event loop in (io_dispatch)
198 * whenever ngIRCd has queued a signal.
200 * This function runs in normal context, not from the real signal handler,
201 * thus its not necessary to only use functions that are signal safe.
202 * @param Signal Number of the signal that was queued.
204 static void Signal_Handler_BH(int Signal)
208 /* re-read configuration */
217 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
220 Signal_Unblock(Signal);
223 static void Sig_callback(int fd, short UNUSED what)
229 ret = read(fd, &sig, sizeof(sig));
230 if (ret == sizeof(int))
231 Signal_Handler_BH(sig);
232 } while (ret == sizeof(int));
235 if (errno == EAGAIN || errno == EINTR)
238 Log(LOG_EMERG, "read from signal pipe: %s", strerror(errno));
242 Log(LOG_EMERG, "EOF on signal pipe");
247 static const int signals_catch[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2 };
249 * Initialize the signal handlers, catch
250 * those signals we are interested in and sets SIGPIPE to be ignored.
251 * @return true if initialization was sucessful.
253 bool Signals_Init(void)
256 #ifdef HAVE_SIGACTION
257 struct sigaction saction;
260 if (pipe(signalpipe))
263 if (!io_setnonblock(signalpipe[0]) ||
264 !io_setnonblock(signalpipe[1]))
266 if (!io_setcloexec(signalpipe[0]) ||
267 !io_setcloexec(signalpipe[1]))
269 #ifdef HAVE_SIGACTION
270 memset( &saction, 0, sizeof( saction ));
271 saction.sa_handler = Signal_Handler;
273 saction.sa_flags |= SA_RESTART;
276 saction.sa_flags |= SA_NOCLDWAIT;
279 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
280 sigaction(signals_catch[i], &saction, NULL);
282 /* we handle write errors properly; ignore SIGPIPE */
283 saction.sa_handler = SIG_IGN;
284 sigaction(SIGPIPE, &saction, NULL);
286 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
287 signal(signals_catch[i], Signal_Handler);
289 signal(SIGPIPE, SIG_IGN);
291 return io_event_create(signalpipe[0], IO_WANTREAD,
293 } /* Initialize_Signal_Handler */
297 * Restores signals to their default behaviour.
299 * This should be called after a fork() in the new
300 * child prodcess, especially when we are about to call
301 * 3rd party code (e.g. PAM).
303 void Signals_Exit(void)
306 #ifdef HAVE_SIGACTION
307 struct sigaction saction;
309 memset(&saction, 0, sizeof(saction));
310 saction.sa_handler = SIG_DFL;
312 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
313 sigaction(signals_catch[i], &saction, NULL);
314 sigaction(SIGPIPE, &saction, NULL);
316 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
317 sigaction(signals_catch[i], &saction, NULL);
318 signal(SIGPIPE, SIG_DFL);
320 close(signalpipe[1]);
321 close(signalpipe[0]);