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 -0000 1.7 +++ usr.sbin/syslogd/Makefile 16 Mar 2017 00:03:30 -0000 @@ -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 -0000 +++ usr.sbin/syslogd/log.c 16 Mar 2017 01:08:11 -0000 @@ -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 <errno.h> +#include <stdio.h> +#include <string.h> +#include <syslog.h> +#include <time.h> + +#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 char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(pri, emsg, ap); + va_end(ap); +} + +void +log_debug(const char *emsg, ...) +{ + char ebuf[ERRBUFSIZE]; + va_list ap; + int saved_errno; + + if (verbose) { + saved_errno = errno; + va_start(ap, emsg); + vsnprintf(ebuf, sizeof(ebuf), emsg, ap); + fprintf(stderr, "%s\n", ebuf); + fflush(stderr); + va_end(ap); + errno = saved_errno; + } +} + +static void +vfatalc(int error, const char *emsg, va_list ap) +{ + char ebuf[ERRBUFSIZE]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(ebuf, sizeof(ebuf), emsg, ap); + sep = ": "; + } else { + ebuf[0] = '\0'; + sep = ""; + } + if (error) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, ebuf, sep, strerror(error)); + else + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, ebuf); +} + +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatalc(errno, emsg, ap); + va_end(ap); + die(0); +} + +void +fatalx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatalc(0, emsg, ap); + va_end(ap); + die(0); +} Index: usr.sbin/syslogd/log.h =================================================================== RCS file: usr.sbin/syslogd/log.h diff -N usr.sbin/syslogd/log.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ usr.sbin/syslogd/log.h 16 Mar 2017 01:08:11 -0000 @@ -0,0 +1,50 @@ +/* $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. + */ + +#ifndef LOG_H +#define LOG_H + +#include <sys/cdefs.h> + +#include <stdarg.h> + +void log_init(int, int); +void log_procinit(const char *); +void log_setdebug(int); +int log_getdebug(void); +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(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +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: usr.sbin/syslogd/syslogd.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.c,v retrieving revision 1.228 diff -u -p -r1.228 syslogd.c --- usr.sbin/syslogd/syslogd.c 14 Mar 2017 15:35:48 -0000 1.228 +++ usr.sbin/syslogd/syslogd.c 16 Mar 2017 01:08:11 -0000 @@ -99,6 +99,7 @@ #define SYSLOG_NAMES #include <sys/syslog.h> +#include "log.h" #include "syslogd.h" #include "evbuffer_tls.h" @@ -314,7 +315,6 @@ void ctlconn_cleanup(void); struct filed *cfline(char *, char *, char *); void cvthname(struct sockaddr *, char *, size_t); int decode(const char *, const CODE *); -void die(int); void markit(void); void fprintlog(struct filed *, int, char *); void init(void); @@ -462,6 +462,9 @@ main(int argc, char *argv[]) if (argc != optind) usage(); + log_init(Debug, LOG_SYSLOG); + log_procinit("syslogd"); + log_setdebug(1); if (Debug) setvbuf(stdout, NULL, _IOLBF, 0); @@ -693,8 +696,6 @@ main(int argc, char *argv[]) logdebug("off & running....\n"); - tzset(); - if (!Debug && !Foreground) { char c; @@ -786,6 +787,7 @@ main(int argc, char *argv[]) init(); + log_setdebug(0); Startup = 0; /* Allocate ctl socket reply buffer if we have a ctl socket */ @@ -1635,6 +1637,18 @@ printsys(char *msg) } } +void +vlogmsg(int pri, const char *proc, const char *fmt, va_list ap) +{ + char msg[ERRBUFSIZE]; + size_t l; + + l = snprintf(msg, sizeof(msg), "%s[%d]: ", proc, getpid()); + if (l < sizeof(msg)); + vsnprintf(msg + l, sizeof(msg) - l, fmt, ap); + logmsg(pri, msg, LocalHostName, ADDDATE); +} + struct timeval now; /* @@ -2251,7 +2265,7 @@ logerror_reason(const char *message, con logmsg(LOG_SYSLOG|LOG_ERR, ebuf, LocalHostName, ADDDATE); } -void +__dead void die(int signo) { struct filed *f; Index: usr.sbin/syslogd/syslogd.h =================================================================== RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.h,v retrieving revision 1.26 diff -u -p -r1.26 syslogd.h --- usr.sbin/syslogd/syslogd.h 17 Oct 2016 11:19:55 -0000 1.26 +++ usr.sbin/syslogd/syslogd.h 16 Mar 2017 01:06:09 -0000 @@ -19,6 +19,7 @@ #include <sys/types.h> #include <sys/socket.h> #include <sys/uio.h> +#include <stdarg.h> /* Privilege separation */ void priv_init(int, int, int, char **); @@ -49,6 +50,8 @@ extern char *path_ctlsock; #define MAXLINE 8192 /* maximum line length */ #define ERRBUFSIZE 256 void logdebug(const char *, ...) __attribute__((__format__ (printf, 1, 2))); +void vlogmsg(int pri, const char *, const char *, va_list); +__dead void die(int); extern int Debug; extern int Startup;