1 /* $NetBSD: syslog.c,v 1.54 2014/09/18 13:58:20 christos Exp $ */
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
37 __RCSID("$NetBSD: syslog.c,v 1.54 2014/09/18 13:58:20 christos Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
59 #include "reentrant.h"
63 __weak_alias(closelog
,_closelog
)
64 __weak_alias(openlog
,_openlog
)
65 __weak_alias(setlogmask
,_setlogmask
)
66 __weak_alias(syslog
,_syslog
)
67 __weak_alias(vsyslog
,_vsyslog
)
68 __weak_alias(syslogp
,_syslogp
)
69 __weak_alias(vsyslogp
,_vsyslogp
)
72 static struct syslog_data sdata
= SYSLOG_DATA_INIT
;
74 static void openlog_unlocked_r(const char *, int, int,
75 struct syslog_data
*);
76 static void disconnectlog_r(struct syslog_data
*);
77 static void connectlog_r(struct syslog_data
*);
79 #define LOG_SIGNAL_SAFE (int)0x80000000
83 static mutex_t syslog_mutex
= MUTEX_INITIALIZER
;
88 * print message on log file; output is intended for syslogd(8).
91 syslog(int pri
, const char *fmt
, ...)
96 vsyslog(pri
, fmt
, ap
);
101 vsyslog(int pri
, const char *fmt
, va_list ap
)
103 vsyslog_r(pri
, &sdata
, fmt
, ap
);
107 * syslogp, vsyslogp --
108 * like syslog but take additional arguments for MSGID and SD
111 syslogp(int pri
, const char *msgid
, const char *sdfmt
, const char *msgfmt
, ...)
115 va_start(ap
, msgfmt
);
116 vsyslogp(pri
, msgid
, sdfmt
, msgfmt
, ap
);
121 vsyslogp(int pri
, const char *msgid
, const char *sdfmt
, const char *msgfmt
, va_list ap
)
123 vsyslogp_r(pri
, &sdata
, msgid
, sdfmt
, msgfmt
, ap
);
127 openlog(const char *ident
, int logstat
, int logfac
)
129 openlog_r(ident
, logstat
, logfac
, &sdata
);
138 /* setlogmask -- set the log mask level */
140 setlogmask(int pmask
)
142 return setlogmask_r(pmask
, &sdata
);
145 /* Reentrant version of syslog, i.e. syslog_r() */
148 syslog_r(int pri
, struct syslog_data
*data
, const char *fmt
, ...)
153 vsyslog_r(pri
, data
, fmt
, ap
);
158 syslogp_r(int pri
, struct syslog_data
*data
, const char *msgid
,
159 const char *sdfmt
, const char *msgfmt
, ...)
163 va_start(ap
, msgfmt
);
164 vsyslogp_r(pri
, data
, msgid
, sdfmt
, msgfmt
, ap
);
169 syslog_ss(int pri
, struct syslog_data
*data
, const char *fmt
, ...)
174 vsyslog_r(pri
| LOG_SIGNAL_SAFE
, data
, fmt
, ap
);
179 syslogp_ss(int pri
, struct syslog_data
*data
, const char *msgid
,
180 const char *sdfmt
, const char *msgfmt
, ...)
184 va_start(ap
, msgfmt
);
185 vsyslogp_r(pri
| LOG_SIGNAL_SAFE
, data
, msgid
, sdfmt
, msgfmt
, ap
);
190 vsyslog_ss(int pri
, struct syslog_data
*data
, const char *fmt
, va_list ap
)
192 vsyslog_r(pri
| LOG_SIGNAL_SAFE
, data
, fmt
, ap
);
196 vsyslogp_ss(int pri
, struct syslog_data
*data
, const char *msgid
,
197 const char *sdfmt
, const char *msgfmt
, va_list ap
)
199 vsyslogp_r(pri
| LOG_SIGNAL_SAFE
, data
, msgid
, sdfmt
, msgfmt
, ap
);
204 vsyslog_r(int pri
, struct syslog_data
*data
, const char *fmt
, va_list ap
)
206 vsyslogp_r(pri
, data
, NULL
, NULL
, fmt
, ap
);
210 vsyslogp_r(int pri
, struct syslog_data
*data
, const char *msgid
,
211 const char *sdfmt
, const char *msgfmt
, va_list ap
)
213 static const char BRCOSP
[] = "]: ";
214 static const char CRLF
[] = "\r\n";
215 size_t cnt
, prlen
, tries
;
221 #define TBUF_LEN 2048
224 char tbuf
[TBUF_LEN
], fmt_cpy
[FMT_LEN
], fmt_cat
[FMT_LEN
] = "";
225 size_t tbuf_left
, fmt_left
, msgsdlen
;
227 int signal_safe
= pri
& LOG_SIGNAL_SAFE
;
228 struct iovec iov
[7]; /* prog + [ + pid + ]: + fmt + crlf */
231 pri
&= ~LOG_SIGNAL_SAFE
;
233 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
234 /* Check for invalid bits. */
235 if (pri
& ~(LOG_PRIMASK
|LOG_FACMASK
)) {
236 syslog_r(INTERNALLOG
| signal_safe
, data
,
237 "syslog_r: unknown facility/priority: %x", pri
);
238 pri
&= LOG_PRIMASK
|LOG_FACMASK
;
241 /* Check priority against setlogmask values. */
242 if (!(LOG_MASK(LOG_PRI(pri
)) & data
->log_mask
))
247 /* Set default facility if none specified. */
248 if ((pri
& LOG_FACMASK
) == 0)
249 pri
|= data
->log_fac
;
251 /* Build the message. */
253 tbuf_left
= TBUF_LEN
;
257 if (prlen >= tbuf_left) \
258 prlen = tbuf_left - 1; \
260 tbuf_left -= prlen; \
261 } while (/*CONSTCOND*/0)
263 prlen
= snprintf_ss(p
, tbuf_left
, "<%d>1 ", pri
);
266 if (!signal_safe
&& (gettimeofday(&tv
, NULL
) != -1)) {
267 /* strftime() implies tzset(), localtime_r() doesn't. */
269 now
= (time_t) tv
.tv_sec
;
270 localtime_r(&now
, &tmnow
);
272 prlen
= strftime(p
, tbuf_left
, "%FT%T", &tmnow
);
274 prlen
= snprintf(p
, tbuf_left
, ".%06ld", (long)tv
.tv_usec
);
276 prlen
= strftime(p
, tbuf_left
-1, "%z", &tmnow
);
277 /* strftime gives eg. "+0200", but we need "+02:00" */
279 p
[prlen
+1] = p
[prlen
];
280 p
[prlen
] = p
[prlen
-1];
281 p
[prlen
-1] = p
[prlen
-2];
286 prlen
= snprintf_ss(p
, tbuf_left
, "-");
289 * if gmtime_r() was signal-safe we could output
292 gmtime_r(&now
, &tmnow
);
293 prlen
= strftime(p
, tbuf_left
, "%FT%TZ", &tmnow
);
297 #if !defined(__minix)
299 mutex_lock(&syslog_mutex
);
300 #endif /* !defined(__minix) */
302 if (data
->log_hostname
[0] == '\0' && gethostname(data
->log_hostname
,
303 sizeof(data
->log_hostname
)) == -1) {
304 /* can this really happen? */
305 data
->log_hostname
[0] = '-';
306 data
->log_hostname
[1] = '\0';
310 prlen
= snprintf_ss(p
, tbuf_left
, " %s ", data
->log_hostname
);
312 if (data
->log_tag
== NULL
)
313 data
->log_tag
= getprogname();
316 prlen
= snprintf_ss(p
, tbuf_left
, "%s ",
317 data
->log_tag
? data
->log_tag
: "-");
319 #if !defined(__minix)
321 mutex_unlock(&syslog_mutex
);
322 #endif /* !defined(__minix) */
324 if (data
->log_stat
& (LOG_PERROR
|LOG_CONS
)) {
326 iov
[iovcnt
].iov_base
= p
;
327 iov
[iovcnt
].iov_len
= prlen
- 1;
332 if (data
->log_stat
& LOG_PID
) {
333 prlen
= snprintf_ss(p
, tbuf_left
, "%d ", getpid());
334 if (data
->log_stat
& (LOG_PERROR
|LOG_CONS
)) {
335 iov
[iovcnt
].iov_base
= __UNCONST("[");
336 iov
[iovcnt
].iov_len
= 1;
338 iov
[iovcnt
].iov_base
= p
;
339 iov
[iovcnt
].iov_len
= prlen
- 1;
341 iov
[iovcnt
].iov_base
= __UNCONST(BRCOSP
);
342 iov
[iovcnt
].iov_len
= 3;
346 prlen
= snprintf_ss(p
, tbuf_left
, "- ");
347 if (data
->log_stat
& (LOG_PERROR
|LOG_CONS
)) {
348 iov
[iovcnt
].iov_base
= __UNCONST(BRCOSP
+ 1);
349 iov
[iovcnt
].iov_len
= 2;
356 * concat the format strings, then use one vsnprintf()
358 if (msgid
!= NULL
&& *msgid
!= '\0') {
359 strlcat(fmt_cat
, msgid
, FMT_LEN
);
360 strlcat(fmt_cat
, " ", FMT_LEN
);
362 strlcat(fmt_cat
, "- ", FMT_LEN
);
364 if (sdfmt
!= NULL
&& *sdfmt
!= '\0') {
365 strlcat(fmt_cat
, sdfmt
, FMT_LEN
);
367 strlcat(fmt_cat
, "-", FMT_LEN
);
369 if (data
->log_stat
& (LOG_PERROR
|LOG_CONS
))
370 msgsdlen
= strlen(fmt_cat
) + 1;
372 msgsdlen
= 0; /* XXX: GCC */
374 if (msgfmt
!= NULL
&& *msgfmt
!= '\0') {
375 strlcat(fmt_cat
, " ", FMT_LEN
);
376 strlcat(fmt_cat
, msgfmt
, FMT_LEN
);
380 * We wouldn't need this mess if printf handled %m, or if
381 * strerror() had been invented before syslog().
383 for (t
= fmt_cpy
, fmt_left
= FMT_LEN
; (ch
= *fmt
) != '\0'; ++fmt
) {
384 if (ch
== '%' && fmt
[1] == 'm') {
388 strerror_r(saved_errno
, ebuf
, sizeof(ebuf
)))
389 prlen
= snprintf_ss(t
, fmt_left
, "Error %d",
392 prlen
= snprintf_ss(t
, fmt_left
, "%s", ebuf
);
393 if (prlen
>= fmt_left
)
394 prlen
= fmt_left
- 1;
397 } else if (ch
== '%' && fmt
[1] == '%' && fmt_left
> 2) {
412 prlen
= vsnprintf_ss(p
, tbuf_left
, fmt_cpy
, ap
);
414 prlen
= vsnprintf(p
, tbuf_left
, fmt_cpy
, ap
);
416 if (data
->log_stat
& (LOG_PERROR
|LOG_CONS
)) {
417 iov
[iovcnt
].iov_base
= p
+ msgsdlen
;
418 iov
[iovcnt
].iov_len
= prlen
- msgsdlen
;
425 /* Output to stderr if requested. */
426 if (data
->log_stat
& LOG_PERROR
) {
427 iov
[iovcnt
].iov_base
= __UNCONST(CRLF
+ 1);
428 iov
[iovcnt
].iov_len
= 1;
429 (void)writev(STDERR_FILENO
, iov
, iovcnt
+ 1);
432 /* Get connected, output the message to the local logger. */
433 #if !defined(__minix)
435 mutex_lock(&syslog_mutex
);
436 #endif /* !defined(__minix) */
437 opened
= !data
->log_opened
;
439 openlog_unlocked_r(data
->log_tag
, data
->log_stat
, 0, data
);
443 * If the send() failed, there are two likely scenarios:
444 * 1) syslogd was restarted
445 * 2) /dev/log is out of socket buffer space
446 * We attempt to reconnect to /dev/log to take care of
447 * case #1 and keep send()ing data to cover case #2
448 * to give syslogd a chance to empty its socket buffer.
450 for (tries
= 0; tries
< MAXTRIES
; tries
++) {
451 if (send(data
->log_file
, tbuf
, cnt
, 0) != -1)
453 if (errno
!= ENOBUFS
) {
454 disconnectlog_r(data
);
461 * Output the message to the console; try not to block
462 * as a blocking console should not stop other processes.
463 * Make sure the error reported is the one from the syslogd failure.
465 if (tries
== MAXTRIES
&& (data
->log_stat
& LOG_CONS
) &&
466 (fd
= open(_PATH_CONSOLE
,
467 O_WRONLY
| O_NONBLOCK
| O_CLOEXEC
, 0)) >= 0) {
468 iov
[iovcnt
].iov_base
= __UNCONST(CRLF
);
469 iov
[iovcnt
].iov_len
= 2;
470 (void)writev(fd
, iov
, iovcnt
+ 1);
474 #if !defined(__minix)
476 mutex_unlock(&syslog_mutex
);
477 #endif /* !defined(__minix) */
479 if (data
!= &sdata
&& opened
) {
480 /* preserve log tag */
481 const char *ident
= data
->log_tag
;
483 data
->log_tag
= ident
;
488 disconnectlog_r(struct syslog_data
*data
)
491 * If the user closed the FD and opened another in the same slot,
492 * that's their problem. They should close it before calling on
495 if (data
->log_file
!= -1) {
496 (void)close(data
->log_file
);
499 data
->log_connected
= 0; /* retry connect */
503 connectlog_r(struct syslog_data
*data
)
505 /* AF_UNIX address of local logger */
506 static const struct sockaddr_un sun
= {
507 .sun_family
= AF_LOCAL
,
508 .sun_len
= sizeof(sun
),
509 .sun_path
= _PATH_LOG
,
512 if (data
->log_file
== -1 || fcntl(data
->log_file
, F_GETFL
, 0) == -1) {
513 if ((data
->log_file
= socket(AF_UNIX
, SOCK_DGRAM
| SOCK_CLOEXEC
,
516 data
->log_connected
= 0;
518 if (!data
->log_connected
) {
519 if (connect(data
->log_file
,
520 (const struct sockaddr
*)(const void *)&sun
,
521 (socklen_t
)sizeof(sun
)) == -1)
523 (void)close(data
->log_file
);
526 data
->log_connected
= 1;
531 openlog_unlocked_r(const char *ident
, int logstat
, int logfac
,
532 struct syslog_data
*data
)
535 data
->log_tag
= ident
;
536 data
->log_stat
= logstat
;
537 if (logfac
!= 0 && (logfac
&~ LOG_FACMASK
) == 0)
538 data
->log_fac
= logfac
;
540 if (data
->log_stat
& LOG_NDELAY
) /* open immediately */
543 data
->log_opened
= 1;
547 openlog_r(const char *ident
, int logstat
, int logfac
, struct syslog_data
*data
)
549 #if !defined(__minix)
551 mutex_lock(&syslog_mutex
);
552 #endif /* !defined(__minix) */
553 openlog_unlocked_r(ident
, logstat
, logfac
, data
);
554 #if !defined(__minix)
556 mutex_unlock(&syslog_mutex
);
557 #endif /* !defined(__minix) */
561 closelog_r(struct syslog_data
*data
)
563 #if !defined(__minix)
565 mutex_lock(&syslog_mutex
);
566 #endif /* !defined(__minix) */
567 (void)close(data
->log_file
);
569 data
->log_connected
= 0;
570 data
->log_tag
= NULL
;
571 #if !defined(__minix)
573 mutex_unlock(&syslog_mutex
);
574 #endif /* !defined(__minix) */
578 setlogmask_r(int pmask
, struct syslog_data
*data
)
582 omask
= data
->log_mask
;
584 data
->log_mask
= pmask
;