]> git.saurik.com Git - apple/libc.git/blob - gen.subproj/syslog.c
Libc-167.tar.gz
[apple/libc.git] / gen.subproj / syslog.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 */
54
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <sys/uio.h>
59 #include <sys/un.h>
60 #include <netdb.h>
61
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <paths.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <time.h>
68 #include <unistd.h>
69
70 #ifdef __STDC__
71 #include <stdarg.h>
72 #else
73 #include <varargs.h>
74 #endif
75
76 #include <crt_externs.h>
77
78 static int LogFile = -1; /* fd for log */
79 static int connected; /* have done connect */
80 static int LogStat = 0; /* status bits, set by openlog() */
81 static const char *LogTag = NULL; /* string to tag the entry with */
82 static int LogFacility = LOG_USER; /* default facility code */
83 static int LogMask = 0xff; /* mask of priorities to be logged */
84
85 /*
86 * syslog, vsyslog --
87 * print message on log file; output is intended for syslogd(8).
88 */
89 void
90 #ifdef __STDC__
91 syslog(int pri, const char *fmt, ...)
92 #else
93 syslog(pri, fmt, va_alist)
94 int pri;
95 char *fmt;
96 va_dcl
97 #endif
98 {
99 va_list ap;
100
101 #ifdef __STDC__
102 va_start(ap, fmt);
103 #else
104 va_start(ap);
105 #endif
106 vsyslog(pri, fmt, ap);
107 va_end(ap);
108 }
109
110 void
111 vsyslog(pri, fmt, ap)
112 int pri;
113 register const char *fmt;
114 va_list ap;
115 {
116 register int cnt;
117 register char ch, *p, *t;
118 time_t now;
119 int fd, saved_errno;
120 #define TBUF_LEN 2048
121 #define FMT_LEN 1024
122 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
123 int tbuf_left, fmt_left, prlen;
124
125 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
126 /* Check for invalid bits. */
127 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
128 syslog(INTERNALLOG,
129 "syslog: unknown facility/priority: %x", pri);
130 pri &= LOG_PRIMASK|LOG_FACMASK;
131 }
132
133 /* Check priority against setlogmask values. */
134 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
135 return;
136
137 saved_errno = errno;
138
139 /* Set default facility if none specified. */
140 if ((pri & LOG_FACMASK) == 0)
141 pri |= LogFacility;
142
143 /* Build the message. */
144
145 /*
146 * Although it's tempting, we can't ignore the possibility of
147 * overflowing the buffer when assembling the "fixed" portion
148 * of the message. Strftime's "%h" directive expands to the
149 * locale's abbreviated month name, but if the user has the
150 * ability to construct to his own locale files, it may be
151 * arbitrarily long.
152 */
153 (void)time(&now);
154
155 p = tbuf;
156 tbuf_left = TBUF_LEN;
157
158 #define DEC() \
159 do { \
160 if (prlen >= tbuf_left) \
161 prlen = tbuf_left - 1; \
162 p += prlen; \
163 tbuf_left -= prlen; \
164 } while (0)
165
166 prlen = snprintf(p, tbuf_left, "<%d>", pri);
167 DEC();
168
169 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now));
170 DEC();
171
172 if (LogStat & LOG_PERROR)
173 stdp = p;
174 if (LogTag == NULL)
175 LogTag = *(*_NSGetArgv());
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); ++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_un SyslogAddr; /* AF_UNIX 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.sun_family = AF_UNIX;
272 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
273 sizeof(SyslogAddr.sun_path));
274 if (LogStat & LOG_NDELAY) {
275 if ((LogFile = socket(AF_UNIX, 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 void
289 closelog()
290 {
291 (void)close(LogFile);
292 LogFile = -1;
293 connected = 0;
294 }
295
296 /* setlogmask -- set the log mask level */
297 int
298 setlogmask(pmask)
299 int pmask;
300 {
301 int omask;
302
303 omask = LogMask;
304 if (pmask != 0)
305 LogMask = pmask;
306 return (omask);
307 }