Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.31
      1  1.31  christos /*	$NetBSD: syslog.c,v 1.31 2005/11/29 03:11:59 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.31  christos __RCSID("$NetBSD: syslog.c,v 1.31 2005/11/29 03:11:59 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.1       cgd #include <sys/socket.h>
     44   1.1       cgd #include <sys/syslog.h>
     45   1.1       cgd #include <sys/uio.h>
     46  1.21        is #include <sys/un.h>
     47   1.1       cgd #include <netdb.h>
     48   1.2     glass 
     49   1.2     glass #include <errno.h>
     50   1.2     glass #include <fcntl.h>
     51   1.2     glass #include <paths.h>
     52  1.28       wiz #include <stdarg.h>
     53   1.2     glass #include <stdio.h>
     54  1.26       cgd #include <stdlib.h>
     55   1.1       cgd #include <string.h>
     56   1.2     glass #include <time.h>
     57   1.2     glass #include <unistd.h>
     58  1.20    kleink #include "reentrant.h"
     59   1.2     glass 
     60  1.12       jtc #ifdef __weak_alias
     61  1.24   mycroft __weak_alias(closelog,_closelog)
     62  1.24   mycroft __weak_alias(openlog,_openlog)
     63  1.24   mycroft __weak_alias(setlogmask,_setlogmask)
     64  1.24   mycroft __weak_alias(syslog,_syslog)
     65  1.24   mycroft __weak_alias(vsyslog,_vsyslog)
     66   1.1       cgd #endif
     67   1.1       cgd 
     68   1.1       cgd static int	LogFile = -1;		/* fd for log */
     69   1.1       cgd static int	connected;		/* have done connect */
     70   1.1       cgd static int	LogStat = 0;		/* status bits, set by openlog() */
     71   1.6       cgd static const char *LogTag = NULL;	/* string to tag the entry with */
     72   1.1       cgd static int	LogFacility = LOG_USER;	/* default facility code */
     73   1.1       cgd static int	LogMask = 0xff;		/* mask of priorities to be logged */
     74   1.1       cgd 
     75  1.20    kleink static void	openlog_unlocked __P((const char *, int, int));
     76  1.20    kleink static void	closelog_unlocked __P((void));
     77  1.20    kleink 
     78  1.29   thorpej #ifdef _REENTRANT
     79  1.20    kleink static mutex_t	syslog_mutex = MUTEX_INITIALIZER;
     80  1.20    kleink #endif
     81  1.20    kleink 
     82  1.19  christos #ifdef lint
     83  1.19  christos static const int ZERO = 0;
     84  1.19  christos #else
     85  1.19  christos #define ZERO	0
     86  1.19  christos #endif
     87  1.19  christos 
     88   1.1       cgd /*
     89   1.1       cgd  * syslog, vsyslog --
     90   1.1       cgd  *	print message on log file; output is intended for syslogd(8).
     91   1.1       cgd  */
     92   1.1       cgd void
     93   1.1       cgd syslog(int pri, const char *fmt, ...)
     94   1.1       cgd {
     95   1.1       cgd 	va_list ap;
     96   1.1       cgd 
     97   1.1       cgd 	va_start(ap, fmt);
     98   1.1       cgd 	vsyslog(pri, fmt, ap);
     99   1.1       cgd 	va_end(ap);
    100   1.1       cgd }
    101   1.1       cgd 
    102   1.1       cgd void
    103   1.1       cgd vsyslog(pri, fmt, ap)
    104   1.1       cgd 	int pri;
    105  1.14     perry 	const char *fmt;
    106  1.25    martin 	_BSD_VA_LIST_ ap;
    107   1.1       cgd {
    108  1.19  christos 	size_t cnt;
    109  1.14     perry 	char ch, *p, *t;
    110   1.2     glass 	time_t now;
    111  1.18    kleink 	struct tm tmnow;
    112  1.27    atatat 	int fd, saved_errno, tries;
    113  1.10   mycroft #define	TBUF_LEN	2048
    114  1.10   mycroft #define	FMT_LEN		1024
    115  1.11  christos 	char *stdp = NULL;	/* pacify gcc */
    116  1.11  christos 	char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
    117  1.16   mycroft 	size_t tbuf_left, fmt_left, prlen;
    118   1.2     glass 
    119   1.2     glass #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    120   1.2     glass 	/* Check for invalid bits. */
    121   1.2     glass 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    122   1.2     glass 		syslog(INTERNALLOG,
    123   1.2     glass 		    "syslog: unknown facility/priority: %x", pri);
    124   1.2     glass 		pri &= LOG_PRIMASK|LOG_FACMASK;
    125   1.2     glass 	}
    126   1.1       cgd 
    127   1.2     glass 	/* Check priority against setlogmask values. */
    128   1.8       cgd 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
    129   1.1       cgd 		return;
    130   1.1       cgd 
    131   1.1       cgd 	saved_errno = errno;
    132   1.1       cgd 
    133   1.6       cgd 	/* Set default facility if none specified. */
    134   1.1       cgd 	if ((pri & LOG_FACMASK) == 0)
    135   1.1       cgd 		pri |= LogFacility;
    136   1.1       cgd 
    137   1.6       cgd 	/* Build the message. */
    138   1.9       jtc 
    139  1.10   mycroft 	/*
    140   1.9       jtc  	 * Although it's tempting, we can't ignore the possibility of
    141   1.9       jtc 	 * overflowing the buffer when assembling the "fixed" portion
    142   1.9       jtc 	 * of the message.  Strftime's "%h" directive expands to the
    143   1.9       jtc 	 * locale's abbreviated month name, but if the user has the
    144   1.9       jtc 	 * ability to construct to his own locale files, it may be
    145   1.9       jtc 	 * arbitrarily long.
    146   1.9       jtc 	 */
    147   1.1       cgd 	(void)time(&now);
    148   1.9       jtc 
    149   1.9       jtc 	p = tbuf;
    150  1.10   mycroft 	tbuf_left = TBUF_LEN;
    151   1.9       jtc 
    152  1.19  christos #define	DEC()							\
    153  1.19  christos 	do {							\
    154  1.19  christos 		if (prlen >= tbuf_left)				\
    155  1.19  christos 			prlen = tbuf_left - 1;			\
    156  1.19  christos 		p += prlen;					\
    157  1.19  christos 		tbuf_left -= prlen;				\
    158  1.19  christos 	} while (ZERO)
    159  1.10   mycroft 
    160  1.10   mycroft 	prlen = snprintf(p, tbuf_left, "<%d>", pri);
    161   1.9       jtc 	DEC();
    162   1.9       jtc 
    163  1.18    kleink 	tzset(); /* strftime() implies tzset(), localtime_r() doesn't. */
    164  1.18    kleink 	prlen = strftime(p, tbuf_left, "%h %e %T ", localtime_r(&now, &tmnow));
    165   1.9       jtc 	DEC();
    166   1.9       jtc 
    167   1.1       cgd 	if (LogStat & LOG_PERROR)
    168   1.1       cgd 		stdp = p;
    169   1.6       cgd 	if (LogTag == NULL)
    170  1.26       cgd 		LogTag = getprogname();
    171   1.6       cgd 	if (LogTag != NULL) {
    172  1.10   mycroft 		prlen = snprintf(p, tbuf_left, "%s", LogTag);
    173   1.9       jtc 		DEC();
    174   1.9       jtc 	}
    175   1.9       jtc 	if (LogStat & LOG_PID) {
    176  1.10   mycroft 		prlen = snprintf(p, tbuf_left, "[%d]", getpid());
    177   1.9       jtc 		DEC();
    178   1.9       jtc 	}
    179   1.9       jtc 	if (LogTag != NULL) {
    180  1.10   mycroft 		if (tbuf_left > 1) {
    181   1.9       jtc 			*p++ = ':';
    182  1.10   mycroft 			tbuf_left--;
    183   1.9       jtc 		}
    184  1.10   mycroft 		if (tbuf_left > 1) {
    185   1.9       jtc 			*p++ = ' ';
    186  1.10   mycroft 			tbuf_left--;
    187   1.9       jtc 		}
    188   1.1       cgd 	}
    189   1.1       cgd 
    190   1.9       jtc 	/*
    191   1.9       jtc 	 * We wouldn't need this mess if printf handled %m, or if
    192   1.9       jtc 	 * strerror() had been invented before syslog().
    193   1.9       jtc 	 */
    194  1.11  christos 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
    195   1.6       cgd 		if (ch == '%' && fmt[1] == 'm') {
    196   1.6       cgd 			++fmt;
    197  1.10   mycroft 			prlen = snprintf(t, fmt_left, "%s",
    198  1.10   mycroft 			    strerror(saved_errno));
    199  1.10   mycroft 			if (prlen >= fmt_left)
    200  1.10   mycroft 				prlen = fmt_left - 1;
    201  1.10   mycroft 			t += prlen;
    202  1.10   mycroft 			fmt_left -= prlen;
    203   1.9       jtc 		} else {
    204  1.10   mycroft 			if (fmt_left > 1) {
    205   1.9       jtc 				*t++ = ch;
    206  1.10   mycroft 				fmt_left--;
    207   1.9       jtc 			}
    208   1.9       jtc 		}
    209   1.9       jtc 	}
    210   1.6       cgd 	*t = '\0';
    211   1.1       cgd 
    212  1.10   mycroft 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
    213   1.9       jtc 	DEC();
    214   1.6       cgd 	cnt = p - tbuf;
    215   1.1       cgd 
    216   1.6       cgd 	/* Output to stderr if requested. */
    217   1.1       cgd 	if (LogStat & LOG_PERROR) {
    218   1.1       cgd 		struct iovec iov[2];
    219   1.1       cgd 
    220  1.10   mycroft 		iov[0].iov_base = stdp;
    221  1.10   mycroft 		iov[0].iov_len = cnt - (stdp - tbuf);
    222  1.31  christos 		iov[1].iov_base = __UNCONST("\n");
    223  1.10   mycroft 		iov[1].iov_len = 1;
    224   1.1       cgd 		(void)writev(STDERR_FILENO, iov, 2);
    225   1.1       cgd 	}
    226   1.1       cgd 
    227   1.6       cgd 	/* Get connected, output the message to the local logger. */
    228  1.20    kleink 	mutex_lock(&syslog_mutex);
    229  1.27    atatat 	/*
    230  1.27    atatat 	 * Try to send it twice.  If the first try fails, we might
    231  1.27    atatat 	 * be able to simply reconnect and send the message (which
    232  1.27    atatat 	 * means syslogd was restarted since we connected the first
    233  1.27    atatat 	 * time).  If the second attempt doesn't work, then something
    234  1.27    atatat 	 * else is wrong and there's probably not much we can do
    235  1.27    atatat 	 * here.
    236  1.27    atatat 	 */
    237  1.27    atatat 	for (tries = 1; tries <= 2; tries++) {
    238  1.27    atatat 		if (!connected)
    239  1.27    atatat 			openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
    240  1.27    atatat 		if (send(LogFile, tbuf, cnt, 0) >= 0) {
    241  1.27    atatat 			mutex_unlock(&syslog_mutex);
    242  1.27    atatat 			return;
    243  1.27    atatat 		}
    244  1.27    atatat 		else
    245  1.27    atatat 			closelog_unlocked();
    246  1.27    atatat 	}
    247  1.20    kleink 	mutex_unlock(&syslog_mutex);
    248   1.1       cgd 
    249   1.1       cgd 	/*
    250   1.1       cgd 	 * Output the message to the console; don't worry about blocking,
    251   1.1       cgd 	 * if console blocks everything will.  Make sure the error reported
    252   1.1       cgd 	 * is the one from the syslogd failure.
    253   1.1       cgd 	 */
    254   1.6       cgd 	if (LogStat & LOG_CONS &&
    255   1.6       cgd 	    (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
    256   1.9       jtc 		struct iovec iov[2];
    257   1.9       jtc 
    258   1.5       jtc 		p = strchr(tbuf, '>') + 1;
    259  1.10   mycroft 		iov[0].iov_base = p;
    260  1.10   mycroft 		iov[0].iov_len = cnt - (p - tbuf);
    261  1.31  christos 		iov[1].iov_base = __UNCONST("\r\n");
    262  1.10   mycroft 		iov[1].iov_len = 2;
    263   1.9       jtc 		(void)writev(fd, iov, 2);
    264   1.1       cgd 		(void)close(fd);
    265   1.1       cgd 	}
    266   1.1       cgd }
    267   1.1       cgd 
    268  1.21        is static struct sockaddr_un SyslogAddr;	/* AF_LOCAL address of local logger */
    269   1.1       cgd 
    270  1.20    kleink static void
    271  1.20    kleink openlog_unlocked(ident, logstat, logfac)
    272   1.1       cgd 	const char *ident;
    273   1.1       cgd 	int logstat, logfac;
    274   1.1       cgd {
    275  1.20    kleink 
    276   1.1       cgd 	if (ident != NULL)
    277   1.1       cgd 		LogTag = ident;
    278   1.1       cgd 	LogStat = logstat;
    279   1.1       cgd 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
    280   1.1       cgd 		LogFacility = logfac;
    281   1.1       cgd 
    282   1.1       cgd 	if (LogFile == -1) {
    283  1.21        is 		SyslogAddr.sun_family = AF_LOCAL;
    284  1.21        is 		(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
    285  1.21        is 		    sizeof(SyslogAddr.sun_path));
    286   1.1       cgd 		if (LogStat & LOG_NDELAY) {
    287  1.15     lukem 			if ((LogFile = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
    288   1.1       cgd 				return;
    289   1.1       cgd 			(void)fcntl(LogFile, F_SETFD, 1);
    290   1.1       cgd 		}
    291   1.1       cgd 	}
    292  1.17   thorpej 	if (LogFile != -1 && !connected) {
    293  1.23  christos 		if (connect(LogFile, (struct sockaddr *)(void *)&SyslogAddr,
    294  1.22        is 		    SUN_LEN(&SyslogAddr)) == -1) {
    295   1.1       cgd 			(void)close(LogFile);
    296   1.1       cgd 			LogFile = -1;
    297   1.1       cgd 		} else
    298   1.1       cgd 			connected = 1;
    299  1.17   thorpej 	}
    300   1.1       cgd }
    301   1.1       cgd 
    302   1.1       cgd void
    303  1.20    kleink openlog(ident, logstat, logfac)
    304  1.20    kleink 	const char *ident;
    305  1.20    kleink 	int logstat, logfac;
    306  1.20    kleink {
    307  1.20    kleink 
    308  1.20    kleink 	mutex_lock(&syslog_mutex);
    309  1.20    kleink 	openlog_unlocked(ident, logstat, logfac);
    310  1.20    kleink 	mutex_unlock(&syslog_mutex);
    311  1.20    kleink }
    312  1.20    kleink 
    313  1.20    kleink static void
    314  1.20    kleink closelog_unlocked()
    315   1.1       cgd {
    316   1.1       cgd 	(void)close(LogFile);
    317   1.1       cgd 	LogFile = -1;
    318   1.1       cgd 	connected = 0;
    319  1.20    kleink }
    320  1.20    kleink 
    321  1.20    kleink void
    322  1.20    kleink closelog()
    323  1.20    kleink {
    324  1.20    kleink 
    325  1.20    kleink 	mutex_lock(&syslog_mutex);
    326  1.20    kleink 	closelog_unlocked();
    327  1.20    kleink 	mutex_unlock(&syslog_mutex);
    328   1.1       cgd }
    329   1.1       cgd 
    330   1.1       cgd /* setlogmask -- set the log mask level */
    331   1.2     glass int
    332   1.1       cgd setlogmask(pmask)
    333   1.1       cgd 	int pmask;
    334   1.1       cgd {
    335   1.1       cgd 	int omask;
    336   1.1       cgd 
    337   1.1       cgd 	omask = LogMask;
    338   1.1       cgd 	if (pmask != 0)
    339   1.1       cgd 		LogMask = pmask;
    340   1.1       cgd 	return (omask);
    341   1.1       cgd }
    342