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 kern_return_t kstatus
= bootstrap_look_up2(bootstrap_port
, ASL_SERVICE_NAME
, &_asl_global
.server_port
, 0, BOOTSTRAP_PRIVILEGED_SERVER
);
272 if (kstatus
!= KERN_SUCCESS
)
274 _asl_global
.server_port
= MACH_PORT_NULL
;
281 asl_core_get_service_port(__unused
int reset
)
284 return _asl_global
.server_port
;
288 #pragma mark asl_client
291 asl_open(const char *ident
, const char *facility
, uint32_t opts
)
293 asl_client_t
*asl
= asl_client_open(ident
, facility
, opts
);
294 if (asl
== NULL
) return NULL
;
296 if (!(opts
& ASL_OPT_NO_REMOTE
)) _asl_notify_open(1);
298 return (asl_object_t
)asl
;
302 asl_open_from_file(int fd
, const char *ident
, const char *facility
)
304 return (asl_object_t
)asl_client_open_from_file(fd
, ident
, facility
);
308 asl_close(asl_object_t obj
)
313 __private_extern__ asl_client_t
*
316 static dispatch_once_t once
;
318 dispatch_once(&once
, ^{
320 * Do a sleight-of-hand with ASL_OPT_NO_REMOTE to avoid a deadlock
321 * since asl_open(xxx, yyy, 0) calls _asl_notify_open(1)
322 * which locks _asl_global.lock.
324 _asl_global
.asl
= (asl_client_t
*)asl_open(NULL
, NULL
, ASL_OPT_NO_REMOTE
);
326 /* Reset options to clear ASL_OPT_NO_REMOTE bit */
327 if (_asl_global
.asl
!= NULL
) _asl_global
.asl
->options
= 0;
329 /* Now call _asl_notify_open(0) to finish the work */
333 return _asl_global
.asl
;
337 * asl_add_file: write log messages to the given file descriptor
338 * Log messages will be written to this file as well as to the server.
341 asl_add_output_file(asl_object_t client
, int fd
, const char *mfmt
, const char *tfmt
, int filter
, int text_encoding
)
343 int status
, use_global_lock
= 0;
346 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
348 asl
= (asl_client_t
*)client
;
351 asl
= _asl_open_default();
352 if (asl
== NULL
) return -1;
353 pthread_mutex_lock(&_asl_global
.lock
);
357 status
= asl_client_add_output_file(asl
, fd
, mfmt
, tfmt
, filter
, text_encoding
);
359 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
360 return (status
== ASL_STATUS_OK
) ? 0 : -1;
363 /* returns previous filter value or -1 on error */
365 asl_set_output_file_filter(asl_object_t client
, int fd
, int filter
)
368 int use_global_lock
= 0;
371 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
373 asl
= (asl_client_t
*)client
;
376 asl
= _asl_open_default();
377 if (asl
== NULL
) return -1;
378 pthread_mutex_lock(&_asl_global
.lock
);
382 last
= asl_client_set_output_file_filter(asl
, fd
, filter
);
384 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
388 /* SPI - Deprecated */
390 asl_add_output(asl_object_t client
, int fd
, const char *mfmt
, const char *tfmt
, uint32_t text_encoding
)
392 return asl_add_output_file(client
, fd
, mfmt
, tfmt
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
), text_encoding
);
395 /* SPI - Deprecated */
397 asl_add_log_file(asl_object_t client
, int fd
)
399 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
);
403 * asl_remove_output: stop writing log messages to the given file descriptor
406 asl_remove_output_file(asl_object_t client
, int fd
)
408 int status
, use_global_lock
= 0;
411 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
413 asl
= (asl_client_t
*)client
;
416 asl
= _asl_open_default();
417 if (asl
== NULL
) return -1;
418 pthread_mutex_lock(&_asl_global
.lock
);
422 status
= asl_client_remove_output_file(asl
, fd
);
424 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
425 return (status
== ASL_STATUS_OK
) ? 0 : -1;
429 asl_remove_output(asl_object_t client
, int fd
)
431 return asl_remove_output_file(client
, fd
);
435 asl_remove_log_file(asl_object_t client
, int fd
)
437 return asl_remove_output_file(client
, fd
);
440 /* returns previous filter value or -1 on error */
442 asl_set_filter(asl_object_t client
, int f
)
444 int last
, use_global_lock
= 0;
447 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
449 asl
= (asl_client_t
*)client
;
452 asl
= _asl_open_default();
453 if (asl
== NULL
) return -1;
454 pthread_mutex_lock(&_asl_global
.lock
);
458 last
= asl_client_set_filter(asl
, f
);
460 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
466 #pragma mark message sending
469 * Evaluate client / message / level to determine what to do with a message.
470 * Checks filters, tunneling, and log files.
471 * Returns the bits below, ORed with the message level.
473 * EVAL_SEND_ASL - send this message to syslogd
474 * EVAL_SEND_TRACE - log this message with Activity Tracing
475 * EVAL_ASL_FILE - write to a standalone ASL file (see asl_open_from_file)
476 * EVAL_TEXT_FILE - write this message to a text file / stderr
477 * EVAL_TUNNEL - tunneling enabled when sending to syslogd
480 _asl_evaluate_send(asl_object_t client
, asl_object_t m
, int slevel
)
483 asl_msg_t
*msg
= (asl_msg_t
*)m
;
484 uint32_t level
, lmask
, filter
, status
, tunnel
;
490 level
= ASL_LEVEL_DEBUG
;
491 if (slevel
>= 0) level
= slevel
;
494 if ((asl_msg_lookup(msg
, ASL_KEY_LEVEL
, &val
, NULL
) == 0) && (val
!= NULL
)) level
= atoi(val
);
496 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
497 else if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
499 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
))
501 /* sending to something other than a client */
502 return EVAL_ACTIVE
| EVAL_SEND_ASL
| level
;
505 asl
= (asl_client_t
*)client
;
506 if ((asl
!= NULL
) && (asl
->aslfile
!= NULL
)) return (EVAL_ASL_FILE
| level
);
508 if (asl
== NULL
) asl
= _asl_open_default();
511 eval
= EVAL_ACTIVE
| EVAL_DEFAULT_ACTION
| level
;
512 eval
&= ~EVAL_SEND_ASL
;
516 eval
= (asl_client_get_control(asl
) & EVAL_ACTION_MASK
) | level
;
518 filter
= asl
->filter
& 0xff;
519 tunnel
= (asl
->filter
& ASL_FILTER_MASK_TUNNEL
) >> 8;
520 if (tunnel
!= 0) eval
|= EVAL_TUNNEL
;
522 if ((asl
->options
& ASL_OPT_NO_REMOTE
) == 0)
524 pthread_mutex_lock(&_asl_global
.lock
);
526 if (_asl_global
.rc_change_token
>= 0)
528 /* initialize or re-check process-specific and master filters */
530 status
= notify_check(_asl_global
.rc_change_token
, &check
);
531 if ((status
== NOTIFY_STATUS_OK
) && (check
!= 0))
533 if (_asl_global
.master_token
>= 0)
536 status
= notify_get_state(_asl_global
.master_token
, &v64
);
537 if (status
== NOTIFY_STATUS_OK
) _asl_global
.master_filter
= v64
;
540 if (_asl_global
.notify_token
>= 0)
543 status
= notify_get_state(_asl_global
.notify_token
, &v64
);
544 if (status
== NOTIFY_STATUS_OK
) _asl_global
.proc_filter
= v64
;
549 if (_asl_global
.master_filter
& EVAL_ACTIVE
)
551 /* clear bits and set according to master */
552 eval
&= ~(EVAL_SEND_ASL
| EVAL_SEND_TRACE
);
553 eval
|= (_asl_global
.master_filter
& (EVAL_SEND_ASL
| EVAL_SEND_TRACE
));
556 if ((_asl_global
.master_filter
& EVAL_LEVEL_MASK
) != 0) filter
= _asl_global
.proc_filter
& EVAL_LEVEL_MASK
;
559 if (_asl_global
.proc_filter
& EVAL_ACTIVE
)
561 /* clear bits and set according to proc */
562 eval
&= ~(EVAL_SEND_ASL
| EVAL_SEND_TRACE
| EVAL_TEXT_FILE
);
563 eval
|= (_asl_global
.proc_filter
& (EVAL_SEND_ASL
| EVAL_SEND_TRACE
| EVAL_TEXT_FILE
));
566 if ((_asl_global
.proc_filter
& EVAL_LEVEL_MASK
) != 0) filter
= _asl_global
.proc_filter
& EVAL_LEVEL_MASK
;
569 pthread_mutex_unlock(&_asl_global
.lock
);
572 lmask
= ASL_FILTER_MASK(level
);
573 if ((filter
!= 0) && ((filter
& lmask
) == 0)) eval
&= ~EVAL_SEND_ASL
;
574 if (asl
->out_count
> 0) eval
|= EVAL_TEXT_FILE
;
576 /* don't send MessageTracer messages to Activity Tracing */
578 if ((asl_msg_lookup(msg
, ASL_KEY_MESSAGETRACER
, &val
, NULL
) == 0) && (val
!= NULL
))
580 eval
&= ~EVAL_SEND_TRACE
;
581 eval
|= EVAL_MT_SHIM
;
584 /* don't send PowerManagement messages to Activity Tracing */
586 if ((asl_msg_lookup(msg
, ASL_KEY_POWERMANAGEMENT
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
588 /* don't send control messages to Activity Tracing */
590 if ((asl_msg_lookup(msg
, ASL_KEY_OPTION
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
592 /* don't send CFLog messages to Activity Tracing */
594 if ((asl_msg_lookup(msg
, ASL_KEY_CFLOG_LOCAL_TIME
, &val
, NULL
) == 0) && (val
!= NULL
)) eval
&= ~EVAL_SEND_TRACE
;
597 if (((asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &val
, NULL
) == 0) && (val
!= NULL
)) ||
598 ((asl_msg_lookup(asl
->kvdict
, ASL_KEY_FACILITY
, &val
, NULL
) == 0) && (val
!= NULL
)))
600 /* don't send lastlog/utmp messages to Activity Tracing */
601 if (!strcmp(val
, FACILITY_LASTLOG
) || !strcmp(val
, FACILITY_UTMPX
)) eval
&= ~EVAL_SEND_TRACE
;
603 /* don't send LOG_INSTALL messages to Activity Tracing */
604 if (!strcmp(val
, asl_syslog_faciliy_num_to_name(LOG_INSTALL
))) eval
&= ~EVAL_SEND_TRACE
;
612 * Internal routine used by asl_vlog.
613 * msg: an asl messsage
614 * eval: log level and send flags for the message
615 * format: A formating string
616 * ap: va_list for the format
617 * returns 0 for success, non-zero for failure
620 __private_extern__ ASL_STATUS
621 _asl_lib_vlog_text(asl_object_t obj
, uint32_t eval
, asl_object_t msg
, const char *format
, va_list ap
)
623 int saved_errno
= errno
;
625 char *str
, *fmt
, estr
[NL_TEXTMAX
];
626 uint32_t i
, len
, elen
, expand
;
628 if (format
== NULL
) return ASL_STATUS_INVALID_ARG
;
630 /* insert strerror for %m */
635 for (i
= 0; format
[i
] != '\0'; i
++)
637 if (format
[i
] == '%')
639 if (format
[i
+1] == '\0') len
++;
640 else if (format
[i
+1] == 'm')
643 strerror_r(saved_errno
, estr
, sizeof(estr
));
657 fmt
= (char *)format
;
661 fmt
= malloc(len
+ 1);
662 if (fmt
== NULL
) return ASL_STATUS_NO_MEMORY
;
666 for (i
= 0; format
[i
] != '\0'; i
++)
668 if (format
[i
] == '%')
670 if (format
[i
+1] == '\0')
673 else if ((format
[i
+1] == 'm') && (elen
!= 0))
675 memcpy(fmt
+len
, estr
, elen
);
681 fmt
[len
++] = format
[i
++];
682 fmt
[len
++] = format
[i
];
685 else fmt
[len
++] = format
[i
];
692 vasprintf(&str
, fmt
, ap
);
693 if (expand
!= 0) free(fmt
);
695 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
697 status
= _asl_send_message_text(NULL
, NULL
, obj
, eval
, (asl_msg_t
*)msg
, str
, true);
705 * Internal routine used by asl_vlog.
706 * msg: an asl messsage
707 * eval: log level and send flags for the message
708 * format: A formating string
709 * ap: va_list for the format
710 * returns 0 for success, non-zero for failure
712 __private_extern__ ASL_STATUS
713 _asl_lib_vlog(asl_object_t obj
, uint32_t eval
, asl_object_t msg
, const char *format
, va_list ap
)
715 int saved_errno
= errno
;
717 char *str
, *fmt
, estr
[NL_TEXTMAX
];
718 uint32_t i
, len
, elen
, expand
;
720 if (format
== NULL
) return ASL_STATUS_INVALID_ARG
;
722 /* insert strerror for %m */
727 for (i
= 0; format
[i
] != '\0'; i
++)
729 if (format
[i
] == '%')
731 if (format
[i
+1] == '\0') len
++;
732 else if (format
[i
+1] == 'm')
735 strerror_r(saved_errno
, estr
, sizeof(estr
));
749 fmt
= (char *)format
;
753 fmt
= malloc(len
+ 1);
754 if (fmt
== NULL
) return ASL_STATUS_NO_MEMORY
;
758 for (i
= 0; format
[i
] != '\0'; i
++)
760 if (format
[i
] == '%')
762 if (format
[i
+1] == '\0')
765 else if ((format
[i
+1] == 'm') && (elen
!= 0))
767 memcpy(fmt
+len
, estr
, elen
);
773 fmt
[len
++] = format
[i
++];
774 fmt
[len
++] = format
[i
];
777 else fmt
[len
++] = format
[i
];
784 vasprintf(&str
, fmt
, ap
);
785 if (expand
!= 0) free(fmt
);
787 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
789 status
= _asl_send_message(obj
, eval
, (asl_msg_t
*)msg
, str
);
797 * Similar to asl_log, but take a va_list instead of a list of arguments.
798 * msg: an asl message
799 * level: the log level of the associated message
800 * format: A formating string
801 * ap: va_list for the format
802 * returns 0 for success, non-zero for failure
805 asl_vlog(asl_object_t client
, asl_object_t msg
, int level
, const char *format
, va_list ap
)
807 ASL_STATUS status
= ASL_STATUS_OK
;
808 uint32_t eval
= _asl_evaluate_send(client
, msg
, level
);
809 void *addr
= __builtin_return_address(0);
811 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
814 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
815 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
816 os_log_type_t type
= shim_asl_to_log_type
[level
];
818 va_copy(ap_copy
, ap
);
819 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap_copy
, addr
);
822 if (eval
& EVAL_TEXT_FILE
)
824 status
= _asl_lib_vlog_text(client
, eval
, msg
, format
, ap
);
827 else if (eval
& EVAL_ASL
)
829 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
832 return (status
== ASL_STATUS_OK
) ? 0 : -1;
837 * SPI used by legacy (non-shim) ASL_PREFILTER_LOG. Converts format arguments to a va_list and
838 * forwards the call to _asl_lib_vlog.
839 * msg: an asl message
840 * eval: log level and send flags for the message
841 * format: A formating string
842 * ... args for format
843 * returns 0 for success, non-zero for failure
846 _asl_lib_log(asl_object_t client
, uint32_t eval
, asl_object_t msg
, const char *format
, ...)
854 va_start(ap
, format
);
855 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
864 * Processes an ASL log message.
865 * msg: an asl message
866 * level: the log level of the associated message
867 * format: A formating string
868 * ... args for format
869 * returns 0 for success, non-zero for failure
872 asl_log(asl_object_t client
, asl_object_t msg
, int level
, const char *format
, ...)
874 ASL_STATUS status
= ASL_STATUS_OK
;
875 uint32_t eval
= _asl_evaluate_send(client
, msg
, level
);
876 void *addr
= __builtin_return_address(0);
878 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
881 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
882 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
883 os_log_type_t type
= shim_asl_to_log_type
[level
];
885 va_start(ap
, format
);
886 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
889 if (eval
& EVAL_TEXT_FILE
)
892 va_start(ap
, format
);
893 status
= _asl_lib_vlog_text(client
, eval
, msg
, format
, ap
);
897 else if (eval
& EVAL_ASL
)
900 va_start(ap
, format
);
901 status
= _asl_lib_vlog(client
, eval
, msg
, format
, ap
);
905 return (status
== ASL_STATUS_OK
) ? 0 : -1;
910 * Like asl_log, supplies NULL client and msg.
911 * level: the log level of the associated message
912 * format: A formating string
913 * ... args for format
914 * returns 0 for success, non-zero for failure
917 asl_log_message(int level
, const char *format
, ...)
919 ASL_STATUS status
= ASL_STATUS_OK
;
920 uint32_t eval
= _asl_evaluate_send(NULL
, NULL
, level
);
921 void *addr
= __builtin_return_address(0);
923 if ((eval
& EVAL_SEND_TRACE
) && os_log_shim_enabled(addr
))
926 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
927 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
928 os_log_type_t type
= shim_asl_to_log_type
[level
];
930 va_start(ap
, format
);
931 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
934 if (eval
& EVAL_TEXT_FILE
)
937 va_start(ap
, format
);
938 status
= _asl_lib_vlog_text(NULL
, eval
, NULL
, format
, ap
);
942 else if (eval
& EVAL_ASL
)
945 va_start(ap
, format
);
946 status
= _asl_lib_vlog(NULL
, eval
, NULL
, format
, ap
);
950 return (status
== ASL_STATUS_OK
) ? 0 : -1;
954 * asl_get_filter: gets the values for the local, master, and remote filters,
955 * and indicates which one is active.
958 asl_get_filter(asl_object_t client
, int *local
, int *master
, int *remote
, int *active
)
960 asl_client_t
*asl
, *asl_default
;
965 if ((client
!= NULL
) && (asl_get_type(client
) != ASL_TYPE_CLIENT
)) return -1;
972 asl_default
= _asl_open_default();
974 asl
= (asl_client_t
*)client
;
975 if (asl
== NULL
) asl
= asl_default
;
976 if (asl
!= NULL
) l
= asl
->filter
& EVAL_LEVEL_MASK
;
978 if ((asl_default
!= NULL
) && (!(asl_default
->options
& ASL_OPT_NO_REMOTE
)))
980 pthread_mutex_lock(&_asl_global
.lock
);
982 if (_asl_global
.rc_change_token
>= 0)
984 /* initialize or re-check process-specific and master filters */
986 status
= notify_check(_asl_global
.rc_change_token
, &check
);
987 if ((status
== NOTIFY_STATUS_OK
) && (check
!= 0))
989 if (_asl_global
.master_token
>= 0)
992 status
= notify_get_state(_asl_global
.master_token
, &v64
);
993 if (status
== NOTIFY_STATUS_OK
) _asl_global
.master_filter
= v64
;
996 if (_asl_global
.notify_token
>= 0)
999 status
= notify_get_state(_asl_global
.notify_token
, &v64
);
1000 if (status
== NOTIFY_STATUS_OK
) _asl_global
.proc_filter
= v64
;
1005 m
= _asl_global
.master_filter
;
1008 r
= _asl_global
.proc_filter
;
1011 pthread_mutex_unlock(&_asl_global
.lock
);
1014 if (local
!= NULL
) *local
= l
;
1015 if (master
!= NULL
) *master
= m
;
1016 if (remote
!= NULL
) *remote
= r
;
1017 if (active
!= NULL
) *active
= x
;
1022 /* SPI for SHIM control */
1024 asl_set_local_control(asl_object_t client
, uint32_t filter
)
1026 asl_client_t
*asl
= (asl_client_t
*)client
;
1029 asl
= _asl_open_default();
1030 if (asl
== NULL
) return UINT32_MAX
;
1032 else if (asl_get_type(client
) != ASL_TYPE_CLIENT
) return UINT32_MAX
;
1034 return asl_client_set_control(asl
, filter
);
1038 asl_get_local_control(asl_object_t client
)
1040 asl_client_t
*asl
= (asl_client_t
*)client
;
1043 asl
= _asl_open_default();
1044 if (asl
== NULL
) return UINT32_MAX
;
1046 else if (asl_get_type(client
) != ASL_TYPE_CLIENT
) return UINT32_MAX
;
1048 return asl_client_get_control(asl
);
1052 * Sets PID and OSActivityID values in a new message.
1053 * Also sets Level, Time, TimeNanoSec, Sender, Facility and Message if provided.
1056 asl_base_msg(asl_client_t
*asl
, uint32_t level
, const struct timeval
*tv
, const char *sstr
, const char *fstr
, const char *mstr
)
1061 os_activity_id_t osaid
;
1063 aux
= asl_msg_new(ASL_TYPE_MSG
);
1064 if (aux
== NULL
) return NULL
;
1067 if (level
<= 7) asl_msg_set_key_val(aux
, ASL_KEY_LEVEL
, level_to_number_string
[level
]);
1069 /* Time and TimeNanoSec */
1072 snprintf(aux_val
, sizeof(aux_val
), "%llu", (unsigned long long) tv
->tv_sec
);
1073 asl_msg_set_key_val(aux
, ASL_KEY_TIME
, aux_val
);
1075 snprintf(aux_val
, sizeof(aux_val
), "%d", tv
->tv_usec
* 1000);
1076 asl_msg_set_key_val(aux
, ASL_KEY_TIME_NSEC
, aux_val
);
1080 if (mstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_MSG
, mstr
);
1083 snprintf(aux_val
, sizeof(aux_val
), "%u", getpid());
1084 asl_msg_set_key_val(aux
, ASL_KEY_PID
, aux_val
);
1087 osaid
= os_activity_get_identifier(OS_ACTIVITY_CURRENT
, NULL
);
1090 snprintf(aux_val
, sizeof(aux_val
), "0x%016llx", osaid
);
1091 asl_msg_set_key_val(aux
, ASL_KEY_OS_ACTIVITY_ID
, aux_val
);
1095 if ((sstr
== NULL
) && (asl
!= NULL
))
1097 /* See if the client has a value for ASL_KEY_SENDER */
1098 status
= asl_msg_lookup((asl_msg_t
*)asl
->kvdict
, ASL_KEY_SENDER
, &sstr
, NULL
);
1099 if ((status
!= 0) || (sstr
== NULL
))
1103 /* See if the global cache has a value for ASL_KEY_SENDER */
1104 if (_asl_global
.sender
== NULL
)
1106 /* Get the process name with _NSGetArgv */
1107 char *name
= *(*_NSGetArgv());
1110 char *x
= strrchr(name
, '/');
1114 /* Set the cache value */
1115 pthread_mutex_lock(&_asl_global
.lock
);
1116 if (_asl_global
.sender
== NULL
) _asl_global
.sender
= strdup(x
);
1117 pthread_mutex_unlock(&_asl_global
.lock
);
1121 if (_asl_global
.sender
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, _asl_global
.sender
);
1122 else asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, "Unknown");
1126 if (sstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_SENDER
, sstr
);
1129 if ((fstr
== NULL
) && (asl
!= NULL
))
1131 status
= asl_msg_lookup((asl_msg_t
*)asl
->kvdict
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1132 if (status
!= 0) fstr
= NULL
;
1135 if (fstr
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_FACILITY
, fstr
);
1142 * Possibly useful someday...
1145 asl_prepared_message(asl_client_t
*asl
, asl_msg_t
*msg
)
1147 uint32_t i
, len
, level
, outstatus
;
1148 const char *val
, *sstr
, *fstr
;
1149 struct timeval tval
= {0, 0};
1155 asl
= _asl_open_default();
1156 if (asl
== NULL
) return NULL
;
1159 status
= gettimeofday(&tval
, NULL
);
1162 time_t tick
= time(NULL
);
1168 status
= asl_msg_lookup(msg
, ASL_KEY_LEVEL
, &val
, NULL
);
1169 if (status
!= 0) val
= NULL
;
1171 level
= ASL_LEVEL_DEBUG
;
1172 if (val
!= NULL
) level
= atoi(val
);
1173 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
1176 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1177 if (status
!= 0) sstr
= NULL
;
1180 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1181 if (status
!= 0) fstr
= NULL
;
1183 out
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, NULL
);
1184 out
= asl_msg_merge(out
, msg
);
1191 _asl_set_option(asl_msg_t
*msg
, const char *opt
)
1193 const char *val
= NULL
;
1196 if (msg
== NULL
) return;
1197 if (opt
== NULL
) return;
1199 status
= asl_msg_lookup(msg
, ASL_KEY_OPTION
, &val
, NULL
);
1200 if ((status
!= 0) || (val
== NULL
))
1202 asl_msg_set_key_val(msg
, ASL_KEY_OPTION
, opt
);
1206 char *option
= NULL
;
1207 asprintf(&option
, "%s %s", opt
, val
);
1208 asl_msg_set_key_val(msg
, ASL_KEY_OPTION
, option
);
1214 _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
)
1217 uint32_t level
, lmask
;
1218 asl_msg_t
*release_sendmsg
= NULL
;
1222 asl
= _asl_open_default();
1223 if (asl
== NULL
) return ASL_STATUS_FAILED
;
1225 uint32_t objtype
= asl_get_type(obj
);
1226 if (objtype
== ASL_TYPE_CLIENT
) asl
= (asl_client_t
*)obj
;
1230 level
= eval
& EVAL_LEVEL_MASK
;
1231 if (level
> 7) level
= 7;
1232 lmask
= ASL_FILTER_MASK(level
);
1234 if (sendmsg
== NULL
) {
1235 struct timeval tval
= {0, 0};
1236 const char *sstr
, *fstr
;
1238 status
= gettimeofday(&tval
, NULL
);
1240 time_t tick
= time(NULL
);
1246 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1252 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1256 sendmsg
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, mstr
);
1257 if (sendmsg
== NULL
) {
1258 return ASL_STATUS_FAILED
;
1261 release_sendmsg
= sendmsg
= asl_msg_merge(sendmsg
, msg
);
1264 /* write to file descriptors */
1265 for (uint32_t i
= 0; i
< asl
->out_count
; i
++) {
1267 if ((asl
->out_list
[i
].fd
!= STDOUT_FILENO
) && (asl
->out_list
[i
].fd
!= STDERR_FILENO
)) {
1268 continue; // new logging only support stdout/stderr
1272 if ((asl
->out_list
[i
].fd
>= 0) && (asl
->out_list
[i
].filter
!= 0) && ((asl
->out_list
[i
].filter
& lmask
) != 0)) {
1276 str
= asl_format_message(sendmsg
, asl
->out_list
[i
].mfmt
, asl
->out_list
[i
].tfmt
, asl
->out_list
[i
].encoding
, &len
);
1277 if (str
== NULL
) continue;
1279 status
= write(asl
->out_list
[i
].fd
, str
, len
- 1);
1282 /* soft error for fd 2 (stderr) */
1283 if (asl
->out_list
[i
].fd
== 2) status
= 0;
1284 asl
->out_list
[i
].fd
= -1;
1292 if (release_sendmsg
) {
1293 asl_msg_release(release_sendmsg
);
1300 _asl_send_message(asl_object_t obj
, uint32_t eval
, asl_msg_t
*msg
, const char *mstr
)
1302 uint32_t len
, level
, lmask
, outstatus
, objtype
;
1303 const char *sstr
, *fstr
;
1304 struct timeval tval
= {0, 0};
1306 int use_global_lock
= 0;
1307 kern_return_t kstatus
;
1309 asl_msg_t
*qd_msg
= NULL
;
1310 asl_client_t
*asl
= NULL
;
1311 static dispatch_once_t noquota_once
;
1312 __block
int log_quota_msg
= 0;
1314 if ((eval
& EVAL_ASL
) == 0) return ASL_STATUS_OK
;
1318 asl
= _asl_open_default();
1319 if (asl
== NULL
) return ASL_STATUS_FAILED
;
1320 use_global_lock
= 1;
1321 objtype
= ASL_TYPE_CLIENT
;
1325 objtype
= asl_get_type(obj
);
1326 if (objtype
== ASL_TYPE_CLIENT
) asl
= (asl_client_t
*)obj
;
1329 level
= eval
& EVAL_LEVEL_MASK
;
1330 if (level
> 7) level
= 7;
1331 lmask
= ASL_FILTER_MASK(level
);
1333 if ((objtype
== ASL_TYPE_CLIENT
) && (asl
->aslfile
!= NULL
)) use_global_lock
= 1;
1335 status
= gettimeofday(&tval
, NULL
);
1338 time_t tick
= time(NULL
);
1344 status
= asl_msg_lookup(msg
, ASL_KEY_SENDER
, &sstr
, NULL
);
1345 if (status
!= 0) sstr
= NULL
;
1348 status
= asl_msg_lookup(msg
, ASL_KEY_FACILITY
, &fstr
, NULL
);
1349 if (status
!= 0) fstr
= NULL
;
1351 sendmsg
= asl_base_msg(asl
, level
, &tval
, sstr
, fstr
, mstr
);
1352 if (sendmsg
== NULL
) return ASL_STATUS_FAILED
;
1354 /* Set "ASLOption store" if tunneling */
1355 if (eval
& EVAL_TUNNEL
) _asl_set_option(sendmsg
, ASL_OPT_STORE
);
1359 if (use_global_lock
!= 0) pthread_mutex_lock(&_asl_global
.lock
);
1361 sendmsg
= asl_msg_merge(sendmsg
, msg
);
1363 if (objtype
!= ASL_TYPE_CLIENT
)
1365 asl_append(obj
, (asl_object_t
)sendmsg
);
1366 asl_msg_release(sendmsg
);
1367 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1368 return ASL_STATUS_OK
;
1372 * If there is an aslfile this is a stand-alone file client.
1373 * Just save to the file.
1375 if (asl
->aslfile
!= NULL
)
1377 outstatus
= ASL_STATUS_FAILED
;
1379 if (sendmsg
!= NULL
)
1381 outstatus
= asl_file_save(asl
->aslfile
, sendmsg
, &(asl
->aslfileid
));
1385 asl_msg_release(sendmsg
);
1387 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1395 * Quotas are disabled if:
1396 * - a remote control filter is in place (EVAL_TUNNEL)
1397 * - Environment variable ASL_QUOTA_DISABLED == 1
1398 * - /etc/asl/.noquota existed at the time that the process started
1400 * We just check /etc/asl/.noquota once, since it would be
1401 * expensive to stat() for every log message.
1403 * We only check the Environment variable once, since getenv() is
1404 * not thread safe. If someone is changing the environment,
1408 dispatch_once(&noquota_once
, ^{
1410 memset(&sb
, 0, sizeof(struct stat
));
1412 int save_errno
= errno
;
1414 if (stat(NOQUOTA_FILE_PATH
, &sb
) == 0)
1416 _asl_global
.quota
= UINT32_MAX
;
1420 const char *qtest
= getenv(NOQUOTA_ENV
);
1421 if ((qtest
!= NULL
) && (!strcmp(qtest
, "1")))
1423 _asl_global
.quota
= UINT32_MAX
;
1428 /* reset errno since we want stat() to fail silently */
1432 if (log_quota_msg
!= 0)
1434 qd_msg
= asl_base_msg(asl
, QUOTA_LEVEL
, &tval
, sstr
, fstr
, QUOTA_DISABLED_MSG
);
1435 asl_msg_set_key_val(qd_msg
, ASL_KEY_OPTION
, ASL_OPT_STORE
);
1438 if (((eval
& EVAL_TUNNEL
) == 0) && (_asl_global
.quota
!= UINT32_MAX
))
1440 time_t last_send
= _asl_global
.last_send
;
1441 time_t last_oq
= _asl_global
.last_oq_msg
;
1442 uint32_t qcurr
= _asl_global
.quota
;
1444 uint32_t qinc
, qnew
;
1448 /* add QUOTA_MPS to quota for each second we've been idle */
1449 if (tval
.tv_sec
> last_send
)
1451 delta
= tval
.tv_sec
- last_send
;
1454 if (delta
< (QUOTA_MPH
/ QUOTA_MPS
)) qinc
= delta
* QUOTA_MPS
;
1456 qnew
= MIN(QUOTA_MPH
, qcurr
+ qinc
);
1457 OSAtomicCompareAndSwapLongBarrier(last_send
, tval
.tv_sec
, (long *)&_asl_global
.last_send
);
1462 if ((tval
.tv_sec
- last_oq
) > QUOTA_MSG_INTERVAL
)
1465 OSAtomicCompareAndSwapLongBarrier(last_oq
, tval
.tv_sec
, (long *)&_asl_global
.last_oq_msg
);
1469 eval
&= ~EVAL_SEND_ASL
;
1474 OSAtomicCompareAndSwap32Barrier(qcurr
, qnew
- 1, (int32_t *)&_asl_global
.quota
);
1479 if (eval
& EVAL_MT_SHIM
)
1481 _asl_mt_shim_send_message(sendmsg
);
1486 if ((_asl_global
.server_port
!= MACH_PORT_NULL
) && (eval
& EVAL_SEND_ASL
))
1488 asl_string_t
*send_str
;
1492 if (eval
& EVAL_QUOTA
)
1494 asl_msg_set_key_val(sendmsg
, ASL_KEY_LEVEL
, QUOTA_LEVEL_STR
);
1495 asl_msg_set_key_val(sendmsg
, ASL_KEY_MSG
, QUOTA_MSG
);
1500 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, qd_msg
, "raw");
1501 len
= asl_string_length(send_str
);
1502 vmsize
= asl_string_allocated_size(send_str
);
1503 str
= asl_string_release_return_bytes(send_str
);
1506 kstatus
= _asl_server_message(_asl_global
.server_port
, (caddr_t
)str
, len
);
1507 if (kstatus
!= KERN_SUCCESS
)
1509 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1512 else if (vmsize
!= 0)
1514 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1516 asl_msg_release(qd_msg
);
1519 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, sendmsg
, "raw");
1520 len
= asl_string_length(send_str
);
1522 if (len
> LIBASL_MAX_MSG_SIZE
)
1526 snprintf(tmp
, sizeof(tmp
), SIZE_LIMIT_MSG
, len
, LIBASL_MAX_MSG_SIZE
);
1527 asl_msg_t
*limitmsg
= asl_base_msg(asl
, ASL_LEVEL_CRIT
, &tval
, sstr
, fstr
, tmp
);
1529 asl_string_release(send_str
);
1532 if (limitmsg
!= NULL
)
1534 /* Set "ASLOption store" if tunneling */
1535 if (eval
& EVAL_TUNNEL
) _asl_set_option(limitmsg
, ASL_OPT_STORE
);
1537 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, limitmsg
, "raw");
1538 len
= asl_string_length(send_str
);
1539 asl_msg_release(limitmsg
);
1543 vmsize
= asl_string_allocated_size(send_str
);
1544 str
= asl_string_release_return_bytes(send_str
);
1548 /* send a mach message to syslogd */
1549 kstatus
= _asl_server_message(_asl_global
.server_port
, (caddr_t
)str
, len
);
1550 if (kstatus
!= KERN_SUCCESS
)
1552 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1556 else if (vmsize
>0) vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1559 if ((sendmsg
!= NULL
) && (asl
->out_count
> 0))
1561 status
= _asl_send_message_text(asl
, sendmsg
, obj
, eval
, msg
, mstr
, false);
1564 asl_msg_release(sendmsg
);
1566 if (use_global_lock
!= 0) pthread_mutex_unlock(&_asl_global
.lock
);
1572 os_log_with_args_wrapper(void *addr
, int level
, const char *format
, ...)
1575 if (level
< ASL_LEVEL_EMERG
) level
= ASL_LEVEL_EMERG
;
1576 if (level
> ASL_LEVEL_DEBUG
) level
= ASL_LEVEL_DEBUG
;
1577 os_log_type_t type
= shim_asl_to_log_type
[level
];
1579 va_start(ap
, format
);
1580 os_log_with_args(OS_LOG_DEFAULT
, type
, format
, ap
, addr
);
1585 * asl_send: send a message
1586 * This routine may be used instead of asl_log() or asl_vlog() if asl_set()
1587 * has been used to set all of a message's attributes.
1588 * eval: hints about what to do with the message
1589 * msg: an asl message
1590 * returns 0 for success, non-zero for failure
1592 __private_extern__ ASL_STATUS
1593 asl_client_internal_send(asl_object_t obj
, asl_object_t msg
, void *addr
)
1595 int status
= ASL_STATUS_OK
;
1596 uint32_t eval
= _asl_evaluate_send(obj
, msg
, -1);
1598 const char *message
= asl_msg_get_val_for_key((asl_msg_t
*)msg
, ASL_KEY_MSG
);
1600 if ((eval
& EVAL_SEND_TRACE
) && message
&& message
[0] && os_log_shim_enabled(addr
))
1602 int level
= ASL_LEVEL_DEBUG
;
1603 const char *lval
= asl_msg_get_val_for_key((asl_msg_t
*)msg
, ASL_KEY_LEVEL
);
1604 if (lval
!= NULL
) level
= atoi(lval
);
1607 * If the return address and the format string are from different
1608 * binaries, os_log_with_args will not record the return address.
1609 * Work around this by passing a dynamic format string.
1611 char dynamic_format
[] = "%s";
1612 os_log_with_args_wrapper(addr
, level
, dynamic_format
, message
);
1614 if (eval
& EVAL_TEXT_FILE
)
1616 status
= _asl_send_message_text(NULL
, NULL
, obj
, eval
, (asl_msg_t
*)msg
, NULL
, true);
1619 else if (eval
& EVAL_ASL
)
1621 status
= _asl_send_message(obj
, eval
, (asl_msg_t
*)msg
, NULL
);
1628 #pragma mark auxiliary files and URLs
1631 _asl_aux_save_context(asl_aux_context_t
*ctx
)
1633 if (ctx
== NULL
) return ASL_STATUS_FAILED
;
1635 pthread_mutex_lock(&_asl_global
.lock
);
1637 _asl_global
.aux_ctx
= (asl_aux_context_t
**)reallocf(_asl_global
.aux_ctx
, (_asl_global
.aux_count
+ 1) * sizeof(asl_aux_context_t
*));
1638 if (_asl_global
.aux_ctx
== NULL
)
1640 _asl_global
.aux_count
= 0;
1641 pthread_mutex_unlock(&_asl_global
.lock
);
1642 return ASL_STATUS_FAILED
;
1645 _asl_global
.aux_ctx
[_asl_global
.aux_count
++] = ctx
;
1647 pthread_mutex_unlock(&_asl_global
.lock
);
1649 return ASL_STATUS_OK
;
1653 * Creates an auxiliary file that may be used to save arbitrary data. The ASL message msg
1654 * will be saved at the time that the auxiliary file is created. The message will include
1655 * any keys and values found in msg, and it will include the title and Uniform Type
1656 * Identifier specified. Output parameter out_fd will contain the file descriptor of the
1657 * new auxiliary file.
1660 _asl_auxiliary(asl_msg_t
*msg
, const char *title
, const char *uti
, const char *url
, int *out_fd
)
1663 asl_string_t
*send_str
;
1665 fileport_t fileport
;
1666 kern_return_t kstatus
;
1668 uint32_t newurllen
, where
;
1669 int status
, fd
, fdpair
[2];
1671 dispatch_queue_t pipe_q
;
1672 dispatch_io_t pipe_channel
;
1673 dispatch_semaphore_t sem
;
1675 aux
= asl_msg_new(ASL_TYPE_MSG
);
1677 if (url
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_URL
, url
);
1678 if (title
!= NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_TITLE
, title
);
1679 if (uti
== NULL
) asl_msg_set_key_val(aux
, ASL_KEY_AUX_UTI
, "public.data");
1680 else asl_msg_set_key_val(aux
, ASL_KEY_AUX_UTI
, uti
);
1682 aux
= asl_msg_merge(aux
, msg
);
1684 /* if (out_fd == NULL), this is from asl_log_auxiliary_location */
1687 uint32_t eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux
, -1);
1688 status
= _asl_send_message(NULL
, eval
, aux
, NULL
);
1689 asl_msg_release(aux
);
1693 where
= asl_store_location();
1694 if (where
== ASL_STORE_LOCATION_MEMORY
)
1697 asl_aux_context_t
*ctx
= (asl_aux_context_t
*)calloc(1, sizeof(asl_aux_context_t
));
1698 if (ctx
== NULL
) return ASL_STATUS_FAILED
;
1700 status
= pipe(fdpair
);
1704 return ASL_STATUS_FAILED
;
1707 /* give read end to dispatch_io_read */
1709 sem
= dispatch_semaphore_create(0);
1711 ctx
->fd
= fdpair
[1];
1713 status
= _asl_aux_save_context(ctx
);
1714 if (status
!= ASL_STATUS_OK
)
1718 dispatch_release(sem
);
1720 return ASL_STATUS_FAILED
;
1723 pipe_q
= dispatch_queue_create("ASL_AUX_PIPE_Q", NULL
);
1724 pipe_channel
= dispatch_io_create(DISPATCH_IO_STREAM
, fd
, pipe_q
, ^(int err
){
1728 *out_fd
= fdpair
[1];
1730 dispatch_io_set_low_water(pipe_channel
, SIZE_MAX
);
1732 dispatch_io_read(pipe_channel
, 0, SIZE_MAX
, pipe_q
, ^(bool done
, dispatch_data_t pipedata
, int err
){
1735 size_t len
= dispatch_data_get_size(pipedata
);
1738 const char *bytes
= NULL
;
1742 dispatch_data_t md
= dispatch_data_create_map(pipedata
, (const void **)&bytes
, &len
);
1743 encoded
= asl_core_encode_buffer(bytes
, len
);
1744 asl_msg_set_key_val(aux
, ASL_KEY_AUX_DATA
, encoded
);
1746 eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux
, -1);
1747 _asl_send_message(NULL
, eval
, aux
, NULL
);
1748 asl_msg_release(aux
);
1749 dispatch_release(md
);
1755 dispatch_semaphore_signal(sem
);
1756 dispatch_release(pipe_channel
);
1757 dispatch_release(pipe_q
);
1761 return ASL_STATUS_OK
;
1765 if (_asl_global
.server_port
== MACH_PORT_NULL
) return ASL_STATUS_FAILED
;
1767 send_str
= asl_msg_to_string_raw(ASL_STRING_MIG
, aux
, "raw");
1768 len
= asl_string_length(send_str
);
1769 vmsize
= asl_string_allocated_size(send_str
);
1770 str
= asl_string_release_return_bytes(send_str
);
1774 asl_msg_release(aux
);
1775 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1776 return ASL_STATUS_FAILED
;
1780 fileport
= MACH_PORT_NULL
;
1781 status
= KERN_SUCCESS
;
1783 kstatus
= _asl_server_create_aux_link(_asl_global
.server_port
, (caddr_t
)str
, len
, &fileport
, &newurl
, &newurllen
, &status
);
1784 if (kstatus
!= KERN_SUCCESS
)
1786 vm_deallocate(mach_task_self(), (vm_address_t
)str
, vmsize
);
1787 asl_msg_release(aux
);
1788 return ASL_STATUS_FAILED
;
1793 asl_msg_release(aux
);
1799 asl_msg_set_key_val(aux
, ASL_KEY_AUX_URL
, newurl
);
1800 vm_deallocate(mach_task_self(), (vm_address_t
)newurl
, newurllen
);
1803 if (fileport
== MACH_PORT_NULL
)
1805 asl_msg_release(aux
);
1806 return ASL_STATUS_FAILED
;
1809 fd
= fileport_makefd(fileport
);
1810 mach_port_deallocate(mach_task_self(), fileport
);
1813 asl_msg_release(aux
);
1818 asl_aux_context_t
*ctx
= (asl_aux_context_t
*)calloc(1, sizeof(asl_aux_context_t
));
1830 status
= _asl_aux_save_context(ctx
);
1838 asl_create_auxiliary_file(asl_object_t msg
, const char *title
, const char *uti
, int *out_fd
)
1840 if (out_fd
== NULL
) return -1;
1842 ASL_STATUS status
= _asl_auxiliary((asl_msg_t
*)msg
, title
, uti
, NULL
, out_fd
);
1843 return (status
== ASL_STATUS_OK
) ? 0 : -1;
1847 asl_log_auxiliary_location(asl_object_t msg
, const char *title
, const char *uti
, const char *url
)
1849 ASL_STATUS status
= _asl_auxiliary((asl_msg_t
*)msg
, title
, uti
, url
, NULL
);
1850 return (status
== ASL_STATUS_OK
) ? 0 : -1;
1854 * Close an auxiliary file.
1855 * Sends the cached auxiliary message to syslogd.
1856 * Returns 0 on success, -1 on error.
1859 asl_close_auxiliary_file(int fd
)
1863 dispatch_semaphore_t aux_sem
= NULL
;
1865 pthread_mutex_lock(&(_asl_global
.lock
));
1870 for (i
= 0; i
< _asl_global
.aux_count
; i
++)
1872 if (_asl_global
.aux_ctx
[i
]->fd
== fd
)
1876 aux_msg
= _asl_global
.aux_ctx
[i
]->msg
;
1877 aux_sem
= _asl_global
.aux_ctx
[i
]->sem
;
1879 free(_asl_global
.aux_ctx
[i
]);
1881 for (j
= i
+ 1; j
< _asl_global
.aux_count
; i
++, j
++)
1883 _asl_global
.aux_ctx
[i
] = _asl_global
.aux_ctx
[j
];
1886 _asl_global
.aux_count
--;
1888 if (_asl_global
.aux_count
== 0)
1890 free(_asl_global
.aux_ctx
);
1891 _asl_global
.aux_ctx
= NULL
;
1895 _asl_global
.aux_ctx
= (asl_aux_context_t
**)reallocf(_asl_global
.aux_ctx
, _asl_global
.aux_count
* sizeof(asl_aux_context_t
*));
1896 if (_asl_global
.aux_ctx
== NULL
)
1898 _asl_global
.aux_count
= 0;
1907 pthread_mutex_unlock(&(_asl_global
.lock
));
1911 if (aux_msg
!= NULL
)
1913 uint32_t eval
= _asl_evaluate_send(NULL
, (asl_object_t
)aux_msg
, -1);
1914 if (_asl_send_message(NULL
, eval
, aux_msg
, NULL
) != ASL_STATUS_OK
) status
= -1;
1915 asl_msg_release(aux_msg
);
1918 if (aux_sem
!= NULL
)
1920 dispatch_semaphore_wait(aux_sem
, DISPATCH_TIME_FOREVER
);
1921 dispatch_release(aux_sem
);
1930 _asl_server_control_query(void)
1932 asl_msg_list_t
*list
= NULL
;
1934 uint32_t len
, reslen
, status
;
1935 uint64_t cmax
, qmin
;
1936 kern_return_t kstatus
;
1938 asl_msg_t
*m
= NULL
;
1939 static const char ctlstr
[] = "1\nQ [= ASLOption control]\n";
1942 if (_asl_global
.server_port
== MACH_PORT_NULL
) return NULL
;
1944 len
= strlen(ctlstr
) + 1;
1951 kstatus
= vm_allocate(mach_task_self(), (vm_address_t
*)&vmstr
, len
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_MEMORY_ASL
));
1952 if (kstatus
!= KERN_SUCCESS
) return NULL
;
1954 memmove(vmstr
, ctlstr
, len
);
1957 kstatus
= _asl_server_query_2(_asl_global
.server_port
, vmstr
, len
, qmin
, FETCH_BATCH
, 0, (caddr_t
*)&res
, &reslen
, &cmax
, (int *)&status
);
1958 if (kstatus
!= KERN_SUCCESS
) return NULL
;
1960 list
= asl_msg_list_from_string(res
);
1961 vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1963 if (list
== NULL
) return NULL
;
1964 if (list
->count
> 0) m
= asl_msg_retain(list
->msg
[0]);
1965 asl_msg_list_release(list
);
1970 * Returns ASL_STORE_LOCATION_FILE or ASL_STORE_LOCATION_MEMORY
1973 asl_store_location()
1975 kern_return_t kstatus
;
1977 uint32_t reslen
, status
;
1981 if (_asl_global
.server_port
== MACH_PORT_NULL
) return ASL_STORE_LOCATION_FILE
;
1986 status
= ASL_STATUS_OK
;
1988 kstatus
= _asl_server_query_2(_asl_global
.server_port
, NULL
, 0, 0, -1, 0, (caddr_t
*)&res
, &reslen
, &cmax
, (int *)&status
);
1989 if (kstatus
!= KERN_SUCCESS
) return ASL_STORE_LOCATION_FILE
;
1991 /* res should never be returned, but just to be certain we don't leak VM ... */
1992 if (res
!= NULL
) vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1994 if (status
== ASL_STATUS_OK
) return ASL_STORE_LOCATION_MEMORY
;
1995 return ASL_STORE_LOCATION_FILE
;
1999 asl_open_path(const char *path
, uint32_t opts
)
2002 asl_file_t
*fout
= NULL
;
2003 asl_store_t
*sout
= NULL
;
2005 if (opts
== 0) opts
= ASL_OPT_OPEN_READ
;
2007 if (opts
& ASL_OPT_OPEN_READ
)
2011 if (asl_store_open_read(ASL_PLACE_DATABASE_DEFAULT
, &sout
) != ASL_STATUS_OK
) return NULL
;
2012 return (asl_object_t
)sout
;
2015 memset(&sb
, 0, sizeof(struct stat
));
2016 if (stat(path
, &sb
) < 0) return NULL
;
2018 if (sb
.st_mode
& S_IFREG
)
2020 if (asl_file_open_read(path
, &fout
) != ASL_STATUS_OK
) return NULL
;
2021 return (asl_object_t
)fout
;
2023 else if (sb
.st_mode
& S_IFDIR
)
2025 if (asl_store_open_read(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2026 return (asl_object_t
)sout
;
2031 else if (opts
& ASL_OPT_OPEN_WRITE
)
2033 if (path
== NULL
) return NULL
;
2035 memset(&sb
, 0, sizeof(struct stat
));
2036 if (stat(path
, &sb
) < 0)
2038 if (errno
!= ENOENT
) return NULL
;
2040 if (opts
& ASL_OPT_CREATE_STORE
)
2042 if (asl_store_open_write(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2043 return (asl_object_t
)sout
;
2047 if (asl_file_open_write(path
, 0644, geteuid(), getegid(), &fout
) != ASL_STATUS_OK
) return NULL
;
2048 return (asl_object_t
)fout
;
2051 else if (sb
.st_mode
& S_IFREG
)
2053 if (asl_file_open_write(path
, 0644, geteuid(), getegid(), &fout
) != ASL_STATUS_OK
) return NULL
;
2054 return (asl_object_t
)fout
;
2056 else if (sb
.st_mode
& S_IFDIR
)
2058 if (asl_store_open_write(path
, &sout
) != ASL_STATUS_OK
) return NULL
;
2059 return (asl_object_t
)sout
;