Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/gen Split syslog.c to:



details:   https://anonhg.NetBSD.org/src/rev/155f6daf8849
branches:  trunk
changeset: 820594:155f6daf8849
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Jan 12 00:38:01 2017 +0000

description:
Split syslog.c to:

- syslog_ss.c *_ss api functions (don't use stdio, time)
- syslog.c: *syslog* non _ss api functions (use stdio, time)
- xsyslog.c> common guts.

The motivation for this is not to drag in stdio/locale/floating point/time
for every binary, since syslog_ss() is used in __stack_check_fail() for SSP.

diffstat:

 lib/libc/gen/Makefile.inc     |    8 +-
 lib/libc/gen/syslog.c         |  494 ++++-------------------------------------
 lib/libc/gen/syslog_private.h |   50 ++++
 lib/libc/gen/syslog_ss.c      |   95 ++++++++
 lib/libc/gen/xsyslog.c        |  422 +++++++++++++++++++++++++++++++++++
 5 files changed, 629 insertions(+), 440 deletions(-)

diffs (truncated from 1188 to 300 lines):

diff -r 8b071a9403d6 -r 155f6daf8849 lib/libc/gen/Makefile.inc
--- a/lib/libc/gen/Makefile.inc Thu Jan 12 00:35:38 2017 +0000
+++ b/lib/libc/gen/Makefile.inc Thu Jan 12 00:38:01 2017 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.196 2016/10/04 09:41:41 kamil Exp $
+#      $NetBSD: Makefile.inc,v 1.197 2017/01/12 00:38:01 christos Exp $
 #      from: @(#)Makefile.inc  8.6 (Berkeley) 5/4/95
 
 # gen sources
@@ -32,12 +32,12 @@
        siglist.c signal.c signame.c sigrelse.c \
        sigset.c sigsetops.c sleep.c \
        stringlist.c sysconf.c sysctl.c sysctlbyname.c sysctlgetmibinfo.c \
-       sysctlnametomib.c syslog.c telldir.c time.c timespec_get.c \
+       sysctlnametomib.c syslog.c syslog_ss.c telldir.c time.c timespec_get.c \
        times.c toascii.c tolower_.c ttyname.c ttyslot.c toupper_.c ualarm.c \
        ulimit.c uname.c unvis.c usleep.c utime.c utimens.c utmp.c \
        utmpx.c valloc.c vis.c wait.c wait3.c waitid.c waitpid.c warn.c \
-       warnx.c warnc.c vwarn.c vwarnx.c vwarnc.c verr.c verrx.c verrc.c \
-       wordexp.c
+       warnx.c warnc.c wordexp.c xsyslog.c \
+       vwarn.c vwarnx.c vwarnc.c verr.c verrx.c verrc.c
 
 # uses alloca
 COPTS.execvp.c = -Wno-stack-protector
diff -r 8b071a9403d6 -r 155f6daf8849 lib/libc/gen/syslog.c
--- a/lib/libc/gen/syslog.c     Thu Jan 12 00:35:38 2017 +0000
+++ b/lib/libc/gen/syslog.c     Thu Jan 12 00:38:01 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: syslog.c,v 1.55 2015/10/26 11:44:30 roy Exp $  */
+/*     $NetBSD: syslog.c,v 1.56 2017/01/12 00:38:01 christos Exp $     */
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)syslog.c   8.5 (Berkeley) 4/29/95";
 #else
-__RCSID("$NetBSD: syslog.c,v 1.55 2015/10/26 11:44:30 roy Exp $");
+__RCSID("$NetBSD: syslog.c,v 1.56 2017/01/12 00:38:01 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -48,6 +48,7 @@
 #include <netdb.h>
 
 #include <errno.h>
+#include <stdio.h>
 #include <fcntl.h>
 #include <paths.h>
 #include <stdarg.h>
@@ -56,32 +57,61 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+
+#include "syslog_private.h"
 #include "reentrant.h"
 #include "extern.h"
 
 #ifdef __weak_alias
-__weak_alias(closelog,_closelog)
-__weak_alias(openlog,_openlog)
-__weak_alias(setlogmask,_setlogmask)
 __weak_alias(syslog,_syslog)
 __weak_alias(vsyslog,_vsyslog)
 __weak_alias(syslogp,_syslogp)
 __weak_alias(vsyslogp,_vsyslogp)
 #endif
 
-static struct syslog_data sdata = SYSLOG_DATA_INIT;
+static size_t
+timefun(char *p, size_t tbuf_left)
+{
+       struct timeval tv;
+       time_t now;
+       struct tm tmnow;
+       size_t prlen;
+       char *op = p;
 
-static void    openlog_unlocked_r(const char *, int, int,
-    struct syslog_data *);
-static void    disconnectlog_r(struct syslog_data *);
-static void    connectlog_r(struct syslog_data *);
+       if (gettimeofday(&tv, NULL) == -1)
+               return snprintf_ss(p, tbuf_left, "-");
+       
+       /* strftime() implies tzset(), localtime_r() doesn't. */
+       tzset();
+       now = (time_t) tv.tv_sec;
+       localtime_r(&now, &tmnow);
 
-#define LOG_SIGNAL_SAFE        (int)0x80000000
-
+       prlen = strftime(p, tbuf_left, "%FT%T", &tmnow);
+       DEC();
+       prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec);
+       DEC();
+       prlen = strftime(p, tbuf_left-1, "%z", &tmnow);
+       /* strftime gives eg. "+0200", but we need "+02:00" */
+       if (prlen == 5) {
+               p[prlen+1] = p[prlen];
+               p[prlen]   = p[prlen-1];
+               p[prlen-1] = p[prlen-2];
+               p[prlen-2] = ':';
+               prlen += 1;
+       }
+       DEC();
+       return (size_t)(p - op);
+}
 
-#ifdef _REENTRANT
-static mutex_t syslog_mutex = MUTEX_INITIALIZER;
+static struct syslog_fun _syslog_fun = {
+       timefun,
+       strerror_r,
+#ifndef __lint__
+       _vsnprintf,
+#else
+       vsnprintf,
 #endif
+};
 
 /*
  * syslog, vsyslog --
@@ -93,14 +123,14 @@
        va_list ap;
 
        va_start(ap, fmt);
-       vsyslog(pri, fmt, ap);
+       _vxsyslogp_r(pri, &_syslog_fun, &_syslog_data, NULL, NULL, fmt, ap);
        va_end(ap);
 }
 
 void
 vsyslog(int pri, const char *fmt, va_list ap)
 {
-       vsyslog_r(pri, &sdata, fmt, ap);
+       _vxsyslogp_r(pri, &_syslog_fun, &_syslog_data, NULL, NULL, fmt, ap);
 }
 
 /*
@@ -113,44 +143,33 @@
        va_list ap;
 
        va_start(ap, msgfmt);
-       vsyslogp(pri, msgid, sdfmt, msgfmt, ap);
+       _vxsyslogp_r(pri, &_syslog_fun, &_syslog_data,
+           msgid, sdfmt, msgfmt, ap);
        va_end(ap);
 }
 
 void
-vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, va_list ap)
+vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt,
+    va_list ap)
 {
-       vsyslogp_r(pri, &sdata, msgid, sdfmt, msgfmt, ap);
-}
-
-void
-openlog(const char *ident, int logstat, int logfac)
-{
-       openlog_r(ident, logstat, logfac, &sdata);
+       _vxsyslogp_r(pri, &_syslog_fun, &_syslog_data,
+           msgid, sdfmt, msgfmt, ap);
 }
 
 void
-closelog(void)
+vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
+    const char *sdfmt, const char *msgfmt, va_list ap)
 {
-       closelog_r(&sdata);
+       _vxsyslogp_r(pri, &_syslog_fun, data, msgid, sdfmt, msgfmt, ap);
 }
 
-/* setlogmask -- set the log mask level */
-int
-setlogmask(int pmask)
-{
-       return setlogmask_r(pmask, &sdata);
-}
-
-/* Reentrant version of syslog, i.e. syslog_r() */
-
 void
 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
 {
        va_list ap;
 
        va_start(ap, fmt);
-       vsyslog_r(pri, data, fmt, ap);
+       _vxsyslogp_r(pri, &_syslog_fun, data, NULL, NULL, fmt, ap);
        va_end(ap);
 }
 
@@ -161,409 +180,12 @@
        va_list ap;
 
        va_start(ap, msgfmt);
-       vsyslogp_r(pri, data, msgid, sdfmt, msgfmt, ap);
-       va_end(ap);
-}
-
-void
-syslog_ss(int pri, struct syslog_data *data, const char *fmt, ...)
-{
-       va_list ap;
-
-       va_start(ap, fmt);
-       vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap);
+       _vxsyslogp_r(pri, &_syslog_fun, data, msgid, sdfmt, msgfmt, ap);
        va_end(ap);
 }
 
 void
-syslogp_ss(int pri, struct syslog_data *data, const char *msgid,
-       const char *sdfmt, const char *msgfmt, ...)
-{
-       va_list ap;
-
-       va_start(ap, msgfmt);
-       vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap);
-       va_end(ap);
-}
-
-void
-vsyslog_ss(int pri, struct syslog_data *data, const char *fmt, va_list ap)
-{
-       vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap);
-}
-
-void
-vsyslogp_ss(int pri, struct syslog_data *data, const char *msgid,
-       const char *sdfmt, const char *msgfmt, va_list ap)
-{
-       vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap);
-}
-
-
-void
 vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
 {
-       vsyslogp_r(pri, data, NULL, NULL, fmt, ap);
+       _vxsyslogp_r(pri, &_syslog_fun, data, NULL, NULL, fmt, ap);
 }
-
-void
-vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
-       const char *sdfmt, const char *msgfmt, va_list ap)
-{
-       static const char BRCOSP[] = "]: ";
-       static const char CRLF[] = "\r\n";
-       size_t cnt, prlen, tries;
-       char ch, *p, *t;
-       struct timeval tv;
-       struct tm tmnow;
-       time_t now;
-       int fd, saved_errno;
-#define TBUF_LEN       2048
-#define FMT_LEN                1024
-#define MAXTRIES       10
-       char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = "";
-       size_t tbuf_left, fmt_left, msgsdlen;
-       char *fmt = fmt_cat;
-       int signal_safe = pri & LOG_SIGNAL_SAFE;
-       struct iovec iov[7];    /* prog + [ + pid + ]: + fmt + crlf */
-       int opened, iovcnt;
-
-       pri &= ~LOG_SIGNAL_SAFE;
-
-#define INTERNALLOG    LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
-       /* Check for invalid bits. */
-       if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
-               syslog_r(INTERNALLOG | signal_safe, data,
-                   "syslog_r: unknown facility/priority: %x", pri);
-               pri &= LOG_PRIMASK|LOG_FACMASK;
-       }
-
-       /* Check priority against setlogmask values. */
-       if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
-               return;
-
-       saved_errno = errno;
-
-       /* Set default facility if none specified. */
-       if ((pri & LOG_FACMASK) == 0)
-               pri |= data->log_fac;
-
-       /* Build the message. */
-       p = tbuf;
-       tbuf_left = TBUF_LEN;
-
-#define DEC()                                                  \
-       do {                                                    \
-               if (prlen >= tbuf_left)                         \
-                       prlen = tbuf_left - 1;                  \
-               p += prlen;                                     \
-               tbuf_left -= prlen;                             \



Home | Main Index | Thread Index | Old Index