syslog.c revision 1.20 1 /* $NetBSD: syslog.c,v 1.20 1999/02/04 16:23:17 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
40 #else
41 __RCSID("$NetBSD: syslog.c,v 1.20 1999/02/04 16:23:17 kleink Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include "namespace.h"
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/syslog.h>
49 #include <sys/uio.h>
50 #include <netdb.h>
51
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <paths.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include "reentrant.h"
60
61 #if __STDC__
62 #include <stdarg.h>
63 #else
64 #include <varargs.h>
65 #endif
66
67 #ifdef __weak_alias
68 __weak_alias(closelog,_closelog);
69 __weak_alias(openlog,_openlog);
70 __weak_alias(setlogmask,_setlogmask);
71 __weak_alias(syslog,_syslog);
72 __weak_alias(vsyslog,_vsyslog);
73 #endif
74
75 static int LogFile = -1; /* fd for log */
76 static int connected; /* have done connect */
77 static int LogStat = 0; /* status bits, set by openlog() */
78 static const char *LogTag = NULL; /* string to tag the entry with */
79 static int LogFacility = LOG_USER; /* default facility code */
80 static int LogMask = 0xff; /* mask of priorities to be logged */
81 extern char *__progname; /* Program name, from crt0. */
82
83 static void openlog_unlocked __P((const char *, int, int));
84 static void closelog_unlocked __P((void));
85
86 #ifdef _REENT
87 static mutex_t syslog_mutex = MUTEX_INITIALIZER;
88 #endif
89
90 #ifdef lint
91 static const int ZERO = 0;
92 #else
93 #define ZERO 0
94 #endif
95
96 /*
97 * syslog, vsyslog --
98 * print message on log file; output is intended for syslogd(8).
99 */
100 void
101 #if __STDC__
102 syslog(int pri, const char *fmt, ...)
103 #else
104 syslog(pri, fmt, va_alist)
105 int pri;
106 char *fmt;
107 va_dcl
108 #endif
109 {
110 va_list ap;
111
112 #if __STDC__
113 va_start(ap, fmt);
114 #else
115 va_start(ap);
116 #endif
117 vsyslog(pri, fmt, ap);
118 va_end(ap);
119 }
120
121 void
122 vsyslog(pri, fmt, ap)
123 int pri;
124 const char *fmt;
125 va_list ap;
126 {
127 size_t cnt;
128 char ch, *p, *t;
129 time_t now;
130 struct tm tmnow;
131 int fd, saved_errno;
132 #define TBUF_LEN 2048
133 #define FMT_LEN 1024
134 char *stdp = NULL; /* pacify gcc */
135 char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
136 size_t tbuf_left, fmt_left, prlen;
137
138 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
139 /* Check for invalid bits. */
140 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
141 syslog(INTERNALLOG,
142 "syslog: unknown facility/priority: %x", pri);
143 pri &= LOG_PRIMASK|LOG_FACMASK;
144 }
145
146 /* Check priority against setlogmask values. */
147 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
148 return;
149
150 saved_errno = errno;
151
152 /* Set default facility if none specified. */
153 if ((pri & LOG_FACMASK) == 0)
154 pri |= LogFacility;
155
156 /* Build the message. */
157
158 /*
159 * Although it's tempting, we can't ignore the possibility of
160 * overflowing the buffer when assembling the "fixed" portion
161 * of the message. Strftime's "%h" directive expands to the
162 * locale's abbreviated month name, but if the user has the
163 * ability to construct to his own locale files, it may be
164 * arbitrarily long.
165 */
166 (void)time(&now);
167
168 p = tbuf;
169 tbuf_left = TBUF_LEN;
170
171 #define DEC() \
172 do { \
173 if (prlen >= tbuf_left) \
174 prlen = tbuf_left - 1; \
175 p += prlen; \
176 tbuf_left -= prlen; \
177 } while (ZERO)
178
179 prlen = snprintf(p, tbuf_left, "<%d>", pri);
180 DEC();
181
182 tzset(); /* strftime() implies tzset(), localtime_r() doesn't. */
183 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime_r(&now, &tmnow));
184 DEC();
185
186 if (LogStat & LOG_PERROR)
187 stdp = p;
188 if (LogTag == NULL)
189 LogTag = __progname;
190 if (LogTag != NULL) {
191 prlen = snprintf(p, tbuf_left, "%s", LogTag);
192 DEC();
193 }
194 if (LogStat & LOG_PID) {
195 prlen = snprintf(p, tbuf_left, "[%d]", getpid());
196 DEC();
197 }
198 if (LogTag != NULL) {
199 if (tbuf_left > 1) {
200 *p++ = ':';
201 tbuf_left--;
202 }
203 if (tbuf_left > 1) {
204 *p++ = ' ';
205 tbuf_left--;
206 }
207 }
208
209 /*
210 * We wouldn't need this mess if printf handled %m, or if
211 * strerror() had been invented before syslog().
212 */
213 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
214 if (ch == '%' && fmt[1] == 'm') {
215 ++fmt;
216 prlen = snprintf(t, fmt_left, "%s",
217 strerror(saved_errno));
218 if (prlen >= fmt_left)
219 prlen = fmt_left - 1;
220 t += prlen;
221 fmt_left -= prlen;
222 } else {
223 if (fmt_left > 1) {
224 *t++ = ch;
225 fmt_left--;
226 }
227 }
228 }
229 *t = '\0';
230
231 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
232 DEC();
233 cnt = p - tbuf;
234
235 /* Output to stderr if requested. */
236 if (LogStat & LOG_PERROR) {
237 struct iovec iov[2];
238
239 iov[0].iov_base = stdp;
240 iov[0].iov_len = cnt - (stdp - tbuf);
241 iov[1].iov_base = "\n";
242 iov[1].iov_len = 1;
243 (void)writev(STDERR_FILENO, iov, 2);
244 }
245
246 /* Get connected, output the message to the local logger. */
247 mutex_lock(&syslog_mutex);
248 if (!connected)
249 openlog_unlocked(LogTag, LogStat | LOG_NDELAY, 0);
250 if (send(LogFile, tbuf, cnt, 0) >= 0) {
251 mutex_unlock(&syslog_mutex);
252 return;
253 }
254 mutex_unlock(&syslog_mutex);
255
256 /*
257 * Output the message to the console; don't worry about blocking,
258 * if console blocks everything will. Make sure the error reported
259 * is the one from the syslogd failure.
260 */
261 if (LogStat & LOG_CONS &&
262 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
263 struct iovec iov[2];
264
265 p = strchr(tbuf, '>') + 1;
266 iov[0].iov_base = p;
267 iov[0].iov_len = cnt - (p - tbuf);
268 iov[1].iov_base = "\r\n";
269 iov[1].iov_len = 2;
270 (void)writev(fd, iov, 2);
271 (void)close(fd);
272 }
273 }
274
275 static struct sockaddr SyslogAddr; /* AF_LOCAL address of local logger */
276
277 static void
278 openlog_unlocked(ident, logstat, logfac)
279 const char *ident;
280 int logstat, logfac;
281 {
282
283 if (ident != NULL)
284 LogTag = ident;
285 LogStat = logstat;
286 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
287 LogFacility = logfac;
288
289 if (LogFile == -1) {
290 SyslogAddr.sa_family = AF_LOCAL;
291 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
292 sizeof(SyslogAddr.sa_data));
293 if (LogStat & LOG_NDELAY) {
294 if ((LogFile = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
295 return;
296 (void)fcntl(LogFile, F_SETFD, 1);
297 }
298 }
299 if (LogFile != -1 && !connected) {
300 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
301 (void)close(LogFile);
302 LogFile = -1;
303 } else
304 connected = 1;
305 }
306 }
307
308 void
309 openlog(ident, logstat, logfac)
310 const char *ident;
311 int logstat, logfac;
312 {
313
314 mutex_lock(&syslog_mutex);
315 openlog_unlocked(ident, logstat, logfac);
316 mutex_unlock(&syslog_mutex);
317 }
318
319 static void
320 closelog_unlocked()
321 {
322 (void)close(LogFile);
323 LogFile = -1;
324 connected = 0;
325 }
326
327 void
328 closelog()
329 {
330
331 mutex_lock(&syslog_mutex);
332 closelog_unlocked();
333 mutex_unlock(&syslog_mutex);
334 }
335
336 /* setlogmask -- set the log mask level */
337 int
338 setlogmask(pmask)
339 int pmask;
340 {
341 int omask;
342
343 omask = LogMask;
344 if (pmask != 0)
345 LogMask = pmask;
346 return (omask);
347 }
348