]> git.saurik.com Git - apple/libc.git/blob - gen/syslog.c
Libc-594.1.4.tar.gz
[apple/libc.git] / gen / syslog.c
1 /*
2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <sys/syslog.h>
61 #include <sys/uio.h>
62 #include <sys/un.h>
63 #include <netdb.h>
64 #include <mach/mach.h>
65 #include <servers/bootstrap.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <paths.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <stdint.h>
72 #include <string.h>
73 #include <time.h>
74 #include <sys/time.h>
75 #include <unistd.h>
76 #include <notify.h>
77 #include <asl.h>
78 #include <asl_private.h>
79 #include <asl_ipc.h>
80
81 #ifdef __STDC__
82 #include <stdarg.h>
83 #else
84 #include <varargs.h>
85 #endif
86
87 #include <crt_externs.h>
88
89 #define LOG_NO_NOTIFY 0x1000
90 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
91
92 #ifdef BUILDING_VARIANT
93 __private_extern__ int _sl_LogStat; /* status bits, set by openlog() */
94 __private_extern__ const char *_sl_LogTag; /* string to tag the entry with */
95 __private_extern__ int _sl_LogFacility; /* default facility code */
96 __private_extern__ int _sl_LogMask; /* local mask of priorities to be logged */
97 __private_extern__ int _sl_MasterLogMask; /* master (remote control) mask of priorities to be logged */
98 __private_extern__ int _sl_ProcLogMask; /* process-specific (remote control) mask of priorities to be logged */
99 __private_extern__ int _sl_RCToken; /* for remote control change notification */
100 __private_extern__ int _sl_NotifyToken; /* for remote control of priority filter */
101 __private_extern__ int _sl_NotifyMaster; /* for remote control of priority filter */
102 __private_extern__ int _sl_pid; /* pid */
103 #else /* !BUILDING_VARIANT */
104 __private_extern__ int _sl_LogStat = 0; /* status bits, set by openlog() */
105 __private_extern__ const char *_sl_LogTag = NULL; /* string to tag the entry with */
106 __private_extern__ int _sl_LogFacility = LOG_USER; /* default facility code */
107 __private_extern__ int _sl_LogMask = 0xff; /* mask of priorities to be logged */
108 __private_extern__ int _sl_MasterLogMask = 0; /* master mask of priorities to be logged */
109 __private_extern__ int _sl_ProcLogMask = 0; /* process-specific mask of priorities to be logged */
110 __private_extern__ int _sl_RCToken = -1; /* for remote control change notification */
111 __private_extern__ int _sl_NotifyToken = -1; /* for remote control of max logged priority */
112 __private_extern__ int _sl_NotifyMaster = -1; /* for remote control of max logged priority */
113 __private_extern__ int _sl_pid = -1; /* pid */
114 #endif /* BUILDING_VARIANT */
115
116 __private_extern__ void _sl_init_notify();
117
118 #define ASL_SERVICE_NAME "com.apple.system.logger"
119 static mach_port_t asl_server_port = MACH_PORT_NULL;
120
121 #define NOTIFY_SYSTEM_MASTER "com.apple.system.syslog.master"
122 #define NOTIFY_PREFIX_SYSTEM "com.apple.system.syslog"
123 #define NOTIFY_PREFIX_USER "user.syslog"
124 #define NOTIFY_STATE_OFFSET 1000
125
126 /* notify SPI */
127 uint32_t notify_register_plain(const char *name, int *out_token);
128 const char *asl_syslog_faciliy_num_to_name(int);
129
130 /*
131 * syslog, vsyslog --
132 * print message on log file; output is intended for syslogd(8).
133 */
134 void
135 #ifdef __STDC__
136 syslog(int pri, const char *fmt, ...)
137 #else
138 syslog(pri, fmt, va_alist)
139 int pri;
140 char *fmt;
141 va_dcl
142 #endif
143 {
144 va_list ap;
145
146 #ifdef __STDC__
147 va_start(ap, fmt);
148 #else
149 va_start(ap);
150 #endif
151 vsyslog(pri, fmt, ap);
152 va_end(ap);
153 }
154
155 void
156 vsyslog(int pri, const char *fmt, va_list ap)
157 {
158 int status, i, saved_errno, filter, check, rc_filter;
159 time_t tick;
160 struct timeval tval;
161 uint32_t elen, count, outlen;
162 char *p, *str, *expanded, *err_str, hname[MAXHOSTNAMELEN+1];
163 const char *val;
164 uint64_t cval;
165 int fd, mask, level, facility;
166 aslmsg msg;
167 kern_return_t kstatus;
168 caddr_t out;
169
170 saved_errno = errno;
171
172 if (_sl_pid == -1) _sl_pid = getpid();
173
174 /* Check for invalid bits. */
175 if (pri & ~(LOG_PRIMASK | LOG_FACMASK))
176 {
177 syslog(INTERNALLOG, "syslog: unknown facility/priority: %x", pri);
178 pri &= (LOG_PRIMASK | LOG_FACMASK);
179 }
180
181 level = LOG_PRI(pri);
182 facility = pri & LOG_FACMASK;
183
184 if (facility == 0) facility = _sl_LogFacility;
185
186 _sl_init_notify();
187
188 /* initialize or re-check process-specific and master filters */
189 if (_sl_RCToken >= 0)
190 {
191 check = 0;
192 status = notify_check(_sl_RCToken, &check);
193 if ((status == NOTIFY_STATUS_OK) && (check != 0))
194 {
195 if (_sl_NotifyMaster >= 0)
196 {
197 cval = 0;
198 if (notify_get_state(_sl_NotifyMaster, &cval) == NOTIFY_STATUS_OK) _sl_MasterLogMask = cval;
199 }
200
201 if (_sl_NotifyToken >= 0)
202 {
203 cval = 0;
204 if (notify_get_state(_sl_NotifyToken, &cval) == NOTIFY_STATUS_OK) _sl_ProcLogMask = cval;
205 }
206 }
207 }
208
209 filter = _sl_LogMask;
210 rc_filter = 0;
211
212 /* master filter overrides local filter */
213 if (_sl_MasterLogMask != 0)
214 {
215 filter = _sl_MasterLogMask;
216 rc_filter = 1;
217 }
218
219 /* process-specific filter overrides local and master */
220 if (_sl_ProcLogMask != 0)
221 {
222 filter = _sl_ProcLogMask;
223 rc_filter = 1;
224 }
225
226 mask = LOG_MASK(level);
227 if ((mask & filter) == 0) return;
228
229 /* Build the message. */
230 msg = asl_new(ASL_TYPE_MSG);
231
232 if (_sl_LogTag == NULL) _sl_LogTag = *(*_NSGetArgv());
233 if (_sl_LogTag != NULL)
234 {
235 asl_set(msg, ASL_KEY_SENDER, _sl_LogTag);
236 }
237
238 str = (char *)asl_syslog_faciliy_num_to_name(facility);
239 if (str != NULL) asl_set(msg, ASL_KEY_FACILITY, str);
240
241 str = NULL;
242 memset(&tval, 0, sizeof(struct timeval));
243
244 status = gettimeofday(&tval, NULL);
245 if (status == 0)
246 {
247 str = NULL;
248 asprintf(&str, "%lu", tval.tv_sec);
249 if (str != NULL)
250 {
251 asl_set(msg, ASL_KEY_TIME, str);
252 free(str);
253 }
254
255 str = NULL;
256 asprintf(&str, "%lu", tval.tv_usec * 1000);
257 if (str != NULL)
258 {
259 asl_set(msg, ASL_KEY_TIME_NSEC, str);
260 free(str);
261 }
262 }
263 else
264 {
265 tick = time(NULL);
266 str = NULL;
267 asprintf(&str, "%lu", tick);
268 if (str != NULL)
269 {
270 asl_set(msg, ASL_KEY_TIME, str);
271 free(str);
272 }
273 }
274
275 str = NULL;
276 asprintf(&str, "%u", _sl_pid);
277 if (str != NULL)
278 {
279 asl_set(msg, ASL_KEY_PID, str);
280 free(str);
281 }
282
283 str = NULL;
284 asprintf(&str, "%d", getuid());
285 if (str != NULL)
286 {
287 asl_set(msg, ASL_KEY_UID, str);
288 free(str);
289 }
290
291 str = NULL;
292 asprintf(&str, "%u", getgid());
293 if (str != NULL)
294 {
295 asl_set(msg, ASL_KEY_GID, str);
296 free(str);
297 }
298
299 str = NULL;
300 asprintf(&str, "%u", level);
301 if (str != NULL)
302 {
303 asl_set(msg, ASL_KEY_LEVEL, str);
304 free(str);
305 }
306
307 status = gethostname(hname, MAXHOSTNAMELEN);
308 if (status < 0) asl_set(msg, ASL_KEY_HOST, "localhost");
309 else asl_set(msg, ASL_KEY_HOST, hname);
310
311 /* check for %m */
312 count = 0;
313 for (i = 0; fmt[i] != '\0'; i++)
314 {
315 if ((fmt[i] == '%') && (fmt[i+1] == 'm')) count++;
316 }
317
318 expanded = NULL;
319 elen = 0;
320 err_str = NULL;
321
322 /* deal with malloc failures gracefully */
323 if (count > 0)
324 {
325 err_str = strdup(strerror(saved_errno));
326 if (err_str == NULL) count = 0;
327 else
328 {
329 elen = strlen(err_str);
330 expanded = malloc(i + (count * elen));
331 if (expanded == NULL) count = 0;
332 }
333 }
334
335 if (expanded == NULL) expanded = (char *)fmt;
336 if (count > 0)
337 {
338 p = expanded;
339
340 for (i = 0; fmt[i] != '\0'; i++)
341 {
342 if ((fmt[i] == '%') && (fmt[i+1] == 'm'))
343 {
344 memcpy(p, err_str, elen);
345 p += elen;
346 i++;
347 }
348 else
349 {
350 *p++ = fmt[i];
351 }
352 }
353
354 *p = '\0';
355 }
356
357 if (err_str != NULL) free(err_str);
358
359 vasprintf(&str, expanded, ap);
360 if (count > 0) free(expanded);
361
362 if (str != NULL)
363 {
364 asl_set(msg, ASL_KEY_MSG, str);
365
366 /* Output to stderr if requested. */
367 if (_sl_LogStat & LOG_PERROR)
368 {
369 p = NULL;
370 if (_sl_LogStat & LOG_PID) asprintf(&p, "%s[%u]: %s", (_sl_LogTag == NULL) ? "???" : _sl_LogTag, _sl_pid, str);
371 else asprintf(&p, "%s: %s", (_sl_LogTag == NULL) ? "???" : _sl_LogTag, str);
372
373 if (p != NULL)
374 {
375 struct iovec iov[2];
376
377 iov[0].iov_base = p;
378 iov[0].iov_len = strlen(p);
379 iov[1].iov_base = "\n";
380 iov[1].iov_len = 1;
381 writev(STDERR_FILENO, iov, 2);
382 free(p);
383 }
384 }
385
386 free(str);
387 }
388
389 /* Set "ASLOption store" if remote control is active */
390 if (rc_filter != 0)
391 {
392 val = asl_get(msg, ASL_KEY_OPTION);
393 if (val == NULL)
394 {
395 asl_set(msg, ASL_KEY_OPTION, ASL_OPT_STORE);
396 }
397 else
398 {
399 str = NULL;
400 asprintf(&str, "%s %s", ASL_OPT_STORE, val);
401 if (str != NULL)
402 {
403 asl_set(msg, ASL_KEY_OPTION, str);
404 free(str);
405 str = NULL;
406 }
407 }
408 }
409
410 /* send a mach message to syslogd */
411 str = asl_format_message(msg, ASL_MSG_FMT_RAW, ASL_TIME_FMT_SEC, ASL_ENCODE_ASL, &count);
412 if (str != NULL)
413 {
414 outlen = count + 11;
415 kstatus = vm_allocate(mach_task_self(), (vm_address_t *)&out, outlen + 1, TRUE);
416 if (kstatus == KERN_SUCCESS)
417 {
418 memset(out, 0, outlen + 1);
419 snprintf((char *)out, outlen, "%10u %s", count, str);
420
421 status = 0;
422 if (asl_server_port == MACH_PORT_NULL) kstatus = bootstrap_look_up(bootstrap_port, ASL_SERVICE_NAME, &asl_server_port);
423
424 if (kstatus == KERN_SUCCESS) kstatus = _asl_server_message(asl_server_port, (caddr_t)out, outlen + 1);
425 else vm_deallocate(mach_task_self(), (vm_address_t)out, outlen + 1);
426
427 if (kstatus == KERN_SUCCESS)
428 {
429 free(str);
430 asl_free(msg);
431 return;
432 }
433 }
434
435 free(str);
436 }
437
438 /*
439 * Output the message to the console.
440 */
441 if (_sl_LogStat & LOG_CONS && (fd = open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0)
442 {
443 count = 0;
444
445 p = asl_format_message(msg, ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_ENCODE_SAFE, &count);
446 if (p != NULL)
447 {
448 struct iovec iov;
449
450 /* count includes trailing nul */
451 iov.iov_len = count - 1;
452 iov.iov_base = p;
453 writev(fd, &iov, 1);
454
455 free(p);
456 }
457
458 close(fd);
459 }
460
461 asl_free(msg);
462 }
463
464 #ifndef BUILDING_VARIANT
465
466 __private_extern__ void
467 _syslog_fork_child()
468 {
469 _sl_RCToken = -1;
470 _sl_NotifyToken = -1;
471 _sl_NotifyMaster = -1;
472
473 asl_server_port = MACH_PORT_NULL;
474
475 _sl_pid = getpid();
476 }
477
478 __private_extern__ void
479 _sl_init_notify()
480 {
481 int status;
482 char *notify_name;
483 uint32_t euid;
484
485 if (_sl_LogStat & LOG_NO_NOTIFY)
486 {
487 _sl_RCToken = -2;
488 _sl_NotifyMaster = -2;
489 _sl_NotifyToken = -2;
490 return;
491 }
492
493 if (_sl_RCToken == -1)
494 {
495 status = notify_register_check(NOTIFY_RC, &_sl_RCToken);
496 if (status != NOTIFY_STATUS_OK) _sl_RCToken = -2;
497 }
498
499 if (_sl_NotifyMaster == -1)
500 {
501 status = notify_register_plain(NOTIFY_SYSTEM_MASTER, &_sl_NotifyMaster);
502 if (status != NOTIFY_STATUS_OK) _sl_NotifyMaster = -2;
503 }
504
505 if (_sl_NotifyToken == -1)
506 {
507 _sl_NotifyToken = -2;
508
509 euid = geteuid();
510 notify_name = NULL;
511 if (euid == 0) asprintf(&notify_name, "%s.%d", NOTIFY_PREFIX_SYSTEM, getpid());
512 else asprintf(&notify_name, "user.uid.%d.syslog.%d", euid, getpid());
513
514 if (notify_name != NULL)
515 {
516 status = notify_register_plain(notify_name, &_sl_NotifyToken);
517 free(notify_name);
518 if (status != NOTIFY_STATUS_OK) _sl_NotifyToken = -2;
519 }
520 }
521 }
522
523 void
524 openlog(const char *ident, int logstat, int logfac)
525 {
526 kern_return_t kstatus;
527
528 if (ident != NULL) _sl_LogTag = ident;
529
530 _sl_LogStat = logstat;
531
532 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) _sl_LogFacility = logfac;
533
534 if (asl_server_port == MACH_PORT_NULL)
535 {
536 kstatus = bootstrap_look_up(bootstrap_port, ASL_SERVICE_NAME, &asl_server_port);
537 }
538
539 _sl_pid = getpid();
540 _sl_init_notify();
541 }
542
543 void
544 closelog()
545 {
546 if (asl_server_port != MACH_PORT_NULL) mach_port_deallocate(mach_task_self(), asl_server_port);
547 asl_server_port = MACH_PORT_NULL;
548
549 if (_sl_NotifyToken != -1) notify_cancel(_sl_NotifyToken);
550 _sl_NotifyToken = -1;
551
552 if (_sl_NotifyMaster != -1) notify_cancel(_sl_NotifyMaster);
553 _sl_NotifyMaster = -1;
554 }
555
556 /* setlogmask -- set the log mask level */
557 int
558 setlogmask(int pmask)
559 {
560 int omask;
561
562 omask = _sl_LogMask;
563 if (pmask != 0) _sl_LogMask = pmask;
564 return (omask);
565 }
566
567 #endif /* !BUILDING_VARIANT */