]> git.saurik.com Git - apple/xnu.git/blob - libsyscall/mach/mach_msg.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / libsyscall / mach / mach_msg.c
1 /*
2 * Copyright (c) 1999-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
32 *
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.
38 *
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.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53
54 #include <stdlib.h>
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>
62
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);
65
66 #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \
67 mach_msg_trap((msg), (opt), (ssize), (rsize), (rname), (to), (not))
68
69 #define LIBMACH_OPTIONS (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT)
70
71 /*
72 * Routine: mach_msg
73 * Purpose:
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
77 * operation.
78 */
79 mach_msg_return_t
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;
85 mach_port_t rcv_name;
86 mach_msg_timeout_t timeout;
87 mach_port_t notify;
88 {
89 mach_msg_return_t mr;
90
91 /*
92 * Consider the following cases:
93 * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED
94 * plus special bits).
95 * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options.
96 * 3) RPC calls with interruptions in one/both halves.
97 *
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).
101 */
102
103 mr = MACH_MSG_TRAP(msg, option & ~LIBMACH_OPTIONS,
104 send_size, rcv_size, rcv_name,
105 timeout, notify);
106 if (mr == MACH_MSG_SUCCESS) {
107 return MACH_MSG_SUCCESS;
108 }
109
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,
115 timeout, notify);
116 }
117 }
118
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,
124 timeout, notify);
125 }
126 }
127
128 return mr;
129 }
130
131 /*
132 * Routine: mach_msg_overwrite
133 * Purpose:
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
137 * operation.
138 *
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.
142 *
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.
146 */
147 mach_msg_return_t
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;
156 mach_port_t notify;
157 mach_msg_header_t *rcv_msg;
158 mach_msg_size_t rcv_scatter_size;
159 {
160 mach_msg_return_t mr;
161
162 /*
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.
168 *
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).
172 */
173
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;
179 }
180
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);
187 }
188 }
189
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);
196 }
197 }
198
199 return mr;
200 }
201
202
203 mach_msg_return_t
204 mach_msg_send(mach_msg_header_t *msg)
205 {
206 return mach_msg(msg, MACH_SEND_MSG,
207 msg->msgh_size, 0, MACH_PORT_NULL,
208 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
209 }
210
211 mach_msg_return_t
212 mach_msg_receive(mach_msg_header_t *msg)
213 {
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);
217 }
218
219
220 static void
221 mach_msg_destroy_port(mach_port_t port, mach_msg_type_name_t type)
222 {
223 if (MACH_PORT_VALID(port)) {
224 switch (type) {
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);
229 break;
230
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);
235 break;
236
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);
242 break;
243
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,
248 &port, &type);
249 (void) mach_port_deallocate(mach_task_self_, port);
250 break;
251 }
252 }
253 }
254
255 static void
256 mach_msg_destroy_memory(vm_offset_t addr, vm_size_t size)
257 {
258 if (size != 0) {
259 (void) vm_deallocate(mach_task_self_, addr, size);
260 }
261 }
262
263
264 /*
265 * Routine: mach_msg_destroy
266 * Purpose:
267 * mach_msg_destroy is useful in two contexts.
268 *
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.
273 *
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.
279 */
280 void
281 mach_msg_destroy(mach_msg_header_t *msg)
282 {
283 mach_msg_bits_t mbits = msg->msgh_bits;
284
285 /*
286 * The msgh_local_port field doesn't hold a port right.
287 * The receive operation consumes the destination port right.
288 */
289
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));
292
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;
297
298 base = (mach_msg_base_t *) msg;
299 count = base->body.msgh_descriptor_count;
300
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;
306
307 /*
308 * Destroy port rights carried in the message
309 */
310 dsc = &daddr->port;
311 mach_msg_destroy_port(dsc->name, dsc->disposition);
312 daddr = (mach_msg_descriptor_t *)(dsc + 1);
313 break;
314 }
315
316 case MACH_MSG_OOL_DESCRIPTOR: {
317 mach_msg_ool_descriptor_t *dsc;
318
319 /*
320 * Destroy memory carried in the message
321 */
322 dsc = &daddr->out_of_line;
323 if (dsc->deallocate) {
324 mach_msg_destroy_memory((vm_offset_t)dsc->address,
325 dsc->size);
326 }
327 daddr = (mach_msg_descriptor_t *)(dsc + 1);
328 break;
329 }
330
331 case MACH_MSG_OOL_VOLATILE_DESCRIPTOR: {
332 mach_msg_ool_descriptor_t *dsc;
333
334 /*
335 * Just skip it.
336 */
337 dsc = &daddr->out_of_line;
338 daddr = (mach_msg_descriptor_t *)(dsc + 1);
339 break;
340 }
341
342 case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
343 mach_port_t *ports;
344 mach_msg_ool_ports_descriptor_t *dsc;
345 mach_msg_type_number_t j;
346
347 /*
348 * Destroy port rights carried in the message
349 */
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);
354 }
355
356 /*
357 * Destroy memory carried in the message
358 */
359 if (dsc->deallocate) {
360 mach_msg_destroy_memory((vm_offset_t)dsc->address,
361 dsc->count * sizeof(mach_port_t));
362 }
363 daddr = (mach_msg_descriptor_t *)(dsc + 1);
364 break;
365 }
366 }
367 }
368 }
369 }
370
371 /*
372 * Routine: mach_msg_server_once
373 * Purpose:
374 * A simple generic server function. It allows more flexibility
375 * than mach_msg_server by processing only one message request
376 * and then returning to the user. Note that more in the way
377 * of error codes are returned to the user; specifically, any
378 * failing error from mach_msg calls will be returned
379 * (though errors from the demux routine or the routine it
380 * calls will not be).
381 */
382 mach_msg_return_t
383 mach_msg_server_once(
384 boolean_t (*demux)(mach_msg_header_t *, mach_msg_header_t *),
385 mach_msg_size_t max_size,
386 mach_port_t rcv_name,
387 mach_msg_options_t options)
388 {
389 mig_reply_error_t *bufRequest, *bufReply;
390 mach_msg_size_t request_size;
391 mach_msg_size_t request_alloc;
392 mach_msg_size_t trailer_alloc;
393 mach_msg_size_t reply_alloc;
394 mach_msg_return_t mr;
395 kern_return_t kr;
396 mach_port_t self = mach_task_self_;
397 voucher_mach_msg_state_t old_state = VOUCHER_MACH_MSG_STATE_UNCHANGED;
398
399 options &= ~(MACH_SEND_MSG | MACH_RCV_MSG | MACH_RCV_VOUCHER);
400
401 trailer_alloc = REQUESTED_TRAILER_SIZE(options);
402 request_alloc = (mach_msg_size_t)round_page(max_size + trailer_alloc);
403
404 request_size = (options & MACH_RCV_LARGE) ?
405 request_alloc : max_size + trailer_alloc;
406
407 reply_alloc = (mach_msg_size_t)round_page((options & MACH_SEND_TRAILER) ?
408 (max_size + MAX_TRAILER_SIZE) :
409 max_size);
410
411 kr = vm_allocate(self,
412 (vm_address_t *)&bufReply,
413 reply_alloc,
414 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE);
415 if (kr != KERN_SUCCESS) {
416 return kr;
417 }
418
419 for (;;) {
420 mach_msg_size_t new_request_alloc;
421
422 kr = vm_allocate(self,
423 (vm_address_t *)&bufRequest,
424 request_alloc,
425 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE);
426 if (kr != KERN_SUCCESS) {
427 vm_deallocate(self,
428 (vm_address_t)bufReply,
429 reply_alloc);
430 return kr;
431 }
432
433 mr = mach_msg(&bufRequest->Head, MACH_RCV_MSG | MACH_RCV_VOUCHER | options,
434 0, request_size, rcv_name,
435 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
436
437 if (!((mr == MACH_RCV_TOO_LARGE) && (options & MACH_RCV_LARGE))) {
438 break;
439 }
440
441 new_request_alloc = (mach_msg_size_t)round_page(bufRequest->Head.msgh_size +
442 trailer_alloc);
443 vm_deallocate(self,
444 (vm_address_t) bufRequest,
445 request_alloc);
446 request_size = request_alloc = new_request_alloc;
447 }
448
449 if (mr == MACH_MSG_SUCCESS) {
450 /* we have a request message */
451
452 old_state = voucher_mach_msg_adopt(&bufRequest->Head);
453
454 (void) (*demux)(&bufRequest->Head, &bufReply->Head);
455
456 if (!(bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) {
457 if (bufReply->RetCode == MIG_NO_REPLY) {
458 bufReply->Head.msgh_remote_port = MACH_PORT_NULL;
459 } else if ((bufReply->RetCode != KERN_SUCCESS) &&
460 (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) {
461 /* destroy the request - but not the reply port */
462 bufRequest->Head.msgh_remote_port = MACH_PORT_NULL;
463 mach_msg_destroy(&bufRequest->Head);
464 }
465 }
466
467 /*
468 * We don't want to block indefinitely because the client
469 * isn't receiving messages from the reply port.
470 * If we have a send-once right for the reply port, then
471 * this isn't a concern because the send won't block.
472 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
473 * To avoid falling off the kernel's fast RPC path unnecessarily,
474 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
475 */
476 if (bufReply->Head.msgh_remote_port != MACH_PORT_NULL) {
477 mr = mach_msg(&bufReply->Head,
478 (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) ==
479 MACH_MSG_TYPE_MOVE_SEND_ONCE) ?
480 MACH_SEND_MSG | options :
481 MACH_SEND_MSG | MACH_SEND_TIMEOUT | options,
482 bufReply->Head.msgh_size, 0, MACH_PORT_NULL,
483 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
484
485 if ((mr != MACH_SEND_INVALID_DEST) &&
486 (mr != MACH_SEND_TIMED_OUT)) {
487 goto done_once;
488 }
489 mr = MACH_MSG_SUCCESS;
490 }
491 if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
492 mach_msg_destroy(&bufReply->Head);
493 }
494 }
495
496 done_once:
497 voucher_mach_msg_revert(old_state);
498
499 (void)vm_deallocate(self,
500 (vm_address_t) bufRequest,
501 request_alloc);
502 (void)vm_deallocate(self,
503 (vm_address_t) bufReply,
504 reply_alloc);
505 return mr;
506 }
507
508 /*
509 * Routine: mach_msg_server
510 * Purpose:
511 * A simple generic server function. Note that changes here
512 * should be considered for duplication above.
513 */
514 mach_msg_return_t
515 mach_msg_server(
516 boolean_t (*demux)(mach_msg_header_t *, mach_msg_header_t *),
517 mach_msg_size_t max_size,
518 mach_port_t rcv_name,
519 mach_msg_options_t options)
520 {
521 mig_reply_error_t *bufRequest, *bufReply;
522 mach_msg_size_t request_size;
523 mach_msg_size_t new_request_alloc;
524 mach_msg_size_t request_alloc;
525 mach_msg_size_t trailer_alloc;
526 mach_msg_size_t reply_alloc;
527 mach_msg_return_t mr;
528 kern_return_t kr;
529 mach_port_t self = mach_task_self_;
530 voucher_mach_msg_state_t old_state = VOUCHER_MACH_MSG_STATE_UNCHANGED;
531 boolean_t buffers_swapped = FALSE;
532
533 options &= ~(MACH_SEND_MSG | MACH_RCV_MSG | MACH_RCV_VOUCHER | MACH_RCV_OVERWRITE);
534
535 reply_alloc = (mach_msg_size_t)round_page((options & MACH_SEND_TRAILER) ?
536 (max_size + MAX_TRAILER_SIZE) : max_size);
537
538 kr = vm_allocate(self,
539 (vm_address_t *)&bufReply,
540 reply_alloc,
541 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE);
542 if (kr != KERN_SUCCESS) {
543 return kr;
544 }
545
546 request_alloc = 0;
547 trailer_alloc = REQUESTED_TRAILER_SIZE(options);
548 new_request_alloc = (mach_msg_size_t)round_page(max_size + trailer_alloc);
549
550 request_size = (options & MACH_RCV_LARGE) ?
551 new_request_alloc : max_size + trailer_alloc;
552
553 for (;;) {
554 if (request_alloc < new_request_alloc) {
555 request_alloc = new_request_alloc;
556 kr = vm_allocate(self,
557 (vm_address_t *)&bufRequest,
558 request_alloc,
559 VM_MAKE_TAG(VM_MEMORY_MACH_MSG) | TRUE);
560 if (kr != KERN_SUCCESS) {
561 vm_deallocate(self,
562 (vm_address_t)bufReply,
563 reply_alloc);
564 return kr;
565 }
566 }
567
568 mr = mach_msg(&bufRequest->Head, MACH_RCV_MSG | MACH_RCV_VOUCHER | options,
569 0, request_size, rcv_name,
570 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
571
572 while (mr == MACH_MSG_SUCCESS) {
573 /* we have another request message */
574
575 buffers_swapped = FALSE;
576 old_state = voucher_mach_msg_adopt(&bufRequest->Head);
577 bufReply->Head = (mach_msg_header_t){};
578
579 (void) (*demux)(&bufRequest->Head, &bufReply->Head);
580
581 if (!(bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) {
582 if (bufReply->RetCode == MIG_NO_REPLY) {
583 bufReply->Head.msgh_remote_port = MACH_PORT_NULL;
584 } else if ((bufReply->RetCode != KERN_SUCCESS) &&
585 (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) {
586 /* destroy the request - but not the reply port */
587 bufRequest->Head.msgh_remote_port = MACH_PORT_NULL;
588 mach_msg_destroy(&bufRequest->Head);
589 }
590 }
591
592 /*
593 * We don't want to block indefinitely because the client
594 * isn't receiving messages from the reply port.
595 * If we have a send-once right for the reply port, then
596 * this isn't a concern because the send won't block.
597 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
598 * To avoid falling off the kernel's fast RPC path,
599 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
600 */
601 if (bufReply->Head.msgh_remote_port != MACH_PORT_NULL) {
602 if (request_alloc == reply_alloc) {
603 mig_reply_error_t *bufTemp;
604
605 mr = mach_msg(
606 &bufReply->Head,
607 (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) ==
608 MACH_MSG_TYPE_MOVE_SEND_ONCE) ?
609 MACH_SEND_MSG | MACH_RCV_MSG | MACH_RCV_TIMEOUT | MACH_RCV_VOUCHER | options :
610 MACH_SEND_MSG | MACH_RCV_MSG | MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT | MACH_RCV_VOUCHER | options,
611 bufReply->Head.msgh_size, request_size, rcv_name,
612 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
613
614 /* swap request and reply */
615 bufTemp = bufRequest;
616 bufRequest = bufReply;
617 bufReply = bufTemp;
618 buffers_swapped = TRUE;
619 } else {
620 mr = mach_msg_overwrite(
621 &bufReply->Head,
622 (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) ==
623 MACH_MSG_TYPE_MOVE_SEND_ONCE) ?
624 MACH_SEND_MSG | MACH_RCV_MSG | MACH_RCV_TIMEOUT | MACH_RCV_VOUCHER | options :
625 MACH_SEND_MSG | MACH_RCV_MSG | MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT | MACH_RCV_VOUCHER | options,
626 bufReply->Head.msgh_size, request_size, rcv_name,
627 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL,
628 &bufRequest->Head, 0);
629 }
630
631 if ((mr != MACH_SEND_INVALID_DEST) &&
632 (mr != MACH_SEND_TIMED_OUT) &&
633 (mr != MACH_RCV_TIMED_OUT)) {
634 voucher_mach_msg_revert(old_state);
635 old_state = VOUCHER_MACH_MSG_STATE_UNCHANGED;
636
637 continue;
638 }
639 }
640 /*
641 * Need to destroy the reply msg in case if there was a send timeout or
642 * invalid destination. The reply msg would be swapped with request msg
643 * if buffers_swapped is true, thus destroy request msg instead of
644 * reply msg in such cases.
645 */
646 if (mr != MACH_RCV_TIMED_OUT) {
647 if (buffers_swapped) {
648 if (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
649 mach_msg_destroy(&bufRequest->Head);
650 }
651 } else {
652 if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
653 mach_msg_destroy(&bufReply->Head);
654 }
655 }
656 }
657 voucher_mach_msg_revert(old_state);
658 old_state = VOUCHER_MACH_MSG_STATE_UNCHANGED;
659
660 mr = mach_msg(&bufRequest->Head, MACH_RCV_MSG | MACH_RCV_VOUCHER | options,
661 0, request_size, rcv_name,
662 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
663 } /* while (mr == MACH_MSG_SUCCESS) */
664
665 if ((mr == MACH_RCV_TOO_LARGE) && (options & MACH_RCV_LARGE)) {
666 new_request_alloc = (mach_msg_size_t)round_page(bufRequest->Head.msgh_size +
667 trailer_alloc);
668 request_size = new_request_alloc;
669 vm_deallocate(self,
670 (vm_address_t) bufRequest,
671 request_alloc);
672 continue;
673 }
674
675 break;
676 } /* for(;;) */
677
678 (void)vm_deallocate(self,
679 (vm_address_t) bufRequest,
680 request_alloc);
681 (void)vm_deallocate(self,
682 (vm_address_t) bufReply,
683 reply_alloc);
684 return mr;
685 }
686
687 /*
688 * Routine: mach_msg_server_importance
689 * Purpose:
690 * A simple generic server function which handles importance
691 * promotion assertions for adaptive daemons.
692 */
693 mach_msg_return_t
694 mach_msg_server_importance(
695 boolean_t (*demux)(mach_msg_header_t *, mach_msg_header_t *),
696 mach_msg_size_t max_size,
697 mach_port_t rcv_name,
698 mach_msg_options_t options)
699 {
700 return mach_msg_server(demux, max_size, rcv_name, options);
701 }
702
703 kern_return_t
704 mach_voucher_deallocate(
705 mach_voucher_t voucher)
706 {
707 return mach_port_deallocate(mach_task_self(), voucher);
708 }