Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.26
      1  1.26       cgd /*	$NetBSD: syslog.c,v 1.26 2001/02/19 22:22:16 cgd 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.11  christos #include <sys/cdefs.h>
     37   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     38   1.6       cgd #if 0
     39  1.13     perry static char sccsid[] = "@(#)syslog.c	8.5 (Berkeley) 4/29/95";
     40   1.6       cgd #else
     41  1.26       cgd __RCSID("$NetBSD: syslog.c,v 1.26 2001/02/19 22:22:16 cgd Exp $");
     42   1.6       cgd #endif
     43   1.1       cgd #endif /* LIBC_SCCS and not lint */
     44   1.1       cgd 
     45  1.12       jtc #include "namespace.h"
     46   1.1       cgd #include <sys/types.h>
     47   1.1       cgd #include <sys/socket.h>
     48   1.1       cgd #include <sys/syslog.h>
     49   1.1       cgd #include <sys/uio.h>
     50  1.21        is #include <sys/un.h>
     51   1.1       cgd #include <netdb.h>
     52   1.2     glass 
     53   1.2     glass #include <errno.h>
     54   1.2     glass #include <fcntl.h>
     55   1.2     glass #include <paths.h>
     56   1.2     glass #include <stdio.h>
     57  1.26       cgd #include <stdlib.h>
     58   1.1       cgd #include <string.h>
     59   1.2     glass #include <time.h>
     60   1.2     glass #include <unistd.h>
     61  1.20    kleink #include "reentrant.h"
     62   1.2     glass 
     63   1.1       cgd #if __STDC__
     64   1.1       cgd #include <stdarg.h>
     65   1.1       cgd #else
     66   1.1       cgd #include <varargs.h>
     67  1.12       jtc #endif
     68  1.12       jtc 
     69  1.12       jtc #ifdef __weak_alias
     70  1.24   mycroft __weak_alias(closelog,_closelog)
     71  1.24   mycroft __weak_alias(openlog,_openlog)
     72  1.24   mycroft __weak_alias(setlogmask,_setlogmask)
     73  1.24   mycroft __weak_alias(syslog,_syslog)
     74  1.24   mycroft __weak_alias(vsyslog,_vsyslog)
     75   1.1       cgd #endif
     76   1.1       cgd 
     77   1.1       cgd static int	LogFile = -1;		/* fd for log */
     78   1.1       cgd static int	connected;		/* have done connect */
     79   1.1       cgd static int	LogStat = 0;		/* status bits, set by openlog() */
     80   1.6       cgd static const char *LogTag = NULL;	/* string to tag the entry with */
     81   1.1       cgd static int	LogFacility = LOG_USER;	/* default facility code */
     82   1.1       cgd static int	LogMask = 0xff;		/* mask of priorities to be logged */
     83   1.1       cgd 
     84  1.20    kleink static void	openlog_unlocked __P((const char *, int, int));
     85  1.20    kleink static void	closelog_unlocked __P((void));
     86  1.20    kleink 
     87  1.20    kleink #ifdef _REENT
     88  1.20    kleink static mutex_t	syslog_mutex = MUTEX_INITIALIZER;
     89  1.20    kleink #endif
     90  1.20    kleink 
     91  1.19  christos #ifdef lint
     92  1.19  christos static const int ZERO = 0;
     93  1.19  christos #else
     94  1.19  christos #define ZERO	0
     95  1.19  christos #endif
     96  1.19  christos 
     97   1.1       cgd /*
     98   1.1       cgd  * syslog, vsyslog --
     99   1.1       cgd  *	print message on log file; output is intended for syslogd(8).
    100   1.1       cgd  */
    101   1.1       cgd void
    102   1.1       cgd #if __STDC__
    103   1.1       cgd syslog(int pri, const char *fmt, ...)
    104   1.1       cgd #else
    105   1.1       cgd syslog(pri, fmt, va_alist)
    106   1.1       cgd 	int pri;
    107   1.1       cgd 	char *fmt;
    108   1.1       cgd 	va_dcl
    109   1.1       cgd #endif
    110   1.1       cgd {
    111   1.1       cgd 	va_list ap;
    112   1.1       cgd 
    113   1.1       cgd #if __STDC__
    114   1.1       cgd 	va_start(ap, fmt);
    115   1.1       cgd #else
    116   1.1       cgd 	va_start(ap);
    117   1.1       cgd #endif
    118   1.1       cgd 	vsyslog(pri, fmt, ap);
    119   1.1       cgd 	va_end(ap);
    120   1.1       cgd }
    121   1.1       cgd 
    122   1.1       cgd void
    123   1.1       cgd vsyslog(pri, fmt, ap)
    124   1.1       cgd 	int pri;
    125  1.14     perry 	const char *fmt;
    126  1.25    martin 	_BSD_VA_LIST_ ap;
    127   1.1       cgd {
    128  1.19  christos 	size_t cnt;
    129  1.14     perry 	char ch, *p, *t;
    130   1.2     glass 	time_t now;
    131  1.18    kleink 	struct tm tmnow;
    132   1.1       cgd 	int fd, saved_errno;
    133  1.10   mycroft #define	TBUF_LEN	2048
    134  1.10   mycroft #define	FMT_LEN		1024
    135  1.11  christos 	char *stdp = NULL;	/* pacify gcc */
    136  1.11  christos 	char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
    137  1.16   mycroft 	size_t tbuf_left, fmt_left, prlen;
    138   1.2     glass 
    139   1.2     glass #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    140   1.2     glass 	/* Check for invalid bits. */
    141   1.2     glass 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    142   1.2     glass 		syslog(INTERNALLOG,
    143   1.2     glass 		    "syslog: unknown facility/priority: %x", pri);
    144   1.2     glass 		pri &= LOG_PRIMASK|LOG_FACMASK;
    145   1.2     glass 	}
    146   1.1       cgd 
    147   1.2     glass 	/* Check priority against setlogmask values. */
    148   1.8       cgd 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
    149   1.1       cgd 		return;
    150   1.1       cgd 
    151   1.1       cgd 	saved_errno = errno;
    152   1.1       cgd 
    153   1.6       cgd 	/* Set default facility if none specified. */
    154   1.1       cgd 	if ((pri & LOG_FACMASK) == 0)
    155   1.1       cgd 		pri |= LogFacility;
    156   1.1       cgd 
    157   1.6       cgd 	/* Build the message. */
    158   1.9       jtc 
    159  1.10   mycroft 	/*
    160   1.9       jtc  	 * Although it's tempting, we can't ignore the possibility of
    161   1.9       jtc 	 * overflowing the buffer when assembling the "fixed" portion
    162   1.9       jtc 	 * of the message.  Strftime's "%h" directive expands to the
    163   1.9       jtc 	 * locale's abbreviated month name, but if the user has the
    164   1.9       jtc 	 * ability to construct to his own locale files, it may be
    165   1.9       jtc 	 * arbitrarily long.
    166   1.9       jtc 	 */
    167   1.1       cgd 	(void)time(&now);
    168   1.9       jtc 
    169   1.9       jtc 	p = tbuf;
    170  1.10   mycroft 	tbuf_left = TBUF_LEN;
    171   1.9       jtc 
    172  1.19  christos #define	DEC()							\
    173  1.19  christos 	do {							\
    174  1.19  christos 		if (prlen >= tbuf_left)				\
    175  1.19  christos 			prlen = tbuf_left - 1;			\
    176  1.19  christos 		p += prlen;					\
    177  1.19  christos 		tbuf_left -= prlen;				\
    178  1.19  christos 	} while (ZERO)
    179  1.10   mycroft 
    180  1.10   mycroft 	prlen = snprintf(p, tbuf_left, "<%d>", pri);
    181   1.9       jtc 	DEC();
    182   1.9       jtc 
    183  1.18    kleink 	tzset(); /* strftime() implies tzset(), localtime_r() doesn't. */
    184  1.18    kleink 	prlen = strftime(p, tbuf_left, "%h %e %T ", localtime_r(&now, &tmnow));
    185   1.9       jtc 	DEC();
    186   1.9       jtc 
    187   1.1       cgd 	if (LogStat & LOG_PERROR)
    188   1.1       cgd 		stdp = p;
    189   1.6       cgd 	if (LogTag == NULL)
    190  1.26       cgd 		LogTag = getprogname();
    191   1.6       cgd 	if (LogTag != NULL) {
    192  1.10   mycroft 		prlen = snprintf(p, tbuf_left, "%s", LogTag);
    193   1.9       jtc 		DEC();
    194   1.9       jtc 	}
    195   1.9       jtc 	if (LogStat & LOG_PID) {
    196  1.10   mycroft 		prlen = snprintf(p, tbuf_left, "[%d]", getpid());
    197   1.9       jtc 		DEC();
    198   1.9       jtc 	}
    199   1.9       jtc 	if (LogTag != NULL) {
    200  1.10   mycroft 		if (tbuf_left > 1) {
    201   1.9       jtc 			*p++ = ':';
    202  1.10   mycroft 			tbuf_left--;
    203   1.9       jtc 		}
    204  1.10   mycroft 		if (tbuf_left > 1) {
    205   1.9       jtc 			*p++ = ' ';
    206  1.10   mycroft 			tbuf_left--;
    207   1.9       jtc 		}
    208   1.1       cgd 	}
    209   1.1       cgd 
    210   1.9       jtc 	/*
    211   1.9       jtc 	 * We wouldn't need this mess if printf handled %m, or if
    212   1.9       jtc 	 * strerror() had been invented before syslog().
    213   1.9       jtc 	 */
    214  1.11  christos 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
    215   1.6       cgd 		if (ch == '%' && fmt[1] == 'm') {
    216   1.6       cgd 			++fmt;
    217  1.10   mycroft 			prlen = snprintf(t, fmt_left, "%s",
    218  1.10   mycroft 			    strerror(saved_errno));
    219  1.10   mycroft 			if (prlen >= fmt_left)
    220  1.10   mycroft 				prlen = fmt_left - 1;
    221  1.10   mycroft 			t += prlen;
    222  1.10   mycroft 			fmt_left -= prlen;
    223   1.9       jtc 		} else {
    224  1.10   mycroft 			if (fmt_left > 1) {
    225   1.9       jtc 				*t++ = ch;
    226  1.10   mycroft 				fmt_left--;
    227   1.9       jtc 			}
    228   1.9       jtc 		}
    229   1.9       jtc 	}
    230   1.6       cgd 	*t = '\0';
    231   1.1       cgd 
    232  1.10   mycroft 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
    233   1.9       jtc 	DEC();
    234   1.6       cgd 	cnt = p - tbuf;
    235   1.1       cgd 
    236   1.6       cgd 	/* Output to stderr if requested. */
    237   1.1       cgd 	if (LogStat & LOG_PERROR) {
    238   1.1       cgd 		struct iovec iov[2];
    239   1.1       cgd 
    240  1.10   mycroft 		iov[0].iov_base = stdp;
    241  1.10   mycroft 		iov[0].iov_len = cnt - (stdp - tbuf);
    242  1.10   mycroft 		iov[1].iov_base = "\n";
    243  1.10   mycroft 		iov[1].iov_len = 1;
    244   1.1       cgd 		(void)writev(STDERR_FILENO, iov, 2);
    245   1.1       cgd 	}
    246   1.1       cgd 
    247   1.6       cgd 	/* Get connected, output the message to the local logger. */
    248  1.20    kleink 	mutex_lock(&syslog_mutex);
    249   1.1       cgd 	if (!connected)
    250  1.20    kleink 		openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
    251  1.20    kleink 	if (send(LogFile, tbuf, cnt, 0) >= 0) {
    252  1.20    kleink 		mutex_unlock(&syslog_mutex);
    253   1.1       cgd 		return;
    254  1.20    kleink 	}
    255  1.20    kleink 	mutex_unlock(&syslog_mutex);
    256   1.1       cgd 
    257   1.1       cgd 	/*
    258   1.1       cgd 	 * Output the message to the console; don't worry about blocking,
    259   1.1       cgd 	 * if console blocks everything will.  Make sure the error reported
    260   1.1       cgd 	 * is the one from the syslogd failure.
    261   1.1       cgd 	 */
    262   1.6       cgd 	if (LogStat & LOG_CONS &&
    263   1.6       cgd 	    (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
    264   1.9       jtc 		struct iovec iov[2];
    265   1.9       jtc 
    266   1.5       jtc 		p = strchr(tbuf, '>') + 1;
    267  1.10   mycroft 		iov[0].iov_base = p;
    268  1.10   mycroft 		iov[0].iov_len = cnt - (p - tbuf);
    269  1.10   mycroft 		iov[1].iov_base = "\r\n";
    270  1.10   mycroft 		iov[1].iov_len = 2;
    271   1.9       jtc 		(void)writev(fd, iov, 2);
    272   1.1       cgd 		(void)close(fd);
    273   1.1       cgd 	}
    274   1.1       cgd }
    275   1.1       cgd 
    276  1.21        is static struct sockaddr_un SyslogAddr;	/* AF_LOCAL address of local logger */
    277   1.1       cgd 
    278  1.20    kleink static void
    279  1.20    kleink openlog_unlocked(ident, logstat, logfac)
    280   1.1       cgd 	const char *ident;
    281   1.1       cgd 	int logstat, logfac;
    282   1.1       cgd {
    283  1.20    kleink 
    284   1.1       cgd 	if (ident != NULL)
    285   1.1       cgd 		LogTag = ident;
    286   1.1       cgd 	LogStat = logstat;
    287   1.1       cgd 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
    288   1.1       cgd 		LogFacility = logfac;
    289   1.1       cgd 
    290   1.1       cgd 	if (LogFile == -1) {
    291  1.21        is 		SyslogAddr.sun_family = AF_LOCAL;
    292  1.21        is 		(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
    293  1.21        is 		    sizeof(SyslogAddr.sun_path));
    294   1.1       cgd 		if (LogStat & LOG_NDELAY) {
    295  1.15     lukem 			if ((LogFile = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
    296   1.1       cgd 				return;
    297   1.1       cgd 			(void)fcntl(LogFile, F_SETFD, 1);
    298   1.1       cgd 		}
    299   1.1       cgd 	}
    300  1.17   thorpej 	if (LogFile != -1 && !connected) {
    301  1.23  christos 		if (connect(LogFile, (struct sockaddr *)(void *)&SyslogAddr,
    302  1.22        is 		    SUN_LEN(&SyslogAddr)) == -1) {
    303   1.1       cgd 			(void)close(LogFile);
    304   1.1       cgd 			LogFile = -1;
    305   1.1       cgd 		} else
    306   1.1       cgd 			connected = 1;
    307  1.17   thorpej 	}
    308   1.1       cgd }
    309   1.1       cgd 
    310   1.1       cgd void
    311  1.20    kleink openlog(ident, logstat, logfac)
    312  1.20    kleink 	const char *ident;
    313  1.20    kleink 	int logstat, logfac;
    314  1.20    kleink {
    315  1.20    kleink 
    316  1.20    kleink 	mutex_lock(&syslog_mutex);
    317  1.20    kleink 	openlog_unlocked(ident, logstat, logfac);
    318  1.20    kleink 	mutex_unlock(&syslog_mutex);
    319  1.20    kleink }
    320  1.20    kleink 
    321  1.20    kleink static void
    322  1.20    kleink closelog_unlocked()
    323   1.1       cgd {
    324   1.1       cgd 	(void)close(LogFile);
    325   1.1       cgd 	LogFile = -1;
    326   1.1       cgd 	connected = 0;
    327  1.20    kleink }
    328  1.20    kleink 
    329  1.20    kleink void
    330  1.20    kleink closelog()
    331  1.20    kleink {
    332  1.20    kleink 
    333  1.20    kleink 	mutex_lock(&syslog_mutex);
    334  1.20    kleink 	closelog_unlocked();
    335  1.20    kleink 	mutex_unlock(&syslog_mutex);
    336   1.1       cgd }
    337   1.1       cgd 
    338   1.1       cgd /* setlogmask -- set the log mask level */
    339   1.2     glass int
    340   1.1       cgd setlogmask(pmask)
    341   1.1       cgd 	int pmask;
    342   1.1       cgd {
    343   1.1       cgd 	int omask;
    344   1.1       cgd 
    345   1.1       cgd 	omask = LogMask;
    346   1.1       cgd 	if (pmask != 0)
    347   1.1       cgd 		LogMask = pmask;
    348   1.1       cgd 	return (omask);
    349   1.1       cgd }
    350