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