2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
23 #include "launch_priv.h"
24 #include "launch_internal.h"
27 #include <mach/mach.h>
28 #include <libkern/OSByteOrder.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/fcntl.h>
43 #include <uuid/uuid.h>
44 #include <sys/syscall.h>
48 /* workaround: 5723161 */
49 #ifndef __DARWIN_ALIGN32
50 #define __DARWIN_ALIGN32(x) (((size_t)(x) + 3) & ~3)
53 #define CMSG_DATA(cmsg) \
54 ((uint8_t *)(cmsg) + __DARWIN_ALIGN32(sizeof(struct cmsghdr)))
56 #define CMSG_SPACE(l) \
57 (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l))
60 (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l))
68 launch_data_t
*_array
;
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 */
88 #include "bootstrap.h"
90 #include "vproc_priv.h"
91 #include "vproc_internal.h"
93 /* __OSBogusByteSwap__() must not really exist in the symbol namespace
94 * in order for the following to generate an error at build time.
96 extern void __OSBogusByteSwap__(void);
98 #define host2wire(x) \
99 ({ typeof (x) _X, _x = (x); \
100 switch (sizeof(_x)) { \
102 _X = OSSwapHostToLittleInt64(_x); \
105 _X = OSSwapHostToLittleInt32(_x); \
108 _X = OSSwapHostToLittleInt16(_x); \
114 __OSBogusByteSwap__(); \
121 #define big2wire(x) \
122 ({ typeof (x) _X, _x = (x); \
123 switch (sizeof(_x)) { \
125 _X = OSSwapLittleToHostInt64(_x); \
128 _X = OSSwapLittleToHostInt32(_x); \
131 _X = OSSwapLittleToHostInt16(_x); \
137 __OSBogusByteSwap__(); \
143 union _launch_double_u
{
148 #define host2wire_f(x) ({ \
149 typeof(x) _F, _f = (x); \
150 union _launch_double_u s; \
152 s.iv = host2wire(s.iv); \
157 #define big2wire_f(x) ({ \
158 typeof(x) _F, _f = (x); \
159 union _launch_double_u s; \
161 s.iv = big2wire(s.iv); \
167 struct launch_msg_header
{
172 #define LAUNCH_MSG_HEADER_MAGIC 0xD2FEA02366B39A41ull
175 LAUNCHD_USE_CHECKIN_FD
,
176 LAUNCHD_USE_OTHER_FD
,
192 static launch_data_t
launch_data_array_pop_first(launch_data_t where
);
193 static int _fd(int fd
);
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
);
199 static int64_t s_am_embedded_god
= false;
200 static launch_t in_flight_msg_recv_client
;
201 static pthread_once_t _lc_once
= PTHREAD_ONCE_INIT
;
203 bool launchd_apple_internal
= false;
205 static struct _launch_client
{
208 launch_data_t async_resp
;
212 launch_client_init(void)
214 struct sockaddr_un sun
;
215 char *where
= getenv(LAUNCHD_SOCKET_ENV
);
216 char *_launchd_fd
= getenv(LAUNCHD_TRUSTED_FD_ENV
);
217 int dfd
, lfd
= -1, cifd
= -1;
220 _lc
= calloc(1, sizeof(struct _launch_client
));
225 pthread_mutex_init(&_lc
->mtx
, NULL
);
228 cifd
= strtol(_launchd_fd
, NULL
, 10);
229 if ((dfd
= dup(cifd
)) >= 0) {
235 unsetenv(LAUNCHD_TRUSTED_FD_ENV
);
238 memset(&sun
, 0, sizeof(sun
));
239 sun
.sun_family
= AF_UNIX
;
241 /* The rules are as follows.
242 * - All users (including root) talk to their per-user launchd's by default.
243 * - If we have been invoked under sudo, talk to the system launchd.
244 * - If we're the root user and the __USE_SYSTEM_LAUNCHD environment variable is set, then
245 * talk to the system launchd.
247 if (where
&& where
[0] != '\0') {
248 strncpy(sun
.sun_path
, where
, sizeof(sun
.sun_path
));
250 if (_vprocmgr_getsocket(spath
) == 0) {
251 if ((getenv("SUDO_COMMAND") || getenv("__USE_SYSTEM_LAUNCHD")) && geteuid() == 0) {
252 /* Talk to the system launchd. */
253 strncpy(sun
.sun_path
, LAUNCHD_SOCK_PREFIX
"/sock", sizeof(sun
.sun_path
));
255 /* Talk to our per-user launchd. */
258 min_len
= sizeof(sun
.sun_path
) < sizeof(spath
) ? sizeof(sun
.sun_path
) : sizeof(spath
);
260 strncpy(sun
.sun_path
, spath
, min_len
);
265 if ((lfd
= _fd(socket(AF_UNIX
, SOCK_STREAM
, 0))) == -1) {
269 #if TARGET_OS_EMBEDDED
270 (void)vproc_swap_integer(NULL
, VPROC_GSK_EMBEDDEDROOTEQUIVALENT
, NULL
, &s_am_embedded_god
);
272 if (-1 == connect(lfd
, (struct sockaddr
*)&sun
, sizeof(sun
))) {
273 if (cifd
!= -1 || s_am_embedded_god
) {
274 /* There is NO security enforced by this check. This is just a hint to our
275 * library that we shouldn't error out due to failing to open this socket. If
276 * we inherited a trusted file descriptor, we shouldn't fail. This should be
277 * adequate for clients' expectations.
286 if (!(_lc
->l
= launchd_fdopen(lfd
, cifd
))) {
290 if (!(_lc
->async_resp
= launch_data_alloc(LAUNCH_DATA_ARRAY
))) {
297 launchd_close(_lc
->l
, close
);
309 launch_data_alloc(launch_data_type_t t
)
311 launch_data_t d
= calloc(1, sizeof(struct _launch_data
));
316 case LAUNCH_DATA_DICTIONARY
:
317 case LAUNCH_DATA_ARRAY
:
318 d
->_array
= malloc(0);
320 case LAUNCH_DATA_OPAQUE
:
321 d
->opaque
= malloc(0);
331 launch_data_get_type(launch_data_t d
)
337 launch_data_free(launch_data_t d
)
342 case LAUNCH_DATA_DICTIONARY
:
343 case LAUNCH_DATA_ARRAY
:
344 for (i
= 0; i
< d
->_array_cnt
; i
++) {
346 launch_data_free(d
->_array
[i
]);
351 case LAUNCH_DATA_STRING
:
355 case LAUNCH_DATA_OPAQUE
:
366 launch_data_dict_get_count(launch_data_t dict
)
368 return dict
->_array_cnt
/ 2;
372 launch_data_dict_insert(launch_data_t dict
, launch_data_t what
, const char *key
)
375 launch_data_t thekey
= launch_data_alloc(LAUNCH_DATA_STRING
);
377 launch_data_set_string(thekey
, key
);
379 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
380 if (!strcasecmp(key
, dict
->_array
[i
]->string
)) {
381 launch_data_array_set_index(dict
, thekey
, i
);
382 launch_data_array_set_index(dict
, what
, i
+ 1);
386 launch_data_array_set_index(dict
, thekey
, i
);
387 launch_data_array_set_index(dict
, what
, i
+ 1);
392 launch_data_dict_lookup(launch_data_t dict
, const char *key
)
396 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
)
399 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
400 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
401 return dict
->_array
[i
+ 1];
408 launch_data_dict_remove(launch_data_t dict
, const char *key
)
412 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
413 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
416 if (i
== dict
->_array_cnt
)
418 launch_data_free(dict
->_array
[i
]);
419 launch_data_free(dict
->_array
[i
+ 1]);
420 memmove(dict
->_array
+ i
, dict
->_array
+ i
+ 2, (dict
->_array_cnt
- (i
+ 2)) * sizeof(launch_data_t
));
421 dict
->_array_cnt
-= 2;
426 launch_data_dict_iterate(launch_data_t dict
, void (*cb
)(launch_data_t
, const char *, void *), void *context
)
430 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
) {
434 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
435 cb(dict
->_array
[i
+ 1], dict
->_array
[i
]->string
, context
);
440 launch_data_array_set_index(launch_data_t where
, launch_data_t what
, size_t ind
)
442 if ((ind
+ 1) >= where
->_array_cnt
) {
443 where
->_array
= reallocf(where
->_array
, (ind
+ 1) * sizeof(launch_data_t
));
444 memset(where
->_array
+ where
->_array_cnt
, 0, (ind
+ 1 - where
->_array_cnt
) * sizeof(launch_data_t
));
445 where
->_array_cnt
= ind
+ 1;
448 if (where
->_array
[ind
]) {
449 launch_data_free(where
->_array
[ind
]);
452 where
->_array
[ind
] = what
;
457 launch_data_array_get_index(launch_data_t where
, size_t ind
)
459 if (LAUNCH_DATA_ARRAY
!= where
->type
|| ind
>= where
->_array_cnt
) {
462 return where
->_array
[ind
];
467 launch_data_array_pop_first(launch_data_t where
)
469 launch_data_t r
= NULL
;
471 if (where
->_array_cnt
> 0) {
472 r
= where
->_array
[0];
473 memmove(where
->_array
, where
->_array
+ 1, (where
->_array_cnt
- 1) * sizeof(launch_data_t
));
480 launch_data_array_get_count(launch_data_t where
)
482 if (LAUNCH_DATA_ARRAY
!= where
->type
)
484 return where
->_array_cnt
;
488 launch_data_set_errno(launch_data_t d
, int e
)
495 launch_data_set_fd(launch_data_t d
, int fd
)
502 launch_data_set_machport(launch_data_t d
, mach_port_t p
)
509 launch_data_set_integer(launch_data_t d
, long long n
)
516 launch_data_set_bool(launch_data_t d
, bool b
)
523 launch_data_set_real(launch_data_t d
, double n
)
530 launch_data_set_string(launch_data_t d
, const char *s
)
534 d
->string
= strdup(s
);
536 d
->string_len
= strlen(d
->string
);
543 launch_data_set_opaque(launch_data_t d
, const void *o
, size_t os
)
548 d
->opaque
= malloc(os
);
550 memcpy(d
->opaque
, o
, os
);
557 launch_data_get_errno(launch_data_t d
)
563 launch_data_get_fd(launch_data_t d
)
569 launch_data_get_machport(launch_data_t d
)
575 launch_data_get_integer(launch_data_t d
)
581 launch_data_get_bool(launch_data_t d
)
587 launch_data_get_real(launch_data_t d
)
593 launch_data_get_string(launch_data_t d
)
595 if (LAUNCH_DATA_STRING
!= d
->type
)
601 launch_data_get_opaque(launch_data_t d
)
603 if (LAUNCH_DATA_OPAQUE
!= d
->type
)
609 launch_data_get_opaque_size(launch_data_t d
)
611 return d
->opaque_size
;
615 launchd_getfd(launch_t l
)
617 return (l
->which
== LAUNCHD_USE_CHECKIN_FD
) ? l
->cifd
: l
->fd
;
621 launchd_fdopen(int fd
, int cifd
)
625 c
= calloc(1, sizeof(struct _launch
));
632 if (c
->fd
== -1 || (c
->fd
!= -1 && c
->cifd
!= -1)) {
633 c
->which
= LAUNCHD_USE_CHECKIN_FD
;
634 } else if (c
->cifd
== -1) {
635 c
->which
= LAUNCHD_USE_OTHER_FD
;
638 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
639 fcntl(cifd
, F_SETFL
, O_NONBLOCK
);
641 if ((c
->sendbuf
= malloc(0)) == NULL
)
643 if ((c
->sendfds
= malloc(0)) == NULL
)
645 if ((c
->recvbuf
= malloc(0)) == NULL
)
647 if ((c
->recvfds
= malloc(0)) == NULL
)
666 launchd_close(launch_t lh
, typeof(close
) closefunc
)
668 if (in_flight_msg_recv_client
== lh
) {
669 in_flight_msg_recv_client
= NULL
;
685 #define ROUND_TO_64BIT_WORD_SIZE(x) ((x + 7) & ~7)
688 launch_data_pack(launch_data_t d
, void *where
, size_t len
, int *fd_where
, size_t *fd_cnt
)
690 launch_data_t o_in_w
= where
;
691 size_t i
, rsz
, node_data_len
= sizeof(struct _launch_data
);
693 if (node_data_len
> len
) {
697 where
+= node_data_len
;
699 o_in_w
->type
= host2wire(d
->type
);
703 case LAUNCH_DATA_INTEGER
:
704 o_in_w
->number
= host2wire(d
->number
);
706 case LAUNCH_DATA_REAL
:
707 o_in_w
->float_num
= host2wire_f(d
->float_num
);
709 case LAUNCH_DATA_BOOL
:
710 o_in_w
->boolean
= host2wire(d
->boolean
);
712 case LAUNCH_DATA_ERRNO
:
713 o_in_w
->err
= host2wire(d
->err
);
716 o_in_w
->fd
= host2wire(d
->fd
);
717 if (fd_where
&& d
->fd
!= -1) {
718 fd_where
[*fd_cnt
] = d
->fd
;
722 case LAUNCH_DATA_STRING
:
723 o_in_w
->string_len
= host2wire(d
->string_len
);
724 node_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->string_len
+ 1);
726 if (node_data_len
> len
) {
729 memcpy(where
, d
->string
, d
->string_len
+ 1);
731 /* Zero padded data. */
732 pad_len
= ROUND_TO_64BIT_WORD_SIZE(d
->string_len
+ 1) - (d
->string_len
+ 1);
733 bzero(where
+ d
->string_len
+ 1, pad_len
);
736 case LAUNCH_DATA_OPAQUE
:
737 o_in_w
->opaque_size
= host2wire(d
->opaque_size
);
738 node_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
);
739 if (node_data_len
> len
) {
742 memcpy(where
, d
->opaque
, d
->opaque_size
);
744 /* Zero padded data. */
745 pad_len
= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
) - d
->opaque_size
;
746 bzero(where
+ d
->opaque_size
, pad_len
);
749 case LAUNCH_DATA_DICTIONARY
:
750 case LAUNCH_DATA_ARRAY
:
751 o_in_w
->_array_cnt
= host2wire(d
->_array_cnt
);
752 node_data_len
+= d
->_array_cnt
* sizeof(uint64_t);
753 if (node_data_len
> len
) {
757 where
+= d
->_array_cnt
* sizeof(uint64_t);
759 for (i
= 0; i
< d
->_array_cnt
; i
++) {
760 rsz
= launch_data_pack(d
->_array
[i
], where
, len
- node_data_len
, fd_where
, fd_cnt
);
765 node_data_len
+= rsz
;
772 return node_data_len
;
776 launch_data_unpack(void *data
, size_t data_size
, int *fds
, size_t fd_cnt
, size_t *data_offset
, size_t *fdoffset
)
778 launch_data_t r
= data
+ *data_offset
;
781 if ((data_size
- *data_offset
) < sizeof(struct _launch_data
))
783 *data_offset
+= sizeof(struct _launch_data
);
785 switch (big2wire(r
->type
)) {
786 case LAUNCH_DATA_DICTIONARY
:
787 case LAUNCH_DATA_ARRAY
:
788 tmpcnt
= big2wire(r
->_array_cnt
);
789 if ((data_size
- *data_offset
) < (tmpcnt
* sizeof(uint64_t))) {
793 r
->_array
= data
+ *data_offset
;
794 *data_offset
+= tmpcnt
* sizeof(uint64_t);
795 for (i
= 0; i
< tmpcnt
; i
++) {
796 r
->_array
[i
] = launch_data_unpack(data
, data_size
, fds
, fd_cnt
, data_offset
, fdoffset
);
797 if (r
->_array
[i
] == NULL
)
800 r
->_array_cnt
= tmpcnt
;
802 case LAUNCH_DATA_STRING
:
803 tmpcnt
= big2wire(r
->string_len
);
804 if ((data_size
- *data_offset
) < (tmpcnt
+ 1)) {
808 r
->string
= data
+ *data_offset
;
809 r
->string_len
= tmpcnt
;
810 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
+ 1);
812 case LAUNCH_DATA_OPAQUE
:
813 tmpcnt
= big2wire(r
->opaque_size
);
814 if ((data_size
- *data_offset
) < tmpcnt
) {
818 r
->opaque
= data
+ *data_offset
;
819 r
->opaque_size
= tmpcnt
;
820 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
);
823 if (r
->fd
!= -1 && fd_cnt
> *fdoffset
) {
824 r
->fd
= _fd(fds
[*fdoffset
]);
828 case LAUNCH_DATA_INTEGER
:
829 r
->number
= big2wire(r
->number
);
831 case LAUNCH_DATA_REAL
:
832 r
->float_num
= big2wire_f(r
->float_num
);
834 case LAUNCH_DATA_BOOL
:
835 r
->boolean
= big2wire(r
->boolean
);
837 case LAUNCH_DATA_ERRNO
:
838 r
->err
= big2wire(r
->err
);
839 case LAUNCH_DATA_MACHPORT
:
847 r
->type
= big2wire(r
->type
);
853 launchd_msg_send(launch_t lh
, launch_data_t d
)
855 struct launch_msg_header lmh
;
856 struct cmsghdr
*cm
= NULL
;
859 size_t sentctrllen
= 0;
862 int fd2use
= launchd_getfd(lh
);
868 memset(&mh
, 0, sizeof(mh
));
870 /* confirm that the next hack works */
871 assert((d
&& lh
->sendlen
== 0) || (!d
&& lh
->sendlen
));
874 size_t fd_slots_used
= 0;
875 size_t good_enough_size
= 10 * 1024 * 1024;
878 /* hack, see the above assert to verify "correctness" */
880 lh
->sendbuf
= malloc(good_enough_size
);
887 lh
->sendfds
= malloc(4 * 1024);
895 lh
->sendlen
= launch_data_pack(d
, lh
->sendbuf
, good_enough_size
, lh
->sendfds
, &fd_slots_used
);
897 if (lh
->sendlen
== 0) {
902 lh
->sendfdcnt
= fd_slots_used
;
904 msglen
= lh
->sendlen
+ sizeof(struct launch_msg_header
); /* type promotion to make the host2wire() macro work right */
905 lmh
.len
= host2wire(msglen
);
906 lmh
.magic
= host2wire(LAUNCH_MSG_HEADER_MAGIC
);
908 iov
[0].iov_base
= &lmh
;
909 iov
[0].iov_len
= sizeof(lmh
);
913 mh
.msg_iov
= iov
+ 1;
917 iov
[1].iov_base
= lh
->sendbuf
;
918 iov
[1].iov_len
= lh
->sendlen
;
921 if (lh
->sendfdcnt
> 0) {
922 sentctrllen
= mh
.msg_controllen
= CMSG_SPACE(lh
->sendfdcnt
* sizeof(int));
923 cm
= alloca(mh
.msg_controllen
);
926 memset(cm
, 0, mh
.msg_controllen
);
928 cm
->cmsg_len
= CMSG_LEN(lh
->sendfdcnt
* sizeof(int));
929 cm
->cmsg_level
= SOL_SOCKET
;
930 cm
->cmsg_type
= SCM_RIGHTS
;
932 memcpy(CMSG_DATA(cm
), lh
->sendfds
, lh
->sendfdcnt
* sizeof(int));
935 if ((r
= sendmsg(fd2use
, &mh
, 0)) == -1) {
940 } else if (sentctrllen
!= mh
.msg_controllen
) {
946 r
-= sizeof(struct launch_msg_header
);
950 if (lh
->sendlen
> 0) {
951 memmove(lh
->sendbuf
, lh
->sendbuf
+ r
, lh
->sendlen
);
954 lh
->sendbuf
= malloc(0);
959 lh
->sendfds
= malloc(0);
961 if (lh
->sendlen
> 0) {
972 pthread_once(&_lc_once
, launch_client_init
);
983 launch_msg_getmsgs(launch_data_t m
, void *context
)
985 launch_data_t async_resp
, *sync_resp
= context
;
987 if ((LAUNCH_DATA_DICTIONARY
== launch_data_get_type(m
)) && (async_resp
= launch_data_dict_lookup(m
, LAUNCHD_ASYNC_MSG_KEY
))) {
988 launch_data_array_set_index(_lc
->async_resp
, launch_data_copy(async_resp
), launch_data_array_get_count(_lc
->async_resp
));
990 *sync_resp
= launch_data_copy(m
);
995 launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
__attribute__((unused
)))
997 kern_return_t result
;
1001 strlcpy(srvnm
, key
, sizeof(srvnm
));
1003 result
= bootstrap_check_in(bootstrap_port
, srvnm
, &p
);
1005 if (result
== BOOTSTRAP_SUCCESS
)
1006 launch_data_set_machport(obj
, p
);
1010 launch_msg(launch_data_t d
)
1012 launch_data_t mps
, r
= launch_msg_internal(d
);
1014 if (launch_data_get_type(d
) == LAUNCH_DATA_STRING
) {
1015 if (strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) != 0)
1019 if (launch_data_get_type(r
) != LAUNCH_DATA_DICTIONARY
)
1021 mps
= launch_data_dict_lookup(r
, LAUNCH_JOBKEY_MACHSERVICES
);
1024 launch_data_dict_iterate(mps
, launch_mach_checkin_service
, NULL
);
1030 extern kern_return_t
vproc_mig_set_security_session(mach_port_t
, uuid_t
, mach_port_t
);
1033 uuid_data_is_null(launch_data_t d
)
1035 bool result
= false;
1036 if (launch_data_get_type(d
) == LAUNCH_DATA_OPAQUE
&& launch_data_get_opaque_size(d
) == sizeof(uuid_t
)) {
1037 uuid_t existing_uuid
;
1038 memcpy(existing_uuid
, launch_data_get_opaque(d
), sizeof(uuid_t
));
1040 /* A NULL UUID tells us to keep the session inherited from the parent. */
1041 result
= (bool)uuid_is_null(existing_uuid
);
1048 launch_msg_internal(launch_data_t d
)
1050 launch_data_t resp
= NULL
;
1052 if (d
&& (launch_data_get_type(d
) == LAUNCH_DATA_STRING
)
1053 && (strcmp(launch_data_get_string(d
), LAUNCH_KEY_GETJOBS
) == 0)
1054 && vproc_swap_complex(NULL
, VPROC_GSK_ALLJOBS
, NULL
, &resp
) == NULL
) {
1058 pthread_once(&_lc_once
, launch_client_init
);
1065 if ((launch_data_get_type(d
) == LAUNCH_DATA_STRING
&& strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) == 0) || s_am_embedded_god
) {
1066 _lc
->l
->which
= LAUNCHD_USE_CHECKIN_FD
;
1068 _lc
->l
->which
= LAUNCHD_USE_OTHER_FD
;
1071 fd2use
= launchd_getfd(_lc
->l
);
1078 #if !TARGET_OS_EMBEDDED
1080 launch_data_t uuid_d
= NULL
;
1081 size_t jobs_that_need_sessions
= 0;
1082 if (d
&& launch_data_get_type(d
) == LAUNCH_DATA_DICTIONARY
) {
1083 launch_data_t v
= launch_data_dict_lookup(d
, LAUNCH_KEY_SUBMITJOB
);
1085 if (v
&& launch_data_get_type(v
) == LAUNCH_DATA_ARRAY
) {
1086 size_t cnt
= launch_data_array_get_count(v
);
1089 uuid_generate(uuid
);
1090 for (i
= 0; i
< cnt
; i
++) {
1091 launch_data_t ji
= launch_data_array_get_index(v
, i
);
1092 if (launch_data_get_type(ji
) == LAUNCH_DATA_DICTIONARY
) {
1093 launch_data_t existing_v
= launch_data_dict_lookup(ji
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1095 /* I really wish these were reference-counted. Sigh... */
1096 uuid_d
= launch_data_new_opaque(uuid
, sizeof(uuid
));
1097 launch_data_dict_insert(ji
, uuid_d
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1098 jobs_that_need_sessions
++;
1099 } else if (launch_data_get_type(existing_v
) == LAUNCH_DATA_OPAQUE
) {
1100 jobs_that_need_sessions
+= uuid_data_is_null(existing_v
) ? 0 : 1;
1104 } else if (v
&& launch_data_get_type(v
) == LAUNCH_DATA_DICTIONARY
) {
1105 launch_data_t existing_v
= launch_data_dict_lookup(v
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1107 uuid_generate(uuid
);
1108 uuid_d
= launch_data_new_opaque(uuid
, sizeof(uuid
));
1109 launch_data_dict_insert(v
, uuid_d
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1110 jobs_that_need_sessions
++;
1112 jobs_that_need_sessions
+= uuid_data_is_null(existing_v
) ? 0 : 1;
1118 pthread_mutex_lock(&_lc
->mtx
);
1120 if (d
&& launchd_msg_send(_lc
->l
, d
) == -1) {
1122 if (errno
!= EAGAIN
)
1124 } while (launchd_msg_send(_lc
->l
, NULL
) == -1);
1127 while (resp
== NULL
) {
1128 if (d
== NULL
&& launch_data_array_get_count(_lc
->async_resp
) > 0) {
1129 resp
= launch_data_array_pop_first(_lc
->async_resp
);
1132 if (launchd_msg_recv(_lc
->l
, launch_msg_getmsgs
, &resp
) == -1) {
1133 if (errno
!= EAGAIN
) {
1135 } else if (d
== NULL
) {
1142 FD_SET(fd2use
, &rfds
);
1144 select(fd2use
+ 1, &rfds
, NULL
, NULL
, NULL
);
1150 #if !TARGET_OS_EMBEDDED
1151 if (!uuid_is_null(uuid
) && resp
&& jobs_that_need_sessions
> 0) {
1152 mach_port_t session_port
= _audit_session_self();
1153 launch_data_type_t resp_type
= launch_data_get_type(resp
);
1155 bool set_session
= false;
1156 if (resp_type
== LAUNCH_DATA_ERRNO
) {
1157 set_session
= (launch_data_get_errno(resp
) == ENEEDAUTH
);
1158 } else if (resp_type
== LAUNCH_DATA_ARRAY
) {
1162 kern_return_t kr
= KERN_FAILURE
;
1164 kr
= vproc_mig_set_security_session(bootstrap_port
, uuid
, session_port
);
1167 if (kr
== KERN_SUCCESS
) {
1168 if (resp_type
== LAUNCH_DATA_ERRNO
) {
1169 launch_data_set_errno(resp
, 0);
1172 for (i
= 0; i
< launch_data_array_get_count(resp
); i
++) {
1173 launch_data_t ri
= launch_data_array_get_index(resp
, i
);
1176 if (launch_data_get_type(ri
) == LAUNCH_DATA_ERRNO
&& (recvd_err
= launch_data_get_errno(ri
))) {
1177 launch_data_set_errno(ri
, recvd_err
== ENEEDAUTH
? 0 : recvd_err
);
1183 mach_port_deallocate(mach_task_self(), session_port
);
1187 pthread_mutex_unlock(&_lc
->mtx
);
1193 launchd_msg_recv(launch_t lh
, void (*cb
)(launch_data_t
, void *), void *context
)
1195 struct cmsghdr
*cm
= alloca(4096);
1196 launch_data_t rmsg
= NULL
;
1197 size_t data_offset
, fd_offset
;
1202 int fd2use
= launchd_getfd(lh
);
1208 memset(&mh
, 0, sizeof(mh
));
1212 lh
->recvbuf
= reallocf(lh
->recvbuf
, lh
->recvlen
+ 8*1024);
1214 iov
.iov_base
= lh
->recvbuf
+ lh
->recvlen
;
1215 iov
.iov_len
= 8*1024;
1216 mh
.msg_control
= cm
;
1217 mh
.msg_controllen
= 4096;
1219 if ((r
= recvmsg(fd2use
, &mh
, 0)) == -1)
1225 if (mh
.msg_flags
& MSG_CTRUNC
) {
1226 errno
= ECONNABORTED
;
1230 if (mh
.msg_controllen
> 0) {
1231 lh
->recvfds
= reallocf(lh
->recvfds
, lh
->recvfdcnt
* sizeof(int) + mh
.msg_controllen
- sizeof(struct cmsghdr
));
1232 memcpy(lh
->recvfds
+ lh
->recvfdcnt
, CMSG_DATA(cm
), mh
.msg_controllen
- sizeof(struct cmsghdr
));
1233 lh
->recvfdcnt
+= (mh
.msg_controllen
- sizeof(struct cmsghdr
)) / sizeof(int);
1238 while (lh
->recvlen
> 0) {
1239 struct launch_msg_header
*lmhp
= lh
->recvbuf
;
1241 data_offset
= sizeof(struct launch_msg_header
);
1244 if (lh
->recvlen
< sizeof(struct launch_msg_header
))
1245 goto need_more_data
;
1247 tmplen
= big2wire(lmhp
->len
);
1249 if (big2wire(lmhp
->magic
) != LAUNCH_MSG_HEADER_MAGIC
|| tmplen
<= sizeof(struct launch_msg_header
)) {
1254 if (lh
->recvlen
< tmplen
) {
1255 goto need_more_data
;
1258 if ((rmsg
= launch_data_unpack(lh
->recvbuf
, lh
->recvlen
, lh
->recvfds
, lh
->recvfdcnt
, &data_offset
, &fd_offset
)) == NULL
) {
1263 in_flight_msg_recv_client
= lh
;
1267 /* launchd and only launchd can call launchd_close() as a part of the callback */
1268 if (in_flight_msg_recv_client
== NULL
) {
1273 lh
->recvlen
-= data_offset
;
1274 if (lh
->recvlen
> 0) {
1275 memmove(lh
->recvbuf
, lh
->recvbuf
+ data_offset
, lh
->recvlen
);
1278 lh
->recvbuf
= malloc(0);
1281 lh
->recvfdcnt
-= fd_offset
;
1282 if (lh
->recvfdcnt
> 0) {
1283 memmove(lh
->recvfds
, lh
->recvfds
+ fd_offset
, lh
->recvfdcnt
* sizeof(int));
1286 lh
->recvfds
= malloc(0);
1299 launch_data_copy(launch_data_t o
)
1301 launch_data_t r
= launch_data_alloc(o
->type
);
1305 memcpy(r
, o
, sizeof(struct _launch_data
));
1308 case LAUNCH_DATA_DICTIONARY
:
1309 case LAUNCH_DATA_ARRAY
:
1310 r
->_array
= calloc(1, o
->_array_cnt
* sizeof(launch_data_t
));
1311 for (i
= 0; i
< o
->_array_cnt
; i
++) {
1313 r
->_array
[i
] = launch_data_copy(o
->_array
[i
]);
1316 case LAUNCH_DATA_STRING
:
1317 r
->string
= strdup(o
->string
);
1319 case LAUNCH_DATA_OPAQUE
:
1320 r
->opaque
= malloc(o
->opaque_size
);
1321 memcpy(r
->opaque
, o
->opaque
, o
->opaque_size
);
1334 fcntl(fd
, F_SETFD
, 1);
1339 launch_data_new_errno(int e
)
1341 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_ERRNO
);
1344 launch_data_set_errno(r
, e
);
1350 launch_data_new_fd(int fd
)
1352 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_FD
);
1355 launch_data_set_fd(r
, fd
);
1361 launch_data_new_machport(mach_port_t p
)
1363 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_MACHPORT
);
1366 launch_data_set_machport(r
, p
);
1372 launch_data_new_integer(long long n
)
1374 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_INTEGER
);
1377 launch_data_set_integer(r
, n
);
1383 launch_data_new_bool(bool b
)
1385 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_BOOL
);
1388 launch_data_set_bool(r
, b
);
1394 launch_data_new_real(double d
)
1396 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_REAL
);
1399 launch_data_set_real(r
, d
);
1405 launch_data_new_string(const char *s
)
1407 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_STRING
);
1412 if (!launch_data_set_string(r
, s
)) {
1413 launch_data_free(r
);
1421 launch_data_new_opaque(const void *o
, size_t os
)
1423 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_OPAQUE
);
1428 if (!launch_data_set_opaque(r
, o
, os
)) {
1429 launch_data_free(r
);
1437 load_launchd_jobs_at_loginwindow_prompt(int flags
__attribute__((unused
)), ...)
1439 _vprocmgr_init(VPROCMGR_SESSION_LOGINWINDOW
);
1443 create_and_switch_to_per_session_launchd(const char *login
__attribute__((unused
)), int flags
, ...)
1445 uid_t target_user
= geteuid() ? geteuid() : getuid();
1446 if (_vprocmgr_move_subset_to_user(target_user
, VPROCMGR_SESSION_AQUA
, flags
)) {