]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2003-2009 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 | ||
0a7de745 | 40 | decl_lck_mtx_data(, host_notify_lock) |
b0d623f7 | 41 | |
0a7de745 A |
42 | lck_mtx_ext_t host_notify_lock_ext; |
43 | lck_grp_t host_notify_lock_grp; | |
44 | lck_attr_t host_notify_lock_attr; | |
45 | static lck_grp_attr_t host_notify_lock_grp_attr; | |
46 | static zone_t host_notify_zone; | |
55e303ae | 47 | |
0a7de745 | 48 | static queue_head_t host_notify_queue[HOST_NOTIFY_TYPE_MAX + 1]; |
55e303ae | 49 | |
0a7de745 A |
50 | static mach_msg_id_t host_notify_replyid[HOST_NOTIFY_TYPE_MAX + 1] = |
51 | { HOST_CALENDAR_CHANGED_REPLYID, | |
52 | HOST_CALENDAR_SET_REPLYID }; | |
55e303ae A |
53 | |
54 | struct host_notify_entry { | |
0a7de745 A |
55 | queue_chain_t entries; |
56 | ipc_port_t port; | |
55e303ae A |
57 | }; |
58 | ||
0a7de745 | 59 | typedef struct host_notify_entry *host_notify_t; |
55e303ae A |
60 | |
61 | void | |
62 | host_notify_init(void) | |
63 | { | |
0a7de745 | 64 | int i; |
55e303ae | 65 | |
0a7de745 | 66 | for (i = 0; i <= HOST_NOTIFY_TYPE_MAX; i++) { |
55e303ae | 67 | queue_init(&host_notify_queue[i]); |
0a7de745 | 68 | } |
55e303ae | 69 | |
b0d623f7 A |
70 | lck_grp_attr_setdefault(&host_notify_lock_grp_attr); |
71 | lck_grp_init(&host_notify_lock_grp, "host_notify", &host_notify_lock_grp_attr); | |
72 | lck_attr_setdefault(&host_notify_lock_attr); | |
73 | ||
74 | lck_mtx_init_ext(&host_notify_lock, &host_notify_lock_ext, &host_notify_lock_grp, &host_notify_lock_attr); | |
55e303ae | 75 | |
0a7de745 | 76 | i = sizeof(struct host_notify_entry); |
55e303ae | 77 | host_notify_zone = |
0a7de745 | 78 | zinit(i, (4096 * i), (16 * i), "host_notify"); |
55e303ae A |
79 | } |
80 | ||
81 | kern_return_t | |
82 | host_request_notification( | |
0a7de745 A |
83 | host_t host, |
84 | host_flavor_t notify_type, | |
85 | ipc_port_t port) | |
55e303ae | 86 | { |
0a7de745 | 87 | host_notify_t entry; |
55e303ae | 88 | |
0a7de745 A |
89 | if (host == HOST_NULL) { |
90 | return KERN_INVALID_ARGUMENT; | |
91 | } | |
55e303ae | 92 | |
0a7de745 A |
93 | if (!IP_VALID(port)) { |
94 | return KERN_INVALID_CAPABILITY; | |
95 | } | |
55e303ae | 96 | |
0a7de745 A |
97 | if (notify_type > HOST_NOTIFY_TYPE_MAX || notify_type < 0) { |
98 | return KERN_INVALID_ARGUMENT; | |
99 | } | |
55e303ae A |
100 | |
101 | entry = (host_notify_t)zalloc(host_notify_zone); | |
0a7de745 A |
102 | if (entry == NULL) { |
103 | return KERN_RESOURCE_SHORTAGE; | |
104 | } | |
55e303ae | 105 | |
b0d623f7 | 106 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
107 | |
108 | ip_lock(port); | |
fe8ab488 | 109 | if (!ip_active(port) || port->ip_tempowner || ip_kotype(port) != IKOT_NONE) { |
55e303ae A |
110 | ip_unlock(port); |
111 | ||
b0d623f7 | 112 | lck_mtx_unlock(&host_notify_lock); |
91447636 | 113 | zfree(host_notify_zone, entry); |
55e303ae | 114 | |
0a7de745 | 115 | return KERN_FAILURE; |
55e303ae A |
116 | } |
117 | ||
118 | entry->port = port; | |
119 | ipc_kobject_set_atomically(port, (ipc_kobject_t)entry, IKOT_HOST_NOTIFY); | |
120 | ip_unlock(port); | |
121 | ||
122 | enqueue_tail(&host_notify_queue[notify_type], (queue_entry_t)entry); | |
b0d623f7 | 123 | lck_mtx_unlock(&host_notify_lock); |
55e303ae | 124 | |
0a7de745 | 125 | return KERN_SUCCESS; |
55e303ae A |
126 | } |
127 | ||
128 | void | |
129 | host_notify_port_destroy( | |
0a7de745 | 130 | ipc_port_t port) |
55e303ae | 131 | { |
0a7de745 | 132 | host_notify_t entry; |
55e303ae | 133 | |
b0d623f7 | 134 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
135 | |
136 | ip_lock(port); | |
137 | if (ip_kotype(port) == IKOT_HOST_NOTIFY) { | |
138 | entry = (host_notify_t)port->ip_kobject; | |
139 | assert(entry != NULL); | |
140 | ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE); | |
141 | ip_unlock(port); | |
142 | ||
143 | assert(entry->port == port); | |
6d2010ae | 144 | remqueue((queue_entry_t)entry); |
b0d623f7 | 145 | lck_mtx_unlock(&host_notify_lock); |
91447636 | 146 | zfree(host_notify_zone, entry); |
55e303ae A |
147 | |
148 | ipc_port_release_sonce(port); | |
149 | return; | |
150 | } | |
151 | ip_unlock(port); | |
152 | ||
b0d623f7 | 153 | lck_mtx_unlock(&host_notify_lock); |
55e303ae A |
154 | } |
155 | ||
156 | static void | |
157 | host_notify_all( | |
0a7de745 A |
158 | host_flavor_t notify_type, |
159 | mach_msg_header_t *msg, | |
160 | mach_msg_size_t msg_size) | |
55e303ae | 161 | { |
0a7de745 | 162 | queue_t notify_queue = &host_notify_queue[notify_type]; |
55e303ae | 163 | |
b0d623f7 | 164 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
165 | |
166 | if (!queue_empty(notify_queue)) { | |
0a7de745 A |
167 | queue_head_t send_queue; |
168 | host_notify_t entry; | |
55e303ae A |
169 | |
170 | send_queue = *notify_queue; | |
171 | queue_init(notify_queue); | |
172 | ||
173 | send_queue.next->prev = &send_queue; | |
174 | send_queue.prev->next = &send_queue; | |
175 | ||
fe8ab488 A |
176 | msg->msgh_bits = |
177 | MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, 0); | |
55e303ae | 178 | msg->msgh_local_port = MACH_PORT_NULL; |
fe8ab488 | 179 | msg->msgh_voucher_port = MACH_PORT_NULL; |
55e303ae | 180 | msg->msgh_id = host_notify_replyid[notify_type]; |
55e303ae A |
181 | |
182 | while ((entry = (host_notify_t)dequeue(&send_queue)) != NULL) { | |
0a7de745 | 183 | ipc_port_t port; |
55e303ae A |
184 | |
185 | port = entry->port; | |
186 | assert(port != IP_NULL); | |
187 | ||
188 | ip_lock(port); | |
189 | assert(ip_kotype(port) == IKOT_HOST_NOTIFY); | |
190 | assert(port->ip_kobject == (ipc_kobject_t)entry); | |
191 | ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE); | |
192 | ip_unlock(port); | |
193 | ||
b0d623f7 | 194 | lck_mtx_unlock(&host_notify_lock); |
91447636 | 195 | zfree(host_notify_zone, entry); |
55e303ae A |
196 | |
197 | msg->msgh_remote_port = port; | |
198 | ||
b0d623f7 | 199 | (void) mach_msg_send_from_kernel_proper(msg, msg_size); |
55e303ae | 200 | |
b0d623f7 | 201 | lck_mtx_lock(&host_notify_lock); |
55e303ae A |
202 | } |
203 | } | |
204 | ||
b0d623f7 | 205 | lck_mtx_unlock(&host_notify_lock); |
55e303ae A |
206 | } |
207 | ||
208 | void | |
209 | host_notify_calendar_change(void) | |
210 | { | |
0a7de745 | 211 | __Request__host_calendar_changed_t msg; |
55e303ae | 212 | |
0a7de745 | 213 | host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE, &msg.Head, sizeof(msg)); |
55e303ae | 214 | } |
39037602 A |
215 | |
216 | void | |
217 | host_notify_calendar_set(void) | |
218 | { | |
0a7de745 | 219 | __Request__host_calendar_set_t msg; |
39037602 | 220 | |
0a7de745 | 221 | host_notify_all(HOST_NOTIFY_CALENDAR_SET, &msg.Head, sizeof(msg)); |
39037602 | 222 | } |