Module Name:    src
Committed By:   christos
Date:           Tue Feb 10 20:38:15 UTC 2015

Modified Files:
        src/usr.sbin/syslogd: extern.h sign.c syslogd.c syslogd.h

Log Message:
- 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.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/syslogd/extern.h
cvs rdiff -u -r1.5 -r1.6 src/usr.sbin/syslogd/sign.c
cvs rdiff -u -r1.120 -r1.121 src/usr.sbin/syslogd/syslogd.c
cvs rdiff -u -r1.4 -r1.5 src/usr.sbin/syslogd/syslogd.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/syslogd/extern.h
diff -u src/usr.sbin/syslogd/extern.h:1.3 src/usr.sbin/syslogd/extern.h:1.4
--- src/usr.sbin/syslogd/extern.h:1.3	Wed Jun  9 17:55:42 2010
+++ src/usr.sbin/syslogd/extern.h	Tue Feb 10 15:38:15 2015
@@ -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

Index: src/usr.sbin/syslogd/sign.c
diff -u src/usr.sbin/syslogd/sign.c:1.5 src/usr.sbin/syslogd/sign.c:1.6
--- src/usr.sbin/syslogd/sign.c:1.5	Tue Jun  5 20:33:45 2012
+++ src/usr.sbin/syslogd/sign.c	Tue Feb 10 15:38:15 2015
@@ -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 signa
 		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
 
 	/* 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;

Index: src/usr.sbin/syslogd/syslogd.c
diff -u src/usr.sbin/syslogd/syslogd.c:1.120 src/usr.sbin/syslogd/syslogd.c:1.121
--- src/usr.sbin/syslogd/syslogd.c:1.120	Mon Aug 18 01:21:16 2014
+++ src/usr.sbin/syslogd/syslogd.c	Tue Feb 10 15:38:15 2015
@@ -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, 19
 #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 *hna
 
 	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, c
 
 	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
 		"\"%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, co
 		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,
 				/* 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,
 		(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,
 			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_ms
 			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, sho
  * 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';
-		return timestamp;
+	} 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] = ':';
+		}
 	}
 
-	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] = ':';
+	switch (tlen) {
+	case (size_t)-1:
+		return timestamp;
+	case 0:
+		return strdup(timestamp);
+	default:
+		return strndup(timestamp, tlen);
 	}
-	return timestamp;
 }
 
 /* auxillary code to allocate memory and copy a string */
@@ -4829,3 +4832,20 @@ writev1(int fd, struct iovec *iov, size_
 	}
 	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

Index: src/usr.sbin/syslogd/syslogd.h
diff -u src/usr.sbin/syslogd/syslogd.h:1.4 src/usr.sbin/syslogd/syslogd.h:1.5
--- src/usr.sbin/syslogd/syslogd.h:1.4	Mon May 27 19:15:51 2013
+++ src/usr.sbin/syslogd/syslogd.h	Tue Feb 10 15:38:15 2015
@@ -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 */

Reply via email to