2 * Copyright (C) 2004, 2005, 2007-2009, 2013-2015 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
18 /* $Id: app.c,v 1.64 2009/11/04 05:58:46 marka Exp $ */
24 #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
25 #include <sys/types.h>
34 #include <sys/epoll.h>
38 #include <isc/boolean.h>
39 #include <isc/condition.h>
42 #include <isc/mutex.h>
43 #include <isc/event.h>
44 #include <isc/platform.h>
45 #include <isc/strerror.h>
46 #include <isc/string.h>
52 * For BIND9 internal applications built with threads, we use a single app
53 * context and let multiple worker, I/O, timer threads do actual jobs.
54 * For other cases (including BIND9 built without threads) an app context acts
55 * as an event loop dispatching various events.
57 #if defined(ISC_PLATFORM_USETHREADS) && defined(BIND9)
58 #define USE_THREADS_SINGLECTX
61 #ifdef ISC_PLATFORM_USETHREADS
65 #ifndef USE_THREADS_SINGLECTX
66 #include "../timer_p.h"
67 #include "../task_p.h"
69 #endif /* USE_THREADS_SINGLECTX */
71 #ifdef ISC_PLATFORM_USETHREADS
72 static pthread_t blockedthread;
73 #endif /* ISC_PLATFORM_USETHREADS */
76 * The following can be either static or public, depending on build environment.
80 #define ISC_APPFUNC_SCOPE
82 #define ISC_APPFUNC_SCOPE static
85 ISC_APPFUNC_SCOPE isc_result_t isc__app_start(void);
86 ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxstart(isc_appctx_t *ctx);
87 ISC_APPFUNC_SCOPE isc_result_t isc__app_onrun(isc_mem_t *mctx,
89 isc_taskaction_t action,
91 ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxrun(isc_appctx_t *ctx);
92 ISC_APPFUNC_SCOPE isc_result_t isc__app_run(void);
93 ISC_APPFUNC_SCOPE isc_boolean_t isc__app_isrunning(void);
94 ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxshutdown(isc_appctx_t *ctx);
95 ISC_APPFUNC_SCOPE isc_result_t isc__app_shutdown(void);
96 ISC_APPFUNC_SCOPE isc_result_t isc__app_reload(void);
97 ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxsuspend(isc_appctx_t *ctx);
98 ISC_APPFUNC_SCOPE void isc__app_ctxfinish(isc_appctx_t *ctx);
99 ISC_APPFUNC_SCOPE void isc__app_finish(void);
100 ISC_APPFUNC_SCOPE void isc__app_block(void);
101 ISC_APPFUNC_SCOPE void isc__app_unblock(void);
102 ISC_APPFUNC_SCOPE isc_result_t isc__appctx_create(isc_mem_t *mctx,
103 isc_appctx_t **ctxp);
104 ISC_APPFUNC_SCOPE void isc__appctx_destroy(isc_appctx_t **ctxp);
105 ISC_APPFUNC_SCOPE void isc__appctx_settaskmgr(isc_appctx_t *ctx,
106 isc_taskmgr_t *taskmgr);
107 ISC_APPFUNC_SCOPE void isc__appctx_setsocketmgr(isc_appctx_t *ctx,
108 isc_socketmgr_t *socketmgr);
109 ISC_APPFUNC_SCOPE void isc__appctx_settimermgr(isc_appctx_t *ctx,
110 isc_timermgr_t *timermgr);
111 ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxonrun(isc_appctx_t *ctx,
114 isc_taskaction_t action,
118 * The application context of this module. This implementation actually
119 * doesn't use it. (This may change in the future).
121 #define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
122 #define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
124 typedef struct isc__appctx {
128 isc_eventlist_t on_run;
129 isc_boolean_t shutdown_requested;
130 isc_boolean_t running;
133 * We assume that 'want_shutdown' can be read and written atomically.
135 isc_boolean_t want_shutdown;
137 * We assume that 'want_reload' can be read and written atomically.
139 isc_boolean_t want_reload;
141 isc_boolean_t blocked;
143 isc_taskmgr_t *taskmgr;
144 isc_socketmgr_t *socketmgr;
145 isc_timermgr_t *timermgr;
148 static isc__appctx_t isc_g_appctx;
149 static isc_boolean_t is_running = ISC_FALSE;
152 isc_appmethods_t methods;
155 * The following are defined just for avoiding unused static functions.
158 void *run, *shutdown, *start, *onrun,
159 *reload, *finish, *block, *unblock,
168 isc__app_ctxshutdown,
170 isc__appctx_settaskmgr,
171 isc__appctx_setsocketmgr,
172 isc__appctx_settimermgr,
177 (void *)isc__app_run, (void *)isc__app_shutdown,
178 (void *)isc__app_start, (void *)isc__app_onrun, (void *)isc__app_reload,
179 (void *)isc__app_finish, (void *)isc__app_block,
180 (void *)isc__app_unblock, (void *)isc__app_isrunning
184 #ifdef HAVE_LINUXTHREADS
186 * Linux has sigwait(), but it appears to prevent signal handlers from
187 * running, even if they're not in the set being waited for. This makes
188 * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
189 * Instead of messing with it, we just use sigsuspend() instead.
193 * We need to remember which thread is the main thread...
195 static pthread_t main_thread;
200 exit_action(int arg) {
202 isc_g_appctx.want_shutdown = ISC_TRUE;
206 reload_action(int arg) {
208 isc_g_appctx.want_reload = ISC_TRUE;
213 handle_signal(int sig, void (*handler)(int)) {
215 char strbuf[ISC_STRERRORSIZE];
217 memset(&sa, 0, sizeof(sa));
218 sa.sa_handler = handler;
220 if (sigfillset(&sa.sa_mask) != 0 ||
221 sigaction(sig, &sa, NULL) < 0) {
222 isc__strerror(errno, strbuf, sizeof(strbuf));
223 UNEXPECTED_ERROR(__FILE__, __LINE__,
224 isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
226 "handle_signal() %d setup: %s"),
228 return (ISC_R_UNEXPECTED);
231 return (ISC_R_SUCCESS);
234 ISC_APPFUNC_SCOPE isc_result_t
235 isc__app_ctxstart(isc_appctx_t *ctx0) {
236 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
239 REQUIRE(VALID_APPCTX(ctx));
242 * Start an ISC library application.
245 #ifdef NEED_PTHREAD_INIT
247 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
249 presult = pthread_init();
251 isc__strerror(presult, strbuf, sizeof(strbuf));
252 UNEXPECTED_ERROR(__FILE__, __LINE__,
253 "isc_app_start() pthread_init: %s", strbuf);
254 return (ISC_R_UNEXPECTED);
258 #ifdef HAVE_LINUXTHREADS
259 main_thread = pthread_self();
262 result = isc_mutex_init(&ctx->lock);
263 if (result != ISC_R_SUCCESS)
266 ISC_LIST_INIT(ctx->on_run);
268 ctx->shutdown_requested = ISC_FALSE;
269 ctx->running = ISC_FALSE;
270 ctx->want_shutdown = ISC_FALSE;
271 ctx->want_reload = ISC_FALSE;
272 ctx->blocked = ISC_FALSE;
274 return (ISC_R_SUCCESS);
277 ISC_APPFUNC_SCOPE isc_result_t
278 isc__app_start(void) {
282 char strbuf[ISC_STRERRORSIZE];
284 isc_g_appctx.common.impmagic = APPCTX_MAGIC;
285 isc_g_appctx.common.magic = ISCAPI_APPCTX_MAGIC;
286 isc_g_appctx.common.methods = &appmethods.methods;
287 isc_g_appctx.mctx = NULL;
288 /* The remaining members will be initialized in ctxstart() */
290 result = isc__app_ctxstart((isc_appctx_t *)&isc_g_appctx);
291 if (result != ISC_R_SUCCESS)
296 * Install do-nothing handlers for SIGINT and SIGTERM.
298 * We install them now because BSDI 3.1 won't block
299 * the default actions, regardless of what we do with
302 result = handle_signal(SIGINT, exit_action);
303 if (result != ISC_R_SUCCESS)
305 result = handle_signal(SIGTERM, exit_action);
306 if (result != ISC_R_SUCCESS)
311 * Always ignore SIGPIPE.
313 result = handle_signal(SIGPIPE, SIG_IGN);
314 if (result != ISC_R_SUCCESS)
318 * On Solaris 2, delivery of a signal whose action is SIG_IGN
319 * will not cause sigwait() to return. We may have inherited
320 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
321 * process (e.g, Solaris cron). Set an action of SIG_DFL to make
322 * sure sigwait() works as expected. Only do this for SIGTERM and
323 * SIGINT if we don't have sigwait(), since a different handler is
326 result = handle_signal(SIGHUP, SIG_DFL);
327 if (result != ISC_R_SUCCESS)
331 result = handle_signal(SIGTERM, SIG_DFL);
332 if (result != ISC_R_SUCCESS)
334 result = handle_signal(SIGINT, SIG_DFL);
335 if (result != ISC_R_SUCCESS)
339 #ifdef ISC_PLATFORM_USETHREADS
341 * Block SIGHUP, SIGINT, SIGTERM.
343 * If isc_app_start() is called from the main thread before any other
344 * threads have been created, then the pthread_sigmask() call below
345 * will result in all threads having SIGHUP, SIGINT and SIGTERM
346 * blocked by default, ensuring that only the thread that calls
347 * sigwait() for them will get those signals.
349 if (sigemptyset(&sset) != 0 ||
350 sigaddset(&sset, SIGHUP) != 0 ||
351 sigaddset(&sset, SIGINT) != 0 ||
352 sigaddset(&sset, SIGTERM) != 0) {
353 isc__strerror(errno, strbuf, sizeof(strbuf));
354 UNEXPECTED_ERROR(__FILE__, __LINE__,
355 "isc_app_start() sigsetops: %s", strbuf);
356 return (ISC_R_UNEXPECTED);
358 presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
360 isc__strerror(presult, strbuf, sizeof(strbuf));
361 UNEXPECTED_ERROR(__FILE__, __LINE__,
362 "isc_app_start() pthread_sigmask: %s",
364 return (ISC_R_UNEXPECTED);
366 #else /* ISC_PLATFORM_USETHREADS */
368 * Unblock SIGHUP, SIGINT, SIGTERM.
370 * If we're not using threads, we need to make sure that SIGHUP,
371 * SIGINT and SIGTERM are not inherited as blocked from the parent
374 if (sigemptyset(&sset) != 0 ||
375 sigaddset(&sset, SIGHUP) != 0 ||
376 sigaddset(&sset, SIGINT) != 0 ||
377 sigaddset(&sset, SIGTERM) != 0) {
378 isc__strerror(errno, strbuf, sizeof(strbuf));
379 UNEXPECTED_ERROR(__FILE__, __LINE__,
380 "isc_app_start() sigsetops: %s", strbuf);
381 return (ISC_R_UNEXPECTED);
383 presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
385 isc__strerror(errno, strbuf, sizeof(strbuf));
386 UNEXPECTED_ERROR(__FILE__, __LINE__,
387 "isc_app_start() sigprocmask: %s", strbuf);
388 return (ISC_R_UNEXPECTED);
390 #endif /* ISC_PLATFORM_USETHREADS */
392 return (ISC_R_SUCCESS);
395 ISC_APPFUNC_SCOPE isc_result_t
396 isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
399 return (isc__app_ctxonrun((isc_appctx_t *)&isc_g_appctx, mctx,
404 isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
405 isc_taskaction_t action, void *arg)
407 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
409 isc_task_t *cloned_task = NULL;
415 result = ISC_R_ALREADYRUNNING;
420 * Note that we store the task to which we're going to send the event
421 * in the event's "sender" field.
423 isc_task_attach(task, &cloned_task);
424 event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
425 action, arg, sizeof(*event));
427 result = ISC_R_NOMEMORY;
431 ISC_LIST_APPEND(ctx->on_run, event, ev_link);
433 result = ISC_R_SUCCESS;
441 #ifndef USE_THREADS_SINGLECTX
443 * Event loop for nonthreaded programs.
446 evloop(isc__appctx_t *ctx) {
449 while (!ctx->want_shutdown) {
451 isc_time_t when, now;
452 struct timeval tv, *tvp;
453 isc_socketwait_t *swait;
454 isc_boolean_t readytasks;
455 isc_boolean_t call_timer_dispatch = ISC_FALSE;
458 * Check the reload (or suspend) case first for exiting the
459 * loop as fast as possible in case:
460 * - the direct call to isc__taskmgr_dispatch() in
461 * isc__app_ctxrun() completes all the tasks so far,
462 * - there is thus currently no active task, and
463 * - there is a timer event
465 if (ctx->want_reload) {
466 ctx->want_reload = ISC_FALSE;
467 return (ISC_R_RELOAD);
470 readytasks = isc__taskmgr_ready(ctx->taskmgr);
475 call_timer_dispatch = ISC_TRUE;
477 result = isc__timermgr_nextevent(ctx->timermgr, &when);
478 if (result != ISC_R_SUCCESS)
484 us = isc_time_microdiff(&when, &now);
486 call_timer_dispatch = ISC_TRUE;
487 tv.tv_sec = us / 1000000;
488 tv.tv_usec = us % 1000000;
494 n = isc__socketmgr_waitevents(ctx->socketmgr, tvp, &swait);
496 if (n == 0 || call_timer_dispatch) {
498 * We call isc__timermgr_dispatch() only when
499 * necessary, in order to reduce overhead. If the
500 * select() call indicates a timeout, we need the
501 * dispatch. Even if not, if we set the 0-timeout
502 * for the select() call, we need to check the timer
503 * events. In the 'readytasks' case, there may be no
504 * timeout event actually, but there is no other way
505 * to reduce the overhead.
506 * Note that we do not have to worry about the case
507 * where a new timer is inserted during the select()
508 * call, since this loop only runs in the non-thread
511 isc__timermgr_dispatch(ctx->timermgr);
514 (void)isc__socketmgr_dispatch(ctx->socketmgr, swait);
515 (void)isc__taskmgr_dispatch(ctx->taskmgr);
517 return (ISC_R_SUCCESS);
519 #endif /* USE_THREADS_SINGLECTX */
521 #ifndef ISC_PLATFORM_USETHREADS
523 * This is a gross hack to support waiting for condition
524 * variables in nonthreaded programs in a limited way;
525 * see lib/isc/nothreads/include/isc/condition.h.
526 * We implement isc_condition_wait() by entering the
527 * event loop recursively until the want_shutdown flag
528 * is set by isc_condition_signal().
532 * \brief True if we are currently executing in the recursive
535 static isc_boolean_t in_recursive_evloop = ISC_FALSE;
538 * \brief True if we are exiting the event loop as the result of
539 * a call to isc_condition_signal() rather than a shutdown
542 static isc_boolean_t signalled = ISC_FALSE;
545 isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
551 INSIST(!in_recursive_evloop);
552 in_recursive_evloop = ISC_TRUE;
554 INSIST(*mp == 1); /* Mutex must be locked on entry. */
557 result = evloop(&isc_g_appctx);
558 if (result == ISC_R_RELOAD)
559 isc_g_appctx.want_reload = ISC_TRUE;
561 isc_g_appctx.want_shutdown = ISC_FALSE;
562 signalled = ISC_FALSE;
566 in_recursive_evloop = ISC_FALSE;
567 return (ISC_R_SUCCESS);
571 isc__nothread_signal_hack(isc_condition_t *cp) {
575 INSIST(in_recursive_evloop);
577 isc_g_appctx.want_shutdown = ISC_TRUE;
578 signalled = ISC_TRUE;
579 return (ISC_R_SUCCESS);
582 #endif /* ISC_PLATFORM_USETHREADS */
584 ISC_APPFUNC_SCOPE isc_result_t
585 isc__app_ctxrun(isc_appctx_t *ctx0) {
586 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
588 isc_event_t *event, *next_event;
590 #ifdef USE_THREADS_SINGLECTX
592 char strbuf[ISC_STRERRORSIZE];
596 #endif /* USE_THREADS_SINGLECTX */
598 REQUIRE(VALID_APPCTX(ctx));
600 #ifdef HAVE_LINUXTHREADS
601 REQUIRE(main_thread == pthread_self());
607 ctx->running = ISC_TRUE;
610 * Post any on-run events (in FIFO order).
612 for (event = ISC_LIST_HEAD(ctx->on_run);
614 event = next_event) {
615 next_event = ISC_LIST_NEXT(event, ev_link);
616 ISC_LIST_UNLINK(ctx->on_run, event, ev_link);
617 task = event->ev_sender;
618 event->ev_sender = NULL;
619 isc_task_sendanddetach(&task, &event);
630 * We do this here to ensure that the signal handler is installed
631 * (i.e. that it wasn't a "one-shot" handler).
633 if (ctx == &isc_g_appctx) {
634 result = handle_signal(SIGHUP, reload_action);
635 if (result != ISC_R_SUCCESS)
636 return (ISC_R_SUCCESS);
640 #ifdef USE_THREADS_SINGLECTX
642 * When we are using multiple contexts, we don't rely on signals.
644 if (ctx != &isc_g_appctx)
645 return (ISC_R_SUCCESS);
648 * There is no danger if isc_app_shutdown() is called before we wait
649 * for signals. Signals are blocked, so any such signal will simply
650 * be made pending and we will get it when we call sigwait().
653 while (!ctx->want_shutdown) {
656 * Wait for SIGHUP, SIGINT, or SIGTERM.
658 if (sigemptyset(&sset) != 0 ||
659 sigaddset(&sset, SIGHUP) != 0 ||
660 sigaddset(&sset, SIGINT) != 0 ||
661 sigaddset(&sset, SIGTERM) != 0) {
662 isc__strerror(errno, strbuf, sizeof(strbuf));
663 UNEXPECTED_ERROR(__FILE__, __LINE__,
664 "isc_app_run() sigsetops: %s", strbuf);
665 return (ISC_R_UNEXPECTED);
668 #ifndef HAVE_UNIXWARE_SIGWAIT
669 result = sigwait(&sset, &sig);
671 if (sig == SIGINT || sig == SIGTERM)
672 ctx->want_shutdown = ISC_TRUE;
673 else if (sig == SIGHUP)
674 ctx->want_reload = ISC_TRUE;
677 #else /* Using UnixWare sigwait semantics. */
678 sig = sigwait(&sset);
680 if (sig == SIGINT || sig == SIGTERM)
681 ctx->want_shutdown = ISC_TRUE;
682 else if (sig == SIGHUP)
683 ctx->want_reload = ISC_TRUE;
686 #endif /* HAVE_UNIXWARE_SIGWAIT */
687 #else /* Don't have sigwait(). */
689 * Listen for all signals.
691 if (sigemptyset(&sset) != 0) {
692 isc__strerror(errno, strbuf, sizeof(strbuf));
693 UNEXPECTED_ERROR(__FILE__, __LINE__,
694 "isc_app_run() sigsetops: %s",
696 return (ISC_R_UNEXPECTED);
698 #ifdef HAVE_GPERFTOOLS_PROFILER
699 if (sigaddset(&sset, SIGALRM) != 0) {
700 isc__strerror(errno, strbuf, sizeof(strbuf));
701 UNEXPECTED_ERROR(__FILE__, __LINE__,
702 "isc_app_run() sigsetops: %s",
704 return (ISC_R_UNEXPECTED);
707 (void)sigsuspend(&sset);
708 #endif /* HAVE_SIGWAIT */
710 if (ctx->want_reload) {
711 ctx->want_reload = ISC_FALSE;
712 return (ISC_R_RELOAD);
715 if (ctx->want_shutdown && ctx->blocked)
719 #else /* USE_THREADS_SINGLECTX */
721 (void)isc__taskmgr_dispatch(ctx->taskmgr);
723 result = evloop(ctx);
724 if (result != ISC_R_SUCCESS)
727 #endif /* USE_THREADS_SINGLECTX */
729 return (ISC_R_SUCCESS);
732 ISC_APPFUNC_SCOPE isc_result_t
736 is_running = ISC_TRUE;
737 result = isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx);
738 is_running = ISC_FALSE;
743 ISC_APPFUNC_SCOPE isc_boolean_t
744 isc__app_isrunning(void) {
748 ISC_APPFUNC_SCOPE isc_result_t
749 isc__app_ctxshutdown(isc_appctx_t *ctx0) {
750 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
751 isc_boolean_t want_kill = ISC_TRUE;
752 char strbuf[ISC_STRERRORSIZE];
754 REQUIRE(VALID_APPCTX(ctx));
758 REQUIRE(ctx->running);
760 if (ctx->shutdown_requested)
761 want_kill = ISC_FALSE;
763 ctx->shutdown_requested = ISC_TRUE;
768 if (ctx != &isc_g_appctx)
769 ctx->want_shutdown = ISC_TRUE;
771 #ifdef HAVE_LINUXTHREADS
774 result = pthread_kill(main_thread, SIGTERM);
776 isc__strerror(result, strbuf, sizeof(strbuf));
777 UNEXPECTED_ERROR(__FILE__, __LINE__,
778 "isc_app_shutdown() "
781 return (ISC_R_UNEXPECTED);
784 if (kill(getpid(), SIGTERM) < 0) {
785 isc__strerror(errno, strbuf, sizeof(strbuf));
786 UNEXPECTED_ERROR(__FILE__, __LINE__,
787 "isc_app_shutdown() "
789 return (ISC_R_UNEXPECTED);
791 #endif /* HAVE_LINUXTHREADS */
795 return (ISC_R_SUCCESS);
798 ISC_APPFUNC_SCOPE isc_result_t
799 isc__app_shutdown(void) {
800 return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
803 ISC_APPFUNC_SCOPE isc_result_t
804 isc__app_ctxsuspend(isc_appctx_t *ctx0) {
805 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
806 isc_boolean_t want_kill = ISC_TRUE;
807 char strbuf[ISC_STRERRORSIZE];
809 REQUIRE(VALID_APPCTX(ctx));
813 REQUIRE(ctx->running);
816 * Don't send the reload signal if we're shutting down.
818 if (ctx->shutdown_requested)
819 want_kill = ISC_FALSE;
824 if (ctx != &isc_g_appctx)
825 ctx->want_reload = ISC_TRUE;
827 #ifdef HAVE_LINUXTHREADS
830 result = pthread_kill(main_thread, SIGHUP);
832 isc__strerror(result, strbuf, sizeof(strbuf));
833 UNEXPECTED_ERROR(__FILE__, __LINE__,
837 return (ISC_R_UNEXPECTED);
840 if (kill(getpid(), SIGHUP) < 0) {
841 isc__strerror(errno, strbuf, sizeof(strbuf));
842 UNEXPECTED_ERROR(__FILE__, __LINE__,
845 return (ISC_R_UNEXPECTED);
851 return (ISC_R_SUCCESS);
854 ISC_APPFUNC_SCOPE isc_result_t
855 isc__app_reload(void) {
856 return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
859 ISC_APPFUNC_SCOPE void
860 isc__app_ctxfinish(isc_appctx_t *ctx0) {
861 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
863 REQUIRE(VALID_APPCTX(ctx));
865 DESTROYLOCK(&ctx->lock);
868 ISC_APPFUNC_SCOPE void
869 isc__app_finish(void) {
870 isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
873 ISC_APPFUNC_SCOPE void
874 isc__app_block(void) {
875 #ifdef ISC_PLATFORM_USETHREADS
877 #endif /* ISC_PLATFORM_USETHREADS */
878 REQUIRE(isc_g_appctx.running);
879 REQUIRE(!isc_g_appctx.blocked);
881 isc_g_appctx.blocked = ISC_TRUE;
882 #ifdef ISC_PLATFORM_USETHREADS
883 blockedthread = pthread_self();
884 RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
885 sigaddset(&sset, SIGINT) == 0 &&
886 sigaddset(&sset, SIGTERM) == 0);
887 RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
888 #endif /* ISC_PLATFORM_USETHREADS */
891 ISC_APPFUNC_SCOPE void
892 isc__app_unblock(void) {
893 #ifdef ISC_PLATFORM_USETHREADS
895 #endif /* ISC_PLATFORM_USETHREADS */
897 REQUIRE(isc_g_appctx.running);
898 REQUIRE(isc_g_appctx.blocked);
900 isc_g_appctx.blocked = ISC_FALSE;
902 #ifdef ISC_PLATFORM_USETHREADS
903 REQUIRE(blockedthread == pthread_self());
905 RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
906 sigaddset(&sset, SIGINT) == 0 &&
907 sigaddset(&sset, SIGTERM) == 0);
908 RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
909 #endif /* ISC_PLATFORM_USETHREADS */
912 ISC_APPFUNC_SCOPE isc_result_t
913 isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
916 REQUIRE(mctx != NULL);
917 REQUIRE(ctxp != NULL && *ctxp == NULL);
919 ctx = isc_mem_get(mctx, sizeof(*ctx));
921 return (ISC_R_NOMEMORY);
923 ctx->common.impmagic = APPCTX_MAGIC;
924 ctx->common.magic = ISCAPI_APPCTX_MAGIC;
925 ctx->common.methods = &appmethods.methods;
928 isc_mem_attach(mctx, &ctx->mctx);
931 ctx->socketmgr = NULL;
932 ctx->timermgr = NULL;
934 *ctxp = (isc_appctx_t *)ctx;
936 return (ISC_R_SUCCESS);
939 ISC_APPFUNC_SCOPE void
940 isc__appctx_destroy(isc_appctx_t **ctxp) {
943 REQUIRE(ctxp != NULL);
944 ctx = (isc__appctx_t *)*ctxp;
945 REQUIRE(VALID_APPCTX(ctx));
947 isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
952 ISC_APPFUNC_SCOPE void
953 isc__appctx_settaskmgr(isc_appctx_t *ctx0, isc_taskmgr_t *taskmgr) {
954 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
956 REQUIRE(VALID_APPCTX(ctx));
958 ctx->taskmgr = taskmgr;
961 ISC_APPFUNC_SCOPE void
962 isc__appctx_setsocketmgr(isc_appctx_t *ctx0, isc_socketmgr_t *socketmgr) {
963 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
965 REQUIRE(VALID_APPCTX(ctx));
967 ctx->socketmgr = socketmgr;
970 ISC_APPFUNC_SCOPE void
971 isc__appctx_settimermgr(isc_appctx_t *ctx0, isc_timermgr_t *timermgr) {
972 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
974 REQUIRE(VALID_APPCTX(ctx));
976 ctx->timermgr = timermgr;
979 #ifdef USE_APPIMPREGISTER
981 isc__app_register(void) {
982 return (isc_app_register(isc__appctx_create));