]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2003-2020 Apple Inc. All rights reserved. |
55e303ae | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
55e303ae | 27 | */ |
55e303ae A |
28 | |
29 | #include <mach/mach_types.h> | |
91447636 | 30 | #include <mach/mach_host.h> |
55e303ae | 31 | |
91447636 | 32 | #include <kern/kern_types.h> |
55e303ae A |
33 | #include <kern/ipc_kobject.h> |
34 | #include <kern/host_notify.h> | |
35 | ||
36 | #include <kern/queue.h> | |
37 | ||
38 | #include "mach/host_notify_reply.h" | |
39 | ||
f427ee49 A |
40 | struct host_notify_entry { |
41 | queue_chain_t entries; | |
42 | ipc_port_t port; | |
43 | }; | |
44 | typedef struct host_notify_entry *host_notify_t; | |
b0d623f7 | 45 | |
f427ee49 A |
46 | LCK_GRP_DECLARE(host_notify_lock_grp, "host_notify"); |
47 | LCK_MTX_EARLY_DECLARE(host_notify_lock, &host_notify_lock_grp); | |
55e303ae | 48 | |
f427ee49 A |
49 | static ZONE_DECLARE(host_notify_zone, "host_notify", |
50 | sizeof(struct host_notify_entry), ZC_NONE); | |
51 | ||
52 | static queue_head_t host_notify_queue[HOST_NOTIFY_TYPE_MAX + 1]; | |
55e303ae | 53 | |
0a7de745 A |
54 | static mach_msg_id_t host_notify_replyid[HOST_NOTIFY_TYPE_MAX + 1] = |
55 | { HOST_CALENDAR_CHANGED_REPLYID, | |
56 | HOST_CALENDAR_SET_REPLYID }; | |
55e303ae | 57 | |
f427ee49 A |
58 | __startup_func |
59 | static void | |
55e303ae A |
60 | host_notify_init(void) |
61 | { | |
f427ee49 | 62 | for (int i = 0; i <= HOST_NOTIFY_TYPE_MAX; i++) { |
55e303ae | 63 | queue_init(&host_notify_queue[i]); |
0a7de745 | 64 | } |
55e303ae | 65 | } |
f427ee49 | 66 | STARTUP(MACH_IPC, STARTUP_RANK_FIRST, host_notify_init); |
55e303ae A |
67 | |
68 | kern_return_t | |
69 | host_request_notification( | |
0a7de745 A |
70 | host_t host, |
71 | host_flavor_t notify_type, | |
72 | ipc_port_t port) | |
55e303ae | 73 | { |
0a7de745 | 74 | host_notify_t entry; |
55e303ae | 75 | |
0a7de745 A |
76 | if (host == HOST_NULL) { |
77 | return KERN_INVALID_ARGUMENT; | |
78 | } | |
55e303ae | 79 | |
0a7de745 A |
80 | if (!IP_VALID(port)) { |
81 | return KERN_INVALID_CAPABILITY; | |
82 | } | |
55e303ae | 83 | |
0a7de745 A |
84 | if (notify_type > HOST_NOTIFY_TYPE_MAX || notify_type < 0) { |
85 | return KERN_INVALID_ARGUMENT; | |
86 | } | |
55e303ae A |
87 | |
88 | entry = (host_notify_t)zalloc(host_notify_zone); | |
0a7de745 A |
89 | if (entry == NULL) { |
90 | return KERN_RESOURCE_SHORTAGE; | |
91 | } | |
55e303ae | 92 | |
b0d623f7 | 93 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
94 | |
95 | ip_lock(port); | |
f427ee49 A |
96 | if (!ip_active(port) || port->ip_tempowner || port->ip_specialreply || |
97 | ip_is_kolabeled(port) || ip_kotype(port) != IKOT_NONE) { | |
55e303ae A |
98 | ip_unlock(port); |
99 | ||
b0d623f7 | 100 | lck_mtx_unlock(&host_notify_lock); |
91447636 | 101 | zfree(host_notify_zone, entry); |
55e303ae | 102 | |
0a7de745 | 103 | return KERN_FAILURE; |
55e303ae A |
104 | } |
105 | ||
106 | entry->port = port; | |
107 | ipc_kobject_set_atomically(port, (ipc_kobject_t)entry, IKOT_HOST_NOTIFY); | |
108 | ip_unlock(port); | |
109 | ||
110 | enqueue_tail(&host_notify_queue[notify_type], (queue_entry_t)entry); | |
b0d623f7 | 111 | lck_mtx_unlock(&host_notify_lock); |
55e303ae | 112 | |
0a7de745 | 113 | return KERN_SUCCESS; |
55e303ae A |
114 | } |
115 | ||
116 | void | |
117 | host_notify_port_destroy( | |
0a7de745 | 118 | ipc_port_t port) |
55e303ae | 119 | { |
0a7de745 | 120 | host_notify_t entry; |
55e303ae | 121 | |
b0d623f7 | 122 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
123 | |
124 | ip_lock(port); | |
125 | if (ip_kotype(port) == IKOT_HOST_NOTIFY) { | |
ea3f0419 | 126 | entry = (host_notify_t)ip_get_kobject(port); |
55e303ae A |
127 | assert(entry != NULL); |
128 | ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE); | |
129 | ip_unlock(port); | |
130 | ||
131 | assert(entry->port == port); | |
6d2010ae | 132 | remqueue((queue_entry_t)entry); |
b0d623f7 | 133 | lck_mtx_unlock(&host_notify_lock); |
91447636 | 134 | zfree(host_notify_zone, entry); |
55e303ae A |
135 | |
136 | ipc_port_release_sonce(port); | |
137 | return; | |
138 | } | |
139 | ip_unlock(port); | |
140 | ||
b0d623f7 | 141 | lck_mtx_unlock(&host_notify_lock); |
55e303ae A |
142 | } |
143 | ||
144 | static void | |
145 | host_notify_all( | |
0a7de745 A |
146 | host_flavor_t notify_type, |
147 | mach_msg_header_t *msg, | |
148 | mach_msg_size_t msg_size) | |
55e303ae | 149 | { |
0a7de745 | 150 | queue_t notify_queue = &host_notify_queue[notify_type]; |
55e303ae | 151 | |
b0d623f7 | 152 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
153 | |
154 | if (!queue_empty(notify_queue)) { | |
0a7de745 A |
155 | queue_head_t send_queue; |
156 | host_notify_t entry; | |
55e303ae A |
157 | |
158 | send_queue = *notify_queue; | |
159 | queue_init(notify_queue); | |
160 | ||
161 | send_queue.next->prev = &send_queue; | |
162 | send_queue.prev->next = &send_queue; | |
163 | ||
fe8ab488 A |
164 | msg->msgh_bits = |
165 | MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, 0); | |
55e303ae | 166 | msg->msgh_local_port = MACH_PORT_NULL; |
fe8ab488 | 167 | msg->msgh_voucher_port = MACH_PORT_NULL; |
55e303ae | 168 | msg->msgh_id = host_notify_replyid[notify_type]; |
55e303ae A |
169 | |
170 | while ((entry = (host_notify_t)dequeue(&send_queue)) != NULL) { | |
0a7de745 | 171 | ipc_port_t port; |
55e303ae A |
172 | |
173 | port = entry->port; | |
174 | assert(port != IP_NULL); | |
175 | ||
176 | ip_lock(port); | |
177 | assert(ip_kotype(port) == IKOT_HOST_NOTIFY); | |
ea3f0419 | 178 | assert(ip_get_kobject(port) == (ipc_kobject_t)entry); |
55e303ae A |
179 | ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE); |
180 | ip_unlock(port); | |
181 | ||
b0d623f7 | 182 | lck_mtx_unlock(&host_notify_lock); |
91447636 | 183 | zfree(host_notify_zone, entry); |
55e303ae A |
184 | |
185 | msg->msgh_remote_port = port; | |
186 | ||
b0d623f7 | 187 | (void) mach_msg_send_from_kernel_proper(msg, msg_size); |
55e303ae | 188 | |
b0d623f7 | 189 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
190 | } |
191 | } | |
192 | ||
b0d623f7 | 193 | lck_mtx_unlock(&host_notify_lock); |
55e303ae A |
194 | } |
195 | ||
196 | void | |
197 | host_notify_calendar_change(void) | |
198 | { | |
0a7de745 | 199 | __Request__host_calendar_changed_t msg; |
55e303ae | 200 | |
0a7de745 | 201 | host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE, &msg.Head, sizeof(msg)); |
55e303ae | 202 | } |
39037602 A |
203 | |
204 | void | |
205 | host_notify_calendar_set(void) | |
206 | { | |
0a7de745 | 207 | __Request__host_calendar_set_t msg; |
39037602 | 208 | |
0a7de745 | 209 | host_notify_all(HOST_NOTIFY_CALENDAR_SET, &msg.Head, sizeof(msg)); |
39037602 | 210 | } |