]>
Commit | Line | Data |
---|---|---|
e91b9f68 | 1 | /* |
95379394 | 2 | * Copyright (c) 2005-2012 Apple Inc. All rights reserved. |
e91b9f68 | 3 | * |
ed34e3c3 | 4 | * @APPLE_APACHE_LICENSE_HEADER_START@ |
e91b9f68 | 5 | * |
ed34e3c3 A |
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 | |
e91b9f68 | 9 | * |
ed34e3c3 A |
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 | |
e91b9f68 A |
16 | * limitations under the License. |
17 | * | |
ed34e3c3 | 18 | * @APPLE_APACHE_LICENSE_HEADER_END@ |
e91b9f68 | 19 | */ |
5b0a4722 A |
20 | |
21 | #include "config.h" | |
ef398931 A |
22 | #include "launch.h" |
23 | #include "launch_priv.h" | |
24 | #include "launch_internal.h" | |
eabd1701 | 25 | #include "ktrace.h" |
5b0a4722 | 26 | |
ed34e3c3 | 27 | #include <mach/mach.h> |
aa59983a | 28 | #include <libkern/OSByteOrder.h> |
e91b9f68 A |
29 | #include <sys/types.h> |
30 | #include <sys/socket.h> | |
31 | #include <sys/fcntl.h> | |
32 | #include <sys/un.h> | |
33 | #include <sys/uio.h> | |
ed34e3c3 | 34 | #include <sys/stat.h> |
e91b9f68 A |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
38 | #include <pthread.h> | |
39 | #include <unistd.h> | |
40 | #include <errno.h> | |
ed34e3c3 | 41 | #include <pwd.h> |
5b0a4722 | 42 | #include <assert.h> |
ddbbfbc1 A |
43 | #include <uuid/uuid.h> |
44 | #include <sys/syscall.h> | |
dcace88f | 45 | #include <dlfcn.h> |
ddbbfbc1 A |
46 | |
47 | #ifdef __LP64__ | |
48 | /* workaround: 5723161 */ | |
49 | #ifndef __DARWIN_ALIGN32 | |
50 | #define __DARWIN_ALIGN32(x) (((size_t)(x) + 3) & ~3) | |
51 | #endif | |
52 | #undef CMSG_DATA | |
53 | #define CMSG_DATA(cmsg) \ | |
54 | ((uint8_t *)(cmsg) + __DARWIN_ALIGN32(sizeof(struct cmsghdr))) | |
55 | #undef CMSG_SPACE | |
56 | #define CMSG_SPACE(l) \ | |
57 | (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l)) | |
58 | #undef CMSG_LEN | |
59 | #define CMSG_LEN(l) \ | |
60 | (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l)) | |
61 | #endif | |
e91b9f68 | 62 | |
eabd1701 A |
63 | struct _launch_data { |
64 | uint64_t type; | |
65 | union { | |
66 | struct { | |
67 | union { | |
68 | launch_data_t *_array; | |
69 | char *string; | |
70 | void *opaque; | |
71 | int64_t __junk; | |
72 | }; | |
73 | union { | |
74 | uint64_t _array_cnt; | |
75 | uint64_t string_len; | |
76 | uint64_t opaque_size; | |
77 | }; | |
78 | }; | |
79 | int64_t fd; | |
80 | uint64_t mp; | |
81 | uint64_t err; | |
82 | int64_t number; | |
83 | uint64_t boolean; /* We'd use 'bool' but this struct needs to be used under Rosetta, and sizeof(bool) is different between PowerPC and Intel */ | |
84 | double float_num; | |
85 | }; | |
86 | }; | |
87 | ||
ef398931 A |
88 | #include "bootstrap.h" |
89 | #include "vproc.h" | |
90 | #include "vproc_priv.h" | |
91 | #include "vproc_internal.h" | |
e91b9f68 | 92 | |
aa59983a A |
93 | /* __OSBogusByteSwap__() must not really exist in the symbol namespace |
94 | * in order for the following to generate an error at build time. | |
95 | */ | |
96 | extern void __OSBogusByteSwap__(void); | |
97 | ||
ddbbfbc1 | 98 | #define host2wire(x) \ |
aa59983a A |
99 | ({ typeof (x) _X, _x = (x); \ |
100 | switch (sizeof(_x)) { \ | |
101 | case 8: \ | |
ddbbfbc1 | 102 | _X = OSSwapHostToLittleInt64(_x); \ |
aa59983a A |
103 | break; \ |
104 | case 4: \ | |
ddbbfbc1 | 105 | _X = OSSwapHostToLittleInt32(_x); \ |
aa59983a A |
106 | break; \ |
107 | case 2: \ | |
ddbbfbc1 | 108 | _X = OSSwapHostToLittleInt16(_x); \ |
aa59983a A |
109 | break; \ |
110 | case 1: \ | |
111 | _X = _x; \ | |
112 | break; \ | |
113 | default: \ | |
114 | __OSBogusByteSwap__(); \ | |
115 | break; \ | |
116 | } \ | |
117 | _X; \ | |
118 | }) | |
119 | ||
120 | ||
ddbbfbc1 | 121 | #define big2wire(x) \ |
aa59983a A |
122 | ({ typeof (x) _X, _x = (x); \ |
123 | switch (sizeof(_x)) { \ | |
124 | case 8: \ | |
ddbbfbc1 | 125 | _X = OSSwapLittleToHostInt64(_x); \ |
aa59983a A |
126 | break; \ |
127 | case 4: \ | |
ddbbfbc1 | 128 | _X = OSSwapLittleToHostInt32(_x); \ |
aa59983a A |
129 | break; \ |
130 | case 2: \ | |
ddbbfbc1 | 131 | _X = OSSwapLittleToHostInt16(_x); \ |
aa59983a A |
132 | break; \ |
133 | case 1: \ | |
134 | _X = _x; \ | |
135 | break; \ | |
136 | default: \ | |
137 | __OSBogusByteSwap__(); \ | |
138 | break; \ | |
139 | } \ | |
140 | _X; \ | |
141 | }) | |
142 | ||
eabd1701 A |
143 | union _launch_double_u { |
144 | uint64_t iv; | |
145 | double dv; | |
146 | }; | |
147 | ||
148 | #define host2wire_f(x) ({ \ | |
149 | typeof(x) _F, _f = (x); \ | |
150 | union _launch_double_u s; \ | |
151 | s.dv = _f; \ | |
152 | s.iv = host2wire(s.iv); \ | |
153 | _F = s.dv; \ | |
154 | _F; \ | |
155 | }) | |
156 | ||
157 | #define big2wire_f(x) ({ \ | |
158 | typeof(x) _F, _f = (x); \ | |
159 | union _launch_double_u s; \ | |
160 | s.dv = _f; \ | |
161 | s.iv = big2wire(s.iv); \ | |
162 | _F = s.dv; \ | |
163 | _F; \ | |
164 | }) | |
165 | ||
aa59983a A |
166 | |
167 | struct launch_msg_header { | |
168 | uint64_t magic; | |
169 | uint64_t len; | |
170 | }; | |
171 | ||
172 | #define LAUNCH_MSG_HEADER_MAGIC 0xD2FEA02366B39A41ull | |
173 | ||
ddbbfbc1 A |
174 | enum { |
175 | LAUNCHD_USE_CHECKIN_FD, | |
176 | LAUNCHD_USE_OTHER_FD, | |
177 | }; | |
e91b9f68 A |
178 | struct _launch { |
179 | void *sendbuf; | |
ed34e3c3 | 180 | int *sendfds; |
e91b9f68 | 181 | void *recvbuf; |
ed34e3c3 A |
182 | int *recvfds; |
183 | size_t sendlen; | |
184 | size_t sendfdcnt; | |
185 | size_t recvlen; | |
186 | size_t recvfdcnt; | |
ddbbfbc1 A |
187 | int which; |
188 | int cifd; | |
ed34e3c3 | 189 | int fd; |
e91b9f68 A |
190 | }; |
191 | ||
ed34e3c3 | 192 | static launch_data_t launch_data_array_pop_first(launch_data_t where); |
e91b9f68 | 193 | static int _fd(int fd); |
ed34e3c3 A |
194 | static void launch_client_init(void); |
195 | static void launch_msg_getmsgs(launch_data_t m, void *context); | |
196 | static launch_data_t launch_msg_internal(launch_data_t d); | |
197 | static void launch_mach_checkin_service(launch_data_t obj, const char *key, void *context); | |
e91b9f68 | 198 | |
95379394 A |
199 | void |
200 | _launch_init_globals(launch_globals_t globals) | |
201 | { | |
202 | pthread_once_t once = PTHREAD_ONCE_INIT; | |
203 | globals->lc_once = once; | |
204 | pthread_mutex_init(&globals->lc_mtx, NULL); | |
205 | } | |
e91b9f68 | 206 | |
95379394 A |
207 | #if !_LIBLAUNCH_HAS_ALLOC_ONCE |
208 | launch_globals_t __launch_globals; | |
209 | ||
210 | void | |
211 | _launch_globals_init(void) | |
212 | { | |
213 | __launch_globals = calloc(1, sizeof(struct launch_globals_s)); | |
214 | _launch_init_globals(__launch_globals); | |
215 | } | |
ddbbfbc1 | 216 | |
95379394 A |
217 | launch_globals_t |
218 | _launch_globals_impl(void) | |
219 | { | |
220 | static pthread_once_t once = PTHREAD_ONCE_INIT; | |
221 | pthread_once(&once, &_launch_globals_init); | |
222 | return __launch_globals; | |
223 | } | |
224 | #endif | |
e91b9f68 | 225 | |
ed34e3c3 A |
226 | void |
227 | launch_client_init(void) | |
e91b9f68 A |
228 | { |
229 | struct sockaddr_un sun; | |
230 | char *where = getenv(LAUNCHD_SOCKET_ENV); | |
231 | char *_launchd_fd = getenv(LAUNCHD_TRUSTED_FD_ENV); | |
ddbbfbc1 | 232 | int dfd, lfd = -1, cifd = -1; |
5b0a4722 | 233 | name_t spath; |
eabd1701 | 234 | |
e91b9f68 | 235 | if (_launchd_fd) { |
ddbbfbc1 A |
236 | cifd = strtol(_launchd_fd, NULL, 10); |
237 | if ((dfd = dup(cifd)) >= 0) { | |
ed34e3c3 | 238 | close(dfd); |
ddbbfbc1 | 239 | _fd(cifd); |
e91b9f68 | 240 | } else { |
ddbbfbc1 | 241 | cifd = -1; |
e91b9f68 A |
242 | } |
243 | unsetenv(LAUNCHD_TRUSTED_FD_ENV); | |
244 | } | |
eabd1701 | 245 | |
ddbbfbc1 A |
246 | memset(&sun, 0, sizeof(sun)); |
247 | sun.sun_family = AF_UNIX; | |
eabd1701 | 248 | |
ddbbfbc1 A |
249 | /* The rules are as follows. |
250 | * - All users (including root) talk to their per-user launchd's by default. | |
251 | * - If we have been invoked under sudo, talk to the system launchd. | |
252 | * - If we're the root user and the __USE_SYSTEM_LAUNCHD environment variable is set, then | |
253 | * talk to the system launchd. | |
254 | */ | |
255 | if (where && where[0] != '\0') { | |
256 | strncpy(sun.sun_path, where, sizeof(sun.sun_path)); | |
257 | } else { | |
dcace88f A |
258 | if (_vprocmgr_getsocket(spath) == 0) { |
259 | if ((getenv("SUDO_COMMAND") || getenv("__USE_SYSTEM_LAUNCHD")) && geteuid() == 0) { | |
ddbbfbc1 A |
260 | /* Talk to the system launchd. */ |
261 | strncpy(sun.sun_path, LAUNCHD_SOCK_PREFIX "/sock", sizeof(sun.sun_path)); | |
262 | } else { | |
263 | /* Talk to our per-user launchd. */ | |
264 | size_t min_len; | |
eabd1701 | 265 | |
ddbbfbc1 | 266 | min_len = sizeof(sun.sun_path) < sizeof(spath) ? sizeof(sun.sun_path) : sizeof(spath); |
eabd1701 | 267 | |
ddbbfbc1 A |
268 | strncpy(sun.sun_path, spath, min_len); |
269 | } | |
270 | } | |
271 | } | |
eabd1701 | 272 | |
95379394 | 273 | launch_globals_t globals = _launch_globals(); |
ddbbfbc1 A |
274 | if ((lfd = _fd(socket(AF_UNIX, SOCK_STREAM, 0))) == -1) { |
275 | goto out_bad; | |
276 | } | |
eabd1701 | 277 | |
ddbbfbc1 | 278 | #if TARGET_OS_EMBEDDED |
95379394 | 279 | (void)vproc_swap_integer(NULL, VPROC_GSK_EMBEDDEDROOTEQUIVALENT, NULL, &globals->s_am_embedded_god); |
ddbbfbc1 A |
280 | #endif |
281 | if (-1 == connect(lfd, (struct sockaddr *)&sun, sizeof(sun))) { | |
95379394 | 282 | if (cifd != -1 || globals->s_am_embedded_god) { |
ddbbfbc1 A |
283 | /* There is NO security enforced by this check. This is just a hint to our |
284 | * library that we shouldn't error out due to failing to open this socket. If | |
285 | * we inherited a trusted file descriptor, we shouldn't fail. This should be | |
286 | * adequate for clients' expectations. | |
287 | */ | |
288 | close(lfd); | |
289 | lfd = -1; | |
ed34e3c3 | 290 | } else { |
e91b9f68 | 291 | goto out_bad; |
ddbbfbc1 | 292 | } |
e91b9f68 | 293 | } |
95379394 A |
294 | |
295 | if (!(globals->l = launchd_fdopen(lfd, cifd))) { | |
e91b9f68 | 296 | goto out_bad; |
ddbbfbc1 | 297 | } |
eabd1701 | 298 | |
95379394 | 299 | if (!(globals->async_resp = launch_data_alloc(LAUNCH_DATA_ARRAY))) { |
e91b9f68 | 300 | goto out_bad; |
ddbbfbc1 | 301 | } |
eabd1701 | 302 | |
e91b9f68 A |
303 | return; |
304 | out_bad: | |
95379394 A |
305 | if (globals->l) { |
306 | launchd_close(globals->l, close); | |
307 | globals->l = NULL; | |
308 | } else if (lfd != -1) { | |
ed34e3c3 | 309 | close(lfd); |
95379394 | 310 | } |
dcace88f | 311 | if (cifd != -1) { |
ddbbfbc1 A |
312 | close(cifd); |
313 | } | |
e91b9f68 A |
314 | } |
315 | ||
ed34e3c3 A |
316 | launch_data_t |
317 | launch_data_alloc(launch_data_type_t t) | |
e91b9f68 | 318 | { |
dcace88f | 319 | launch_data_t d = calloc(1, sizeof(struct _launch_data)); |
e91b9f68 A |
320 | |
321 | if (d) { | |
322 | d->type = t; | |
323 | switch (t) { | |
324 | case LAUNCH_DATA_DICTIONARY: | |
325 | case LAUNCH_DATA_ARRAY: | |
326 | d->_array = malloc(0); | |
327 | break; | |
dcace88f A |
328 | case LAUNCH_DATA_OPAQUE: |
329 | d->opaque = malloc(0); | |
e91b9f68 A |
330 | default: |
331 | break; | |
332 | } | |
333 | } | |
334 | ||
335 | return d; | |
336 | } | |
337 | ||
ed34e3c3 A |
338 | launch_data_type_t |
339 | launch_data_get_type(launch_data_t d) | |
e91b9f68 A |
340 | { |
341 | return d->type; | |
342 | } | |
343 | ||
ed34e3c3 A |
344 | void |
345 | launch_data_free(launch_data_t d) | |
e91b9f68 A |
346 | { |
347 | size_t i; | |
348 | ||
349 | switch (d->type) { | |
350 | case LAUNCH_DATA_DICTIONARY: | |
351 | case LAUNCH_DATA_ARRAY: | |
dcace88f A |
352 | for (i = 0; i < d->_array_cnt; i++) { |
353 | if (d->_array[i]) { | |
354 | launch_data_free(d->_array[i]); | |
355 | } | |
356 | } | |
e91b9f68 A |
357 | free(d->_array); |
358 | break; | |
359 | case LAUNCH_DATA_STRING: | |
360 | if (d->string) | |
361 | free(d->string); | |
362 | break; | |
363 | case LAUNCH_DATA_OPAQUE: | |
364 | if (d->opaque) | |
365 | free(d->opaque); | |
366 | break; | |
367 | default: | |
368 | break; | |
369 | } | |
370 | free(d); | |
371 | } | |
372 | ||
ed34e3c3 A |
373 | size_t |
374 | launch_data_dict_get_count(launch_data_t dict) | |
e91b9f68 A |
375 | { |
376 | return dict->_array_cnt / 2; | |
377 | } | |
378 | ||
ed34e3c3 A |
379 | bool |
380 | launch_data_dict_insert(launch_data_t dict, launch_data_t what, const char *key) | |
e91b9f68 A |
381 | { |
382 | size_t i; | |
ed34e3c3 | 383 | launch_data_t thekey = launch_data_alloc(LAUNCH_DATA_STRING); |
e91b9f68 | 384 | |
ed34e3c3 | 385 | launch_data_set_string(thekey, key); |
e91b9f68 A |
386 | |
387 | for (i = 0; i < dict->_array_cnt; i += 2) { | |
388 | if (!strcasecmp(key, dict->_array[i]->string)) { | |
389 | launch_data_array_set_index(dict, thekey, i); | |
390 | launch_data_array_set_index(dict, what, i + 1); | |
391 | return true; | |
392 | } | |
393 | } | |
394 | launch_data_array_set_index(dict, thekey, i); | |
395 | launch_data_array_set_index(dict, what, i + 1); | |
396 | return true; | |
397 | } | |
398 | ||
ed34e3c3 A |
399 | launch_data_t |
400 | launch_data_dict_lookup(launch_data_t dict, const char *key) | |
e91b9f68 A |
401 | { |
402 | size_t i; | |
403 | ||
ed34e3c3 | 404 | if (LAUNCH_DATA_DICTIONARY != dict->type) |
e91b9f68 A |
405 | return NULL; |
406 | ||
407 | for (i = 0; i < dict->_array_cnt; i += 2) { | |
408 | if (!strcasecmp(key, dict->_array[i]->string)) | |
409 | return dict->_array[i + 1]; | |
410 | } | |
411 | ||
412 | return NULL; | |
413 | } | |
414 | ||
ed34e3c3 A |
415 | bool |
416 | launch_data_dict_remove(launch_data_t dict, const char *key) | |
e91b9f68 A |
417 | { |
418 | size_t i; | |
419 | ||
420 | for (i = 0; i < dict->_array_cnt; i += 2) { | |
421 | if (!strcasecmp(key, dict->_array[i]->string)) | |
422 | break; | |
423 | } | |
424 | if (i == dict->_array_cnt) | |
425 | return false; | |
426 | launch_data_free(dict->_array[i]); | |
427 | launch_data_free(dict->_array[i + 1]); | |
428 | memmove(dict->_array + i, dict->_array + i + 2, (dict->_array_cnt - (i + 2)) * sizeof(launch_data_t)); | |
429 | dict->_array_cnt -= 2; | |
430 | return true; | |
431 | } | |
432 | ||
ed34e3c3 A |
433 | void |
434 | launch_data_dict_iterate(launch_data_t dict, void (*cb)(launch_data_t, const char *, void *), void *context) | |
e91b9f68 A |
435 | { |
436 | size_t i; | |
437 | ||
ddbbfbc1 | 438 | if (LAUNCH_DATA_DICTIONARY != dict->type) { |
e91b9f68 | 439 | return; |
ddbbfbc1 | 440 | } |
e91b9f68 | 441 | |
ddbbfbc1 | 442 | for (i = 0; i < dict->_array_cnt; i += 2) { |
e91b9f68 | 443 | cb(dict->_array[i + 1], dict->_array[i]->string, context); |
ddbbfbc1 | 444 | } |
e91b9f68 A |
445 | } |
446 | ||
ed34e3c3 A |
447 | bool |
448 | launch_data_array_set_index(launch_data_t where, launch_data_t what, size_t ind) | |
e91b9f68 A |
449 | { |
450 | if ((ind + 1) >= where->_array_cnt) { | |
5b0a4722 | 451 | where->_array = reallocf(where->_array, (ind + 1) * sizeof(launch_data_t)); |
e91b9f68 A |
452 | memset(where->_array + where->_array_cnt, 0, (ind + 1 - where->_array_cnt) * sizeof(launch_data_t)); |
453 | where->_array_cnt = ind + 1; | |
454 | } | |
455 | ||
ddbbfbc1 | 456 | if (where->_array[ind]) { |
e91b9f68 | 457 | launch_data_free(where->_array[ind]); |
ddbbfbc1 A |
458 | } |
459 | ||
e91b9f68 A |
460 | where->_array[ind] = what; |
461 | return true; | |
462 | } | |
463 | ||
ed34e3c3 A |
464 | launch_data_t |
465 | launch_data_array_get_index(launch_data_t where, size_t ind) | |
e91b9f68 | 466 | { |
ddbbfbc1 | 467 | if (LAUNCH_DATA_ARRAY != where->type || ind >= where->_array_cnt) { |
e91b9f68 | 468 | return NULL; |
ddbbfbc1 | 469 | } else { |
e91b9f68 | 470 | return where->_array[ind]; |
ddbbfbc1 | 471 | } |
e91b9f68 A |
472 | } |
473 | ||
ed34e3c3 A |
474 | launch_data_t |
475 | launch_data_array_pop_first(launch_data_t where) | |
e91b9f68 A |
476 | { |
477 | launch_data_t r = NULL; | |
5b0a4722 | 478 | |
e91b9f68 A |
479 | if (where->_array_cnt > 0) { |
480 | r = where->_array[0]; | |
481 | memmove(where->_array, where->_array + 1, (where->_array_cnt - 1) * sizeof(launch_data_t)); | |
482 | where->_array_cnt--; | |
483 | } | |
484 | return r; | |
485 | } | |
486 | ||
ed34e3c3 A |
487 | size_t |
488 | launch_data_array_get_count(launch_data_t where) | |
e91b9f68 | 489 | { |
ed34e3c3 | 490 | if (LAUNCH_DATA_ARRAY != where->type) |
e91b9f68 A |
491 | return 0; |
492 | return where->_array_cnt; | |
493 | } | |
494 | ||
ed34e3c3 A |
495 | bool |
496 | launch_data_set_errno(launch_data_t d, int e) | |
e91b9f68 A |
497 | { |
498 | d->err = e; | |
499 | return true; | |
500 | } | |
501 | ||
ed34e3c3 A |
502 | bool |
503 | launch_data_set_fd(launch_data_t d, int fd) | |
e91b9f68 A |
504 | { |
505 | d->fd = fd; | |
506 | return true; | |
507 | } | |
508 | ||
ed34e3c3 A |
509 | bool |
510 | launch_data_set_machport(launch_data_t d, mach_port_t p) | |
511 | { | |
512 | d->mp = p; | |
513 | return true; | |
514 | } | |
515 | ||
516 | bool | |
517 | launch_data_set_integer(launch_data_t d, long long n) | |
e91b9f68 A |
518 | { |
519 | d->number = n; | |
520 | return true; | |
521 | } | |
522 | ||
ed34e3c3 A |
523 | bool |
524 | launch_data_set_bool(launch_data_t d, bool b) | |
e91b9f68 A |
525 | { |
526 | d->boolean = b; | |
527 | return true; | |
528 | } | |
529 | ||
ed34e3c3 A |
530 | bool |
531 | launch_data_set_real(launch_data_t d, double n) | |
e91b9f68 A |
532 | { |
533 | d->float_num = n; | |
534 | return true; | |
535 | } | |
536 | ||
ed34e3c3 A |
537 | bool |
538 | launch_data_set_string(launch_data_t d, const char *s) | |
e91b9f68 A |
539 | { |
540 | if (d->string) | |
541 | free(d->string); | |
542 | d->string = strdup(s); | |
543 | if (d->string) { | |
544 | d->string_len = strlen(d->string); | |
545 | return true; | |
546 | } | |
547 | return false; | |
548 | } | |
549 | ||
ed34e3c3 A |
550 | bool |
551 | launch_data_set_opaque(launch_data_t d, const void *o, size_t os) | |
e91b9f68 A |
552 | { |
553 | d->opaque_size = os; | |
554 | if (d->opaque) | |
555 | free(d->opaque); | |
556 | d->opaque = malloc(os); | |
557 | if (d->opaque) { | |
558 | memcpy(d->opaque, o, os); | |
559 | return true; | |
560 | } | |
561 | return false; | |
562 | } | |
563 | ||
ed34e3c3 A |
564 | int |
565 | launch_data_get_errno(launch_data_t d) | |
e91b9f68 A |
566 | { |
567 | return d->err; | |
568 | } | |
569 | ||
ed34e3c3 A |
570 | int |
571 | launch_data_get_fd(launch_data_t d) | |
e91b9f68 A |
572 | { |
573 | return d->fd; | |
574 | } | |
575 | ||
ed34e3c3 A |
576 | mach_port_t |
577 | launch_data_get_machport(launch_data_t d) | |
578 | { | |
579 | return d->mp; | |
580 | } | |
581 | ||
582 | long long | |
583 | launch_data_get_integer(launch_data_t d) | |
e91b9f68 A |
584 | { |
585 | return d->number; | |
586 | } | |
587 | ||
ed34e3c3 A |
588 | bool |
589 | launch_data_get_bool(launch_data_t d) | |
e91b9f68 A |
590 | { |
591 | return d->boolean; | |
592 | } | |
593 | ||
ed34e3c3 A |
594 | double |
595 | launch_data_get_real(launch_data_t d) | |
e91b9f68 A |
596 | { |
597 | return d->float_num; | |
598 | } | |
599 | ||
ed34e3c3 A |
600 | const char * |
601 | launch_data_get_string(launch_data_t d) | |
e91b9f68 | 602 | { |
ed34e3c3 | 603 | if (LAUNCH_DATA_STRING != d->type) |
e91b9f68 A |
604 | return NULL; |
605 | return d->string; | |
606 | } | |
607 | ||
ed34e3c3 A |
608 | void * |
609 | launch_data_get_opaque(launch_data_t d) | |
e91b9f68 | 610 | { |
ed34e3c3 | 611 | if (LAUNCH_DATA_OPAQUE != d->type) |
e91b9f68 A |
612 | return NULL; |
613 | return d->opaque; | |
614 | } | |
615 | ||
ed34e3c3 A |
616 | size_t |
617 | launch_data_get_opaque_size(launch_data_t d) | |
e91b9f68 A |
618 | { |
619 | return d->opaque_size; | |
620 | } | |
621 | ||
ed34e3c3 A |
622 | int |
623 | launchd_getfd(launch_t l) | |
e91b9f68 | 624 | { |
eabd1701 | 625 | return (l->which == LAUNCHD_USE_CHECKIN_FD) ? l->cifd : l->fd; |
e91b9f68 A |
626 | } |
627 | ||
ed34e3c3 | 628 | launch_t |
ddbbfbc1 | 629 | launchd_fdopen(int fd, int cifd) |
e91b9f68 | 630 | { |
5b0a4722 | 631 | launch_t c; |
e91b9f68 | 632 | |
5b0a4722 | 633 | c = calloc(1, sizeof(struct _launch)); |
e91b9f68 A |
634 | if (!c) |
635 | return NULL; | |
636 | ||
5b0a4722 | 637 | c->fd = fd; |
ddbbfbc1 A |
638 | c->cifd = cifd; |
639 | ||
dcace88f | 640 | if (c->fd == -1 || (c->fd != -1 && c->cifd != -1)) { |
ddbbfbc1 | 641 | c->which = LAUNCHD_USE_CHECKIN_FD; |
dcace88f | 642 | } else if (c->cifd == -1) { |
ddbbfbc1 A |
643 | c->which = LAUNCHD_USE_OTHER_FD; |
644 | } | |
e91b9f68 | 645 | |
ed34e3c3 | 646 | fcntl(fd, F_SETFL, O_NONBLOCK); |
ddbbfbc1 | 647 | fcntl(cifd, F_SETFL, O_NONBLOCK); |
e91b9f68 | 648 | |
5b0a4722 | 649 | if ((c->sendbuf = malloc(0)) == NULL) |
e91b9f68 | 650 | goto out_bad; |
5b0a4722 | 651 | if ((c->sendfds = malloc(0)) == NULL) |
e91b9f68 | 652 | goto out_bad; |
5b0a4722 | 653 | if ((c->recvbuf = malloc(0)) == NULL) |
e91b9f68 | 654 | goto out_bad; |
5b0a4722 | 655 | if ((c->recvfds = malloc(0)) == NULL) |
e91b9f68 A |
656 | goto out_bad; |
657 | ||
658 | return c; | |
659 | ||
660 | out_bad: | |
661 | if (c->sendbuf) | |
662 | free(c->sendbuf); | |
663 | if (c->sendfds) | |
664 | free(c->sendfds); | |
665 | if (c->recvbuf) | |
666 | free(c->recvbuf); | |
667 | if (c->recvfds) | |
668 | free(c->recvfds); | |
669 | free(c); | |
670 | return NULL; | |
671 | } | |
672 | ||
ed34e3c3 | 673 | void |
5b0a4722 | 674 | launchd_close(launch_t lh, typeof(close) closefunc) |
e91b9f68 | 675 | { |
95379394 A |
676 | launch_globals_t globals = _launch_globals(); |
677 | ||
678 | if (globals->in_flight_msg_recv_client == lh) { | |
679 | globals->in_flight_msg_recv_client = NULL; | |
f36da725 A |
680 | } |
681 | ||
e91b9f68 A |
682 | if (lh->sendbuf) |
683 | free(lh->sendbuf); | |
684 | if (lh->sendfds) | |
685 | free(lh->sendfds); | |
686 | if (lh->recvbuf) | |
687 | free(lh->recvbuf); | |
688 | if (lh->recvfds) | |
689 | free(lh->recvfds); | |
5b0a4722 | 690 | closefunc(lh->fd); |
ddbbfbc1 | 691 | closefunc(lh->cifd); |
e91b9f68 A |
692 | free(lh); |
693 | } | |
694 | ||
5b0a4722 A |
695 | #define ROUND_TO_64BIT_WORD_SIZE(x) ((x + 7) & ~7) |
696 | ||
697 | size_t | |
698 | launch_data_pack(launch_data_t d, void *where, size_t len, int *fd_where, size_t *fd_cnt) | |
e91b9f68 | 699 | { |
5b0a4722 | 700 | launch_data_t o_in_w = where; |
ddbbfbc1 | 701 | size_t i, rsz, node_data_len = sizeof(struct _launch_data); |
e91b9f68 | 702 | |
ddbbfbc1 | 703 | if (node_data_len > len) { |
5b0a4722 A |
704 | return 0; |
705 | } | |
aa59983a | 706 | |
ddbbfbc1 | 707 | where += node_data_len; |
e91b9f68 | 708 | |
ddbbfbc1 | 709 | o_in_w->type = host2wire(d->type); |
aa59983a | 710 | |
ddbbfbc1 | 711 | size_t pad_len = 0; |
e91b9f68 | 712 | switch (d->type) { |
aa59983a | 713 | case LAUNCH_DATA_INTEGER: |
ddbbfbc1 | 714 | o_in_w->number = host2wire(d->number); |
aa59983a A |
715 | break; |
716 | case LAUNCH_DATA_REAL: | |
eabd1701 | 717 | o_in_w->float_num = host2wire_f(d->float_num); |
aa59983a A |
718 | break; |
719 | case LAUNCH_DATA_BOOL: | |
ddbbfbc1 | 720 | o_in_w->boolean = host2wire(d->boolean); |
aa59983a A |
721 | break; |
722 | case LAUNCH_DATA_ERRNO: | |
ddbbfbc1 | 723 | o_in_w->err = host2wire(d->err); |
aa59983a | 724 | break; |
e91b9f68 | 725 | case LAUNCH_DATA_FD: |
ddbbfbc1 | 726 | o_in_w->fd = host2wire(d->fd); |
5b0a4722 A |
727 | if (fd_where && d->fd != -1) { |
728 | fd_where[*fd_cnt] = d->fd; | |
729 | (*fd_cnt)++; | |
e91b9f68 A |
730 | } |
731 | break; | |
732 | case LAUNCH_DATA_STRING: | |
ddbbfbc1 A |
733 | o_in_w->string_len = host2wire(d->string_len); |
734 | node_data_len += ROUND_TO_64BIT_WORD_SIZE(d->string_len + 1); | |
eabd1701 | 735 | |
ddbbfbc1 | 736 | if (node_data_len > len) { |
5b0a4722 A |
737 | return 0; |
738 | } | |
ddbbfbc1 | 739 | memcpy(where, d->string, d->string_len + 1); |
eabd1701 | 740 | |
ddbbfbc1 A |
741 | /* Zero padded data. */ |
742 | pad_len = ROUND_TO_64BIT_WORD_SIZE(d->string_len + 1) - (d->string_len + 1); | |
743 | bzero(where + d->string_len + 1, pad_len); | |
eabd1701 | 744 | |
e91b9f68 A |
745 | break; |
746 | case LAUNCH_DATA_OPAQUE: | |
ddbbfbc1 A |
747 | o_in_w->opaque_size = host2wire(d->opaque_size); |
748 | node_data_len += ROUND_TO_64BIT_WORD_SIZE(d->opaque_size); | |
749 | if (node_data_len > len) { | |
5b0a4722 A |
750 | return 0; |
751 | } | |
752 | memcpy(where, d->opaque, d->opaque_size); | |
eabd1701 | 753 | |
ddbbfbc1 A |
754 | /* Zero padded data. */ |
755 | pad_len = ROUND_TO_64BIT_WORD_SIZE(d->opaque_size) - d->opaque_size; | |
756 | bzero(where + d->opaque_size, pad_len); | |
eabd1701 | 757 | |
e91b9f68 A |
758 | break; |
759 | case LAUNCH_DATA_DICTIONARY: | |
760 | case LAUNCH_DATA_ARRAY: | |
ddbbfbc1 A |
761 | o_in_w->_array_cnt = host2wire(d->_array_cnt); |
762 | node_data_len += d->_array_cnt * sizeof(uint64_t); | |
763 | if (node_data_len > len) { | |
5b0a4722 A |
764 | return 0; |
765 | } | |
e91b9f68 | 766 | |
5b0a4722 A |
767 | where += d->_array_cnt * sizeof(uint64_t); |
768 | ||
769 | for (i = 0; i < d->_array_cnt; i++) { | |
ddbbfbc1 | 770 | rsz = launch_data_pack(d->_array[i], where, len - node_data_len, fd_where, fd_cnt); |
5b0a4722 A |
771 | if (rsz == 0) { |
772 | return 0; | |
773 | } | |
774 | where += rsz; | |
ddbbfbc1 | 775 | node_data_len += rsz; |
5b0a4722 | 776 | } |
e91b9f68 A |
777 | break; |
778 | default: | |
779 | break; | |
780 | } | |
5b0a4722 | 781 | |
ddbbfbc1 | 782 | return node_data_len; |
e91b9f68 A |
783 | } |
784 | ||
5b0a4722 A |
785 | launch_data_t |
786 | launch_data_unpack(void *data, size_t data_size, int *fds, size_t fd_cnt, size_t *data_offset, size_t *fdoffset) | |
e91b9f68 | 787 | { |
5b0a4722 | 788 | launch_data_t r = data + *data_offset; |
aa59983a | 789 | size_t i, tmpcnt; |
e91b9f68 | 790 | |
5f168eaf A |
791 | //Check for integer underflow |
792 | if (data_size < *data_offset) | |
793 | return NULL; | |
794 | ||
5b0a4722 | 795 | if ((data_size - *data_offset) < sizeof(struct _launch_data)) |
e91b9f68 A |
796 | return NULL; |
797 | *data_offset += sizeof(struct _launch_data); | |
798 | ||
ddbbfbc1 | 799 | switch (big2wire(r->type)) { |
e91b9f68 A |
800 | case LAUNCH_DATA_DICTIONARY: |
801 | case LAUNCH_DATA_ARRAY: | |
ddbbfbc1 | 802 | tmpcnt = big2wire(r->_array_cnt); |
5f168eaf A |
803 | |
804 | //Check for integer overflows | |
805 | if (tmpcnt > SIZE_MAX / sizeof(uint64_t)) { | |
806 | errno = EAGAIN; | |
807 | return NULL; | |
808 | } | |
809 | ||
5b0a4722 | 810 | if ((data_size - *data_offset) < (tmpcnt * sizeof(uint64_t))) { |
e91b9f68 A |
811 | errno = EAGAIN; |
812 | return NULL; | |
813 | } | |
5b0a4722 A |
814 | r->_array = data + *data_offset; |
815 | *data_offset += tmpcnt * sizeof(uint64_t); | |
aa59983a | 816 | for (i = 0; i < tmpcnt; i++) { |
5b0a4722 | 817 | r->_array[i] = launch_data_unpack(data, data_size, fds, fd_cnt, data_offset, fdoffset); |
e91b9f68 A |
818 | if (r->_array[i] == NULL) |
819 | return NULL; | |
820 | } | |
aa59983a | 821 | r->_array_cnt = tmpcnt; |
e91b9f68 A |
822 | break; |
823 | case LAUNCH_DATA_STRING: | |
ddbbfbc1 | 824 | tmpcnt = big2wire(r->string_len); |
5b0a4722 | 825 | if ((data_size - *data_offset) < (tmpcnt + 1)) { |
e91b9f68 A |
826 | errno = EAGAIN; |
827 | return NULL; | |
828 | } | |
5b0a4722 | 829 | r->string = data + *data_offset; |
aa59983a | 830 | r->string_len = tmpcnt; |
5b0a4722 | 831 | *data_offset += ROUND_TO_64BIT_WORD_SIZE(tmpcnt + 1); |
e91b9f68 A |
832 | break; |
833 | case LAUNCH_DATA_OPAQUE: | |
ddbbfbc1 | 834 | tmpcnt = big2wire(r->opaque_size); |
5b0a4722 | 835 | if ((data_size - *data_offset) < tmpcnt) { |
e91b9f68 A |
836 | errno = EAGAIN; |
837 | return NULL; | |
838 | } | |
5b0a4722 | 839 | r->opaque = data + *data_offset; |
aa59983a | 840 | r->opaque_size = tmpcnt; |
5b0a4722 | 841 | *data_offset += ROUND_TO_64BIT_WORD_SIZE(tmpcnt); |
e91b9f68 A |
842 | break; |
843 | case LAUNCH_DATA_FD: | |
5b0a4722 A |
844 | if (r->fd != -1 && fd_cnt > *fdoffset) { |
845 | r->fd = _fd(fds[*fdoffset]); | |
e91b9f68 A |
846 | *fdoffset += 1; |
847 | } | |
848 | break; | |
849 | case LAUNCH_DATA_INTEGER: | |
ddbbfbc1 | 850 | r->number = big2wire(r->number); |
aa59983a | 851 | break; |
e91b9f68 | 852 | case LAUNCH_DATA_REAL: |
eabd1701 | 853 | r->float_num = big2wire_f(r->float_num); |
aa59983a | 854 | break; |
e91b9f68 | 855 | case LAUNCH_DATA_BOOL: |
ddbbfbc1 | 856 | r->boolean = big2wire(r->boolean); |
aa59983a | 857 | break; |
e91b9f68 | 858 | case LAUNCH_DATA_ERRNO: |
ddbbfbc1 | 859 | r->err = big2wire(r->err); |
ed34e3c3 | 860 | case LAUNCH_DATA_MACHPORT: |
e91b9f68 A |
861 | break; |
862 | default: | |
863 | errno = EINVAL; | |
864 | return NULL; | |
865 | break; | |
866 | } | |
867 | ||
ddbbfbc1 | 868 | r->type = big2wire(r->type); |
aa59983a | 869 | |
e91b9f68 A |
870 | return r; |
871 | } | |
872 | ||
ddbbfbc1 A |
873 | int |
874 | launchd_msg_send(launch_t lh, launch_data_t d) | |
e91b9f68 | 875 | { |
aa59983a | 876 | struct launch_msg_header lmh; |
e91b9f68 A |
877 | struct cmsghdr *cm = NULL; |
878 | struct msghdr mh; | |
aa59983a | 879 | struct iovec iov[2]; |
e91b9f68 | 880 | size_t sentctrllen = 0; |
aa59983a | 881 | int r; |
e91b9f68 | 882 | |
ddbbfbc1 | 883 | int fd2use = launchd_getfd(lh); |
dcace88f | 884 | if (fd2use == -1) { |
ddbbfbc1 A |
885 | errno = EPERM; |
886 | return -1; | |
887 | } | |
888 | ||
e91b9f68 A |
889 | memset(&mh, 0, sizeof(mh)); |
890 | ||
5b0a4722 A |
891 | /* confirm that the next hack works */ |
892 | assert((d && lh->sendlen == 0) || (!d && lh->sendlen)); | |
893 | ||
aa59983a | 894 | if (d) { |
5b0a4722 A |
895 | size_t fd_slots_used = 0; |
896 | size_t good_enough_size = 10 * 1024 * 1024; | |
897 | uint64_t msglen; | |
898 | ||
899 | /* hack, see the above assert to verify "correctness" */ | |
900 | free(lh->sendbuf); | |
901 | lh->sendbuf = malloc(good_enough_size); | |
dcace88f A |
902 | if (!lh->sendbuf) { |
903 | errno = ENOMEM; | |
904 | return -1; | |
905 | } | |
906 | ||
5b0a4722 A |
907 | free(lh->sendfds); |
908 | lh->sendfds = malloc(4 * 1024); | |
dcace88f A |
909 | if (!lh->sendfds) { |
910 | free(lh->sendbuf); | |
911 | lh->sendbuf = NULL; | |
912 | errno = ENOMEM; | |
913 | return -1; | |
914 | } | |
5b0a4722 A |
915 | |
916 | lh->sendlen = launch_data_pack(d, lh->sendbuf, good_enough_size, lh->sendfds, &fd_slots_used); | |
917 | ||
918 | if (lh->sendlen == 0) { | |
919 | errno = ENOMEM; | |
920 | return -1; | |
921 | } | |
e91b9f68 | 922 | |
5b0a4722 | 923 | lh->sendfdcnt = fd_slots_used; |
e91b9f68 | 924 | |
ddbbfbc1 A |
925 | msglen = lh->sendlen + sizeof(struct launch_msg_header); /* type promotion to make the host2wire() macro work right */ |
926 | lmh.len = host2wire(msglen); | |
927 | lmh.magic = host2wire(LAUNCH_MSG_HEADER_MAGIC); | |
aa59983a A |
928 | |
929 | iov[0].iov_base = &lmh; | |
930 | iov[0].iov_len = sizeof(lmh); | |
931 | mh.msg_iov = iov; | |
5b0a4722 | 932 | mh.msg_iovlen = 2; |
aa59983a A |
933 | } else { |
934 | mh.msg_iov = iov + 1; | |
5b0a4722 | 935 | mh.msg_iovlen = 1; |
aa59983a A |
936 | } |
937 | ||
938 | iov[1].iov_base = lh->sendbuf; | |
939 | iov[1].iov_len = lh->sendlen; | |
940 | ||
941 | ||
e91b9f68 A |
942 | if (lh->sendfdcnt > 0) { |
943 | sentctrllen = mh.msg_controllen = CMSG_SPACE(lh->sendfdcnt * sizeof(int)); | |
944 | cm = alloca(mh.msg_controllen); | |
945 | mh.msg_control = cm; | |
946 | ||
947 | memset(cm, 0, mh.msg_controllen); | |
948 | ||
949 | cm->cmsg_len = CMSG_LEN(lh->sendfdcnt * sizeof(int)); | |
950 | cm->cmsg_level = SOL_SOCKET; | |
951 | cm->cmsg_type = SCM_RIGHTS; | |
952 | ||
953 | memcpy(CMSG_DATA(cm), lh->sendfds, lh->sendfdcnt * sizeof(int)); | |
954 | } | |
955 | ||
ddbbfbc1 | 956 | if ((r = sendmsg(fd2use, &mh, 0)) == -1) { |
e91b9f68 A |
957 | return -1; |
958 | } else if (r == 0) { | |
959 | errno = ECONNRESET; | |
960 | return -1; | |
961 | } else if (sentctrllen != mh.msg_controllen) { | |
962 | errno = ECONNRESET; | |
963 | return -1; | |
964 | } | |
965 | ||
aa59983a A |
966 | if (d) { |
967 | r -= sizeof(struct launch_msg_header); | |
968 | } | |
969 | ||
e91b9f68 A |
970 | lh->sendlen -= r; |
971 | if (lh->sendlen > 0) { | |
972 | memmove(lh->sendbuf, lh->sendbuf + r, lh->sendlen); | |
973 | } else { | |
974 | free(lh->sendbuf); | |
975 | lh->sendbuf = malloc(0); | |
976 | } | |
977 | ||
978 | lh->sendfdcnt = 0; | |
979 | free(lh->sendfds); | |
980 | lh->sendfds = malloc(0); | |
981 | ||
982 | if (lh->sendlen > 0) { | |
983 | errno = EAGAIN; | |
984 | return -1; | |
985 | } | |
986 | ||
987 | return 0; | |
988 | } | |
989 | ||
ed34e3c3 A |
990 | int |
991 | launch_get_fd(void) | |
e91b9f68 | 992 | { |
95379394 A |
993 | launch_globals_t globals = _launch_globals(); |
994 | pthread_once(&globals->lc_once, launch_client_init); | |
e91b9f68 | 995 | |
95379394 | 996 | if (!globals->l) { |
e91b9f68 A |
997 | errno = ENOTCONN; |
998 | return -1; | |
999 | } | |
1000 | ||
95379394 | 1001 | return globals->l->fd; |
e91b9f68 A |
1002 | } |
1003 | ||
ed34e3c3 A |
1004 | void |
1005 | launch_msg_getmsgs(launch_data_t m, void *context) | |
e91b9f68 A |
1006 | { |
1007 | launch_data_t async_resp, *sync_resp = context; | |
eabd1701 | 1008 | |
95379394 A |
1009 | launch_globals_t globals = _launch_globals(); |
1010 | ||
e91b9f68 | 1011 | if ((LAUNCH_DATA_DICTIONARY == launch_data_get_type(m)) && (async_resp = launch_data_dict_lookup(m, LAUNCHD_ASYNC_MSG_KEY))) { |
95379394 | 1012 | launch_data_array_set_index(globals->async_resp, launch_data_copy(async_resp), launch_data_array_get_count(globals->async_resp)); |
e91b9f68 A |
1013 | } else { |
1014 | *sync_resp = launch_data_copy(m); | |
1015 | } | |
1016 | } | |
1017 | ||
ed34e3c3 A |
1018 | void |
1019 | launch_mach_checkin_service(launch_data_t obj, const char *key, void *context __attribute__((unused))) | |
1020 | { | |
1021 | kern_return_t result; | |
1022 | mach_port_t p; | |
1023 | name_t srvnm; | |
1024 | ||
1025 | strlcpy(srvnm, key, sizeof(srvnm)); | |
1026 | ||
1027 | result = bootstrap_check_in(bootstrap_port, srvnm, &p); | |
1028 | ||
1029 | if (result == BOOTSTRAP_SUCCESS) | |
1030 | launch_data_set_machport(obj, p); | |
1031 | } | |
1032 | ||
1033 | launch_data_t | |
1034 | launch_msg(launch_data_t d) | |
1035 | { | |
1036 | launch_data_t mps, r = launch_msg_internal(d); | |
1037 | ||
1038 | if (launch_data_get_type(d) == LAUNCH_DATA_STRING) { | |
1039 | if (strcmp(launch_data_get_string(d), LAUNCH_KEY_CHECKIN) != 0) | |
1040 | return r; | |
1041 | if (r == NULL) | |
1042 | return r; | |
1043 | if (launch_data_get_type(r) != LAUNCH_DATA_DICTIONARY) | |
1044 | return r; | |
1045 | mps = launch_data_dict_lookup(r, LAUNCH_JOBKEY_MACHSERVICES); | |
1046 | if (mps == NULL) | |
1047 | return r; | |
1048 | launch_data_dict_iterate(mps, launch_mach_checkin_service, NULL); | |
1049 | } | |
1050 | ||
1051 | return r; | |
1052 | } | |
1053 | ||
ddbbfbc1 A |
1054 | extern kern_return_t vproc_mig_set_security_session(mach_port_t, uuid_t, mach_port_t); |
1055 | ||
1056 | static inline bool | |
1057 | uuid_data_is_null(launch_data_t d) | |
1058 | { | |
1059 | bool result = false; | |
dcace88f | 1060 | if (launch_data_get_type(d) == LAUNCH_DATA_OPAQUE && launch_data_get_opaque_size(d) == sizeof(uuid_t)) { |
ddbbfbc1 A |
1061 | uuid_t existing_uuid; |
1062 | memcpy(existing_uuid, launch_data_get_opaque(d), sizeof(uuid_t)); | |
eabd1701 | 1063 | |
ddbbfbc1 A |
1064 | /* A NULL UUID tells us to keep the session inherited from the parent. */ |
1065 | result = (bool)uuid_is_null(existing_uuid); | |
1066 | } | |
eabd1701 | 1067 | |
ddbbfbc1 A |
1068 | return result; |
1069 | } | |
1070 | ||
ed34e3c3 A |
1071 | launch_data_t |
1072 | launch_msg_internal(launch_data_t d) | |
e91b9f68 A |
1073 | { |
1074 | launch_data_t resp = NULL; | |
1075 | ||
5b0a4722 A |
1076 | if (d && (launch_data_get_type(d) == LAUNCH_DATA_STRING) |
1077 | && (strcmp(launch_data_get_string(d), LAUNCH_KEY_GETJOBS) == 0) | |
1078 | && vproc_swap_complex(NULL, VPROC_GSK_ALLJOBS, NULL, &resp) == NULL) { | |
1079 | return resp; | |
1080 | } | |
1081 | ||
95379394 A |
1082 | launch_globals_t globals = _launch_globals(); |
1083 | pthread_once(&globals->lc_once, launch_client_init); | |
1084 | if (!globals->l) { | |
e91b9f68 A |
1085 | errno = ENOTCONN; |
1086 | return NULL; | |
1087 | } | |
1088 | ||
ddbbfbc1 | 1089 | int fd2use = -1; |
95379394 A |
1090 | if ((launch_data_get_type(d) == LAUNCH_DATA_STRING && strcmp(launch_data_get_string(d), LAUNCH_KEY_CHECKIN) == 0) || globals->s_am_embedded_god) { |
1091 | globals->l->which = LAUNCHD_USE_CHECKIN_FD; | |
ddbbfbc1 | 1092 | } else { |
95379394 | 1093 | globals->l->which = LAUNCHD_USE_OTHER_FD; |
ddbbfbc1 | 1094 | } |
eabd1701 | 1095 | |
95379394 | 1096 | fd2use = launchd_getfd(globals->l); |
eabd1701 | 1097 | |
dcace88f | 1098 | if (fd2use == -1) { |
ddbbfbc1 A |
1099 | errno = EPERM; |
1100 | return NULL; | |
1101 | } | |
eabd1701 | 1102 | |
ddbbfbc1 A |
1103 | #if !TARGET_OS_EMBEDDED |
1104 | uuid_t uuid; | |
1105 | launch_data_t uuid_d = NULL; | |
1106 | size_t jobs_that_need_sessions = 0; | |
dcace88f | 1107 | if (d && launch_data_get_type(d) == LAUNCH_DATA_DICTIONARY) { |
ddbbfbc1 A |
1108 | launch_data_t v = launch_data_dict_lookup(d, LAUNCH_KEY_SUBMITJOB); |
1109 | ||
dcace88f | 1110 | if (v && launch_data_get_type(v) == LAUNCH_DATA_ARRAY) { |
ddbbfbc1 A |
1111 | size_t cnt = launch_data_array_get_count(v); |
1112 | size_t i = 0; | |
1113 | ||
1114 | uuid_generate(uuid); | |
dcace88f | 1115 | for (i = 0; i < cnt; i++) { |
ddbbfbc1 | 1116 | launch_data_t ji = launch_data_array_get_index(v, i); |
dcace88f | 1117 | if (launch_data_get_type(ji) == LAUNCH_DATA_DICTIONARY) { |
ddbbfbc1 | 1118 | launch_data_t existing_v = launch_data_dict_lookup(ji, LAUNCH_JOBKEY_SECURITYSESSIONUUID); |
dcace88f | 1119 | if (!existing_v) { |
ddbbfbc1 A |
1120 | /* I really wish these were reference-counted. Sigh... */ |
1121 | uuid_d = launch_data_new_opaque(uuid, sizeof(uuid)); | |
1122 | launch_data_dict_insert(ji, uuid_d, LAUNCH_JOBKEY_SECURITYSESSIONUUID); | |
1123 | jobs_that_need_sessions++; | |
dcace88f | 1124 | } else if (launch_data_get_type(existing_v) == LAUNCH_DATA_OPAQUE) { |
ddbbfbc1 A |
1125 | jobs_that_need_sessions += uuid_data_is_null(existing_v) ? 0 : 1; |
1126 | } | |
1127 | } | |
1128 | } | |
dcace88f | 1129 | } else if (v && launch_data_get_type(v) == LAUNCH_DATA_DICTIONARY) { |
ddbbfbc1 | 1130 | launch_data_t existing_v = launch_data_dict_lookup(v, LAUNCH_JOBKEY_SECURITYSESSIONUUID); |
dcace88f | 1131 | if (!existing_v) { |
ddbbfbc1 A |
1132 | uuid_generate(uuid); |
1133 | uuid_d = launch_data_new_opaque(uuid, sizeof(uuid)); | |
1134 | launch_data_dict_insert(v, uuid_d, LAUNCH_JOBKEY_SECURITYSESSIONUUID); | |
1135 | jobs_that_need_sessions++; | |
1136 | } else { | |
1137 | jobs_that_need_sessions += uuid_data_is_null(existing_v) ? 0 : 1; | |
1138 | } | |
1139 | } | |
1140 | } | |
1141 | #endif | |
1142 | ||
95379394 | 1143 | pthread_mutex_lock(&globals->lc_mtx); |
e91b9f68 | 1144 | |
95379394 | 1145 | if (d && launchd_msg_send(globals->l, d) == -1) { |
e91b9f68 | 1146 | do { |
ed34e3c3 | 1147 | if (errno != EAGAIN) |
e91b9f68 | 1148 | goto out; |
95379394 | 1149 | } while (launchd_msg_send(globals->l, NULL) == -1); |
e91b9f68 | 1150 | } |
eabd1701 | 1151 | |
e91b9f68 | 1152 | while (resp == NULL) { |
95379394 A |
1153 | if (d == NULL && launch_data_array_get_count(globals->async_resp) > 0) { |
1154 | resp = launch_data_array_pop_first(globals->async_resp); | |
e91b9f68 A |
1155 | goto out; |
1156 | } | |
95379394 | 1157 | if (launchd_msg_recv(globals->l, launch_msg_getmsgs, &resp) == -1) { |
ed34e3c3 | 1158 | if (errno != EAGAIN) { |
e91b9f68 A |
1159 | goto out; |
1160 | } else if (d == NULL) { | |
1161 | errno = 0; | |
1162 | goto out; | |
1163 | } else { | |
1164 | fd_set rfds; | |
1165 | ||
1166 | FD_ZERO(&rfds); | |
ddbbfbc1 | 1167 | FD_SET(fd2use, &rfds); |
eabd1701 | 1168 | |
ddbbfbc1 | 1169 | select(fd2use + 1, &rfds, NULL, NULL, NULL); |
e91b9f68 A |
1170 | } |
1171 | } | |
1172 | } | |
1173 | ||
1174 | out: | |
ddbbfbc1 | 1175 | #if !TARGET_OS_EMBEDDED |
dcace88f | 1176 | if (!uuid_is_null(uuid) && resp && jobs_that_need_sessions > 0) { |
ddbbfbc1 A |
1177 | mach_port_t session_port = _audit_session_self(); |
1178 | launch_data_type_t resp_type = launch_data_get_type(resp); | |
eabd1701 | 1179 | |
ddbbfbc1 | 1180 | bool set_session = false; |
dcace88f | 1181 | if (resp_type == LAUNCH_DATA_ERRNO) { |
eabd1701 | 1182 | set_session = (launch_data_get_errno(resp) == ENEEDAUTH); |
dcace88f | 1183 | } else if (resp_type == LAUNCH_DATA_ARRAY) { |
ddbbfbc1 A |
1184 | set_session = true; |
1185 | } | |
eabd1701 | 1186 | |
ddbbfbc1 | 1187 | kern_return_t kr = KERN_FAILURE; |
dcace88f | 1188 | if (set_session) { |
ddbbfbc1 A |
1189 | kr = vproc_mig_set_security_session(bootstrap_port, uuid, session_port); |
1190 | } | |
eabd1701 | 1191 | |
dcace88f A |
1192 | if (kr == KERN_SUCCESS) { |
1193 | if (resp_type == LAUNCH_DATA_ERRNO) { | |
ddbbfbc1 A |
1194 | launch_data_set_errno(resp, 0); |
1195 | } else { | |
1196 | size_t i = 0; | |
dcace88f | 1197 | for (i = 0; i < launch_data_array_get_count(resp); i++) { |
ddbbfbc1 | 1198 | launch_data_t ri = launch_data_array_get_index(resp, i); |
eabd1701 | 1199 | |
ddbbfbc1 | 1200 | int recvd_err = 0; |
dcace88f | 1201 | if (launch_data_get_type(ri) == LAUNCH_DATA_ERRNO && (recvd_err = launch_data_get_errno(ri))) { |
ddbbfbc1 A |
1202 | launch_data_set_errno(ri, recvd_err == ENEEDAUTH ? 0 : recvd_err); |
1203 | } | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | mach_port_deallocate(mach_task_self(), session_port); | |
1209 | } | |
1210 | #endif | |
1211 | ||
95379394 | 1212 | pthread_mutex_unlock(&globals->lc_mtx); |
e91b9f68 A |
1213 | |
1214 | return resp; | |
1215 | } | |
1216 | ||
ddbbfbc1 A |
1217 | int |
1218 | launchd_msg_recv(launch_t lh, void (*cb)(launch_data_t, void *), void *context) | |
e91b9f68 A |
1219 | { |
1220 | struct cmsghdr *cm = alloca(4096); | |
aa59983a | 1221 | launch_data_t rmsg = NULL; |
e91b9f68 | 1222 | size_t data_offset, fd_offset; |
5b0a4722 A |
1223 | struct msghdr mh; |
1224 | struct iovec iov; | |
e91b9f68 A |
1225 | int r; |
1226 | ||
ddbbfbc1 | 1227 | int fd2use = launchd_getfd(lh); |
dcace88f | 1228 | if (fd2use == -1) { |
ddbbfbc1 A |
1229 | errno = EPERM; |
1230 | return -1; | |
1231 | } | |
1232 | ||
5b0a4722 A |
1233 | memset(&mh, 0, sizeof(mh)); |
1234 | mh.msg_iov = &iov; | |
1235 | mh.msg_iovlen = 1; | |
e91b9f68 | 1236 | |
5b0a4722 | 1237 | lh->recvbuf = reallocf(lh->recvbuf, lh->recvlen + 8*1024); |
e91b9f68 A |
1238 | |
1239 | iov.iov_base = lh->recvbuf + lh->recvlen; | |
1240 | iov.iov_len = 8*1024; | |
1241 | mh.msg_control = cm; | |
1242 | mh.msg_controllen = 4096; | |
1243 | ||
ddbbfbc1 | 1244 | if ((r = recvmsg(fd2use, &mh, 0)) == -1) |
e91b9f68 A |
1245 | return -1; |
1246 | if (r == 0) { | |
1247 | errno = ECONNRESET; | |
1248 | return -1; | |
1249 | } | |
ed34e3c3 | 1250 | if (mh.msg_flags & MSG_CTRUNC) { |
e91b9f68 A |
1251 | errno = ECONNABORTED; |
1252 | return -1; | |
1253 | } | |
1254 | lh->recvlen += r; | |
1255 | if (mh.msg_controllen > 0) { | |
5b0a4722 | 1256 | lh->recvfds = reallocf(lh->recvfds, lh->recvfdcnt * sizeof(int) + mh.msg_controllen - sizeof(struct cmsghdr)); |
e91b9f68 A |
1257 | memcpy(lh->recvfds + lh->recvfdcnt, CMSG_DATA(cm), mh.msg_controllen - sizeof(struct cmsghdr)); |
1258 | lh->recvfdcnt += (mh.msg_controllen - sizeof(struct cmsghdr)) / sizeof(int); | |
1259 | } | |
1260 | ||
aa59983a A |
1261 | r = 0; |
1262 | ||
1263 | while (lh->recvlen > 0) { | |
1264 | struct launch_msg_header *lmhp = lh->recvbuf; | |
1265 | uint64_t tmplen; | |
1266 | data_offset = sizeof(struct launch_msg_header); | |
1267 | fd_offset = 0; | |
1268 | ||
1269 | if (lh->recvlen < sizeof(struct launch_msg_header)) | |
1270 | goto need_more_data; | |
1271 | ||
ddbbfbc1 | 1272 | tmplen = big2wire(lmhp->len); |
aa59983a | 1273 | |
ddbbfbc1 | 1274 | if (big2wire(lmhp->magic) != LAUNCH_MSG_HEADER_MAGIC || tmplen <= sizeof(struct launch_msg_header)) { |
aa59983a A |
1275 | errno = EBADRPC; |
1276 | goto out_bad; | |
1277 | } | |
1278 | ||
1279 | if (lh->recvlen < tmplen) { | |
1280 | goto need_more_data; | |
1281 | } | |
e91b9f68 | 1282 | |
5b0a4722 | 1283 | if ((rmsg = launch_data_unpack(lh->recvbuf, lh->recvlen, lh->recvfds, lh->recvfdcnt, &data_offset, &fd_offset)) == NULL) { |
aa59983a A |
1284 | errno = EBADRPC; |
1285 | goto out_bad; | |
1286 | } | |
e91b9f68 | 1287 | |
95379394 A |
1288 | launch_globals_t globals = _launch_globals(); |
1289 | ||
1290 | globals->in_flight_msg_recv_client = lh; | |
f36da725 | 1291 | |
e91b9f68 A |
1292 | cb(rmsg, context); |
1293 | ||
f36da725 | 1294 | /* launchd and only launchd can call launchd_close() as a part of the callback */ |
95379394 | 1295 | if (globals->in_flight_msg_recv_client == NULL) { |
f36da725 A |
1296 | r = 0; |
1297 | break; | |
1298 | } | |
1299 | ||
e91b9f68 A |
1300 | lh->recvlen -= data_offset; |
1301 | if (lh->recvlen > 0) { | |
1302 | memmove(lh->recvbuf, lh->recvbuf + data_offset, lh->recvlen); | |
1303 | } else { | |
1304 | free(lh->recvbuf); | |
1305 | lh->recvbuf = malloc(0); | |
1306 | } | |
1307 | ||
1308 | lh->recvfdcnt -= fd_offset; | |
1309 | if (lh->recvfdcnt > 0) { | |
1310 | memmove(lh->recvfds, lh->recvfds + fd_offset, lh->recvfdcnt * sizeof(int)); | |
1311 | } else { | |
1312 | free(lh->recvfds); | |
1313 | lh->recvfds = malloc(0); | |
1314 | } | |
e91b9f68 A |
1315 | } |
1316 | ||
1317 | return r; | |
aa59983a A |
1318 | |
1319 | need_more_data: | |
1320 | errno = EAGAIN; | |
1321 | out_bad: | |
1322 | return -1; | |
e91b9f68 A |
1323 | } |
1324 | ||
ddbbfbc1 A |
1325 | launch_data_t |
1326 | launch_data_copy(launch_data_t o) | |
e91b9f68 A |
1327 | { |
1328 | launch_data_t r = launch_data_alloc(o->type); | |
1329 | size_t i; | |
1330 | ||
1331 | free(r->_array); | |
1332 | memcpy(r, o, sizeof(struct _launch_data)); | |
1333 | ||
1334 | switch (o->type) { | |
1335 | case LAUNCH_DATA_DICTIONARY: | |
1336 | case LAUNCH_DATA_ARRAY: | |
1337 | r->_array = calloc(1, o->_array_cnt * sizeof(launch_data_t)); | |
1338 | for (i = 0; i < o->_array_cnt; i++) { | |
1339 | if (o->_array[i]) | |
1340 | r->_array[i] = launch_data_copy(o->_array[i]); | |
1341 | } | |
1342 | break; | |
1343 | case LAUNCH_DATA_STRING: | |
1344 | r->string = strdup(o->string); | |
1345 | break; | |
1346 | case LAUNCH_DATA_OPAQUE: | |
1347 | r->opaque = malloc(o->opaque_size); | |
1348 | memcpy(r->opaque, o->opaque, o->opaque_size); | |
1349 | break; | |
1350 | default: | |
1351 | break; | |
1352 | } | |
1353 | ||
1354 | return r; | |
1355 | } | |
1356 | ||
ddbbfbc1 A |
1357 | int |
1358 | _fd(int fd) | |
e91b9f68 A |
1359 | { |
1360 | if (fd >= 0) | |
ed34e3c3 | 1361 | fcntl(fd, F_SETFD, 1); |
e91b9f68 A |
1362 | return fd; |
1363 | } | |
1364 | ||
ddbbfbc1 A |
1365 | launch_data_t |
1366 | launch_data_new_errno(int e) | |
e91b9f68 A |
1367 | { |
1368 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_ERRNO); | |
1369 | ||
1370 | if (r) | |
5b0a4722 | 1371 | launch_data_set_errno(r, e); |
e91b9f68 A |
1372 | |
1373 | return r; | |
1374 | } | |
1375 | ||
ddbbfbc1 A |
1376 | launch_data_t |
1377 | launch_data_new_fd(int fd) | |
e91b9f68 A |
1378 | { |
1379 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_FD); | |
1380 | ||
1381 | if (r) | |
5b0a4722 | 1382 | launch_data_set_fd(r, fd); |
e91b9f68 A |
1383 | |
1384 | return r; | |
1385 | } | |
1386 | ||
ddbbfbc1 A |
1387 | launch_data_t |
1388 | launch_data_new_machport(mach_port_t p) | |
ed34e3c3 A |
1389 | { |
1390 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_MACHPORT); | |
1391 | ||
1392 | if (r) | |
5b0a4722 | 1393 | launch_data_set_machport(r, p); |
ed34e3c3 A |
1394 | |
1395 | return r; | |
1396 | } | |
1397 | ||
ddbbfbc1 A |
1398 | launch_data_t |
1399 | launch_data_new_integer(long long n) | |
e91b9f68 A |
1400 | { |
1401 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_INTEGER); | |
1402 | ||
1403 | if (r) | |
1404 | launch_data_set_integer(r, n); | |
1405 | ||
1406 | return r; | |
1407 | } | |
1408 | ||
ddbbfbc1 A |
1409 | launch_data_t |
1410 | launch_data_new_bool(bool b) | |
e91b9f68 A |
1411 | { |
1412 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_BOOL); | |
1413 | ||
1414 | if (r) | |
1415 | launch_data_set_bool(r, b); | |
1416 | ||
1417 | return r; | |
1418 | } | |
1419 | ||
ddbbfbc1 A |
1420 | launch_data_t |
1421 | launch_data_new_real(double d) | |
e91b9f68 A |
1422 | { |
1423 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_REAL); | |
1424 | ||
1425 | if (r) | |
1426 | launch_data_set_real(r, d); | |
1427 | ||
1428 | return r; | |
1429 | } | |
1430 | ||
ddbbfbc1 A |
1431 | launch_data_t |
1432 | launch_data_new_string(const char *s) | |
e91b9f68 A |
1433 | { |
1434 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_STRING); | |
1435 | ||
1436 | if (r == NULL) | |
1437 | return NULL; | |
1438 | ||
1439 | if (!launch_data_set_string(r, s)) { | |
1440 | launch_data_free(r); | |
1441 | return NULL; | |
1442 | } | |
1443 | ||
1444 | return r; | |
1445 | } | |
1446 | ||
ddbbfbc1 A |
1447 | launch_data_t |
1448 | launch_data_new_opaque(const void *o, size_t os) | |
e91b9f68 A |
1449 | { |
1450 | launch_data_t r = launch_data_alloc(LAUNCH_DATA_OPAQUE); | |
1451 | ||
1452 | if (r == NULL) | |
1453 | return NULL; | |
1454 | ||
1455 | if (!launch_data_set_opaque(r, o, os)) { | |
1456 | launch_data_free(r); | |
1457 | return NULL; | |
1458 | } | |
1459 | ||
1460 | return r; | |
1461 | } | |
ed34e3c3 | 1462 | |
5b0a4722 A |
1463 | void |
1464 | load_launchd_jobs_at_loginwindow_prompt(int flags __attribute__((unused)), ...) | |
ed34e3c3 | 1465 | { |
ddbbfbc1 | 1466 | _vprocmgr_init(VPROCMGR_SESSION_LOGINWINDOW); |
ed34e3c3 A |
1467 | } |
1468 | ||
1469 | pid_t | |
ddbbfbc1 | 1470 | create_and_switch_to_per_session_launchd(const char *login __attribute__((unused)), int flags, ...) |
ed34e3c3 | 1471 | { |
5b0a4722 | 1472 | uid_t target_user = geteuid() ? geteuid() : getuid(); |
ddbbfbc1 | 1473 | if (_vprocmgr_move_subset_to_user(target_user, VPROCMGR_SESSION_AQUA, flags)) { |
ed34e3c3 | 1474 | return -1; |
5b0a4722 | 1475 | } |
ed34e3c3 | 1476 | |
5b0a4722 | 1477 | return 1; |
ed34e3c3 | 1478 | } |