]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/mach_msg.c
xnu-3248.40.184.tar.gz
[apple/xnu.git] / osfmk / ipc / mach_msg.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
2d21ac55
A
56/*
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 * Copyright (c) 2005 SPARTA, Inc.
62 */
1c79356b
A
63/*
64 */
65/*
66 * File: ipc/mach_msg.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Exported message traps. See mach/message.h.
71 */
72
91447636 73#include <mach/mach_types.h>
1c79356b
A
74#include <mach/kern_return.h>
75#include <mach/port.h>
76#include <mach/message.h>
77#include <mach/mig_errors.h>
91447636
A
78#include <mach/mach_traps.h>
79
80#include <kern/kern_types.h>
1c79356b
A
81#include <kern/assert.h>
82#include <kern/counters.h>
83#include <kern/cpu_number.h>
91447636
A
84#include <kern/ipc_kobject.h>
85#include <kern/ipc_mig.h>
1c79356b
A
86#include <kern/task.h>
87#include <kern/thread.h>
1c79356b
A
88#include <kern/sched_prim.h>
89#include <kern/exception.h>
90#include <kern/misc_protos.h>
91447636
A
91#include <kern/kalloc.h>
92#include <kern/processor.h>
93#include <kern/syscall_subr.h>
94
1c79356b 95#include <vm/vm_map.h>
91447636
A
96
97#include <ipc/ipc_types.h>
1c79356b
A
98#include <ipc/ipc_kmsg.h>
99#include <ipc/ipc_mqueue.h>
100#include <ipc/ipc_object.h>
101#include <ipc/ipc_notify.h>
102#include <ipc/ipc_port.h>
103#include <ipc/ipc_pset.h>
104#include <ipc/ipc_space.h>
105#include <ipc/ipc_entry.h>
fe8ab488 106#include <ipc/ipc_importance.h>
490019cf 107#include <ipc/ipc_voucher.h>
0b4e3aa0 108
1c79356b 109#include <machine/machine_routines.h>
2d21ac55 110#include <security/mac_mach_internal.h>
91447636 111
1c79356b
A
112#include <sys/kdebug.h>
113
91447636
A
114#ifndef offsetof
115#define offsetof(type, member) ((size_t)(&((type *)0)->member))
116#endif /* offsetof */
117
1c79356b 118/*
91447636 119 * Forward declarations - kernel internal routines
1c79356b
A
120 */
121
122mach_msg_return_t mach_msg_send(
123 mach_msg_header_t *msg,
124 mach_msg_option_t option,
125 mach_msg_size_t send_size,
91447636 126 mach_msg_timeout_t send_timeout,
1c79356b
A
127 mach_port_name_t notify);
128
129mach_msg_return_t mach_msg_receive(
130 mach_msg_header_t *msg,
131 mach_msg_option_t option,
132 mach_msg_size_t rcv_size,
133 mach_port_name_t rcv_name,
91447636 134 mach_msg_timeout_t rcv_timeout,
1c79356b
A
135 void (*continuation)(mach_msg_return_t),
136 mach_msg_size_t slist_size);
137
138
139mach_msg_return_t msg_receive_error(
140 ipc_kmsg_t kmsg,
91447636 141 mach_vm_address_t msg_addr,
1c79356b
A
142 mach_msg_option_t option,
143 mach_port_seqno_t seqno,
144 ipc_space_t space);
145
1c79356b 146security_token_t KERNEL_SECURITY_TOKEN = KERNEL_SECURITY_TOKEN_VALUE;
55e303ae 147audit_token_t KERNEL_AUDIT_TOKEN = KERNEL_AUDIT_TOKEN_VALUE;
1c79356b
A
148
149mach_msg_format_0_trailer_t trailer_template = {
150 /* mach_msg_trailer_type_t */ MACH_MSG_TRAILER_FORMAT_0,
151 /* mach_msg_trailer_size_t */ MACH_MSG_TRAILER_MINIMUM_SIZE,
152 /* mach_port_seqno_t */ 0,
153 /* security_token_t */ KERNEL_SECURITY_TOKEN_VALUE
154};
155
156/*
39236c6e 157 * Routine: mach_msg_send [Kernel Internal]
1c79356b 158 * Purpose:
39236c6e
A
159 * Routine for kernel-task threads to send a message.
160 *
161 * Unlike mach_msg_send_from_kernel(), this routine
162 * looks port names up in the kernel's port namespace
163 * and copies in the kernel virtual memory (instead
164 * of taking a vm_map_copy_t pointer for OOL descriptors).
1c79356b
A
165 * Conditions:
166 * Nothing locked.
167 * Returns:
168 * MACH_MSG_SUCCESS Sent the message.
169 * MACH_SEND_MSG_TOO_SMALL Message smaller than a header.
170 * MACH_SEND_NO_BUFFER Couldn't allocate buffer.
171 * MACH_SEND_INVALID_DATA Couldn't copy message data.
172 * MACH_SEND_INVALID_HEADER
173 * Illegal value in the message header bits.
174 * MACH_SEND_INVALID_DEST The space is dead.
175 * MACH_SEND_INVALID_NOTIFY Bad notify port.
176 * MACH_SEND_INVALID_DEST Can't copyin destination port.
177 * MACH_SEND_INVALID_REPLY Can't copyin reply port.
178 * MACH_SEND_TIMED_OUT Timeout expired without delivery.
179 * MACH_SEND_INTERRUPTED Delivery interrupted.
1c79356b
A
180 */
181
182mach_msg_return_t
183mach_msg_send(
184 mach_msg_header_t *msg,
185 mach_msg_option_t option,
186 mach_msg_size_t send_size,
91447636 187 mach_msg_timeout_t send_timeout,
6d2010ae 188 __unused mach_port_name_t notify)
1c79356b
A
189{
190 ipc_space_t space = current_space();
191 vm_map_t map = current_map();
192 ipc_kmsg_t kmsg;
193 mach_msg_return_t mr;
91447636
A
194 mach_msg_size_t msg_and_trailer_size;
195 mach_msg_max_trailer_t *trailer;
1c79356b 196
91447636
A
197 if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3))
198 return MACH_SEND_MSG_TOO_SMALL;
1c79356b 199
8ad349bb
A
200 if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE)
201 return MACH_SEND_TOO_LARGE;
202
91447636
A
203 msg_and_trailer_size = send_size + MAX_TRAILER_SIZE;
204
205 kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
206
207 if (kmsg == IKM_NULL)
208 return MACH_SEND_NO_BUFFER;
209
210 (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size);
1c79356b 211
91447636
A
212 kmsg->ikm_header->msgh_size = send_size;
213
214 /*
215 * reserve for the trailer the largest space (MAX_TRAILER_SIZE)
216 * However, the internal size field of the trailer (msgh_trailer_size)
217 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
218 * the cases where no implicit data is requested.
219 */
220 trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size);
221 trailer->msgh_sender = current_thread()->task->sec_token;
222 trailer->msgh_audit = current_thread()->task->audit_token;
223 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
224 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
39236c6e
A
225
226 mr = ipc_kmsg_copyin(kmsg, space, map, &option);
227
1c79356b
A
228 if (mr != MACH_MSG_SUCCESS) {
229 ipc_kmsg_free(kmsg);
230 return mr;
231 }
232
39236c6e
A
233 mr = ipc_kmsg_send(kmsg, option, send_timeout);
234
1c79356b
A
235 if (mr != MACH_MSG_SUCCESS) {
236 mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL);
91447636
A
237 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header,
238 kmsg->ikm_header->msgh_size);
239 ipc_kmsg_free(kmsg);
1c79356b
A
240 }
241
242 return mr;
243}
244
39236c6e
A
245/*
246 * message header as seen at user-space
247 * (for MACH_RCV_LARGE/IDENTITY updating)
248 */
249typedef struct
250{
251 mach_msg_bits_t msgh_bits;
252 mach_msg_size_t msgh_size;
253 mach_port_name_t msgh_remote_port;
254 mach_port_name_t msgh_local_port;
255 mach_msg_size_t msgh_reserved;
256 mach_msg_id_t msgh_id;
257} mach_msg_user_header_t;
258
1c79356b 259/*
6d2010ae 260 * Routine: mach_msg_receive_results
1c79356b
A
261 * Purpose:
262 * Receive a message.
263 * Conditions:
264 * Nothing locked.
265 * Returns:
266 * MACH_MSG_SUCCESS Received a message.
267 * MACH_RCV_INVALID_NAME The name doesn't denote a right,
268 * or the denoted right is not receive or port set.
269 * MACH_RCV_IN_SET Receive right is a member of a set.
270 * MACH_RCV_TOO_LARGE Message wouldn't fit into buffer.
271 * MACH_RCV_TIMED_OUT Timeout expired without a message.
272 * MACH_RCV_INTERRUPTED Reception interrupted.
273 * MACH_RCV_PORT_DIED Port/set died while receiving.
274 * MACH_RCV_PORT_CHANGED Port moved into set while receiving.
275 * MACH_RCV_INVALID_DATA Couldn't copy to user buffer.
276 * MACH_RCV_INVALID_NOTIFY Bad notify port.
277 * MACH_RCV_HEADER_ERROR
278 */
279
280mach_msg_return_t
281mach_msg_receive_results(void)
282{
283 thread_t self = current_thread();
284 ipc_space_t space = current_space();
316670eb 285 vm_map_t map = current_map();
1c79356b
A
286
287 ipc_object_t object = self->ith_object;
288 mach_msg_return_t mr = self->ith_state;
91447636 289 mach_vm_address_t msg_addr = self->ith_msg_addr;
1c79356b
A
290 mach_msg_option_t option = self->ith_option;
291 ipc_kmsg_t kmsg = self->ith_kmsg;
292 mach_port_seqno_t seqno = self->ith_seqno;
316670eb 293 mach_msg_trailer_size_t trailer_size;
1c79356b 294
316670eb 295 io_release(object);
1c79356b
A
296
297 if (mr != MACH_MSG_SUCCESS) {
298
299 if (mr == MACH_RCV_TOO_LARGE ) {
300 if (option & MACH_RCV_LARGE) {
301 /*
302 * We need to inform the user-level code that it needs more
303 * space. The value for how much space was returned in the
304 * msize save area instead of the message (which was left on
305 * the queue).
306 */
39236c6e
A
307 if (option & MACH_RCV_LARGE_IDENTITY) {
308 if (copyout((char *) &self->ith_receiver_name,
309 msg_addr + offsetof(mach_msg_user_header_t, msgh_local_port),
310 sizeof(mach_port_name_t)))
311 mr = MACH_RCV_INVALID_DATA;
312 }
1c79356b 313 if (copyout((char *) &self->ith_msize,
39236c6e 314 msg_addr + offsetof(mach_msg_user_header_t, msgh_size),
1c79356b 315 sizeof(mach_msg_size_t)))
fe8ab488
A
316 mr = MACH_RCV_INVALID_DATA;
317 } else {
318
319 /* discard importance in message */
320 ipc_importance_clean(kmsg);
321
322 if (msg_receive_error(kmsg, msg_addr, option, seqno, space)
323 == MACH_RCV_INVALID_DATA)
324 mr = MACH_RCV_INVALID_DATA;
1c79356b 325 }
1c79356b 326 }
fe8ab488 327 return mr;
1c79356b
A
328 }
329
39236c6e 330#if IMPORTANCE_INHERITANCE
39236c6e 331
fe8ab488
A
332 /* adopt/transform any importance attributes carried in the message */
333 ipc_importance_receive(kmsg, option);
334
39236c6e
A
335#endif /* IMPORTANCE_INHERITANCE */
336
490019cf
A
337 /* auto redeem the voucher in the message */
338 ipc_voucher_receive_postprocessing(kmsg, option);
339
316670eb
A
340 trailer_size = ipc_kmsg_add_trailer(kmsg, space, option, self, seqno, FALSE,
341 kmsg->ikm_header->msgh_remote_port->ip_context);
3e170ce0 342 mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL, option);
1c79356b
A
343
344 if (mr != MACH_MSG_SUCCESS) {
fe8ab488
A
345 /* already received importance, so have to undo that here */
346 ipc_importance_unreceive(kmsg, option);
347
1c79356b 348 if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
91447636 349 if (ipc_kmsg_put(msg_addr, kmsg, kmsg->ikm_header->msgh_size +
316670eb 350 trailer_size) == MACH_RCV_INVALID_DATA)
1c79356b
A
351 mr = MACH_RCV_INVALID_DATA;
352 }
353 else {
91447636 354 if (msg_receive_error(kmsg, msg_addr, option, seqno, space)
1c79356b
A
355 == MACH_RCV_INVALID_DATA)
356 mr = MACH_RCV_INVALID_DATA;
357 }
fe8ab488
A
358 } else {
359 mr = ipc_kmsg_put(msg_addr,
360 kmsg,
361 kmsg->ikm_header->msgh_size +
362 trailer_size);
1c79356b 363 }
fe8ab488 364
1c79356b
A
365 return mr;
366}
367
39236c6e
A
368/*
369 * Routine: mach_msg_receive [Kernel Internal]
370 * Purpose:
371 * Routine for kernel-task threads to actively receive a message.
372 *
373 * Unlike being dispatched to by ipc_kobject_server() or the
374 * reply part of mach_msg_rpc_from_kernel(), this routine
375 * looks up the receive port name in the kernel's port
376 * namespace and copies out received port rights to that namespace
377 * as well. Out-of-line memory is copied out the kernel's
378 * address space (rather than just providing the vm_map_copy_t).
379 * Conditions:
380 * Nothing locked.
381 * Returns:
382 * MACH_MSG_SUCCESS Received a message.
383 * See <mach/message.h> for list of MACH_RCV_XXX errors.
384 */
1c79356b
A
385mach_msg_return_t
386mach_msg_receive(
387 mach_msg_header_t *msg,
388 mach_msg_option_t option,
389 mach_msg_size_t rcv_size,
390 mach_port_name_t rcv_name,
91447636 391 mach_msg_timeout_t rcv_timeout,
1c79356b 392 void (*continuation)(mach_msg_return_t),
3e170ce0 393 __unused mach_msg_size_t slist_size)
1c79356b
A
394{
395 thread_t self = current_thread();
396 ipc_space_t space = current_space();
1c79356b
A
397 ipc_object_t object;
398 ipc_mqueue_t mqueue;
1c79356b 399 mach_msg_return_t mr;
1c79356b
A
400
401 mr = ipc_mqueue_copyin(space, rcv_name, &mqueue, &object);
402 if (mr != MACH_MSG_SUCCESS) {
403 return mr;
404 }
405 /* hold ref for object */
406
91447636 407 self->ith_msg_addr = CAST_DOWN(mach_vm_address_t, msg);
1c79356b
A
408 self->ith_object = object;
409 self->ith_msize = rcv_size;
410 self->ith_option = option;
1c79356b
A
411 self->ith_continuation = continuation;
412
91447636
A
413 ipc_mqueue_receive(mqueue, option, rcv_size, rcv_timeout, THREAD_ABORTSAFE);
414 if ((option & MACH_RCV_TIMEOUT) && rcv_timeout == 0)
415 thread_poll_yield(self);
1c79356b
A
416 return mach_msg_receive_results();
417}
418
419void
420mach_msg_receive_continue(void)
421{
422 thread_t self = current_thread();
423
424 (*self->ith_continuation)(mach_msg_receive_results());
425}
426
1c79356b
A
427
428/*
429 * Routine: mach_msg_overwrite_trap [mach trap]
430 * Purpose:
431 * Possibly send a message; possibly receive a message.
432 * Conditions:
433 * Nothing locked.
434 * Returns:
435 * All of mach_msg_send and mach_msg_receive error codes.
436 */
437
438mach_msg_return_t
439mach_msg_overwrite_trap(
91447636 440 struct mach_msg_overwrite_trap_args *args)
1c79356b 441{
91447636
A
442 mach_vm_address_t msg_addr = args->msg;
443 mach_msg_option_t option = args->option;
444 mach_msg_size_t send_size = args->send_size;
445 mach_msg_size_t rcv_size = args->rcv_size;
446 mach_port_name_t rcv_name = args->rcv_name;
447 mach_msg_timeout_t msg_timeout = args->timeout;
6d2010ae 448 __unused mach_port_name_t notify = args->notify;
91447636 449 mach_vm_address_t rcv_msg_addr = args->rcv_msg;
2d21ac55 450 __unused mach_port_seqno_t temp_seqno = 0;
91447636 451
1c79356b 452 mach_msg_return_t mr = MACH_MSG_SUCCESS;
b0d623f7 453 vm_map_t map = current_map();
39236c6e
A
454
455 /* Only accept options allowed by the user */
456 option &= MACH_MSG_OPTION_USER;
457
1c79356b 458 if (option & MACH_SEND_MSG) {
91447636 459 ipc_space_t space = current_space();
91447636
A
460 ipc_kmsg_t kmsg;
461
462 mr = ipc_kmsg_get(msg_addr, send_size, &kmsg);
463
464 if (mr != MACH_MSG_SUCCESS)
465 return mr;
466
39236c6e
A
467 mr = ipc_kmsg_copyin(kmsg, space, map, &option);
468
1c79356b 469 if (mr != MACH_MSG_SUCCESS) {
91447636 470 ipc_kmsg_free(kmsg);
1c79356b
A
471 return mr;
472 }
91447636 473
39236c6e 474 mr = ipc_kmsg_send(kmsg, option, msg_timeout);
91447636
A
475
476 if (mr != MACH_MSG_SUCCESS) {
477 mr |= ipc_kmsg_copyout_pseudo(kmsg, space, map, MACH_MSG_BODY_NULL);
478 (void) ipc_kmsg_put(msg_addr, kmsg, kmsg->ikm_header->msgh_size);
479 return mr;
480 }
481
1c79356b
A
482 }
483
484 if (option & MACH_RCV_MSG) {
91447636
A
485 thread_t self = current_thread();
486 ipc_space_t space = current_space();
487 ipc_object_t object;
488 ipc_mqueue_t mqueue;
489
490 mr = ipc_mqueue_copyin(space, rcv_name, &mqueue, &object);
491 if (mr != MACH_MSG_SUCCESS) {
492 return mr;
493 }
494 /* hold ref for object */
1c79356b 495
3e170ce0 496 if (rcv_msg_addr != (mach_vm_address_t)0)
91447636 497 self->ith_msg_addr = rcv_msg_addr;
1c79356b 498 else
91447636
A
499 self->ith_msg_addr = msg_addr;
500 self->ith_object = object;
501 self->ith_msize = rcv_size;
502 self->ith_option = option;
b0d623f7 503 self->ith_receiver_name = MACH_PORT_NULL;
91447636
A
504 self->ith_continuation = thread_syscall_return;
505
506 ipc_mqueue_receive(mqueue, option, rcv_size, msg_timeout, THREAD_ABORTSAFE);
507 if ((option & MACH_RCV_TIMEOUT) && msg_timeout == 0)
508 thread_poll_yield(self);
509 return mach_msg_receive_results();
1c79356b
A
510 }
511
512 return MACH_MSG_SUCCESS;
513}
514
9bccf70c
A
515/*
516 * Routine: mach_msg_trap [mach trap]
517 * Purpose:
518 * Possibly send a message; possibly receive a message.
519 * Conditions:
520 * Nothing locked.
521 * Returns:
522 * All of mach_msg_send and mach_msg_receive error codes.
523 */
524
525mach_msg_return_t
526mach_msg_trap(
91447636 527 struct mach_msg_overwrite_trap_args *args)
9bccf70c 528{
91447636
A
529 kern_return_t kr;
530 args->rcv_msg = (mach_vm_address_t)0;
531
532 kr = mach_msg_overwrite_trap(args);
533 return kr;
9bccf70c
A
534}
535
536
1c79356b
A
537/*
538 * Routine: msg_receive_error [internal]
539 * Purpose:
540 * Builds a minimal header/trailer and copies it to
541 * the user message buffer. Invoked when in the case of a
542 * MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error.
543 * Conditions:
544 * Nothing locked.
545 * Returns:
546 * MACH_MSG_SUCCESS minimal header/trailer copied
547 * MACH_RCV_INVALID_DATA copyout to user buffer failed
548 */
549
550mach_msg_return_t
551msg_receive_error(
552 ipc_kmsg_t kmsg,
91447636 553 mach_vm_address_t msg_addr,
1c79356b
A
554 mach_msg_option_t option,
555 mach_port_seqno_t seqno,
556 ipc_space_t space)
557{
b0d623f7 558 mach_vm_address_t context;
316670eb
A
559 mach_msg_trailer_size_t trailer_size;
560 mach_msg_max_trailer_t *trailer;
b0d623f7
A
561
562 context = kmsg->ikm_header->msgh_remote_port->ip_context;
1c79356b
A
563
564 /*
565 * Copy out the destination port in the message.
566 * Destroy all other rights and memory in the message.
567 */
568 ipc_kmsg_copyout_dest(kmsg, space);
569
570 /*
571 * Build a minimal message with the requested trailer.
572 */
b0d623f7 573 trailer = (mach_msg_max_trailer_t *)
91447636 574 ((vm_offset_t)kmsg->ikm_header +
1c79356b 575 round_msg(sizeof(mach_msg_header_t)));
91447636 576 kmsg->ikm_header->msgh_size = sizeof(mach_msg_header_t);
1c79356b
A
577 bcopy( (char *)&trailer_template,
578 (char *)trailer,
579 sizeof(trailer_template));
316670eb
A
580
581 trailer_size = ipc_kmsg_add_trailer(kmsg, space,
582 option, current_thread(), seqno,
583 TRUE, context);
1c79356b
A
584
585 /*
586 * Copy the message to user space
587 */
91447636 588 if (ipc_kmsg_put(msg_addr, kmsg, kmsg->ikm_header->msgh_size +
316670eb 589 trailer_size) == MACH_RCV_INVALID_DATA)
1c79356b
A
590 return(MACH_RCV_INVALID_DATA);
591 else
592 return(MACH_MSG_SUCCESS);
593}