syslog.c revision 1.18 1 /* $NetBSD: syslog.c,v 1.18 1998/09/13 16:09:06 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.18 1998/09/13 16:09:06 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
60 #if __STDC__
61 #include <stdarg.h>
62 #else
63 #include <varargs.h>
64 #endif
65
66 #ifdef __weak_alias
67 __weak_alias(closelog,_closelog);
68 __weak_alias(openlog,_openlog);
69 __weak_alias(setlogmask,_setlogmask);
70 __weak_alias(syslog,_syslog);
71 __weak_alias(vsyslog,_vsyslog);
72 #endif
73
74 static int LogFile = -1; /* fd for log */
75 static int connected; /* have done connect */
76 static int LogStat = 0; /* status bits, set by openlog() */
77 static const char *LogTag = NULL; /* string to tag the entry with */
78 static int LogFacility = LOG_USER; /* default facility code */
79 static int LogMask = 0xff; /* mask of priorities to be logged */
80 extern char *__progname; /* Program name, from crt0. */
81
82 /*
83 * syslog, vsyslog --
84 * print message on log file; output is intended for syslogd(8).
85 */
86 void
87 #if __STDC__
88 syslog(int pri, const char *fmt, ...)
89 #else
90 syslog(pri, fmt, va_alist)
91 int pri;
92 char *fmt;
93 va_dcl
94 #endif
95 {
96 va_list ap;
97
98 #if __STDC__
99 va_start(ap, fmt);
100 #else
101 va_start(ap);
102 #endif
103 vsyslog(pri, fmt, ap);
104 va_end(ap);
105 }
106
107 void
108 vsyslog(pri, fmt, ap)
109 int pri;
110 const char *fmt;
111 va_list ap;
112 {
113 int cnt;
114 char ch, *p, *t;
115 time_t now;
116 struct tm tmnow;
117 int fd, saved_errno;
118 #define TBUF_LEN 2048
119 #define FMT_LEN 1024
120 char *stdp = NULL; /* pacify gcc */
121 char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
122 size_t tbuf_left, fmt_left, prlen;
123
124 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
125 /* Check for invalid bits. */
126 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
127 syslog(INTERNALLOG,
128 "syslog: unknown facility/priority: %x", pri);
129 pri &= LOG_PRIMASK|LOG_FACMASK;
130 }
131
132 /* Check priority against setlogmask values. */
133 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
134 return;
135
136 saved_errno = errno;
137
138 /* Set default facility if none specified. */
139 if ((pri & LOG_FACMASK) == 0)
140 pri |= LogFacility;
141
142 /* Build the message. */
143
144 /*
145 * Although it's tempting, we can't ignore the possibility of
146 * overflowing the buffer when assembling the "fixed" portion
147 * of the message. Strftime's "%h" directive expands to the
148 * locale's abbreviated month name, but if the user has the
149 * ability to construct to his own locale files, it may be
150 * arbitrarily long.
151 */
152 (void)time(&now);
153
154 p = tbuf;
155 tbuf_left = TBUF_LEN;
156
157 #define DEC() \
158 do { \
159 if (prlen >= tbuf_left) \
160 prlen = tbuf_left - 1; \
161 p += prlen; \
162 tbuf_left -= prlen; \
163 } while (0)
164
165 prlen = snprintf(p, tbuf_left, "<%d>", pri);
166 DEC();
167
168 tzset(); /* strftime() implies tzset(), localtime_r() doesn't. */
169 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime_r(&now, &tmnow));
170 DEC();
171
172 if (LogStat & LOG_PERROR)
173 stdp = p;
174 if (LogTag == NULL)
175 LogTag = __progname;
176 if (LogTag != NULL) {
177 prlen = snprintf(p, tbuf_left, "%s", LogTag);
178 DEC();
179 }
180 if (LogStat & LOG_PID) {
181 prlen = snprintf(p, tbuf_left, "[%d]", getpid());
182 DEC();
183 }
184 if (LogTag != NULL) {
185 if (tbuf_left > 1) {
186 *p++ = ':';
187 tbuf_left--;
188 }
189 if (tbuf_left > 1) {
190 *p++ = ' ';
191 tbuf_left--;
192 }
193 }
194
195 /*
196 * We wouldn't need this mess if printf handled %m, or if
197 * strerror() had been invented before syslog().
198 */
199 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
200 if (ch == '%' && fmt[1] == 'm') {
201 ++fmt;
202 prlen = snprintf(t, fmt_left, "%s",
203 strerror(saved_errno));
204 if (prlen >= fmt_left)
205 prlen = fmt_left - 1;
206 t += prlen;
207 fmt_left -= prlen;
208 } else {
209 if (fmt_left > 1) {
210 *t++ = ch;
211 fmt_left--;
212 }
213 }
214 }
215 *t = '\0';
216
217 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
218 DEC();
219 cnt = p - tbuf;
220
221 /* Output to stderr if requested. */
222 if (LogStat & LOG_PERROR) {
223 struct iovec iov[2];
224
225 iov[0].iov_base = stdp;
226 iov[0].iov_len = cnt - (stdp - tbuf);
227 iov[1].iov_base = "\n";
228 iov[1].iov_len = 1;
229 (void)writev(STDERR_FILENO, iov, 2);
230 }
231
232 /* Get connected, output the message to the local logger. */
233 if (!connected)
234 openlog(LogTag, LogStat | LOG_NDELAY, 0);
235 if (send(LogFile, tbuf, cnt, 0) >= 0)
236 return;
237
238 /*
239 * Output the message to the console; don't worry about blocking,
240 * if console blocks everything will. Make sure the error reported
241 * is the one from the syslogd failure.
242 */
243 if (LogStat & LOG_CONS &&
244 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
245 struct iovec iov[2];
246
247 p = strchr(tbuf, '>') + 1;
248 iov[0].iov_base = p;
249 iov[0].iov_len = cnt - (p - tbuf);
250 iov[1].iov_base = "\r\n";
251 iov[1].iov_len = 2;
252 (void)writev(fd, iov, 2);
253 (void)close(fd);
254 }
255 }
256
257 static struct sockaddr SyslogAddr; /* AF_LOCAL address of local logger */
258
259 void
260 openlog(ident, logstat, logfac)
261 const char *ident;
262 int logstat, logfac;
263 {
264 if (ident != NULL)
265 LogTag = ident;
266 LogStat = logstat;
267 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
268 LogFacility = logfac;
269
270 if (LogFile == -1) {
271 SyslogAddr.sa_family = AF_LOCAL;
272 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
273 sizeof(SyslogAddr.sa_data));
274 if (LogStat & LOG_NDELAY) {
275 if ((LogFile = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
276 return;
277 (void)fcntl(LogFile, F_SETFD, 1);
278 }
279 }
280 if (LogFile != -1 && !connected) {
281 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
282 (void)close(LogFile);
283 LogFile = -1;
284 } else
285 connected = 1;
286 }
287 }
288
289 void
290 closelog()
291 {
292 (void)close(LogFile);
293 LogFile = -1;
294 connected = 0;
295 }
296
297 /* setlogmask -- set the log mask level */
298 int
299 setlogmask(pmask)
300 int pmask;
301 {
302 int omask;
303
304 omask = LogMask;
305 if (pmask != 0)
306 LogMask = pmask;
307 return (omask);
308 }
309