]>
Commit | Line | Data |
---|---|---|
86a8bcf5 A |
1 | /* |
2 | * Copyright (c) 2019 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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 | |
11 | * file. | |
12 | * | |
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. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | #include <asl_msg.h> | |
25 | #include <os/lock_private.h> | |
26 | #include <xpc/private.h> | |
27 | ||
28 | #if TARGET_OS_OSX | |
29 | ||
30 | #define MT_SHIM_SERVICE_NAME "com.apple.analyticsd.messagetracer" | |
31 | ||
32 | static os_unfair_lock _mt_shim_lock = OS_UNFAIR_LOCK_INIT; | |
33 | static xpc_pipe_t _mt_shim_pipe; | |
34 | ||
35 | void | |
36 | _asl_mt_shim_fork_child(void) | |
37 | { | |
38 | if (_mt_shim_pipe) { | |
39 | xpc_pipe_invalidate(_mt_shim_pipe); | |
40 | xpc_release(_mt_shim_pipe); | |
41 | _mt_shim_pipe = NULL; | |
42 | } | |
43 | _mt_shim_lock = OS_UNFAIR_LOCK_INIT; | |
44 | } | |
45 | ||
46 | static xpc_pipe_t | |
47 | _asl_mt_shim_pipe_copy(OS_OBJECT_CONSUMED xpc_pipe_t prev) | |
48 | { | |
49 | xpc_pipe_t pipe; | |
50 | ||
51 | os_unfair_lock_lock_with_options(&_mt_shim_lock, | |
52 | OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION); | |
53 | pipe = _mt_shim_pipe; | |
54 | if (prev) { | |
55 | if (pipe == prev) { | |
56 | xpc_release(pipe); | |
57 | pipe = NULL; | |
58 | } | |
59 | xpc_release(prev); | |
60 | } | |
61 | if (!pipe) { | |
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); | |
64 | } | |
65 | if (pipe) xpc_retain(pipe); | |
66 | os_unfair_lock_unlock(&_mt_shim_lock); | |
67 | return pipe; | |
68 | } | |
69 | ||
70 | static xpc_object_t | |
71 | _asl_mt_shim_send_with_reply(xpc_object_t msg) | |
72 | { | |
73 | xpc_object_t reply = NULL; | |
74 | xpc_pipe_t pipe = _asl_mt_shim_pipe_copy(NULL); | |
75 | ||
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); | |
79 | } | |
80 | if (pipe) xpc_release(pipe); | |
81 | return reply; | |
82 | } | |
83 | ||
84 | void | |
85 | _asl_mt_shim_send_message(asl_msg_t *msg) | |
86 | { | |
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; | |
91 | } | |
92 | ||
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); | |
97 | xpc_release(xmsg); | |
98 | } | |
99 | ||
100 | #endif /* TARGET_OS_OSX */ |