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