xref: /csrg-svn/lib/libc/gen/syslog.c (revision 66370)
121362Sdist /*
261111Sbostic  * Copyright (c) 1983, 1988, 1993
361111Sbostic  *	The Regents of the University of California.  All rights reserved.
433786Skarels  *
542626Sbostic  * %sccs.include.redist.c%
621362Sdist  */
721362Sdist 
826602Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*66370Seric static char sccsid[] = "@(#)syslog.c	8.3 (Berkeley) 03/17/94";
1033786Skarels #endif /* LIBC_SCCS and not lint */
1112742Ssam 
1212742Ssam #include <sys/types.h>
1312742Ssam #include <sys/socket.h>
1426870Skarels #include <sys/syslog.h>
1536792Sbostic #include <sys/uio.h>
1612742Ssam #include <netdb.h>
1756411Sbostic 
1856411Sbostic #include <errno.h>
1956411Sbostic #include <fcntl.h>
2056411Sbostic #include <paths.h>
2156411Sbostic #include <stdio.h>
2242025Sbostic #include <string.h>
2356411Sbostic #include <time.h>
2456411Sbostic #include <unistd.h>
2556411Sbostic 
2646597Sdonn #if __STDC__
2746597Sdonn #include <stdarg.h>
2846597Sdonn #else
2936442Sbostic #include <varargs.h>
3046597Sdonn #endif
3112742Ssam 
3216414Sralph static int	LogFile = -1;		/* fd for log */
3334563Skarels static int	connected;		/* have done connect */
3436440Sbostic static int	LogStat = 0;		/* status bits, set by openlog() */
35*66370Seric static const char *LogTag = NULL;	/* string to tag the entry with */
3624846Seric static int	LogFacility = LOG_USER;	/* default facility code */
3750158Smckusick static int	LogMask = 0xff;		/* mask of priorities to be logged */
38*66370Seric extern char	*__progname;		/* Program name, from crt0. */
3912742Ssam 
4045264Sbostic /*
4145264Sbostic  * syslog, vsyslog --
4245264Sbostic  *	print message on log file; output is intended for syslogd(8).
4345264Sbostic  */
4446597Sdonn void
4546597Sdonn #if __STDC__
4646597Sdonn syslog(int pri, const char *fmt, ...)
4746597Sdonn #else
4846597Sdonn syslog(pri, fmt, va_alist)
4946597Sdonn 	int pri;
5012742Ssam 	char *fmt;
5146597Sdonn 	va_dcl
5246597Sdonn #endif
5312742Ssam {
5446597Sdonn 	va_list ap;
5546597Sdonn 
5646597Sdonn #if __STDC__
5746597Sdonn 	va_start(ap, fmt);
5846597Sdonn #else
5946597Sdonn 	va_start(ap);
6046597Sdonn #endif
6146597Sdonn 	vsyslog(pri, fmt, ap);
6246597Sdonn 	va_end(ap);
6336442Sbostic }
6436442Sbostic 
6546597Sdonn void
6636442Sbostic vsyslog(pri, fmt, ap)
6736442Sbostic 	int pri;
6846597Sdonn 	register const char *fmt;
6936442Sbostic 	va_list ap;
7036442Sbostic {
7136440Sbostic 	register int cnt;
7236440Sbostic 	register char *p;
7354367Sbostic 	time_t now;
7438567Sbostic 	int fd, saved_errno;
7554367Sbostic 	char *stdp, tbuf[2048], fmt_cpy[1024];
7612742Ssam 
7754367Sbostic #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
7854367Sbostic 	/* Check for invalid bits. */
7954367Sbostic 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
8054367Sbostic 		syslog(INTERNALLOG,
8154367Sbostic 		    "syslog: unknown facility/priority: %x", pri);
8254367Sbostic 		pri &= LOG_PRIMASK|LOG_FACMASK;
8354367Sbostic 	}
8454367Sbostic 
8554367Sbostic 	/* Check priority against setlogmask values. */
8654367Sbostic 	if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
8745430Sbostic 		return;
8845277Sbostic 
8936443Sbostic 	saved_errno = errno;
9036443Sbostic 
9124846Seric 	/* set default facility if none specified */
9224846Seric 	if ((pri & LOG_FACMASK) == 0)
9324846Seric 		pri |= LogFacility;
9424846Seric 
9524846Seric 	/* build the message */
9636440Sbostic 	(void)time(&now);
9766369Sbostic 	p = tbuf + sprintf(tbuf, "<%d>", pri);
9866369Sbostic 	p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
9966369Sbostic 	    localtime(&now));
10036792Sbostic 	if (LogStat & LOG_PERROR)
10136792Sbostic 		stdp = p;
102*66370Seric 	if (!LogTag)
103*66370Seric 		LogTag = __progname;
10416414Sralph 	if (LogTag) {
10536440Sbostic 		(void)strcpy(p, LogTag);
10636440Sbostic 		for (; *p; ++p);
10716414Sralph 	}
10866369Sbostic 	if (LogStat & LOG_PID)
10966369Sbostic 		p += sprintf(p, "[%d]", getpid());
11024846Seric 	if (LogTag) {
11136440Sbostic 		*p++ = ':';
11236440Sbostic 		*p++ = ' ';
11324846Seric 	}
11412742Ssam 
11536443Sbostic 	/* substitute error message for %m */
11636443Sbostic 	{
11736443Sbostic 		register char ch, *t1, *t2;
11824846Seric 
11936443Sbostic 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
12036443Sbostic 			if (ch == '%' && fmt[1] == 'm') {
12136443Sbostic 				++fmt;
12236443Sbostic 				for (t2 = strerror(saved_errno);
12336443Sbostic 				    *t1 = *t2++; ++t1);
12436443Sbostic 			}
12536443Sbostic 			else
12636443Sbostic 				*t1++ = ch;
12736583Sbostic 		*t1 = '\0';
12836443Sbostic 	}
12936443Sbostic 
13066369Sbostic 	p += vsprintf(p, fmt_cpy, ap);
13166369Sbostic 	cnt = p - tbuf;
13236443Sbostic 
13336792Sbostic 	/* output to stderr if requested */
13436792Sbostic 	if (LogStat & LOG_PERROR) {
13536792Sbostic 		struct iovec iov[2];
13636792Sbostic 		register struct iovec *v = iov;
13736792Sbostic 
13836792Sbostic 		v->iov_base = stdp;
13936792Sbostic 		v->iov_len = cnt - (stdp - tbuf);
14036792Sbostic 		++v;
14136792Sbostic 		v->iov_base = "\n";
14236792Sbostic 		v->iov_len = 1;
14345646Sbostic 		(void)writev(STDERR_FILENO, iov, 2);
14436792Sbostic 	}
14536792Sbostic 
14645264Sbostic 	/* get connected, output the message to the local logger */
14746597Sdonn 	if (!connected)
14846597Sdonn 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
14946597Sdonn 	if (send(LogFile, tbuf, cnt, 0) >= 0)
15045430Sbostic 		return;
15124846Seric 
15245264Sbostic 	/* see if should attempt the console */
15345264Sbostic 	if (!(LogStat&LOG_CONS))
15445430Sbostic 		return;
15545264Sbostic 
15638567Sbostic 	/*
15745264Sbostic 	 * Output the message to the console; don't worry about blocking,
15845264Sbostic 	 * if console blocks everything will.  Make sure the error reported
15945264Sbostic 	 * is the one from the syslogd failure.
16038567Sbostic 	 */
16145264Sbostic 	if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
16245264Sbostic 		(void)strcat(tbuf, "\r\n");
16345264Sbostic 		cnt += 2;
16445264Sbostic 		p = index(tbuf, '>') + 1;
16545264Sbostic 		(void)write(fd, p, cnt - (p - tbuf));
16645264Sbostic 		(void)close(fd);
16745430Sbostic 	}
16812742Ssam }
16912742Ssam 
17036440Sbostic static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
17145264Sbostic 
17246597Sdonn void
17324846Seric openlog(ident, logstat, logfac)
17446597Sdonn 	const char *ident;
17524846Seric 	int logstat, logfac;
17612742Ssam {
17724846Seric 	if (ident != NULL)
17824846Seric 		LogTag = ident;
17912742Ssam 	LogStat = logstat;
18033786Skarels 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
18133786Skarels 		LogFacility = logfac;
18245264Sbostic 
18334563Skarels 	if (LogFile == -1) {
18434563Skarels 		SyslogAddr.sa_family = AF_UNIX;
18545264Sbostic 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
18638557Sbostic 		    sizeof(SyslogAddr.sa_data));
18734563Skarels 		if (LogStat & LOG_NDELAY) {
18845264Sbostic 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
18945430Sbostic 				return;
19045264Sbostic 			(void)fcntl(LogFile, F_SETFD, 1);
19134563Skarels 		}
19217918Sralph 	}
19345264Sbostic 	if (LogFile != -1 && !connected)
19445264Sbostic 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
19545264Sbostic 			(void)close(LogFile);
19645264Sbostic 			LogFile = -1;
19745264Sbostic 		} else
19845264Sbostic 			connected = 1;
19912742Ssam }
20012742Ssam 
20146597Sdonn void
20212742Ssam closelog()
20312742Ssam {
20445430Sbostic 	(void)close(LogFile);
20512742Ssam 	LogFile = -1;
20634563Skarels 	connected = 0;
20712742Ssam }
20816414Sralph 
20945264Sbostic /* setlogmask -- set the log mask level */
21054367Sbostic int
21117526Sralph setlogmask(pmask)
21217526Sralph 	int pmask;
21316414Sralph {
21417526Sralph 	int omask;
21516414Sralph 
21617526Sralph 	omask = LogMask;
21717526Sralph 	if (pmask != 0)
21817526Sralph 		LogMask = pmask;
21917526Sralph 	return (omask);
22016414Sralph }
221