From: benno Date: Sun, 18 Jun 2017 12:03:47 +0000 (+0000) Subject: update log.c in ifstated to the same used in bgpd etc. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=bfdb9ad444cb5d17bc1b9442362fbf55eee5e8f5;p=openbsd update log.c in ifstated to the same used in bgpd etc. Done by Rob Pierce , thanks --- diff --git a/usr.sbin/ifstated/ifstated.c b/usr.sbin/ifstated/ifstated.c index 0783d473d54..371c5eec9ba 100644 --- a/usr.sbin/ifstated/ifstated.c +++ b/usr.sbin/ifstated/ifstated.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.c,v 1.41 2013/05/30 19:22:48 henning Exp $ */ +/* $OpenBSD: ifstated.c,v 1.42 2017/06/18 12:03:47 benno Exp $ */ /* * Copyright (c) 2004 Marco Pfatschbacher @@ -36,12 +36,14 @@ #include #include #include +#include #include #include #include #include #include "ifstated.h" +#include "log.h" struct ifsd_config *conf = NULL, *newconf = NULL; @@ -88,7 +90,8 @@ main(int argc, char *argv[]) int ch; int debug = 0; - log_init(1); + log_init(1, LOG_DAEMON); /* log to stderr until daemonized */ + log_setverbose(1); while ((ch = getopt(argc, argv, "dD:f:hniv")) != -1) { switch (ch) { @@ -138,7 +141,8 @@ main(int argc, char *argv[]) daemon(1, 0); event_init(); - log_init(debug); + log_init(debug, LOG_DAEMON); + log_setverbose(opts & IFSD_OPT_VERBOSE); signal_set(&sigchld_ev, SIGCHLD, sigchld_handler, NULL); signal_add(&sigchld_ev, NULL); @@ -169,12 +173,12 @@ startup_handler(int fd, short event, void *arg) rtfilter = ROUTE_FILTER(RTM_IFINFO); if (setsockopt(rt_fd, PF_ROUTE, ROUTE_MSGFILTER, &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */ - log_warn("startup_handler: setsockopt msgfilter"); + log_warn("%s: setsockopt msgfilter", __func__); rtfilter = RTABLE_ANY; if (setsockopt(rt_fd, PF_ROUTE, ROUTE_TABLEFILTER, &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */ - log_warn("startup_handler: setsockopt tablefilter"); + log_warn("%s: setsockopt tablefilter", __func__); event_set(&rt_msg_ev, rt_fd, EV_READ|EV_PERSIST, rt_msg_handler, NULL); event_add(&rt_msg_ev, NULL); @@ -580,7 +584,7 @@ do_action(struct ifsd_action *action) } break; default: - log_debug("do_action: unknown action %d", action->type); + log_debug("%s: unknown action %d", __func__, action->type); break; } } diff --git a/usr.sbin/ifstated/ifstated.h b/usr.sbin/ifstated/ifstated.h index 2756595e90d..6807c793b61 100644 --- a/usr.sbin/ifstated/ifstated.h +++ b/usr.sbin/ifstated/ifstated.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.h,v 1.10 2016/07/19 08:04:53 benno Exp $ */ +/* $OpenBSD: ifstated.h,v 1.11 2017/06/18 12:03:47 benno Exp $ */ /* * Copyright (c) 2004 Ryan McBride @@ -135,20 +135,3 @@ enum { IFSD_EVTIMER_ADD, IFSD_EVTIMER_DEL }; struct ifsd_config *parse_config(char *, int); int cmdline_symset(char *); void clear_config(struct ifsd_config *); - -/* log.c */ -void log_init(int); -void log_warn(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_warnx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_info(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void vlog(int, const char *, va_list) - __attribute__((__format__ (printf, 2, 0))); -void logit(int, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); -void fatal(const char *) __dead; -void fatalx(const char *) __dead; diff --git a/usr.sbin/ifstated/log.c b/usr.sbin/ifstated/log.c index 75e4d86dadf..db925ae200b 100644 --- a/usr.sbin/ifstated/log.c +++ b/usr.sbin/ifstated/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.4 2017/03/21 12:06:55 bluhm Exp $ */ +/* $OpenBSD: log.c,v 1.5 2017/06/18 12:03:47 benno Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -16,39 +16,54 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include #include #include +#include #include #include +#include #include -void log_init(int); -void log_warn(const char *, ...); -void log_warnx(const char *, ...); -void log_info(const char *, ...); -void log_debug(const char *, ...); -__dead void fatal(const char *); -__dead void fatalx(const char *); -void vlog(int, const char *, va_list); -void logit(int, const char *, ...); +#include "log.h" -int debug; +static int debug; +static int verbose; +static const char *log_procname; 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_setverbose(int v) +{ + verbose = v; +} + +int +log_getverbose(void) +{ + return (verbose); +} + void logit(int pri, const char *fmt, ...) { @@ -63,6 +78,7 @@ void vlog(int pri, const char *fmt, va_list ap) { char *nfmt; + int saved_errno = errno; if (debug) { /* best effort in out of mem situations */ @@ -76,30 +92,36 @@ vlog(int pri, const char *fmt, va_list ap) fflush(stderr); } else vsyslog(pri, fmt, ap); + + errno = saved_errno; } void log_warn(const char *emsg, ...) { - char *nfmt; - va_list ap; + char *nfmt; + va_list ap; + int saved_errno = errno; /* best effort to even work in out of memory situations */ if (emsg == NULL) - logit(LOG_ERR, "%s", strerror(errno)); + logit(LOG_ERR, "%s", strerror(saved_errno)); else { va_start(ap, emsg); - if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { + if (asprintf(&nfmt, "%s: %s", emsg, + strerror(saved_errno)) == -1) { /* we tried it... */ vlog(LOG_ERR, emsg, ap); - logit(LOG_ERR, "%s", strerror(errno)); + logit(LOG_ERR, "%s", strerror(saved_errno)); } else { vlog(LOG_ERR, nfmt, ap); free(nfmt); } va_end(ap); } + + errno = saved_errno; } void @@ -127,31 +149,51 @@ log_debug(const char *emsg, ...) { va_list ap; - if (debug) { + if (verbose) { va_start(ap, emsg); vlog(LOG_DEBUG, emsg, ap); va_end(ap); } } -void -fatal(const char *emsg) +static void +vfatalc(int code, 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 (code) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, s, sep, strerror(code)); 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); + vfatalc(errno, emsg, ap); + va_end(ap); exit(1); } void -fatalx(const char *emsg) +fatalx(const char *emsg, ...) { - errno = 0; - fatal(emsg); + va_list ap; + + va_start(ap, emsg); + vfatalc(0, emsg, ap); + va_end(ap); + exit(1); } diff --git a/usr.sbin/ifstated/log.h b/usr.sbin/ifstated/log.h new file mode 100644 index 00000000000..112f2e9909f --- /dev/null +++ b/usr.sbin/ifstated/log.h @@ -0,0 +1,46 @@ +/* $OpenBSD: log.h,v 1.1 2017/06/18 12:03:47 benno Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef LOG_H +#define LOG_H + +#include +#include + +void log_init(int, int); +void log_procinit(const char *); +void log_setverbose(int); +int log_getverbose(void); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +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 *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); + +#endif /* LOG_H */ diff --git a/usr.sbin/ifstated/parse.y b/usr.sbin/ifstated/parse.y index 70f0e356d85..254ef740323 100644 --- a/usr.sbin/ifstated/parse.y +++ b/usr.sbin/ifstated/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.40 2017/01/05 13:53:09 krw Exp $ */ +/* $OpenBSD: parse.y,v 1.41 2017/06/18 12:03:47 benno Exp $ */ /* * Copyright (c) 2004 Ryan McBride @@ -41,6 +41,7 @@ #include #include "ifstated.h" +#include "log.h" TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); static struct file {