#include <config.h>
#include <sys/param.h>
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#ifdef HAVE_EPOLL
#include <sys/epoll.h>
#endif
#include <isc/app.h>
#include <isc/boolean.h>
#include <isc/condition.h>
#include <isc/msgs.h>
#include <isc/mutex.h>
#include <isc/event.h>
#include <isc/platform.h>
#include <isc/strerror.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/time.h>
#include <isc/util.h>
#ifdef ISC_PLATFORM_USETHREADS
#include <pthread.h>
#else
#include "../timer_p.h"
#include "../task_p.h"
#include "socket_p.h"
#endif
static isc_eventlist_t on_run;
static isc_mutex_t lock;
static isc_boolean_t shutdown_requested = ISC_FALSE;
static isc_boolean_t running = ISC_FALSE;
static volatile isc_boolean_t want_shutdown = ISC_FALSE;
static volatile isc_boolean_t want_reload = ISC_FALSE;
static isc_boolean_t blocked = ISC_FALSE;
#ifdef ISC_PLATFORM_USETHREADS
static pthread_t blockedthread;
#endif
#ifdef HAVE_LINUXTHREADS
#undef HAVE_SIGWAIT
static pthread_t main_thread;
#endif
#ifndef HAVE_SIGWAIT
static void
exit_action(int arg) {
UNUSED(arg);
want_shutdown = ISC_TRUE;
}
static void
reload_action(int arg) {
UNUSED(arg);
want_reload = ISC_TRUE;
}
#endif
static isc_result_t
handle_signal(int sig, void (*handler)(int)) {
struct sigaction sa;
char strbuf[ISC_STRERRORSIZE];
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
if (sigfillset(&sa.sa_mask) != 0 ||
sigaction(sig, &sa, NULL) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat, ISC_MSGSET_APP,
ISC_MSG_SIGNALSETUP,
"handle_signal() %d setup: %s"),
sig, strbuf);
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_start(void) {
isc_result_t result;
int presult;
sigset_t sset;
char strbuf[ISC_STRERRORSIZE];
#ifdef NEED_PTHREAD_INIT
presult = pthread_init();
if (presult != 0) {
isc__strerror(presult, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_start() pthread_init: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
#endif
#ifdef HAVE_LINUXTHREADS
main_thread = pthread_self();
#endif
result = isc_mutex_init(&lock);
if (result != ISC_R_SUCCESS)
return (result);
#ifndef HAVE_SIGWAIT
result = handle_signal(SIGINT, exit_action);
if (result != ISC_R_SUCCESS)
return (result);
result = handle_signal(SIGTERM, exit_action);
if (result != ISC_R_SUCCESS)
return (result);
#endif
result = handle_signal(SIGPIPE, SIG_IGN);
if (result != ISC_R_SUCCESS)
return (result);
result = handle_signal(SIGHUP, SIG_DFL);
if (result != ISC_R_SUCCESS)
return (result);
#ifdef HAVE_SIGWAIT
result = handle_signal(SIGTERM, SIG_DFL);
if (result != ISC_R_SUCCESS)
return (result);
result = handle_signal(SIGINT, SIG_DFL);
if (result != ISC_R_SUCCESS)
return (result);
#endif
#ifdef ISC_PLATFORM_USETHREADS
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_start() sigsetops: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
if (presult != 0) {
isc__strerror(presult, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_start() pthread_sigmask: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
#else
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_start() sigsetops: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
presult = sigprocmask(SIG_UNBLOCK, &sset, NULL);
if (presult != 0) {
isc__strerror(presult, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_start() sigprocmask: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
#endif
ISC_LIST_INIT(on_run);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
void *arg)
{
isc_event_t *event;
isc_task_t *cloned_task = NULL;
isc_result_t result;
LOCK(&lock);
if (running) {
result = ISC_R_ALREADYRUNNING;
goto unlock;
}
isc_task_attach(task, &cloned_task);
event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
action, arg, sizeof(*event));
if (event == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
ISC_LIST_APPEND(on_run, event, ev_link);
result = ISC_R_SUCCESS;
unlock:
UNLOCK(&lock);
return (result);
}
#ifndef ISC_PLATFORM_USETHREADS
static isc_result_t
evloop(void) {
isc_result_t result;
while (!want_shutdown) {
int n;
isc_time_t when, now;
struct timeval tv, *tvp;
isc_socketwait_t *swait;
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;
}
}
swait = NULL;
n = isc__socketmgr_waitevents(tvp, &swait);
if (n == 0 || call_timer_dispatch) {
isc__timermgr_dispatch();
}
if (n > 0)
(void)isc__socketmgr_dispatch(swait);
(void)isc__taskmgr_dispatch();
if (want_reload) {
want_reload = ISC_FALSE;
return (ISC_R_RELOAD);
}
}
return (ISC_R_SUCCESS);
}
static isc_boolean_t in_recursive_evloop = ISC_FALSE;
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);
--*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_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];
#ifdef HAVE_SIGWAIT
int sig;
#endif
#endif
#ifdef HAVE_LINUXTHREADS
REQUIRE(main_thread == pthread_self());
#endif
LOCK(&lock);
if (!running) {
running = ISC_TRUE;
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
result = handle_signal(SIGHUP, reload_action);
if (result != ISC_R_SUCCESS)
return (ISC_R_SUCCESS);
#endif
#ifdef ISC_PLATFORM_USETHREADS
while (!want_shutdown) {
#ifdef HAVE_SIGWAIT
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
sig = sigwait(&sset);
if (sig >= 0) {
if (sig == SIGINT ||
sig == SIGTERM)
want_shutdown = ISC_TRUE;
else if (sig == SIGHUP)
want_reload = ISC_TRUE;
}
#endif
#else
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
if (want_reload) {
want_reload = ISC_FALSE;
return (ISC_R_RELOAD);
}
if (want_shutdown && blocked)
exit(1);
}
#else
(void)isc__taskmgr_dispatch();
result = evloop();
if (result != ISC_R_SUCCESS)
return (result);
#endif
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);
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
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
}
void
isc_app_unblock(void) {
#ifdef ISC_PLATFORM_USETHREADS
sigset_t sset;
#endif
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
}