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