2 * Copyright (c) 1999-2013 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 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
55 #include <mach/mach.h>
56 #include <mach/boolean.h>
57 #include <mach/kern_return.h>
58 #include <mach/message.h>
59 #include <mach/mig_errors.h>
60 #include <mach/vm_statistics.h>
61 #include <TargetConditionals.h>
63 extern int proc_importance_assertion_begin_with_msg(mach_msg_header_t
* msg
, mach_msg_trailer_t
* trailer
, uint64_t * assertion_handlep
);
64 extern int proc_importance_assertion_complete(uint64_t assertion_handle
);
66 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
67 mach_msg_trap((msg), (opt), (ssize), (rsize), (rname), (to), (not))
69 #define LIBMACH_OPTIONS (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT)
74 * Send and/or receive a message. If the message operation
75 * is interrupted, and the user did not request an indication
76 * of that fact, then restart the appropriate parts of the
80 mach_msg(msg
, option
, send_size
, rcv_size
, rcv_name
, timeout
, notify
)
81 mach_msg_header_t
*msg
;
82 mach_msg_option_t option
;
83 mach_msg_size_t send_size
;
84 mach_msg_size_t rcv_size
;
86 mach_msg_timeout_t timeout
;
92 * Consider the following cases:
93 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
95 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
96 * 3) RPC calls with interruptions in one/both halves.
98 * We refrain from passing the option bits that we implement
99 * to the kernel. This prevents their presence from inhibiting
100 * the kernel's fast paths (when it checks the option value).
103 mr
= MACH_MSG_TRAP(msg
, option
& ~LIBMACH_OPTIONS
,
104 send_size
, rcv_size
, rcv_name
,
106 if (mr
== MACH_MSG_SUCCESS
) {
107 return MACH_MSG_SUCCESS
;
110 if ((option
& MACH_SEND_INTERRUPT
) == 0) {
111 while (mr
== MACH_SEND_INTERRUPTED
) {
112 mr
= MACH_MSG_TRAP(msg
,
113 option
& ~LIBMACH_OPTIONS
,
114 send_size
, rcv_size
, rcv_name
,
119 if ((option
& MACH_RCV_INTERRUPT
) == 0) {
120 while (mr
== MACH_RCV_INTERRUPTED
) {
121 mr
= MACH_MSG_TRAP(msg
,
122 option
& ~(LIBMACH_OPTIONS
| MACH_SEND_MSG
),
123 0, rcv_size
, rcv_name
,
132 * Routine: mach_msg_overwrite
134 * Send and/or receive a message. If the message operation
135 * is interrupted, and the user did not request an indication
136 * of that fact, then restart the appropriate parts of the
139 * Distinct send and receive buffers may be specified. If
140 * no separate receive buffer is specified, the msg parameter
141 * will be used for both send and receive operations.
143 * In addition to a distinct receive buffer, that buffer may
144 * already contain scatter control information to direct the
145 * receiving of the message.
148 mach_msg_overwrite(msg
, option
, send_size
, rcv_limit
, rcv_name
, timeout
,
149 notify
, rcv_msg
, rcv_scatter_size
)
150 mach_msg_header_t
*msg
;
151 mach_msg_option_t option
;
152 mach_msg_size_t send_size
;
153 mach_msg_size_t rcv_limit
;
154 mach_port_t rcv_name
;
155 mach_msg_timeout_t timeout
;
157 mach_msg_header_t
*rcv_msg
;
158 mach_msg_size_t rcv_scatter_size
;
160 mach_msg_return_t mr
;
163 * Consider the following cases:
164 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
165 * plus special bits).
166 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
167 * 3) RPC calls with interruptions in one/both halves.
169 * We refrain from passing the option bits that we implement
170 * to the kernel. This prevents their presence from inhibiting
171 * the kernel's fast paths (when it checks the option value).
174 mr
= mach_msg_overwrite_trap(msg
, option
& ~LIBMACH_OPTIONS
,
175 send_size
, rcv_limit
, rcv_name
,
176 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
177 if (mr
== MACH_MSG_SUCCESS
) {
178 return MACH_MSG_SUCCESS
;
181 if ((option
& MACH_SEND_INTERRUPT
) == 0) {
182 while (mr
== MACH_SEND_INTERRUPTED
) {
183 mr
= mach_msg_overwrite_trap(msg
,
184 option
& ~LIBMACH_OPTIONS
,
185 send_size
, rcv_limit
, rcv_name
,
186 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
190 if ((option
& MACH_RCV_INTERRUPT
) == 0) {
191 while (mr
== MACH_RCV_INTERRUPTED
) {
192 mr
= mach_msg_overwrite_trap(msg
,
193 option
& ~(LIBMACH_OPTIONS
| MACH_SEND_MSG
),
194 0, rcv_limit
, rcv_name
,
195 timeout
, notify
, rcv_msg
, rcv_scatter_size
);
204 mach_msg_send(mach_msg_header_t
*msg
)
206 return mach_msg(msg
, MACH_SEND_MSG
,
207 msg
->msgh_size
, 0, MACH_PORT_NULL
,
208 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
212 mach_msg_receive(mach_msg_header_t
*msg
)
214 return mach_msg(msg
, MACH_RCV_MSG
,
215 0, msg
->msgh_size
, msg
->msgh_local_port
,
216 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
221 mach_msg_destroy_port(mach_port_t port
, mach_msg_type_name_t type
)
223 if (MACH_PORT_VALID(port
)) {
225 case MACH_MSG_TYPE_MOVE_SEND
:
226 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
227 /* destroy the send/send-once right */
228 (void) mach_port_deallocate(mach_task_self_
, port
);
231 case MACH_MSG_TYPE_MOVE_RECEIVE
:
232 /* destroy the receive right */
233 (void) mach_port_mod_refs(mach_task_self_
, port
,
234 MACH_PORT_RIGHT_RECEIVE
, -1);
237 case MACH_MSG_TYPE_MAKE_SEND
:
238 /* create a send right and then destroy it */
239 (void) mach_port_insert_right(mach_task_self_
, port
,
240 port
, MACH_MSG_TYPE_MAKE_SEND
);
241 (void) mach_port_deallocate(mach_task_self_
, port
);
244 case MACH_MSG_TYPE_MAKE_SEND_ONCE
:
245 /* create a send-once right and then destroy it */
246 (void) mach_port_extract_right(mach_task_self_
, port
,
247 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
249 (void) mach_port_deallocate(mach_task_self_
, port
);
256 mach_msg_destroy_memory(vm_offset_t addr
, vm_size_t size
)
259 (void) vm_deallocate(mach_task_self_
, addr
, size
);
265 * Routine: mach_msg_destroy
267 * mach_msg_destroy is useful in two contexts.
269 * First, it can deallocate all port rights and
270 * out-of-line memory in a received message.
271 * When a server receives a request it doesn't want,
272 * it needs this functionality.
274 * Second, it can mimic the side-effects of a msg-send
275 * operation. The effect is as if the message were sent
276 * and then destroyed inside the kernel. When a server
277 * can't send a reply (because the client died),
278 * it needs this functionality.
281 mach_msg_destroy(mach_msg_header_t
*msg
)
283 mach_msg_bits_t mbits
= msg
->msgh_bits
;
286 * The msgh_local_port field doesn't hold a port right.
287 * The receive operation consumes the destination port right.
290 mach_msg_destroy_port(msg
->msgh_remote_port
, MACH_MSGH_BITS_REMOTE(mbits
));
291 mach_msg_destroy_port(msg
->msgh_voucher_port
, MACH_MSGH_BITS_VOUCHER(mbits
));
293 if (mbits
& MACH_MSGH_BITS_COMPLEX
) {
294 mach_msg_base_t
*base
;
295 mach_msg_type_number_t count
, i
;
296 mach_msg_descriptor_t
*daddr
;
298 base
= (mach_msg_base_t
*) msg
;
299 count
= base
->body
.msgh_descriptor_count
;
301 daddr
= (mach_msg_descriptor_t
*) (base
+ 1);
302 for (i
= 0; i
< count
; i
++) {
303 switch (daddr
->type
.type
) {
304 case MACH_MSG_PORT_DESCRIPTOR
: {
305 mach_msg_port_descriptor_t
*dsc
;
308 * Destroy port rights carried in the message
311 mach_msg_destroy_port(dsc
->name
, dsc
->disposition
);
312 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
316 case MACH_MSG_OOL_DESCRIPTOR
: {
317 mach_msg_ool_descriptor_t
*dsc
;
320 * Destroy memory carried in the message
322 dsc
= &daddr
->out_of_line
;
323 if (dsc
->deallocate
) {
324 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
327 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
331 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR
: {
332 mach_msg_ool_descriptor_t
*dsc
;
337 dsc
= &daddr
->out_of_line
;
338 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
342 case MACH_MSG_OOL_PORTS_DESCRIPTOR
: {
344 mach_msg_ool_ports_descriptor_t
*dsc
;
345 mach_msg_type_number_t j
;
348 * Destroy port rights carried in the message
350 dsc
= &daddr
->ool_ports
;
351 ports
= (mach_port_t
*) dsc
->address
;
352 for (j
= 0; j
< dsc
->count
; j
++, ports
++) {
353 mach_msg_destroy_port(*ports
, dsc
->disposition
);
357 * Destroy memory carried in the message
359 if (dsc
->deallocate
) {
360 mach_msg_destroy_memory((vm_offset_t
)dsc
->address
,
361 dsc
->count
* sizeof(mach_port_t
));
363 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
367 case MACH_MSG_GUARDED_PORT_DESCRIPTOR
: {
368 mach_msg_guarded_port_descriptor_t
*dsc
;
369 mach_msg_guard_flags_t flags
;
371 * Destroy port right carried in the message
373 dsc
= &daddr
->guarded_port
;
375 if ((flags
& MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND
) == 0) {
376 /* Need to unguard before destroying the port */
377 mach_port_unguard(mach_task_self_
, dsc
->name
, (uint64_t)dsc
->context
);
379 mach_msg_destroy_port(dsc
->name
, dsc
->disposition
);
380 daddr
= (mach_msg_descriptor_t
*)(dsc
+ 1);
388 static inline boolean_t
389 mach_msg_server_is_recoverable_send_error(kern_return_t kr
)
392 case MACH_SEND_INVALID_DEST
:
393 case MACH_SEND_TIMED_OUT
:
394 case MACH_SEND_INTERRUPTED
:
398 * Other errors mean that the message may have been partially destroyed
399 * by the kernel, and these can't be recovered and may leak resources.
406 mach_msg_server_mig_return_code(mig_reply_error_t
*reply
)
409 * If the message is complex, it is assumed that the reply was successful,
410 * as the RetCode is where the count of out of line descriptors is.
412 * If not, we read RetCode.
414 if (reply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
417 return reply
->RetCode
;
421 mach_msg_server_consume_unsent_message(mach_msg_header_t
*hdr
)
423 /* mach_msg_destroy doesn't handle the local port */
424 mach_port_t port
= hdr
->msgh_local_port
;
425 if (MACH_PORT_VALID(port
)) {
426 switch (MACH_MSGH_BITS_LOCAL(hdr
->msgh_bits
)) {
427 case MACH_MSG_TYPE_MOVE_SEND
:
428 case MACH_MSG_TYPE_MOVE_SEND_ONCE
:
429 /* destroy the send/send-once right */
430 (void) mach_port_deallocate(mach_task_self_
, port
);
431 hdr
->msgh_local_port
= MACH_PORT_NULL
;
435 mach_msg_destroy(hdr
);
439 * Routine: mach_msg_server_once
441 * A simple generic server function. It allows more flexibility
442 * than mach_msg_server by processing only one message request
443 * and then returning to the user. Note that more in the way
444 * of error codes are returned to the user; specifically, any
445 * failing error from mach_msg calls will be returned
446 * (though errors from the demux routine or the routine it
447 * calls will not be).
450 mach_msg_server_once(
451 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
452 mach_msg_size_t max_size
,
453 mach_port_t rcv_name
,
454 mach_msg_options_t options
)
456 mig_reply_error_t
*bufRequest
, *bufReply
;
457 mach_msg_size_t request_size
;
458 mach_msg_size_t request_alloc
;
459 mach_msg_size_t trailer_alloc
;
460 mach_msg_size_t reply_alloc
;
461 mach_msg_return_t mr
;
463 mach_port_t self
= mach_task_self_
;
464 voucher_mach_msg_state_t old_state
= VOUCHER_MACH_MSG_STATE_UNCHANGED
;
466 options
&= ~(MACH_SEND_MSG
| MACH_RCV_MSG
| MACH_RCV_VOUCHER
);
468 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
469 request_alloc
= (mach_msg_size_t
)round_page(max_size
+ trailer_alloc
);
471 request_size
= (options
& MACH_RCV_LARGE
) ?
472 request_alloc
: max_size
+ trailer_alloc
;
474 reply_alloc
= (mach_msg_size_t
)round_page((options
& MACH_SEND_TRAILER
) ?
475 (max_size
+ MAX_TRAILER_SIZE
) :
478 kr
= vm_allocate(self
,
479 (vm_address_t
*)&bufReply
,
481 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
) | TRUE
);
482 if (kr
!= KERN_SUCCESS
) {
487 mach_msg_size_t new_request_alloc
;
489 kr
= vm_allocate(self
,
490 (vm_address_t
*)&bufRequest
,
492 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
) | TRUE
);
493 if (kr
!= KERN_SUCCESS
) {
495 (vm_address_t
)bufReply
,
500 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
| MACH_RCV_VOUCHER
| options
,
501 0, request_size
, rcv_name
,
502 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
504 if (!((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
))) {
508 new_request_alloc
= (mach_msg_size_t
)round_page(bufRequest
->Head
.msgh_size
+
511 (vm_address_t
) bufRequest
,
513 request_size
= request_alloc
= new_request_alloc
;
516 if (mr
== MACH_MSG_SUCCESS
) {
517 /* we have a request message */
519 old_state
= voucher_mach_msg_adopt(&bufRequest
->Head
);
521 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
523 switch (mach_msg_server_mig_return_code(bufReply
)) {
527 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
531 * destroy the request - but not the reply port
532 * (MIG moved it into the bufReply).
534 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
535 mach_msg_destroy(&bufRequest
->Head
);
539 * We don't want to block indefinitely because the client
540 * isn't receiving messages from the reply port.
541 * If we have a send-once right for the reply port, then
542 * this isn't a concern because the send won't block.
543 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
544 * To avoid falling off the kernel's fast RPC path unnecessarily,
545 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
547 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
548 mr
= mach_msg(&bufReply
->Head
,
549 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
550 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
551 MACH_SEND_MSG
| options
:
552 MACH_SEND_MSG
| MACH_SEND_TIMEOUT
| options
,
553 bufReply
->Head
.msgh_size
, 0, MACH_PORT_NULL
,
554 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
556 if (mach_msg_server_is_recoverable_send_error(mr
)) {
557 mach_msg_server_consume_unsent_message(&bufReply
->Head
);
558 mr
= MACH_MSG_SUCCESS
;
563 voucher_mach_msg_revert(old_state
);
565 (void)vm_deallocate(self
,
566 (vm_address_t
) bufRequest
,
568 (void)vm_deallocate(self
,
569 (vm_address_t
) bufReply
,
575 * Routine: mach_msg_server
577 * A simple generic server function. Note that changes here
578 * should be considered for duplication above.
582 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
583 mach_msg_size_t max_size
,
584 mach_port_t rcv_name
,
585 mach_msg_options_t options
)
587 mig_reply_error_t
*bufRequest
, *bufReply
;
588 mach_msg_size_t request_size
;
589 mach_msg_size_t new_request_alloc
;
590 mach_msg_size_t request_alloc
;
591 mach_msg_size_t trailer_alloc
;
592 mach_msg_size_t reply_alloc
;
593 mach_msg_return_t mr
;
595 mach_port_t self
= mach_task_self_
;
596 voucher_mach_msg_state_t old_state
= VOUCHER_MACH_MSG_STATE_UNCHANGED
;
597 boolean_t buffers_swapped
= FALSE
;
599 options
&= ~(MACH_SEND_MSG
| MACH_RCV_MSG
| MACH_RCV_VOUCHER
);
601 reply_alloc
= (mach_msg_size_t
)round_page((options
& MACH_SEND_TRAILER
) ?
602 (max_size
+ MAX_TRAILER_SIZE
) : max_size
);
604 kr
= vm_allocate(self
,
605 (vm_address_t
*)&bufReply
,
607 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
) | TRUE
);
608 if (kr
!= KERN_SUCCESS
) {
613 trailer_alloc
= REQUESTED_TRAILER_SIZE(options
);
614 new_request_alloc
= (mach_msg_size_t
)round_page(max_size
+ trailer_alloc
);
616 request_size
= (options
& MACH_RCV_LARGE
) ?
617 new_request_alloc
: max_size
+ trailer_alloc
;
620 if (request_alloc
< new_request_alloc
) {
621 request_alloc
= new_request_alloc
;
622 kr
= vm_allocate(self
,
623 (vm_address_t
*)&bufRequest
,
625 VM_MAKE_TAG(VM_MEMORY_MACH_MSG
) | TRUE
);
626 if (kr
!= KERN_SUCCESS
) {
628 (vm_address_t
)bufReply
,
634 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
| MACH_RCV_VOUCHER
| options
,
635 0, request_size
, rcv_name
,
636 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
638 while (mr
== MACH_MSG_SUCCESS
) {
639 /* we have another request message */
641 buffers_swapped
= FALSE
;
642 old_state
= voucher_mach_msg_adopt(&bufRequest
->Head
);
643 bufReply
->Head
= (mach_msg_header_t
){};
645 (void) (*demux
)(&bufRequest
->Head
, &bufReply
->Head
);
647 switch (mach_msg_server_mig_return_code(bufReply
)) {
651 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
655 * destroy the request - but not the reply port
656 * (MIG moved it into the bufReply).
658 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
659 mach_msg_destroy(&bufRequest
->Head
);
663 * We don't want to block indefinitely because the client
664 * isn't receiving messages from the reply port.
665 * If we have a send-once right for the reply port, then
666 * this isn't a concern because the send won't block.
667 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
668 * To avoid falling off the kernel's fast RPC path,
669 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
671 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
672 if (request_alloc
== reply_alloc
) {
673 mig_reply_error_t
*bufTemp
;
677 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
678 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
679 MACH_SEND_MSG
| MACH_RCV_MSG
| MACH_RCV_TIMEOUT
| MACH_RCV_VOUCHER
| options
:
680 MACH_SEND_MSG
| MACH_RCV_MSG
| MACH_SEND_TIMEOUT
| MACH_RCV_TIMEOUT
| MACH_RCV_VOUCHER
| options
,
681 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
682 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
684 /* swap request and reply */
685 bufTemp
= bufRequest
;
686 bufRequest
= bufReply
;
688 buffers_swapped
= TRUE
;
690 mr
= mach_msg_overwrite(
692 (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) ==
693 MACH_MSG_TYPE_MOVE_SEND_ONCE
) ?
694 MACH_SEND_MSG
| MACH_RCV_MSG
| MACH_RCV_TIMEOUT
| MACH_RCV_VOUCHER
| options
:
695 MACH_SEND_MSG
| MACH_RCV_MSG
| MACH_SEND_TIMEOUT
| MACH_RCV_TIMEOUT
| MACH_RCV_VOUCHER
| options
,
696 bufReply
->Head
.msgh_size
, request_size
, rcv_name
,
697 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
,
698 &bufRequest
->Head
, 0);
702 * Need to destroy the reply msg in case if there was a send timeout or
703 * invalid destination. The reply msg would be swapped with request msg
704 * if buffers_swapped is true, thus destroy request msg instead of
705 * reply msg in such cases.
707 if (mach_msg_server_is_recoverable_send_error(mr
)) {
708 if (buffers_swapped
) {
709 mach_msg_server_consume_unsent_message(&bufRequest
->Head
);
711 mach_msg_server_consume_unsent_message(&bufReply
->Head
);
713 } else if (mr
!= MACH_RCV_TIMED_OUT
) {
714 voucher_mach_msg_revert(old_state
);
715 old_state
= VOUCHER_MACH_MSG_STATE_UNCHANGED
;
720 voucher_mach_msg_revert(old_state
);
721 old_state
= VOUCHER_MACH_MSG_STATE_UNCHANGED
;
723 mr
= mach_msg(&bufRequest
->Head
, MACH_RCV_MSG
| MACH_RCV_VOUCHER
| options
,
724 0, request_size
, rcv_name
,
725 MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
726 } /* while (mr == MACH_MSG_SUCCESS) */
728 if ((mr
== MACH_RCV_TOO_LARGE
) && (options
& MACH_RCV_LARGE
)) {
729 new_request_alloc
= (mach_msg_size_t
)round_page(bufRequest
->Head
.msgh_size
+
731 request_size
= new_request_alloc
;
733 (vm_address_t
) bufRequest
,
741 (void)vm_deallocate(self
,
742 (vm_address_t
) bufRequest
,
744 (void)vm_deallocate(self
,
745 (vm_address_t
) bufReply
,
751 * Routine: mach_msg_server_importance
753 * A simple generic server function which handles importance
754 * promotion assertions for adaptive daemons.
757 mach_msg_server_importance(
758 boolean_t (*demux
)(mach_msg_header_t
*, mach_msg_header_t
*),
759 mach_msg_size_t max_size
,
760 mach_port_t rcv_name
,
761 mach_msg_options_t options
)
763 return mach_msg_server(demux
, max_size
, rcv_name
, options
);
767 mach_voucher_deallocate(
768 mach_voucher_t voucher
)
770 return mach_port_deallocate(mach_task_self(), voucher
);
773 #undef mach_msg_priority_is_pthread_priority
775 mach_msg_priority_is_pthread_priority(mach_msg_priority_t pri
)
777 return mach_msg_priority_is_pthread_priority_inline(pri
);
780 #undef mach_msg_priority_encode
782 mach_msg_priority_encode(mach_msg_qos_t override_qos
, mach_msg_qos_t qos
, int relpri
)
784 return mach_msg_priority_encode_inline(override_qos
, qos
, relpri
);
787 #undef mach_msg_priority_overide_qos
789 mach_msg_priority_overide_qos(mach_msg_priority_t pri
)
791 return mach_msg_priority_overide_qos_inline(pri
);
794 #undef mach_msg_priority_qos
796 mach_msg_priority_qos(mach_msg_priority_t pri
)
798 return mach_msg_priority_qos_inline(pri
);
801 #undef mach_msg_priority_relpri
803 mach_msg_priority_relpri(mach_msg_priority_t pri
)
805 return mach_msg_priority_relpri_inline(pri
);