diff --git a/src/usr.sbin/ntpd/log.c b/src/usr.sbin/ntpd/log.c index 0db0245c..12b28718 100644 --- a/src/usr.sbin/ntpd/log.c +++ b/src/usr.sbin/ntpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.9 2014/11/03 20:15:30 bluhm Exp $ */ +/* $OpenBSD: log.c,v 1.10 2015/01/08 00:30:08 bcook Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -16,7 +16,11 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include + #include +#include +#include #include #include #include @@ -24,24 +28,36 @@ #include #include -#include "ntpd.h" +#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; -extern int debugsyslog; void -log_init(int n_debug) +log_init(int n_foreground) { extern char *__progname; - debug = n_debug; - - if (!debug) - openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + foreground = n_foreground; + if (! foreground) + openlog(__progname, LOG_PID | LOG_NDELAY, LOG_MAIL); tzset(); } +void +log_verbose(int v) +{ + verbose = v; +} + void logit(int pri, const char *fmt, ...) { @@ -57,7 +73,7 @@ vlog(int pri, const char *fmt, va_list ap) { char *nfmt; - if (debug) { + if (foreground) { /* best effort in out of mem situations */ if (asprintf(&nfmt, "%s\n", fmt) == -1) { vfprintf(stderr, fmt, ap); @@ -121,7 +137,7 @@ log_debug(const char *emsg, ...) { va_list ap; - if (debug || debugsyslog) { + if (verbose & TRACE_DEBUG) { va_start(ap, emsg); vlog(LOG_DEBUG, emsg, ap); va_end(ap); @@ -129,25 +145,58 @@ log_debug(const char *emsg, ...) } void -fatal(const char *emsg) +log_trace(int mask, const char *emsg, ...) +{ + va_list ap; + + if (verbose & mask) { + va_start(ap, emsg); + vlog(LOG_DEBUG, emsg, ap); + va_end(ap); + } +} + +static void +fatal_arg(const char *emsg, va_list ap) { +#define FATALBUFSIZE 1024 + static char ebuffer[FATALBUFSIZE]; + if (emsg == NULL) - logit(LOG_CRIT, "fatal: %s", strerror(errno)); - else - if (errno) - logit(LOG_CRIT, "fatal: %s: %s", - emsg, strerror(errno)); + (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 - logit(LOG_CRIT, "fatal: %s", emsg); + (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); + } + logit(LOG_CRIT, "fatal: %s", ebuffer); +} +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + fatal_arg(emsg, ap); + va_end(ap); exit(1); } void -fatalx(const char *emsg) +fatalx(const char *emsg, ...) { + va_list ap; + errno = 0; - fatal(emsg); + va_start(ap, emsg); + fatal_arg(emsg, ap); + va_end(ap); + exit(1); } const char * diff --git a/src/usr.sbin/ntpd/log.h b/src/usr.sbin/ntpd/log.h new file mode 100644 index 00000000..a5854622 --- /dev/null +++ b/src/usr.sbin/ntpd/log.h @@ -0,0 +1,35 @@ +/* $OpenBSD: log.h,v 1.1 2015/01/08 00:30:08 bcook Exp $ */ + +/* + * Copyright (c) 2010 Gilles Chehade + * + * 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. + */ + +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))); +const char * log_sockaddr(struct sockaddr *sa); diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 519b0c63..4cb918bc 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.81 2015/01/04 01:24:43 bcook Exp $ */ +/* $OpenBSD: ntpd.c,v 1.82 2015/01/08 00:30:08 bcook Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -58,7 +58,6 @@ volatile sig_atomic_t quit = 0; volatile sig_atomic_t reconfig = 0; volatile sig_atomic_t sigchld = 0; struct imsgbuf *ibuf; -int debugsyslog = 0; int timeout = INFTIM; const char *showopt; @@ -127,6 +126,7 @@ main(int argc, char *argv[]) switch (ch) { case 'd': lconf.debug = 1; + log_verbose(1); break; case 'f': conffile = optarg; @@ -141,7 +141,7 @@ main(int argc, char *argv[]) lconf.settime = 0; break; case 'v': - debugsyslog = 1; + log_verbose(1); break; default: usage(); diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 5db89282..20dfedb5 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.113 2015/01/04 01:48:49 bcook Exp $ */ +/* $OpenBSD: ntpd.h,v 1.114 2015/01/08 00:30:08 bcook Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -30,6 +30,7 @@ #include #include +#include "log.h" #include "ntp.h" #include @@ -261,17 +262,6 @@ enum ctl_actions { }; /* prototypes */ -/* log.c */ -void log_init(int); -void logit(int, const char *, ...); -void vlog(int, const char *, va_list); -void log_warn(const char *, ...); -void log_warnx(const char *, ...); -void log_info(const char *, ...); -void log_debug(const char *, ...); -void fatal(const char *); -void fatalx(const char *); -const char *log_sockaddr(struct sockaddr *); /* ntp.c */ pid_t ntp_main(int[2], int, struct ntpd_conf *, struct passwd *); diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 9608c3f6..2d2a5095 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.55 2014/11/20 05:51:20 jsg Exp $ */ +/* $OpenBSD: parse.y,v 1.56 2015/01/08 00:30:08 bcook Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -348,7 +348,7 @@ yyerror(const char *fmt, ...) if (vasprintf(&msg, fmt, ap) == -1) fatalx("yyerror vasprintf"); va_end(ap); - logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg); + log_warnx("%s:%d: %s", file->name, yylval.lineno, msg); free(msg); return (0); }