Browse Source

Allow logging to both stderr and syslog; don't reset the log level if

the log destination changes. ok claudio@ benno@
OPENBSD_6_6
otto 4 years ago
parent
commit
c47029baaf
6 changed files with 82 additions and 69 deletions
  1. +10
    -29
      src/usr.sbin/ntpd/log.c
  2. +49
    -0
      src/usr.sbin/ntpd/log.h
  3. +3
    -3
      src/usr.sbin/ntpd/ntp.c
  4. +3
    -3
      src/usr.sbin/ntpd/ntp_dns.c
  5. +15
    -12
      src/usr.sbin/ntpd/ntpd.c
  6. +2
    -22
      src/usr.sbin/ntpd/ntpd.h

+ 10
- 29
src/usr.sbin/ntpd/log.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: log.c,v 1.17 2017/03/21 12:06:56 bluhm Exp $ */
/* $OpenBSD: log.c,v 1.18 2019/06/27 15:18:42 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -23,42 +23,22 @@
#include <syslog.h>
#include <errno.h>
#include <time.h>
#include "log.h"
static int debug;
static int dest;
static int verbose;
const char *log_procname;
void log_init(int, int);
void log_procinit(const char *);
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(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_debug, int facility)
log_init(int n_dest, int n_verbose, int facility)
{
extern char *__progname;
debug = n_debug;
verbose = n_debug;
dest = n_dest;
verbose = n_verbose;
log_procinit(__progname);
if (!debug)
if (dest & LOG_TO_SYSLOG)
openlog(__progname, LOG_PID | LOG_NDELAY, facility);
tzset();
@ -99,7 +79,7 @@ vlog(int pri, const char *fmt, va_list ap)
char *nfmt;
int saved_errno = errno;
if (debug) {
if (dest & LOG_TO_STDERR) {
/* best effort in out of mem situations */
if (asprintf(&nfmt, "%s\n", fmt) == -1) {
vfprintf(stderr, fmt, ap);
@ -109,7 +89,8 @@ vlog(int pri, const char *fmt, va_list ap)
free(nfmt);
}
fflush(stderr);
} else
}
if (dest & LOG_TO_SYSLOG)
vsyslog(pri, fmt, ap);
errno = saved_errno;


+ 49
- 0
src/usr.sbin/ntpd/log.h View File

@ -0,0 +1,49 @@
/* $OpenBSD: log.h,v 1.5 2019/06/27 15:18:42 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@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 <stdarg.h>
#include <sys/cdefs.h>
#define LOG_TO_STDERR (1<<0)
#define LOG_TO_SYSLOG (1<<1)
void log_init(int, int, int);
void log_procinit(const char *);
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(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)));
#endif /* LOG_H */

+ 3
- 3
src/usr.sbin/ntpd/ntp.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp.c,v 1.156 2019/06/20 07:28:18 otto Exp $ */
/* $OpenBSD: ntp.c,v 1.157 2019/06/27 15:18:42 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -95,8 +95,8 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
start_child(NTPDNS_PROC_NAME, pipe_dns[1], argc, argv);
log_init(nconf->debug, LOG_DAEMON);
log_setverbose(nconf->verbose);
log_init(nconf->debug ? LOG_TO_STDERR : LOG_TO_SYSLOG, nconf->verbose,
LOG_DAEMON);
if (!nconf->debug && setsid() == -1)
fatal("setsid");
log_procinit("ntp");


+ 3
- 3
src/usr.sbin/ntpd/ntp_dns.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp_dns.c,v 1.23 2019/06/20 07:28:18 otto Exp $ */
/* $OpenBSD: ntp_dns.c,v 1.24 2019/06/27 15:18:42 otto Exp $ */
/*
* Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org>
@ -67,8 +67,8 @@ ntp_dns(struct ntpd_conf *nconf, struct passwd *pw)
if (setpriority(PRIO_PROCESS, 0, 0) == -1)
log_warn("could not set priority");
log_init(nconf->debug, LOG_DAEMON);
log_setverbose(nconf->verbose);
log_init(nconf->debug ? LOG_TO_STDERR : LOG_TO_SYSLOG, nconf->verbose,
LOG_DAEMON);
if (!nconf->debug && setsid() == -1)
fatal("setsid");
log_procinit("dns");


+ 15
- 12
src/usr.sbin/ntpd/ntpd.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.c,v 1.122 2019/06/12 05:04:45 otto Exp $ */
/* $OpenBSD: ntpd.c,v 1.123 2019/06/27 15:18:42 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -135,7 +135,7 @@ main(int argc, char *argv[])
struct constraint *cstr;
struct passwd *pw;
void *newp;
int argc0 = argc;
int argc0 = argc, logdest;
char **argv0 = argv;
char *pname = NULL;
@ -151,13 +151,13 @@ main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "df:nP:sSv")) != -1) {
switch (ch) {
case 'd':
lconf.debug = 2;
lconf.debug = 1;
break;
case 'f':
conffile = optarg;
break;
case 'n':
lconf.debug = 2;
lconf.debug = 1;
lconf.noaction = 1;
break;
case 'P':
@ -179,8 +179,11 @@ main(int argc, char *argv[])
}
/* log to stderr until daemonized */
log_init(1, LOG_DAEMON);
log_setverbose(lconf.verbose);
logdest = LOG_TO_STDERR;
if (!lconf.debug)
logdest |= LOG_TO_SYSLOG;
log_init(logdest, lconf.verbose, LOG_DAEMON);
argc -= optind;
argv += optind;
@ -230,9 +233,10 @@ main(int argc, char *argv[])
if (setpriority(PRIO_PROCESS, 0, -20) == -1)
warn("can't set priority");
reset_adjtime();
logdest = lconf.debug ? LOG_TO_STDERR : LOG_TO_SYSLOG;
if (!lconf.settime) {
log_init(lconf.debug, LOG_DAEMON);
log_setverbose(lconf.verbose);
log_init(logdest, lconf.verbose, LOG_DAEMON);
if (!lconf.debug)
if (daemon(1, 0))
fatal("daemon");
@ -313,8 +317,7 @@ main(int argc, char *argv[])
if (nfds == 0 && lconf.settime) {
lconf.settime = 0;
timeout = INFTIM;
log_init(lconf.debug, LOG_DAEMON);
log_setverbose(lconf.verbose);
log_init(logdest, lconf.verbose, LOG_DAEMON);
log_warnx("no reply received in time, skipping initial "
"time setting");
if (!lconf.debug)
@ -413,8 +416,8 @@ dispatch_imsg(struct ntpd_conf *lconf, int argc, char **argv)
fatalx("invalid IMSG_SETTIME received");
if (!lconf->settime)
break;
log_init(lconf->debug, LOG_DAEMON);
log_setverbose(lconf->verbose);
log_init(lconf->debug ? LOG_TO_STDERR : LOG_TO_SYSLOG,
lconf->verbose, LOG_DAEMON);
memcpy(&d, imsg.data, sizeof(d));
ntpd_settime(d);
/* daemonize now */


+ 2
- 22
src/usr.sbin/ntpd/ntpd.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.h,v 1.144 2019/06/20 07:28:18 otto Exp $ */
/* $OpenBSD: ntpd.h,v 1.145 2019/06/27 15:18:42 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -32,6 +32,7 @@
#include <imsg.h>
#include "ntp.h"
#include "log.h"
#define MAXIMUM(a, b) ((a) > (b) ? (a) : (b))
@ -423,24 +424,3 @@ void build_show_peer(struct ctl_show_peer *,
void build_show_sensor(struct ctl_show_sensor *,
struct ntp_sensor *);
/* log.c */
void log_init(int, int);
void log_procinit(const char *);
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(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)));

Loading…
Cancel
Save