2 * Copyright (c) 2019 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@
25 #include <os/lock_private.h>
26 #include <xpc/private.h>
30 #define MT_SHIM_SERVICE_NAME "com.apple.analyticsd.messagetracer"
32 static os_unfair_lock _mt_shim_lock
= OS_UNFAIR_LOCK_INIT
;
33 static xpc_pipe_t _mt_shim_pipe
;
36 _asl_mt_shim_fork_child(void)
39 xpc_pipe_invalidate(_mt_shim_pipe
);
40 xpc_release(_mt_shim_pipe
);
43 _mt_shim_lock
= OS_UNFAIR_LOCK_INIT
;
47 _asl_mt_shim_pipe_copy(OS_OBJECT_CONSUMED xpc_pipe_t prev
)
51 os_unfair_lock_lock_with_options(&_mt_shim_lock
,
52 OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION
);
62 uint64_t flags
= XPC_PIPE_PRIVILEGED
| XPC_PIPE_USE_SYNC_IPC_OVERRIDE
;
63 _mt_shim_pipe
= pipe
= xpc_pipe_create(MT_SHIM_SERVICE_NAME
, flags
);
65 if (pipe
) xpc_retain(pipe
);
66 os_unfair_lock_unlock(&_mt_shim_lock
);
71 _asl_mt_shim_send_with_reply(xpc_object_t msg
)
73 xpc_object_t reply
= NULL
;
74 xpc_pipe_t pipe
= _asl_mt_shim_pipe_copy(NULL
);
76 if (pipe
&& xpc_pipe_routine(pipe
, msg
, &reply
) == EPIPE
) {
77 pipe
= _asl_mt_shim_pipe_copy(pipe
);
78 if (pipe
) (void)xpc_pipe_routine(pipe
, msg
, &reply
);
80 if (pipe
) xpc_release(pipe
);
85 _asl_mt_shim_send_message(asl_msg_t
*msg
)
87 /* don't send messages that were already shimmed by the caller */
88 const char *val
= NULL
;
89 if ((asl_msg_lookup(msg
, "com.apple.message.__source__", &val
, NULL
) == 0) && (val
!= NULL
)) {
90 if (!strcmp(val
, "SPI")) return;
93 xpc_object_t xmsg
= xpc_dictionary_create(NULL
, NULL
, 0);
94 _asl_log_args_to_xpc(NULL
, (asl_object_t
)msg
, xmsg
);
95 xpc_object_t reply
= _asl_mt_shim_send_with_reply(xmsg
);
96 if (reply
) xpc_release(reply
);
100 #endif /* TARGET_OS_OSX */