]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * Copyright (c) 1999-2007 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 | ||
62 | #define MACH_MSG_TRAP(msg, opt, ssize, rsize, rname, to, not) \ | |
63 | mach_msg_trap((msg), (opt), (ssize), (rsize), (rname), (to), (not)) | |
64 | ||
65 | #define LIBMACH_OPTIONS (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT) | |
66 | ||
67 | /* | |
68 | * Routine: mach_msg | |
69 | * Purpose: | |
70 | * Send and/or receive a message. If the message operation | |
71 | * is interrupted, and the user did not request an indication | |
72 | * of that fact, then restart the appropriate parts of the | |
73 | * operation. | |
74 | */ | |
75 | mach_msg_return_t | |
76 | mach_msg(msg, option, send_size, rcv_size, rcv_name, timeout, notify) | |
77 | mach_msg_header_t *msg; | |
78 | mach_msg_option_t option; | |
79 | mach_msg_size_t send_size; | |
80 | mach_msg_size_t rcv_size; | |
81 | mach_port_t rcv_name; | |
82 | mach_msg_timeout_t timeout; | |
83 | mach_port_t notify; | |
84 | { | |
85 | mach_msg_return_t mr; | |
86 | ||
87 | /* | |
88 | * Consider the following cases: | |
89 | * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED | |
90 | * plus special bits). | |
91 | * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options. | |
92 | * 3) RPC calls with interruptions in one/both halves. | |
93 | * | |
94 | * We refrain from passing the option bits that we implement | |
95 | * to the kernel. This prevents their presence from inhibiting | |
96 | * the kernel's fast paths (when it checks the option value). | |
97 | */ | |
98 | ||
99 | mr = MACH_MSG_TRAP(msg, option &~ LIBMACH_OPTIONS, | |
100 | send_size, rcv_size, rcv_name, | |
101 | timeout, notify); | |
102 | if (mr == MACH_MSG_SUCCESS) | |
103 | return MACH_MSG_SUCCESS; | |
104 | ||
105 | if ((option & MACH_SEND_INTERRUPT) == 0) | |
106 | while (mr == MACH_SEND_INTERRUPTED) | |
107 | mr = MACH_MSG_TRAP(msg, | |
108 | option &~ LIBMACH_OPTIONS, | |
109 | send_size, rcv_size, rcv_name, | |
110 | timeout, notify); | |
111 | ||
112 | if ((option & MACH_RCV_INTERRUPT) == 0) | |
113 | while (mr == MACH_RCV_INTERRUPTED) | |
114 | mr = MACH_MSG_TRAP(msg, | |
115 | option &~ (LIBMACH_OPTIONS|MACH_SEND_MSG), | |
116 | 0, rcv_size, rcv_name, | |
117 | timeout, notify); | |
118 | ||
119 | return mr; | |
120 | } | |
121 | ||
122 | /* | |
123 | * Routine: mach_msg_overwrite | |
124 | * Purpose: | |
125 | * Send and/or receive a message. If the message operation | |
126 | * is interrupted, and the user did not request an indication | |
127 | * of that fact, then restart the appropriate parts of the | |
128 | * operation. | |
129 | * | |
130 | * Distinct send and receive buffers may be specified. If | |
131 | * no separate receive buffer is specified, the msg parameter | |
132 | * will be used for both send and receive operations. | |
133 | * | |
134 | * In addition to a distinct receive buffer, that buffer may | |
135 | * already contain scatter control information to direct the | |
136 | * receiving of the message. | |
137 | */ | |
138 | mach_msg_return_t | |
139 | mach_msg_overwrite(msg, option, send_size, rcv_limit, rcv_name, timeout, | |
140 | notify, rcv_msg, rcv_scatter_size) | |
141 | mach_msg_header_t *msg; | |
142 | mach_msg_option_t option; | |
143 | mach_msg_size_t send_size; | |
144 | mach_msg_size_t rcv_limit; | |
145 | mach_port_t rcv_name; | |
146 | mach_msg_timeout_t timeout; | |
147 | mach_port_t notify; | |
148 | mach_msg_header_t *rcv_msg; | |
149 | mach_msg_size_t rcv_scatter_size; | |
150 | { | |
151 | mach_msg_return_t mr; | |
152 | ||
153 | /* | |
154 | * Consider the following cases: | |
155 | * 1) Errors in pseudo-receive (eg, MACH_SEND_INTERRUPTED | |
156 | * plus special bits). | |
157 | * 2) Use of MACH_SEND_INTERRUPT/MACH_RCV_INTERRUPT options. | |
158 | * 3) RPC calls with interruptions in one/both halves. | |
159 | * | |
160 | * We refrain from passing the option bits that we implement | |
161 | * to the kernel. This prevents their presence from inhibiting | |
162 | * the kernel's fast paths (when it checks the option value). | |
163 | */ | |
164 | ||
165 | mr = mach_msg_overwrite_trap(msg, option &~ LIBMACH_OPTIONS, | |
166 | send_size, rcv_limit, rcv_name, | |
167 | timeout, notify, rcv_msg, rcv_scatter_size); | |
168 | if (mr == MACH_MSG_SUCCESS) | |
169 | return MACH_MSG_SUCCESS; | |
170 | ||
171 | if ((option & MACH_SEND_INTERRUPT) == 0) | |
172 | while (mr == MACH_SEND_INTERRUPTED) | |
173 | mr = mach_msg_overwrite_trap(msg, | |
174 | option &~ LIBMACH_OPTIONS, | |
175 | send_size, rcv_limit, rcv_name, | |
176 | timeout, notify, rcv_msg, rcv_scatter_size); | |
177 | ||
178 | if ((option & MACH_RCV_INTERRUPT) == 0) | |
179 | while (mr == MACH_RCV_INTERRUPTED) | |
180 | mr = mach_msg_overwrite_trap(msg, | |
181 | option &~ (LIBMACH_OPTIONS|MACH_SEND_MSG), | |
182 | 0, rcv_limit, rcv_name, | |
183 | timeout, notify, rcv_msg, rcv_scatter_size); | |
184 | ||
185 | return mr; | |
186 | } | |
187 | ||
188 | ||
189 | mach_msg_return_t | |
190 | mach_msg_send(mach_msg_header_t *msg) | |
191 | { | |
192 | return mach_msg(msg, MACH_SEND_MSG, | |
193 | msg->msgh_size, 0, MACH_PORT_NULL, | |
194 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
195 | } | |
196 | ||
197 | mach_msg_return_t | |
198 | mach_msg_receive(mach_msg_header_t *msg) | |
199 | { | |
200 | return mach_msg(msg, MACH_RCV_MSG, | |
201 | 0, msg->msgh_size, msg->msgh_local_port, | |
202 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
203 | } | |
204 | ||
205 | ||
206 | static void | |
207 | mach_msg_destroy_port(mach_port_t port, mach_msg_type_name_t type) | |
208 | { | |
209 | if (MACH_PORT_VALID(port)) switch (type) { | |
210 | case MACH_MSG_TYPE_MOVE_SEND: | |
211 | case MACH_MSG_TYPE_MOVE_SEND_ONCE: | |
212 | /* destroy the send/send-once right */ | |
213 | (void) mach_port_deallocate(mach_task_self(), port); | |
214 | break; | |
215 | ||
216 | case MACH_MSG_TYPE_MOVE_RECEIVE: | |
217 | /* destroy the receive right */ | |
218 | (void) mach_port_mod_refs(mach_task_self(), port, | |
219 | MACH_PORT_RIGHT_RECEIVE, -1); | |
220 | break; | |
221 | ||
222 | case MACH_MSG_TYPE_MAKE_SEND: | |
223 | /* create a send right and then destroy it */ | |
224 | (void) mach_port_insert_right(mach_task_self(), port, | |
225 | port, MACH_MSG_TYPE_MAKE_SEND); | |
226 | (void) mach_port_deallocate(mach_task_self(), port); | |
227 | break; | |
228 | ||
229 | case MACH_MSG_TYPE_MAKE_SEND_ONCE: | |
230 | /* create a send-once right and then destroy it */ | |
231 | (void) mach_port_extract_right(mach_task_self(), port, | |
232 | MACH_MSG_TYPE_MAKE_SEND_ONCE, | |
233 | &port, &type); | |
234 | (void) mach_port_deallocate(mach_task_self(), port); | |
235 | break; | |
236 | } | |
237 | } | |
238 | ||
239 | static void | |
240 | mach_msg_destroy_memory(vm_offset_t addr, vm_size_t size) | |
241 | { | |
242 | if (size != 0) | |
243 | (void) vm_deallocate(mach_task_self(), addr, size); | |
244 | } | |
245 | ||
246 | ||
247 | /* | |
248 | * Routine: mach_msg_destroy | |
249 | * Purpose: | |
250 | * mach_msg_destroy is useful in two contexts. | |
251 | * | |
252 | * First, it can deallocate all port rights and | |
253 | * out-of-line memory in a received message. | |
254 | * When a server receives a request it doesn't want, | |
255 | * it needs this functionality. | |
256 | * | |
257 | * Second, it can mimic the side-effects of a msg-send | |
258 | * operation. The effect is as if the message were sent | |
259 | * and then destroyed inside the kernel. When a server | |
260 | * can't send a reply (because the client died), | |
261 | * it needs this functionality. | |
262 | */ | |
263 | void | |
264 | mach_msg_destroy(mach_msg_header_t *msg) | |
265 | { | |
266 | mach_msg_bits_t mbits = msg->msgh_bits; | |
267 | ||
268 | /* | |
269 | * The msgh_local_port field doesn't hold a port right. | |
270 | * The receive operation consumes the destination port right. | |
271 | */ | |
272 | ||
273 | mach_msg_destroy_port(msg->msgh_remote_port, MACH_MSGH_BITS_REMOTE(mbits)); | |
274 | ||
275 | if (mbits & MACH_MSGH_BITS_COMPLEX) { | |
276 | mach_msg_body_t *body; | |
277 | mach_msg_descriptor_t *saddr, *eaddr; | |
278 | ||
279 | body = (mach_msg_body_t *) (msg + 1); | |
280 | saddr = (mach_msg_descriptor_t *) | |
281 | ((mach_msg_base_t *) msg + 1); | |
282 | eaddr = saddr + body->msgh_descriptor_count; | |
283 | ||
284 | for ( ; saddr < eaddr; saddr++) { | |
285 | switch (saddr->type.type) { | |
286 | ||
287 | case MACH_MSG_PORT_DESCRIPTOR: { | |
288 | mach_msg_port_descriptor_t *dsc; | |
289 | ||
290 | /* | |
291 | * Destroy port rights carried in the message | |
292 | */ | |
293 | dsc = &saddr->port; | |
294 | mach_msg_destroy_port(dsc->name, dsc->disposition); | |
295 | break; | |
296 | } | |
297 | ||
298 | case MACH_MSG_OOL_DESCRIPTOR : { | |
299 | mach_msg_ool_descriptor_t *dsc; | |
300 | ||
301 | /* | |
302 | * Destroy memory carried in the message | |
303 | */ | |
304 | dsc = &saddr->out_of_line; | |
305 | if (dsc->deallocate) { | |
306 | mach_msg_destroy_memory((vm_offset_t)dsc->address, | |
307 | dsc->size); | |
308 | } | |
309 | break; | |
310 | } | |
311 | ||
312 | case MACH_MSG_OOL_PORTS_DESCRIPTOR : { | |
313 | mach_port_t *ports; | |
314 | mach_msg_ool_ports_descriptor_t *dsc; | |
315 | mach_msg_type_number_t j; | |
316 | ||
317 | /* | |
318 | * Destroy port rights carried in the message | |
319 | */ | |
320 | dsc = &saddr->ool_ports; | |
321 | ports = (mach_port_t *) dsc->address; | |
322 | for (j = 0; j < dsc->count; j++, ports++) { | |
323 | mach_msg_destroy_port(*ports, dsc->disposition); | |
324 | } | |
325 | ||
326 | /* | |
327 | * Destroy memory carried in the message | |
328 | */ | |
329 | if (dsc->deallocate) { | |
330 | mach_msg_destroy_memory((vm_offset_t)dsc->address, | |
331 | dsc->count * sizeof(mach_port_t)); | |
332 | } | |
333 | break; | |
334 | } | |
335 | } | |
336 | } | |
337 | } | |
338 | } | |
339 | ||
340 | /* | |
341 | * Routine: mach_msg_server_once | |
342 | * Purpose: | |
343 | * A simple generic server function. It allows more flexibility | |
344 | * than mach_msg_server by processing only one message request | |
345 | * and then returning to the user. Note that more in the way | |
346 | * of error codes are returned to the user; specifically, any | |
347 | * failing error from mach_msg calls will be returned | |
348 | * (though errors from the demux routine or the routine it | |
349 | * calls will not be). | |
350 | */ | |
351 | mach_msg_return_t | |
352 | mach_msg_server_once( | |
353 | boolean_t (*demux)(mach_msg_header_t *, mach_msg_header_t *), | |
354 | mach_msg_size_t max_size, | |
355 | mach_port_t rcv_name, | |
356 | mach_msg_options_t options) | |
357 | { | |
358 | mig_reply_error_t *bufRequest, *bufReply; | |
359 | mach_msg_size_t request_size; | |
360 | mach_msg_size_t request_alloc; | |
361 | mach_msg_size_t trailer_alloc; | |
362 | mach_msg_size_t reply_alloc; | |
363 | mach_msg_return_t mr; | |
364 | kern_return_t kr; | |
365 | mach_port_t self = mach_task_self(); | |
366 | ||
367 | options &= ~(MACH_SEND_MSG|MACH_RCV_MSG); | |
368 | ||
369 | trailer_alloc = REQUESTED_TRAILER_SIZE(options); | |
370 | request_alloc = round_page(max_size + trailer_alloc); | |
371 | ||
372 | request_size = (options & MACH_RCV_LARGE) ? | |
373 | request_alloc : max_size + trailer_alloc; | |
374 | ||
375 | reply_alloc = round_page((options & MACH_SEND_TRAILER) ? | |
376 | (max_size + MAX_TRAILER_SIZE) : | |
377 | max_size); | |
378 | ||
379 | kr = vm_allocate(self, | |
380 | (vm_address_t *)&bufReply, | |
381 | reply_alloc, | |
382 | VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|TRUE); | |
383 | if (kr != KERN_SUCCESS) | |
384 | return kr; | |
385 | ||
386 | for (;;) { | |
387 | mach_msg_size_t new_request_alloc; | |
388 | ||
389 | kr = vm_allocate(self, | |
390 | (vm_address_t *)&bufRequest, | |
391 | request_alloc, | |
392 | VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|TRUE); | |
393 | if (kr != KERN_SUCCESS) { | |
394 | vm_deallocate(self, | |
395 | (vm_address_t)bufReply, | |
396 | reply_alloc); | |
397 | return kr; | |
398 | } | |
399 | ||
400 | mr = mach_msg(&bufRequest->Head, MACH_RCV_MSG|options, | |
401 | 0, request_size, rcv_name, | |
402 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
403 | ||
404 | if (!((mr == MACH_RCV_TOO_LARGE) && (options & MACH_RCV_LARGE))) | |
405 | break; | |
406 | ||
407 | new_request_alloc = round_page(bufRequest->Head.msgh_size + | |
408 | trailer_alloc); | |
409 | vm_deallocate(self, | |
410 | (vm_address_t) bufRequest, | |
411 | request_alloc); | |
412 | request_size = request_alloc = new_request_alloc; | |
413 | } | |
414 | ||
415 | if (mr == MACH_MSG_SUCCESS) { | |
416 | /* we have a request message */ | |
417 | ||
418 | (void) (*demux)(&bufRequest->Head, &bufReply->Head); | |
419 | ||
420 | if (!(bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) { | |
421 | if (bufReply->RetCode == MIG_NO_REPLY) | |
422 | bufReply->Head.msgh_remote_port = MACH_PORT_NULL; | |
423 | else if ((bufReply->RetCode != KERN_SUCCESS) && | |
424 | (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) { | |
425 | /* destroy the request - but not the reply port */ | |
426 | bufRequest->Head.msgh_remote_port = MACH_PORT_NULL; | |
427 | mach_msg_destroy(&bufRequest->Head); | |
428 | } | |
429 | } | |
430 | ||
431 | /* | |
432 | * We don't want to block indefinitely because the client | |
433 | * isn't receiving messages from the reply port. | |
434 | * If we have a send-once right for the reply port, then | |
435 | * this isn't a concern because the send won't block. | |
436 | * If we have a send right, we need to use MACH_SEND_TIMEOUT. | |
437 | * To avoid falling off the kernel's fast RPC path unnecessarily, | |
438 | * we only supply MACH_SEND_TIMEOUT when absolutely necessary. | |
439 | */ | |
440 | if (bufReply->Head.msgh_remote_port != MACH_PORT_NULL) { | |
441 | ||
442 | mr = mach_msg(&bufReply->Head, | |
443 | (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) == | |
444 | MACH_MSG_TYPE_MOVE_SEND_ONCE) ? | |
445 | MACH_SEND_MSG|options : | |
446 | MACH_SEND_MSG|MACH_SEND_TIMEOUT|options, | |
447 | bufReply->Head.msgh_size, 0, MACH_PORT_NULL, | |
448 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
449 | ||
450 | if ((mr != MACH_SEND_INVALID_DEST) && | |
451 | (mr != MACH_SEND_TIMED_OUT)) | |
452 | goto done_once; | |
453 | mr = MACH_MSG_SUCCESS; | |
454 | } | |
455 | if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) | |
456 | mach_msg_destroy(&bufReply->Head); | |
457 | } | |
458 | ||
459 | done_once: | |
460 | (void)vm_deallocate(self, | |
461 | (vm_address_t) bufRequest, | |
462 | request_alloc); | |
463 | (void)vm_deallocate(self, | |
464 | (vm_address_t) bufReply, | |
465 | reply_alloc); | |
466 | return mr; | |
467 | } | |
468 | ||
469 | /* | |
470 | * Routine: mach_msg_server | |
471 | * Purpose: | |
472 | * A simple generic server function. Note that changes here | |
473 | * should be considered for duplication above. | |
474 | */ | |
475 | mach_msg_return_t | |
476 | mach_msg_server( | |
477 | boolean_t (*demux)(mach_msg_header_t *, mach_msg_header_t *), | |
478 | mach_msg_size_t max_size, | |
479 | mach_port_t rcv_name, | |
480 | mach_msg_options_t options) | |
481 | { | |
482 | mig_reply_error_t *bufRequest, *bufReply; | |
483 | mach_msg_size_t request_size; | |
484 | mach_msg_size_t new_request_alloc; | |
485 | mach_msg_size_t request_alloc; | |
486 | mach_msg_size_t trailer_alloc; | |
487 | mach_msg_size_t reply_alloc; | |
488 | mach_msg_return_t mr; | |
489 | kern_return_t kr; | |
490 | mach_port_t self = mach_task_self(); | |
491 | ||
492 | options &= ~(MACH_SEND_MSG|MACH_RCV_MSG|MACH_RCV_OVERWRITE); | |
493 | ||
494 | reply_alloc = round_page((options & MACH_SEND_TRAILER) ? | |
495 | (max_size + MAX_TRAILER_SIZE) : max_size); | |
496 | ||
497 | kr = vm_allocate(self, | |
498 | (vm_address_t *)&bufReply, | |
499 | reply_alloc, | |
500 | VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|TRUE); | |
501 | if (kr != KERN_SUCCESS) | |
502 | return kr; | |
503 | ||
504 | request_alloc = 0; | |
505 | trailer_alloc = REQUESTED_TRAILER_SIZE(options); | |
506 | new_request_alloc = round_page(max_size + trailer_alloc); | |
507 | ||
508 | request_size = (options & MACH_RCV_LARGE) ? | |
509 | new_request_alloc : max_size + trailer_alloc; | |
510 | ||
511 | for (;;) { | |
512 | if (request_alloc < new_request_alloc) { | |
513 | request_alloc = new_request_alloc; | |
514 | kr = vm_allocate(self, | |
515 | (vm_address_t *)&bufRequest, | |
516 | request_alloc, | |
517 | VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|TRUE); | |
518 | if (kr != KERN_SUCCESS) { | |
519 | vm_deallocate(self, | |
520 | (vm_address_t)bufReply, | |
521 | reply_alloc); | |
522 | return kr; | |
523 | } | |
524 | } | |
525 | ||
526 | mr = mach_msg(&bufRequest->Head, MACH_RCV_MSG|options, | |
527 | 0, request_size, rcv_name, | |
528 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
529 | ||
530 | while (mr == MACH_MSG_SUCCESS) { | |
531 | /* we have another request message */ | |
532 | ||
533 | (void) (*demux)(&bufRequest->Head, &bufReply->Head); | |
534 | ||
535 | if (!(bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) { | |
536 | if (bufReply->RetCode == MIG_NO_REPLY) | |
537 | bufReply->Head.msgh_remote_port = MACH_PORT_NULL; | |
538 | else if ((bufReply->RetCode != KERN_SUCCESS) && | |
539 | (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) { | |
540 | /* destroy the request - but not the reply port */ | |
541 | bufRequest->Head.msgh_remote_port = MACH_PORT_NULL; | |
542 | mach_msg_destroy(&bufRequest->Head); | |
543 | } | |
544 | } | |
545 | ||
546 | /* | |
547 | * We don't want to block indefinitely because the client | |
548 | * isn't receiving messages from the reply port. | |
549 | * If we have a send-once right for the reply port, then | |
550 | * this isn't a concern because the send won't block. | |
551 | * If we have a send right, we need to use MACH_SEND_TIMEOUT. | |
552 | * To avoid falling off the kernel's fast RPC path, | |
553 | * we only supply MACH_SEND_TIMEOUT when absolutely necessary. | |
554 | */ | |
555 | if (bufReply->Head.msgh_remote_port != MACH_PORT_NULL) { | |
556 | if (request_alloc == reply_alloc) { | |
557 | mig_reply_error_t *bufTemp; | |
558 | ||
559 | mr = mach_msg( | |
560 | &bufReply->Head, | |
561 | (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) == | |
562 | MACH_MSG_TYPE_MOVE_SEND_ONCE) ? | |
563 | MACH_SEND_MSG|MACH_RCV_MSG|options : | |
564 | MACH_SEND_MSG|MACH_RCV_MSG|MACH_SEND_TIMEOUT|options, | |
565 | bufReply->Head.msgh_size, request_size, rcv_name, | |
566 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
567 | ||
568 | /* swap request and reply */ | |
569 | bufTemp = bufRequest; | |
570 | bufRequest = bufReply; | |
571 | bufReply = bufTemp; | |
572 | ||
573 | } else { | |
574 | mr = mach_msg_overwrite( | |
575 | &bufReply->Head, | |
576 | (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) == | |
577 | MACH_MSG_TYPE_MOVE_SEND_ONCE) ? | |
578 | MACH_SEND_MSG|MACH_RCV_MSG|options : | |
579 | MACH_SEND_MSG|MACH_RCV_MSG|MACH_SEND_TIMEOUT|options, | |
580 | bufReply->Head.msgh_size, request_size, rcv_name, | |
581 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL, | |
582 | &bufRequest->Head, 0); | |
583 | } | |
584 | ||
585 | if ((mr != MACH_SEND_INVALID_DEST) && | |
586 | (mr != MACH_SEND_TIMED_OUT)) | |
587 | continue; | |
588 | } | |
589 | if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) | |
590 | mach_msg_destroy(&bufReply->Head); | |
591 | ||
592 | mr = mach_msg(&bufRequest->Head, MACH_RCV_MSG|options, | |
593 | 0, request_size, rcv_name, | |
594 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
595 | ||
596 | } /* while (mr == MACH_MSG_SUCCESS) */ | |
597 | ||
598 | if ((mr == MACH_RCV_TOO_LARGE) && (options & MACH_RCV_LARGE)) { | |
599 | new_request_alloc = round_page(bufRequest->Head.msgh_size + | |
600 | trailer_alloc); | |
601 | request_size = new_request_alloc; | |
602 | vm_deallocate(self, | |
603 | (vm_address_t) bufRequest, | |
604 | request_alloc); | |
605 | continue; | |
606 | } | |
607 | ||
608 | break; | |
609 | ||
610 | } /* for(;;) */ | |
611 | ||
612 | (void)vm_deallocate(self, | |
613 | (vm_address_t) bufRequest, | |
614 | request_alloc); | |
615 | (void)vm_deallocate(self, | |
616 | (vm_address_t) bufReply, | |
617 | reply_alloc); | |
618 | return mr; | |
619 | } |