From: bluhm Date: Fri, 28 Apr 2017 14:52:13 +0000 (+0000) Subject: When syslogd(8) failed to open a logfile, the error message could X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9244353d92eebc58bb8dcd775b8bff3e20c6cde0;p=openbsd When syslogd(8) failed to open a logfile, the error message could get lost. Remove log_setdebug() as it adds too much abstraction, use the global variable Started instead. Set the Started value before the init() function. Then errors during config file processing will be logged to the console as Initialize is still 0. This is better than stderr as the latter may be redirected to /dev/null. Print the timestamp and hostname also for direct messages to console, so that they look like all others. bug report jung@; OK benno@ --- diff --git a/usr.sbin/syslogd/log.c b/usr.sbin/syslogd/log.c index 7de0fc7164a..e4ca2ec038b 100644 --- a/usr.sbin/syslogd/log.c +++ b/usr.sbin/syslogd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.3 2017/04/06 14:55:43 bluhm Exp $ */ +/* $OpenBSD: log.c,v 1.4 2017/04/28 14:52:13 bluhm Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -28,7 +28,6 @@ #include "log.h" #include "syslogd.h" -static int debug; static int verbose; static int facility; static const char *log_procname; @@ -40,7 +39,6 @@ log_init(int n_debug, int fac) { extern char *__progname; - debug = n_debug; verbose = n_debug; facility = fac; log_procinit(__progname); @@ -61,18 +59,6 @@ log_procinit(const char *procname) log_procname = procname; } -void -log_setdebug(int d) -{ - debug = d; -} - -int -log_getdebug(void) -{ - return (debug); -} - void log_setverbose(int v) { @@ -98,18 +84,9 @@ logit(int pri, const char *fmt, ...) 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(facility|pri, log_procname, fmt, ap); + vlogmsg(facility|pri, log_procname, fmt, ap); errno = saved_errno; } diff --git a/usr.sbin/syslogd/log.h b/usr.sbin/syslogd/log.h index b3f70fb2073..c7c6c9bbe3e 100644 --- a/usr.sbin/syslogd/log.h +++ b/usr.sbin/syslogd/log.h @@ -1,4 +1,4 @@ -/* $OpenBSD: log.h,v 1.2 2017/04/05 11:31:45 bluhm Exp $ */ +/* $OpenBSD: log.h,v 1.3 2017/04/28 14:52:13 bluhm Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -26,8 +26,6 @@ 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 *, ...) diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 39ba8422464..04cba389ef3 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: syslogd.c,v 1.243 2017/04/25 17:45:50 bluhm Exp $ */ +/* $OpenBSD: syslogd.c,v 1.244 2017/04/28 14:52:13 bluhm Exp $ */ /* * Copyright (c) 1983, 1988, 1993, 1994 @@ -204,6 +204,7 @@ int Debug; /* debug flag */ int Foreground; /* run in foreground, instead of daemonizing */ char LocalHostName[HOST_NAME_MAX+1]; /* our hostname */ char *LocalDomain; /* our local domain name */ +int Started = 0; /* set after privsep */ int Initialized = 0; /* set when we have initialized ourselves */ int MarkInterval = 20 * 60; /* interval between marks in seconds */ @@ -465,7 +466,6 @@ main(int argc, char *argv[]) log_init(Debug, LOG_SYSLOG); log_procinit("syslogd"); - log_setdebug(1); if (Debug) setvbuf(stdout, NULL, _IOLBF, 0); @@ -731,6 +731,8 @@ main(int argc, char *argv[]) if (pledge("stdio unix inet recvfd", NULL) == -1) err(1, "pledge"); + Started = 1; + /* Process is now unprivileged and inside a chroot */ if (Debug) event_set_log_callback(logevent); @@ -791,8 +793,6 @@ main(int argc, char *argv[]) init(); - log_setdebug(0); - /* Allocate ctl socket reply buffer if we have a ctl socket */ if (fd_ctlsock != -1 && (ctl_reply = malloc(CTL_REPLY_MAXSIZE)) == NULL) @@ -1627,6 +1627,10 @@ vlogmsg(int pri, const char *proc, const char *fmt, va_list ap) l = snprintf(msg, sizeof(msg), "%s[%d]: ", proc, getpid()); if (l < sizeof(msg)) vsnprintf(msg + l, sizeof(msg) - l, fmt, ap); + if (!Started) { + fprintf(stderr, "%s\n", msg); + return; + } logline(pri, ADDDATE, LocalHostName, msg); } @@ -1763,6 +1767,10 @@ logline(int pri, int flags, char *from, char *msg) f->f_file = priv_open_tty(ctty); if (f->f_file >= 0) { + strlcpy(f->f_lasttime, timestamp, + sizeof(f->f_lasttime)); + strlcpy(f->f_prevhost, from, + sizeof(f->f_prevhost)); fprintlog(f, flags, msg); (void)close(f->f_file); f->f_file = -1;