ok
Alexander Bluhm([email protected]) on 2017.04.28 02:17:23 +0200:
> Hi,
>
> jung@ reported that syslogd's error messages are lost if it cannot
> open a logfile. Problem is there is interfering logic during startup
> whether to log to stderr or /dev/console.
>
> My log_setdebug() feature adds too much abstraction, use the global
> variable Started instead. Then all decisions can be done in syslog.c
>
> Set the Started value before the init() function. This means that
> errors during config read will be logged to the console as Initialize
> is still 0. This is better than stderr as this may be redirected
> to /dev/null.
>
> Print the timestamp and hostname also for locally generated messages
> to console, so that they look like all others.
>
> ok?
>
> bluhm
>
> Index: usr.sbin/syslogd/log.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/log.c,v
> retrieving revision 1.3
> diff -u -p -r1.3 log.c
> --- usr.sbin/syslogd/log.c 6 Apr 2017 14:55:43 -0000 1.3
> +++ usr.sbin/syslogd/log.c 27 Apr 2017 13:38:54 -0000
> @@ -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);
> @@ -62,18 +60,6 @@ log_procinit(const char *procname)
> }
>
> void
> -log_setdebug(int d)
> -{
> - debug = d;
> -}
> -
> -int
> -log_getdebug(void)
> -{
> - return (debug);
> -}
> -
> -void
> log_setverbose(int v)
> {
> verbose = 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;
> }
> Index: usr.sbin/syslogd/log.h
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/log.h,v
> retrieving revision 1.2
> diff -u -p -r1.2 log.h
> --- usr.sbin/syslogd/log.h 5 Apr 2017 11:31:45 -0000 1.2
> +++ usr.sbin/syslogd/log.h 27 Apr 2017 13:37:52 -0000
> @@ -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 *, ...)
> Index: usr.sbin/syslogd/syslogd.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.c,v
> retrieving revision 1.243
> diff -u -p -r1.243 syslogd.c
> --- usr.sbin/syslogd/syslogd.c 25 Apr 2017 17:45:50 -0000 1.243
> +++ usr.sbin/syslogd/syslogd.c 27 Apr 2017 21:55:10 -0000
> @@ -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
> 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,
> 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;
>