summaryrefslogtreecommitdiff
path: root/usr.sbin/ntpd/log.c
diff options
context:
space:
mode:
authorBrent Cook <bcook@cvs.openbsd.org>2015-01-08 00:30:09 +0000
committerBrent Cook <bcook@cvs.openbsd.org>2015-01-08 00:30:09 +0000
commit020edf3cb03171d18806fc2d4c74ac5158914c05 (patch)
tree927d05f06ecee8ebebafaa41bd6224d534a357f1 /usr.sbin/ntpd/log.c
parent200b730369f4e91a32d265c7ea83f53084bb0a74 (diff)
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@
Diffstat (limited to 'usr.sbin/ntpd/log.c')
-rw-r--r--usr.sbin/ntpd/log.c89
1 files changed, 69 insertions, 20 deletions
diff --git a/usr.sbin/ntpd/log.c b/usr.sbin/ntpd/log.c
index 0db0245c46e..12b287184a5 100644
--- a/usr.sbin/ntpd/log.c
+++ b/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 <henning@openbsd.org>
@@ -16,7 +16,11 @@
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <sys/socket.h>
+
#include <errno.h>
+#include <netdb.h>
+#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -24,25 +28,37 @@
#include <syslog.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
-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, ...)
{
va_list ap;
@@ -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 *