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