syslog.c revision 1.45.2.3 1 1.45.2.3 christos /* $NetBSD: syslog.c,v 1.45.2.3 2009/01/04 17:02:19 christos Exp $ */
2 1.45.2.2 christos
3 1.45.2.2 christos /*
4 1.45.2.2 christos * Copyright (c) 1983, 1988, 1993
5 1.45.2.2 christos * The Regents of the University of California. All rights reserved.
6 1.45.2.2 christos *
7 1.45.2.2 christos * Redistribution and use in source and binary forms, with or without
8 1.45.2.2 christos * modification, are permitted provided that the following conditions
9 1.45.2.2 christos * are met:
10 1.45.2.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.45.2.2 christos * notice, this list of conditions and the following disclaimer.
12 1.45.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.45.2.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.45.2.2 christos * documentation and/or other materials provided with the distribution.
15 1.45.2.2 christos * 3. Neither the name of the University nor the names of its contributors
16 1.45.2.2 christos * may be used to endorse or promote products derived from this software
17 1.45.2.2 christos * without specific prior written permission.
18 1.45.2.2 christos *
19 1.45.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.45.2.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.45.2.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.45.2.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.45.2.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.45.2.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.45.2.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.45.2.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.45.2.2 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.45.2.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.45.2.2 christos * SUCH DAMAGE.
30 1.45.2.2 christos */
31 1.45.2.2 christos
32 1.45.2.2 christos #include <sys/cdefs.h>
33 1.45.2.2 christos #if defined(LIBC_SCCS) && !defined(lint)
34 1.45.2.2 christos #if 0
35 1.45.2.2 christos static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
36 1.45.2.2 christos #else
37 1.45.2.3 christos __RCSID("$NetBSD: syslog.c,v 1.45.2.3 2009/01/04 17:02:19 christos Exp $");
38 1.45.2.2 christos #endif
39 1.45.2.2 christos #endif /* LIBC_SCCS and not lint */
40 1.45.2.2 christos
41 1.45.2.2 christos #include "namespace.h"
42 1.45.2.2 christos #include <sys/types.h>
43 1.45.2.2 christos #include <sys/param.h>
44 1.45.2.2 christos #include <sys/socket.h>
45 1.45.2.2 christos #include <sys/syslog.h>
46 1.45.2.2 christos #include <sys/uio.h>
47 1.45.2.2 christos #include <sys/un.h>
48 1.45.2.2 christos #include <netdb.h>
49 1.45.2.2 christos
50 1.45.2.2 christos #include <errno.h>
51 1.45.2.2 christos #include <fcntl.h>
52 1.45.2.2 christos #include <paths.h>
53 1.45.2.2 christos #include <stdarg.h>
54 1.45.2.2 christos #include <stdio.h>
55 1.45.2.2 christos #include <stdlib.h>
56 1.45.2.2 christos #include <string.h>
57 1.45.2.2 christos #include <time.h>
58 1.45.2.2 christos #include <unistd.h>
59 1.45.2.2 christos #include "reentrant.h"
60 1.45.2.2 christos #include "extern.h"
61 1.45.2.2 christos
62 1.45.2.2 christos #ifdef __weak_alias
63 1.45.2.2 christos __weak_alias(closelog,_closelog)
64 1.45.2.2 christos __weak_alias(openlog,_openlog)
65 1.45.2.2 christos __weak_alias(setlogmask,_setlogmask)
66 1.45.2.2 christos __weak_alias(syslog,_syslog)
67 1.45.2.2 christos __weak_alias(vsyslog,_vsyslog)
68 1.45.2.2 christos __weak_alias(syslogp,_syslogp)
69 1.45.2.2 christos __weak_alias(vsyslogp,_vsyslogp)
70 1.45.2.2 christos
71 1.45.2.2 christos __weak_alias(closelog_r,_closelog_r)
72 1.45.2.2 christos __weak_alias(openlog_r,_openlog_r)
73 1.45.2.2 christos __weak_alias(setlogmask_r,_setlogmask_r)
74 1.45.2.2 christos __weak_alias(syslog_r,_syslog_r)
75 1.45.2.2 christos __weak_alias(vsyslog_r,_vsyslog_r)
76 1.45.2.2 christos __weak_alias(syslog_ss,_syslog_ss)
77 1.45.2.2 christos __weak_alias(vsyslog_ss,_vsyslog_ss)
78 1.45.2.2 christos __weak_alias(syslogp_r,_syslogp_r)
79 1.45.2.2 christos __weak_alias(vsyslogp_r,_vsyslogp_r)
80 1.45.2.2 christos __weak_alias(syslogp_ss,_syslogp_ss)
81 1.45.2.2 christos __weak_alias(vsyslogp_ss,_vsyslogp_ss)
82 1.45.2.2 christos #endif
83 1.45.2.2 christos
84 1.45.2.2 christos static struct syslog_data sdata = SYSLOG_DATA_INIT;
85 1.45.2.2 christos
86 1.45.2.2 christos static void openlog_unlocked_r(const char *, int, int,
87 1.45.2.2 christos struct syslog_data *);
88 1.45.2.2 christos static void disconnectlog_r(struct syslog_data *);
89 1.45.2.2 christos static void connectlog_r(struct syslog_data *);
90 1.45.2.2 christos
91 1.45.2.2 christos #define LOG_SIGNAL_SAFE (int)0x80000000
92 1.45.2.2 christos
93 1.45.2.2 christos
94 1.45.2.2 christos #ifdef _REENTRANT
95 1.45.2.2 christos static mutex_t syslog_mutex = MUTEX_INITIALIZER;
96 1.45.2.2 christos #endif
97 1.45.2.2 christos
98 1.45.2.2 christos static char hostname[MAXHOSTNAMELEN];
99 1.45.2.2 christos
100 1.45.2.2 christos /*
101 1.45.2.2 christos * syslog, vsyslog --
102 1.45.2.2 christos * print message on log file; output is intended for syslogd(8).
103 1.45.2.2 christos */
104 1.45.2.2 christos void
105 1.45.2.2 christos syslog(int pri, const char *fmt, ...)
106 1.45.2.2 christos {
107 1.45.2.2 christos va_list ap;
108 1.45.2.2 christos
109 1.45.2.2 christos va_start(ap, fmt);
110 1.45.2.2 christos vsyslog(pri, fmt, ap);
111 1.45.2.2 christos va_end(ap);
112 1.45.2.2 christos }
113 1.45.2.2 christos
114 1.45.2.2 christos void
115 1.45.2.2 christos vsyslog(int pri, const char *fmt, va_list ap)
116 1.45.2.2 christos {
117 1.45.2.2 christos vsyslog_r(pri, &sdata, fmt, ap);
118 1.45.2.2 christos }
119 1.45.2.2 christos
120 1.45.2.2 christos /*
121 1.45.2.2 christos * syslogp, vsyslogp --
122 1.45.2.2 christos * like syslog but take additional arguments for MSGID and SD
123 1.45.2.2 christos */
124 1.45.2.2 christos void
125 1.45.2.2 christos syslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, ...)
126 1.45.2.2 christos {
127 1.45.2.2 christos va_list ap;
128 1.45.2.2 christos
129 1.45.2.2 christos va_start(ap, msgfmt);
130 1.45.2.2 christos vsyslogp(pri, msgid, sdfmt, msgfmt, ap);
131 1.45.2.2 christos va_end(ap);
132 1.45.2.2 christos }
133 1.45.2.2 christos
134 1.45.2.2 christos void
135 1.45.2.2 christos vsyslogp(int pri, const char *msgid, const char *sdfmt, const char *msgfmt, va_list ap)
136 1.45.2.2 christos {
137 1.45.2.2 christos vsyslogp_r(pri, &sdata, msgid, sdfmt, msgfmt, ap);
138 1.45.2.2 christos }
139 1.45.2.2 christos
140 1.45.2.2 christos void
141 1.45.2.2 christos openlog(const char *ident, int logstat, int logfac)
142 1.45.2.2 christos {
143 1.45.2.2 christos openlog_r(ident, logstat, logfac, &sdata);
144 1.45.2.2 christos }
145 1.45.2.2 christos
146 1.45.2.2 christos void
147 1.45.2.2 christos closelog(void)
148 1.45.2.2 christos {
149 1.45.2.2 christos closelog_r(&sdata);
150 1.45.2.2 christos }
151 1.45.2.2 christos
152 1.45.2.2 christos /* setlogmask -- set the log mask level */
153 1.45.2.2 christos int
154 1.45.2.2 christos setlogmask(int pmask)
155 1.45.2.2 christos {
156 1.45.2.2 christos return setlogmask_r(pmask, &sdata);
157 1.45.2.2 christos }
158 1.45.2.2 christos
159 1.45.2.2 christos /* Reentrant version of syslog, i.e. syslog_r() */
160 1.45.2.2 christos
161 1.45.2.2 christos void
162 1.45.2.2 christos syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
163 1.45.2.2 christos {
164 1.45.2.2 christos va_list ap;
165 1.45.2.2 christos
166 1.45.2.2 christos va_start(ap, fmt);
167 1.45.2.2 christos vsyslog_r(pri, data, fmt, ap);
168 1.45.2.2 christos va_end(ap);
169 1.45.2.2 christos }
170 1.45.2.2 christos
171 1.45.2.2 christos void
172 1.45.2.2 christos syslogp_r(int pri, struct syslog_data *data, const char *msgid,
173 1.45.2.2 christos const char *sdfmt, const char *msgfmt, ...)
174 1.45.2.2 christos {
175 1.45.2.2 christos va_list ap;
176 1.45.2.2 christos
177 1.45.2.2 christos va_start(ap, msgfmt);
178 1.45.2.2 christos vsyslogp_r(pri, data, msgid, sdfmt, msgfmt, ap);
179 1.45.2.2 christos va_end(ap);
180 1.45.2.2 christos }
181 1.45.2.2 christos
182 1.45.2.2 christos void
183 1.45.2.2 christos syslog_ss(int pri, struct syslog_data *data, const char *fmt, ...)
184 1.45.2.2 christos {
185 1.45.2.2 christos va_list ap;
186 1.45.2.2 christos
187 1.45.2.2 christos va_start(ap, fmt);
188 1.45.2.2 christos vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap);
189 1.45.2.2 christos va_end(ap);
190 1.45.2.2 christos }
191 1.45.2.2 christos
192 1.45.2.2 christos void
193 1.45.2.2 christos syslogp_ss(int pri, struct syslog_data *data, const char *msgid,
194 1.45.2.2 christos const char *sdfmt, const char *msgfmt, ...)
195 1.45.2.2 christos {
196 1.45.2.2 christos va_list ap;
197 1.45.2.2 christos
198 1.45.2.2 christos va_start(ap, msgfmt);
199 1.45.2.2 christos vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap);
200 1.45.2.2 christos va_end(ap);
201 1.45.2.2 christos }
202 1.45.2.2 christos
203 1.45.2.2 christos void
204 1.45.2.2 christos vsyslog_ss(int pri, struct syslog_data *data, const char *fmt, va_list ap)
205 1.45.2.2 christos {
206 1.45.2.2 christos vsyslog_r(pri | LOG_SIGNAL_SAFE, data, fmt, ap);
207 1.45.2.2 christos }
208 1.45.2.2 christos
209 1.45.2.2 christos void
210 1.45.2.2 christos vsyslogp_ss(int pri, struct syslog_data *data, const char *msgid,
211 1.45.2.2 christos const char *sdfmt, const char *msgfmt, va_list ap)
212 1.45.2.2 christos {
213 1.45.2.2 christos vsyslogp_r(pri | LOG_SIGNAL_SAFE, data, msgid, sdfmt, msgfmt, ap);
214 1.45.2.2 christos }
215 1.45.2.2 christos
216 1.45.2.2 christos
217 1.45.2.2 christos void
218 1.45.2.2 christos vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
219 1.45.2.2 christos {
220 1.45.2.2 christos vsyslogp_r(pri, data, NULL, NULL, fmt, ap);
221 1.45.2.2 christos }
222 1.45.2.2 christos
223 1.45.2.2 christos void
224 1.45.2.2 christos vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
225 1.45.2.2 christos const char *sdfmt, const char *msgfmt, va_list ap)
226 1.45.2.2 christos {
227 1.45.2.2 christos size_t cnt, prlen, tries;
228 1.45.2.2 christos char ch, *p, *t;
229 1.45.2.2 christos struct timeval tv;
230 1.45.2.2 christos struct tm tmnow;
231 1.45.2.2 christos time_t now;
232 1.45.2.2 christos int fd, saved_errno;
233 1.45.2.2 christos #define TBUF_LEN 2048
234 1.45.2.2 christos #define FMT_LEN 1024
235 1.45.2.2 christos #define MAXTRIES 10
236 1.45.2.2 christos char *stdp = NULL; /* pacify gcc */
237 1.45.2.2 christos char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = "";
238 1.45.2.2 christos size_t tbuf_left, fmt_left;
239 1.45.2.2 christos char *fmt = fmt_cat;
240 1.45.2.2 christos int signal_safe = pri & LOG_SIGNAL_SAFE;
241 1.45.2.2 christos int opened;
242 1.45.2.2 christos
243 1.45.2.2 christos pri &= ~LOG_SIGNAL_SAFE;
244 1.45.2.2 christos
245 1.45.2.2 christos #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
246 1.45.2.2 christos /* Check for invalid bits. */
247 1.45.2.2 christos if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
248 1.45.2.2 christos syslog_r(INTERNALLOG | signal_safe, data,
249 1.45.2.2 christos "syslog_r: unknown facility/priority: %x", pri);
250 1.45.2.2 christos pri &= LOG_PRIMASK|LOG_FACMASK;
251 1.45.2.2 christos }
252 1.45.2.2 christos
253 1.45.2.2 christos /* Check priority against setlogmask values. */
254 1.45.2.2 christos if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
255 1.45.2.2 christos return;
256 1.45.2.2 christos
257 1.45.2.2 christos saved_errno = errno;
258 1.45.2.2 christos
259 1.45.2.2 christos /* Set default facility if none specified. */
260 1.45.2.2 christos if ((pri & LOG_FACMASK) == 0)
261 1.45.2.2 christos pri |= data->log_fac;
262 1.45.2.2 christos
263 1.45.2.2 christos /* Build the message. */
264 1.45.2.2 christos p = tbuf;
265 1.45.2.2 christos tbuf_left = TBUF_LEN;
266 1.45.2.2 christos
267 1.45.2.2 christos #define DEC() \
268 1.45.2.2 christos do { \
269 1.45.2.2 christos if (prlen >= tbuf_left) \
270 1.45.2.2 christos prlen = tbuf_left - 1; \
271 1.45.2.2 christos p += prlen; \
272 1.45.2.2 christos tbuf_left -= prlen; \
273 1.45.2.2 christos } while (/*CONSTCOND*/0)
274 1.45.2.2 christos
275 1.45.2.2 christos prlen = snprintf_ss(p, tbuf_left, "<%d>1 ", pri);
276 1.45.2.2 christos DEC();
277 1.45.2.2 christos
278 1.45.2.2 christos if (!signal_safe && (gettimeofday(&tv, NULL) != -1)) {
279 1.45.2.2 christos /* strftime() implies tzset(), localtime_r() doesn't. */
280 1.45.2.2 christos tzset();
281 1.45.2.2 christos now = (time_t) tv.tv_sec;
282 1.45.2.2 christos localtime_r(&now, &tmnow);
283 1.45.2.2 christos
284 1.45.2.2 christos prlen = strftime(p, tbuf_left, "%FT%T", &tmnow);
285 1.45.2.2 christos DEC();
286 1.45.2.2 christos prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec);
287 1.45.2.2 christos DEC();
288 1.45.2.2 christos prlen = strftime(p, tbuf_left-1, "%z", &tmnow);
289 1.45.2.2 christos /* strftime gives eg. "+0200", but we need "+02:00" */
290 1.45.2.2 christos if (prlen == 5) {
291 1.45.2.2 christos p[prlen+1] = p[prlen];
292 1.45.2.2 christos p[prlen] = p[prlen-1];
293 1.45.2.2 christos p[prlen-1] = p[prlen-2];
294 1.45.2.2 christos p[prlen-2] = ':';
295 1.45.2.2 christos prlen += 1;
296 1.45.2.2 christos }
297 1.45.2.2 christos } else {
298 1.45.2.2 christos prlen = snprintf_ss(p, tbuf_left, "-");
299 1.45.2.2 christos
300 1.45.2.2 christos /* if gmtime_r() was signal-safe we could output the UTC-time:
301 1.45.2.2 christos gmtime_r(&now, &tmnow);
302 1.45.2.2 christos prlen = strftime(p, tbuf_left, "%FT%TZ", &tmnow);
303 1.45.2.2 christos */
304 1.45.2.2 christos }
305 1.45.2.2 christos DEC();
306 1.45.2.2 christos prlen = snprintf_ss(p, tbuf_left, " %s ", hostname);
307 1.45.2.2 christos DEC();
308 1.45.2.2 christos
309 1.45.2.2 christos if (data->log_stat & LOG_PERROR)
310 1.45.2.2 christos stdp = p;
311 1.45.2.2 christos if (data->log_tag == NULL)
312 1.45.2.2 christos data->log_tag = getprogname();
313 1.45.2.2 christos
314 1.45.2.2 christos prlen = snprintf_ss(p, tbuf_left, "%s ",
315 1.45.2.2 christos data->log_tag ? data->log_tag : "-");
316 1.45.2.2 christos DEC();
317 1.45.2.2 christos
318 1.45.2.2 christos if (data->log_stat & LOG_PID)
319 1.45.2.2 christos prlen = snprintf_ss(p, tbuf_left, "%d ", getpid());
320 1.45.2.2 christos else
321 1.45.2.2 christos prlen = snprintf_ss(p, tbuf_left, "- ");
322 1.45.2.2 christos DEC();
323 1.45.2.2 christos
324 1.45.2.2 christos /*
325 1.45.2.2 christos * concat the format strings, then use one vsnprintf()
326 1.45.2.2 christos */
327 1.45.2.2 christos if (msgid != NULL && *msgid != '\0') {
328 1.45.2.2 christos strlcat(fmt_cat, msgid, FMT_LEN);
329 1.45.2.2 christos strlcat(fmt_cat, " ", FMT_LEN);
330 1.45.2.2 christos } else
331 1.45.2.2 christos strlcat(fmt_cat, "- ", FMT_LEN);
332 1.45.2.2 christos
333 1.45.2.2 christos if (sdfmt != NULL && *sdfmt != '\0') {
334 1.45.2.2 christos strlcat(fmt_cat, sdfmt, FMT_LEN);
335 1.45.2.2 christos } else
336 1.45.2.2 christos strlcat(fmt_cat, "-", FMT_LEN);
337 1.45.2.2 christos
338 1.45.2.2 christos if (msgfmt != NULL && *msgfmt != '\0') {
339 1.45.2.2 christos strlcat(fmt_cat, " ", FMT_LEN);
340 1.45.2.2 christos strlcat(fmt_cat, msgfmt, FMT_LEN);
341 1.45.2.2 christos }
342 1.45.2.2 christos
343 1.45.2.2 christos /*
344 1.45.2.2 christos * We wouldn't need this mess if printf handled %m, or if
345 1.45.2.2 christos * strerror() had been invented before syslog().
346 1.45.2.2 christos */
347 1.45.2.2 christos for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
348 1.45.2.2 christos if (ch == '%' && fmt[1] == 'm') {
349 1.45.2.2 christos char ebuf[128];
350 1.45.2.2 christos ++fmt;
351 1.45.2.2 christos if (signal_safe ||
352 1.45.2.2 christos strerror_r(saved_errno, ebuf, sizeof(ebuf)))
353 1.45.2.2 christos prlen = snprintf_ss(t, fmt_left, "Error %d",
354 1.45.2.2 christos saved_errno);
355 1.45.2.2 christos else
356 1.45.2.2 christos prlen = snprintf_ss(t, fmt_left, "%s", ebuf);
357 1.45.2.2 christos if (prlen >= fmt_left)
358 1.45.2.2 christos prlen = fmt_left - 1;
359 1.45.2.2 christos t += prlen;
360 1.45.2.2 christos fmt_left -= prlen;
361 1.45.2.2 christos } else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
362 1.45.2.2 christos *t++ = '%';
363 1.45.2.2 christos *t++ = '%';
364 1.45.2.2 christos fmt++;
365 1.45.2.2 christos fmt_left -= 2;
366 1.45.2.2 christos } else {
367 1.45.2.2 christos if (fmt_left > 1) {
368 1.45.2.2 christos *t++ = ch;
369 1.45.2.2 christos fmt_left--;
370 1.45.2.2 christos }
371 1.45.2.2 christos }
372 1.45.2.2 christos }
373 1.45.2.2 christos *t = '\0';
374 1.45.2.2 christos
375 1.45.2.2 christos if (signal_safe)
376 1.45.2.2 christos prlen = vsnprintf_ss(p, tbuf_left, fmt_cpy, ap);
377 1.45.2.2 christos else
378 1.45.2.2 christos prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
379 1.45.2.2 christos DEC();
380 1.45.2.2 christos cnt = p - tbuf;
381 1.45.2.2 christos
382 1.45.2.2 christos /* Output to stderr if requested. */
383 1.45.2.2 christos if (data->log_stat & LOG_PERROR) {
384 1.45.2.2 christos struct iovec iov[2];
385 1.45.2.2 christos
386 1.45.2.2 christos iov[0].iov_base = stdp;
387 1.45.2.2 christos iov[0].iov_len = cnt - (stdp - tbuf);
388 1.45.2.2 christos iov[1].iov_base = __UNCONST("\n");
389 1.45.2.2 christos iov[1].iov_len = 1;
390 1.45.2.2 christos (void)writev(STDERR_FILENO, iov, 2);
391 1.45.2.2 christos }
392 1.45.2.2 christos
393 1.45.2.2 christos /* Get connected, output the message to the local logger. */
394 1.45.2.2 christos if (data == &sdata)
395 1.45.2.2 christos mutex_lock(&syslog_mutex);
396 1.45.2.2 christos opened = !data->opened;
397 1.45.2.2 christos if (opened)
398 1.45.2.2 christos openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
399 1.45.2.2 christos connectlog_r(data);
400 1.45.2.2 christos
401 1.45.2.2 christos /*
402 1.45.2.2 christos * If the send() failed, there are two likely scenarios:
403 1.45.2.2 christos * 1) syslogd was restarted
404 1.45.2.2 christos * 2) /dev/log is out of socket buffer space
405 1.45.2.2 christos * We attempt to reconnect to /dev/log to take care of
406 1.45.2.2 christos * case #1 and keep send()ing data to cover case #2
407 1.45.2.2 christos * to give syslogd a chance to empty its socket buffer.
408 1.45.2.2 christos */
409 1.45.2.2 christos for (tries = 0; tries < MAXTRIES; tries++) {
410 1.45.2.2 christos if (send(data->log_file, tbuf, cnt, 0) != -1)
411 1.45.2.2 christos break;
412 1.45.2.2 christos if (errno != ENOBUFS) {
413 1.45.2.2 christos disconnectlog_r(data);
414 1.45.2.2 christos connectlog_r(data);
415 1.45.2.2 christos } else
416 1.45.2.2 christos (void)usleep(1);
417 1.45.2.2 christos }
418 1.45.2.2 christos
419 1.45.2.2 christos /*
420 1.45.2.2 christos * Output the message to the console; try not to block
421 1.45.2.2 christos * as a blocking console should not stop other processes.
422 1.45.2.2 christos * Make sure the error reported is the one from the syslogd failure.
423 1.45.2.2 christos */
424 1.45.2.2 christos if (tries == MAXTRIES && (data->log_stat & LOG_CONS) &&
425 1.45.2.2 christos (fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0 &&
426 1.45.2.2 christos (p = strchr(tbuf, '>')) != NULL) {
427 1.45.2.2 christos struct iovec iov[2];
428 1.45.2.2 christos iov[0].iov_base = ++p;
429 1.45.2.2 christos iov[0].iov_len = cnt - (p - tbuf);
430 1.45.2.2 christos iov[1].iov_base = __UNCONST("\r\n");
431 1.45.2.2 christos iov[1].iov_len = 2;
432 1.45.2.2 christos (void)writev(fd, iov, 2);
433 1.45.2.2 christos (void)close(fd);
434 1.45.2.2 christos }
435 1.45.2.2 christos
436 1.45.2.2 christos if (data == &sdata)
437 1.45.2.2 christos mutex_unlock(&syslog_mutex);
438 1.45.2.2 christos
439 1.45.2.2 christos if (data != &sdata && opened) {
440 1.45.2.2 christos /* preserve log tag */
441 1.45.2.2 christos const char *ident = data->log_tag;
442 1.45.2.2 christos closelog_r(data);
443 1.45.2.2 christos data->log_tag = ident;
444 1.45.2.2 christos }
445 1.45.2.2 christos }
446 1.45.2.2 christos
447 1.45.2.2 christos static void
448 1.45.2.2 christos disconnectlog_r(struct syslog_data *data)
449 1.45.2.2 christos {
450 1.45.2.2 christos /*
451 1.45.2.2 christos * If the user closed the FD and opened another in the same slot,
452 1.45.2.2 christos * that's their problem. They should close it before calling on
453 1.45.2.2 christos * system services.
454 1.45.2.2 christos */
455 1.45.2.2 christos if (data->log_file != -1) {
456 1.45.2.2 christos (void)close(data->log_file);
457 1.45.2.2 christos data->log_file = -1;
458 1.45.2.2 christos }
459 1.45.2.2 christos data->connected = 0; /* retry connect */
460 1.45.2.2 christos }
461 1.45.2.2 christos
462 1.45.2.2 christos static void
463 1.45.2.2 christos connectlog_r(struct syslog_data *data)
464 1.45.2.2 christos {
465 1.45.2.2 christos /* AF_UNIX address of local logger */
466 1.45.2.2 christos static const struct sockaddr_un sun = {
467 1.45.2.2 christos .sun_family = AF_LOCAL,
468 1.45.2.2 christos .sun_len = sizeof(sun),
469 1.45.2.2 christos .sun_path = _PATH_LOG,
470 1.45.2.2 christos };
471 1.45.2.2 christos
472 1.45.2.2 christos if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) {
473 1.45.2.2 christos if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
474 1.45.2.2 christos return;
475 1.45.2.2 christos (void)fcntl(data->log_file, F_SETFD, FD_CLOEXEC);
476 1.45.2.2 christos data->connected = 0;
477 1.45.2.2 christos }
478 1.45.2.2 christos if (!data->connected) {
479 1.45.2.2 christos if (connect(data->log_file,
480 1.45.2.2 christos (const struct sockaddr *)(const void *)&sun,
481 1.45.2.2 christos sizeof(sun)) == -1) {
482 1.45.2.2 christos (void)close(data->log_file);
483 1.45.2.2 christos data->log_file = -1;
484 1.45.2.2 christos } else
485 1.45.2.2 christos data->connected = 1;
486 1.45.2.2 christos }
487 1.45.2.2 christos }
488 1.45.2.2 christos
489 1.45.2.2 christos static void
490 1.45.2.2 christos openlog_unlocked_r(const char *ident, int logstat, int logfac,
491 1.45.2.2 christos struct syslog_data *data)
492 1.45.2.2 christos {
493 1.45.2.2 christos if (ident != NULL)
494 1.45.2.2 christos data->log_tag = ident;
495 1.45.2.2 christos data->log_stat = logstat;
496 1.45.2.2 christos if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
497 1.45.2.2 christos data->log_fac = logfac;
498 1.45.2.2 christos
499 1.45.2.2 christos if (data->log_stat & LOG_NDELAY) /* open immediately */
500 1.45.2.2 christos connectlog_r(data);
501 1.45.2.2 christos
502 1.45.2.2 christos /* We could cache this, but then it might change */
503 1.45.2.3 christos if (gethostname(hostname, sizeof(hostname)) == -1
504 1.45.2.3 christos || hostname[0] == '\0') {
505 1.45.2.3 christos /* can this really happen? */
506 1.45.2.3 christos hostname[0] = '-';
507 1.45.2.3 christos hostname[1] = '\0';
508 1.45.2.3 christos }
509 1.45.2.2 christos data->opened = 1;
510 1.45.2.2 christos }
511 1.45.2.2 christos
512 1.45.2.2 christos void
513 1.45.2.2 christos openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
514 1.45.2.2 christos {
515 1.45.2.2 christos if (data == &sdata)
516 1.45.2.2 christos mutex_lock(&syslog_mutex);
517 1.45.2.2 christos openlog_unlocked_r(ident, logstat, logfac, data);
518 1.45.2.2 christos if (data == &sdata)
519 1.45.2.2 christos mutex_unlock(&syslog_mutex);
520 1.45.2.2 christos }
521 1.45.2.2 christos
522 1.45.2.2 christos void
523 1.45.2.2 christos closelog_r(struct syslog_data *data)
524 1.45.2.2 christos {
525 1.45.2.2 christos if (data == &sdata)
526 1.45.2.2 christos mutex_lock(&syslog_mutex);
527 1.45.2.2 christos (void)close(data->log_file);
528 1.45.2.2 christos data->log_file = -1;
529 1.45.2.2 christos data->connected = 0;
530 1.45.2.2 christos data->log_tag = NULL;
531 1.45.2.2 christos if (data == &sdata)
532 1.45.2.2 christos mutex_unlock(&syslog_mutex);
533 1.45.2.2 christos }
534 1.45.2.2 christos
535 1.45.2.2 christos int
536 1.45.2.2 christos setlogmask_r(int pmask, struct syslog_data *data)
537 1.45.2.2 christos {
538 1.45.2.2 christos int omask;
539 1.45.2.2 christos
540 1.45.2.2 christos omask = data->log_mask;
541 1.45.2.2 christos if (pmask != 0)
542 1.45.2.2 christos data->log_mask = pmask;
543 1.45.2.2 christos return omask;
544 1.45.2.2 christos }
545