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