xref: /openbsd/lib/libc/gen/syslog_r.c (revision 264ca280)
1 /*	$OpenBSD: syslog_r.c,v 1.15 2016/03/27 16:28:56 chl 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 	/* strerror() is not reentrant */
142 
143 	for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
144 		if (ch == '%' && fmt[1] == 'm') {
145 			++fmt;
146 			if (reentrant) {
147 				prlen = snprintf(t, fmt_left, "Error %d",
148 				    saved_errno);
149 			} else {
150 				prlen = snprintf(t, fmt_left, "%s",
151 				    strerror(saved_errno));
152 			}
153 			if (prlen < 0)
154 				prlen = 0;
155 			if (prlen >= fmt_left)
156 				prlen = fmt_left - 1;
157 			t += prlen;
158 			fmt_left -= prlen;
159 		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
160 			*t++ = '%';
161 			*t++ = '%';
162 			fmt++;
163 			fmt_left -= 2;
164 		} else {
165 			if (fmt_left > 1) {
166 				*t++ = ch;
167 				fmt_left--;
168 			}
169 		}
170 	}
171 	*t = '\0';
172 
173 	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
174 	DEC();
175 	cnt = p - tbuf;
176 	while (cnt > 0 && p[-1] == '\n') {
177 		*(--p) = '\0';
178 		--cnt;
179 	}
180 
181 	/* Output to stderr if requested. */
182 	if (data->log_stat & LOG_PERROR) {
183 		struct iovec iov[2];
184 
185 		iov[0].iov_base = stdp;
186 		iov[0].iov_len = cnt > stdp - tbuf ? cnt - (stdp - tbuf) : 0;
187 		iov[1].iov_base = "\n";
188 		iov[1].iov_len = 1;
189 		(void)writev(STDERR_FILENO, iov, 2);
190 	}
191 
192 	/*
193 	 * If the sendsyslog() fails, it means that syslogd
194 	 * is not running or the kernel ran out of buffers.
195 	 */
196 	sendsyslog(tbuf, cnt, data->log_stat & LOG_CONS);
197 }
198 
199 void
200 openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
201 {
202 	if (ident != NULL)
203 		data->log_tag = ident;
204 	data->log_stat = logstat;
205 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
206 		data->log_fac = logfac;
207 }
208 DEF_WEAK(openlog_r);
209 
210 void
211 closelog_r(struct syslog_data *data)
212 {
213 	data->log_tag = NULL;
214 }
215 DEF_WEAK(closelog_r);
216