]> git.saurik.com Git - apple/libc.git/blob - gen/syslog.c
Libc-498.1.7.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/param.h>
58 #include <sys/socket.h>
59 #include <sys/syslog.h>
60 #include <sys/uio.h>
61 #include <sys/un.h>
62 #include <netdb.h>
63
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <paths.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <stdint.h>
70 #include <string.h>
71 #include <time.h>
72 #include <sys/time.h>
73 #include <unistd.h>
74 #include <notify.h>
75 #include <asl.h>
76 #include <asl_private.h>
77
78 #ifdef __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
83
84 #include <crt_externs.h>
85
86 #define LOG_NO_NOTIFY 0x1000
87 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
88
89 #ifdef BUILDING_VARIANT
90 __private_extern__ int _sl_LogFile; /* fd for log */
91 __private_extern__ int _sl_connected; /* have done connect */
92 __private_extern__ int _sl_LogStat; /* status bits, set by openlog() */
93 __private_extern__ const char *_sl_LogTag; /* string to tag the entry with */
94 __private_extern__ int _sl_LogFacility; /* default facility code */
95 __private_extern__ int _sl_LogMask; /* mask of priorities to be logged */
96 __private_extern__ int _sl_NotifyToken; /* for remote control of priority filter */
97 __private_extern__ int _sl_NotifyMaster; /* for remote control of priority filter */
98 #else /* !BUILDING_VARIANT */
99 __private_extern__ int _sl_LogFile = -1; /* fd for log */
100 __private_extern__ int _sl_connected = 0; /* have done connect */
101 __private_extern__ int _sl_LogStat = 0; /* status bits, set by openlog() */
102 __private_extern__ const char *_sl_LogTag = NULL; /* string to tag the entry with */
103 __private_extern__ int _sl_LogFacility = LOG_USER; /* default facility code */
104 __private_extern__ int _sl_LogMask = 0xff; /* mask of priorities to be logged */
105 __private_extern__ int _sl_NotifyToken = -1; /* for remote control of max logged priority */
106 __private_extern__ int _sl_NotifyMaster = -1; /* for remote control of max logged priority */
107 #endif /* BUILDING_VARIANT */
108
109 __private_extern__ void _sl_init_notify();
110
111 #define NOTIFY_SYSTEM_MASTER "com.apple.system.syslog.master"
112 #define NOTIFY_PREFIX_SYSTEM "com.apple.system.syslog"
113 #define NOTIFY_PREFIX_USER "user.syslog"
114 #define NOTIFY_STATE_OFFSET 1000
115
116 /* notify SPI */
117 uint32_t notify_register_plain(const char *name, int *out_token);
118 const char *asl_syslog_faciliy_num_to_name(int);
119
120 /*
121 * syslog, vsyslog --
122 * print message on log file; output is intended for syslogd(8).
123 */
124 void
125 #ifdef __STDC__
126 syslog(int pri, const char *fmt, ...)
127 #else
128 syslog(pri, fmt, va_alist)
129 int pri;
130 char *fmt;
131 va_dcl
132 #endif
133 {
134 va_list ap;
135
136 #ifdef __STDC__
137 va_start(ap, fmt);
138 #else
139 va_start(ap);
140 #endif
141 vsyslog(pri, fmt, ap);
142 va_end(ap);
143 }
144
145 void
146 vsyslog(int pri, const char *fmt, va_list ap)
147 {
148 int status, i, saved_errno, filter, rc_filter;
149 time_t tick;
150 struct timeval tval;
151 pid_t pid;
152 uint32_t elen, count;
153 char *p, *str, *expanded, *err_str, hname[MAXHOSTNAMELEN+1];
154 uint64_t cval;
155 int fd, mask, level, facility;
156 aslmsg msg;
157
158 saved_errno = errno;
159
160 /* Check for invalid bits. */
161 if (pri & ~(LOG_PRIMASK | LOG_FACMASK))
162 {
163 syslog(INTERNALLOG, "syslog: unknown facility/priority: %x", pri);
164 pri &= (LOG_PRIMASK | LOG_FACMASK);
165 }
166
167 level = LOG_PRI(pri);
168 facility = pri & LOG_FACMASK;
169
170 if (facility == 0) facility = _sl_LogFacility;
171
172 /* Get remote-control priority filter */
173 filter = _sl_LogMask;
174 rc_filter = 0;
175
176 _sl_init_notify();
177
178 if (_sl_NotifyToken >= 0)
179 {
180 if (notify_get_state(_sl_NotifyToken, &cval) == NOTIFY_STATUS_OK)
181 {
182 if (cval != 0)
183 {
184 filter = cval;
185 rc_filter = 1;
186 }
187 }
188 }
189
190 if ((rc_filter == 0) && (_sl_NotifyMaster >= 0))
191 {
192 if (notify_get_state(_sl_NotifyMaster, &cval) == NOTIFY_STATUS_OK)
193 {
194 if (cval != 0)
195 {
196 filter = cval;
197 }
198 }
199 }
200
201 mask = LOG_MASK(level);
202 if ((mask & filter) == 0) return;
203
204 /* Build the message. */
205 msg = asl_new(ASL_TYPE_MSG);
206
207 if (_sl_LogTag == NULL) _sl_LogTag = *(*_NSGetArgv());
208 if (_sl_LogTag != NULL)
209 {
210 asl_set(msg, ASL_KEY_SENDER, _sl_LogTag);
211 }
212
213 str = (char *)asl_syslog_faciliy_num_to_name(facility);
214 if (str != NULL) asl_set(msg, ASL_KEY_FACILITY, str);
215
216 str = NULL;
217 memset(&tval, 0, sizeof(struct timeval));
218
219 status = gettimeofday(&tval, NULL);
220 if (status == 0)
221 {
222 str = NULL;
223 asprintf(&str, "%lu", tval.tv_sec);
224 if (str != NULL)
225 {
226 asl_set(msg, ASL_KEY_TIME, str);
227 free(str);
228 }
229
230 str = NULL;
231 asprintf(&str, "%lu", tval.tv_usec * 1000);
232 if (str != NULL)
233 {
234 asl_set(msg, ASL_KEY_TIME_NSEC, str);
235 free(str);
236 }
237 }
238 else
239 {
240 tick = time(NULL);
241 str = NULL;
242 asprintf(&str, "%lu", tick);
243 if (str != NULL)
244 {
245 asl_set(msg, ASL_KEY_TIME, str);
246 free(str);
247 }
248 }
249
250 pid = getpid();
251 str = NULL;
252 asprintf(&str, "%u", pid);
253 if (str != NULL)
254 {
255 asl_set(msg, ASL_KEY_PID, str);
256 free(str);
257 }
258
259 str = NULL;
260 asprintf(&str, "%d", getuid());
261 if (str != NULL)
262 {
263 asl_set(msg, ASL_KEY_UID, str);
264 free(str);
265 }
266
267 str = NULL;
268 asprintf(&str, "%u", getgid());
269 if (str != NULL)
270 {
271 asl_set(msg, ASL_KEY_GID, str);
272 free(str);
273 }
274
275 str = NULL;
276 asprintf(&str, "%u", level);
277 if (str != NULL)
278 {
279 asl_set(msg, ASL_KEY_LEVEL, str);
280 free(str);
281 }
282
283 status = gethostname(hname, MAXHOSTNAMELEN);
284 if (status < 0) asl_set(msg, ASL_KEY_HOST, "localhost");
285 else asl_set(msg, ASL_KEY_HOST, hname);
286
287 /* check for %m */
288 count = 0;
289 for (i = 0; fmt[i] != '\0'; i++)
290 {
291 if ((fmt[i] == '%') && (fmt[i+1] == 'm')) count++;
292 }
293
294 expanded = NULL;
295 elen = 0;
296 err_str = NULL;
297
298 /* deal with malloc failures gracefully */
299 if (count > 0)
300 {
301 err_str = strdup(strerror(saved_errno));
302 if (err_str == NULL) count = 0;
303 else
304 {
305 elen = strlen(err_str);
306 expanded = malloc(i + (count * elen));
307 if (expanded == NULL) count = 0;
308 }
309 }
310
311 if (expanded == NULL) expanded = (char *)fmt;
312 if (count > 0)
313 {
314 p = expanded;
315
316 for (i = 0; fmt[i] != '\0'; i++)
317 {
318 if ((fmt[i] == '%') && (fmt[i+1] == 'm'))
319 {
320 memcpy(p, err_str, elen);
321 p += elen;
322 i++;
323 }
324 else
325 {
326 *p++ = fmt[i];
327 }
328 }
329
330 *p = '\0';
331 }
332
333 if (err_str != NULL) free(err_str);
334
335 vasprintf(&str, expanded, ap);
336 if (count > 0) free(expanded);
337
338 if (str != NULL)
339 {
340 asl_set(msg, ASL_KEY_MSG, str);
341
342 /* Output to stderr if requested. */
343 if (_sl_LogStat & LOG_PERROR)
344 {
345 p = NULL;
346 if (_sl_LogStat & LOG_PID) asprintf(&p, "%s[%u]: %s", (_sl_LogTag == NULL) ? "???" : _sl_LogTag, pid, str);
347 else asprintf(&p, "%s: %s", (_sl_LogTag == NULL) ? "???" : _sl_LogTag, str);
348
349 if (p != NULL)
350 {
351 struct iovec iov[2];
352
353 iov[0].iov_base = p;
354 iov[0].iov_len = strlen(p);
355 iov[1].iov_base = "\n";
356 iov[1].iov_len = 1;
357 writev(STDERR_FILENO, iov, 2);
358 free(p);
359 }
360 }
361
362 free(str);
363 }
364
365 /* Get connected, output the message to the local logger. */
366 str = asl_format_message(msg, ASL_MSG_FMT_RAW, ASL_TIME_FMT_SEC, ASL_ENCODE_ASL, &count);
367 if (str != NULL)
368 {
369 p = NULL;
370 asprintf(&p, "%10u %s", count, str);
371 free(str);
372
373 if (p != NULL)
374 {
375 count += 12;
376 if (_sl_connected == 0) openlog(_sl_LogTag, _sl_LogStat | LOG_NDELAY, 0);
377
378 status = send(_sl_LogFile, p, count, 0);
379 if (status< 0)
380 {
381 closelog();
382 openlog(_sl_LogTag, _sl_LogStat | LOG_NDELAY, 0);
383 status = send(_sl_LogFile, p, count, 0);
384 }
385
386 if (status >= 0)
387 {
388 free(p);
389 asl_free(msg);
390 return;
391 }
392
393 free(p);
394 }
395 }
396
397 /*
398 * Output the message to the console.
399 */
400 if (_sl_LogStat & LOG_CONS && (fd = open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0)
401 {
402 count = 0;
403
404 p = asl_format_message(msg, ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_ENCODE_SAFE, &count);
405 if (p != NULL)
406 {
407 struct iovec iov;
408
409 /* count includes trailing nul */
410 iov.iov_len = count - 1;
411 iov.iov_base = p;
412 writev(fd, &iov, 1);
413
414 free(p);
415 }
416
417 close(fd);
418 }
419
420 asl_free(msg);
421 }
422
423 #ifndef BUILDING_VARIANT
424
425 static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
426
427 __private_extern__ void
428 _sl_init_notify()
429 {
430 int status;
431 char *notify_name;
432 const char *prefix;
433
434 if (_sl_LogStat & LOG_NO_NOTIFY)
435 {
436 _sl_NotifyMaster = -2;
437 _sl_NotifyToken = -2;
438 return;
439 }
440
441 if (_sl_NotifyMaster == -1)
442 {
443 status = notify_register_plain(NOTIFY_SYSTEM_MASTER, &_sl_NotifyMaster);
444 if (status != NOTIFY_STATUS_OK) _sl_NotifyMaster = -2;
445 }
446
447 if (_sl_NotifyToken == -1)
448 {
449 _sl_NotifyToken = -2;
450
451 notify_name = NULL;
452 prefix = NOTIFY_PREFIX_USER;
453 if (getuid() == 0) prefix = NOTIFY_PREFIX_SYSTEM;
454 asprintf(&notify_name, "%s.%d", prefix, getpid());
455
456 if (notify_name != NULL)
457 {
458 status = notify_register_plain(notify_name, &_sl_NotifyToken);
459 free(notify_name);
460 if (status != NOTIFY_STATUS_OK) _sl_NotifyToken = -2;
461 }
462 }
463 }
464
465 void
466 openlog(ident, logstat, logfac)
467 const char *ident;
468 int logstat, logfac;
469 {
470 if (ident != NULL) _sl_LogTag = ident;
471
472 _sl_LogStat = logstat;
473
474 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) _sl_LogFacility = logfac;
475
476 if (_sl_LogFile == -1)
477 {
478 SyslogAddr.sun_family = AF_UNIX;
479 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, sizeof(SyslogAddr.sun_path));
480 if (_sl_LogStat & LOG_NDELAY)
481 {
482 if ((_sl_LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) return;
483 (void)fcntl(_sl_LogFile, F_SETFD, 1);
484 }
485 }
486
487 if ((_sl_LogFile != -1) && (_sl_connected == 0))
488 {
489 if (connect(_sl_LogFile, (struct sockaddr *)&SyslogAddr, sizeof(SyslogAddr)) == -1)
490 {
491 (void)close(_sl_LogFile);
492 _sl_LogFile = -1;
493 }
494 else
495 {
496 _sl_connected = 1;
497 }
498 }
499
500 _sl_init_notify();
501 }
502
503 void
504 closelog()
505 {
506 if (_sl_LogFile >= 0) {
507 (void)close(_sl_LogFile);
508 _sl_LogFile = -1;
509 }
510 _sl_connected = 0;
511 }
512
513 /* setlogmask -- set the log mask level */
514 int
515 setlogmask(pmask)
516 int pmask;
517 {
518 int omask;
519
520 omask = _sl_LogMask;
521 if (pmask != 0) _sl_LogMask = pmask;
522 return (omask);
523 }
524
525 #endif /* !BUILDING_VARIANT */