mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-03 08:05:21 +00:00
add app
This commit is contained in:
@@ -28,7 +28,7 @@ CWARNINGS =
|
||||
OBJS = @ISC_EXTRA_OBJS@ \
|
||||
assertions.o buffer.o error.o heap.o lex.o mem.o result.o \
|
||||
rwlock.o symtab.o str.o event.o task.o timer.o version.o \
|
||||
unix/time.o unix/stdtime.o unix/socket.o \
|
||||
unix/app.o unix/time.o unix/stdtime.o unix/socket.o \
|
||||
pthreads/condition.o
|
||||
|
||||
SUBDIRS = include unix pthreads
|
||||
|
@@ -24,7 +24,7 @@ CINCLUDES = -I${srcdir}/.. \
|
||||
CDEFINES =
|
||||
CWARNINGS =
|
||||
|
||||
OBJS = time.o stdtime.o socket.o
|
||||
OBJS = app.o time.o stdtime.o socket.o
|
||||
|
||||
SUBDIRS = include
|
||||
TARGETS = ${OBJS}
|
||||
|
231
lib/isc/unix/app.c
Normal file
231
lib/isc/unix/app.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (C) 1999 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <isc/app.h>
|
||||
#include <isc/error.h>
|
||||
#include <isc/task.h>
|
||||
#include <isc/event.h>
|
||||
#include <isc/boolean.h>
|
||||
#include <isc/mutex.h>
|
||||
|
||||
#include "../util.h" /* XXX */
|
||||
|
||||
static isc_eventlist_t on_run;
|
||||
static isc_mutex_t lock;
|
||||
static isc_boolean_t shutdown_requested = ISC_FALSE;
|
||||
|
||||
#ifndef HAVE_SIGWAIT
|
||||
static void
|
||||
empty_action(int arg) {
|
||||
(void)arg;
|
||||
}
|
||||
#endif
|
||||
|
||||
isc_result_t
|
||||
isc_app_start(void) {
|
||||
isc_result_t result;
|
||||
int presult;
|
||||
sigset_t sset;
|
||||
#ifndef HAVE_SIGWAIT
|
||||
struct sigaction sa;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_start() pthread_init: %s",
|
||||
strerror(presult));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
#endif
|
||||
|
||||
result = isc_mutex_init(&lock);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
return (result);
|
||||
|
||||
#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().
|
||||
*/
|
||||
memset(&sa, 0, sizeof sa);
|
||||
sa.sa_handler = empty_action;
|
||||
if (sigfillset(&sa.sa_mask) != 0 ||
|
||||
sigaction(SIGINT, &sa, NULL) < 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_run() SIGINT setup: %s",
|
||||
strerror(errno));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
if (sigfillset(&sa.sa_mask) != 0 ||
|
||||
sigaction(SIGTERM, &sa, NULL) < 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_run() SIGTERM setup: %s",
|
||||
strerror(errno));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Block SIGINT and 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 SIGINT and SIGTERM blocked by
|
||||
* default.
|
||||
*/
|
||||
if (sigemptyset(&sset) != 0 ||
|
||||
sigaddset(&sset, SIGINT) != 0 ||
|
||||
sigaddset(&sset, SIGTERM) != 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_start() sigsetops: %s",
|
||||
strerror(errno));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
|
||||
if (presult != 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_start() pthread_sigmask: %s",
|
||||
strerror(presult));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
|
||||
ISC_LIST_INIT(on_run);
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_app_run(void) {
|
||||
int result;
|
||||
sigset_t sset;
|
||||
#if 0
|
||||
isc_event_t *event, *next_event;
|
||||
#endif
|
||||
#ifdef HAVE_SIGWAIT
|
||||
int sig;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Run an ISC library application.
|
||||
*/
|
||||
|
||||
#if 0
|
||||
/*
|
||||
* Post any on-run events (in LIFO order).
|
||||
*/
|
||||
for (event = ISC_LIST_HEAD(on_run);
|
||||
event != NULL;
|
||||
event = next_event) {
|
||||
next_event = ISC_LIST_NEXT(event, link);
|
||||
ISC_LIST_DEQUEUE(task->on_run, event, link);
|
||||
isc_task_send(task, &event);
|
||||
isc_task_detach(&task);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SIGWAIT
|
||||
/*
|
||||
* Wait for SIGINT or SIGTERM.
|
||||
*/
|
||||
if (sigemptyset(&sset) != 0 ||
|
||||
sigaddset(&sset, SIGINT) != 0 ||
|
||||
sigaddset(&sset, SIGTERM) != 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_run() sigsetops: %s",
|
||||
strerror(errno));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
result = sigwait(&sset, &sig);
|
||||
#else
|
||||
/*
|
||||
* Block all signals except for SIGINT and SIGTERM, and then
|
||||
* wait for one of them to occur.
|
||||
*/
|
||||
if (sigfillset(&sset) != 0 ||
|
||||
sigdelset(&sset, SIGINT) != 0 ||
|
||||
sigdelset(&sset, SIGTERM) != 0 ||
|
||||
sigdelset(&sset, SIGABRT) != 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_run() sigsetops: %s",
|
||||
strerror(errno));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
result = sigsuspend(&sset);
|
||||
#endif
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_app_shutdown(void) {
|
||||
isc_boolean_t want_kill = ISC_TRUE;
|
||||
|
||||
/*
|
||||
* Request application shutdown.
|
||||
*/
|
||||
|
||||
LOCK(&lock);
|
||||
|
||||
if (shutdown_requested)
|
||||
want_kill = ISC_FALSE;
|
||||
else
|
||||
shutdown_requested = ISC_TRUE;
|
||||
|
||||
UNLOCK(&lock);
|
||||
|
||||
if (want_kill && kill(getpid(), SIGTERM) < 0) {
|
||||
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
||||
"isc_app_shutdown() kill: %s",
|
||||
strerror(errno));
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
void
|
||||
isc_app_finish(void) {
|
||||
/*
|
||||
* Finish an ISC library application.
|
||||
*/
|
||||
|
||||
(void)isc_mutex_destroy(&lock);
|
||||
}
|
@@ -19,7 +19,7 @@ top_srcdir = @top_srcdir@
|
||||
|
||||
@BIND9_VERSION@
|
||||
|
||||
HEADERS = sockaddr.h time.h stdtime.h
|
||||
HEADERS = app.h sockaddr.h time.h stdtime.h
|
||||
|
||||
SUBDIRS =
|
||||
TARGETS =
|
||||
|
127
lib/isc/unix/include/isc/app.h
Normal file
127
lib/isc/unix/include/isc/app.h
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (C) 1999 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ISC_APP_H
|
||||
#define ISC_APP_H 1
|
||||
|
||||
/*****
|
||||
***** Module Info
|
||||
*****/
|
||||
|
||||
/*
|
||||
* ISC Application Support
|
||||
*
|
||||
* Dealing with program termination can be difficult, especially in a
|
||||
* multithreaded program. The routines in this module help coordinate
|
||||
* the shutdown process. They are used as follows by the initial (main)
|
||||
* thread of the application:
|
||||
*
|
||||
* isc_app_start(); Call very early in main(), before
|
||||
* any other threads have been created.
|
||||
*
|
||||
* isc_app_run(); This will post any on-run events,
|
||||
* and then block until application
|
||||
* shutdown is requested. A shutdown
|
||||
* request is made by calling
|
||||
* isc_app_shutdown(), or by sending
|
||||
* SIGINT or SIGTERM to the process.
|
||||
* After isc_app_run() returns, the
|
||||
* application should shutdown itself.
|
||||
*
|
||||
* isc_app_finish(); Call very late in main().
|
||||
*
|
||||
* Use of this module is not required. In particular, isc_app_start() is
|
||||
* NOT an ISC library initialization routine.
|
||||
*
|
||||
* MP:
|
||||
* Clients must ensure that isc_app_start(), isc_app_run(), and
|
||||
* isc_app_finish() are called at most once. isc_app_shutdown()
|
||||
* is safe to use by any thread (provided isc_app_start() has been
|
||||
* called previously).
|
||||
*
|
||||
* Reliability:
|
||||
* No anticipated impact.
|
||||
*
|
||||
* Resources:
|
||||
* None.
|
||||
*
|
||||
* Security:
|
||||
* No anticipated impact.
|
||||
*
|
||||
* Standards:
|
||||
* None.
|
||||
*/
|
||||
|
||||
#include <isc/lang.h>
|
||||
#include <isc/result.h>
|
||||
#include <isc/event.h>
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
typedef isc_event_t isc_appevent_t;
|
||||
|
||||
isc_result_t
|
||||
isc_app_start(void);
|
||||
/*
|
||||
* Start an ISC library application.
|
||||
*
|
||||
* Notes:
|
||||
* This call should be made before any other ISC library call, and as
|
||||
* close to the beginning of the application as possible.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
isc_app_run(void);
|
||||
/*
|
||||
* Run an ISC library application.
|
||||
*
|
||||
* Notes:
|
||||
* The caller (typically the initial thread of an application) will
|
||||
* block until shutdown is requested. When the call returns, the
|
||||
* caller should start shutting down the application.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
isc_app_shutdown(void);
|
||||
/*
|
||||
* Request application shutdown.
|
||||
*
|
||||
* Notes:
|
||||
* It is safe to call isc_app_shutdown() multiple times.
|
||||
*
|
||||
* Requires:
|
||||
* isc_app_start() has been called.
|
||||
*/
|
||||
|
||||
void
|
||||
isc_app_finish(void);
|
||||
/*
|
||||
* Finish an ISC library application.
|
||||
*
|
||||
* Notes:
|
||||
* This call should be made at or near the end of main().
|
||||
*
|
||||
* Requires:
|
||||
* isc_app_start() has been called.
|
||||
*
|
||||
* Ensures:
|
||||
* Any resources allocated by isc_app_start() have been released.
|
||||
*/
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif /* ISC_APP_H */
|
Reference in New Issue
Block a user