xref: /csrg-svn/lib/libc/gen/syslog.c (revision 36583)
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*36583Sbostic static char sccsid[] = "@(#)syslog.c	5.20 (Berkeley) 01/19/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>
4312742Ssam #include <netdb.h>
4426870Skarels #include <strings.h>
4536442Sbostic #include <varargs.h>
4636440Sbostic #include <stdio.h>
4712742Ssam 
4836440Sbostic #define	LOGNAME	"/dev/log"
4936440Sbostic #define	CONSOLE	"/dev/console"
5012742Ssam 
5116414Sralph static int	LogFile = -1;		/* fd for log */
5234563Skarels static int	connected;		/* have done connect */
5336440Sbostic static int	LogStat = 0;		/* status bits, set by openlog() */
5424846Seric static char	*LogTag = "syslog";	/* string to tag the entry with */
5524846Seric static int	LogFacility = LOG_USER;	/* default facility code */
5612742Ssam 
5736440Sbostic syslog(pri, fmt, args)
5836440Sbostic 	int pri, args;
5912742Ssam 	char *fmt;
6012742Ssam {
6136442Sbostic 	vsyslog(pri, fmt, &args);
6236442Sbostic }
6336442Sbostic 
6436442Sbostic vsyslog(pri, fmt, ap)
6536442Sbostic 	int pri;
6636443Sbostic 	register char *fmt;
6736442Sbostic 	va_list ap;
6836442Sbostic {
6936443Sbostic 	extern int errno;
7036440Sbostic 	register int cnt;
7136440Sbostic 	register char *p;
7236440Sbostic 	time_t now, time();
7336443Sbostic 	int pid, saved_errno;
7436443Sbostic 	char tbuf[2048], fmt_cpy[1024], *ctime();
7512742Ssam 
7636443Sbostic 	saved_errno = errno;
7736443Sbostic 
7812742Ssam 	/* see if we should just throw out this message */
7936440Sbostic 	if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES ||
8036440Sbostic 	    !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
8112742Ssam 		return;
8234563Skarels 	if (LogFile < 0 || !connected)
8325187Seric 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
8424846Seric 
8524846Seric 	/* set default facility if none specified */
8624846Seric 	if ((pri & LOG_FACMASK) == 0)
8724846Seric 		pri |= LogFacility;
8824846Seric 
8924846Seric 	/* build the message */
9036440Sbostic 	(void)time(&now);
9136440Sbostic 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
9236440Sbostic 	for (p = tbuf; *p; ++p);
9316414Sralph 	if (LogTag) {
9436440Sbostic 		(void)strcpy(p, LogTag);
9536440Sbostic 		for (; *p; ++p);
9616414Sralph 	}
9716414Sralph 	if (LogStat & LOG_PID) {
9836440Sbostic 		(void)sprintf(p, "[%d]", getpid());
9936440Sbostic 		for (; *p; ++p);
10016414Sralph 	}
10124846Seric 	if (LogTag) {
10236440Sbostic 		*p++ = ':';
10336440Sbostic 		*p++ = ' ';
10424846Seric 	}
10512742Ssam 
10636443Sbostic 	/* substitute error message for %m */
10736443Sbostic 	{
10836443Sbostic 		register char ch, *t1, *t2;
10936443Sbostic 		char *strerror();
11024846Seric 
11136443Sbostic 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
11236443Sbostic 			if (ch == '%' && fmt[1] == 'm') {
11336443Sbostic 				++fmt;
11436443Sbostic 				for (t2 = strerror(saved_errno);
11536443Sbostic 				    *t1 = *t2++; ++t1);
11636443Sbostic 			}
11736443Sbostic 			else
11836443Sbostic 				*t1++ = ch;
119*36583Sbostic 		*t1 = '\0';
12036443Sbostic 	}
12136443Sbostic 
12236443Sbostic 	(void)vsprintf(p, fmt_cpy, ap);
12336443Sbostic 
12424846Seric 	/* output the message to the local logger */
12536440Sbostic 	if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 ||
12636440Sbostic 	    !(LogStat&LOG_CONS))
12716414Sralph 		return;
12824846Seric 
12924846Seric 	/* output the message to the console */
13026508Skarels 	pid = vfork();
13116414Sralph 	if (pid == -1)
13216414Sralph 		return;
13316414Sralph 	if (pid == 0) {
13427795Skarels 		int fd;
13536440Sbostic 		long sigsetmask();
13627795Skarels 
13736440Sbostic 		(void)signal(SIGALRM, SIG_DFL);
13836440Sbostic 		sigsetmask((long)~sigmask(SIGALRM));
13936440Sbostic 		(void)alarm((u_int)5);
14036440Sbostic 		if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0)
14136440Sbostic 			return;
14236440Sbostic 		(void)alarm((u_int)0);
14336440Sbostic 		(void)strcat(tbuf, "\r");
14436440Sbostic 		p = index(tbuf, '>') + 1;
14536440Sbostic 		(void)write(fd, p, cnt + 1 - (p - tbuf));
14636440Sbostic 		(void)close(fd);
14727795Skarels 		_exit(0);
14816414Sralph 	}
14927343Skarels 	if (!(LogStat & LOG_NOWAIT))
15036440Sbostic 		while ((cnt = wait((int *)0)) > 0 && cnt != pid);
15112742Ssam }
15212742Ssam 
15336440Sbostic static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
15412742Ssam /*
15512742Ssam  * OPENLOG -- open system log
15612742Ssam  */
15724846Seric openlog(ident, logstat, logfac)
15812742Ssam 	char *ident;
15924846Seric 	int logstat, logfac;
16012742Ssam {
16124846Seric 	if (ident != NULL)
16224846Seric 		LogTag = ident;
16312742Ssam 	LogStat = logstat;
16433786Skarels 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
16533786Skarels 		LogFacility = logfac;
16634563Skarels 	if (LogFile == -1) {
16734563Skarels 		SyslogAddr.sa_family = AF_UNIX;
16836440Sbostic 		strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data);
16934563Skarels 		if (LogStat & LOG_NDELAY) {
17034563Skarels 			LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
17134563Skarels 			fcntl(LogFile, F_SETFD, 1);
17234563Skarels 		}
17317918Sralph 	}
17434563Skarels 	if (LogFile != -1 && !connected &&
17534563Skarels 	    connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
17634563Skarels 		connected = 1;
17712742Ssam }
17812742Ssam 
17912742Ssam /*
18012742Ssam  * CLOSELOG -- close the system log
18112742Ssam  */
18212742Ssam closelog()
18312742Ssam {
18412742Ssam 	(void) close(LogFile);
18512742Ssam 	LogFile = -1;
18634563Skarels 	connected = 0;
18712742Ssam }
18816414Sralph 
18936440Sbostic static int	LogMask = 0xff;		/* mask of priorities to be logged */
19016414Sralph /*
19116414Sralph  * SETLOGMASK -- set the log mask level
19216414Sralph  */
19317526Sralph setlogmask(pmask)
19417526Sralph 	int pmask;
19516414Sralph {
19617526Sralph 	int omask;
19716414Sralph 
19817526Sralph 	omask = LogMask;
19917526Sralph 	if (pmask != 0)
20017526Sralph 		LogMask = pmask;
20117526Sralph 	return (omask);
20216414Sralph }
203