xref: /original-bsd/lib/libc/gen/syslog.c (revision 2610eb8c)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)syslog.c	8.2 (Berkeley) 03/17/94";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/syslog.h>
15 #include <sys/uio.h>
16 #include <netdb.h>
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <paths.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <time.h>
24 #include <unistd.h>
25 
26 #if __STDC__
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31 
32 static int	LogFile = -1;		/* fd for log */
33 static int	connected;		/* have done connect */
34 static int	LogStat = 0;		/* status bits, set by openlog() */
35 static const char *LogTag = "syslog";	/* string to tag the entry with */
36 static int	LogFacility = LOG_USER;	/* default facility code */
37 static int	LogMask = 0xff;		/* mask of priorities to be logged */
38 
39 /*
40  * syslog, vsyslog --
41  *	print message on log file; output is intended for syslogd(8).
42  */
43 void
44 #if __STDC__
45 syslog(int pri, const char *fmt, ...)
46 #else
47 syslog(pri, fmt, va_alist)
48 	int pri;
49 	char *fmt;
50 	va_dcl
51 #endif
52 {
53 	va_list ap;
54 
55 #if __STDC__
56 	va_start(ap, fmt);
57 #else
58 	va_start(ap);
59 #endif
60 	vsyslog(pri, fmt, ap);
61 	va_end(ap);
62 }
63 
64 void
65 vsyslog(pri, fmt, ap)
66 	int pri;
67 	register const char *fmt;
68 	va_list ap;
69 {
70 	register int cnt;
71 	register char *p;
72 	time_t now;
73 	int fd, saved_errno;
74 	char *stdp, tbuf[2048], fmt_cpy[1024];
75 
76 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
77 	/* Check for invalid bits. */
78 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
79 		syslog(INTERNALLOG,
80 		    "syslog: unknown facility/priority: %x", pri);
81 		pri &= LOG_PRIMASK|LOG_FACMASK;
82 	}
83 
84 	/* Check priority against setlogmask values. */
85 	if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
86 		return;
87 
88 	saved_errno = errno;
89 
90 	/* set default facility if none specified */
91 	if ((pri & LOG_FACMASK) == 0)
92 		pri |= LogFacility;
93 
94 	/* build the message */
95 	(void)time(&now);
96 	p = tbuf + sprintf(tbuf, "<%d>", pri);
97 	p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
98 	    localtime(&now));
99 	if (LogStat & LOG_PERROR)
100 		stdp = p;
101 	if (LogTag) {
102 		(void)strcpy(p, LogTag);
103 		for (; *p; ++p);
104 	}
105 	if (LogStat & LOG_PID)
106 		p += sprintf(p, "[%d]", getpid());
107 	if (LogTag) {
108 		*p++ = ':';
109 		*p++ = ' ';
110 	}
111 
112 	/* substitute error message for %m */
113 	{
114 		register char ch, *t1, *t2;
115 
116 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
117 			if (ch == '%' && fmt[1] == 'm') {
118 				++fmt;
119 				for (t2 = strerror(saved_errno);
120 				    *t1 = *t2++; ++t1);
121 			}
122 			else
123 				*t1++ = ch;
124 		*t1 = '\0';
125 	}
126 
127 	p += vsprintf(p, fmt_cpy, ap);
128 	cnt = p - tbuf;
129 
130 	/* output to stderr if requested */
131 	if (LogStat & LOG_PERROR) {
132 		struct iovec iov[2];
133 		register struct iovec *v = iov;
134 
135 		v->iov_base = stdp;
136 		v->iov_len = cnt - (stdp - tbuf);
137 		++v;
138 		v->iov_base = "\n";
139 		v->iov_len = 1;
140 		(void)writev(STDERR_FILENO, iov, 2);
141 	}
142 
143 	/* get connected, output the message to the local logger */
144 	if (!connected)
145 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
146 	if (send(LogFile, tbuf, cnt, 0) >= 0)
147 		return;
148 
149 	/* see if should attempt the console */
150 	if (!(LogStat&LOG_CONS))
151 		return;
152 
153 	/*
154 	 * Output the message to the console; don't worry about blocking,
155 	 * if console blocks everything will.  Make sure the error reported
156 	 * is the one from the syslogd failure.
157 	 */
158 	if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
159 		(void)strcat(tbuf, "\r\n");
160 		cnt += 2;
161 		p = index(tbuf, '>') + 1;
162 		(void)write(fd, p, cnt - (p - tbuf));
163 		(void)close(fd);
164 	}
165 }
166 
167 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
168 
169 void
170 openlog(ident, logstat, logfac)
171 	const char *ident;
172 	int logstat, logfac;
173 {
174 	if (ident != NULL)
175 		LogTag = ident;
176 	LogStat = logstat;
177 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
178 		LogFacility = logfac;
179 
180 	if (LogFile == -1) {
181 		SyslogAddr.sa_family = AF_UNIX;
182 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
183 		    sizeof(SyslogAddr.sa_data));
184 		if (LogStat & LOG_NDELAY) {
185 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
186 				return;
187 			(void)fcntl(LogFile, F_SETFD, 1);
188 		}
189 	}
190 	if (LogFile != -1 && !connected)
191 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
192 			(void)close(LogFile);
193 			LogFile = -1;
194 		} else
195 			connected = 1;
196 }
197 
198 void
199 closelog()
200 {
201 	(void)close(LogFile);
202 	LogFile = -1;
203 	connected = 0;
204 }
205 
206 /* setlogmask -- set the log mask level */
207 int
208 setlogmask(pmask)
209 	int pmask;
210 {
211 	int omask;
212 
213 	omask = LogMask;
214 	if (pmask != 0)
215 		LogMask = pmask;
216 	return (omask);
217 }
218