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