Hi,
I want to replace the home grown syslogd(8) internal debug and
logging functions with a more common log.c implementation. But of
course I cannot use openlog(3), so I need something special. I
have copied log.[ch] form ospfd(8) and adapted it to syslogd's
needs. As the messages are limited to ERRBUFSIZE anyway, I can
avoid malloc(3) in the error logging code.
The whole diff converting all the messages has more than 2000 lines
as it touches every part of syslogd code. I would refuse to review
such a huge diff, so I have splitted it. Let's start with the log.c
implementation.
ok?
bluhm
Index: usr.sbin/syslogd/Makefile
===
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/Makefile,v
retrieving revision 1.7
diff -u -p -r1.7 Makefile
--- usr.sbin/syslogd/Makefile 18 Jan 2015 19:37:59 - 1.7
+++ usr.sbin/syslogd/Makefile 16 Mar 2017 00:03:30 -
@@ -1,7 +1,8 @@
# $OpenBSD: Makefile,v 1.7 2015/01/18 19:37:59 bluhm Exp $
PROG= syslogd
-SRCS= syslogd.c ttymsg.c privsep.c privsep_fdpass.c ringbuf.c evbuffer_tls.c
+SRCS= evbuffer_tls.c log.c privsep.c privsep_fdpass.c ringbuf.c syslogd.c \
+ ttymsg.c
MAN= syslogd.8 syslog.conf.5
LDADD= -levent -ltls -lssl -lcrypto
DPADD= ${LIBEVENT} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO}
Index: usr.sbin/syslogd/log.c
===
RCS file: usr.sbin/syslogd/log.c
diff -N usr.sbin/syslogd/log.c
--- /dev/null 1 Jan 1970 00:00:00 -
+++ usr.sbin/syslogd/log.c 16 Mar 2017 01:08:11 -
@@ -0,0 +1,208 @@
+/* $OpenBSD$ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henn...@openbsd.org>
+ * Copyright (c) 2017 Alexander Bluhm <bl...@openbsd.org>
+ *
+ * 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.
+ */
+
+#include
+#include
+#include
+#include
+#include
+
+#include "log.h"
+#include "syslogd.h"
+
+static int debug;
+static int verbose;
+static int facility;
+static const char *log_procname;
+
+void
+log_init(int n_debug, int fac)
+{
+ extern char *__progname;
+
+ debug = n_debug;
+ verbose = n_debug;
+ facility = fac;
+ log_procinit(__progname);
+
+ tzset();
+}
+
+void
+log_procinit(const char *procname)
+{
+ if (procname != NULL)
+ log_procname = procname;
+}
+
+void
+log_setdebug(int d)
+{
+ debug = d;
+}
+
+int
+log_getdebug(void)
+{
+ return (debug);
+}
+
+void
+log_setverbose(int v)
+{
+ verbose = v;
+}
+
+int
+log_getverbose(void)
+{
+ return (verbose);
+}
+
+void
+logit(int pri, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vlog(pri, fmt, ap);
+ va_end(ap);
+}
+
+void
+vlog(int pri, const char *fmt, va_list ap)
+{
+ char ebuf[ERRBUFSIZE];
+ size_t l;
+ int saved_errno = errno;
+
+ if (debug) {
+ l = snprintf(ebuf, sizeof(ebuf), "%s: ", log_procname);
+ if (l < sizeof(ebuf))
+ vsnprintf(ebuf+l, sizeof(ebuf)-l, fmt, ap);
+ fprintf(stderr, "%s\n", ebuf);
+ fflush(stderr);
+ } else
+ vlogmsg(pri, log_procname, fmt, ap);
+
+ errno = saved_errno;
+}
+
+void
+log_warn(const char *emsg, ...)
+{
+ char ebuf[ERRBUFSIZE];
+ size_t l;
+ 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(saved_errno));
+ else {
+ va_start(ap, emsg);
+ l = vsnprintf(ebuf, sizeof(ebuf), emsg, ap);
+ if (l < sizeof(ebuf))
+ snprintf(ebuf+l, sizeof(ebuf)-l, ": %s",
+ strerror(saved_errno));
+ logit(LOG_ERR, "%s", ebuf);
+ va_end(ap);
+ }
+ errno = saved_errno;
+}
+
+void
+log_warnx(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vlog(LOG_ERR, emsg, ap);
+ va_end(ap);
+}
+
+void
+log_info(int pri, const