]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/host_notify.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / kern / host_notify.c
CommitLineData
55e303ae 1/*
6d2010ae 2 * Copyright (c) 2003-2009 Apple Inc. All rights reserved.
55e303ae 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
55e303ae 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
55e303ae 27 */
55e303ae
A
28
29#include <mach/mach_types.h>
91447636 30#include <mach/mach_host.h>
55e303ae 31
91447636 32#include <kern/kern_types.h>
55e303ae
A
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
b0d623f7
A
40decl_lck_mtx_data(,host_notify_lock)
41
42lck_mtx_ext_t host_notify_lock_ext;
43lck_grp_t host_notify_lock_grp;
44lck_attr_t host_notify_lock_attr;
45static lck_grp_attr_t host_notify_lock_grp_attr;
55e303ae 46static zone_t host_notify_zone;
55e303ae
A
47
48static queue_head_t host_notify_queue[HOST_NOTIFY_TYPE_MAX+1];
49
50static mach_msg_id_t host_notify_replyid[HOST_NOTIFY_TYPE_MAX+1] =
51 { HOST_CALENDAR_CHANGED_REPLYID };
52
53struct host_notify_entry {
54 queue_chain_t entries;
55 ipc_port_t port;
56};
57
58typedef struct host_notify_entry *host_notify_t;
59
60void
61host_notify_init(void)
62{
63 int i;
64
65 for (i = 0; i <= HOST_NOTIFY_TYPE_MAX; i++)
66 queue_init(&host_notify_queue[i]);
67
b0d623f7
A
68 lck_grp_attr_setdefault(&host_notify_lock_grp_attr);
69 lck_grp_init(&host_notify_lock_grp, "host_notify", &host_notify_lock_grp_attr);
70 lck_attr_setdefault(&host_notify_lock_attr);
71
72 lck_mtx_init_ext(&host_notify_lock, &host_notify_lock_ext, &host_notify_lock_grp, &host_notify_lock_attr);
55e303ae
A
73
74 i = sizeof (struct host_notify_entry);
75 host_notify_zone =
76 zinit(i, (4096 * i), (16 * i), "host_notify");
77}
78
79kern_return_t
80host_request_notification(
81 host_t host,
82 host_flavor_t notify_type,
83 ipc_port_t port)
84{
85 host_notify_t entry;
86
87 if (host == HOST_NULL)
88 return (KERN_INVALID_ARGUMENT);
89
90 if (!IP_VALID(port))
91 return (KERN_INVALID_CAPABILITY);
92
93 if (notify_type > HOST_NOTIFY_TYPE_MAX || notify_type < 0)
94 return (KERN_INVALID_ARGUMENT);
95
96 entry = (host_notify_t)zalloc(host_notify_zone);
97 if (entry == NULL)
98 return (KERN_RESOURCE_SHORTAGE);
99
b0d623f7 100 lck_mtx_lock(&host_notify_lock);
55e303ae
A
101
102 ip_lock(port);
fe8ab488 103 if (!ip_active(port) || port->ip_tempowner || ip_kotype(port) != IKOT_NONE) {
55e303ae
A
104 ip_unlock(port);
105
b0d623f7 106 lck_mtx_unlock(&host_notify_lock);
91447636 107 zfree(host_notify_zone, entry);
55e303ae
A
108
109 return (KERN_FAILURE);
110 }
111
112 entry->port = port;
113 ipc_kobject_set_atomically(port, (ipc_kobject_t)entry, IKOT_HOST_NOTIFY);
114 ip_unlock(port);
115
116 enqueue_tail(&host_notify_queue[notify_type], (queue_entry_t)entry);
b0d623f7 117 lck_mtx_unlock(&host_notify_lock);
55e303ae
A
118
119 return (KERN_SUCCESS);
120}
121
122void
123host_notify_port_destroy(
124 ipc_port_t port)
125{
126 host_notify_t entry;
127
b0d623f7 128 lck_mtx_lock(&host_notify_lock);
55e303ae
A
129
130 ip_lock(port);
131 if (ip_kotype(port) == IKOT_HOST_NOTIFY) {
132 entry = (host_notify_t)port->ip_kobject;
133 assert(entry != NULL);
134 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
135 ip_unlock(port);
136
137 assert(entry->port == port);
6d2010ae 138 remqueue((queue_entry_t)entry);
b0d623f7 139 lck_mtx_unlock(&host_notify_lock);
91447636 140 zfree(host_notify_zone, entry);
55e303ae
A
141
142 ipc_port_release_sonce(port);
143 return;
144 }
145 ip_unlock(port);
146
b0d623f7 147 lck_mtx_unlock(&host_notify_lock);
55e303ae
A
148}
149
150static void
151host_notify_all(
152 host_flavor_t notify_type,
153 mach_msg_header_t *msg,
154 mach_msg_size_t msg_size)
155{
156 queue_t notify_queue = &host_notify_queue[notify_type];
157
b0d623f7 158 lck_mtx_lock(&host_notify_lock);
55e303ae
A
159
160 if (!queue_empty(notify_queue)) {
161 queue_head_t send_queue;
162 host_notify_t entry;
163
164 send_queue = *notify_queue;
165 queue_init(notify_queue);
166
167 send_queue.next->prev = &send_queue;
168 send_queue.prev->next = &send_queue;
169
fe8ab488
A
170 msg->msgh_bits =
171 MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, 0);
55e303ae 172 msg->msgh_local_port = MACH_PORT_NULL;
fe8ab488 173 msg->msgh_voucher_port = MACH_PORT_NULL;
55e303ae 174 msg->msgh_id = host_notify_replyid[notify_type];
55e303ae
A
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
b0d623f7 188 lck_mtx_unlock(&host_notify_lock);
91447636 189 zfree(host_notify_zone, entry);
55e303ae
A
190
191 msg->msgh_remote_port = port;
192
b0d623f7 193 (void) mach_msg_send_from_kernel_proper(msg, msg_size);
55e303ae 194
b0d623f7 195 lck_mtx_lock(&host_notify_lock);
55e303ae
A
196 }
197 }
198
b0d623f7 199 lck_mtx_unlock(&host_notify_lock);
55e303ae
A
200}
201
202void
203host_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}