Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / unix / app.c
blobb536fb24083903b3c8de2355578d7b90e2724204
1 /* $NetBSD: app.c,v 1.1.1.3 2009/12/26 22:25:56 christos Exp $ */
3 /*
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 */
22 /*! \file */
24 #include <config.h>
26 #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
27 #include <sys/types.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <sys/time.h>
35 #ifdef HAVE_EPOLL
36 #include <sys/epoll.h>
37 #endif
39 #include <isc/app.h>
40 #include <isc/boolean.h>
41 #include <isc/condition.h>
42 #include <isc/mem.h>
43 #include <isc/msgs.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>
49 #include <isc/task.h>
50 #include <isc/time.h>
51 #include <isc/util.h>
53 /*%
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
61 #endif
63 #ifdef ISC_PLATFORM_USETHREADS
64 #include <pthread.h>
65 #endif
67 #ifndef USE_THREADS_SINGLECTX
68 #include "../timer_p.h"
69 #include "../task_p.h"
70 #include "socket_p.h"
71 #endif /* USE_THREADS_SINGLECTX */
73 #ifdef ISC_PLATFORM_USETHREADS
74 static pthread_t blockedthread;
75 #endif /* ISC_PLATFORM_USETHREADS */
77 /*%
78 * The following can be either static or public, depending on build environment.
81 #ifdef BIND9
82 #define ISC_APPFUNC_SCOPE
83 #else
84 #define ISC_APPFUNC_SCOPE static
85 #endif
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,
90 isc_task_t *task,
91 isc_taskaction_t action,
92 void *arg);
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);
106 #endif
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 {
123 isc_appctx_t common;
124 isc_mem_t *mctx;
125 isc_mutex_t lock;
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;
144 } isc__appctx_t;
146 static isc__appctx_t isc_g_appctx;
148 static struct {
149 isc_appmethods_t methods;
152 * The following are defined just for avoiding unused static functions.
154 #ifndef BIND9
155 void *run, *shutdown, *start, *onrun, *reload, *finish,
156 *block, *unblock;
157 #endif
158 } appmethods = {
160 isc__appctx_destroy,
161 isc__app_ctxstart,
162 isc__app_ctxrun,
163 isc__app_ctxsuspend,
164 isc__app_ctxshutdown,
165 isc__app_ctxfinish,
166 isc__appctx_settaskmgr,
167 isc__appctx_setsocketmgr,
168 isc__appctx_settimermgr
170 #ifndef BIND9
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
176 #endif
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.
186 #undef HAVE_SIGWAIT
188 * We need to remember which thread is the main thread...
190 static pthread_t main_thread;
191 #endif
193 #ifndef HAVE_SIGWAIT
194 static void
195 exit_action(int arg) {
196 UNUSED(arg);
197 isc_g_appctx.want_shutdown = ISC_TRUE;
200 static void
201 reload_action(int arg) {
202 UNUSED(arg);
203 isc_g_appctx.want_reload = ISC_TRUE;
205 #endif
207 static isc_result_t
208 handle_signal(int sig, void (*handler)(int)) {
209 struct sigaction sa;
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,
220 ISC_MSG_SIGNALSETUP,
221 "handle_signal() %d setup: %s"),
222 sig, strbuf);
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;
232 isc_result_t result;
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();
245 if (presult != 0) {
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);
251 #endif
253 #ifdef HAVE_LINUXTHREADS
254 main_thread = pthread_self();
255 #endif
257 result = isc_mutex_init(&ctx->lock);
258 if (result != ISC_R_SUCCESS)
259 return (result);
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) {
274 isc_result_t result;
275 int presult;
276 sigset_t sset;
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)
287 return (result);
289 #ifndef HAVE_SIGWAIT
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
295 * pthread_sigmask().
297 result = handle_signal(SIGINT, exit_action);
298 if (result != ISC_R_SUCCESS)
299 return (result);
300 result = handle_signal(SIGTERM, exit_action);
301 if (result != ISC_R_SUCCESS)
302 return (result);
303 #endif
306 * Always ignore SIGPIPE.
308 result = handle_signal(SIGPIPE, SIG_IGN);
309 if (result != ISC_R_SUCCESS)
310 return (result);
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
319 * installed above.
321 result = handle_signal(SIGHUP, SIG_DFL);
322 if (result != ISC_R_SUCCESS)
323 return (result);
325 #ifdef HAVE_SIGWAIT
326 result = handle_signal(SIGTERM, SIG_DFL);
327 if (result != ISC_R_SUCCESS)
328 return (result);
329 result = handle_signal(SIGINT, SIG_DFL);
330 if (result != ISC_R_SUCCESS)
331 return (result);
332 #endif
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);
354 if (presult != 0) {
355 isc__strerror(presult, strbuf, sizeof(strbuf));
356 UNEXPECTED_ERROR(__FILE__, __LINE__,
357 "isc_app_start() pthread_sigmask: %s",
358 strbuf);
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
367 * process.
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);
379 if (presult != 0) {
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,
392 void *arg)
394 isc_event_t *event;
395 isc_task_t *cloned_task = NULL;
396 isc_result_t result;
398 LOCK(&isc_g_appctx.lock);
400 if (isc_g_appctx.running) {
401 result = ISC_R_ALREADYRUNNING;
402 goto unlock;
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));
412 if (event == NULL) {
413 result = ISC_R_NOMEMORY;
414 goto unlock;
417 ISC_LIST_APPEND(isc_g_appctx.on_run, event, ev_link);
419 result = ISC_R_SUCCESS;
421 unlock:
422 UNLOCK(&isc_g_appctx.lock);
424 return (result);
427 #ifndef USE_THREADS_SINGLECTX
429 * Event loop for nonthreaded programs.
431 static isc_result_t
432 evloop(isc__appctx_t *ctx) {
433 isc_result_t result;
435 while (!ctx->want_shutdown) {
436 int n;
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);
457 if (readytasks) {
458 tv.tv_sec = 0;
459 tv.tv_usec = 0;
460 tvp = &tv;
461 call_timer_dispatch = ISC_TRUE;
462 } else {
463 result = isc__timermgr_nextevent(ctx->timermgr, &when);
464 if (result != ISC_R_SUCCESS)
465 tvp = NULL;
466 else {
467 isc_uint64_t us;
469 TIME_NOW(&now);
470 us = isc_time_microdiff(&when, &now);
471 if (us == 0)
472 call_timer_dispatch = ISC_TRUE;
473 tv.tv_sec = us / 1000000;
474 tv.tv_usec = us % 1000000;
475 tvp = &tv;
479 swait = NULL;
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
495 * mode.
497 isc__timermgr_dispatch(ctx->timermgr);
499 if (n > 0)
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
519 * event loop.
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
526 * or reload.
528 static isc_boolean_t signalled = ISC_FALSE;
530 isc_result_t
531 isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
532 isc_result_t result;
534 UNUSED(cp);
535 UNUSED(mp);
537 INSIST(!in_recursive_evloop);
538 in_recursive_evloop = ISC_TRUE;
540 INSIST(*mp == 1); /* Mutex must be locked on entry. */
541 --*mp;
543 result = evloop(&isc_g_appctx);
544 if (result == ISC_R_RELOAD)
545 isc_g_appctx.want_reload = ISC_TRUE;
546 if (signalled) {
547 isc_g_appctx.want_shutdown = ISC_FALSE;
548 signalled = ISC_FALSE;
551 ++*mp;
552 in_recursive_evloop = ISC_FALSE;
553 return (ISC_R_SUCCESS);
556 isc_result_t
557 isc__nothread_signal_hack(isc_condition_t *cp) {
559 UNUSED(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;
573 int result;
574 isc_event_t *event, *next_event;
575 isc_task_t *task;
576 #ifdef USE_THREADS_SINGLECTX
577 sigset_t sset;
578 char strbuf[ISC_STRERRORSIZE];
579 #ifdef HAVE_SIGWAIT
580 int sig;
581 #endif
582 #endif /* USE_THREADS_SINGLECTX */
584 REQUIRE(VALID_APPCTX(ctx));
586 #ifdef HAVE_LINUXTHREADS
587 REQUIRE(main_thread == pthread_self());
588 #endif
590 LOCK(&ctx->lock);
592 if (!ctx->running) {
593 ctx->running = ISC_TRUE;
596 * Post any on-run events (in FIFO order).
598 for (event = ISC_LIST_HEAD(ctx->on_run);
599 event != NULL;
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);
610 UNLOCK(&ctx->lock);
612 #ifndef HAVE_SIGWAIT
614 * Catch SIGHUP.
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);
624 #endif
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) {
640 #ifdef HAVE_SIGWAIT
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);
656 if (result == 0) {
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);
665 if (sig >= 0) {
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",
681 strbuf);
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)
693 exit(1);
696 #else /* USE_THREADS_SINGLECTX */
698 (void)isc__taskmgr_dispatch(ctx->taskmgr);
700 result = evloop(ctx);
701 if (result != ISC_R_SUCCESS)
702 return (result);
704 #endif /* USE_THREADS_SINGLECTX */
706 return (ISC_R_SUCCESS);
709 ISC_APPFUNC_SCOPE isc_result_t
710 isc__app_run() {
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));
722 LOCK(&ctx->lock);
724 REQUIRE(ctx->running);
726 if (ctx->shutdown_requested)
727 want_kill = ISC_FALSE;
728 else
729 ctx->shutdown_requested = ISC_TRUE;
731 UNLOCK(&ctx->lock);
733 if (want_kill) {
734 if (ctx != &isc_g_appctx)
735 ctx->want_shutdown = ISC_TRUE;
736 else {
737 #ifdef HAVE_LINUXTHREADS
738 int result;
740 result = pthread_kill(main_thread, SIGTERM);
741 if (result != 0) {
742 isc__strerror(result, strbuf, sizeof(strbuf));
743 UNEXPECTED_ERROR(__FILE__, __LINE__,
744 "isc_app_shutdown() "
745 "pthread_kill: %s",
746 strbuf);
747 return (ISC_R_UNEXPECTED);
749 #else
750 if (kill(getpid(), SIGTERM) < 0) {
751 isc__strerror(errno, strbuf, sizeof(strbuf));
752 UNEXPECTED_ERROR(__FILE__, __LINE__,
753 "isc_app_shutdown() "
754 "kill: %s", strbuf);
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));
777 LOCK(&ctx->lock);
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;
787 UNLOCK(&ctx->lock);
789 if (want_kill) {
790 if (ctx != &isc_g_appctx)
791 ctx->want_reload = ISC_TRUE;
792 else {
793 #ifdef HAVE_LINUXTHREADS
794 int result;
796 result = pthread_kill(main_thread, SIGHUP);
797 if (result != 0) {
798 isc__strerror(result, strbuf, sizeof(strbuf));
799 UNEXPECTED_ERROR(__FILE__, __LINE__,
800 "isc_app_reload() "
801 "pthread_kill: %s",
802 strbuf);
803 return (ISC_R_UNEXPECTED);
805 #else
806 if (kill(getpid(), SIGHUP) < 0) {
807 isc__strerror(errno, strbuf, sizeof(strbuf));
808 UNEXPECTED_ERROR(__FILE__, __LINE__,
809 "isc_app_reload() "
810 "kill: %s", strbuf);
811 return (ISC_R_UNEXPECTED);
813 #endif
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
842 sigset_t sset;
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
860 sigset_t sset;
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) {
881 isc__appctx_t *ctx;
883 REQUIRE(mctx != NULL);
884 REQUIRE(ctxp != NULL && *ctxp == NULL);
886 ctx = isc_mem_get(mctx, sizeof(*ctx));
887 if (ctx == NULL)
888 return (ISC_R_NOMEMORY);
890 ctx->common.impmagic = APPCTX_MAGIC;
891 ctx->common.magic = ISCAPI_APPCTX_MAGIC;
892 ctx->common.methods = &appmethods.methods;
894 ctx->mctx = NULL;
895 isc_mem_attach(mctx, &ctx->mctx);
897 ctx->taskmgr = NULL;
898 ctx->socketmgr = NULL;
899 ctx->timermgr = NULL;
901 *ctxp = (isc_appctx_t *)ctx;
903 return (ISC_R_SUCCESS);
905 #endif
907 ISC_APPFUNC_SCOPE void
908 isc__appctx_destroy(isc_appctx_t **ctxp) {
909 isc__appctx_t *ctx;
911 REQUIRE(ctxp != NULL);
912 ctx = (isc__appctx_t *)*ctxp;
913 REQUIRE(VALID_APPCTX(ctx));
915 isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
917 *ctxp = NULL;
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
948 isc_result_t
949 isc__app_register() {
950 return (isc_app_register(isc__appctx_create));
952 #endif