]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/host_notify.c
xnu-1228.15.4.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 static zone_t host_notify_zone;
49 decl_mutex_data(static,host_notify_lock)
50
51 static queue_head_t host_notify_queue[HOST_NOTIFY_TYPE_MAX+1];
52
53 static mach_msg_id_t host_notify_replyid[HOST_NOTIFY_TYPE_MAX+1] =
54 { HOST_CALENDAR_CHANGED_REPLYID };
55
56 struct host_notify_entry {
57 queue_chain_t entries;
58 ipc_port_t port;
59 };
60
61 typedef struct host_notify_entry *host_notify_t;
62
63 void
64 host_notify_init(void)
65 {
66 int i;
67
68 for (i = 0; i <= HOST_NOTIFY_TYPE_MAX; i++)
69 queue_init(&host_notify_queue[i]);
70
71 mutex_init(&host_notify_lock, 0);
72
73 i = sizeof (struct host_notify_entry);
74 host_notify_zone =
75 zinit(i, (4096 * i), (16 * i), "host_notify");
76 }
77
78 kern_return_t
79 host_request_notification(
80 host_t host,
81 host_flavor_t notify_type,
82 ipc_port_t port)
83 {
84 host_notify_t entry;
85
86 if (host == HOST_NULL)
87 return (KERN_INVALID_ARGUMENT);
88
89 if (!IP_VALID(port))
90 return (KERN_INVALID_CAPABILITY);
91
92 if (notify_type > HOST_NOTIFY_TYPE_MAX || notify_type < 0)
93 return (KERN_INVALID_ARGUMENT);
94
95 entry = (host_notify_t)zalloc(host_notify_zone);
96 if (entry == NULL)
97 return (KERN_RESOURCE_SHORTAGE);
98
99 mutex_lock(&host_notify_lock);
100
101 ip_lock(port);
102 if (!ip_active(port) || ip_kotype(port) != IKOT_NONE) {
103 ip_unlock(port);
104
105 mutex_unlock(&host_notify_lock);
106 zfree(host_notify_zone, entry);
107
108 return (KERN_FAILURE);
109 }
110
111 entry->port = port;
112 ipc_kobject_set_atomically(port, (ipc_kobject_t)entry, IKOT_HOST_NOTIFY);
113 ip_unlock(port);
114
115 enqueue_tail(&host_notify_queue[notify_type], (queue_entry_t)entry);
116 mutex_unlock(&host_notify_lock);
117
118 return (KERN_SUCCESS);
119 }
120
121 void
122 host_notify_port_destroy(
123 ipc_port_t port)
124 {
125 host_notify_t entry;
126
127 mutex_lock(&host_notify_lock);
128
129 ip_lock(port);
130 if (ip_kotype(port) == IKOT_HOST_NOTIFY) {
131 entry = (host_notify_t)port->ip_kobject;
132 assert(entry != NULL);
133 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
134 ip_unlock(port);
135
136 assert(entry->port == port);
137 remqueue(NULL, (queue_entry_t)entry);
138 mutex_unlock(&host_notify_lock);
139 zfree(host_notify_zone, entry);
140
141 ipc_port_release_sonce(port);
142 return;
143 }
144 ip_unlock(port);
145
146 mutex_unlock(&host_notify_lock);
147 }
148
149 static void
150 host_notify_all(
151 host_flavor_t notify_type,
152 mach_msg_header_t *msg,
153 mach_msg_size_t msg_size)
154 {
155 queue_t notify_queue = &host_notify_queue[notify_type];
156
157 mutex_lock(&host_notify_lock);
158
159 if (!queue_empty(notify_queue)) {
160 queue_head_t send_queue;
161 host_notify_t entry;
162
163 send_queue = *notify_queue;
164 queue_init(notify_queue);
165
166 send_queue.next->prev = &send_queue;
167 send_queue.prev->next = &send_queue;
168
169 msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0);
170 msg->msgh_local_port = MACH_PORT_NULL;
171 msg->msgh_id = host_notify_replyid[notify_type];
172 msg->msgh_reserved = 0;
173
174 while ((entry = (host_notify_t)dequeue(&send_queue)) != NULL) {
175 ipc_port_t port;
176
177 port = entry->port;
178 assert(port != IP_NULL);
179
180 ip_lock(port);
181 assert(ip_kotype(port) == IKOT_HOST_NOTIFY);
182 assert(port->ip_kobject == (ipc_kobject_t)entry);
183 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
184 ip_unlock(port);
185
186 mutex_unlock(&host_notify_lock);
187 zfree(host_notify_zone, entry);
188
189 msg->msgh_remote_port = port;
190
191 (void) mach_msg_send_from_kernel(msg, msg_size);
192
193 mutex_lock(&host_notify_lock);
194 }
195 }
196
197 mutex_unlock(&host_notify_lock);
198 }
199
200 void
201 host_notify_calendar_change(void)
202 {
203 __Request__host_calendar_changed_t msg;
204
205 host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE, &msg.Head, sizeof (msg));
206 }