From 97252e04ad5c0a4a781ebee4f9da0bee65e5626e Mon Sep 17 00:00:00 2001 From: reyk <> Date: Sat, 19 Dec 2015 17:55:29 +0000 Subject: [PATCH] Switch and sync to the log.c variant from httpd/relayd/iked/snmpd/vmd. OK bcook@ jung@ --- src/usr.sbin/ntpd/constraint.c | 5 +- src/usr.sbin/ntpd/log.c | 120 +++++++++++++++++---------------- src/usr.sbin/ntpd/log.h | 37 +++++----- src/usr.sbin/ntpd/ntp.c | 7 +- src/usr.sbin/ntpd/ntp_dns.c | 9 ++- src/usr.sbin/ntpd/ntpd.c | 12 ++-- 6 files changed, 103 insertions(+), 87 deletions(-) diff --git a/src/usr.sbin/ntpd/constraint.c b/src/usr.sbin/ntpd/constraint.c index dd81153b..91d6ceb5 100644 --- a/src/usr.sbin/ntpd/constraint.c +++ b/src/usr.sbin/ntpd/constraint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: constraint.c,v 1.23 2015/12/05 13:12:16 claudio Exp $ */ +/* $OpenBSD: constraint.c,v 1.24 2015/12/19 17:55:29 reyk Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -42,7 +42,6 @@ #include #include -#include "log.h" #include "ntpd.h" int constraint_addr_init(struct constraint *); @@ -285,6 +284,8 @@ priv_constraint_child(struct constraint *cstr, struct ntp_addr_msg *am, struct iovec iov[2]; int i; + log_procinit("constraint"); + if (setpriority(PRIO_PROCESS, 0, 0) == -1) log_warn("could not set priority"); diff --git a/src/usr.sbin/ntpd/log.c b/src/usr.sbin/ntpd/log.c index 9108f25d..5b35b33e 100644 --- a/src/usr.sbin/ntpd/log.c +++ b/src/usr.sbin/ntpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.12 2015/12/19 13:58:08 reyk Exp $ */ +/* $OpenBSD: log.c,v 1.13 2015/12/19 17:55:29 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -11,49 +11,65 @@ * 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 MIND, 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. + * 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 -#include #include #include +#include #include #include +#include #include -#include "log.h" - -#define TRACE_DEBUG 0x1 - -static int foreground; -static int verbose; - -void vlog(int, const char *, va_list); -void logit(int, const char *, ...) - __attribute__((format (printf, 2, 3))); - +int debug; +int verbose; +const char *log_procname; + +void log_init(int, int); +void log_procinit(const char *); +void log_verbose(int); +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))); void -log_init(int n_foreground) +log_init(int n_debug, int facility) { extern char *__progname; - foreground = n_foreground; - if (! foreground) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + debug = n_debug; + verbose = n_debug; + log_procinit(__progname); + + if (!debug) + openlog(__progname, LOG_PID | LOG_NDELAY, facility); tzset(); } +void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + void log_verbose(int v) { @@ -75,7 +91,7 @@ vlog(int pri, const char *fmt, va_list ap) { char *nfmt; - if (foreground) { + if (debug) { /* best effort in out of mem situations */ if (asprintf(&nfmt, "%s\n", fmt) == -1) { vfprintf(stderr, fmt, ap); @@ -139,19 +155,7 @@ log_debug(const char *emsg, ...) { va_list ap; - if (verbose & TRACE_DEBUG) { - va_start(ap, emsg); - vlog(LOG_DEBUG, emsg, ap); - va_end(ap); - } -} - -void -log_trace(int mask, const char *emsg, ...) -{ - va_list ap; - - if (verbose & mask) { + if (verbose > 1) { va_start(ap, emsg); vlog(LOG_DEBUG, emsg, ap); va_end(ap); @@ -159,23 +163,23 @@ log_trace(int mask, const char *emsg, ...) } static void -fatal_arg(const char *emsg, va_list ap) +vfatal(const char *emsg, va_list ap) { -#define FATALBUFSIZE 1024 - static char ebuffer[FATALBUFSIZE]; - - if (emsg == NULL) - (void)strlcpy(ebuffer, strerror(errno), sizeof ebuffer); - else { - if (errno) { - (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); - (void)strlcat(ebuffer, ": ", sizeof ebuffer); - (void)strlcat(ebuffer, strerror(errno), sizeof ebuffer); - } - else - (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; } - logit(LOG_CRIT, "fatal: %s", ebuffer); + if (errno) + logit(LOG_CRIT, "%s: %s%s%s", + log_procname, s, sep, strerror(errno)); + else + logit(LOG_CRIT, "%s%s%s", log_procname, sep, s); } void @@ -184,7 +188,7 @@ fatal(const char *emsg, ...) va_list ap; va_start(ap, emsg); - fatal_arg(emsg, ap); + vfatal(emsg, ap); va_end(ap); exit(1); } @@ -196,7 +200,7 @@ fatalx(const char *emsg, ...) errno = 0; va_start(ap, emsg); - fatal_arg(emsg, ap); + vfatal(emsg, ap); va_end(ap); exit(1); } diff --git a/src/usr.sbin/ntpd/log.h b/src/usr.sbin/ntpd/log.h index 2e735b41..4afaf164 100644 --- a/src/usr.sbin/ntpd/log.h +++ b/src/usr.sbin/ntpd/log.h @@ -1,4 +1,4 @@ -/* $OpenBSD: log.h,v 1.2 2015/12/19 13:58:08 reyk Exp $ */ +/* $OpenBSD: log.h,v 1.3 2015/12/19 17:55:29 reyk Exp $ */ /* * Copyright (c) 2010 Gilles Chehade @@ -16,19 +16,22 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -void log_init(int); -void log_verbose(int); -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 log_trace(int, const char *, ...) - __attribute__((format (printf, 2, 3))); -__dead void fatal(const char *, ...) - __attribute__((format (printf, 1, 2))); -__dead void fatalx(const char *, ...) - __attribute__((format (printf, 1, 2))); +void log_init(int, int); +void log_procinit(const char *); +void log_verbose(int); +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))); diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index cde91548..bf79920a 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.140 2015/12/05 13:12:16 claudio Exp $ */ +/* $OpenBSD: ntp.c,v 1.141 2015/12/19 17:55:29 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -101,10 +102,12 @@ ntp_main(int pipe_prnt[2], int fd_ctl, struct ntpd_conf *nconf, /* in this case the parent didn't init logging and didn't daemonize */ if (nconf->settime && !nconf->debug) { - log_init(nconf->debug); + log_init(nconf->debug, LOG_DAEMON); if (setsid() == -1) fatal("setsid"); } + log_procinit("ntp"); + if ((se = getservbyname("ntp", "udp")) == NULL) fatal("getservbyname"); diff --git a/src/usr.sbin/ntpd/ntp_dns.c b/src/usr.sbin/ntpd/ntp_dns.c index 538ff360..aa47096b 100644 --- a/src/usr.sbin/ntpd/ntp_dns.c +++ b/src/usr.sbin/ntpd/ntp_dns.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp_dns.c,v 1.15 2015/12/05 13:12:16 claudio Exp $ */ +/* $OpenBSD: ntp_dns.c,v 1.16 2015/12/19 17:55:29 reyk Exp $ */ /* * Copyright (c) 2003-2008 Henning Brauer @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "ntpd.h" @@ -66,14 +67,15 @@ ntp_dns(int pipe_ntp[2], struct ntpd_conf *nconf, struct passwd *pw) } if (setpriority(PRIO_PROCESS, 0, 0) == -1) - warn("could not set priority"); + log_warn("could not set priority"); /* in this case the parent didn't init logging and didn't daemonize */ if (nconf->settime && !nconf->debug) { - log_init(nconf->debug); + log_init(nconf->debug, LOG_DAEMON); if (setsid() == -1) fatal("setsid"); } + log_procinit("dns"); if ((nullfd = open("/dev/null", O_RDWR, 0)) == -1) fatal(NULL); @@ -86,6 +88,7 @@ ntp_dns(int pipe_ntp[2], struct ntpd_conf *nconf, struct passwd *pw) close(nullfd); setproctitle("dns engine"); + close(pipe_ntp[0]); if (setgroups(1, &pw->pw_gid) || diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 99e0acfd..cf88fe87 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.100 2015/12/05 13:12:16 claudio Exp $ */ +/* $OpenBSD: ntpd.c,v 1.101 2015/12/19 17:55:29 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -131,7 +132,7 @@ main(int argc, char *argv[]) memset(&lconf, 0, sizeof(lconf)); - log_init(1); /* log to stderr until daemonized */ + log_init(1, LOG_DAEMON); /* log to stderr until daemonized */ while ((ch = getopt(argc, argv, "df:nsSv")) != -1) { switch (ch) { @@ -188,7 +189,7 @@ main(int argc, char *argv[]) reset_adjtime(); if (!lconf.settime) { - log_init(lconf.debug); + log_init(lconf.debug, LOG_DAEMON); if (!lconf.debug) if (daemon(1, 0)) fatal("daemon"); @@ -208,6 +209,7 @@ main(int argc, char *argv[]) chld_pid = ntp_main(pipe_chld, fd_ctl, &lconf, pw); setproctitle("[priv]"); + log_procinit("[priv]"); readfreq(); signal(SIGTERM, sighdlr); @@ -266,7 +268,7 @@ main(int argc, char *argv[]) if (nfds == 0 && lconf.settime) { lconf.settime = 0; timeout = INFTIM; - log_init(lconf.debug); + log_init(lconf.debug, LOG_DAEMON); log_warnx("no reply received in time, skipping initial " "time setting"); if (!lconf.debug) @@ -392,7 +394,7 @@ dispatch_imsg(struct ntpd_conf *lconf, const char *pw_dir, fatalx("invalid IMSG_SETTIME received"); if (!lconf->settime) break; - log_init(lconf->debug); + log_init(lconf->debug, LOG_DAEMON); memcpy(&d, imsg.data, sizeof(d)); ntpd_settime(d); /* daemonize now */