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