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: %s ---",
46 Client_ID(Client_ThisServer()));
47 Log(LOG_DEBUG, "time()=%ld", time(NULL));
50 Log(LOG_DEBUG, "--- End of state dump ---");
56 static void Signal_Block(int sig)
58 #ifdef HAVE_SIGPROCMASK
64 sigprocmask(SIG_BLOCK, &set, NULL);
71 static void Signal_Unblock(int sig)
73 #ifdef HAVE_SIGPROCMASK
79 sigprocmask(SIG_UNBLOCK, &set, NULL);
81 int old = sigblock(0) & ~sig;
87 * Reload the server configuration file.
92 char old_name[CLIENT_ID_LEN];
95 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
97 /* Remember old server name and nick name length */
98 strlcpy( old_name, Conf_ServerName, sizeof old_name );
99 old_nicklen = Conf_MaxNickLength;
101 /* Re-read configuration ... */
105 /* Close down all listening sockets */
106 Conn_ExitListeners( );
108 /* Recover old server name and nick name length: these values can't
109 * be changed during run-time */
110 if (strcmp(old_name, Conf_ServerName) != 0 ) {
111 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
112 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
114 if (old_nicklen != Conf_MaxNickLength) {
115 Conf_MaxNickLength = old_nicklen;
116 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
119 /* Create new pre-defined channels */
120 Channel_InitPredefined( );
122 if (!ConnSSL_InitLibrary())
123 Log(LOG_WARNING, "Re-Initializing SSL failed, using old keys");
125 /* Start listening on sockets */
126 Conn_InitListeners( );
128 /* Sync configuration with established connections */
129 Conn_SyncServerStruct( );
131 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
132 } /* NGIRCd_Rehash */
137 * Signal handler of ngIRCd.
138 * This function is called whenever ngIRCd catches a signal sent by the
139 * user and/or the system to it. For example SIGTERM and SIGHUP.
141 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
142 * @param Signal Number of the signal to handle.
144 static void Signal_Handler(int Signal)
150 /* shut down sever */
151 NGIRCd_SignalQuit = true;
156 /* child-process exited, avoid zombies */
157 while (waitpid( -1, NULL, WNOHANG) > 0)
162 if (! NGIRCd_Debug) {
163 Log(LOG_INFO|LOG_snotice,
164 "Got SIGUSR1, debug mode activated.");
166 strcpy(NGIRCd_DebugLevel, "2");
168 NGIRCd_Sniffer = true;
170 strcpy(NGIRCd_DebugLevel, "1");
174 Log(LOG_INFO|LOG_snotice,
175 "Got SIGUSR1, debug mode deactivated.");
176 strcpy(NGIRCd_DebugLevel, "");
177 NGIRCd_Debug = false;
179 NGIRCd_Sniffer = false;
187 * other signal: queue for later execution.
188 * This has the advantage that we are not restricted
189 * to functions that can be called safely from signal handlers.
191 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
192 Signal_Block(Signal);
193 } /* Signal_Handler */
197 * Signal processing handler of ngIRCd.
198 * This function is called from the main conn event loop in (io_dispatch)
199 * whenever ngIRCd has queued a signal.
201 * This function runs in normal context, not from the real signal handler,
202 * thus its not necessary to only use functions that are signal safe.
203 * @param Signal Number of the signal that was queued.
205 static void Signal_Handler_BH(int Signal)
209 /* re-read configuration */
218 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
221 Signal_Unblock(Signal);
224 static void Sig_callback(int fd, short UNUSED what)
230 ret = read(fd, &sig, sizeof(sig));
231 if (ret == sizeof(int))
232 Signal_Handler_BH(sig);
233 } while (ret == sizeof(int));
236 if (errno == EAGAIN || errno == EINTR)
239 Log(LOG_EMERG, "read from signal pipe: %s", strerror(errno));
243 Log(LOG_EMERG, "EOF on signal pipe");
248 static const int signals_catch[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2 };
250 * Initialize the signal handlers, catch
251 * those signals we are interested in and sets SIGPIPE to be ignored.
252 * @return true if initialization was sucessful.
254 bool Signals_Init(void)
257 #ifdef HAVE_SIGACTION
258 struct sigaction saction;
261 if (pipe(signalpipe))
264 if (!io_setnonblock(signalpipe[0]) ||
265 !io_setnonblock(signalpipe[1]))
267 if (!io_setcloexec(signalpipe[0]) ||
268 !io_setcloexec(signalpipe[1]))
270 #ifdef HAVE_SIGACTION
271 memset( &saction, 0, sizeof( saction ));
272 saction.sa_handler = Signal_Handler;
274 saction.sa_flags |= SA_RESTART;
277 saction.sa_flags |= SA_NOCLDWAIT;
280 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
281 sigaction(signals_catch[i], &saction, NULL);
283 /* we handle write errors properly; ignore SIGPIPE */
284 saction.sa_handler = SIG_IGN;
285 sigaction(SIGPIPE, &saction, NULL);
287 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
288 signal(signals_catch[i], Signal_Handler);
290 signal(SIGPIPE, SIG_IGN);
292 return io_event_create(signalpipe[0], IO_WANTREAD,
294 } /* Initialize_Signal_Handler */
298 * Restores signals to their default behaviour.
300 * This should be called after a fork() in the new
301 * child prodcess, especially when we are about to call
302 * 3rd party code (e.g. PAM).
304 void Signals_Exit(void)
307 #ifdef HAVE_SIGACTION
308 struct sigaction saction;
310 memset(&saction, 0, sizeof(saction));
311 saction.sa_handler = SIG_DFL;
313 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
314 sigaction(signals_catch[i], &saction, NULL);
315 sigaction(SIGPIPE, &saction, NULL);
317 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
318 sigaction(signals_catch[i], &saction, NULL);
319 signal(SIGPIPE, SIG_DFL);
321 close(signalpipe[1]);
322 close(signalpipe[0]);