Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.9
      1  1.9    jtc /*	$NetBSD: syslog.c,v 1.9 1995/08/30 21:20:36 jtc 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.1    cgd #if defined(LIBC_SCCS) && !defined(lint)
     37  1.6    cgd #if 0
     38  1.6    cgd static char sccsid[] = "@(#)syslog.c	8.4 (Berkeley) 3/18/94";
     39  1.6    cgd #else
     40  1.9    jtc static char rcsid[] = "$NetBSD: syslog.c,v 1.9 1995/08/30 21:20:36 jtc Exp $";
     41  1.6    cgd #endif
     42  1.1    cgd #endif /* LIBC_SCCS and not lint */
     43  1.1    cgd 
     44  1.1    cgd #include <sys/types.h>
     45  1.1    cgd #include <sys/socket.h>
     46  1.1    cgd #include <sys/syslog.h>
     47  1.1    cgd #include <sys/uio.h>
     48  1.1    cgd #include <netdb.h>
     49  1.2  glass 
     50  1.2  glass #include <errno.h>
     51  1.2  glass #include <fcntl.h>
     52  1.2  glass #include <paths.h>
     53  1.2  glass #include <stdio.h>
     54  1.1    cgd #include <string.h>
     55  1.2  glass #include <time.h>
     56  1.2  glass #include <unistd.h>
     57  1.2  glass 
     58  1.1    cgd #if __STDC__
     59  1.1    cgd #include <stdarg.h>
     60  1.1    cgd #else
     61  1.1    cgd #include <varargs.h>
     62  1.1    cgd #endif
     63  1.1    cgd 
     64  1.1    cgd static int	LogFile = -1;		/* fd for log */
     65  1.1    cgd static int	connected;		/* have done connect */
     66  1.1    cgd static int	LogStat = 0;		/* status bits, set by openlog() */
     67  1.6    cgd static const char *LogTag = NULL;	/* string to tag the entry with */
     68  1.1    cgd static int	LogFacility = LOG_USER;	/* default facility code */
     69  1.1    cgd static int	LogMask = 0xff;		/* mask of priorities to be logged */
     70  1.6    cgd extern char	*__progname;		/* Program name, from crt0. */
     71  1.1    cgd 
     72  1.1    cgd /*
     73  1.1    cgd  * syslog, vsyslog --
     74  1.1    cgd  *	print message on log file; output is intended for syslogd(8).
     75  1.1    cgd  */
     76  1.1    cgd void
     77  1.1    cgd #if __STDC__
     78  1.1    cgd syslog(int pri, const char *fmt, ...)
     79  1.1    cgd #else
     80  1.1    cgd syslog(pri, fmt, va_alist)
     81  1.1    cgd 	int pri;
     82  1.1    cgd 	char *fmt;
     83  1.1    cgd 	va_dcl
     84  1.1    cgd #endif
     85  1.1    cgd {
     86  1.1    cgd 	va_list ap;
     87  1.1    cgd 
     88  1.1    cgd #if __STDC__
     89  1.1    cgd 	va_start(ap, fmt);
     90  1.1    cgd #else
     91  1.1    cgd 	va_start(ap);
     92  1.1    cgd #endif
     93  1.1    cgd 	vsyslog(pri, fmt, ap);
     94  1.1    cgd 	va_end(ap);
     95  1.1    cgd }
     96  1.1    cgd 
     97  1.1    cgd void
     98  1.1    cgd vsyslog(pri, fmt, ap)
     99  1.1    cgd 	int pri;
    100  1.1    cgd 	register const char *fmt;
    101  1.1    cgd 	va_list ap;
    102  1.1    cgd {
    103  1.1    cgd 	register int cnt;
    104  1.6    cgd 	register char ch, *p, *t;
    105  1.2  glass 	time_t now;
    106  1.1    cgd 	int fd, saved_errno;
    107  1.2  glass 	char *stdp, tbuf[2048], fmt_cpy[1024];
    108  1.9    jtc 	int tbuf_len, fmt_len, prlen;
    109  1.2  glass 
    110  1.2  glass #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    111  1.2  glass 	/* Check for invalid bits. */
    112  1.2  glass 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    113  1.2  glass 		syslog(INTERNALLOG,
    114  1.2  glass 		    "syslog: unknown facility/priority: %x", pri);
    115  1.2  glass 		pri &= LOG_PRIMASK|LOG_FACMASK;
    116  1.2  glass 	}
    117  1.1    cgd 
    118  1.2  glass 	/* Check priority against setlogmask values. */
    119  1.8    cgd 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
    120  1.1    cgd 		return;
    121  1.1    cgd 
    122  1.1    cgd 	saved_errno = errno;
    123  1.1    cgd 
    124  1.6    cgd 	/* Set default facility if none specified. */
    125  1.1    cgd 	if ((pri & LOG_FACMASK) == 0)
    126  1.1    cgd 		pri |= LogFacility;
    127  1.1    cgd 
    128  1.6    cgd 	/* Build the message. */
    129  1.9    jtc 
    130  1.9    jtc         /*
    131  1.9    jtc  	 * Although it's tempting, we can't ignore the possibility of
    132  1.9    jtc 	 * overflowing the buffer when assembling the "fixed" portion
    133  1.9    jtc 	 * of the message.  Strftime's "%h" directive expands to the
    134  1.9    jtc 	 * locale's abbreviated month name, but if the user has the
    135  1.9    jtc 	 * ability to construct to his own locale files, it may be
    136  1.9    jtc 	 * arbitrarily long.
    137  1.9    jtc 	 */
    138  1.1    cgd 	(void)time(&now);
    139  1.9    jtc 
    140  1.9    jtc #define DEC()				        \
    141  1.9    jtc 	if (tbuf_len > prlen)			\
    142  1.9    jtc 		tbuf_len -= prlen;		\
    143  1.9    jtc 	else					\
    144  1.9    jtc 		tbuf_len = 0;			\
    145  1.9    jtc 	p = tbuf + sizeof(tbuf) - tbuf_len;
    146  1.9    jtc 
    147  1.9    jtc 	p = tbuf;
    148  1.9    jtc         tbuf_len = sizeof(tbuf);
    149  1.9    jtc 
    150  1.9    jtc 	prlen = sprintf(p, "<%d>", pri);
    151  1.9    jtc 	DEC();
    152  1.9    jtc 
    153  1.9    jtc 	prlen = strftime(p, tbuf_len, "%h %e %T ", localtime(&now));
    154  1.9    jtc 	DEC();
    155  1.9    jtc 
    156  1.1    cgd 	if (LogStat & LOG_PERROR)
    157  1.1    cgd 		stdp = p;
    158  1.6    cgd 	if (LogTag == NULL)
    159  1.6    cgd 		LogTag = __progname;
    160  1.6    cgd 	if (LogTag != NULL) {
    161  1.9    jtc 		prlen = snprintf(p, tbuf_len, "%s", LogTag);
    162  1.9    jtc 		DEC();
    163  1.9    jtc 	}
    164  1.9    jtc 	if (LogStat & LOG_PID) {
    165  1.9    jtc 		prlen = snprintf(p, tbuf_len, "[%d]", getpid());
    166  1.9    jtc 		DEC();
    167  1.9    jtc 	}
    168  1.9    jtc 	if (LogTag != NULL) {
    169  1.9    jtc 		if (tbuf_len > 1) {
    170  1.9    jtc 			*p++ = ':';
    171  1.9    jtc 			tbuf_len--;
    172  1.9    jtc 		}
    173  1.9    jtc 		if (tbuf_len > 1) {
    174  1.9    jtc 			*p++ = ' ';
    175  1.9    jtc 			tbuf_len--;
    176  1.9    jtc 		}
    177  1.1    cgd 	}
    178  1.1    cgd 
    179  1.9    jtc 	/*
    180  1.9    jtc 	 * We wouldn't need this mess if printf handled %m, or if
    181  1.9    jtc 	 * strerror() had been invented before syslog().
    182  1.9    jtc 	 */
    183  1.9    jtc 	for (t = fmt_cpy, fmt_len = sizeof(fmt_cpy); ch = *fmt; ++fmt) {
    184  1.6    cgd 		if (ch == '%' && fmt[1] == 'm') {
    185  1.6    cgd 			++fmt;
    186  1.9    jtc 			prlen = snprintf(t, fmt_len,
    187  1.9    jtc 					 "%s", strerror(saved_errno));
    188  1.9    jtc 			if (fmt_len > prlen) {
    189  1.9    jtc 				t += prlen;
    190  1.9    jtc 				fmt_len -= prlen;
    191  1.9    jtc 			} else {
    192  1.9    jtc 				t = fmt_cpy + sizeof(fmt_cpy) - 1;
    193  1.9    jtc 				break;
    194  1.9    jtc 			}
    195  1.9    jtc 		} else {
    196  1.9    jtc 			if (fmt_len > 1) {
    197  1.9    jtc 				*t++ = ch;
    198  1.9    jtc 				fmt_len--;
    199  1.9    jtc 			} else {
    200  1.9    jtc 				break;
    201  1.9    jtc 			}
    202  1.9    jtc 		}
    203  1.9    jtc 	}
    204  1.6    cgd 	*t = '\0';
    205  1.1    cgd 
    206  1.9    jtc 	prlen = vsnprintf(p, tbuf_len, fmt_cpy, ap);
    207  1.9    jtc 	DEC();
    208  1.6    cgd 	cnt = p - tbuf;
    209  1.1    cgd 
    210  1.6    cgd 	/* Output to stderr if requested. */
    211  1.1    cgd 	if (LogStat & LOG_PERROR) {
    212  1.1    cgd 		struct iovec iov[2];
    213  1.1    cgd 		register struct iovec *v = iov;
    214  1.1    cgd 
    215  1.1    cgd 		v->iov_base = stdp;
    216  1.1    cgd 		v->iov_len = cnt - (stdp - tbuf);
    217  1.1    cgd 		++v;
    218  1.1    cgd 		v->iov_base = "\n";
    219  1.1    cgd 		v->iov_len = 1;
    220  1.1    cgd 		(void)writev(STDERR_FILENO, iov, 2);
    221  1.1    cgd 	}
    222  1.1    cgd 
    223  1.6    cgd 	/* Get connected, output the message to the local logger. */
    224  1.1    cgd 	if (!connected)
    225  1.1    cgd 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
    226  1.1    cgd 	if (send(LogFile, tbuf, cnt, 0) >= 0)
    227  1.1    cgd 		return;
    228  1.1    cgd 
    229  1.1    cgd 	/*
    230  1.1    cgd 	 * Output the message to the console; don't worry about blocking,
    231  1.1    cgd 	 * if console blocks everything will.  Make sure the error reported
    232  1.1    cgd 	 * is the one from the syslogd failure.
    233  1.1    cgd 	 */
    234  1.6    cgd 	if (LogStat & LOG_CONS &&
    235  1.6    cgd 	    (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
    236  1.9    jtc 		struct iovec iov[2];
    237  1.9    jtc 		register struct iovec *v = iov;
    238  1.9    jtc 
    239  1.5    jtc 		p = strchr(tbuf, '>') + 1;
    240  1.9    jtc 		v->iov_base = p;
    241  1.9    jtc 		v->iov_len = cnt - (p - tbuf);
    242  1.9    jtc 		++v;
    243  1.9    jtc 		v->iov_base = "\r\n";
    244  1.9    jtc 		v->iov_len = 2;
    245  1.9    jtc 		(void)writev(fd, iov, 2);
    246  1.1    cgd 		(void)close(fd);
    247  1.1    cgd 	}
    248  1.1    cgd }
    249  1.1    cgd 
    250  1.1    cgd static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
    251  1.1    cgd 
    252  1.1    cgd void
    253  1.1    cgd openlog(ident, logstat, logfac)
    254  1.1    cgd 	const char *ident;
    255  1.1    cgd 	int logstat, logfac;
    256  1.1    cgd {
    257  1.1    cgd 	if (ident != NULL)
    258  1.1    cgd 		LogTag = ident;
    259  1.1    cgd 	LogStat = logstat;
    260  1.1    cgd 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
    261  1.1    cgd 		LogFacility = logfac;
    262  1.1    cgd 
    263  1.1    cgd 	if (LogFile == -1) {
    264  1.1    cgd 		SyslogAddr.sa_family = AF_UNIX;
    265  1.1    cgd 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
    266  1.1    cgd 		    sizeof(SyslogAddr.sa_data));
    267  1.1    cgd 		if (LogStat & LOG_NDELAY) {
    268  1.1    cgd 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    269  1.1    cgd 				return;
    270  1.1    cgd 			(void)fcntl(LogFile, F_SETFD, 1);
    271  1.1    cgd 		}
    272  1.1    cgd 	}
    273  1.1    cgd 	if (LogFile != -1 && !connected)
    274  1.1    cgd 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
    275  1.1    cgd 			(void)close(LogFile);
    276  1.1    cgd 			LogFile = -1;
    277  1.1    cgd 		} else
    278  1.1    cgd 			connected = 1;
    279  1.1    cgd }
    280  1.1    cgd 
    281  1.1    cgd void
    282  1.1    cgd closelog()
    283  1.1    cgd {
    284  1.1    cgd 	(void)close(LogFile);
    285  1.1    cgd 	LogFile = -1;
    286  1.1    cgd 	connected = 0;
    287  1.1    cgd }
    288  1.1    cgd 
    289  1.1    cgd /* setlogmask -- set the log mask level */
    290  1.2  glass int
    291  1.1    cgd setlogmask(pmask)
    292  1.1    cgd 	int pmask;
    293  1.1    cgd {
    294  1.1    cgd 	int omask;
    295  1.1    cgd 
    296  1.1    cgd 	omask = LogMask;
    297  1.1    cgd 	if (pmask != 0)
    298  1.1    cgd 		LogMask = pmask;
    299  1.1    cgd 	return (omask);
    300  1.1    cgd }
    301