app.c revision 660a209ee4febf8cd6df7c317b363391e65809f4
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Copyright (C) 1999-2003 Internet Software Consortium.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * purpose with or without fee is hereby granted, provided that the above
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * copyright notice and this permission notice appear in all copies.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/* $Id: app.c,v 1.56 2008/01/04 23:46:55 tbox Exp $ */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/*! \file */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <config.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <sys/types.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <stddef.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <stdlib.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <errno.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <unistd.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <signal.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <sys/time.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/app.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/boolean.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/condition.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/msgs.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/mutex.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/event.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/platform.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/strerror.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/string.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/task.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/time.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/util.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef ISC_PLATFORM_USETHREADS
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <pthread.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#else /* ISC_PLATFORM_USETHREADS */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include "../timer_p.h"
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include "../task_p.h"
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include "socket_p.h"
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif /* ISC_PLATFORM_USETHREADS */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic isc_eventlist_t on_run;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic isc_mutex_t lock;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic isc_boolean_t shutdown_requested = ISC_FALSE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic isc_boolean_t running = ISC_FALSE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/*!
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * We assume that 'want_shutdown' can be read and written atomically.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic volatile isc_boolean_t want_shutdown = ISC_FALSE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * We assume that 'want_reload' can be read and written atomically.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic volatile isc_boolean_t want_reload = ISC_FALSE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic isc_boolean_t blocked = ISC_FALSE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef ISC_PLATFORM_USETHREADS
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic pthread_t blockedthread;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif /* ISC_PLATFORM_USETHREADS */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef HAVE_LINUXTHREADS
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/*!
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Linux has sigwait(), but it appears to prevent signal handlers from
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * running, even if they're not in the set being waited for. This makes
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Instead of messing with it, we just use sigsuspend() instead.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#undef HAVE_SIGWAIT
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews/*!
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * We need to remember which thread is the main thread...
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic pthread_t main_thread;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifndef HAVE_SIGWAIT
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic void
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsexit_action(int arg) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNUSED(arg);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews want_shutdown = ISC_TRUE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews}
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic void
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsreload_action(int arg) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNUSED(arg);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews want_reload = ISC_TRUE;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews}
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsstatic isc_result_t
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewshandle_signal(int sig, void (*handler)(int)) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews struct sigaction sa;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews char strbuf[ISC_STRERRORSIZE];
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews memset(&sa, 0, sizeof(sa));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sa.sa_handler = handler;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (sigfillset(&sa.sa_mask) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaction(sig, &sa, NULL) < 0) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews ISC_MSG_SIGNALSETUP,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews "handle_signal() %d setup: %s"),
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sig, strbuf);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_UNEXPECTED);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_SUCCESS);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews}
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsisc_result_t
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsisc_app_start(void) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc_result_t result;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews int presult;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigset_t sset;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews char strbuf[ISC_STRERRORSIZE];
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Start an ISC library application.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef NEED_PTHREAD_INIT
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews presult = pthread_init();
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (presult != 0) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc__strerror(presult, strbuf, sizeof(strbuf));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews "isc_app_start() pthread_init: %s", strbuf);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_UNEXPECTED);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef HAVE_LINUXTHREADS
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews main_thread = pthread_self();
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = isc_mutex_init(&lock);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifndef HAVE_SIGWAIT
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Install do-nothing handlers for SIGINT and SIGTERM.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * We install them now because BSDI 3.1 won't block
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * the default actions, regardless of what we do with
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * pthread_sigmask().
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = handle_signal(SIGINT, exit_action);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = handle_signal(SIGTERM, exit_action);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Always ignore SIGPIPE.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = handle_signal(SIGPIPE, SIG_IGN);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * On Solaris 2, delivery of a signal whose action is SIG_IGN
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * will not cause sigwait() to return. We may have inherited
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * process (e.g, Solaris cron). Set an action of SIG_DFL to make
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * sure sigwait() works as expected. Only do this for SIGTERM and
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * SIGINT if we don't have sigwait(), since a different handler is
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * installed above.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = handle_signal(SIGHUP, SIG_DFL);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef HAVE_SIGWAIT
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = handle_signal(SIGTERM, SIG_DFL);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = handle_signal(SIGINT, SIG_DFL);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (result != ISC_R_SUCCESS)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (result);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#ifdef ISC_PLATFORM_USETHREADS
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Block SIGHUP, SIGINT, SIGTERM.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * If isc_app_start() is called from the main thread before any other
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * threads have been created, then the pthread_sigmask() call below
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * will result in all threads having SIGHUP, SIGINT and SIGTERM
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * blocked by default, ensuring that only the thread that calls
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * sigwait() for them will get those signals.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (sigemptyset(&sset) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaddset(&sset, SIGHUP) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaddset(&sset, SIGINT) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaddset(&sset, SIGTERM) != 0) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews "isc_app_start() sigsetops: %s", strbuf);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_UNEXPECTED);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (presult != 0) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc__strerror(presult, strbuf, sizeof(strbuf));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews "isc_app_start() pthread_sigmask: %s",
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews strbuf);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_UNEXPECTED);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#else /* ISC_PLATFORM_USETHREADS */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Unblock SIGHUP, SIGINT, SIGTERM.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * If we're not using threads, we need to make sure that SIGHUP,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * SIGINT and SIGTERM are not inherited as blocked from the parent
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * process.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (sigemptyset(&sset) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaddset(&sset, SIGHUP) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaddset(&sset, SIGINT) != 0 ||
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews sigaddset(&sset, SIGTERM) != 0) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews "isc_app_start() sigsetops: %s", strbuf);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_UNEXPECTED);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (presult != 0) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc__strerror(presult, strbuf, sizeof(strbuf));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews "isc_app_start() sigprocmask: %s", strbuf);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_UNEXPECTED);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#endif /* ISC_PLATFORM_USETHREADS */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews ISC_LIST_INIT(on_run);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews return (ISC_R_SUCCESS);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews}
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsisc_result_t
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrewsisc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews void *arg)
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews{
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc_event_t *event;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc_task_t *cloned_task = NULL;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc_result_t result;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews LOCK(&lock);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (running) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = ISC_R_ALREADYRUNNING;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews goto unlock;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews /*
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Note that we store the task to which we're going to send the event
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * in the event's "sender" field.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews */
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews isc_task_attach(task, &cloned_task);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews action, arg, sizeof(*event));
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews if (event == NULL) {
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = ISC_R_NOMEMORY;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews goto unlock;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews }
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews ISC_LIST_APPEND(on_run, event, ev_link);
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews result = ISC_R_SUCCESS;
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews unlock:
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews UNLOCK(&lock);
return (result);
}
#ifndef ISC_PLATFORM_USETHREADS
/*!
* Event loop for nonthreaded programs.
*/
static isc_result_t
evloop() {
isc_result_t result;
while (!want_shutdown) {
int n;
isc_time_t when, now;
struct timeval tv, *tvp;
fd_set readfds, writefds;
int maxfd;
isc_boolean_t readytasks;
isc_boolean_t call_timer_dispatch = ISC_FALSE;
readytasks = isc__taskmgr_ready();
if (readytasks) {
tv.tv_sec = 0;
tv.tv_usec = 0;
tvp = &tv;
call_timer_dispatch = ISC_TRUE;
} else {
result = isc__timermgr_nextevent(&when);
if (result != ISC_R_SUCCESS)
tvp = NULL;
else {
isc_uint64_t us;
TIME_NOW(&now);
us = isc_time_microdiff(&when, &now);
if (us == 0)
call_timer_dispatch = ISC_TRUE;
tv.tv_sec = us / 1000000;
tv.tv_usec = us % 1000000;
tvp = &tv;
}
}
isc__socketmgr_getfdsets(&readfds, &writefds, &maxfd);
n = select(maxfd, &readfds, &writefds, NULL, tvp);
if (n == 0 || call_timer_dispatch) {
/*
* We call isc__timermgr_dispatch() only when
* necessary, in order to reduce overhead. If the
* select() call indicates a timeout, we need the
* dispatch. Even if not, if we set the 0-timeout
* for the select() call, we need to check the timer
* events. In the 'readytasks' case, there may be no
* timeout event actually, but there is no other way
* to reduce the overhead.
* Note that we do not have to worry about the case
* where a new timer is inserted during the select()
* call, since this loop only runs in the non-thread
* mode.
*/
isc__timermgr_dispatch();
}
if (n > 0)
(void)isc__socketmgr_dispatch(&readfds, &writefds,
maxfd);
(void)isc__taskmgr_dispatch();
if (want_reload) {
want_reload = ISC_FALSE;
return (ISC_R_RELOAD);
}
}
return (ISC_R_SUCCESS);
}
/*
* This is a gross hack to support waiting for condition
* variables in nonthreaded programs in a limited way;
* see lib/isc/nothreads/include/isc/condition.h.
* We implement isc_condition_wait() by entering the
* event loop recursively until the want_shutdown flag
* is set by isc_condition_signal().
*/
/*!
* \brief True if we are currently executing in the recursive
* event loop.
*/
static isc_boolean_t in_recursive_evloop = ISC_FALSE;
/*!
* \brief True if we are exiting the event loop as the result of
* a call to isc_condition_signal() rather than a shutdown
* or reload.
*/
static isc_boolean_t signalled = ISC_FALSE;
isc_result_t
isc__nothread_wait_hack(isc_condition_t *cp, isc_mutex_t *mp) {
isc_result_t result;
UNUSED(cp);
UNUSED(mp);
INSIST(!in_recursive_evloop);
in_recursive_evloop = ISC_TRUE;
INSIST(*mp == 1); /* Mutex must be locked on entry. */
--*mp;
result = evloop();
if (result == ISC_R_RELOAD)
want_reload = ISC_TRUE;
if (signalled) {
want_shutdown = ISC_FALSE;
signalled = ISC_FALSE;
}
++*mp;
in_recursive_evloop = ISC_FALSE;
return (ISC_R_SUCCESS);
}
isc_result_t
isc__nothread_signal_hack(isc_condition_t *cp) {
UNUSED(cp);
INSIST(in_recursive_evloop);
want_shutdown = ISC_TRUE;
signalled = ISC_TRUE;
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */
isc_result_t
isc_app_run(void) {
int result;
isc_event_t *event, *next_event;
isc_task_t *task;
#ifdef ISC_PLATFORM_USETHREADS
sigset_t sset;
char strbuf[ISC_STRERRORSIZE];
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef HAVE_SIGWAIT
int sig;
#endif
#ifdef HAVE_LINUXTHREADS
REQUIRE(main_thread == pthread_self());
#endif
LOCK(&lock);
if (!running) {
running = ISC_TRUE;
/*
* Post any on-run events (in FIFO order).
*/
for (event = ISC_LIST_HEAD(on_run);
event != NULL;
event = next_event) {
next_event = ISC_LIST_NEXT(event, ev_link);
ISC_LIST_UNLINK(on_run, event, ev_link);
task = event->ev_sender;
event->ev_sender = NULL;
isc_task_sendanddetach(&task, &event);
}
}
UNLOCK(&lock);
#ifndef HAVE_SIGWAIT
/*
* Catch SIGHUP.
*
* We do this here to ensure that the signal handler is installed
* (i.e. that it wasn't a "one-shot" handler).
*/
result = handle_signal(SIGHUP, reload_action);
if (result != ISC_R_SUCCESS)
return (ISC_R_SUCCESS);
#endif
#ifdef ISC_PLATFORM_USETHREADS
/*
* There is no danger if isc_app_shutdown() is called before we wait
* for signals. Signals are blocked, so any such signal will simply
* be made pending and we will get it when we call sigwait().
*/
while (!want_shutdown) {
#ifdef HAVE_SIGWAIT
/*
* Wait for SIGHUP, SIGINT, or SIGTERM.
*/
if (sigemptyset(&sset) != 0 ||
sigaddset(&sset, SIGHUP) != 0 ||
sigaddset(&sset, SIGINT) != 0 ||
sigaddset(&sset, SIGTERM) != 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_run() sigsetops: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
#ifndef HAVE_UNIXWARE_SIGWAIT
result = sigwait(&sset, &sig);
if (result == 0) {
if (sig == SIGINT ||
sig == SIGTERM)
want_shutdown = ISC_TRUE;
else if (sig == SIGHUP)
want_reload = ISC_TRUE;
}
#else /* Using UnixWare sigwait semantics. */
sig = sigwait(&sset);
if (sig >= 0) {
if (sig == SIGINT ||
sig == SIGTERM)
want_shutdown = ISC_TRUE;
else if (sig == SIGHUP)
want_reload = ISC_TRUE;
}
#endif /* HAVE_UNIXWARE_SIGWAIT */
#else /* Don't have sigwait(). */
/*
* Listen for all signals.
*/
if (sigemptyset(&sset) != 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_run() sigsetops: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
result = sigsuspend(&sset);
#endif /* HAVE_SIGWAIT */
if (want_reload) {
want_reload = ISC_FALSE;
return (ISC_R_RELOAD);
}
if (want_shutdown && blocked)
exit(1);
}
#else /* ISC_PLATFORM_USETHREADS */
(void)isc__taskmgr_dispatch();
result = evloop();
if (result != ISC_R_SUCCESS)
return (result);
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_shutdown(void) {
isc_boolean_t want_kill = ISC_TRUE;
char strbuf[ISC_STRERRORSIZE];
LOCK(&lock);
REQUIRE(running);
if (shutdown_requested)
want_kill = ISC_FALSE;
else
shutdown_requested = ISC_TRUE;
UNLOCK(&lock);
if (want_kill) {
#ifdef HAVE_LINUXTHREADS
int result;
result = pthread_kill(main_thread, SIGTERM);
if (result != 0) {
isc__strerror(result, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_shutdown() pthread_kill: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
#else
if (kill(getpid(), SIGTERM) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_shutdown() kill: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
#endif
}
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_reload(void) {
isc_boolean_t want_kill = ISC_TRUE;
char strbuf[ISC_STRERRORSIZE];
LOCK(&lock);
REQUIRE(running);
/*
* Don't send the reload signal if we're shutting down.
*/
if (shutdown_requested)
want_kill = ISC_FALSE;
UNLOCK(&lock);
if (want_kill) {
#ifdef HAVE_LINUXTHREADS
int result;
result = pthread_kill(main_thread, SIGHUP);
if (result != 0) {
isc__strerror(result, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_reload() pthread_kill: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
#else
if (kill(getpid(), SIGHUP) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_reload() kill: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
#endif
}
return (ISC_R_SUCCESS);
}
void
isc_app_finish(void) {
DESTROYLOCK(&lock);
}
void
isc_app_block(void) {
#ifdef ISC_PLATFORM_USETHREADS
sigset_t sset;
#endif /* ISC_PLATFORM_USETHREADS */
REQUIRE(running);
REQUIRE(!blocked);
blocked = ISC_TRUE;
#ifdef ISC_PLATFORM_USETHREADS
blockedthread = pthread_self();
RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
sigaddset(&sset, SIGINT) == 0 &&
sigaddset(&sset, SIGTERM) == 0);
RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK, &sset, NULL) == 0);
#endif /* ISC_PLATFORM_USETHREADS */
}
void
isc_app_unblock(void) {
#ifdef ISC_PLATFORM_USETHREADS
sigset_t sset;
#endif /* ISC_PLATFORM_USETHREADS */
REQUIRE(running);
REQUIRE(blocked);
blocked = ISC_FALSE;
#ifdef ISC_PLATFORM_USETHREADS
REQUIRE(blockedthread == pthread_self());
RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
sigaddset(&sset, SIGINT) == 0 &&
sigaddset(&sset, SIGTERM) == 0);
RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
#endif /* ISC_PLATFORM_USETHREADS */
}