sighandlers.c 7.6 KB

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