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 mach_port_guard_exception(name1
, 0, 0, kGUARD_EXC_INVALID_RIGHT
);
206 return KERN_INVALID_RIGHT
;
209 if ((entry2
->ie_bits
& MACH_PORT_TYPE(right2
)) == MACH_PORT_TYPE_NONE
) {
210 is_read_unlock(space
);
211 mach_port_guard_exception(name2
, 0, 0, kGUARD_EXC_INVALID_RIGHT
);
212 return KERN_INVALID_RIGHT
;
215 object
= entry1
->ie_object
;
216 assert(object
!= IO_NULL
);
220 object
= entry2
->ie_object
;
221 assert(object
!= IO_NULL
);
225 is_read_unlock(space
);
230 * Routine: ipc_object_alloc_dead
232 * Allocate a dead-name entry.
236 * KERN_SUCCESS The dead name is allocated.
237 * KERN_INVALID_TASK The space is dead.
238 * KERN_NO_SPACE No room for an entry in the space.
239 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
243 ipc_object_alloc_dead(
245 mach_port_name_t
*namep
)
250 kr
= ipc_entry_alloc(space
, namep
, &entry
);
251 if (kr
!= KERN_SUCCESS
)
253 /* space is write-locked */
255 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
257 assert(entry
->ie_object
== IO_NULL
);
258 entry
->ie_bits
|= MACH_PORT_TYPE_DEAD_NAME
| 1;
259 ipc_entry_modified(space
, *namep
, entry
);
260 is_write_unlock(space
);
265 * Routine: ipc_object_alloc_dead_name
267 * Allocate a dead-name entry, with a specific name.
271 * KERN_SUCCESS The dead name is allocated.
272 * KERN_INVALID_TASK The space is dead.
273 * KERN_NAME_EXISTS The name already denotes a right.
274 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
278 ipc_object_alloc_dead_name(
280 mach_port_name_t name
)
285 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
286 if (kr
!= KERN_SUCCESS
)
288 /* space is write-locked */
290 if (ipc_right_inuse(space
, name
, entry
))
291 return KERN_NAME_EXISTS
;
293 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
295 assert(entry
->ie_object
== IO_NULL
);
296 entry
->ie_bits
|= MACH_PORT_TYPE_DEAD_NAME
| 1;
297 ipc_entry_modified(space
, name
, entry
);
298 is_write_unlock(space
);
303 * Routine: ipc_object_alloc
305 * Allocate an object.
307 * Nothing locked. If successful, the object is returned locked.
308 * The space is write locked on successful return.
309 * The caller doesn't get a reference for the object.
311 * KERN_SUCCESS The object is allocated.
312 * KERN_INVALID_TASK The space is dead.
313 * KERN_NO_SPACE No room for an entry in the space.
314 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
320 ipc_object_type_t otype
,
321 mach_port_type_t type
,
322 mach_port_urefs_t urefs
,
323 mach_port_name_t
*namep
,
324 ipc_object_t
*objectp
)
330 assert(otype
< IOT_NUMBER
);
331 assert((type
& MACH_PORT_TYPE_ALL_RIGHTS
) == type
);
332 assert(type
!= MACH_PORT_TYPE_NONE
);
333 assert(urefs
<= MACH_PORT_UREFS_MAX
);
335 object
= io_alloc(otype
);
336 if (object
== IO_NULL
)
337 return KERN_RESOURCE_SHORTAGE
;
339 if (otype
== IOT_PORT
) {
340 ipc_port_t port
= (ipc_port_t
)object
;
342 bzero((char *)port
, sizeof(*port
));
343 } else if (otype
== IOT_PORT_SET
) {
344 ipc_pset_t pset
= (ipc_pset_t
)object
;
346 bzero((char *)pset
, sizeof(*pset
));
349 io_lock_init(object
);
350 *namep
= CAST_MACH_PORT_TO_NAME(object
);
351 kr
= ipc_entry_alloc(space
, namep
, &entry
);
352 if (kr
!= KERN_SUCCESS
) {
353 io_free(otype
, object
);
356 /* space is write-locked */
358 entry
->ie_bits
|= type
| urefs
;
359 entry
->ie_object
= object
;
360 ipc_entry_modified(space
, *namep
, entry
);
364 object
->io_references
= 1; /* for entry, not caller */
365 object
->io_bits
= io_makebits(TRUE
, otype
, 0);
372 * Routine: ipc_object_alloc_name
374 * Allocate an object, with a specific name.
376 * Nothing locked. If successful, the object is returned locked.
377 * The caller doesn't get a reference for the object.
379 * KERN_SUCCESS The object is allocated.
380 * KERN_INVALID_TASK The space is dead.
381 * KERN_NAME_EXISTS The name already denotes a right.
382 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
386 ipc_object_alloc_name(
388 ipc_object_type_t otype
,
389 mach_port_type_t type
,
390 mach_port_urefs_t urefs
,
391 mach_port_name_t name
,
392 ipc_object_t
*objectp
)
398 assert(otype
< IOT_NUMBER
);
399 assert((type
& MACH_PORT_TYPE_ALL_RIGHTS
) == type
);
400 assert(type
!= MACH_PORT_TYPE_NONE
);
401 assert(urefs
<= MACH_PORT_UREFS_MAX
);
403 object
= io_alloc(otype
);
404 if (object
== IO_NULL
)
405 return KERN_RESOURCE_SHORTAGE
;
407 if (otype
== IOT_PORT
) {
408 ipc_port_t port
= (ipc_port_t
)object
;
410 bzero((char *)port
, sizeof(*port
));
411 } else if (otype
== IOT_PORT_SET
) {
412 ipc_pset_t pset
= (ipc_pset_t
)object
;
414 bzero((char *)pset
, sizeof(*pset
));
417 io_lock_init(object
);
418 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
419 if (kr
!= KERN_SUCCESS
) {
420 io_free(otype
, object
);
423 /* space is write-locked */
425 if (ipc_right_inuse(space
, name
, entry
)) {
426 io_free(otype
, object
);
427 return KERN_NAME_EXISTS
;
430 entry
->ie_bits
|= type
| urefs
;
431 entry
->ie_object
= object
;
432 ipc_entry_modified(space
, name
, entry
);
435 is_write_unlock(space
);
437 object
->io_references
= 1; /* for entry, not caller */
438 object
->io_bits
= io_makebits(TRUE
, otype
, 0);
445 * Routine: ipc_object_copyin_type
447 * Convert a send type name to a received type name.
451 ipc_object_copyin_type(
452 mach_msg_type_name_t msgt_name
)
456 case MACH_MSG_TYPE_MOVE_RECEIVE
:
457 return MACH_MSG_TYPE_PORT_RECEIVE
;
459 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
460 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
461 return MACH_MSG_TYPE_PORT_SEND_ONCE
;
463 case MACH_MSG_TYPE_MOVE_SEND
:
464 case MACH_MSG_TYPE_MAKE_SEND
:
465 case MACH_MSG_TYPE_COPY_SEND
:
466 return MACH_MSG_TYPE_PORT_SEND
;
468 case MACH_MSG_TYPE_DISPOSE_RECEIVE
:
469 case MACH_MSG_TYPE_DISPOSE_SEND
:
470 case MACH_MSG_TYPE_DISPOSE_SEND_ONCE
:
473 return MACH_MSG_TYPE_PORT_NONE
;
478 * Routine: ipc_object_copyin
480 * Copyin a capability from a space.
481 * If successful, the caller gets a ref
482 * for the resulting object, unless it is IO_DEAD.
486 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
487 * KERN_INVALID_TASK The space is dead.
488 * KERN_INVALID_NAME Name doesn't exist in space.
489 * KERN_INVALID_RIGHT Name doesn't denote correct right.
495 mach_port_name_t name
,
496 mach_msg_type_name_t msgt_name
,
497 ipc_object_t
*objectp
)
501 ipc_port_t release_port
;
506 * Could first try a read lock when doing
507 * MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND,
508 * and MACH_MSG_TYPE_MAKE_SEND_ONCE.
511 kr
= ipc_right_lookup_write(space
, name
, &entry
);
512 if (kr
!= KERN_SUCCESS
)
514 /* space is write-locked and active */
516 release_port
= IP_NULL
;
517 kr
= ipc_right_copyin(space
, name
, entry
,
522 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
523 ipc_entry_dealloc(space
, name
, entry
);
524 is_write_unlock(space
);
526 #if IMPORTANCE_INHERITANCE
527 if (0 < assertcnt
&& ipc_importance_task_is_any_receiver_type(current_task()->task_imp_base
)) {
528 ipc_importance_task_drop_internal_assertion(current_task()->task_imp_base
, assertcnt
);
530 #endif /* IMPORTANCE_INHERITANCE */
532 if (release_port
!= IP_NULL
)
533 ip_release(release_port
);
535 if ((kr
== KERN_SUCCESS
) && (soright
!= IP_NULL
))
536 ipc_notify_port_deleted(soright
, name
);
542 * Routine: ipc_object_copyin_from_kernel
544 * Copyin a naked capability from the kernel.
546 * MACH_MSG_TYPE_MOVE_RECEIVE
547 * The receiver must be ipc_space_kernel
548 * or the receive right must already be in limbo.
549 * Consumes the naked receive right.
550 * MACH_MSG_TYPE_COPY_SEND
551 * A naked send right must be supplied.
552 * The port gains a reference, and a send right
553 * if the port is still active.
554 * MACH_MSG_TYPE_MAKE_SEND
555 * The receiver must be ipc_space_kernel.
556 * The port gains a reference and a send right.
557 * MACH_MSG_TYPE_MOVE_SEND
558 * Consumes a naked send right.
559 * MACH_MSG_TYPE_MAKE_SEND_ONCE
560 * The port gains a reference and a send-once right.
561 * Receiver also be the caller of device subsystem,
563 * MACH_MSG_TYPE_MOVE_SEND_ONCE
564 * Consumes a naked send-once right.
570 ipc_object_copyin_from_kernel(
572 mach_msg_type_name_t msgt_name
)
574 assert(IO_VALID(object
));
577 case MACH_MSG_TYPE_MOVE_RECEIVE
: {
578 ipc_port_t port
= (ipc_port_t
) object
;
581 imq_lock(&port
->ip_messages
);
582 assert(ip_active(port
));
583 if (port
->ip_destination
!= IP_NULL
) {
584 assert(port
->ip_receiver
== ipc_space_kernel
);
586 /* relevant part of ipc_port_clear_receiver */
587 ipc_port_set_mscount(port
, 0);
589 port
->ip_receiver_name
= MACH_PORT_NULL
;
590 port
->ip_destination
= IP_NULL
;
592 imq_unlock(&port
->ip_messages
);
597 case MACH_MSG_TYPE_COPY_SEND
: {
598 ipc_port_t port
= (ipc_port_t
) object
;
601 if (ip_active(port
)) {
602 assert(port
->ip_srights
> 0);
610 case MACH_MSG_TYPE_MAKE_SEND
: {
611 ipc_port_t port
= (ipc_port_t
) object
;
614 if (ip_active(port
)) {
615 assert(port
->ip_receiver_name
!= MACH_PORT_NULL
);
616 assert((port
->ip_receiver
== ipc_space_kernel
) ||
617 (port
->ip_receiver
->is_node_id
!= HOST_LOCAL_NODE
));
627 case MACH_MSG_TYPE_MOVE_SEND
: {
628 /* move naked send right into the message */
629 assert(((ipc_port_t
)object
)->ip_srights
);
633 case MACH_MSG_TYPE_MAKE_SEND_ONCE
: {
634 ipc_port_t port
= (ipc_port_t
) object
;
637 if (ip_active(port
)) {
638 assert(port
->ip_receiver_name
!= MACH_PORT_NULL
);
646 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
647 /* move naked send-once right into the message */
648 assert(((ipc_port_t
)object
)->ip_sorights
);
653 panic("ipc_object_copyin_from_kernel: strange rights");
658 * Routine: ipc_object_destroy
660 * Destroys a naked capability.
661 * Consumes a ref for the object.
663 * A receive right should be in limbo or in transit.
671 mach_msg_type_name_t msgt_name
)
673 assert(IO_VALID(object
));
674 assert(io_otype(object
) == IOT_PORT
);
677 case MACH_MSG_TYPE_PORT_SEND
:
678 ipc_port_release_send((ipc_port_t
) object
);
681 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
682 ipc_notify_send_once((ipc_port_t
) object
);
685 case MACH_MSG_TYPE_PORT_RECEIVE
:
686 ipc_port_release_receive((ipc_port_t
) object
);
690 panic("ipc_object_destroy: strange rights");
695 * Routine: ipc_object_destroy_dest
697 * Destroys a naked capability for the destination of
698 * of a message. Consumes a ref for the object.
705 ipc_object_destroy_dest(
707 mach_msg_type_name_t msgt_name
)
709 assert(IO_VALID(object
));
710 assert(io_otype(object
) == IOT_PORT
);
713 case MACH_MSG_TYPE_PORT_SEND
:
714 ipc_port_release_send((ipc_port_t
) object
);
717 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
718 if (io_active(object
) &&
719 !ip_full_kernel((ipc_port_t
) object
))
720 ipc_notify_send_once((ipc_port_t
) object
);
722 ipc_port_release_sonce((ipc_port_t
) object
);
726 panic("ipc_object_destroy_dest: strange rights");
731 * Routine: ipc_object_copyout
733 * Copyout a capability, placing it into a space.
734 * If successful, consumes a ref for the object.
738 * KERN_SUCCESS Copied out object, consumed ref.
739 * KERN_INVALID_TASK The space is dead.
740 * KERN_INVALID_CAPABILITY The object is dead.
741 * KERN_NO_SPACE No room in space for another right.
742 * KERN_RESOURCE_SHORTAGE No memory available.
743 * KERN_UREFS_OVERFLOW Urefs limit exceeded
744 * and overflow wasn't specified.
751 mach_msg_type_name_t msgt_name
,
753 mach_port_name_t
*namep
)
755 struct knote
*kn
= current_thread()->ith_knote
;
756 mach_port_name_t name
;
760 assert(IO_VALID(object
));
761 assert(io_otype(object
) == IOT_PORT
);
763 if (ITH_KNOTE_VALID(kn
, msgt_name
)) {
764 filt_machport_turnstile_prepare_lazily(kn
,
765 msgt_name
, (ipc_port_t
)object
);
768 is_write_lock(space
);
771 if (!is_active(space
)) {
772 is_write_unlock(space
);
773 return KERN_INVALID_TASK
;
776 if ((msgt_name
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
777 ipc_right_reverse(space
, object
, &name
, &entry
)) {
778 /* object is locked and active */
780 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
784 name
= CAST_MACH_PORT_TO_NAME(object
);
785 kr
= ipc_entry_get(space
, &name
, &entry
);
786 if (kr
!= KERN_SUCCESS
) {
787 /* unlocks/locks space, so must start again */
789 kr
= ipc_entry_grow_table(space
, ITS_SIZE_NONE
);
790 if (kr
!= KERN_SUCCESS
)
791 return kr
; /* space is unlocked */
796 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
);
797 assert(entry
->ie_object
== IO_NULL
);
800 if (!io_active(object
)) {
802 ipc_entry_dealloc(space
, name
, entry
);
803 is_write_unlock(space
);
804 return KERN_INVALID_CAPABILITY
;
807 entry
->ie_object
= object
;
811 /* space is write-locked and active, object is locked and active */
813 kr
= ipc_right_copyout(space
, name
, entry
,
814 msgt_name
, overflow
, object
);
816 /* object is unlocked */
817 is_write_unlock(space
);
819 if (kr
== KERN_SUCCESS
)
825 * Routine: ipc_object_copyout_name
827 * Copyout a capability, placing it into a space.
828 * The specified name is used for the capability.
829 * If successful, consumes a ref for the object.
833 * KERN_SUCCESS Copied out object, consumed ref.
834 * KERN_INVALID_TASK The space is dead.
835 * KERN_INVALID_CAPABILITY The object is dead.
836 * KERN_RESOURCE_SHORTAGE No memory available.
837 * KERN_UREFS_OVERFLOW Urefs limit exceeded
838 * and overflow wasn't specified.
839 * KERN_RIGHT_EXISTS Space has rights under another name.
840 * KERN_NAME_EXISTS Name is already used.
844 ipc_object_copyout_name(
847 mach_msg_type_name_t msgt_name
,
849 mach_port_name_t name
)
851 mach_port_name_t oname
;
855 struct knote
*kn
= current_thread()->ith_knote
;
857 #if IMPORTANCE_INHERITANCE
859 ipc_importance_task_t task_imp
= IIT_NULL
;
860 #endif /* IMPORTANCE_INHERITANCE */
862 assert(IO_VALID(object
));
863 assert(io_otype(object
) == IOT_PORT
);
865 if (ITH_KNOTE_VALID(kn
, msgt_name
)) {
866 filt_machport_turnstile_prepare_lazily(kn
,
867 msgt_name
, (ipc_port_t
)object
);
870 kr
= ipc_entry_alloc_name(space
, name
, &entry
);
871 if (kr
!= KERN_SUCCESS
)
873 /* space is write-locked and active */
875 if ((msgt_name
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) &&
876 ipc_right_reverse(space
, object
, &oname
, &oentry
)) {
877 /* object is locked and active */
882 if (IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
)
883 ipc_entry_dealloc(space
, name
, entry
);
885 is_write_unlock(space
);
886 return KERN_RIGHT_EXISTS
;
889 assert(entry
== oentry
);
890 assert(entry
->ie_bits
& MACH_PORT_TYPE_SEND_RECEIVE
);
892 if (ipc_right_inuse(space
, name
, entry
))
893 return KERN_NAME_EXISTS
;
895 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_NONE
);
896 assert(entry
->ie_object
== IO_NULL
);
899 if (!io_active(object
)) {
901 ipc_entry_dealloc(space
, name
, entry
);
902 is_write_unlock(space
);
903 return KERN_INVALID_CAPABILITY
;
906 entry
->ie_object
= object
;
909 /* space is write-locked and active, object is locked and active */
911 #if IMPORTANCE_INHERITANCE
913 * We are slamming a receive right into the space, without
914 * first having been enqueued on a port destined there. So,
915 * we have to arrange to boost the task appropriately if this
916 * port has assertions (and the task wants them).
918 if (msgt_name
== MACH_MSG_TYPE_PORT_RECEIVE
) {
919 ipc_port_t port
= (ipc_port_t
)object
;
921 if (space
->is_task
!= TASK_NULL
) {
922 task_imp
= space
->is_task
->task_imp_base
;
923 if (ipc_importance_task_is_any_receiver_type(task_imp
)) {
924 assertcnt
= port
->ip_impcount
;
925 ipc_importance_task_reference(task_imp
);
931 /* take port out of limbo */
932 assert(port
->ip_tempowner
!= 0);
933 port
->ip_tempowner
= 0;
936 #endif /* IMPORTANCE_INHERITANCE */
938 kr
= ipc_right_copyout(space
, name
, entry
,
939 msgt_name
, overflow
, object
);
941 /* object is unlocked */
942 is_write_unlock(space
);
944 #if IMPORTANCE_INHERITANCE
946 * Add the assertions to the task that we captured before
948 if (task_imp
!= IIT_NULL
) {
949 ipc_importance_task_hold_internal_assertion(task_imp
, assertcnt
);
950 ipc_importance_task_release(task_imp
);
952 #endif /* IMPORTANCE_INHERITANCE */
958 * Routine: ipc_object_copyout_dest
960 * Translates/consumes the destination right of a message.
961 * This is unlike normal copyout because the right is consumed
962 * in a funny way instead of being given to the receiving space.
963 * The receiver gets his name for the port, if he has receive
964 * rights, otherwise MACH_PORT_NULL.
966 * The object is locked and active. Nothing else locked.
967 * The object is unlocked and loses a reference.
971 ipc_object_copyout_dest(
974 mach_msg_type_name_t msgt_name
,
975 mach_port_name_t
*namep
)
977 mach_port_name_t name
;
979 assert(IO_VALID(object
));
980 assert(io_active(object
));
985 * If the space is the receiver/owner of the object,
986 * then we quietly consume the right and return
987 * the space's name for the object. Otherwise
988 * we destroy the right and return MACH_PORT_NULL.
992 case MACH_MSG_TYPE_PORT_SEND
: {
993 ipc_port_t port
= (ipc_port_t
) object
;
994 ipc_port_t nsrequest
= IP_NULL
;
995 mach_port_mscount_t mscount
;
997 if (port
->ip_receiver
== space
)
998 name
= port
->ip_receiver_name
;
1000 name
= MACH_PORT_NULL
;
1002 assert(port
->ip_srights
> 0);
1003 if (--port
->ip_srights
== 0 &&
1004 port
->ip_nsrequest
!= IP_NULL
) {
1005 nsrequest
= port
->ip_nsrequest
;
1006 port
->ip_nsrequest
= IP_NULL
;
1007 mscount
= port
->ip_mscount
;
1009 ipc_notify_no_senders(nsrequest
, mscount
);
1015 case MACH_MSG_TYPE_PORT_SEND_ONCE
: {
1016 ipc_port_t port
= (ipc_port_t
) object
;
1018 assert(port
->ip_sorights
> 0);
1020 if (port
->ip_receiver
== space
) {
1021 /* quietly consume the send-once right */
1023 port
->ip_sorights
--;
1024 name
= port
->ip_receiver_name
;
1028 * A very bizarre case. The message
1029 * was received, but before this copyout
1030 * happened the space lost receive rights.
1031 * We can't quietly consume the soright
1032 * out from underneath some other task,
1033 * so generate a send-once notification.
1036 ip_reference(port
); /* restore ref */
1039 ipc_notify_send_once(port
);
1040 name
= MACH_PORT_NULL
;
1047 panic("ipc_object_copyout_dest: strange rights");
1048 name
= MACH_PORT_DEAD
;
1055 * Routine: ipc_object_rename
1057 * Rename an entry in a space.
1061 * KERN_SUCCESS Renamed the entry.
1062 * KERN_INVALID_TASK The space was dead.
1063 * KERN_INVALID_NAME oname didn't denote an entry.
1064 * KERN_NAME_EXISTS nname already denoted an entry.
1065 * KERN_RESOURCE_SHORTAGE Couldn't allocate new entry.
1071 mach_port_name_t oname
,
1072 mach_port_name_t nname
)
1074 ipc_entry_t oentry
, nentry
;
1077 kr
= ipc_entry_alloc_name(space
, nname
, &nentry
);
1078 if (kr
!= KERN_SUCCESS
)
1081 /* space is write-locked and active */
1083 if (ipc_right_inuse(space
, nname
, nentry
)) {
1084 /* space is unlocked */
1085 return KERN_NAME_EXISTS
;
1088 /* don't let ipc_entry_lookup see the uninitialized new entry */
1090 if ((oname
== nname
) ||
1091 ((oentry
= ipc_entry_lookup(space
, oname
)) == IE_NULL
)) {
1092 ipc_entry_dealloc(space
, nname
, nentry
);
1093 is_write_unlock(space
);
1094 return KERN_INVALID_NAME
;
1097 kr
= ipc_right_rename(space
, oname
, oentry
, nname
, nentry
);
1098 /* space is unlocked */
1103 * Check whether the object is a port if so, free it. But
1104 * keep track of that fact.
1109 ipc_object_t object
)
1113 if (otype
== IOT_PORT
) {
1114 port
= (ipc_port_t
) object
;
1115 ipc_port_finalize(port
);
1117 io_lock_destroy(object
);
1118 zfree(ipc_object_zones
[otype
], object
);