xref: /netbsd-src/lib/libc/gen/xsyslog.c (revision 4b48a30a7acdad6340e752637e3c0ee3f5ef678d)
1 /*	$NetBSD: xsyslog.c,v 1.4 2017/03/22 19:36:04 kre Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)syslog.c	8.5 (Berkeley) 4/29/95";
36 #else
37 __RCSID("$NetBSD: xsyslog.c,v 1.4 2017/03/22 19:36:04 kre Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #include <sys/uio.h>
47 #include <sys/un.h>
48 
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdarg.h>
52 #include <string.h>
53 #include <fcntl.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <paths.h>
57 #include "syslog_private.h"
58 #include "reentrant.h"
59 #include "extern.h"
60 
61 static void
62 disconnectlog_r(struct syslog_data *data)
63 {
64 	/*
65 	 * If the user closed the FD and opened another in the same slot,
66 	 * that's their problem.  They should close it before calling on
67 	 * system services.
68 	 */
69 	if (data->log_file != -1) {
70 		(void)close(data->log_file);
71 		data->log_file = -1;
72 	}
73 	data->log_connected = 0;		/* retry connect */
74 }
75 
76 static void
77 connectlog_r(struct syslog_data *data)
78 {
79 	/* AF_UNIX address of local logger */
80 	static const struct sockaddr_un sun = {
81 		.sun_family = AF_LOCAL,
82 		.sun_len = sizeof(sun),
83 		.sun_path = _PATH_LOG,
84 	};
85 
86 	if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) {
87 		if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
88 		    0)) == -1)
89 			return;
90 		data->log_connected = 0;
91 	}
92 	if (!data->log_connected) {
93 		if (connect(data->log_file,
94 		    (const struct sockaddr *)(const void *)&sun,
95 		    (socklen_t)sizeof(sun)) == -1) {
96 			(void)close(data->log_file);
97 			data->log_file = -1;
98 		} else
99 			data->log_connected = 1;
100 	}
101 }
102 
103 void
104 _openlog_unlocked_r(const char *ident, int logstat, int logfac,
105     struct syslog_data *data)
106 {
107 	if (ident != NULL)
108 		data->log_tag = ident;
109 	data->log_stat = logstat;
110 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
111 		data->log_fac = logfac;
112 
113 	if (data->log_stat & LOG_NDELAY)	/* open immediately */
114 		connectlog_r(data);
115 
116 	data->log_opened = 1;
117 }
118 
119 void
120 _closelog_unlocked_r(struct syslog_data *data)
121 {
122 	(void)close(data->log_file);
123 	data->log_file = -1;
124 	data->log_connected = 0;
125 }
126 
127 static void
128 _xsyslogp_r(int pri, struct syslog_fun *fun,
129     struct syslog_data *data, const char *msgid,
130     const char *sdfmt, const char *msgfmt, ...)
131 {
132 	va_list ap;
133 	va_start(ap, msgfmt);
134 	_vxsyslogp_r(pri, fun, data, msgid, sdfmt, msgfmt, ap);
135 	va_end(ap);
136 }
137 
138 void
139 _vxsyslogp_r(int pri, struct syslog_fun *fun,
140     struct syslog_data *data, const char *msgid,
141     const char *sdfmt, const char *msgfmt, va_list ap)
142 {
143 	static const char BRCOSP[] = "]: ";
144 	static const char CRLF[] = "\r\n";
145 	size_t cnt, prlen, tries;
146 	char ch, *p, *t;
147 	int fd, saved_errno;
148 #define TBUF_LEN	2048
149 #define FMT_LEN		1024
150 #define MAXTRIES	10
151 	char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = "";
152 	size_t tbuf_left, fmt_left, msgsdlen;
153 	char *fmt = fmt_cat;
154 	struct iovec iov[7];	/* prog + [ + pid + ]: + fmt + crlf */
155 	int opened, iovcnt;
156 
157 	opened = 0;
158 
159 #define INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
160 	/* Check for invalid bits. */
161 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
162 		_xsyslogp_r(INTERNALLOG, &_syslog_ss_fun, data, NULL, NULL,
163 		    "%s: unknown facility/priority: %x", pri);
164 		pri &= LOG_PRIMASK|LOG_FACMASK;
165 	}
166 
167 	/* Check priority against setlogmask values. */
168 	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
169 		return;
170 
171 	saved_errno = errno;
172 
173 	/* Set default facility if none specified. */
174 	if ((pri & LOG_FACMASK) == 0)
175 		pri |= data->log_fac;
176 
177 	/* Build the message. */
178 	p = tbuf;
179 	tbuf_left = TBUF_LEN;
180 
181 	prlen = snprintf_ss(p, tbuf_left, "<%d>1 ", pri);
182 	DEC();
183 
184 	prlen = (*fun->timefun)(p, tbuf_left);
185 
186 	(*fun->lock)(data);
187 
188 	if (data->log_hostname[0] == '\0' && gethostname(data->log_hostname,
189 	    sizeof(data->log_hostname)) == -1) {
190 		/* can this really happen? */
191 		data->log_hostname[0] = '-';
192 		data->log_hostname[1] = '\0';
193 	}
194 
195 	DEC();
196 	prlen = snprintf_ss(p, tbuf_left, " %s ", data->log_hostname);
197 
198 	if (data->log_tag == NULL)
199 		data->log_tag = getprogname();
200 
201 	DEC();
202 	prlen = snprintf_ss(p, tbuf_left, "%s ",
203 	    data->log_tag ? data->log_tag : "-");
204 
205 	(*fun->unlock)(data);
206 
207 	if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
208 		iovcnt = 0;
209 		iov[iovcnt].iov_base = p;
210 		iov[iovcnt].iov_len = prlen - 1;
211 		iovcnt++;
212 	}
213 	DEC();
214 
215 	if (data->log_stat & LOG_PID) {
216 		prlen = snprintf_ss(p, tbuf_left, "%d ", getpid());
217 		if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
218 			iov[iovcnt].iov_base = __UNCONST("[");
219 			iov[iovcnt].iov_len = 1;
220 			iovcnt++;
221 			iov[iovcnt].iov_base = p;
222 			iov[iovcnt].iov_len = prlen - 1;
223 			iovcnt++;
224 			iov[iovcnt].iov_base = __UNCONST(BRCOSP);
225 			iov[iovcnt].iov_len = 3;
226 			iovcnt++;
227 		}
228 	} else {
229 		prlen = snprintf_ss(p, tbuf_left, "- ");
230 		if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
231 			iov[iovcnt].iov_base = __UNCONST(BRCOSP + 1);
232 			iov[iovcnt].iov_len = 2;
233 			iovcnt++;
234 		}
235 	}
236 	DEC();
237 
238 	/*
239 	 * concat the format strings, then use one vsnprintf()
240 	 */
241 	if (msgid != NULL && *msgid != '\0') {
242 		strlcat(fmt_cat, msgid, FMT_LEN);
243 		strlcat(fmt_cat, " ", FMT_LEN);
244 	} else
245 		strlcat(fmt_cat, "- ", FMT_LEN);
246 
247 	if (sdfmt != NULL && *sdfmt != '\0') {
248 		strlcat(fmt_cat, sdfmt, FMT_LEN);
249 	} else
250 		strlcat(fmt_cat, "-", FMT_LEN);
251 
252 	if (data->log_stat & (LOG_PERROR|LOG_CONS))
253 		msgsdlen = strlen(fmt_cat) + 1;
254 	else
255 		msgsdlen = 0;	/* XXX: GCC */
256 
257 	if (msgfmt != NULL && *msgfmt != '\0') {
258 		strlcat(fmt_cat, " ", FMT_LEN);
259 		strlcat(fmt_cat, msgfmt, FMT_LEN);
260 	}
261 
262 	/*
263 	 * We wouldn't need this mess if printf handled %m, or if
264 	 * strerror() had been invented before syslog().
265 	 */
266 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
267 		if (ch == '%' && fmt[1] == 'm') {
268 			char buf[256];
269 
270 			if ((*fun->errfun)(saved_errno, buf, sizeof(buf)) != 0)
271 				prlen = snprintf_ss(t, fmt_left, "Error %d",
272 				    saved_errno);
273 			else
274 				prlen = strlcpy(t, buf, fmt_left);
275 			if (prlen >= fmt_left)
276 				prlen = fmt_left - 1;
277 			t += prlen;
278 			fmt++;
279 			fmt_left -= prlen;
280 		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
281 			*t++ = '%';
282 			*t++ = '%';
283 			fmt++;
284 			fmt_left -= 2;
285 		} else {
286 			if (fmt_left > 1) {
287 				*t++ = ch;
288 				fmt_left--;
289 			}
290 		}
291 	}
292 	*t = '\0';
293 
294 	prlen = (*fun->prfun)(p, tbuf_left, fmt_cpy, ap);
295 	if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
296 		iov[iovcnt].iov_base = p + msgsdlen;
297 		iov[iovcnt].iov_len = prlen - msgsdlen;
298 		iovcnt++;
299 	}
300 
301 	DEC();
302 	cnt = p - tbuf;
303 
304 	/* Output to stderr if requested. */
305 	if (data->log_stat & LOG_PERROR) {
306 		struct iovec *piov;
307 		int piovcnt;
308 
309 		iov[iovcnt].iov_base = __UNCONST(CRLF + 1);
310 		iov[iovcnt].iov_len = 1;
311 		if (data->log_stat & LOG_PTRIM) {
312 			piov = &iov[iovcnt - 1];
313 			piovcnt = 2;
314 		} else {
315 			piov = iov;
316 			piovcnt = iovcnt + 1;
317 		}
318 		(void)writev(STDERR_FILENO, piov, piovcnt);
319 	}
320 
321 	if (data->log_stat & LOG_NLOG)
322 		goto out;
323 
324 	/* Get connected, output the message to the local logger. */
325 	(*fun->lock)(data);
326 	opened = !data->log_opened;
327 	if (opened)
328 		_openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
329 	connectlog_r(data);
330 
331 	/*
332 	 * If the send() failed, there are two likely scenarios:
333 	 *  1) syslogd was restarted
334 	 *  2) /dev/log is out of socket buffer space
335 	 * We attempt to reconnect to /dev/log to take care of
336 	 * case #1 and keep send()ing data to cover case #2
337 	 * to give syslogd a chance to empty its socket buffer.
338 	 */
339 	for (tries = 0; tries < MAXTRIES; tries++) {
340 		if (send(data->log_file, tbuf, cnt, 0) != -1)
341 			break;
342 		if (errno != ENOBUFS) {
343 			disconnectlog_r(data);
344 			connectlog_r(data);
345 		} else
346 			(void)usleep(1);
347 	}
348 
349 	/*
350 	 * Output the message to the console; try not to block
351 	 * as a blocking console should not stop other processes.
352 	 * Make sure the error reported is the one from the syslogd failure.
353 	 */
354 	if (tries == MAXTRIES && (data->log_stat & LOG_CONS) &&
355 	    (fd = open(_PATH_CONSOLE,
356 		O_WRONLY | O_NONBLOCK | O_CLOEXEC, 0)) >= 0) {
357 		iov[iovcnt].iov_base = __UNCONST(CRLF);
358 		iov[iovcnt].iov_len = 2;
359 		(void)writev(fd, iov, iovcnt + 1);
360 		(void)close(fd);
361 	}
362 
363 out:
364 	if (!(*fun->unlock)(data) && opened)
365 		_closelog_unlocked_r(data);
366 }
367 
368 int
369 setlogmask_r(int pmask, struct syslog_data *data)
370 {
371 	int omask;
372 
373 	omask = data->log_mask;
374 	if (pmask != 0)
375 		data->log_mask = pmask;
376 	return omask;
377 }
378