2 * Copyright (c) 2004-2015 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
37 #include <sys/fcntl.h>
38 #include <sys/param.h>
39 #include <sys/fileport.h>
40 #include <crt_externs.h>
44 #include <mach/mach.h>
45 #include <mach/std_types.h>
47 #include <mach/mach_types.h>
48 #include <sys/types.h>
49 #include <servers/bootstrap.h>
50 #include <bootstrap_priv.h>
52 #include <dispatch/dispatch.h>
53 #include <libkern/OSAtomic.h>
54 #include <os/activity.h>
56 #include <os/log_private.h>
58 #include <asl_client.h>
61 #include <asl_msg_list.h>
62 #include <asl_store.h>
63 #include <asl_private.h>
65 #define forever for(;;)
67 #define FETCH_BATCH 256
69 #define EVAL_DEFAULT_ACTION (EVAL_SEND_ASL | EVAL_SEND_TRACE)
70 #define EVAL_ASL (EVAL_SEND_ASL | EVAL_TEXT_FILE | EVAL_ASL_FILE)
73 * Clients get a max of 36000 messages per hour.
74 * Their quota gets refilled at a rate of 10 messages per second.
76 #define QUOTA_MPH 36000
78 #define QUOTA_MSG_INTERVAL 60
79 #define NOQUOTA_ENV "ASL_QUOTA_DISABLED"
80 #define QUOTA_DISABLED_MSG "*** MESSAGE QUOTA DISABLED FOR THIS PROCESS ***"
81 #define QUOTA_MSG "*** LOG MESSAGE QUOTA EXCEEDED - SOME MESSAGES FROM THIS PROCESS HAVE BEEN DISCARDED ***"
82 #define QUOTA_LEVEL ASL_LEVEL_CRIT
83 #define QUOTA_LEVEL_STR ASL_STRING_CRIT
86 * Limit the size of messages sent to syslogd.
88 #define SIZE_LIMIT_MSG "*** ASL MESSAGE SIZE (%u bytes) EXCEEDED MAXIMIMUM SIZE (%u bytes) ***"
90 static const os_log_type_t shim_asl_to_log_type
[8] = {
91 OS_LOG_TYPE_DEFAULT
, // ASL_LEVEL_EMERG
92 OS_LOG_TYPE_DEFAULT
, // ASL_LEVEL_ALERT
93 OS_LOG_TYPE_DEFAULT
, // ASL_LEVEL_CRIT
94 OS_LOG_TYPE_DEFAULT
, // ASL_LEVEL_ERR
95 OS_LOG_TYPE_DEFAULT
, // ASL_LEVEL_WARNING
96 OS_LOG_TYPE_DEFAULT
, // ASL_LEVEL_NOTICE
97 OS_LOG_TYPE_INFO
, // ASL_LEVEL_INFO
98 OS_LOG_TYPE_DEBUG
// ASL_LEVEL_DEBUG
102 static ASL_STATUS
_asl_send_message(asl_object_t obj
, uint32_t eval
, asl_msg_t
*msg
, const char *mstring
);
103 static ASL_STATUS
_asl_send_message_text(asl_client_t
*asl
, asl_msg_t
*sendmsg
, asl_object_t obj
, uint32_t eval
, asl_msg_t
*msg
, const char *mstring
, bool shimmed
);
104 __private_extern__ asl_client_t
*_asl_open_default();
107 uint32_t notify_register_plain(const char *name
, int *out_token
);
109 /* fork handling in asl_fd.c */
110 extern void _asl_redirect_fork_child(void);
116 dispatch_semaphore_t sem
;
125 uint64_t proc_filter
;
126 uint64_t master_filter
;
130 dispatch_once_t port_lookup_once
;
131 mach_port_t server_port
;
133 pthread_mutex_t lock
;
135 asl_aux_context_t
**aux_ctx
;
139 __private_extern__ _asl_global_t _asl_global
= {0, -1, -1, -1, 0LL, 0LL, 0LL, 0LL, 0, 0, MACH_PORT_NULL
, NULL
, PTHREAD_MUTEX_INITIALIZER
, 0, NULL
, NULL
};
141 static const char *level_to_number_string
[] = {"0", "1", "2", "3", "4", "5", "6", "7"};
143 #define ASL_SERVICE_NAME "com.apple.system.logger"
146 * Called from the child process inside fork() to clean up
147 * inherited state from the parent process.
149 * NB. A lock isn't required, since we're single threaded in this call.
154 _asl_global
.notify_count
= 0;
155 _asl_global
.rc_change_token
= -1;
156 _asl_global
.master_token
= -1;
157 _asl_global
.notify_token
= -1;
158 _asl_global
.quota
= 0;
159 _asl_global
.last_send
= 0;
160 _asl_global
.last_oq_msg
= 0;
162 _asl_global
.port_lookup_once
= 0;
163 _asl_global
.server_port
= MACH_PORT_NULL
;
165 pthread_mutex_init(&(_asl_global
.lock
), NULL
);
167 _asl_redirect_fork_child();
171 * asl_remote_notify_name: returns the notification key for remote-control filter
172 * changes for this process.
175 asl_remote_notify_name()
177 pid_t pid
= getpid();
178 uid_t euid
= geteuid();
181 if (euid
== 0) asprintf(&str
, "%s.%d", NOTIFY_PREFIX_SYSTEM
, pid
);
182 else asprintf(&str
, "user.uid.%d.syslog.%d", euid
, pid
);
188 _asl_notify_open(int do_lock
)
193 if (do_lock
!= 0) pthread_mutex_lock(&_asl_global
.lock
);
195 _asl_global
.notify_count
++;
197 if (_asl_global
.notify_token
!= -1)
199 if (do_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
200 return ASL_STATUS_OK
;
203 if (_asl_global
.rc_change_token
== -1)
205 status
= notify_register_check(NOTIFY_RC
, &_asl_global
.rc_change_token
);
206 if (status
!= NOTIFY_STATUS_OK
) _asl_global
.rc_change_token
= -1;
209 if (_asl_global
.master_token
== -1)
211 status
= notify_register_plain(NOTIFY_SYSTEM_MASTER
, &_asl_global
.master_token
);
212 if (status
!= NOTIFY_STATUS_OK
) _asl_global
.master_token
= -1;
215 notify_name
= asl_remote_notify_name();
216 if (notify_name
!= NULL
)
218 status
= notify_register_plain(notify_name
, &_asl_global
.notify_token
);
220 if (status
!= NOTIFY_STATUS_OK
) _asl_global
.notify_token
= -1;
223 if (do_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
225 if (_asl_global
.notify_token
== -1) return ASL_STATUS_FAILED
;
226 return ASL_STATUS_OK
;
233 pthread_mutex_lock(&_asl_global
.lock
);
235 if (_asl_global
.notify_count
> 0) _asl_global
.notify_count
--;
237 if (_asl_global
.notify_count
> 0)
239 pthread_mutex_unlock(&_asl_global
.lock
);
243 if (_asl_global
.rc_change_token
>= 0) notify_cancel(_asl_global
.rc_change_token
);
244 _asl_global
.rc_change_token
= -1;
246 if (_asl_global
.master_token
>= 0) notify_cancel(_asl_global
.master_token
);
247 _asl_global
.master_token
= -1;
249 if (_asl_global
.notify_token
>= 0) notify_cancel(_asl_global
.notify_token
);
250 _asl_global
.notify_token
= -1;
252 pthread_mutex_unlock(&_asl_global
.lock
);
259 dispatch_once(&_asl_global
.port_lookup_once
, ^{
260 char *str
= getenv("ASL_DISABLE");
261 if ((str
== NULL
) || strcmp(str
, "1"))
263 bootstrap_look_up2(bootstrap_port
, ASL_SERVICE_NAME
, &_asl_global
.server_port
, 0, BOOTSTRAP_PRIVILEGED_SERVER
);
269 asl_core_get_service_port(__unused
int reset
)
272 return _asl_global
.server_port
;
276 #pragma mark asl_client
279 asl_open(const char *ident
, const char *facility
, uint32_t opts
)
281 asl_client_t
*asl
= asl_client_open(ident
, facility
, opts
);
282 if (asl
== NULL
) return NULL
;
285 if (!(opts
& ASL_OPT_NO_REMOTE
)) _asl_notify_open(1);
287 return (asl_object_t
)asl
;
291 asl_open_from_file(int fd
, const char *ident
, const char *facility
)
293 return (asl_object_t
)asl_client_open_from_file(fd
, ident
, facility
);
297 asl_close(asl_object_t obj
)
302 __private_extern__ asl_client_t
*
305 static dispatch_once_t once
;
307 dispatch_once(&once
, ^{
309 * Do a sleight-of-hand with ASL_OPT_NO_REMOTE to avoid a deadlock
310 * since asl_open(xxx, yyy, 0) calls _asl_notify_open(1)
311 * which locks _asl_global.lock.
313 _asl_global
.asl
= (asl_client_t
*)asl_open(NULL
, NULL
, ASL_OPT_NO_REMOTE
);
315 /* Reset options to clear ASL_OPT_NO_REMOTE bit */
316 if (_asl_global
.asl
!= NULL
) _asl_global
.asl
->options
= 0;
318 /* Now call _asl_notify_open(0) to finish the work */
322 return _asl_global
.asl
;
326 * asl_add_file: write log messages to the given file descriptor
327 * Log messages will be written to this file as well as to the server.
330 asl_add_output_file(asl_object_t client
, int fd
, const char *mfmt
, const char *tfmt
, int filter
, int text_encoding
)
332 int status
, use_global_lock
= 0;
335 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
337 asl
= (asl_client_t
*)client
;
340 asl
= _asl_open_default();
341 if (asl
== NULL
) return -1;
342 pthread_mutex_lock(&_asl_global
.lock
);
346 status
= asl_client_add_output_file(asl
, fd
, mfmt
, tfmt
, filter
, text_encoding
);
348 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
349 return (status
== ASL_STATUS_OK
) ? 0 : -1;
352 /* returns previous filter value or -1 on error */
354 asl_set_output_file_filter(asl_object_t client
, int fd
, int filter
)
357 int use_global_lock
= 0;
360 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
362 asl
= (asl_client_t
*)client
;
365 asl
= _asl_open_default();
366 if (asl
== NULL
) return -1;
367 pthread_mutex_lock(&_asl_global
.lock
);
371 last
= asl_client_set_output_file_filter(asl
, fd
, filter
);
373 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
377 /* SPI - Deprecated */
379 asl_add_output(asl_object_t client
, int fd
, const char *mfmt
, const char *tfmt
, uint32_t text_encoding
)
381 return asl_add_output_file(client
, fd
, mfmt
, tfmt
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
), text_encoding
);
384 /* SPI - Deprecated */
386 asl_add_log_file(asl_object_t client
, int fd
)
388 return asl_add_output_file(client
, fd
, ASL_MSG_FMT_STD
, ASL_TIME_FMT_LCL
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
), ASL_ENCODE_SAFE
);
392 * asl_remove_output: stop writing log messages to the given file descriptor
395 asl_remove_output_file(asl_object_t client
, int fd
)
397 int status
, use_global_lock
= 0;
400 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
402 asl
= (asl_client_t
*)client
;
405 asl
= _asl_open_default();
406 if (asl
== NULL
) return -1;
407 pthread_mutex_lock(&_asl_global
.lock
);
411 status
= asl_client_remove_output_file(asl
, fd
);
413 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
414 return (status
== ASL_STATUS_OK
) ? 0 : -1;
418 asl_remove_output(asl_object_t client
, int fd
)
420 return asl_remove_output_file(client
, fd
);
424 asl_remove_log_file(asl_object_t client
, int fd
)
426 return asl_remove_output_file(client
, fd
);
429 /* returns previous filter value or -1 on error */
431 asl_set_filter(asl_object_t client
, int f
)
433 int last
, use_global_lock
= 0;
436 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
438 asl
= (asl_client_t
*)client
;
441 asl
= _asl_open_default();
442 if (asl
== NULL
) return -1;
443 pthread_mutex_lock(&_asl_global
.lock
);
447 last
= asl_client_set_filter(asl
, f
);
449 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
455 #pragma mark message sending
458 * Evaluate client / message / level to determine what to do with a message.
459 * Checks filters, tunneling, and log files.
460 * Returns the bits below, ORed with the message level.
462 * EVAL_SEND_ASL - send this message to syslogd
463 * EVAL_SEND_TRACE - log this message with Activity Tracing
464 * EVAL_ASL_FILE - write to a standalone ASL file (see asl_open_from_file)
465 * EVAL_TEXT_FILE - write this message to a text file / stderr
466 * EVAL_TUNNEL - tunneling enabled when sending to syslogd
469 _asl_evaluate_send(asl_object_t client
, asl_object_t m
, int slevel
)
472 asl_msg_t
*msg
= (asl_msg_t
*)m
;
473 uint32_t level
, lmask
, filter
, status
, tunnel
;
479 level
= ASL_LEVEL_DEBUG
;
480 if (slevel
>= 0) level
= slevel
;
483 if ((asl_msg_lookup(msg
, ASL_KEY_LEVEL
, &val
, NULL
) == 0) && (val
!= NULL
)) level
= atoi(val
);
485 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
486 else if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
488 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
))
490 /* sending to something other than a client */
491 return EVAL_ACTIVE
| EVAL_SEND_ASL
| level
;
494 asl
= (asl_client_t
*)client
;
495 if ((asl
!= NULL
) && (asl
->aslfile
!= NULL
)) return (EVAL_ASL_FILE
| level
);
497 if (asl
== NULL
) asl
= _asl_open_default();
500 eval
= EVAL_ACTIVE
| EVAL_DEFAULT_ACTION
| level
;
501 eval
&= ~EVAL_SEND_ASL
;
505 eval
= (asl_client_get_control(asl
) & EVAL_ACTION_MASK
) | level
;
507 filter
= asl
->filter
& 0xff;
508 tunnel
= (asl
->filter
& ASL_FILTER_MASK_TUNNEL
) >> 8;
509 if (tunnel
!= 0) eval
|= EVAL_TUNNEL
;
511 if ((asl
->options
& ASL_OPT_NO_REMOTE
) == 0)
513 pthread_mutex_lock(&_asl_global
.lock
);
515 if (_asl_global
.rc_change_token
>= 0)
517 /* initialize or re-check process-specific and master filters */
519 status
= notify_check(_asl_global
.rc_change_token
, &check
);
520 if ((status
== NOTIFY_STATUS_OK
) && (check
!= 0))
522 if (_asl_global
.master_token
>= 0)
525 status
= notify_get_state(_asl_global
.master_token
, &v64
);
526 if (status
== NOTIFY_STATUS_OK
) _asl_global
.master_filter
= v64
;
529 if (_asl_global
.notify_token
>= 0)
532 status
= notify_get_state(_asl_global
.notify_token
, &v64
);
533 if (status
== NOTIFY_STATUS_OK
) _asl_global
.proc_filter
= v64
;
538 if (_asl_global
.master_filter
& EVAL_ACTIVE
)
540 /* clear bits and set according to master */
541 eval
&= ~(EVAL_SEND_ASL
| EVAL_SEND_TRACE
);
542 eval
|= (_asl_global
.master_filter
& (EVAL_SEND_ASL
| EVAL_SEND_TRACE
));
545 if ((_asl_global
.master_filter
& EVAL_LEVEL_MASK
) != 0) filter
= _asl_global
.proc_filter
& EVAL_LEVEL_MASK
;
548 if (_asl_global
.proc_filter
& EVAL_ACTIVE
)
550 /* clear bits and set according to proc */
551 eval
&= ~(EVAL_SEND_ASL
| EVAL_SEND_TRACE
| EVAL_TEXT_FILE
);
552 eval
|= (_asl_global
.proc_filter
& (EVAL_SEND_ASL
| EVAL_SEND_TRACE
| EVAL_TEXT_FILE
));
555 if ((_asl_global
.proc_filter
& EVAL_LEVEL_MASK
) != 0) filter
= _asl_global
.proc_filter
& EVAL_LEVEL_MASK
;
558 pthread_mutex_unlock(&_asl_global
.lock
);
561 lmask
= ASL_FILTER_MASK(level
);
562 if ((filter
!= 0) && ((filter
& lmask
) == 0)) eval
&= ~EVAL_SEND_ASL
;
563 if (asl
->out_count
> 0) eval
|= EVAL_TEXT_FILE
;
565 /* don't send MessageTracer messages to Activity Tracing */
567 if ((asl_msg_lookup(msg
, ASL_KEY_MESSAGETRACER
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
569 /* don't send PowerManagement messages to Activity Tracing */
571 if ((asl_msg_lookup(msg
, ASL_KEY_POWERMANAGEMENT
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
573 /* don't send control messages to Activity Tracing */
575 if ((asl_msg_lookup(msg
, ASL_KEY_OPTION
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
577 /* don't send CFLog messages to Activity Tracing */
579 if ((asl_msg_lookup(msg
, ASL_KEY_CFLOG_LOCAL_TIME
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
582 if (((asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &val
, NULL
) == 0) && (val
!= NULL
)) ||
583 ((asl_msg_lookup(asl
->kvdict
, ASL_KEY_FACILITY
, &val
, NULL
) == 0) && (val
!= NULL
)))
585 /* don't send lastlog/utmp messages to Activity Tracing */
586 if (!strcmp(val
, FACILITY_LASTLOG
) || !strcmp(val
, FACILITY_UTMPX
)) eval
&= ~EVAL_SEND_TRACE
;
588 /* don't send LOG_INSTALL messages to Activity Tracing */
589 if (!strcmp(val
, asl_syslog_faciliy_num_to_name(LOG_INSTALL
))) eval
&= ~EVAL_SEND_TRACE
;
597 * Internal routine used by asl_vlog.
598 * msg: an asl messsage
599 * eval: log level and send flags for the message
600 * format: A formating string
601 * ap: va_list for the format
602 * returns 0 for success, non-zero for failure
605 __private_extern__ ASL_STATUS
606 _asl_lib_vlog_text(asl_object_t obj
, uint32_t eval
, asl_object_t msg
, const char *format
, va_list ap
)
608 int saved_errno
= errno
;
610 char *str
, *fmt
, estr
[NL_TEXTMAX
];
611 uint32_t i
, len
, elen
, expand
;
613 if (format
== NULL
) return ASL_STATUS_INVALID_ARG
;
615 /* insert strerror for %m */
620 for (i
= 0; format
[i
] != '\0'; i
++)
622 if (format
[i
] == '%')
624 if (format
[i
+1] == '\0') len
++;
625 else if (format
[i
+1] == 'm')
628 strerror_r(saved_errno
, estr
, sizeof(estr
));
642 fmt
= (char *)format
;
646 fmt
= malloc(len
+ 1);
647 if (fmt
== NULL
) return ASL_STATUS_NO_MEMORY
;
651 for (i
= 0; format
[i
] != '\0'; i
++)
653 if (format
[i
] == '%')
655 if (format
[i
+1] == '\0')
658 else if ((format
[i
+1] == 'm') && (elen
!= 0))
660 memcpy(fmt
+len
, estr
, elen
);
666 fmt
[len
++] = format
[i
++];
667 fmt
[len
++] = format
[i
];
670 else fmt
[len
++] = format
[i
];
677 vasprintf(&str
, fmt
, ap
);
678 if (expand
!= 0) free(fmt
);
680 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
682 status
= _asl_send_message_text(NULL
, NULL
, obj
, eval
, (asl_msg_t
*)msg
, str
, true);
690 * Internal routine used by asl_vlog.
691 * msg: an asl messsage
692 * eval: log level and send flags for the message
693 * format: A formating string
694 * ap: va_list for the format
695 * returns 0 for success, non-zero for failure
697 __private_extern__ ASL_STATUS
698 _asl_lib_vlog(asl_object_t obj
, uint32_t eval
, asl_object_t msg
, const char *format
, va_list ap
)
700 int saved_errno
= errno
;
702 char *str
, *fmt
, estr
[NL_TEXTMAX
];
703 uint32_t i
, len
, elen
, expand
;
705 if (format
== NULL
) return ASL_STATUS_INVALID_ARG
;
707 /* insert strerror for %m */
712 for (i
= 0; format
[i
] != '\0'; i
++)
714 if (format
[i
] == '%')
716 if (format
[i
+1] == '\0') len
++;
717 else if (format
[i
+1] == 'm')
720 strerror_r(saved_errno
, estr
, sizeof(estr
));
734 fmt
= (char *)format
;
738 fmt
= malloc(len
+ 1);
739 if (fmt
== NULL
) return ASL_STATUS_NO_MEMORY
;
743 for (i
= 0; format
[i
] != '\0'; i
++)
745 if (format
[i
] == '%')
747 if (format
[i
+1] == '\0')
750 else if ((format
[i
+1] == 'm') && (elen
!= 0))
752 memcpy(fmt
+len
, estr
, elen
);
758 fmt
[len
++] = format
[i
++];
759 fmt
[len
++] = format
[i
];
762 else fmt
[len
++] = format
[i
];
769 vasprintf(&str
, fmt
, ap
);
770 if (expand
!= 0) free(fmt
);
772 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
774 status
= _asl_send_message(obj
, eval
, (asl_msg_t
*)msg
, str
);
782 * Similar to asl_log, but take a va_list instead of a list of arguments.
783 * msg: an asl message
784 * level: the log level of the associated message
785 * format: A formating string
786 * ap: va_list for the format
787 * returns 0 for success, non-zero for failure
790 asl_vlog(asl_object_t client
, asl_object_t msg
, int level
, const char *format
, va_list ap
)
792 ASL_STATUS status
= ASL_STATUS_OK
;
793 uint32_t eval
= _asl_evaluate_send(client
, msg
, level
);
794 void *addr
= __builtin_return_address(0);
796 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
799 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
800 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
801 os_log_type_t type
= shim_asl_to_log_type
[level
];
803 va_copy(ap_copy
, ap
);
804 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap_copy
, addr
);
807 if (eval
& EVAL_TEXT_FILE
)
809 status
= _asl_lib_vlog_text(client
, eval
, msg
, format
, ap
);
812 else if (eval
& EVAL_ASL
)
814 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
817 return (status
== ASL_STATUS_OK
) ? 0 : -1;
822 * SPI used by legacy (non-shim) ASL_PREFILTER_LOG. Converts format arguments to a va_list and
823 * forwards the call to _asl_lib_vlog.
824 * msg: an asl message
825 * eval: log level and send flags for the message
826 * format: A formating string
827 * ... args for format
828 * returns 0 for success, non-zero for failure
831 _asl_lib_log(asl_object_t client
, uint32_t eval
, asl_object_t msg
, const char *format
, ...)
839 va_start(ap
, format
);
840 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
849 * Processes an ASL log message.
850 * msg: an asl message
851 * level: the log level of the associated message
852 * format: A formating string
853 * ... args for format
854 * returns 0 for success, non-zero for failure
857 asl_log(asl_object_t client
, asl_object_t msg
, int level
, const char *format
, ...)
859 ASL_STATUS status
= ASL_STATUS_OK
;
860 uint32_t eval
= _asl_evaluate_send(client
, msg
, level
);
861 void *addr
= __builtin_return_address(0);
863 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
866 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
867 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
868 os_log_type_t type
= shim_asl_to_log_type
[level
];
870 va_start(ap
, format
);
871 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
874 if (eval
& EVAL_TEXT_FILE
)
877 va_start(ap
, format
);
878 status
= _asl_lib_vlog_text(client
, eval
, msg
, format
, ap
);
882 else if (eval
& EVAL_ASL
)
885 va_start(ap
, format
);
886 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
890 return (status
== ASL_STATUS_OK
) ? 0 : -1;
895 * Like asl_log, supplies NULL client and msg.
896 * level: the log level of the associated message
897 * format: A formating string
898 * ... args for format
899 * returns 0 for success, non-zero for failure
902 asl_log_message(int level
, const char *format
, ...)
904 ASL_STATUS status
= ASL_STATUS_OK
;
905 uint32_t eval
= _asl_evaluate_send(NULL
, NULL
, level
);
906 void *addr
= __builtin_return_address(0);
908 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
911 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
912 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
913 os_log_type_t type
= shim_asl_to_log_type
[level
];
915 va_start(ap
, format
);
916 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
919 if (eval
& EVAL_TEXT_FILE
)
922 va_start(ap
, format
);
923 status
= _asl_lib_vlog_text(NULL
, eval
, NULL
, format
, ap
);
927 else if (eval
& EVAL_ASL
)
930 va_start(ap
, format
);
931 status
= _asl_lib_vlog(NULL
, eval
, NULL
, format
, ap
);
935 return (status
== ASL_STATUS_OK
) ? 0 : -1;
939 * asl_get_filter: gets the values for the local, master, and remote filters,
940 * and indicates which one is active.
943 asl_get_filter(asl_object_t client
, int *local
, int *master
, int *remote
, int *active
)
945 asl_client_t
*asl
, *asl_default
;
950 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
957 asl_default
= _asl_open_default();
959 asl
= (asl_client_t
*)client
;
960 if (asl
== NULL
) asl
= asl_default
;
961 if (asl
!= NULL
) l
= asl
->filter
& EVAL_LEVEL_MASK
;
963 if ((asl_default
!= NULL
) && (!(asl_default
->options
& ASL_OPT_NO_REMOTE
)))
965 pthread_mutex_lock(&_asl_global
.lock
);
967 if (_asl_global
.rc_change_token
>= 0)
969 /* initialize or re-check process-specific and master filters */
971 status
= notify_check(_asl_global
.rc_change_token
, &check
);
972 if ((status
== NOTIFY_STATUS_OK
) && (check
!= 0))
974 if (_asl_global
.master_token
>= 0)
977 status
= notify_get_state(_asl_global
.master_token
, &v64
);
978 if (status
== NOTIFY_STATUS_OK
) _asl_global
.master_filter
= v64
;
981 if (_asl_global
.notify_token
>= 0)
984 status
= notify_get_state(_asl_global
.notify_token
, &v64
);
985 if (status
== NOTIFY_STATUS_OK
) _asl_global
.proc_filter
= v64
;
990 m
= _asl_global
.master_filter
;
993 r
= _asl_global
.proc_filter
;
996 pthread_mutex_unlock(&_asl_global
.lock
);
999 if (local
!= NULL
) *local
= l
;
1000 if (master
!= NULL
) *master
= m
;
1001 if (remote
!= NULL
) *remote
= r
;
1002 if (active
!= NULL
) *active
= x
;
1007 /* SPI for SHIM control */
1009 asl_set_local_control(asl_object_t client
, uint32_t filter
)
1011 asl_client_t
*asl
= (asl_client_t
*)client
;
1014 asl
= _asl_open_default();
1015 if (asl
== NULL
) return UINT32_MAX
;
1017 else if (asl_get_type(client
) != ASL_TYPE_CLIENT
) return UINT32_MAX
;
1019 return asl_client_set_control(asl
, filter
);
1023 asl_get_local_control(asl_object_t client
)
1025 asl_client_t
*asl
= (asl_client_t
*)client
;
1028 asl
= _asl_open_default();
1029 if (asl
== NULL
) return UINT32_MAX
;
1031 else if (asl_get_type(client
) != ASL_TYPE_CLIENT
) return UINT32_MAX
;
1033 return asl_client_get_control(asl
);
1037 * Sets PID and OSActivityID values in a new message.
1038 * Also sets Level, Time, TimeNanoSec, Sender, Facility and Message if provided.
1041 asl_base_msg(asl_client_t
*asl
, uint32_t level
, const struct timeval
*tv
, const char *sstr
, const char *fstr
, const char *mstr
)
1046 os_activity_id_t osaid
;
1048 aux
= asl_msg_new(ASL_TYPE_MSG
);
1049 if (aux
== NULL
) return NULL
;
1052 if (level
<= 7) asl_msg_set_key_val(aux
, ASL_KEY_LEVEL
, level_to_number_string
[level
]);
1054 /* Time and TimeNanoSec */
1057 snprintf(aux_val
, sizeof(aux_val
), "%llu", (unsigned long long) tv
->tv_sec
);
1058 asl_msg_set_key_val(aux
, ASL_KEY_TIME
, aux_val
);
1060 snprintf(aux_val
, sizeof(aux_val
), "%d", tv
->tv_usec
* 1000);
1061 asl_msg_set_key_val(aux
, ASL_KEY_TIME_NSEC
, aux_val
);
1065 if (mstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_MSG
, mstr
);
1068 snprintf(aux_val
, sizeof(aux_val
), "%u", getpid());
1069 asl_msg_set_key_val(aux
, ASL_KEY_PID
, aux_val
);
1072 osaid
= os_activity_get_identifier(OS_ACTIVITY_CURRENT
, NULL
);
1075 snprintf(aux_val
, sizeof(aux_val
), "0x%016llx", osaid
);
1076 asl_msg_set_key_val(aux
, ASL_KEY_OS_ACTIVITY_ID
, aux_val
);
1080 if ((sstr
== NULL
) && (asl
!= NULL
))
1082 /* See if the client has a value for ASL_KEY_SENDER */
1083 status
= asl_msg_lookup((asl_msg_t
*)asl
->kvdict
, ASL_KEY_SENDER
, &sstr
, NULL
);
1084 if ((status
!= 0) || (sstr
== NULL
))
1088 /* See if the global cache has a value for ASL_KEY_SENDER */
1089 if (_asl_global
.sender
== NULL
)
1091 /* Get the process name with _NSGetArgv */
1092 char *name
= *(*_NSGetArgv());
1095 char *x
= strrchr(name
, '/');
1099 /* Set the cache value */
1100 pthread_mutex_lock(&_asl_global
.lock
);
1101 if (_asl_global
.sender
== NULL
) _asl_global
.sender
= strdup(x
);
1102 pthread_mutex_unlock(&_asl_global
.lock
);
1106 if (_asl_global
.sender
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, _asl_global
.sender
);
1107 else asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, "Unknown");
1111 if (sstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, sstr
);
1114 if ((fstr
== NULL
) && (asl
!= NULL
))
1116 status
= asl_msg_lookup((asl_msg_t
*)asl
->kvdict
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1117 if (status
!= 0) fstr
= NULL
;
1120 if (fstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_FACILITY
, fstr
);
1127 * Possibly useful someday...
1130 asl_prepared_message(asl_client_t
*asl
, asl_msg_t
*msg
)
1132 uint32_t i
, len
, level
, outstatus
;
1133 const char *val
, *sstr
, *fstr
;
1134 struct timeval tval
= {0, 0};
1140 asl
= _asl_open_default();
1141 if (asl
== NULL
) return NULL
;
1144 status
= gettimeofday(&tval
, NULL
);
1147 time_t tick
= time(NULL
);
1153 status
= asl_msg_lookup(msg
, ASL_KEY_LEVEL
, &val
, NULL
);
1154 if (status
!= 0) val
= NULL
;
1156 level
= ASL_LEVEL_DEBUG
;
1157 if (val
!= NULL
) level
= atoi(val
);
1158 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
1161 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1162 if (status
!= 0) sstr
= NULL
;
1165 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1166 if (status
!= 0) fstr
= NULL
;
1168 out
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, NULL
);
1169 out
= asl_msg_merge(out
, msg
);
1176 _asl_set_option(asl_msg_t
*msg
, const char *opt
)
1178 const char *val
= NULL
;
1181 if (msg
== NULL
) return;
1182 if (opt
== NULL
) return;
1184 status
= asl_msg_lookup(msg
, ASL_KEY_OPTION
, &val
, NULL
);
1185 if ((status
!= 0) || (val
== NULL
))
1187 asl_msg_set_key_val(msg
, ASL_KEY_OPTION
, opt
);
1191 char *option
= NULL
;
1192 asprintf(&option
, "%s %s", opt
, val
);
1193 asl_msg_set_key_val(msg
, ASL_KEY_OPTION
, option
);
1199 _asl_send_message_text(asl_client_t
*asl
, asl_msg_t
*sendmsg
, asl_object_t obj
, uint32_t eval
, asl_msg_t
*msg
, const char *mstr
, bool shimmed
)
1202 uint32_t level
, lmask
;
1203 asl_msg_t
*release_sendmsg
= NULL
;
1207 asl
= _asl_open_default();
1208 if (asl
== NULL
) return ASL_STATUS_FAILED
;
1210 uint32_t objtype
= asl_get_type(obj
);
1211 if (objtype
== ASL_TYPE_CLIENT
) asl
= (asl_client_t
*)obj
;
1215 level
= eval
& EVAL_LEVEL_MASK
;
1216 if (level
> 7) level
= 7;
1217 lmask
= ASL_FILTER_MASK(level
);
1219 if (sendmsg
== NULL
) {
1220 struct timeval tval
= {0, 0};
1221 const char *sstr
, *fstr
;
1223 status
= gettimeofday(&tval
, NULL
);
1225 time_t tick
= time(NULL
);
1231 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1237 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1241 sendmsg
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, mstr
);
1242 if (sendmsg
== NULL
) {
1243 return ASL_STATUS_FAILED
;
1246 release_sendmsg
= sendmsg
= asl_msg_merge(sendmsg
, msg
);
1249 /* write to file descriptors */
1250 for (uint32_t i
= 0; i
< asl
->out_count
; i
++) {
1252 if ((asl
->out_list
[i
].fd
!= STDOUT_FILENO
) && (asl
->out_list
[i
].fd
!= STDERR_FILENO
)) {
1253 continue; // new logging only support stdout/stderr
1257 if ((asl
->out_list
[i
].fd
>= 0) && (asl
->out_list
[i
].filter
!= 0) && ((asl
->out_list
[i
].filter
& lmask
) != 0)) {
1261 str
= asl_format_message(sendmsg
, asl
->out_list
[i
].mfmt
, asl
->out_list
[i
].tfmt
, asl
->out_list
[i
].encoding
, &len
);
1262 if (str
== NULL
) continue;
1264 status
= write(asl
->out_list
[i
].fd
, str
, len
- 1);
1267 /* soft error for fd 2 (stderr) */
1268 if (asl
->out_list
[i
].fd
== 2) status
= 0;
1269 asl
->out_list
[i
].fd
= -1;
1277 if (release_sendmsg
) {
1278 asl_msg_release(release_sendmsg
);
1285 _asl_send_message(asl_object_t obj
, uint32_t eval
, asl_msg_t
*msg
, const char *mstr
)
1287 uint32_t len
, level
, lmask
, outstatus
, objtype
;
1288 const char *sstr
, *fstr
;
1289 struct timeval tval
= {0, 0};
1291 int use_global_lock
= 0;
1292 kern_return_t kstatus
;
1294 asl_msg_t
*qd_msg
= NULL
;
1295 asl_client_t
*asl
= NULL
;
1296 static dispatch_once_t noquota_once
;
1297 __block
int log_quota_msg
= 0;
1299 if ((eval
& EVAL_ASL
) == 0) return ASL_STATUS_OK
;
1303 asl
= _asl_open_default();
1304 if (asl
== NULL
) return ASL_STATUS_FAILED
;
1305 use_global_lock
= 1;
1306 objtype
= ASL_TYPE_CLIENT
;
1310 objtype
= asl_get_type(obj
);
1311 if (objtype
== ASL_TYPE_CLIENT
) asl
= (asl_client_t
*)obj
;
1314 level
= eval
& EVAL_LEVEL_MASK
;
1315 if (level
> 7) level
= 7;
1316 lmask
= ASL_FILTER_MASK(level
);
1318 if ((objtype
== ASL_TYPE_CLIENT
) && (asl
->aslfile
!= NULL
)) use_global_lock
= 1;
1320 status
= gettimeofday(&tval
, NULL
);
1323 time_t tick
= time(NULL
);
1329 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1330 if (status
!= 0) sstr
= NULL
;
1333 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1334 if (status
!= 0) fstr
= NULL
;
1336 sendmsg
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, mstr
);
1337 if (sendmsg
== NULL
) return ASL_STATUS_FAILED
;
1339 /* Set "ASLOption store" if tunneling */
1340 if (eval
& EVAL_TUNNEL
) _asl_set_option(sendmsg
, ASL_OPT_STORE
);
1344 if (use_global_lock
!= 0) pthread_mutex_lock(&_asl_global
.lock
);
1346 sendmsg
= asl_msg_merge(sendmsg
, msg
);
1348 if (objtype
!= ASL_TYPE_CLIENT
)
1350 asl_append(obj
, (asl_object_t
)sendmsg
);
1351 asl_msg_release(sendmsg
);
1352 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1353 return ASL_STATUS_OK
;
1357 * If there is an aslfile this is a stand-alone file client.
1358 * Just save to the file.
1360 if (asl
->aslfile
!= NULL
)
1362 outstatus
= ASL_STATUS_FAILED
;
1364 if (sendmsg
!= NULL
)
1366 outstatus
= asl_file_save(asl
->aslfile
, sendmsg
, &(asl
->aslfileid
));
1370 asl_msg_release(sendmsg
);
1372 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1381 * Quotas are disabled if:
1382 * - a remote control filter is in place (EVAL_TUNNEL)
1383 * - Environment variable ASL_QUOTA_DISABLED == 1
1384 * - /etc/asl/.noquota existed at the time that the process started
1386 * We just check /etc/asl/.noquota once, since it would be
1387 * expensive to stat() for every log message.
1389 * We only check the Environment variable once, since getenv() is
1390 * not thread safe. If someone is changing the environment,
1394 dispatch_once(&noquota_once
, ^{
1396 memset(&sb
, 0, sizeof(struct stat
));
1398 int save_errno
= errno
;
1400 if (stat(NOQUOTA_FILE_PATH
, &sb
) == 0)
1402 _asl_global
.quota
= UINT32_MAX
;
1406 const char *qtest
= getenv(NOQUOTA_ENV
);
1407 if ((qtest
!= NULL
) && (!strcmp(qtest
, "1")))
1409 _asl_global
.quota
= UINT32_MAX
;
1414 /* reset errno since we want stat() to fail silently */
1418 if (log_quota_msg
!= 0)
1420 qd_msg
= asl_base_msg(asl
, QUOTA_LEVEL
, &tval
, sstr
, fstr
, QUOTA_DISABLED_MSG
);
1421 asl_msg_set_key_val(qd_msg
, ASL_KEY_OPTION
, ASL_OPT_STORE
);
1424 if (((eval
& EVAL_TUNNEL
) == 0) && (_asl_global
.quota
!= UINT32_MAX
))
1426 time_t last_send
= _asl_global
.last_send
;
1427 time_t last_oq
= _asl_global
.last_oq_msg
;
1428 uint32_t qcurr
= _asl_global
.quota
;
1430 uint32_t qinc
, qnew
;
1434 /* add QUOTA_MPS to quota for each second we've been idle */
1435 if (tval
.tv_sec
> last_send
)
1437 delta
= tval
.tv_sec
- last_send
;
1440 if (delta
< (QUOTA_MPH
/ QUOTA_MPS
)) qinc
= delta
* QUOTA_MPS
;
1442 qnew
= MIN(QUOTA_MPH
, qcurr
+ qinc
);
1443 OSAtomicCompareAndSwapLongBarrier(last_send
, tval
.tv_sec
, (long *)&_asl_global
.last_send
);
1448 if ((tval
.tv_sec
- last_oq
) > QUOTA_MSG_INTERVAL
)
1451 OSAtomicCompareAndSwapLongBarrier(last_oq
, tval
.tv_sec
, (long *)&_asl_global
.last_oq_msg
);
1455 eval
&= ~EVAL_SEND_ASL
;
1460 OSAtomicCompareAndSwap32Barrier(qcurr
, qnew
- 1, (int32_t *)&_asl_global
.quota
);
1464 if ((_asl_global
.server_port
!= MACH_PORT_NULL
) && (eval
& EVAL_SEND_ASL
))
1466 asl_string_t
*send_str
;
1470 if (eval
& EVAL_QUOTA
)
1472 asl_msg_set_key_val(sendmsg
, ASL_KEY_LEVEL
, QUOTA_LEVEL_STR
);
1473 asl_msg_set_key_val(sendmsg
, ASL_KEY_MSG
, QUOTA_MSG
);
1478 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, qd_msg
, "raw");
1479 len
= asl_string_length(send_str
);
1480 vmsize
= asl_string_allocated_size(send_str
);
1481 str
= asl_string_release_return_bytes(send_str
);
1484 kstatus
= _asl_server_message(_asl_global
.server_port
, (caddr_t
)str
, len
);
1485 if (kstatus
!= KERN_SUCCESS
)
1487 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1490 else if (vmsize
!= 0)
1492 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1494 asl_msg_release(qd_msg
);
1497 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, sendmsg
, "raw");
1498 len
= asl_string_length(send_str
);
1500 if (len
> LIBASL_MAX_MSG_SIZE
)
1504 snprintf(tmp
, sizeof(tmp
), SIZE_LIMIT_MSG
, len
, LIBASL_MAX_MSG_SIZE
);
1505 asl_msg_t
*limitmsg
= asl_base_msg(asl
, ASL_LEVEL_CRIT
, &tval
, sstr
, fstr
, tmp
);
1507 asl_string_release(send_str
);
1510 if (limitmsg
!= NULL
)
1512 /* Set "ASLOption store" if tunneling */
1513 if (eval
& EVAL_TUNNEL
) _asl_set_option(limitmsg
, ASL_OPT_STORE
);
1515 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, limitmsg
, "raw");
1516 len
= asl_string_length(send_str
);
1517 asl_msg_release(limitmsg
);
1521 vmsize
= asl_string_allocated_size(send_str
);
1522 str
= asl_string_release_return_bytes(send_str
);
1526 /* send a mach message to syslogd */
1527 kstatus
= _asl_server_message(_asl_global
.server_port
, (caddr_t
)str
, len
);
1528 if (kstatus
!= KERN_SUCCESS
)
1530 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1534 else if (vmsize
>0) vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1537 if ((sendmsg
!= NULL
) && (asl
->out_count
> 0))
1539 status
= _asl_send_message_text(asl
, sendmsg
, obj
, eval
, msg
, mstr
, false);
1542 asl_msg_release(sendmsg
);
1544 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1550 os_log_with_args_wrapper(void *addr
, int level
, const char *format
, ...)
1553 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
1554 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
1555 os_log_type_t type
= shim_asl_to_log_type
[level
];
1557 va_start(ap
, format
);
1558 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
1563 * asl_send: send a message
1564 * This routine may be used instead of asl_log() or asl_vlog() if asl_set()
1565 * has been used to set all of a message's attributes.
1566 * eval: hints about what to do with the message
1567 * msg: an asl message
1568 * returns 0 for success, non-zero for failure
1570 __private_extern__ ASL_STATUS
1571 asl_client_internal_send(asl_object_t obj
, asl_object_t msg
, void *addr
)
1573 int status
= ASL_STATUS_OK
;
1574 uint32_t eval
= _asl_evaluate_send(obj
, msg
, -1);
1576 const char *message
= asl_msg_get_val_for_key((asl_msg_t
*)msg
, ASL_KEY_MSG
);
1578 if ((eval
& EVAL_SEND_TRACE
) && message
&& message
[0] && os_log_shim_enabled(addr
))
1580 int level
= ASL_LEVEL_DEBUG
;
1581 const char *lval
= asl_msg_get_val_for_key((asl_msg_t
*)msg
, ASL_KEY_LEVEL
);
1582 if (lval
!= NULL
) level
= atoi(lval
);
1585 * If the return address and the format string are from different
1586 * binaries, os_log_with_args will not record the return address.
1587 * Work around this by passing a dynamic format string.
1589 char dynamic_format
[] = "%s";
1590 os_log_with_args_wrapper(addr
, level
, dynamic_format
, message
);
1592 if (eval
& EVAL_TEXT_FILE
)
1594 status
= _asl_send_message_text(NULL
, NULL
, obj
, eval
, (asl_msg_t
*)msg
, NULL
, true);
1597 else if (eval
& EVAL_ASL
)
1599 status
= _asl_send_message(obj
, eval
, (asl_msg_t
*)msg
, NULL
);
1606 #pragma mark auxiliary files and URLs
1609 _asl_aux_save_context(asl_aux_context_t
*ctx
)
1611 if (ctx
== NULL
) return ASL_STATUS_FAILED
;
1613 pthread_mutex_lock(&_asl_global
.lock
);
1615 _asl_global
.aux_ctx
= (asl_aux_context_t
**)reallocf(_asl_global
.aux_ctx
, (_asl_global
.aux_count
+ 1) * sizeof(asl_aux_context_t
*));
1616 if (_asl_global
.aux_ctx
== NULL
)
1618 _asl_global
.aux_count
= 0;
1619 pthread_mutex_unlock(&_asl_global
.lock
);
1620 return ASL_STATUS_FAILED
;
1623 _asl_global
.aux_ctx
[_asl_global
.aux_count
++] = ctx
;
1625 pthread_mutex_unlock(&_asl_global
.lock
);
1627 return ASL_STATUS_OK
;
1631 * Creates an auxiliary file that may be used to save arbitrary data. The ASL message msg
1632 * will be saved at the time that the auxiliary file is created. The message will include
1633 * any keys and values found in msg, and it will include the title and Uniform Type
1634 * Identifier specified. Output parameter out_fd will contain the file descriptor of the
1635 * new auxiliary file.
1638 _asl_auxiliary(asl_msg_t
*msg
, const char *title
, const char *uti
, const char *url
, int *out_fd
)
1641 asl_string_t
*send_str
;
1643 fileport_t fileport
;
1644 kern_return_t kstatus
;
1646 uint32_t newurllen
, where
;
1647 int status
, fd
, fdpair
[2];
1649 dispatch_queue_t pipe_q
;
1650 dispatch_io_t pipe_channel
;
1651 dispatch_semaphore_t sem
;
1653 aux
= asl_msg_new(ASL_TYPE_MSG
);
1655 if (url
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_URL
, url
);
1656 if (title
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_TITLE
, title
);
1657 if (uti
== NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_UTI
, "public.data");
1658 else asl_msg_set_key_val(aux
, ASL_KEY_AUX_UTI
, uti
);
1660 aux
= asl_msg_merge(aux
, msg
);
1662 /* if (out_fd == NULL), this is from asl_log_auxiliary_location */
1665 uint32_t eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux
, -1);
1666 status
= _asl_send_message(NULL
, eval
, aux
, NULL
);
1667 asl_msg_release(aux
);
1671 where
= asl_store_location();
1672 if (where
== ASL_STORE_LOCATION_MEMORY
)
1675 asl_aux_context_t
*ctx
= (asl_aux_context_t
*)calloc(1, sizeof(asl_aux_context_t
));
1676 if (ctx
== NULL
) return ASL_STATUS_FAILED
;
1678 status
= pipe(fdpair
);
1682 return ASL_STATUS_FAILED
;
1685 /* give read end to dispatch_io_read */
1687 sem
= dispatch_semaphore_create(0);
1689 ctx
->fd
= fdpair
[1];
1691 status
= _asl_aux_save_context(ctx
);
1692 if (status
!= ASL_STATUS_OK
)
1696 dispatch_release(sem
);
1698 return ASL_STATUS_FAILED
;
1701 pipe_q
= dispatch_queue_create("ASL_AUX_PIPE_Q", NULL
);
1702 pipe_channel
= dispatch_io_create(DISPATCH_IO_STREAM
, fd
, pipe_q
, ^(int err
){
1706 *out_fd
= fdpair
[1];
1708 dispatch_io_set_low_water(pipe_channel
, SIZE_MAX
);
1710 dispatch_io_read(pipe_channel
, 0, SIZE_MAX
, pipe_q
, ^(bool done
, dispatch_data_t pipedata
, int err
){
1713 size_t len
= dispatch_data_get_size(pipedata
);
1716 const char *bytes
= NULL
;
1720 dispatch_data_t md
= dispatch_data_create_map(pipedata
, (const void **)&bytes
, &len
);
1721 encoded
= asl_core_encode_buffer(bytes
, len
);
1722 asl_msg_set_key_val(aux
, ASL_KEY_AUX_DATA
, encoded
);
1724 eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux
, -1);
1725 _asl_send_message(NULL
, eval
, aux
, NULL
);
1726 asl_msg_release(aux
);
1727 dispatch_release(md
);
1733 dispatch_semaphore_signal(sem
);
1734 dispatch_release(pipe_channel
);
1735 dispatch_release(pipe_q
);
1739 return ASL_STATUS_OK
;
1743 if (_asl_global
.server_port
== MACH_PORT_NULL
) return ASL_STATUS_FAILED
;
1745 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, aux
, "raw");
1746 len
= asl_string_length(send_str
);
1747 vmsize
= asl_string_allocated_size(send_str
);
1748 str
= asl_string_release_return_bytes(send_str
);
1752 asl_msg_release(aux
);
1753 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1754 return ASL_STATUS_FAILED
;
1758 fileport
= MACH_PORT_NULL
;
1759 status
= KERN_SUCCESS
;
1761 kstatus
= _asl_server_create_aux_link(_asl_global
.server_port
, (caddr_t
)str
, len
, &fileport
, &newurl
, &newurllen
, &status
);
1762 if (kstatus
!= KERN_SUCCESS
)
1764 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1765 asl_msg_release(aux
);
1766 return ASL_STATUS_FAILED
;
1771 asl_msg_release(aux
);
1777 asl_msg_set_key_val(aux
, ASL_KEY_AUX_URL
, newurl
);
1778 vm_deallocate(mach_task_self(), (vm_address_t
)newurl
, newurllen
);
1781 if (fileport
== MACH_PORT_NULL
)
1783 asl_msg_release(aux
);
1784 return ASL_STATUS_FAILED
;
1787 fd
= fileport_makefd(fileport
);
1788 mach_port_deallocate(mach_task_self(), fileport
);
1791 asl_msg_release(aux
);
1796 asl_aux_context_t
*ctx
= (asl_aux_context_t
*)calloc(1, sizeof(asl_aux_context_t
));
1808 status
= _asl_aux_save_context(ctx
);
1816 asl_create_auxiliary_file(asl_object_t msg
, const char *title
, const char *uti
, int *out_fd
)
1818 if (out_fd
== NULL
) return -1;
1820 ASL_STATUS status
= _asl_auxiliary((asl_msg_t
*)msg
, title
, uti
, NULL
, out_fd
);
1821 return (status
== ASL_STATUS_OK
) ? 0 : -1;
1825 asl_log_auxiliary_location(asl_object_t msg
, const char *title
, const char *uti
, const char *url
)
1827 ASL_STATUS status
= _asl_auxiliary((asl_msg_t
*)msg
, title
, uti
, url
, NULL
);
1828 return (status
== ASL_STATUS_OK
) ? 0 : -1;
1832 * Close an auxiliary file.
1833 * Sends the cached auxiliary message to syslogd.
1834 * Returns 0 on success, -1 on error.
1837 asl_close_auxiliary_file(int fd
)
1841 dispatch_semaphore_t aux_sem
= NULL
;
1843 pthread_mutex_lock(&(_asl_global
.lock
));
1848 for (i
= 0; i
< _asl_global
.aux_count
; i
++)
1850 if (_asl_global
.aux_ctx
[i
]->fd
== fd
)
1854 aux_msg
= _asl_global
.aux_ctx
[i
]->msg
;
1855 aux_sem
= _asl_global
.aux_ctx
[i
]->sem
;
1857 free(_asl_global
.aux_ctx
[i
]);
1859 for (j
= i
+ 1; j
< _asl_global
.aux_count
; i
++, j
++)
1861 _asl_global
.aux_ctx
[i
] = _asl_global
.aux_ctx
[j
];
1864 _asl_global
.aux_count
--;
1866 if (_asl_global
.aux_count
== 0)
1868 free(_asl_global
.aux_ctx
);
1869 _asl_global
.aux_ctx
= NULL
;
1873 _asl_global
.aux_ctx
= (asl_aux_context_t
**)reallocf(_asl_global
.aux_ctx
, _asl_global
.aux_count
* sizeof(asl_aux_context_t
*));
1874 if (_asl_global
.aux_ctx
== NULL
)
1876 _asl_global
.aux_count
= 0;
1885 pthread_mutex_unlock(&(_asl_global
.lock
));
1889 if (aux_msg
!= NULL
)
1891 uint32_t eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux_msg
, -1);
1892 if (_asl_send_message(NULL
, eval
, aux_msg
, NULL
) != ASL_STATUS_OK
) status
= -1;
1893 asl_msg_release(aux_msg
);
1896 if (aux_sem
!= NULL
)
1898 dispatch_semaphore_wait(aux_sem
, DISPATCH_TIME_FOREVER
);
1899 dispatch_release(aux_sem
);
1908 _asl_server_control_query(void)
1910 asl_msg_list_t
*list
= NULL
;
1912 uint32_t len
, reslen
, status
;
1913 uint64_t cmax
, qmin
;
1914 kern_return_t kstatus
;
1916 asl_msg_t
*m
= NULL
;
1917 static const char ctlstr
[] = "1\nQ [= ASLOption control]\n";
1920 if (_asl_global
.server_port
== MACH_PORT_NULL
) return NULL
;
1922 len
= strlen(ctlstr
) + 1;
1929 kstatus
= vm_allocate(mach_task_self(), (vm_address_t
*)&vmstr
, len
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_MEMORY_ASL
));
1930 if (kstatus
!= KERN_SUCCESS
) return NULL
;
1932 memmove(vmstr
, ctlstr
, len
);
1935 kstatus
= _asl_server_query_2(_asl_global
.server_port
, vmstr
, len
, qmin
, FETCH_BATCH
, 0, (caddr_t
*)&res
, &reslen
, &cmax
, (int *)&status
);
1936 if (kstatus
!= KERN_SUCCESS
) return NULL
;
1938 list
= asl_msg_list_from_string(res
);
1939 vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1941 if (list
== NULL
) return NULL
;
1942 if (list
->count
> 0) m
= asl_msg_retain(list
->msg
[0]);
1943 asl_msg_list_release(list
);
1948 * Returns ASL_STORE_LOCATION_FILE or ASL_STORE_LOCATION_MEMORY
1951 asl_store_location()
1953 kern_return_t kstatus
;
1955 uint32_t reslen
, status
;
1959 if (_asl_global
.server_port
== MACH_PORT_NULL
) return ASL_STORE_LOCATION_FILE
;
1964 status
= ASL_STATUS_OK
;
1966 kstatus
= _asl_server_query_2(_asl_global
.server_port
, NULL
, 0, 0, -1, 0, (caddr_t
*)&res
, &reslen
, &cmax
, (int *)&status
);
1967 if (kstatus
!= KERN_SUCCESS
) return ASL_STORE_LOCATION_FILE
;
1969 /* res should never be returned, but just to be certain we don't leak VM ... */
1970 if (res
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1972 if (status
== ASL_STATUS_OK
) return ASL_STORE_LOCATION_MEMORY
;
1973 return ASL_STORE_LOCATION_FILE
;
1977 asl_open_path(const char *path
, uint32_t opts
)
1980 asl_file_t
*fout
= NULL
;
1981 asl_store_t
*sout
= NULL
;
1983 if (opts
== 0) opts
= ASL_OPT_OPEN_READ
;
1985 if (opts
& ASL_OPT_OPEN_READ
)
1989 if (asl_store_open_read(ASL_PLACE_DATABASE_DEFAULT
, &sout
) != ASL_STATUS_OK
) return NULL
;
1990 return (asl_object_t
)sout
;
1993 memset(&sb
, 0, sizeof(struct stat
));
1994 if (stat(path
, &sb
) < 0) return NULL
;
1996 if (sb
.st_mode
& S_IFREG
)
1998 if (asl_file_open_read(path
, &fout
) != ASL_STATUS_OK
) return NULL
;
1999 return (asl_object_t
)fout
;
2001 else if (sb
.st_mode
& S_IFDIR
)
2003 if (asl_store_open_read(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2004 return (asl_object_t
)sout
;
2009 else if (opts
& ASL_OPT_OPEN_WRITE
)
2011 if (path
== NULL
) return NULL
;
2013 memset(&sb
, 0, sizeof(struct stat
));
2014 if (stat(path
, &sb
) < 0)
2016 if (errno
!= ENOENT
) return NULL
;
2018 if (opts
& ASL_OPT_CREATE_STORE
)
2020 if (asl_store_open_write(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2021 return (asl_object_t
)sout
;
2025 if (asl_file_open_write(path
, 0644, geteuid(), getegid(), &fout
) != ASL_STATUS_OK
) return NULL
;
2026 return (asl_object_t
)fout
;
2029 else if (sb
.st_mode
& S_IFREG
)
2031 if (asl_file_open_write(path
, 0644, geteuid(), getegid(), &fout
) != ASL_STATUS_OK
) return NULL
;
2032 return (asl_object_t
)fout
;
2034 else if (sb
.st_mode
& S_IFDIR
)
2036 if (asl_store_open_write(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2037 return (asl_object_t
)sout
;