xref: /csrg-svn/lib/libc/gen/syslog.c (revision 38557)
121362Sdist /*
233786Skarels  * Copyright (c) 1983, 1988 Regents of the University of California.
333786Skarels  * All rights reserved.
433786Skarels  *
533786Skarels  * Redistribution and use in source and binary forms are permitted
634821Sbostic  * provided that the above copyright notice and this paragraph are
734821Sbostic  * duplicated in all such forms and that any documentation,
834821Sbostic  * advertising materials, and other materials related to such
934821Sbostic  * distribution and use acknowledge that the software was developed
1034821Sbostic  * by the University of California, Berkeley.  The name of the
1134821Sbostic  * University may not be used to endorse or promote products derived
1234821Sbostic  * from this software without specific prior written permission.
1334821Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434821Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534821Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621362Sdist  */
1721362Sdist 
1826602Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*38557Sbostic static char sccsid[] = "@(#)syslog.c	5.22 (Berkeley) 08/02/89";
2033786Skarels #endif /* LIBC_SCCS and not lint */
2112742Ssam 
2212742Ssam /*
2312742Ssam  * SYSLOG -- print message on log file
2412742Ssam  *
2536440Sbostic  * This routine looks a lot like printf, except that it outputs to the
2636440Sbostic  * log file instead of the standard output.  Also:
2716414Sralph  *	adds a timestamp,
2816414Sralph  *	prints the module name in front of the message,
2916414Sralph  *	has some other formatting types (or will sometime),
3016414Sralph  *	adds a newline on the end of the message.
3112742Ssam  *
3236440Sbostic  * The output of this routine is intended to be read by syslogd(8).
3316943Sralph  *
3416943Sralph  * Author: Eric Allman
3516943Sralph  * Modified to use UNIX domain IPC by Ralph Campbell
3612742Ssam  */
3716414Sralph 
3812742Ssam #include <sys/types.h>
3912742Ssam #include <sys/socket.h>
4016414Sralph #include <sys/file.h>
4126870Skarels #include <sys/signal.h>
4226870Skarels #include <sys/syslog.h>
4336792Sbostic #include <sys/uio.h>
44*38557Sbostic #include <sys/wait.h>
4512742Ssam #include <netdb.h>
4626870Skarels #include <strings.h>
4736442Sbostic #include <varargs.h>
48*38557Sbostic #include <paths.h>
4936440Sbostic #include <stdio.h>
5012742Ssam 
51*38557Sbostic #define	_PATH_LOGNAME	"/dev/log"
5212742Ssam 
5316414Sralph static int	LogFile = -1;		/* fd for log */
5434563Skarels static int	connected;		/* have done connect */
5536440Sbostic static int	LogStat = 0;		/* status bits, set by openlog() */
5624846Seric static char	*LogTag = "syslog";	/* string to tag the entry with */
5724846Seric static int	LogFacility = LOG_USER;	/* default facility code */
5812742Ssam 
5936440Sbostic syslog(pri, fmt, args)
6036440Sbostic 	int pri, args;
6112742Ssam 	char *fmt;
6212742Ssam {
6336442Sbostic 	vsyslog(pri, fmt, &args);
6436442Sbostic }
6536442Sbostic 
6636442Sbostic vsyslog(pri, fmt, ap)
6736442Sbostic 	int pri;
6836443Sbostic 	register char *fmt;
6936442Sbostic 	va_list ap;
7036442Sbostic {
7136443Sbostic 	extern int errno;
7236440Sbostic 	register int cnt;
7336440Sbostic 	register char *p;
7436440Sbostic 	time_t now, time();
7536443Sbostic 	int pid, saved_errno;
7636792Sbostic 	char tbuf[2048], fmt_cpy[1024], *stdp, *ctime();
7712742Ssam 
7836443Sbostic 	saved_errno = errno;
7936443Sbostic 
8012742Ssam 	/* see if we should just throw out this message */
8136440Sbostic 	if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES ||
8236440Sbostic 	    !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
8312742Ssam 		return;
8434563Skarels 	if (LogFile < 0 || !connected)
8525187Seric 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
8624846Seric 
8724846Seric 	/* set default facility if none specified */
8824846Seric 	if ((pri & LOG_FACMASK) == 0)
8924846Seric 		pri |= LogFacility;
9024846Seric 
9124846Seric 	/* build the message */
9236440Sbostic 	(void)time(&now);
9336440Sbostic 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
9436440Sbostic 	for (p = tbuf; *p; ++p);
9536792Sbostic 	if (LogStat & LOG_PERROR)
9636792Sbostic 		stdp = p;
9716414Sralph 	if (LogTag) {
9836440Sbostic 		(void)strcpy(p, LogTag);
9936440Sbostic 		for (; *p; ++p);
10016414Sralph 	}
10116414Sralph 	if (LogStat & LOG_PID) {
10236440Sbostic 		(void)sprintf(p, "[%d]", getpid());
10336440Sbostic 		for (; *p; ++p);
10416414Sralph 	}
10524846Seric 	if (LogTag) {
10636440Sbostic 		*p++ = ':';
10736440Sbostic 		*p++ = ' ';
10824846Seric 	}
10912742Ssam 
11036443Sbostic 	/* substitute error message for %m */
11136443Sbostic 	{
11236443Sbostic 		register char ch, *t1, *t2;
11336443Sbostic 		char *strerror();
11424846Seric 
11536443Sbostic 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
11636443Sbostic 			if (ch == '%' && fmt[1] == 'm') {
11736443Sbostic 				++fmt;
11836443Sbostic 				for (t2 = strerror(saved_errno);
11936443Sbostic 				    *t1 = *t2++; ++t1);
12036443Sbostic 			}
12136443Sbostic 			else
12236443Sbostic 				*t1++ = ch;
12336583Sbostic 		*t1 = '\0';
12436443Sbostic 	}
12536443Sbostic 
12636443Sbostic 	(void)vsprintf(p, fmt_cpy, ap);
12736443Sbostic 
12836792Sbostic 	cnt = strlen(tbuf);
12936792Sbostic 
13036792Sbostic 	/* output to stderr if requested */
13136792Sbostic 	if (LogStat & LOG_PERROR) {
13236792Sbostic 		struct iovec iov[2];
13336792Sbostic 		register struct iovec *v = iov;
13436792Sbostic 
13536792Sbostic 		v->iov_base = stdp;
13636792Sbostic 		v->iov_len = cnt - (stdp - tbuf);
13736792Sbostic 		++v;
13836792Sbostic 		v->iov_base = "\n";
13936792Sbostic 		v->iov_len = 1;
14036792Sbostic 		(void)writev(2, iov, 2);
14136792Sbostic 	}
14236792Sbostic 
14324846Seric 	/* output the message to the local logger */
144*38557Sbostic 	if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS))
14516414Sralph 		return;
14624846Seric 
14724846Seric 	/* output the message to the console */
14826508Skarels 	pid = vfork();
14916414Sralph 	if (pid == -1)
15016414Sralph 		return;
15116414Sralph 	if (pid == 0) {
15227795Skarels 		int fd;
15336440Sbostic 		long sigsetmask();
15427795Skarels 
15536440Sbostic 		(void)signal(SIGALRM, SIG_DFL);
15636440Sbostic 		sigsetmask((long)~sigmask(SIGALRM));
15736440Sbostic 		(void)alarm((u_int)5);
158*38557Sbostic 		if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0)
15936440Sbostic 			return;
16036440Sbostic 		(void)alarm((u_int)0);
16136440Sbostic 		(void)strcat(tbuf, "\r");
16236440Sbostic 		p = index(tbuf, '>') + 1;
16336440Sbostic 		(void)write(fd, p, cnt + 1 - (p - tbuf));
16436440Sbostic 		(void)close(fd);
16527795Skarels 		_exit(0);
16616414Sralph 	}
16727343Skarels 	if (!(LogStat & LOG_NOWAIT))
168*38557Sbostic 		(void)waitpid(pid, (union wait *)NULL, WSIGRESTART);
16912742Ssam }
17012742Ssam 
17136440Sbostic static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
17212742Ssam /*
17312742Ssam  * OPENLOG -- open system log
17412742Ssam  */
17524846Seric openlog(ident, logstat, logfac)
17612742Ssam 	char *ident;
17724846Seric 	int logstat, logfac;
17812742Ssam {
17924846Seric 	if (ident != NULL)
18024846Seric 		LogTag = ident;
18112742Ssam 	LogStat = logstat;
18233786Skarels 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
18333786Skarels 		LogFacility = logfac;
18434563Skarels 	if (LogFile == -1) {
18534563Skarels 		SyslogAddr.sa_family = AF_UNIX;
186*38557Sbostic 		strncpy(SyslogAddr.sa_data, _PATH_LOGNAME,
187*38557Sbostic 		    sizeof(SyslogAddr.sa_data));
18834563Skarels 		if (LogStat & LOG_NDELAY) {
18934563Skarels 			LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
19034563Skarels 			fcntl(LogFile, F_SETFD, 1);
19134563Skarels 		}
19217918Sralph 	}
19334563Skarels 	if (LogFile != -1 && !connected &&
19434563Skarels 	    connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
19534563Skarels 		connected = 1;
19612742Ssam }
19712742Ssam 
19812742Ssam /*
19912742Ssam  * CLOSELOG -- close the system log
20012742Ssam  */
20112742Ssam closelog()
20212742Ssam {
20312742Ssam 	(void) close(LogFile);
20412742Ssam 	LogFile = -1;
20534563Skarels 	connected = 0;
20612742Ssam }
20716414Sralph 
20836440Sbostic static int	LogMask = 0xff;		/* mask of priorities to be logged */
20916414Sralph /*
21016414Sralph  * SETLOGMASK -- set the log mask level
21116414Sralph  */
21217526Sralph setlogmask(pmask)
21317526Sralph 	int pmask;
21416414Sralph {
21517526Sralph 	int omask;
21616414Sralph 
21717526Sralph 	omask = LogMask;
21817526Sralph 	if (pmask != 0)
21917526Sralph 		LogMask = pmask;
22017526Sralph 	return (omask);
22116414Sralph }
222