Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/openlaunchd
Path: blob/master/liblaunch/libbootstrap.c
374 views
1
/*
2
* Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
3
*
4
* @APPLE_APACHE_LICENSE_HEADER_START@
5
*
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
9
*
10
* http://www.apache.org/licenses/LICENSE-2.0
11
*
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
17
*
18
* @APPLE_APACHE_LICENSE_HEADER_END@
19
*/
20
21
22
/*
23
* Only Apple OSes contain `bootstrap`
24
*/
25
#ifdef __APPLE__
26
27
#include "config.h"
28
#include "launch.h"
29
#include "launch_priv.h"
30
#include "bootstrap.h"
31
#include "bootstrap_priv.h"
32
#include "vproc.h"
33
#include "vproc_priv.h"
34
35
#include <mach/mach.h>
36
#include <mach/vm_map.h>
37
#include <sys/types.h>
38
#include <sys/syslog.h>
39
#include <sys/stat.h>
40
#include <pthread.h>
41
#include <stdlib.h>
42
43
#include "job.h"
44
45
void
46
bootstrap_init(void)
47
{
48
kern_return_t kr = task_get_special_port(task_self_trap(), TASK_BOOTSTRAP_PORT, &bootstrap_port);
49
if (kr != KERN_SUCCESS) {
50
abort();
51
}
52
}
53
54
kern_return_t
55
bootstrap_create_server(mach_port_t bp, cmd_t server_cmd, uid_t server_uid, boolean_t on_demand, mach_port_t *server_port)
56
{
57
kern_return_t kr;
58
59
kr = vproc_mig_create_server(bp, server_cmd, server_uid, on_demand, server_port);
60
61
if (kr == VPROC_ERR_TRY_PER_USER) {
62
mach_port_t puc;
63
64
if (vproc_mig_lookup_per_user_context(bp, 0, &puc) == 0) {
65
kr = vproc_mig_create_server(puc, server_cmd, server_uid, on_demand, server_port);
66
mach_port_deallocate(mach_task_self(), puc);
67
}
68
}
69
70
return kr;
71
}
72
73
kern_return_t
74
bootstrap_subset(mach_port_t bp, mach_port_t requestor_port, mach_port_t *subset_port)
75
{
76
return vproc_mig_subset(bp, requestor_port, subset_port);
77
}
78
79
kern_return_t
80
bootstrap_unprivileged(mach_port_t bp, mach_port_t *unpriv_port)
81
{
82
kern_return_t kr;
83
84
*unpriv_port = MACH_PORT_NULL;
85
86
kr = mach_port_mod_refs(mach_task_self(), bp, MACH_PORT_RIGHT_SEND, 1);
87
88
if (kr == KERN_SUCCESS) {
89
*unpriv_port = bp;
90
}
91
92
return kr;
93
}
94
95
kern_return_t
96
bootstrap_parent(mach_port_t bp, mach_port_t *parent_port)
97
{
98
return vproc_mig_parent(bp, parent_port);
99
}
100
101
kern_return_t
102
bootstrap_register(mach_port_t bp, name_t service_name, mach_port_t sp)
103
{
104
return bootstrap_register2(bp, service_name, sp, 0);
105
}
106
107
kern_return_t
108
bootstrap_register2(mach_port_t bp, name_t service_name, mach_port_t sp, uint64_t flags)
109
{
110
kern_return_t kr = vproc_mig_register2(bp, service_name, sp, flags);
111
112
if (kr == VPROC_ERR_TRY_PER_USER) {
113
mach_port_t puc;
114
115
if (vproc_mig_lookup_per_user_context(bp, 0, &puc) == 0) {
116
kr = vproc_mig_register2(puc, service_name, sp, flags);
117
mach_port_deallocate(mach_task_self(), puc);
118
}
119
}
120
121
return kr;
122
}
123
124
kern_return_t
125
bootstrap_create_service(mach_port_t bp, name_t service_name, mach_port_t *sp)
126
{
127
kern_return_t kr;
128
129
if ((kr = bootstrap_check_in(bp, service_name, sp))) {
130
return kr;
131
}
132
133
if ((kr = mach_port_mod_refs(mach_task_self(), *sp, MACH_PORT_RIGHT_RECEIVE, -1))) {
134
return kr;
135
}
136
137
return bootstrap_look_up(bp, service_name, sp);
138
}
139
140
kern_return_t
141
bootstrap_check_in(mach_port_t bp, const name_t service_name, mach_port_t *sp)
142
{
143
uuid_t junk;
144
(void)bzero(junk, sizeof(junk));
145
return vproc_mig_check_in2(bp, (char *)service_name, sp, junk, 0);
146
}
147
148
kern_return_t
149
bootstrap_check_in2(mach_port_t bp, const name_t service_name, mach_port_t *sp, uint64_t flags)
150
{
151
uuid_t junk;
152
(void)bzero(junk, sizeof(junk));
153
return vproc_mig_check_in2(bp, (char *)service_name, sp, junk, flags);
154
}
155
156
kern_return_t
157
bootstrap_look_up_per_user(mach_port_t bp, const name_t service_name, uid_t target_user, mach_port_t *sp)
158
{
159
audit_token_t au_tok;
160
kern_return_t kr;
161
mach_port_t puc;
162
163
/* See rdar://problem/4890134. */
164
165
if ((kr = vproc_mig_lookup_per_user_context(bp, target_user, &puc)) != 0) {
166
return kr;
167
}
168
169
if (!service_name) {
170
*sp = puc;
171
} else {
172
uuid_t junk;
173
kr = vproc_mig_look_up2(puc, (char *)service_name, sp, &au_tok, 0, junk, 0);
174
mach_port_deallocate(mach_task_self(), puc);
175
}
176
177
return kr;
178
}
179
180
kern_return_t
181
bootstrap_lookup_children(mach_port_t bp, mach_port_array_t *children, name_array_t *names, bootstrap_property_array_t *properties, mach_msg_type_number_t *n_children)
182
{
183
mach_msg_type_number_t junk = 0;
184
return vproc_mig_lookup_children(bp, children, &junk, names, n_children, properties, &junk);
185
}
186
187
kern_return_t
188
bootstrap_look_up(mach_port_t bp, const name_t service_name, mach_port_t *sp)
189
{
190
return bootstrap_look_up2(bp, service_name, sp, 0, 0);
191
}
192
193
kern_return_t
194
bootstrap_look_up2(mach_port_t bp, const name_t service_name, mach_port_t *sp, pid_t target_pid, uint64_t flags)
195
{
196
uuid_t instance_id;
197
return bootstrap_look_up3(bp, service_name, sp, target_pid, instance_id, flags);
198
}
199
200
kern_return_t
201
bootstrap_look_up3(mach_port_t bp, const name_t service_name, mach_port_t *sp, pid_t target_pid, const uuid_t instance_id, uint64_t flags)
202
{
203
audit_token_t au_tok;
204
bool privileged_server_lookup = flags & BOOTSTRAP_PRIVILEGED_SERVER;
205
kern_return_t kr = 0;
206
mach_port_t puc;
207
208
// We have to cast instance_id here because the MIG-generated method
209
// doesn't expect a const parameter.
210
if ((kr = vproc_mig_look_up2(bp, (char *)service_name, sp, &au_tok, target_pid, (unsigned char*)instance_id, flags)) != VPROC_ERR_TRY_PER_USER) {
211
goto out;
212
}
213
214
if ((kr = vproc_mig_lookup_per_user_context(bp, 0, &puc)) != 0) {
215
goto out;
216
}
217
218
kr = vproc_mig_look_up2(puc, (char *)service_name, sp, &au_tok, target_pid, (unsigned char*)instance_id, flags);
219
mach_port_deallocate(mach_task_self(), puc);
220
221
out:
222
if ((kr == 0) && privileged_server_lookup) {
223
uid_t server_euid;
224
225
/*
226
* The audit token magic is dependent on the per-user launchd
227
* forwarding MIG requests to the root launchd when it cannot
228
* find the answer locally.
229
*/
230
231
/* This API should be in Libsystem, but is not */
232
//audit_token_to_au32(au_tok, NULL, &server_euid, NULL, NULL, NULL, NULL, NULL, NULL);
233
234
server_euid = au_tok.val[1];
235
236
if (server_euid) {
237
mach_port_deallocate(mach_task_self(), *sp);
238
*sp = MACH_PORT_NULL;
239
kr = BOOTSTRAP_NOT_PRIVILEGED;
240
}
241
}
242
243
return kr;
244
}
245
246
kern_return_t
247
bootstrap_check_in3(mach_port_t bp, const name_t service_name, mach_port_t *sp, uuid_t instance_id, uint64_t flags)
248
{
249
return vproc_mig_check_in2(bp, (char *)service_name, sp, instance_id, flags);
250
}
251
252
kern_return_t
253
bootstrap_get_root(mach_port_t bp, mach_port_t *root)
254
{
255
return vproc_mig_get_root_bootstrap(bp, root);
256
}
257
258
kern_return_t
259
bootstrap_status(mach_port_t bp, name_t service_name, bootstrap_status_t *service_active)
260
{
261
kern_return_t kr;
262
mach_port_t p;
263
264
if ((kr = bootstrap_look_up(bp, service_name, &p))) {
265
return kr;
266
}
267
268
mach_port_deallocate(mach_task_self(), p);
269
*service_active = BOOTSTRAP_STATUS_ACTIVE;
270
271
if (bootstrap_check_in(bp, service_name, &p) == BOOTSTRAP_SUCCESS) {
272
mach_port_mod_refs(mach_task_self(), p, MACH_PORT_RIGHT_RECEIVE, -1);
273
*service_active = BOOTSTRAP_STATUS_ON_DEMAND;
274
}
275
276
return BOOTSTRAP_SUCCESS;
277
}
278
279
kern_return_t
280
bootstrap_info(mach_port_t bp,
281
name_array_t *service_names, mach_msg_type_number_t *service_namesCnt,
282
name_array_t *service_jobs, mach_msg_type_number_t *service_jobsCnt,
283
bootstrap_status_array_t *service_active, mach_msg_type_number_t *service_activeCnt,
284
uint64_t flags)
285
{
286
return vproc_mig_info(bp, service_names, service_namesCnt, service_jobs, service_jobsCnt, service_active, service_activeCnt, flags);
287
}
288
289
const char *
290
bootstrap_strerror(kern_return_t r)
291
{
292
switch (r) {
293
case BOOTSTRAP_SUCCESS:
294
return "Success";
295
case BOOTSTRAP_NOT_PRIVILEGED:
296
return "Permission denied";
297
case BOOTSTRAP_NAME_IN_USE:
298
case BOOTSTRAP_SERVICE_ACTIVE:
299
return "Service name already exists";
300
case BOOTSTRAP_UNKNOWN_SERVICE:
301
return "Unknown service name";
302
case BOOTSTRAP_BAD_COUNT:
303
return "Too many lookups were requested in one request";
304
case BOOTSTRAP_NO_MEMORY:
305
return "Out of memory";
306
default:
307
return mach_error_string(r);
308
}
309
}
310
#endif
311
312