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@
29 * @OSF_FREE_COPYRIGHT@
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_right.c
70 * Functions to manipulate IPC capabilities.
73 #include <mach/boolean.h>
74 #include <mach/kern_return.h>
75 #include <mach/port.h>
76 #include <mach/message.h>
77 #include <kern/assert.h>
78 #include <kern/misc_protos.h>
80 #include <ipc/ipc_entry.h>
81 #include <ipc/ipc_space.h>
82 #include <ipc/ipc_object.h>
83 #include <ipc/ipc_hash.h>
84 #include <ipc/ipc_port.h>
85 #include <ipc/ipc_pset.h>
86 #include <ipc/ipc_right.h>
87 #include <ipc/ipc_notify.h>
88 #include <ipc/ipc_table.h>
89 #include <ipc/ipc_importance.h>
90 #include <security/mac_mach_internal.h>
92 /* Allow IPC to generate mach port guard exceptions */
94 mach_port_guard_exception(
95 mach_port_name_t name
,
100 * Routine: ipc_right_lookup_write
102 * Finds an entry in a space, given the name.
104 * Nothing locked. If successful, the space is write-locked.
106 * KERN_SUCCESS Found an entry.
107 * KERN_INVALID_TASK The space is dead.
108 * KERN_INVALID_NAME Name doesn't exist in space.
112 ipc_right_lookup_write(
114 mach_port_name_t name
,
119 assert(space
!= IS_NULL
);
121 is_write_lock(space
);
123 if (!is_active(space
)) {
124 is_write_unlock(space
);
125 return KERN_INVALID_TASK
;
128 if ((entry
= ipc_entry_lookup(space
, name
)) == IE_NULL
) {
129 is_write_unlock(space
);
130 return KERN_INVALID_NAME
;
138 * Routine: ipc_right_lookup_two_write
140 * Like ipc_right_lookup except that it returns two
141 * entries for two different names that were looked
142 * up under the same space lock.
144 * Nothing locked. If successful, the space is write-locked.
146 * KERN_INVALID_TASK The space is dead.
147 * KERN_INVALID_NAME Name doesn't exist in space.
151 ipc_right_lookup_two_write(
153 mach_port_name_t name1
,
154 ipc_entry_t
*entryp1
,
155 mach_port_name_t name2
,
156 ipc_entry_t
*entryp2
)
161 assert(space
!= IS_NULL
);
163 is_write_lock(space
);
165 if (!is_active(space
)) {
166 is_write_unlock(space
);
167 return KERN_INVALID_TASK
;
170 if ((entry1
= ipc_entry_lookup(space
, name1
)) == IE_NULL
) {
171 is_write_unlock(space
);
172 return KERN_INVALID_NAME
;
174 if ((entry2
= ipc_entry_lookup(space
, name2
)) == IE_NULL
) {
175 is_write_unlock(space
);
176 return KERN_INVALID_NAME
;
184 * Routine: ipc_right_reverse
186 * Translate (space, object) -> (name, entry).
187 * Only finds send/receive rights.
188 * Returns TRUE if an entry is found; if so,
189 * the object is locked and active.
191 * The space must be locked (read or write) and active.
192 * Nothing else locked.
199 mach_port_name_t
*namep
,
203 mach_port_name_t name
;
206 /* would switch on io_otype to handle multiple types of object */
208 assert(is_active(space
));
209 assert(io_otype(object
) == IOT_PORT
);
211 port
= (ipc_port_t
) object
;
214 if (!ip_active(port
)) {
220 if (port
->ip_receiver
== space
) {
221 name
= port
->ip_receiver_name
;
222 assert(name
!= MACH_PORT_NULL
);
224 entry
= ipc_entry_lookup(space
, name
);
226 assert(entry
!= IE_NULL
);
227 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
228 assert(port
== (ipc_port_t
) entry
->ie_object
);
235 if (ipc_hash_lookup(space
, (ipc_object_t
) port
, namep
, entryp
)) {
236 assert((entry
= *entryp
) != IE_NULL
);
237 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_SEND
);
238 assert(port
== (ipc_port_t
) entry
->ie_object
);
248 * Routine: ipc_right_dnrequest
250 * Make a dead-name request, returning the previously
251 * registered send-once right. If notify is IP_NULL,
252 * just cancels the previously registered request.
255 * Nothing locked. May allocate memory.
256 * Only consumes/returns refs if successful.
258 * KERN_SUCCESS Made/canceled dead-name request.
259 * KERN_INVALID_TASK The space is dead.
260 * KERN_INVALID_NAME Name doesn't exist in space.
261 * KERN_INVALID_RIGHT Name doesn't denote port/dead rights.
262 * KERN_INVALID_ARGUMENT Name denotes dead name, but
263 * immediate is FALSE or notify is IP_NULL.
264 * KERN_UREFS_OVERFLOW Name denotes dead name, but
265 * generating immediate notif. would overflow urefs.
266 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
270 ipc_right_request_alloc(
272 mach_port_name_t name
,
274 boolean_t send_possible
,
276 ipc_port_t
*previousp
)
278 ipc_port_request_index_t prev_request
;
279 ipc_port_t previous
= IP_NULL
;
283 #if IMPORTANCE_INHERITANCE
284 boolean_t needboost
= FALSE
;
285 #endif /* IMPORTANCE_INHERITANCE */
288 ipc_port_t port
= IP_NULL
;
290 kr
= ipc_right_lookup_write(space
, name
, &entry
);
291 if (kr
!= KERN_SUCCESS
)
294 /* space is write-locked and active */
296 prev_request
= entry
->ie_request
;
298 /* if nothing to do or undo, we're done */
299 if (notify
== IP_NULL
&& prev_request
== IE_REQ_NONE
) {
300 is_write_unlock(space
);
301 *previousp
= IP_NULL
;
305 /* see if the entry is of proper type for requests */
306 if (entry
->ie_bits
& MACH_PORT_TYPE_PORT_RIGHTS
) {
307 ipc_port_request_index_t new_request
;
309 port
= (ipc_port_t
) entry
->ie_object
;
310 assert(port
!= IP_NULL
);
312 if (!ipc_right_check(space
, port
, name
, entry
)) {
313 /* port is locked and active */
315 /* if no new request, just cancel previous */
316 if (notify
== IP_NULL
) {
317 if (prev_request
!= IE_REQ_NONE
)
318 previous
= ipc_port_request_cancel(port
, name
, prev_request
);
320 entry
->ie_request
= IE_REQ_NONE
;
321 ipc_entry_modified(space
, name
, entry
);
322 is_write_unlock(space
);
327 * send-once rights, kernel objects, and non-full other queues
328 * fire immediately (if immediate specified).
330 if (send_possible
&& immediate
&&
331 ((entry
->ie_bits
& MACH_PORT_TYPE_SEND_ONCE
) ||
332 port
->ip_receiver
== ipc_space_kernel
|| !ip_full(port
))) {
333 if (prev_request
!= IE_REQ_NONE
)
334 previous
= ipc_port_request_cancel(port
, name
, prev_request
);
336 entry
->ie_request
= IE_REQ_NONE
;
337 ipc_entry_modified(space
, name
, entry
);
338 is_write_unlock(space
);
340 ipc_notify_send_possible(notify
, name
);
345 * If there is a previous request, free it. Any subsequent
346 * allocation cannot fail, thus assuring an atomic swap.
348 if (prev_request
!= IE_REQ_NONE
)
349 previous
= ipc_port_request_cancel(port
, name
, prev_request
);
351 #if IMPORTANCE_INHERITANCE
352 kr
= ipc_port_request_alloc(port
, name
, notify
,
353 send_possible
, immediate
,
354 &new_request
, &needboost
);
356 kr
= ipc_port_request_alloc(port
, name
, notify
,
357 send_possible
, immediate
,
359 #endif /* IMPORTANCE_INHERITANCE */
360 if (kr
!= KERN_SUCCESS
) {
361 assert(previous
== IP_NULL
);
362 is_write_unlock(space
);
364 kr
= ipc_port_request_grow(port
, ITS_SIZE_NONE
);
365 /* port is unlocked */
367 if (kr
!= KERN_SUCCESS
)
374 assert(new_request
!= IE_REQ_NONE
);
375 entry
->ie_request
= new_request
;
376 ipc_entry_modified(space
, name
, entry
);
377 is_write_unlock(space
);
379 #if IMPORTANCE_INHERITANCE
380 if (needboost
== TRUE
) {
381 if (ipc_port_importance_delta(port
, IPID_OPTION_SENDPOSSIBLE
, 1) == FALSE
)
384 #endif /* IMPORTANCE_INHERITANCE */
389 /* entry may have changed to dead-name by ipc_right_check() */
393 /* treat send_possible requests as immediate w.r.t. dead-name */
394 if ((send_possible
|| immediate
) && notify
!= IP_NULL
&&
395 (entry
->ie_bits
& MACH_PORT_TYPE_DEAD_NAME
)) {
396 mach_port_urefs_t urefs
= IE_BITS_UREFS(entry
->ie_bits
);
400 if (MACH_PORT_UREFS_OVERFLOW(urefs
, 1)) {
401 is_write_unlock(space
);
404 return KERN_UREFS_OVERFLOW
;
407 (entry
->ie_bits
)++; /* increment urefs */
408 ipc_entry_modified(space
, name
, entry
);
409 is_write_unlock(space
);
414 ipc_notify_dead_name(notify
, name
);
419 is_write_unlock(space
);
424 if (entry
->ie_bits
& MACH_PORT_TYPE_PORT_OR_DEAD
)
425 return KERN_INVALID_ARGUMENT
;
427 return KERN_INVALID_RIGHT
;
430 *previousp
= previous
;
435 * Routine: ipc_right_request_cancel
437 * Cancel a notification request and return the send-once right.
438 * Afterwards, entry->ie_request == 0.
440 * The space must be write-locked; the port must be locked.
441 * The port must be active; the space doesn't have to be.
445 ipc_right_request_cancel(
446 __unused ipc_space_t space
,
448 mach_port_name_t name
,
453 assert(ip_active(port
));
454 assert(port
== (ipc_port_t
) entry
->ie_object
);
456 if (entry
->ie_request
== IE_REQ_NONE
)
459 previous
= ipc_port_request_cancel(port
, name
, entry
->ie_request
);
460 entry
->ie_request
= IE_REQ_NONE
;
461 ipc_entry_modified(space
, name
, entry
);
466 * Routine: ipc_right_inuse
468 * Check if an entry is being used.
469 * Returns TRUE if it is.
471 * The space is write-locked and active.
472 * It is unlocked if the entry is inuse.
478 __unused mach_port_name_t name
,
481 if (IE_BITS_TYPE(entry
->ie_bits
) != MACH_PORT_TYPE_NONE
) {
482 is_write_unlock(space
);
489 * Routine: ipc_right_check
491 * Check if the port has died. If it has,
492 * clean up the entry and return TRUE.
494 * The space is write-locked; the port is not locked.
495 * If returns FALSE, the port is also locked and active.
496 * Otherwise, entry is converted to a dead name.
498 * Caller is responsible for a reference to port if it
499 * had died (returns TRUE).
506 mach_port_name_t name
,
509 ipc_entry_bits_t bits
;
511 assert(is_active(space
));
512 assert(port
== (ipc_port_t
) entry
->ie_object
);
518 /* this was either a pure send right or a send-once right */
520 bits
= entry
->ie_bits
;
521 assert((bits
& MACH_PORT_TYPE_RECEIVE
) == 0);
522 assert(IE_BITS_UREFS(bits
) > 0);
524 if (bits
& MACH_PORT_TYPE_SEND
) {
525 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
526 assert(IE_BITS_UREFS(bits
) > 0);
527 assert(port
->ip_srights
> 0);
530 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
531 assert(IE_BITS_UREFS(bits
) == 1);
532 assert(port
->ip_sorights
> 0);
538 * delete SEND rights from ipc hash.
541 if ((bits
& MACH_PORT_TYPE_SEND
) != 0) {
542 ipc_hash_delete(space
, (ipc_object_t
)port
, name
, entry
);
545 /* convert entry to dead name */
546 bits
= (bits
&~ IE_BITS_TYPE_MASK
) | MACH_PORT_TYPE_DEAD_NAME
;
549 * If there was a notification request outstanding on this
550 * name, and the port went dead, that notification
551 * must already be on its way up from the port layer.
553 * Add the reference that the notification carries. It
554 * is done here, and not in the notification delivery,
555 * because the latter doesn't have a space reference and
556 * trying to actually move a send-right reference would
557 * get short-circuited into a MACH_PORT_DEAD by IPC. Since
558 * all calls that deal with the right eventually come
559 * through here, it has the same result.
561 * Once done, clear the request index so we only account
564 if (entry
->ie_request
!= IE_REQ_NONE
) {
565 if (ipc_port_request_type(port
, name
, entry
->ie_request
) != 0) {
566 assert(IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
);
569 entry
->ie_request
= IE_REQ_NONE
;
571 entry
->ie_bits
= bits
;
572 entry
->ie_object
= IO_NULL
;
573 ipc_entry_modified(space
, name
, entry
);
578 * Routine: ipc_right_terminate
580 * Cleans up an entry in a terminated space.
581 * The entry isn't deallocated or removed
582 * from reverse hash tables.
584 * The space is dead and unlocked.
590 mach_port_name_t name
,
593 ipc_entry_bits_t bits
;
594 mach_port_type_t type
;
596 bits
= entry
->ie_bits
;
597 type
= IE_BITS_TYPE(bits
);
599 assert(!is_active(space
));
602 * IE_BITS_COMPAT/ipc_right_dncancel doesn't have this
603 * problem, because we check that the port is active. If
604 * we didn't cancel IE_BITS_COMPAT, ipc_port_destroy
605 * would still work, but dead space refs would accumulate
606 * in ip_dnrequests. They would use up slots in
607 * ip_dnrequests and keep the spaces from being freed.
611 case MACH_PORT_TYPE_DEAD_NAME
:
612 assert(entry
->ie_request
== IE_REQ_NONE
);
613 assert(entry
->ie_object
== IO_NULL
);
616 case MACH_PORT_TYPE_PORT_SET
: {
617 ipc_pset_t pset
= (ipc_pset_t
) entry
->ie_object
;
619 assert(entry
->ie_request
== IE_REQ_NONE
);
620 assert(pset
!= IPS_NULL
);
623 assert(ips_active(pset
));
624 ipc_pset_destroy(pset
); /* consumes ref, unlocks */
628 case MACH_PORT_TYPE_SEND
:
629 case MACH_PORT_TYPE_RECEIVE
:
630 case MACH_PORT_TYPE_SEND_RECEIVE
:
631 case MACH_PORT_TYPE_SEND_ONCE
: {
632 ipc_port_t port
= (ipc_port_t
) entry
->ie_object
;
634 ipc_port_t nsrequest
= IP_NULL
;
635 mach_port_mscount_t mscount
= 0;
637 assert(port
!= IP_NULL
);
640 if (!ip_active(port
)) {
646 request
= ipc_right_request_cancel_macro(space
, port
,
649 if (type
& MACH_PORT_TYPE_SEND
) {
650 assert(port
->ip_srights
> 0);
651 if (--port
->ip_srights
== 0
653 nsrequest
= port
->ip_nsrequest
;
654 if (nsrequest
!= IP_NULL
) {
655 port
->ip_nsrequest
= IP_NULL
;
656 mscount
= port
->ip_mscount
;
661 if (type
& MACH_PORT_TYPE_RECEIVE
) {
662 assert(port
->ip_receiver_name
== name
);
663 assert(port
->ip_receiver
== space
);
665 ipc_port_clear_receiver(port
);
666 ipc_port_destroy(port
); /* consumes our ref, unlocks */
668 } else if (type
& MACH_PORT_TYPE_SEND_ONCE
) {
669 assert(port
->ip_sorights
> 0);
672 ipc_notify_send_once(port
); /* consumes our ref */
674 assert(port
->ip_receiver
!= space
);
680 if (nsrequest
!= IP_NULL
)
681 ipc_notify_no_senders(nsrequest
, mscount
);
683 if (request
!= IP_NULL
)
684 ipc_notify_port_deleted(request
, name
);
689 panic("ipc_right_terminate: strange type - 0x%x", type
);
694 * Routine: ipc_right_destroy
696 * Destroys an entry in a space.
698 * The space is write-locked (returns unlocked).
699 * The space must be active.
701 * KERN_SUCCESS The entry was destroyed.
707 mach_port_name_t name
,
709 boolean_t check_guard
,
712 ipc_entry_bits_t bits
;
713 mach_port_type_t type
;
715 bits
= entry
->ie_bits
;
716 entry
->ie_bits
&= ~IE_BITS_TYPE_MASK
;
717 type
= IE_BITS_TYPE(bits
);
719 assert(is_active(space
));
722 case MACH_PORT_TYPE_DEAD_NAME
:
723 assert(entry
->ie_request
== IE_REQ_NONE
);
724 assert(entry
->ie_object
== IO_NULL
);
726 ipc_entry_dealloc(space
, name
, entry
);
727 is_write_unlock(space
);
730 case MACH_PORT_TYPE_PORT_SET
: {
731 ipc_pset_t pset
= (ipc_pset_t
) entry
->ie_object
;
733 assert(entry
->ie_request
== IE_REQ_NONE
);
734 assert(pset
!= IPS_NULL
);
736 entry
->ie_object
= IO_NULL
;
737 ipc_entry_dealloc(space
, name
, entry
);
740 is_write_unlock(space
);
742 assert(ips_active(pset
));
743 ipc_pset_destroy(pset
); /* consumes ref, unlocks */
747 case MACH_PORT_TYPE_SEND
:
748 case MACH_PORT_TYPE_RECEIVE
:
749 case MACH_PORT_TYPE_SEND_RECEIVE
:
750 case MACH_PORT_TYPE_SEND_ONCE
: {
751 ipc_port_t port
= (ipc_port_t
) entry
->ie_object
;
752 ipc_port_t nsrequest
= IP_NULL
;
753 mach_port_mscount_t mscount
= 0;
756 assert(port
!= IP_NULL
);
758 if (type
== MACH_PORT_TYPE_SEND
)
759 ipc_hash_delete(space
, (ipc_object_t
) port
,
764 if (!ip_active(port
)) {
765 assert((type
& MACH_PORT_TYPE_RECEIVE
) == 0);
767 entry
->ie_request
= IE_REQ_NONE
;
768 entry
->ie_object
= IO_NULL
;
769 ipc_entry_dealloc(space
, name
, entry
);
770 is_write_unlock(space
);
775 /* For receive rights, check for guarding */
776 if ((type
& MACH_PORT_TYPE_RECEIVE
) &&
777 (check_guard
) && (port
->ip_guarded
) &&
778 (guard
!= port
->ip_context
)) {
779 /* Guard Violation */
780 uint64_t portguard
= port
->ip_context
;
782 is_write_unlock(space
);
783 /* Raise mach port guard exception */
784 mach_port_guard_exception(name
, 0, portguard
, kGUARD_EXC_DESTROY
);
785 return KERN_INVALID_RIGHT
;
789 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
791 entry
->ie_object
= IO_NULL
;
792 ipc_entry_dealloc(space
, name
, entry
);
793 is_write_unlock(space
);
795 if (type
& MACH_PORT_TYPE_SEND
) {
796 assert(port
->ip_srights
> 0);
797 if (--port
->ip_srights
== 0) {
798 nsrequest
= port
->ip_nsrequest
;
799 if (nsrequest
!= IP_NULL
) {
800 port
->ip_nsrequest
= IP_NULL
;
801 mscount
= port
->ip_mscount
;
806 if (type
& MACH_PORT_TYPE_RECEIVE
) {
807 assert(ip_active(port
));
808 assert(port
->ip_receiver
== space
);
810 ipc_port_clear_receiver(port
);
811 ipc_port_destroy(port
); /* consumes our ref, unlocks */
813 } else if (type
& MACH_PORT_TYPE_SEND_ONCE
) {
814 assert(port
->ip_sorights
> 0);
817 ipc_notify_send_once(port
); /* consumes our ref */
819 assert(port
->ip_receiver
!= space
);
825 if (nsrequest
!= IP_NULL
)
826 ipc_notify_no_senders(nsrequest
, mscount
);
828 if (request
!= IP_NULL
)
829 ipc_notify_port_deleted(request
, name
);
836 panic("ipc_right_destroy: strange type");
843 * Routine: ipc_right_dealloc
845 * Releases a send/send-once/dead-name user ref.
846 * Like ipc_right_delta with a delta of -1,
847 * but looks at the entry to determine the right.
849 * The space is write-locked, and is unlocked upon return.
850 * The space must be active.
852 * KERN_SUCCESS A user ref was released.
853 * KERN_INVALID_RIGHT Entry has wrong type.
859 mach_port_name_t name
,
862 ipc_port_t port
= IP_NULL
;
863 ipc_entry_bits_t bits
;
864 mach_port_type_t type
;
866 bits
= entry
->ie_bits
;
867 type
= IE_BITS_TYPE(bits
);
870 assert(is_active(space
));
873 case MACH_PORT_TYPE_DEAD_NAME
: {
876 assert(IE_BITS_UREFS(bits
) > 0);
877 assert(entry
->ie_request
== IE_REQ_NONE
);
878 assert(entry
->ie_object
== IO_NULL
);
880 if (IE_BITS_UREFS(bits
) == 1) {
881 ipc_entry_dealloc(space
, name
, entry
);
883 entry
->ie_bits
= bits
-1; /* decrement urefs */
884 ipc_entry_modified(space
, name
, entry
);
886 is_write_unlock(space
);
888 /* release any port that got converted to dead name below */
894 case MACH_PORT_TYPE_SEND_ONCE
: {
897 assert(IE_BITS_UREFS(bits
) == 1);
899 port
= (ipc_port_t
) entry
->ie_object
;
900 assert(port
!= IP_NULL
);
902 if (ipc_right_check(space
, port
, name
, entry
)) {
904 bits
= entry
->ie_bits
;
905 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
906 goto dead_name
; /* it will release port */
908 /* port is locked and active */
910 assert(port
->ip_sorights
> 0);
912 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
915 entry
->ie_object
= IO_NULL
;
916 ipc_entry_dealloc(space
, name
, entry
);
918 is_write_unlock(space
);
920 ipc_notify_send_once(port
);
922 if (request
!= IP_NULL
)
923 ipc_notify_port_deleted(request
, name
);
927 case MACH_PORT_TYPE_SEND
: {
928 ipc_port_t request
= IP_NULL
;
929 ipc_port_t nsrequest
= IP_NULL
;
930 mach_port_mscount_t mscount
= 0;
933 assert(IE_BITS_UREFS(bits
) > 0);
935 port
= (ipc_port_t
) entry
->ie_object
;
936 assert(port
!= IP_NULL
);
938 if (ipc_right_check(space
, port
, name
, entry
)) {
939 bits
= entry
->ie_bits
;
940 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
941 goto dead_name
; /* it will release port */
943 /* port is locked and active */
945 assert(port
->ip_srights
> 0);
947 if (IE_BITS_UREFS(bits
) == 1) {
948 if (--port
->ip_srights
== 0) {
949 nsrequest
= port
->ip_nsrequest
;
950 if (nsrequest
!= IP_NULL
) {
951 port
->ip_nsrequest
= IP_NULL
;
952 mscount
= port
->ip_mscount
;
956 request
= ipc_right_request_cancel_macro(space
, port
,
958 ipc_hash_delete(space
, (ipc_object_t
) port
,
962 entry
->ie_object
= IO_NULL
;
963 ipc_entry_dealloc(space
, name
, entry
);
964 is_write_unlock(space
);
969 entry
->ie_bits
= bits
-1; /* decrement urefs */
970 ipc_entry_modified(space
, name
, entry
);
971 is_write_unlock(space
);
975 if (nsrequest
!= IP_NULL
)
976 ipc_notify_no_senders(nsrequest
, mscount
);
978 if (request
!= IP_NULL
)
979 ipc_notify_port_deleted(request
, name
);
983 case MACH_PORT_TYPE_SEND_RECEIVE
: {
984 ipc_port_t nsrequest
= IP_NULL
;
985 mach_port_mscount_t mscount
= 0;
987 assert(IE_BITS_UREFS(bits
) > 0);
989 port
= (ipc_port_t
) entry
->ie_object
;
990 assert(port
!= IP_NULL
);
993 assert(ip_active(port
));
994 assert(port
->ip_receiver_name
== name
);
995 assert(port
->ip_receiver
== space
);
996 assert(port
->ip_srights
> 0);
998 if (IE_BITS_UREFS(bits
) == 1) {
999 if (--port
->ip_srights
== 0) {
1000 nsrequest
= port
->ip_nsrequest
;
1001 if (nsrequest
!= IP_NULL
) {
1002 port
->ip_nsrequest
= IP_NULL
;
1003 mscount
= port
->ip_mscount
;
1007 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|
1008 MACH_PORT_TYPE_SEND
);
1010 entry
->ie_bits
= bits
-1; /* decrement urefs */
1014 ipc_entry_modified(space
, name
, entry
);
1015 is_write_unlock(space
);
1017 if (nsrequest
!= IP_NULL
)
1018 ipc_notify_no_senders(nsrequest
, mscount
);
1023 is_write_unlock(space
);
1024 return KERN_INVALID_RIGHT
;
1027 return KERN_SUCCESS
;
1031 * Routine: ipc_right_delta
1033 * Modifies the user-reference count for a right.
1034 * May deallocate the right, if the count goes to zero.
1036 * The space is write-locked, and is unlocked upon return.
1037 * The space must be active.
1039 * KERN_SUCCESS Count was modified.
1040 * KERN_INVALID_RIGHT Entry has wrong type.
1041 * KERN_INVALID_VALUE Bad delta for the right.
1042 * KERN_UREFS_OVERFLOW OK delta, except would overflow.
1048 mach_port_name_t name
,
1050 mach_port_right_t right
,
1051 mach_port_delta_t delta
)
1053 ipc_port_t port
= IP_NULL
;
1054 ipc_entry_bits_t bits
;
1056 bits
= entry
->ie_bits
;
1060 * The following is used (for case MACH_PORT_RIGHT_DEAD_NAME) in the
1061 * switch below. It is used to keep track of those cases (in DIPC)
1062 * where we have postponed the dropping of a port reference. Since
1063 * the dropping of the reference could cause the port to disappear
1064 * we postpone doing so when we are holding the space lock.
1067 assert(is_active(space
));
1068 assert(right
< MACH_PORT_RIGHT_NUMBER
);
1070 /* Rights-specific restrictions and operations. */
1073 case MACH_PORT_RIGHT_PORT_SET
: {
1076 if ((bits
& MACH_PORT_TYPE_PORT_SET
) == 0)
1079 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_PORT_SET
);
1080 assert(IE_BITS_UREFS(bits
) == 0);
1081 assert(entry
->ie_request
== IE_REQ_NONE
);
1089 pset
= (ipc_pset_t
) entry
->ie_object
;
1090 assert(pset
!= IPS_NULL
);
1092 entry
->ie_object
= IO_NULL
;
1093 ipc_entry_dealloc(space
, name
, entry
);
1096 assert(ips_active(pset
));
1097 is_write_unlock(space
);
1099 ipc_pset_destroy(pset
); /* consumes ref, unlocks */
1103 case MACH_PORT_RIGHT_RECEIVE
: {
1104 ipc_port_t request
= IP_NULL
;
1106 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1115 port
= (ipc_port_t
) entry
->ie_object
;
1116 assert(port
!= IP_NULL
);
1119 * The port lock is needed for ipc_right_dncancel;
1120 * otherwise, we wouldn't have to take the lock
1121 * until just before dropping the space lock.
1125 assert(ip_active(port
));
1126 assert(port
->ip_receiver_name
== name
);
1127 assert(port
->ip_receiver
== space
);
1129 /* Mach Port Guard Checking */
1130 if(port
->ip_guarded
) {
1131 uint64_t portguard
= port
->ip_context
;
1133 is_write_unlock(space
);
1134 /* Raise mach port guard exception */
1135 mach_port_guard_exception(name
, 0, portguard
, kGUARD_EXC_MOD_REFS
);
1139 if (bits
& MACH_PORT_TYPE_SEND
) {
1140 assert(IE_BITS_TYPE(bits
) ==
1141 MACH_PORT_TYPE_SEND_RECEIVE
);
1142 assert(IE_BITS_UREFS(bits
) > 0);
1143 assert(IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
);
1144 assert(port
->ip_srights
> 0);
1146 if (port
->ip_pdrequest
!= NULL
) {
1148 * Since another task has requested a
1149 * destroy notification for this port, it
1150 * isn't actually being destroyed - the receive
1151 * right is just being moved to another task.
1152 * Since we still have one or more send rights,
1153 * we need to record the loss of the receive
1154 * right and enter the remaining send right
1155 * into the hash table.
1157 ipc_entry_modified(space
, name
, entry
);
1158 entry
->ie_bits
&= ~MACH_PORT_TYPE_RECEIVE
;
1159 ipc_hash_insert(space
, (ipc_object_t
) port
,
1164 * The remaining send right turns into a
1165 * dead name. Notice we don't decrement
1166 * ip_srights, generate a no-senders notif,
1167 * or use ipc_right_dncancel, because the
1168 * port is destroyed "first".
1170 bits
&= ~IE_BITS_TYPE_MASK
;
1171 bits
|= MACH_PORT_TYPE_DEAD_NAME
;
1172 if (entry
->ie_request
) {
1173 entry
->ie_request
= IE_REQ_NONE
;
1176 entry
->ie_bits
= bits
;
1177 entry
->ie_object
= IO_NULL
;
1178 ipc_entry_modified(space
, name
, entry
);
1181 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
1182 assert(IE_BITS_UREFS(bits
) == 0);
1184 request
= ipc_right_request_cancel_macro(space
, port
,
1186 entry
->ie_object
= IO_NULL
;
1187 ipc_entry_dealloc(space
, name
, entry
);
1189 is_write_unlock(space
);
1191 ipc_port_clear_receiver(port
);
1192 ipc_port_destroy(port
); /* consumes ref, unlocks */
1194 if (request
!= IP_NULL
)
1195 ipc_notify_port_deleted(request
, name
);
1199 case MACH_PORT_RIGHT_SEND_ONCE
: {
1202 if ((bits
& MACH_PORT_TYPE_SEND_ONCE
) == 0)
1205 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
1206 assert(IE_BITS_UREFS(bits
) == 1);
1208 port
= (ipc_port_t
) entry
->ie_object
;
1209 assert(port
!= IP_NULL
);
1211 if (ipc_right_check(space
, port
, name
, entry
)) {
1212 assert(!(entry
->ie_bits
& MACH_PORT_TYPE_SEND_ONCE
));
1215 /* port is locked and active */
1217 assert(port
->ip_sorights
> 0);
1219 if ((delta
> 0) || (delta
< -1)) {
1229 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
1232 entry
->ie_object
= IO_NULL
;
1233 ipc_entry_dealloc(space
, name
, entry
);
1235 is_write_unlock(space
);
1237 ipc_notify_send_once(port
);
1239 if (request
!= IP_NULL
)
1240 ipc_notify_port_deleted(request
, name
);
1244 case MACH_PORT_RIGHT_DEAD_NAME
: {
1245 ipc_port_t relport
= IP_NULL
;
1246 mach_port_urefs_t urefs
;
1248 if (bits
& MACH_PORT_TYPE_SEND_RIGHTS
) {
1250 port
= (ipc_port_t
) entry
->ie_object
;
1251 assert(port
!= IP_NULL
);
1253 if (!ipc_right_check(space
, port
, name
, entry
)) {
1254 /* port is locked and active */
1259 bits
= entry
->ie_bits
;
1262 } else if ((bits
& MACH_PORT_TYPE_DEAD_NAME
) == 0)
1265 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
1266 assert(IE_BITS_UREFS(bits
) > 0);
1267 assert(entry
->ie_object
== IO_NULL
);
1268 assert(entry
->ie_request
== IE_REQ_NONE
);
1270 urefs
= IE_BITS_UREFS(bits
);
1271 if (MACH_PORT_UREFS_UNDERFLOW(urefs
, delta
))
1273 if (MACH_PORT_UREFS_OVERFLOW(urefs
, delta
))
1274 goto urefs_overflow
;
1276 if ((urefs
+ delta
) == 0) {
1277 ipc_entry_dealloc(space
, name
, entry
);
1279 entry
->ie_bits
= bits
+ delta
;
1280 ipc_entry_modified(space
, name
, entry
);
1282 is_write_unlock(space
);
1284 if (relport
!= IP_NULL
)
1285 ip_release(relport
);
1290 case MACH_PORT_RIGHT_SEND
: {
1291 mach_port_urefs_t urefs
;
1292 ipc_port_t request
= IP_NULL
;
1293 ipc_port_t nsrequest
= IP_NULL
;
1294 mach_port_mscount_t mscount
= 0;
1296 if ((bits
& MACH_PORT_TYPE_SEND
) == 0)
1299 /* maximum urefs for send is MACH_PORT_UREFS_MAX-1 */
1301 port
= (ipc_port_t
) entry
->ie_object
;
1302 assert(port
!= IP_NULL
);
1304 if (ipc_right_check(space
, port
, name
, entry
)) {
1305 assert((entry
->ie_bits
& MACH_PORT_TYPE_SEND
) == 0);
1308 /* port is locked and active */
1310 assert(port
->ip_srights
> 0);
1312 urefs
= IE_BITS_UREFS(bits
);
1313 if (MACH_PORT_UREFS_UNDERFLOW(urefs
, delta
)) {
1317 if (MACH_PORT_UREFS_OVERFLOW(urefs
+1, delta
)) {
1319 goto urefs_overflow
;
1322 if ((urefs
+ delta
) == 0) {
1323 if (--port
->ip_srights
== 0) {
1324 nsrequest
= port
->ip_nsrequest
;
1325 if (nsrequest
!= IP_NULL
) {
1326 port
->ip_nsrequest
= IP_NULL
;
1327 mscount
= port
->ip_mscount
;
1331 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
1332 assert(port
->ip_receiver_name
== name
);
1333 assert(port
->ip_receiver
== space
);
1335 assert(IE_BITS_TYPE(bits
) ==
1336 MACH_PORT_TYPE_SEND_RECEIVE
);
1338 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|
1339 MACH_PORT_TYPE_SEND
);
1340 ipc_entry_modified(space
, name
, entry
);
1342 assert(IE_BITS_TYPE(bits
) ==
1343 MACH_PORT_TYPE_SEND
);
1345 request
= ipc_right_request_cancel_macro(space
, port
,
1347 ipc_hash_delete(space
, (ipc_object_t
) port
,
1353 entry
->ie_object
= IO_NULL
;
1354 ipc_entry_dealloc(space
, name
, entry
);
1358 entry
->ie_bits
= bits
+ delta
;
1359 ipc_entry_modified(space
, name
, entry
);
1362 is_write_unlock(space
);
1364 if (nsrequest
!= IP_NULL
)
1365 ipc_notify_no_senders(nsrequest
, mscount
);
1367 if (request
!= IP_NULL
)
1368 ipc_notify_port_deleted(request
, name
);
1373 panic("ipc_right_delta: strange right");
1376 return KERN_SUCCESS
;
1379 is_write_unlock(space
);
1380 return KERN_SUCCESS
;
1383 is_write_unlock(space
);
1384 if (port
!= IP_NULL
)
1386 return KERN_INVALID_RIGHT
;
1389 is_write_unlock(space
);
1390 return KERN_INVALID_VALUE
;
1393 is_write_unlock(space
);
1394 return KERN_UREFS_OVERFLOW
;
1397 return KERN_INVALID_RIGHT
;
1401 * Routine: ipc_right_destruct
1403 * Deallocates the receive right and modifies the
1404 * user-reference count for the send rights as requested.
1406 * The space is write-locked, and is unlocked upon return.
1407 * The space must be active.
1409 * KERN_SUCCESS Count was modified.
1410 * KERN_INVALID_RIGHT Entry has wrong type.
1411 * KERN_INVALID_VALUE Bad delta for the right.
1417 mach_port_name_t name
,
1419 mach_port_delta_t srdelta
,
1422 ipc_port_t port
= IP_NULL
;
1423 ipc_entry_bits_t bits
;
1425 mach_port_urefs_t urefs
;
1426 ipc_port_t request
= IP_NULL
;
1427 ipc_port_t nsrequest
= IP_NULL
;
1428 mach_port_mscount_t mscount
= 0;
1430 bits
= entry
->ie_bits
;
1432 assert(is_active(space
));
1434 if (((bits
& MACH_PORT_TYPE_RECEIVE
) == 0) ||
1435 (srdelta
&& ((bits
& MACH_PORT_TYPE_SEND
) == 0))) {
1436 is_write_unlock(space
);
1437 return KERN_INVALID_RIGHT
;
1443 port
= (ipc_port_t
) entry
->ie_object
;
1444 assert(port
!= IP_NULL
);
1447 assert(ip_active(port
));
1448 assert(port
->ip_receiver_name
== name
);
1449 assert(port
->ip_receiver
== space
);
1451 /* Mach Port Guard Checking */
1452 if(port
->ip_guarded
&& (guard
!= port
->ip_context
)) {
1453 uint64_t portguard
= port
->ip_context
;
1455 is_write_unlock(space
);
1456 mach_port_guard_exception(name
, 0, portguard
, kGUARD_EXC_DESTROY
);
1457 return KERN_INVALID_ARGUMENT
;
1461 * First reduce the send rights as requested and
1462 * adjust the entry->ie_bits accordingly. The
1463 * ipc_entry_modified() call is made once the receive
1464 * right is destroyed too.
1469 assert(port
->ip_srights
> 0);
1471 urefs
= IE_BITS_UREFS(bits
);
1473 * Since we made sure that srdelta is negative,
1474 * the check for urefs overflow is not required.
1476 if (MACH_PORT_UREFS_UNDERFLOW(urefs
, srdelta
)) {
1480 if ((urefs
+ srdelta
) == 0) {
1481 if (--port
->ip_srights
== 0) {
1482 nsrequest
= port
->ip_nsrequest
;
1483 if (nsrequest
!= IP_NULL
) {
1484 port
->ip_nsrequest
= IP_NULL
;
1485 mscount
= port
->ip_mscount
;
1488 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_RECEIVE
);
1489 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|
1490 MACH_PORT_TYPE_SEND
);
1492 entry
->ie_bits
= bits
+ srdelta
;
1497 * Now destroy the receive right. Update space and
1498 * entry accordingly.
1501 bits
= entry
->ie_bits
;
1502 if (bits
& MACH_PORT_TYPE_SEND
) {
1503 assert(IE_BITS_UREFS(bits
) > 0);
1504 assert(IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
);
1506 if (port
->ip_pdrequest
!= NULL
) {
1508 * Since another task has requested a
1509 * destroy notification for this port, it
1510 * isn't actually being destroyed - the receive
1511 * right is just being moved to another task.
1512 * Since we still have one or more send rights,
1513 * we need to record the loss of the receive
1514 * right and enter the remaining send right
1515 * into the hash table.
1517 ipc_entry_modified(space
, name
, entry
);
1518 entry
->ie_bits
&= ~MACH_PORT_TYPE_RECEIVE
;
1519 ipc_hash_insert(space
, (ipc_object_t
) port
,
1524 * The remaining send right turns into a
1525 * dead name. Notice we don't decrement
1526 * ip_srights, generate a no-senders notif,
1527 * or use ipc_right_dncancel, because the
1528 * port is destroyed "first".
1530 bits
&= ~IE_BITS_TYPE_MASK
;
1531 bits
|= MACH_PORT_TYPE_DEAD_NAME
;
1532 if (entry
->ie_request
) {
1533 entry
->ie_request
= IE_REQ_NONE
;
1536 entry
->ie_bits
= bits
;
1537 entry
->ie_object
= IO_NULL
;
1538 ipc_entry_modified(space
, name
, entry
);
1541 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
1542 assert(IE_BITS_UREFS(bits
) == 0);
1543 request
= ipc_right_request_cancel_macro(space
, port
,
1545 entry
->ie_object
= IO_NULL
;
1546 ipc_entry_dealloc(space
, name
, entry
);
1550 is_write_unlock(space
);
1552 if (nsrequest
!= IP_NULL
)
1553 ipc_notify_no_senders(nsrequest
, mscount
);
1555 ipc_port_clear_receiver(port
);
1556 ipc_port_destroy(port
); /* consumes ref, unlocks */
1558 if (request
!= IP_NULL
)
1559 ipc_notify_port_deleted(request
, name
);
1561 return KERN_SUCCESS
;
1564 is_write_unlock(space
);
1565 return KERN_INVALID_VALUE
;
1571 * Routine: ipc_right_info
1573 * Retrieves information about the right.
1575 * The space is active and write-locked.
1576 * The space is unlocked upon return.
1578 * KERN_SUCCESS Retrieved info
1584 mach_port_name_t name
,
1586 mach_port_type_t
*typep
,
1587 mach_port_urefs_t
*urefsp
)
1590 ipc_entry_bits_t bits
;
1591 mach_port_type_t type
= 0;
1592 ipc_port_request_index_t request
;
1594 bits
= entry
->ie_bits
;
1595 request
= entry
->ie_request
;
1596 port
= (ipc_port_t
) entry
->ie_object
;
1598 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
1599 assert(IP_VALID(port
));
1601 if (request
!= IE_REQ_NONE
) {
1603 assert(ip_active(port
));
1604 type
|= ipc_port_request_type(port
, name
, request
);
1607 is_write_unlock(space
);
1609 } else if (bits
& MACH_PORT_TYPE_SEND_RIGHTS
) {
1611 * validate port is still alive - if so, get request
1612 * types while we still have it locked. Otherwise,
1613 * recapture the (now dead) bits.
1615 if (!ipc_right_check(space
, port
, name
, entry
)) {
1616 if (request
!= IE_REQ_NONE
)
1617 type
|= ipc_port_request_type(port
, name
, request
);
1619 is_write_unlock(space
);
1621 bits
= entry
->ie_bits
;
1622 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
1623 is_write_unlock(space
);
1627 is_write_unlock(space
);
1630 type
|= IE_BITS_TYPE(bits
);
1633 *urefsp
= IE_BITS_UREFS(bits
);
1634 return KERN_SUCCESS
;
1638 * Routine: ipc_right_copyin_check
1640 * Check if a subsequent ipc_right_copyin would succeed.
1642 * The space is locked (read or write) and active.
1646 ipc_right_copyin_check(
1647 __assert_only ipc_space_t space
,
1648 __unused mach_port_name_t name
,
1650 mach_msg_type_name_t msgt_name
)
1652 ipc_entry_bits_t bits
;
1655 bits
= entry
->ie_bits
;
1656 assert(is_active(space
));
1658 switch (msgt_name
) {
1659 case MACH_MSG_TYPE_MAKE_SEND
:
1660 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1664 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
1665 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1669 case MACH_MSG_TYPE_MOVE_RECEIVE
:
1670 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1674 case MACH_MSG_TYPE_COPY_SEND
:
1675 case MACH_MSG_TYPE_MOVE_SEND
:
1676 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
1678 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1681 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1684 port
= (ipc_port_t
) entry
->ie_object
;
1685 assert(port
!= IP_NULL
);
1688 * active status peek to avoid checks that will be skipped
1689 * on copyin for dead ports. Lock not held, so will not be
1690 * atomic (but once dead, there's no going back).
1692 if (!ip_active(port
)) {
1696 if (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
1697 if ((bits
& MACH_PORT_TYPE_SEND_ONCE
) == 0)
1700 if ((bits
& MACH_PORT_TYPE_SEND
) == 0)
1708 panic("ipc_right_copyin_check: strange rights");
1715 * Routine: ipc_right_copyin
1717 * Copyin a capability from a space.
1718 * If successful, the caller gets a ref
1719 * for the resulting object, unless it is IO_DEAD,
1720 * and possibly a send-once right which should
1721 * be used in a port-deleted notification.
1723 * If deadok is not TRUE, the copyin operation
1724 * will fail instead of producing IO_DEAD.
1726 * The entry is never deallocated (except
1727 * when KERN_INVALID_NAME), so the caller
1728 * should deallocate the entry if its type
1729 * is MACH_PORT_TYPE_NONE.
1731 * The space is write-locked and active.
1733 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
1734 * KERN_INVALID_RIGHT Name doesn't denote correct right.
1740 mach_port_name_t name
,
1742 mach_msg_type_name_t msgt_name
,
1744 ipc_object_t
*objectp
,
1745 ipc_port_t
*sorightp
,
1746 ipc_port_t
*releasep
,
1749 ipc_entry_bits_t bits
;
1752 *releasep
= IP_NULL
;
1755 bits
= entry
->ie_bits
;
1757 assert(is_active(space
));
1759 switch (msgt_name
) {
1760 case MACH_MSG_TYPE_MAKE_SEND
: {
1762 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1765 port
= (ipc_port_t
) entry
->ie_object
;
1766 assert(port
!= IP_NULL
);
1769 assert(ip_active(port
));
1770 assert(port
->ip_receiver_name
== name
);
1771 assert(port
->ip_receiver
== space
);
1778 *objectp
= (ipc_object_t
) port
;
1779 *sorightp
= IP_NULL
;
1783 case MACH_MSG_TYPE_MAKE_SEND_ONCE
: {
1785 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1788 port
= (ipc_port_t
) entry
->ie_object
;
1789 assert(port
!= IP_NULL
);
1792 assert(ip_active(port
));
1793 assert(port
->ip_receiver_name
== name
);
1794 assert(port
->ip_receiver
== space
);
1796 port
->ip_sorights
++;
1800 *objectp
= (ipc_object_t
) port
;
1801 *sorightp
= IP_NULL
;
1805 case MACH_MSG_TYPE_MOVE_RECEIVE
: {
1806 ipc_port_t request
= IP_NULL
;
1808 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1811 port
= (ipc_port_t
) entry
->ie_object
;
1812 assert(port
!= IP_NULL
);
1815 assert(ip_active(port
));
1816 assert(port
->ip_receiver_name
== name
);
1817 assert(port
->ip_receiver
== space
);
1819 if (bits
& MACH_PORT_TYPE_SEND
) {
1820 assert(IE_BITS_TYPE(bits
) ==
1821 MACH_PORT_TYPE_SEND_RECEIVE
);
1822 assert(IE_BITS_UREFS(bits
) > 0);
1823 assert(port
->ip_srights
> 0);
1825 ipc_hash_insert(space
, (ipc_object_t
) port
,
1829 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
1830 assert(IE_BITS_UREFS(bits
) == 0);
1832 request
= ipc_right_request_cancel_macro(space
, port
,
1834 entry
->ie_object
= IO_NULL
;
1836 entry
->ie_bits
= bits
&~ MACH_PORT_TYPE_RECEIVE
;
1837 ipc_entry_modified(space
, name
, entry
);
1839 ipc_port_clear_receiver(port
);
1840 port
->ip_receiver_name
= MACH_PORT_NULL
;
1841 port
->ip_destination
= IP_NULL
;
1843 #if IMPORTANCE_INHERITANCE
1845 * Account for boosts the current task is going to lose when
1846 * copying this right in. Tempowner ports have either not
1847 * been accounting to any task (and therefore are already in
1848 * "limbo" state w.r.t. assertions) or to some other specific
1849 * task. As we have no way to drop the latter task's assertions
1850 * here, We'll deduct those when we enqueue it on its
1851 * destination port (see ipc_port_check_circularity()).
1853 if (port
->ip_tempowner
== 0) {
1854 assert(IIT_NULL
== port
->ip_imp_task
);
1856 /* ports in limbo have to be tempowner */
1857 port
->ip_tempowner
= 1;
1858 *assertcntp
= port
->ip_impcount
;
1860 #endif /* IMPORTANCE_INHERITANCE */
1864 *objectp
= (ipc_object_t
) port
;
1865 *sorightp
= request
;
1869 case MACH_MSG_TYPE_COPY_SEND
: {
1871 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1874 /* allow for dead send-once rights */
1876 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1879 assert(IE_BITS_UREFS(bits
) > 0);
1881 port
= (ipc_port_t
) entry
->ie_object
;
1882 assert(port
!= IP_NULL
);
1884 if (ipc_right_check(space
, port
, name
, entry
)) {
1885 bits
= entry
->ie_bits
;
1889 /* port is locked and active */
1891 if ((bits
& MACH_PORT_TYPE_SEND
) == 0) {
1892 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
1893 assert(port
->ip_sorights
> 0);
1899 assert(port
->ip_srights
> 0);
1905 *objectp
= (ipc_object_t
) port
;
1906 *sorightp
= IP_NULL
;
1910 case MACH_MSG_TYPE_MOVE_SEND
: {
1911 ipc_port_t request
= IP_NULL
;
1913 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1916 /* allow for dead send-once rights */
1918 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1921 assert(IE_BITS_UREFS(bits
) > 0);
1923 port
= (ipc_port_t
) entry
->ie_object
;
1924 assert(port
!= IP_NULL
);
1926 if (ipc_right_check(space
, port
, name
, entry
)) {
1927 bits
= entry
->ie_bits
;
1931 /* port is locked and active */
1933 if ((bits
& MACH_PORT_TYPE_SEND
) == 0) {
1934 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
1935 assert(port
->ip_sorights
> 0);
1941 assert(port
->ip_srights
> 0);
1943 if (IE_BITS_UREFS(bits
) == 1) {
1944 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
1945 assert(port
->ip_receiver_name
== name
);
1946 assert(port
->ip_receiver
== space
);
1947 assert(IE_BITS_TYPE(bits
) ==
1948 MACH_PORT_TYPE_SEND_RECEIVE
);
1952 assert(IE_BITS_TYPE(bits
) ==
1953 MACH_PORT_TYPE_SEND
);
1955 request
= ipc_right_request_cancel_macro(space
, port
,
1957 ipc_hash_delete(space
, (ipc_object_t
) port
,
1959 entry
->ie_object
= IO_NULL
;
1961 entry
->ie_bits
= bits
&~
1962 (IE_BITS_UREFS_MASK
|MACH_PORT_TYPE_SEND
);
1966 entry
->ie_bits
= bits
-1; /* decrement urefs */
1968 ipc_entry_modified(space
, name
, entry
);
1971 *objectp
= (ipc_object_t
) port
;
1972 *sorightp
= request
;
1976 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
1979 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1982 /* allow for dead send rights */
1984 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1987 assert(IE_BITS_UREFS(bits
) > 0);
1989 port
= (ipc_port_t
) entry
->ie_object
;
1990 assert(port
!= IP_NULL
);
1992 if (ipc_right_check(space
, port
, name
, entry
)) {
1993 bits
= entry
->ie_bits
;
1997 /* port is locked and active */
1999 if ((bits
& MACH_PORT_TYPE_SEND_ONCE
) == 0) {
2000 assert(bits
& MACH_PORT_TYPE_SEND
);
2001 assert(port
->ip_srights
> 0);
2007 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
2008 assert(IE_BITS_UREFS(bits
) == 1);
2009 assert(port
->ip_sorights
> 0);
2011 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
2014 entry
->ie_object
= IO_NULL
;
2015 entry
->ie_bits
= bits
&~
2016 (IE_BITS_UREFS_MASK
| MACH_PORT_TYPE_SEND_ONCE
);
2017 ipc_entry_modified(space
, name
, entry
);
2018 *objectp
= (ipc_object_t
) port
;
2019 *sorightp
= request
;
2025 return KERN_INVALID_RIGHT
;
2028 return KERN_SUCCESS
;
2031 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
2032 assert(IE_BITS_UREFS(bits
) > 0);
2033 assert(entry
->ie_request
== IE_REQ_NONE
);
2034 assert(entry
->ie_object
== 0);
2040 *sorightp
= IP_NULL
;
2041 return KERN_SUCCESS
;
2044 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
2045 assert(IE_BITS_UREFS(bits
) > 0);
2046 assert(entry
->ie_request
== IE_REQ_NONE
);
2047 assert(entry
->ie_object
== 0);
2052 if (IE_BITS_UREFS(bits
) == 1) {
2053 bits
&= ~MACH_PORT_TYPE_DEAD_NAME
;
2055 entry
->ie_bits
= bits
-1; /* decrement urefs */
2056 ipc_entry_modified(space
, name
, entry
);
2058 *sorightp
= IP_NULL
;
2059 return KERN_SUCCESS
;
2064 * Routine: ipc_right_copyin_undo
2066 * Undoes the effects of an ipc_right_copyin
2067 * of a send/send-once right that is dead.
2068 * (Object is either IO_DEAD or a dead port.)
2070 * The space is write-locked and active.
2074 ipc_right_copyin_undo(
2076 mach_port_name_t name
,
2078 mach_msg_type_name_t msgt_name
,
2079 ipc_object_t object
,
2082 ipc_entry_bits_t bits
;
2084 bits
= entry
->ie_bits
;
2086 assert(is_active(space
));
2088 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2089 (msgt_name
== MACH_MSG_TYPE_COPY_SEND
) ||
2090 (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
));
2092 if (soright
!= IP_NULL
) {
2093 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2094 (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
));
2095 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2096 assert(object
!= IO_DEAD
);
2098 entry
->ie_bits
= ((bits
&~ IE_BITS_RIGHT_MASK
) |
2099 MACH_PORT_TYPE_DEAD_NAME
| 2);
2101 } else if (IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
) {
2102 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2103 (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
));
2105 entry
->ie_bits
= ((bits
&~ IE_BITS_RIGHT_MASK
) |
2106 MACH_PORT_TYPE_DEAD_NAME
| 1);
2107 } else if (IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
) {
2108 assert(object
== IO_DEAD
);
2109 assert(IE_BITS_UREFS(bits
) > 0);
2111 if (msgt_name
!= MACH_MSG_TYPE_COPY_SEND
) {
2112 assert(IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
);
2113 entry
->ie_bits
= bits
+1; /* increment urefs */
2116 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2117 (msgt_name
== MACH_MSG_TYPE_COPY_SEND
));
2118 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
2119 assert(object
!= IO_DEAD
);
2120 assert(entry
->ie_object
== object
);
2121 assert(IE_BITS_UREFS(bits
) > 0);
2123 if (msgt_name
!= MACH_MSG_TYPE_COPY_SEND
) {
2124 assert(IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
-1);
2125 entry
->ie_bits
= bits
+1; /* increment urefs */
2129 * May as well convert the entry to a dead name.
2130 * (Or if it is a compat entry, destroy it.)
2133 (void) ipc_right_check(space
, (ipc_port_t
) object
,
2135 /* object is dead so it is not locked */
2137 ipc_entry_modified(space
, name
, entry
);
2138 /* release the reference acquired by copyin */
2140 if (object
!= IO_DEAD
)
2145 * Routine: ipc_right_copyin_two_move_sends
2147 * Like ipc_right_copyin with MACH_MSG_TYPE_MOVE_SEND
2148 * and deadok == FALSE, except that this moves two
2149 * send rights at once.
2151 * The space is write-locked and active.
2152 * The object is returned with two refs/send rights.
2154 * KERN_SUCCESS Acquired an object.
2155 * KERN_INVALID_RIGHT Name doesn't denote correct right.
2159 ipc_right_copyin_two_move_sends(
2161 mach_port_name_t name
,
2163 ipc_object_t
*objectp
,
2164 ipc_port_t
*sorightp
,
2165 ipc_port_t
*releasep
)
2167 ipc_entry_bits_t bits
;
2168 mach_port_urefs_t urefs
;
2170 ipc_port_t request
= IP_NULL
;
2172 *releasep
= IP_NULL
;
2174 assert(is_active(space
));
2176 bits
= entry
->ie_bits
;
2178 if ((bits
& MACH_PORT_TYPE_SEND
) == 0)
2181 urefs
= IE_BITS_UREFS(bits
);
2185 port
= (ipc_port_t
) entry
->ie_object
;
2186 assert(port
!= IP_NULL
);
2188 if (ipc_right_check(space
, port
, name
, entry
)) {
2192 /* port is locked and active */
2194 assert(port
->ip_srights
> 0);
2197 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
2198 assert(port
->ip_receiver_name
== name
);
2199 assert(port
->ip_receiver
== space
);
2200 assert(IE_BITS_TYPE(bits
) ==
2201 MACH_PORT_TYPE_SEND_RECEIVE
);
2207 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
2209 request
= ipc_right_request_cancel_macro(space
, port
,
2214 ipc_hash_delete(space
, (ipc_object_t
) port
,
2216 entry
->ie_object
= IO_NULL
;
2218 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|MACH_PORT_TYPE_SEND
);
2220 port
->ip_srights
+= 2;
2223 entry
->ie_bits
= bits
-2; /* decrement urefs */
2225 ipc_entry_modified(space
, name
, entry
);
2229 *objectp
= (ipc_object_t
) port
;
2230 *sorightp
= request
;
2231 return KERN_SUCCESS
;
2234 return KERN_INVALID_RIGHT
;
2239 * Routine: ipc_right_copyin_two
2241 * Like ipc_right_copyin with two dispositions,
2242 * each of which results in a send or send-once right,
2243 * and deadok = FALSE.
2245 * The space is write-locked and active.
2246 * The object is returned with two refs/rights.
2248 * KERN_SUCCESS Acquired an object.
2249 * KERN_INVALID_RIGHT Name doesn't denote correct right(s).
2250 * KERN_INVALID_CAPABILITY Name doesn't denote correct right for msgt_two.
2253 ipc_right_copyin_two(
2255 mach_port_name_t name
,
2257 mach_msg_type_name_t msgt_one
,
2258 mach_msg_type_name_t msgt_two
,
2259 ipc_object_t
*objectp
,
2260 ipc_port_t
*sorightp
,
2261 ipc_port_t
*releasep
)
2266 assert(MACH_MSG_TYPE_PORT_ANY_SEND(msgt_one
));
2267 assert(MACH_MSG_TYPE_PORT_ANY_SEND(msgt_two
));
2271 * Pre-validate the second disposition is possible all by itself.
2273 if (!ipc_right_copyin_check(space
, name
, entry
, msgt_two
)) {
2274 return KERN_INVALID_CAPABILITY
;
2278 * This is a little tedious to make atomic, because
2279 * there are 25 combinations of valid dispositions.
2280 * However, most are easy.
2284 * If either is move-sonce, then there must be an error.
2286 if (msgt_one
== MACH_MSG_TYPE_MOVE_SEND_ONCE
||
2287 msgt_two
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
2288 return KERN_INVALID_RIGHT
;
2291 if ((msgt_one
== MACH_MSG_TYPE_MAKE_SEND
) ||
2292 (msgt_one
== MACH_MSG_TYPE_MAKE_SEND_ONCE
) ||
2293 (msgt_two
== MACH_MSG_TYPE_MAKE_SEND
) ||
2294 (msgt_two
== MACH_MSG_TYPE_MAKE_SEND_ONCE
)) {
2296 * One of the dispositions needs a receive right.
2298 * If the copyin below succeeds, we know the receive
2299 * right is there (because the pre-validation of
2300 * the second disposition already succeeded in our
2303 * Hence the port is not in danger of dying.
2305 ipc_object_t object_two
;
2307 kr
= ipc_right_copyin(space
, name
, entry
,
2309 objectp
, sorightp
, releasep
,
2311 assert(assertcnt
== 0);
2312 if (kr
!= KERN_SUCCESS
) {
2316 assert(IO_VALID(*objectp
));
2317 assert(*sorightp
== IP_NULL
);
2318 assert(*releasep
== IP_NULL
);
2321 * Now copyin the second (previously validated)
2322 * disposition. The result can't be a dead port,
2323 * as no valid disposition can make us lose our
2326 kr
= ipc_right_copyin(space
, name
, entry
,
2328 &object_two
, sorightp
, releasep
,
2330 assert(assertcnt
== 0);
2331 assert(kr
== KERN_SUCCESS
);
2332 assert(*sorightp
== IP_NULL
);
2333 assert(*releasep
== IP_NULL
);
2334 assert(object_two
== *objectp
);
2335 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
2337 } else if ((msgt_one
== MACH_MSG_TYPE_MOVE_SEND
) &&
2338 (msgt_two
== MACH_MSG_TYPE_MOVE_SEND
)) {
2340 * This is an easy case. Just use our
2341 * handy-dandy special-purpose copyin call
2342 * to get two send rights for the price of one.
2344 kr
= ipc_right_copyin_two_move_sends(space
, name
, entry
,
2347 if (kr
!= KERN_SUCCESS
) {
2352 mach_msg_type_name_t msgt_name
;
2355 * Must be either a single move-send and a
2356 * copy-send, or two copy-send dispositions.
2357 * Use the disposition with the greatest side
2358 * effects for the actual copyin - then just
2359 * duplicate the send right you get back.
2361 if (msgt_one
== MACH_MSG_TYPE_MOVE_SEND
||
2362 msgt_two
== MACH_MSG_TYPE_MOVE_SEND
) {
2363 msgt_name
= MACH_MSG_TYPE_MOVE_SEND
;
2365 msgt_name
= MACH_MSG_TYPE_COPY_SEND
;
2368 kr
= ipc_right_copyin(space
, name
, entry
,
2370 objectp
, sorightp
, releasep
,
2372 assert(assertcnt
== 0);
2373 if (kr
!= KERN_SUCCESS
) {
2378 * Copy the right we got back. If it is dead now,
2379 * that's OK. Neither right will be usable to send
2382 (void)ipc_port_copy_send((ipc_port_t
)*objectp
);
2385 return KERN_SUCCESS
;
2390 * Routine: ipc_right_copyout
2392 * Copyout a capability to a space.
2393 * If successful, consumes a ref for the object.
2395 * Always succeeds when given a newly-allocated entry,
2396 * because user-reference overflow isn't a possibility.
2398 * If copying out the object would cause the user-reference
2399 * count in the entry to overflow, and overflow is TRUE,
2400 * then instead the user-reference count is left pegged
2401 * to its maximum value and the copyout succeeds anyway.
2403 * The space is write-locked and active.
2404 * The object is locked and active.
2405 * The object is unlocked; the space isn't.
2407 * KERN_SUCCESS Copied out capability.
2408 * KERN_UREFS_OVERFLOW User-refs would overflow;
2409 * guaranteed not to happen with a fresh entry
2410 * or if overflow=TRUE was specified.
2416 mach_port_name_t name
,
2418 mach_msg_type_name_t msgt_name
,
2420 ipc_object_t object
)
2422 ipc_entry_bits_t bits
;
2425 bits
= entry
->ie_bits
;
2427 assert(IO_VALID(object
));
2428 assert(io_otype(object
) == IOT_PORT
);
2429 assert(io_active(object
));
2430 assert(entry
->ie_object
== object
);
2432 port
= (ipc_port_t
) object
;
2434 switch (msgt_name
) {
2435 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
2437 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2438 assert(port
->ip_sorights
> 0);
2440 /* transfer send-once right and ref to entry */
2443 entry
->ie_bits
= bits
| (MACH_PORT_TYPE_SEND_ONCE
| 1);
2444 ipc_entry_modified(space
, name
, entry
);
2447 case MACH_MSG_TYPE_PORT_SEND
:
2448 assert(port
->ip_srights
> 0);
2450 if (bits
& MACH_PORT_TYPE_SEND
) {
2451 mach_port_urefs_t urefs
= IE_BITS_UREFS(bits
);
2453 assert(port
->ip_srights
> 1);
2455 assert(urefs
< MACH_PORT_UREFS_MAX
);
2457 if (urefs
+1 == MACH_PORT_UREFS_MAX
) {
2459 /* leave urefs pegged to maximum */
2464 return KERN_SUCCESS
;
2468 return KERN_UREFS_OVERFLOW
;
2474 } else if (bits
& MACH_PORT_TYPE_RECEIVE
) {
2475 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
2476 assert(IE_BITS_UREFS(bits
) == 0);
2478 /* transfer send right to entry */
2483 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2484 assert(IE_BITS_UREFS(bits
) == 0);
2486 /* transfer send right and ref to entry */
2489 /* entry is locked holding ref, so can use port */
2491 ipc_hash_insert(space
, (ipc_object_t
) port
,
2495 entry
->ie_bits
= (bits
| MACH_PORT_TYPE_SEND
) + 1;
2496 ipc_entry_modified(space
, name
, entry
);
2499 case MACH_MSG_TYPE_PORT_RECEIVE
: {
2502 #if IMPORTANCE_INHERITANCE
2503 natural_t assertcnt
= port
->ip_impcount
;
2504 #endif /* IMPORTANCE_INHERITANCE */
2506 assert(port
->ip_mscount
== 0);
2507 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
2508 dest
= port
->ip_destination
;
2510 port
->ip_receiver_name
= name
;
2511 port
->ip_receiver
= space
;
2513 assert((bits
& MACH_PORT_TYPE_RECEIVE
) == 0);
2515 if (bits
& MACH_PORT_TYPE_SEND
) {
2516 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
2517 assert(IE_BITS_UREFS(bits
) > 0);
2518 assert(port
->ip_srights
> 0);
2523 /* entry is locked holding ref, so can use port */
2525 ipc_hash_delete(space
, (ipc_object_t
) port
,
2528 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2529 assert(IE_BITS_UREFS(bits
) == 0);
2531 /* transfer ref to entry */
2534 entry
->ie_bits
= bits
| MACH_PORT_TYPE_RECEIVE
;
2535 ipc_entry_modified(space
, name
, entry
);
2537 if (dest
!= IP_NULL
) {
2538 #if IMPORTANCE_INHERITANCE
2540 * Deduct the assertion counts we contributed to
2541 * the old destination port. They've already
2542 * been reflected into the task as a result of
2546 ipc_port_impcount_delta(dest
, 0 - assertcnt
, IP_NULL
);
2548 #endif /* IMPORTANCE_INHERITANCE */
2555 panic("ipc_right_copyout: strange rights");
2557 return KERN_SUCCESS
;
2561 * Routine: ipc_right_rename
2563 * Transfer an entry from one name to another.
2564 * The old entry is deallocated.
2566 * The space is write-locked and active.
2567 * The new entry is unused. Upon return,
2568 * the space is unlocked.
2570 * KERN_SUCCESS Moved entry to new name.
2576 mach_port_name_t oname
,
2578 mach_port_name_t nname
,
2581 ipc_port_request_index_t request
= oentry
->ie_request
;
2582 ipc_entry_bits_t bits
= oentry
->ie_bits
;
2583 ipc_object_t object
= oentry
->ie_object
;
2584 ipc_port_t release_port
= IP_NULL
;
2586 assert(is_active(space
));
2587 assert(oname
!= nname
);
2590 * If IE_BITS_COMPAT, we can't allow the entry to be renamed
2591 * if the port is dead. (This would foil ipc_port_destroy.)
2592 * Instead we should fail because oentry shouldn't exist.
2593 * Note IE_BITS_COMPAT implies ie_request != 0.
2596 if (request
!= IE_REQ_NONE
) {
2599 assert(bits
& MACH_PORT_TYPE_PORT_RIGHTS
);
2600 port
= (ipc_port_t
) object
;
2601 assert(port
!= IP_NULL
);
2603 if (ipc_right_check(space
, port
, oname
, oentry
)) {
2604 request
= IE_REQ_NONE
;
2606 bits
= oentry
->ie_bits
;
2607 release_port
= port
;
2608 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
2609 assert(oentry
->ie_request
== IE_REQ_NONE
);
2611 /* port is locked and active */
2613 ipc_port_request_rename(port
, request
, oname
, nname
);
2615 oentry
->ie_request
= IE_REQ_NONE
;
2619 /* initialize nentry before letting ipc_hash_insert see it */
2621 assert((nentry
->ie_bits
& IE_BITS_RIGHT_MASK
) == 0);
2622 nentry
->ie_bits
|= bits
& IE_BITS_RIGHT_MASK
;
2623 nentry
->ie_request
= request
;
2624 nentry
->ie_object
= object
;
2626 switch (IE_BITS_TYPE(bits
)) {
2627 case MACH_PORT_TYPE_SEND
: {
2630 port
= (ipc_port_t
) object
;
2631 assert(port
!= IP_NULL
);
2633 /* remember, there are no other share entries possible */
2634 /* or we can't do the rename. Therefore we do not need */
2635 /* to check the other subspaces */
2636 ipc_hash_delete(space
, (ipc_object_t
) port
, oname
, oentry
);
2637 ipc_hash_insert(space
, (ipc_object_t
) port
, nname
, nentry
);
2641 case MACH_PORT_TYPE_RECEIVE
:
2642 case MACH_PORT_TYPE_SEND_RECEIVE
: {
2645 port
= (ipc_port_t
) object
;
2646 assert(port
!= IP_NULL
);
2649 assert(ip_active(port
));
2650 assert(port
->ip_receiver_name
== oname
);
2651 assert(port
->ip_receiver
== space
);
2653 port
->ip_receiver_name
= nname
;
2658 case MACH_PORT_TYPE_PORT_SET
: {
2661 pset
= (ipc_pset_t
) object
;
2662 assert(pset
!= IPS_NULL
);
2665 assert(ips_active(pset
));
2666 assert(pset
->ips_local_name
== oname
);
2668 pset
->ips_local_name
= nname
;
2673 case MACH_PORT_TYPE_SEND_ONCE
:
2674 case MACH_PORT_TYPE_DEAD_NAME
:
2678 panic("ipc_right_rename: strange rights");
2681 assert(oentry
->ie_request
== IE_REQ_NONE
);
2682 oentry
->ie_object
= IO_NULL
;
2683 ipc_entry_dealloc(space
, oname
, oentry
);
2684 ipc_entry_modified(space
, nname
, nentry
);
2685 is_write_unlock(space
);
2687 if (release_port
!= IP_NULL
)
2688 ip_release(release_port
);
2690 return KERN_SUCCESS
;