app.c revision ec5347e2c775f027573ce5648b910361aa926c01
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Copyright (C) 1999-2003 Internet Software Consortium.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 *
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* $Id: app.c,v 1.53 2007/06/18 23:47:48 tbox Exp $ */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*! \file */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <config.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <sys/types.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stddef.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdlib.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <errno.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <unistd.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <signal.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <sys/time.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/app.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/boolean.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/condition.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/msgs.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/mutex.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/event.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/platform.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/strerror.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/string.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/task.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/time.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/util.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#ifdef ISC_PLATFORM_USETHREADS
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <pthread.h>
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#else /* ISC_PLATFORM_USETHREADS */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "../timer_p.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "../task_p.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include "socket_p.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_eventlist_t on_run;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_mutex_t lock;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t shutdown_requested = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t running = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*!
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We assume that 'want_shutdown' can be read and written atomically.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t want_shutdown = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We assume that 'want_reload' can be read and written atomically.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t want_reload = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t blocked = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic pthread_t blockedthread;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_LINUXTHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*!
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#undef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*!
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * We need to remember which thread is the main thread...
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic pthread_t main_thread;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifndef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic void
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntexit_action(int arg) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNUSED(arg);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_shutdown = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic void
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntreload_action(int arg) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNUSED(arg);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_reload = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunthandle_signal(int sig, void (*handler)(int)) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct sigaction sa;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char strbuf[ISC_STRERRORSIZE];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt memset(&sa, 0, sizeof(sa));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sa.sa_handler = handler;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sigfillset(&sa.sa_mask) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaction(sig, &sa, NULL) < 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_MSG_SIGNALSETUP,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "handle_signal() %d setup: %s"),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sig, strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_start(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_result_t result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int presult;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigset_t sset;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char strbuf[ISC_STRERRORSIZE];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Start an ISC library application.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef NEED_PTHREAD_INIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt presult = pthread_init();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (presult != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(presult, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_start() pthread_init: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_LINUXTHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt main_thread = pthread_self();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = isc_mutex_init(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifndef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Install do-nothing handlers for SIGINT and SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGINT, exit_action);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGTERM, exit_action);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Always ignore SIGPIPE.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGPIPE, SIG_IGN);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGHUP, SIG_DFL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGTERM, SIG_DFL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGINT, SIG_DFL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Block SIGHUP, SIGINT, SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sigemptyset(&sset) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGHUP) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGINT) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGTERM) != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_start() sigsetops: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (presult != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(presult, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_start() pthread_sigmask: %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Unblock SIGHUP, SIGINT, SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 * process.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sigemptyset(&sset) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGHUP) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGINT) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGTERM) != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_start() sigsetops: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (presult != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(presult, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_start() sigprocmask: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LIST_INIT(on_run);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt void *arg)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_event_t *event;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_task_t *cloned_task = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_result_t result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt LOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (running) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = ISC_R_ALREADYRUNNING;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto unlock;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_task_attach(task, &cloned_task);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt action, arg, sizeof(*event));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (event == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = ISC_R_NOMEMORY;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto unlock;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LIST_APPEND(on_run, event, ev_link);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = ISC_R_SUCCESS;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt unlock:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNLOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifndef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*!
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Event loop for nonthreaded programs.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntevloop() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_result_t result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt while (!want_shutdown) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int n;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_time_t when, now;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct timeval tv, *tvp;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fd_set readfds, writefds;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int maxfd;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_boolean_t readytasks;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_boolean_t call_timer_dispatch = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt readytasks = isc__taskmgr_ready();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (readytasks) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tv.tv_sec = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tv.tv_usec = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tvp = &tv;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt call_timer_dispatch = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = isc__timermgr_nextevent(&when);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tvp = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_uint64_t us;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt TIME_NOW(&now);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt us = isc_time_microdiff(&when, &now);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (us == 0)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt call_timer_dispatch = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tv.tv_sec = us / 1000000;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tv.tv_usec = us % 1000000;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt tvp = &tv;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__socketmgr_getfdsets(&readfds, &writefds, &maxfd);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt n = select(maxfd, &readfds, &writefds, NULL, tvp);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (n == 0 || call_timer_dispatch) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
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 * mode.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__timermgr_dispatch();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (n > 0)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (void)isc__socketmgr_dispatch(&readfds, &writefds,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt maxfd);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (void)isc__taskmgr_dispatch();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (want_reload) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_reload = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_RELOAD);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * This is a gross hack to support waiting for condition
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * variables in nonthreaded programs in a limited way;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * see lib/isc/nothreads/include/isc/condition.h.
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*!
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * \brief True if we are currently executing in the recursive
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * event loop.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t in_recursive_evloop = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*!
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 Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic isc_boolean_t signalled = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_result_t result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNUSED(cp);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNUSED(mp);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(!in_recursive_evloop);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt in_recursive_evloop = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(*mp == 1); /* Mutex must be locked on entry. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt --*mp;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = evloop();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result == ISC_R_RELOAD)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_reload = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (signalled) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_shutdown = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt signalled = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ++*mp;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt in_recursive_evloop = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc__nothread_signal_hack(isc_condition_t *cp) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNUSED(cp);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(in_recursive_evloop);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_shutdown = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt signalled = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_run(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_event_t *event, *next_event;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_task_t *task;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigset_t sset;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char strbuf[ISC_STRERRORSIZE];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int sig;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_LINUXTHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(main_thread == pthread_self());
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt LOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!running) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt running = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Post any on-run events (in FIFO order).
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for (event = ISC_LIST_HEAD(on_run);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt event != NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt event = next_event) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt next_event = ISC_LIST_NEXT(event, ev_link);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LIST_UNLINK(on_run, event, ev_link);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt task = event->ev_sender;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt event->ev_sender = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_task_sendanddetach(&task, &event);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNLOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifndef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Catch SIGHUP.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = handle_signal(SIGHUP, reload_action);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
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 */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt while (!want_shutdown) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Wait for SIGHUP, SIGINT, or SIGTERM.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sigemptyset(&sset) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGHUP) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGINT) != 0 ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGTERM) != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_run() sigsetops: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifndef HAVE_UNIXWARE_SIGWAIT
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = sigwait(&sset, &sig);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result == 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sig == SIGINT ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sig == SIGTERM)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_shutdown = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else if (sig == SIGHUP)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_reload = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* Using UnixWare sigwait semantics. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sig = sigwait(&sset);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sig >= 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sig == SIGINT ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sig == SIGTERM)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_shutdown = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else if (sig == SIGHUP)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_reload = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* HAVE_UNIXWARE_SIGWAIT */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* Don't have sigwait(). */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Listen for all signals.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (sigemptyset(&sset) != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_run() sigsetops: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = sigsuspend(&sset);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* HAVE_SIGWAIT */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (want_reload) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_reload = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_RELOAD);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (want_shutdown && blocked)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt exit(1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (void)isc__taskmgr_dispatch();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = evloop();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_shutdown(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_boolean_t want_kill = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char strbuf[ISC_STRERRORSIZE];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt LOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(running);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (shutdown_requested)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_kill = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt shutdown_requested = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNLOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (want_kill) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_LINUXTHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = pthread_kill(main_thread, SIGTERM);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(result, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_shutdown() pthread_kill: %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strbuf);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (kill(getpid(), SIGTERM) < 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_shutdown() kill: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_reload(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_boolean_t want_kill = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char strbuf[ISC_STRERRORSIZE];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt LOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(running);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Don't send the reload signal if we're shutting down.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (shutdown_requested)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt want_kill = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNLOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (want_kill) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef HAVE_LINUXTHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = pthread_kill(main_thread, SIGHUP);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(result, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_reload() pthread_kill: %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#else
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (kill(getpid(), SIGHUP) < 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc__strerror(errno, strbuf, sizeof(strbuf));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "isc_app_reload() kill: %s", strbuf);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_UNEXPECTED);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_finish(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt DESTROYLOCK(&lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_block(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigset_t sset;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(running);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(!blocked);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt blocked = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt blockedthread = pthread_self();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGINT) == 0 &&
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGTERM) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_app_unblock(void) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigset_t sset;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(running);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(blocked);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt blocked = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#ifdef ISC_PLATFORM_USETHREADS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(blockedthread == pthread_self());
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGINT) == 0 &&
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sigaddset(&sset, SIGTERM) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#endif /* ISC_PLATFORM_USETHREADS */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt