1258945Sroberto/*
2280849Scy * Copyright (C) 2004, 2005, 2007-2009  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 1999-2003  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18280849Scy/* $Id: app.c,v 1.64 2009/11/04 05:58:46 marka Exp $ */
19258945Sroberto
20258945Sroberto/*! \file */
21258945Sroberto
22258945Sroberto#include <config.h>
23258945Sroberto
24258945Sroberto#include <sys/param.h>	/* Openserver 5.0.6A and FD_SETSIZE */
25258945Sroberto#include <sys/types.h>
26258945Sroberto
27258945Sroberto#include <stddef.h>
28258945Sroberto#include <stdlib.h>
29258945Sroberto#include <errno.h>
30258945Sroberto#include <unistd.h>
31258945Sroberto#include <signal.h>
32258945Sroberto#include <sys/time.h>
33258945Sroberto#ifdef HAVE_EPOLL
34258945Sroberto#include <sys/epoll.h>
35258945Sroberto#endif
36258945Sroberto
37258945Sroberto#include <isc/app.h>
38258945Sroberto#include <isc/boolean.h>
39258945Sroberto#include <isc/condition.h>
40280849Scy#include <isc/mem.h>
41258945Sroberto#include <isc/msgs.h>
42258945Sroberto#include <isc/mutex.h>
43258945Sroberto#include <isc/event.h>
44258945Sroberto#include <isc/platform.h>
45258945Sroberto#include <isc/strerror.h>
46258945Sroberto#include <isc/string.h>
47258945Sroberto#include <isc/task.h>
48258945Sroberto#include <isc/time.h>
49258945Sroberto#include <isc/util.h>
50258945Sroberto
51280849Scy/*%
52280849Scy * For BIND9 internal applications built with threads, we use a single app
53280849Scy * context and let multiple worker, I/O, timer threads do actual jobs.
54280849Scy * For other cases (including BIND9 built without threads) an app context acts
55280849Scy * as an event loop dispatching various events.
56280849Scy */
57280849Scy#if defined(ISC_PLATFORM_USETHREADS) && defined(BIND9)
58280849Scy#define USE_THREADS_SINGLECTX
59280849Scy#endif
60280849Scy
61258945Sroberto#ifdef ISC_PLATFORM_USETHREADS
62258945Sroberto#include <pthread.h>
63280849Scy#endif
64280849Scy
65280849Scy#ifndef USE_THREADS_SINGLECTX
66258945Sroberto#include "../timer_p.h"
67258945Sroberto#include "../task_p.h"
68258945Sroberto#include "socket_p.h"
69280849Scy#endif /* USE_THREADS_SINGLECTX */
70280849Scy
71280849Scy#ifdef ISC_PLATFORM_USETHREADS
72280849Scystatic pthread_t		blockedthread;
73258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
74258945Sroberto
75280849Scy/*%
76280849Scy * The following can be either static or public, depending on build environment.
77258945Sroberto */
78280849Scy
79280849Scy#ifdef BIND9
80280849Scy#define ISC_APPFUNC_SCOPE
81280849Scy#else
82280849Scy#define ISC_APPFUNC_SCOPE static
83280849Scy#endif
84280849Scy
85280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_start(void);
86280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_ctxstart(isc_appctx_t *ctx);
87280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_onrun(isc_mem_t *mctx,
88280849Scy					      isc_task_t *task,
89280849Scy					      isc_taskaction_t action,
90280849Scy					      void *arg);
91280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_ctxrun(isc_appctx_t *ctx);
92280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_run(void);
93280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_ctxshutdown(isc_appctx_t *ctx);
94280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_shutdown(void);
95280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_reload(void);
96280849ScyISC_APPFUNC_SCOPE isc_result_t isc__app_ctxsuspend(isc_appctx_t *ctx);
97280849ScyISC_APPFUNC_SCOPE void isc__app_ctxfinish(isc_appctx_t *ctx);
98280849ScyISC_APPFUNC_SCOPE void isc__app_finish(void);
99280849ScyISC_APPFUNC_SCOPE void isc__app_block(void);
100280849ScyISC_APPFUNC_SCOPE void isc__app_unblock(void);
101280849ScyISC_APPFUNC_SCOPE isc_result_t isc__appctx_create(isc_mem_t *mctx,
102280849Scy						  isc_appctx_t **ctxp);
103280849ScyISC_APPFUNC_SCOPE void isc__appctx_destroy(isc_appctx_t **ctxp);
104280849ScyISC_APPFUNC_SCOPE void isc__appctx_settaskmgr(isc_appctx_t *ctx,
105280849Scy					      isc_taskmgr_t *taskmgr);
106280849ScyISC_APPFUNC_SCOPE void isc__appctx_setsocketmgr(isc_appctx_t *ctx,
107280849Scy						isc_socketmgr_t *socketmgr);
108280849ScyISC_APPFUNC_SCOPE void isc__appctx_settimermgr(isc_appctx_t *ctx,
109280849Scy					       isc_timermgr_t *timermgr);
110280849Scy
111258945Sroberto/*
112280849Scy * The application context of this module.  This implementation actually
113280849Scy * doesn't use it. (This may change in the future).
114258945Sroberto */
115280849Scy#define APPCTX_MAGIC		ISC_MAGIC('A', 'p', 'c', 'x')
116280849Scy#define VALID_APPCTX(c)		ISC_MAGIC_VALID(c, APPCTX_MAGIC)
117258945Sroberto
118280849Scytypedef struct isc__appctx {
119280849Scy	isc_appctx_t		common;
120280849Scy	isc_mem_t		*mctx;
121280849Scy	isc_mutex_t		lock;
122280849Scy	isc_eventlist_t		on_run;
123280849Scy	isc_boolean_t		shutdown_requested;
124280849Scy	isc_boolean_t		running;
125258945Sroberto
126280849Scy	/*!
127280849Scy	 * We assume that 'want_shutdown' can be read and written atomically.
128280849Scy	 */
129280849Scy	isc_boolean_t		want_shutdown;
130280849Scy	/*
131280849Scy	 * We assume that 'want_reload' can be read and written atomically.
132280849Scy	 */
133280849Scy	isc_boolean_t		want_reload;
134280849Scy
135280849Scy	isc_boolean_t		blocked;
136280849Scy
137280849Scy	isc_taskmgr_t		*taskmgr;
138280849Scy	isc_socketmgr_t		*socketmgr;
139280849Scy	isc_timermgr_t		*timermgr;
140280849Scy} isc__appctx_t;
141280849Scy
142280849Scystatic isc__appctx_t isc_g_appctx;
143280849Scy
144280849Scystatic struct {
145280849Scy	isc_appmethods_t methods;
146280849Scy
147280849Scy	/*%
148280849Scy	 * The following are defined just for avoiding unused static functions.
149280849Scy	 */
150280849Scy#ifndef BIND9
151280849Scy	void *run, *shutdown, *start, *onrun, *reload, *finish,
152280849Scy		*block, *unblock;
153280849Scy#endif
154280849Scy} appmethods = {
155280849Scy	{
156280849Scy		isc__appctx_destroy,
157280849Scy		isc__app_ctxstart,
158280849Scy		isc__app_ctxrun,
159280849Scy		isc__app_ctxsuspend,
160280849Scy		isc__app_ctxshutdown,
161280849Scy		isc__app_ctxfinish,
162280849Scy		isc__appctx_settaskmgr,
163280849Scy		isc__appctx_setsocketmgr,
164280849Scy		isc__appctx_settimermgr
165280849Scy	}
166280849Scy#ifndef BIND9
167280849Scy	,
168280849Scy	(void *)isc__app_run, (void *)isc__app_shutdown,
169280849Scy	(void *)isc__app_start, (void *)isc__app_onrun, (void *)isc__app_reload,
170280849Scy	(void *)isc__app_finish, (void *)isc__app_block,
171280849Scy	(void *)isc__app_unblock
172280849Scy#endif
173280849Scy};
174280849Scy
175258945Sroberto#ifdef HAVE_LINUXTHREADS
176258945Sroberto/*!
177258945Sroberto * Linux has sigwait(), but it appears to prevent signal handlers from
178258945Sroberto * running, even if they're not in the set being waited for.  This makes
179258945Sroberto * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
180258945Sroberto * Instead of messing with it, we just use sigsuspend() instead.
181258945Sroberto */
182258945Sroberto#undef HAVE_SIGWAIT
183258945Sroberto/*!
184258945Sroberto * We need to remember which thread is the main thread...
185258945Sroberto */
186258945Srobertostatic pthread_t		main_thread;
187258945Sroberto#endif
188258945Sroberto
189258945Sroberto#ifndef HAVE_SIGWAIT
190258945Srobertostatic void
191258945Srobertoexit_action(int arg) {
192258945Sroberto	UNUSED(arg);
193280849Scy	isc_g_appctx.want_shutdown = ISC_TRUE;
194258945Sroberto}
195258945Sroberto
196258945Srobertostatic void
197258945Srobertoreload_action(int arg) {
198258945Sroberto	UNUSED(arg);
199280849Scy	isc_g_appctx.want_reload = ISC_TRUE;
200258945Sroberto}
201258945Sroberto#endif
202258945Sroberto
203258945Srobertostatic isc_result_t
204258945Srobertohandle_signal(int sig, void (*handler)(int)) {
205258945Sroberto	struct sigaction sa;
206258945Sroberto	char strbuf[ISC_STRERRORSIZE];
207258945Sroberto
208258945Sroberto	memset(&sa, 0, sizeof(sa));
209258945Sroberto	sa.sa_handler = handler;
210258945Sroberto
211258945Sroberto	if (sigfillset(&sa.sa_mask) != 0 ||
212258945Sroberto	    sigaction(sig, &sa, NULL) < 0) {
213258945Sroberto		isc__strerror(errno, strbuf, sizeof(strbuf));
214258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
215258945Sroberto				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
216258945Sroberto					       ISC_MSG_SIGNALSETUP,
217258945Sroberto					       "handle_signal() %d setup: %s"),
218258945Sroberto				 sig, strbuf);
219258945Sroberto		return (ISC_R_UNEXPECTED);
220258945Sroberto	}
221258945Sroberto
222258945Sroberto	return (ISC_R_SUCCESS);
223258945Sroberto}
224258945Sroberto
225280849ScyISC_APPFUNC_SCOPE isc_result_t
226280849Scyisc__app_ctxstart(isc_appctx_t *ctx0) {
227280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
228258945Sroberto	isc_result_t result;
229258945Sroberto
230280849Scy	REQUIRE(VALID_APPCTX(ctx));
231280849Scy
232258945Sroberto	/*
233258945Sroberto	 * Start an ISC library application.
234258945Sroberto	 */
235258945Sroberto
236258945Sroberto#ifdef NEED_PTHREAD_INIT
237258945Sroberto	/*
238258945Sroberto	 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
239258945Sroberto	 */
240258945Sroberto	presult = pthread_init();
241258945Sroberto	if (presult != 0) {
242258945Sroberto		isc__strerror(presult, strbuf, sizeof(strbuf));
243258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
244258945Sroberto				 "isc_app_start() pthread_init: %s", strbuf);
245258945Sroberto		return (ISC_R_UNEXPECTED);
246258945Sroberto	}
247258945Sroberto#endif
248258945Sroberto
249258945Sroberto#ifdef HAVE_LINUXTHREADS
250258945Sroberto	main_thread = pthread_self();
251258945Sroberto#endif
252258945Sroberto
253280849Scy	result = isc_mutex_init(&ctx->lock);
254258945Sroberto	if (result != ISC_R_SUCCESS)
255258945Sroberto		return (result);
256258945Sroberto
257280849Scy	ISC_LIST_INIT(ctx->on_run);
258280849Scy
259280849Scy	ctx->shutdown_requested = ISC_FALSE;
260280849Scy	ctx->running = ISC_FALSE;
261280849Scy	ctx->want_shutdown = ISC_FALSE;
262280849Scy	ctx->want_reload = ISC_FALSE;
263280849Scy	ctx->blocked = ISC_FALSE;
264280849Scy
265280849Scy	return (ISC_R_SUCCESS);
266280849Scy}
267280849Scy
268280849ScyISC_APPFUNC_SCOPE isc_result_t
269280849Scyisc__app_start(void) {
270280849Scy	isc_result_t result;
271280849Scy	int presult;
272280849Scy	sigset_t sset;
273280849Scy	char strbuf[ISC_STRERRORSIZE];
274280849Scy
275280849Scy	isc_g_appctx.common.impmagic = APPCTX_MAGIC;
276280849Scy	isc_g_appctx.common.magic = ISCAPI_APPCTX_MAGIC;
277280849Scy	isc_g_appctx.common.methods = &appmethods.methods;
278280849Scy	isc_g_appctx.mctx = NULL;
279280849Scy	/* The remaining members will be initialized in ctxstart() */
280280849Scy
281280849Scy	result = isc__app_ctxstart((isc_appctx_t *)&isc_g_appctx);
282280849Scy	if (result != ISC_R_SUCCESS)
283280849Scy		return (result);
284280849Scy
285258945Sroberto#ifndef HAVE_SIGWAIT
286258945Sroberto	/*
287258945Sroberto	 * Install do-nothing handlers for SIGINT and SIGTERM.
288258945Sroberto	 *
289258945Sroberto	 * We install them now because BSDI 3.1 won't block
290258945Sroberto	 * the default actions, regardless of what we do with
291258945Sroberto	 * pthread_sigmask().
292258945Sroberto	 */
293258945Sroberto	result = handle_signal(SIGINT, exit_action);
294258945Sroberto	if (result != ISC_R_SUCCESS)
295258945Sroberto		return (result);
296258945Sroberto	result = handle_signal(SIGTERM, exit_action);
297258945Sroberto	if (result != ISC_R_SUCCESS)
298258945Sroberto		return (result);
299258945Sroberto#endif
300258945Sroberto
301258945Sroberto	/*
302258945Sroberto	 * Always ignore SIGPIPE.
303258945Sroberto	 */
304258945Sroberto	result = handle_signal(SIGPIPE, SIG_IGN);
305258945Sroberto	if (result != ISC_R_SUCCESS)
306258945Sroberto		return (result);
307258945Sroberto
308258945Sroberto	/*
309258945Sroberto	 * On Solaris 2, delivery of a signal whose action is SIG_IGN
310258945Sroberto	 * will not cause sigwait() to return. We may have inherited
311258945Sroberto	 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
312258945Sroberto	 * process (e.g, Solaris cron).  Set an action of SIG_DFL to make
313258945Sroberto	 * sure sigwait() works as expected.  Only do this for SIGTERM and
314258945Sroberto	 * SIGINT if we don't have sigwait(), since a different handler is
315258945Sroberto	 * installed above.
316258945Sroberto	 */
317258945Sroberto	result = handle_signal(SIGHUP, SIG_DFL);
318258945Sroberto	if (result != ISC_R_SUCCESS)
319258945Sroberto		return (result);
320258945Sroberto
321258945Sroberto#ifdef HAVE_SIGWAIT
322258945Sroberto	result = handle_signal(SIGTERM, SIG_DFL);
323258945Sroberto	if (result != ISC_R_SUCCESS)
324258945Sroberto		return (result);
325258945Sroberto	result = handle_signal(SIGINT, SIG_DFL);
326258945Sroberto	if (result != ISC_R_SUCCESS)
327258945Sroberto		return (result);
328258945Sroberto#endif
329258945Sroberto
330258945Sroberto#ifdef ISC_PLATFORM_USETHREADS
331258945Sroberto	/*
332258945Sroberto	 * Block SIGHUP, SIGINT, SIGTERM.
333258945Sroberto	 *
334258945Sroberto	 * If isc_app_start() is called from the main thread before any other
335258945Sroberto	 * threads have been created, then the pthread_sigmask() call below
336258945Sroberto	 * will result in all threads having SIGHUP, SIGINT and SIGTERM
337258945Sroberto	 * blocked by default, ensuring that only the thread that calls
338258945Sroberto	 * sigwait() for them will get those signals.
339258945Sroberto	 */
340258945Sroberto	if (sigemptyset(&sset) != 0 ||
341258945Sroberto	    sigaddset(&sset, SIGHUP) != 0 ||
342258945Sroberto	    sigaddset(&sset, SIGINT) != 0 ||
343258945Sroberto	    sigaddset(&sset, SIGTERM) != 0) {
344258945Sroberto		isc__strerror(errno, strbuf, sizeof(strbuf));
345258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
346258945Sroberto				 "isc_app_start() sigsetops: %s", strbuf);
347258945Sroberto		return (ISC_R_UNEXPECTED);
348258945Sroberto	}
349258945Sroberto	presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
350258945Sroberto	if (presult != 0) {
351258945Sroberto		isc__strerror(presult, strbuf, sizeof(strbuf));
352258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
353258945Sroberto				 "isc_app_start() pthread_sigmask: %s",
354258945Sroberto				 strbuf);
355258945Sroberto		return (ISC_R_UNEXPECTED);
356258945Sroberto	}
357258945Sroberto#else /* ISC_PLATFORM_USETHREADS */
358258945Sroberto	/*
359258945Sroberto	 * Unblock SIGHUP, SIGINT, SIGTERM.
360258945Sroberto	 *
361258945Sroberto	 * If we're not using threads, we need to make sure that SIGHUP,
362258945Sroberto	 * SIGINT and SIGTERM are not inherited as blocked from the parent
363258945Sroberto	 * process.
364258945Sroberto	 */
365258945Sroberto	if (sigemptyset(&sset) != 0 ||
366258945Sroberto	    sigaddset(&sset, SIGHUP) != 0 ||
367258945Sroberto	    sigaddset(&sset, SIGINT) != 0 ||
368258945Sroberto	    sigaddset(&sset, SIGTERM) != 0) {
369258945Sroberto		isc__strerror(errno, strbuf, sizeof(strbuf));
370258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
371258945Sroberto				 "isc_app_start() sigsetops: %s", strbuf);
372258945Sroberto		return (ISC_R_UNEXPECTED);
373258945Sroberto	}
374258945Sroberto	presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
375258945Sroberto	if (presult != 0) {
376258945Sroberto		isc__strerror(presult, strbuf, sizeof(strbuf));
377258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
378258945Sroberto				 "isc_app_start() sigprocmask: %s", strbuf);
379258945Sroberto		return (ISC_R_UNEXPECTED);
380258945Sroberto	}
381258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
382258945Sroberto
383258945Sroberto	return (ISC_R_SUCCESS);
384258945Sroberto}
385258945Sroberto
386280849ScyISC_APPFUNC_SCOPE isc_result_t
387280849Scyisc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
388258945Sroberto	      void *arg)
389258945Sroberto{
390258945Sroberto	isc_event_t *event;
391258945Sroberto	isc_task_t *cloned_task = NULL;
392258945Sroberto	isc_result_t result;
393258945Sroberto
394280849Scy	LOCK(&isc_g_appctx.lock);
395258945Sroberto
396280849Scy	if (isc_g_appctx.running) {
397258945Sroberto		result = ISC_R_ALREADYRUNNING;
398258945Sroberto		goto unlock;
399258945Sroberto	}
400258945Sroberto
401258945Sroberto	/*
402258945Sroberto	 * Note that we store the task to which we're going to send the event
403258945Sroberto	 * in the event's "sender" field.
404258945Sroberto	 */
405258945Sroberto	isc_task_attach(task, &cloned_task);
406258945Sroberto	event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
407258945Sroberto				   action, arg, sizeof(*event));
408258945Sroberto	if (event == NULL) {
409258945Sroberto		result = ISC_R_NOMEMORY;
410258945Sroberto		goto unlock;
411258945Sroberto	}
412258945Sroberto
413280849Scy	ISC_LIST_APPEND(isc_g_appctx.on_run, event, ev_link);
414258945Sroberto
415258945Sroberto	result = ISC_R_SUCCESS;
416258945Sroberto
417258945Sroberto unlock:
418280849Scy	UNLOCK(&isc_g_appctx.lock);
419258945Sroberto
420258945Sroberto	return (result);
421258945Sroberto}
422258945Sroberto
423280849Scy#ifndef USE_THREADS_SINGLECTX
424258945Sroberto/*!
425258945Sroberto * Event loop for nonthreaded programs.
426258945Sroberto */
427258945Srobertostatic isc_result_t
428280849Scyevloop(isc__appctx_t *ctx) {
429258945Sroberto	isc_result_t result;
430280849Scy
431280849Scy	while (!ctx->want_shutdown) {
432258945Sroberto		int n;
433258945Sroberto		isc_time_t when, now;
434258945Sroberto		struct timeval tv, *tvp;
435258945Sroberto		isc_socketwait_t *swait;
436258945Sroberto		isc_boolean_t readytasks;
437258945Sroberto		isc_boolean_t call_timer_dispatch = ISC_FALSE;
438258945Sroberto
439280849Scy		/*
440280849Scy		 * Check the reload (or suspend) case first for exiting the
441280849Scy		 * loop as fast as possible in case:
442280849Scy		 *   - the direct call to isc__taskmgr_dispatch() in
443280849Scy		 *     isc__app_ctxrun() completes all the tasks so far,
444280849Scy		 *   - there is thus currently no active task, and
445280849Scy		 *   - there is a timer event
446280849Scy		 */
447280849Scy		if (ctx->want_reload) {
448280849Scy			ctx->want_reload = ISC_FALSE;
449280849Scy			return (ISC_R_RELOAD);
450280849Scy		}
451280849Scy
452280849Scy		readytasks = isc__taskmgr_ready(ctx->taskmgr);
453258945Sroberto		if (readytasks) {
454258945Sroberto			tv.tv_sec = 0;
455258945Sroberto			tv.tv_usec = 0;
456258945Sroberto			tvp = &tv;
457258945Sroberto			call_timer_dispatch = ISC_TRUE;
458258945Sroberto		} else {
459280849Scy			result = isc__timermgr_nextevent(ctx->timermgr, &when);
460258945Sroberto			if (result != ISC_R_SUCCESS)
461258945Sroberto				tvp = NULL;
462258945Sroberto			else {
463258945Sroberto				isc_uint64_t us;
464258945Sroberto
465258945Sroberto				TIME_NOW(&now);
466258945Sroberto				us = isc_time_microdiff(&when, &now);
467258945Sroberto				if (us == 0)
468258945Sroberto					call_timer_dispatch = ISC_TRUE;
469258945Sroberto				tv.tv_sec = us / 1000000;
470258945Sroberto				tv.tv_usec = us % 1000000;
471258945Sroberto				tvp = &tv;
472258945Sroberto			}
473258945Sroberto		}
474258945Sroberto
475258945Sroberto		swait = NULL;
476280849Scy		n = isc__socketmgr_waitevents(ctx->socketmgr, tvp, &swait);
477258945Sroberto
478258945Sroberto		if (n == 0 || call_timer_dispatch) {
479258945Sroberto			/*
480258945Sroberto			 * We call isc__timermgr_dispatch() only when
481258945Sroberto			 * necessary, in order to reduce overhead.  If the
482258945Sroberto			 * select() call indicates a timeout, we need the
483258945Sroberto			 * dispatch.  Even if not, if we set the 0-timeout
484258945Sroberto			 * for the select() call, we need to check the timer
485258945Sroberto			 * events.  In the 'readytasks' case, there may be no
486258945Sroberto			 * timeout event actually, but there is no other way
487258945Sroberto			 * to reduce the overhead.
488258945Sroberto			 * Note that we do not have to worry about the case
489258945Sroberto			 * where a new timer is inserted during the select()
490258945Sroberto			 * call, since this loop only runs in the non-thread
491258945Sroberto			 * mode.
492258945Sroberto			 */
493280849Scy			isc__timermgr_dispatch(ctx->timermgr);
494258945Sroberto		}
495258945Sroberto		if (n > 0)
496280849Scy			(void)isc__socketmgr_dispatch(ctx->socketmgr, swait);
497280849Scy		(void)isc__taskmgr_dispatch(ctx->taskmgr);
498258945Sroberto	}
499258945Sroberto	return (ISC_R_SUCCESS);
500258945Sroberto}
501280849Scy#endif	/* USE_THREADS_SINGLECTX */
502258945Sroberto
503280849Scy#ifndef ISC_PLATFORM_USETHREADS
504258945Sroberto/*
505258945Sroberto * This is a gross hack to support waiting for condition
506258945Sroberto * variables in nonthreaded programs in a limited way;
507258945Sroberto * see lib/isc/nothreads/include/isc/condition.h.
508258945Sroberto * We implement isc_condition_wait() by entering the
509258945Sroberto * event loop recursively until the want_shutdown flag
510258945Sroberto * is set by isc_condition_signal().
511258945Sroberto */
512258945Sroberto
513258945Sroberto/*!
514258945Sroberto * \brief True if we are currently executing in the recursive
515258945Sroberto * event loop.
516258945Sroberto */
517258945Srobertostatic isc_boolean_t in_recursive_evloop = ISC_FALSE;
518258945Sroberto
519258945Sroberto/*!
520258945Sroberto * \brief True if we are exiting the event loop as the result of
521258945Sroberto * a call to isc_condition_signal() rather than a shutdown
522258945Sroberto * or reload.
523258945Sroberto */
524258945Srobertostatic isc_boolean_t signalled = ISC_FALSE;
525258945Sroberto
526258945Srobertoisc_result_t
527258945Srobertoisc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
528258945Sroberto	isc_result_t result;
529258945Sroberto
530258945Sroberto	UNUSED(cp);
531258945Sroberto	UNUSED(mp);
532258945Sroberto
533258945Sroberto	INSIST(!in_recursive_evloop);
534258945Sroberto	in_recursive_evloop = ISC_TRUE;
535258945Sroberto
536258945Sroberto	INSIST(*mp == 1); /* Mutex must be locked on entry. */
537258945Sroberto	--*mp;
538258945Sroberto
539280849Scy	result = evloop(&isc_g_appctx);
540258945Sroberto	if (result == ISC_R_RELOAD)
541280849Scy		isc_g_appctx.want_reload = ISC_TRUE;
542258945Sroberto	if (signalled) {
543280849Scy		isc_g_appctx.want_shutdown = ISC_FALSE;
544258945Sroberto		signalled = ISC_FALSE;
545258945Sroberto	}
546258945Sroberto
547258945Sroberto	++*mp;
548258945Sroberto	in_recursive_evloop = ISC_FALSE;
549258945Sroberto	return (ISC_R_SUCCESS);
550258945Sroberto}
551258945Sroberto
552258945Srobertoisc_result_t
553258945Srobertoisc__nothread_signal_hack(isc_condition_t *cp) {
554258945Sroberto
555258945Sroberto	UNUSED(cp);
556258945Sroberto
557258945Sroberto	INSIST(in_recursive_evloop);
558258945Sroberto
559280849Scy	isc_g_appctx.want_shutdown = ISC_TRUE;
560258945Sroberto	signalled = ISC_TRUE;
561258945Sroberto	return (ISC_R_SUCCESS);
562258945Sroberto}
563258945Sroberto
564258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
565258945Sroberto
566280849ScyISC_APPFUNC_SCOPE isc_result_t
567280849Scyisc__app_ctxrun(isc_appctx_t *ctx0) {
568280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
569258945Sroberto	int result;
570258945Sroberto	isc_event_t *event, *next_event;
571258945Sroberto	isc_task_t *task;
572280849Scy#ifdef USE_THREADS_SINGLECTX
573258945Sroberto	sigset_t sset;
574258945Sroberto	char strbuf[ISC_STRERRORSIZE];
575258945Sroberto#ifdef HAVE_SIGWAIT
576258945Sroberto	int sig;
577258945Sroberto#endif
578280849Scy#endif /* USE_THREADS_SINGLECTX */
579258945Sroberto
580280849Scy	REQUIRE(VALID_APPCTX(ctx));
581280849Scy
582258945Sroberto#ifdef HAVE_LINUXTHREADS
583258945Sroberto	REQUIRE(main_thread == pthread_self());
584258945Sroberto#endif
585258945Sroberto
586280849Scy	LOCK(&ctx->lock);
587258945Sroberto
588280849Scy	if (!ctx->running) {
589280849Scy		ctx->running = ISC_TRUE;
590258945Sroberto
591258945Sroberto		/*
592258945Sroberto		 * Post any on-run events (in FIFO order).
593258945Sroberto		 */
594280849Scy		for (event = ISC_LIST_HEAD(ctx->on_run);
595258945Sroberto		     event != NULL;
596258945Sroberto		     event = next_event) {
597258945Sroberto			next_event = ISC_LIST_NEXT(event, ev_link);
598280849Scy			ISC_LIST_UNLINK(ctx->on_run, event, ev_link);
599258945Sroberto			task = event->ev_sender;
600258945Sroberto			event->ev_sender = NULL;
601258945Sroberto			isc_task_sendanddetach(&task, &event);
602258945Sroberto		}
603258945Sroberto
604258945Sroberto	}
605258945Sroberto
606280849Scy	UNLOCK(&ctx->lock);
607258945Sroberto
608258945Sroberto#ifndef HAVE_SIGWAIT
609258945Sroberto	/*
610258945Sroberto	 * Catch SIGHUP.
611258945Sroberto	 *
612258945Sroberto	 * We do this here to ensure that the signal handler is installed
613258945Sroberto	 * (i.e. that it wasn't a "one-shot" handler).
614258945Sroberto	 */
615280849Scy	if (ctx == &isc_g_appctx) {
616280849Scy		result = handle_signal(SIGHUP, reload_action);
617280849Scy		if (result != ISC_R_SUCCESS)
618280849Scy			return (ISC_R_SUCCESS);
619280849Scy	}
620258945Sroberto#endif
621258945Sroberto
622280849Scy#ifdef USE_THREADS_SINGLECTX
623258945Sroberto	/*
624280849Scy	 * When we are using multiple contexts, we don't rely on signals.
625280849Scy	 */
626280849Scy	if (ctx != &isc_g_appctx)
627280849Scy		return (ISC_R_SUCCESS);
628280849Scy
629280849Scy	/*
630258945Sroberto	 * There is no danger if isc_app_shutdown() is called before we wait
631258945Sroberto	 * for signals.  Signals are blocked, so any such signal will simply
632258945Sroberto	 * be made pending and we will get it when we call sigwait().
633258945Sroberto	 */
634258945Sroberto
635280849Scy	while (!ctx->want_shutdown) {
636258945Sroberto#ifdef HAVE_SIGWAIT
637258945Sroberto		/*
638258945Sroberto		 * Wait for SIGHUP, SIGINT, or SIGTERM.
639258945Sroberto		 */
640258945Sroberto		if (sigemptyset(&sset) != 0 ||
641258945Sroberto		    sigaddset(&sset, SIGHUP) != 0 ||
642258945Sroberto		    sigaddset(&sset, SIGINT) != 0 ||
643258945Sroberto		    sigaddset(&sset, SIGTERM) != 0) {
644258945Sroberto			isc__strerror(errno, strbuf, sizeof(strbuf));
645258945Sroberto			UNEXPECTED_ERROR(__FILE__, __LINE__,
646258945Sroberto					 "isc_app_run() sigsetops: %s", strbuf);
647258945Sroberto			return (ISC_R_UNEXPECTED);
648258945Sroberto		}
649258945Sroberto
650258945Sroberto#ifndef HAVE_UNIXWARE_SIGWAIT
651258945Sroberto		result = sigwait(&sset, &sig);
652258945Sroberto		if (result == 0) {
653280849Scy			if (sig == SIGINT || sig == SIGTERM)
654280849Scy				ctx->want_shutdown = ISC_TRUE;
655258945Sroberto			else if (sig == SIGHUP)
656280849Scy				ctx->want_reload = ISC_TRUE;
657258945Sroberto		}
658258945Sroberto
659258945Sroberto#else /* Using UnixWare sigwait semantics. */
660258945Sroberto		sig = sigwait(&sset);
661258945Sroberto		if (sig >= 0) {
662280849Scy			if (sig == SIGINT || sig == SIGTERM)
663280849Scy				ctx->want_shutdown = ISC_TRUE;
664258945Sroberto			else if (sig == SIGHUP)
665280849Scy				ctx->want_reload = ISC_TRUE;
666258945Sroberto		}
667258945Sroberto
668258945Sroberto#endif /* HAVE_UNIXWARE_SIGWAIT */
669258945Sroberto#else  /* Don't have sigwait(). */
670258945Sroberto		/*
671258945Sroberto		 * Listen for all signals.
672258945Sroberto		 */
673258945Sroberto		if (sigemptyset(&sset) != 0) {
674258945Sroberto			isc__strerror(errno, strbuf, sizeof(strbuf));
675258945Sroberto			UNEXPECTED_ERROR(__FILE__, __LINE__,
676280849Scy					 "isc_app_run() sigsetops: %s",
677280849Scy					 strbuf);
678258945Sroberto			return (ISC_R_UNEXPECTED);
679258945Sroberto		}
680258945Sroberto		result = sigsuspend(&sset);
681258945Sroberto#endif /* HAVE_SIGWAIT */
682258945Sroberto
683280849Scy		if (ctx->want_reload) {
684280849Scy			ctx->want_reload = ISC_FALSE;
685258945Sroberto			return (ISC_R_RELOAD);
686258945Sroberto		}
687258945Sroberto
688280849Scy		if (ctx->want_shutdown && ctx->blocked)
689258945Sroberto			exit(1);
690258945Sroberto	}
691258945Sroberto
692280849Scy#else /* USE_THREADS_SINGLECTX */
693258945Sroberto
694280849Scy	(void)isc__taskmgr_dispatch(ctx->taskmgr);
695258945Sroberto
696280849Scy	result = evloop(ctx);
697258945Sroberto	if (result != ISC_R_SUCCESS)
698258945Sroberto		return (result);
699258945Sroberto
700280849Scy#endif /* USE_THREADS_SINGLECTX */
701258945Sroberto
702258945Sroberto	return (ISC_R_SUCCESS);
703258945Sroberto}
704258945Sroberto
705280849ScyISC_APPFUNC_SCOPE isc_result_t
706280849Scyisc__app_run() {
707280849Scy	return (isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx));
708280849Scy}
709280849Scy
710280849ScyISC_APPFUNC_SCOPE isc_result_t
711280849Scyisc__app_ctxshutdown(isc_appctx_t *ctx0) {
712280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
713258945Sroberto	isc_boolean_t want_kill = ISC_TRUE;
714258945Sroberto	char strbuf[ISC_STRERRORSIZE];
715258945Sroberto
716280849Scy	REQUIRE(VALID_APPCTX(ctx));
717258945Sroberto
718280849Scy	LOCK(&ctx->lock);
719258945Sroberto
720280849Scy	REQUIRE(ctx->running);
721280849Scy
722280849Scy	if (ctx->shutdown_requested)
723258945Sroberto		want_kill = ISC_FALSE;
724258945Sroberto	else
725280849Scy		ctx->shutdown_requested = ISC_TRUE;
726258945Sroberto
727280849Scy	UNLOCK(&ctx->lock);
728258945Sroberto
729258945Sroberto	if (want_kill) {
730280849Scy		if (ctx != &isc_g_appctx)
731280849Scy			ctx->want_shutdown = ISC_TRUE;
732280849Scy		else {
733258945Sroberto#ifdef HAVE_LINUXTHREADS
734280849Scy			int result;
735258945Sroberto
736280849Scy			result = pthread_kill(main_thread, SIGTERM);
737280849Scy			if (result != 0) {
738280849Scy				isc__strerror(result, strbuf, sizeof(strbuf));
739280849Scy				UNEXPECTED_ERROR(__FILE__, __LINE__,
740280849Scy						 "isc_app_shutdown() "
741280849Scy						 "pthread_kill: %s",
742280849Scy						 strbuf);
743280849Scy				return (ISC_R_UNEXPECTED);
744280849Scy			}
745258945Sroberto#else
746280849Scy			if (kill(getpid(), SIGTERM) < 0) {
747280849Scy				isc__strerror(errno, strbuf, sizeof(strbuf));
748280849Scy				UNEXPECTED_ERROR(__FILE__, __LINE__,
749280849Scy						 "isc_app_shutdown() "
750280849Scy						 "kill: %s", strbuf);
751280849Scy				return (ISC_R_UNEXPECTED);
752280849Scy			}
753280849Scy#endif	/* HAVE_LINUXTHREADS */
754258945Sroberto		}
755258945Sroberto	}
756258945Sroberto
757258945Sroberto	return (ISC_R_SUCCESS);
758258945Sroberto}
759258945Sroberto
760280849ScyISC_APPFUNC_SCOPE isc_result_t
761280849Scyisc__app_shutdown() {
762280849Scy	return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
763280849Scy}
764280849Scy
765280849ScyISC_APPFUNC_SCOPE isc_result_t
766280849Scyisc__app_ctxsuspend(isc_appctx_t *ctx0) {
767280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
768258945Sroberto	isc_boolean_t want_kill = ISC_TRUE;
769258945Sroberto	char strbuf[ISC_STRERRORSIZE];
770258945Sroberto
771280849Scy	REQUIRE(VALID_APPCTX(ctx));
772258945Sroberto
773280849Scy	LOCK(&ctx->lock);
774258945Sroberto
775280849Scy	REQUIRE(ctx->running);
776280849Scy
777258945Sroberto	/*
778258945Sroberto	 * Don't send the reload signal if we're shutting down.
779258945Sroberto	 */
780280849Scy	if (ctx->shutdown_requested)
781258945Sroberto		want_kill = ISC_FALSE;
782258945Sroberto
783280849Scy	UNLOCK(&ctx->lock);
784258945Sroberto
785258945Sroberto	if (want_kill) {
786280849Scy		if (ctx != &isc_g_appctx)
787280849Scy			ctx->want_reload = ISC_TRUE;
788280849Scy		else {
789258945Sroberto#ifdef HAVE_LINUXTHREADS
790280849Scy			int result;
791258945Sroberto
792280849Scy			result = pthread_kill(main_thread, SIGHUP);
793280849Scy			if (result != 0) {
794280849Scy				isc__strerror(result, strbuf, sizeof(strbuf));
795280849Scy				UNEXPECTED_ERROR(__FILE__, __LINE__,
796280849Scy						 "isc_app_reload() "
797280849Scy						 "pthread_kill: %s",
798280849Scy						 strbuf);
799280849Scy				return (ISC_R_UNEXPECTED);
800280849Scy			}
801258945Sroberto#else
802280849Scy			if (kill(getpid(), SIGHUP) < 0) {
803280849Scy				isc__strerror(errno, strbuf, sizeof(strbuf));
804280849Scy				UNEXPECTED_ERROR(__FILE__, __LINE__,
805280849Scy						 "isc_app_reload() "
806280849Scy						 "kill: %s", strbuf);
807280849Scy				return (ISC_R_UNEXPECTED);
808280849Scy			}
809280849Scy#endif
810258945Sroberto		}
811258945Sroberto	}
812258945Sroberto
813258945Sroberto	return (ISC_R_SUCCESS);
814258945Sroberto}
815258945Sroberto
816280849ScyISC_APPFUNC_SCOPE isc_result_t
817280849Scyisc__app_reload(void) {
818280849Scy	return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
819258945Sroberto}
820258945Sroberto
821280849ScyISC_APPFUNC_SCOPE void
822280849Scyisc__app_ctxfinish(isc_appctx_t *ctx0) {
823280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
824280849Scy
825280849Scy	REQUIRE(VALID_APPCTX(ctx));
826280849Scy
827280849Scy	DESTROYLOCK(&ctx->lock);
828280849Scy}
829280849Scy
830280849ScyISC_APPFUNC_SCOPE void
831280849Scyisc__app_finish(void) {
832280849Scy	isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
833280849Scy}
834280849Scy
835280849ScyISC_APPFUNC_SCOPE void
836280849Scyisc__app_block(void) {
837258945Sroberto#ifdef ISC_PLATFORM_USETHREADS
838258945Sroberto	sigset_t sset;
839258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
840280849Scy	REQUIRE(isc_g_appctx.running);
841280849Scy	REQUIRE(!isc_g_appctx.blocked);
842258945Sroberto
843280849Scy	isc_g_appctx.blocked = ISC_TRUE;
844258945Sroberto#ifdef ISC_PLATFORM_USETHREADS
845258945Sroberto	blockedthread = pthread_self();
846258945Sroberto	RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
847258945Sroberto		      sigaddset(&sset, SIGINT) == 0 &&
848258945Sroberto		      sigaddset(&sset, SIGTERM) == 0);
849258945Sroberto	RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
850258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
851258945Sroberto}
852258945Sroberto
853280849ScyISC_APPFUNC_SCOPE void
854280849Scyisc__app_unblock(void) {
855258945Sroberto#ifdef ISC_PLATFORM_USETHREADS
856258945Sroberto	sigset_t sset;
857258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
858258945Sroberto
859280849Scy	REQUIRE(isc_g_appctx.running);
860280849Scy	REQUIRE(isc_g_appctx.blocked);
861258945Sroberto
862280849Scy	isc_g_appctx.blocked = ISC_FALSE;
863258945Sroberto
864258945Sroberto#ifdef ISC_PLATFORM_USETHREADS
865258945Sroberto	REQUIRE(blockedthread == pthread_self());
866258945Sroberto
867258945Sroberto	RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
868258945Sroberto		      sigaddset(&sset, SIGINT) == 0 &&
869258945Sroberto		      sigaddset(&sset, SIGTERM) == 0);
870258945Sroberto	RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
871258945Sroberto#endif /* ISC_PLATFORM_USETHREADS */
872258945Sroberto}
873280849Scy
874280849ScyISC_APPFUNC_SCOPE isc_result_t
875280849Scyisc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
876280849Scy	isc__appctx_t *ctx;
877280849Scy
878280849Scy	REQUIRE(mctx != NULL);
879280849Scy	REQUIRE(ctxp != NULL && *ctxp == NULL);
880280849Scy
881280849Scy	ctx = isc_mem_get(mctx, sizeof(*ctx));
882280849Scy	if (ctx == NULL)
883280849Scy		return (ISC_R_NOMEMORY);
884280849Scy
885280849Scy	ctx->common.impmagic = APPCTX_MAGIC;
886280849Scy	ctx->common.magic = ISCAPI_APPCTX_MAGIC;
887280849Scy	ctx->common.methods = &appmethods.methods;
888280849Scy
889280849Scy	ctx->mctx = NULL;
890280849Scy	isc_mem_attach(mctx, &ctx->mctx);
891280849Scy
892280849Scy	ctx->taskmgr = NULL;
893280849Scy	ctx->socketmgr = NULL;
894280849Scy	ctx->timermgr = NULL;
895280849Scy
896280849Scy	*ctxp = (isc_appctx_t *)ctx;
897280849Scy
898280849Scy	return (ISC_R_SUCCESS);
899280849Scy}
900280849Scy
901280849ScyISC_APPFUNC_SCOPE void
902280849Scyisc__appctx_destroy(isc_appctx_t **ctxp) {
903280849Scy	isc__appctx_t *ctx;
904280849Scy
905280849Scy	REQUIRE(ctxp != NULL);
906280849Scy	ctx = (isc__appctx_t *)*ctxp;
907280849Scy	REQUIRE(VALID_APPCTX(ctx));
908280849Scy
909280849Scy	isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
910280849Scy
911280849Scy	*ctxp = NULL;
912280849Scy}
913280849Scy
914280849ScyISC_APPFUNC_SCOPE void
915280849Scyisc__appctx_settaskmgr(isc_appctx_t *ctx0, isc_taskmgr_t *taskmgr) {
916280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
917280849Scy
918280849Scy	REQUIRE(VALID_APPCTX(ctx));
919280849Scy
920280849Scy	ctx->taskmgr = taskmgr;
921280849Scy}
922280849Scy
923280849ScyISC_APPFUNC_SCOPE void
924280849Scyisc__appctx_setsocketmgr(isc_appctx_t *ctx0, isc_socketmgr_t *socketmgr) {
925280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
926280849Scy
927280849Scy	REQUIRE(VALID_APPCTX(ctx));
928280849Scy
929280849Scy	ctx->socketmgr = socketmgr;
930280849Scy}
931280849Scy
932280849ScyISC_APPFUNC_SCOPE void
933280849Scyisc__appctx_settimermgr(isc_appctx_t *ctx0, isc_timermgr_t *timermgr) {
934280849Scy	isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
935280849Scy
936280849Scy	REQUIRE(VALID_APPCTX(ctx));
937280849Scy
938280849Scy	ctx->timermgr = timermgr;
939280849Scy}
940280849Scy
941280849Scy#ifdef USE_APPIMPREGISTER
942280849Scyisc_result_t
943280849Scyisc__app_register() {
944280849Scy	return (isc_app_register(isc__appctx_create));
945280849Scy}
946280849Scy#endif
947