xref: /openbsd-src/lib/libc/gen/syslog_r.c (revision d4741794dd2f512d997014f8bd85fbb24d935059)
1 /*	$OpenBSD: syslog_r.c,v 1.16 2016/10/19 16:09:24 millert Exp $ */
2 /*
3  * Copyright (c) 1983, 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 #include <netdb.h>
34 
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <limits.h>
45 #include <stdarg.h>
46 
47 /* Reentrant version of syslog, i.e. syslog_r() */
48 void
49 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
50 {
51 	va_list ap;
52 
53 	va_start(ap, fmt);
54 	vsyslog_r(pri, data, fmt, ap);
55 	va_end(ap);
56 }
57 DEF_WEAK(syslog_r);
58 
59 void
60 vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
61 {
62 	__vsyslog_r(pri, data, 1, fmt, ap);
63 }
64 DEF_WEAK(vsyslog_r);
65 
66 /*
67  * This is used by both syslog_r and syslog.
68  */
69 void
70 __vsyslog_r(int pri, struct syslog_data *data,
71     int reentrant, const char *fmt, va_list ap)
72 {
73 	int cnt;
74 	char ch, *p, *t;
75 	int saved_errno;
76 #define	TBUF_LEN	(8192+1)
77 #define	FMT_LEN		1024
78 	char *conp = NULL, *stdp = NULL, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
79 	int tbuf_left, fmt_left, prlen;
80 
81 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
82 	/* Check for invalid bits. */
83 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
84 		syslog_r(INTERNALLOG, data,
85 		    "syslog%s: unknown facility/priority: %x",
86 		    reentrant ? "_r" : "", pri);
87 		pri &= LOG_PRIMASK|LOG_FACMASK;
88 	}
89 
90 	/* Check priority against setlogmask values. */
91 	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
92 		return;
93 
94 	saved_errno = errno;
95 
96 	/* Set default facility if none specified. */
97 	if ((pri & LOG_FACMASK) == 0)
98 		pri |= data->log_fac;
99 
100 	p = tbuf;
101 	tbuf_left = TBUF_LEN;
102 
103 #define	DEC()	\
104 	do {					\
105 		if (prlen < 0)			\
106 			prlen = 0;		\
107 		if (prlen >= tbuf_left)		\
108 			prlen = tbuf_left - 1;	\
109 		p += prlen;			\
110 		tbuf_left -= prlen;		\
111 	} while (0)
112 
113 	prlen = snprintf(p, tbuf_left, "<%d>", pri);
114 	DEC();
115 	if (data->log_stat & LOG_CONS)
116 		conp = p;
117 
118 	if (data->log_stat & LOG_PERROR)
119 		stdp = p;
120 	if (data->log_tag == NULL)
121 		data->log_tag = __progname;
122 	if (data->log_tag != NULL) {
123 		prlen = snprintf(p, tbuf_left, "%.*s", NAME_MAX, data->log_tag);
124 		DEC();
125 	}
126 	if (data->log_stat & LOG_PID) {
127 		prlen = snprintf(p, tbuf_left, "[%ld]", (long)getpid());
128 		DEC();
129 	}
130 	if (data->log_tag != NULL) {
131 		if (tbuf_left > 1) {
132 			*p++ = ':';
133 			tbuf_left--;
134 		}
135 		if (tbuf_left > 1) {
136 			*p++ = ' ';
137 			tbuf_left--;
138 		}
139 	}
140 
141 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
142 		if (ch == '%' && fmt[1] == 'm') {
143 			char ebuf[NL_TEXTMAX];
144 
145 			++fmt;
146 			(void)strerror_r(saved_errno, ebuf, sizeof(ebuf));
147 			prlen = snprintf(t, fmt_left, "%s", ebuf);
148 			if (prlen < 0)
149 				prlen = 0;
150 			if (prlen >= fmt_left)
151 				prlen = fmt_left - 1;
152 			t += prlen;
153 			fmt_left -= prlen;
154 		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
155 			*t++ = '%';
156 			*t++ = '%';
157 			fmt++;
158 			fmt_left -= 2;
159 		} else {
160 			if (fmt_left > 1) {
161 				*t++ = ch;
162 				fmt_left--;
163 			}
164 		}
165 	}
166 	*t = '\0';
167 
168 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
169 	DEC();
170 	cnt = p - tbuf;
171 	while (cnt > 0 && p[-1] == '\n') {
172 		*(--p) = '\0';
173 		--cnt;
174 	}
175 
176 	/* Output to stderr if requested. */
177 	if (data->log_stat & LOG_PERROR) {
178 		struct iovec iov[2];
179 
180 		iov[0].iov_base = stdp;
181 		iov[0].iov_len = cnt > stdp - tbuf ? cnt - (stdp - tbuf) : 0;
182 		iov[1].iov_base = "\n";
183 		iov[1].iov_len = 1;
184 		(void)writev(STDERR_FILENO, iov, 2);
185 	}
186 
187 	/*
188 	 * If the sendsyslog() fails, it means that syslogd
189 	 * is not running or the kernel ran out of buffers.
190 	 */
191 	sendsyslog(tbuf, cnt, data->log_stat & LOG_CONS);
192 }
193 
194 void
195 openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
196 {
197 	if (ident != NULL)
198 		data->log_tag = ident;
199 	data->log_stat = logstat;
200 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
201 		data->log_fac = logfac;
202 }
203 DEF_WEAK(openlog_r);
204 
205 void
206 closelog_r(struct syslog_data *data)
207 {
208 	data->log_tag = NULL;
209 }
210 DEF_WEAK(closelog_r);
211