On Tue, Jun 13, 2017 at 08:44:46AM +0200, Sebastian Benoit wrote:
> Rob Pierce([email protected]) on 2017.06.11 18:04:31 -0400:
> > This minimizes differences with the latest log.c.
> >
> > I was not sure how to handle verbosity, as the current implementation is
> > verbose by default in debug mode. The diff below requires actually
> > requesting (double) verbosity on the command line in order to retain the
> > same behaviour (in debug mode).
>
> Thanks.
>
> can you redo this with the log.c from bgpd/ospfd/... ?
> The difference is exactly in the handling of verbose, and that way we dont
> change the -v semantics right now.
>
> I havent unified the verbose handling yet, but if you diff them you'll see
> its only a small difference now. My plan is to narrow all the log.c users
> down to this small difference and then fix that in all of them in one go.
>
> Also please add a log.h with the declarations just like in bgpd/ospfd/...
>
> /Benno
Ah yes, thank you for the pointer. How is this?
log.[ch] are now identical to bgpd and ospfd.
Rob
cvs server: Diffing .
Index: ifstated.c
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/ifstated.c,v
retrieving revision 1.41
diff -u -p -r1.41 ifstated.c
--- ifstated.c 30 May 2013 19:22:48 -0000 1.41
+++ ifstated.c 13 Jun 2017 23:09:05 -0000
@@ -36,12 +36,14 @@
#include <stdlib.h>
#include <string.h>
#include <signal.h>
+#include <syslog.h>
#include <err.h>
#include <event.h>
#include <unistd.h>
#include <ifaddrs.h>
#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, voi
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;
}
}
Index: ifstated.h
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/ifstated.h,v
retrieving revision 1.10
diff -u -p -r1.10 ifstated.h
--- ifstated.h 19 Jul 2016 08:04:53 -0000 1.10
+++ ifstated.h 13 Jun 2017 23:09:05 -0000
@@ -135,20 +135,3 @@ enum { IFSD_EVTIMER_ADD, IFSD_EVTIMER_DE
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;
Index: log.c
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/log.c,v
retrieving revision 1.4
diff -u -p -r1.4 log.c
--- log.c 21 Mar 2017 12:06:55 -0000 1.4
+++ log.c 13 Jun 2017 23:09:05 -0000
@@ -16,40 +16,55 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <errno.h>
-#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <syslog.h>
+#include <errno.h>
#include <time.h>
-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, ...)
{
va_list ap;
@@ -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 a
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);
}
Index: log.h
===================================================================
RCS file: log.h
diff -N log.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ log.h 13 Jun 2017 23:09:05 -0000
@@ -0,0 +1,46 @@
+/* $OpenBSD$ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <[email protected]>
+ *
+ * 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 <stdarg.h>
+#include <sys/cdefs.h>
+
+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 */
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/parse.y,v
retrieving revision 1.40
diff -u -p -r1.40 parse.y
--- parse.y 5 Jan 2017 13:53:09 -0000 1.40
+++ parse.y 13 Jun 2017 23:09:05 -0000
@@ -41,6 +41,7 @@
#include <event.h>
#include "ifstated.h"
+#include "log.h"
TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
static struct file {