summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorSebastian Benoit <benno@cvs.openbsd.org>2017-01-24 04:24:26 +0000
committerSebastian Benoit <benno@cvs.openbsd.org>2017-01-24 04:24:26 +0000
commit38412a6d74198dd2d9af8a80c45ab4c057847286 (patch)
treeb3c816b6753b2a06f89eb29140e3fb8ecf1426a6 /usr.sbin
parent65a5704a2229bec5a709051a5c53c40d037acef2 (diff)
sync log.c from relayd et al to ospfd.
there is still a little difference regarding handling of the verbosity value that will be handled later. ok claudio@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/ospfd/control.c4
-rw-r--r--usr.sbin/ospfd/log.c98
-rw-r--r--usr.sbin/ospfd/log.h42
-rw-r--r--usr.sbin/ospfd/ospfd.c15
-rw-r--r--usr.sbin/ospfd/ospfd.h5
-rw-r--r--usr.sbin/ospfd/ospfe.c10
-rw-r--r--usr.sbin/ospfd/rde.c12
7 files changed, 116 insertions, 70 deletions
diff --git a/usr.sbin/ospfd/control.c b/usr.sbin/ospfd/control.c
index 0cfde8c677a..150c38c9b43 100644
--- a/usr.sbin/ospfd/control.c
+++ b/usr.sbin/ospfd/control.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.c,v 1.43 2017/01/08 23:04:42 krw Exp $ */
+/* $OpenBSD: control.c,v 1.44 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -301,7 +301,7 @@ control_dispatch_imsg(int fd, short event, void *bula)
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
memcpy(&verbose, imsg.data, sizeof(verbose));
- log_verbose(verbose);
+ log_setverbose(verbose);
break;
default:
log_debug("control_dispatch_imsg: "
diff --git a/usr.sbin/ospfd/log.c b/usr.sbin/ospfd/log.c
index 711a62f0b91..085f3ecd126 100644
--- a/usr.sbin/ospfd/log.c
+++ b/usr.sbin/ospfd/log.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: log.c,v 1.10 2017/01/23 08:40:45 benno Exp $ */
+/* $OpenBSD: log.c,v 1.11 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -16,40 +16,54 @@
* 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>
#include "log.h"
-#include "ospfd.h"
-int debug;
-int verbose;
-const char *log_procname;
+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_verbose(int v)
+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, ...)
{
@@ -64,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 */
@@ -77,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_CRIT, "%s", strerror(errno));
+ logit(LOG_CRIT, "%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_CRIT, emsg, ap);
- logit(LOG_CRIT, "%s", strerror(errno));
+ logit(LOG_CRIT, "%s", strerror(saved_errno));
} else {
vlog(LOG_CRIT, nfmt, ap);
free(nfmt);
}
va_end(ap);
}
+
+ errno = saved_errno;
}
void
@@ -135,33 +156,44 @@ log_debug(const char *emsg, ...)
}
}
+static void
+vfatalc(int code, const char *emsg, va_list ap)
+{
+ 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
+ logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
+}
+
void
fatal(const char *emsg, ...)
{
- char s[1024];
- va_list ap;
+ va_list ap;
va_start(ap, emsg);
- vsnprintf(s, sizeof(s), emsg, ap);
+ vfatalc(errno, emsg, ap);
va_end(ap);
-
- if (emsg == NULL)
- logit(LOG_CRIT, "fatal in %s: %s", log_procname,
- strerror(errno));
- else
- if (errno)
- logit(LOG_CRIT, "fatal in %s: %s: %s",
- log_procname, s, strerror(errno));
- else
- logit(LOG_CRIT, "fatal in %s: %s",
- log_procname, s);
-
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/ospfd/log.h b/usr.sbin/ospfd/log.h
index 803a02707f4..8bba62975c1 100644
--- a/usr.sbin/ospfd/log.h
+++ b/usr.sbin/ospfd/log.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: log.h,v 1.9 2017/01/23 08:40:45 benno Exp $ */
+/* $OpenBSD: log.h,v 1.10 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -22,25 +22,25 @@
#include <stdarg.h>
#include <sys/cdefs.h>
-extern const char *log_procname;
-
-void log_init(int);
-void log_verbose(int);
-void logit(int, const char *, ...)
- __attribute__((__format__ (printf, 2, 3)));
-void vlog(int, const char *, va_list)
- __attribute__((__format__ (printf, 2, 0)));
-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 fatal(const char *, ...) __dead
- __attribute__((__format__ (printf, 1, 2)));
-void fatalx(const char *) __dead
- __attribute__((__format__ (printf, 1, 0)));
+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/ospfd/ospfd.c b/usr.sbin/ospfd/ospfd.c
index 3dad49f5dac..6e907d4b328 100644
--- a/usr.sbin/ospfd/ospfd.c
+++ b/usr.sbin/ospfd/ospfd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ospfd.c,v 1.93 2016/09/28 14:39:52 krw Exp $ */
+/* $OpenBSD: ospfd.c,v 1.94 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -25,6 +25,7 @@
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/sysctl.h>
+#include <syslog.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@@ -117,11 +118,11 @@ main(int argc, char *argv[])
conffile = CONF_FILE;
ospfd_process = PROC_MAIN;
- log_procname = log_procnames[ospfd_process];
sockname = OSPFD_SOCKET;
- log_init(1); /* log to stderr until daemonized */
- log_verbose(1);
+ log_init(1, LOG_DAEMON); /* log to stderr until daemonized */
+ log_procinit(log_procnames[ospfd_process]);
+ log_setverbose(1);
while ((ch = getopt(argc, argv, "cdD:f:ns:v")) != -1) {
switch (ch) {
@@ -202,8 +203,8 @@ main(int argc, char *argv[])
if (getpwnam(OSPFD_USER) == NULL)
errx(1, "unknown user %s", OSPFD_USER);
- log_init(debug);
- log_verbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE);
+ log_init(debug, LOG_DAEMON);
+ log_setverbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE);
if (!debug)
daemon(1, 0);
@@ -391,7 +392,7 @@ main_dispatch_ospfe(int fd, short event, void *bula)
case IMSG_CTL_LOG_VERBOSE:
/* already checked by ospfe */
memcpy(&verbose, imsg.data, sizeof(verbose));
- log_verbose(verbose);
+ log_setverbose(verbose);
break;
default:
log_debug("main_dispatch_ospfe: error handling imsg %d",
diff --git a/usr.sbin/ospfd/ospfd.h b/usr.sbin/ospfd/ospfd.h
index 442c7032278..bbf3ce01197 100644
--- a/usr.sbin/ospfd/ospfd.h
+++ b/usr.sbin/ospfd/ospfd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ospfd.h,v 1.96 2017/01/23 00:12:36 jca Exp $ */
+/* $OpenBSD: ospfd.h,v 1.97 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2004 Esben Norby <norby@openbsd.org>
@@ -31,6 +31,7 @@
#include <imsg.h>
#include "ospf.h"
+#include "log.h"
#define CONF_FILE "/etc/ospfd.conf"
#define OSPFD_SOCKET "/var/run/ospfd.sock"
@@ -578,7 +579,7 @@ void kr_reload(void);
u_int8_t mask2prefixlen(in_addr_t);
in_addr_t prefixlen2mask(u_int8_t);
-/* log.h */
+/* logmsg.h */
const char *nbr_state_name(int);
const char *if_state_name(int);
const char *if_type_name(enum iface_type);
diff --git a/usr.sbin/ospfd/ospfe.c b/usr.sbin/ospfd/ospfe.c
index 6d43a5f0bee..86f7e796c40 100644
--- a/usr.sbin/ospfd/ospfe.c
+++ b/usr.sbin/ospfd/ospfe.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ospfe.c,v 1.98 2016/12/21 10:30:11 jca Exp $ */
+/* $OpenBSD: ospfe.c,v 1.99 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -122,8 +122,14 @@ ospfe(struct ospfd_conf *xconf, int pipe_parent2ospfe[2], int pipe_ospfe2rde[2],
fatal("chdir(\"/\")");
setproctitle("ospf engine");
+ /*
+ * XXX needed with fork+exec
+ * log_init(debug, LOG_DAEMON);
+ * log_setverbose(verbose);
+ */
+
ospfd_process = PROC_OSPF_ENGINE;
- log_procname = log_procnames[ospfd_process];
+ log_procinit(log_procnames[ospfd_process]);
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
diff --git a/usr.sbin/ospfd/rde.c b/usr.sbin/ospfd/rde.c
index b024fcb4b6c..941a25d67f2 100644
--- a/usr.sbin/ospfd/rde.c
+++ b/usr.sbin/ospfd/rde.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rde.c,v 1.107 2017/01/23 00:12:36 jca Exp $ */
+/* $OpenBSD: rde.c,v 1.108 2017/01/24 04:24:25 benno Exp $ */
/*
* Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
@@ -125,8 +125,14 @@ rde(struct ospfd_conf *xconf, int pipe_parent2rde[2], int pipe_ospfe2rde[2],
fatal("chdir(\"/\")");
setproctitle("route decision engine");
+ /*
+ * XXX needed with fork+exec
+ * log_init(debug, LOG_DAEMON);
+ * log_setverbose(verbose);
+ */
+
ospfd_process = PROC_RDE_ENGINE;
- log_procname = log_procnames[ospfd_process];
+ log_procinit(log_procnames[ospfd_process]);
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
@@ -603,7 +609,7 @@ rde_dispatch_imsg(int fd, short event, void *bula)
case IMSG_CTL_LOG_VERBOSE:
/* already checked by ospfe */
memcpy(&verbose, imsg.data, sizeof(verbose));
- log_verbose(verbose);
+ log_setverbose(verbose);
break;
default:
log_debug("rde_dispatch_imsg: unexpected imsg %d",