2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-25 19:47:42 +00:00
bind/lib/isc/unix/app.c

489 lines
11 KiB
C
Raw Normal View History

1999-05-12 09:44:35 +00:00
/*
* Copyright (C) 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/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1999-05-12 09:44:35 +00:00
*/
/*! \file */
2000-06-22 22:00:42 +00:00
#include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
1999-05-12 09:44:35 +00:00
#include <sys/types.h>
#include <stdbool.h>
1999-05-12 09:44:35 +00:00
#include <stddef.h>
#include <inttypes.h>
#include <stdlib.h>
1999-05-12 09:44:35 +00:00
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#ifdef HAVE_EPOLL
#include <sys/epoll.h>
#endif
1999-05-12 09:44:35 +00:00
#include <isc/platform.h>
#include <isc/atomic.h>
1999-05-12 09:44:35 +00:00
#include <isc/app.h>
#include <isc/condition.h>
#include <isc/mem.h>
#include <isc/mutex.h>
1999-07-14 02:03:44 +00:00
#include <isc/event.h>
#include <isc/platform.h>
#include <isc/strerr.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/time.h>
1999-12-16 22:24:22 +00:00
#include <isc/util.h>
1999-05-12 09:44:35 +00:00
#include <pthread.h>
/*%
* 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.
*/
static pthread_t blockedthread;
static atomic_bool is_running;
/*
* The application context of this module. This implementation actually
* doesn't use it. (This may change in the future).
*/
#define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
#define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
struct isc_appctx {
unsigned int magic;
isc_mem_t *mctx;
isc_mutex_t lock;
isc_eventlist_t on_run;
atomic_bool shutdown_requested;
atomic_bool running;
atomic_bool want_shutdown;
atomic_bool want_reload;
atomic_bool blocked;
isc_mutex_t readylock;
isc_condition_t ready;
};
static isc_appctx_t isc_g_appctx;
2000-01-22 01:39:35 +00:00
static void
1999-05-12 22:54:46 +00:00
handle_signal(int sig, void (*handler)(int)) {
struct sigaction sa;
2001-11-27 01:56:32 +00:00
memset(&sa, 0, sizeof(sa));
1999-05-12 22:54:46 +00:00
sa.sa_handler = handler;
if (sigfillset(&sa.sa_mask) != 0 ||
sigaction(sig, &sa, NULL) < 0) {
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno, strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__,
2018-11-23 21:35:01 +01:00
"handle_signal() %d setup: %s",
sig, strbuf);
1999-05-12 22:54:46 +00:00
}
}
isc_result_t
isc_app_ctxstart(isc_appctx_t *ctx) {
int presult;
sigset_t sset;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(VALID_APPCTX(ctx));
1999-05-12 09:44:35 +00:00
/*
* Start an ISC library application.
*/
isc_mutex_init(&ctx->lock);
isc_mutex_init(&ctx->readylock);
2018-11-15 17:20:36 +01:00
isc_condition_init(&ctx->ready);
ISC_LIST_INIT(ctx->on_run);
atomic_init(&ctx->shutdown_requested, false);
atomic_init(&ctx->running, false);
atomic_init(&ctx->want_shutdown, false);
atomic_init(&ctx->want_reload, false);
atomic_init(&ctx->blocked, false);
1999-05-12 09:44:35 +00:00
1999-05-12 22:54:46 +00:00
/*
* Always ignore SIGPIPE.
*/
handle_signal(SIGPIPE, SIG_IGN);
1999-05-12 22:54:46 +00:00
handle_signal(SIGHUP, SIG_DFL);
handle_signal(SIGTERM, SIG_DFL);
handle_signal(SIGINT, SIG_DFL);
1999-05-12 09:44:35 +00:00
/*
2000-01-22 01:39:35 +00:00
* Block SIGHUP, SIGINT, SIGTERM.
1999-05-12 09:44:35 +00:00
*
* If isc_app_start() is called from the main thread before any other
* threads have been created, then the pthread_sigmask() call below
2000-03-18 01:29:48 +00:00
* 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.
1999-05-12 09:44:35 +00:00
*/
if (sigemptyset(&sset) != 0 ||
2000-01-22 01:39:35 +00:00
sigaddset(&sset, SIGHUP) != 0 ||
1999-05-12 09:44:35 +00:00
sigaddset(&sset, SIGINT) != 0 ||
sigaddset(&sset, SIGTERM) != 0) {
strerror_r(errno, strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__,
"isc_app_start() sigsetops: %s", strbuf);
1999-05-12 09:44:35 +00:00
}
presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
if (presult != 0) {
strerror_r(presult, strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__,
"isc_app_start() pthread_sigmask: %s",
strbuf);
1999-05-12 09:44:35 +00:00
}
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_start(void) {
isc_g_appctx.magic = APPCTX_MAGIC;
isc_g_appctx.mctx = NULL;
/* The remaining members will be initialized in ctxstart() */
return (isc_app_ctxstart(&isc_g_appctx));
1999-05-12 09:44:35 +00:00
}
isc_result_t
isc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
1999-07-14 02:03:44 +00:00
void *arg)
{
return (isc_app_ctxonrun(&isc_g_appctx, mctx, task, action, arg));
}
isc_result_t
isc_app_ctxonrun(isc_appctx_t *ctx, isc_mem_t *mctx, isc_task_t *task,
isc_taskaction_t action, void *arg)
{
1999-07-14 02:03:44 +00:00
isc_event_t *event;
isc_task_t *cloned_task = NULL;
if (atomic_load_acquire(&ctx->running)) {
return (ISC_R_ALREADYRUNNING);
1999-07-14 02:03:44 +00:00
}
/*
* Note that we store the task to which we're going to send the event
* in the event's "sender" field.
*/
isc_task_attach(task, &cloned_task);
event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
2001-11-27 01:56:32 +00:00
action, arg, sizeof(*event));
1999-07-14 02:03:44 +00:00
if (event == NULL) {
isc_task_detach(&cloned_task);
return (ISC_R_NOMEMORY);
1999-07-14 02:03:44 +00:00
}
LOCK(&ctx->lock);
ISC_LIST_APPEND(ctx->on_run, event, ev_link);
UNLOCK(&ctx->lock);
1999-07-14 02:03:44 +00:00
return (ISC_R_SUCCESS);
1999-07-14 02:03:44 +00:00
}
isc_result_t
isc_app_ctxrun(isc_appctx_t *ctx) {
1999-05-12 09:44:35 +00:00
isc_event_t *event, *next_event;
1999-07-14 02:03:44 +00:00
isc_task_t *task;
sigset_t sset;
1999-05-12 09:44:35 +00:00
int sig;
bool exp_false = false;
bool exp_true = true;
REQUIRE(VALID_APPCTX(ctx));
1999-05-12 09:44:35 +00:00
if (atomic_compare_exchange_weak_acq_rel(
&ctx->running, &exp_false, true) == true)
{
2000-01-22 01:39:35 +00:00
/*
* Post any on-run events (in FIFO order).
*/
LOCK(&ctx->lock);
for (event = ISC_LIST_HEAD(ctx->on_run);
2000-01-22 01:39:35 +00:00
event != NULL;
event = next_event) {
next_event = ISC_LIST_NEXT(event, ev_link);
ISC_LIST_UNLINK(ctx->on_run, event, ev_link);
task = event->ev_sender;
event->ev_sender = NULL;
2000-01-22 01:39:35 +00:00
isc_task_sendanddetach(&task, &event);
}
UNLOCK(&ctx->lock);
1999-05-12 09:44:35 +00:00
}
/*
* BIND9 internal tools using multiple contexts do not
* rely on signal. */
if (isc_bind9 && ctx != &isc_g_appctx) {
return (ISC_R_SUCCESS);
}
1999-07-14 02:03:44 +00:00
/*
* 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().
1999-07-14 02:03:44 +00:00
*/
while (atomic_load_acquire(&ctx->want_shutdown) == false) {
if (isc_bind9) {
/*
* BIND9 internal; single context:
* Wait for SIGHUP, SIGINT, or SIGTERM.
*/
if (sigemptyset(&sset) != 0 ||
sigaddset(&sset, SIGHUP) != 0 ||
sigaddset(&sset, SIGINT) != 0 ||
sigaddset(&sset, SIGTERM) != 0) {
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno, strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__,
"isc_app_run() sigsetops: %s",
strbuf);
}
2000-05-18 22:39:24 +00:00
if (sigwait(&sset, &sig) == 0) {
switch (sig) {
case SIGINT:
case SIGTERM:
atomic_store_release(
&ctx->want_shutdown, true);
break;
case SIGHUP:
atomic_store_release(
&ctx->want_reload, true);
break;
default:
INSIST(0);
ISC_UNREACHABLE();
}
}
} else {
/*
* External, or BIND9 using multiple contexts:
* wait until woken up.
*/
if (atomic_load_acquire(&ctx->want_shutdown)) {
break;
}
if (!atomic_load_acquire(&ctx->want_reload)) {
LOCK(&ctx->readylock);
WAIT(&ctx->ready, &ctx->readylock);
UNLOCK(&ctx->readylock);
}
}
exp_true = true;
if (atomic_compare_exchange_weak_acq_rel(&ctx->want_reload,
&exp_true,
false))
{
return (ISC_R_RELOAD);
}
if (atomic_load_acquire(&ctx->want_shutdown) &&
atomic_load_acquire(&ctx->blocked)) {
2001-03-14 06:31:17 +00:00
exit(1);
}
2000-01-22 01:39:35 +00:00
}
1999-05-12 09:44:35 +00:00
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_run(void) {
isc_result_t result;
bool exp_false = false;
REQUIRE(atomic_compare_exchange_weak_acq_rel(
&is_running, &exp_false, true) == true);
result = isc_app_ctxrun(&isc_g_appctx);
atomic_store_release(&is_running, false);
return (result);
}
bool
isc_app_isrunning() {
return (atomic_load_acquire(&is_running));
}
void
isc_app_ctxshutdown(isc_appctx_t *ctx) {
bool exp_false = false;
1999-05-12 09:44:35 +00:00
REQUIRE(VALID_APPCTX(ctx));
REQUIRE(atomic_load_acquire(&ctx->running));
1999-05-12 09:44:35 +00:00
/* If ctx->shutdown_requested == true, we are already shutting
* down and we want to just bail out.
*/
if (atomic_compare_exchange_weak_acq_rel(
&ctx->shutdown_requested,
&exp_false,
true))
{
if (isc_bind9 && ctx != &isc_g_appctx) {
/* BIND9 internal, but using multiple contexts */
atomic_store_release(&ctx->want_shutdown, true);
} else if (isc_bind9) {
/* BIND9 internal, single context */
if (kill(getpid(), SIGTERM) < 0) {
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno,
strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__,
"isc_app_shutdown() "
"kill: %s", strbuf);
}
} else {
/* External, multiple contexts */
atomic_store_release(&ctx->want_shutdown, true);
SIGNAL(&ctx->ready);
1999-05-12 22:35:40 +00:00
}
1999-05-12 09:44:35 +00:00
}
}
void
isc_app_shutdown(void) {
isc_app_ctxshutdown(&isc_g_appctx);
}
void
isc_app_ctxsuspend(isc_appctx_t *ctx) {
REQUIRE(VALID_APPCTX(ctx));
REQUIRE(atomic_load(&ctx->running));
2000-01-22 01:39:35 +00:00
/*
* Don't send the reload signal if we're shutting down.
*/
if (atomic_load_acquire(&ctx->shutdown_requested) == false) {
if (isc_bind9 && ctx != &isc_g_appctx) {
/* BIND9 internal, but using multiple contexts */
atomic_store_release(&ctx->want_reload, true);
} else if (isc_bind9) {
/* BIND9 internal, single context */
if (kill(getpid(), SIGHUP) < 0) {
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno,
strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__,
"isc_app_reload() "
"kill: %s", strbuf);
}
} else {
/* External, multiple contexts */
atomic_store_release(&ctx->want_reload, true);
SIGNAL(&ctx->ready);
}
2000-01-22 01:39:35 +00:00
}
2000-01-22 01:39:35 +00:00
}
void
isc_app_reload(void) {
return (isc_app_ctxsuspend(&isc_g_appctx));
1999-05-12 09:44:35 +00:00
}
void
isc_app_ctxfinish(isc_appctx_t *ctx) {
REQUIRE(VALID_APPCTX(ctx));
isc_mutex_destroy(&ctx->lock);
isc_mutex_destroy(&ctx->readylock);
isc_condition_destroy(&ctx->ready);
}
void
isc_app_finish(void) {
isc_app_ctxfinish(&isc_g_appctx);
}
void
isc_app_block(void) {
sigset_t sset;
REQUIRE(isc_g_appctx.running);
REQUIRE(!isc_g_appctx.blocked);
atomic_store_release(&isc_g_appctx.blocked, true);
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);
}
void
isc_app_unblock(void) {
sigset_t sset;
bool exp_true = true;
REQUIRE(atomic_load_acquire(&isc_g_appctx.running));
REQUIRE(atomic_compare_exchange_weak_acq_rel(
&isc_g_appctx.blocked,
&exp_true,
false));
REQUIRE(blockedthread == pthread_self());
RUNTIME_CHECK(sigemptyset(&sset) == 0 &&
2008-01-18 23:46:58 +00:00
sigaddset(&sset, SIGINT) == 0 &&
sigaddset(&sset, SIGTERM) == 0);
RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK, &sset, NULL) == 0);
}
isc_result_t
isc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
isc_appctx_t *ctx;
REQUIRE(mctx != NULL);
REQUIRE(ctxp != NULL && *ctxp == NULL);
ctx = isc_mem_get(mctx, sizeof(*ctx));
if (ctx == NULL)
return (ISC_R_NOMEMORY);
ctx->magic = APPCTX_MAGIC;
ctx->mctx = NULL;
isc_mem_attach(mctx, &ctx->mctx);
*ctxp = ctx;
return (ISC_R_SUCCESS);
}
void
isc_appctx_destroy(isc_appctx_t **ctxp) {
isc_appctx_t *ctx;
REQUIRE(ctxp != NULL);
ctx = *ctxp;
REQUIRE(VALID_APPCTX(ctx));
*ctxp = NULL;
ctx->magic = 0;
isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
}