Browse Source

sync log.c from smtpd.

Reduces the number of log.c snowflakes by a little, and gives ntpd a
variadic fatal() function to be used later.
ok deraadt@
OPENBSD_5_7
bcook 9 years ago
parent
commit
e69bd15d31
5 changed files with 111 additions and 37 deletions
  1. +69
    -20
      src/usr.sbin/ntpd/log.c
  2. +35
    -0
      src/usr.sbin/ntpd/log.h
  3. +3
    -3
      src/usr.sbin/ntpd/ntpd.c
  4. +2
    -12
      src/usr.sbin/ntpd/ntpd.h
  5. +2
    -2
      src/usr.sbin/ntpd/parse.y

+ 69
- 20
src/usr.sbin/ntpd/log.c View File

@ -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 <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -16,7 +16,11 @@
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <sys/socket.h>
#include <errno.h> #include <errno.h>
#include <netdb.h>
#include <pwd.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -24,24 +28,36 @@
#include <syslog.h> #include <syslog.h>
#include <time.h> #include <time.h>
#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 void
log_init(int n_debug)
log_init(int n_foreground)
{ {
extern char *__progname; 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(); tzset();
} }
void
log_verbose(int v)
{
verbose = v;
}
void void
logit(int pri, const char *fmt, ...) logit(int pri, const char *fmt, ...)
{ {
@ -57,7 +73,7 @@ vlog(int pri, const char *fmt, va_list ap)
{ {
char *nfmt; char *nfmt;
if (debug) {
if (foreground) {
/* best effort in out of mem situations */ /* best effort in out of mem situations */
if (asprintf(&nfmt, "%s\n", fmt) == -1) { if (asprintf(&nfmt, "%s\n", fmt) == -1) {
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
@ -121,7 +137,7 @@ log_debug(const char *emsg, ...)
{ {
va_list ap; va_list ap;
if (debug || debugsyslog) {
if (verbose & TRACE_DEBUG) {
va_start(ap, emsg); va_start(ap, emsg);
vlog(LOG_DEBUG, emsg, ap); vlog(LOG_DEBUG, emsg, ap);
va_end(ap); va_end(ap);
@ -129,25 +145,58 @@ log_debug(const char *emsg, ...)
} }
void 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) 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 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); exit(1);
} }
void void
fatalx(const char *emsg)
fatalx(const char *emsg, ...)
{ {
va_list ap;
errno = 0; errno = 0;
fatal(emsg);
va_start(ap, emsg);
fatal_arg(emsg, ap);
va_end(ap);
exit(1);
} }
const char * const char *


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

@ -0,0 +1,35 @@
/* $OpenBSD: log.h,v 1.1 2015/01/08 00:30:08 bcook Exp $ */
/*
* Copyright (c) 2010 Gilles Chehade <gilles@poolp.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.
*/
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);

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

@ -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 <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -58,7 +58,6 @@ volatile sig_atomic_t quit = 0;
volatile sig_atomic_t reconfig = 0; volatile sig_atomic_t reconfig = 0;
volatile sig_atomic_t sigchld = 0; volatile sig_atomic_t sigchld = 0;
struct imsgbuf *ibuf; struct imsgbuf *ibuf;
int debugsyslog = 0;
int timeout = INFTIM; int timeout = INFTIM;
const char *showopt; const char *showopt;
@ -127,6 +126,7 @@ main(int argc, char *argv[])
switch (ch) { switch (ch) {
case 'd': case 'd':
lconf.debug = 1; lconf.debug = 1;
log_verbose(1);
break; break;
case 'f': case 'f':
conffile = optarg; conffile = optarg;
@ -141,7 +141,7 @@ main(int argc, char *argv[])
lconf.settime = 0; lconf.settime = 0;
break; break;
case 'v': case 'v':
debugsyslog = 1;
log_verbose(1);
break; break;
default: default:
usage(); usage();


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

@ -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 <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -30,6 +30,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <poll.h> #include <poll.h>
#include "log.h"
#include "ntp.h" #include "ntp.h"
#include <imsg.h> #include <imsg.h>
@ -261,17 +262,6 @@ enum ctl_actions {
}; };
/* prototypes */ /* 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 */ /* ntp.c */
pid_t ntp_main(int[2], int, struct ntpd_conf *, struct passwd *); pid_t ntp_main(int[2], int, struct ntpd_conf *, struct passwd *);


+ 2
- 2
src/usr.sbin/ntpd/parse.y View File

@ -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 <henning@openbsd.org> * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -348,7 +348,7 @@ yyerror(const char *fmt, ...)
if (vasprintf(&msg, fmt, ap) == -1) if (vasprintf(&msg, fmt, ap) == -1)
fatalx("yyerror vasprintf"); fatalx("yyerror vasprintf");
va_end(ap); 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); free(msg);
return (0); return (0);
} }


Loading…
Cancel
Save