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