1 /* $NetBSD: app.c,v 1.13 2015/07/08 17:28:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007-2009, 2013-2015 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
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>
51 #ifdef ISC_PLATFORM_USETHREADS
56 * For BIND9 internal applications built with threads, we use a single app
57 * context and let multiple worker, I/O, timer threads do actual jobs.
58 * For other cases (including BIND9 built without threads) an app context acts
59 * as an event loop dispatching various events.
61 #ifndef ISC_PLATFORM_USETHREADS
62 #include "../timer_p.h"
63 #include "../task_p.h"
65 #endif /* ISC_PLATFORM_USETHREADS */
67 #ifdef ISC_PLATFORM_USETHREADS
68 static pthread_t blockedthread
;
69 #endif /* ISC_PLATFORM_USETHREADS */
72 * The following are intended for internal use (indicated by "isc__"
73 * prefix) but are not declared as static, allowing direct access from
76 isc_result_t
isc__app_start(void);
77 isc_result_t
isc__app_ctxstart(isc_appctx_t
*ctx
);
78 isc_result_t
isc__app_onrun(isc_mem_t
*mctx
, isc_task_t
*task
,
79 isc_taskaction_t action
, void *arg
);
80 isc_result_t
isc__app_ctxrun(isc_appctx_t
*ctx
);
81 isc_result_t
isc__app_run(void);
82 isc_result_t
isc__app_ctxshutdown(isc_appctx_t
*ctx
);
83 isc_result_t
isc__app_shutdown(void);
84 isc_result_t
isc__app_reload(void);
85 isc_result_t
isc__app_ctxsuspend(isc_appctx_t
*ctx
);
86 void isc__app_ctxfinish(isc_appctx_t
*ctx
);
87 void isc__app_finish(void);
88 void isc__app_block(void);
89 void isc__app_unblock(void);
90 isc_result_t
isc__appctx_create(isc_mem_t
*mctx
, isc_appctx_t
**ctxp
);
91 void isc__appctx_destroy(isc_appctx_t
**ctxp
);
92 void isc__appctx_settaskmgr(isc_appctx_t
*ctx
, isc_taskmgr_t
*taskmgr
);
93 void isc__appctx_setsocketmgr(isc_appctx_t
*ctx
, isc_socketmgr_t
*socketmgr
);
94 void isc__appctx_settimermgr(isc_appctx_t
*ctx
, isc_timermgr_t
*timermgr
);
95 isc_result_t
isc__app_ctxonrun(isc_appctx_t
*ctx
, isc_mem_t
*mctx
,
96 isc_task_t
*task
, isc_taskaction_t action
,
100 * The application context of this module. This implementation actually
101 * doesn't use it. (This may change in the future).
103 #define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
104 #define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
106 typedef struct isc__appctx
{
110 isc_eventlist_t on_run
;
111 isc_boolean_t shutdown_requested
;
112 isc_boolean_t running
;
115 * We assume that 'want_shutdown' can be read and written atomically.
117 isc_boolean_t want_shutdown
;
119 * We assume that 'want_reload' can be read and written atomically.
121 isc_boolean_t want_reload
;
123 isc_boolean_t blocked
;
125 isc_taskmgr_t
*taskmgr
;
126 isc_socketmgr_t
*socketmgr
;
127 isc_timermgr_t
*timermgr
;
128 #ifdef ISC_PLATFORM_USETHREADS
129 isc_mutex_t readylock
;
130 isc_condition_t ready
;
131 #endif /* ISC_PLATFORM_USETHREADS */
134 static isc__appctx_t isc_g_appctx
;
137 isc_appmethods_t methods
;
140 * The following are defined just for avoiding unused static functions.
142 void *run
, *shutdown
, *start
, *reload
, *finish
, *block
, *unblock
;
149 isc__app_ctxshutdown
,
151 isc__appctx_settaskmgr
,
152 isc__appctx_setsocketmgr
,
153 isc__appctx_settimermgr
,
156 (void *)isc__app_run
,
157 (void *)isc__app_shutdown
,
158 (void *)isc__app_start
,
159 (void *)isc__app_reload
,
160 (void *)isc__app_finish
,
161 (void *)isc__app_block
,
162 (void *)isc__app_unblock
165 #ifdef HAVE_LINUXTHREADS
167 * Linux has sigwait(), but it appears to prevent signal handlers from
168 * running, even if they're not in the set being waited for. This makes
169 * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
170 * Instead of messing with it, we just use sigsuspend() instead.
174 * We need to remember which thread is the main thread...
176 static pthread_t main_thread
;
181 exit_action(int arg
) {
183 isc_g_appctx
.want_shutdown
= ISC_TRUE
;
187 reload_action(int arg
) {
189 isc_g_appctx
.want_reload
= ISC_TRUE
;
194 handle_signal(int sig
, void (*handler
)(int)) {
196 char strbuf
[ISC_STRERRORSIZE
];
198 memset(&sa
, 0, sizeof(sa
));
199 sa
.sa_handler
= handler
;
201 if (sigfillset(&sa
.sa_mask
) != 0 ||
202 sigaction(sig
, &sa
, NULL
) < 0) {
203 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
204 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
205 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_APP
,
207 "handle_signal() %d setup: %s"),
209 return (ISC_R_UNEXPECTED
);
212 return (ISC_R_SUCCESS
);
216 isc__app_ctxstart(isc_appctx_t
*ctx0
) {
217 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
221 char strbuf
[ISC_STRERRORSIZE
];
223 REQUIRE(VALID_APPCTX(ctx
));
226 * Start an ISC library application.
229 #ifdef NEED_PTHREAD_INIT
231 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
233 presult
= pthread_init();
235 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
236 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
237 "isc_app_start() pthread_init: %s", strbuf
);
238 return (ISC_R_UNEXPECTED
);
242 #ifdef ISC_PLATFORM_USETHREADS
243 #ifdef HAVE_LINUXTHREADS
244 main_thread
= pthread_self();
245 #endif /* HAVE_LINUXTHREADS */
247 result
= isc_mutex_init(&ctx
->readylock
);
248 if (result
!= ISC_R_SUCCESS
)
251 result
= isc_condition_init(&ctx
->ready
);
252 if (result
!= ISC_R_SUCCESS
)
255 result
= isc_mutex_init(&ctx
->lock
);
256 if (result
!= ISC_R_SUCCESS
)
258 #else /* ISC_PLATFORM_USETHREADS */
259 result
= isc_mutex_init(&ctx
->lock
);
260 if (result
!= ISC_R_SUCCESS
)
262 #endif /* ISC_PLATFORM_USETHREADS */
264 ISC_LIST_INIT(ctx
->on_run
);
266 ctx
->shutdown_requested
= ISC_FALSE
;
267 ctx
->running
= ISC_FALSE
;
268 ctx
->want_shutdown
= ISC_FALSE
;
269 ctx
->want_reload
= ISC_FALSE
;
270 ctx
->blocked
= ISC_FALSE
;
274 * Install do-nothing handlers for SIGINT and SIGTERM.
276 * We install them now because BSDI 3.1 won't block
277 * the default actions, regardless of what we do with
280 result
= handle_signal(SIGINT
, exit_action
);
281 if (result
!= ISC_R_SUCCESS
)
283 result
= handle_signal(SIGTERM
, exit_action
);
284 if (result
!= ISC_R_SUCCESS
)
289 * Always ignore SIGPIPE.
291 result
= handle_signal(SIGPIPE
, SIG_IGN
);
292 if (result
!= ISC_R_SUCCESS
)
296 * On Solaris 2, delivery of a signal whose action is SIG_IGN
297 * will not cause sigwait() to return. We may have inherited
298 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
299 * process (e.g, Solaris cron). Set an action of SIG_DFL to make
300 * sure sigwait() works as expected. Only do this for SIGTERM and
301 * SIGINT if we don't have sigwait(), since a different handler is
304 result
= handle_signal(SIGHUP
, SIG_DFL
);
305 if (result
!= ISC_R_SUCCESS
)
309 result
= handle_signal(SIGTERM
, SIG_DFL
);
310 if (result
!= ISC_R_SUCCESS
)
312 result
= handle_signal(SIGINT
, SIG_DFL
);
313 if (result
!= ISC_R_SUCCESS
)
317 #ifdef ISC_PLATFORM_USETHREADS
320 * Block SIGHUP, SIGINT, SIGTERM.
322 * If isc_app_start() is called from the main thread before any other
323 * threads have been created, then the pthread_sigmask() call below
324 * will result in all threads having SIGHUP, SIGINT and SIGTERM
325 * blocked by default, ensuring that only the thread that calls
326 * sigwait() for them will get those signals.
328 if (sigemptyset(&sset
) != 0 ||
329 sigaddset(&sset
, SIGHUP
) != 0 ||
330 sigaddset(&sset
, SIGINT
) != 0 ||
331 sigaddset(&sset
, SIGTERM
) != 0) {
332 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
333 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
334 "isc_app_start() sigsetops: %s", strbuf
);
335 result
= ISC_R_UNEXPECTED
;
338 presult
= pthread_sigmask(SIG_BLOCK
, &sset
, NULL
);
340 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
341 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
342 "isc_app_start() pthread_sigmask: %s",
344 result
= ISC_R_UNEXPECTED
;
348 #else /* ISC_PLATFORM_USETHREADS */
350 * Unblock SIGHUP, SIGINT, SIGTERM.
352 * If we're not using threads, we need to make sure that SIGHUP,
353 * SIGINT and SIGTERM are not inherited as blocked from the parent
356 if (sigemptyset(&sset
) != 0 ||
357 sigaddset(&sset
, SIGHUP
) != 0 ||
358 sigaddset(&sset
, SIGINT
) != 0 ||
359 sigaddset(&sset
, SIGTERM
) != 0) {
360 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
361 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
362 "isc_app_start() sigsetops: %s", strbuf
);
363 result
= ISC_R_UNEXPECTED
;
366 presult
= sigprocmask(SIG_UNBLOCK
, &sset
, NULL
);
368 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
369 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
370 "isc_app_start() sigprocmask: %s", strbuf
);
371 result
= ISC_R_UNEXPECTED
;
374 #endif /* ISC_PLATFORM_USETHREADS */
376 return (ISC_R_SUCCESS
);
379 #ifdef ISC_PLATFORM_USETHREADS
381 (void)isc_condition_destroy(&ctx
->ready
);
384 (void)isc_mutex_destroy(&ctx
->readylock
);
385 #endif /* ISC_PLATFORM_USETHREADS */
390 isc__app_start(void) {
391 isc_g_appctx
.common
.impmagic
= APPCTX_MAGIC
;
392 isc_g_appctx
.common
.magic
= ISCAPI_APPCTX_MAGIC
;
393 isc_g_appctx
.common
.methods
= &appmethods
.methods
;
394 isc_g_appctx
.mctx
= NULL
;
395 /* The remaining members will be initialized in ctxstart() */
397 return (isc__app_ctxstart((isc_appctx_t
*)&isc_g_appctx
));
401 isc__app_onrun(isc_mem_t
*mctx
, isc_task_t
*task
, isc_taskaction_t action
,
404 return (isc__app_ctxonrun((isc_appctx_t
*)&isc_g_appctx
, mctx
,
409 isc__app_ctxonrun(isc_appctx_t
*ctx0
, isc_mem_t
*mctx
, isc_task_t
*task
,
410 isc_taskaction_t action
, void *arg
)
412 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
414 isc_task_t
*cloned_task
= NULL
;
420 result
= ISC_R_ALREADYRUNNING
;
425 * Note that we store the task to which we're going to send the event
426 * in the event's "sender" field.
428 isc_task_attach(task
, &cloned_task
);
429 event
= isc_event_allocate(mctx
, cloned_task
, ISC_APPEVENT_SHUTDOWN
,
430 action
, arg
, sizeof(*event
));
432 result
= ISC_R_NOMEMORY
;
436 ISC_LIST_APPEND(ctx
->on_run
, event
, ev_link
);
438 result
= ISC_R_SUCCESS
;
446 #ifndef ISC_PLATFORM_USETHREADS
448 * Event loop for nonthreaded programs.
451 evloop(isc__appctx_t
*ctx
) {
454 while (!ctx
->want_shutdown
) {
456 isc_time_t when
, now
;
457 struct timeval tv
, *tvp
;
458 isc_socketwait_t
*swait
;
459 isc_boolean_t readytasks
;
460 isc_boolean_t call_timer_dispatch
= ISC_FALSE
;
463 * Check the reload (or suspend) case first for exiting the
464 * loop as fast as possible in case:
465 * - the direct call to isc__taskmgr_dispatch() in
466 * isc__app_ctxrun() completes all the tasks so far,
467 * - there is thus currently no active task, and
468 * - there is a timer event
470 if (ctx
->want_reload
) {
471 ctx
->want_reload
= ISC_FALSE
;
472 return (ISC_R_RELOAD
);
475 readytasks
= isc__taskmgr_ready(ctx
->taskmgr
);
480 call_timer_dispatch
= ISC_TRUE
;
482 result
= isc__timermgr_nextevent(ctx
->timermgr
, &when
);
483 if (result
!= ISC_R_SUCCESS
)
489 us
= isc_time_microdiff(&when
, &now
);
491 call_timer_dispatch
= ISC_TRUE
;
492 tv
.tv_sec
= us
/ 1000000;
493 tv
.tv_usec
= us
% 1000000;
499 n
= isc__socketmgr_waitevents(ctx
->socketmgr
, tvp
, &swait
);
501 if (n
== 0 || call_timer_dispatch
) {
503 * We call isc__timermgr_dispatch() only when
504 * necessary, in order to reduce overhead. If the
505 * select() call indicates a timeout, we need the
506 * dispatch. Even if not, if we set the 0-timeout
507 * for the select() call, we need to check the timer
508 * events. In the 'readytasks' case, there may be no
509 * timeout event actually, but there is no other way
510 * to reduce the overhead.
511 * Note that we do not have to worry about the case
512 * where a new timer is inserted during the select()
513 * call, since this loop only runs in the non-thread
516 isc__timermgr_dispatch(ctx
->timermgr
);
519 (void)isc__socketmgr_dispatch(ctx
->socketmgr
, swait
);
520 (void)isc__taskmgr_dispatch(ctx
->taskmgr
);
522 return (ISC_R_SUCCESS
);
526 * This is a gross hack to support waiting for condition
527 * variables in nonthreaded programs in a limited way;
528 * see lib/isc/nothreads/include/isc/condition.h.
529 * We implement isc_condition_wait() by entering the
530 * event loop recursively until the want_shutdown flag
531 * is set by isc_condition_signal().
535 * \brief True if we are currently executing in the recursive
538 static isc_boolean_t in_recursive_evloop
= ISC_FALSE
;
541 * \brief True if we are exiting the event loop as the result of
542 * a call to isc_condition_signal() rather than a shutdown
545 static isc_boolean_t signalled
= ISC_FALSE
;
548 isc__nothread_wait_hack(isc_condition_t
*cp
, isc_mutex_t
*mp
) {
554 INSIST(!in_recursive_evloop
);
555 in_recursive_evloop
= ISC_TRUE
;
557 INSIST(*mp
== 1); /* Mutex must be locked on entry. */
560 result
= evloop(&isc_g_appctx
);
561 if (result
== ISC_R_RELOAD
)
562 isc_g_appctx
.want_reload
= ISC_TRUE
;
564 isc_g_appctx
.want_shutdown
= ISC_FALSE
;
565 signalled
= ISC_FALSE
;
569 in_recursive_evloop
= ISC_FALSE
;
570 return (ISC_R_SUCCESS
);
574 isc__nothread_signal_hack(isc_condition_t
*cp
) {
578 INSIST(in_recursive_evloop
);
580 isc_g_appctx
.want_shutdown
= ISC_TRUE
;
581 signalled
= ISC_TRUE
;
582 return (ISC_R_SUCCESS
);
584 #endif /* ISC_PLATFORM_USETHREADS */
587 isc__app_ctxrun(isc_appctx_t
*ctx0
) {
588 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
590 isc_event_t
*event
, *next_event
;
592 #ifdef ISC_PLATFORM_USETHREADS
594 char strbuf
[ISC_STRERRORSIZE
];
597 #endif /* HAVE_SIGWAIT */
598 #endif /* ISC_PLATFORM_USETHREADS */
600 REQUIRE(VALID_APPCTX(ctx
));
602 #ifdef HAVE_LINUXTHREADS
603 REQUIRE(main_thread
== pthread_self());
609 ctx
->running
= ISC_TRUE
;
612 * Post any on-run events (in FIFO order).
614 for (event
= ISC_LIST_HEAD(ctx
->on_run
);
616 event
= next_event
) {
617 next_event
= ISC_LIST_NEXT(event
, ev_link
);
618 ISC_LIST_UNLINK(ctx
->on_run
, event
, ev_link
);
619 task
= event
->ev_sender
;
620 event
->ev_sender
= NULL
;
621 isc_task_sendanddetach(&task
, &event
);
628 #ifndef ISC_PLATFORM_USETHREADS
629 if (isc_bind9
&& ctx
== &isc_g_appctx
) {
630 result
= handle_signal(SIGHUP
, reload_action
);
631 if (result
!= ISC_R_SUCCESS
)
632 return (ISC_R_SUCCESS
);
635 (void) isc__taskmgr_dispatch(ctx
->taskmgr
);
636 result
= evloop(ctx
);
638 #else /* ISC_PLATFORM_USETHREADS */
640 * BIND9 internal tools using multiple contexts do not
643 if (isc_bind9
&& ctx
!= &isc_g_appctx
)
644 return (ISC_R_SUCCESS
);
647 * There is no danger if isc_app_shutdown() is called before we
648 * wait for signals. Signals are blocked, so any such signal will
649 * simply be made pending and we will get it when we call
652 while (!ctx
->want_shutdown
) {
656 * BIND9 internal; single context:
657 * Wait for SIGHUP, SIGINT, or SIGTERM.
659 if (sigemptyset(&sset
) != 0 ||
660 sigaddset(&sset
, SIGHUP
) != 0 ||
661 sigaddset(&sset
, SIGINT
) != 0 ||
662 sigaddset(&sset
, SIGTERM
) != 0) {
663 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
664 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
665 "isc_app_run() sigsetops: %s",
667 return (ISC_R_UNEXPECTED
);
670 #ifndef HAVE_UNIXWARE_SIGWAIT
671 result
= sigwait(&sset
, &sig
);
673 if (sig
== SIGINT
|| sig
== SIGTERM
)
674 ctx
->want_shutdown
= ISC_TRUE
;
675 else if (sig
== SIGHUP
)
676 ctx
->want_reload
= ISC_TRUE
;
679 #else /* Using UnixWare sigwait semantics. */
680 sig
= sigwait(&sset
);
682 if (sig
== SIGINT
|| sig
== SIGTERM
)
683 ctx
->want_shutdown
= ISC_TRUE
;
684 else if (sig
== SIGHUP
)
685 ctx
->want_reload
= ISC_TRUE
;
687 #endif /* HAVE_UNIXWARE_SIGWAIT */
690 * External, or BIND9 using multiple contexts:
691 * wait until woken up.
693 LOCK(&ctx
->readylock
);
694 if (ctx
->want_shutdown
) {
695 /* shutdown() won the race. */
696 UNLOCK(&ctx
->readylock
);
699 if (!ctx
->want_reload
)
700 WAIT(&ctx
->ready
, &ctx
->readylock
);
701 UNLOCK(&ctx
->readylock
);
703 #else /* Don't have sigwait(). */
706 * BIND9 internal; single context:
707 * Install a signal handler for SIGHUP, then wait for
710 result
= handle_signal(SIGHUP
, reload_action
);
711 if (result
!= ISC_R_SUCCESS
)
712 return (ISC_R_SUCCESS
);
714 if (sigemptyset(&sset
) != 0) {
715 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
716 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
717 "isc_app_run() sigsetops: %s",
719 return (ISC_R_UNEXPECTED
);
721 #ifdef HAVE_GPERFTOOLS_PROFILER
722 if (sigaddset(&sset
, SIGALRM
) != 0) {
723 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
724 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
725 "isc_app_run() sigsetops: %s",
727 return (ISC_R_UNEXPECTED
);
730 result
= sigsuspend(&sset
);
733 * External, or BIND9 using multiple contexts:
734 * wait until woken up.
736 LOCK(&ctx
->readylock
);
737 if (ctx
->want_shutdown
) {
738 /* shutdown() won the race. */
739 UNLOCK(&ctx
->readylock
);
742 if (!ctx
->want_reload
)
743 WAIT(&ctx
->ready
, &ctx
->readylock
);
744 UNLOCK(&ctx
->readylock
);
746 #endif /* HAVE_SIGWAIT */
748 if (ctx
->want_reload
) {
749 ctx
->want_reload
= ISC_FALSE
;
750 return (ISC_R_RELOAD
);
753 if (ctx
->want_shutdown
&& ctx
->blocked
)
757 return (ISC_R_SUCCESS
);
758 #endif /* ISC_PLATFORM_USETHREADS */
763 return (isc__app_ctxrun((isc_appctx_t
*)&isc_g_appctx
));
767 isc__app_ctxshutdown(isc_appctx_t
*ctx0
) {
768 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
769 isc_boolean_t want_kill
= ISC_TRUE
;
770 #ifdef ISC_PLATFORM_USETHREADS
771 char strbuf
[ISC_STRERRORSIZE
];
772 #endif /* ISC_PLATFORM_USETHREADS */
774 REQUIRE(VALID_APPCTX(ctx
));
778 REQUIRE(ctx
->running
);
780 if (ctx
->shutdown_requested
)
781 want_kill
= ISC_FALSE
;
783 ctx
->shutdown_requested
= ISC_TRUE
;
788 if (isc_bind9
&& ctx
!= &isc_g_appctx
)
789 /* BIND9 internal, but using multiple contexts */
790 ctx
->want_shutdown
= ISC_TRUE
;
792 #ifndef ISC_PLATFORM_USETHREADS
793 ctx
->want_shutdown
= ISC_TRUE
;
794 #else /* ISC_PLATFORM_USETHREADS */
795 #ifdef HAVE_LINUXTHREADS
797 /* BIND9 internal, single context */
800 result
= pthread_kill(main_thread
, SIGTERM
);
802 isc__strerror(result
,
803 strbuf
, sizeof(strbuf
));
804 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
805 "isc_app_shutdown() "
808 return (ISC_R_UNEXPECTED
);
813 /* BIND9 internal, single context */
814 if (kill(getpid(), SIGTERM
) < 0) {
816 strbuf
, sizeof(strbuf
));
817 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
818 "isc_app_shutdown() "
820 return (ISC_R_UNEXPECTED
);
823 #endif /* HAVE_LINUXTHREADS */
825 /* External, multiple contexts */
826 LOCK(&ctx
->readylock
);
827 ctx
->want_shutdown
= ISC_TRUE
;
828 UNLOCK(&ctx
->readylock
);
831 #endif /* ISC_PLATFORM_USETHREADS */
835 return (ISC_R_SUCCESS
);
839 isc__app_shutdown(void) {
840 return (isc__app_ctxshutdown((isc_appctx_t
*)&isc_g_appctx
));
844 isc__app_ctxsuspend(isc_appctx_t
*ctx0
) {
845 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
846 isc_boolean_t want_kill
= ISC_TRUE
;
847 #ifdef ISC_PLATFORM_USETHREADS
848 char strbuf
[ISC_STRERRORSIZE
];
851 REQUIRE(VALID_APPCTX(ctx
));
855 REQUIRE(ctx
->running
);
858 * Don't send the reload signal if we're shutting down.
860 if (ctx
->shutdown_requested
)
861 want_kill
= ISC_FALSE
;
866 if (isc_bind9
&& ctx
!= &isc_g_appctx
)
867 /* BIND9 internal, but using multiple contexts */
868 ctx
->want_reload
= ISC_TRUE
;
870 #ifndef ISC_PLATFORM_USETHREADS
871 ctx
->want_reload
= ISC_TRUE
;
872 #else /* ISC_PLATFORM_USETHREADS */
873 #ifdef HAVE_LINUXTHREADS
875 /* BIND9 internal, single context */
878 result
= pthread_kill(main_thread
, SIGHUP
);
880 isc__strerror(result
,
881 strbuf
, sizeof(strbuf
));
882 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
886 return (ISC_R_UNEXPECTED
);
891 /* BIND9 internal, single context */
892 if (kill(getpid(), SIGHUP
) < 0) {
894 strbuf
, sizeof(strbuf
));
895 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
898 return (ISC_R_UNEXPECTED
);
901 #endif /* HAVE_LINUXTHREADS */
903 /* External, multiple contexts */
904 LOCK(&ctx
->readylock
);
905 ctx
->want_reload
= ISC_TRUE
;
906 UNLOCK(&ctx
->readylock
);
909 #endif /* ISC_PLATFORM_USETHREADS */
913 return (ISC_R_SUCCESS
);
917 isc__app_reload(void) {
918 return (isc__app_ctxsuspend((isc_appctx_t
*)&isc_g_appctx
));
922 isc__app_ctxfinish(isc_appctx_t
*ctx0
) {
923 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
925 REQUIRE(VALID_APPCTX(ctx
));
927 DESTROYLOCK(&ctx
->lock
);
931 isc__app_finish(void) {
932 isc__app_ctxfinish((isc_appctx_t
*)&isc_g_appctx
);
936 isc__app_block(void) {
937 #ifdef ISC_PLATFORM_USETHREADS
939 #endif /* ISC_PLATFORM_USETHREADS */
940 REQUIRE(isc_g_appctx
.running
);
941 REQUIRE(!isc_g_appctx
.blocked
);
943 isc_g_appctx
.blocked
= ISC_TRUE
;
944 #ifdef ISC_PLATFORM_USETHREADS
945 blockedthread
= pthread_self();
946 RUNTIME_CHECK(sigemptyset(&sset
) == 0 &&
947 sigaddset(&sset
, SIGINT
) == 0 &&
948 sigaddset(&sset
, SIGTERM
) == 0);
949 RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK
, &sset
, NULL
) == 0);
950 #endif /* ISC_PLATFORM_USETHREADS */
954 isc__app_unblock(void) {
955 #ifdef ISC_PLATFORM_USETHREADS
957 #endif /* ISC_PLATFORM_USETHREADS */
959 REQUIRE(isc_g_appctx
.running
);
960 REQUIRE(isc_g_appctx
.blocked
);
962 isc_g_appctx
.blocked
= ISC_FALSE
;
964 #ifdef ISC_PLATFORM_USETHREADS
965 REQUIRE(blockedthread
== pthread_self());
967 RUNTIME_CHECK(sigemptyset(&sset
) == 0 &&
968 sigaddset(&sset
, SIGINT
) == 0 &&
969 sigaddset(&sset
, SIGTERM
) == 0);
970 RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK
, &sset
, NULL
) == 0);
971 #endif /* ISC_PLATFORM_USETHREADS */
975 isc__appctx_create(isc_mem_t
*mctx
, isc_appctx_t
**ctxp
) {
978 REQUIRE(mctx
!= NULL
);
979 REQUIRE(ctxp
!= NULL
&& *ctxp
== NULL
);
981 ctx
= isc_mem_get(mctx
, sizeof(*ctx
));
983 return (ISC_R_NOMEMORY
);
985 ctx
->common
.impmagic
= APPCTX_MAGIC
;
986 ctx
->common
.magic
= ISCAPI_APPCTX_MAGIC
;
987 ctx
->common
.methods
= &appmethods
.methods
;
990 isc_mem_attach(mctx
, &ctx
->mctx
);
993 ctx
->socketmgr
= NULL
;
994 ctx
->timermgr
= NULL
;
996 *ctxp
= (isc_appctx_t
*)ctx
;
998 return (ISC_R_SUCCESS
);
1002 isc__appctx_destroy(isc_appctx_t
**ctxp
) {
1005 REQUIRE(ctxp
!= NULL
);
1006 ctx
= (isc__appctx_t
*)*ctxp
;
1007 REQUIRE(VALID_APPCTX(ctx
));
1009 isc_mem_putanddetach(&ctx
->mctx
, ctx
, sizeof(*ctx
));
1015 isc__appctx_settaskmgr(isc_appctx_t
*ctx0
, isc_taskmgr_t
*taskmgr
) {
1016 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
1018 REQUIRE(VALID_APPCTX(ctx
));
1020 ctx
->taskmgr
= taskmgr
;
1024 isc__appctx_setsocketmgr(isc_appctx_t
*ctx0
, isc_socketmgr_t
*socketmgr
) {
1025 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
1027 REQUIRE(VALID_APPCTX(ctx
));
1029 ctx
->socketmgr
= socketmgr
;
1033 isc__appctx_settimermgr(isc_appctx_t
*ctx0
, isc_timermgr_t
*timermgr
) {
1034 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
1036 REQUIRE(VALID_APPCTX(ctx
));
1038 ctx
->timermgr
= timermgr
;
1042 isc__app_register(void) {
1043 return (isc_app_register(isc__appctx_create
));
1046 #include "../app_api.c"