xref: /minix/external/bsd/bind/dist/lib/isc/unix/app.c (revision 00b67f09)
1 /*	$NetBSD: app.c,v 1.13 2015/07/08 17:28:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007-2009, 2013-2015  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2003  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /*! \file */
21 
22 #include <config.h>
23 
24 #include <sys/param.h>	/* Openserver 5.0.6A and FD_SETSIZE */
25 #include <sys/types.h>
26 
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
36 
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>
50 
51 #ifdef ISC_PLATFORM_USETHREADS
52 #include <pthread.h>
53 #endif
54 
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.
60  */
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 */
66 
67 #ifdef ISC_PLATFORM_USETHREADS
68 static pthread_t		blockedthread;
69 #endif /* ISC_PLATFORM_USETHREADS */
70 
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.
75  */
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);
98 
99 /*
100  * The application context of this module.  This implementation actually
101  * doesn't use it. (This may change in the future).
102  */
103 #define APPCTX_MAGIC		ISC_MAGIC('A', 'p', 'c', 'x')
104 #define VALID_APPCTX(c)		ISC_MAGIC_VALID(c, APPCTX_MAGIC)
105 
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;
113 
114 	/*!
115 	 * We assume that 'want_shutdown' can be read and written atomically.
116 	 */
117 	isc_boolean_t		want_shutdown;
118 	/*
119 	 * We assume that 'want_reload' can be read and written atomically.
120 	 */
121 	isc_boolean_t		want_reload;
122 
123 	isc_boolean_t		blocked;
124 
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;
133 
134 static isc__appctx_t isc_g_appctx;
135 
136 static struct {
137 	isc_appmethods_t methods;
138 
139 	/*%
140 	 * The following are defined just for avoiding unused static functions.
141 	 */
142 	void *run, *shutdown, *start, *reload, *finish, *block, *unblock;
143 } appmethods = {
144 	{
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
155 	},
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
163 };
164 
165 #ifdef HAVE_LINUXTHREADS
166 /*!
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.
171  */
172 #undef HAVE_SIGWAIT
173 /*!
174  * We need to remember which thread is the main thread...
175  */
176 static pthread_t		main_thread;
177 #endif
178 
179 #ifndef HAVE_SIGWAIT
180 static void
exit_action(int arg)181 exit_action(int arg) {
182 	UNUSED(arg);
183 	isc_g_appctx.want_shutdown = ISC_TRUE;
184 }
185 
186 static void
reload_action(int arg)187 reload_action(int arg) {
188 	UNUSED(arg);
189 	isc_g_appctx.want_reload = ISC_TRUE;
190 }
191 #endif
192 
193 static isc_result_t
handle_signal(int sig,void (* handler)(int))194 handle_signal(int sig, void (*handler)(int)) {
195 	struct sigaction sa;
196 	char strbuf[ISC_STRERRORSIZE];
197 
198 	memset(&sa, 0, sizeof(sa));
199 	sa.sa_handler = handler;
200 
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);
210 	}
211 
212 	return (ISC_R_SUCCESS);
213 }
214 
215 isc_result_t
isc__app_ctxstart(isc_appctx_t * ctx0)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];
222 
223 	REQUIRE(VALID_APPCTX(ctx));
224 
225 	/*
226 	 * Start an ISC library application.
227 	 */
228 
229 #ifdef NEED_PTHREAD_INIT
230 	/*
231 	 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
232 	 */
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);
239 	}
240 #endif
241 
242 #ifdef ISC_PLATFORM_USETHREADS
243 #ifdef HAVE_LINUXTHREADS
244 	main_thread = pthread_self();
245 #endif /* HAVE_LINUXTHREADS */
246 
247 	result = isc_mutex_init(&ctx->readylock);
248 	if (result != ISC_R_SUCCESS)
249 		return (result);
250 
251 	result = isc_condition_init(&ctx->ready);
252 	if (result != ISC_R_SUCCESS)
253 		goto cleanup_rlock;
254 
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 */
263 
264 	ISC_LIST_INIT(ctx->on_run);
265 
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;
271 
272 #ifndef HAVE_SIGWAIT
273 	/*
274 	 * Install do-nothing handlers for SIGINT and SIGTERM.
275 	 *
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().
279 	 */
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
287 
288 	/*
289 	 * Always ignore SIGPIPE.
290 	 */
291 	result = handle_signal(SIGPIPE, SIG_IGN);
292 	if (result != ISC_R_SUCCESS)
293 		goto cleanup;
294 
295 	/*
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.
303 	 */
304 	result = handle_signal(SIGHUP, SIG_DFL);
305 	if (result != ISC_R_SUCCESS)
306 		goto cleanup;
307 
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
316 
317 #ifdef ISC_PLATFORM_USETHREADS
318 	if (isc_bind9) {
319 	/*
320 	 * Block SIGHUP, SIGINT, SIGTERM.
321 	 *
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.
327 	 */
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;
337 	}
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;
346 	}
347 	}
348 #else /* ISC_PLATFORM_USETHREADS */
349 	/*
350 	 * Unblock SIGHUP, SIGINT, SIGTERM.
351 	 *
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.
355 	 */
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;
365 	}
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;
373 	}
374 #endif /* ISC_PLATFORM_USETHREADS */
375 
376 	return (ISC_R_SUCCESS);
377 
378  cleanup:
379 #ifdef ISC_PLATFORM_USETHREADS
380  cleanup_rcond:
381 	(void)isc_condition_destroy(&ctx->ready);
382 
383  cleanup_rlock:
384 	(void)isc_mutex_destroy(&ctx->readylock);
385 #endif /* ISC_PLATFORM_USETHREADS */
386 	return (result);
387 }
388 
389 isc_result_t
isc__app_start(void)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() */
396 
397 	return (isc__app_ctxstart((isc_appctx_t *)&isc_g_appctx));
398 }
399 
400 isc_result_t
isc__app_onrun(isc_mem_t * mctx,isc_task_t * task,isc_taskaction_t action,void * arg)401 isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
402 	      void *arg)
403 {
404 	return (isc__app_ctxonrun((isc_appctx_t *)&isc_g_appctx, mctx,
405 				  task, action, arg));
406 }
407 
408 isc_result_t
isc__app_ctxonrun(isc_appctx_t * ctx0,isc_mem_t * mctx,isc_task_t * task,isc_taskaction_t action,void * arg)409 isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
410 		  isc_taskaction_t action, void *arg)
411 {
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;
416 
417 	LOCK(&ctx->lock);
418 
419 	if (ctx->running) {
420 		result = ISC_R_ALREADYRUNNING;
421 		goto unlock;
422 	}
423 
424 	/*
425 	 * Note that we store the task to which we're going to send the event
426 	 * in the event's "sender" field.
427 	 */
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;
434 	}
435 
436 	ISC_LIST_APPEND(ctx->on_run, event, ev_link);
437 
438 	result = ISC_R_SUCCESS;
439 
440  unlock:
441 	UNLOCK(&ctx->lock);
442 
443 	return (result);
444 }
445 
446 #ifndef ISC_PLATFORM_USETHREADS
447 /*!
448  * Event loop for nonthreaded programs.
449  */
450 static isc_result_t
evloop(isc__appctx_t * ctx)451 evloop(isc__appctx_t *ctx) {
452 	isc_result_t result;
453 
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;
461 
462 		/*
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
469 		 */
470 		if (ctx->want_reload) {
471 			ctx->want_reload = ISC_FALSE;
472 			return (ISC_R_RELOAD);
473 		}
474 
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;
487 
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;
495 			}
496 		}
497 
498 		swait = NULL;
499 		n = isc__socketmgr_waitevents(ctx->socketmgr, tvp, &swait);
500 
501 		if (n == 0 || call_timer_dispatch) {
502 			/*
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.
515 			 */
516 			isc__timermgr_dispatch(ctx->timermgr);
517 		}
518 		if (n > 0)
519 			(void)isc__socketmgr_dispatch(ctx->socketmgr, swait);
520 		(void)isc__taskmgr_dispatch(ctx->taskmgr);
521 	}
522 	return (ISC_R_SUCCESS);
523 }
524 
525 /*
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().
532  */
533 
534 /*!
535  * \brief True if we are currently executing in the recursive
536  * event loop.
537  */
538 static isc_boolean_t in_recursive_evloop = ISC_FALSE;
539 
540 /*!
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.
544  */
545 static isc_boolean_t signalled = ISC_FALSE;
546 
547 isc_result_t
isc__nothread_wait_hack(isc_condition_t * cp,isc_mutex_t * mp)548 isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
549 	isc_result_t result;
550 
551 	UNUSED(cp);
552 	UNUSED(mp);
553 
554 	INSIST(!in_recursive_evloop);
555 	in_recursive_evloop = ISC_TRUE;
556 
557 	INSIST(*mp == 1); /* Mutex must be locked on entry. */
558 	--*mp;
559 
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;
566 	}
567 
568 	++*mp;
569 	in_recursive_evloop = ISC_FALSE;
570 	return (ISC_R_SUCCESS);
571 }
572 
573 isc_result_t
isc__nothread_signal_hack(isc_condition_t * cp)574 isc__nothread_signal_hack(isc_condition_t *cp) {
575 
576 	UNUSED(cp);
577 
578 	INSIST(in_recursive_evloop);
579 
580 	isc_g_appctx.want_shutdown = ISC_TRUE;
581 	signalled = ISC_TRUE;
582 	return (ISC_R_SUCCESS);
583 }
584 #endif /* ISC_PLATFORM_USETHREADS */
585 
586 isc_result_t
isc__app_ctxrun(isc_appctx_t * ctx0)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 */
599 
600 	REQUIRE(VALID_APPCTX(ctx));
601 
602 #ifdef HAVE_LINUXTHREADS
603 	REQUIRE(main_thread == pthread_self());
604 #endif
605 
606 	LOCK(&ctx->lock);
607 
608 	if (!ctx->running) {
609 		ctx->running = ISC_TRUE;
610 
611 		/*
612 		 * Post any on-run events (in FIFO order).
613 		 */
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);
622 		}
623 
624 	}
625 
626 	UNLOCK(&ctx->lock);
627 
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);
633 	}
634 
635 	(void) isc__taskmgr_dispatch(ctx->taskmgr);
636 	result = evloop(ctx);
637 	return (result);
638 #else /* ISC_PLATFORM_USETHREADS */
639 	/*
640 	 * BIND9 internal tools using multiple contexts do not
641 	 * rely on signal.
642 	 */
643 	if (isc_bind9 && ctx != &isc_g_appctx)
644 		return (ISC_R_SUCCESS);
645 
646 	/*
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().
651 	 */
652 	while (!ctx->want_shutdown) {
653 #ifdef HAVE_SIGWAIT
654 		if (isc_bind9) {
655 			/*
656 			 * BIND9 internal; single context:
657 			 * Wait for SIGHUP, SIGINT, or SIGTERM.
658 			 */
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);
668 			}
669 
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;
677 			}
678 
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;
686 			}
687 #endif /* HAVE_UNIXWARE_SIGWAIT */
688 		} else {
689 			/*
690 			 * External, or BIND9 using multiple contexts:
691 			 * wait until woken up.
692 			 */
693 			LOCK(&ctx->readylock);
694 			if (ctx->want_shutdown) {
695 				/* shutdown() won the race. */
696 				UNLOCK(&ctx->readylock);
697 				break;
698 			}
699 			if (!ctx->want_reload)
700 				WAIT(&ctx->ready, &ctx->readylock);
701 			UNLOCK(&ctx->readylock);
702 		}
703 #else  /* Don't have sigwait(). */
704 		if (isc_bind9) {
705 			/*
706 			 * BIND9 internal; single context:
707 			 * Install a signal handler for SIGHUP, then wait for
708 			 * all signals.
709 			 */
710 			result = handle_signal(SIGHUP, reload_action);
711 			if (result != ISC_R_SUCCESS)
712 				return (ISC_R_SUCCESS);
713 
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);
720 			}
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);
728 			}
729 #endif
730 			result = sigsuspend(&sset);
731 		} else {
732 			/*
733 			 * External, or BIND9 using multiple contexts:
734 			 * wait until woken up.
735 			 */
736 			LOCK(&ctx->readylock);
737 			if (ctx->want_shutdown) {
738 				/* shutdown() won the race. */
739 				UNLOCK(&ctx->readylock);
740 				break;
741 			}
742 			if (!ctx->want_reload)
743 				WAIT(&ctx->ready, &ctx->readylock);
744 			UNLOCK(&ctx->readylock);
745 		}
746 #endif /* HAVE_SIGWAIT */
747 
748 		if (ctx->want_reload) {
749 			ctx->want_reload = ISC_FALSE;
750 			return (ISC_R_RELOAD);
751 		}
752 
753 		if (ctx->want_shutdown && ctx->blocked)
754 			exit(1);
755 	}
756 
757 	return (ISC_R_SUCCESS);
758 #endif /* ISC_PLATFORM_USETHREADS */
759 }
760 
761 isc_result_t
isc__app_run(void)762 isc__app_run(void) {
763 	return (isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx));
764 }
765 
766 isc_result_t
isc__app_ctxshutdown(isc_appctx_t * ctx0)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 */
773 
774 	REQUIRE(VALID_APPCTX(ctx));
775 
776 	LOCK(&ctx->lock);
777 
778 	REQUIRE(ctx->running);
779 
780 	if (ctx->shutdown_requested)
781 		want_kill = ISC_FALSE;
782 	else
783 		ctx->shutdown_requested = ISC_TRUE;
784 
785 	UNLOCK(&ctx->lock);
786 
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;
799 
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);
809 				}
810 			}
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);
821 				}
822 			}
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);
830 			}
831 #endif /* ISC_PLATFORM_USETHREADS */
832 		}
833 	}
834 
835 	return (ISC_R_SUCCESS);
836 }
837 
838 isc_result_t
isc__app_shutdown(void)839 isc__app_shutdown(void) {
840 	return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
841 }
842 
843 isc_result_t
isc__app_ctxsuspend(isc_appctx_t * ctx0)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
850 
851 	REQUIRE(VALID_APPCTX(ctx));
852 
853 	LOCK(&ctx->lock);
854 
855 	REQUIRE(ctx->running);
856 
857 	/*
858 	 * Don't send the reload signal if we're shutting down.
859 	 */
860 	if (ctx->shutdown_requested)
861 		want_kill = ISC_FALSE;
862 
863 	UNLOCK(&ctx->lock);
864 
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;
877 
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);
887 				}
888 			}
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);
899 				}
900 			}
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);
908 			}
909 #endif /* ISC_PLATFORM_USETHREADS */
910 		}
911 	}
912 
913 	return (ISC_R_SUCCESS);
914 }
915 
916 isc_result_t
isc__app_reload(void)917 isc__app_reload(void) {
918 	return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
919 }
920 
921 void
isc__app_ctxfinish(isc_appctx_t * ctx0)922 isc__app_ctxfinish(isc_appctx_t *ctx0) {
923 	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
924 
925 	REQUIRE(VALID_APPCTX(ctx));
926 
927 	DESTROYLOCK(&ctx->lock);
928 }
929 
930 void
isc__app_finish(void)931 isc__app_finish(void) {
932 	isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
933 }
934 
935 void
isc__app_block(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);
942 
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 */
951 }
952 
953 void
isc__app_unblock(void)954 isc__app_unblock(void) {
955 #ifdef ISC_PLATFORM_USETHREADS
956 	sigset_t sset;
957 #endif /* ISC_PLATFORM_USETHREADS */
958 
959 	REQUIRE(isc_g_appctx.running);
960 	REQUIRE(isc_g_appctx.blocked);
961 
962 	isc_g_appctx.blocked = ISC_FALSE;
963 
964 #ifdef ISC_PLATFORM_USETHREADS
965 	REQUIRE(blockedthread == pthread_self());
966 
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 */
972 }
973 
974 isc_result_t
isc__appctx_create(isc_mem_t * mctx,isc_appctx_t ** ctxp)975 isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
976 	isc__appctx_t *ctx;
977 
978 	REQUIRE(mctx != NULL);
979 	REQUIRE(ctxp != NULL && *ctxp == NULL);
980 
981 	ctx = isc_mem_get(mctx, sizeof(*ctx));
982 	if (ctx == NULL)
983 		return (ISC_R_NOMEMORY);
984 
985 	ctx->common.impmagic = APPCTX_MAGIC;
986 	ctx->common.magic = ISCAPI_APPCTX_MAGIC;
987 	ctx->common.methods = &appmethods.methods;
988 
989 	ctx->mctx = NULL;
990 	isc_mem_attach(mctx, &ctx->mctx);
991 
992 	ctx->taskmgr = NULL;
993 	ctx->socketmgr = NULL;
994 	ctx->timermgr = NULL;
995 
996 	*ctxp = (isc_appctx_t *)ctx;
997 
998 	return (ISC_R_SUCCESS);
999 }
1000 
1001 void
isc__appctx_destroy(isc_appctx_t ** ctxp)1002 isc__appctx_destroy(isc_appctx_t **ctxp) {
1003 	isc__appctx_t *ctx;
1004 
1005 	REQUIRE(ctxp != NULL);
1006 	ctx = (isc__appctx_t *)*ctxp;
1007 	REQUIRE(VALID_APPCTX(ctx));
1008 
1009 	isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
1010 
1011 	*ctxp = NULL;
1012 }
1013 
1014 void
isc__appctx_settaskmgr(isc_appctx_t * ctx0,isc_taskmgr_t * taskmgr)1015 isc__appctx_settaskmgr(isc_appctx_t *ctx0, isc_taskmgr_t *taskmgr) {
1016 	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
1017 
1018 	REQUIRE(VALID_APPCTX(ctx));
1019 
1020 	ctx->taskmgr = taskmgr;
1021 }
1022 
1023 void
isc__appctx_setsocketmgr(isc_appctx_t * ctx0,isc_socketmgr_t * socketmgr)1024 isc__appctx_setsocketmgr(isc_appctx_t *ctx0, isc_socketmgr_t *socketmgr) {
1025 	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
1026 
1027 	REQUIRE(VALID_APPCTX(ctx));
1028 
1029 	ctx->socketmgr = socketmgr;
1030 }
1031 
1032 void
isc__appctx_settimermgr(isc_appctx_t * ctx0,isc_timermgr_t * timermgr)1033 isc__appctx_settimermgr(isc_appctx_t *ctx0, isc_timermgr_t *timermgr) {
1034 	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
1035 
1036 	REQUIRE(VALID_APPCTX(ctx));
1037 
1038 	ctx->timermgr = timermgr;
1039 }
1040 
1041 isc_result_t
isc__app_register(void)1042 isc__app_register(void) {
1043 	return (isc_app_register(isc__appctx_create));
1044 }
1045 
1046 #include "../app_api.c"
1047