Commit ae3ffdf7 authored by Geoff Simmons's avatar Geoff Simmons

varnishevent: fixed reopen on HUP

parent 4f9899eb
......@@ -112,6 +112,7 @@ CONF_Add(const char *lval, const char *rval)
confString("cformat", cformat);
confString("bformat", bformat);
confString("zformat", zformat);
confString("output.file", output_file);
confUnsigned("max.reclen", max_reclen);
confUnsigned("max.headers", max_headers);
......@@ -121,6 +122,7 @@ CONF_Add(const char *lval, const char *rval)
confUnsigned("output.bufsiz", output_bufsiz);
confUnsigned("housekeep.interval", housekeep_interval);
confUnsigned("ttl", ttl);
confUnsigned("append", append);
if (strcmp(lval, "syslog.facility") == 0) {
if ((ret = conf_getFacility(rval)) < 0)
......@@ -194,6 +196,10 @@ CONF_Init(void)
config.housekeep_interval = DEFAULT_HOUSEKEEP_INTERVAL;
config.ttl = DEFAULT_TTL;
/* Default is stdout */
config.output_file[0] = '\0';
config.append = 0;
pw = getpwnam(DEFAULT_USER);
if (pw == NULL)
pw = getpwuid(getuid());
......@@ -265,6 +271,9 @@ CONF_Dump(void)
confdump("log.file = %s",
strcmp(config.log_file,"-") == 0 ? "stdout" : config.log_file);
confdump("varnish.bindump = %s", config.varnish_bindump);
confdump("output.file = %s",
EMPTY(config.output_file) ? "stdout" : config.output_file);
confdump("append = %u", config.append);
confdump("syslog.facility = %s", config.syslog_facility_name);
confdump("monitor.interval = %u", config.monitor_interval);
confdump("max.reclen = %u", config.max_reclen);
......
......@@ -127,8 +127,6 @@ static unsigned rdr_free = 0;
static int waiting = 0;
static char *obuf;
static char cli_config_filename[BUFSIZ] = "";
int
......@@ -422,27 +420,6 @@ dump(int sig)
DATA_Dump();
}
static FILE *
open_log(const char *ofn, int append)
{
FILE *of;
if ((of = fopen(ofn, append ? "a" : "w")) == NULL) {
perror(ofn);
exit(1);
}
obuf = (char *) malloc(config.output_bufsiz);
if (obuf == NULL) {
perror(ofn);
exit(1);
}
if (setvbuf(of, obuf, _IOFBF, config.output_bufsiz) != 0) {
perror(ofn);
exit(1);
}
return (of);
}
/*--------------------------------------------------------------------*/
static void
......@@ -474,15 +451,13 @@ main(int argc, char *argv[])
int c, errnum;
int a_flag = 0, g_flag = 0, D_flag = 0, format_flag = 0;
const char *P_arg = NULL, *w_arg = NULL;
char scratch[BUFSIZ];
char scratch[BUFSIZ], cformat[BUFSIZ];
struct vpf_fh *pfh = NULL;
FILE *of;
vd = VSM_New();
VSL_Setup(vd);
CONF_Init();
read_default_config();
cformat[0] = '\0';
while ((c = getopt(argc, argv, VSL_ARGS "aDP:Vw:fF:gG:")) != -1) {
switch (c) {
case 'a':
......@@ -493,7 +468,7 @@ main(int argc, char *argv[])
fprintf(stderr, "-f and -F can not be combined\n");
exit(1);
}
strcpy(config.cformat, ALT_CFORMAT);
strcpy(cformat, ALT_CFORMAT);
format_flag = 1;
break;
case 'F':
......@@ -502,7 +477,7 @@ main(int argc, char *argv[])
exit(1);
}
format_flag = 1;
strcpy(config.cformat, optarg);
strcpy(cformat, optarg);
break;
case 'D':
D_flag = 1;
......@@ -537,7 +512,10 @@ main(int argc, char *argv[])
usage();
}
}
CONF_Init();
read_default_config();
if (! EMPTY(cli_config_filename)) {
printf("Reading config from %s\n", cli_config_filename);
if (CONF_ReadFile(cli_config_filename) != 0) {
......@@ -547,6 +525,9 @@ main(int argc, char *argv[])
}
}
if (! EMPTY(cformat))
strcpy(config.cformat, cformat);
if (VSL_Open(vd, 1))
exit(1);
......@@ -565,13 +546,12 @@ main(int argc, char *argv[])
if (pfh != NULL)
VPF_Write(pfh);
if (w_arg) {
of = open_log(w_arg, a_flag);
if (w_arg)
strcpy(config.output_file, w_arg);
if (!EMPTY(config.output_file))
signal(SIGHUP, sighup);
} else {
w_arg = "stdout";
of = stdout;
}
if (a_flag)
config.append = 1;
if (LOG_Open("varnishevent") != 0) {
exit(EXIT_FAILURE);
......@@ -623,7 +603,7 @@ main(int argc, char *argv[])
else
LOG_Log0(LOG_INFO, "Monitoring thread not running");
if ((errnum = WRT_Init(of)) != 0) {
if ((errnum = WRT_Init()) != 0) {
LOG_Log(LOG_ALERT, "Cannot init writer thread: %s\n", strerror(errnum));
exit(EXIT_FAILURE);
}
......@@ -649,17 +629,9 @@ main(int argc, char *argv[])
assert(VSL_Arg(vd, 'i', scratch) > 0);
LOG_Log(LOG_INFO, "Reading SHM tags: %s", scratch);
while (VSL_Dispatch(vd, h_ncsa, of) >= 0) {
if (fflush(of) != 0) {
perror(w_arg);
exit(1);
}
if (reopen && of != stdout) {
fclose(of);
of = open_log(w_arg, a_flag);
reopen = 0;
}
}
while (VSL_Dispatch(vd, h_ncsa, NULL) >= 0)
if (reopen)
WRT_Reopen();
WRT_Halt();
MON_Shutdown();
......
......@@ -33,6 +33,7 @@
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <limits.h>
#include "varnishapi.h"
#include "vqueue.h"
......@@ -129,6 +130,9 @@ struct config {
char varnish_name[BUFSIZ];
char log_file[BUFSIZ];
char output_file[PATH_MAX];
unsigned append;
/* VSL 'r' argument */
char varnish_bindump[BUFSIZ];
......@@ -207,11 +211,12 @@ void DATA_Return_Freelist(struct freehead_s *returned, unsigned nreturned);
void DATA_Dump(void);
/* writer.c */
int WRT_Init(FILE *out);
int WRT_Init(void);
void WRT_Start(void);
void WRT_Stats(void);
int WRT_Running(void);
int WRT_Waiting(void);
void WRT_Reopen(void);
void WRT_Halt(void);
void WRT_Shutdown(void);
......
......@@ -70,7 +70,10 @@ static struct freehead_s wrt_freelist;
static unsigned wrt_nfree;
static struct vsb *os;
static FILE *fo;
static char *obuf;
static pthread_mutex_t reopen_lock;
/* stats */
static unsigned deqs = 0;
......@@ -86,7 +89,26 @@ typedef struct writer_data_s {
static writer_data_t wrt_data;
static unsigned run, cleaned = 0;
static unsigned run, cleaned = 0, reopen = 0;
static int
open_log(void)
{
if (EMPTY(config.output_file))
fo = stdout;
else if ((fo = fopen(config.output_file, config.append ? "a" : "w"))
== NULL)
return errno;
obuf = (char *) malloc(config.output_bufsiz);
if (obuf == NULL)
return errno;
if (setvbuf(fo, obuf, _IOFBF, config.output_bufsiz) != 0)
return errno;
return 0;
}
static inline void
wrt_return_freelist(void)
......@@ -103,6 +125,27 @@ wrt_write(logline_t *ll)
CHECK_OBJ_NOTNULL(ll, LOGLINE_MAGIC);
assert(ll->state == DATA_DONE);
AZ(pthread_mutex_lock(&reopen_lock));
if (reopen && fo != stdout) {
if (fflush(fo) != 0) {
LOG_Log(LOG_ALERT, "Cannot flush to %s, exiting: %s",
config.output_file, strerror(errno));
exit(EXIT_FAILURE);
}
if (fclose(fo) != 0) {
LOG_Log(LOG_ALERT, "Cannot close %s, exiting: %s",
config.output_file, strerror(errno));
exit(EXIT_FAILURE);
}
if (open_log() != 0) {
LOG_Log(LOG_ALERT, "Cannot reopen %s, exiting: %s",
config.output_file, strerror(errno));
exit(EXIT_FAILURE);
}
reopen = 0;
}
AZ(pthread_mutex_unlock(&reopen_lock));
VSB_clear(os);
FMT_Format(ll, os);
VSB_finish(os);
......@@ -192,20 +235,23 @@ static void wrt_cleanup(void)
cleaned = 1;
}
/* XXX: init out from config */
int
WRT_Init(FILE *out)
WRT_Init(void)
{
run = 1;
int err;
if ((err = open_log()) != 0)
return err;
AZ(pthread_mutex_init(&reopen_lock, &attr_lock));
wrt_data.magic = WRITER_DATA_MAGIC;
wrt_data.state = WRT_NOTSTARTED;
fo = out;
/* XXX: fixed size? */
os = VSB_new_auto();
run = 1;
return 0;
}
......@@ -237,6 +283,14 @@ WRT_Waiting(void)
return wrt_data.state == WRT_WAITING;
}
void
WRT_Reopen(void)
{
AZ(pthread_mutex_lock(&reopen_lock));
reopen = 1;
AZ(pthread_mutex_unlock(&reopen_lock));
}
void
WRT_Halt(void)
{
......
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