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));
51 Log(LOG_DEBUG, "--- End of state dump ---");
57 static void Signal_Block(int sig)
59 #ifdef HAVE_SIGPROCMASK
65 sigprocmask(SIG_BLOCK, &set, NULL);
72 static void Signal_Unblock(int sig)
74 #ifdef HAVE_SIGPROCMASK
80 sigprocmask(SIG_UNBLOCK, &set, NULL);
82 int old = sigblock(0) & ~sig;
88 * Reload the server configuration file.
93 char old_name[CLIENT_ID_LEN];
96 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
98 /* Remember old server name and nick name length */
99 strlcpy( old_name, Conf_ServerName, sizeof old_name );
100 old_nicklen = Conf_MaxNickLength;
102 /* Re-read configuration ... */
106 /* Close down all listening sockets */
107 Conn_ExitListeners( );
109 /* Recover old server name and nick name length: these values can't
110 * be changed during run-time */
111 if (strcmp(old_name, Conf_ServerName) != 0 ) {
112 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
113 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
115 if (old_nicklen != Conf_MaxNickLength) {
116 Conf_MaxNickLength = old_nicklen;
117 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
120 /* Create new pre-defined channels */
121 Channel_InitPredefined( );
123 if (!ConnSSL_InitLibrary())
124 Log(LOG_WARNING, "Re-Initializing SSL failed, using old keys");
126 /* Start listening on sockets */
127 Conn_InitListeners( );
129 /* Sync configuration with established connections */
130 Conn_SyncServerStruct( );
132 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
133 } /* NGIRCd_Rehash */
138 * Signal handler of ngIRCd.
139 * This function is called whenever ngIRCd catches a signal sent by the
140 * user and/or the system to it. For example SIGTERM and SIGHUP.
142 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
143 * @param Signal Number of the signal to handle.
145 static void Signal_Handler(int Signal)
151 /* shut down sever */
152 NGIRCd_SignalQuit = true;
157 /* child-process exited, avoid zombies */
158 while (waitpid( -1, NULL, WNOHANG) > 0)
163 if (! NGIRCd_Debug) {
164 Log(LOG_INFO|LOG_snotice,
165 "Got SIGUSR1, debug mode activated.");
167 strcpy(NGIRCd_DebugLevel, "2");
169 NGIRCd_Sniffer = true;
171 strcpy(NGIRCd_DebugLevel, "1");
175 Log(LOG_INFO|LOG_snotice,
176 "Got SIGUSR1, debug mode deactivated.");
177 strcpy(NGIRCd_DebugLevel, "");
178 NGIRCd_Debug = false;
180 NGIRCd_Sniffer = false;
188 * other signal: queue for later execution.
189 * This has the advantage that we are not restricted
190 * to functions that can be called safely from signal handlers.
192 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
193 Signal_Block(Signal);
194 } /* Signal_Handler */
198 * Signal processing handler of ngIRCd.
199 * This function is called from the main conn event loop in (io_dispatch)
200 * whenever ngIRCd has queued a signal.
202 * This function runs in normal context, not from the real signal handler,
203 * thus its not necessary to only use functions that are signal safe.
204 * @param Signal Number of the signal that was queued.
206 static void Signal_Handler_BH(int Signal)
210 /* re-read configuration */
219 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
222 Signal_Unblock(Signal);
225 static void Sig_callback(int fd, short UNUSED what)
231 ret = read(fd, &sig, sizeof(sig));
232 if (ret == sizeof(int))
233 Signal_Handler_BH(sig);
234 } while (ret == sizeof(int));
237 if (errno == EAGAIN || errno == EINTR)
240 Log(LOG_EMERG, "read from signal pipe: %s", strerror(errno));
244 Log(LOG_EMERG, "EOF on signal pipe");
249 static const int signals_catch[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2 };
251 * Initialize the signal handlers, catch
252 * those signals we are interested in and sets SIGPIPE to be ignored.
253 * @return true if initialization was sucessful.
255 bool Signals_Init(void)
258 #ifdef HAVE_SIGACTION
259 struct sigaction saction;
262 if (pipe(signalpipe))
265 if (!io_setnonblock(signalpipe[0]) ||
266 !io_setnonblock(signalpipe[1]))
268 if (!io_setcloexec(signalpipe[0]) ||
269 !io_setcloexec(signalpipe[1]))
271 #ifdef HAVE_SIGACTION
272 memset( &saction, 0, sizeof( saction ));
273 saction.sa_handler = Signal_Handler;
275 saction.sa_flags |= SA_RESTART;
278 saction.sa_flags |= SA_NOCLDWAIT;
281 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
282 sigaction(signals_catch[i], &saction, NULL);
284 /* we handle write errors properly; ignore SIGPIPE */
285 saction.sa_handler = SIG_IGN;
286 sigaction(SIGPIPE, &saction, NULL);
288 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
289 signal(signals_catch[i], Signal_Handler);
291 signal(SIGPIPE, SIG_IGN);
293 return io_event_create(signalpipe[0], IO_WANTREAD,
295 } /* Initialize_Signal_Handler */
299 * Restores signals to their default behaviour.
301 * This should be called after a fork() in the new
302 * child prodcess, especially when we are about to call
303 * 3rd party code (e.g. PAM).
305 void Signals_Exit(void)
308 #ifdef HAVE_SIGACTION
309 struct sigaction saction;
311 memset(&saction, 0, sizeof(saction));
312 saction.sa_handler = SIG_DFL;
314 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
315 sigaction(signals_catch[i], &saction, NULL);
316 sigaction(SIGPIPE, &saction, NULL);
318 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
319 sigaction(signals_catch[i], &saction, NULL);
320 signal(SIGPIPE, SIG_DFL);
322 close(signalpipe[1]);
323 close(signalpipe[0]);