etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / lib / isc / unix / app.c
blob676050ee0b8d2b9cdec451d00433a1bdb4751ca9
1 /* $NetBSD: app.c,v 1.13 2015/07/08 17:28:59 christos Exp $ */
3 /*
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.
20 /*! \file */
22 #include <config.h>
24 #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
25 #include <sys/types.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <signal.h>
32 #include <sys/time.h>
33 #ifdef HAVE_EPOLL
34 #include <sys/epoll.h>
35 #endif
37 #include <isc/app.h>
38 #include <isc/boolean.h>
39 #include <isc/condition.h>
40 #include <isc/mem.h>
41 #include <isc/msgs.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>
47 #include <isc/task.h>
48 #include <isc/time.h>
49 #include <isc/util.h>
51 #ifdef ISC_PLATFORM_USETHREADS
52 #include <pthread.h>
53 #endif
55 /*%
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"
64 #include "socket_p.h"
65 #endif /* ISC_PLATFORM_USETHREADS */
67 #ifdef ISC_PLATFORM_USETHREADS
68 static pthread_t blockedthread;
69 #endif /* ISC_PLATFORM_USETHREADS */
71 /*%
72 * The following are intended for internal use (indicated by "isc__"
73 * prefix) but are not declared as static, allowing direct access from
74 * unit tests etc.
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,
97 void *arg);
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 {
107 isc_appctx_t common;
108 isc_mem_t *mctx;
109 isc_mutex_t lock;
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 */
132 } isc__appctx_t;
134 static isc__appctx_t isc_g_appctx;
136 static struct {
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;
143 } appmethods = {
145 isc__appctx_destroy,
146 isc__app_ctxstart,
147 isc__app_ctxrun,
148 isc__app_ctxsuspend,
149 isc__app_ctxshutdown,
150 isc__app_ctxfinish,
151 isc__appctx_settaskmgr,
152 isc__appctx_setsocketmgr,
153 isc__appctx_settimermgr,
154 isc__app_ctxonrun
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.
172 #undef HAVE_SIGWAIT
174 * We need to remember which thread is the main thread...
176 static pthread_t main_thread;
177 #endif
179 #ifndef HAVE_SIGWAIT
180 static void
181 exit_action(int arg) {
182 UNUSED(arg);
183 isc_g_appctx.want_shutdown = ISC_TRUE;
186 static void
187 reload_action(int arg) {
188 UNUSED(arg);
189 isc_g_appctx.want_reload = ISC_TRUE;
191 #endif
193 static isc_result_t
194 handle_signal(int sig, void (*handler)(int)) {
195 struct sigaction sa;
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,
206 ISC_MSG_SIGNALSETUP,
207 "handle_signal() %d setup: %s"),
208 sig, strbuf);
209 return (ISC_R_UNEXPECTED);
212 return (ISC_R_SUCCESS);
215 isc_result_t
216 isc__app_ctxstart(isc_appctx_t *ctx0) {
217 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
218 isc_result_t result;
219 int presult;
220 sigset_t sset;
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();
234 if (presult != 0) {
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);
240 #endif
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)
249 return (result);
251 result = isc_condition_init(&ctx->ready);
252 if (result != ISC_R_SUCCESS)
253 goto cleanup_rlock;
255 result = isc_mutex_init(&ctx->lock);
256 if (result != ISC_R_SUCCESS)
257 goto cleanup_rcond;
258 #else /* ISC_PLATFORM_USETHREADS */
259 result = isc_mutex_init(&ctx->lock);
260 if (result != ISC_R_SUCCESS)
261 goto cleanup;
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;
272 #ifndef HAVE_SIGWAIT
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
278 * pthread_sigmask().
280 result = handle_signal(SIGINT, exit_action);
281 if (result != ISC_R_SUCCESS)
282 goto cleanup;
283 result = handle_signal(SIGTERM, exit_action);
284 if (result != ISC_R_SUCCESS)
285 goto cleanup;
286 #endif
289 * Always ignore SIGPIPE.
291 result = handle_signal(SIGPIPE, SIG_IGN);
292 if (result != ISC_R_SUCCESS)
293 goto cleanup;
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
302 * installed above.
304 result = handle_signal(SIGHUP, SIG_DFL);
305 if (result != ISC_R_SUCCESS)
306 goto cleanup;
308 #ifdef HAVE_SIGWAIT
309 result = handle_signal(SIGTERM, SIG_DFL);
310 if (result != ISC_R_SUCCESS)
311 goto cleanup;
312 result = handle_signal(SIGINT, SIG_DFL);
313 if (result != ISC_R_SUCCESS)
314 goto cleanup;
315 #endif
317 #ifdef ISC_PLATFORM_USETHREADS
318 if (isc_bind9) {
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;
336 goto cleanup;
338 presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
339 if (presult != 0) {
340 isc__strerror(presult, strbuf, sizeof(strbuf));
341 UNEXPECTED_ERROR(__FILE__, __LINE__,
342 "isc_app_start() pthread_sigmask: %s",
343 strbuf);
344 result = ISC_R_UNEXPECTED;
345 goto cleanup;
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
354 * process.
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;
364 goto cleanup;
366 presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
367 if (presult != 0) {
368 isc__strerror(errno, strbuf, sizeof(strbuf));
369 UNEXPECTED_ERROR(__FILE__, __LINE__,
370 "isc_app_start() sigprocmask: %s", strbuf);
371 result = ISC_R_UNEXPECTED;
372 goto cleanup;
374 #endif /* ISC_PLATFORM_USETHREADS */
376 return (ISC_R_SUCCESS);
378 cleanup:
379 #ifdef ISC_PLATFORM_USETHREADS
380 cleanup_rcond:
381 (void)isc_condition_destroy(&ctx->ready);
383 cleanup_rlock:
384 (void)isc_mutex_destroy(&ctx->readylock);
385 #endif /* ISC_PLATFORM_USETHREADS */
386 return (result);
389 isc_result_t
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));
400 isc_result_t
401 isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
402 void *arg)
404 return (isc__app_ctxonrun((isc_appctx_t *)&isc_g_appctx, mctx,
405 task, action, arg));
408 isc_result_t
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;
413 isc_event_t *event;
414 isc_task_t *cloned_task = NULL;
415 isc_result_t result;
417 LOCK(&ctx->lock);
419 if (ctx->running) {
420 result = ISC_R_ALREADYRUNNING;
421 goto unlock;
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));
431 if (event == NULL) {
432 result = ISC_R_NOMEMORY;
433 goto unlock;
436 ISC_LIST_APPEND(ctx->on_run, event, ev_link);
438 result = ISC_R_SUCCESS;
440 unlock:
441 UNLOCK(&ctx->lock);
443 return (result);
446 #ifndef ISC_PLATFORM_USETHREADS
448 * Event loop for nonthreaded programs.
450 static isc_result_t
451 evloop(isc__appctx_t *ctx) {
452 isc_result_t result;
454 while (!ctx->want_shutdown) {
455 int n;
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);
476 if (readytasks) {
477 tv.tv_sec = 0;
478 tv.tv_usec = 0;
479 tvp = &tv;
480 call_timer_dispatch = ISC_TRUE;
481 } else {
482 result = isc__timermgr_nextevent(ctx->timermgr, &when);
483 if (result != ISC_R_SUCCESS)
484 tvp = NULL;
485 else {
486 isc_uint64_t us;
488 TIME_NOW(&now);
489 us = isc_time_microdiff(&when, &now);
490 if (us == 0)
491 call_timer_dispatch = ISC_TRUE;
492 tv.tv_sec = us / 1000000;
493 tv.tv_usec = us % 1000000;
494 tvp = &tv;
498 swait = NULL;
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
514 * mode.
516 isc__timermgr_dispatch(ctx->timermgr);
518 if (n > 0)
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
536 * event loop.
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
543 * or reload.
545 static isc_boolean_t signalled = ISC_FALSE;
547 isc_result_t
548 isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
549 isc_result_t result;
551 UNUSED(cp);
552 UNUSED(mp);
554 INSIST(!in_recursive_evloop);
555 in_recursive_evloop = ISC_TRUE;
557 INSIST(*mp == 1); /* Mutex must be locked on entry. */
558 --*mp;
560 result = evloop(&isc_g_appctx);
561 if (result == ISC_R_RELOAD)
562 isc_g_appctx.want_reload = ISC_TRUE;
563 if (signalled) {
564 isc_g_appctx.want_shutdown = ISC_FALSE;
565 signalled = ISC_FALSE;
568 ++*mp;
569 in_recursive_evloop = ISC_FALSE;
570 return (ISC_R_SUCCESS);
573 isc_result_t
574 isc__nothread_signal_hack(isc_condition_t *cp) {
576 UNUSED(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 */
586 isc_result_t
587 isc__app_ctxrun(isc_appctx_t *ctx0) {
588 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
589 int result;
590 isc_event_t *event, *next_event;
591 isc_task_t *task;
592 #ifdef ISC_PLATFORM_USETHREADS
593 sigset_t sset;
594 char strbuf[ISC_STRERRORSIZE];
595 #ifdef HAVE_SIGWAIT
596 int sig;
597 #endif /* HAVE_SIGWAIT */
598 #endif /* ISC_PLATFORM_USETHREADS */
600 REQUIRE(VALID_APPCTX(ctx));
602 #ifdef HAVE_LINUXTHREADS
603 REQUIRE(main_thread == pthread_self());
604 #endif
606 LOCK(&ctx->lock);
608 if (!ctx->running) {
609 ctx->running = ISC_TRUE;
612 * Post any on-run events (in FIFO order).
614 for (event = ISC_LIST_HEAD(ctx->on_run);
615 event != NULL;
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);
626 UNLOCK(&ctx->lock);
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);
637 return (result);
638 #else /* ISC_PLATFORM_USETHREADS */
640 * BIND9 internal tools using multiple contexts do not
641 * rely on signal.
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
650 * sigwait().
652 while (!ctx->want_shutdown) {
653 #ifdef HAVE_SIGWAIT
654 if (isc_bind9) {
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",
666 strbuf);
667 return (ISC_R_UNEXPECTED);
670 #ifndef HAVE_UNIXWARE_SIGWAIT
671 result = sigwait(&sset, &sig);
672 if (result == 0) {
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);
681 if (sig >= 0) {
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 */
688 } else {
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);
697 break;
699 if (!ctx->want_reload)
700 WAIT(&ctx->ready, &ctx->readylock);
701 UNLOCK(&ctx->readylock);
703 #else /* Don't have sigwait(). */
704 if (isc_bind9) {
706 * BIND9 internal; single context:
707 * Install a signal handler for SIGHUP, then wait for
708 * all signals.
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",
718 strbuf);
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",
726 strbuf);
727 return (ISC_R_UNEXPECTED);
729 #endif
730 result = sigsuspend(&sset);
731 } else {
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);
740 break;
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)
754 exit(1);
757 return (ISC_R_SUCCESS);
758 #endif /* ISC_PLATFORM_USETHREADS */
761 isc_result_t
762 isc__app_run(void) {
763 return (isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx));
766 isc_result_t
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));
776 LOCK(&ctx->lock);
778 REQUIRE(ctx->running);
780 if (ctx->shutdown_requested)
781 want_kill = ISC_FALSE;
782 else
783 ctx->shutdown_requested = ISC_TRUE;
785 UNLOCK(&ctx->lock);
787 if (want_kill) {
788 if (isc_bind9 && ctx != &isc_g_appctx)
789 /* BIND9 internal, but using multiple contexts */
790 ctx->want_shutdown = ISC_TRUE;
791 else {
792 #ifndef ISC_PLATFORM_USETHREADS
793 ctx->want_shutdown = ISC_TRUE;
794 #else /* ISC_PLATFORM_USETHREADS */
795 #ifdef HAVE_LINUXTHREADS
796 if (isc_bind9) {
797 /* BIND9 internal, single context */
798 int result;
800 result = pthread_kill(main_thread, SIGTERM);
801 if (result != 0) {
802 isc__strerror(result,
803 strbuf, sizeof(strbuf));
804 UNEXPECTED_ERROR(__FILE__, __LINE__,
805 "isc_app_shutdown() "
806 "pthread_kill: %s",
807 strbuf);
808 return (ISC_R_UNEXPECTED);
811 #else
812 if (isc_bind9) {
813 /* BIND9 internal, single context */
814 if (kill(getpid(), SIGTERM) < 0) {
815 isc__strerror(errno,
816 strbuf, sizeof(strbuf));
817 UNEXPECTED_ERROR(__FILE__, __LINE__,
818 "isc_app_shutdown() "
819 "kill: %s", strbuf);
820 return (ISC_R_UNEXPECTED);
823 #endif /* HAVE_LINUXTHREADS */
824 else {
825 /* External, multiple contexts */
826 LOCK(&ctx->readylock);
827 ctx->want_shutdown = ISC_TRUE;
828 UNLOCK(&ctx->readylock);
829 SIGNAL(&ctx->ready);
831 #endif /* ISC_PLATFORM_USETHREADS */
835 return (ISC_R_SUCCESS);
838 isc_result_t
839 isc__app_shutdown(void) {
840 return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
843 isc_result_t
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];
849 #endif
851 REQUIRE(VALID_APPCTX(ctx));
853 LOCK(&ctx->lock);
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;
863 UNLOCK(&ctx->lock);
865 if (want_kill) {
866 if (isc_bind9 && ctx != &isc_g_appctx)
867 /* BIND9 internal, but using multiple contexts */
868 ctx->want_reload = ISC_TRUE;
869 else {
870 #ifndef ISC_PLATFORM_USETHREADS
871 ctx->want_reload = ISC_TRUE;
872 #else /* ISC_PLATFORM_USETHREADS */
873 #ifdef HAVE_LINUXTHREADS
874 if (isc_bind9) {
875 /* BIND9 internal, single context */
876 int result;
878 result = pthread_kill(main_thread, SIGHUP);
879 if (result != 0) {
880 isc__strerror(result,
881 strbuf, sizeof(strbuf));
882 UNEXPECTED_ERROR(__FILE__, __LINE__,
883 "isc_app_reload() "
884 "pthread_kill: %s",
885 strbuf);
886 return (ISC_R_UNEXPECTED);
889 #else
890 if (isc_bind9) {
891 /* BIND9 internal, single context */
892 if (kill(getpid(), SIGHUP) < 0) {
893 isc__strerror(errno,
894 strbuf, sizeof(strbuf));
895 UNEXPECTED_ERROR(__FILE__, __LINE__,
896 "isc_app_reload() "
897 "kill: %s", strbuf);
898 return (ISC_R_UNEXPECTED);
901 #endif /* HAVE_LINUXTHREADS */
902 else {
903 /* External, multiple contexts */
904 LOCK(&ctx->readylock);
905 ctx->want_reload = ISC_TRUE;
906 UNLOCK(&ctx->readylock);
907 SIGNAL(&ctx->ready);
909 #endif /* ISC_PLATFORM_USETHREADS */
913 return (ISC_R_SUCCESS);
916 isc_result_t
917 isc__app_reload(void) {
918 return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
921 void
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);
930 void
931 isc__app_finish(void) {
932 isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
935 void
936 isc__app_block(void) {
937 #ifdef ISC_PLATFORM_USETHREADS
938 sigset_t sset;
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 */
953 void
954 isc__app_unblock(void) {
955 #ifdef ISC_PLATFORM_USETHREADS
956 sigset_t sset;
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 */
974 isc_result_t
975 isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
976 isc__appctx_t *ctx;
978 REQUIRE(mctx != NULL);
979 REQUIRE(ctxp != NULL && *ctxp == NULL);
981 ctx = isc_mem_get(mctx, sizeof(*ctx));
982 if (ctx == NULL)
983 return (ISC_R_NOMEMORY);
985 ctx->common.impmagic = APPCTX_MAGIC;
986 ctx->common.magic = ISCAPI_APPCTX_MAGIC;
987 ctx->common.methods = &appmethods.methods;
989 ctx->mctx = NULL;
990 isc_mem_attach(mctx, &ctx->mctx);
992 ctx->taskmgr = NULL;
993 ctx->socketmgr = NULL;
994 ctx->timermgr = NULL;
996 *ctxp = (isc_appctx_t *)ctx;
998 return (ISC_R_SUCCESS);
1001 void
1002 isc__appctx_destroy(isc_appctx_t **ctxp) {
1003 isc__appctx_t *ctx;
1005 REQUIRE(ctxp != NULL);
1006 ctx = (isc__appctx_t *)*ctxp;
1007 REQUIRE(VALID_APPCTX(ctx));
1009 isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
1011 *ctxp = NULL;
1014 void
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;
1023 void
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;
1032 void
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;
1041 isc_result_t
1042 isc__app_register(void) {
1043 return (isc_app_register(isc__appctx_create));
1046 #include "../app_api.c"