Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.56
      1 /*	$NetBSD: syslog.c,v 1.56 2017/01/12 00:38:01 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char sccsid[] = "@(#)syslog.c	8.5 (Berkeley) 4/29/95";
     36 #else
     37 __RCSID("$NetBSD: syslog.c,v 1.56 2017/01/12 00:38:01 christos Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include <sys/types.h>
     43 #include <sys/param.h>
     44 #include <sys/socket.h>
     45 #include <sys/syslog.h>
     46 #include <sys/uio.h>
     47 #include <sys/un.h>
     48 #include <netdb.h>
     49 
     50 #include <errno.h>
     51 #include <stdio.h>
     52 #include <fcntl.h>
     53 #include <paths.h>
     54 #include <stdarg.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <time.h>
     59 #include <unistd.h>
     60 
     61 #include "syslog_private.h"
     62 #include "reentrant.h"
     63 #include "extern.h"
     64 
     65 #ifdef __weak_alias
     66 __weak_alias(syslog,_syslog)
     67 __weak_alias(vsyslog,_vsyslog)
     68 __weak_alias(syslogp,_syslogp)
     69 __weak_alias(vsyslogp,_vsyslogp)
     70 #endif
     71 
     72 static size_t
     73 timefun(char *p, size_t tbuf_left)
     74 {
     75 	struct timeval tv;
     76 	time_t now;
     77 	struct tm tmnow;
     78 	size_t prlen;
     79 	char *op = p;
     80 
     81 	if (gettimeofday(&tv, NULL) == -1)
     82 		return snprintf_ss(p, tbuf_left, "-");
     83 
     84 	/* strftime() implies tzset(), localtime_r() doesn't. */
     85 	tzset();
     86 	now = (time_t) tv.tv_sec;
     87 	localtime_r(&now, &tmnow);
     88 
     89 	prlen = strftime(p, tbuf_left, "%FT%T", &tmnow);
     90 	DEC();
     91 	prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec);
     92 	DEC();
     93 	prlen = strftime(p, tbuf_left-1, "%z", &tmnow);
     94 	/* strftime gives eg. "+0200", but we need "+02:00" */
     95 	if (prlen == 5) {
     96 		p[prlen+1] = p[prlen];
     97 		p[prlen]   = p[prlen-1];
     98 		p[prlen-1] = p[prlen-2];
     99 		p[prlen-2] = ':';
    100 		prlen += 1;
    101 	}
    102 	DEC();
    103 	return (size_t)(p - op);
    104 }
    105 
    106 static struct syslog_fun _syslog_fun = {
    107 	timefun,
    108 	strerror_r,
    109 #ifndef __lint__
    110 	_vsnprintf,
    111 #else
    112 	vsnprintf,
    113 #endif
    114 };
    115 
    116 /*
    117  * syslog, vsyslog --
    118  *	print message on log file; output is intended for syslogd(8).
    119  */
    120 void
    121 syslog(int pri, const char *fmt, ...)
    122 {
    123 	va_list ap;
    124 
    125 	va_start(ap, fmt);
    126 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data, NULL, NULL, fmt, ap);
    127 	va_end(ap);
    128 }
    129 
    130 void
    131 vsyslog(int pri, const char *fmt, va_list ap)
    132 {
    133 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data, NULL, NULL, fmt, ap);
    134 }
    135 
    136 /*
    137  * syslogp, vsyslogp --
    138  *	like syslog but take additional arguments for MSGID and SD
    139  */
    140 void
    141 syslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, ...)
    142 {
    143 	va_list ap;
    144 
    145 	va_start(ap, msgfmt);
    146 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data,
    147 	    msgid, sdfmt, msgfmt, ap);
    148 	va_end(ap);
    149 }
    150 
    151 void
    152 vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt,
    153     va_list ap)
    154 {
    155 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data,
    156 	    msgid, sdfmt, msgfmt, ap);
    157 }
    158 
    159 void
    160 vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
    161     const char *sdfmt, const char *msgfmt, va_list ap)
    162 {
    163 	_vxsyslogp_r(pri, &_syslog_fun, data, msgid, sdfmt, msgfmt, ap);
    164 }
    165 
    166 void
    167 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
    168 {
    169 	va_list ap;
    170 
    171 	va_start(ap, fmt);
    172 	_vxsyslogp_r(pri, &_syslog_fun, data, NULL, NULL, fmt, ap);
    173 	va_end(ap);
    174 }
    175 
    176 void
    177 syslogp_r(int pri, struct syslog_data *data, const char *msgid,
    178 	const char *sdfmt, const char *msgfmt, ...)
    179 {
    180 	va_list ap;
    181 
    182 	va_start(ap, msgfmt);
    183 	_vxsyslogp_r(pri, &_syslog_fun, data, msgid, sdfmt, msgfmt, ap);
    184 	va_end(ap);
    185 }
    186 
    187 void
    188 vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
    189 {
    190 	_vxsyslogp_r(pri, &_syslog_fun, data, NULL, NULL, fmt, ap);
    191 }
    192