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