app.c revision 40f53fa8d9c6a4fc38c0014495e7a42b08f52481
/*
* Copyright (C) 1999, 2000 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.
*/
/* $Id: app.c,v 1.23 2000/08/01 01:31:11 tale Exp $ */
#include <config.h>
#include <pthread.h>
#include <stddef.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
static isc_eventlist_t on_run;
static isc_mutex_t lock;
/*
* We assume that 'want_shutdown' can be read and written atomically.
*/
/*
* We assume that 'want_reload' can be read and written atomically.
*/
#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
"handle_signal() %d setup: %s", sig,
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
isc_app_start(void) {
int presult;
/*
* 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",
return (ISC_R_UNEXPECTED);
}
#endif
#ifdef HAVE_LINUXTHREADS
main_thread = pthread_self();
#endif
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().
*/
if (result != ISC_R_SUCCESS)
return (result);
if (result != ISC_R_SUCCESS)
return (result);
#endif
/*
* Always ignore SIGPIPE.
*/
if (result != ISC_R_SUCCESS)
return (result);
/*
* 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)
return (result);
#ifdef HAVE_SIGWAIT
if (result != ISC_R_SUCCESS)
return (result);
if (result != ISC_R_SUCCESS)
return (result);
#endif
/*
* 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",
return (ISC_R_UNEXPECTED);
}
if (presult != 0) {
"isc_app_start() pthread_sigmask: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
void *arg)
{
/*
* Request delivery of an event when the application is run.
*/
if (running) {
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);
}
isc_app_run(void) {
int result;
#ifdef HAVE_SIGWAIT
int sig;
#endif
/*
* Run an ISC library application.
*/
#ifdef HAVE_LINUXTHREADS
#endif
if (!running) {
/*
* Post any on-run events (in FIFO order).
*/
event = next_event) {
}
}
#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).
*/
if (result != ISC_R_SUCCESS)
return (ISC_R_SUCCESS);
#endif
/*
* 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 ||
"isc_app_run() sigsetops: %s",
return (ISC_R_UNEXPECTED);
}
#ifndef HAVE_UNIXWARE_SIGWAIT
if (result == 0) {
}
#else /* Using UnixWare sigwait semantics. */
if (sig >= 0) {
}
#endif /* HAVE_UNIXWARE_SIGWAIT */
#else /* Don't have sigwait(). */
/*
* Listen for all signals.
*/
if (sigemptyset(&sset) != 0) {
"isc_app_run() sigsetops: %s",
return (ISC_R_UNEXPECTED);
}
#endif /* HAVE_SIGWAIT */
if (want_reload) {
return (ISC_R_RELOAD);
}
}
return (ISC_R_SUCCESS);
}
isc_app_shutdown(void) {
/*
* Request application shutdown.
*/
if (shutdown_requested)
else
if (want_kill) {
#ifdef HAVE_LINUXTHREADS
int result;
if (result != 0) {
"isc_app_shutdown() pthread_kill: %s",
return (ISC_R_UNEXPECTED);
}
#else
"isc_app_shutdown() kill: %s",
return (ISC_R_UNEXPECTED);
}
#endif
}
return (ISC_R_SUCCESS);
}
isc_app_reload(void) {
/*
* Request application reload.
*/
/*
* Don't send the reload signal if we're shutting down.
*/
if (shutdown_requested)
if (want_kill) {
#ifdef HAVE_LINUXTHREADS
int result;
if (result != 0) {
"isc_app_reload() pthread_kill: %s",
return (ISC_R_UNEXPECTED);
}
#else
"isc_app_reload() kill: %s",
return (ISC_R_UNEXPECTED);
}
#endif
}
return (ISC_R_SUCCESS);
}
void
isc_app_finish(void) {
/*
* Finish an ISC library application.
*/
(void)isc_mutex_destroy(&lock);
}