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