1 /* $NetBSD: app.c,v 1.1.1.3 2009/12/26 22:25:56 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007-2009 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.
20 /* Id: app.c,v 1.64 2009/11/04 05:58:46 marka Exp */
26 #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
27 #include <sys/types.h>
36 #include <sys/epoll.h>
40 #include <isc/boolean.h>
41 #include <isc/condition.h>
44 #include <isc/mutex.h>
45 #include <isc/event.h>
46 #include <isc/platform.h>
47 #include <isc/strerror.h>
48 #include <isc/string.h>
54 * For BIND9 internal applications built with threads, we use a single app
55 * context and let multiple worker, I/O, timer threads do actual jobs.
56 * For other cases (including BIND9 built without threads) an app context acts
57 * as an event loop dispatching various events.
59 #if defined(ISC_PLATFORM_USETHREADS) && defined(BIND9)
60 #define USE_THREADS_SINGLECTX
63 #ifdef ISC_PLATFORM_USETHREADS
67 #ifndef USE_THREADS_SINGLECTX
68 #include "../timer_p.h"
69 #include "../task_p.h"
71 #endif /* USE_THREADS_SINGLECTX */
73 #ifdef ISC_PLATFORM_USETHREADS
74 static pthread_t blockedthread
;
75 #endif /* ISC_PLATFORM_USETHREADS */
78 * The following can be either static or public, depending on build environment.
82 #define ISC_APPFUNC_SCOPE
84 #define ISC_APPFUNC_SCOPE static
87 ISC_APPFUNC_SCOPE isc_result_t
isc__app_start(void);
88 ISC_APPFUNC_SCOPE isc_result_t
isc__app_ctxstart(isc_appctx_t
*ctx
);
89 ISC_APPFUNC_SCOPE isc_result_t
isc__app_onrun(isc_mem_t
*mctx
,
91 isc_taskaction_t action
,
93 ISC_APPFUNC_SCOPE isc_result_t
isc__app_ctxrun(isc_appctx_t
*ctx
);
94 ISC_APPFUNC_SCOPE isc_result_t
isc__app_run(void);
95 ISC_APPFUNC_SCOPE isc_result_t
isc__app_ctxshutdown(isc_appctx_t
*ctx
);
96 ISC_APPFUNC_SCOPE isc_result_t
isc__app_shutdown(void);
97 ISC_APPFUNC_SCOPE isc_result_t
isc__app_reload(void);
98 ISC_APPFUNC_SCOPE isc_result_t
isc__app_ctxsuspend(isc_appctx_t
*ctx
);
99 ISC_APPFUNC_SCOPE
void isc__app_ctxfinish(isc_appctx_t
*ctx
);
100 ISC_APPFUNC_SCOPE
void isc__app_finish(void);
101 ISC_APPFUNC_SCOPE
void isc__app_block(void);
102 ISC_APPFUNC_SCOPE
void isc__app_unblock(void);
103 #ifdef USE_APPIMPREGISTER
104 ISC_APPFUNC_SCOPE isc_result_t
isc__appctx_create(isc_mem_t
*mctx
,
105 isc_appctx_t
**ctxp
);
107 ISC_APPFUNC_SCOPE
void isc__appctx_destroy(isc_appctx_t
**ctxp
);
108 ISC_APPFUNC_SCOPE
void isc__appctx_settaskmgr(isc_appctx_t
*ctx
,
109 isc_taskmgr_t
*taskmgr
);
110 ISC_APPFUNC_SCOPE
void isc__appctx_setsocketmgr(isc_appctx_t
*ctx
,
111 isc_socketmgr_t
*socketmgr
);
112 ISC_APPFUNC_SCOPE
void isc__appctx_settimermgr(isc_appctx_t
*ctx
,
113 isc_timermgr_t
*timermgr
);
116 * The application context of this module. This implementation actually
117 * doesn't use it. (This may change in the future).
119 #define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
120 #define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
122 typedef struct isc__appctx
{
126 isc_eventlist_t on_run
;
127 isc_boolean_t shutdown_requested
;
128 isc_boolean_t running
;
131 * We assume that 'want_shutdown' can be read and written atomically.
133 isc_boolean_t want_shutdown
;
135 * We assume that 'want_reload' can be read and written atomically.
137 isc_boolean_t want_reload
;
139 isc_boolean_t blocked
;
141 isc_taskmgr_t
*taskmgr
;
142 isc_socketmgr_t
*socketmgr
;
143 isc_timermgr_t
*timermgr
;
146 static isc__appctx_t isc_g_appctx
;
149 isc_appmethods_t methods
;
152 * The following are defined just for avoiding unused static functions.
155 void *run
, *shutdown
, *start
, *onrun
, *reload
, *finish
,
164 isc__app_ctxshutdown
,
166 isc__appctx_settaskmgr
,
167 isc__appctx_setsocketmgr
,
168 isc__appctx_settimermgr
172 (void *)isc__app_run
, (void *)isc__app_shutdown
,
173 (void *)isc__app_start
, (void *)isc__app_onrun
, (void *)isc__app_reload
,
174 (void *)isc__app_finish
, (void *)isc__app_block
,
175 (void *)isc__app_unblock
179 #ifdef HAVE_LINUXTHREADS
181 * Linux has sigwait(), but it appears to prevent signal handlers from
182 * running, even if they're not in the set being waited for. This makes
183 * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
184 * Instead of messing with it, we just use sigsuspend() instead.
188 * We need to remember which thread is the main thread...
190 static pthread_t main_thread
;
195 exit_action(int arg
) {
197 isc_g_appctx
.want_shutdown
= ISC_TRUE
;
201 reload_action(int arg
) {
203 isc_g_appctx
.want_reload
= ISC_TRUE
;
208 handle_signal(int sig
, void (*handler
)(int)) {
210 char strbuf
[ISC_STRERRORSIZE
];
212 memset(&sa
, 0, sizeof(sa
));
213 sa
.sa_handler
= handler
;
215 if (sigfillset(&sa
.sa_mask
) != 0 ||
216 sigaction(sig
, &sa
, NULL
) < 0) {
217 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
218 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
219 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_APP
,
221 "handle_signal() %d setup: %s"),
223 return (ISC_R_UNEXPECTED
);
226 return (ISC_R_SUCCESS
);
229 ISC_APPFUNC_SCOPE isc_result_t
230 isc__app_ctxstart(isc_appctx_t
*ctx0
) {
231 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
234 REQUIRE(VALID_APPCTX(ctx
));
237 * Start an ISC library application.
240 #ifdef NEED_PTHREAD_INIT
242 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
244 presult
= pthread_init();
246 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
247 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
248 "isc_app_start() pthread_init: %s", strbuf
);
249 return (ISC_R_UNEXPECTED
);
253 #ifdef HAVE_LINUXTHREADS
254 main_thread
= pthread_self();
257 result
= isc_mutex_init(&ctx
->lock
);
258 if (result
!= ISC_R_SUCCESS
)
261 ISC_LIST_INIT(ctx
->on_run
);
263 ctx
->shutdown_requested
= ISC_FALSE
;
264 ctx
->running
= ISC_FALSE
;
265 ctx
->want_shutdown
= ISC_FALSE
;
266 ctx
->want_reload
= ISC_FALSE
;
267 ctx
->blocked
= ISC_FALSE
;
269 return (ISC_R_SUCCESS
);
272 ISC_APPFUNC_SCOPE isc_result_t
273 isc__app_start(void) {
277 char strbuf
[ISC_STRERRORSIZE
];
279 isc_g_appctx
.common
.impmagic
= APPCTX_MAGIC
;
280 isc_g_appctx
.common
.magic
= ISCAPI_APPCTX_MAGIC
;
281 isc_g_appctx
.common
.methods
= &appmethods
.methods
;
282 isc_g_appctx
.mctx
= NULL
;
283 /* The remaining members will be initialized in ctxstart() */
285 result
= isc__app_ctxstart((isc_appctx_t
*)&isc_g_appctx
);
286 if (result
!= ISC_R_SUCCESS
)
291 * Install do-nothing handlers for SIGINT and SIGTERM.
293 * We install them now because BSDI 3.1 won't block
294 * the default actions, regardless of what we do with
297 result
= handle_signal(SIGINT
, exit_action
);
298 if (result
!= ISC_R_SUCCESS
)
300 result
= handle_signal(SIGTERM
, exit_action
);
301 if (result
!= ISC_R_SUCCESS
)
306 * Always ignore SIGPIPE.
308 result
= handle_signal(SIGPIPE
, SIG_IGN
);
309 if (result
!= ISC_R_SUCCESS
)
313 * On Solaris 2, delivery of a signal whose action is SIG_IGN
314 * will not cause sigwait() to return. We may have inherited
315 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
316 * process (e.g, Solaris cron). Set an action of SIG_DFL to make
317 * sure sigwait() works as expected. Only do this for SIGTERM and
318 * SIGINT if we don't have sigwait(), since a different handler is
321 result
= handle_signal(SIGHUP
, SIG_DFL
);
322 if (result
!= ISC_R_SUCCESS
)
326 result
= handle_signal(SIGTERM
, SIG_DFL
);
327 if (result
!= ISC_R_SUCCESS
)
329 result
= handle_signal(SIGINT
, SIG_DFL
);
330 if (result
!= ISC_R_SUCCESS
)
334 #ifdef ISC_PLATFORM_USETHREADS
336 * Block SIGHUP, SIGINT, SIGTERM.
338 * If isc_app_start() is called from the main thread before any other
339 * threads have been created, then the pthread_sigmask() call below
340 * will result in all threads having SIGHUP, SIGINT and SIGTERM
341 * blocked by default, ensuring that only the thread that calls
342 * sigwait() for them will get those signals.
344 if (sigemptyset(&sset
) != 0 ||
345 sigaddset(&sset
, SIGHUP
) != 0 ||
346 sigaddset(&sset
, SIGINT
) != 0 ||
347 sigaddset(&sset
, SIGTERM
) != 0) {
348 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
349 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
350 "isc_app_start() sigsetops: %s", strbuf
);
351 return (ISC_R_UNEXPECTED
);
353 presult
= pthread_sigmask(SIG_BLOCK
, &sset
, NULL
);
355 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
356 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
357 "isc_app_start() pthread_sigmask: %s",
359 return (ISC_R_UNEXPECTED
);
361 #else /* ISC_PLATFORM_USETHREADS */
363 * Unblock SIGHUP, SIGINT, SIGTERM.
365 * If we're not using threads, we need to make sure that SIGHUP,
366 * SIGINT and SIGTERM are not inherited as blocked from the parent
369 if (sigemptyset(&sset
) != 0 ||
370 sigaddset(&sset
, SIGHUP
) != 0 ||
371 sigaddset(&sset
, SIGINT
) != 0 ||
372 sigaddset(&sset
, SIGTERM
) != 0) {
373 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
374 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
375 "isc_app_start() sigsetops: %s", strbuf
);
376 return (ISC_R_UNEXPECTED
);
378 presult
= sigprocmask(SIG_UNBLOCK
, &sset
, NULL
);
380 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
381 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
382 "isc_app_start() sigprocmask: %s", strbuf
);
383 return (ISC_R_UNEXPECTED
);
385 #endif /* ISC_PLATFORM_USETHREADS */
387 return (ISC_R_SUCCESS
);
390 ISC_APPFUNC_SCOPE isc_result_t
391 isc__app_onrun(isc_mem_t
*mctx
, isc_task_t
*task
, isc_taskaction_t action
,
395 isc_task_t
*cloned_task
= NULL
;
398 LOCK(&isc_g_appctx
.lock
);
400 if (isc_g_appctx
.running
) {
401 result
= ISC_R_ALREADYRUNNING
;
406 * Note that we store the task to which we're going to send the event
407 * in the event's "sender" field.
409 isc_task_attach(task
, &cloned_task
);
410 event
= isc_event_allocate(mctx
, cloned_task
, ISC_APPEVENT_SHUTDOWN
,
411 action
, arg
, sizeof(*event
));
413 result
= ISC_R_NOMEMORY
;
417 ISC_LIST_APPEND(isc_g_appctx
.on_run
, event
, ev_link
);
419 result
= ISC_R_SUCCESS
;
422 UNLOCK(&isc_g_appctx
.lock
);
427 #ifndef USE_THREADS_SINGLECTX
429 * Event loop for nonthreaded programs.
432 evloop(isc__appctx_t
*ctx
) {
435 while (!ctx
->want_shutdown
) {
437 isc_time_t when
, now
;
438 struct timeval tv
, *tvp
;
439 isc_socketwait_t
*swait
;
440 isc_boolean_t readytasks
;
441 isc_boolean_t call_timer_dispatch
= ISC_FALSE
;
444 * Check the reload (or suspend) case first for exiting the
445 * loop as fast as possible in case:
446 * - the direct call to isc__taskmgr_dispatch() in
447 * isc__app_ctxrun() completes all the tasks so far,
448 * - there is thus currently no active task, and
449 * - there is a timer event
451 if (ctx
->want_reload
) {
452 ctx
->want_reload
= ISC_FALSE
;
453 return (ISC_R_RELOAD
);
456 readytasks
= isc__taskmgr_ready(ctx
->taskmgr
);
461 call_timer_dispatch
= ISC_TRUE
;
463 result
= isc__timermgr_nextevent(ctx
->timermgr
, &when
);
464 if (result
!= ISC_R_SUCCESS
)
470 us
= isc_time_microdiff(&when
, &now
);
472 call_timer_dispatch
= ISC_TRUE
;
473 tv
.tv_sec
= us
/ 1000000;
474 tv
.tv_usec
= us
% 1000000;
480 n
= isc__socketmgr_waitevents(ctx
->socketmgr
, tvp
, &swait
);
482 if (n
== 0 || call_timer_dispatch
) {
484 * We call isc__timermgr_dispatch() only when
485 * necessary, in order to reduce overhead. If the
486 * select() call indicates a timeout, we need the
487 * dispatch. Even if not, if we set the 0-timeout
488 * for the select() call, we need to check the timer
489 * events. In the 'readytasks' case, there may be no
490 * timeout event actually, but there is no other way
491 * to reduce the overhead.
492 * Note that we do not have to worry about the case
493 * where a new timer is inserted during the select()
494 * call, since this loop only runs in the non-thread
497 isc__timermgr_dispatch(ctx
->timermgr
);
500 (void)isc__socketmgr_dispatch(ctx
->socketmgr
, swait
);
501 (void)isc__taskmgr_dispatch(ctx
->taskmgr
);
503 return (ISC_R_SUCCESS
);
505 #endif /* USE_THREADS_SINGLECTX */
507 #ifndef ISC_PLATFORM_USETHREADS
509 * This is a gross hack to support waiting for condition
510 * variables in nonthreaded programs in a limited way;
511 * see lib/isc/nothreads/include/isc/condition.h.
512 * We implement isc_condition_wait() by entering the
513 * event loop recursively until the want_shutdown flag
514 * is set by isc_condition_signal().
518 * \brief True if we are currently executing in the recursive
521 static isc_boolean_t in_recursive_evloop
= ISC_FALSE
;
524 * \brief True if we are exiting the event loop as the result of
525 * a call to isc_condition_signal() rather than a shutdown
528 static isc_boolean_t signalled
= ISC_FALSE
;
531 isc__nothread_wait_hack(isc_condition_t
*cp
, isc_mutex_t
*mp
) {
537 INSIST(!in_recursive_evloop
);
538 in_recursive_evloop
= ISC_TRUE
;
540 INSIST(*mp
== 1); /* Mutex must be locked on entry. */
543 result
= evloop(&isc_g_appctx
);
544 if (result
== ISC_R_RELOAD
)
545 isc_g_appctx
.want_reload
= ISC_TRUE
;
547 isc_g_appctx
.want_shutdown
= ISC_FALSE
;
548 signalled
= ISC_FALSE
;
552 in_recursive_evloop
= ISC_FALSE
;
553 return (ISC_R_SUCCESS
);
557 isc__nothread_signal_hack(isc_condition_t
*cp
) {
561 INSIST(in_recursive_evloop
);
563 isc_g_appctx
.want_shutdown
= ISC_TRUE
;
564 signalled
= ISC_TRUE
;
565 return (ISC_R_SUCCESS
);
568 #endif /* ISC_PLATFORM_USETHREADS */
570 ISC_APPFUNC_SCOPE isc_result_t
571 isc__app_ctxrun(isc_appctx_t
*ctx0
) {
572 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
574 isc_event_t
*event
, *next_event
;
576 #ifdef USE_THREADS_SINGLECTX
578 char strbuf
[ISC_STRERRORSIZE
];
582 #endif /* USE_THREADS_SINGLECTX */
584 REQUIRE(VALID_APPCTX(ctx
));
586 #ifdef HAVE_LINUXTHREADS
587 REQUIRE(main_thread
== pthread_self());
593 ctx
->running
= ISC_TRUE
;
596 * Post any on-run events (in FIFO order).
598 for (event
= ISC_LIST_HEAD(ctx
->on_run
);
600 event
= next_event
) {
601 next_event
= ISC_LIST_NEXT(event
, ev_link
);
602 ISC_LIST_UNLINK(ctx
->on_run
, event
, ev_link
);
603 task
= event
->ev_sender
;
604 event
->ev_sender
= NULL
;
605 isc_task_sendanddetach(&task
, &event
);
616 * We do this here to ensure that the signal handler is installed
617 * (i.e. that it wasn't a "one-shot" handler).
619 if (ctx
== &isc_g_appctx
) {
620 result
= handle_signal(SIGHUP
, reload_action
);
621 if (result
!= ISC_R_SUCCESS
)
622 return (ISC_R_SUCCESS
);
626 #ifdef USE_THREADS_SINGLECTX
628 * When we are using multiple contexts, we don't rely on signals.
630 if (ctx
!= &isc_g_appctx
)
631 return (ISC_R_SUCCESS
);
634 * There is no danger if isc_app_shutdown() is called before we wait
635 * for signals. Signals are blocked, so any such signal will simply
636 * be made pending and we will get it when we call sigwait().
639 while (!ctx
->want_shutdown
) {
642 * Wait for SIGHUP, SIGINT, or SIGTERM.
644 if (sigemptyset(&sset
) != 0 ||
645 sigaddset(&sset
, SIGHUP
) != 0 ||
646 sigaddset(&sset
, SIGINT
) != 0 ||
647 sigaddset(&sset
, SIGTERM
) != 0) {
648 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
649 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
650 "isc_app_run() sigsetops: %s", strbuf
);
651 return (ISC_R_UNEXPECTED
);
654 #ifndef HAVE_UNIXWARE_SIGWAIT
655 result
= sigwait(&sset
, &sig
);
657 if (sig
== SIGINT
|| sig
== SIGTERM
)
658 ctx
->want_shutdown
= ISC_TRUE
;
659 else if (sig
== SIGHUP
)
660 ctx
->want_reload
= ISC_TRUE
;
663 #else /* Using UnixWare sigwait semantics. */
664 sig
= sigwait(&sset
);
666 if (sig
== SIGINT
|| sig
== SIGTERM
)
667 ctx
->want_shutdown
= ISC_TRUE
;
668 else if (sig
== SIGHUP
)
669 ctx
->want_reload
= ISC_TRUE
;
672 #endif /* HAVE_UNIXWARE_SIGWAIT */
673 #else /* Don't have sigwait(). */
675 * Listen for all signals.
677 if (sigemptyset(&sset
) != 0) {
678 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
679 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
680 "isc_app_run() sigsetops: %s",
682 return (ISC_R_UNEXPECTED
);
684 result
= sigsuspend(&sset
);
685 #endif /* HAVE_SIGWAIT */
687 if (ctx
->want_reload
) {
688 ctx
->want_reload
= ISC_FALSE
;
689 return (ISC_R_RELOAD
);
692 if (ctx
->want_shutdown
&& ctx
->blocked
)
696 #else /* USE_THREADS_SINGLECTX */
698 (void)isc__taskmgr_dispatch(ctx
->taskmgr
);
700 result
= evloop(ctx
);
701 if (result
!= ISC_R_SUCCESS
)
704 #endif /* USE_THREADS_SINGLECTX */
706 return (ISC_R_SUCCESS
);
709 ISC_APPFUNC_SCOPE isc_result_t
711 return (isc__app_ctxrun((isc_appctx_t
*)&isc_g_appctx
));
714 ISC_APPFUNC_SCOPE isc_result_t
715 isc__app_ctxshutdown(isc_appctx_t
*ctx0
) {
716 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
717 isc_boolean_t want_kill
= ISC_TRUE
;
718 char strbuf
[ISC_STRERRORSIZE
];
720 REQUIRE(VALID_APPCTX(ctx
));
724 REQUIRE(ctx
->running
);
726 if (ctx
->shutdown_requested
)
727 want_kill
= ISC_FALSE
;
729 ctx
->shutdown_requested
= ISC_TRUE
;
734 if (ctx
!= &isc_g_appctx
)
735 ctx
->want_shutdown
= ISC_TRUE
;
737 #ifdef HAVE_LINUXTHREADS
740 result
= pthread_kill(main_thread
, SIGTERM
);
742 isc__strerror(result
, strbuf
, sizeof(strbuf
));
743 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
744 "isc_app_shutdown() "
747 return (ISC_R_UNEXPECTED
);
750 if (kill(getpid(), SIGTERM
) < 0) {
751 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
752 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
753 "isc_app_shutdown() "
755 return (ISC_R_UNEXPECTED
);
757 #endif /* HAVE_LINUXTHREADS */
761 return (ISC_R_SUCCESS
);
764 ISC_APPFUNC_SCOPE isc_result_t
765 isc__app_shutdown() {
766 return (isc__app_ctxshutdown((isc_appctx_t
*)&isc_g_appctx
));
769 ISC_APPFUNC_SCOPE isc_result_t
770 isc__app_ctxsuspend(isc_appctx_t
*ctx0
) {
771 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
772 isc_boolean_t want_kill
= ISC_TRUE
;
773 char strbuf
[ISC_STRERRORSIZE
];
775 REQUIRE(VALID_APPCTX(ctx
));
779 REQUIRE(ctx
->running
);
782 * Don't send the reload signal if we're shutting down.
784 if (ctx
->shutdown_requested
)
785 want_kill
= ISC_FALSE
;
790 if (ctx
!= &isc_g_appctx
)
791 ctx
->want_reload
= ISC_TRUE
;
793 #ifdef HAVE_LINUXTHREADS
796 result
= pthread_kill(main_thread
, SIGHUP
);
798 isc__strerror(result
, strbuf
, sizeof(strbuf
));
799 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
803 return (ISC_R_UNEXPECTED
);
806 if (kill(getpid(), SIGHUP
) < 0) {
807 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
808 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
811 return (ISC_R_UNEXPECTED
);
817 return (ISC_R_SUCCESS
);
820 ISC_APPFUNC_SCOPE isc_result_t
821 isc__app_reload(void) {
822 return (isc__app_ctxsuspend((isc_appctx_t
*)&isc_g_appctx
));
825 ISC_APPFUNC_SCOPE
void
826 isc__app_ctxfinish(isc_appctx_t
*ctx0
) {
827 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
829 REQUIRE(VALID_APPCTX(ctx
));
831 DESTROYLOCK(&ctx
->lock
);
834 ISC_APPFUNC_SCOPE
void
835 isc__app_finish(void) {
836 isc__app_ctxfinish((isc_appctx_t
*)&isc_g_appctx
);
839 ISC_APPFUNC_SCOPE
void
840 isc__app_block(void) {
841 #ifdef ISC_PLATFORM_USETHREADS
843 #endif /* ISC_PLATFORM_USETHREADS */
844 REQUIRE(isc_g_appctx
.running
);
845 REQUIRE(!isc_g_appctx
.blocked
);
847 isc_g_appctx
.blocked
= ISC_TRUE
;
848 #ifdef ISC_PLATFORM_USETHREADS
849 blockedthread
= pthread_self();
850 RUNTIME_CHECK(sigemptyset(&sset
) == 0 &&
851 sigaddset(&sset
, SIGINT
) == 0 &&
852 sigaddset(&sset
, SIGTERM
) == 0);
853 RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK
, &sset
, NULL
) == 0);
854 #endif /* ISC_PLATFORM_USETHREADS */
857 ISC_APPFUNC_SCOPE
void
858 isc__app_unblock(void) {
859 #ifdef ISC_PLATFORM_USETHREADS
861 #endif /* ISC_PLATFORM_USETHREADS */
863 REQUIRE(isc_g_appctx
.running
);
864 REQUIRE(isc_g_appctx
.blocked
);
866 isc_g_appctx
.blocked
= ISC_FALSE
;
868 #ifdef ISC_PLATFORM_USETHREADS
869 REQUIRE(blockedthread
== pthread_self());
871 RUNTIME_CHECK(sigemptyset(&sset
) == 0 &&
872 sigaddset(&sset
, SIGINT
) == 0 &&
873 sigaddset(&sset
, SIGTERM
) == 0);
874 RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK
, &sset
, NULL
) == 0);
875 #endif /* ISC_PLATFORM_USETHREADS */
878 #ifdef USE_APPIMPREGISTER
879 ISC_APPFUNC_SCOPE isc_result_t
880 isc__appctx_create(isc_mem_t
*mctx
, isc_appctx_t
**ctxp
) {
883 REQUIRE(mctx
!= NULL
);
884 REQUIRE(ctxp
!= NULL
&& *ctxp
== NULL
);
886 ctx
= isc_mem_get(mctx
, sizeof(*ctx
));
888 return (ISC_R_NOMEMORY
);
890 ctx
->common
.impmagic
= APPCTX_MAGIC
;
891 ctx
->common
.magic
= ISCAPI_APPCTX_MAGIC
;
892 ctx
->common
.methods
= &appmethods
.methods
;
895 isc_mem_attach(mctx
, &ctx
->mctx
);
898 ctx
->socketmgr
= NULL
;
899 ctx
->timermgr
= NULL
;
901 *ctxp
= (isc_appctx_t
*)ctx
;
903 return (ISC_R_SUCCESS
);
907 ISC_APPFUNC_SCOPE
void
908 isc__appctx_destroy(isc_appctx_t
**ctxp
) {
911 REQUIRE(ctxp
!= NULL
);
912 ctx
= (isc__appctx_t
*)*ctxp
;
913 REQUIRE(VALID_APPCTX(ctx
));
915 isc_mem_putanddetach(&ctx
->mctx
, ctx
, sizeof(*ctx
));
920 ISC_APPFUNC_SCOPE
void
921 isc__appctx_settaskmgr(isc_appctx_t
*ctx0
, isc_taskmgr_t
*taskmgr
) {
922 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
924 REQUIRE(VALID_APPCTX(ctx
));
926 ctx
->taskmgr
= taskmgr
;
929 ISC_APPFUNC_SCOPE
void
930 isc__appctx_setsocketmgr(isc_appctx_t
*ctx0
, isc_socketmgr_t
*socketmgr
) {
931 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
933 REQUIRE(VALID_APPCTX(ctx
));
935 ctx
->socketmgr
= socketmgr
;
938 ISC_APPFUNC_SCOPE
void
939 isc__appctx_settimermgr(isc_appctx_t
*ctx0
, isc_timermgr_t
*timermgr
) {
940 isc__appctx_t
*ctx
= (isc__appctx_t
*)ctx0
;
942 REQUIRE(VALID_APPCTX(ctx
));
944 ctx
->timermgr
= timermgr
;
947 #ifdef USE_APPIMPREGISTER
949 isc__app_register() {
950 return (isc_app_register(isc__appctx_create
));