/contrib/bind9/lib/isc/unix/app.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 946 lines · 632 code · 151 blank · 163 comment · 127 complexity · 762862a951790e9aa45fe691e0ef4bd4 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: app.c,v 1.64 2009/11/04 05:58:46 marka Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
  21. #include <sys/types.h>
  22. #include <stddef.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <signal.h>
  27. #include <sys/time.h>
  28. #ifdef HAVE_EPOLL
  29. #include <sys/epoll.h>
  30. #endif
  31. #include <isc/app.h>
  32. #include <isc/boolean.h>
  33. #include <isc/condition.h>
  34. #include <isc/mem.h>
  35. #include <isc/msgs.h>
  36. #include <isc/mutex.h>
  37. #include <isc/event.h>
  38. #include <isc/platform.h>
  39. #include <isc/strerror.h>
  40. #include <isc/string.h>
  41. #include <isc/task.h>
  42. #include <isc/time.h>
  43. #include <isc/util.h>
  44. /*%
  45. * For BIND9 internal applications built with threads, we use a single app
  46. * context and let multiple worker, I/O, timer threads do actual jobs.
  47. * For other cases (including BIND9 built without threads) an app context acts
  48. * as an event loop dispatching various events.
  49. */
  50. #if defined(ISC_PLATFORM_USETHREADS) && defined(BIND9)
  51. #define USE_THREADS_SINGLECTX
  52. #endif
  53. #ifdef ISC_PLATFORM_USETHREADS
  54. #include <pthread.h>
  55. #endif
  56. #ifndef USE_THREADS_SINGLECTX
  57. #include "../timer_p.h"
  58. #include "../task_p.h"
  59. #include "socket_p.h"
  60. #endif /* USE_THREADS_SINGLECTX */
  61. #ifdef ISC_PLATFORM_USETHREADS
  62. static pthread_t blockedthread;
  63. #endif /* ISC_PLATFORM_USETHREADS */
  64. /*%
  65. * The following can be either static or public, depending on build environment.
  66. */
  67. #ifdef BIND9
  68. #define ISC_APPFUNC_SCOPE
  69. #else
  70. #define ISC_APPFUNC_SCOPE static
  71. #endif
  72. ISC_APPFUNC_SCOPE isc_result_t isc__app_start(void);
  73. ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxstart(isc_appctx_t *ctx);
  74. ISC_APPFUNC_SCOPE isc_result_t isc__app_onrun(isc_mem_t *mctx,
  75. isc_task_t *task,
  76. isc_taskaction_t action,
  77. void *arg);
  78. ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxrun(isc_appctx_t *ctx);
  79. ISC_APPFUNC_SCOPE isc_result_t isc__app_run(void);
  80. ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxshutdown(isc_appctx_t *ctx);
  81. ISC_APPFUNC_SCOPE isc_result_t isc__app_shutdown(void);
  82. ISC_APPFUNC_SCOPE isc_result_t isc__app_reload(void);
  83. ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxsuspend(isc_appctx_t *ctx);
  84. ISC_APPFUNC_SCOPE void isc__app_ctxfinish(isc_appctx_t *ctx);
  85. ISC_APPFUNC_SCOPE void isc__app_finish(void);
  86. ISC_APPFUNC_SCOPE void isc__app_block(void);
  87. ISC_APPFUNC_SCOPE void isc__app_unblock(void);
  88. ISC_APPFUNC_SCOPE isc_result_t isc__appctx_create(isc_mem_t *mctx,
  89. isc_appctx_t **ctxp);
  90. ISC_APPFUNC_SCOPE void isc__appctx_destroy(isc_appctx_t **ctxp);
  91. ISC_APPFUNC_SCOPE void isc__appctx_settaskmgr(isc_appctx_t *ctx,
  92. isc_taskmgr_t *taskmgr);
  93. ISC_APPFUNC_SCOPE void isc__appctx_setsocketmgr(isc_appctx_t *ctx,
  94. isc_socketmgr_t *socketmgr);
  95. ISC_APPFUNC_SCOPE void isc__appctx_settimermgr(isc_appctx_t *ctx,
  96. isc_timermgr_t *timermgr);
  97. /*
  98. * The application context of this module. This implementation actually
  99. * doesn't use it. (This may change in the future).
  100. */
  101. #define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
  102. #define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
  103. typedef struct isc__appctx {
  104. isc_appctx_t common;
  105. isc_mem_t *mctx;
  106. isc_mutex_t lock;
  107. isc_eventlist_t on_run;
  108. isc_boolean_t shutdown_requested;
  109. isc_boolean_t running;
  110. /*!
  111. * We assume that 'want_shutdown' can be read and written atomically.
  112. */
  113. isc_boolean_t want_shutdown;
  114. /*
  115. * We assume that 'want_reload' can be read and written atomically.
  116. */
  117. isc_boolean_t want_reload;
  118. isc_boolean_t blocked;
  119. isc_taskmgr_t *taskmgr;
  120. isc_socketmgr_t *socketmgr;
  121. isc_timermgr_t *timermgr;
  122. } isc__appctx_t;
  123. static isc__appctx_t isc_g_appctx;
  124. static struct {
  125. isc_appmethods_t methods;
  126. /*%
  127. * The following are defined just for avoiding unused static functions.
  128. */
  129. #ifndef BIND9
  130. void *run, *shutdown, *start, *onrun, *reload, *finish,
  131. *block, *unblock;
  132. #endif
  133. } appmethods = {
  134. {
  135. isc__appctx_destroy,
  136. isc__app_ctxstart,
  137. isc__app_ctxrun,
  138. isc__app_ctxsuspend,
  139. isc__app_ctxshutdown,
  140. isc__app_ctxfinish,
  141. isc__appctx_settaskmgr,
  142. isc__appctx_setsocketmgr,
  143. isc__appctx_settimermgr
  144. }
  145. #ifndef BIND9
  146. ,
  147. (void *)isc__app_run, (void *)isc__app_shutdown,
  148. (void *)isc__app_start, (void *)isc__app_onrun, (void *)isc__app_reload,
  149. (void *)isc__app_finish, (void *)isc__app_block,
  150. (void *)isc__app_unblock
  151. #endif
  152. };
  153. #ifdef HAVE_LINUXTHREADS
  154. /*!
  155. * Linux has sigwait(), but it appears to prevent signal handlers from
  156. * running, even if they're not in the set being waited for. This makes
  157. * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
  158. * Instead of messing with it, we just use sigsuspend() instead.
  159. */
  160. #undef HAVE_SIGWAIT
  161. /*!
  162. * We need to remember which thread is the main thread...
  163. */
  164. static pthread_t main_thread;
  165. #endif
  166. #ifndef HAVE_SIGWAIT
  167. static void
  168. exit_action(int arg) {
  169. UNUSED(arg);
  170. isc_g_appctx.want_shutdown = ISC_TRUE;
  171. }
  172. static void
  173. reload_action(int arg) {
  174. UNUSED(arg);
  175. isc_g_appctx.want_reload = ISC_TRUE;
  176. }
  177. #endif
  178. static isc_result_t
  179. handle_signal(int sig, void (*handler)(int)) {
  180. struct sigaction sa;
  181. char strbuf[ISC_STRERRORSIZE];
  182. memset(&sa, 0, sizeof(sa));
  183. sa.sa_handler = handler;
  184. if (sigfillset(&sa.sa_mask) != 0 ||
  185. sigaction(sig, &sa, NULL) < 0) {
  186. isc__strerror(errno, strbuf, sizeof(strbuf));
  187. UNEXPECTED_ERROR(__FILE__, __LINE__,
  188. isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
  189. ISC_MSG_SIGNALSETUP,
  190. "handle_signal() %d setup: %s"),
  191. sig, strbuf);
  192. return (ISC_R_UNEXPECTED);
  193. }
  194. return (ISC_R_SUCCESS);
  195. }
  196. ISC_APPFUNC_SCOPE isc_result_t
  197. isc__app_ctxstart(isc_appctx_t *ctx0) {
  198. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  199. isc_result_t result;
  200. REQUIRE(VALID_APPCTX(ctx));
  201. /*
  202. * Start an ISC library application.
  203. */
  204. #ifdef NEED_PTHREAD_INIT
  205. /*
  206. * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
  207. */
  208. presult = pthread_init();
  209. if (presult != 0) {
  210. isc__strerror(presult, strbuf, sizeof(strbuf));
  211. UNEXPECTED_ERROR(__FILE__, __LINE__,
  212. "isc_app_start() pthread_init: %s", strbuf);
  213. return (ISC_R_UNEXPECTED);
  214. }
  215. #endif
  216. #ifdef HAVE_LINUXTHREADS
  217. main_thread = pthread_self();
  218. #endif
  219. result = isc_mutex_init(&ctx->lock);
  220. if (result != ISC_R_SUCCESS)
  221. return (result);
  222. ISC_LIST_INIT(ctx->on_run);
  223. ctx->shutdown_requested = ISC_FALSE;
  224. ctx->running = ISC_FALSE;
  225. ctx->want_shutdown = ISC_FALSE;
  226. ctx->want_reload = ISC_FALSE;
  227. ctx->blocked = ISC_FALSE;
  228. return (ISC_R_SUCCESS);
  229. }
  230. ISC_APPFUNC_SCOPE isc_result_t
  231. isc__app_start(void) {
  232. isc_result_t result;
  233. int presult;
  234. sigset_t sset;
  235. char strbuf[ISC_STRERRORSIZE];
  236. isc_g_appctx.common.impmagic = APPCTX_MAGIC;
  237. isc_g_appctx.common.magic = ISCAPI_APPCTX_MAGIC;
  238. isc_g_appctx.common.methods = &appmethods.methods;
  239. isc_g_appctx.mctx = NULL;
  240. /* The remaining members will be initialized in ctxstart() */
  241. result = isc__app_ctxstart((isc_appctx_t *)&isc_g_appctx);
  242. if (result != ISC_R_SUCCESS)
  243. return (result);
  244. #ifndef HAVE_SIGWAIT
  245. /*
  246. * Install do-nothing handlers for SIGINT and SIGTERM.
  247. *
  248. * We install them now because BSDI 3.1 won't block
  249. * the default actions, regardless of what we do with
  250. * pthread_sigmask().
  251. */
  252. result = handle_signal(SIGINT, exit_action);
  253. if (result != ISC_R_SUCCESS)
  254. return (result);
  255. result = handle_signal(SIGTERM, exit_action);
  256. if (result != ISC_R_SUCCESS)
  257. return (result);
  258. #endif
  259. /*
  260. * Always ignore SIGPIPE.
  261. */
  262. result = handle_signal(SIGPIPE, SIG_IGN);
  263. if (result != ISC_R_SUCCESS)
  264. return (result);
  265. /*
  266. * On Solaris 2, delivery of a signal whose action is SIG_IGN
  267. * will not cause sigwait() to return. We may have inherited
  268. * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
  269. * process (e.g, Solaris cron). Set an action of SIG_DFL to make
  270. * sure sigwait() works as expected. Only do this for SIGTERM and
  271. * SIGINT if we don't have sigwait(), since a different handler is
  272. * installed above.
  273. */
  274. result = handle_signal(SIGHUP, SIG_DFL);
  275. if (result != ISC_R_SUCCESS)
  276. return (result);
  277. #ifdef HAVE_SIGWAIT
  278. result = handle_signal(SIGTERM, SIG_DFL);
  279. if (result != ISC_R_SUCCESS)
  280. return (result);
  281. result = handle_signal(SIGINT, SIG_DFL);
  282. if (result != ISC_R_SUCCESS)
  283. return (result);
  284. #endif
  285. #ifdef ISC_PLATFORM_USETHREADS
  286. /*
  287. * Block SIGHUP, SIGINT, SIGTERM.
  288. *
  289. * If isc_app_start() is called from the main thread before any other
  290. * threads have been created, then the pthread_sigmask() call below
  291. * will result in all threads having SIGHUP, SIGINT and SIGTERM
  292. * blocked by default, ensuring that only the thread that calls
  293. * sigwait() for them will get those signals.
  294. */
  295. if (sigemptyset(&sset) != 0 ||
  296. sigaddset(&sset, SIGHUP) != 0 ||
  297. sigaddset(&sset, SIGINT) != 0 ||
  298. sigaddset(&sset, SIGTERM) != 0) {
  299. isc__strerror(errno, strbuf, sizeof(strbuf));
  300. UNEXPECTED_ERROR(__FILE__, __LINE__,
  301. "isc_app_start() sigsetops: %s", strbuf);
  302. return (ISC_R_UNEXPECTED);
  303. }
  304. presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
  305. if (presult != 0) {
  306. isc__strerror(presult, strbuf, sizeof(strbuf));
  307. UNEXPECTED_ERROR(__FILE__, __LINE__,
  308. "isc_app_start() pthread_sigmask: %s",
  309. strbuf);
  310. return (ISC_R_UNEXPECTED);
  311. }
  312. #else /* ISC_PLATFORM_USETHREADS */
  313. /*
  314. * Unblock SIGHUP, SIGINT, SIGTERM.
  315. *
  316. * If we're not using threads, we need to make sure that SIGHUP,
  317. * SIGINT and SIGTERM are not inherited as blocked from the parent
  318. * process.
  319. */
  320. if (sigemptyset(&sset) != 0 ||
  321. sigaddset(&sset, SIGHUP) != 0 ||
  322. sigaddset(&sset, SIGINT) != 0 ||
  323. sigaddset(&sset, SIGTERM) != 0) {
  324. isc__strerror(errno, strbuf, sizeof(strbuf));
  325. UNEXPECTED_ERROR(__FILE__, __LINE__,
  326. "isc_app_start() sigsetops: %s", strbuf);
  327. return (ISC_R_UNEXPECTED);
  328. }
  329. presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
  330. if (presult != 0) {
  331. isc__strerror(presult, strbuf, sizeof(strbuf));
  332. UNEXPECTED_ERROR(__FILE__, __LINE__,
  333. "isc_app_start() sigprocmask: %s", strbuf);
  334. return (ISC_R_UNEXPECTED);
  335. }
  336. #endif /* ISC_PLATFORM_USETHREADS */
  337. return (ISC_R_SUCCESS);
  338. }
  339. ISC_APPFUNC_SCOPE isc_result_t
  340. isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
  341. void *arg)
  342. {
  343. isc_event_t *event;
  344. isc_task_t *cloned_task = NULL;
  345. isc_result_t result;
  346. LOCK(&isc_g_appctx.lock);
  347. if (isc_g_appctx.running) {
  348. result = ISC_R_ALREADYRUNNING;
  349. goto unlock;
  350. }
  351. /*
  352. * Note that we store the task to which we're going to send the event
  353. * in the event's "sender" field.
  354. */
  355. isc_task_attach(task, &cloned_task);
  356. event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
  357. action, arg, sizeof(*event));
  358. if (event == NULL) {
  359. result = ISC_R_NOMEMORY;
  360. goto unlock;
  361. }
  362. ISC_LIST_APPEND(isc_g_appctx.on_run, event, ev_link);
  363. result = ISC_R_SUCCESS;
  364. unlock:
  365. UNLOCK(&isc_g_appctx.lock);
  366. return (result);
  367. }
  368. #ifndef USE_THREADS_SINGLECTX
  369. /*!
  370. * Event loop for nonthreaded programs.
  371. */
  372. static isc_result_t
  373. evloop(isc__appctx_t *ctx) {
  374. isc_result_t result;
  375. while (!ctx->want_shutdown) {
  376. int n;
  377. isc_time_t when, now;
  378. struct timeval tv, *tvp;
  379. isc_socketwait_t *swait;
  380. isc_boolean_t readytasks;
  381. isc_boolean_t call_timer_dispatch = ISC_FALSE;
  382. /*
  383. * Check the reload (or suspend) case first for exiting the
  384. * loop as fast as possible in case:
  385. * - the direct call to isc__taskmgr_dispatch() in
  386. * isc__app_ctxrun() completes all the tasks so far,
  387. * - there is thus currently no active task, and
  388. * - there is a timer event
  389. */
  390. if (ctx->want_reload) {
  391. ctx->want_reload = ISC_FALSE;
  392. return (ISC_R_RELOAD);
  393. }
  394. readytasks = isc__taskmgr_ready(ctx->taskmgr);
  395. if (readytasks) {
  396. tv.tv_sec = 0;
  397. tv.tv_usec = 0;
  398. tvp = &tv;
  399. call_timer_dispatch = ISC_TRUE;
  400. } else {
  401. result = isc__timermgr_nextevent(ctx->timermgr, &when);
  402. if (result != ISC_R_SUCCESS)
  403. tvp = NULL;
  404. else {
  405. isc_uint64_t us;
  406. TIME_NOW(&now);
  407. us = isc_time_microdiff(&when, &now);
  408. if (us == 0)
  409. call_timer_dispatch = ISC_TRUE;
  410. tv.tv_sec = us / 1000000;
  411. tv.tv_usec = us % 1000000;
  412. tvp = &tv;
  413. }
  414. }
  415. swait = NULL;
  416. n = isc__socketmgr_waitevents(ctx->socketmgr, tvp, &swait);
  417. if (n == 0 || call_timer_dispatch) {
  418. /*
  419. * We call isc__timermgr_dispatch() only when
  420. * necessary, in order to reduce overhead. If the
  421. * select() call indicates a timeout, we need the
  422. * dispatch. Even if not, if we set the 0-timeout
  423. * for the select() call, we need to check the timer
  424. * events. In the 'readytasks' case, there may be no
  425. * timeout event actually, but there is no other way
  426. * to reduce the overhead.
  427. * Note that we do not have to worry about the case
  428. * where a new timer is inserted during the select()
  429. * call, since this loop only runs in the non-thread
  430. * mode.
  431. */
  432. isc__timermgr_dispatch(ctx->timermgr);
  433. }
  434. if (n > 0)
  435. (void)isc__socketmgr_dispatch(ctx->socketmgr, swait);
  436. (void)isc__taskmgr_dispatch(ctx->taskmgr);
  437. }
  438. return (ISC_R_SUCCESS);
  439. }
  440. #endif /* USE_THREADS_SINGLECTX */
  441. #ifndef ISC_PLATFORM_USETHREADS
  442. /*
  443. * This is a gross hack to support waiting for condition
  444. * variables in nonthreaded programs in a limited way;
  445. * see lib/isc/nothreads/include/isc/condition.h.
  446. * We implement isc_condition_wait() by entering the
  447. * event loop recursively until the want_shutdown flag
  448. * is set by isc_condition_signal().
  449. */
  450. /*!
  451. * \brief True if we are currently executing in the recursive
  452. * event loop.
  453. */
  454. static isc_boolean_t in_recursive_evloop = ISC_FALSE;
  455. /*!
  456. * \brief True if we are exiting the event loop as the result of
  457. * a call to isc_condition_signal() rather than a shutdown
  458. * or reload.
  459. */
  460. static isc_boolean_t signalled = ISC_FALSE;
  461. isc_result_t
  462. isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
  463. isc_result_t result;
  464. UNUSED(cp);
  465. UNUSED(mp);
  466. INSIST(!in_recursive_evloop);
  467. in_recursive_evloop = ISC_TRUE;
  468. INSIST(*mp == 1); /* Mutex must be locked on entry. */
  469. --*mp;
  470. result = evloop(&isc_g_appctx);
  471. if (result == ISC_R_RELOAD)
  472. isc_g_appctx.want_reload = ISC_TRUE;
  473. if (signalled) {
  474. isc_g_appctx.want_shutdown = ISC_FALSE;
  475. signalled = ISC_FALSE;
  476. }
  477. ++*mp;
  478. in_recursive_evloop = ISC_FALSE;
  479. return (ISC_R_SUCCESS);
  480. }
  481. isc_result_t
  482. isc__nothread_signal_hack(isc_condition_t *cp) {
  483. UNUSED(cp);
  484. INSIST(in_recursive_evloop);
  485. isc_g_appctx.want_shutdown = ISC_TRUE;
  486. signalled = ISC_TRUE;
  487. return (ISC_R_SUCCESS);
  488. }
  489. #endif /* ISC_PLATFORM_USETHREADS */
  490. ISC_APPFUNC_SCOPE isc_result_t
  491. isc__app_ctxrun(isc_appctx_t *ctx0) {
  492. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  493. int result;
  494. isc_event_t *event, *next_event;
  495. isc_task_t *task;
  496. #ifdef USE_THREADS_SINGLECTX
  497. sigset_t sset;
  498. char strbuf[ISC_STRERRORSIZE];
  499. #ifdef HAVE_SIGWAIT
  500. int sig;
  501. #endif
  502. #endif /* USE_THREADS_SINGLECTX */
  503. REQUIRE(VALID_APPCTX(ctx));
  504. #ifdef HAVE_LINUXTHREADS
  505. REQUIRE(main_thread == pthread_self());
  506. #endif
  507. LOCK(&ctx->lock);
  508. if (!ctx->running) {
  509. ctx->running = ISC_TRUE;
  510. /*
  511. * Post any on-run events (in FIFO order).
  512. */
  513. for (event = ISC_LIST_HEAD(ctx->on_run);
  514. event != NULL;
  515. event = next_event) {
  516. next_event = ISC_LIST_NEXT(event, ev_link);
  517. ISC_LIST_UNLINK(ctx->on_run, event, ev_link);
  518. task = event->ev_sender;
  519. event->ev_sender = NULL;
  520. isc_task_sendanddetach(&task, &event);
  521. }
  522. }
  523. UNLOCK(&ctx->lock);
  524. #ifndef HAVE_SIGWAIT
  525. /*
  526. * Catch SIGHUP.
  527. *
  528. * We do this here to ensure that the signal handler is installed
  529. * (i.e. that it wasn't a "one-shot" handler).
  530. */
  531. if (ctx == &isc_g_appctx) {
  532. result = handle_signal(SIGHUP, reload_action);
  533. if (result != ISC_R_SUCCESS)
  534. return (ISC_R_SUCCESS);
  535. }
  536. #endif
  537. #ifdef USE_THREADS_SINGLECTX
  538. /*
  539. * When we are using multiple contexts, we don't rely on signals.
  540. */
  541. if (ctx != &isc_g_appctx)
  542. return (ISC_R_SUCCESS);
  543. /*
  544. * There is no danger if isc_app_shutdown() is called before we wait
  545. * for signals. Signals are blocked, so any such signal will simply
  546. * be made pending and we will get it when we call sigwait().
  547. */
  548. while (!ctx->want_shutdown) {
  549. #ifdef HAVE_SIGWAIT
  550. /*
  551. * Wait for SIGHUP, SIGINT, or SIGTERM.
  552. */
  553. if (sigemptyset(&sset) != 0 ||
  554. sigaddset(&sset, SIGHUP) != 0 ||
  555. sigaddset(&sset, SIGINT) != 0 ||
  556. sigaddset(&sset, SIGTERM) != 0) {
  557. isc__strerror(errno, strbuf, sizeof(strbuf));
  558. UNEXPECTED_ERROR(__FILE__, __LINE__,
  559. "isc_app_run() sigsetops: %s", strbuf);
  560. return (ISC_R_UNEXPECTED);
  561. }
  562. #ifndef HAVE_UNIXWARE_SIGWAIT
  563. result = sigwait(&sset, &sig);
  564. if (result == 0) {
  565. if (sig == SIGINT || sig == SIGTERM)
  566. ctx->want_shutdown = ISC_TRUE;
  567. else if (sig == SIGHUP)
  568. ctx->want_reload = ISC_TRUE;
  569. }
  570. #else /* Using UnixWare sigwait semantics. */
  571. sig = sigwait(&sset);
  572. if (sig >= 0) {
  573. if (sig == SIGINT || sig == SIGTERM)
  574. ctx->want_shutdown = ISC_TRUE;
  575. else if (sig == SIGHUP)
  576. ctx->want_reload = ISC_TRUE;
  577. }
  578. #endif /* HAVE_UNIXWARE_SIGWAIT */
  579. #else /* Don't have sigwait(). */
  580. /*
  581. * Listen for all signals.
  582. */
  583. if (sigemptyset(&sset) != 0) {
  584. isc__strerror(errno, strbuf, sizeof(strbuf));
  585. UNEXPECTED_ERROR(__FILE__, __LINE__,
  586. "isc_app_run() sigsetops: %s",
  587. strbuf);
  588. return (ISC_R_UNEXPECTED);
  589. }
  590. result = sigsuspend(&sset);
  591. #endif /* HAVE_SIGWAIT */
  592. if (ctx->want_reload) {
  593. ctx->want_reload = ISC_FALSE;
  594. return (ISC_R_RELOAD);
  595. }
  596. if (ctx->want_shutdown && ctx->blocked)
  597. exit(1);
  598. }
  599. #else /* USE_THREADS_SINGLECTX */
  600. (void)isc__taskmgr_dispatch(ctx->taskmgr);
  601. result = evloop(ctx);
  602. if (result != ISC_R_SUCCESS)
  603. return (result);
  604. #endif /* USE_THREADS_SINGLECTX */
  605. return (ISC_R_SUCCESS);
  606. }
  607. ISC_APPFUNC_SCOPE isc_result_t
  608. isc__app_run() {
  609. return (isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx));
  610. }
  611. ISC_APPFUNC_SCOPE isc_result_t
  612. isc__app_ctxshutdown(isc_appctx_t *ctx0) {
  613. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  614. isc_boolean_t want_kill = ISC_TRUE;
  615. char strbuf[ISC_STRERRORSIZE];
  616. REQUIRE(VALID_APPCTX(ctx));
  617. LOCK(&ctx->lock);
  618. REQUIRE(ctx->running);
  619. if (ctx->shutdown_requested)
  620. want_kill = ISC_FALSE;
  621. else
  622. ctx->shutdown_requested = ISC_TRUE;
  623. UNLOCK(&ctx->lock);
  624. if (want_kill) {
  625. if (ctx != &isc_g_appctx)
  626. ctx->want_shutdown = ISC_TRUE;
  627. else {
  628. #ifdef HAVE_LINUXTHREADS
  629. int result;
  630. result = pthread_kill(main_thread, SIGTERM);
  631. if (result != 0) {
  632. isc__strerror(result, strbuf, sizeof(strbuf));
  633. UNEXPECTED_ERROR(__FILE__, __LINE__,
  634. "isc_app_shutdown() "
  635. "pthread_kill: %s",
  636. strbuf);
  637. return (ISC_R_UNEXPECTED);
  638. }
  639. #else
  640. if (kill(getpid(), SIGTERM) < 0) {
  641. isc__strerror(errno, strbuf, sizeof(strbuf));
  642. UNEXPECTED_ERROR(__FILE__, __LINE__,
  643. "isc_app_shutdown() "
  644. "kill: %s", strbuf);
  645. return (ISC_R_UNEXPECTED);
  646. }
  647. #endif /* HAVE_LINUXTHREADS */
  648. }
  649. }
  650. return (ISC_R_SUCCESS);
  651. }
  652. ISC_APPFUNC_SCOPE isc_result_t
  653. isc__app_shutdown() {
  654. return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
  655. }
  656. ISC_APPFUNC_SCOPE isc_result_t
  657. isc__app_ctxsuspend(isc_appctx_t *ctx0) {
  658. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  659. isc_boolean_t want_kill = ISC_TRUE;
  660. char strbuf[ISC_STRERRORSIZE];
  661. REQUIRE(VALID_APPCTX(ctx));
  662. LOCK(&ctx->lock);
  663. REQUIRE(ctx->running);
  664. /*
  665. * Don't send the reload signal if we're shutting down.
  666. */
  667. if (ctx->shutdown_requested)
  668. want_kill = ISC_FALSE;
  669. UNLOCK(&ctx->lock);
  670. if (want_kill) {
  671. if (ctx != &isc_g_appctx)
  672. ctx->want_reload = ISC_TRUE;
  673. else {
  674. #ifdef HAVE_LINUXTHREADS
  675. int result;
  676. result = pthread_kill(main_thread, SIGHUP);
  677. if (result != 0) {
  678. isc__strerror(result, strbuf, sizeof(strbuf));
  679. UNEXPECTED_ERROR(__FILE__, __LINE__,
  680. "isc_app_reload() "
  681. "pthread_kill: %s",
  682. strbuf);
  683. return (ISC_R_UNEXPECTED);
  684. }
  685. #else
  686. if (kill(getpid(), SIGHUP) < 0) {
  687. isc__strerror(errno, strbuf, sizeof(strbuf));
  688. UNEXPECTED_ERROR(__FILE__, __LINE__,
  689. "isc_app_reload() "
  690. "kill: %s", strbuf);
  691. return (ISC_R_UNEXPECTED);
  692. }
  693. #endif
  694. }
  695. }
  696. return (ISC_R_SUCCESS);
  697. }
  698. ISC_APPFUNC_SCOPE isc_result_t
  699. isc__app_reload(void) {
  700. return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
  701. }
  702. ISC_APPFUNC_SCOPE void
  703. isc__app_ctxfinish(isc_appctx_t *ctx0) {
  704. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  705. REQUIRE(VALID_APPCTX(ctx));
  706. DESTROYLOCK(&ctx->lock);
  707. }
  708. ISC_APPFUNC_SCOPE void
  709. isc__app_finish(void) {
  710. isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
  711. }
  712. ISC_APPFUNC_SCOPE void
  713. isc__app_block(void) {
  714. #ifdef ISC_PLATFORM_USETHREADS
  715. sigset_t sset;
  716. #endif /* ISC_PLATFORM_USETHREADS */
  717. REQUIRE(isc_g_appctx.running);
  718. REQUIRE(!isc_g_appctx.blocked);
  719. isc_g_appctx.blocked = ISC_TRUE;
  720. #ifdef ISC_PLATFORM_USETHREADS
  721. blockedthread = pthread_self();
  722. RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
  723. sigaddset(&sset, SIGINT) == 0 &&
  724. sigaddset(&sset, SIGTERM) == 0);
  725. RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
  726. #endif /* ISC_PLATFORM_USETHREADS */
  727. }
  728. ISC_APPFUNC_SCOPE void
  729. isc__app_unblock(void) {
  730. #ifdef ISC_PLATFORM_USETHREADS
  731. sigset_t sset;
  732. #endif /* ISC_PLATFORM_USETHREADS */
  733. REQUIRE(isc_g_appctx.running);
  734. REQUIRE(isc_g_appctx.blocked);
  735. isc_g_appctx.blocked = ISC_FALSE;
  736. #ifdef ISC_PLATFORM_USETHREADS
  737. REQUIRE(blockedthread == pthread_self());
  738. RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
  739. sigaddset(&sset, SIGINT) == 0 &&
  740. sigaddset(&sset, SIGTERM) == 0);
  741. RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
  742. #endif /* ISC_PLATFORM_USETHREADS */
  743. }
  744. ISC_APPFUNC_SCOPE isc_result_t
  745. isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
  746. isc__appctx_t *ctx;
  747. REQUIRE(mctx != NULL);
  748. REQUIRE(ctxp != NULL && *ctxp == NULL);
  749. ctx = isc_mem_get(mctx, sizeof(*ctx));
  750. if (ctx == NULL)
  751. return (ISC_R_NOMEMORY);
  752. ctx->common.impmagic = APPCTX_MAGIC;
  753. ctx->common.magic = ISCAPI_APPCTX_MAGIC;
  754. ctx->common.methods = &appmethods.methods;
  755. ctx->mctx = NULL;
  756. isc_mem_attach(mctx, &ctx->mctx);
  757. ctx->taskmgr = NULL;
  758. ctx->socketmgr = NULL;
  759. ctx->timermgr = NULL;
  760. *ctxp = (isc_appctx_t *)ctx;
  761. return (ISC_R_SUCCESS);
  762. }
  763. ISC_APPFUNC_SCOPE void
  764. isc__appctx_destroy(isc_appctx_t **ctxp) {
  765. isc__appctx_t *ctx;
  766. REQUIRE(ctxp != NULL);
  767. ctx = (isc__appctx_t *)*ctxp;
  768. REQUIRE(VALID_APPCTX(ctx));
  769. isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
  770. *ctxp = NULL;
  771. }
  772. ISC_APPFUNC_SCOPE void
  773. isc__appctx_settaskmgr(isc_appctx_t *ctx0, isc_taskmgr_t *taskmgr) {
  774. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  775. REQUIRE(VALID_APPCTX(ctx));
  776. ctx->taskmgr = taskmgr;
  777. }
  778. ISC_APPFUNC_SCOPE void
  779. isc__appctx_setsocketmgr(isc_appctx_t *ctx0, isc_socketmgr_t *socketmgr) {
  780. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  781. REQUIRE(VALID_APPCTX(ctx));
  782. ctx->socketmgr = socketmgr;
  783. }
  784. ISC_APPFUNC_SCOPE void
  785. isc__appctx_settimermgr(isc_appctx_t *ctx0, isc_timermgr_t *timermgr) {
  786. isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
  787. REQUIRE(VALID_APPCTX(ctx));
  788. ctx->timermgr = timermgr;
  789. }
  790. #ifdef USE_APPIMPREGISTER
  791. isc_result_t
  792. isc__app_register() {
  793. return (isc_app_register(isc__appctx_create));
  794. }
  795. #endif