xref: /openbsd-src/lib/libc/gen/syslog.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char rcsid[] = "$OpenBSD: syslog.c,v 1.13 2001/10/31 14:24:11 fgsch Exp $";
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/uio.h>
42 #include <sys/un.h>
43 #include <netdb.h>
44 
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <paths.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <time.h>
51 #include <unistd.h>
52 
53 #ifdef __STDC__
54 #include <stdarg.h>
55 #else
56 #include <varargs.h>
57 #endif
58 
59 static struct syslog_data sdata = SYSLOG_DATA_INIT;
60 
61 extern char	*__progname;		/* Program name, from crt0. */
62 
63 static void	disconnectlog __P((void)); /* disconnect from syslogd */
64 static void	connectlog __P((void));	/* (re)connect to syslogd */
65 static void	disconnectlog_r __P((struct syslog_data *));
66 static void	connectlog_r __P((struct syslog_data *));
67 
68 /*
69  * syslog, vsyslog --
70  *	print message on log file; output is intended for syslogd(8).
71  */
72 void
73 #ifdef __STDC__
74 syslog(int pri, const char *fmt, ...)
75 #else
76 syslog(pri, fmt, va_alist)
77 	int pri;
78 	char *fmt;
79 	va_dcl
80 #endif
81 {
82 	va_list ap;
83 
84 #ifdef __STDC__
85 	va_start(ap, fmt);
86 #else
87 	va_start(ap);
88 #endif
89 	vsyslog(pri, fmt, ap);
90 	va_end(ap);
91 }
92 
93 void
94 vsyslog(pri, fmt, ap)
95 	int pri;
96 	register const char *fmt;
97 	va_list ap;
98 {
99 	vsyslog_r(pri, &sdata, fmt, ap);
100 }
101 
102 static void
103 disconnectlog()
104 {
105 	disconnectlog_r(&sdata);
106 }
107 
108 static void
109 connectlog()
110 {
111 	connectlog_r(&sdata);
112 }
113 
114 void
115 openlog(ident, logstat, logfac)
116 	const char *ident;
117 	int logstat, logfac;
118 {
119 	openlog_r(ident, logstat, logfac, &sdata);
120 }
121 
122 void
123 closelog()
124 {
125 	closelog_r(&sdata);
126 }
127 
128 /* setlogmask -- set the log mask level */
129 int
130 setlogmask(pmask)
131 	int pmask;
132 {
133 	return setlogmask_r(pmask, &sdata);
134 }
135 
136 /* Reentrant version of syslog, i.e. syslog_r() */
137 
138 void
139 #ifdef __STDC__
140 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
141 #else
142 syslog_r(pri, data, fmt, va_alist)
143 	int pri;
144 	struct syslog_data *data;
145 	char *fmt;
146 	va_dcl
147 #endif
148 {
149 	va_list ap;
150 
151 #ifdef __STDC__
152 	va_start(ap, fmt);
153 #else
154 	va_start(ap);
155 #endif
156 	vsyslog_r(pri, data, fmt, ap);
157 	va_end(ap);
158 }
159 
160 void
161 vsyslog_r(pri, data, fmt, ap)
162 	int pri;
163 	struct syslog_data *data;
164 	const char *fmt;
165 	va_list ap;
166 {
167 	int cnt;
168 	char ch, *p, *t;
169 	time_t now;
170 	int fd, saved_errno;
171 #define	TBUF_LEN	2048
172 #define	FMT_LEN		1024
173 	char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
174 	int tbuf_left, fmt_left, prlen;
175 
176 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
177 	/* Check for invalid bits. */
178 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
179 		if (data == &sdata) {
180 			syslog(INTERNALLOG,
181 			    "syslog: unknown facility/priority: %x", pri);
182 		} else {
183 			syslog_r(INTERNALLOG, data,
184 			    "syslog_r: unknown facility/priority: %x", pri);
185 		}
186 		pri &= LOG_PRIMASK|LOG_FACMASK;
187 	}
188 
189 	/* Check priority against setlogmask values. */
190 	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
191 		return;
192 
193 	saved_errno = errno;
194 
195 	/* Set default facility if none specified. */
196 	if ((pri & LOG_FACMASK) == 0)
197 		pri |= data->log_fac;
198 
199 	/* If we have been called through syslog(), no need for reentrancy. */
200 	if (data == &sdata)
201 		(void)time(&now);
202 
203 	p = tbuf;
204 	tbuf_left = TBUF_LEN;
205 
206 #define	DEC()	\
207 	do {					\
208 		if (prlen < 0)			\
209 			prlen = 0;		\
210 		if (prlen >= tbuf_left)		\
211 			prlen = tbuf_left - 1;	\
212 		p += prlen;			\
213 		tbuf_left -= prlen;		\
214 	} while (0)
215 
216 	prlen = snprintf(p, tbuf_left, "<%d>", pri);
217 	DEC();
218 
219 	/*
220 	 * syslogd will expand time automagically for reentrant case, and
221 	 * for normal case, just do like before
222 	 */
223 	if (data == &sdata) {
224 		prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now));
225 		DEC();
226 	}
227 
228 	if (data->log_stat & LOG_PERROR)
229 		stdp = p;
230 	if (data->log_tag == NULL)
231 		data->log_tag = __progname;
232 	if (data->log_tag != NULL) {
233 		prlen = snprintf(p, tbuf_left, "%s", data->log_tag);
234 		DEC();
235 	}
236 	if (data->log_stat & LOG_PID) {
237 		prlen = snprintf(p, tbuf_left, "[%d]", getpid());
238 		DEC();
239 	}
240 	if (data->log_tag != NULL) {
241 		if (tbuf_left > 1) {
242 			*p++ = ':';
243 			tbuf_left--;
244 		}
245 		if (tbuf_left > 1) {
246 			*p++ = ' ';
247 			tbuf_left--;
248 		}
249 	}
250 
251 	/* strerror() is not reentrant */
252 
253 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
254 		if (ch == '%' && fmt[1] == 'm') {
255 			++fmt;
256 			if (data == &sdata) {
257 				prlen = snprintf(t, fmt_left, "%s",
258 				    strerror(saved_errno));
259 			} else {
260 				prlen = snprintf(t, fmt_left, "Error %d",
261 				    saved_errno);
262 			}
263 			if (prlen >= fmt_left)
264 				prlen = fmt_left - 1;
265 			t += prlen;
266 			fmt_left -= prlen;
267 		} else {
268 			if (fmt_left > 1) {
269 				*t++ = ch;
270 				fmt_left--;
271 			}
272 		}
273 	}
274 	*t = '\0';
275 
276 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
277 	DEC();
278 	cnt = p - tbuf;
279 
280 	/* Output to stderr if requested. */
281 	if (data->log_stat & LOG_PERROR) {
282 		struct iovec iov[2];
283 
284 		iov[0].iov_base = stdp;
285 		iov[0].iov_len = cnt - (stdp - tbuf);
286 		iov[1].iov_base = "\n";
287 		iov[1].iov_len = 1;
288 		(void)writev(STDERR_FILENO, iov, 2);
289 	}
290 
291 	/* Get connected, output the message to the local logger. */
292 	if (!data->opened)
293 		openlog_r(data->log_tag, data->log_stat, 0, data);
294 	connectlog_r(data);
295 	if (send(data->log_file, tbuf, cnt, 0) >= 0)
296 		return;
297 
298 	/*
299 	 * If the send() failed, the odds are syslogd was restarted.
300 	 * Make one (only) attempt to reconnect to /dev/log.
301 	 */
302 	disconnectlog_r(data);
303 	connectlog_r(data);
304 	if (send(data->log_file, tbuf, cnt, 0) >= 0)
305 		return;
306 
307 	/*
308 	 * Output the message to the console; don't worry about blocking,
309 	 * if console blocks everything will.  Make sure the error reported
310 	 * is the one from the syslogd failure.
311 	 */
312 	if (data->log_stat & LOG_CONS &&
313 	    (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
314 		struct iovec iov[2];
315 
316 		p = strchr(tbuf, '>') + 1;
317 		iov[0].iov_base = p;
318 		iov[0].iov_len = cnt - (p - tbuf);
319 		iov[1].iov_base = "\r\n";
320 		iov[1].iov_len = 2;
321 		(void)writev(fd, iov, 2);
322 		(void)close(fd);
323 	}
324 }
325 
326 static void
327 disconnectlog_r(data)
328 	struct syslog_data *data;
329 {
330 	/*
331 	 * If the user closed the FD and opened another in the same slot,
332 	 * that's their problem.  They should close it before calling on
333 	 * system services.
334 	 */
335 	if (data->log_file != -1) {
336 		close(data->log_file);
337 		data->log_file = -1;
338 	}
339 	data->connected = 0;		/* retry connect */
340 }
341 
342 static void
343 connectlog_r(data)
344 	struct syslog_data *data;
345 {
346 	struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
347 
348 	if (data->log_file == -1) {
349 		if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
350 			return;
351 		(void)fcntl(data->log_file, F_SETFD, 1);
352 	}
353 	if (data->log_file != -1 && !data->connected) {
354 		memset(&SyslogAddr, '\0', sizeof(SyslogAddr));
355 		SyslogAddr.sun_len = sizeof(SyslogAddr);
356 		SyslogAddr.sun_family = AF_UNIX;
357 		strlcpy(SyslogAddr.sun_path, _PATH_LOG,
358 		    sizeof(SyslogAddr.sun_path));
359 		if (connect(data->log_file, (struct sockaddr *)&SyslogAddr,
360 		    sizeof(SyslogAddr)) == -1) {
361 			(void)close(data->log_file);
362 			data->log_file = -1;
363 		} else
364 			data->connected = 1;
365 	}
366 }
367 
368 void
369 openlog_r(ident, logstat, logfac, data)
370 	const char *ident;
371 	int logstat, logfac;
372 	struct syslog_data *data;
373 {
374 	if (ident != NULL)
375 		data->log_tag = ident;
376 	data->log_stat = logstat;
377 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
378 		data->log_fac = logfac;
379 
380 	if (data->log_stat & LOG_NDELAY)	/* open immediately */
381 		connectlog_r(data);
382 
383 	data->opened = 1;	/* ident and facility has been set */
384 }
385 
386 void
387 closelog_r(data)
388 	struct syslog_data *data;
389 {
390 	(void)close(data->log_file);
391 	data->log_file = -1;
392 	data->connected = 0;
393 }
394 
395 /* setlogmask -- set the log mask level */
396 int
397 setlogmask_r(pmask, data)
398 	int pmask;
399 	struct syslog_data *data;
400 {
401 	int omask;
402 
403 	omask = data->log_mask;
404 	if (pmask != 0)
405 		data->log_mask = pmask;
406 	return (omask);
407 }
408