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/ipc_kobject.h>
79 #include <kern/misc_protos.h>
81 #include <ipc/ipc_entry.h>
82 #include <ipc/ipc_space.h>
83 #include <ipc/ipc_object.h>
84 #include <ipc/ipc_hash.h>
85 #include <ipc/ipc_port.h>
86 #include <ipc/ipc_pset.h>
87 #include <ipc/ipc_right.h>
88 #include <ipc/ipc_notify.h>
89 #include <ipc/ipc_table.h>
90 #include <ipc/ipc_importance.h>
91 #include <security/mac_mach_internal.h>
93 /* Allow IPC to generate mach port guard exceptions */
95 mach_port_guard_exception(
96 mach_port_name_t name
,
101 * Routine: ipc_right_lookup_write
103 * Finds an entry in a space, given the name.
105 * Nothing locked. If successful, the space is write-locked.
107 * KERN_SUCCESS Found an entry.
108 * KERN_INVALID_TASK The space is dead.
109 * KERN_INVALID_NAME Name doesn't exist in space.
113 ipc_right_lookup_write(
115 mach_port_name_t name
,
120 assert(space
!= IS_NULL
);
122 is_write_lock(space
);
124 if (!is_active(space
)) {
125 is_write_unlock(space
);
126 return KERN_INVALID_TASK
;
129 if ((entry
= ipc_entry_lookup(space
, name
)) == IE_NULL
) {
130 is_write_unlock(space
);
131 return KERN_INVALID_NAME
;
139 * Routine: ipc_right_lookup_two_write
141 * Like ipc_right_lookup except that it returns two
142 * entries for two different names that were looked
143 * up under the same space lock.
145 * Nothing locked. If successful, the space is write-locked.
147 * KERN_INVALID_TASK The space is dead.
148 * KERN_INVALID_NAME Name doesn't exist in space.
152 ipc_right_lookup_two_write(
154 mach_port_name_t name1
,
155 ipc_entry_t
*entryp1
,
156 mach_port_name_t name2
,
157 ipc_entry_t
*entryp2
)
162 assert(space
!= IS_NULL
);
164 is_write_lock(space
);
166 if (!is_active(space
)) {
167 is_write_unlock(space
);
168 return KERN_INVALID_TASK
;
171 if ((entry1
= ipc_entry_lookup(space
, name1
)) == IE_NULL
) {
172 is_write_unlock(space
);
173 return KERN_INVALID_NAME
;
175 if ((entry2
= ipc_entry_lookup(space
, name2
)) == IE_NULL
) {
176 is_write_unlock(space
);
177 return KERN_INVALID_NAME
;
185 * Routine: ipc_right_reverse
187 * Translate (space, object) -> (name, entry).
188 * Only finds send/receive rights.
189 * Returns TRUE if an entry is found; if so,
190 * the object is locked and active.
192 * The space must be locked (read or write) and active.
193 * Nothing else locked.
200 mach_port_name_t
*namep
,
204 mach_port_name_t name
;
207 /* would switch on io_otype to handle multiple types of object */
209 assert(is_active(space
));
210 assert(io_otype(object
) == IOT_PORT
);
212 port
= (ipc_port_t
) object
;
215 if (!ip_active(port
)) {
221 if (port
->ip_receiver
== space
) {
222 name
= port
->ip_receiver_name
;
223 assert(name
!= MACH_PORT_NULL
);
225 entry
= ipc_entry_lookup(space
, name
);
227 assert(entry
!= IE_NULL
);
228 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
229 assert(port
== (ipc_port_t
) entry
->ie_object
);
236 if (ipc_hash_lookup(space
, (ipc_object_t
) port
, namep
, entryp
)) {
237 assert((entry
= *entryp
) != IE_NULL
);
238 assert(IE_BITS_TYPE(entry
->ie_bits
) == MACH_PORT_TYPE_SEND
);
239 assert(port
== (ipc_port_t
) entry
->ie_object
);
249 * Routine: ipc_right_dnrequest
251 * Make a dead-name request, returning the previously
252 * registered send-once right. If notify is IP_NULL,
253 * just cancels the previously registered request.
256 * Nothing locked. May allocate memory.
257 * Only consumes/returns refs if successful.
259 * KERN_SUCCESS Made/canceled dead-name request.
260 * KERN_INVALID_TASK The space is dead.
261 * KERN_INVALID_NAME Name doesn't exist in space.
262 * KERN_INVALID_RIGHT Name doesn't denote port/dead rights.
263 * KERN_INVALID_ARGUMENT Name denotes dead name, but
264 * immediate is FALSE or notify is IP_NULL.
265 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
269 ipc_right_request_alloc(
271 mach_port_name_t name
,
273 boolean_t send_possible
,
275 ipc_port_t
*previousp
)
277 ipc_port_request_index_t prev_request
;
278 ipc_port_t previous
= IP_NULL
;
282 #if IMPORTANCE_INHERITANCE
283 boolean_t needboost
= FALSE
;
284 #endif /* IMPORTANCE_INHERITANCE */
287 ipc_port_t port
= IP_NULL
;
289 kr
= ipc_right_lookup_write(space
, name
, &entry
);
290 if (kr
!= KERN_SUCCESS
)
293 /* space is write-locked and active */
295 prev_request
= entry
->ie_request
;
297 /* if nothing to do or undo, we're done */
298 if (notify
== IP_NULL
&& prev_request
== IE_REQ_NONE
) {
299 is_write_unlock(space
);
300 *previousp
= IP_NULL
;
304 /* see if the entry is of proper type for requests */
305 if (entry
->ie_bits
& MACH_PORT_TYPE_PORT_RIGHTS
) {
306 ipc_port_request_index_t new_request
;
308 port
= (ipc_port_t
) entry
->ie_object
;
309 assert(port
!= IP_NULL
);
311 if (!ipc_right_check(space
, port
, name
, entry
)) {
312 /* port is locked and active */
314 /* if no new request, just cancel previous */
315 if (notify
== IP_NULL
) {
316 if (prev_request
!= IE_REQ_NONE
)
317 previous
= ipc_port_request_cancel(port
, name
, prev_request
);
319 entry
->ie_request
= IE_REQ_NONE
;
320 ipc_entry_modified(space
, name
, entry
);
321 is_write_unlock(space
);
326 * send-once rights, kernel objects, and non-full other queues
327 * fire immediately (if immediate specified).
329 if (send_possible
&& immediate
&&
330 ((entry
->ie_bits
& MACH_PORT_TYPE_SEND_ONCE
) ||
331 port
->ip_receiver
== ipc_space_kernel
|| !ip_full(port
))) {
332 if (prev_request
!= IE_REQ_NONE
)
333 previous
= ipc_port_request_cancel(port
, name
, prev_request
);
335 entry
->ie_request
= IE_REQ_NONE
;
336 ipc_entry_modified(space
, name
, entry
);
337 is_write_unlock(space
);
339 ipc_notify_send_possible(notify
, name
);
344 * If there is a previous request, free it. Any subsequent
345 * allocation cannot fail, thus assuring an atomic swap.
347 if (prev_request
!= IE_REQ_NONE
)
348 previous
= ipc_port_request_cancel(port
, name
, prev_request
);
350 #if IMPORTANCE_INHERITANCE
351 kr
= ipc_port_request_alloc(port
, name
, notify
,
352 send_possible
, immediate
,
353 &new_request
, &needboost
);
355 kr
= ipc_port_request_alloc(port
, name
, notify
,
356 send_possible
, immediate
,
358 #endif /* IMPORTANCE_INHERITANCE */
359 if (kr
!= KERN_SUCCESS
) {
360 assert(previous
== IP_NULL
);
361 is_write_unlock(space
);
363 kr
= ipc_port_request_grow(port
, ITS_SIZE_NONE
);
364 /* port is unlocked */
366 if (kr
!= KERN_SUCCESS
)
373 assert(new_request
!= IE_REQ_NONE
);
374 entry
->ie_request
= new_request
;
375 ipc_entry_modified(space
, name
, entry
);
376 is_write_unlock(space
);
378 #if IMPORTANCE_INHERITANCE
379 if (needboost
== TRUE
) {
380 if (ipc_port_importance_delta(port
, IPID_OPTION_SENDPOSSIBLE
, 1) == FALSE
)
383 #endif /* IMPORTANCE_INHERITANCE */
388 /* entry may have changed to dead-name by ipc_right_check() */
392 /* treat send_possible requests as immediate w.r.t. dead-name */
393 if ((send_possible
|| immediate
) && notify
!= IP_NULL
&&
394 (entry
->ie_bits
& MACH_PORT_TYPE_DEAD_NAME
)) {
395 mach_port_urefs_t urefs
= IE_BITS_UREFS(entry
->ie_bits
);
399 /* leave urefs pegged to maximum if it overflowed */
400 if (urefs
< MACH_PORT_UREFS_MAX
)
401 (entry
->ie_bits
)++; /* increment urefs */
403 ipc_entry_modified(space
, name
, entry
);
405 is_write_unlock(space
);
410 ipc_notify_dead_name(notify
, name
);
415 kr
= (entry
->ie_bits
& MACH_PORT_TYPE_PORT_OR_DEAD
) ?
416 KERN_INVALID_ARGUMENT
: KERN_INVALID_RIGHT
;
418 is_write_unlock(space
);
426 *previousp
= previous
;
431 * Routine: ipc_right_request_cancel
433 * Cancel a notification request and return the send-once right.
434 * Afterwards, entry->ie_request == 0.
436 * The space must be write-locked; the port must be locked.
437 * The port must be active; the space doesn't have to be.
441 ipc_right_request_cancel(
442 __unused ipc_space_t space
,
444 mach_port_name_t name
,
449 assert(ip_active(port
));
450 assert(port
== (ipc_port_t
) entry
->ie_object
);
452 if (entry
->ie_request
== IE_REQ_NONE
)
455 previous
= ipc_port_request_cancel(port
, name
, entry
->ie_request
);
456 entry
->ie_request
= IE_REQ_NONE
;
457 ipc_entry_modified(space
, name
, entry
);
462 * Routine: ipc_right_inuse
464 * Check if an entry is being used.
465 * Returns TRUE if it is.
467 * The space is write-locked and active.
468 * It is unlocked if the entry is inuse.
474 __unused mach_port_name_t name
,
477 if (IE_BITS_TYPE(entry
->ie_bits
) != MACH_PORT_TYPE_NONE
) {
478 is_write_unlock(space
);
485 * Routine: ipc_right_check
487 * Check if the port has died. If it has,
488 * clean up the entry and return TRUE.
490 * The space is write-locked; the port is not locked.
491 * If returns FALSE, the port is also locked and active.
492 * Otherwise, entry is converted to a dead name.
494 * Caller is responsible for a reference to port if it
495 * had died (returns TRUE).
502 mach_port_name_t name
,
505 ipc_entry_bits_t bits
;
507 assert(is_active(space
));
508 assert(port
== (ipc_port_t
) entry
->ie_object
);
514 /* this was either a pure send right or a send-once right */
516 bits
= entry
->ie_bits
;
517 assert((bits
& MACH_PORT_TYPE_RECEIVE
) == 0);
518 assert(IE_BITS_UREFS(bits
) > 0);
520 if (bits
& MACH_PORT_TYPE_SEND
) {
521 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
522 assert(IE_BITS_UREFS(bits
) > 0);
523 assert(port
->ip_srights
> 0);
526 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
527 assert(IE_BITS_UREFS(bits
) == 1);
528 assert(port
->ip_sorights
> 0);
534 * delete SEND rights from ipc hash.
537 if ((bits
& MACH_PORT_TYPE_SEND
) != 0) {
538 ipc_hash_delete(space
, (ipc_object_t
)port
, name
, entry
);
541 /* convert entry to dead name */
542 bits
= (bits
&~ IE_BITS_TYPE_MASK
) | MACH_PORT_TYPE_DEAD_NAME
;
545 * If there was a notification request outstanding on this
546 * name, and the port went dead, that notification
547 * must already be on its way up from the port layer.
549 * Add the reference that the notification carries. It
550 * is done here, and not in the notification delivery,
551 * because the latter doesn't have a space reference and
552 * trying to actually move a send-right reference would
553 * get short-circuited into a MACH_PORT_DEAD by IPC. Since
554 * all calls that deal with the right eventually come
555 * through here, it has the same result.
557 * Once done, clear the request index so we only account
560 if (entry
->ie_request
!= IE_REQ_NONE
) {
561 if (ipc_port_request_type(port
, name
, entry
->ie_request
) != 0) {
562 /* if urefs are pegged due to overflow, leave them pegged */
563 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
564 bits
++; /* increment urefs */
566 entry
->ie_request
= IE_REQ_NONE
;
568 entry
->ie_bits
= bits
;
569 entry
->ie_object
= IO_NULL
;
570 ipc_entry_modified(space
, name
, entry
);
575 * Routine: ipc_right_terminate
577 * Cleans up an entry in a terminated space.
578 * The entry isn't deallocated or removed
579 * from reverse hash tables.
581 * The space is dead and unlocked.
587 mach_port_name_t name
,
590 ipc_entry_bits_t bits
;
591 mach_port_type_t type
;
593 bits
= entry
->ie_bits
;
594 type
= IE_BITS_TYPE(bits
);
596 assert(!is_active(space
));
599 * IE_BITS_COMPAT/ipc_right_dncancel doesn't have this
600 * problem, because we check that the port is active. If
601 * we didn't cancel IE_BITS_COMPAT, ipc_port_destroy
602 * would still work, but dead space refs would accumulate
603 * in ip_dnrequests. They would use up slots in
604 * ip_dnrequests and keep the spaces from being freed.
608 case MACH_PORT_TYPE_DEAD_NAME
:
609 assert(entry
->ie_request
== IE_REQ_NONE
);
610 assert(entry
->ie_object
== IO_NULL
);
613 case MACH_PORT_TYPE_PORT_SET
: {
614 ipc_pset_t pset
= (ipc_pset_t
) entry
->ie_object
;
616 assert(entry
->ie_request
== IE_REQ_NONE
);
617 assert(pset
!= IPS_NULL
);
620 assert(ips_active(pset
));
621 ipc_pset_destroy(pset
); /* consumes ref, unlocks */
625 case MACH_PORT_TYPE_SEND
:
626 case MACH_PORT_TYPE_RECEIVE
:
627 case MACH_PORT_TYPE_SEND_RECEIVE
:
628 case MACH_PORT_TYPE_SEND_ONCE
: {
629 ipc_port_t port
= (ipc_port_t
) entry
->ie_object
;
631 ipc_port_t nsrequest
= IP_NULL
;
632 mach_port_mscount_t mscount
= 0;
634 assert(port
!= IP_NULL
);
637 if (!ip_active(port
)) {
643 request
= ipc_right_request_cancel_macro(space
, port
,
646 if (type
& MACH_PORT_TYPE_SEND
) {
647 assert(port
->ip_srights
> 0);
648 if (--port
->ip_srights
== 0
650 nsrequest
= port
->ip_nsrequest
;
651 if (nsrequest
!= IP_NULL
) {
652 port
->ip_nsrequest
= IP_NULL
;
653 mscount
= port
->ip_mscount
;
658 if (type
& MACH_PORT_TYPE_RECEIVE
) {
659 assert(port
->ip_receiver_name
== name
);
660 assert(port
->ip_receiver
== space
);
662 ipc_port_destroy(port
); /* clears receiver, consumes our ref, unlocks */
664 } else if (type
& MACH_PORT_TYPE_SEND_ONCE
) {
665 assert(port
->ip_sorights
> 0);
668 ipc_notify_send_once(port
); /* consumes our ref */
670 assert(port
->ip_receiver
!= space
);
676 if (nsrequest
!= IP_NULL
)
677 ipc_notify_no_senders(nsrequest
, mscount
);
679 if (request
!= IP_NULL
)
680 ipc_notify_port_deleted(request
, name
);
685 panic("ipc_right_terminate: strange type - 0x%x", type
);
690 * Routine: ipc_right_destroy
692 * Destroys an entry in a space.
694 * The space is write-locked (returns unlocked).
695 * The space must be active.
697 * KERN_SUCCESS The entry was destroyed.
703 mach_port_name_t name
,
705 boolean_t check_guard
,
708 ipc_entry_bits_t bits
;
709 mach_port_type_t type
;
711 bits
= entry
->ie_bits
;
712 entry
->ie_bits
&= ~IE_BITS_TYPE_MASK
;
713 type
= IE_BITS_TYPE(bits
);
715 assert(is_active(space
));
718 case MACH_PORT_TYPE_DEAD_NAME
:
719 assert(entry
->ie_request
== IE_REQ_NONE
);
720 assert(entry
->ie_object
== IO_NULL
);
722 ipc_entry_dealloc(space
, name
, entry
);
723 is_write_unlock(space
);
726 case MACH_PORT_TYPE_PORT_SET
: {
727 ipc_pset_t pset
= (ipc_pset_t
) entry
->ie_object
;
729 assert(entry
->ie_request
== IE_REQ_NONE
);
730 assert(pset
!= IPS_NULL
);
732 entry
->ie_object
= IO_NULL
;
733 ipc_entry_dealloc(space
, name
, entry
);
736 is_write_unlock(space
);
738 assert(ips_active(pset
));
739 ipc_pset_destroy(pset
); /* consumes ref, unlocks */
743 case MACH_PORT_TYPE_SEND
:
744 case MACH_PORT_TYPE_RECEIVE
:
745 case MACH_PORT_TYPE_SEND_RECEIVE
:
746 case MACH_PORT_TYPE_SEND_ONCE
: {
747 ipc_port_t port
= (ipc_port_t
) entry
->ie_object
;
748 ipc_port_t nsrequest
= IP_NULL
;
749 mach_port_mscount_t mscount
= 0;
752 assert(port
!= IP_NULL
);
754 if (type
== MACH_PORT_TYPE_SEND
)
755 ipc_hash_delete(space
, (ipc_object_t
) port
,
760 if (!ip_active(port
)) {
761 assert((type
& MACH_PORT_TYPE_RECEIVE
) == 0);
763 entry
->ie_request
= IE_REQ_NONE
;
764 entry
->ie_object
= IO_NULL
;
765 ipc_entry_dealloc(space
, name
, entry
);
766 is_write_unlock(space
);
771 /* For receive rights, check for guarding */
772 if ((type
& MACH_PORT_TYPE_RECEIVE
) &&
773 (check_guard
) && (port
->ip_guarded
) &&
774 (guard
!= port
->ip_context
)) {
775 /* Guard Violation */
776 uint64_t portguard
= port
->ip_context
;
778 is_write_unlock(space
);
779 /* Raise mach port guard exception */
780 mach_port_guard_exception(name
, 0, portguard
, kGUARD_EXC_DESTROY
);
781 return KERN_INVALID_RIGHT
;
785 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
787 entry
->ie_object
= IO_NULL
;
788 ipc_entry_dealloc(space
, name
, entry
);
789 is_write_unlock(space
);
791 if (type
& MACH_PORT_TYPE_SEND
) {
792 assert(port
->ip_srights
> 0);
793 if (--port
->ip_srights
== 0) {
794 nsrequest
= port
->ip_nsrequest
;
795 if (nsrequest
!= IP_NULL
) {
796 port
->ip_nsrequest
= IP_NULL
;
797 mscount
= port
->ip_mscount
;
802 if (type
& MACH_PORT_TYPE_RECEIVE
) {
803 assert(ip_active(port
));
804 assert(port
->ip_receiver
== space
);
806 ipc_port_destroy(port
); /* clears receiver, consumes our ref, unlocks */
808 } else if (type
& MACH_PORT_TYPE_SEND_ONCE
) {
809 assert(port
->ip_sorights
> 0);
812 ipc_notify_send_once(port
); /* consumes our ref */
814 assert(port
->ip_receiver
!= space
);
820 if (nsrequest
!= IP_NULL
)
821 ipc_notify_no_senders(nsrequest
, mscount
);
823 if (request
!= IP_NULL
)
824 ipc_notify_port_deleted(request
, name
);
831 panic("ipc_right_destroy: strange type");
838 * Routine: ipc_right_dealloc
840 * Releases a send/send-once/dead-name user ref.
841 * Like ipc_right_delta with a delta of -1,
842 * but looks at the entry to determine the right.
844 * The space is write-locked, and is unlocked upon return.
845 * The space must be active.
847 * KERN_SUCCESS A user ref was released.
848 * KERN_INVALID_RIGHT Entry has wrong type.
854 mach_port_name_t name
,
857 ipc_port_t port
= IP_NULL
;
858 ipc_entry_bits_t bits
;
859 mach_port_type_t type
;
861 bits
= entry
->ie_bits
;
862 type
= IE_BITS_TYPE(bits
);
865 assert(is_active(space
));
868 case MACH_PORT_TYPE_DEAD_NAME
: {
871 assert(IE_BITS_UREFS(bits
) > 0);
872 assert(entry
->ie_request
== IE_REQ_NONE
);
873 assert(entry
->ie_object
== IO_NULL
);
875 if (IE_BITS_UREFS(bits
) == 1) {
876 ipc_entry_dealloc(space
, name
, entry
);
878 /* if urefs are pegged due to overflow, leave them pegged */
879 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
880 entry
->ie_bits
= bits
-1; /* decrement urefs */
881 ipc_entry_modified(space
, name
, entry
);
883 is_write_unlock(space
);
885 /* release any port that got converted to dead name below */
891 case MACH_PORT_TYPE_SEND_ONCE
: {
894 assert(IE_BITS_UREFS(bits
) == 1);
896 port
= (ipc_port_t
) entry
->ie_object
;
897 assert(port
!= IP_NULL
);
899 if (ipc_right_check(space
, port
, name
, entry
)) {
901 bits
= entry
->ie_bits
;
902 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
903 goto dead_name
; /* it will release port */
905 /* port is locked and active */
907 assert(port
->ip_sorights
> 0);
909 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
912 entry
->ie_object
= IO_NULL
;
913 ipc_entry_dealloc(space
, name
, entry
);
915 is_write_unlock(space
);
917 ipc_notify_send_once(port
);
919 if (request
!= IP_NULL
)
920 ipc_notify_port_deleted(request
, name
);
924 case MACH_PORT_TYPE_SEND
: {
925 ipc_port_t request
= IP_NULL
;
926 ipc_port_t nsrequest
= IP_NULL
;
927 mach_port_mscount_t mscount
= 0;
930 assert(IE_BITS_UREFS(bits
) > 0);
932 port
= (ipc_port_t
) entry
->ie_object
;
933 assert(port
!= IP_NULL
);
935 if (ipc_right_check(space
, port
, name
, entry
)) {
936 bits
= entry
->ie_bits
;
937 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
938 goto dead_name
; /* it will release port */
940 /* port is locked and active */
942 assert(port
->ip_srights
> 0);
944 if (IE_BITS_UREFS(bits
) == 1) {
945 if (--port
->ip_srights
== 0) {
946 nsrequest
= port
->ip_nsrequest
;
947 if (nsrequest
!= IP_NULL
) {
948 port
->ip_nsrequest
= IP_NULL
;
949 mscount
= port
->ip_mscount
;
953 request
= ipc_right_request_cancel_macro(space
, port
,
955 ipc_hash_delete(space
, (ipc_object_t
) port
,
959 entry
->ie_object
= IO_NULL
;
960 ipc_entry_dealloc(space
, name
, entry
);
961 is_write_unlock(space
);
966 /* if urefs are pegged due to overflow, leave them pegged */
967 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
968 entry
->ie_bits
= bits
-1; /* decrement urefs */
969 ipc_entry_modified(space
, name
, entry
);
970 is_write_unlock(space
);
973 if (nsrequest
!= IP_NULL
)
974 ipc_notify_no_senders(nsrequest
, mscount
);
976 if (request
!= IP_NULL
)
977 ipc_notify_port_deleted(request
, name
);
981 case MACH_PORT_TYPE_SEND_RECEIVE
: {
982 ipc_port_t nsrequest
= IP_NULL
;
983 mach_port_mscount_t mscount
= 0;
985 assert(IE_BITS_UREFS(bits
) > 0);
987 port
= (ipc_port_t
) entry
->ie_object
;
988 assert(port
!= IP_NULL
);
991 assert(ip_active(port
));
992 assert(port
->ip_receiver_name
== name
);
993 assert(port
->ip_receiver
== space
);
994 assert(port
->ip_srights
> 0);
996 if (IE_BITS_UREFS(bits
) == 1) {
997 if (--port
->ip_srights
== 0) {
998 nsrequest
= port
->ip_nsrequest
;
999 if (nsrequest
!= IP_NULL
) {
1000 port
->ip_nsrequest
= IP_NULL
;
1001 mscount
= port
->ip_mscount
;
1005 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|
1006 MACH_PORT_TYPE_SEND
);
1008 /* if urefs are pegged due to overflow, leave them pegged */
1009 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
) {
1010 entry
->ie_bits
= bits
-1; /* decrement urefs */
1015 ipc_entry_modified(space
, name
, entry
);
1016 is_write_unlock(space
);
1018 if (nsrequest
!= IP_NULL
)
1019 ipc_notify_no_senders(nsrequest
, mscount
);
1024 is_write_unlock(space
);
1025 return KERN_INVALID_RIGHT
;
1028 return KERN_SUCCESS
;
1032 * Routine: ipc_right_delta
1034 * Modifies the user-reference count for a right.
1035 * May deallocate the right, if the count goes to zero.
1037 * The space is write-locked, and is unlocked upon return.
1038 * The space must be active.
1040 * KERN_SUCCESS Count was modified.
1041 * KERN_INVALID_RIGHT Entry has wrong type.
1042 * KERN_INVALID_VALUE Bad delta for the right.
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(port
->ip_srights
> 0);
1145 if (port
->ip_pdrequest
!= NULL
) {
1147 * Since another task has requested a
1148 * destroy notification for this port, it
1149 * isn't actually being destroyed - the receive
1150 * right is just being moved to another task.
1151 * Since we still have one or more send rights,
1152 * we need to record the loss of the receive
1153 * right and enter the remaining send right
1154 * into the hash table.
1156 ipc_entry_modified(space
, name
, entry
);
1157 entry
->ie_bits
&= ~MACH_PORT_TYPE_RECEIVE
;
1158 ipc_hash_insert(space
, (ipc_object_t
) port
,
1163 * The remaining send right turns into a
1164 * dead name. Notice we don't decrement
1165 * ip_srights, generate a no-senders notif,
1166 * or use ipc_right_dncancel, because the
1167 * port is destroyed "first".
1169 bits
&= ~IE_BITS_TYPE_MASK
;
1170 bits
|= MACH_PORT_TYPE_DEAD_NAME
;
1171 if (entry
->ie_request
) {
1172 entry
->ie_request
= IE_REQ_NONE
;
1173 /* if urefs are pegged due to overflow, leave them pegged */
1174 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
1175 bits
++; /* increment urefs */
1177 entry
->ie_bits
= bits
;
1178 entry
->ie_object
= IO_NULL
;
1179 ipc_entry_modified(space
, name
, entry
);
1182 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
1183 assert(IE_BITS_UREFS(bits
) == 0);
1185 request
= ipc_right_request_cancel_macro(space
, port
,
1187 entry
->ie_object
= IO_NULL
;
1188 ipc_entry_dealloc(space
, name
, entry
);
1190 is_write_unlock(space
);
1192 ipc_port_destroy(port
); /* clears receiver, 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) {
1266 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
1267 assert(IE_BITS_UREFS(bits
) > 0);
1268 assert(entry
->ie_object
== IO_NULL
);
1269 assert(entry
->ie_request
== IE_REQ_NONE
);
1271 if (delta
> ((mach_port_delta_t
)MACH_PORT_UREFS_MAX
) ||
1272 delta
< (-((mach_port_delta_t
)MACH_PORT_UREFS_MAX
))) {
1276 urefs
= IE_BITS_UREFS(bits
);
1278 if (urefs
== MACH_PORT_UREFS_MAX
) {
1280 * urefs are pegged due to an overflow
1281 * only a delta removing all refs at once can change it
1284 if (delta
!= (-((mach_port_delta_t
)MACH_PORT_UREFS_MAX
)))
1287 if (MACH_PORT_UREFS_UNDERFLOW(urefs
, delta
))
1289 if (MACH_PORT_UREFS_OVERFLOW(urefs
, delta
)) {
1290 /* leave urefs pegged to maximum if it overflowed */
1291 delta
= MACH_PORT_UREFS_MAX
- urefs
;
1295 if ((urefs
+ delta
) == 0) {
1296 ipc_entry_dealloc(space
, name
, entry
);
1297 } else if (delta
!= 0) {
1298 entry
->ie_bits
= bits
+ delta
;
1299 ipc_entry_modified(space
, name
, entry
);
1302 is_write_unlock(space
);
1304 if (relport
!= IP_NULL
)
1305 ip_release(relport
);
1310 case MACH_PORT_RIGHT_SEND
: {
1311 mach_port_urefs_t urefs
;
1312 ipc_port_t request
= IP_NULL
;
1313 ipc_port_t nsrequest
= IP_NULL
;
1314 ipc_port_t port_to_release
= IP_NULL
;
1315 mach_port_mscount_t mscount
= 0;
1317 if ((bits
& MACH_PORT_TYPE_SEND
) == 0)
1320 /* maximum urefs for send is MACH_PORT_UREFS_MAX */
1322 port
= (ipc_port_t
) entry
->ie_object
;
1323 assert(port
!= IP_NULL
);
1325 if (ipc_right_check(space
, port
, name
, entry
)) {
1326 assert((entry
->ie_bits
& MACH_PORT_TYPE_SEND
) == 0);
1329 /* port is locked and active */
1331 assert(port
->ip_srights
> 0);
1333 if (delta
> ((mach_port_delta_t
)MACH_PORT_UREFS_MAX
) ||
1334 delta
< (-((mach_port_delta_t
)MACH_PORT_UREFS_MAX
))) {
1339 urefs
= IE_BITS_UREFS(bits
);
1341 if (urefs
== MACH_PORT_UREFS_MAX
) {
1343 * urefs are pegged due to an overflow
1344 * only a delta removing all refs at once can change it
1347 if (delta
!= (-((mach_port_delta_t
)MACH_PORT_UREFS_MAX
)))
1350 if (MACH_PORT_UREFS_UNDERFLOW(urefs
, delta
)) {
1354 if (MACH_PORT_UREFS_OVERFLOW(urefs
, delta
)) {
1355 /* leave urefs pegged to maximum if it overflowed */
1356 delta
= MACH_PORT_UREFS_MAX
- urefs
;
1360 if ((urefs
+ delta
) == 0) {
1361 if (--port
->ip_srights
== 0) {
1362 nsrequest
= port
->ip_nsrequest
;
1363 if (nsrequest
!= IP_NULL
) {
1364 port
->ip_nsrequest
= IP_NULL
;
1365 mscount
= port
->ip_mscount
;
1369 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
1370 assert(port
->ip_receiver_name
== name
);
1371 assert(port
->ip_receiver
== space
);
1373 assert(IE_BITS_TYPE(bits
) ==
1374 MACH_PORT_TYPE_SEND_RECEIVE
);
1376 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|
1377 MACH_PORT_TYPE_SEND
);
1378 ipc_entry_modified(space
, name
, entry
);
1380 assert(IE_BITS_TYPE(bits
) ==
1381 MACH_PORT_TYPE_SEND
);
1383 request
= ipc_right_request_cancel_macro(space
, port
,
1385 ipc_hash_delete(space
, (ipc_object_t
) port
,
1389 port_to_release
= port
;
1391 entry
->ie_object
= IO_NULL
;
1392 ipc_entry_dealloc(space
, name
, entry
);
1394 } else if (delta
!= 0) {
1396 entry
->ie_bits
= bits
+ delta
;
1397 ipc_entry_modified(space
, name
, entry
);
1402 is_write_unlock(space
);
1404 if (port_to_release
!= IP_NULL
)
1405 ip_release(port_to_release
);
1407 if (nsrequest
!= IP_NULL
)
1408 ipc_notify_no_senders(nsrequest
, mscount
);
1410 if (request
!= IP_NULL
)
1411 ipc_notify_port_deleted(request
, name
);
1416 panic("ipc_right_delta: strange right");
1419 return KERN_SUCCESS
;
1422 is_write_unlock(space
);
1423 return KERN_SUCCESS
;
1426 is_write_unlock(space
);
1427 if (port
!= IP_NULL
)
1429 return KERN_INVALID_RIGHT
;
1432 is_write_unlock(space
);
1433 return KERN_INVALID_VALUE
;
1436 return KERN_INVALID_RIGHT
;
1440 * Routine: ipc_right_destruct
1442 * Deallocates the receive right and modifies the
1443 * user-reference count for the send rights as requested.
1445 * The space is write-locked, and is unlocked upon return.
1446 * The space must be active.
1448 * KERN_SUCCESS Count was modified.
1449 * KERN_INVALID_RIGHT Entry has wrong type.
1450 * KERN_INVALID_VALUE Bad delta for the right.
1456 mach_port_name_t name
,
1458 mach_port_delta_t srdelta
,
1461 ipc_port_t port
= IP_NULL
;
1462 ipc_entry_bits_t bits
;
1464 mach_port_urefs_t urefs
;
1465 ipc_port_t request
= IP_NULL
;
1466 ipc_port_t nsrequest
= IP_NULL
;
1467 mach_port_mscount_t mscount
= 0;
1469 bits
= entry
->ie_bits
;
1471 assert(is_active(space
));
1473 if (((bits
& MACH_PORT_TYPE_RECEIVE
) == 0) ||
1474 (srdelta
&& ((bits
& MACH_PORT_TYPE_SEND
) == 0))) {
1475 is_write_unlock(space
);
1476 return KERN_INVALID_RIGHT
;
1482 port
= (ipc_port_t
) entry
->ie_object
;
1483 assert(port
!= IP_NULL
);
1486 assert(ip_active(port
));
1487 assert(port
->ip_receiver_name
== name
);
1488 assert(port
->ip_receiver
== space
);
1490 /* Mach Port Guard Checking */
1491 if(port
->ip_guarded
&& (guard
!= port
->ip_context
)) {
1492 uint64_t portguard
= port
->ip_context
;
1494 is_write_unlock(space
);
1495 mach_port_guard_exception(name
, 0, portguard
, kGUARD_EXC_DESTROY
);
1496 return KERN_INVALID_ARGUMENT
;
1500 * First reduce the send rights as requested and
1501 * adjust the entry->ie_bits accordingly. The
1502 * ipc_entry_modified() call is made once the receive
1503 * right is destroyed too.
1508 assert(port
->ip_srights
> 0);
1510 urefs
= IE_BITS_UREFS(bits
);
1513 * Since we made sure that srdelta is negative,
1514 * the check for urefs overflow is not required.
1516 if (MACH_PORT_UREFS_UNDERFLOW(urefs
, srdelta
)) {
1521 if (urefs
== MACH_PORT_UREFS_MAX
) {
1523 * urefs are pegged due to an overflow
1524 * only a delta removing all refs at once can change it
1526 if (srdelta
!= (-((mach_port_delta_t
)MACH_PORT_UREFS_MAX
)))
1530 if ((urefs
+ srdelta
) == 0) {
1531 if (--port
->ip_srights
== 0) {
1532 nsrequest
= port
->ip_nsrequest
;
1533 if (nsrequest
!= IP_NULL
) {
1534 port
->ip_nsrequest
= IP_NULL
;
1535 mscount
= port
->ip_mscount
;
1538 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_RECEIVE
);
1539 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|
1540 MACH_PORT_TYPE_SEND
);
1542 entry
->ie_bits
= bits
+ srdelta
;
1547 * Now destroy the receive right. Update space and
1548 * entry accordingly.
1551 bits
= entry
->ie_bits
;
1552 if (bits
& MACH_PORT_TYPE_SEND
) {
1553 assert(IE_BITS_UREFS(bits
) > 0);
1554 assert(IE_BITS_UREFS(bits
) <= MACH_PORT_UREFS_MAX
);
1556 if (port
->ip_pdrequest
!= NULL
) {
1558 * Since another task has requested a
1559 * destroy notification for this port, it
1560 * isn't actually being destroyed - the receive
1561 * right is just being moved to another task.
1562 * Since we still have one or more send rights,
1563 * we need to record the loss of the receive
1564 * right and enter the remaining send right
1565 * into the hash table.
1567 ipc_entry_modified(space
, name
, entry
);
1568 entry
->ie_bits
&= ~MACH_PORT_TYPE_RECEIVE
;
1569 ipc_hash_insert(space
, (ipc_object_t
) port
,
1574 * The remaining send right turns into a
1575 * dead name. Notice we don't decrement
1576 * ip_srights, generate a no-senders notif,
1577 * or use ipc_right_dncancel, because the
1578 * port is destroyed "first".
1580 bits
&= ~IE_BITS_TYPE_MASK
;
1581 bits
|= MACH_PORT_TYPE_DEAD_NAME
;
1582 if (entry
->ie_request
) {
1583 entry
->ie_request
= IE_REQ_NONE
;
1584 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
1585 bits
++; /* increment urefs */
1587 entry
->ie_bits
= bits
;
1588 entry
->ie_object
= IO_NULL
;
1589 ipc_entry_modified(space
, name
, entry
);
1592 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
1593 assert(IE_BITS_UREFS(bits
) == 0);
1594 request
= ipc_right_request_cancel_macro(space
, port
,
1596 entry
->ie_object
= IO_NULL
;
1597 ipc_entry_dealloc(space
, name
, entry
);
1601 is_write_unlock(space
);
1603 if (nsrequest
!= IP_NULL
)
1604 ipc_notify_no_senders(nsrequest
, mscount
);
1606 ipc_port_destroy(port
); /* clears receiver, consumes ref, unlocks */
1608 if (request
!= IP_NULL
)
1609 ipc_notify_port_deleted(request
, name
);
1611 return KERN_SUCCESS
;
1614 is_write_unlock(space
);
1615 return KERN_INVALID_VALUE
;
1621 * Routine: ipc_right_info
1623 * Retrieves information about the right.
1625 * The space is active and write-locked.
1626 * The space is unlocked upon return.
1628 * KERN_SUCCESS Retrieved info
1634 mach_port_name_t name
,
1636 mach_port_type_t
*typep
,
1637 mach_port_urefs_t
*urefsp
)
1640 ipc_entry_bits_t bits
;
1641 mach_port_type_t type
= 0;
1642 ipc_port_request_index_t request
;
1644 bits
= entry
->ie_bits
;
1645 request
= entry
->ie_request
;
1646 port
= (ipc_port_t
) entry
->ie_object
;
1648 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
1649 assert(IP_VALID(port
));
1651 if (request
!= IE_REQ_NONE
) {
1653 assert(ip_active(port
));
1654 type
|= ipc_port_request_type(port
, name
, request
);
1657 is_write_unlock(space
);
1659 } else if (bits
& MACH_PORT_TYPE_SEND_RIGHTS
) {
1661 * validate port is still alive - if so, get request
1662 * types while we still have it locked. Otherwise,
1663 * recapture the (now dead) bits.
1665 if (!ipc_right_check(space
, port
, name
, entry
)) {
1666 if (request
!= IE_REQ_NONE
)
1667 type
|= ipc_port_request_type(port
, name
, request
);
1669 is_write_unlock(space
);
1671 bits
= entry
->ie_bits
;
1672 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
1673 is_write_unlock(space
);
1677 is_write_unlock(space
);
1680 type
|= IE_BITS_TYPE(bits
);
1683 *urefsp
= IE_BITS_UREFS(bits
);
1684 return KERN_SUCCESS
;
1688 * Routine: ipc_right_copyin_check
1690 * Check if a subsequent ipc_right_copyin would succeed.
1692 * The space is locked (read or write) and active.
1696 ipc_right_copyin_check(
1697 __assert_only ipc_space_t space
,
1698 __unused mach_port_name_t name
,
1700 mach_msg_type_name_t msgt_name
)
1702 ipc_entry_bits_t bits
;
1705 bits
= entry
->ie_bits
;
1706 assert(is_active(space
));
1708 switch (msgt_name
) {
1709 case MACH_MSG_TYPE_MAKE_SEND
:
1710 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1714 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
1715 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1719 case MACH_MSG_TYPE_MOVE_RECEIVE
:
1720 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1722 if (io_kotype(entry
->ie_object
) != IKOT_NONE
)
1726 case MACH_MSG_TYPE_COPY_SEND
:
1727 case MACH_MSG_TYPE_MOVE_SEND
:
1728 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
1730 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1733 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1736 port
= (ipc_port_t
) entry
->ie_object
;
1737 assert(port
!= IP_NULL
);
1740 * active status peek to avoid checks that will be skipped
1741 * on copyin for dead ports. Lock not held, so will not be
1742 * atomic (but once dead, there's no going back).
1744 if (!ip_active(port
)) {
1748 if (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
1749 if ((bits
& MACH_PORT_TYPE_SEND_ONCE
) == 0)
1752 if ((bits
& MACH_PORT_TYPE_SEND
) == 0)
1760 panic("ipc_right_copyin_check: strange rights");
1767 * Routine: ipc_right_copyin
1769 * Copyin a capability from a space.
1770 * If successful, the caller gets a ref
1771 * for the resulting object, unless it is IO_DEAD,
1772 * and possibly a send-once right which should
1773 * be used in a port-deleted notification.
1775 * If deadok is not TRUE, the copyin operation
1776 * will fail instead of producing IO_DEAD.
1778 * The entry is never deallocated (except
1779 * when KERN_INVALID_NAME), so the caller
1780 * should deallocate the entry if its type
1781 * is MACH_PORT_TYPE_NONE.
1783 * The space is write-locked and active.
1785 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
1786 * KERN_INVALID_RIGHT Name doesn't denote correct right.
1792 mach_port_name_t name
,
1794 mach_msg_type_name_t msgt_name
,
1796 ipc_object_t
*objectp
,
1797 ipc_port_t
*sorightp
,
1798 ipc_port_t
*releasep
,
1801 ipc_entry_bits_t bits
;
1804 *releasep
= IP_NULL
;
1807 bits
= entry
->ie_bits
;
1809 assert(is_active(space
));
1811 switch (msgt_name
) {
1812 case MACH_MSG_TYPE_MAKE_SEND
: {
1814 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1817 port
= (ipc_port_t
) entry
->ie_object
;
1818 assert(port
!= IP_NULL
);
1821 assert(ip_active(port
));
1822 assert(port
->ip_receiver_name
== name
);
1823 assert(port
->ip_receiver
== space
);
1830 *objectp
= (ipc_object_t
) port
;
1831 *sorightp
= IP_NULL
;
1835 case MACH_MSG_TYPE_MAKE_SEND_ONCE
: {
1837 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1840 port
= (ipc_port_t
) entry
->ie_object
;
1841 assert(port
!= IP_NULL
);
1844 assert(ip_active(port
));
1845 assert(port
->ip_receiver_name
== name
);
1846 assert(port
->ip_receiver
== space
);
1848 port
->ip_sorights
++;
1852 *objectp
= (ipc_object_t
) port
;
1853 *sorightp
= IP_NULL
;
1857 case MACH_MSG_TYPE_MOVE_RECEIVE
: {
1858 ipc_port_t request
= IP_NULL
;
1860 if ((bits
& MACH_PORT_TYPE_RECEIVE
) == 0)
1864 * Disallow moving receive-right kobjects, e.g. mk_timer ports
1865 * The ipc_port structure uses the kdata union of kobject and
1866 * imp_task exclusively. Thus, general use of a kobject port as
1867 * a receive right can cause type confusion in the importance
1870 if (io_kotype(entry
->ie_object
) != IKOT_NONE
) {
1872 * Distinguish an invalid right, e.g., trying to move
1873 * a send right as a receive right, from this
1874 * situation which is, "This is a valid receive right,
1875 * but it's also a kobject and you can't move it."
1877 return KERN_INVALID_CAPABILITY
;
1880 port
= (ipc_port_t
) entry
->ie_object
;
1881 assert(port
!= IP_NULL
);
1884 assert(ip_active(port
));
1885 assert(port
->ip_receiver_name
== name
);
1886 assert(port
->ip_receiver
== space
);
1888 if (bits
& MACH_PORT_TYPE_SEND
) {
1889 assert(IE_BITS_TYPE(bits
) ==
1890 MACH_PORT_TYPE_SEND_RECEIVE
);
1891 assert(IE_BITS_UREFS(bits
) > 0);
1892 assert(port
->ip_srights
> 0);
1894 ipc_hash_insert(space
, (ipc_object_t
) port
,
1898 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
1899 assert(IE_BITS_UREFS(bits
) == 0);
1901 request
= ipc_right_request_cancel_macro(space
, port
,
1903 entry
->ie_object
= IO_NULL
;
1905 entry
->ie_bits
= bits
&~ MACH_PORT_TYPE_RECEIVE
;
1906 ipc_entry_modified(space
, name
, entry
);
1908 (void)ipc_port_clear_receiver(port
, FALSE
); /* don't destroy the port/mqueue */
1909 port
->ip_receiver_name
= MACH_PORT_NULL
;
1910 port
->ip_destination
= IP_NULL
;
1912 #if IMPORTANCE_INHERITANCE
1914 * Account for boosts the current task is going to lose when
1915 * copying this right in. Tempowner ports have either not
1916 * been accounting to any task (and therefore are already in
1917 * "limbo" state w.r.t. assertions) or to some other specific
1918 * task. As we have no way to drop the latter task's assertions
1919 * here, We'll deduct those when we enqueue it on its
1920 * destination port (see ipc_port_check_circularity()).
1922 if (port
->ip_tempowner
== 0) {
1923 assert(IIT_NULL
== port
->ip_imp_task
);
1925 /* ports in limbo have to be tempowner */
1926 port
->ip_tempowner
= 1;
1927 *assertcntp
= port
->ip_impcount
;
1929 #endif /* IMPORTANCE_INHERITANCE */
1933 *objectp
= (ipc_object_t
) port
;
1934 *sorightp
= request
;
1938 case MACH_MSG_TYPE_COPY_SEND
: {
1940 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1943 /* allow for dead send-once rights */
1945 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1948 assert(IE_BITS_UREFS(bits
) > 0);
1950 port
= (ipc_port_t
) entry
->ie_object
;
1951 assert(port
!= IP_NULL
);
1953 if (ipc_right_check(space
, port
, name
, entry
)) {
1954 bits
= entry
->ie_bits
;
1958 /* port is locked and active */
1960 if ((bits
& MACH_PORT_TYPE_SEND
) == 0) {
1961 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
1962 assert(port
->ip_sorights
> 0);
1968 assert(port
->ip_srights
> 0);
1974 *objectp
= (ipc_object_t
) port
;
1975 *sorightp
= IP_NULL
;
1979 case MACH_MSG_TYPE_MOVE_SEND
: {
1980 ipc_port_t request
= IP_NULL
;
1982 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
1985 /* allow for dead send-once rights */
1987 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
1990 assert(IE_BITS_UREFS(bits
) > 0);
1992 port
= (ipc_port_t
) entry
->ie_object
;
1993 assert(port
!= IP_NULL
);
1995 if (ipc_right_check(space
, port
, name
, entry
)) {
1996 bits
= entry
->ie_bits
;
2000 /* port is locked and active */
2002 if ((bits
& MACH_PORT_TYPE_SEND
) == 0) {
2003 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
2004 assert(port
->ip_sorights
> 0);
2010 assert(port
->ip_srights
> 0);
2012 if (IE_BITS_UREFS(bits
) == 1) {
2013 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
2014 assert(port
->ip_receiver_name
== name
);
2015 assert(port
->ip_receiver
== space
);
2016 assert(IE_BITS_TYPE(bits
) ==
2017 MACH_PORT_TYPE_SEND_RECEIVE
);
2021 assert(IE_BITS_TYPE(bits
) ==
2022 MACH_PORT_TYPE_SEND
);
2024 request
= ipc_right_request_cancel_macro(space
, port
,
2026 ipc_hash_delete(space
, (ipc_object_t
) port
,
2028 entry
->ie_object
= IO_NULL
;
2029 /* transfer entry's reference to caller */
2031 entry
->ie_bits
= bits
&~
2032 (IE_BITS_UREFS_MASK
|MACH_PORT_TYPE_SEND
);
2036 /* if urefs are pegged due to overflow, leave them pegged */
2037 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
2038 entry
->ie_bits
= bits
-1; /* decrement urefs */
2041 ipc_entry_modified(space
, name
, entry
);
2044 *objectp
= (ipc_object_t
) port
;
2045 *sorightp
= request
;
2049 case MACH_MSG_TYPE_MOVE_SEND_ONCE
: {
2052 if (bits
& MACH_PORT_TYPE_DEAD_NAME
)
2055 /* allow for dead send rights */
2057 if ((bits
& MACH_PORT_TYPE_SEND_RIGHTS
) == 0)
2060 assert(IE_BITS_UREFS(bits
) > 0);
2062 port
= (ipc_port_t
) entry
->ie_object
;
2063 assert(port
!= IP_NULL
);
2065 if (ipc_right_check(space
, port
, name
, entry
)) {
2066 bits
= entry
->ie_bits
;
2070 /* port is locked and active */
2072 if ((bits
& MACH_PORT_TYPE_SEND_ONCE
) == 0) {
2073 assert(bits
& MACH_PORT_TYPE_SEND
);
2074 assert(port
->ip_srights
> 0);
2080 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND_ONCE
);
2081 assert(IE_BITS_UREFS(bits
) == 1);
2082 assert(port
->ip_sorights
> 0);
2084 request
= ipc_right_request_cancel_macro(space
, port
, name
, entry
);
2087 entry
->ie_object
= IO_NULL
;
2088 entry
->ie_bits
= bits
&~
2089 (IE_BITS_UREFS_MASK
| MACH_PORT_TYPE_SEND_ONCE
);
2090 ipc_entry_modified(space
, name
, entry
);
2091 *objectp
= (ipc_object_t
) port
;
2092 *sorightp
= request
;
2098 return KERN_INVALID_RIGHT
;
2101 return KERN_SUCCESS
;
2104 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
2105 assert(IE_BITS_UREFS(bits
) > 0);
2106 assert(entry
->ie_request
== IE_REQ_NONE
);
2107 assert(entry
->ie_object
== 0);
2113 *sorightp
= IP_NULL
;
2114 return KERN_SUCCESS
;
2117 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
2118 assert(IE_BITS_UREFS(bits
) > 0);
2119 assert(entry
->ie_request
== IE_REQ_NONE
);
2120 assert(entry
->ie_object
== 0);
2125 if (IE_BITS_UREFS(bits
) == 1) {
2126 bits
&= ~MACH_PORT_TYPE_DEAD_NAME
;
2128 /* if urefs are pegged due to overflow, leave them pegged */
2129 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
2130 entry
->ie_bits
= bits
-1; /* decrement urefs */
2132 ipc_entry_modified(space
, name
, entry
);
2134 *sorightp
= IP_NULL
;
2135 return KERN_SUCCESS
;
2140 * Routine: ipc_right_copyin_undo
2142 * Undoes the effects of an ipc_right_copyin
2143 * of a send/send-once right that is dead.
2144 * (Object is either IO_DEAD or a dead port.)
2146 * The space is write-locked and active.
2150 ipc_right_copyin_undo(
2152 mach_port_name_t name
,
2154 mach_msg_type_name_t msgt_name
,
2155 ipc_object_t object
,
2158 ipc_entry_bits_t bits
;
2160 bits
= entry
->ie_bits
;
2162 assert(is_active(space
));
2164 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2165 (msgt_name
== MACH_MSG_TYPE_COPY_SEND
) ||
2166 (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
));
2168 if (soright
!= IP_NULL
) {
2169 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2170 (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
));
2171 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2172 assert(object
!= IO_DEAD
);
2174 entry
->ie_bits
= ((bits
&~ IE_BITS_RIGHT_MASK
) |
2175 MACH_PORT_TYPE_DEAD_NAME
| 2);
2177 } else if (IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
) {
2178 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2179 (msgt_name
== MACH_MSG_TYPE_MOVE_SEND_ONCE
));
2181 entry
->ie_bits
= ((bits
&~ IE_BITS_RIGHT_MASK
) |
2182 MACH_PORT_TYPE_DEAD_NAME
| 1);
2183 } else if (IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
) {
2184 assert(object
== IO_DEAD
);
2185 assert(IE_BITS_UREFS(bits
) > 0);
2187 if (msgt_name
!= MACH_MSG_TYPE_COPY_SEND
) {
2188 assert(IE_BITS_UREFS(bits
) <= MACH_PORT_UREFS_MAX
);
2189 /* if urefs are pegged due to overflow, leave them pegged */
2190 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
2191 entry
->ie_bits
= bits
+1; /* increment urefs */
2194 assert((msgt_name
== MACH_MSG_TYPE_MOVE_SEND
) ||
2195 (msgt_name
== MACH_MSG_TYPE_COPY_SEND
));
2196 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
2197 assert(object
!= IO_DEAD
);
2198 assert(entry
->ie_object
== object
);
2199 assert(IE_BITS_UREFS(bits
) > 0);
2201 if (msgt_name
!= MACH_MSG_TYPE_COPY_SEND
) {
2202 assert(IE_BITS_UREFS(bits
) <= MACH_PORT_UREFS_MAX
);
2203 /* if urefs are pegged due to overflow, leave them pegged */
2204 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
2205 entry
->ie_bits
= bits
+1; /* increment urefs */
2209 * May as well convert the entry to a dead name.
2210 * (Or if it is a compat entry, destroy it.)
2213 (void) ipc_right_check(space
, (ipc_port_t
) object
,
2215 /* object is dead so it is not locked */
2217 ipc_entry_modified(space
, name
, entry
);
2218 /* release the reference acquired by copyin */
2220 if (object
!= IO_DEAD
)
2225 * Routine: ipc_right_copyin_two_move_sends
2227 * Like ipc_right_copyin with MACH_MSG_TYPE_MOVE_SEND
2228 * and deadok == FALSE, except that this moves two
2229 * send rights at once.
2231 * The space is write-locked and active.
2232 * The object is returned with two refs/send rights.
2234 * KERN_SUCCESS Acquired an object.
2235 * KERN_INVALID_RIGHT Name doesn't denote correct right.
2239 ipc_right_copyin_two_move_sends(
2241 mach_port_name_t name
,
2243 ipc_object_t
*objectp
,
2244 ipc_port_t
*sorightp
,
2245 ipc_port_t
*releasep
)
2247 ipc_entry_bits_t bits
;
2248 mach_port_urefs_t urefs
;
2250 ipc_port_t request
= IP_NULL
;
2252 *releasep
= IP_NULL
;
2254 assert(is_active(space
));
2256 bits
= entry
->ie_bits
;
2258 if ((bits
& MACH_PORT_TYPE_SEND
) == 0)
2261 urefs
= IE_BITS_UREFS(bits
);
2265 port
= (ipc_port_t
) entry
->ie_object
;
2266 assert(port
!= IP_NULL
);
2268 if (ipc_right_check(space
, port
, name
, entry
)) {
2272 /* port is locked and active */
2274 assert(port
->ip_srights
> 0);
2277 if (bits
& MACH_PORT_TYPE_RECEIVE
) {
2278 assert(port
->ip_receiver_name
== name
);
2279 assert(port
->ip_receiver
== space
);
2280 assert(IE_BITS_TYPE(bits
) ==
2281 MACH_PORT_TYPE_SEND_RECEIVE
);
2287 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
2289 request
= ipc_right_request_cancel_macro(space
, port
,
2294 ipc_hash_delete(space
, (ipc_object_t
) port
,
2296 entry
->ie_object
= IO_NULL
;
2298 entry
->ie_bits
= bits
&~ (IE_BITS_UREFS_MASK
|MACH_PORT_TYPE_SEND
);
2300 port
->ip_srights
+= 2;
2303 /* if urefs are pegged due to overflow, leave them pegged */
2304 if (IE_BITS_UREFS(bits
) < MACH_PORT_UREFS_MAX
)
2305 entry
->ie_bits
= bits
-2; /* decrement urefs */
2307 ipc_entry_modified(space
, name
, entry
);
2311 *objectp
= (ipc_object_t
) port
;
2312 *sorightp
= request
;
2313 return KERN_SUCCESS
;
2316 return KERN_INVALID_RIGHT
;
2321 * Routine: ipc_right_copyin_two
2323 * Like ipc_right_copyin with two dispositions,
2324 * each of which results in a send or send-once right,
2325 * and deadok = FALSE.
2327 * The space is write-locked and active.
2328 * The object is returned with two refs/rights.
2330 * KERN_SUCCESS Acquired an object.
2331 * KERN_INVALID_RIGHT Name doesn't denote correct right(s).
2332 * KERN_INVALID_CAPABILITY Name doesn't denote correct right for msgt_two.
2335 ipc_right_copyin_two(
2337 mach_port_name_t name
,
2339 mach_msg_type_name_t msgt_one
,
2340 mach_msg_type_name_t msgt_two
,
2341 ipc_object_t
*objectp
,
2342 ipc_port_t
*sorightp
,
2343 ipc_port_t
*releasep
)
2348 assert(MACH_MSG_TYPE_PORT_ANY_SEND(msgt_one
));
2349 assert(MACH_MSG_TYPE_PORT_ANY_SEND(msgt_two
));
2353 * Pre-validate the second disposition is possible all by itself.
2355 if (!ipc_right_copyin_check(space
, name
, entry
, msgt_two
)) {
2356 return KERN_INVALID_CAPABILITY
;
2360 * This is a little tedious to make atomic, because
2361 * there are 25 combinations of valid dispositions.
2362 * However, most are easy.
2366 * If either is move-sonce, then there must be an error.
2368 if (msgt_one
== MACH_MSG_TYPE_MOVE_SEND_ONCE
||
2369 msgt_two
== MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
2370 return KERN_INVALID_RIGHT
;
2373 if ((msgt_one
== MACH_MSG_TYPE_MAKE_SEND
) ||
2374 (msgt_one
== MACH_MSG_TYPE_MAKE_SEND_ONCE
) ||
2375 (msgt_two
== MACH_MSG_TYPE_MAKE_SEND
) ||
2376 (msgt_two
== MACH_MSG_TYPE_MAKE_SEND_ONCE
)) {
2378 * One of the dispositions needs a receive right.
2380 * If the copyin below succeeds, we know the receive
2381 * right is there (because the pre-validation of
2382 * the second disposition already succeeded in our
2385 * Hence the port is not in danger of dying.
2387 ipc_object_t object_two
;
2389 kr
= ipc_right_copyin(space
, name
, entry
,
2391 objectp
, sorightp
, releasep
,
2393 assert(assertcnt
== 0);
2394 if (kr
!= KERN_SUCCESS
) {
2398 assert(IO_VALID(*objectp
));
2399 assert(*sorightp
== IP_NULL
);
2400 assert(*releasep
== IP_NULL
);
2403 * Now copyin the second (previously validated)
2404 * disposition. The result can't be a dead port,
2405 * as no valid disposition can make us lose our
2408 kr
= ipc_right_copyin(space
, name
, entry
,
2410 &object_two
, sorightp
, releasep
,
2412 assert(assertcnt
== 0);
2413 assert(kr
== KERN_SUCCESS
);
2414 assert(*sorightp
== IP_NULL
);
2415 assert(*releasep
== IP_NULL
);
2416 assert(object_two
== *objectp
);
2417 assert(entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
);
2419 } else if ((msgt_one
== MACH_MSG_TYPE_MOVE_SEND
) &&
2420 (msgt_two
== MACH_MSG_TYPE_MOVE_SEND
)) {
2422 * This is an easy case. Just use our
2423 * handy-dandy special-purpose copyin call
2424 * to get two send rights for the price of one.
2426 kr
= ipc_right_copyin_two_move_sends(space
, name
, entry
,
2429 if (kr
!= KERN_SUCCESS
) {
2434 mach_msg_type_name_t msgt_name
;
2437 * Must be either a single move-send and a
2438 * copy-send, or two copy-send dispositions.
2439 * Use the disposition with the greatest side
2440 * effects for the actual copyin - then just
2441 * duplicate the send right you get back.
2443 if (msgt_one
== MACH_MSG_TYPE_MOVE_SEND
||
2444 msgt_two
== MACH_MSG_TYPE_MOVE_SEND
) {
2445 msgt_name
= MACH_MSG_TYPE_MOVE_SEND
;
2447 msgt_name
= MACH_MSG_TYPE_COPY_SEND
;
2450 kr
= ipc_right_copyin(space
, name
, entry
,
2452 objectp
, sorightp
, releasep
,
2454 assert(assertcnt
== 0);
2455 if (kr
!= KERN_SUCCESS
) {
2460 * Copy the right we got back. If it is dead now,
2461 * that's OK. Neither right will be usable to send
2464 (void)ipc_port_copy_send((ipc_port_t
)*objectp
);
2467 return KERN_SUCCESS
;
2472 * Routine: ipc_right_copyout
2474 * Copyout a capability to a space.
2475 * If successful, consumes a ref for the object.
2477 * Always succeeds when given a newly-allocated entry,
2478 * because user-reference overflow isn't a possibility.
2480 * If copying out the object would cause the user-reference
2481 * count in the entry to overflow, and overflow is TRUE,
2482 * then instead the user-reference count is left pegged
2483 * to its maximum value and the copyout succeeds anyway.
2485 * The space is write-locked and active.
2486 * The object is locked and active.
2487 * The object is unlocked; the space isn't.
2489 * KERN_SUCCESS Copied out capability.
2495 mach_port_name_t name
,
2497 mach_msg_type_name_t msgt_name
,
2498 __unused boolean_t overflow
,
2499 ipc_object_t object
)
2501 ipc_entry_bits_t bits
;
2504 bits
= entry
->ie_bits
;
2506 assert(IO_VALID(object
));
2507 assert(io_otype(object
) == IOT_PORT
);
2508 assert(io_active(object
));
2509 assert(entry
->ie_object
== object
);
2511 port
= (ipc_port_t
) object
;
2513 switch (msgt_name
) {
2514 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
2516 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2517 assert(IE_BITS_UREFS(bits
) == 0);
2518 assert(port
->ip_sorights
> 0);
2520 /* transfer send-once right and ref to entry */
2523 entry
->ie_bits
= bits
| (MACH_PORT_TYPE_SEND_ONCE
| 1); /* set urefs to 1 */
2524 ipc_entry_modified(space
, name
, entry
);
2527 case MACH_MSG_TYPE_PORT_SEND
:
2528 assert(port
->ip_srights
> 0);
2530 if (bits
& MACH_PORT_TYPE_SEND
) {
2531 mach_port_urefs_t urefs
= IE_BITS_UREFS(bits
);
2533 assert(port
->ip_srights
> 1);
2535 assert(urefs
<= MACH_PORT_UREFS_MAX
);
2537 if (urefs
== MACH_PORT_UREFS_MAX
) {
2539 * leave urefs pegged to maximum,
2540 * consume send right and ref
2546 return KERN_SUCCESS
;
2549 /* consume send right and ref */
2554 } else if (bits
& MACH_PORT_TYPE_RECEIVE
) {
2555 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_RECEIVE
);
2556 assert(IE_BITS_UREFS(bits
) == 0);
2558 /* transfer send right to entry, consume ref */
2563 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2564 assert(IE_BITS_UREFS(bits
) == 0);
2566 /* transfer send right and ref to entry */
2569 /* entry is locked holding ref, so can use port */
2571 ipc_hash_insert(space
, (ipc_object_t
) port
,
2575 entry
->ie_bits
= (bits
| MACH_PORT_TYPE_SEND
) + 1; /* increment urefs */
2576 ipc_entry_modified(space
, name
, entry
);
2579 case MACH_MSG_TYPE_PORT_RECEIVE
: {
2582 #if IMPORTANCE_INHERITANCE
2583 natural_t assertcnt
= port
->ip_impcount
;
2584 #endif /* IMPORTANCE_INHERITANCE */
2586 assert(port
->ip_mscount
== 0);
2587 assert(port
->ip_receiver_name
== MACH_PORT_NULL
);
2588 dest
= port
->ip_destination
;
2590 port
->ip_receiver_name
= name
;
2591 port
->ip_receiver
= space
;
2593 assert((bits
& MACH_PORT_TYPE_RECEIVE
) == 0);
2595 if (bits
& MACH_PORT_TYPE_SEND
) {
2596 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_SEND
);
2597 assert(IE_BITS_UREFS(bits
) > 0);
2598 assert(port
->ip_srights
> 0);
2603 /* entry is locked holding ref, so can use port */
2605 ipc_hash_delete(space
, (ipc_object_t
) port
,
2608 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_NONE
);
2609 assert(IE_BITS_UREFS(bits
) == 0);
2611 /* transfer ref to entry */
2614 entry
->ie_bits
= bits
| MACH_PORT_TYPE_RECEIVE
;
2615 ipc_entry_modified(space
, name
, entry
);
2617 if (dest
!= IP_NULL
) {
2618 #if IMPORTANCE_INHERITANCE
2620 * Deduct the assertion counts we contributed to
2621 * the old destination port. They've already
2622 * been reflected into the task as a result of
2626 ipc_port_impcount_delta(dest
, 0 - assertcnt
, IP_NULL
);
2628 #endif /* IMPORTANCE_INHERITANCE */
2635 panic("ipc_right_copyout: strange rights");
2637 return KERN_SUCCESS
;
2641 * Routine: ipc_right_rename
2643 * Transfer an entry from one name to another.
2644 * The old entry is deallocated.
2646 * The space is write-locked and active.
2647 * The new entry is unused. Upon return,
2648 * the space is unlocked.
2650 * KERN_SUCCESS Moved entry to new name.
2656 mach_port_name_t oname
,
2658 mach_port_name_t nname
,
2661 ipc_port_request_index_t request
= oentry
->ie_request
;
2662 ipc_entry_bits_t bits
= oentry
->ie_bits
;
2663 ipc_object_t object
= oentry
->ie_object
;
2664 ipc_port_t release_port
= IP_NULL
;
2666 assert(is_active(space
));
2667 assert(oname
!= nname
);
2670 * If IE_BITS_COMPAT, we can't allow the entry to be renamed
2671 * if the port is dead. (This would foil ipc_port_destroy.)
2672 * Instead we should fail because oentry shouldn't exist.
2673 * Note IE_BITS_COMPAT implies ie_request != 0.
2676 if (request
!= IE_REQ_NONE
) {
2679 assert(bits
& MACH_PORT_TYPE_PORT_RIGHTS
);
2680 port
= (ipc_port_t
) object
;
2681 assert(port
!= IP_NULL
);
2683 if (ipc_right_check(space
, port
, oname
, oentry
)) {
2684 request
= IE_REQ_NONE
;
2686 bits
= oentry
->ie_bits
;
2687 release_port
= port
;
2688 assert(IE_BITS_TYPE(bits
) == MACH_PORT_TYPE_DEAD_NAME
);
2689 assert(oentry
->ie_request
== IE_REQ_NONE
);
2691 /* port is locked and active */
2693 ipc_port_request_rename(port
, request
, oname
, nname
);
2695 oentry
->ie_request
= IE_REQ_NONE
;
2699 /* initialize nentry before letting ipc_hash_insert see it */
2701 assert((nentry
->ie_bits
& IE_BITS_RIGHT_MASK
) == 0);
2702 nentry
->ie_bits
|= bits
& IE_BITS_RIGHT_MASK
;
2703 nentry
->ie_request
= request
;
2704 nentry
->ie_object
= object
;
2706 switch (IE_BITS_TYPE(bits
)) {
2707 case MACH_PORT_TYPE_SEND
: {
2710 port
= (ipc_port_t
) object
;
2711 assert(port
!= IP_NULL
);
2713 /* remember, there are no other share entries possible */
2714 /* or we can't do the rename. Therefore we do not need */
2715 /* to check the other subspaces */
2716 ipc_hash_delete(space
, (ipc_object_t
) port
, oname
, oentry
);
2717 ipc_hash_insert(space
, (ipc_object_t
) port
, nname
, nentry
);
2721 case MACH_PORT_TYPE_RECEIVE
:
2722 case MACH_PORT_TYPE_SEND_RECEIVE
: {
2725 port
= (ipc_port_t
) object
;
2726 assert(port
!= IP_NULL
);
2729 assert(ip_active(port
));
2730 assert(port
->ip_receiver_name
== oname
);
2731 assert(port
->ip_receiver
== space
);
2733 port
->ip_receiver_name
= nname
;
2738 case MACH_PORT_TYPE_PORT_SET
: {
2741 pset
= (ipc_pset_t
) object
;
2742 assert(pset
!= IPS_NULL
);
2745 assert(ips_active(pset
));
2751 case MACH_PORT_TYPE_SEND_ONCE
:
2752 case MACH_PORT_TYPE_DEAD_NAME
:
2756 panic("ipc_right_rename: strange rights");
2759 assert(oentry
->ie_request
== IE_REQ_NONE
);
2760 oentry
->ie_object
= IO_NULL
;
2761 ipc_entry_dealloc(space
, oname
, oentry
);
2762 ipc_entry_modified(space
, nname
, nentry
);
2763 is_write_unlock(space
);
2765 if (release_port
!= IP_NULL
)
2766 ip_release(release_port
);
2768 return KERN_SUCCESS
;