2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
61 * Copyright (c) 2005-2006 SPARTA, Inc.
66 * File: ipc/ipc_object.c
70 * Functions to manipulate IPC objects.
73 #include <mach/mach_types.h>
74 #include <mach/boolean.h>
75 #include <mach/kern_return.h>
76 #include <mach/port.h>
77 #include <mach/message.h>
79 #include <kern/kern_types.h>
80 #include <kern/misc_protos.h>
81 #include <kern/ipc_kobject.h>
83 #include <ipc/ipc_types.h>
84 #include <ipc/ipc_importance.h>
86 #include <ipc/ipc_space.h>
87 #include <ipc/ipc_entry.h>
88 #include <ipc/ipc_object.h>
89 #include <ipc/ipc_hash.h>
90 #include <ipc/ipc_right.h>
91 #include <ipc/ipc_notify.h>
92 #include <ipc/ipc_port.h>
93 #include <ipc/ipc_pset.h>
95 #include <security/mac_mach_internal.h>
97 zone_t ipc_object_zones
[IOT_NUMBER
];
100 * Routine: ipc_object_reference
102 * Take a reference to an object.
106 ipc_object_reference(
109 io_reference(object
);
113 * Routine: ipc_object_release
115 * Release a reference to an object.
126 * Routine: ipc_object_translate
128 * Look up an object in a space.
130 * Nothing locked before. If successful, the object
131 * is returned locked. The caller doesn't get a ref.
133 * KERN_SUCCESS Object returned locked.
134 * KERN_INVALID_TASK The space is dead.
135 * KERN_INVALID_NAME The name doesn't denote a right
136 * KERN_INVALID_RIGHT Name doesn't denote the correct right
139 ipc_object_translate(
141 mach_port_name_t name
,
142 mach_port_right_t right
,
143 ipc_object_t
*objectp
)
149 kr
= ipc_right_lookup_read(space
, name
, &entry
);
150 if (kr
!= KERN_SUCCESS
)
152 /* space is read-locked and active */
154 if ((entry
->ie_bits
& MACH_PORT_TYPE(right
)) == MACH_PORT_TYPE_NONE
) {
155 is_read_unlock(space
);
156 return KERN_INVALID_RIGHT
;
159 object
= entry
->ie_object
;
160 assert(object
!= IO_NULL
);
163 is_read_unlock(space
);
170 * Routine: ipc_object_translate_two
172 * Look up two objects in a space.
174 * Nothing locked before. If successful, the objects
175 * are returned locked. The caller doesn't get a ref.
177 * KERN_SUCCESS Objects returned locked.
178 * KERN_INVALID_TASK The space is dead.
179 * KERN_INVALID_NAME A name doesn't denote a right.
180 * KERN_INVALID_RIGHT A name doesn't denote the correct right.
184 ipc_object_translate_two(
186 mach_port_name_t name1
,
187 mach_port_right_t right1
,
188 ipc_object_t
*objectp1
,
189 mach_port_name_t name2
,
190 mach_port_right_t right2
,
191 ipc_object_t
*objectp2
)
198 kr
= ipc_right_lookup_two_read(space
, name1
, &entry1
, name2
, &entry2
);
199 if (kr
!= KERN_SUCCESS
)
201 /* space is read-locked and active */
203 if ((entry1
->ie_bits
& MACH_PORT_TYPE(right1
)) == MACH_PORT_TYPE_NONE
) {
204 is_read_unlock(space
);
205 return KERN_INVALID_RIGHT
;
208 if ((entry2
->ie_bits
& MACH_PORT_TYPE(right2
)) == MACH_PORT_TYPE_NONE
) {
209 is_read_unlock(space
);
210 return KERN_INVALID_RIGHT
;
213 object
= entry1
->ie_object
;
214 assert(object
!= IO_NULL
);
218 object
= entry2
->ie_object
;
219 assert(object
!= IO_NULL
);
223 is_read_unlock(space
);
228 * Routine: ipc_object_alloc_dead
230 * Allocate a dead-name entry.
234 * KERN_SUCCESS The dead name is allocated.
235 * KERN_INVALID_TASK The space is dead.
236 * KERN_NO_SPACE No room for an entry in the space.
237 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
241 ipc_object_alloc_dead(
243 mach_port_name_t
*namep
)
248 kr
= ipc_entry_alloc(space
, namep
, &entry
);
249 if (kr
!= KERN_SUCCESS
)
251 /* space is write-locked */
253 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
255 assert(entry
->ie_object
== IO_NULL
);
256 entry
->ie_bits
|= MACH_PORT_TYPE_DEAD_NAME
| 1;
257 ipc_entry_modified(space
, *namep
, entry
);
258 is_write_unlock(space
);
263 * Routine: ipc_object_alloc_dead_name
265 * Allocate a dead-name entry, with a specific name.
269 * KERN_SUCCESS The dead name is allocated.
270 * KERN_INVALID_TASK The space is dead.
271 * KERN_NAME_EXISTS The name already denotes a right.
272 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
276 ipc_object_alloc_dead_name(
278 mach_port_name_t name
)
283 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
284 if (kr
!= KERN_SUCCESS
)
286 /* space is write-locked */
288 if (ipc_right_inuse(space
, name
, entry
))
289 return KERN_NAME_EXISTS
;
291 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
293 assert(entry
->ie_object
== IO_NULL
);
294 entry
->ie_bits
|= MACH_PORT_TYPE_DEAD_NAME
| 1;
295 ipc_entry_modified(space
, name
, entry
);
296 is_write_unlock(space
);
301 * Routine: ipc_object_alloc
303 * Allocate an object.
305 * Nothing locked. If successful, the object is returned locked.
306 * The space is write locked on successful return.
307 * The caller doesn't get a reference for the object.
309 * KERN_SUCCESS The object is allocated.
310 * KERN_INVALID_TASK The space is dead.
311 * KERN_NO_SPACE No room for an entry in the space.
312 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
318 ipc_object_type_t otype
,
319 mach_port_type_t type
,
320 mach_port_urefs_t urefs
,
321 mach_port_name_t
*namep
,
322 ipc_object_t
*objectp
)
328 assert(otype
< IOT_NUMBER
);
329 assert((type
& MACH_PORT_TYPE_ALL_RIGHTS
) == type
);
330 assert(type
!= MACH_PORT_TYPE_NONE
);
331 assert(urefs
<= MACH_PORT_UREFS_MAX
);
333 object
= io_alloc(otype
);
334 if (object
== IO_NULL
)
335 return KERN_RESOURCE_SHORTAGE
;
337 if (otype
== IOT_PORT
) {
338 ipc_port_t port
= (ipc_port_t
)object
;
340 bzero((char *)port
, sizeof(*port
));
341 } else if (otype
== IOT_PORT_SET
) {
342 ipc_pset_t pset
= (ipc_pset_t
)object
;
344 bzero((char *)pset
, sizeof(*pset
));
347 io_lock_init(object
);
348 *namep
= CAST_MACH_PORT_TO_NAME(object
);
349 kr
= ipc_entry_alloc(space
, namep
, &entry
);
350 if (kr
!= KERN_SUCCESS
) {
351 io_free(otype
, object
);
354 /* space is write-locked */
356 entry
->ie_bits
|= type
| urefs
;
357 entry
->ie_object
= object
;
358 ipc_entry_modified(space
, *namep
, entry
);
362 object
->io_references
= 1; /* for entry, not caller */
363 object
->io_bits
= io_makebits(TRUE
, otype
, 0);
370 * Routine: ipc_object_alloc_name
372 * Allocate an object, with a specific name.
374 * Nothing locked. If successful, the object is returned locked.
375 * The caller doesn't get a reference for the object.
377 * KERN_SUCCESS The object is allocated.
378 * KERN_INVALID_TASK The space is dead.
379 * KERN_NAME_EXISTS The name already denotes a right.
380 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
384 ipc_object_alloc_name(
386 ipc_object_type_t otype
,
387 mach_port_type_t type
,
388 mach_port_urefs_t urefs
,
389 mach_port_name_t name
,
390 ipc_object_t
*objectp
)
396 assert(otype
< IOT_NUMBER
);
397 assert((type
& MACH_PORT_TYPE_ALL_RIGHTS
) == type
);
398 assert(type
!= MACH_PORT_TYPE_NONE
);
399 assert(urefs
<= MACH_PORT_UREFS_MAX
);
401 object
= io_alloc(otype
);
402 if (object
== IO_NULL
)
403 return KERN_RESOURCE_SHORTAGE
;
405 if (otype
== IOT_PORT
) {
406 ipc_port_t port
= (ipc_port_t
)object
;
408 bzero((char *)port
, sizeof(*port
));
409 } else if (otype
== IOT_PORT_SET
) {
410 ipc_pset_t pset
= (ipc_pset_t
)object
;
412 bzero((char *)pset
, sizeof(*pset
));
415 io_lock_init(object
);
416 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
417 if (kr
!= KERN_SUCCESS
) {
418 io_free(otype
, object
);
421 /* space is write-locked */
423 if (ipc_right_inuse(space
, name
, entry
)) {
424 io_free(otype
, object
);
425 return KERN_NAME_EXISTS
;
428 entry
->ie_bits
|= type
| urefs
;
429 entry
->ie_object
= object
;
430 ipc_entry_modified(space
, name
, entry
);
433 is_write_unlock(space
);
435 object
->io_references
= 1; /* for entry, not caller */
436 object
->io_bits
= io_makebits(TRUE
, otype
, 0);
443 * Routine: ipc_object_copyin_type
445 * Convert a send type name to a received type name.
449 ipc_object_copyin_type(
450 mach_msg_type_name_t msgt_name
)
454 case MACH_MSG_TYPE_MOVE_RECEIVE
:
455 return MACH_MSG_TYPE_PORT_RECEIVE
;
457 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
458 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
459 return MACH_MSG_TYPE_PORT_SEND_ONCE
;
461 case MACH_MSG_TYPE_MOVE_SEND
:
462 case MACH_MSG_TYPE_MAKE_SEND
:
463 case MACH_MSG_TYPE_COPY_SEND
:
464 return MACH_MSG_TYPE_PORT_SEND
;
466 case MACH_MSG_TYPE_DISPOSE_RECEIVE
:
467 case MACH_MSG_TYPE_DISPOSE_SEND
:
468 case MACH_MSG_TYPE_DISPOSE_SEND_ONCE
:
471 return MACH_MSG_TYPE_PORT_NONE
;
476 * Routine: ipc_object_copyin
478 * Copyin a capability from a space.
479 * If successful, the caller gets a ref
480 * for the resulting object, unless it is IO_DEAD.
484 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
485 * KERN_INVALID_TASK The space is dead.
486 * KERN_INVALID_NAME Name doesn't exist in space.
487 * KERN_INVALID_RIGHT Name doesn't denote correct right.
493 mach_port_name_t name
,
494 mach_msg_type_name_t msgt_name
,
495 ipc_object_t
*objectp
)
499 ipc_port_t release_port
;
504 * Could first try a read lock when doing
505 * MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND,
506 * and MACH_MSG_TYPE_MAKE_SEND_ONCE.
509 kr
= ipc_right_lookup_write(space
, name
, &entry
);
510 if (kr
!= KERN_SUCCESS
)
512 /* space is write-locked and active */
514 release_port
= IP_NULL
;
515 kr
= ipc_right_copyin(space
, name
, entry
,
520 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
521 ipc_entry_dealloc(space
, name
, entry
);
522 is_write_unlock(space
);
524 #if IMPORTANCE_INHERITANCE
525 if (0 < assertcnt
&& ipc_importance_task_is_any_receiver_type(current_task()->task_imp_base
)) {
526 ipc_importance_task_drop_internal_assertion(current_task()->task_imp_base
, assertcnt
);
528 #endif /* IMPORTANCE_INHERITANCE */
530 if (release_port
!= IP_NULL
)
531 ip_release(release_port
);
533 if ((kr
== KERN_SUCCESS
) && (soright
!= IP_NULL
))
534 ipc_notify_port_deleted(soright
, name
);
540 * Routine: ipc_object_copyin_from_kernel
542 * Copyin a naked capability from the kernel.
544 * MACH_MSG_TYPE_MOVE_RECEIVE
545 * The receiver must be ipc_space_kernel
546 * or the receive right must already be in limbo.
547 * Consumes the naked receive right.
548 * MACH_MSG_TYPE_COPY_SEND
549 * A naked send right must be supplied.
550 * The port gains a reference, and a send right
551 * if the port is still active.
552 * MACH_MSG_TYPE_MAKE_SEND
553 * The receiver must be ipc_space_kernel.
554 * The port gains a reference and a send right.
555 * MACH_MSG_TYPE_MOVE_SEND
556 * Consumes a naked send right.
557 * MACH_MSG_TYPE_MAKE_SEND_ONCE
558 * The port gains a reference and a send-once right.
559 * Receiver also be the caller of device subsystem,
561 * MACH_MSG_TYPE_MOVE_SEND_ONCE
562 * Consumes a naked send-once right.
568 ipc_object_copyin_from_kernel(
570 mach_msg_type_name_t msgt_name
)
572 assert(IO_VALID(object
));
575 case MACH_MSG_TYPE_MOVE_RECEIVE
: {
576 ipc_port_t port
= (ipc_port_t
) object
;
579 assert(ip_active(port
));
580 if (port
->ip_destination
!= IP_NULL
) {
581 assert(port
->ip_receiver
== ipc_space_kernel
);
583 /* relevant part of ipc_port_clear_receiver */
584 ipc_port_set_mscount(port
, 0);
586 port
->ip_receiver_name
= MACH_PORT_NULL
;
587 port
->ip_destination
= IP_NULL
;
593 case MACH_MSG_TYPE_COPY_SEND
: {
594 ipc_port_t port
= (ipc_port_t
) object
;
597 if (ip_active(port
)) {
598 assert(port
->ip_srights
> 0);
606 case MACH_MSG_TYPE_MAKE_SEND
: {
607 ipc_port_t port
= (ipc_port_t
) object
;
610 if (ip_active(port
)) {
611 assert(port
->ip_receiver_name
!= MACH_PORT_NULL
);
612 assert((port
->ip_receiver
== ipc_space_kernel
) ||
613 (port
->ip_receiver
->is_node_id
!= HOST_LOCAL_NODE
));
623 case MACH_MSG_TYPE_MOVE_SEND
: {
624 /* move naked send right into the message */
625 assert(((ipc_port_t
)object
)->ip_srights
);
629 case MACH_MSG_TYPE_MAKE_SEND_ONCE
: {
630 ipc_port_t port
= (ipc_port_t
) object
;
633 if (ip_active(port
)) {
634 assert(port
->ip_receiver_name
!= MACH_PORT_NULL
);
642 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
643 /* move naked send-once right into the message */
644 assert(((ipc_port_t
)object
)->ip_sorights
);
649 panic("ipc_object_copyin_from_kernel: strange rights");
654 * Routine: ipc_object_destroy
656 * Destroys a naked capability.
657 * Consumes a ref for the object.
659 * A receive right should be in limbo or in transit.
667 mach_msg_type_name_t msgt_name
)
669 assert(IO_VALID(object
));
670 assert(io_otype(object
) == IOT_PORT
);
673 case MACH_MSG_TYPE_PORT_SEND
:
674 ipc_port_release_send((ipc_port_t
) object
);
677 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
678 ipc_notify_send_once((ipc_port_t
) object
);
681 case MACH_MSG_TYPE_PORT_RECEIVE
:
682 ipc_port_release_receive((ipc_port_t
) object
);
686 panic("ipc_object_destroy: strange rights");
691 * Routine: ipc_object_destroy_dest
693 * Destroys a naked capability for the destination of
694 * of a message. Consumes a ref for the object.
701 ipc_object_destroy_dest(
703 mach_msg_type_name_t msgt_name
)
705 assert(IO_VALID(object
));
706 assert(io_otype(object
) == IOT_PORT
);
709 case MACH_MSG_TYPE_PORT_SEND
:
710 ipc_port_release_send((ipc_port_t
) object
);
713 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
714 if (io_active(object
) &&
715 !ip_full_kernel((ipc_port_t
) object
))
716 ipc_notify_send_once((ipc_port_t
) object
);
718 ipc_port_release_sonce((ipc_port_t
) object
);
722 panic("ipc_object_destroy_dest: strange rights");
727 * Routine: ipc_object_copyout
729 * Copyout a capability, placing it into a space.
730 * If successful, consumes a ref for the object.
734 * KERN_SUCCESS Copied out object, consumed ref.
735 * KERN_INVALID_TASK The space is dead.
736 * KERN_INVALID_CAPABILITY The object is dead.
737 * KERN_NO_SPACE No room in space for another right.
738 * KERN_RESOURCE_SHORTAGE No memory available.
739 * KERN_UREFS_OVERFLOW Urefs limit exceeded
740 * and overflow wasn't specified.
747 mach_msg_type_name_t msgt_name
,
749 mach_port_name_t
*namep
)
751 mach_port_name_t name
;
755 assert(IO_VALID(object
));
756 assert(io_otype(object
) == IOT_PORT
);
758 is_write_lock(space
);
761 if (!is_active(space
)) {
762 is_write_unlock(space
);
763 return KERN_INVALID_TASK
;
766 if ((msgt_name
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
767 ipc_right_reverse(space
, object
, &name
, &entry
)) {
768 /* object is locked and active */
770 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
774 name
= CAST_MACH_PORT_TO_NAME(object
);
775 kr
= ipc_entry_get(space
, &name
, &entry
);
776 if (kr
!= KERN_SUCCESS
) {
777 /* unlocks/locks space, so must start again */
779 kr
= ipc_entry_grow_table(space
, ITS_SIZE_NONE
);
780 if (kr
!= KERN_SUCCESS
)
781 return kr
; /* space is unlocked */
786 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
);
787 assert(entry
->ie_object
== IO_NULL
);
790 if (!io_active(object
)) {
792 ipc_entry_dealloc(space
, name
, entry
);
793 is_write_unlock(space
);
794 return KERN_INVALID_CAPABILITY
;
797 entry
->ie_object
= object
;
801 /* space is write-locked and active, object is locked and active */
803 kr
= ipc_right_copyout(space
, name
, entry
,
804 msgt_name
, overflow
, object
);
806 /* object is unlocked */
807 is_write_unlock(space
);
809 if (kr
== KERN_SUCCESS
)
815 * Routine: ipc_object_copyout_name
817 * Copyout a capability, placing it into a space.
818 * The specified name is used for the capability.
819 * If successful, consumes a ref for the object.
823 * KERN_SUCCESS Copied out object, consumed ref.
824 * KERN_INVALID_TASK The space is dead.
825 * KERN_INVALID_CAPABILITY The object is dead.
826 * KERN_RESOURCE_SHORTAGE No memory available.
827 * KERN_UREFS_OVERFLOW Urefs limit exceeded
828 * and overflow wasn't specified.
829 * KERN_RIGHT_EXISTS Space has rights under another name.
830 * KERN_NAME_EXISTS Name is already used.
834 ipc_object_copyout_name(
837 mach_msg_type_name_t msgt_name
,
839 mach_port_name_t name
)
841 mach_port_name_t oname
;
846 #if IMPORTANCE_INHERITANCE
848 ipc_importance_task_t task_imp
= IIT_NULL
;
849 #endif /* IMPORTANCE_INHERITANCE */
851 assert(IO_VALID(object
));
852 assert(io_otype(object
) == IOT_PORT
);
854 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
855 if (kr
!= KERN_SUCCESS
)
857 /* space is write-locked and active */
859 if ((msgt_name
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
860 ipc_right_reverse(space
, object
, &oname
, &oentry
)) {
861 /* object is locked and active */
866 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
867 ipc_entry_dealloc(space
, name
, entry
);
869 is_write_unlock(space
);
870 return KERN_RIGHT_EXISTS
;
873 assert(entry
== oentry
);
874 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
876 if (ipc_right_inuse(space
, name
, entry
))
877 return KERN_NAME_EXISTS
;
879 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
);
880 assert(entry
->ie_object
== IO_NULL
);
883 if (!io_active(object
)) {
885 ipc_entry_dealloc(space
, name
, entry
);
886 is_write_unlock(space
);
887 return KERN_INVALID_CAPABILITY
;
890 entry
->ie_object
= object
;
893 /* space is write-locked and active, object is locked and active */
895 #if IMPORTANCE_INHERITANCE
897 * We are slamming a receive right into the space, without
898 * first having been enqueued on a port destined there. So,
899 * we have to arrange to boost the task appropriately if this
900 * port has assertions (and the task wants them).
902 if (msgt_name
== MACH_MSG_TYPE_PORT_RECEIVE
) {
903 ipc_port_t port
= (ipc_port_t
)object
;
905 if (space
->is_task
!= TASK_NULL
) {
906 task_imp
= space
->is_task
->task_imp_base
;
907 if (ipc_importance_task_is_any_receiver_type(task_imp
)) {
908 assertcnt
= port
->ip_impcount
;
909 ipc_importance_task_reference(task_imp
);
915 /* take port out of limbo */
916 assert(port
->ip_tempowner
!= 0);
917 port
->ip_tempowner
= 0;
920 #endif /* IMPORTANCE_INHERITANCE */
922 kr
= ipc_right_copyout(space
, name
, entry
,
923 msgt_name
, overflow
, object
);
925 /* object is unlocked */
926 is_write_unlock(space
);
928 #if IMPORTANCE_INHERITANCE
930 * Add the assertions to the task that we captured before
932 if (task_imp
!= IIT_NULL
) {
933 ipc_importance_task_hold_internal_assertion(task_imp
, assertcnt
);
934 ipc_importance_task_release(task_imp
);
936 #endif /* IMPORTANCE_INHERITANCE */
942 * Routine: ipc_object_copyout_dest
944 * Translates/consumes the destination right of a message.
945 * This is unlike normal copyout because the right is consumed
946 * in a funny way instead of being given to the receiving space.
947 * The receiver gets his name for the port, if he has receive
948 * rights, otherwise MACH_PORT_NULL.
950 * The object is locked and active. Nothing else locked.
951 * The object is unlocked and loses a reference.
955 ipc_object_copyout_dest(
958 mach_msg_type_name_t msgt_name
,
959 mach_port_name_t
*namep
)
961 mach_port_name_t name
;
963 assert(IO_VALID(object
));
964 assert(io_active(object
));
969 * If the space is the receiver/owner of the object,
970 * then we quietly consume the right and return
971 * the space's name for the object. Otherwise
972 * we destroy the right and return MACH_PORT_NULL.
976 case MACH_MSG_TYPE_PORT_SEND
: {
977 ipc_port_t port
= (ipc_port_t
) object
;
978 ipc_port_t nsrequest
= IP_NULL
;
979 mach_port_mscount_t mscount
;
981 if (port
->ip_receiver
== space
)
982 name
= port
->ip_receiver_name
;
984 name
= MACH_PORT_NULL
;
986 assert(port
->ip_srights
> 0);
987 if (--port
->ip_srights
== 0 &&
988 port
->ip_nsrequest
!= IP_NULL
) {
989 nsrequest
= port
->ip_nsrequest
;
990 port
->ip_nsrequest
= IP_NULL
;
991 mscount
= port
->ip_mscount
;
993 ipc_notify_no_senders(nsrequest
, mscount
);
999 case MACH_MSG_TYPE_PORT_SEND_ONCE
: {
1000 ipc_port_t port
= (ipc_port_t
) object
;
1002 assert(port
->ip_sorights
> 0);
1004 if (port
->ip_receiver
== space
) {
1005 /* quietly consume the send-once right */
1007 port
->ip_sorights
--;
1008 name
= port
->ip_receiver_name
;
1012 * A very bizarre case. The message
1013 * was received, but before this copyout
1014 * happened the space lost receive rights.
1015 * We can't quietly consume the soright
1016 * out from underneath some other task,
1017 * so generate a send-once notification.
1020 ip_reference(port
); /* restore ref */
1023 ipc_notify_send_once(port
);
1024 name
= MACH_PORT_NULL
;
1031 panic("ipc_object_copyout_dest: strange rights");
1032 name
= MACH_PORT_DEAD
;
1039 * Routine: ipc_object_rename
1041 * Rename an entry in a space.
1045 * KERN_SUCCESS Renamed the entry.
1046 * KERN_INVALID_TASK The space was dead.
1047 * KERN_INVALID_NAME oname didn't denote an entry.
1048 * KERN_NAME_EXISTS nname already denoted an entry.
1049 * KERN_RESOURCE_SHORTAGE Couldn't allocate new entry.
1055 mach_port_name_t oname
,
1056 mach_port_name_t nname
)
1058 ipc_entry_t oentry
, nentry
;
1061 kr
= ipc_entry_alloc_name(space
, nname
, &nentry
);
1062 if (kr
!= KERN_SUCCESS
)
1065 /* space is write-locked and active */
1067 if (ipc_right_inuse(space
, nname
, nentry
)) {
1068 /* space is unlocked */
1069 return KERN_NAME_EXISTS
;
1072 /* don't let ipc_entry_lookup see the uninitialized new entry */
1074 if ((oname
== nname
) ||
1075 ((oentry
= ipc_entry_lookup(space
, oname
)) == IE_NULL
)) {
1076 ipc_entry_dealloc(space
, nname
, nentry
);
1077 is_write_unlock(space
);
1078 return KERN_INVALID_NAME
;
1081 kr
= ipc_right_rename(space
, oname
, oentry
, nname
, nentry
);
1082 /* space is unlocked */
1087 * Check whether the object is a port if so, free it. But
1088 * keep track of that fact.
1093 ipc_object_t object
)
1097 if (otype
== IOT_PORT
) {
1098 port
= (ipc_port_t
) object
;
1099 ipc_port_finalize(port
);
1101 io_lock_destroy(object
);
1102 zfree(ipc_object_zones
[otype
], object
);