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];
39 static void Signal_Block(int sig)
41 #ifdef HAVE_SIGPROCMASK
47 sigprocmask(SIG_BLOCK, &set, NULL);
52 static void Signal_Unblock(int sig)
54 #ifdef HAVE_SIGPROCMASK
60 sigprocmask(SIG_UNBLOCK, &set, NULL);
65 * Reload the server configuration file.
70 char old_name[CLIENT_ID_LEN];
73 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
75 /* Remember old server name and nick name length */
76 strlcpy( old_name, Conf_ServerName, sizeof old_name );
77 old_nicklen = Conf_MaxNickLength;
79 /* Re-read configuration ... */
83 /* Close down all listening sockets */
84 Conn_ExitListeners( );
86 /* Recover old server name and nick name length: these values can't
87 * be changed during run-time */
88 if (strcmp(old_name, Conf_ServerName) != 0 ) {
89 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
90 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
92 if (old_nicklen != Conf_MaxNickLength) {
93 Conf_MaxNickLength = old_nicklen;
94 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
97 /* Create new pre-defined channels */
98 Channel_InitPredefined( );
100 if (!ConnSSL_InitLibrary())
101 Log(LOG_WARNING, "Re-Initializing SSL failed, using old keys");
103 /* Start listening on sockets */
104 Conn_InitListeners( );
106 /* Sync configuration with established connections */
107 Conn_SyncServerStruct( );
109 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
110 } /* NGIRCd_Rehash */
115 * Signal handler of ngIRCd.
116 * This function is called whenever ngIRCd catches a signal sent by the
117 * user and/or the system to it. For example SIGTERM and SIGHUP.
119 * It blocks the signal and queues it for later execution by Signal_Handler_BH.
120 * @param Signal Number of the signal to handle.
122 static void Signal_Handler(int Signal)
128 /* shut down sever */
129 NGIRCd_SignalQuit = true;
134 /* child-process exited, avoid zombies */
135 while (waitpid( -1, NULL, WNOHANG) > 0)
141 * other signal: queue for later execution.
142 * This has the advantage that we are not restricted
143 * to functions that can be called safely from signal handlers.
145 if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
146 Signal_Block(Signal);
147 } /* Signal_Handler */
151 * Signal processing handler of ngIRCd.
152 * This function is called from the main conn event loop in (io_dispatch)
153 * whenever ngIRCd has queued a signal.
155 * This function runs in normal context, not from the real signal handler,
156 * thus its not necessary to only use functions that are signal safe.
157 * @param Signal Number of the signal that was queued.
159 static void Signal_Handler_BH(int Signal)
163 /* re-read configuration */
168 Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
171 Signal_Unblock(Signal);
174 static void Sig_callback(int fd, short UNUSED what)
180 ret = read(fd, &sig, sizeof(sig));
181 if (ret == sizeof(int))
182 Signal_Handler_BH(sig);
183 } while (ret == sizeof(int));
186 if (errno == EAGAIN || errno == EINTR)
189 Log(LOG_EMERG, "read from signal pipe: %s", strerror(errno));
193 Log(LOG_EMERG, "EOF on signal pipe");
198 static const int signals_catch[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2 };
200 * Initialize the signal handlers, catch
201 * those signals we are interested in and sets SIGPIPE to be ignored.
202 * @return true if initialization was sucessful.
204 bool Signals_Init(void)
207 #ifdef HAVE_SIGACTION
208 struct sigaction saction;
211 if (pipe(signalpipe))
214 if (!io_setnonblock(signalpipe[0]) ||
215 !io_setnonblock(signalpipe[1]))
217 if (!io_setcloexec(signalpipe[0]) ||
218 !io_setcloexec(signalpipe[1]))
220 #ifdef HAVE_SIGACTION
221 memset( &saction, 0, sizeof( saction ));
222 saction.sa_handler = Signal_Handler;
224 saction.sa_flags |= SA_RESTART;
227 saction.sa_flags |= SA_NOCLDWAIT;
230 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
231 sigaction(signals_catch[i], &saction, NULL);
233 /* we handle write errors properly; ignore SIGPIPE */
234 saction.sa_handler = SIG_IGN;
235 sigaction(SIGPIPE, &saction, NULL);
237 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
238 signal(signals_catch[i], Signal_Handler);
240 signal(SIGPIPE, SIG_IGN);
242 return io_event_create(signalpipe[0], IO_WANTREAD,
244 } /* Initialize_Signal_Handler */
248 * Restores signals to their default behaviour.
250 * This should be called after a fork() in the new
251 * child prodcess, especially when we are about to call
252 * 3rd party code (e.g. PAM).
254 void Signals_Exit(void)
257 #ifdef HAVE_SIGACTION
258 struct sigaction saction;
260 memset(&saction, 0, sizeof(saction));
261 saction.sa_handler = SIG_DFL;
263 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
264 sigaction(signals_catch[i], &saction, NULL);
265 sigaction(SIGPIPE, &saction, NULL);
267 for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
268 sigaction(signals_catch[i], &saction, NULL);
269 signal(SIGPIPE, SIG_DFL);
271 close(signalpipe[1]);
272 close(signalpipe[0]);