]> git.saurik.com Git - apple/libc.git/blob - gen/syslog.c
Libc-391.5.22.tar.gz
[apple/libc.git] / gen / syslog.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/syslog.h>
59 #include <sys/uio.h>
60 #include <sys/un.h>
61 #include <netdb.h>
62
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <paths.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <time.h>
70 #include <unistd.h>
71 #include <notify.h>
72
73 #ifdef __STDC__
74 #include <stdarg.h>
75 #else
76 #include <varargs.h>
77 #endif
78
79 #include <crt_externs.h>
80
81 #define LOG_NO_NOTIFY 0x1000
82
83 #ifdef BUILDING_VARIANT
84 __private_extern__ int _sl_LogFile; /* fd for log */
85 __private_extern__ int _sl_connected; /* have done connect */
86 __private_extern__ int _sl_LogStat; /* status bits, set by openlog() */
87 __private_extern__ const char *_sl_LogTag; /* string to tag the entry with */
88 __private_extern__ int _sl_LogFacility; /* default facility code */
89 __private_extern__ int _sl_LogMask; /* mask of priorities to be logged */
90 __private_extern__ int _sl_NotifyToken; /* for remote control of priority filter */
91 __private_extern__ int _sl_NotifyMaster; /* for remote control of priority filter */
92 #else /* !BUILDING_VARIANT */
93 __private_extern__ int _sl_LogFile = -1; /* fd for log */
94 __private_extern__ int _sl_connected = 0; /* have done connect */
95 __private_extern__ int _sl_LogStat = 0; /* status bits, set by openlog() */
96 __private_extern__ const char *_sl_LogTag = NULL; /* string to tag the entry with */
97 __private_extern__ int _sl_LogFacility = LOG_USER; /* default facility code */
98 __private_extern__ int _sl_LogMask = 0xff; /* mask of priorities to be logged */
99 __private_extern__ int _sl_NotifyToken = -1; /* for remote control of max logged priority */
100 __private_extern__ int _sl_NotifyMaster = -1; /* for remote control of max logged priority */
101 #endif /* BUILDING_VARIANT */
102
103 __private_extern__ void _sl_init_notify();
104
105 #define NOTIFY_SYSTEM_MASTER "com.apple.system.syslog.master"
106 #define NOTIFY_PREFIX_SYSTEM "com.apple.system.syslog"
107 #define NOTIFY_PREFIX_USER "user.syslog"
108 #define NOTIFY_STATE_OFFSET 1000
109
110 /* notify SPI */
111 uint32_t notify_get_state(int token, int *state);
112 uint32_t notify_register_plain(const char *name, int *out_token);
113
114 /*
115 * syslog, vsyslog --
116 * print message on log file; output is intended for syslogd(8).
117 */
118 void
119 #ifdef __STDC__
120 syslog(int pri, const char *fmt, ...)
121 #else
122 syslog(pri, fmt, va_alist)
123 int pri;
124 char *fmt;
125 va_dcl
126 #endif
127 {
128 va_list ap;
129
130 #ifdef __STDC__
131 va_start(ap, fmt);
132 #else
133 va_start(ap);
134 #endif
135 vsyslog(pri, fmt, ap);
136 va_end(ap);
137 }
138
139 void
140 vsyslog(pri, fmt, ap)
141 int pri;
142 register const char *fmt;
143 va_list ap;
144 {
145 register int cnt;
146 register char ch, *p, *t;
147 time_t now;
148 int fd, saved_errno, filter, cval, rc_filter, primask;
149 #define TBUF_LEN 2048
150 #define FMT_LEN 1024
151 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
152 int tbuf_left, fmt_left, prlen;
153 struct tm tm;
154
155 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
156 /* Check for invalid bits. */
157 if (pri & ~(LOG_PRIMASK|LOG_FACMASK))
158 {
159 syslog(INTERNALLOG, "syslog: unknown facility/priority: %x", pri);
160 pri &= LOG_PRIMASK|LOG_FACMASK;
161 }
162
163 /* Get remote-control priority filter */
164 filter = _sl_LogMask;
165 rc_filter = 0;
166
167 _sl_init_notify();
168
169 if (_sl_NotifyToken >= 0)
170 {
171 if (notify_get_state(_sl_NotifyToken, &cval) == NOTIFY_STATUS_OK)
172 {
173 if (cval != 0)
174 {
175 filter = cval;
176 rc_filter = 1;
177 }
178 }
179 }
180
181 if ((rc_filter == 0) && (_sl_NotifyMaster >= 0))
182 {
183 if (notify_get_state(_sl_NotifyMaster, &cval) == NOTIFY_STATUS_OK)
184 {
185 if (cval != 0)
186 {
187 filter = cval;
188 }
189 }
190 }
191
192 primask = LOG_MASK(LOG_PRI(pri));
193 if ((primask & filter) == 0) return;
194
195 saved_errno = errno;
196
197 /* Set default facility if none specified. */
198 if ((pri & LOG_FACMASK) == 0) pri |= _sl_LogFacility;
199
200 /* Build the message. */
201
202 /*
203 * Although it's tempting, we can't ignore the possibility of
204 * overflowing the buffer when assembling the "fixed" portion
205 * of the message. Strftime's "%h" directive expands to the
206 * locale's abbreviated month name, but if the user has the
207 * ability to construct to his own locale files, it may be
208 * arbitrarily long.
209 */
210 (void)time(&now);
211
212 p = tbuf;
213 tbuf_left = TBUF_LEN;
214
215 #define DEC() \
216 do { \
217 if (prlen >= tbuf_left) \
218 prlen = tbuf_left - 1; \
219 p += prlen; \
220 tbuf_left -= prlen; \
221 } while (0)
222
223 prlen = snprintf(p, tbuf_left, "<%d>", pri);
224 DEC();
225
226 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime_r(&now, &tm));
227 DEC();
228
229 if (_sl_LogStat & LOG_PERROR) stdp = p;
230
231 if (_sl_LogTag == NULL) _sl_LogTag = *(*_NSGetArgv());
232
233 if (_sl_LogTag != NULL)
234 {
235 prlen = snprintf(p, tbuf_left, "%s", _sl_LogTag);
236 DEC();
237 }
238
239 if (_sl_LogStat & LOG_PID)
240 {
241 prlen = snprintf(p, tbuf_left, "[%d]", getpid());
242 DEC();
243 }
244
245 if (_sl_LogTag != NULL)
246 {
247 if (tbuf_left > 1)
248 {
249 *p++ = ':';
250 tbuf_left--;
251 }
252 if (tbuf_left > 1)
253 {
254 *p++ = ' ';
255 tbuf_left--;
256 }
257 }
258
259 /*
260 * We wouldn't need this mess if printf handled %m, or if
261 * strerror() had been invented before syslog().
262 */
263 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt)
264 {
265 if (ch == '%' && fmt[1] == 'm')
266 {
267 ++fmt;
268 prlen = snprintf(t, fmt_left, "%s", strerror(saved_errno));
269 if (prlen >= fmt_left) prlen = fmt_left - 1;
270 t += prlen;
271 fmt_left -= prlen;
272 }
273 else
274 {
275 if (fmt_left > 1)
276 {
277 *t++ = ch;
278 fmt_left--;
279 }
280 }
281 }
282
283 *t = '\0';
284
285 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
286 DEC();
287 cnt = p - tbuf;
288
289 /* Output to stderr if requested. */
290 if (_sl_LogStat & LOG_PERROR)
291 {
292 struct iovec iov[2];
293
294 iov[0].iov_base = stdp;
295 iov[0].iov_len = cnt - (stdp - tbuf);
296 iov[1].iov_base = "\n";
297 iov[1].iov_len = 1;
298 (void)writev(STDERR_FILENO, iov, 2);
299 }
300
301 /* Get connected, output the message to the local logger. */
302 if (_sl_connected == 0) openlog(_sl_LogTag, _sl_LogStat | LOG_NDELAY, 0);
303 if (send(_sl_LogFile, tbuf, cnt, 0) >= 0) return;
304
305 /*
306 * Output the message to the console; don't worry about blocking,
307 * if console blocks everything will. Make sure the error reported
308 * is the one from the syslogd failure.
309 */
310 if (_sl_LogStat & LOG_CONS && (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0)
311 {
312 struct iovec iov[2];
313
314 p = strchr(tbuf, '>') + 1;
315 iov[0].iov_base = p;
316 iov[0].iov_len = cnt - (p - tbuf);
317 iov[1].iov_base = "\r\n";
318 iov[1].iov_len = 2;
319 (void)writev(fd, iov, 2);
320 (void)close(fd);
321 }
322 }
323
324 #ifndef BUILDING_VARIANT
325
326 static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
327
328 __private_extern__ void
329 _sl_init_notify()
330 {
331 int status;
332 char *notify_name;
333 const char *prefix;
334
335 if (_sl_LogStat & LOG_NO_NOTIFY)
336 {
337 _sl_NotifyMaster = -2;
338 _sl_NotifyToken = -2;
339 return;
340 }
341
342 if (_sl_NotifyMaster == -1)
343 {
344 status = notify_register_plain(NOTIFY_SYSTEM_MASTER, &_sl_NotifyMaster);
345 if (status != NOTIFY_STATUS_OK) _sl_NotifyMaster = -2;
346 }
347
348 if (_sl_NotifyToken == -1)
349 {
350 _sl_NotifyToken = -2;
351
352 notify_name = NULL;
353 prefix = NOTIFY_PREFIX_USER;
354 if (getuid() == 0) prefix = NOTIFY_PREFIX_SYSTEM;
355 asprintf(&notify_name, "%s.%d", prefix, getpid());
356
357 if (notify_name != NULL)
358 {
359 status = notify_register_plain(notify_name, &_sl_NotifyToken);
360 free(notify_name);
361 if (status != NOTIFY_STATUS_OK) _sl_NotifyToken = -2;
362 }
363 }
364 }
365
366 void
367 openlog(ident, logstat, logfac)
368 const char *ident;
369 int logstat, logfac;
370 {
371 if (ident != NULL) _sl_LogTag = ident;
372
373 _sl_LogStat = logstat;
374
375 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) _sl_LogFacility = logfac;
376
377 if (_sl_LogFile == -1)
378 {
379 SyslogAddr.sun_family = AF_UNIX;
380 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, sizeof(SyslogAddr.sun_path));
381 if (_sl_LogStat & LOG_NDELAY)
382 {
383 if ((_sl_LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) return;
384 (void)fcntl(_sl_LogFile, F_SETFD, 1);
385 }
386 }
387
388 if ((_sl_LogFile != -1) && (_sl_connected == 0))
389 {
390 if (connect(_sl_LogFile, (struct sockaddr *)&SyslogAddr, sizeof(SyslogAddr)) == -1)
391 {
392 (void)close(_sl_LogFile);
393 _sl_LogFile = -1;
394 }
395 else
396 {
397 _sl_connected = 1;
398 }
399 }
400
401 _sl_init_notify();
402 }
403
404 void
405 closelog()
406 {
407 (void)close(_sl_LogFile);
408 _sl_LogFile = -1;
409 _sl_connected = 0;
410 }
411
412 /* setlogmask -- set the log mask level */
413 int
414 setlogmask(pmask)
415 int pmask;
416 {
417 int omask;
418
419 omask = _sl_LogMask;
420 if (pmask != 0) _sl_LogMask = pmask;
421 return (omask);
422 }
423
424 #endif /* !BUILDING_VARIANT */