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