From ae00fbc8a4af662a3b1361075ec3b2936485b844 Mon Sep 17 00:00:00 2001 From: Reyk Floeter Date: Sun, 22 Nov 2015 13:27:14 +0000 Subject: Update log.c: change fatal() and fatalx() into variadic functions, include the process name, and replace all calls of fatal*(NULL) with fatal(__func__) for better debugging. OK benno@ --- sbin/iked/log.c | 71 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 19 deletions(-) (limited to 'sbin/iked/log.c') diff --git a/sbin/iked/log.c b/sbin/iked/log.c index 36a15cd333c..a8bae17d9b2 100644 --- a/sbin/iked/log.c +++ b/sbin/iked/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.6 2015/11/21 13:46:29 reyk Exp $ */ +/* $OpenBSD: log.c,v 1.7 2015/11/22 13:27:13 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -24,10 +24,12 @@ #include #include -int debug; -int verbose; +int debug; +int verbose; +const char *log_procname; -void log_init(int); +void log_init(int, int); +void log_procinit(const char *); void log_verbose(int); void log_warn(const char *, ...) __attribute__((__format__ (printf, 1, 2))); @@ -41,23 +43,33 @@ void logit(int, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void vlog(int, const char *, va_list) __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *); -__dead void fatalx(const char *); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); void -log_init(int n_debug) +log_init(int n_debug, int facility) { extern char *__progname; debug = n_debug; verbose = n_debug; + log_procinit(__progname); if (!debug) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + openlog(__progname, LOG_PID | LOG_NDELAY, facility); tzset(); } +void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + void log_verbose(int v) { @@ -150,24 +162,45 @@ log_debug(const char *emsg, ...) } } -void -fatal(const char *emsg) +static void +vfatal(const char *emsg, va_list ap) { - if (emsg == NULL) - logit(LOG_CRIT, "fatal: %s", strerror(errno)); + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; + } + if (errno) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, s, sep, strerror(errno)); else - if (errno) - logit(LOG_CRIT, "fatal: %s: %s", - emsg, strerror(errno)); - else - logit(LOG_CRIT, "fatal: %s", emsg); + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s); +} +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); exit(1); } void -fatalx(const char *emsg) +fatalx(const char *emsg, ...) { + va_list ap; + errno = 0; - fatal(emsg); + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); + exit(1); } -- cgit v1.2.3