2 * Copyright (C) 2004, 2005, 2007-2009 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_result_t isc__app_ctxshutdown(isc_appctx_t *ctx);
94 ISC_APPFUNC_SCOPE isc_result_t isc__app_shutdown(void);
95 ISC_APPFUNC_SCOPE isc_result_t isc__app_reload(void);
96 ISC_APPFUNC_SCOPE isc_result_t isc__app_ctxsuspend(isc_appctx_t *ctx);
97 ISC_APPFUNC_SCOPE void isc__app_ctxfinish(isc_appctx_t *ctx);
98 ISC_APPFUNC_SCOPE void isc__app_finish(void);
99 ISC_APPFUNC_SCOPE void isc__app_block(void);
100 ISC_APPFUNC_SCOPE void isc__app_unblock(void);
101 ISC_APPFUNC_SCOPE isc_result_t isc__appctx_create(isc_mem_t *mctx,
102 isc_appctx_t **ctxp);
103 ISC_APPFUNC_SCOPE void isc__appctx_destroy(isc_appctx_t **ctxp);
104 ISC_APPFUNC_SCOPE void isc__appctx_settaskmgr(isc_appctx_t *ctx,
105 isc_taskmgr_t *taskmgr);
106 ISC_APPFUNC_SCOPE void isc__appctx_setsocketmgr(isc_appctx_t *ctx,
107 isc_socketmgr_t *socketmgr);
108 ISC_APPFUNC_SCOPE void isc__appctx_settimermgr(isc_appctx_t *ctx,
109 isc_timermgr_t *timermgr);
112 * The application context of this module. This implementation actually
113 * doesn't use it. (This may change in the future).
115 #define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
116 #define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
118 typedef struct isc__appctx {
122 isc_eventlist_t on_run;
123 isc_boolean_t shutdown_requested;
124 isc_boolean_t running;
127 * We assume that 'want_shutdown' can be read and written atomically.
129 isc_boolean_t want_shutdown;
131 * We assume that 'want_reload' can be read and written atomically.
133 isc_boolean_t want_reload;
135 isc_boolean_t blocked;
137 isc_taskmgr_t *taskmgr;
138 isc_socketmgr_t *socketmgr;
139 isc_timermgr_t *timermgr;
142 static isc__appctx_t isc_g_appctx;
145 isc_appmethods_t methods;
148 * The following are defined just for avoiding unused static functions.
151 void *run, *shutdown, *start, *onrun, *reload, *finish,
160 isc__app_ctxshutdown,
162 isc__appctx_settaskmgr,
163 isc__appctx_setsocketmgr,
164 isc__appctx_settimermgr
168 (void *)isc__app_run, (void *)isc__app_shutdown,
169 (void *)isc__app_start, (void *)isc__app_onrun, (void *)isc__app_reload,
170 (void *)isc__app_finish, (void *)isc__app_block,
171 (void *)isc__app_unblock
175 #ifdef HAVE_LINUXTHREADS
177 * Linux has sigwait(), but it appears to prevent signal handlers from
178 * running, even if they're not in the set being waited for. This makes
179 * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
180 * Instead of messing with it, we just use sigsuspend() instead.
184 * We need to remember which thread is the main thread...
186 static pthread_t main_thread;
191 exit_action(int arg) {
193 isc_g_appctx.want_shutdown = ISC_TRUE;
197 reload_action(int arg) {
199 isc_g_appctx.want_reload = ISC_TRUE;
204 handle_signal(int sig, void (*handler)(int)) {
206 char strbuf[ISC_STRERRORSIZE];
208 memset(&sa, 0, sizeof(sa));
209 sa.sa_handler = handler;
211 if (sigfillset(&sa.sa_mask) != 0 ||
212 sigaction(sig, &sa, NULL) < 0) {
213 isc__strerror(errno, strbuf, sizeof(strbuf));
214 UNEXPECTED_ERROR(__FILE__, __LINE__,
215 isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
217 "handle_signal() %d setup: %s"),
219 return (ISC_R_UNEXPECTED);
222 return (ISC_R_SUCCESS);
225 ISC_APPFUNC_SCOPE isc_result_t
226 isc__app_ctxstart(isc_appctx_t *ctx0) {
227 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
230 REQUIRE(VALID_APPCTX(ctx));
233 * Start an ISC library application.
236 #ifdef NEED_PTHREAD_INIT
238 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
240 presult = pthread_init();
242 isc__strerror(presult, strbuf, sizeof(strbuf));
243 UNEXPECTED_ERROR(__FILE__, __LINE__,
244 "isc_app_start() pthread_init: %s", strbuf);
245 return (ISC_R_UNEXPECTED);
249 #ifdef HAVE_LINUXTHREADS
250 main_thread = pthread_self();
253 result = isc_mutex_init(&ctx->lock);
254 if (result != ISC_R_SUCCESS)
257 ISC_LIST_INIT(ctx->on_run);
259 ctx->shutdown_requested = ISC_FALSE;
260 ctx->running = ISC_FALSE;
261 ctx->want_shutdown = ISC_FALSE;
262 ctx->want_reload = ISC_FALSE;
263 ctx->blocked = ISC_FALSE;
265 return (ISC_R_SUCCESS);
268 ISC_APPFUNC_SCOPE isc_result_t
269 isc__app_start(void) {
273 char strbuf[ISC_STRERRORSIZE];
275 isc_g_appctx.common.impmagic = APPCTX_MAGIC;
276 isc_g_appctx.common.magic = ISCAPI_APPCTX_MAGIC;
277 isc_g_appctx.common.methods = &appmethods.methods;
278 isc_g_appctx.mctx = NULL;
279 /* The remaining members will be initialized in ctxstart() */
281 result = isc__app_ctxstart((isc_appctx_t *)&isc_g_appctx);
282 if (result != ISC_R_SUCCESS)
287 * Install do-nothing handlers for SIGINT and SIGTERM.
289 * We install them now because BSDI 3.1 won't block
290 * the default actions, regardless of what we do with
293 result = handle_signal(SIGINT, exit_action);
294 if (result != ISC_R_SUCCESS)
296 result = handle_signal(SIGTERM, exit_action);
297 if (result != ISC_R_SUCCESS)
302 * Always ignore SIGPIPE.
304 result = handle_signal(SIGPIPE, SIG_IGN);
305 if (result != ISC_R_SUCCESS)
309 * On Solaris 2, delivery of a signal whose action is SIG_IGN
310 * will not cause sigwait() to return. We may have inherited
311 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
312 * process (e.g, Solaris cron). Set an action of SIG_DFL to make
313 * sure sigwait() works as expected. Only do this for SIGTERM and
314 * SIGINT if we don't have sigwait(), since a different handler is
317 result = handle_signal(SIGHUP, SIG_DFL);
318 if (result != ISC_R_SUCCESS)
322 result = handle_signal(SIGTERM, SIG_DFL);
323 if (result != ISC_R_SUCCESS)
325 result = handle_signal(SIGINT, SIG_DFL);
326 if (result != ISC_R_SUCCESS)
330 #ifdef ISC_PLATFORM_USETHREADS
332 * Block SIGHUP, SIGINT, SIGTERM.
334 * If isc_app_start() is called from the main thread before any other
335 * threads have been created, then the pthread_sigmask() call below
336 * will result in all threads having SIGHUP, SIGINT and SIGTERM
337 * blocked by default, ensuring that only the thread that calls
338 * sigwait() for them will get those signals.
340 if (sigemptyset(&sset) != 0 ||
341 sigaddset(&sset, SIGHUP) != 0 ||
342 sigaddset(&sset, SIGINT) != 0 ||
343 sigaddset(&sset, SIGTERM) != 0) {
344 isc__strerror(errno, strbuf, sizeof(strbuf));
345 UNEXPECTED_ERROR(__FILE__, __LINE__,
346 "isc_app_start() sigsetops: %s", strbuf);
347 return (ISC_R_UNEXPECTED);
349 presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
351 isc__strerror(presult, strbuf, sizeof(strbuf));
352 UNEXPECTED_ERROR(__FILE__, __LINE__,
353 "isc_app_start() pthread_sigmask: %s",
355 return (ISC_R_UNEXPECTED);
357 #else /* ISC_PLATFORM_USETHREADS */
359 * Unblock SIGHUP, SIGINT, SIGTERM.
361 * If we're not using threads, we need to make sure that SIGHUP,
362 * SIGINT and SIGTERM are not inherited as blocked from the parent
365 if (sigemptyset(&sset) != 0 ||
366 sigaddset(&sset, SIGHUP) != 0 ||
367 sigaddset(&sset, SIGINT) != 0 ||
368 sigaddset(&sset, SIGTERM) != 0) {
369 isc__strerror(errno, strbuf, sizeof(strbuf));
370 UNEXPECTED_ERROR(__FILE__, __LINE__,
371 "isc_app_start() sigsetops: %s", strbuf);
372 return (ISC_R_UNEXPECTED);
374 presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
376 isc__strerror(presult, strbuf, sizeof(strbuf));
377 UNEXPECTED_ERROR(__FILE__, __LINE__,
378 "isc_app_start() sigprocmask: %s", strbuf);
379 return (ISC_R_UNEXPECTED);
381 #endif /* ISC_PLATFORM_USETHREADS */
383 return (ISC_R_SUCCESS);
386 ISC_APPFUNC_SCOPE isc_result_t
387 isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
391 isc_task_t *cloned_task = NULL;
394 LOCK(&isc_g_appctx.lock);
396 if (isc_g_appctx.running) {
397 result = ISC_R_ALREADYRUNNING;
402 * Note that we store the task to which we're going to send the event
403 * in the event's "sender" field.
405 isc_task_attach(task, &cloned_task);
406 event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
407 action, arg, sizeof(*event));
409 result = ISC_R_NOMEMORY;
413 ISC_LIST_APPEND(isc_g_appctx.on_run, event, ev_link);
415 result = ISC_R_SUCCESS;
418 UNLOCK(&isc_g_appctx.lock);
423 #ifndef USE_THREADS_SINGLECTX
425 * Event loop for nonthreaded programs.
428 evloop(isc__appctx_t *ctx) {
431 while (!ctx->want_shutdown) {
433 isc_time_t when, now;
434 struct timeval tv, *tvp;
435 isc_socketwait_t *swait;
436 isc_boolean_t readytasks;
437 isc_boolean_t call_timer_dispatch = ISC_FALSE;
440 * Check the reload (or suspend) case first for exiting the
441 * loop as fast as possible in case:
442 * - the direct call to isc__taskmgr_dispatch() in
443 * isc__app_ctxrun() completes all the tasks so far,
444 * - there is thus currently no active task, and
445 * - there is a timer event
447 if (ctx->want_reload) {
448 ctx->want_reload = ISC_FALSE;
449 return (ISC_R_RELOAD);
452 readytasks = isc__taskmgr_ready(ctx->taskmgr);
457 call_timer_dispatch = ISC_TRUE;
459 result = isc__timermgr_nextevent(ctx->timermgr, &when);
460 if (result != ISC_R_SUCCESS)
466 us = isc_time_microdiff(&when, &now);
468 call_timer_dispatch = ISC_TRUE;
469 tv.tv_sec = us / 1000000;
470 tv.tv_usec = us % 1000000;
476 n = isc__socketmgr_waitevents(ctx->socketmgr, tvp, &swait);
478 if (n == 0 || call_timer_dispatch) {
480 * We call isc__timermgr_dispatch() only when
481 * necessary, in order to reduce overhead. If the
482 * select() call indicates a timeout, we need the
483 * dispatch. Even if not, if we set the 0-timeout
484 * for the select() call, we need to check the timer
485 * events. In the 'readytasks' case, there may be no
486 * timeout event actually, but there is no other way
487 * to reduce the overhead.
488 * Note that we do not have to worry about the case
489 * where a new timer is inserted during the select()
490 * call, since this loop only runs in the non-thread
493 isc__timermgr_dispatch(ctx->timermgr);
496 (void)isc__socketmgr_dispatch(ctx->socketmgr, swait);
497 (void)isc__taskmgr_dispatch(ctx->taskmgr);
499 return (ISC_R_SUCCESS);
501 #endif /* USE_THREADS_SINGLECTX */
503 #ifndef ISC_PLATFORM_USETHREADS
505 * This is a gross hack to support waiting for condition
506 * variables in nonthreaded programs in a limited way;
507 * see lib/isc/nothreads/include/isc/condition.h.
508 * We implement isc_condition_wait() by entering the
509 * event loop recursively until the want_shutdown flag
510 * is set by isc_condition_signal().
514 * \brief True if we are currently executing in the recursive
517 static isc_boolean_t in_recursive_evloop = ISC_FALSE;
520 * \brief True if we are exiting the event loop as the result of
521 * a call to isc_condition_signal() rather than a shutdown
524 static isc_boolean_t signalled = ISC_FALSE;
527 isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
533 INSIST(!in_recursive_evloop);
534 in_recursive_evloop = ISC_TRUE;
536 INSIST(*mp == 1); /* Mutex must be locked on entry. */
539 result = evloop(&isc_g_appctx);
540 if (result == ISC_R_RELOAD)
541 isc_g_appctx.want_reload = ISC_TRUE;
543 isc_g_appctx.want_shutdown = ISC_FALSE;
544 signalled = ISC_FALSE;
548 in_recursive_evloop = ISC_FALSE;
549 return (ISC_R_SUCCESS);
553 isc__nothread_signal_hack(isc_condition_t *cp) {
557 INSIST(in_recursive_evloop);
559 isc_g_appctx.want_shutdown = ISC_TRUE;
560 signalled = ISC_TRUE;
561 return (ISC_R_SUCCESS);
564 #endif /* ISC_PLATFORM_USETHREADS */
566 ISC_APPFUNC_SCOPE isc_result_t
567 isc__app_ctxrun(isc_appctx_t *ctx0) {
568 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
570 isc_event_t *event, *next_event;
572 #ifdef USE_THREADS_SINGLECTX
574 char strbuf[ISC_STRERRORSIZE];
578 #endif /* USE_THREADS_SINGLECTX */
580 REQUIRE(VALID_APPCTX(ctx));
582 #ifdef HAVE_LINUXTHREADS
583 REQUIRE(main_thread == pthread_self());
589 ctx->running = ISC_TRUE;
592 * Post any on-run events (in FIFO order).
594 for (event = ISC_LIST_HEAD(ctx->on_run);
596 event = next_event) {
597 next_event = ISC_LIST_NEXT(event, ev_link);
598 ISC_LIST_UNLINK(ctx->on_run, event, ev_link);
599 task = event->ev_sender;
600 event->ev_sender = NULL;
601 isc_task_sendanddetach(&task, &event);
612 * We do this here to ensure that the signal handler is installed
613 * (i.e. that it wasn't a "one-shot" handler).
615 if (ctx == &isc_g_appctx) {
616 result = handle_signal(SIGHUP, reload_action);
617 if (result != ISC_R_SUCCESS)
618 return (ISC_R_SUCCESS);
622 #ifdef USE_THREADS_SINGLECTX
624 * When we are using multiple contexts, we don't rely on signals.
626 if (ctx != &isc_g_appctx)
627 return (ISC_R_SUCCESS);
630 * There is no danger if isc_app_shutdown() is called before we wait
631 * for signals. Signals are blocked, so any such signal will simply
632 * be made pending and we will get it when we call sigwait().
635 while (!ctx->want_shutdown) {
638 * Wait for SIGHUP, SIGINT, or SIGTERM.
640 if (sigemptyset(&sset) != 0 ||
641 sigaddset(&sset, SIGHUP) != 0 ||
642 sigaddset(&sset, SIGINT) != 0 ||
643 sigaddset(&sset, SIGTERM) != 0) {
644 isc__strerror(errno, strbuf, sizeof(strbuf));
645 UNEXPECTED_ERROR(__FILE__, __LINE__,
646 "isc_app_run() sigsetops: %s", strbuf);
647 return (ISC_R_UNEXPECTED);
650 #ifndef HAVE_UNIXWARE_SIGWAIT
651 result = sigwait(&sset, &sig);
653 if (sig == SIGINT || sig == SIGTERM)
654 ctx->want_shutdown = ISC_TRUE;
655 else if (sig == SIGHUP)
656 ctx->want_reload = ISC_TRUE;
659 #else /* Using UnixWare sigwait semantics. */
660 sig = sigwait(&sset);
662 if (sig == SIGINT || sig == SIGTERM)
663 ctx->want_shutdown = ISC_TRUE;
664 else if (sig == SIGHUP)
665 ctx->want_reload = ISC_TRUE;
668 #endif /* HAVE_UNIXWARE_SIGWAIT */
669 #else /* Don't have sigwait(). */
671 * Listen for all signals.
673 if (sigemptyset(&sset) != 0) {
674 isc__strerror(errno, strbuf, sizeof(strbuf));
675 UNEXPECTED_ERROR(__FILE__, __LINE__,
676 "isc_app_run() sigsetops: %s",
678 return (ISC_R_UNEXPECTED);
680 result = sigsuspend(&sset);
681 #endif /* HAVE_SIGWAIT */
683 if (ctx->want_reload) {
684 ctx->want_reload = ISC_FALSE;
685 return (ISC_R_RELOAD);
688 if (ctx->want_shutdown && ctx->blocked)
692 #else /* USE_THREADS_SINGLECTX */
694 (void)isc__taskmgr_dispatch(ctx->taskmgr);
696 result = evloop(ctx);
697 if (result != ISC_R_SUCCESS)
700 #endif /* USE_THREADS_SINGLECTX */
702 return (ISC_R_SUCCESS);
705 ISC_APPFUNC_SCOPE isc_result_t
707 return (isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx));
710 ISC_APPFUNC_SCOPE isc_result_t
711 isc__app_ctxshutdown(isc_appctx_t *ctx0) {
712 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
713 isc_boolean_t want_kill = ISC_TRUE;
714 char strbuf[ISC_STRERRORSIZE];
716 REQUIRE(VALID_APPCTX(ctx));
720 REQUIRE(ctx->running);
722 if (ctx->shutdown_requested)
723 want_kill = ISC_FALSE;
725 ctx->shutdown_requested = ISC_TRUE;
730 if (ctx != &isc_g_appctx)
731 ctx->want_shutdown = ISC_TRUE;
733 #ifdef HAVE_LINUXTHREADS
736 result = pthread_kill(main_thread, SIGTERM);
738 isc__strerror(result, strbuf, sizeof(strbuf));
739 UNEXPECTED_ERROR(__FILE__, __LINE__,
740 "isc_app_shutdown() "
743 return (ISC_R_UNEXPECTED);
746 if (kill(getpid(), SIGTERM) < 0) {
747 isc__strerror(errno, strbuf, sizeof(strbuf));
748 UNEXPECTED_ERROR(__FILE__, __LINE__,
749 "isc_app_shutdown() "
751 return (ISC_R_UNEXPECTED);
753 #endif /* HAVE_LINUXTHREADS */
757 return (ISC_R_SUCCESS);
760 ISC_APPFUNC_SCOPE isc_result_t
761 isc__app_shutdown() {
762 return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
765 ISC_APPFUNC_SCOPE isc_result_t
766 isc__app_ctxsuspend(isc_appctx_t *ctx0) {
767 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
768 isc_boolean_t want_kill = ISC_TRUE;
769 char strbuf[ISC_STRERRORSIZE];
771 REQUIRE(VALID_APPCTX(ctx));
775 REQUIRE(ctx->running);
778 * Don't send the reload signal if we're shutting down.
780 if (ctx->shutdown_requested)
781 want_kill = ISC_FALSE;
786 if (ctx != &isc_g_appctx)
787 ctx->want_reload = ISC_TRUE;
789 #ifdef HAVE_LINUXTHREADS
792 result = pthread_kill(main_thread, SIGHUP);
794 isc__strerror(result, strbuf, sizeof(strbuf));
795 UNEXPECTED_ERROR(__FILE__, __LINE__,
799 return (ISC_R_UNEXPECTED);
802 if (kill(getpid(), SIGHUP) < 0) {
803 isc__strerror(errno, strbuf, sizeof(strbuf));
804 UNEXPECTED_ERROR(__FILE__, __LINE__,
807 return (ISC_R_UNEXPECTED);
813 return (ISC_R_SUCCESS);
816 ISC_APPFUNC_SCOPE isc_result_t
817 isc__app_reload(void) {
818 return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
821 ISC_APPFUNC_SCOPE void
822 isc__app_ctxfinish(isc_appctx_t *ctx0) {
823 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
825 REQUIRE(VALID_APPCTX(ctx));
827 DESTROYLOCK(&ctx->lock);
830 ISC_APPFUNC_SCOPE void
831 isc__app_finish(void) {
832 isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
835 ISC_APPFUNC_SCOPE void
836 isc__app_block(void) {
837 #ifdef ISC_PLATFORM_USETHREADS
839 #endif /* ISC_PLATFORM_USETHREADS */
840 REQUIRE(isc_g_appctx.running);
841 REQUIRE(!isc_g_appctx.blocked);
843 isc_g_appctx.blocked = ISC_TRUE;
844 #ifdef ISC_PLATFORM_USETHREADS
845 blockedthread = pthread_self();
846 RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
847 sigaddset(&sset, SIGINT) == 0 &&
848 sigaddset(&sset, SIGTERM) == 0);
849 RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
850 #endif /* ISC_PLATFORM_USETHREADS */
853 ISC_APPFUNC_SCOPE void
854 isc__app_unblock(void) {
855 #ifdef ISC_PLATFORM_USETHREADS
857 #endif /* ISC_PLATFORM_USETHREADS */
859 REQUIRE(isc_g_appctx.running);
860 REQUIRE(isc_g_appctx.blocked);
862 isc_g_appctx.blocked = ISC_FALSE;
864 #ifdef ISC_PLATFORM_USETHREADS
865 REQUIRE(blockedthread == pthread_self());
867 RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
868 sigaddset(&sset, SIGINT) == 0 &&
869 sigaddset(&sset, SIGTERM) == 0);
870 RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
871 #endif /* ISC_PLATFORM_USETHREADS */
874 ISC_APPFUNC_SCOPE isc_result_t
875 isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
878 REQUIRE(mctx != NULL);
879 REQUIRE(ctxp != NULL && *ctxp == NULL);
881 ctx = isc_mem_get(mctx, sizeof(*ctx));
883 return (ISC_R_NOMEMORY);
885 ctx->common.impmagic = APPCTX_MAGIC;
886 ctx->common.magic = ISCAPI_APPCTX_MAGIC;
887 ctx->common.methods = &appmethods.methods;
890 isc_mem_attach(mctx, &ctx->mctx);
893 ctx->socketmgr = NULL;
894 ctx->timermgr = NULL;
896 *ctxp = (isc_appctx_t *)ctx;
898 return (ISC_R_SUCCESS);
901 ISC_APPFUNC_SCOPE void
902 isc__appctx_destroy(isc_appctx_t **ctxp) {
905 REQUIRE(ctxp != NULL);
906 ctx = (isc__appctx_t *)*ctxp;
907 REQUIRE(VALID_APPCTX(ctx));
909 isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
914 ISC_APPFUNC_SCOPE void
915 isc__appctx_settaskmgr(isc_appctx_t *ctx0, isc_taskmgr_t *taskmgr) {
916 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
918 REQUIRE(VALID_APPCTX(ctx));
920 ctx->taskmgr = taskmgr;
923 ISC_APPFUNC_SCOPE void
924 isc__appctx_setsocketmgr(isc_appctx_t *ctx0, isc_socketmgr_t *socketmgr) {
925 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
927 REQUIRE(VALID_APPCTX(ctx));
929 ctx->socketmgr = socketmgr;
932 ISC_APPFUNC_SCOPE void
933 isc__appctx_settimermgr(isc_appctx_t *ctx0, isc_timermgr_t *timermgr) {
934 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
936 REQUIRE(VALID_APPCTX(ctx));
938 ctx->timermgr = timermgr;
941 #ifdef USE_APPIMPREGISTER
943 isc__app_register() {
944 return (isc_app_register(isc__appctx_create));