2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/syslog.h>
59 #include "un-namespace.h"
61 #include "libc_private.h"
63 static int LogFile = -1; /* fd for log */
64 static int status; /* connection status */
65 static int opened; /* have done openlog() */
66 static int LogStat = 0; /* status bits, set by openlog() */
67 static const char *LogTag = NULL; /* string to tag the entry with */
68 static int LogFacility = LOG_USER; /* default facility code */
69 static int LogMask = 0xff; /* mask of priorities to be logged */
70 static pthread_mutex_t syslog_mutex = PTHREAD_MUTEX_INITIALIZER;
72 #define THREAD_LOCK() \
74 if (__isthreaded) _pthread_mutex_lock(&syslog_mutex); \
76 #define THREAD_UNLOCK() \
78 if (__isthreaded) _pthread_mutex_unlock(&syslog_mutex); \
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 vsyslog(int pri, const char *fmt, va_list ap)
143 char *stdp, tbuf[2048], fmt_cpy[1024], timbuf[26], errstr[64];
145 struct bufcookie tbuf_cookie;
146 struct bufcookie fmt_cookie;
148 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
149 /* Check for invalid bits. */
150 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
152 "syslog: unknown facility/priority: %x", pri);
153 pri &= LOG_PRIMASK|LOG_FACMASK;
160 /* Check priority against setlogmask values. */
161 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) {
166 /* Set default facility if none specified. */
167 if ((pri & LOG_FACMASK) == 0)
170 /* Create the primary stdio hook */
171 tbuf_cookie.base = tbuf;
172 tbuf_cookie.left = sizeof(tbuf);
173 fp = fwopen(&tbuf_cookie, writehook);
179 /* Build the message. */
181 (void)fprintf(fp, "<%d>", pri);
182 (void)fprintf(fp, "%.15s ", ctime_r(&now, timbuf) + 4);
183 if (LogStat & LOG_PERROR) {
184 /* Transfer to string buffer */
186 stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
189 LogTag = _getprogname();
191 (void)fprintf(fp, "%s", LogTag);
192 if (LogStat & LOG_PID)
193 (void)fprintf(fp, "[%d]", getpid());
194 if (LogTag != NULL) {
195 (void)fprintf(fp, ": ");
198 /* Check to see if we can skip expanding the %m */
199 if (strstr(fmt, "%m")) {
201 /* Create the second stdio hook */
202 fmt_cookie.base = fmt_cpy;
203 fmt_cookie.left = sizeof(fmt_cpy) - 1;
204 fmt_fp = fwopen(&fmt_cookie, writehook);
205 if (fmt_fp == NULL) {
212 * Substitute error message for %m. Be careful not to
213 * molest an escaped percent "%%m". We want to pass it
214 * on untouched as the format is later parsed by vfprintf.
216 for ( ; (ch = *fmt); ++fmt) {
217 if (ch == '%' && fmt[1] == 'm') {
219 strerror_r(saved_errno, errstr, sizeof(errstr));
220 fputs(errstr, fmt_fp);
221 } else if (ch == '%' && fmt[1] == '%') {
230 /* Null terminate if room */
234 /* Guarantee null termination */
235 fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
240 (void)vfprintf(fp, fmt, ap);
243 cnt = sizeof(tbuf) - tbuf_cookie.left;
245 /* Remove a trailing newline */
246 if (tbuf[cnt - 1] == '\n')
249 /* Output to stderr if requested. */
250 if (LogStat & LOG_PERROR) {
252 struct iovec *v = iov;
255 v->iov_len = cnt - (stdp - tbuf);
259 (void)_writev(STDERR_FILENO, iov, 2);
262 /* Get connected, output the message to the local logger. */
264 openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
268 * If the send() failed, there are two likely scenarios:
269 * 1) syslogd was restarted
270 * 2) /var/run/log is out of socket buffer space, which
271 * in most cases means local DoS.
272 * We attempt to reconnect to /var/run/log to take care of
273 * case #1 and keep send()ing data to cover case #2
274 * to give syslogd a chance to empty its socket buffer.
276 * If we are working with a priveleged socket, then take
277 * only one attempt, because we don't want to freeze a
278 * critical application like su(1) or sshd(8).
282 if (send(LogFile, tbuf, cnt, 0) < 0) {
283 if (errno != ENOBUFS) {
289 if (send(LogFile, tbuf, cnt, 0) >= 0) {
293 if (status == CONNPRIV)
295 } while (errno == ENOBUFS);
302 * Output the message to the console; try not to block
303 * as a blocking console should not stop other processes.
304 * Make sure the error reported is the one from the syslogd failure.
306 if (LogStat & LOG_CONS &&
307 (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
309 struct iovec *v = iov;
311 p = strchr(tbuf, '>') + 1;
313 v->iov_len = cnt - (p - tbuf);
315 v->iov_base = "\r\n";
317 (void)_writev(fd, iov, 2);
324 /* Should be called with mutex acquired */
329 * If the user closed the FD and opened another in the same slot,
330 * that's their problem. They should close it before calling on
337 status = NOCONN; /* retry connect */
340 /* Should be called with mutex acquired */
344 struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
347 if ((LogFile = _socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
349 (void)_fcntl(LogFile, F_SETFD, 1);
351 if (LogFile != -1 && status == NOCONN) {
352 SyslogAddr.sun_len = sizeof(SyslogAddr);
353 SyslogAddr.sun_family = AF_UNIX;
356 * First try priveleged socket. If no success,
357 * then try default socket.
359 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG_PRIV,
360 sizeof SyslogAddr.sun_path);
361 if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
362 sizeof(SyslogAddr)) != -1)
365 if (status == NOCONN) {
366 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
367 sizeof SyslogAddr.sun_path);
368 if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
369 sizeof(SyslogAddr)) != -1)
373 if (status == NOCONN) {
375 * Try the old "/dev/log" path, for backward
378 (void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG,
379 sizeof SyslogAddr.sun_path);
380 if (_connect(LogFile, (struct sockaddr *)&SyslogAddr,
381 sizeof(SyslogAddr)) != -1)
385 if (status == NOCONN) {
386 (void)_close(LogFile);
393 openlog_unlocked(const char *ident, int logstat, int logfac)
398 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
399 LogFacility = logfac;
401 if (LogStat & LOG_NDELAY) /* open immediately */
404 opened = 1; /* ident and facility has been set */
408 openlog(const char *ident, int logstat, int logfac)
411 openlog_unlocked(ident, logstat, logfac);
420 (void)_close(LogFile);
427 /* setlogmask -- set the log mask level */
429 setlogmask(int pmask)