]> git.saurik.com Git - apple/libc.git/blame - gen/syslog.c
Libc-594.1.4.tar.gz
[apple/libc.git] / gen / syslog.c
CommitLineData
e9ce8d39 1/*
34e8f829 2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
e9ce8d39
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
34e8f829
A
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.
734aad71
A
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
e9ce8d39
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
34e8f829
A
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."
e9ce8d39
A
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>
224c7076 58#include <sys/param.h>
e9ce8d39
A
59#include <sys/socket.h>
60#include <sys/syslog.h>
61#include <sys/uio.h>
62#include <sys/un.h>
63#include <netdb.h>
34e8f829
A
64#include <mach/mach.h>
65#include <servers/bootstrap.h>
e9ce8d39
A
66#include <errno.h>
67#include <fcntl.h>
68#include <paths.h>
69#include <stdio.h>
3d9156a7 70#include <stdlib.h>
224c7076 71#include <stdint.h>
e9ce8d39
A
72#include <string.h>
73#include <time.h>
b5d655f7 74#include <sys/time.h>
e9ce8d39 75#include <unistd.h>
3d9156a7 76#include <notify.h>
224c7076
A
77#include <asl.h>
78#include <asl_private.h>
34e8f829 79#include <asl_ipc.h>
e9ce8d39
A
80
81#ifdef __STDC__
82#include <stdarg.h>
83#else
84#include <varargs.h>
85#endif
86
87#include <crt_externs.h>
3d9156a7
A
88
89#define LOG_NO_NOTIFY 0x1000
224c7076 90#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
3d9156a7
A
91
92#ifdef BUILDING_VARIANT
3d9156a7
A
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 */
34e8f829
A
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 */
3d9156a7
A
100__private_extern__ int _sl_NotifyToken; /* for remote control of priority filter */
101__private_extern__ int _sl_NotifyMaster; /* for remote control of priority filter */
34e8f829 102__private_extern__ int _sl_pid; /* pid */
3d9156a7 103#else /* !BUILDING_VARIANT */
34e8f829 104__private_extern__ int _sl_LogStat = 0; /* status bits, set by openlog() */
3d9156a7
A
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 */
34e8f829
A
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 */
3d9156a7
A
114#endif /* BUILDING_VARIANT */
115
116__private_extern__ void _sl_init_notify();
117
34e8f829
A
118#define ASL_SERVICE_NAME "com.apple.system.logger"
119static mach_port_t asl_server_port = MACH_PORT_NULL;
120
3d9156a7
A
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 */
3d9156a7 127uint32_t notify_register_plain(const char *name, int *out_token);
224c7076 128const char *asl_syslog_faciliy_num_to_name(int);
e9ce8d39
A
129
130/*
131 * syslog, vsyslog --
132 * print message on log file; output is intended for syslogd(8).
133 */
134void
135#ifdef __STDC__
136syslog(int pri, const char *fmt, ...)
137#else
138syslog(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
155void
224c7076 156vsyslog(int pri, const char *fmt, va_list ap)
e9ce8d39 157{
34e8f829 158 int status, i, saved_errno, filter, check, rc_filter;
224c7076 159 time_t tick;
b5d655f7 160 struct timeval tval;
34e8f829 161 uint32_t elen, count, outlen;
224c7076 162 char *p, *str, *expanded, *err_str, hname[MAXHOSTNAMELEN+1];
34e8f829 163 const char *val;
224c7076
A
164 uint64_t cval;
165 int fd, mask, level, facility;
166 aslmsg msg;
34e8f829
A
167 kern_return_t kstatus;
168 caddr_t out;
224c7076
A
169
170 saved_errno = errno;
171
34e8f829
A
172 if (_sl_pid == -1) _sl_pid = getpid();
173
e9ce8d39 174 /* Check for invalid bits. */
224c7076 175 if (pri & ~(LOG_PRIMASK | LOG_FACMASK))
3d9156a7
A
176 {
177 syslog(INTERNALLOG, "syslog: unknown facility/priority: %x", pri);
224c7076 178 pri &= (LOG_PRIMASK | LOG_FACMASK);
e9ce8d39
A
179 }
180
224c7076
A
181 level = LOG_PRI(pri);
182 facility = pri & LOG_FACMASK;
183
184 if (facility == 0) facility = _sl_LogFacility;
185
3d9156a7
A
186 _sl_init_notify();
187
34e8f829
A
188 /* initialize or re-check process-specific and master filters */
189 if (_sl_RCToken >= 0)
3d9156a7 190 {
34e8f829
A
191 check = 0;
192 status = notify_check(_sl_RCToken, &check);
193 if ((status == NOTIFY_STATUS_OK) && (check != 0))
3d9156a7 194 {
34e8f829 195 if (_sl_NotifyMaster >= 0)
3d9156a7 196 {
34e8f829
A
197 cval = 0;
198 if (notify_get_state(_sl_NotifyMaster, &cval) == NOTIFY_STATUS_OK) _sl_MasterLogMask = cval;
3d9156a7 199 }
3d9156a7 200
34e8f829 201 if (_sl_NotifyToken >= 0)
3d9156a7 202 {
34e8f829
A
203 cval = 0;
204 if (notify_get_state(_sl_NotifyToken, &cval) == NOTIFY_STATUS_OK) _sl_ProcLogMask = cval;
3d9156a7
A
205 }
206 }
207 }
208
34e8f829
A
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
224c7076
A
226 mask = LOG_MASK(level);
227 if ((mask & filter) == 0) return;
e9ce8d39
A
228
229 /* Build the message. */
224c7076 230 msg = asl_new(ASL_TYPE_MSG);
e9ce8d39 231
224c7076
A
232 if (_sl_LogTag == NULL) _sl_LogTag = *(*_NSGetArgv());
233 if (_sl_LogTag != NULL)
234 {
235 asl_set(msg, ASL_KEY_SENDER, _sl_LogTag);
236 }
e9ce8d39 237
224c7076
A
238 str = (char *)asl_syslog_faciliy_num_to_name(facility);
239 if (str != NULL) asl_set(msg, ASL_KEY_FACILITY, str);
e9ce8d39 240
224c7076 241 str = NULL;
b5d655f7
A
242 memset(&tval, 0, sizeof(struct timeval));
243
244 status = gettimeofday(&tval, NULL);
245 if (status == 0)
224c7076 246 {
b5d655f7
A
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 }
e9ce8d39 254
b5d655f7
A
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 }
34e8f829 274
b5d655f7 275 str = NULL;
34e8f829 276 asprintf(&str, "%u", _sl_pid);
224c7076
A
277 if (str != NULL)
278 {
279 asl_set(msg, ASL_KEY_PID, str);
280 free(str);
281 }
3d9156a7 282
224c7076
A
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 }
3d9156a7 290
224c7076
A
291 str = NULL;
292 asprintf(&str, "%u", getgid());
293 if (str != NULL)
3d9156a7 294 {
224c7076
A
295 asl_set(msg, ASL_KEY_GID, str);
296 free(str);
e9ce8d39 297 }
3d9156a7 298
224c7076
A
299 str = NULL;
300 asprintf(&str, "%u", level);
301 if (str != NULL)
3d9156a7 302 {
224c7076
A
303 asl_set(msg, ASL_KEY_LEVEL, str);
304 free(str);
e9ce8d39 305 }
3d9156a7 306
224c7076
A
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++)
3d9156a7 314 {
224c7076 315 if ((fmt[i] == '%') && (fmt[i+1] == 'm')) count++;
e9ce8d39
A
316 }
317
224c7076
A
318 expanded = NULL;
319 elen = 0;
320 err_str = NULL;
321
322 /* deal with malloc failures gracefully */
323 if (count > 0)
3d9156a7 324 {
224c7076
A
325 err_str = strdup(strerror(saved_errno));
326 if (err_str == NULL) count = 0;
327 else
3d9156a7 328 {
224c7076
A
329 elen = strlen(err_str);
330 expanded = malloc(i + (count * elen));
331 if (expanded == NULL) count = 0;
3d9156a7 332 }
224c7076
A
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++)
3d9156a7 341 {
224c7076 342 if ((fmt[i] == '%') && (fmt[i+1] == 'm'))
3d9156a7 343 {
224c7076
A
344 memcpy(p, err_str, elen);
345 p += elen;
346 i++;
347 }
348 else
349 {
350 *p++ = fmt[i];
e9ce8d39
A
351 }
352 }
224c7076
A
353
354 *p = '\0';
e9ce8d39 355 }
3d9156a7 356
224c7076 357 if (err_str != NULL) free(err_str);
e9ce8d39 358
224c7076
A
359 vasprintf(&str, expanded, ap);
360 if (count > 0) free(expanded);
e9ce8d39 361
224c7076 362 if (str != NULL)
3d9156a7 363 {
224c7076
A
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;
34e8f829 370 if (_sl_LogStat & LOG_PID) asprintf(&p, "%s[%u]: %s", (_sl_LogTag == NULL) ? "???" : _sl_LogTag, _sl_pid, str);
224c7076
A
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 }
e9ce8d39 385
224c7076 386 free(str);
e9ce8d39
A
387 }
388
34e8f829
A
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 */
b5d655f7 411 str = asl_format_message(msg, ASL_MSG_FMT_RAW, ASL_TIME_FMT_SEC, ASL_ENCODE_ASL, &count);
224c7076
A
412 if (str != NULL)
413 {
34e8f829
A
414 outlen = count + 11;
415 kstatus = vm_allocate(mach_task_self(), (vm_address_t *)&out, outlen + 1, TRUE);
416 if (kstatus == KERN_SUCCESS)
224c7076 417 {
34e8f829
A
418 memset(out, 0, outlen + 1);
419 snprintf((char *)out, outlen, "%10u %s", count, str);
224c7076 420
34e8f829
A
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);
224c7076 426
34e8f829 427 if (kstatus == KERN_SUCCESS)
224c7076 428 {
34e8f829 429 free(str);
224c7076
A
430 asl_free(msg);
431 return;
432 }
224c7076 433 }
34e8f829
A
434
435 free(str);
224c7076 436 }
e9ce8d39
A
437
438 /*
224c7076 439 * Output the message to the console.
e9ce8d39 440 */
224c7076 441 if (_sl_LogStat & LOG_CONS && (fd = open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0)
3d9156a7 442 {
224c7076
A
443 count = 0;
444
b5d655f7 445 p = asl_format_message(msg, ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_ENCODE_SAFE, &count);
224c7076
A
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);
34e8f829 454
224c7076
A
455 free(p);
456 }
457
458 close(fd);
e9ce8d39 459 }
224c7076
A
460
461 asl_free(msg);
e9ce8d39
A
462}
463
3d9156a7
A
464#ifndef BUILDING_VARIANT
465
34e8f829
A
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}
e9ce8d39 477
3d9156a7
A
478__private_extern__ void
479_sl_init_notify()
480{
481 int status;
482 char *notify_name;
34e8f829 483 uint32_t euid;
224c7076 484
3d9156a7
A
485 if (_sl_LogStat & LOG_NO_NOTIFY)
486 {
34e8f829 487 _sl_RCToken = -2;
3d9156a7
A
488 _sl_NotifyMaster = -2;
489 _sl_NotifyToken = -2;
490 return;
491 }
224c7076 492
34e8f829
A
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
3d9156a7
A
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 }
224c7076 504
3d9156a7
A
505 if (_sl_NotifyToken == -1)
506 {
507 _sl_NotifyToken = -2;
224c7076 508
34e8f829 509 euid = geteuid();
3d9156a7 510 notify_name = NULL;
34e8f829
A
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());
224c7076 513
3d9156a7
A
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
e9ce8d39 523void
34e8f829 524openlog(const char *ident, int logstat, int logfac)
e9ce8d39 525{
34e8f829
A
526 kern_return_t kstatus;
527
3d9156a7
A
528 if (ident != NULL) _sl_LogTag = ident;
529
530 _sl_LogStat = logstat;
531
532 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) _sl_LogFacility = logfac;
e9ce8d39 533
34e8f829 534 if (asl_server_port == MACH_PORT_NULL)
3d9156a7 535 {
34e8f829 536 kstatus = bootstrap_look_up(bootstrap_port, ASL_SERVICE_NAME, &asl_server_port);
3d9156a7
A
537 }
538
34e8f829 539 _sl_pid = getpid();
3d9156a7 540 _sl_init_notify();
e9ce8d39
A
541}
542
543void
544closelog()
545{
34e8f829
A
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;
e9ce8d39
A
554}
555
556/* setlogmask -- set the log mask level */
557int
34e8f829 558setlogmask(int pmask)
e9ce8d39
A
559{
560 int omask;
561
3d9156a7
A
562 omask = _sl_LogMask;
563 if (pmask != 0) _sl_LogMask = pmask;
e9ce8d39
A
564 return (omask);
565}
3d9156a7
A
566
567#endif /* !BUILDING_VARIANT */