sighandlers.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Signal Handlers: Actions to be performed when the program
  15. * receives a signal.
  16. */
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <signal.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <time.h>
  25. #include "conn.h"
  26. #include "channel.h"
  27. #include "conf.h"
  28. #include "io.h"
  29. #include "log.h"
  30. #include "ngircd.h"
  31. #include "sighandlers.h"
  32. static int signalpipe[2];
  33. static const int signals_catch[] = {
  34. SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2
  35. };
  36. #ifdef DEBUG
  37. static void
  38. Dump_State(void)
  39. {
  40. Log(LOG_DEBUG, "--- Internal server state: %s ---",
  41. Client_ID(Client_ThisServer()));
  42. Log(LOG_DEBUG, "time()=%ld", time(NULL));
  43. Conf_DebugDump();
  44. Conn_DebugDump();
  45. Client_DebugDump();
  46. Log(LOG_DEBUG, "--- End of state dump ---");
  47. } /* Dump_State */
  48. #endif
  49. static void
  50. Signal_Block(int sig)
  51. {
  52. #ifdef HAVE_SIGPROCMASK
  53. sigset_t set;
  54. sigemptyset(&set);
  55. sigaddset(&set, sig);
  56. sigprocmask(SIG_BLOCK, &set, NULL);
  57. #else
  58. sigblock(sig);
  59. #endif
  60. }
  61. static void
  62. Signal_Unblock(int sig)
  63. {
  64. #ifdef HAVE_SIGPROCMASK
  65. sigset_t set;
  66. sigemptyset(&set);
  67. sigaddset(&set, sig);
  68. sigprocmask(SIG_UNBLOCK, &set, NULL);
  69. #else
  70. int old = sigblock(0) & ~sig;
  71. sigsetmask(old);
  72. #endif
  73. }
  74. /**
  75. * Reload the server configuration file.
  76. */
  77. static void
  78. Rehash(void)
  79. {
  80. char old_name[CLIENT_ID_LEN];
  81. unsigned old_nicklen;
  82. Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
  83. /* Remember old server name and nickname length */
  84. strlcpy( old_name, Conf_ServerName, sizeof old_name );
  85. old_nicklen = Conf_MaxNickLength;
  86. /* Re-read configuration ... */
  87. if (!Conf_Rehash( ))
  88. return;
  89. /* Close down all listening sockets */
  90. Conn_ExitListeners( );
  91. /* Recover old server name and nickname length: these values can't
  92. * be changed during run-time */
  93. if (strcmp(old_name, Conf_ServerName) != 0 ) {
  94. strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
  95. Log(LOG_ERR,
  96. "Can't change \"ServerName\" on runtime! Ignored new name.");
  97. }
  98. if (old_nicklen != Conf_MaxNickLength) {
  99. Conf_MaxNickLength = old_nicklen;
  100. Log(LOG_ERR,
  101. "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
  102. }
  103. /* Create new pre-defined channels */
  104. Channel_InitPredefined( );
  105. if (!ConnSSL_InitLibrary())
  106. Log(LOG_WARNING,
  107. "Re-Initializing of SSL failed, using old keys!");
  108. /* Start listening on sockets */
  109. Conn_InitListeners( );
  110. /* Sync configuration with established connections */
  111. Conn_SyncServerStruct( );
  112. Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
  113. } /* Rehash */
  114. /**
  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.
  118. *
  119. * It blocks the signal and queues it for later execution by Signal_Handler_BH.
  120. * @param Signal Number of the signal to handle.
  121. */
  122. static void
  123. Signal_Handler(int Signal)
  124. {
  125. switch (Signal) {
  126. case SIGTERM:
  127. case SIGINT:
  128. case SIGQUIT:
  129. /* shut down sever */
  130. NGIRCd_SignalQuit = true;
  131. return;
  132. case SIGCHLD:
  133. /* child-process exited, avoid zombies */
  134. while (waitpid( -1, NULL, WNOHANG) > 0)
  135. ;
  136. return;
  137. #ifdef DEBUG
  138. case SIGUSR1:
  139. if (! NGIRCd_Debug) {
  140. Log(LOG_INFO|LOG_snotice,
  141. "Got SIGUSR1, debug mode activated.");
  142. #ifdef SNIFFER
  143. strcpy(NGIRCd_DebugLevel, "2");
  144. NGIRCd_Debug = true;
  145. NGIRCd_Sniffer = true;
  146. #else
  147. strcpy(NGIRCd_DebugLevel, "1");
  148. NGIRCd_Debug = true;
  149. #endif /* SNIFFER */
  150. } else {
  151. Log(LOG_INFO|LOG_snotice,
  152. "Got SIGUSR1, debug mode deactivated.");
  153. strcpy(NGIRCd_DebugLevel, "");
  154. NGIRCd_Debug = false;
  155. #ifdef SNIFFER
  156. NGIRCd_Sniffer = false;
  157. #endif /* SNIFFER */
  158. }
  159. return;
  160. #endif
  161. }
  162. /*
  163. * other signal: queue for later execution.
  164. * This has the advantage that we are not restricted
  165. * to functions that can be called safely from signal handlers.
  166. */
  167. if (write(signalpipe[1], &Signal, sizeof(Signal)) != -1)
  168. Signal_Block(Signal);
  169. } /* Signal_Handler */
  170. /**
  171. * Signal processing handler of ngIRCd.
  172. * This function is called from the main conn event loop in (io_dispatch)
  173. * whenever ngIRCd has queued a signal.
  174. *
  175. * This function runs in normal context, not from the real signal handler,
  176. * thus its not necessary to only use functions that are signal safe.
  177. * @param Signal Number of the signal that was queued.
  178. */
  179. static void
  180. Signal_Handler_BH(int Signal)
  181. {
  182. switch (Signal) {
  183. case SIGHUP:
  184. /* re-read configuration */
  185. Rehash();
  186. break;
  187. #ifdef DEBUG
  188. case SIGUSR2:
  189. if (NGIRCd_Debug) {
  190. Log(LOG_INFO|LOG_snotice,
  191. "Got SIGUSR2, dumping internal state ...");
  192. Dump_State();
  193. }
  194. break;
  195. default:
  196. Log(LOG_DEBUG, "Got signal %d! Ignored.", Signal);
  197. #endif
  198. }
  199. Signal_Unblock(Signal);
  200. }
  201. static void
  202. Signal_Callback(int fd, short UNUSED what)
  203. {
  204. int sig, ret;
  205. (void) what;
  206. do {
  207. ret = (int)read(fd, &sig, sizeof(sig));
  208. if (ret == sizeof(int))
  209. Signal_Handler_BH(sig);
  210. } while (ret == sizeof(int));
  211. if (ret == -1) {
  212. if (errno == EAGAIN || errno == EINTR)
  213. return;
  214. Log(LOG_EMERG, "Read from signal pipe: %s - Exiting!",
  215. strerror(errno));
  216. exit(1);
  217. }
  218. Log(LOG_EMERG, "EOF on signal pipe!? - Exiting!");
  219. exit(1);
  220. }
  221. /**
  222. * Initialize the signal handlers, catch
  223. * those signals we are interested in and sets SIGPIPE to be ignored.
  224. * @return true if initialization was successful.
  225. */
  226. bool
  227. Signals_Init(void)
  228. {
  229. size_t i;
  230. #ifdef HAVE_SIGACTION
  231. struct sigaction saction;
  232. #endif
  233. if (signalpipe[0] > 0 || signalpipe[1] > 0)
  234. return true;
  235. if (pipe(signalpipe))
  236. return false;
  237. if (!io_setnonblock(signalpipe[0]) ||
  238. !io_setnonblock(signalpipe[1]))
  239. return false;
  240. if (!io_setcloexec(signalpipe[0]) ||
  241. !io_setcloexec(signalpipe[1]))
  242. return false;
  243. #ifdef HAVE_SIGACTION
  244. memset( &saction, 0, sizeof( saction ));
  245. saction.sa_handler = Signal_Handler;
  246. #ifdef SA_RESTART
  247. saction.sa_flags |= SA_RESTART;
  248. #endif
  249. #ifdef SA_NOCLDWAIT
  250. saction.sa_flags |= SA_NOCLDWAIT;
  251. #endif
  252. for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
  253. sigaction(signals_catch[i], &saction, NULL);
  254. /* we handle write errors properly; ignore SIGPIPE */
  255. saction.sa_handler = SIG_IGN;
  256. sigaction(SIGPIPE, &saction, NULL);
  257. #else
  258. for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
  259. signal(signals_catch[i], Signal_Handler);
  260. signal(SIGPIPE, SIG_IGN);
  261. #endif
  262. return io_event_create(signalpipe[0], IO_WANTREAD, Signal_Callback);
  263. } /* Signals_Init */
  264. /**
  265. * Restores signals to their default behavior.
  266. *
  267. * This should be called after a fork() in the new
  268. * child prodcess, especially when we are about to call
  269. * 3rd party code (e.g. PAM).
  270. */
  271. void
  272. Signals_Exit(void)
  273. {
  274. size_t i;
  275. #ifdef HAVE_SIGACTION
  276. struct sigaction saction;
  277. memset(&saction, 0, sizeof(saction));
  278. saction.sa_handler = SIG_DFL;
  279. for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
  280. sigaction(signals_catch[i], &saction, NULL);
  281. sigaction(SIGPIPE, &saction, NULL);
  282. #else
  283. for (i=0; i < C_ARRAY_SIZE(signals_catch) ; i++)
  284. signal(signals_catch[i], SIG_DFL);
  285. signal(SIGPIPE, SIG_DFL);
  286. #endif
  287. close(signalpipe[1]);
  288. close(signalpipe[0]);
  289. signalpipe[0] = signalpipe[1] = 0;
  290. }
  291. /* -eof- */