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.
59 * File: ipc/ipc_mqueue.c
63 * Functions to manipulate IPC message queues.
66 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
67 * support for mandatory and extensible security protections. This notice
68 * is included in support of clause 2.2 (b) of the Apple Public License,
73 #include <mach/port.h>
74 #include <mach/message.h>
75 #include <mach/sync_policy.h>
77 #include <kern/assert.h>
78 #include <kern/counters.h>
79 #include <kern/sched_prim.h>
80 #include <kern/ipc_kobject.h>
81 #include <kern/ipc_mig.h> /* XXX - for mach_msg_receive_continue */
82 #include <kern/misc_protos.h>
83 #include <kern/task.h>
84 #include <kern/thread.h>
85 #include <kern/wait_queue.h>
87 #include <ipc/ipc_mqueue.h>
88 #include <ipc/ipc_kmsg.h>
89 #include <ipc/ipc_port.h>
90 #include <ipc/ipc_pset.h>
91 #include <ipc/ipc_space.h>
94 #include <vm/vm_map.h>
97 int ipc_mqueue_full
; /* address is event for queue space */
98 int ipc_mqueue_rcv
; /* address is event for message arrival */
100 /* forward declarations */
101 void ipc_mqueue_receive_results(wait_result_t result
);
104 * Routine: ipc_mqueue_init
106 * Initialize a newly-allocated message queue.
114 wait_queue_set_init(&mqueue
->imq_set_queue
, SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
);
116 wait_queue_init(&mqueue
->imq_wait_queue
, SYNC_POLICY_FIFO
);
117 ipc_kmsg_queue_init(&mqueue
->imq_messages
);
118 mqueue
->imq_seqno
= 0;
119 mqueue
->imq_msgcount
= 0;
120 mqueue
->imq_qlimit
= MACH_PORT_QLIMIT_DEFAULT
;
121 mqueue
->imq_fullwaiters
= FALSE
;
126 * Routine: ipc_mqueue_member
128 * Indicate whether the (port) mqueue is a member of
129 * this portset's mqueue. We do this by checking
130 * whether the portset mqueue's waitq is an member of
131 * the port's mqueue waitq.
133 * the portset's mqueue is not already a member
134 * this may block while allocating linkage structures.
139 ipc_mqueue_t port_mqueue
,
140 ipc_mqueue_t set_mqueue
)
142 wait_queue_t port_waitq
= &port_mqueue
->imq_wait_queue
;
143 wait_queue_set_t set_waitq
= &set_mqueue
->imq_set_queue
;
145 return (wait_queue_member(port_waitq
, set_waitq
));
150 * Routine: ipc_mqueue_remove
152 * Remove the association between the queue and the specified
159 ipc_mqueue_t set_mqueue
,
160 wait_queue_link_t
*wqlp
)
162 wait_queue_t mq_waitq
= &mqueue
->imq_wait_queue
;
163 wait_queue_set_t set_waitq
= &set_mqueue
->imq_set_queue
;
165 return wait_queue_unlink_nofree(mq_waitq
, set_waitq
, wqlp
);
169 * Routine: ipc_mqueue_remove_from_all
171 * Remove the mqueue from all the sets it is a member of
176 ipc_mqueue_remove_from_all(
180 wait_queue_t mq_waitq
= &mqueue
->imq_wait_queue
;
182 wait_queue_unlink_all_nofree(mq_waitq
, links
);
187 * Routine: ipc_mqueue_remove_all
189 * Remove all the member queues from the specified set.
194 ipc_mqueue_remove_all(
198 wait_queue_set_t mq_setq
= &mqueue
->imq_set_queue
;
200 wait_queue_set_unlink_all_nofree(mq_setq
, links
);
206 * Routine: ipc_mqueue_add
208 * Associate the portset's mqueue with the port's mqueue.
209 * This has to be done so that posting the port will wakeup
210 * a portset waiter. If there are waiters on the portset
211 * mqueue and messages on the port mqueue, try to match them
218 ipc_mqueue_t port_mqueue
,
219 ipc_mqueue_t set_mqueue
,
220 wait_queue_link_t wql
)
222 wait_queue_t port_waitq
= &port_mqueue
->imq_wait_queue
;
223 wait_queue_set_t set_waitq
= &set_mqueue
->imq_set_queue
;
224 ipc_kmsg_queue_t kmsgq
;
225 ipc_kmsg_t kmsg
, next
;
229 kr
= wait_queue_link_noalloc(port_waitq
, set_waitq
, wql
);
230 if (kr
!= KERN_SUCCESS
)
234 * Now that the set has been added to the port, there may be
235 * messages queued on the port and threads waiting on the set
236 * waitq. Lets get them together.
239 imq_lock(port_mqueue
);
240 kmsgq
= &port_mqueue
->imq_messages
;
241 for (kmsg
= ipc_kmsg_queue_first(kmsgq
);
244 next
= ipc_kmsg_queue_next(kmsgq
, kmsg
);
248 mach_msg_size_t msize
;
250 th
= wait_queue_wakeup64_identity_locked(
255 /* waitq/mqueue still locked, thread locked */
257 if (th
== THREAD_NULL
)
261 * If the receiver waited with a facility not directly
262 * related to Mach messaging, then it isn't prepared to get
263 * handed the message directly. Just set it running, and
264 * go look for another thread that can.
266 if (th
->ith_state
!= MACH_RCV_IN_PROGRESS
) {
272 * Found a receiver. see if they can handle the message
273 * correctly (the message is not too large for them, or
274 * they didn't care to be informed that the message was
275 * too large). If they can't handle it, take them off
276 * the list and let them go back and figure it out and
277 * just move onto the next.
279 msize
= ipc_kmsg_copyout_size(kmsg
, th
->map
);
281 (msize
+ REQUESTED_TRAILER_SIZE(thread_is_64bit(th
), th
->ith_option
))) {
282 th
->ith_state
= MACH_RCV_TOO_LARGE
;
283 th
->ith_msize
= msize
;
284 if (th
->ith_option
& MACH_RCV_LARGE
) {
286 * let him go without message
288 th
->ith_receiver_name
= port_mqueue
->imq_receiver_name
;
289 th
->ith_kmsg
= IKM_NULL
;
292 continue; /* find another thread */
295 th
->ith_state
= MACH_MSG_SUCCESS
;
299 * This thread is going to take this message,
302 ipc_kmsg_rmqueue(kmsgq
, kmsg
);
303 ipc_mqueue_release_msgcount(port_mqueue
);
306 th
->ith_seqno
= port_mqueue
->imq_seqno
++;
308 break; /* go to next message */
313 imq_unlock(port_mqueue
);
319 * Routine: ipc_mqueue_changed
321 * Wake up receivers waiting in a message queue.
323 * The message queue is locked.
330 wait_queue_wakeup64_all_locked(
331 &mqueue
->imq_wait_queue
,
334 FALSE
); /* unlock waitq? */
341 * Routine: ipc_mqueue_send
343 * Send a message to a message queue. The message holds a reference
344 * for the destination port for this message queue in the
345 * msgh_remote_port field.
347 * If unsuccessful, the caller still has possession of
348 * the message and must do something with it. If successful,
349 * the message is queued, given to a receiver, or destroyed.
353 * MACH_MSG_SUCCESS The message was accepted.
354 * MACH_SEND_TIMED_OUT Caller still has message.
355 * MACH_SEND_INTERRUPTED Caller still has message.
361 mach_msg_option_t option
,
362 mach_msg_timeout_t send_timeout
,
369 * 1) We're under the queue limit.
370 * 2) Caller used the MACH_SEND_ALWAYS internal option.
371 * 3) Message is sent to a send-once right.
373 if (!imq_full(mqueue
) ||
374 (!imq_full_kernel(mqueue
) &&
375 ((option
& MACH_SEND_ALWAYS
) ||
376 (MACH_MSGH_BITS_REMOTE(kmsg
->ikm_header
->msgh_bits
) ==
377 MACH_MSG_TYPE_PORT_SEND_ONCE
)))) {
378 mqueue
->imq_msgcount
++;
379 assert(mqueue
->imq_msgcount
> 0);
383 thread_t cur_thread
= current_thread();
387 * We have to wait for space to be granted to us.
389 if ((option
& MACH_SEND_TIMEOUT
) && (send_timeout
== 0)) {
392 return MACH_SEND_TIMED_OUT
;
394 if (imq_full_kernel(mqueue
)) {
397 return MACH_SEND_NO_BUFFER
;
399 mqueue
->imq_fullwaiters
= TRUE
;
400 thread_lock(cur_thread
);
401 if (option
& MACH_SEND_TIMEOUT
)
402 clock_interval_to_deadline(send_timeout
, 1000*NSEC_PER_USEC
, &deadline
);
405 wresult
= wait_queue_assert_wait64_locked(
406 &mqueue
->imq_wait_queue
,
409 TIMEOUT_URGENCY_USER_NORMAL
,
412 thread_unlock(cur_thread
);
416 if (wresult
== THREAD_WAITING
) {
417 wresult
= thread_block(THREAD_CONTINUE_NULL
);
418 counter(c_ipc_mqueue_send_block
++);
422 case THREAD_TIMED_OUT
:
423 assert(option
& MACH_SEND_TIMEOUT
);
424 return MACH_SEND_TIMED_OUT
;
426 case THREAD_AWAKENED
:
427 /* we can proceed - inherited msgcount from waker */
428 assert(mqueue
->imq_msgcount
> 0);
431 case THREAD_INTERRUPTED
:
432 return MACH_SEND_INTERRUPTED
;
435 /* mqueue is being destroyed */
436 return MACH_SEND_INVALID_DEST
;
438 panic("ipc_mqueue_send");
442 ipc_mqueue_post(mqueue
, kmsg
);
443 return MACH_MSG_SUCCESS
;
448 * Routine: ipc_mqueue_release_msgcount
450 * Release a message queue reference in the case where we
454 * The message queue is locked.
455 * The message corresponding to this reference is off the queue.
458 ipc_mqueue_release_msgcount(
461 assert(imq_held(mqueue
));
462 assert(mqueue
->imq_msgcount
> 1 || ipc_kmsg_queue_empty(&mqueue
->imq_messages
));
464 mqueue
->imq_msgcount
--;
466 if (!imq_full(mqueue
) && mqueue
->imq_fullwaiters
) {
467 if (wait_queue_wakeup64_one_locked(
468 &mqueue
->imq_wait_queue
,
471 FALSE
) != KERN_SUCCESS
) {
472 mqueue
->imq_fullwaiters
= FALSE
;
474 /* gave away our slot - add reference back */
475 mqueue
->imq_msgcount
++;
481 * Routine: ipc_mqueue_post
483 * Post a message to a waiting receiver or enqueue it. If a
484 * receiver is waiting, we can release our reserved space in
488 * If we need to queue, our space in the message queue is reserved.
492 register ipc_mqueue_t mqueue
,
493 register ipc_kmsg_t kmsg
)
498 * While the msg queue is locked, we have control of the
499 * kmsg, so the ref in it for the port is still good.
501 * Check for a receiver for the message.
506 wait_queue_t waitq
= &mqueue
->imq_wait_queue
;
508 mach_msg_size_t msize
;
510 receiver
= wait_queue_wakeup64_identity_locked(
515 /* waitq still locked, thread locked */
517 if (receiver
== THREAD_NULL
) {
519 * no receivers; queue kmsg
521 assert(mqueue
->imq_msgcount
> 0);
522 ipc_kmsg_enqueue_macro(&mqueue
->imq_messages
, kmsg
);
527 * If the receiver waited with a facility not directly
528 * related to Mach messaging, then it isn't prepared to get
529 * handed the message directly. Just set it running, and
530 * go look for another thread that can.
532 if (receiver
->ith_state
!= MACH_RCV_IN_PROGRESS
) {
533 thread_unlock(receiver
);
539 * We found a waiting thread.
540 * If the message is too large or the scatter list is too small
541 * the thread we wake up will get that as its status.
543 msize
= ipc_kmsg_copyout_size(kmsg
, receiver
->map
);
544 if (receiver
->ith_msize
<
545 (msize
+ REQUESTED_TRAILER_SIZE(thread_is_64bit(receiver
), receiver
->ith_option
))) {
546 receiver
->ith_msize
= msize
;
547 receiver
->ith_state
= MACH_RCV_TOO_LARGE
;
549 receiver
->ith_state
= MACH_MSG_SUCCESS
;
553 * If there is no problem with the upcoming receive, or the
554 * receiver thread didn't specifically ask for special too
555 * large error condition, go ahead and select it anyway.
557 if ((receiver
->ith_state
== MACH_MSG_SUCCESS
) ||
558 !(receiver
->ith_option
& MACH_RCV_LARGE
)) {
560 receiver
->ith_kmsg
= kmsg
;
561 receiver
->ith_seqno
= mqueue
->imq_seqno
++;
562 thread_unlock(receiver
);
564 /* we didn't need our reserved spot in the queue */
565 ipc_mqueue_release_msgcount(mqueue
);
570 * Otherwise, this thread needs to be released to run
571 * and handle its error without getting the message. We
572 * need to go back and pick another one.
574 receiver
->ith_receiver_name
= mqueue
->imq_receiver_name
;
575 receiver
->ith_kmsg
= IKM_NULL
;
576 receiver
->ith_seqno
= 0;
577 thread_unlock(receiver
);
583 current_task()->messages_sent
++;
589 ipc_mqueue_receive_results(wait_result_t saved_wait_result
)
591 thread_t self
= current_thread();
592 mach_msg_option_t option
= self
->ith_option
;
595 * why did we wake up?
597 switch (saved_wait_result
) {
598 case THREAD_TIMED_OUT
:
599 self
->ith_state
= MACH_RCV_TIMED_OUT
;
602 case THREAD_INTERRUPTED
:
603 self
->ith_state
= MACH_RCV_INTERRUPTED
;
607 /* something bad happened to the port/set */
608 self
->ith_state
= MACH_RCV_PORT_CHANGED
;
611 case THREAD_AWAKENED
:
613 * We do not need to go select a message, somebody
614 * handed us one (or a too-large indication).
616 switch (self
->ith_state
) {
617 case MACH_RCV_SCATTER_SMALL
:
618 case MACH_RCV_TOO_LARGE
:
620 * Somebody tried to give us a too large
621 * message. If we indicated that we cared,
622 * then they only gave us the indication,
623 * otherwise they gave us the indication
624 * AND the message anyway.
626 if (option
& MACH_RCV_LARGE
) {
630 case MACH_MSG_SUCCESS
:
634 panic("ipc_mqueue_receive_results: strange ith_state");
638 panic("ipc_mqueue_receive_results: strange wait_result");
643 ipc_mqueue_receive_continue(
644 __unused
void *param
,
645 wait_result_t wresult
)
647 ipc_mqueue_receive_results(wresult
);
648 mach_msg_receive_continue(); /* hard-coded for now */
652 * Routine: ipc_mqueue_receive
654 * Receive a message from a message queue.
656 * If continuation is non-zero, then we might discard
657 * our kernel stack when we block. We will continue
658 * after unblocking by executing continuation.
660 * If resume is true, then we are resuming a receive
661 * operation after a blocked receive discarded our stack.
663 * Our caller must hold a reference for the port or port set
664 * to which this queue belongs, to keep the queue
665 * from being deallocated.
667 * The kmsg is returned with clean header fields
668 * and with the circular bit turned off.
670 * MACH_MSG_SUCCESS Message returned in kmsgp.
671 * MACH_RCV_TOO_LARGE Message size returned in kmsgp.
672 * MACH_RCV_TIMED_OUT No message obtained.
673 * MACH_RCV_INTERRUPTED No message obtained.
674 * MACH_RCV_PORT_DIED Port/set died; no message.
675 * MACH_RCV_PORT_CHANGED Port moved into set; no msg.
682 mach_msg_option_t option
,
683 mach_msg_size_t max_size
,
684 mach_msg_timeout_t rcv_timeout
,
687 wait_result_t wresult
;
688 thread_t self
= current_thread();
690 wresult
= ipc_mqueue_receive_on_thread(mqueue
, option
, max_size
,
691 rcv_timeout
, interruptible
,
693 if (wresult
== THREAD_NOT_WAITING
)
696 if (wresult
== THREAD_WAITING
) {
697 counter((interruptible
== THREAD_ABORTSAFE
) ?
698 c_ipc_mqueue_receive_block_user
++ :
699 c_ipc_mqueue_receive_block_kernel
++);
701 if (self
->ith_continuation
)
702 thread_block(ipc_mqueue_receive_continue
);
705 wresult
= thread_block(THREAD_CONTINUE_NULL
);
707 ipc_mqueue_receive_results(wresult
);
711 ipc_mqueue_receive_on_thread(
713 mach_msg_option_t option
,
714 mach_msg_size_t max_size
,
715 mach_msg_timeout_t rcv_timeout
,
719 ipc_kmsg_queue_t kmsgs
;
720 wait_result_t wresult
;
727 if (imq_is_set(mqueue
)) {
730 q
= &mqueue
->imq_preposts
;
733 * If we are waiting on a portset mqueue, we need to see if
734 * any of the member ports have work for us. Ports that
735 * have (or recently had) messages will be linked in the
736 * prepost queue for the portset. By holding the portset's
737 * mqueue lock during the search, we tie up any attempts by
738 * mqueue_deliver or portset membership changes that may
742 while(!queue_empty(q
)) {
743 wait_queue_link_t wql
;
744 ipc_mqueue_t port_mq
;
746 queue_remove_first(q
, wql
, wait_queue_link_t
, wql_preposts
);
747 assert(!wql_is_preposted(wql
));
750 * This is a lock order violation, so we have to do it
751 * "softly," putting the link back on the prepost list
752 * if it fails (at the tail is fine since the order of
753 * handling messages from different sources in a set is
754 * not guaranteed and we'd like to skip to the next source
755 * if one is available).
757 port_mq
= (ipc_mqueue_t
)wql
->wql_queue
;
758 if (!imq_lock_try(port_mq
)) {
759 queue_enter(q
, wql
, wait_queue_link_t
, wql_preposts
);
765 goto search_set
; /* start again at beginning - SMP */
769 * If there are no messages on this queue, just skip it
770 * (we already removed the link from the set's prepost queue).
772 kmsgs
= &port_mq
->imq_messages
;
773 if (ipc_kmsg_queue_first(kmsgs
) == IKM_NULL
) {
779 * There are messages, so reinsert the link back
780 * at the tail of the preposted queue (for fairness)
781 * while we still have the portset mqueue locked.
783 queue_enter(q
, wql
, wait_queue_link_t
, wql_preposts
);
787 * Continue on to handling the message with just
788 * the port mqueue locked.
790 ipc_mqueue_select_on_thread(port_mq
, option
, max_size
, thread
);
793 return THREAD_NOT_WAITING
;
800 * Receive on a single port. Just try to get the messages.
802 kmsgs
= &mqueue
->imq_messages
;
803 if (ipc_kmsg_queue_first(kmsgs
) != IKM_NULL
) {
804 ipc_mqueue_select_on_thread(mqueue
, option
, max_size
, thread
);
807 return THREAD_NOT_WAITING
;
812 * Looks like we'll have to block. The mqueue we will
813 * block on (whether the set's or the local port's) is
816 if (option
& MACH_RCV_TIMEOUT
) {
817 if (rcv_timeout
== 0) {
820 thread
->ith_state
= MACH_RCV_TIMED_OUT
;
821 return THREAD_NOT_WAITING
;
826 thread
->ith_state
= MACH_RCV_IN_PROGRESS
;
827 thread
->ith_option
= option
;
828 thread
->ith_msize
= max_size
;
830 if (option
& MACH_RCV_TIMEOUT
)
831 clock_interval_to_deadline(rcv_timeout
, 1000*NSEC_PER_USEC
, &deadline
);
835 wresult
= wait_queue_assert_wait64_locked(&mqueue
->imq_wait_queue
,
838 TIMEOUT_URGENCY_USER_NORMAL
,
841 /* preposts should be detected above, not here */
842 if (wresult
== THREAD_AWAKENED
)
843 panic("ipc_mqueue_receive_on_thread: sleep walking");
845 thread_unlock(thread
);
853 * Routine: ipc_mqueue_select_on_thread
855 * A receiver discovered that there was a message on the queue
856 * before he had to block. Pick the message off the queue and
857 * "post" it to thread.
861 * There is a message.
863 * MACH_MSG_SUCCESS Actually selected a message for ourselves.
864 * MACH_RCV_TOO_LARGE May or may not have pull it, but it is large
867 ipc_mqueue_select_on_thread(
869 mach_msg_option_t option
,
870 mach_msg_size_t max_size
,
874 mach_msg_return_t mr
= MACH_MSG_SUCCESS
;
875 mach_msg_size_t rcv_size
;
878 * Do some sanity checking of our ability to receive
879 * before pulling the message off the queue.
881 kmsg
= ipc_kmsg_queue_first(&mqueue
->imq_messages
);
882 assert(kmsg
!= IKM_NULL
);
885 * If we really can't receive it, but we had the
886 * MACH_RCV_LARGE option set, then don't take it off
887 * the queue, instead return the appropriate error
890 rcv_size
= ipc_kmsg_copyout_size(kmsg
, thread
->map
);
891 if (rcv_size
+ REQUESTED_TRAILER_SIZE(thread_is_64bit(thread
), option
) > max_size
) {
892 mr
= MACH_RCV_TOO_LARGE
;
893 if (option
& MACH_RCV_LARGE
) {
894 thread
->ith_receiver_name
= mqueue
->imq_receiver_name
;
895 thread
->ith_kmsg
= IKM_NULL
;
896 thread
->ith_msize
= rcv_size
;
897 thread
->ith_seqno
= 0;
898 thread
->ith_state
= mr
;
903 ipc_kmsg_rmqueue_first_macro(&mqueue
->imq_messages
, kmsg
);
904 ipc_mqueue_release_msgcount(mqueue
);
905 thread
->ith_seqno
= mqueue
->imq_seqno
++;
906 thread
->ith_kmsg
= kmsg
;
907 thread
->ith_state
= mr
;
909 current_task()->messages_received
++;
914 * Routine: ipc_mqueue_peek
916 * Peek at a (non-set) message queue to see if it has a message
917 * matching the sequence number provided (if zero, then the
918 * first message in the queue) and return vital info about the
922 * Locks may be held by callers, so this routine cannot block.
923 * Caller holds reference on the message queue.
926 ipc_mqueue_peek(ipc_mqueue_t mq
,
927 mach_port_seqno_t
*seqnop
,
928 mach_msg_size_t
*msg_sizep
,
929 mach_msg_id_t
*msg_idp
,
930 mach_msg_max_trailer_t
*msg_trailerp
)
932 ipc_kmsg_queue_t kmsgq
;
934 mach_port_seqno_t seqno
, msgoff
;
938 assert(!imq_is_set(mq
));
943 seqno
= (seqnop
!= NULL
) ? seqno
= *seqnop
: 0;
946 seqno
= mq
->imq_seqno
;
948 } else if (seqno
>= mq
->imq_seqno
&&
949 seqno
< mq
->imq_seqno
+ mq
->imq_msgcount
) {
950 msgoff
= seqno
- mq
->imq_seqno
;
954 /* look for the message that would match that seqno */
955 kmsgq
= &mq
->imq_messages
;
956 kmsg
= ipc_kmsg_queue_first(kmsgq
);
957 while (msgoff
-- && kmsg
!= IKM_NULL
) {
958 kmsg
= ipc_kmsg_queue_next(kmsgq
, kmsg
);
960 if (kmsg
== IKM_NULL
)
963 /* found one - return the requested info */
966 if (msg_sizep
!= NULL
)
967 *msg_sizep
= kmsg
->ikm_header
->msgh_size
;
969 *msg_idp
= kmsg
->ikm_header
->msgh_id
;
970 if (msg_trailerp
!= NULL
)
972 (mach_msg_max_trailer_t
*)((vm_offset_t
)kmsg
->ikm_header
+
973 round_msg(kmsg
->ikm_header
->msgh_size
)),
974 sizeof(mach_msg_max_trailer_t
));
984 * Routine: ipc_mqueue_set_peek
986 * Peek at a message queue set to see if it has any ports
990 * Locks may be held by callers, so this routine cannot block.
991 * Caller holds reference on the message queue.
994 ipc_mqueue_set_peek(ipc_mqueue_t mq
)
996 wait_queue_link_t wql
;
1001 assert(imq_is_set(mq
));
1007 * peek at the contained port message queues, return as soon as
1008 * we spot a message on one of the message queues linked on the
1009 * prepost list. No need to lock each message queue, as only the
1010 * head of each queue is checked. If a message wasn't there before
1011 * we entered here, no need to find it (if we do, great).
1014 q
= &mq
->imq_preposts
;
1015 queue_iterate(q
, wql
, wait_queue_link_t
, wql_preposts
) {
1016 ipc_mqueue_t port_mq
= (ipc_mqueue_t
)wql
->wql_queue
;
1017 ipc_kmsg_queue_t kmsgs
= &port_mq
->imq_messages
;
1019 if (ipc_kmsg_queue_first(kmsgs
) != IKM_NULL
) {
1030 * Routine: ipc_mqueue_set_gather_member_names
1032 * Iterate a message queue set to identify the member port
1033 * names. Actual returned names is limited to maxnames entries,
1034 * but we keep counting the actual number of members to let
1035 * the caller decide to retry if necessary.
1038 * Locks may be held by callers, so this routine cannot block.
1039 * Caller holds reference on the message queue.
1042 ipc_mqueue_set_gather_member_names(
1044 ipc_entry_num_t maxnames
,
1045 mach_port_name_t
*names
,
1046 ipc_entry_num_t
*actualp
)
1048 wait_queue_link_t wql
;
1051 ipc_entry_num_t actual
= 0;
1053 assert(imq_is_set(mq
));
1059 * Iterate over the member ports through the mqueue set links
1060 * capturing as many names as we can.
1062 q
= &mq
->imq_setlinks
;
1063 queue_iterate(q
, wql
, wait_queue_link_t
, wql_setlinks
) {
1064 ipc_mqueue_t port_mq
= (ipc_mqueue_t
)wql
->wql_queue
;
1066 if (actual
< maxnames
)
1067 names
[actual
] = port_mq
->imq_receiver_name
;
1078 * Routine: ipc_mqueue_destroy
1080 * Destroy a (non-set) message queue.
1081 * Set any blocked senders running.
1082 * Destroy the kmsgs in the queue.
1085 * Receivers were removed when the receive right was "changed"
1089 ipc_mqueue_t mqueue
)
1091 ipc_kmsg_queue_t kmqueue
;
1093 boolean_t reap
= FALSE
;
1099 * rouse all blocked senders
1101 mqueue
->imq_fullwaiters
= FALSE
;
1102 wait_queue_wakeup64_all_locked(
1103 &mqueue
->imq_wait_queue
,
1109 * Move messages from the specified queue to the per-thread
1110 * clean/drain queue while we have the mqueue lock.
1112 kmqueue
= &mqueue
->imq_messages
;
1113 while ((kmsg
= ipc_kmsg_dequeue(kmqueue
)) != IKM_NULL
) {
1115 first
= ipc_kmsg_delayed_destroy(kmsg
);
1124 * Destroy the messages we enqueued if we aren't nested
1125 * inside some other attempt to drain the same queue.
1128 ipc_kmsg_reap_delayed();
1132 * Routine: ipc_mqueue_set_qlimit
1134 * Changes a message queue limit; the maximum number
1135 * of messages which may be queued.
1141 ipc_mqueue_set_qlimit(
1142 ipc_mqueue_t mqueue
,
1143 mach_port_msgcount_t qlimit
)
1147 assert(qlimit
<= MACH_PORT_QLIMIT_MAX
);
1149 /* wake up senders allowed by the new qlimit */
1152 if (qlimit
> mqueue
->imq_qlimit
) {
1153 mach_port_msgcount_t i
, wakeup
;
1155 /* caution: wakeup, qlimit are unsigned */
1156 wakeup
= qlimit
- mqueue
->imq_qlimit
;
1158 for (i
= 0; i
< wakeup
; i
++) {
1159 if (wait_queue_wakeup64_one_locked(
1160 &mqueue
->imq_wait_queue
,
1163 FALSE
) == KERN_NOT_WAITING
) {
1164 mqueue
->imq_fullwaiters
= FALSE
;
1167 mqueue
->imq_msgcount
++; /* give it to the awakened thread */
1170 mqueue
->imq_qlimit
= qlimit
;
1176 * Routine: ipc_mqueue_set_seqno
1178 * Changes an mqueue's sequence number.
1180 * Caller holds a reference to the queue's containing object.
1183 ipc_mqueue_set_seqno(
1184 ipc_mqueue_t mqueue
,
1185 mach_port_seqno_t seqno
)
1191 mqueue
->imq_seqno
= seqno
;
1198 * Routine: ipc_mqueue_copyin
1200 * Convert a name in a space to a message queue.
1202 * Nothing locked. If successful, the caller gets a ref for
1203 * for the object. This ref ensures the continued existence of
1206 * MACH_MSG_SUCCESS Found a message queue.
1207 * MACH_RCV_INVALID_NAME The space is dead.
1208 * MACH_RCV_INVALID_NAME The name doesn't denote a right.
1209 * MACH_RCV_INVALID_NAME
1210 * The denoted right is not receive or port set.
1211 * MACH_RCV_IN_SET Receive right is a member of a set.
1217 mach_port_name_t name
,
1218 ipc_mqueue_t
*mqueuep
,
1219 ipc_object_t
*objectp
)
1222 ipc_object_t object
;
1223 ipc_mqueue_t mqueue
;
1225 is_read_lock(space
);
1226 if (!is_active(space
)) {
1227 is_read_unlock(space
);
1228 return MACH_RCV_INVALID_NAME
;
1231 entry
= ipc_entry_lookup(space
, name
);
1232 if (entry
== IE_NULL
) {
1233 is_read_unlock(space
);
1234 return MACH_RCV_INVALID_NAME
;
1237 object
= entry
->ie_object
;
1239 if (entry
->ie_bits
& MACH_PORT_TYPE_RECEIVE
) {
1242 port
= (ipc_port_t
) object
;
1243 assert(port
!= IP_NULL
);
1246 assert(ip_active(port
));
1247 assert(port
->ip_receiver_name
== name
);
1248 assert(port
->ip_receiver
== space
);
1249 is_read_unlock(space
);
1250 mqueue
= &port
->ip_messages
;
1252 } else if (entry
->ie_bits
& MACH_PORT_TYPE_PORT_SET
) {
1255 pset
= (ipc_pset_t
) object
;
1256 assert(pset
!= IPS_NULL
);
1259 assert(ips_active(pset
));
1260 assert(pset
->ips_local_name
== name
);
1261 is_read_unlock(space
);
1263 mqueue
= &pset
->ips_messages
;
1265 is_read_unlock(space
);
1266 return MACH_RCV_INVALID_NAME
;
1270 * At this point, the object is locked and active,
1271 * the space is unlocked, and mqueue is initialized.
1274 io_reference(object
);
1279 return MACH_MSG_SUCCESS
;