2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: app.c,v 1.50.18.2 2005/04/29 00:17:06 marka Exp $ */
24 #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
25 #include <sys/types.h>
35 #include <isc/boolean.h>
36 #include <isc/condition.h>
38 #include <isc/mutex.h>
39 #include <isc/event.h>
40 #include <isc/platform.h>
41 #include <isc/strerror.h>
42 #include <isc/string.h>
47 #ifdef ISC_PLATFORM_USETHREADS
49 #else /* ISC_PLATFORM_USETHREADS */
50 #include "../timer_p.h"
51 #include "../task_p.h"
53 #endif /* ISC_PLATFORM_USETHREADS */
55 static isc_eventlist_t on_run;
56 static isc_mutex_t lock;
57 static isc_boolean_t shutdown_requested = ISC_FALSE;
58 static isc_boolean_t running = ISC_FALSE;
60 * We assume that 'want_shutdown' can be read and written atomically.
62 static isc_boolean_t want_shutdown = ISC_FALSE;
64 * We assume that 'want_reload' can be read and written atomically.
66 static isc_boolean_t want_reload = ISC_FALSE;
68 static isc_boolean_t blocked = ISC_FALSE;
69 #ifdef ISC_PLATFORM_USETHREADS
70 static pthread_t blockedthread;
71 #endif /* ISC_PLATFORM_USETHREADS */
73 #ifdef HAVE_LINUXTHREADS
75 * Linux has sigwait(), but it appears to prevent signal handlers from
76 * running, even if they're not in the set being waited for. This makes
77 * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
78 * Instead of messing with it, we just use sigsuspend() instead.
82 * We need to remember which thread is the main thread...
84 static pthread_t main_thread;
89 exit_action(int arg) {
91 want_shutdown = ISC_TRUE;
95 reload_action(int arg) {
97 want_reload = ISC_TRUE;
102 handle_signal(int sig, void (*handler)(int)) {
104 char strbuf[ISC_STRERRORSIZE];
106 memset(&sa, 0, sizeof(sa));
107 sa.sa_handler = handler;
109 if (sigfillset(&sa.sa_mask) != 0 ||
110 sigaction(sig, &sa, NULL) < 0) {
111 isc__strerror(errno, strbuf, sizeof(strbuf));
112 UNEXPECTED_ERROR(__FILE__, __LINE__,
113 isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
115 "handle_signal() %d setup: %s"),
117 return (ISC_R_UNEXPECTED);
120 return (ISC_R_SUCCESS);
124 isc_app_start(void) {
128 char strbuf[ISC_STRERRORSIZE];
131 * Start an ISC library application.
134 #ifdef NEED_PTHREAD_INIT
136 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
138 presult = pthread_init();
140 isc__strerror(presult, strbuf, sizeof(strbuf));
141 UNEXPECTED_ERROR(__FILE__, __LINE__,
142 "isc_app_start() pthread_init: %s", strbuf);
143 return (ISC_R_UNEXPECTED);
147 #ifdef HAVE_LINUXTHREADS
148 main_thread = pthread_self();
151 result = isc_mutex_init(&lock);
152 if (result != ISC_R_SUCCESS)
157 * Install do-nothing handlers for SIGINT and SIGTERM.
159 * We install them now because BSDI 3.1 won't block
160 * the default actions, regardless of what we do with
163 result = handle_signal(SIGINT, exit_action);
164 if (result != ISC_R_SUCCESS)
166 result = handle_signal(SIGTERM, exit_action);
167 if (result != ISC_R_SUCCESS)
172 * Always ignore SIGPIPE.
174 result = handle_signal(SIGPIPE, SIG_IGN);
175 if (result != ISC_R_SUCCESS)
179 * On Solaris 2, delivery of a signal whose action is SIG_IGN
180 * will not cause sigwait() to return. We may have inherited
181 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
182 * process (e.g, Solaris cron). Set an action of SIG_DFL to make
183 * sure sigwait() works as expected. Only do this for SIGTERM and
184 * SIGINT if we don't have sigwait(), since a different handler is
187 result = handle_signal(SIGHUP, SIG_DFL);
188 if (result != ISC_R_SUCCESS)
192 result = handle_signal(SIGTERM, SIG_DFL);
193 if (result != ISC_R_SUCCESS)
195 result = handle_signal(SIGINT, SIG_DFL);
196 if (result != ISC_R_SUCCESS)
200 #ifdef ISC_PLATFORM_USETHREADS
202 * Block SIGHUP, SIGINT, SIGTERM.
204 * If isc_app_start() is called from the main thread before any other
205 * threads have been created, then the pthread_sigmask() call below
206 * will result in all threads having SIGHUP, SIGINT and SIGTERM
207 * blocked by default, ensuring that only the thread that calls
208 * sigwait() for them will get those signals.
210 if (sigemptyset(&sset) != 0 ||
211 sigaddset(&sset, SIGHUP) != 0 ||
212 sigaddset(&sset, SIGINT) != 0 ||
213 sigaddset(&sset, SIGTERM) != 0) {
214 isc__strerror(errno, strbuf, sizeof(strbuf));
215 UNEXPECTED_ERROR(__FILE__, __LINE__,
216 "isc_app_start() sigsetops: %s", strbuf);
217 return (ISC_R_UNEXPECTED);
219 presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
221 isc__strerror(presult, strbuf, sizeof(strbuf));
222 UNEXPECTED_ERROR(__FILE__, __LINE__,
223 "isc_app_start() pthread_sigmask: %s",
225 return (ISC_R_UNEXPECTED);
227 #else /* ISC_PLATFORM_USETHREADS */
229 * Unblock SIGHUP, SIGINT, SIGTERM.
231 * If we're not using threads, we need to make sure that SIGHUP,
232 * SIGINT and SIGTERM are not inherited as blocked from the parent
235 if (sigemptyset(&sset) != 0 ||
236 sigaddset(&sset, SIGHUP) != 0 ||
237 sigaddset(&sset, SIGINT) != 0 ||
238 sigaddset(&sset, SIGTERM) != 0) {
239 isc__strerror(errno, strbuf, sizeof(strbuf));
240 UNEXPECTED_ERROR(__FILE__, __LINE__,
241 "isc_app_start() sigsetops: %s", strbuf);
242 return (ISC_R_UNEXPECTED);
244 presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
246 isc__strerror(presult, strbuf, sizeof(strbuf));
247 UNEXPECTED_ERROR(__FILE__, __LINE__,
248 "isc_app_start() sigprocmask: %s", strbuf);
249 return (ISC_R_UNEXPECTED);
251 #endif /* ISC_PLATFORM_USETHREADS */
253 ISC_LIST_INIT(on_run);
255 return (ISC_R_SUCCESS);
259 isc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
263 isc_task_t *cloned_task = NULL;
269 result = ISC_R_ALREADYRUNNING;
274 * Note that we store the task to which we're going to send the event
275 * in the event's "sender" field.
277 isc_task_attach(task, &cloned_task);
278 event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
279 action, arg, sizeof(*event));
281 result = ISC_R_NOMEMORY;
285 ISC_LIST_APPEND(on_run, event, ev_link);
287 result = ISC_R_SUCCESS;
295 #ifndef ISC_PLATFORM_USETHREADS
297 * Event loop for nonthreaded programs.
302 while (!want_shutdown) {
304 isc_time_t when, now;
305 struct timeval tv, *tvp;
306 fd_set readfds, writefds;
308 isc_boolean_t readytasks;
309 isc_boolean_t call_timer_dispatch = ISC_FALSE;
311 readytasks = isc__taskmgr_ready();
316 call_timer_dispatch = ISC_TRUE;
318 result = isc__timermgr_nextevent(&when);
319 if (result != ISC_R_SUCCESS)
325 us = isc_time_microdiff(&when, &now);
327 call_timer_dispatch = ISC_TRUE;
328 tv.tv_sec = us / 1000000;
329 tv.tv_usec = us % 1000000;
334 isc__socketmgr_getfdsets(&readfds, &writefds, &maxfd);
335 n = select(maxfd, &readfds, &writefds, NULL, tvp);
337 if (n == 0 || call_timer_dispatch) {
339 * We call isc__timermgr_dispatch() only when
340 * necessary, in order to reduce overhead. If the
341 * select() call indicates a timeout, we need the
342 * dispatch. Even if not, if we set the 0-timeout
343 * for the select() call, we need to check the timer
344 * events. In the 'readytasks' case, there may be no
345 * timeout event actually, but there is no other way
346 * to reduce the overhead.
347 * Note that we do not have to worry about the case
348 * where a new timer is inserted during the select()
349 * call, since this loop only runs in the non-thread
352 isc__timermgr_dispatch();
355 (void)isc__socketmgr_dispatch(&readfds, &writefds,
357 (void)isc__taskmgr_dispatch();
360 want_reload = ISC_FALSE;
361 return (ISC_R_RELOAD);
364 return (ISC_R_SUCCESS);
368 * This is a gross hack to support waiting for condition
369 * variables in nonthreaded programs in a limited way;
370 * see lib/isc/nothreads/include/isc/condition.h.
371 * We implement isc_condition_wait() by entering the
372 * event loop recursively until the want_shutdown flag
373 * is set by isc_condition_signal().
377 * \brief True if we are currently executing in the recursive
380 static isc_boolean_t in_recursive_evloop = ISC_FALSE;
383 * \brief True if we are exiting the event loop as the result of
384 * a call to isc_condition_signal() rather than a shutdown
387 static isc_boolean_t signalled = ISC_FALSE;
390 isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
396 INSIST(!in_recursive_evloop);
397 in_recursive_evloop = ISC_TRUE;
399 INSIST(*mp == 1); /* Mutex must be locked on entry. */
403 if (result == ISC_R_RELOAD)
404 want_reload = ISC_TRUE;
406 want_shutdown = ISC_FALSE;
407 signalled = ISC_FALSE;
411 in_recursive_evloop = ISC_FALSE;
412 return (ISC_R_SUCCESS);
416 isc__nothread_signal_hack(isc_condition_t *cp) {
420 INSIST(in_recursive_evloop);
422 want_shutdown = ISC_TRUE;
423 signalled = ISC_TRUE;
424 return (ISC_R_SUCCESS);
427 #endif /* ISC_PLATFORM_USETHREADS */
432 isc_event_t *event, *next_event;
434 #ifdef ISC_PLATFORM_USETHREADS
436 char strbuf[ISC_STRERRORSIZE];
437 #endif /* ISC_PLATFORM_USETHREADS */
442 #ifdef HAVE_LINUXTHREADS
443 REQUIRE(main_thread == pthread_self());
452 * Post any on-run events (in FIFO order).
454 for (event = ISC_LIST_HEAD(on_run);
456 event = next_event) {
457 next_event = ISC_LIST_NEXT(event, ev_link);
458 ISC_LIST_UNLINK(on_run, event, ev_link);
459 task = event->ev_sender;
460 event->ev_sender = NULL;
461 isc_task_sendanddetach(&task, &event);
472 * We do this here to ensure that the signal handler is installed
473 * (i.e. that it wasn't a "one-shot" handler).
475 result = handle_signal(SIGHUP, reload_action);
476 if (result != ISC_R_SUCCESS)
477 return (ISC_R_SUCCESS);
480 #ifdef ISC_PLATFORM_USETHREADS
482 * There is no danger if isc_app_shutdown() is called before we wait
483 * for signals. Signals are blocked, so any such signal will simply
484 * be made pending and we will get it when we call sigwait().
487 while (!want_shutdown) {
490 * Wait for SIGHUP, SIGINT, or SIGTERM.
492 if (sigemptyset(&sset) != 0 ||
493 sigaddset(&sset, SIGHUP) != 0 ||
494 sigaddset(&sset, SIGINT) != 0 ||
495 sigaddset(&sset, SIGTERM) != 0) {
496 isc__strerror(errno, strbuf, sizeof(strbuf));
497 UNEXPECTED_ERROR(__FILE__, __LINE__,
498 "isc_app_run() sigsetops: %s", strbuf);
499 return (ISC_R_UNEXPECTED);
502 #ifndef HAVE_UNIXWARE_SIGWAIT
503 result = sigwait(&sset, &sig);
507 want_shutdown = ISC_TRUE;
508 else if (sig == SIGHUP)
509 want_reload = ISC_TRUE;
512 #else /* Using UnixWare sigwait semantics. */
513 sig = sigwait(&sset);
517 want_shutdown = ISC_TRUE;
518 else if (sig == SIGHUP)
519 want_reload = ISC_TRUE;
522 #endif /* HAVE_UNIXWARE_SIGWAIT */
523 #else /* Don't have sigwait(). */
525 * Listen for all signals.
527 if (sigemptyset(&sset) != 0) {
528 isc__strerror(errno, strbuf, sizeof(strbuf));
529 UNEXPECTED_ERROR(__FILE__, __LINE__,
530 "isc_app_run() sigsetops: %s", strbuf);
531 return (ISC_R_UNEXPECTED);
533 result = sigsuspend(&sset);
534 #endif /* HAVE_SIGWAIT */
537 want_reload = ISC_FALSE;
538 return (ISC_R_RELOAD);
541 if (want_shutdown && blocked)
545 #else /* ISC_PLATFORM_USETHREADS */
547 (void)isc__taskmgr_dispatch();
550 if (result != ISC_R_SUCCESS)
553 #endif /* ISC_PLATFORM_USETHREADS */
555 return (ISC_R_SUCCESS);
559 isc_app_shutdown(void) {
560 isc_boolean_t want_kill = ISC_TRUE;
561 char strbuf[ISC_STRERRORSIZE];
567 if (shutdown_requested)
568 want_kill = ISC_FALSE;
570 shutdown_requested = ISC_TRUE;
575 #ifdef HAVE_LINUXTHREADS
578 result = pthread_kill(main_thread, SIGTERM);
580 isc__strerror(result, strbuf, sizeof(strbuf));
581 UNEXPECTED_ERROR(__FILE__, __LINE__,
582 "isc_app_shutdown() pthread_kill: %s",
584 return (ISC_R_UNEXPECTED);
587 if (kill(getpid(), SIGTERM) < 0) {
588 isc__strerror(errno, strbuf, sizeof(strbuf));
589 UNEXPECTED_ERROR(__FILE__, __LINE__,
590 "isc_app_shutdown() kill: %s", strbuf);
591 return (ISC_R_UNEXPECTED);
596 return (ISC_R_SUCCESS);
600 isc_app_reload(void) {
601 isc_boolean_t want_kill = ISC_TRUE;
602 char strbuf[ISC_STRERRORSIZE];
609 * Don't send the reload signal if we're shutting down.
611 if (shutdown_requested)
612 want_kill = ISC_FALSE;
617 #ifdef HAVE_LINUXTHREADS
620 result = pthread_kill(main_thread, SIGHUP);
622 isc__strerror(result, strbuf, sizeof(strbuf));
623 UNEXPECTED_ERROR(__FILE__, __LINE__,
624 "isc_app_reload() pthread_kill: %s",
626 return (ISC_R_UNEXPECTED);
629 if (kill(getpid(), SIGHUP) < 0) {
630 isc__strerror(errno, strbuf, sizeof(strbuf));
631 UNEXPECTED_ERROR(__FILE__, __LINE__,
632 "isc_app_reload() kill: %s", strbuf);
633 return (ISC_R_UNEXPECTED);
638 return (ISC_R_SUCCESS);
642 isc_app_finish(void) {
647 isc_app_block(void) {
648 #ifdef ISC_PLATFORM_USETHREADS
650 #endif /* ISC_PLATFORM_USETHREADS */
655 #ifdef ISC_PLATFORM_USETHREADS
656 blockedthread = pthread_self();
657 RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
658 sigaddset(&sset, SIGINT) == 0 &&
659 sigaddset(&sset, SIGTERM) == 0);
660 RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
661 #endif /* ISC_PLATFORM_USETHREADS */
665 isc_app_unblock(void) {
666 #ifdef ISC_PLATFORM_USETHREADS
668 #endif /* ISC_PLATFORM_USETHREADS */
675 #ifdef ISC_PLATFORM_USETHREADS
676 REQUIRE(blockedthread == pthread_self());
678 RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
679 sigaddset(&sset, SIGINT) == 0 &&
680 sigaddset(&sset, SIGTERM) == 0);
681 RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
682 #endif /* ISC_PLATFORM_USETHREADS */