From 44d4ab7a960cf143c43d1645f2985cc9d74e3077 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Wed, 7 Apr 2010 11:00:36 +0200 Subject: Configurable syslog name. Also fixes a bug in syslog initialization. --- sysdep/unix/config.Y | 10 ++++++++-- sysdep/unix/log.c | 49 +++++++++++++++++++++++++++++++++---------------- sysdep/unix/main.c | 23 +++++++++++++++++++---- sysdep/unix/unix.h | 4 ++-- 4 files changed, 62 insertions(+), 24 deletions(-) (limited to 'sysdep/unix') diff --git a/sysdep/unix/config.Y b/sysdep/unix/config.Y index ac5be7e2..844f53df 100644 --- a/sysdep/unix/config.Y +++ b/sysdep/unix/config.Y @@ -14,12 +14,13 @@ CF_HDR CF_DECLS CF_KEYWORDS(LOG, SYSLOG, ALL, DEBUG, TRACE, INFO, REMOTE, WARNING, ERROR, AUTH, FATAL, BUG, STDERR, SOFT) -CF_KEYWORDS(TIMEFORMAT, ISO, SHORT, LONG, BASE) +CF_KEYWORDS(TIMEFORMAT, ISO, SHORT, LONG, BASE, NAME) %type log_mask log_mask_list log_cat %type log_file %type cfg_name %type timeformat_which +%type syslog_name CF_GRAMMAR @@ -33,13 +34,18 @@ log_config: LOG log_file log_mask ';' { } ; +syslog_name: + NAME TEXT { $$ = $2; } + | { $$ = bird_name; } + ; + log_file: TEXT { FILE *f = tracked_fopen(new_config->pool, $1, "a"); if (!f) cf_error("Unable to open log file `%s': %m", $1); $$ = f; } - | SYSLOG { $$ = NULL; } + | SYSLOG syslog_name { $$ = NULL; new_config->syslog_name = $2; } | STDERR { $$ = stderr; } ; diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c index 3d3b4337..d4c36488 100644 --- a/sysdep/unix/log.c +++ b/sysdep/unix/log.c @@ -28,9 +28,9 @@ #include "lib/lists.h" #include "lib/unix.h" -static FILE *dbgf = NULL; +static FILE *dbgf; static list *current_log_list; -static list init_log_list; +static char *current_syslog_name; /* NULL -> syslog closed */ bird_clock_t rate_limit_time = 5; int rate_limit_count = 5; @@ -209,38 +209,55 @@ debug(char *msg, ...) va_end(args); } -void -log_init(int debug, int init) +static list * +default_log_list(int debug, int init, char **syslog_name) { - static struct log_config lc_stderr = { mask: ~0, terminal_flag: 1 }; - + static list init_log_list; init_list(&init_log_list); - current_log_list = &init_log_list; + *syslog_name = NULL; #ifdef HAVE_SYSLOG if (!debug) { static struct log_config lc_syslog = { mask: ~0 }; - openlog("bird", LOG_CONS | LOG_NDELAY, LOG_DAEMON); - add_tail(current_log_list, &lc_syslog.n); + add_tail(&init_log_list, &lc_syslog.n); + *syslog_name = bird_name; if (!init) - return; + return &init_log_list; } #endif + static struct log_config lc_stderr = { mask: ~0, terminal_flag: 1 }; lc_stderr.fh = stderr; - add_tail(current_log_list, &lc_stderr.n); + add_tail(&init_log_list, &lc_stderr.n); + return &init_log_list; } void -log_switch(int debug, list *l) +log_switch(int debug, list *l, char *new_syslog_name) { - if (EMPTY_LIST(*l)) - log_init(debug, 0); - else - current_log_list = l; + if (!l || EMPTY_LIST(*l)) + l = default_log_list(debug, !l, &new_syslog_name); + + current_log_list = l; + +#ifdef HAVE_SYSLOG + if (current_syslog_name && new_syslog_name && + !strcmp(current_syslog_name, new_syslog_name)) + return; + + if (current_syslog_name) + closelog(); + + if (new_syslog_name) + openlog(new_syslog_name, LOG_CONS | LOG_NDELAY, LOG_DAEMON); + + current_syslog_name = new_syslog_name; +#endif } + + void log_init_debug(char *f) { diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index e0e0d636..732c9169 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -83,7 +83,7 @@ sysdep_preconfig(struct config *c) int sysdep_commit(struct config *new, struct config *old UNUSED) { - log_switch(debug_flag, &new->logfiles); + log_switch(debug_flag, &new->logfiles, new->syslog_name); return 0; } @@ -378,21 +378,36 @@ signal_init(void) */ static char *opt_list = "c:dD:ps:"; +static int parse_and_exit; +char *bird_name; static void usage(void) { - fprintf(stderr, "Usage: bird [-c ] [-d] [-D ] [-p] [-s ]\n"); + fprintf(stderr, "Usage: %s [-c ] [-d] [-D ] [-p] [-s ]\n", bird_name); exit(1); } -int parse_and_exit; +static inline char * +get_bird_name(char *s, char *def) +{ + char *t; + if (!s) + return def; + t = strrchr(s, '/'); + if (!t) + return s; + if (!t[1]) + return def; + return t+1; +} static void parse_args(int argc, char **argv) { int c; + bird_name = get_bird_name(argv[0], "bird"); if (argc == 2) { if (!strcmp(argv[1], "--version")) @@ -444,7 +459,7 @@ main(int argc, char **argv) parse_args(argc, argv); if (debug_flag == 1) log_init_debug(""); - log_init(debug_flag, 1); + log_switch(debug_flag, NULL, NULL); if (!parse_and_exit) test_old_bird(path_control_socket); diff --git a/sysdep/unix/unix.h b/sysdep/unix/unix.h index 4de74f28..76825aad 100644 --- a/sysdep/unix/unix.h +++ b/sysdep/unix/unix.h @@ -15,6 +15,7 @@ struct pool; /* main.c */ +extern char *bird_name; void async_config(void); void async_dump(void); void async_shutdown(void); @@ -60,9 +61,8 @@ void krt_io_init(void); /* log.c */ -void log_init(int debug, int init); void log_init_debug(char *); /* Initialize debug dump to given file (NULL=stderr, ""=off) */ -void log_switch(int debug, struct list *); +void log_switch(int debug, list *l, char *); /* Use l=NULL for initial switch */ struct log_config { node n; -- cgit v1.2.3