]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_pset.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_pset.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: ipc/ipc_pset.c
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Functions to manipulate IPC port sets.
64 */
65
66#include <mach/port.h>
67#include <mach/kern_return.h>
68#include <mach/message.h>
69#include <ipc/ipc_mqueue.h>
70#include <ipc/ipc_object.h>
71#include <ipc/ipc_pset.h>
72#include <ipc/ipc_right.h>
73#include <ipc/ipc_space.h>
74#include <ipc/ipc_port.h>
75#include <ipc/ipc_print.h>
1c79356b 76
91447636
A
77#include <kern/kern_types.h>
78#include <kern/spl.h>
1c79356b
A
79/*
80 * Routine: ipc_pset_alloc
81 * Purpose:
82 * Allocate a port set.
83 * Conditions:
84 * Nothing locked. If successful, the port set is returned
85 * locked. (The caller doesn't have a reference.)
86 * Returns:
87 * KERN_SUCCESS The port set is allocated.
88 * KERN_INVALID_TASK The space is dead.
89 * KERN_NO_SPACE No room for an entry in the space.
90 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
91 */
92
93kern_return_t
94ipc_pset_alloc(
95 ipc_space_t space,
96 mach_port_name_t *namep,
97 ipc_pset_t *psetp)
98{
99 ipc_pset_t pset;
100 mach_port_name_t name;
101 kern_return_t kr;
102
103 kr = ipc_object_alloc(space, IOT_PORT_SET,
104 MACH_PORT_TYPE_PORT_SET, 0,
105 &name, (ipc_object_t *) &pset);
106 if (kr != KERN_SUCCESS)
107 return kr;
108 /* pset is locked */
109
110 pset->ips_local_name = name;
1c79356b 111 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
1c79356b
A
112
113 *namep = name;
114 *psetp = pset;
115 return KERN_SUCCESS;
116}
117
118/*
119 * Routine: ipc_pset_alloc_name
120 * Purpose:
121 * Allocate a port set, with a specific name.
122 * Conditions:
123 * Nothing locked. If successful, the port set is returned
124 * locked. (The caller doesn't have a reference.)
125 * Returns:
126 * KERN_SUCCESS The port set is allocated.
127 * KERN_INVALID_TASK The space is dead.
128 * KERN_NAME_EXISTS The name already denotes a right.
129 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
130 */
131
132kern_return_t
133ipc_pset_alloc_name(
134 ipc_space_t space,
135 mach_port_name_t name,
136 ipc_pset_t *psetp)
137{
138 ipc_pset_t pset;
139 kern_return_t kr;
140
141
142 kr = ipc_object_alloc_name(space, IOT_PORT_SET,
143 MACH_PORT_TYPE_PORT_SET, 0,
144 name, (ipc_object_t *) &pset);
145 if (kr != KERN_SUCCESS)
146 return kr;
147 /* pset is locked */
148
149 pset->ips_local_name = name;
1c79356b 150 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
1c79356b
A
151
152 *psetp = pset;
153 return KERN_SUCCESS;
154}
155
156/*
157 * Routine: ipc_pset_member
158 * Purpose:
159 * Checks to see if a port is a member of a pset
160 * Conditions:
161 * Both port and port set are locked.
162 * The port must be active.
163 */
164boolean_t
165ipc_pset_member(
166 ipc_pset_t pset,
167 ipc_port_t port)
168{
169 assert(ip_active(port));
170
171 return (ipc_mqueue_member(&port->ip_messages, &pset->ips_messages));
172}
173
174
175/*
176 * Routine: ipc_pset_add
177 * Purpose:
178 * Puts a port into a port set.
1c79356b
A
179 * Conditions:
180 * Both port and port set are locked and active.
181 * The owner of the port set is also receiver for the port.
182 */
183
184kern_return_t
185ipc_pset_add(
186 ipc_pset_t pset,
187 ipc_port_t port)
188{
9bccf70c
A
189 kern_return_t kr;
190
1c79356b
A
191 assert(ips_active(pset));
192 assert(ip_active(port));
193
9bccf70c 194 kr = ipc_mqueue_add(&port->ip_messages, &pset->ips_messages);
1c79356b 195
9bccf70c
A
196 if (kr == KERN_SUCCESS)
197 port->ip_pset_count++;
1c79356b 198
9bccf70c 199 return kr;
1c79356b
A
200}
201
202
203
204/*
205 * Routine: ipc_pset_remove
206 * Purpose:
207 * Removes a port from a port set.
208 * The port set loses a reference.
209 * Conditions:
210 * Both port and port set are locked.
211 * The port must be active.
212 */
213
214kern_return_t
215ipc_pset_remove(
216 ipc_pset_t pset,
217 ipc_port_t port)
218{
9bccf70c 219 kern_return_t kr;
1c79356b
A
220
221 assert(ip_active(port));
222
223 if (port->ip_pset_count == 0)
224 return KERN_NOT_IN_SET;
225
9bccf70c 226 kr = ipc_mqueue_remove(&port->ip_messages, &pset->ips_messages);
1c79356b 227
9bccf70c 228 if (kr == KERN_SUCCESS)
1c79356b 229 port->ip_pset_count--;
9bccf70c
A
230
231 return kr;
1c79356b
A
232}
233
234/*
9bccf70c 235 * Routine: ipc_pset_remove_from_all
1c79356b
A
236 * Purpose:
237 * Removes a port from all it's port sets.
1c79356b
A
238 * Conditions:
239 * port is locked and active.
240 */
241
242kern_return_t
9bccf70c 243ipc_pset_remove_from_all(
1c79356b
A
244 ipc_port_t port)
245{
1c79356b
A
246 assert(ip_active(port));
247
248 if (port->ip_pset_count == 0)
249 return KERN_NOT_IN_SET;
250
251 /*
9bccf70c 252 * Remove the port's mqueue from all sets
1c79356b 253 */
9bccf70c
A
254 ipc_mqueue_remove_from_all(&port->ip_messages);
255 port->ip_pset_count = 0;
1c79356b
A
256 return KERN_SUCCESS;
257}
258
259
260/*
261 * Routine: ipc_pset_destroy
262 * Purpose:
263 * Destroys a port_set.
1c79356b
A
264 * Conditions:
265 * The port_set is locked and alive.
266 * The caller has a reference, which is consumed.
267 * Afterwards, the port_set is unlocked and dead.
268 */
269
270void
271ipc_pset_destroy(
272 ipc_pset_t pset)
273{
274 spl_t s;
275
276 assert(ips_active(pset));
277
278 pset->ips_object.io_bits &= ~IO_BITS_ACTIVE;
279
9bccf70c
A
280 /*
281 * remove all the member message queues
282 */
283 ipc_mqueue_remove_all(&pset->ips_messages);
284
1c79356b
A
285 s = splsched();
286 imq_lock(&pset->ips_messages);
287 ipc_mqueue_changed(&pset->ips_messages);
288 imq_unlock(&pset->ips_messages);
289 splx(s);
290
291 /* XXXX Perhaps ought to verify ips_thread_pool is empty */
292
293 ips_release(pset); /* consume the ref our caller gave us */
294 ips_check_unlock(pset);
295}
296
297#include <mach_kdb.h>
298#if MACH_KDB
299
300#include <ddb/db_output.h>
301
302#define printf kdbprintf
303
304int
305ipc_list_count(
306 struct ipc_kmsg *base)
307{
308 register int count = 0;
309
310 if (base) {
311 struct ipc_kmsg *kmsg = base;
312
313 ++count;
314 while (kmsg && kmsg->ikm_next != base
315 && kmsg->ikm_next != IKM_BOGUS){
316 kmsg = kmsg->ikm_next;
317 ++count;
318 }
319 }
320 return(count);
321}
322
323/*
324 * Routine: ipc_pset_print
325 * Purpose:
326 * Pretty-print a port set for kdb.
327 */
1c79356b
A
328void
329ipc_pset_print(
330 ipc_pset_t pset)
331{
1c79356b
A
332 printf("pset 0x%x\n", pset);
333
334 db_indent += 2;
335
336 ipc_object_print(&pset->ips_object);
337 iprintf("local_name = 0x%x\n", pset->ips_local_name);
338 iprintf("%d kmsgs => 0x%x",
339 ipc_list_count(pset->ips_messages.imq_messages.ikmq_base),
340 pset->ips_messages.imq_messages.ikmq_base);
341 printf(",rcvrs queue= 0x%x\n", &pset->ips_messages.imq_wait_queue);
342
343 db_indent -=2;
344}
345
346#endif /* MACH_KDB */