xref: /xnu-11215/libsyscall/mach/task.c (revision 8d741a5d)
1 /*
2  * Copyright (c) 2020 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 #undef _task_user_
30 #include <TargetConditionals.h>
31 #include <stdbool.h>
32 
33 #include <mach/kern_return.h>
34 #include <mach/mach_param.h>
35 #include <mach/mach_port.h>
36 #include <mach/message.h>
37 #include <mach/mig_errors.h>
38 #include <mach/task_internal.h>
39 #include <mach/vm_map.h>
40 
41 extern mach_port_t      mach_task_self_;
42 
43 boolean_t
mach_task_is_self(task_name_t task)44 mach_task_is_self(task_name_t task)
45 {
46 	boolean_t is_self;
47 	kern_return_t kr;
48 
49 	if (task == mach_task_self_) {
50 		return TRUE;
51 	}
52 
53 	kr = _kernelrpc_mach_task_is_self(task, &is_self);
54 
55 	return kr == KERN_SUCCESS && is_self;
56 }
57 
58 
59 kern_return_t
mach_ports_register(task_t target_task,mach_port_array_t init_port_set,mach_msg_type_number_t init_port_setCnt)60 mach_ports_register(
61 	task_t                  target_task,
62 	mach_port_array_t       init_port_set,
63 	mach_msg_type_number_t  init_port_setCnt)
64 {
65 	mach_port_t array[TASK_PORT_REGISTER_MAX] = { };
66 	kern_return_t kr;
67 
68 	if (init_port_setCnt > TASK_PORT_REGISTER_MAX) {
69 		return KERN_INVALID_ARGUMENT;
70 	}
71 
72 	for (mach_msg_type_number_t i = 0; i < init_port_setCnt; i++) {
73 		array[i] = init_port_set[i];
74 	}
75 
76 	kr = _kernelrpc_mach_ports_register3(target_task, array[0], array[1], array[2]);
77 	return kr;
78 }
79 
80 kern_return_t
mach_ports_lookup(task_t target_task,mach_port_array_t * init_port_set,mach_msg_type_number_t * init_port_setCnt)81 mach_ports_lookup(
82 	task_t                  target_task,
83 	mach_port_array_t      *init_port_set,
84 	mach_msg_type_number_t *init_port_setCnt)
85 {
86 	vm_size_t size = TASK_PORT_REGISTER_MAX * sizeof(mach_port_t);
87 	mach_port_array_t array;
88 	vm_address_t addr = 0;
89 	kern_return_t kr;
90 
91 	kr = vm_allocate(target_task, &addr, size, VM_FLAGS_ANYWHERE);
92 	array = (mach_port_array_t)addr;
93 	if (kr != KERN_SUCCESS) {
94 		return kr;
95 	}
96 
97 
98 	kr = _kernelrpc_mach_ports_lookup3(target_task,
99 	    &array[0], &array[1], &array[2]);
100 	if (kr != KERN_SUCCESS) {
101 		vm_deallocate(target_task, addr, size);
102 		return kr;
103 	}
104 
105 	*init_port_set = array;
106 	*init_port_setCnt = TASK_PORT_REGISTER_MAX;
107 	return KERN_SUCCESS;
108 }
109