app.c revision ec5347e2c775f027573ce5648b910361aa926c01
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Copyright (C) 1999-2003 Internet Software Consortium.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Permission to use, copy, modify, and/or distribute this software for any
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * purpose with or without fee is hereby granted, provided that the above
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * copyright notice and this permission notice appear in all copies.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * PERFORMANCE OF THIS SOFTWARE.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* $Id: app.c,v 1.53 2007/06/18 23:47:48 tbox Exp $ */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#else /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t shutdown_requested = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We assume that 'want_shutdown' can be read and written atomically.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We assume that 'want_reload' can be read and written atomically.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Linux has sigwait(), but it appears to prevent signal handlers from
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * running, even if they're not in the set being waited for. This makes
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Instead of messing with it, we just use sigsuspend() instead.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We need to remember which thread is the main thread...
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "handle_signal() %d setup: %s"),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Start an ISC library application.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Install do-nothing handlers for SIGINT and SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We install them now because BSDI 3.1 won't block
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * the default actions, regardless of what we do with
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * pthread_sigmask().
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Always ignore SIGPIPE.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * On Solaris 2, delivery of a signal whose action is SIG_IGN
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * will not cause sigwait() to return. We may have inherited
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * process (e.g, Solaris cron). Set an action of SIG_DFL to make
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * sure sigwait() works as expected. Only do this for SIGTERM and
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * SIGINT if we don't have sigwait(), since a different handler is
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * installed above.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Block SIGHUP, SIGINT, SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * If isc_app_start() is called from the main thread before any other
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * threads have been created, then the pthread_sigmask() call below
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * will result in all threads having SIGHUP, SIGINT and SIGTERM
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * blocked by default, ensuring that only the thread that calls
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * sigwait() for them will get those signals.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_start() pthread_sigmask: %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Unblock SIGHUP, SIGINT, SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * If we're not using threads, we need to make sure that SIGHUP,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * SIGINT and SIGTERM are not inherited as blocked from the parent
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Note that we store the task to which we're going to send the event
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * in the event's "sender" field.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Event loop for nonthreaded programs.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__socketmgr_getfdsets(&readfds, &writefds, &maxfd);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt n = select(maxfd, &readfds, &writefds, NULL, tvp);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (n == 0 || call_timer_dispatch) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We call isc__timermgr_dispatch() only when
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * necessary, in order to reduce overhead. If the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * select() call indicates a timeout, we need the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * dispatch. Even if not, if we set the 0-timeout
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * for the select() call, we need to check the timer
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * events. In the 'readytasks' case, there may be no
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * timeout event actually, but there is no other way
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * to reduce the overhead.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Note that we do not have to worry about the case
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * where a new timer is inserted during the select()
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * call, since this loop only runs in the non-thread
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (void)isc__socketmgr_dispatch(&readfds, &writefds,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * This is a gross hack to support waiting for condition
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * variables in nonthreaded programs in a limited way;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We implement isc_condition_wait() by entering the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * event loop recursively until the want_shutdown flag
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * is set by isc_condition_signal().
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * \brief True if we are currently executing in the recursive
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * event loop.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t in_recursive_evloop = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * \brief True if we are exiting the event loop as the result of
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * a call to isc_condition_signal() rather than a shutdown
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * or reload.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(*mp == 1); /* Mutex must be locked on entry. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Post any on-run events (in FIFO order).
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Catch SIGHUP.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We do this here to ensure that the signal handler is installed
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * (i.e. that it wasn't a "one-shot" handler).
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * There is no danger if isc_app_shutdown() is called before we wait
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * for signals. Signals are blocked, so any such signal will simply
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * be made pending and we will get it when we call sigwait().
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Wait for SIGHUP, SIGINT, or SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* Using UnixWare sigwait semantics. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sig >= 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* HAVE_UNIXWARE_SIGWAIT */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* Don't have sigwait(). */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Listen for all signals.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* HAVE_SIGWAIT */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_shutdown() pthread_kill: %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Don't send the reload signal if we're shutting down.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_reload() pthread_kill: %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */