- always set *to_buf to a timestamp, even if there was none reported/parseable

(Frank Kardel).
- merge the timestamp copying code to one place.
- factor out the debugging printf function.
This commit is contained in:
christos 2015-02-10 20:38:15 +00:00
parent 652788239c
commit 7779d7745a
4 changed files with 69 additions and 49 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.3 2010/06/09 21:55:42 riz Exp $ */
/* $NetBSD: extern.h,v 1.4 2015/02/10 20:38:15 christos Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -73,7 +73,7 @@ extern struct event *allocev(void);
extern void send_queue(int __unused, short __unused, void *);
extern void schedule_event(struct event **, struct timeval *,
void (*)(int, short, void *), void *);
extern char *make_timestamp(time_t *, bool);
extern char *make_timestamp(time_t *, bool, size_t);
#ifndef DISABLE_TLS
extern struct filed *get_f_by_conninfo(struct tls_conn_settings *conn_info);
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: sign.c,v 1.5 2012/06/06 00:33:45 christos Exp $ */
/* $NetBSD: sign.c,v 1.6 2015/02/10 20:38:15 christos Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -66,7 +66,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: sign.c,v 1.5 2012/06/06 00:33:45 christos Exp $");
__RCSID("$NetBSD: sign.c,v 1.6 2015/02/10 20:38:15 christos Exp $");
#ifndef DISABLE_SIGN
#include "syslogd.h"
@ -514,7 +514,7 @@ sign_send_certificate_block(struct signature_group_t *sg)
return false;
DPRINTF((D_CALL|D_SIGN), "sign_send_certificate_block(%p)\n", sg);
tstamp = make_timestamp(NULL, true);
tstamp = make_timestamp(NULL, true, (size_t)-1);
payload_len = snprintf(payload, sizeof(payload), "%s %c %s", tstamp,
GlobalSign.keytype, GlobalSign.pubkey_b64);
@ -801,7 +801,7 @@ sign_msg_sign(struct buf_msg **bufferptr, char *sd, size_t linesize)
/* set up buffer */
buffer = buf_msg_new(0);
buffer->timestamp = strdup(make_timestamp(NULL, !BSDOutputFormat));
buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0);
buffer->prog = appname;
buffer->pid = include_pid;
buffer->recvhost = buffer->host = LocalFQDN;

View File

@ -1,4 +1,4 @@
/* $NetBSD: syslogd.c,v 1.120 2014/08/18 05:21:16 jnemeth Exp $ */
/* $NetBSD: syslogd.c,v 1.121 2015/02/10 20:38:15 christos Exp $ */
/*
* Copyright (c) 1983, 1988, 1993, 1994
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\
#if 0
static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94";
#else
__RCSID("$NetBSD: syslogd.c,v 1.120 2014/08/18 05:21:16 jnemeth Exp $");
__RCSID("$NetBSD: syslogd.c,v 1.121 2015/02/10 20:38:15 christos Exp $");
#endif
#endif /* not lint */
@ -273,9 +273,7 @@ static inline void
static int writev1(int, struct iovec *, size_t);
/* for make_timestamp() */
#define TIMESTAMPBUFSIZE 35
char timestamp[TIMESTAMPBUFSIZE];
char timestamp[MAX_TIMESTAMPLEN + 1];
/*
* Global line buffer. Since we only process one event at a time,
* a global one will do. But for klog, we use own buffer so that
@ -1045,8 +1043,7 @@ printline_syslogprotocol(const char *hname, char *msg,
if (flags & ADDDATE) {
FREEPTR(buffer->timestamp);
buffer->timestamp = strdup(make_timestamp(NULL,
!BSDOutputFormat));
buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0);
}
start = p;
@ -1262,8 +1259,7 @@ printline_bsdsyslog(const char *hname, char *msg,
if (flags & ADDDATE || !buffer->timestamp) {
FREEPTR(buffer->timestamp);
buffer->timestamp = strdup(make_timestamp(NULL,
!BSDOutputFormat));
buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0);
}
if (*p == ' ') p++; /* SP */
@ -1421,7 +1417,7 @@ printline_kernelprintf(const char *hname, char *msg,
"\"%s\", \"%s\", %d, %d)\n", hname, msg, flags, pri);
buffer = buf_msg_new(0);
buffer->timestamp = strdup(make_timestamp(NULL, !BSDOutputFormat));
buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0);
buffer->pri = pri;
buffer->flags = flags;
@ -1639,7 +1635,7 @@ logmsg_async(int pri, const char *sd, const char *msg, int flags)
buffer = buf_msg_new(0);
}
if (sd) buffer->sd = strdup(sd);
buffer->timestamp = strdup(make_timestamp(NULL, !BSDOutputFormat));
buffer->timestamp = make_timestamp(NULL, !BSDOutputFormat, 0);
buffer->prog = appname;
buffer->pid = include_pid;
buffer->recvhost = buffer->host = LocalFQDN;
@ -1722,10 +1718,11 @@ check_timestamp(unsigned char *from_buf, char **to_buf,
/* with BSD Syslog the field is reqired
* so replace it with current time
*/
*to_buf = strdup(make_timestamp(NULL, false));
*to_buf = make_timestamp(NULL, false, 0);
}
return 2;
}
*to_buf = make_timestamp(NULL, false, 0);
return 0;
}
@ -1769,20 +1766,18 @@ check_timestamp(unsigned char *from_buf, char **to_buf,
(void)strptime(tsbuf, "%FT%T%z", &parsed);
timeval = mktime(&parsed);
*to_buf = strndup(make_timestamp(&timeval, false),
BSD_TIMESTAMPLEN);
*to_buf = make_timestamp(&timeval, false, BSD_TIMESTAMPLEN);
return i;
} else if (!from_iso && to_iso) {
/* convert BSD->ISO */
struct tm parsed;
struct tm *current;
time_t timeval;
char *rc;
(void)memset(&parsed, 0, sizeof(parsed));
parsed.tm_isdst = -1;
DPRINTF(D_CALL, "check_timestamp(): convert BSD->ISO\n");
rc = strptime((char *)from_buf, "%b %d %T", &parsed);
strptime((char *)from_buf, "%b %d %T", &parsed);
current = gmtime(&now);
/* use current year and timezone */
@ -1793,8 +1788,7 @@ check_timestamp(unsigned char *from_buf, char **to_buf,
parsed.tm_year--;
timeval = mktime(&parsed);
rc = make_timestamp(&timeval, true);
*to_buf = strndup(rc, MAX_TIMESTAMPLEN-1);
*to_buf = make_timestamp(&timeval, true, MAX_TIMESTAMPLEN - 1);
return BSD_TIMESTAMPLEN;
} else {
@ -2173,8 +2167,8 @@ fprintlog(struct filed *f, struct buf_msg *passedbuffer, struct buf_queue *qentr
buffer = buf_msg_new(REPBUFSIZE);
buffer->msglen = snprintf(buffer->msg, REPBUFSIZE,
"last message repeated %d times", f->f_prevcount);
buffer->timestamp =
strdup(make_timestamp(NULL, !BSDOutputFormat));
buffer->timestamp = make_timestamp(NULL,
!BSDOutputFormat, 0);
buffer->pri = f->f_prevmsg->pri;
buffer->host = LocalFQDN;
buffer->prog = appname;
@ -4670,7 +4664,7 @@ dispatch_force_tls_reconnect(int fd, short event, void *ev)
* or use the current time if in_now is NULL.
*/
char *
make_timestamp(time_t *in_now, bool iso)
make_timestamp(time_t *in_now, bool iso, size_t tlen)
{
int frac_digits = 6;
struct timeval tv;
@ -4684,31 +4678,40 @@ make_timestamp(time_t *in_now, bool iso)
mytime = *in_now;
} else {
gettimeofday(&tv, NULL);
mytime = now = (time_t) tv.tv_sec;
mytime = now = tv.tv_sec;
}
if (!iso) {
strlcpy(timestamp, ctime(&mytime) + 4, TIMESTAMPBUFSIZE);
strlcpy(timestamp, ctime(&mytime) + 4, sizeof(timestamp));
timestamp[BSD_TIMESTAMPLEN] = '\0';
} else {
localtime_r(&mytime, &ltime);
len += strftime(timestamp, sizeof(timestamp), "%FT%T", &ltime);
snprintf(&timestamp[len], frac_digits + 2, ".%.*jd",
frac_digits, (intmax_t)tv.tv_usec);
len += frac_digits + 1;
tzlen = strftime(&timestamp[len], sizeof(timestamp) - len, "%z",
&ltime);
len += tzlen;
if (tzlen == 5) {
/* strftime gives "+0200", but we need "+02:00" */
timestamp[len + 2] = '\0';
timestamp[len + 1] = timestamp[len];
timestamp[len] = timestamp[len - 1];
timestamp[len - 1] = timestamp[len - 2];
timestamp[len - 2] = ':';
}
}
switch (tlen) {
case (size_t)-1:
return timestamp;
case 0:
return strdup(timestamp);
default:
return strndup(timestamp, tlen);
}
localtime_r(&mytime, &ltime);
len += strftime(timestamp, TIMESTAMPBUFSIZE, "%FT%T", &ltime);
snprintf(&(timestamp[len]), frac_digits+2, ".%.*ld",
frac_digits, (long)tv.tv_usec);
len += frac_digits+1;
tzlen = strftime(&(timestamp[len]), TIMESTAMPBUFSIZE-len, "%z", &ltime);
len += tzlen;
if (tzlen == 5) {
/* strftime gives "+0200", but we need "+02:00" */
timestamp[len+1] = timestamp[len];
timestamp[len] = timestamp[len-1];
timestamp[len-1] = timestamp[len-2];
timestamp[len-2] = ':';
}
return timestamp;
}
/* auxillary code to allocate memory and copy a string */
@ -4829,3 +4832,20 @@ writev1(int fd, struct iovec *iov, size_t count)
}
return tot == 0 ? nw : tot;
}
#ifndef NDEBUG
void
dbprintf(const char *fname, const char *funname,
size_t lnum, const char *fmt, ...)
{
va_list ap;
char *ts;
ts = make_timestamp(NULL, true, (size_t)-1);
printf("%s:%s:%s:%.4zu\t", ts, fname, funname, lnum);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: syslogd.h,v 1.4 2013/05/27 23:15:51 christos Exp $ */
/* $NetBSD: syslogd.h,v 1.5 2015/02/10 20:38:15 christos Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -151,9 +151,9 @@ char *strndup(const char *str, size_t n);
#ifdef NDEBUG
#define DPRINTF(x, ...) (void)0
#else
void dbprintf(const char *, const char *, size_t, const char *, ...);
#define DPRINTF(x, ...) /*LINTED null effect */(void)(Debug & (x) \
? (printf("%s:%s:%s:%.4d\t", make_timestamp(NULL, true), \
__FILE__, __func__, __LINE__), printf(__VA_ARGS__)) : 0)
? dbprintf(__FILE__, __func__, __LINE__, __VA_ARGS__) : 0)
#endif
/* shortcuts for libevent */