app.c revision 032d2134a4c1808696688db9bf6f20253e5d05b5
/*
* Copyright (C) 1999-2005, 2007-2009, 2013-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*! \file */
#include <config.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#ifdef HAVE_EPOLL
#endif
#include <isc/condition.h>
#include <isc/platform.h>
#include <isc/strerror.h>
#ifdef ISC_PLATFORM_USETHREADS
#include <pthread.h>
#endif
/*%
* For BIND9 internal applications built with threads, we use a single app
* context and let multiple worker, I/O, timer threads do actual jobs.
* For other cases (including BIND9 built without threads) an app context acts
* as an event loop dispatching various events.
*/
#ifndef ISC_PLATFORM_USETHREADS
#include "../timer_p.h"
#include "../task_p.h"
#include "socket_p.h"
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
static pthread_t blockedthread;
#endif /* ISC_PLATFORM_USETHREADS */
/*%
* The following are intended for internal use (indicated by "isc__"
* prefix) but are not declared as static, allowing direct access from
* unit tests etc.
*/
isc_result_t isc__app_start(void);
isc_result_t isc__app_run(void);
isc_result_t isc__app_shutdown(void);
isc_result_t isc__app_reload(void);
void isc__app_finish(void);
void isc__app_block(void);
void isc__app_unblock(void);
void *arg);
/*
* The application context of this module. This implementation actually
* doesn't use it. (This may change in the future).
*/
typedef struct isc__appctx {
/*!
* We assume that 'want_shutdown' can be read and written atomically.
*/
/*
* We assume that 'want_reload' can be read and written atomically.
*/
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
static isc__appctx_t isc_g_appctx;
static struct {
/*%
* The following are defined just for avoiding unused static functions.
*/
} appmethods = {
{
},
(void *)isc__app_run,
(void *)isc__app_shutdown,
(void *)isc__app_start,
(void *)isc__app_reload,
(void *)isc__app_finish,
(void *)isc__app_block,
(void *)isc__app_unblock
};
#ifdef HAVE_LINUXTHREADS
/*!
* Linux has sigwait(), but it appears to prevent signal handlers from
* running, even if they're not in the set being waited for. This makes
* it impossible to get the default actions for SIGILL, SIGSEGV, etc.
* Instead of messing with it, we just use sigsuspend() instead.
*/
/*!
* We need to remember which thread is the main thread...
*/
static pthread_t main_thread;
#endif
#ifndef HAVE_SIGWAIT
static void
exit_action(int arg) {
}
static void
reload_action(int arg) {
}
#endif
static isc_result_t
char strbuf[ISC_STRERRORSIZE];
"handle_signal() %d setup: %s"),
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
int presult;
char strbuf[ISC_STRERRORSIZE];
/*
* Start an ISC library application.
*/
#ifdef NEED_PTHREAD_INIT
/*
* BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
*/
presult = pthread_init();
if (presult != 0) {
"isc_app_start() pthread_init: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
#endif
#ifdef ISC_PLATFORM_USETHREADS
#ifdef HAVE_LINUXTHREADS
main_thread = pthread_self();
#endif /* HAVE_LINUXTHREADS */
if (result != ISC_R_SUCCESS)
return (result);
if (result != ISC_R_SUCCESS)
goto cleanup_rlock;
if (result != ISC_R_SUCCESS)
goto cleanup_rcond;
#else /* ISC_PLATFORM_USETHREADS */
if (result != ISC_R_SUCCESS)
goto cleanup;
#endif /* ISC_PLATFORM_USETHREADS */
#ifndef HAVE_SIGWAIT
/*
* Install do-nothing handlers for SIGINT and SIGTERM.
*
* We install them now because BSDI 3.1 won't block
* the default actions, regardless of what we do with
* pthread_sigmask().
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
if (result != ISC_R_SUCCESS)
goto cleanup;
#endif
/*
* Always ignore SIGPIPE.
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
/*
* On Solaris 2, delivery of a signal whose action is SIG_IGN
* will not cause sigwait() to return. We may have inherited
* unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
* process (e.g, Solaris cron). Set an action of SIG_DFL to make
* sure sigwait() works as expected. Only do this for SIGTERM and
* SIGINT if we don't have sigwait(), since a different handler is
* installed above.
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
#ifdef HAVE_SIGWAIT
if (result != ISC_R_SUCCESS)
goto cleanup;
if (result != ISC_R_SUCCESS)
goto cleanup;
#endif
#ifdef ISC_PLATFORM_USETHREADS
/*
* Block SIGHUP, SIGINT, SIGTERM.
*
* If isc_app_start() is called from the main thread before any other
* threads have been created, then the pthread_sigmask() call below
* will result in all threads having SIGHUP, SIGINT and SIGTERM
* blocked by default, ensuring that only the thread that calls
* sigwait() for them will get those signals.
*/
if (sigemptyset(&sset) != 0 ||
"isc_app_start() sigsetops: %s", strbuf);
goto cleanup;
}
if (presult != 0) {
"isc_app_start() pthread_sigmask: %s",
strbuf);
goto cleanup;
}
#else /* ISC_PLATFORM_USETHREADS */
/*
* Unblock SIGHUP, SIGINT, SIGTERM.
*
* If we're not using threads, we need to make sure that SIGHUP,
* SIGINT and SIGTERM are not inherited as blocked from the parent
* process.
*/
if (sigemptyset(&sset) != 0 ||
"isc_app_start() sigsetops: %s", strbuf);
goto cleanup;
}
if (presult != 0) {
"isc_app_start() sigprocmask: %s", strbuf);
goto cleanup;
}
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_SUCCESS);
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
return (result);
}
isc__app_start(void) {
/* The remaining members will be initialized in ctxstart() */
}
void *arg)
{
}
{
goto unlock;
}
/*
* Note that we store the task to which we're going to send the event
* in the event's "sender" field.
*/
goto unlock;
}
return (result);
}
#ifndef ISC_PLATFORM_USETHREADS
/*!
* Event loop for nonthreaded programs.
*/
static isc_result_t
while (!ctx->want_shutdown) {
int n;
/*
* Check the reload (or suspend) case first for exiting the
* loop as fast as possible in case:
* - the direct call to isc__taskmgr_dispatch() in
* isc__app_ctxrun() completes all the tasks so far,
* - there is thus currently no active task, and
* - there is a timer event
*/
if (ctx->want_reload) {
return (ISC_R_RELOAD);
}
if (readytasks) {
} else {
if (result != ISC_R_SUCCESS)
else {
if (us == 0)
}
}
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.
*/
}
if (n > 0)
}
return (ISC_R_SUCCESS);
}
/*
* This is a gross hack to support waiting for condition
* variables in nonthreaded programs in a limited way;
* 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.
*/
/*!
* \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.
*/
--*mp;
if (result == ISC_R_RELOAD)
if (signalled) {
}
++*mp;
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */
int result;
#ifdef ISC_PLATFORM_USETHREADS
char strbuf[ISC_STRERRORSIZE];
#ifdef HAVE_SIGWAIT
int sig;
#endif /* HAVE_SIGWAIT */
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef HAVE_LINUXTHREADS
#endif
/*
* Post any on-run events (in FIFO order).
*/
event = next_event) {
}
}
#ifndef ISC_PLATFORM_USETHREADS
if (result != ISC_R_SUCCESS)
return (ISC_R_SUCCESS);
}
return (result);
#else /* ISC_PLATFORM_USETHREADS */
/*
* BIND9 internal tools using multiple contexts do not
* rely on signal.
*/
return (ISC_R_SUCCESS);
/*
* 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 (!ctx->want_shutdown) {
#ifdef HAVE_SIGWAIT
if (isc_bind9) {
/*
* BIND9 internal; single context:
* Wait for SIGHUP, SIGINT, or SIGTERM.
*/
if (sigemptyset(&sset) != 0 ||
"isc_app_run() sigsetops: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
#ifndef HAVE_UNIXWARE_SIGWAIT
if (result == 0) {
}
#else /* Using UnixWare sigwait semantics. */
if (sig >= 0) {
}
#endif /* HAVE_UNIXWARE_SIGWAIT */
} else {
/*
* External, or BIND9 using multiple contexts:
* wait until woken up.
*/
if (ctx->want_shutdown) {
/* shutdown() won the race. */
break;
}
if (!ctx->want_reload)
}
#else /* Don't have sigwait(). */
if (isc_bind9) {
/*
* BIND9 internal; single context:
* Install a signal handler for SIGHUP, then wait for
* all signals.
*/
if (result != ISC_R_SUCCESS)
return (ISC_R_SUCCESS);
if (sigemptyset(&sset) != 0) {
"isc_app_run() sigsetops: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
#ifdef HAVE_GPERFTOOLS_PROFILER
"isc_app_run() sigsetops: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
#endif
(void)sigsuspend(&sset);
} else {
/*
* External, or BIND9 using multiple contexts:
* wait until woken up.
*/
if (ctx->want_shutdown) {
/* shutdown() won the race. */
break;
}
if (!ctx->want_reload)
}
#endif /* HAVE_SIGWAIT */
if (ctx->want_reload) {
return (ISC_R_RELOAD);
}
exit(1);
}
return (ISC_R_SUCCESS);
#endif /* ISC_PLATFORM_USETHREADS */
}
isc__app_run(void) {
}
#ifdef ISC_PLATFORM_USETHREADS
char strbuf[ISC_STRERRORSIZE];
#endif /* ISC_PLATFORM_USETHREADS */
if (ctx->shutdown_requested)
else
if (want_kill) {
/* BIND9 internal, but using multiple contexts */
else {
#ifndef ISC_PLATFORM_USETHREADS
#else /* ISC_PLATFORM_USETHREADS */
#ifdef HAVE_LINUXTHREADS
if (isc_bind9) {
/* BIND9 internal, single context */
int result;
if (result != 0) {
"isc_app_shutdown() "
"pthread_kill: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
}
#else
if (isc_bind9) {
/* BIND9 internal, single context */
"isc_app_shutdown() "
"kill: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
}
#endif /* HAVE_LINUXTHREADS */
else {
/* External, multiple contexts */
}
#endif /* ISC_PLATFORM_USETHREADS */
}
}
return (ISC_R_SUCCESS);
}
isc__app_shutdown(void) {
}
#ifdef ISC_PLATFORM_USETHREADS
char strbuf[ISC_STRERRORSIZE];
#endif
/*
* Don't send the reload signal if we're shutting down.
*/
if (ctx->shutdown_requested)
if (want_kill) {
/* BIND9 internal, but using multiple contexts */
else {
#ifndef ISC_PLATFORM_USETHREADS
#else /* ISC_PLATFORM_USETHREADS */
#ifdef HAVE_LINUXTHREADS
if (isc_bind9) {
/* BIND9 internal, single context */
int result;
if (result != 0) {
"isc_app_reload() "
"pthread_kill: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
}
#else
if (isc_bind9) {
/* BIND9 internal, single context */
"isc_app_reload() "
"kill: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
}
#endif /* HAVE_LINUXTHREADS */
else {
/* External, multiple contexts */
}
#endif /* ISC_PLATFORM_USETHREADS */
}
}
return (ISC_R_SUCCESS);
}
isc__app_reload(void) {
}
void
}
void
isc__app_finish(void) {
}
void
isc__app_block(void) {
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
}
void
isc__app_unblock(void) {
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
}
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
void
}
void
}
void
}
void
}
isc__app_register(void) {
return (isc_app_register(isc__appctx_create));
}
#include "../app_api.c"