Commit d3884da5 authored by Geoff Simmons's avatar Geoff Simmons

refactor and fix signal handling

parent 0e18aad6
......@@ -97,9 +97,9 @@ static unsigned long seen = 0, submitted = 0, len_overflows = 0, no_data = 0,
ioerr = 0, reacquire = 0, truncated = 0, key_hi = 0, key_overflows = 0;
// no_free_chunk = 0;
static volatile sig_atomic_t flush = 0;
static volatile sig_atomic_t flush = 0, term = 0;
static struct sigaction dump_action;
static struct sigaction terminate_action, dump_action;
/* Local freelist */
static struct freehead_s reader_freelist =
......@@ -125,12 +125,21 @@ RDR_Stats(void)
static void
dump(int sig)
{
LOG_Log(LOG_NOTICE, "Received signal %d (%s), "
LOG_Log(LOG_NOTICE, "Child process received signal %d (%s), "
"dumping config and data table", sig, strsignal(sig));
CONF_Dump();
CONF_Dump(LOG_INFO);
DATA_Dump();
}
static void
term_s(int sig)
{
LOG_Log(LOG_NOTICE, "Child process received signal %d (%s), "
"will flush logs and terminate", sig, strsignal(sig));
term = 1;
flush = 1;
}
/*--------------------------------------------------------------------*/
/* efficiently retrieve a single data entry */
......@@ -445,7 +454,7 @@ CHILD_Main(int readconfig)
LOG_Log(LOG_NOTICE, "Running as %s", pw->pw_name);
if (debug)
CONF_Dump();
CONF_Dump(LOG_DEBUG);
/* read messaging module */
if (config.mq_module[0] == '\0') {
......@@ -474,6 +483,10 @@ CHILD_Main(int readconfig)
AZ(sigemptyset(&dump_action.sa_mask));
dump_action.sa_flags |= SA_RESTART;
terminate_action.sa_handler = term_s;
AZ(sigemptyset(&terminate_action.sa_mask));
terminate_action.sa_flags &= ~SA_RESTART;
#define CHILD(SIG,disp) SIGDISP(SIG,disp)
#define PARENT(SIG,disp) ((void) 0)
#include "signals.h"
......@@ -648,20 +661,19 @@ CHILD_Main(int readconfig)
break;
}
}
if (flush) {
if (flush && !term) {
LOG_Log0(LOG_NOTICE, "Flushing transactions");
take_free();
VSLQ_Flush(vslq, dispatch, NULL);
flush = 0;
if (!term && EMPTY(config.varnish_bindump) &&
status != DISPATCH_CLOSED && status != DISPATCH_OVERRUN
&& status != DISPATCH_IOERR)
if (EMPTY(config.varnish_bindump) && status != DISPATCH_CLOSED
&& status != DISPATCH_OVERRUN && status != DISPATCH_IOERR)
continue;
VSLQ_Delete(&vslq);
AZ(vslq);
/* cf. VUT_Main() in Varnish vut.c */
LOG_Log0(LOG_NOTICE, "Attempting to reacquire the log");
while (!term && vslq == NULL) {
while (vslq == NULL) {
AN(vsm);
VTIM_sleep(0.1);
if (VSM_Open(vsm)) {
......
......@@ -242,31 +242,32 @@ CONF_ReadDefault(void)
return 0;
}
#define confdump(str,val) \
LOG_Log(LOG_DEBUG, "config: " str, (val))
#define confdump(level,str,val) \
LOG_Log((level), "config: " str, (val))
void
CONF_Dump(void)
CONF_Dump(int level)
{
confdump("pid.file = %s", config.pid_file);
confdump("varnish.name = %s", config.varnish_name);
confdump("log.file = %s",
strcmp(config.log_file,"-") == 0 ? "stdout" : config.log_file);
confdump("varnish.bindump = %s", config.varnish_bindump);
confdump("syslog.facility = %s", config.syslog_facility_name);
confdump("monitor.interval = %u", config.monitor_interval);
confdump("monitor.workers = %s", config.monitor_workers ? "true" : "false");
confdump("maxdone = %u", config.maxdone);
confdump("maxdata = %u", config.maxdata);
confdump("maxkeylen = %u", config.maxkeylen);
confdump("qlen.goal = %u", config.qlen_goal);
confdump(level, "pid.file = %s", config.pid_file);
confdump(level, "varnish.name = %s", config.varnish_name);
confdump(level, "log.file = %s",
strcmp(config.log_file,"-") == 0 ? "stdout" : config.log_file);
confdump(level, "varnish.bindump = %s", config.varnish_bindump);
confdump(level, "syslog.facility = %s", config.syslog_facility_name);
confdump(level, "monitor.interval = %u", config.monitor_interval);
confdump(level, "monitor.workers = %s",
config.monitor_workers ? "true" : "false");
confdump(level, "maxdone = %u", config.maxdone);
confdump(level, "maxdata = %u", config.maxdata);
confdump(level, "maxkeylen = %u", config.maxkeylen);
confdump(level, "qlen.goal = %u", config.qlen_goal);
confdump("mq.module = %s", config.mq_module);
confdump("mq.config_file = %s", config.mq_config_file);
confdump("nworkers = %u", config.nworkers);
confdump("restarts = %u", config.restarts);
confdump("restart.pause = %u", config.restart_pause);
confdump("idle.pause = %f", config.idle_pause);
confdump("thread.restarts = %u", config.thread_restarts);
confdump("user = %s", config.user_name);
confdump(level, "mq.module = %s", config.mq_module);
confdump(level, "mq.config_file = %s", config.mq_config_file);
confdump(level, "nworkers = %u", config.nworkers);
confdump(level, "restarts = %u", config.restarts);
confdump(level, "restart.pause = %u", config.restart_pause);
confdump(level, "idle.pause = %f", config.idle_pause);
confdump(level, "thread.restarts = %u", config.thread_restarts);
confdump(level, "user = %s", config.user_name);
}
......@@ -58,13 +58,6 @@
/*--------------------------------------------------------------------*/
void
HNDL_Terminate(int sig)
{
(void) sig;
term = 1;
}
#ifdef HAVE_EXECINFO_H
/*
......
......@@ -62,9 +62,9 @@
#include "vpf.h"
#include "vtim.h"
static volatile sig_atomic_t reload;
static volatile sig_atomic_t term = 0, reload = 0;
static struct sigaction restart_action;
static struct sigaction terminate_action, restart_action;
static const char *version = PACKAGE_STRING " revision " VCS_Version
" branch " VCS_Branch;
......@@ -74,10 +74,19 @@ static const char *version = PACKAGE_STRING " revision " VCS_Version
static void
restart(int sig)
{
(void) sig;
LOG_Log(LOG_NOTICE, "Management process received signal %d (%s), "
"will restart child process with new config", sig, strsignal(sig));
reload = 1;
}
static void
term_s(int sig)
{
LOG_Log(LOG_NOTICE, "Management process received signal %d (%s), "
"will terminate management and worker", sig, strsignal(sig));
term = 1;
}
/*--------------------------------------------------------------------*/
/* Handle for the PID file */
......@@ -89,7 +98,7 @@ parent_shutdown(int status, pid_t child_pid)
if (child_pid && kill(child_pid, SIGTERM) != 0) {
LOG_Log(LOG_ERR, "Cannot kill child process %d: %s", child_pid,
strerror(errno));
exit(EXIT_FAILURE);
status = EXIT_FAILURE;
}
/* Remove PID file if necessary */
......@@ -139,8 +148,10 @@ parent_main(pid_t child_pid)
AZ(sigemptyset(&restart_action.sa_mask));
restart_action.sa_flags &= ~SA_RESTART;
term = 0;
reload = 0;
terminate_action.sa_handler = term_s;
AZ(sigemptyset(&terminate_action.sa_mask));
terminate_action.sa_flags &= ~SA_RESTART;
/* install signal handlers */
#define PARENT(SIG,disp) SIGDISP(SIG,disp)
#define CHILD(SIG,disp) ((void) 0)
......@@ -310,7 +321,7 @@ main(int argc, char * const *argv)
LOG_SetLevel(LOG_DEBUG);
LOG_Log(LOG_INFO, "initializing (%s)", version);
CONF_Dump();
CONF_Dump(LOG_DEBUG);
if (!EMPTY(config.pid_file)
&& (pfh = VPF_Open(config.pid_file, 0644, NULL)) == NULL) {
......@@ -331,10 +342,6 @@ main(int argc, char * const *argv)
if (pfh != NULL)
VPF_Write(pfh);
terminate_action.sa_handler = HNDL_Terminate;
AZ(sigemptyset(&terminate_action.sa_mask));
terminate_action.sa_flags &= ~SA_RESTART;
stacktrace_action.sa_handler = HNDL_Abort;
ignore_action.sa_handler = SIG_IGN;
......
......@@ -79,14 +79,10 @@ struct mqf {
strerror(errno)); \
} while(0)
volatile sig_atomic_t term;
struct sigaction terminate_action, ignore_action, stacktrace_action,
default_action;
struct sigaction ignore_action, stacktrace_action, default_action;
void HNDL_Init(const char *a0);
void HNDL_Abort(int sig);
void HNDL_Terminate(int sig);
/* sandbox.c */
......@@ -275,7 +271,7 @@ int CONF_Add(const char *lval, const char *rval);
* <0 if the file was read but the contents could not be processed
*/
int CONF_ReadDefault(void);
void CONF_Dump(void);
void CONF_Dump(int level);
/* log.c */
typedef void log_log_t(int level, const char *msg, ...);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment