]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/host_notify.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / kern / host_notify.c
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
30 *
31 * HISTORY
32 *
33 * 16 January 2003 (debo)
34 * Created.
35 */
36
37 #include <mach/mach_types.h>
38 #include <mach/mach_host.h>
39
40 #include <kern/kern_types.h>
41 #include <kern/ipc_kobject.h>
42 #include <kern/host_notify.h>
43
44 #include <kern/queue.h>
45
46 #include "mach/host_notify_reply.h"
47
48 decl_lck_mtx_data(,host_notify_lock)
49
50 lck_mtx_ext_t host_notify_lock_ext;
51 lck_grp_t host_notify_lock_grp;
52 lck_attr_t host_notify_lock_attr;
53 static lck_grp_attr_t host_notify_lock_grp_attr;
54 static zone_t host_notify_zone;
55
56 static queue_head_t host_notify_queue[HOST_NOTIFY_TYPE_MAX+1];
57
58 static mach_msg_id_t host_notify_replyid[HOST_NOTIFY_TYPE_MAX+1] =
59 { HOST_CALENDAR_CHANGED_REPLYID };
60
61 struct host_notify_entry {
62 queue_chain_t entries;
63 ipc_port_t port;
64 };
65
66 typedef struct host_notify_entry *host_notify_t;
67
68 void
69 host_notify_init(void)
70 {
71 int i;
72
73 for (i = 0; i <= HOST_NOTIFY_TYPE_MAX; i++)
74 queue_init(&host_notify_queue[i]);
75
76 lck_grp_attr_setdefault(&host_notify_lock_grp_attr);
77 lck_grp_init(&host_notify_lock_grp, "host_notify", &host_notify_lock_grp_attr);
78 lck_attr_setdefault(&host_notify_lock_attr);
79
80 lck_mtx_init_ext(&host_notify_lock, &host_notify_lock_ext, &host_notify_lock_grp, &host_notify_lock_attr);
81
82 i = sizeof (struct host_notify_entry);
83 host_notify_zone =
84 zinit(i, (4096 * i), (16 * i), "host_notify");
85 }
86
87 kern_return_t
88 host_request_notification(
89 host_t host,
90 host_flavor_t notify_type,
91 ipc_port_t port)
92 {
93 host_notify_t entry;
94
95 if (host == HOST_NULL)
96 return (KERN_INVALID_ARGUMENT);
97
98 if (!IP_VALID(port))
99 return (KERN_INVALID_CAPABILITY);
100
101 if (notify_type > HOST_NOTIFY_TYPE_MAX || notify_type < 0)
102 return (KERN_INVALID_ARGUMENT);
103
104 entry = (host_notify_t)zalloc(host_notify_zone);
105 if (entry == NULL)
106 return (KERN_RESOURCE_SHORTAGE);
107
108 lck_mtx_lock(&host_notify_lock);
109
110 ip_lock(port);
111 if (!ip_active(port) || ip_kotype(port) != IKOT_NONE) {
112 ip_unlock(port);
113
114 lck_mtx_unlock(&host_notify_lock);
115 zfree(host_notify_zone, entry);
116
117 return (KERN_FAILURE);
118 }
119
120 entry->port = port;
121 ipc_kobject_set_atomically(port, (ipc_kobject_t)entry, IKOT_HOST_NOTIFY);
122 ip_unlock(port);
123
124 enqueue_tail(&host_notify_queue[notify_type], (queue_entry_t)entry);
125 lck_mtx_unlock(&host_notify_lock);
126
127 return (KERN_SUCCESS);
128 }
129
130 void
131 host_notify_port_destroy(
132 ipc_port_t port)
133 {
134 host_notify_t entry;
135
136 lck_mtx_lock(&host_notify_lock);
137
138 ip_lock(port);
139 if (ip_kotype(port) == IKOT_HOST_NOTIFY) {
140 entry = (host_notify_t)port->ip_kobject;
141 assert(entry != NULL);
142 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
143 ip_unlock(port);
144
145 assert(entry->port == port);
146 remqueue(NULL, (queue_entry_t)entry);
147 lck_mtx_unlock(&host_notify_lock);
148 zfree(host_notify_zone, entry);
149
150 ipc_port_release_sonce(port);
151 return;
152 }
153 ip_unlock(port);
154
155 lck_mtx_unlock(&host_notify_lock);
156 }
157
158 static void
159 host_notify_all(
160 host_flavor_t notify_type,
161 mach_msg_header_t *msg,
162 mach_msg_size_t msg_size)
163 {
164 queue_t notify_queue = &host_notify_queue[notify_type];
165
166 lck_mtx_lock(&host_notify_lock);
167
168 if (!queue_empty(notify_queue)) {
169 queue_head_t send_queue;
170 host_notify_t entry;
171
172 send_queue = *notify_queue;
173 queue_init(notify_queue);
174
175 send_queue.next->prev = &send_queue;
176 send_queue.prev->next = &send_queue;
177
178 msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0);
179 msg->msgh_local_port = MACH_PORT_NULL;
180 msg->msgh_id = host_notify_replyid[notify_type];
181 msg->msgh_reserved = 0;
182
183 while ((entry = (host_notify_t)dequeue(&send_queue)) != NULL) {
184 ipc_port_t port;
185
186 port = entry->port;
187 assert(port != IP_NULL);
188
189 ip_lock(port);
190 assert(ip_kotype(port) == IKOT_HOST_NOTIFY);
191 assert(port->ip_kobject == (ipc_kobject_t)entry);
192 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
193 ip_unlock(port);
194
195 lck_mtx_unlock(&host_notify_lock);
196 zfree(host_notify_zone, entry);
197
198 msg->msgh_remote_port = port;
199
200 (void) mach_msg_send_from_kernel_proper(msg, msg_size);
201
202 lck_mtx_lock(&host_notify_lock);
203 }
204 }
205
206 lck_mtx_unlock(&host_notify_lock);
207 }
208
209 void
210 host_notify_calendar_change(void)
211 {
212 __Request__host_calendar_changed_t msg;
213
214 host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE, &msg.Head, sizeof (msg));
215 }