Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.55.2.1
      1  1.55.2.1  pgoyette /*	$NetBSD: syslog.c,v 1.55.2.1 2017/03/20 06:56:57 pgoyette 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.55.2.1  pgoyette __RCSID("$NetBSD: syslog.c,v 1.55.2.1 2017/03/20 06:56:57 pgoyette 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.55.2.1  pgoyette #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.55.2.1  pgoyette 
     61  1.55.2.1  pgoyette #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.55.2.1  pgoyette __weak_alias(closelog,_closelog)
     71  1.55.2.1  pgoyette __weak_alias(openlog,_openlog)
     72  1.55.2.1  pgoyette __weak_alias(setlogmask,_setlogmask)
     73       1.1       cgd #endif
     74       1.1       cgd 
     75  1.55.2.1  pgoyette static struct syslog_data _syslog_data = SYSLOG_DATA_INIT;
     76      1.20    kleink 
     77      1.29   thorpej #ifdef _REENTRANT
     78      1.20    kleink static mutex_t	syslog_mutex = MUTEX_INITIALIZER;
     79      1.20    kleink #endif
     80      1.20    kleink 
     81  1.55.2.1  pgoyette static size_t
     82  1.55.2.1  pgoyette timefun(char *p, size_t tbuf_left)
     83       1.1       cgd {
     84  1.55.2.1  pgoyette 	struct timeval tv;
     85  1.55.2.1  pgoyette 	time_t now;
     86  1.55.2.1  pgoyette 	struct tm tmnow;
     87  1.55.2.1  pgoyette 	size_t prlen;
     88  1.55.2.1  pgoyette 	char *op = p;
     89       1.1       cgd 
     90  1.55.2.1  pgoyette 	if (gettimeofday(&tv, NULL) == -1)
     91  1.55.2.1  pgoyette 		return snprintf_ss(p, tbuf_left, "-");
     92  1.55.2.1  pgoyette 
     93  1.55.2.1  pgoyette 	/* strftime() implies tzset(), localtime_r() doesn't. */
     94  1.55.2.1  pgoyette 	tzset();
     95  1.55.2.1  pgoyette 	now = (time_t) tv.tv_sec;
     96  1.55.2.1  pgoyette 	localtime_r(&now, &tmnow);
     97       1.1       cgd 
     98  1.55.2.1  pgoyette 	prlen = strftime(p, tbuf_left, "%FT%T", &tmnow);
     99  1.55.2.1  pgoyette 	DEC();
    100  1.55.2.1  pgoyette 	prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec);
    101  1.55.2.1  pgoyette 	DEC();
    102  1.55.2.1  pgoyette 	prlen = strftime(p, tbuf_left-1, "%z", &tmnow);
    103  1.55.2.1  pgoyette 	/* strftime gives eg. "+0200", but we need "+02:00" */
    104  1.55.2.1  pgoyette 	if (prlen == 5) {
    105  1.55.2.1  pgoyette 		p[prlen+1] = p[prlen];
    106  1.55.2.1  pgoyette 		p[prlen]   = p[prlen-1];
    107  1.55.2.1  pgoyette 		p[prlen-1] = p[prlen-2];
    108  1.55.2.1  pgoyette 		p[prlen-2] = ':';
    109  1.55.2.1  pgoyette 		prlen += 1;
    110  1.55.2.1  pgoyette 	}
    111  1.55.2.1  pgoyette 	DEC();
    112  1.55.2.1  pgoyette 	return (size_t)(p - op);
    113      1.32  christos }
    114      1.32  christos 
    115  1.55.2.1  pgoyette static int
    116  1.55.2.1  pgoyette lock(const struct syslog_data *data)
    117      1.43  christos {
    118  1.55.2.1  pgoyette 	int rv = data == &_syslog_data;
    119  1.55.2.1  pgoyette 	if (rv)
    120  1.55.2.1  pgoyette 		mutex_lock(&syslog_mutex);
    121  1.55.2.1  pgoyette 	return rv;
    122      1.43  christos }
    123      1.43  christos 
    124  1.55.2.1  pgoyette static int
    125  1.55.2.1  pgoyette unlock(const struct syslog_data *data)
    126      1.43  christos {
    127  1.55.2.1  pgoyette 	int rv = data == &_syslog_data;
    128  1.55.2.1  pgoyette 	if (rv)
    129  1.55.2.1  pgoyette 		mutex_unlock(&syslog_mutex);
    130  1.55.2.1  pgoyette 	return rv;
    131      1.43  christos }
    132      1.43  christos 
    133  1.55.2.1  pgoyette static struct syslog_fun _syslog_fun = {
    134  1.55.2.1  pgoyette 	timefun,
    135  1.55.2.1  pgoyette 	strerror_r,
    136  1.55.2.1  pgoyette 	vsnprintf,
    137  1.55.2.1  pgoyette 	lock,
    138  1.55.2.1  pgoyette 	unlock,
    139  1.55.2.1  pgoyette };
    140  1.55.2.1  pgoyette 
    141      1.32  christos void
    142      1.32  christos openlog(const char *ident, int logstat, int logfac)
    143       1.1       cgd {
    144  1.55.2.1  pgoyette 	openlog_r(ident, logstat, logfac, &_syslog_data);
    145      1.32  christos }
    146      1.32  christos 
    147      1.32  christos void
    148      1.32  christos closelog(void)
    149      1.32  christos {
    150  1.55.2.1  pgoyette 	closelog_r(&_syslog_data);
    151      1.32  christos }
    152      1.32  christos 
    153      1.32  christos /* setlogmask -- set the log mask level */
    154      1.32  christos int
    155      1.32  christos setlogmask(int pmask)
    156      1.32  christos {
    157  1.55.2.1  pgoyette 	return setlogmask_r(pmask, &_syslog_data);
    158      1.32  christos }
    159      1.32  christos 
    160      1.32  christos void
    161  1.55.2.1  pgoyette openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
    162      1.32  christos {
    163  1.55.2.1  pgoyette 	lock(data);
    164  1.55.2.1  pgoyette 	_openlog_unlocked_r(ident, logstat, logfac, data);
    165  1.55.2.1  pgoyette 	unlock(data);
    166      1.32  christos }
    167      1.32  christos 
    168      1.32  christos void
    169  1.55.2.1  pgoyette closelog_r(struct syslog_data *data)
    170      1.43  christos {
    171  1.55.2.1  pgoyette 	lock(data);
    172  1.55.2.1  pgoyette 	_closelog_unlocked_r(data);
    173  1.55.2.1  pgoyette 	data->log_tag = NULL;
    174  1.55.2.1  pgoyette 	unlock(data);
    175      1.43  christos }
    176      1.43  christos 
    177  1.55.2.1  pgoyette /*
    178  1.55.2.1  pgoyette  * syslog, vsyslog --
    179  1.55.2.1  pgoyette  *	print message on log file; output is intended for syslogd(8).
    180  1.55.2.1  pgoyette  */
    181      1.43  christos void
    182  1.55.2.1  pgoyette syslog(int pri, const char *fmt, ...)
    183      1.34  christos {
    184      1.34  christos 	va_list ap;
    185      1.34  christos 
    186      1.34  christos 	va_start(ap, fmt);
    187  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data, NULL, NULL, fmt, ap);
    188      1.34  christos 	va_end(ap);
    189      1.34  christos }
    190      1.34  christos 
    191      1.34  christos void
    192  1.55.2.1  pgoyette vsyslog(int pri, const char *fmt, va_list ap)
    193      1.43  christos {
    194  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data, NULL, NULL, fmt, ap);
    195      1.43  christos }
    196      1.43  christos 
    197  1.55.2.1  pgoyette /*
    198  1.55.2.1  pgoyette  * syslogp, vsyslogp --
    199  1.55.2.1  pgoyette  *	like syslog but take additional arguments for MSGID and SD
    200  1.55.2.1  pgoyette  */
    201      1.43  christos void
    202  1.55.2.1  pgoyette syslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, ...)
    203      1.34  christos {
    204  1.55.2.1  pgoyette 	va_list ap;
    205      1.34  christos 
    206  1.55.2.1  pgoyette 	va_start(ap, msgfmt);
    207  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data,
    208  1.55.2.1  pgoyette 	    msgid, sdfmt, msgfmt, ap);
    209  1.55.2.1  pgoyette 	va_end(ap);
    210      1.43  christos }
    211      1.43  christos 
    212      1.43  christos void
    213  1.55.2.1  pgoyette vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt,
    214  1.55.2.1  pgoyette     va_list ap)
    215      1.32  christos {
    216  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, &_syslog_data,
    217  1.55.2.1  pgoyette 	    msgid, sdfmt, msgfmt, ap);
    218      1.43  christos }
    219      1.43  christos 
    220      1.43  christos void
    221      1.43  christos vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
    222  1.55.2.1  pgoyette     const char *sdfmt, const char *msgfmt, va_list ap)
    223      1.32  christos {
    224  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, data, msgid, sdfmt, msgfmt, ap);
    225      1.32  christos }
    226       1.1       cgd 
    227  1.55.2.1  pgoyette void
    228  1.55.2.1  pgoyette syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
    229       1.1       cgd {
    230  1.55.2.1  pgoyette 	va_list ap;
    231      1.41  christos 
    232  1.55.2.1  pgoyette 	va_start(ap, fmt);
    233  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, data, NULL, NULL, fmt, ap);
    234  1.55.2.1  pgoyette 	va_end(ap);
    235      1.20    kleink }
    236      1.20    kleink 
    237      1.32  christos void
    238  1.55.2.1  pgoyette syslogp_r(int pri, struct syslog_data *data, const char *msgid,
    239  1.55.2.1  pgoyette 	const char *sdfmt, const char *msgfmt, ...)
    240       1.1       cgd {
    241  1.55.2.1  pgoyette 	va_list ap;
    242      1.20    kleink 
    243  1.55.2.1  pgoyette 	va_start(ap, msgfmt);
    244  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, data, msgid, sdfmt, msgfmt, ap);
    245  1.55.2.1  pgoyette 	va_end(ap);
    246       1.1       cgd }
    247       1.1       cgd 
    248  1.55.2.1  pgoyette void
    249  1.55.2.1  pgoyette vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
    250       1.1       cgd {
    251  1.55.2.1  pgoyette 	_vxsyslogp_r(pri, &_syslog_fun, data, NULL, NULL, fmt, ap);
    252       1.1       cgd }
    253