xref: /csrg-svn/lib/libc/gen/syslog.c (revision 45430)
121362Sdist /*
233786Skarels  * Copyright (c) 1983, 1988 Regents of the University of California.
333786Skarels  * All rights reserved.
433786Skarels  *
542626Sbostic  * %sccs.include.redist.c%
621362Sdist  */
721362Sdist 
826602Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*45430Sbostic static char sccsid[] = "@(#)syslog.c	5.31 (Berkeley) 10/29/90";
1033786Skarels #endif /* LIBC_SCCS and not lint */
1112742Ssam 
1212742Ssam #include <sys/types.h>
1312742Ssam #include <sys/socket.h>
1416414Sralph #include <sys/file.h>
1526870Skarels #include <sys/syslog.h>
1636792Sbostic #include <sys/uio.h>
1745264Sbostic #include <sys/errno.h>
1812742Ssam #include <netdb.h>
1942025Sbostic #include <string.h>
2036442Sbostic #include <varargs.h>
2138557Sbostic #include <paths.h>
2236440Sbostic #include <stdio.h>
2312742Ssam 
2416414Sralph static int	LogFile = -1;		/* fd for log */
2534563Skarels static int	connected;		/* have done connect */
2636440Sbostic static int	LogStat = 0;		/* status bits, set by openlog() */
2724846Seric static char	*LogTag = "syslog";	/* string to tag the entry with */
2824846Seric static int	LogFacility = LOG_USER;	/* default facility code */
2912742Ssam 
3045264Sbostic /*
3145264Sbostic  * syslog, vsyslog --
3245264Sbostic  *	print message on log file; output is intended for syslogd(8).
3345264Sbostic  */
3436440Sbostic syslog(pri, fmt, args)
3536440Sbostic 	int pri, args;
3612742Ssam 	char *fmt;
3712742Ssam {
38*45430Sbostic 	vsyslog(pri, fmt, &args);
3936442Sbostic }
4036442Sbostic 
4136442Sbostic vsyslog(pri, fmt, ap)
4236442Sbostic 	int pri;
4336443Sbostic 	register char *fmt;
4436442Sbostic 	va_list ap;
4536442Sbostic {
4636440Sbostic 	register int cnt;
4736440Sbostic 	register char *p;
4836440Sbostic 	time_t now, time();
4938567Sbostic 	int fd, saved_errno;
5036792Sbostic 	char tbuf[2048], fmt_cpy[1024], *stdp, *ctime();
5112742Ssam 
5245277Sbostic 	/* check for invalid bits or no priority set */
53*45430Sbostic 	if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
54*45430Sbostic 		return;
5545277Sbostic 
5636443Sbostic 	saved_errno = errno;
5736443Sbostic 
5824846Seric 	/* set default facility if none specified */
5924846Seric 	if ((pri & LOG_FACMASK) == 0)
6024846Seric 		pri |= LogFacility;
6124846Seric 
6224846Seric 	/* build the message */
6336440Sbostic 	(void)time(&now);
6436440Sbostic 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
6536440Sbostic 	for (p = tbuf; *p; ++p);
6636792Sbostic 	if (LogStat & LOG_PERROR)
6736792Sbostic 		stdp = p;
6816414Sralph 	if (LogTag) {
6936440Sbostic 		(void)strcpy(p, LogTag);
7036440Sbostic 		for (; *p; ++p);
7116414Sralph 	}
7216414Sralph 	if (LogStat & LOG_PID) {
7336440Sbostic 		(void)sprintf(p, "[%d]", getpid());
7436440Sbostic 		for (; *p; ++p);
7516414Sralph 	}
7624846Seric 	if (LogTag) {
7736440Sbostic 		*p++ = ':';
7836440Sbostic 		*p++ = ' ';
7924846Seric 	}
8012742Ssam 
8136443Sbostic 	/* substitute error message for %m */
8236443Sbostic 	{
8336443Sbostic 		register char ch, *t1, *t2;
8436443Sbostic 		char *strerror();
8524846Seric 
8636443Sbostic 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
8736443Sbostic 			if (ch == '%' && fmt[1] == 'm') {
8836443Sbostic 				++fmt;
8936443Sbostic 				for (t2 = strerror(saved_errno);
9036443Sbostic 				    *t1 = *t2++; ++t1);
9136443Sbostic 			}
9236443Sbostic 			else
9336443Sbostic 				*t1++ = ch;
9436583Sbostic 		*t1 = '\0';
9536443Sbostic 	}
9636443Sbostic 
9736443Sbostic 	(void)vsprintf(p, fmt_cpy, ap);
9836443Sbostic 
9936792Sbostic 	cnt = strlen(tbuf);
10036792Sbostic 
10136792Sbostic 	/* output to stderr if requested */
10236792Sbostic 	if (LogStat & LOG_PERROR) {
10336792Sbostic 		struct iovec iov[2];
10436792Sbostic 		register struct iovec *v = iov;
10536792Sbostic 
10636792Sbostic 		v->iov_base = stdp;
10736792Sbostic 		v->iov_len = cnt - (stdp - tbuf);
10836792Sbostic 		++v;
10936792Sbostic 		v->iov_base = "\n";
11036792Sbostic 		v->iov_len = 1;
11136792Sbostic 		(void)writev(2, iov, 2);
11236792Sbostic 	}
11336792Sbostic 
11445264Sbostic 	/* get connected, output the message to the local logger */
11545264Sbostic 	if ((connected || !openlog(LogTag, LogStat | LOG_NDELAY, 0)) &&
11645264Sbostic 	    send(LogFile, tbuf, cnt, 0) >= 0)
117*45430Sbostic 		return;
11824846Seric 
11945264Sbostic 	/* see if should attempt the console */
12045264Sbostic 	if (!(LogStat&LOG_CONS))
121*45430Sbostic 		return;
12245264Sbostic 
12338567Sbostic 	/*
12445264Sbostic 	 * Output the message to the console; don't worry about blocking,
12545264Sbostic 	 * if console blocks everything will.  Make sure the error reported
12645264Sbostic 	 * is the one from the syslogd failure.
12738567Sbostic 	 */
12845264Sbostic 	if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
12945264Sbostic 		(void)strcat(tbuf, "\r\n");
13045264Sbostic 		cnt += 2;
13145264Sbostic 		p = index(tbuf, '>') + 1;
13245264Sbostic 		(void)write(fd, p, cnt - (p - tbuf));
13345264Sbostic 		(void)close(fd);
134*45430Sbostic 	}
13512742Ssam }
13612742Ssam 
13736440Sbostic static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
13845264Sbostic 
13924846Seric openlog(ident, logstat, logfac)
14012742Ssam 	char *ident;
14124846Seric 	int logstat, logfac;
14212742Ssam {
14324846Seric 	if (ident != NULL)
14424846Seric 		LogTag = ident;
14512742Ssam 	LogStat = logstat;
14633786Skarels 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
14733786Skarels 		LogFacility = logfac;
14845264Sbostic 
14934563Skarels 	if (LogFile == -1) {
15034563Skarels 		SyslogAddr.sa_family = AF_UNIX;
15145264Sbostic 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
15238557Sbostic 		    sizeof(SyslogAddr.sa_data));
15334563Skarels 		if (LogStat & LOG_NDELAY) {
15445264Sbostic 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
155*45430Sbostic 				return;
15645264Sbostic 			(void)fcntl(LogFile, F_SETFD, 1);
15734563Skarels 		}
15817918Sralph 	}
15945264Sbostic 	if (LogFile != -1 && !connected)
16045264Sbostic 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
16145264Sbostic 			(void)close(LogFile);
16245264Sbostic 			LogFile = -1;
16345264Sbostic 		} else
16445264Sbostic 			connected = 1;
16512742Ssam }
16612742Ssam 
16712742Ssam closelog()
16812742Ssam {
169*45430Sbostic 	(void)close(LogFile);
17012742Ssam 	LogFile = -1;
17134563Skarels 	connected = 0;
17212742Ssam }
17316414Sralph 
17436440Sbostic static int	LogMask = 0xff;		/* mask of priorities to be logged */
17545264Sbostic 
17645264Sbostic /* setlogmask -- set the log mask level */
17717526Sralph setlogmask(pmask)
17817526Sralph 	int pmask;
17916414Sralph {
18017526Sralph 	int omask;
18116414Sralph 
18217526Sralph 	omask = LogMask;
18317526Sralph 	if (pmask != 0)
18417526Sralph 		LogMask = pmask;
18517526Sralph 	return (omask);
18616414Sralph }
187