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)
36 static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
38 static const char rcsid[] =
40 #endif /* LIBC_SCCS and not lint */
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/syslog.h>
63 static int LogFile = -1; /* fd for log */
64 static int connected; /* have done connect */
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 extern char *__progname; /* Program name, from crt0. */
72 static void disconnectlog __P((void)); /* disconnect from syslogd */
73 static void connectlog __P((void)); /* (re)connect to syslogd */
76 * Format of the magic cookie passed through the stdio hook
79 char *base; /* start of buffer */
84 * stdio write hook for writing to a static string buffer
85 * XXX: Maybe one day, dynamically allocate it so that the line length
89 int writehook(cookie, buf, len)
90 void *cookie; /* really [struct bufcookie *] */
91 char *buf; /* characters to copy */
92 int len; /* length to copy */
94 struct bufcookie *h; /* private `handle' */
96 h = (struct bufcookie *)cookie;
98 /* clip in case of wraparound */
102 (void)memcpy(h->base, buf, len); /* `write' it. */
111 * print message on log file; output is intended for syslogd(8).
115 syslog(int pri, const char *fmt, ...)
117 syslog(pri, fmt, va_alist)
130 vsyslog(pri, fmt, ap);
135 vsyslog(pri, fmt, ap)
137 register const char *fmt;
141 register char ch, *p;
144 char *stdp, tbuf[2048], fmt_cpy[1024];
146 struct bufcookie tbuf_cookie;
147 struct bufcookie fmt_cookie;
149 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
150 /* Check for invalid bits. */
151 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
153 "syslog: unknown facility/priority: %x", pri);
154 pri &= LOG_PRIMASK|LOG_FACMASK;
157 /* Check priority against setlogmask values. */
158 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
163 /* Set default facility if none specified. */
164 if ((pri & LOG_FACMASK) == 0)
167 /* Create the primary stdio hook */
168 tbuf_cookie.base = tbuf;
169 tbuf_cookie.left = sizeof(tbuf);
170 fp = fwopen(&tbuf_cookie, writehook);
174 /* Build the message. */
176 (void)fprintf(fp, "<%d>", pri);
177 (void)fprintf(fp, "%.15s ", ctime(&now) + 4);
178 if (LogStat & LOG_PERROR) {
179 /* Transfer to string buffer */
181 stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
186 (void)fprintf(fp, "%s", LogTag);
187 if (LogStat & LOG_PID)
188 (void)fprintf(fp, "[%d]", getpid());
189 if (LogTag != NULL) {
190 (void)fprintf(fp, ": ");
193 /* Check to see if we can skip expanding the %m */
194 if (strstr(fmt, "%m")) {
196 /* Create the second stdio hook */
197 fmt_cookie.base = fmt_cpy;
198 fmt_cookie.left = sizeof(fmt_cpy) - 1;
199 fmt_fp = fwopen(&fmt_cookie, writehook);
200 if (fmt_fp == NULL) {
205 /* Substitute error message for %m. */
206 for ( ; (ch = *fmt); ++fmt)
207 if (ch == '%' && fmt[1] == 'm') {
209 fputs(strerror(saved_errno), fmt_fp);
213 /* Null terminate if room */
217 /* Guarantee null termination */
218 fmt_cpy[sizeof(fmt_cpy) - 1] = '\0';
223 (void)vfprintf(fp, fmt, ap);
226 cnt = sizeof(tbuf) - tbuf_cookie.left;
228 /* Output to stderr if requested. */
229 if (LogStat & LOG_PERROR) {
231 register struct iovec *v = iov;
234 v->iov_len = cnt - (stdp - tbuf);
238 (void)writev(STDERR_FILENO, iov, 2);
241 /* Get connected, output the message to the local logger. */
243 openlog(LogTag, LogStat | LOG_NDELAY, 0);
245 if (send(LogFile, tbuf, cnt, 0) >= 0)
249 * If the send() failed, the odds are syslogd was restarted.
250 * Make one (only) attempt to reconnect to /dev/log.
254 if (send(LogFile, tbuf, cnt, 0) >= 0)
258 * Output the message to the console; don't worry about blocking,
259 * if console blocks everything will. Make sure the error reported
260 * is the one from the syslogd failure.
262 if (LogStat & LOG_CONS &&
263 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
265 register struct iovec *v = iov;
267 p = strchr(tbuf, '>') + 1;
269 v->iov_len = cnt - (p - tbuf);
271 v->iov_base = "\r\n";
273 (void)writev(fd, iov, 2);
281 * If the user closed the FD and opened another in the same slot,
282 * that's their problem. They should close it before calling on
289 connected = 0; /* retry connect */
295 struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
298 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
300 (void)fcntl(LogFile, F_SETFD, 1);
302 if (LogFile != -1 && !connected) {
303 SyslogAddr.sun_len = sizeof(SyslogAddr);
304 SyslogAddr.sun_family = AF_UNIX;
305 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
306 sizeof SyslogAddr.sun_path);
307 connected = connect(LogFile, (struct sockaddr *)&SyslogAddr,
308 sizeof(SyslogAddr)) != -1;
312 * Try the old "/dev/log" path, for backward
315 (void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG,
316 sizeof SyslogAddr.sun_path);
317 connected = connect(LogFile,
318 (struct sockaddr *)&SyslogAddr,
319 sizeof(SyslogAddr)) != -1;
323 (void)close(LogFile);
330 openlog(ident, logstat, logfac)
337 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
338 LogFacility = logfac;
340 if (LogStat & LOG_NDELAY) /* open immediately */
343 opened = 1; /* ident and facility has been set */
349 (void)close(LogFile);
354 /* setlogmask -- set the log mask level */