Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/syslogd - always set *to_buf to a timestamp, even i...



details:   https://anonhg.NetBSD.org/src/rev/de3984f6afd3
branches:  trunk
changeset: 336077:de3984f6afd3
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Feb 10 20:38:15 2015 +0000

description:
- 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.

diffstat:

 usr.sbin/syslogd/extern.h  |   4 +-
 usr.sbin/syslogd/sign.c    |   8 +-
 usr.sbin/syslogd/syslogd.c |  98 +++++++++++++++++++++++++++------------------
 usr.sbin/syslogd/syslogd.h |   6 +-
 4 files changed, 68 insertions(+), 48 deletions(-)

diffs (288 lines):

diff -r 40f759a9c930 -r de3984f6afd3 usr.sbin/syslogd/extern.h
--- a/usr.sbin/syslogd/extern.h Tue Feb 10 19:11:52 2015 +0000
+++ b/usr.sbin/syslogd/extern.h Tue Feb 10 20:38:15 2015 +0000
@@ -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 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
diff -r 40f759a9c930 -r de3984f6afd3 usr.sbin/syslogd/sign.c
--- a/usr.sbin/syslogd/sign.c   Tue Feb 10 19:11:52 2015 +0000
+++ b/usr.sbin/syslogd/sign.c   Tue Feb 10 20:38:15 2015 +0000
@@ -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 @@
                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 @@
 
        /* 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;
diff -r 40f759a9c930 -r de3984f6afd3 usr.sbin/syslogd/syslogd.c
--- a/usr.sbin/syslogd/syslogd.c        Tue Feb 10 19:11:52 2015 +0000
+++ b/usr.sbin/syslogd/syslogd.c        Tue Feb 10 20:38:15 2015 +0000
@@ -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 @@
 #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 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 @@
 
        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 @@
 
        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 @@
                "\"%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 @@
                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 @@
                                /* 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 @@
                (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 @@
                        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 @@
                        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 @@
  * 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 @@
                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 @@
        }
        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
diff -r 40f759a9c930 -r de3984f6afd3 usr.sbin/syslogd/syslogd.h
--- a/usr.sbin/syslogd/syslogd.h        Tue Feb 10 19:11:52 2015 +0000
+++ b/usr.sbin/syslogd/syslogd.h        Tue Feb 10 20:38:15 2015 +0000
@@ -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 @@
 #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 */



Home | Main Index | Thread Index | Old Index