Browse Source

Switch and sync to the log.c variant from httpd/relayd/iked/snmpd/vmd.

OK bcook@ jung@
OPENBSD_5_9
reyk 9 years ago
parent
commit
97252e04ad
6 changed files with 103 additions and 87 deletions
  1. +3
    -2
      src/usr.sbin/ntpd/constraint.c
  2. +62
    -58
      src/usr.sbin/ntpd/log.c
  3. +20
    -17
      src/usr.sbin/ntpd/log.h
  4. +5
    -2
      src/usr.sbin/ntpd/ntp.c
  5. +6
    -3
      src/usr.sbin/ntpd/ntp_dns.c
  6. +7
    -5
      src/usr.sbin/ntpd/ntpd.c

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

@ -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 <reyk@openbsd.org>
@ -42,7 +42,6 @@
#include <tls.h>
#include <pwd.h>
#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");


+ 62
- 58
src/usr.sbin/ntpd/log.c View File

@ -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 <henning@openbsd.org>
@ -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 <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/socket.h>
#include <errno.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <time.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;
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);
}

+ 20
- 17
src/usr.sbin/ntpd/log.h View File

@ -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 <gilles@poolp.org>
@ -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)));

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

@ -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 <henning@openbsd.org>
@ -28,6 +28,7 @@
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <err.h>
@ -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");


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

@ -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 <henning@openbsd.org>
@ -27,6 +27,7 @@
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#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) ||


+ 7
- 5
src/usr.sbin/ntpd/ntpd.c View File

@ -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 <henning@openbsd.org>
@ -30,6 +30,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
@ -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 */


Loading…
Cancel
Save