Home | History | Annotate | Line # | Download | only in gen
syslog.c revision 1.10.4.1
      1 /*	$NetBSD: syslog.c,v 1.10.4.1 1996/09/19 20:04:01 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #if defined(LIBC_SCCS) && !defined(lint)
     37 #if 0
     38 static char sccsid[] = "@(#)syslog.c	8.4 (Berkeley) 3/18/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: syslog.c,v 1.10.4.1 1996/09/19 20:04:01 jtc Exp $";
     41 #endif
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #include "namespace.h"
     45 #include <sys/types.h>
     46 #include <sys/socket.h>
     47 #include <sys/syslog.h>
     48 #include <sys/uio.h>
     49 #include <netdb.h>
     50 
     51 #include <errno.h>
     52 #include <fcntl.h>
     53 #include <paths.h>
     54 #include <stdio.h>
     55 #include <string.h>
     56 #include <time.h>
     57 #include <unistd.h>
     58 
     59 #if __STDC__
     60 #include <stdarg.h>
     61 #else
     62 #include <varargs.h>
     63 #endif
     64 
     65 #ifdef __weak_alias
     66 __weak_alias(closelog,_closelog);
     67 __weak_alias(openlog,_openlog);
     68 __weak_alias(setlogmask,_setlogmask);
     69 __weak_alias(syslog,_syslog);
     70 #endif
     71 
     72 static int	LogFile = -1;		/* fd for log */
     73 static int	connected;		/* have done connect */
     74 static int	LogStat = 0;		/* status bits, set by openlog() */
     75 static const char *LogTag = NULL;	/* string to tag the entry with */
     76 static int	LogFacility = LOG_USER;	/* default facility code */
     77 static int	LogMask = 0xff;		/* mask of priorities to be logged */
     78 extern char	*__progname;		/* Program name, from crt0. */
     79 
     80 /*
     81  * syslog, vsyslog --
     82  *	print message on log file; output is intended for syslogd(8).
     83  */
     84 void
     85 #if __STDC__
     86 syslog(int pri, const char *fmt, ...)
     87 #else
     88 syslog(pri, fmt, va_alist)
     89 	int pri;
     90 	char *fmt;
     91 	va_dcl
     92 #endif
     93 {
     94 	va_list ap;
     95 
     96 #if __STDC__
     97 	va_start(ap, fmt);
     98 #else
     99 	va_start(ap);
    100 #endif
    101 	vsyslog(pri, fmt, ap);
    102 	va_end(ap);
    103 }
    104 
    105 void
    106 vsyslog(pri, fmt, ap)
    107 	int pri;
    108 	register const char *fmt;
    109 	va_list ap;
    110 {
    111 	register int cnt;
    112 	register char ch, *p, *t;
    113 	time_t now;
    114 	int fd, saved_errno;
    115 #define	TBUF_LEN	2048
    116 #define	FMT_LEN		1024
    117 	char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
    118 	int tbuf_left, fmt_left, prlen;
    119 
    120 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    121 	/* Check for invalid bits. */
    122 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    123 		syslog(INTERNALLOG,
    124 		    "syslog: unknown facility/priority: %x", pri);
    125 		pri &= LOG_PRIMASK|LOG_FACMASK;
    126 	}
    127 
    128 	/* Check priority against setlogmask values. */
    129 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
    130 		return;
    131 
    132 	saved_errno = errno;
    133 
    134 	/* Set default facility if none specified. */
    135 	if ((pri & LOG_FACMASK) == 0)
    136 		pri |= LogFacility;
    137 
    138 	/* Build the message. */
    139 
    140 	/*
    141  	 * Although it's tempting, we can't ignore the possibility of
    142 	 * overflowing the buffer when assembling the "fixed" portion
    143 	 * of the message.  Strftime's "%h" directive expands to the
    144 	 * locale's abbreviated month name, but if the user has the
    145 	 * ability to construct to his own locale files, it may be
    146 	 * arbitrarily long.
    147 	 */
    148 	(void)time(&now);
    149 
    150 	p = tbuf;
    151 	tbuf_left = TBUF_LEN;
    152 
    153 #define	DEC()	\
    154 	do {					\
    155 		if (prlen >= tbuf_left)		\
    156 			prlen = tbuf_left - 1;	\
    157 		p += prlen;			\
    158 		tbuf_left -= prlen;		\
    159 	} while (0)
    160 
    161 	prlen = snprintf(p, tbuf_left, "<%d>", pri);
    162 	DEC();
    163 
    164 	prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now));
    165 	DEC();
    166 
    167 	if (LogStat & LOG_PERROR)
    168 		stdp = p;
    169 	if (LogTag == NULL)
    170 		LogTag = __progname;
    171 	if (LogTag != NULL) {
    172 		prlen = snprintf(p, tbuf_left, "%s", LogTag);
    173 		DEC();
    174 	}
    175 	if (LogStat & LOG_PID) {
    176 		prlen = snprintf(p, tbuf_left, "[%d]", getpid());
    177 		DEC();
    178 	}
    179 	if (LogTag != NULL) {
    180 		if (tbuf_left > 1) {
    181 			*p++ = ':';
    182 			tbuf_left--;
    183 		}
    184 		if (tbuf_left > 1) {
    185 			*p++ = ' ';
    186 			tbuf_left--;
    187 		}
    188 	}
    189 
    190 	/*
    191 	 * We wouldn't need this mess if printf handled %m, or if
    192 	 * strerror() had been invented before syslog().
    193 	 */
    194 	for (t = fmt_cpy, fmt_left = FMT_LEN; ch = *fmt; ++fmt) {
    195 		if (ch == '%' && fmt[1] == 'm') {
    196 			++fmt;
    197 			prlen = snprintf(t, fmt_left, "%s",
    198 			    strerror(saved_errno));
    199 			if (prlen >= fmt_left)
    200 				prlen = fmt_left - 1;
    201 			t += prlen;
    202 			fmt_left -= prlen;
    203 		} else {
    204 			if (fmt_left > 1) {
    205 				*t++ = ch;
    206 				fmt_left--;
    207 			}
    208 		}
    209 	}
    210 	*t = '\0';
    211 
    212 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
    213 	DEC();
    214 	cnt = p - tbuf;
    215 
    216 	/* Output to stderr if requested. */
    217 	if (LogStat & LOG_PERROR) {
    218 		struct iovec iov[2];
    219 
    220 		iov[0].iov_base = stdp;
    221 		iov[0].iov_len = cnt - (stdp - tbuf);
    222 		iov[1].iov_base = "\n";
    223 		iov[1].iov_len = 1;
    224 		(void)writev(STDERR_FILENO, iov, 2);
    225 	}
    226 
    227 	/* Get connected, output the message to the local logger. */
    228 	if (!connected)
    229 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
    230 	if (send(LogFile, tbuf, cnt, 0) >= 0)
    231 		return;
    232 
    233 	/*
    234 	 * Output the message to the console; don't worry about blocking,
    235 	 * if console blocks everything will.  Make sure the error reported
    236 	 * is the one from the syslogd failure.
    237 	 */
    238 	if (LogStat & LOG_CONS &&
    239 	    (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
    240 		struct iovec iov[2];
    241 
    242 		p = strchr(tbuf, '>') + 1;
    243 		iov[0].iov_base = p;
    244 		iov[0].iov_len = cnt - (p - tbuf);
    245 		iov[1].iov_base = "\r\n";
    246 		iov[1].iov_len = 2;
    247 		(void)writev(fd, iov, 2);
    248 		(void)close(fd);
    249 	}
    250 }
    251 
    252 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
    253 
    254 void
    255 openlog(ident, logstat, logfac)
    256 	const char *ident;
    257 	int logstat, logfac;
    258 {
    259 	if (ident != NULL)
    260 		LogTag = ident;
    261 	LogStat = logstat;
    262 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
    263 		LogFacility = logfac;
    264 
    265 	if (LogFile == -1) {
    266 		SyslogAddr.sa_family = AF_UNIX;
    267 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
    268 		    sizeof(SyslogAddr.sa_data));
    269 		if (LogStat & LOG_NDELAY) {
    270 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    271 				return;
    272 			(void)fcntl(LogFile, F_SETFD, 1);
    273 		}
    274 	}
    275 	if (LogFile != -1 && !connected)
    276 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
    277 			(void)close(LogFile);
    278 			LogFile = -1;
    279 		} else
    280 			connected = 1;
    281 }
    282 
    283 void
    284 closelog()
    285 {
    286 	(void)close(LogFile);
    287 	LogFile = -1;
    288 	connected = 0;
    289 }
    290 
    291 /* setlogmask -- set the log mask level */
    292 int
    293 setlogmask(pmask)
    294 	int pmask;
    295 {
    296 	int omask;
    297 
    298 	omask = LogMask;
    299 	if (pmask != 0)
    300 		LogMask = pmask;
    301 	return (omask);
    302 }
    303