]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_pset.c
xnu-344.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_pset.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
A
70
71/*
72 * Routine: ipc_pset_alloc
73 * Purpose:
74 * Allocate a port set.
75 * Conditions:
76 * Nothing locked. If successful, the port set is returned
77 * locked. (The caller doesn't have a reference.)
78 * Returns:
79 * KERN_SUCCESS The port set is allocated.
80 * KERN_INVALID_TASK The space is dead.
81 * KERN_NO_SPACE No room for an entry in the space.
82 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
83 */
84
85kern_return_t
86ipc_pset_alloc(
87 ipc_space_t space,
88 mach_port_name_t *namep,
89 ipc_pset_t *psetp)
90{
91 ipc_pset_t pset;
92 mach_port_name_t name;
93 kern_return_t kr;
94
95 kr = ipc_object_alloc(space, IOT_PORT_SET,
96 MACH_PORT_TYPE_PORT_SET, 0,
97 &name, (ipc_object_t *) &pset);
98 if (kr != KERN_SUCCESS)
99 return kr;
100 /* pset is locked */
101
102 pset->ips_local_name = name;
1c79356b 103 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
1c79356b
A
104
105 *namep = name;
106 *psetp = pset;
107 return KERN_SUCCESS;
108}
109
110/*
111 * Routine: ipc_pset_alloc_name
112 * Purpose:
113 * Allocate a port set, with a specific name.
114 * Conditions:
115 * Nothing locked. If successful, the port set is returned
116 * locked. (The caller doesn't have a reference.)
117 * Returns:
118 * KERN_SUCCESS The port set is allocated.
119 * KERN_INVALID_TASK The space is dead.
120 * KERN_NAME_EXISTS The name already denotes a right.
121 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
122 */
123
124kern_return_t
125ipc_pset_alloc_name(
126 ipc_space_t space,
127 mach_port_name_t name,
128 ipc_pset_t *psetp)
129{
130 ipc_pset_t pset;
131 kern_return_t kr;
132
133
134 kr = ipc_object_alloc_name(space, IOT_PORT_SET,
135 MACH_PORT_TYPE_PORT_SET, 0,
136 name, (ipc_object_t *) &pset);
137 if (kr != KERN_SUCCESS)
138 return kr;
139 /* pset is locked */
140
141 pset->ips_local_name = name;
1c79356b 142 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
1c79356b
A
143
144 *psetp = pset;
145 return KERN_SUCCESS;
146}
147
148/*
149 * Routine: ipc_pset_member
150 * Purpose:
151 * Checks to see if a port is a member of a pset
152 * Conditions:
153 * Both port and port set are locked.
154 * The port must be active.
155 */
156boolean_t
157ipc_pset_member(
158 ipc_pset_t pset,
159 ipc_port_t port)
160{
161 assert(ip_active(port));
162
163 return (ipc_mqueue_member(&port->ip_messages, &pset->ips_messages));
164}
165
166
167/*
168 * Routine: ipc_pset_add
169 * Purpose:
170 * Puts a port into a port set.
1c79356b
A
171 * Conditions:
172 * Both port and port set are locked and active.
173 * The owner of the port set is also receiver for the port.
174 */
175
176kern_return_t
177ipc_pset_add(
178 ipc_pset_t pset,
179 ipc_port_t port)
180{
9bccf70c
A
181 kern_return_t kr;
182
1c79356b
A
183 assert(ips_active(pset));
184 assert(ip_active(port));
185
9bccf70c 186 kr = ipc_mqueue_add(&port->ip_messages, &pset->ips_messages);
1c79356b 187
9bccf70c
A
188 if (kr == KERN_SUCCESS)
189 port->ip_pset_count++;
1c79356b 190
9bccf70c 191 return kr;
1c79356b
A
192}
193
194
195
196/*
197 * Routine: ipc_pset_remove
198 * Purpose:
199 * Removes a port from a port set.
200 * The port set loses a reference.
201 * Conditions:
202 * Both port and port set are locked.
203 * The port must be active.
204 */
205
206kern_return_t
207ipc_pset_remove(
208 ipc_pset_t pset,
209 ipc_port_t port)
210{
9bccf70c 211 kern_return_t kr;
1c79356b
A
212
213 assert(ip_active(port));
214
215 if (port->ip_pset_count == 0)
216 return KERN_NOT_IN_SET;
217
9bccf70c 218 kr = ipc_mqueue_remove(&port->ip_messages, &pset->ips_messages);
1c79356b 219
9bccf70c 220 if (kr == KERN_SUCCESS)
1c79356b 221 port->ip_pset_count--;
9bccf70c
A
222
223 return kr;
1c79356b
A
224}
225
226/*
9bccf70c 227 * Routine: ipc_pset_remove_from_all
1c79356b
A
228 * Purpose:
229 * Removes a port from all it's port sets.
1c79356b
A
230 * Conditions:
231 * port is locked and active.
232 */
233
234kern_return_t
9bccf70c 235ipc_pset_remove_from_all(
1c79356b
A
236 ipc_port_t port)
237{
1c79356b
A
238 ipc_pset_t pset;
239
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 */
322
323void
324ipc_pset_print(
325 ipc_pset_t pset)
326{
327 extern int db_indent;
328
329 printf("pset 0x%x\n", pset);
330
331 db_indent += 2;
332
333 ipc_object_print(&pset->ips_object);
334 iprintf("local_name = 0x%x\n", pset->ips_local_name);
335 iprintf("%d kmsgs => 0x%x",
336 ipc_list_count(pset->ips_messages.imq_messages.ikmq_base),
337 pset->ips_messages.imq_messages.ikmq_base);
338 printf(",rcvrs queue= 0x%x\n", &pset->ips_messages.imq_wait_queue);
339
340 db_indent -=2;
341}
342
343#endif /* MACH_KDB */