2 * SPDX-License-Identifier: BSD-3-Clause
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 __SCCSID("@(#)syslog.c 8.5 (Berkeley) 4/29/95");
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
56 #include "un-namespace.h"
58 #include "libc_private.h"
60 static int LogFile = -1; /* fd for log */
61 static int status; /* connection status */
62 static int opened; /* have done openlog() */
63 static int LogStat = 0; /* status bits, set by openlog() */
64 static const char *LogTag = NULL; /* string to tag the entry with */
65 static int LogFacility = LOG_USER; /* default facility code */
66 static int LogMask = 0xff; /* mask of priorities to be logged */
67 static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER;
69 #define THREAD_LOCK() \
71 if (__isthreaded) _pthread_mutex_lock(&syslog_mutex); \
73 #define THREAD_UNLOCK() \
75 if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex); \
78 /* RFC5424 defined value. */
81 static void disconnectlog(void); /* disconnect from syslogd */
82 static void connectlog(void); /* (re)connect to syslogd */
83 static void openlog_unlocked(const char *, int, int);
92 * Format of the magic cookie passed through the stdio hook
95 char *base; /* start of buffer */
100 * stdio write hook for writing to a static string buffer
101 * XXX: Maybe one day, dynamically allocate it so that the line length
105 writehook(void *cookie, const char *buf, int len)
107 struct bufcookie *h; /* private `handle' */
109 h = (struct bufcookie *)cookie;
111 /* clip in case of wraparound */
115 (void)memcpy(h->base, buf, len); /* `write' it. */
124 * print message on log file; output is intended for syslogd(8).
127 syslog(int pri, const char *fmt, ...)
132 vsyslog(pri, fmt, ap);
137 vsyslog1(int pri, const char *fmt, va_list ap)
143 int cnt, fd, saved_errno;
144 char hostname[MAXHOSTNAMELEN], *stdp, tbuf[2048], fmt_cpy[1024],
147 struct bufcookie tbuf_cookie;
148 struct bufcookie fmt_cookie;
150 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
151 /* Check for invalid bits. */
152 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
154 "syslog: unknown facility/priority: %x", pri);
155 pri &= LOG_PRIMASK|LOG_FACMASK;
160 /* Check priority against setlogmask values. */
161 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
164 /* Set default facility if none specified. */
165 if ((pri & LOG_FACMASK) == 0)
168 /* Create the primary stdio hook */
169 tbuf_cookie.base = tbuf;
170 tbuf_cookie.left = sizeof(tbuf);
171 fp = fwopen(&tbuf_cookie, writehook);
175 /* Build the message according to RFC 5424. Tag and version. */
176 (void)fprintf(fp, "<%d>1 ", pri);
177 /* Timestamp similar to RFC 3339. */
178 if (gettimeofday(&now, NULL) == 0 &&
179 localtime_r(&now.tv_sec, &tm) != NULL) {
180 if (tm.tm_gmtoff < 0) {
182 tz_offset = -tm.tm_gmtoff;
185 tz_offset = tm.tm_gmtoff;
189 "%04d-%02d-%02d" /* Date. */
190 "T%02d:%02d:%02d.%06ld" /* Time. */
191 "%c%02ld:%02ld ", /* Time zone offset. */
192 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
193 tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec,
194 tz_sign, tz_offset / 3600, (tz_offset % 3600) / 60);
196 (void)fputs(NILVALUE " ", fp);
198 (void)gethostname(hostname, sizeof(hostname));
199 (void)fprintf(fp, "%s ",
200 hostname[0] == '\0' ? NILVALUE : hostname);
201 if (LogStat & LOG_PERROR) {
202 /* Transfer to string buffer */
204 stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
206 /* Application name. */
208 LogTag = _getprogname();
209 (void)fprintf(fp, "%s ", LogTag == NULL ? NILVALUE : LogTag);
211 * Provide the process ID regardless of whether LOG_PID has been
212 * specified, as it provides valuable information. Many
213 * applications tend not to use this, even though they should.
215 (void)fprintf(fp, "%d ", getpid());
217 (void)fputs(NILVALUE " ", fp);
218 /* Structured data. */
219 (void)fputs(NILVALUE " ", fp);
221 /* Check to see if we can skip expanding the %m */
222 if (strstr(fmt, "%m")) {
224 /* Create the second stdio hook */
225 fmt_cookie.base = fmt_cpy;
226 fmt_cookie.left = sizeof(fmt_cpy) - 1;
227 fmt_fp = fwopen(&fmt_cookie, writehook);
228 if (fmt_fp == NULL) {
234 * Substitute error message for %m. Be careful not to
235 * molest an escaped percent "%%m". We want to pass it
236 * on untouched as the format is later parsed by vfprintf.
238 for ( ; (ch = *fmt); ++fmt) {
239 if (ch == '%' && fmt[1] == 'm') {
241 strerror_r(saved_errno, errstr, sizeof(errstr));
242 fputs(errstr, fmt_fp);
243 } else if (ch == '%' && fmt[1] == '%') {
252 /* Null terminate if room */
256 /* Guarantee null termination */
257 fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
263 (void)vfprintf(fp, fmt, ap);
266 cnt = sizeof(tbuf) - tbuf_cookie.left;
268 /* Remove a trailing newline */
269 if (tbuf[cnt - 1] == '\n')
272 /* Output to stderr if requested. */
273 if (LogStat & LOG_PERROR) {
275 struct iovec *v = iov;
278 v->iov_len = cnt - (stdp - tbuf);
282 (void)_writev(STDERR_FILENO, iov, 2);
285 /* Get connected, output the message to the local logger. */
287 openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
291 * If the send() fails, there are two likely scenarios:
292 * 1) syslogd was restarted
293 * 2) /var/run/log is out of socket buffer space, which
294 * in most cases means local DoS.
295 * If the error does not indicate a full buffer, we address
296 * case #1 by attempting to reconnect to /var/run/log[priv]
297 * and resending the message once.
299 * If we are working with a privileged socket, the retry
300 * attempts end there, because we don't want to freeze a
301 * critical application like su(1) or sshd(8).
303 * Otherwise, we address case #2 by repeatedly retrying the
304 * send() to give syslogd a chance to empty its socket buffer.
307 if (send(LogFile, tbuf, cnt, 0) < 0) {
308 if (errno != ENOBUFS) {
310 * Scenario 1: syslogd was restarted
311 * reconnect and resend once
315 if (send(LogFile, tbuf, cnt, 0) >= 0)
318 * if the resend failed, fall through to
319 * possible scenario 2
322 while (errno == ENOBUFS) {
324 * Scenario 2: out of socket buffer space
325 * possible DoS, fail fast on a privileged
328 if (status == CONNPRIV)
331 if (send(LogFile, tbuf, cnt, 0) >= 0)
338 * Output the message to the console; try not to block
339 * as a blocking console should not stop other processes.
340 * Make sure the error reported is the one from the syslogd failure.
342 if (LogStat & LOG_CONS &&
343 (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >=
346 struct iovec *v = iov;
348 p = strchr(tbuf, '>') + 3;
350 v->iov_len = cnt - (p - tbuf);
352 v->iov_base = "\r\n";
354 (void)_writev(fd, iov, 2);
360 syslog_cancel_cleanup(void *arg __unused)
367 vsyslog(int pri, const char *fmt, va_list ap)
371 pthread_cleanup_push(syslog_cancel_cleanup, NULL);
372 vsyslog1(pri, fmt, ap);
373 pthread_cleanup_pop(1);
376 /* Should be called with mutex acquired */
381 * If the user closed the FD and opened another in the same slot,
382 * that's their problem. They should close it before calling on
389 status = NOCONN; /* retry connect */
392 /* Should be called with mutex acquired */
396 struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
399 if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
403 if (LogFile != -1 && status == NOCONN) {
404 SyslogAddr.sun_len = sizeof(SyslogAddr);
405 SyslogAddr.sun_family = AF_UNIX;
408 * First try privileged socket. If no success,
409 * then try default socket.
411 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV,
412 sizeof SyslogAddr.sun_path);
413 if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
414 sizeof(SyslogAddr)) != -1)
417 if (status == NOCONN) {
418 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
419 sizeof SyslogAddr.sun_path);
420 if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
421 sizeof(SyslogAddr)) != -1)
425 if (status == NOCONN) {
427 * Try the old "/dev/log" path, for backward
430 (void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG,
431 sizeof SyslogAddr.sun_path);
432 if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
433 sizeof(SyslogAddr)) != -1)
437 if (status == NOCONN) {
438 (void)_close(LogFile);
445 openlog_unlocked(const char *ident, int logstat, int logfac)
450 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
451 LogFacility = logfac;
453 if (LogStat & LOG_NDELAY) /* open immediately */
456 opened = 1; /* ident and facility has been set */
460 openlog(const char *ident, int logstat, int logfac)
464 pthread_cleanup_push(syslog_cancel_cleanup, NULL);
465 openlog_unlocked(ident, logstat, logfac);
466 pthread_cleanup_pop(1);
475 (void)_close(LogFile);
483 /* setlogmask -- set the log mask level */
485 setlogmask(int pmask)