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);
113 extern void _asl_mt_shim_fork_child(void);
114 extern void _asl_mt_shim_send_message(asl_msg_t
*msg
);
121 dispatch_semaphore_t sem
;
130 uint64_t proc_filter
;
131 uint64_t master_filter
;
135 dispatch_once_t port_lookup_once
;
136 mach_port_t server_port
;
138 pthread_mutex_t lock
;
140 asl_aux_context_t
**aux_ctx
;
144 __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
};
146 static const char *level_to_number_string
[] = {"0", "1", "2", "3", "4", "5", "6", "7"};
148 #define ASL_SERVICE_NAME "com.apple.system.logger"
151 * Called from the child process inside fork() to clean up
152 * inherited state from the parent process.
154 * NB. A lock isn't required, since we're single threaded in this call.
159 _asl_global
.notify_count
= 0;
160 _asl_global
.rc_change_token
= -1;
161 _asl_global
.master_token
= -1;
162 _asl_global
.notify_token
= -1;
163 _asl_global
.quota
= 0;
164 _asl_global
.last_send
= 0;
165 _asl_global
.last_oq_msg
= 0;
167 _asl_global
.port_lookup_once
= 0;
168 _asl_global
.server_port
= MACH_PORT_NULL
;
170 pthread_mutex_init(&(_asl_global
.lock
), NULL
);
172 _asl_redirect_fork_child();
174 _asl_mt_shim_fork_child();
179 * asl_remote_notify_name: returns the notification key for remote-control filter
180 * changes for this process.
183 asl_remote_notify_name()
185 pid_t pid
= getpid();
186 uid_t euid
= geteuid();
189 if (euid
== 0) asprintf(&str
, "%s.%d", NOTIFY_PREFIX_SYSTEM
, pid
);
190 else asprintf(&str
, "user.uid.%d.syslog.%d", euid
, pid
);
196 _asl_notify_open(int do_lock
)
201 if (do_lock
!= 0) pthread_mutex_lock(&_asl_global
.lock
);
203 _asl_global
.notify_count
++;
205 if (_asl_global
.notify_token
!= -1)
207 if (do_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
208 return ASL_STATUS_OK
;
211 if (_asl_global
.rc_change_token
== -1)
213 status
= notify_register_check(NOTIFY_RC
, &_asl_global
.rc_change_token
);
214 if (status
!= NOTIFY_STATUS_OK
) _asl_global
.rc_change_token
= -1;
217 if (_asl_global
.master_token
== -1)
219 status
= notify_register_plain(NOTIFY_SYSTEM_MASTER
, &_asl_global
.master_token
);
220 if (status
!= NOTIFY_STATUS_OK
) _asl_global
.master_token
= -1;
223 notify_name
= asl_remote_notify_name();
224 if (notify_name
!= NULL
)
226 status
= notify_register_plain(notify_name
, &_asl_global
.notify_token
);
228 if (status
!= NOTIFY_STATUS_OK
) _asl_global
.notify_token
= -1;
231 if (do_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
233 if (_asl_global
.notify_token
== -1) return ASL_STATUS_FAILED
;
234 return ASL_STATUS_OK
;
241 pthread_mutex_lock(&_asl_global
.lock
);
243 if (_asl_global
.notify_count
> 0) _asl_global
.notify_count
--;
245 if (_asl_global
.notify_count
> 0)
247 pthread_mutex_unlock(&_asl_global
.lock
);
251 if (_asl_global
.rc_change_token
>= 0) notify_cancel(_asl_global
.rc_change_token
);
252 _asl_global
.rc_change_token
= -1;
254 if (_asl_global
.master_token
>= 0) notify_cancel(_asl_global
.master_token
);
255 _asl_global
.master_token
= -1;
257 if (_asl_global
.notify_token
>= 0) notify_cancel(_asl_global
.notify_token
);
258 _asl_global
.notify_token
= -1;
260 pthread_mutex_unlock(&_asl_global
.lock
);
267 dispatch_once(&_asl_global
.port_lookup_once
, ^{
268 char *str
= getenv("ASL_DISABLE");
269 if ((str
== NULL
) || strcmp(str
, "1"))
271 bootstrap_look_up2(bootstrap_port
, ASL_SERVICE_NAME
, &_asl_global
.server_port
, 0, BOOTSTRAP_PRIVILEGED_SERVER
);
277 asl_core_get_service_port(__unused
int reset
)
280 return _asl_global
.server_port
;
284 #pragma mark asl_client
287 asl_open(const char *ident
, const char *facility
, uint32_t opts
)
289 asl_client_t
*asl
= asl_client_open(ident
, facility
, opts
);
290 if (asl
== NULL
) return NULL
;
293 if (!(opts
& ASL_OPT_NO_REMOTE
)) _asl_notify_open(1);
295 return (asl_object_t
)asl
;
299 asl_open_from_file(int fd
, const char *ident
, const char *facility
)
301 return (asl_object_t
)asl_client_open_from_file(fd
, ident
, facility
);
305 asl_close(asl_object_t obj
)
310 __private_extern__ asl_client_t
*
313 static dispatch_once_t once
;
315 dispatch_once(&once
, ^{
317 * Do a sleight-of-hand with ASL_OPT_NO_REMOTE to avoid a deadlock
318 * since asl_open(xxx, yyy, 0) calls _asl_notify_open(1)
319 * which locks _asl_global.lock.
321 _asl_global
.asl
= (asl_client_t
*)asl_open(NULL
, NULL
, ASL_OPT_NO_REMOTE
);
323 /* Reset options to clear ASL_OPT_NO_REMOTE bit */
324 if (_asl_global
.asl
!= NULL
) _asl_global
.asl
->options
= 0;
326 /* Now call _asl_notify_open(0) to finish the work */
330 return _asl_global
.asl
;
334 * asl_add_file: write log messages to the given file descriptor
335 * Log messages will be written to this file as well as to the server.
338 asl_add_output_file(asl_object_t client
, int fd
, const char *mfmt
, const char *tfmt
, int filter
, int text_encoding
)
340 int status
, use_global_lock
= 0;
343 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
345 asl
= (asl_client_t
*)client
;
348 asl
= _asl_open_default();
349 if (asl
== NULL
) return -1;
350 pthread_mutex_lock(&_asl_global
.lock
);
354 status
= asl_client_add_output_file(asl
, fd
, mfmt
, tfmt
, filter
, text_encoding
);
356 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
357 return (status
== ASL_STATUS_OK
) ? 0 : -1;
360 /* returns previous filter value or -1 on error */
362 asl_set_output_file_filter(asl_object_t client
, int fd
, int filter
)
365 int use_global_lock
= 0;
368 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
370 asl
= (asl_client_t
*)client
;
373 asl
= _asl_open_default();
374 if (asl
== NULL
) return -1;
375 pthread_mutex_lock(&_asl_global
.lock
);
379 last
= asl_client_set_output_file_filter(asl
, fd
, filter
);
381 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
385 /* SPI - Deprecated */
387 asl_add_output(asl_object_t client
, int fd
, const char *mfmt
, const char *tfmt
, uint32_t text_encoding
)
389 return asl_add_output_file(client
, fd
, mfmt
, tfmt
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
), text_encoding
);
392 /* SPI - Deprecated */
394 asl_add_log_file(asl_object_t client
, int fd
)
396 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
);
400 * asl_remove_output: stop writing log messages to the given file descriptor
403 asl_remove_output_file(asl_object_t client
, int fd
)
405 int status
, use_global_lock
= 0;
408 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
410 asl
= (asl_client_t
*)client
;
413 asl
= _asl_open_default();
414 if (asl
== NULL
) return -1;
415 pthread_mutex_lock(&_asl_global
.lock
);
419 status
= asl_client_remove_output_file(asl
, fd
);
421 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
422 return (status
== ASL_STATUS_OK
) ? 0 : -1;
426 asl_remove_output(asl_object_t client
, int fd
)
428 return asl_remove_output_file(client
, fd
);
432 asl_remove_log_file(asl_object_t client
, int fd
)
434 return asl_remove_output_file(client
, fd
);
437 /* returns previous filter value or -1 on error */
439 asl_set_filter(asl_object_t client
, int f
)
441 int last
, use_global_lock
= 0;
444 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
446 asl
= (asl_client_t
*)client
;
449 asl
= _asl_open_default();
450 if (asl
== NULL
) return -1;
451 pthread_mutex_lock(&_asl_global
.lock
);
455 last
= asl_client_set_filter(asl
, f
);
457 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
463 #pragma mark message sending
466 * Evaluate client / message / level to determine what to do with a message.
467 * Checks filters, tunneling, and log files.
468 * Returns the bits below, ORed with the message level.
470 * EVAL_SEND_ASL - send this message to syslogd
471 * EVAL_SEND_TRACE - log this message with Activity Tracing
472 * EVAL_ASL_FILE - write to a standalone ASL file (see asl_open_from_file)
473 * EVAL_TEXT_FILE - write this message to a text file / stderr
474 * EVAL_TUNNEL - tunneling enabled when sending to syslogd
477 _asl_evaluate_send(asl_object_t client
, asl_object_t m
, int slevel
)
480 asl_msg_t
*msg
= (asl_msg_t
*)m
;
481 uint32_t level
, lmask
, filter
, status
, tunnel
;
487 level
= ASL_LEVEL_DEBUG
;
488 if (slevel
>= 0) level
= slevel
;
491 if ((asl_msg_lookup(msg
, ASL_KEY_LEVEL
, &val
, NULL
) == 0) && (val
!= NULL
)) level
= atoi(val
);
493 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
494 else if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
496 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
))
498 /* sending to something other than a client */
499 return EVAL_ACTIVE
| EVAL_SEND_ASL
| level
;
502 asl
= (asl_client_t
*)client
;
503 if ((asl
!= NULL
) && (asl
->aslfile
!= NULL
)) return (EVAL_ASL_FILE
| level
);
505 if (asl
== NULL
) asl
= _asl_open_default();
508 eval
= EVAL_ACTIVE
| EVAL_DEFAULT_ACTION
| level
;
509 eval
&= ~EVAL_SEND_ASL
;
513 eval
= (asl_client_get_control(asl
) & EVAL_ACTION_MASK
) | level
;
515 filter
= asl
->filter
& 0xff;
516 tunnel
= (asl
->filter
& ASL_FILTER_MASK_TUNNEL
) >> 8;
517 if (tunnel
!= 0) eval
|= EVAL_TUNNEL
;
519 if ((asl
->options
& ASL_OPT_NO_REMOTE
) == 0)
521 pthread_mutex_lock(&_asl_global
.lock
);
523 if (_asl_global
.rc_change_token
>= 0)
525 /* initialize or re-check process-specific and master filters */
527 status
= notify_check(_asl_global
.rc_change_token
, &check
);
528 if ((status
== NOTIFY_STATUS_OK
) && (check
!= 0))
530 if (_asl_global
.master_token
>= 0)
533 status
= notify_get_state(_asl_global
.master_token
, &v64
);
534 if (status
== NOTIFY_STATUS_OK
) _asl_global
.master_filter
= v64
;
537 if (_asl_global
.notify_token
>= 0)
540 status
= notify_get_state(_asl_global
.notify_token
, &v64
);
541 if (status
== NOTIFY_STATUS_OK
) _asl_global
.proc_filter
= v64
;
546 if (_asl_global
.master_filter
& EVAL_ACTIVE
)
548 /* clear bits and set according to master */
549 eval
&= ~(EVAL_SEND_ASL
| EVAL_SEND_TRACE
);
550 eval
|= (_asl_global
.master_filter
& (EVAL_SEND_ASL
| EVAL_SEND_TRACE
));
553 if ((_asl_global
.master_filter
& EVAL_LEVEL_MASK
) != 0) filter
= _asl_global
.proc_filter
& EVAL_LEVEL_MASK
;
556 if (_asl_global
.proc_filter
& EVAL_ACTIVE
)
558 /* clear bits and set according to proc */
559 eval
&= ~(EVAL_SEND_ASL
| EVAL_SEND_TRACE
| EVAL_TEXT_FILE
);
560 eval
|= (_asl_global
.proc_filter
& (EVAL_SEND_ASL
| EVAL_SEND_TRACE
| EVAL_TEXT_FILE
));
563 if ((_asl_global
.proc_filter
& EVAL_LEVEL_MASK
) != 0) filter
= _asl_global
.proc_filter
& EVAL_LEVEL_MASK
;
566 pthread_mutex_unlock(&_asl_global
.lock
);
569 lmask
= ASL_FILTER_MASK(level
);
570 if ((filter
!= 0) && ((filter
& lmask
) == 0)) eval
&= ~EVAL_SEND_ASL
;
571 if (asl
->out_count
> 0) eval
|= EVAL_TEXT_FILE
;
573 /* don't send MessageTracer messages to Activity Tracing */
575 if ((asl_msg_lookup(msg
, ASL_KEY_MESSAGETRACER
, &val
, NULL
) == 0) && (val
!= NULL
))
577 eval
&= ~EVAL_SEND_TRACE
;
578 eval
|= EVAL_MT_SHIM
;
581 /* don't send PowerManagement messages to Activity Tracing */
583 if ((asl_msg_lookup(msg
, ASL_KEY_POWERMANAGEMENT
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
585 /* don't send control messages to Activity Tracing */
587 if ((asl_msg_lookup(msg
, ASL_KEY_OPTION
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
589 /* don't send CFLog messages to Activity Tracing */
591 if ((asl_msg_lookup(msg
, ASL_KEY_CFLOG_LOCAL_TIME
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
594 if (((asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &val
, NULL
) == 0) && (val
!= NULL
)) ||
595 ((asl_msg_lookup(asl
->kvdict
, ASL_KEY_FACILITY
, &val
, NULL
) == 0) && (val
!= NULL
)))
597 /* don't send lastlog/utmp messages to Activity Tracing */
598 if (!strcmp(val
, FACILITY_LASTLOG
) || !strcmp(val
, FACILITY_UTMPX
)) eval
&= ~EVAL_SEND_TRACE
;
600 /* don't send LOG_INSTALL messages to Activity Tracing */
601 if (!strcmp(val
, asl_syslog_faciliy_num_to_name(LOG_INSTALL
))) eval
&= ~EVAL_SEND_TRACE
;
609 * Internal routine used by asl_vlog.
610 * msg: an asl messsage
611 * eval: log level and send flags for the message
612 * format: A formating string
613 * ap: va_list for the format
614 * returns 0 for success, non-zero for failure
617 __private_extern__ ASL_STATUS
618 _asl_lib_vlog_text(asl_object_t obj
, uint32_t eval
, asl_object_t msg
, const char *format
, va_list ap
)
620 int saved_errno
= errno
;
622 char *str
, *fmt
, estr
[NL_TEXTMAX
];
623 uint32_t i
, len
, elen
, expand
;
625 if (format
== NULL
) return ASL_STATUS_INVALID_ARG
;
627 /* insert strerror for %m */
632 for (i
= 0; format
[i
] != '\0'; i
++)
634 if (format
[i
] == '%')
636 if (format
[i
+1] == '\0') len
++;
637 else if (format
[i
+1] == 'm')
640 strerror_r(saved_errno
, estr
, sizeof(estr
));
654 fmt
= (char *)format
;
658 fmt
= malloc(len
+ 1);
659 if (fmt
== NULL
) return ASL_STATUS_NO_MEMORY
;
663 for (i
= 0; format
[i
] != '\0'; i
++)
665 if (format
[i
] == '%')
667 if (format
[i
+1] == '\0')
670 else if ((format
[i
+1] == 'm') && (elen
!= 0))
672 memcpy(fmt
+len
, estr
, elen
);
678 fmt
[len
++] = format
[i
++];
679 fmt
[len
++] = format
[i
];
682 else fmt
[len
++] = format
[i
];
689 vasprintf(&str
, fmt
, ap
);
690 if (expand
!= 0) free(fmt
);
692 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
694 status
= _asl_send_message_text(NULL
, NULL
, obj
, eval
, (asl_msg_t
*)msg
, str
, true);
702 * Internal routine used by asl_vlog.
703 * msg: an asl messsage
704 * eval: log level and send flags for the message
705 * format: A formating string
706 * ap: va_list for the format
707 * returns 0 for success, non-zero for failure
709 __private_extern__ ASL_STATUS
710 _asl_lib_vlog(asl_object_t obj
, uint32_t eval
, asl_object_t msg
, const char *format
, va_list ap
)
712 int saved_errno
= errno
;
714 char *str
, *fmt
, estr
[NL_TEXTMAX
];
715 uint32_t i
, len
, elen
, expand
;
717 if (format
== NULL
) return ASL_STATUS_INVALID_ARG
;
719 /* insert strerror for %m */
724 for (i
= 0; format
[i
] != '\0'; i
++)
726 if (format
[i
] == '%')
728 if (format
[i
+1] == '\0') len
++;
729 else if (format
[i
+1] == 'm')
732 strerror_r(saved_errno
, estr
, sizeof(estr
));
746 fmt
= (char *)format
;
750 fmt
= malloc(len
+ 1);
751 if (fmt
== NULL
) return ASL_STATUS_NO_MEMORY
;
755 for (i
= 0; format
[i
] != '\0'; i
++)
757 if (format
[i
] == '%')
759 if (format
[i
+1] == '\0')
762 else if ((format
[i
+1] == 'm') && (elen
!= 0))
764 memcpy(fmt
+len
, estr
, elen
);
770 fmt
[len
++] = format
[i
++];
771 fmt
[len
++] = format
[i
];
774 else fmt
[len
++] = format
[i
];
781 vasprintf(&str
, fmt
, ap
);
782 if (expand
!= 0) free(fmt
);
784 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
786 status
= _asl_send_message(obj
, eval
, (asl_msg_t
*)msg
, str
);
794 * Similar to asl_log, but take a va_list instead of a list of arguments.
795 * msg: an asl message
796 * level: the log level of the associated message
797 * format: A formating string
798 * ap: va_list for the format
799 * returns 0 for success, non-zero for failure
802 asl_vlog(asl_object_t client
, asl_object_t msg
, int level
, const char *format
, va_list ap
)
804 ASL_STATUS status
= ASL_STATUS_OK
;
805 uint32_t eval
= _asl_evaluate_send(client
, msg
, level
);
806 void *addr
= __builtin_return_address(0);
808 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
811 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
812 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
813 os_log_type_t type
= shim_asl_to_log_type
[level
];
815 va_copy(ap_copy
, ap
);
816 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap_copy
, addr
);
819 if (eval
& EVAL_TEXT_FILE
)
821 status
= _asl_lib_vlog_text(client
, eval
, msg
, format
, ap
);
824 else if (eval
& EVAL_ASL
)
826 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
829 return (status
== ASL_STATUS_OK
) ? 0 : -1;
834 * SPI used by legacy (non-shim) ASL_PREFILTER_LOG. Converts format arguments to a va_list and
835 * forwards the call to _asl_lib_vlog.
836 * msg: an asl message
837 * eval: log level and send flags for the message
838 * format: A formating string
839 * ... args for format
840 * returns 0 for success, non-zero for failure
843 _asl_lib_log(asl_object_t client
, uint32_t eval
, asl_object_t msg
, const char *format
, ...)
851 va_start(ap
, format
);
852 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
861 * Processes an ASL log message.
862 * msg: an asl message
863 * level: the log level of the associated message
864 * format: A formating string
865 * ... args for format
866 * returns 0 for success, non-zero for failure
869 asl_log(asl_object_t client
, asl_object_t msg
, int level
, const char *format
, ...)
871 ASL_STATUS status
= ASL_STATUS_OK
;
872 uint32_t eval
= _asl_evaluate_send(client
, msg
, level
);
873 void *addr
= __builtin_return_address(0);
875 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
878 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
879 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
880 os_log_type_t type
= shim_asl_to_log_type
[level
];
882 va_start(ap
, format
);
883 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
886 if (eval
& EVAL_TEXT_FILE
)
889 va_start(ap
, format
);
890 status
= _asl_lib_vlog_text(client
, eval
, msg
, format
, ap
);
894 else if (eval
& EVAL_ASL
)
897 va_start(ap
, format
);
898 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
902 return (status
== ASL_STATUS_OK
) ? 0 : -1;
907 * Like asl_log, supplies NULL client and msg.
908 * level: the log level of the associated message
909 * format: A formating string
910 * ... args for format
911 * returns 0 for success, non-zero for failure
914 asl_log_message(int level
, const char *format
, ...)
916 ASL_STATUS status
= ASL_STATUS_OK
;
917 uint32_t eval
= _asl_evaluate_send(NULL
, NULL
, level
);
918 void *addr
= __builtin_return_address(0);
920 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
923 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
924 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
925 os_log_type_t type
= shim_asl_to_log_type
[level
];
927 va_start(ap
, format
);
928 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
931 if (eval
& EVAL_TEXT_FILE
)
934 va_start(ap
, format
);
935 status
= _asl_lib_vlog_text(NULL
, eval
, NULL
, format
, ap
);
939 else if (eval
& EVAL_ASL
)
942 va_start(ap
, format
);
943 status
= _asl_lib_vlog(NULL
, eval
, NULL
, format
, ap
);
947 return (status
== ASL_STATUS_OK
) ? 0 : -1;
951 * asl_get_filter: gets the values for the local, master, and remote filters,
952 * and indicates which one is active.
955 asl_get_filter(asl_object_t client
, int *local
, int *master
, int *remote
, int *active
)
957 asl_client_t
*asl
, *asl_default
;
962 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
969 asl_default
= _asl_open_default();
971 asl
= (asl_client_t
*)client
;
972 if (asl
== NULL
) asl
= asl_default
;
973 if (asl
!= NULL
) l
= asl
->filter
& EVAL_LEVEL_MASK
;
975 if ((asl_default
!= NULL
) && (!(asl_default
->options
& ASL_OPT_NO_REMOTE
)))
977 pthread_mutex_lock(&_asl_global
.lock
);
979 if (_asl_global
.rc_change_token
>= 0)
981 /* initialize or re-check process-specific and master filters */
983 status
= notify_check(_asl_global
.rc_change_token
, &check
);
984 if ((status
== NOTIFY_STATUS_OK
) && (check
!= 0))
986 if (_asl_global
.master_token
>= 0)
989 status
= notify_get_state(_asl_global
.master_token
, &v64
);
990 if (status
== NOTIFY_STATUS_OK
) _asl_global
.master_filter
= v64
;
993 if (_asl_global
.notify_token
>= 0)
996 status
= notify_get_state(_asl_global
.notify_token
, &v64
);
997 if (status
== NOTIFY_STATUS_OK
) _asl_global
.proc_filter
= v64
;
1002 m
= _asl_global
.master_filter
;
1005 r
= _asl_global
.proc_filter
;
1008 pthread_mutex_unlock(&_asl_global
.lock
);
1011 if (local
!= NULL
) *local
= l
;
1012 if (master
!= NULL
) *master
= m
;
1013 if (remote
!= NULL
) *remote
= r
;
1014 if (active
!= NULL
) *active
= x
;
1019 /* SPI for SHIM control */
1021 asl_set_local_control(asl_object_t client
, uint32_t filter
)
1023 asl_client_t
*asl
= (asl_client_t
*)client
;
1026 asl
= _asl_open_default();
1027 if (asl
== NULL
) return UINT32_MAX
;
1029 else if (asl_get_type(client
) != ASL_TYPE_CLIENT
) return UINT32_MAX
;
1031 return asl_client_set_control(asl
, filter
);
1035 asl_get_local_control(asl_object_t client
)
1037 asl_client_t
*asl
= (asl_client_t
*)client
;
1040 asl
= _asl_open_default();
1041 if (asl
== NULL
) return UINT32_MAX
;
1043 else if (asl_get_type(client
) != ASL_TYPE_CLIENT
) return UINT32_MAX
;
1045 return asl_client_get_control(asl
);
1049 * Sets PID and OSActivityID values in a new message.
1050 * Also sets Level, Time, TimeNanoSec, Sender, Facility and Message if provided.
1053 asl_base_msg(asl_client_t
*asl
, uint32_t level
, const struct timeval
*tv
, const char *sstr
, const char *fstr
, const char *mstr
)
1058 os_activity_id_t osaid
;
1060 aux
= asl_msg_new(ASL_TYPE_MSG
);
1061 if (aux
== NULL
) return NULL
;
1064 if (level
<= 7) asl_msg_set_key_val(aux
, ASL_KEY_LEVEL
, level_to_number_string
[level
]);
1066 /* Time and TimeNanoSec */
1069 snprintf(aux_val
, sizeof(aux_val
), "%llu", (unsigned long long) tv
->tv_sec
);
1070 asl_msg_set_key_val(aux
, ASL_KEY_TIME
, aux_val
);
1072 snprintf(aux_val
, sizeof(aux_val
), "%d", tv
->tv_usec
* 1000);
1073 asl_msg_set_key_val(aux
, ASL_KEY_TIME_NSEC
, aux_val
);
1077 if (mstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_MSG
, mstr
);
1080 snprintf(aux_val
, sizeof(aux_val
), "%u", getpid());
1081 asl_msg_set_key_val(aux
, ASL_KEY_PID
, aux_val
);
1084 osaid
= os_activity_get_identifier(OS_ACTIVITY_CURRENT
, NULL
);
1087 snprintf(aux_val
, sizeof(aux_val
), "0x%016llx", osaid
);
1088 asl_msg_set_key_val(aux
, ASL_KEY_OS_ACTIVITY_ID
, aux_val
);
1092 if ((sstr
== NULL
) && (asl
!= NULL
))
1094 /* See if the client has a value for ASL_KEY_SENDER */
1095 status
= asl_msg_lookup((asl_msg_t
*)asl
->kvdict
, ASL_KEY_SENDER
, &sstr
, NULL
);
1096 if ((status
!= 0) || (sstr
== NULL
))
1100 /* See if the global cache has a value for ASL_KEY_SENDER */
1101 if (_asl_global
.sender
== NULL
)
1103 /* Get the process name with _NSGetArgv */
1104 char *name
= *(*_NSGetArgv());
1107 char *x
= strrchr(name
, '/');
1111 /* Set the cache value */
1112 pthread_mutex_lock(&_asl_global
.lock
);
1113 if (_asl_global
.sender
== NULL
) _asl_global
.sender
= strdup(x
);
1114 pthread_mutex_unlock(&_asl_global
.lock
);
1118 if (_asl_global
.sender
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, _asl_global
.sender
);
1119 else asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, "Unknown");
1123 if (sstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, sstr
);
1126 if ((fstr
== NULL
) && (asl
!= NULL
))
1128 status
= asl_msg_lookup((asl_msg_t
*)asl
->kvdict
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1129 if (status
!= 0) fstr
= NULL
;
1132 if (fstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_FACILITY
, fstr
);
1139 * Possibly useful someday...
1142 asl_prepared_message(asl_client_t
*asl
, asl_msg_t
*msg
)
1144 uint32_t i
, len
, level
, outstatus
;
1145 const char *val
, *sstr
, *fstr
;
1146 struct timeval tval
= {0, 0};
1152 asl
= _asl_open_default();
1153 if (asl
== NULL
) return NULL
;
1156 status
= gettimeofday(&tval
, NULL
);
1159 time_t tick
= time(NULL
);
1165 status
= asl_msg_lookup(msg
, ASL_KEY_LEVEL
, &val
, NULL
);
1166 if (status
!= 0) val
= NULL
;
1168 level
= ASL_LEVEL_DEBUG
;
1169 if (val
!= NULL
) level
= atoi(val
);
1170 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
1173 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1174 if (status
!= 0) sstr
= NULL
;
1177 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1178 if (status
!= 0) fstr
= NULL
;
1180 out
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, NULL
);
1181 out
= asl_msg_merge(out
, msg
);
1188 _asl_set_option(asl_msg_t
*msg
, const char *opt
)
1190 const char *val
= NULL
;
1193 if (msg
== NULL
) return;
1194 if (opt
== NULL
) return;
1196 status
= asl_msg_lookup(msg
, ASL_KEY_OPTION
, &val
, NULL
);
1197 if ((status
!= 0) || (val
== NULL
))
1199 asl_msg_set_key_val(msg
, ASL_KEY_OPTION
, opt
);
1203 char *option
= NULL
;
1204 asprintf(&option
, "%s %s", opt
, val
);
1205 asl_msg_set_key_val(msg
, ASL_KEY_OPTION
, option
);
1211 _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
)
1214 uint32_t level
, lmask
;
1215 asl_msg_t
*release_sendmsg
= NULL
;
1219 asl
= _asl_open_default();
1220 if (asl
== NULL
) return ASL_STATUS_FAILED
;
1222 uint32_t objtype
= asl_get_type(obj
);
1223 if (objtype
== ASL_TYPE_CLIENT
) asl
= (asl_client_t
*)obj
;
1227 level
= eval
& EVAL_LEVEL_MASK
;
1228 if (level
> 7) level
= 7;
1229 lmask
= ASL_FILTER_MASK(level
);
1231 if (sendmsg
== NULL
) {
1232 struct timeval tval
= {0, 0};
1233 const char *sstr
, *fstr
;
1235 status
= gettimeofday(&tval
, NULL
);
1237 time_t tick
= time(NULL
);
1243 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1249 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1253 sendmsg
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, mstr
);
1254 if (sendmsg
== NULL
) {
1255 return ASL_STATUS_FAILED
;
1258 release_sendmsg
= sendmsg
= asl_msg_merge(sendmsg
, msg
);
1261 /* write to file descriptors */
1262 for (uint32_t i
= 0; i
< asl
->out_count
; i
++) {
1264 if ((asl
->out_list
[i
].fd
!= STDOUT_FILENO
) && (asl
->out_list
[i
].fd
!= STDERR_FILENO
)) {
1265 continue; // new logging only support stdout/stderr
1269 if ((asl
->out_list
[i
].fd
>= 0) && (asl
->out_list
[i
].filter
!= 0) && ((asl
->out_list
[i
].filter
& lmask
) != 0)) {
1273 str
= asl_format_message(sendmsg
, asl
->out_list
[i
].mfmt
, asl
->out_list
[i
].tfmt
, asl
->out_list
[i
].encoding
, &len
);
1274 if (str
== NULL
) continue;
1276 status
= write(asl
->out_list
[i
].fd
, str
, len
- 1);
1279 /* soft error for fd 2 (stderr) */
1280 if (asl
->out_list
[i
].fd
== 2) status
= 0;
1281 asl
->out_list
[i
].fd
= -1;
1289 if (release_sendmsg
) {
1290 asl_msg_release(release_sendmsg
);
1297 _asl_send_message(asl_object_t obj
, uint32_t eval
, asl_msg_t
*msg
, const char *mstr
)
1299 uint32_t len
, level
, lmask
, outstatus
, objtype
;
1300 const char *sstr
, *fstr
;
1301 struct timeval tval
= {0, 0};
1303 int use_global_lock
= 0;
1304 kern_return_t kstatus
;
1306 asl_msg_t
*qd_msg
= NULL
;
1307 asl_client_t
*asl
= NULL
;
1308 static dispatch_once_t noquota_once
;
1309 __block
int log_quota_msg
= 0;
1311 if ((eval
& EVAL_ASL
) == 0) return ASL_STATUS_OK
;
1315 asl
= _asl_open_default();
1316 if (asl
== NULL
) return ASL_STATUS_FAILED
;
1317 use_global_lock
= 1;
1318 objtype
= ASL_TYPE_CLIENT
;
1322 objtype
= asl_get_type(obj
);
1323 if (objtype
== ASL_TYPE_CLIENT
) asl
= (asl_client_t
*)obj
;
1326 level
= eval
& EVAL_LEVEL_MASK
;
1327 if (level
> 7) level
= 7;
1328 lmask
= ASL_FILTER_MASK(level
);
1330 if ((objtype
== ASL_TYPE_CLIENT
) && (asl
->aslfile
!= NULL
)) use_global_lock
= 1;
1332 status
= gettimeofday(&tval
, NULL
);
1335 time_t tick
= time(NULL
);
1341 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1342 if (status
!= 0) sstr
= NULL
;
1345 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1346 if (status
!= 0) fstr
= NULL
;
1348 sendmsg
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, mstr
);
1349 if (sendmsg
== NULL
) return ASL_STATUS_FAILED
;
1351 /* Set "ASLOption store" if tunneling */
1352 if (eval
& EVAL_TUNNEL
) _asl_set_option(sendmsg
, ASL_OPT_STORE
);
1356 if (use_global_lock
!= 0) pthread_mutex_lock(&_asl_global
.lock
);
1358 sendmsg
= asl_msg_merge(sendmsg
, msg
);
1360 if (objtype
!= ASL_TYPE_CLIENT
)
1362 asl_append(obj
, (asl_object_t
)sendmsg
);
1363 asl_msg_release(sendmsg
);
1364 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1365 return ASL_STATUS_OK
;
1369 * If there is an aslfile this is a stand-alone file client.
1370 * Just save to the file.
1372 if (asl
->aslfile
!= NULL
)
1374 outstatus
= ASL_STATUS_FAILED
;
1376 if (sendmsg
!= NULL
)
1378 outstatus
= asl_file_save(asl
->aslfile
, sendmsg
, &(asl
->aslfileid
));
1382 asl_msg_release(sendmsg
);
1384 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1393 * Quotas are disabled if:
1394 * - a remote control filter is in place (EVAL_TUNNEL)
1395 * - Environment variable ASL_QUOTA_DISABLED == 1
1396 * - /etc/asl/.noquota existed at the time that the process started
1398 * We just check /etc/asl/.noquota once, since it would be
1399 * expensive to stat() for every log message.
1401 * We only check the Environment variable once, since getenv() is
1402 * not thread safe. If someone is changing the environment,
1406 dispatch_once(&noquota_once
, ^{
1408 memset(&sb
, 0, sizeof(struct stat
));
1410 int save_errno
= errno
;
1412 if (stat(NOQUOTA_FILE_PATH
, &sb
) == 0)
1414 _asl_global
.quota
= UINT32_MAX
;
1418 const char *qtest
= getenv(NOQUOTA_ENV
);
1419 if ((qtest
!= NULL
) && (!strcmp(qtest
, "1")))
1421 _asl_global
.quota
= UINT32_MAX
;
1426 /* reset errno since we want stat() to fail silently */
1430 if (log_quota_msg
!= 0)
1432 qd_msg
= asl_base_msg(asl
, QUOTA_LEVEL
, &tval
, sstr
, fstr
, QUOTA_DISABLED_MSG
);
1433 asl_msg_set_key_val(qd_msg
, ASL_KEY_OPTION
, ASL_OPT_STORE
);
1436 if (((eval
& EVAL_TUNNEL
) == 0) && (_asl_global
.quota
!= UINT32_MAX
))
1438 time_t last_send
= _asl_global
.last_send
;
1439 time_t last_oq
= _asl_global
.last_oq_msg
;
1440 uint32_t qcurr
= _asl_global
.quota
;
1442 uint32_t qinc
, qnew
;
1446 /* add QUOTA_MPS to quota for each second we've been idle */
1447 if (tval
.tv_sec
> last_send
)
1449 delta
= tval
.tv_sec
- last_send
;
1452 if (delta
< (QUOTA_MPH
/ QUOTA_MPS
)) qinc
= delta
* QUOTA_MPS
;
1454 qnew
= MIN(QUOTA_MPH
, qcurr
+ qinc
);
1455 OSAtomicCompareAndSwapLongBarrier(last_send
, tval
.tv_sec
, (long *)&_asl_global
.last_send
);
1460 if ((tval
.tv_sec
- last_oq
) > QUOTA_MSG_INTERVAL
)
1463 OSAtomicCompareAndSwapLongBarrier(last_oq
, tval
.tv_sec
, (long *)&_asl_global
.last_oq_msg
);
1467 eval
&= ~EVAL_SEND_ASL
;
1472 OSAtomicCompareAndSwap32Barrier(qcurr
, qnew
- 1, (int32_t *)&_asl_global
.quota
);
1477 if (eval
& EVAL_MT_SHIM
)
1479 _asl_mt_shim_send_message(sendmsg
);
1483 if ((_asl_global
.server_port
!= MACH_PORT_NULL
) && (eval
& EVAL_SEND_ASL
))
1485 asl_string_t
*send_str
;
1489 if (eval
& EVAL_QUOTA
)
1491 asl_msg_set_key_val(sendmsg
, ASL_KEY_LEVEL
, QUOTA_LEVEL_STR
);
1492 asl_msg_set_key_val(sendmsg
, ASL_KEY_MSG
, QUOTA_MSG
);
1497 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, qd_msg
, "raw");
1498 len
= asl_string_length(send_str
);
1499 vmsize
= asl_string_allocated_size(send_str
);
1500 str
= asl_string_release_return_bytes(send_str
);
1503 kstatus
= _asl_server_message(_asl_global
.server_port
, (caddr_t
)str
, len
);
1504 if (kstatus
!= KERN_SUCCESS
)
1506 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1509 else if (vmsize
!= 0)
1511 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1513 asl_msg_release(qd_msg
);
1516 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, sendmsg
, "raw");
1517 len
= asl_string_length(send_str
);
1519 if (len
> LIBASL_MAX_MSG_SIZE
)
1523 snprintf(tmp
, sizeof(tmp
), SIZE_LIMIT_MSG
, len
, LIBASL_MAX_MSG_SIZE
);
1524 asl_msg_t
*limitmsg
= asl_base_msg(asl
, ASL_LEVEL_CRIT
, &tval
, sstr
, fstr
, tmp
);
1526 asl_string_release(send_str
);
1529 if (limitmsg
!= NULL
)
1531 /* Set "ASLOption store" if tunneling */
1532 if (eval
& EVAL_TUNNEL
) _asl_set_option(limitmsg
, ASL_OPT_STORE
);
1534 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, limitmsg
, "raw");
1535 len
= asl_string_length(send_str
);
1536 asl_msg_release(limitmsg
);
1540 vmsize
= asl_string_allocated_size(send_str
);
1541 str
= asl_string_release_return_bytes(send_str
);
1545 /* send a mach message to syslogd */
1546 kstatus
= _asl_server_message(_asl_global
.server_port
, (caddr_t
)str
, len
);
1547 if (kstatus
!= KERN_SUCCESS
)
1549 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1553 else if (vmsize
>0) vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1556 if ((sendmsg
!= NULL
) && (asl
->out_count
> 0))
1558 status
= _asl_send_message_text(asl
, sendmsg
, obj
, eval
, msg
, mstr
, false);
1561 asl_msg_release(sendmsg
);
1563 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1569 os_log_with_args_wrapper(void *addr
, int level
, const char *format
, ...)
1572 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
1573 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
1574 os_log_type_t type
= shim_asl_to_log_type
[level
];
1576 va_start(ap
, format
);
1577 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
1582 * asl_send: send a message
1583 * This routine may be used instead of asl_log() or asl_vlog() if asl_set()
1584 * has been used to set all of a message's attributes.
1585 * eval: hints about what to do with the message
1586 * msg: an asl message
1587 * returns 0 for success, non-zero for failure
1589 __private_extern__ ASL_STATUS
1590 asl_client_internal_send(asl_object_t obj
, asl_object_t msg
, void *addr
)
1592 int status
= ASL_STATUS_OK
;
1593 uint32_t eval
= _asl_evaluate_send(obj
, msg
, -1);
1595 const char *message
= asl_msg_get_val_for_key((asl_msg_t
*)msg
, ASL_KEY_MSG
);
1597 if ((eval
& EVAL_SEND_TRACE
) && message
&& message
[0] && os_log_shim_enabled(addr
))
1599 int level
= ASL_LEVEL_DEBUG
;
1600 const char *lval
= asl_msg_get_val_for_key((asl_msg_t
*)msg
, ASL_KEY_LEVEL
);
1601 if (lval
!= NULL
) level
= atoi(lval
);
1604 * If the return address and the format string are from different
1605 * binaries, os_log_with_args will not record the return address.
1606 * Work around this by passing a dynamic format string.
1608 char dynamic_format
[] = "%s";
1609 os_log_with_args_wrapper(addr
, level
, dynamic_format
, message
);
1611 if (eval
& EVAL_TEXT_FILE
)
1613 status
= _asl_send_message_text(NULL
, NULL
, obj
, eval
, (asl_msg_t
*)msg
, NULL
, true);
1616 else if (eval
& EVAL_ASL
)
1618 status
= _asl_send_message(obj
, eval
, (asl_msg_t
*)msg
, NULL
);
1625 #pragma mark auxiliary files and URLs
1628 _asl_aux_save_context(asl_aux_context_t
*ctx
)
1630 if (ctx
== NULL
) return ASL_STATUS_FAILED
;
1632 pthread_mutex_lock(&_asl_global
.lock
);
1634 _asl_global
.aux_ctx
= (asl_aux_context_t
**)reallocf(_asl_global
.aux_ctx
, (_asl_global
.aux_count
+ 1) * sizeof(asl_aux_context_t
*));
1635 if (_asl_global
.aux_ctx
== NULL
)
1637 _asl_global
.aux_count
= 0;
1638 pthread_mutex_unlock(&_asl_global
.lock
);
1639 return ASL_STATUS_FAILED
;
1642 _asl_global
.aux_ctx
[_asl_global
.aux_count
++] = ctx
;
1644 pthread_mutex_unlock(&_asl_global
.lock
);
1646 return ASL_STATUS_OK
;
1650 * Creates an auxiliary file that may be used to save arbitrary data. The ASL message msg
1651 * will be saved at the time that the auxiliary file is created. The message will include
1652 * any keys and values found in msg, and it will include the title and Uniform Type
1653 * Identifier specified. Output parameter out_fd will contain the file descriptor of the
1654 * new auxiliary file.
1657 _asl_auxiliary(asl_msg_t
*msg
, const char *title
, const char *uti
, const char *url
, int *out_fd
)
1660 asl_string_t
*send_str
;
1662 fileport_t fileport
;
1663 kern_return_t kstatus
;
1665 uint32_t newurllen
, where
;
1666 int status
, fd
, fdpair
[2];
1668 dispatch_queue_t pipe_q
;
1669 dispatch_io_t pipe_channel
;
1670 dispatch_semaphore_t sem
;
1672 aux
= asl_msg_new(ASL_TYPE_MSG
);
1674 if (url
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_URL
, url
);
1675 if (title
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_TITLE
, title
);
1676 if (uti
== NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_UTI
, "public.data");
1677 else asl_msg_set_key_val(aux
, ASL_KEY_AUX_UTI
, uti
);
1679 aux
= asl_msg_merge(aux
, msg
);
1681 /* if (out_fd == NULL), this is from asl_log_auxiliary_location */
1684 uint32_t eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux
, -1);
1685 status
= _asl_send_message(NULL
, eval
, aux
, NULL
);
1686 asl_msg_release(aux
);
1690 where
= asl_store_location();
1691 if (where
== ASL_STORE_LOCATION_MEMORY
)
1694 asl_aux_context_t
*ctx
= (asl_aux_context_t
*)calloc(1, sizeof(asl_aux_context_t
));
1695 if (ctx
== NULL
) return ASL_STATUS_FAILED
;
1697 status
= pipe(fdpair
);
1701 return ASL_STATUS_FAILED
;
1704 /* give read end to dispatch_io_read */
1706 sem
= dispatch_semaphore_create(0);
1708 ctx
->fd
= fdpair
[1];
1710 status
= _asl_aux_save_context(ctx
);
1711 if (status
!= ASL_STATUS_OK
)
1715 dispatch_release(sem
);
1717 return ASL_STATUS_FAILED
;
1720 pipe_q
= dispatch_queue_create("ASL_AUX_PIPE_Q", NULL
);
1721 pipe_channel
= dispatch_io_create(DISPATCH_IO_STREAM
, fd
, pipe_q
, ^(int err
){
1725 *out_fd
= fdpair
[1];
1727 dispatch_io_set_low_water(pipe_channel
, SIZE_MAX
);
1729 dispatch_io_read(pipe_channel
, 0, SIZE_MAX
, pipe_q
, ^(bool done
, dispatch_data_t pipedata
, int err
){
1732 size_t len
= dispatch_data_get_size(pipedata
);
1735 const char *bytes
= NULL
;
1739 dispatch_data_t md
= dispatch_data_create_map(pipedata
, (const void **)&bytes
, &len
);
1740 encoded
= asl_core_encode_buffer(bytes
, len
);
1741 asl_msg_set_key_val(aux
, ASL_KEY_AUX_DATA
, encoded
);
1743 eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux
, -1);
1744 _asl_send_message(NULL
, eval
, aux
, NULL
);
1745 asl_msg_release(aux
);
1746 dispatch_release(md
);
1752 dispatch_semaphore_signal(sem
);
1753 dispatch_release(pipe_channel
);
1754 dispatch_release(pipe_q
);
1758 return ASL_STATUS_OK
;
1762 if (_asl_global
.server_port
== MACH_PORT_NULL
) return ASL_STATUS_FAILED
;
1764 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, aux
, "raw");
1765 len
= asl_string_length(send_str
);
1766 vmsize
= asl_string_allocated_size(send_str
);
1767 str
= asl_string_release_return_bytes(send_str
);
1771 asl_msg_release(aux
);
1772 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1773 return ASL_STATUS_FAILED
;
1777 fileport
= MACH_PORT_NULL
;
1778 status
= KERN_SUCCESS
;
1780 kstatus
= _asl_server_create_aux_link(_asl_global
.server_port
, (caddr_t
)str
, len
, &fileport
, &newurl
, &newurllen
, &status
);
1781 if (kstatus
!= KERN_SUCCESS
)
1783 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1784 asl_msg_release(aux
);
1785 return ASL_STATUS_FAILED
;
1790 asl_msg_release(aux
);
1796 asl_msg_set_key_val(aux
, ASL_KEY_AUX_URL
, newurl
);
1797 vm_deallocate(mach_task_self(), (vm_address_t
)newurl
, newurllen
);
1800 if (fileport
== MACH_PORT_NULL
)
1802 asl_msg_release(aux
);
1803 return ASL_STATUS_FAILED
;
1806 fd
= fileport_makefd(fileport
);
1807 mach_port_deallocate(mach_task_self(), fileport
);
1810 asl_msg_release(aux
);
1815 asl_aux_context_t
*ctx
= (asl_aux_context_t
*)calloc(1, sizeof(asl_aux_context_t
));
1827 status
= _asl_aux_save_context(ctx
);
1835 asl_create_auxiliary_file(asl_object_t msg
, const char *title
, const char *uti
, int *out_fd
)
1837 if (out_fd
== NULL
) return -1;
1839 ASL_STATUS status
= _asl_auxiliary((asl_msg_t
*)msg
, title
, uti
, NULL
, out_fd
);
1840 return (status
== ASL_STATUS_OK
) ? 0 : -1;
1844 asl_log_auxiliary_location(asl_object_t msg
, const char *title
, const char *uti
, const char *url
)
1846 ASL_STATUS status
= _asl_auxiliary((asl_msg_t
*)msg
, title
, uti
, url
, NULL
);
1847 return (status
== ASL_STATUS_OK
) ? 0 : -1;
1851 * Close an auxiliary file.
1852 * Sends the cached auxiliary message to syslogd.
1853 * Returns 0 on success, -1 on error.
1856 asl_close_auxiliary_file(int fd
)
1860 dispatch_semaphore_t aux_sem
= NULL
;
1862 pthread_mutex_lock(&(_asl_global
.lock
));
1867 for (i
= 0; i
< _asl_global
.aux_count
; i
++)
1869 if (_asl_global
.aux_ctx
[i
]->fd
== fd
)
1873 aux_msg
= _asl_global
.aux_ctx
[i
]->msg
;
1874 aux_sem
= _asl_global
.aux_ctx
[i
]->sem
;
1876 free(_asl_global
.aux_ctx
[i
]);
1878 for (j
= i
+ 1; j
< _asl_global
.aux_count
; i
++, j
++)
1880 _asl_global
.aux_ctx
[i
] = _asl_global
.aux_ctx
[j
];
1883 _asl_global
.aux_count
--;
1885 if (_asl_global
.aux_count
== 0)
1887 free(_asl_global
.aux_ctx
);
1888 _asl_global
.aux_ctx
= NULL
;
1892 _asl_global
.aux_ctx
= (asl_aux_context_t
**)reallocf(_asl_global
.aux_ctx
, _asl_global
.aux_count
* sizeof(asl_aux_context_t
*));
1893 if (_asl_global
.aux_ctx
== NULL
)
1895 _asl_global
.aux_count
= 0;
1904 pthread_mutex_unlock(&(_asl_global
.lock
));
1908 if (aux_msg
!= NULL
)
1910 uint32_t eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux_msg
, -1);
1911 if (_asl_send_message(NULL
, eval
, aux_msg
, NULL
) != ASL_STATUS_OK
) status
= -1;
1912 asl_msg_release(aux_msg
);
1915 if (aux_sem
!= NULL
)
1917 dispatch_semaphore_wait(aux_sem
, DISPATCH_TIME_FOREVER
);
1918 dispatch_release(aux_sem
);
1927 _asl_server_control_query(void)
1929 asl_msg_list_t
*list
= NULL
;
1931 uint32_t len
, reslen
, status
;
1932 uint64_t cmax
, qmin
;
1933 kern_return_t kstatus
;
1935 asl_msg_t
*m
= NULL
;
1936 static const char ctlstr
[] = "1\nQ [= ASLOption control]\n";
1939 if (_asl_global
.server_port
== MACH_PORT_NULL
) return NULL
;
1941 len
= strlen(ctlstr
) + 1;
1948 kstatus
= vm_allocate(mach_task_self(), (vm_address_t
*)&vmstr
, len
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_MEMORY_ASL
));
1949 if (kstatus
!= KERN_SUCCESS
) return NULL
;
1951 memmove(vmstr
, ctlstr
, len
);
1954 kstatus
= _asl_server_query_2(_asl_global
.server_port
, vmstr
, len
, qmin
, FETCH_BATCH
, 0, (caddr_t
*)&res
, &reslen
, &cmax
, (int *)&status
);
1955 if (kstatus
!= KERN_SUCCESS
) return NULL
;
1957 list
= asl_msg_list_from_string(res
);
1958 vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1960 if (list
== NULL
) return NULL
;
1961 if (list
->count
> 0) m
= asl_msg_retain(list
->msg
[0]);
1962 asl_msg_list_release(list
);
1967 * Returns ASL_STORE_LOCATION_FILE or ASL_STORE_LOCATION_MEMORY
1970 asl_store_location()
1972 kern_return_t kstatus
;
1974 uint32_t reslen
, status
;
1978 if (_asl_global
.server_port
== MACH_PORT_NULL
) return ASL_STORE_LOCATION_FILE
;
1983 status
= ASL_STATUS_OK
;
1985 kstatus
= _asl_server_query_2(_asl_global
.server_port
, NULL
, 0, 0, -1, 0, (caddr_t
*)&res
, &reslen
, &cmax
, (int *)&status
);
1986 if (kstatus
!= KERN_SUCCESS
) return ASL_STORE_LOCATION_FILE
;
1988 /* res should never be returned, but just to be certain we don't leak VM ... */
1989 if (res
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1991 if (status
== ASL_STATUS_OK
) return ASL_STORE_LOCATION_MEMORY
;
1992 return ASL_STORE_LOCATION_FILE
;
1996 asl_open_path(const char *path
, uint32_t opts
)
1999 asl_file_t
*fout
= NULL
;
2000 asl_store_t
*sout
= NULL
;
2002 if (opts
== 0) opts
= ASL_OPT_OPEN_READ
;
2004 if (opts
& ASL_OPT_OPEN_READ
)
2008 if (asl_store_open_read(ASL_PLACE_DATABASE_DEFAULT
, &sout
) != ASL_STATUS_OK
) return NULL
;
2009 return (asl_object_t
)sout
;
2012 memset(&sb
, 0, sizeof(struct stat
));
2013 if (stat(path
, &sb
) < 0) return NULL
;
2015 if (sb
.st_mode
& S_IFREG
)
2017 if (asl_file_open_read(path
, &fout
) != ASL_STATUS_OK
) return NULL
;
2018 return (asl_object_t
)fout
;
2020 else if (sb
.st_mode
& S_IFDIR
)
2022 if (asl_store_open_read(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2023 return (asl_object_t
)sout
;
2028 else if (opts
& ASL_OPT_OPEN_WRITE
)
2030 if (path
== NULL
) return NULL
;
2032 memset(&sb
, 0, sizeof(struct stat
));
2033 if (stat(path
, &sb
) < 0)
2035 if (errno
!= ENOENT
) return NULL
;
2037 if (opts
& ASL_OPT_CREATE_STORE
)
2039 if (asl_store_open_write(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2040 return (asl_object_t
)sout
;
2044 if (asl_file_open_write(path
, 0644, geteuid(), getegid(), &fout
) != ASL_STATUS_OK
) return NULL
;
2045 return (asl_object_t
)fout
;
2048 else if (sb
.st_mode
& S_IFREG
)
2050 if (asl_file_open_write(path
, 0644, geteuid(), getegid(), &fout
) != ASL_STATUS_OK
) return NULL
;
2051 return (asl_object_t
)fout
;
2053 else if (sb
.st_mode
& S_IFDIR
)
2055 if (asl_store_open_write(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2056 return (asl_object_t
)sout
;