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"
25 #include "launchd_ktrace.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>
47 /* workaround: 5723161 */
48 #ifndef __DARWIN_ALIGN32
49 #define __DARWIN_ALIGN32(x) (((size_t)(x) + 3) & ~3)
52 #define CMSG_DATA(cmsg) \
53 ((uint8_t *)(cmsg) + __DARWIN_ALIGN32(sizeof(struct cmsghdr)))
55 #define CMSG_SPACE(l) \
56 (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l))
59 (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l))
62 #include "bootstrap.h"
64 #include "vproc_priv.h"
65 #include "vproc_internal.h"
67 /* __OSBogusByteSwap__() must not really exist in the symbol namespace
68 * in order for the following to generate an error at build time.
70 extern void __OSBogusByteSwap__(void);
72 #define host2wire(x) \
73 ({ typeof (x) _X, _x = (x); \
74 switch (sizeof(_x)) { \
76 _X = OSSwapHostToLittleInt64(_x); \
79 _X = OSSwapHostToLittleInt32(_x); \
82 _X = OSSwapHostToLittleInt16(_x); \
88 __OSBogusByteSwap__(); \
96 ({ typeof (x) _X, _x = (x); \
97 switch (sizeof(_x)) { \
99 _X = OSSwapLittleToHostInt64(_x); \
102 _X = OSSwapLittleToHostInt32(_x); \
105 _X = OSSwapLittleToHostInt16(_x); \
111 __OSBogusByteSwap__(); \
118 struct launch_msg_header
{
123 #define LAUNCH_MSG_HEADER_MAGIC 0xD2FEA02366B39A41ull
125 struct _launch_data
{
130 launch_data_t
*_array
;
138 uint64_t opaque_size
;
145 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 */
151 LAUNCHD_USE_CHECKIN_FD
,
152 LAUNCHD_USE_OTHER_FD
,
168 static launch_data_t
launch_data_array_pop_first(launch_data_t where
);
169 static int _fd(int fd
);
170 static void launch_client_init(void);
171 static void launch_msg_getmsgs(launch_data_t m
, void *context
);
172 static launch_data_t
launch_msg_internal(launch_data_t d
);
173 static void launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
);
175 static int64_t s_am_embedded_god
= false;
176 static launch_t in_flight_msg_recv_client
;
177 static pthread_once_t _lc_once
= PTHREAD_ONCE_INIT
;
179 bool do_apple_internal_logging
= false;
181 static struct _launch_client
{
184 launch_data_t async_resp
;
188 launch_client_init(void)
190 struct sockaddr_un sun
;
191 char *where
= getenv(LAUNCHD_SOCKET_ENV
);
192 char *_launchd_fd
= getenv(LAUNCHD_TRUSTED_FD_ENV
);
193 int dfd
, lfd
= -1, cifd
= -1;
196 _lc
= calloc(1, sizeof(struct _launch_client
));
201 pthread_mutex_init(&_lc
->mtx
, NULL
);
204 cifd
= strtol(_launchd_fd
, NULL
, 10);
205 if ((dfd
= dup(cifd
)) >= 0) {
211 unsetenv(LAUNCHD_TRUSTED_FD_ENV
);
214 memset(&sun
, 0, sizeof(sun
));
215 sun
.sun_family
= AF_UNIX
;
217 /* The rules are as follows.
218 * - All users (including root) talk to their per-user launchd's by default.
219 * - If we have been invoked under sudo, talk to the system launchd.
220 * - If we're the root user and the __USE_SYSTEM_LAUNCHD environment variable is set, then
221 * talk to the system launchd.
223 if (where
&& where
[0] != '\0') {
224 strncpy(sun
.sun_path
, where
, sizeof(sun
.sun_path
));
226 if( _vprocmgr_getsocket(spath
) == 0 ) {
227 if( (getenv("SUDO_COMMAND") || getenv("__USE_SYSTEM_LAUNCHD")) && geteuid() == 0 ) {
228 /* Talk to the system launchd. */
229 strncpy(sun
.sun_path
, LAUNCHD_SOCK_PREFIX
"/sock", sizeof(sun
.sun_path
));
231 /* Talk to our per-user launchd. */
234 min_len
= sizeof(sun
.sun_path
) < sizeof(spath
) ? sizeof(sun
.sun_path
) : sizeof(spath
);
236 strncpy(sun
.sun_path
, spath
, min_len
);
241 if ((lfd
= _fd(socket(AF_UNIX
, SOCK_STREAM
, 0))) == -1) {
245 #if TARGET_OS_EMBEDDED
246 (void)vproc_swap_integer(NULL
, VPROC_GSK_EMBEDDEDROOTEQUIVALENT
, NULL
, &s_am_embedded_god
);
248 if (-1 == connect(lfd
, (struct sockaddr
*)&sun
, sizeof(sun
))) {
249 if( cifd
!= -1 || s_am_embedded_god
) {
250 /* There is NO security enforced by this check. This is just a hint to our
251 * library that we shouldn't error out due to failing to open this socket. If
252 * we inherited a trusted file descriptor, we shouldn't fail. This should be
253 * adequate for clients' expectations.
262 if (!(_lc
->l
= launchd_fdopen(lfd
, cifd
))) {
266 if (!(_lc
->async_resp
= launch_data_alloc(LAUNCH_DATA_ARRAY
))) {
273 launchd_close(_lc
->l
, close
);
285 launch_data_alloc(launch_data_type_t t
)
287 launch_data_t d
= calloc(1, sizeof(struct _launch
));
292 case LAUNCH_DATA_DICTIONARY
:
293 case LAUNCH_DATA_ARRAY
:
294 d
->_array
= malloc(0);
305 launch_data_get_type(launch_data_t d
)
311 launch_data_free(launch_data_t d
)
316 case LAUNCH_DATA_DICTIONARY
:
317 case LAUNCH_DATA_ARRAY
:
318 for (i
= 0; i
< d
->_array_cnt
; i
++)
319 launch_data_free(d
->_array
[i
]);
322 case LAUNCH_DATA_STRING
:
326 case LAUNCH_DATA_OPAQUE
:
337 launch_data_dict_get_count(launch_data_t dict
)
339 return dict
->_array_cnt
/ 2;
343 launch_data_dict_insert(launch_data_t dict
, launch_data_t what
, const char *key
)
346 launch_data_t thekey
= launch_data_alloc(LAUNCH_DATA_STRING
);
348 launch_data_set_string(thekey
, key
);
350 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
351 if (!strcasecmp(key
, dict
->_array
[i
]->string
)) {
352 launch_data_array_set_index(dict
, thekey
, i
);
353 launch_data_array_set_index(dict
, what
, i
+ 1);
357 launch_data_array_set_index(dict
, thekey
, i
);
358 launch_data_array_set_index(dict
, what
, i
+ 1);
363 launch_data_dict_lookup(launch_data_t dict
, const char *key
)
367 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
)
370 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
371 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
372 return dict
->_array
[i
+ 1];
379 launch_data_dict_remove(launch_data_t dict
, const char *key
)
383 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
384 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
387 if (i
== dict
->_array_cnt
)
389 launch_data_free(dict
->_array
[i
]);
390 launch_data_free(dict
->_array
[i
+ 1]);
391 memmove(dict
->_array
+ i
, dict
->_array
+ i
+ 2, (dict
->_array_cnt
- (i
+ 2)) * sizeof(launch_data_t
));
392 dict
->_array_cnt
-= 2;
397 launch_data_dict_iterate(launch_data_t dict
, void (*cb
)(launch_data_t
, const char *, void *), void *context
)
401 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
) {
405 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
406 cb(dict
->_array
[i
+ 1], dict
->_array
[i
]->string
, context
);
411 launch_data_array_set_index(launch_data_t where
, launch_data_t what
, size_t ind
)
413 if ((ind
+ 1) >= where
->_array_cnt
) {
414 where
->_array
= reallocf(where
->_array
, (ind
+ 1) * sizeof(launch_data_t
));
415 memset(where
->_array
+ where
->_array_cnt
, 0, (ind
+ 1 - where
->_array_cnt
) * sizeof(launch_data_t
));
416 where
->_array_cnt
= ind
+ 1;
419 if (where
->_array
[ind
]) {
420 launch_data_free(where
->_array
[ind
]);
423 where
->_array
[ind
] = what
;
428 launch_data_array_get_index(launch_data_t where
, size_t ind
)
430 if (LAUNCH_DATA_ARRAY
!= where
->type
|| ind
>= where
->_array_cnt
) {
433 return where
->_array
[ind
];
438 launch_data_array_pop_first(launch_data_t where
)
440 launch_data_t r
= NULL
;
442 if (where
->_array_cnt
> 0) {
443 r
= where
->_array
[0];
444 memmove(where
->_array
, where
->_array
+ 1, (where
->_array_cnt
- 1) * sizeof(launch_data_t
));
451 launch_data_array_get_count(launch_data_t where
)
453 if (LAUNCH_DATA_ARRAY
!= where
->type
)
455 return where
->_array_cnt
;
459 launch_data_set_errno(launch_data_t d
, int e
)
466 launch_data_set_fd(launch_data_t d
, int fd
)
473 launch_data_set_machport(launch_data_t d
, mach_port_t p
)
480 launch_data_set_integer(launch_data_t d
, long long n
)
487 launch_data_set_bool(launch_data_t d
, bool b
)
494 launch_data_set_real(launch_data_t d
, double n
)
501 launch_data_set_string(launch_data_t d
, const char *s
)
505 d
->string
= strdup(s
);
507 d
->string_len
= strlen(d
->string
);
514 launch_data_set_opaque(launch_data_t d
, const void *o
, size_t os
)
519 d
->opaque
= malloc(os
);
521 memcpy(d
->opaque
, o
, os
);
528 launch_data_get_errno(launch_data_t d
)
534 launch_data_get_fd(launch_data_t d
)
540 launch_data_get_machport(launch_data_t d
)
546 launch_data_get_integer(launch_data_t d
)
552 launch_data_get_bool(launch_data_t d
)
558 launch_data_get_real(launch_data_t d
)
564 launch_data_get_string(launch_data_t d
)
566 if (LAUNCH_DATA_STRING
!= d
->type
)
572 launch_data_get_opaque(launch_data_t d
)
574 if (LAUNCH_DATA_OPAQUE
!= d
->type
)
580 launch_data_get_opaque_size(launch_data_t d
)
582 return d
->opaque_size
;
586 launchd_getfd(launch_t l
)
588 return ( l
->which
== LAUNCHD_USE_CHECKIN_FD
) ? l
->cifd
: l
->fd
;
592 launchd_fdopen(int fd
, int cifd
)
596 c
= calloc(1, sizeof(struct _launch
));
603 if( c
->fd
== -1 || (c
->fd
!= -1 && c
->cifd
!= -1) ) {
604 c
->which
= LAUNCHD_USE_CHECKIN_FD
;
605 } else if( c
->cifd
== -1 ) {
606 c
->which
= LAUNCHD_USE_OTHER_FD
;
609 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
610 fcntl(cifd
, F_SETFL
, O_NONBLOCK
);
612 if ((c
->sendbuf
= malloc(0)) == NULL
)
614 if ((c
->sendfds
= malloc(0)) == NULL
)
616 if ((c
->recvbuf
= malloc(0)) == NULL
)
618 if ((c
->recvfds
= malloc(0)) == NULL
)
637 launchd_close(launch_t lh
, typeof(close
) closefunc
)
639 if (in_flight_msg_recv_client
== lh
) {
640 in_flight_msg_recv_client
= NULL
;
656 #define ROUND_TO_64BIT_WORD_SIZE(x) ((x + 7) & ~7)
659 launch_data_pack(launch_data_t d
, void *where
, size_t len
, int *fd_where
, size_t *fd_cnt
)
661 launch_data_t o_in_w
= where
;
662 size_t i
, rsz
, node_data_len
= sizeof(struct _launch_data
);
664 if (node_data_len
> len
) {
668 where
+= node_data_len
;
670 o_in_w
->type
= host2wire(d
->type
);
674 case LAUNCH_DATA_INTEGER
:
675 o_in_w
->number
= host2wire(d
->number
);
677 case LAUNCH_DATA_REAL
:
678 o_in_w
->float_num
= host2wire(d
->float_num
);
680 case LAUNCH_DATA_BOOL
:
681 o_in_w
->boolean
= host2wire(d
->boolean
);
683 case LAUNCH_DATA_ERRNO
:
684 o_in_w
->err
= host2wire(d
->err
);
687 o_in_w
->fd
= host2wire(d
->fd
);
688 if (fd_where
&& d
->fd
!= -1) {
689 fd_where
[*fd_cnt
] = d
->fd
;
693 case LAUNCH_DATA_STRING
:
694 o_in_w
->string_len
= host2wire(d
->string_len
);
695 node_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->string_len
+ 1);
697 if (node_data_len
> len
) {
700 memcpy(where
, d
->string
, d
->string_len
+ 1);
702 /* Zero padded data. */
703 pad_len
= ROUND_TO_64BIT_WORD_SIZE(d
->string_len
+ 1) - (d
->string_len
+ 1);
704 bzero(where
+ d
->string_len
+ 1, pad_len
);
707 case LAUNCH_DATA_OPAQUE
:
708 o_in_w
->opaque_size
= host2wire(d
->opaque_size
);
709 node_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
);
710 if (node_data_len
> len
) {
713 memcpy(where
, d
->opaque
, d
->opaque_size
);
715 /* Zero padded data. */
716 pad_len
= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
) - d
->opaque_size
;
717 bzero(where
+ d
->opaque_size
, pad_len
);
720 case LAUNCH_DATA_DICTIONARY
:
721 case LAUNCH_DATA_ARRAY
:
722 o_in_w
->_array_cnt
= host2wire(d
->_array_cnt
);
723 node_data_len
+= d
->_array_cnt
* sizeof(uint64_t);
724 if (node_data_len
> len
) {
728 where
+= d
->_array_cnt
* sizeof(uint64_t);
730 for (i
= 0; i
< d
->_array_cnt
; i
++) {
731 rsz
= launch_data_pack(d
->_array
[i
], where
, len
- node_data_len
, fd_where
, fd_cnt
);
736 node_data_len
+= rsz
;
743 return node_data_len
;
747 launch_data_unpack(void *data
, size_t data_size
, int *fds
, size_t fd_cnt
, size_t *data_offset
, size_t *fdoffset
)
749 launch_data_t r
= data
+ *data_offset
;
752 if ((data_size
- *data_offset
) < sizeof(struct _launch_data
))
754 *data_offset
+= sizeof(struct _launch_data
);
756 switch (big2wire(r
->type
)) {
757 case LAUNCH_DATA_DICTIONARY
:
758 case LAUNCH_DATA_ARRAY
:
759 tmpcnt
= big2wire(r
->_array_cnt
);
760 if ((data_size
- *data_offset
) < (tmpcnt
* sizeof(uint64_t))) {
764 r
->_array
= data
+ *data_offset
;
765 *data_offset
+= tmpcnt
* sizeof(uint64_t);
766 for (i
= 0; i
< tmpcnt
; i
++) {
767 r
->_array
[i
] = launch_data_unpack(data
, data_size
, fds
, fd_cnt
, data_offset
, fdoffset
);
768 if (r
->_array
[i
] == NULL
)
771 r
->_array_cnt
= tmpcnt
;
773 case LAUNCH_DATA_STRING
:
774 tmpcnt
= big2wire(r
->string_len
);
775 if ((data_size
- *data_offset
) < (tmpcnt
+ 1)) {
779 r
->string
= data
+ *data_offset
;
780 r
->string_len
= tmpcnt
;
781 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
+ 1);
783 case LAUNCH_DATA_OPAQUE
:
784 tmpcnt
= big2wire(r
->opaque_size
);
785 if ((data_size
- *data_offset
) < tmpcnt
) {
789 r
->opaque
= data
+ *data_offset
;
790 r
->opaque_size
= tmpcnt
;
791 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
);
794 if (r
->fd
!= -1 && fd_cnt
> *fdoffset
) {
795 r
->fd
= _fd(fds
[*fdoffset
]);
799 case LAUNCH_DATA_INTEGER
:
800 r
->number
= big2wire(r
->number
);
802 case LAUNCH_DATA_REAL
:
803 r
->float_num
= big2wire(r
->float_num
);
805 case LAUNCH_DATA_BOOL
:
806 r
->boolean
= big2wire(r
->boolean
);
808 case LAUNCH_DATA_ERRNO
:
809 r
->err
= big2wire(r
->err
);
810 case LAUNCH_DATA_MACHPORT
:
818 r
->type
= big2wire(r
->type
);
824 launchd_msg_send(launch_t lh
, launch_data_t d
)
826 struct launch_msg_header lmh
;
827 struct cmsghdr
*cm
= NULL
;
830 size_t sentctrllen
= 0;
833 int fd2use
= launchd_getfd(lh
);
839 memset(&mh
, 0, sizeof(mh
));
841 /* confirm that the next hack works */
842 assert((d
&& lh
->sendlen
== 0) || (!d
&& lh
->sendlen
));
845 size_t fd_slots_used
= 0;
846 size_t good_enough_size
= 10 * 1024 * 1024;
849 /* hack, see the above assert to verify "correctness" */
851 lh
->sendbuf
= malloc(good_enough_size
);
853 lh
->sendfds
= malloc(4 * 1024);
855 lh
->sendlen
= launch_data_pack(d
, lh
->sendbuf
, good_enough_size
, lh
->sendfds
, &fd_slots_used
);
857 if (lh
->sendlen
== 0) {
862 lh
->sendfdcnt
= fd_slots_used
;
864 msglen
= lh
->sendlen
+ sizeof(struct launch_msg_header
); /* type promotion to make the host2wire() macro work right */
865 lmh
.len
= host2wire(msglen
);
866 lmh
.magic
= host2wire(LAUNCH_MSG_HEADER_MAGIC
);
868 iov
[0].iov_base
= &lmh
;
869 iov
[0].iov_len
= sizeof(lmh
);
873 mh
.msg_iov
= iov
+ 1;
877 iov
[1].iov_base
= lh
->sendbuf
;
878 iov
[1].iov_len
= lh
->sendlen
;
881 if (lh
->sendfdcnt
> 0) {
882 sentctrllen
= mh
.msg_controllen
= CMSG_SPACE(lh
->sendfdcnt
* sizeof(int));
883 cm
= alloca(mh
.msg_controllen
);
886 memset(cm
, 0, mh
.msg_controllen
);
888 cm
->cmsg_len
= CMSG_LEN(lh
->sendfdcnt
* sizeof(int));
889 cm
->cmsg_level
= SOL_SOCKET
;
890 cm
->cmsg_type
= SCM_RIGHTS
;
892 memcpy(CMSG_DATA(cm
), lh
->sendfds
, lh
->sendfdcnt
* sizeof(int));
895 if ((r
= sendmsg(fd2use
, &mh
, 0)) == -1) {
900 } else if (sentctrllen
!= mh
.msg_controllen
) {
906 r
-= sizeof(struct launch_msg_header
);
910 if (lh
->sendlen
> 0) {
911 memmove(lh
->sendbuf
, lh
->sendbuf
+ r
, lh
->sendlen
);
914 lh
->sendbuf
= malloc(0);
919 lh
->sendfds
= malloc(0);
921 if (lh
->sendlen
> 0) {
932 pthread_once(&_lc_once
, launch_client_init
);
943 launch_msg_getmsgs(launch_data_t m
, void *context
)
945 launch_data_t async_resp
, *sync_resp
= context
;
947 if ((LAUNCH_DATA_DICTIONARY
== launch_data_get_type(m
)) && (async_resp
= launch_data_dict_lookup(m
, LAUNCHD_ASYNC_MSG_KEY
))) {
948 launch_data_array_set_index(_lc
->async_resp
, launch_data_copy(async_resp
), launch_data_array_get_count(_lc
->async_resp
));
950 *sync_resp
= launch_data_copy(m
);
955 launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
__attribute__((unused
)))
957 kern_return_t result
;
961 strlcpy(srvnm
, key
, sizeof(srvnm
));
963 result
= bootstrap_check_in(bootstrap_port
, srvnm
, &p
);
965 if (result
== BOOTSTRAP_SUCCESS
)
966 launch_data_set_machport(obj
, p
);
970 launch_msg(launch_data_t d
)
972 launch_data_t mps
, r
= launch_msg_internal(d
);
974 if (launch_data_get_type(d
) == LAUNCH_DATA_STRING
) {
975 if (strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) != 0)
979 if (launch_data_get_type(r
) != LAUNCH_DATA_DICTIONARY
)
981 mps
= launch_data_dict_lookup(r
, LAUNCH_JOBKEY_MACHSERVICES
);
984 launch_data_dict_iterate(mps
, launch_mach_checkin_service
, NULL
);
990 extern kern_return_t
vproc_mig_set_security_session(mach_port_t
, uuid_t
, mach_port_t
);
993 uuid_data_is_null(launch_data_t d
)
996 if( launch_data_get_type(d
) == LAUNCH_DATA_OPAQUE
&& launch_data_get_opaque_size(d
) == sizeof(uuid_t
) ) {
997 uuid_t existing_uuid
;
998 memcpy(existing_uuid
, launch_data_get_opaque(d
), sizeof(uuid_t
));
1000 /* A NULL UUID tells us to keep the session inherited from the parent. */
1001 result
= (bool)uuid_is_null(existing_uuid
);
1008 launch_msg_internal(launch_data_t d
)
1010 launch_data_t resp
= NULL
;
1012 if (d
&& (launch_data_get_type(d
) == LAUNCH_DATA_STRING
)
1013 && (strcmp(launch_data_get_string(d
), LAUNCH_KEY_GETJOBS
) == 0)
1014 && vproc_swap_complex(NULL
, VPROC_GSK_ALLJOBS
, NULL
, &resp
) == NULL
) {
1018 pthread_once(&_lc_once
, launch_client_init
);
1025 if( (launch_data_get_type(d
) == LAUNCH_DATA_STRING
&& strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) == 0) || s_am_embedded_god
) {
1026 _lc
->l
->which
= LAUNCHD_USE_CHECKIN_FD
;
1028 _lc
->l
->which
= LAUNCHD_USE_OTHER_FD
;
1031 fd2use
= launchd_getfd(_lc
->l
);
1033 if( fd2use
== -1 ) {
1038 #if !TARGET_OS_EMBEDDED
1040 launch_data_t uuid_d
= NULL
;
1041 size_t jobs_that_need_sessions
= 0;
1042 if( d
&& launch_data_get_type(d
) == LAUNCH_DATA_DICTIONARY
) {
1043 launch_data_t v
= launch_data_dict_lookup(d
, LAUNCH_KEY_SUBMITJOB
);
1045 if( v
&& launch_data_get_type(v
) == LAUNCH_DATA_ARRAY
) {
1046 size_t cnt
= launch_data_array_get_count(v
);
1049 uuid_generate(uuid
);
1050 for( i
= 0; i
< cnt
; i
++ ) {
1051 launch_data_t ji
= launch_data_array_get_index(v
, i
);
1052 if( launch_data_get_type(ji
) == LAUNCH_DATA_DICTIONARY
) {
1053 launch_data_t existing_v
= launch_data_dict_lookup(ji
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1055 /* I really wish these were reference-counted. Sigh... */
1056 uuid_d
= launch_data_new_opaque(uuid
, sizeof(uuid
));
1057 launch_data_dict_insert(ji
, uuid_d
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1058 jobs_that_need_sessions
++;
1059 } else if( launch_data_get_type(existing_v
) == LAUNCH_DATA_OPAQUE
) {
1060 jobs_that_need_sessions
+= uuid_data_is_null(existing_v
) ? 0 : 1;
1064 } else if( v
&& launch_data_get_type(v
) == LAUNCH_DATA_DICTIONARY
) {
1065 launch_data_t existing_v
= launch_data_dict_lookup(v
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1067 uuid_generate(uuid
);
1068 uuid_d
= launch_data_new_opaque(uuid
, sizeof(uuid
));
1069 launch_data_dict_insert(v
, uuid_d
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1070 jobs_that_need_sessions
++;
1072 jobs_that_need_sessions
+= uuid_data_is_null(existing_v
) ? 0 : 1;
1078 pthread_mutex_lock(&_lc
->mtx
);
1080 if (d
&& launchd_msg_send(_lc
->l
, d
) == -1) {
1082 if (errno
!= EAGAIN
)
1084 } while (launchd_msg_send(_lc
->l
, NULL
) == -1);
1087 while (resp
== NULL
) {
1088 if (d
== NULL
&& launch_data_array_get_count(_lc
->async_resp
) > 0) {
1089 resp
= launch_data_array_pop_first(_lc
->async_resp
);
1092 if (launchd_msg_recv(_lc
->l
, launch_msg_getmsgs
, &resp
) == -1) {
1093 if (errno
!= EAGAIN
) {
1095 } else if (d
== NULL
) {
1102 FD_SET(fd2use
, &rfds
);
1104 select(fd2use
+ 1, &rfds
, NULL
, NULL
, NULL
);
1110 #if !TARGET_OS_EMBEDDED
1111 if( !uuid_is_null(uuid
) && resp
&& jobs_that_need_sessions
> 0 ) {
1112 mach_port_t session_port
= _audit_session_self();
1113 launch_data_type_t resp_type
= launch_data_get_type(resp
);
1115 bool set_session
= false;
1116 if( resp_type
== LAUNCH_DATA_ERRNO
) {
1117 set_session
= ( launch_data_get_errno(resp
) == ENEEDAUTH
);
1118 } else if( resp_type
== LAUNCH_DATA_ARRAY
) {
1122 kern_return_t kr
= KERN_FAILURE
;
1124 kr
= vproc_mig_set_security_session(bootstrap_port
, uuid
, session_port
);
1127 if( kr
== KERN_SUCCESS
) {
1128 if( resp_type
== LAUNCH_DATA_ERRNO
) {
1129 launch_data_set_errno(resp
, 0);
1132 for( i
= 0; i
< launch_data_array_get_count(resp
); i
++ ) {
1133 launch_data_t ri
= launch_data_array_get_index(resp
, i
);
1136 if( launch_data_get_type(ri
) == LAUNCH_DATA_ERRNO
&& (recvd_err
= launch_data_get_errno(ri
)) ) {
1137 launch_data_set_errno(ri
, recvd_err
== ENEEDAUTH
? 0 : recvd_err
);
1143 mach_port_deallocate(mach_task_self(), session_port
);
1147 pthread_mutex_unlock(&_lc
->mtx
);
1153 launchd_msg_recv(launch_t lh
, void (*cb
)(launch_data_t
, void *), void *context
)
1155 struct cmsghdr
*cm
= alloca(4096);
1156 launch_data_t rmsg
= NULL
;
1157 size_t data_offset
, fd_offset
;
1162 int fd2use
= launchd_getfd(lh
);
1163 if( fd2use
== -1 ) {
1168 memset(&mh
, 0, sizeof(mh
));
1172 lh
->recvbuf
= reallocf(lh
->recvbuf
, lh
->recvlen
+ 8*1024);
1174 iov
.iov_base
= lh
->recvbuf
+ lh
->recvlen
;
1175 iov
.iov_len
= 8*1024;
1176 mh
.msg_control
= cm
;
1177 mh
.msg_controllen
= 4096;
1179 if ((r
= recvmsg(fd2use
, &mh
, 0)) == -1)
1185 if (mh
.msg_flags
& MSG_CTRUNC
) {
1186 errno
= ECONNABORTED
;
1190 if (mh
.msg_controllen
> 0) {
1191 lh
->recvfds
= reallocf(lh
->recvfds
, lh
->recvfdcnt
* sizeof(int) + mh
.msg_controllen
- sizeof(struct cmsghdr
));
1192 memcpy(lh
->recvfds
+ lh
->recvfdcnt
, CMSG_DATA(cm
), mh
.msg_controllen
- sizeof(struct cmsghdr
));
1193 lh
->recvfdcnt
+= (mh
.msg_controllen
- sizeof(struct cmsghdr
)) / sizeof(int);
1198 while (lh
->recvlen
> 0) {
1199 struct launch_msg_header
*lmhp
= lh
->recvbuf
;
1201 data_offset
= sizeof(struct launch_msg_header
);
1204 if (lh
->recvlen
< sizeof(struct launch_msg_header
))
1205 goto need_more_data
;
1207 tmplen
= big2wire(lmhp
->len
);
1209 if (big2wire(lmhp
->magic
) != LAUNCH_MSG_HEADER_MAGIC
|| tmplen
<= sizeof(struct launch_msg_header
)) {
1214 if (lh
->recvlen
< tmplen
) {
1215 goto need_more_data
;
1218 if ((rmsg
= launch_data_unpack(lh
->recvbuf
, lh
->recvlen
, lh
->recvfds
, lh
->recvfdcnt
, &data_offset
, &fd_offset
)) == NULL
) {
1223 in_flight_msg_recv_client
= lh
;
1227 /* launchd and only launchd can call launchd_close() as a part of the callback */
1228 if (in_flight_msg_recv_client
== NULL
) {
1233 lh
->recvlen
-= data_offset
;
1234 if (lh
->recvlen
> 0) {
1235 memmove(lh
->recvbuf
, lh
->recvbuf
+ data_offset
, lh
->recvlen
);
1238 lh
->recvbuf
= malloc(0);
1241 lh
->recvfdcnt
-= fd_offset
;
1242 if (lh
->recvfdcnt
> 0) {
1243 memmove(lh
->recvfds
, lh
->recvfds
+ fd_offset
, lh
->recvfdcnt
* sizeof(int));
1246 lh
->recvfds
= malloc(0);
1259 launch_data_copy(launch_data_t o
)
1261 launch_data_t r
= launch_data_alloc(o
->type
);
1265 memcpy(r
, o
, sizeof(struct _launch_data
));
1268 case LAUNCH_DATA_DICTIONARY
:
1269 case LAUNCH_DATA_ARRAY
:
1270 r
->_array
= calloc(1, o
->_array_cnt
* sizeof(launch_data_t
));
1271 for (i
= 0; i
< o
->_array_cnt
; i
++) {
1273 r
->_array
[i
] = launch_data_copy(o
->_array
[i
]);
1276 case LAUNCH_DATA_STRING
:
1277 r
->string
= strdup(o
->string
);
1279 case LAUNCH_DATA_OPAQUE
:
1280 r
->opaque
= malloc(o
->opaque_size
);
1281 memcpy(r
->opaque
, o
->opaque
, o
->opaque_size
);
1291 launchd_batch_enable(bool b
)
1295 vproc_swap_integer(NULL
, VPROC_GSK_GLOBAL_ON_DEMAND
, &val
, NULL
);
1299 launchd_batch_query(void)
1303 if (vproc_swap_integer(NULL
, VPROC_GSK_GLOBAL_ON_DEMAND
, NULL
, &val
) == NULL
) {
1314 fcntl(fd
, F_SETFD
, 1);
1319 launch_data_new_errno(int e
)
1321 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_ERRNO
);
1324 launch_data_set_errno(r
, e
);
1330 launch_data_new_fd(int fd
)
1332 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_FD
);
1335 launch_data_set_fd(r
, fd
);
1341 launch_data_new_machport(mach_port_t p
)
1343 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_MACHPORT
);
1346 launch_data_set_machport(r
, p
);
1352 launch_data_new_integer(long long n
)
1354 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_INTEGER
);
1357 launch_data_set_integer(r
, n
);
1363 launch_data_new_bool(bool b
)
1365 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_BOOL
);
1368 launch_data_set_bool(r
, b
);
1374 launch_data_new_real(double d
)
1376 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_REAL
);
1379 launch_data_set_real(r
, d
);
1385 launch_data_new_string(const char *s
)
1387 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_STRING
);
1392 if (!launch_data_set_string(r
, s
)) {
1393 launch_data_free(r
);
1401 launch_data_new_opaque(const void *o
, size_t os
)
1403 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_OPAQUE
);
1408 if (!launch_data_set_opaque(r
, o
, os
)) {
1409 launch_data_free(r
);
1417 load_launchd_jobs_at_loginwindow_prompt(int flags
__attribute__((unused
)), ...)
1419 _vprocmgr_init(VPROCMGR_SESSION_LOGINWINDOW
);
1423 create_and_switch_to_per_session_launchd(const char *login
__attribute__((unused
)), int flags
, ...)
1425 uid_t target_user
= geteuid() ? geteuid() : getuid();
1426 if (_vprocmgr_move_subset_to_user(target_user
, VPROCMGR_SESSION_AQUA
, flags
)) {