]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_pset.c
xnu-792.25.20.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_pset.c
1 /*
2 * Copyright (c) 2000-2004 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>
70
71 #include <kern/kern_types.h>
72 #include <kern/spl.h>
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
87 kern_return_t
88 ipc_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;
105 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
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
126 kern_return_t
127 ipc_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;
144 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
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 */
158 boolean_t
159 ipc_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.
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
178 kern_return_t
179 ipc_pset_add(
180 ipc_pset_t pset,
181 ipc_port_t port)
182 {
183 kern_return_t kr;
184
185 assert(ips_active(pset));
186 assert(ip_active(port));
187
188 kr = ipc_mqueue_add(&port->ip_messages, &pset->ips_messages);
189
190 if (kr == KERN_SUCCESS)
191 port->ip_pset_count++;
192
193 return kr;
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
208 kern_return_t
209 ipc_pset_remove(
210 ipc_pset_t pset,
211 ipc_port_t port)
212 {
213 kern_return_t kr;
214
215 assert(ip_active(port));
216
217 if (port->ip_pset_count == 0)
218 return KERN_NOT_IN_SET;
219
220 kr = ipc_mqueue_remove(&port->ip_messages, &pset->ips_messages);
221
222 if (kr == KERN_SUCCESS)
223 port->ip_pset_count--;
224
225 return kr;
226 }
227
228 /*
229 * Routine: ipc_pset_remove_from_all
230 * Purpose:
231 * Removes a port from all it's port sets.
232 * Conditions:
233 * port is locked and active.
234 */
235
236 kern_return_t
237 ipc_pset_remove_from_all(
238 ipc_port_t port)
239 {
240 assert(ip_active(port));
241
242 if (port->ip_pset_count == 0)
243 return KERN_NOT_IN_SET;
244
245 /*
246 * Remove the port's mqueue from all sets
247 */
248 ipc_mqueue_remove_from_all(&port->ip_messages);
249 port->ip_pset_count = 0;
250 return KERN_SUCCESS;
251 }
252
253
254 /*
255 * Routine: ipc_pset_destroy
256 * Purpose:
257 * Destroys a port_set.
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
264 void
265 ipc_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
274 /*
275 * remove all the member message queues
276 */
277 ipc_mqueue_remove_all(&pset->ips_messages);
278
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
298 int
299 ipc_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 void
323 ipc_pset_print(
324 ipc_pset_t pset)
325 {
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 */