summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2011-03-07 10:38:18 +0000
committerpjd <pjd@FreeBSD.org>2011-03-07 10:38:18 +0000
commit76c47e936ba76108f5c9de55dc5c108264be0a3e (patch)
treeeff79e52e39056b898c7ca3f57a73abe6ef739ca
parentc22d549a366f24694675b1e30c79ba1beea2ae36 (diff)
downloadFreeBSD-src-76c47e936ba76108f5c9de55dc5c108264be0a3e.zip
FreeBSD-src-76c47e936ba76108f5c9de55dc5c108264be0a3e.tar.gz
- Turn on printf extentions.
- Load support for %T for pritning time. - Add support for %N for printing number in human readable form. - Add support for %S for printing sockaddr structure (currently only AF_INET family is supported, as this is all we need in HAST). - Disable gcc compile-time format checking as this will no longer work. MFC after: 2 weeks
-rw-r--r--sbin/hastctl/Makefile5
-rw-r--r--sbin/hastd/Makefile1
-rw-r--r--sbin/hastd/pjdlog.c88
3 files changed, 92 insertions, 2 deletions
diff --git a/sbin/hastctl/Makefile b/sbin/hastctl/Makefile
index c266666..06b2ca0 100644
--- a/sbin/hastctl/Makefile
+++ b/sbin/hastctl/Makefile
@@ -19,6 +19,7 @@ SRCS+= subr.c
SRCS+= y.tab.h
MAN= hastctl.8
+NO_WFORMAT=
CFLAGS+=-I${.CURDIR}/../hastd
CFLAGS+=-DINET
.if ${MK_INET6_SUPPORT} != "no"
@@ -28,8 +29,8 @@ CFLAGS+=-DINET6
CFLAGS+=-DYY_NO_UNPUT
CFLAGS+=-DYY_NO_INPUT
-DPADD= ${LIBL}
-LDADD= -ll
+DPADD= ${LIBL} ${LIBUTIL}
+LDADD= -ll -lutil
.if ${MK_OPENSSL} != "no"
DPADD+= ${LIBCRYPTO}
LDADD+= -lcrypto
diff --git a/sbin/hastd/Makefile b/sbin/hastd/Makefile
index 8172c26..1b55982 100644
--- a/sbin/hastd/Makefile
+++ b/sbin/hastd/Makefile
@@ -19,6 +19,7 @@ SRCS+= token.l
SRCS+= y.tab.h
MAN= hastd.8 hast.conf.5
+NO_WFORMAT=
CFLAGS+=-I${.CURDIR}
CFLAGS+=-DINET
.if ${MK_INET6_SUPPORT} != "no"
diff --git a/sbin/hastd/pjdlog.c b/sbin/hastd/pjdlog.c
index 664bd13..24c5ad4 100644
--- a/sbin/hastd/pjdlog.c
+++ b/sbin/hastd/pjdlog.c
@@ -31,9 +31,15 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <sys/socket.h>
+#include <netinet/in.h>
+
#include <assert.h>
#include <errno.h>
+#include <libutil.h>
+#include <printf.h>
#include <stdarg.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -49,6 +55,77 @@ static int pjdlog_initialized = PJDLOG_NEVER_INITIALIZED;
static int pjdlog_mode, pjdlog_debug_level;
static char pjdlog_prefix[128];
+static int
+pjdlog_printf_arginfo_humanized_number(const struct printf_info *pi __unused,
+ size_t n, int *argt)
+{
+
+ assert(n >= 1);
+ argt[0] = PA_INT | PA_FLAG_INTMAX;
+ return (1);
+}
+
+static int
+pjdlog_printf_render_humanized_number(struct __printf_io *io,
+ const struct printf_info *pi, const void * const *arg)
+{
+ char buf[5];
+ intmax_t num;
+ int ret;
+
+ num = *(const intmax_t *)arg[0];
+ humanize_number(buf, sizeof(buf), (int64_t)num, "", HN_AUTOSCALE,
+ HN_NOSPACE | HN_DECIMAL);
+ ret = __printf_out(io, pi, buf, strlen(buf));
+ __printf_flush(io);
+ return (ret);
+}
+
+static int
+pjdlog_printf_arginfo_sockaddr(const struct printf_info *pi __unused,
+ size_t n, int *argt)
+{
+
+ assert(n >= 1);
+ argt[0] = PA_POINTER;
+ return (1);
+}
+
+static int
+pjdlog_printf_render_sockaddr(struct __printf_io *io,
+ const struct printf_info *pi, const void * const *arg)
+{
+ const struct sockaddr *sa;
+ char buf[64];
+ int ret;
+
+ sa = *(const struct sockaddr * const *)arg[0];
+ switch (sa->sa_family) {
+ case AF_INET:
+ {
+ const struct sockaddr_in *sin;
+ in_addr_t ip;
+ unsigned int port;
+
+ sin = (const struct sockaddr_in *)sa;
+ ip = ntohl(sin->sin_addr.s_addr);
+ port = ntohs(sin->sin_port);
+
+ snprintf(buf, sizeof(buf), "%u.%u.%u.%u:%u",
+ ((ip >> 24) & 0xff), ((ip >> 16) & 0xff),
+ ((ip >> 8) & 0xff), (ip & 0xff), port);
+ break;
+ }
+ default:
+ snprintf(buf, sizeof(buf), "[unsupported family %u]",
+ (unsigned int)sa->sa_family);
+ break;
+ }
+ ret = __printf_out(io, pi, buf, strlen(buf));
+ __printf_flush(io);
+ return (ret);
+}
+
void
pjdlog_init(int mode)
{
@@ -57,6 +134,17 @@ pjdlog_init(int mode)
pjdlog_initialized == PJDLOG_NOT_INITIALIZED);
assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
+ if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) {
+ __use_xprintf = 1;
+ register_printf_render_std("T");
+ register_printf_render('N',
+ pjdlog_printf_render_humanized_number,
+ pjdlog_printf_arginfo_humanized_number);
+ register_printf_render('S',
+ pjdlog_printf_render_sockaddr,
+ pjdlog_printf_arginfo_sockaddr);
+ }
+
if (mode == PJDLOG_MODE_SYSLOG)
openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
pjdlog_mode = mode;
OpenPOWER on IntegriCloud