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 ---");
60 #ifdef HAVE_SIGPROCMASK
66 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 nick name 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 nick name 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);
115 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
117 if (old_nicklen != Conf_MaxNickLength) {
118 Conf_MaxNickLength = old_nicklen;
119 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
122 /* Create new pre-defined channels */
123 Channel_InitPredefined( );
125 if (!ConnSSL_InitLibrary())
126 Log(LOG_WARNING, "Re-Initializing SSL failed, using old keys");
128 /* Start listening on sockets */
129 Conn_InitListeners( );
131 /* Sync configuration with established connections */
132 Conn_SyncServerStruct( );
134 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
135 } /* NGIRCd_Rehash */
140 * Signal handler of ngIRCd.
141 * This function is called whenever ngIRCd catches a signal sent by the
142 * user and/or the system to it. For example SIGTERM and SIGHUP.
144 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
145 * @param Signal Number of the signal to handle.
148 Signal_Handler(int Signal)
154 /* shut down sever */
155 NGIRCd_SignalQuit = true;
160 /* child-process exited, avoid zombies */
161 while (waitpid( -1, NULL, WNOHANG) > 0)
166 if (! NGIRCd_Debug) {
167 Log(LOG_INFO|LOG_snotice,
168 "Got SIGUSR1, debug mode activated.");
170 strcpy(NGIRCd_DebugLevel, "2");
172 NGIRCd_Sniffer = true;
174 strcpy(NGIRCd_DebugLevel, "1");
178 Log(LOG_INFO|LOG_snotice,
179 "Got SIGUSR1, debug mode deactivated.");
180 strcpy(NGIRCd_DebugLevel, "");
181 NGIRCd_Debug = false;
183 NGIRCd_Sniffer = false;
191 * other signal: queue for later execution.
192 * This has the advantage that we are not restricted
193 * to functions that can be called safely from signal handlers.
195 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
196 Signal_Block(Signal);
197 } /* Signal_Handler */
201 * Signal processing handler of ngIRCd.
202 * This function is called from the main conn event loop in (io_dispatch)
203 * whenever ngIRCd has queued a signal.
205 * This function runs in normal context, not from the real signal handler,
206 * thus its not necessary to only use functions that are signal safe.
207 * @param Signal Number of the signal that was queued.
210 Signal_Handler_BH(int Signal)
214 /* re-read configuration */
223 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
226 Signal_Unblock(Signal);
231 Signal_Callback(int fd, short UNUSED what)
237 ret = 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", strerror(errno));
250 Log(LOG_EMERG, "EOF on signal pipe");
255 static const int signals_catch[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2 };
257 * Initialize the signal handlers, catch
258 * those signals we are interested in and sets SIGPIPE to be ignored.
259 * @return true if initialization was sucessful.
265 #ifdef HAVE_SIGACTION
266 struct sigaction saction;
269 if (pipe(signalpipe))
272 if (!io_setnonblock(signalpipe[0]) ||
273 !io_setnonblock(signalpipe[1]))
275 if (!io_setcloexec(signalpipe[0]) ||
276 !io_setcloexec(signalpipe[1]))
278 #ifdef HAVE_SIGACTION
279 memset( &saction, 0, sizeof( saction ));
280 saction.sa_handler = Signal_Handler;
282 saction.sa_flags |= SA_RESTART;
285 saction.sa_flags |= SA_NOCLDWAIT;
288 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
289 sigaction(signals_catch[i], &saction, NULL);
291 /* we handle write errors properly; ignore SIGPIPE */
292 saction.sa_handler = SIG_IGN;
293 sigaction(SIGPIPE, &saction, NULL);
295 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
296 signal(signals_catch[i], Signal_Handler);
298 signal(SIGPIPE, SIG_IGN);
300 return io_event_create(signalpipe[0], IO_WANTREAD, Signal_Callback);
305 * Restores signals to their default behaviour.
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 sigaction(signals_catch[i], &saction, NULL);
327 signal(SIGPIPE, SIG_DFL);
329 close(signalpipe[1]);
330 close(signalpipe[0]);