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>
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))
63 #include "bootstrap.h"
65 #include "vproc_priv.h"
66 #include "vproc_internal.h"
68 /* __OSBogusByteSwap__() must not really exist in the symbol namespace
69 * in order for the following to generate an error at build time.
71 extern void __OSBogusByteSwap__(void);
73 #define host2wire(x) \
74 ({ typeof (x) _X, _x = (x); \
75 switch (sizeof(_x)) { \
77 _X = OSSwapHostToLittleInt64(_x); \
80 _X = OSSwapHostToLittleInt32(_x); \
83 _X = OSSwapHostToLittleInt16(_x); \
89 __OSBogusByteSwap__(); \
97 ({ typeof (x) _X, _x = (x); \
98 switch (sizeof(_x)) { \
100 _X = OSSwapLittleToHostInt64(_x); \
103 _X = OSSwapLittleToHostInt32(_x); \
106 _X = OSSwapLittleToHostInt16(_x); \
112 __OSBogusByteSwap__(); \
119 struct launch_msg_header
{
124 #define LAUNCH_MSG_HEADER_MAGIC 0xD2FEA02366B39A41ull
127 LAUNCHD_USE_CHECKIN_FD
,
128 LAUNCHD_USE_OTHER_FD
,
144 static launch_data_t
launch_data_array_pop_first(launch_data_t where
);
145 static int _fd(int fd
);
146 static void launch_client_init(void);
147 static void launch_msg_getmsgs(launch_data_t m
, void *context
);
148 static launch_data_t
launch_msg_internal(launch_data_t d
);
149 static void launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
);
151 static int64_t s_am_embedded_god
= false;
152 static launch_t in_flight_msg_recv_client
;
153 static pthread_once_t _lc_once
= PTHREAD_ONCE_INIT
;
155 bool do_apple_internal_logging
= false;
157 static struct _launch_client
{
160 launch_data_t async_resp
;
164 launch_client_init(void)
166 struct sockaddr_un sun
;
167 char *where
= getenv(LAUNCHD_SOCKET_ENV
);
168 char *_launchd_fd
= getenv(LAUNCHD_TRUSTED_FD_ENV
);
169 int dfd
, lfd
= -1, cifd
= -1;
172 _lc
= calloc(1, sizeof(struct _launch_client
));
177 pthread_mutex_init(&_lc
->mtx
, NULL
);
180 cifd
= strtol(_launchd_fd
, NULL
, 10);
181 if ((dfd
= dup(cifd
)) >= 0) {
187 unsetenv(LAUNCHD_TRUSTED_FD_ENV
);
190 memset(&sun
, 0, sizeof(sun
));
191 sun
.sun_family
= AF_UNIX
;
193 /* The rules are as follows.
194 * - All users (including root) talk to their per-user launchd's by default.
195 * - If we have been invoked under sudo, talk to the system launchd.
196 * - If we're the root user and the __USE_SYSTEM_LAUNCHD environment variable is set, then
197 * talk to the system launchd.
199 if (where
&& where
[0] != '\0') {
200 strncpy(sun
.sun_path
, where
, sizeof(sun
.sun_path
));
202 if (_vprocmgr_getsocket(spath
) == 0) {
203 if ((getenv("SUDO_COMMAND") || getenv("__USE_SYSTEM_LAUNCHD")) && geteuid() == 0) {
204 /* Talk to the system launchd. */
205 strncpy(sun
.sun_path
, LAUNCHD_SOCK_PREFIX
"/sock", sizeof(sun
.sun_path
));
207 /* Talk to our per-user launchd. */
210 min_len
= sizeof(sun
.sun_path
) < sizeof(spath
) ? sizeof(sun
.sun_path
) : sizeof(spath
);
212 strncpy(sun
.sun_path
, spath
, min_len
);
217 if ((lfd
= _fd(socket(AF_UNIX
, SOCK_STREAM
, 0))) == -1) {
221 #if TARGET_OS_EMBEDDED
222 (void)vproc_swap_integer(NULL
, VPROC_GSK_EMBEDDEDROOTEQUIVALENT
, NULL
, &s_am_embedded_god
);
224 if (-1 == connect(lfd
, (struct sockaddr
*)&sun
, sizeof(sun
))) {
225 if (cifd
!= -1 || s_am_embedded_god
) {
226 /* There is NO security enforced by this check. This is just a hint to our
227 * library that we shouldn't error out due to failing to open this socket. If
228 * we inherited a trusted file descriptor, we shouldn't fail. This should be
229 * adequate for clients' expectations.
238 if (!(_lc
->l
= launchd_fdopen(lfd
, cifd
))) {
242 if (!(_lc
->async_resp
= launch_data_alloc(LAUNCH_DATA_ARRAY
))) {
249 launchd_close(_lc
->l
, close
);
261 launch_data_alloc(launch_data_type_t t
)
263 launch_data_t d
= calloc(1, sizeof(struct _launch_data
));
268 case LAUNCH_DATA_DICTIONARY
:
269 case LAUNCH_DATA_ARRAY
:
270 d
->_array
= malloc(0);
272 case LAUNCH_DATA_OPAQUE
:
273 d
->opaque
= malloc(0);
283 launch_data_get_type(launch_data_t d
)
289 launch_data_free(launch_data_t d
)
294 case LAUNCH_DATA_DICTIONARY
:
295 case LAUNCH_DATA_ARRAY
:
296 for (i
= 0; i
< d
->_array_cnt
; i
++) {
298 launch_data_free(d
->_array
[i
]);
303 case LAUNCH_DATA_STRING
:
307 case LAUNCH_DATA_OPAQUE
:
318 launch_data_dict_get_count(launch_data_t dict
)
320 return dict
->_array_cnt
/ 2;
324 launch_data_dict_insert(launch_data_t dict
, launch_data_t what
, const char *key
)
327 launch_data_t thekey
= launch_data_alloc(LAUNCH_DATA_STRING
);
329 launch_data_set_string(thekey
, key
);
331 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
332 if (!strcasecmp(key
, dict
->_array
[i
]->string
)) {
333 launch_data_array_set_index(dict
, thekey
, i
);
334 launch_data_array_set_index(dict
, what
, i
+ 1);
338 launch_data_array_set_index(dict
, thekey
, i
);
339 launch_data_array_set_index(dict
, what
, i
+ 1);
344 launch_data_dict_lookup(launch_data_t dict
, const char *key
)
348 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
)
351 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
352 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
353 return dict
->_array
[i
+ 1];
360 launch_data_dict_remove(launch_data_t dict
, const char *key
)
364 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
365 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
368 if (i
== dict
->_array_cnt
)
370 launch_data_free(dict
->_array
[i
]);
371 launch_data_free(dict
->_array
[i
+ 1]);
372 memmove(dict
->_array
+ i
, dict
->_array
+ i
+ 2, (dict
->_array_cnt
- (i
+ 2)) * sizeof(launch_data_t
));
373 dict
->_array_cnt
-= 2;
378 launch_data_dict_iterate(launch_data_t dict
, void (*cb
)(launch_data_t
, const char *, void *), void *context
)
382 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
) {
386 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
387 cb(dict
->_array
[i
+ 1], dict
->_array
[i
]->string
, context
);
392 launch_data_array_set_index(launch_data_t where
, launch_data_t what
, size_t ind
)
394 if ((ind
+ 1) >= where
->_array_cnt
) {
395 where
->_array
= reallocf(where
->_array
, (ind
+ 1) * sizeof(launch_data_t
));
396 memset(where
->_array
+ where
->_array_cnt
, 0, (ind
+ 1 - where
->_array_cnt
) * sizeof(launch_data_t
));
397 where
->_array_cnt
= ind
+ 1;
400 if (where
->_array
[ind
]) {
401 launch_data_free(where
->_array
[ind
]);
404 where
->_array
[ind
] = what
;
409 launch_data_array_get_index(launch_data_t where
, size_t ind
)
411 if (LAUNCH_DATA_ARRAY
!= where
->type
|| ind
>= where
->_array_cnt
) {
414 return where
->_array
[ind
];
419 launch_data_array_pop_first(launch_data_t where
)
421 launch_data_t r
= NULL
;
423 if (where
->_array_cnt
> 0) {
424 r
= where
->_array
[0];
425 memmove(where
->_array
, where
->_array
+ 1, (where
->_array_cnt
- 1) * sizeof(launch_data_t
));
432 launch_data_array_get_count(launch_data_t where
)
434 if (LAUNCH_DATA_ARRAY
!= where
->type
)
436 return where
->_array_cnt
;
440 launch_data_set_errno(launch_data_t d
, int e
)
447 launch_data_set_fd(launch_data_t d
, int fd
)
454 launch_data_set_machport(launch_data_t d
, mach_port_t p
)
461 launch_data_set_integer(launch_data_t d
, long long n
)
468 launch_data_set_bool(launch_data_t d
, bool b
)
475 launch_data_set_real(launch_data_t d
, double n
)
482 launch_data_set_string(launch_data_t d
, const char *s
)
486 d
->string
= strdup(s
);
488 d
->string_len
= strlen(d
->string
);
495 launch_data_set_opaque(launch_data_t d
, const void *o
, size_t os
)
500 d
->opaque
= malloc(os
);
502 memcpy(d
->opaque
, o
, os
);
509 launch_data_get_errno(launch_data_t d
)
515 launch_data_get_fd(launch_data_t d
)
521 launch_data_get_machport(launch_data_t d
)
527 launch_data_get_integer(launch_data_t d
)
533 launch_data_get_bool(launch_data_t d
)
539 launch_data_get_real(launch_data_t d
)
545 launch_data_get_string(launch_data_t d
)
547 if (LAUNCH_DATA_STRING
!= d
->type
)
553 launch_data_get_opaque(launch_data_t d
)
555 if (LAUNCH_DATA_OPAQUE
!= d
->type
)
561 launch_data_get_opaque_size(launch_data_t d
)
563 return d
->opaque_size
;
567 launchd_getfd(launch_t l
)
569 return ( l
->which
== LAUNCHD_USE_CHECKIN_FD
) ? l
->cifd
: l
->fd
;
573 launchd_fdopen(int fd
, int cifd
)
577 c
= calloc(1, sizeof(struct _launch
));
584 if (c
->fd
== -1 || (c
->fd
!= -1 && c
->cifd
!= -1)) {
585 c
->which
= LAUNCHD_USE_CHECKIN_FD
;
586 } else if (c
->cifd
== -1) {
587 c
->which
= LAUNCHD_USE_OTHER_FD
;
590 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
591 fcntl(cifd
, F_SETFL
, O_NONBLOCK
);
593 if ((c
->sendbuf
= malloc(0)) == NULL
)
595 if ((c
->sendfds
= malloc(0)) == NULL
)
597 if ((c
->recvbuf
= malloc(0)) == NULL
)
599 if ((c
->recvfds
= malloc(0)) == NULL
)
618 launchd_close(launch_t lh
, typeof(close
) closefunc
)
620 if (in_flight_msg_recv_client
== lh
) {
621 in_flight_msg_recv_client
= NULL
;
637 #define ROUND_TO_64BIT_WORD_SIZE(x) ((x + 7) & ~7)
640 launch_data_pack(launch_data_t d
, void *where
, size_t len
, int *fd_where
, size_t *fd_cnt
)
642 launch_data_t o_in_w
= where
;
643 size_t i
, rsz
, node_data_len
= sizeof(struct _launch_data
);
645 if (node_data_len
> len
) {
649 where
+= node_data_len
;
651 o_in_w
->type
= host2wire(d
->type
);
655 case LAUNCH_DATA_INTEGER
:
656 o_in_w
->number
= host2wire(d
->number
);
658 case LAUNCH_DATA_REAL
:
659 o_in_w
->float_num
= host2wire(d
->float_num
);
661 case LAUNCH_DATA_BOOL
:
662 o_in_w
->boolean
= host2wire(d
->boolean
);
664 case LAUNCH_DATA_ERRNO
:
665 o_in_w
->err
= host2wire(d
->err
);
668 o_in_w
->fd
= host2wire(d
->fd
);
669 if (fd_where
&& d
->fd
!= -1) {
670 fd_where
[*fd_cnt
] = d
->fd
;
674 case LAUNCH_DATA_STRING
:
675 o_in_w
->string_len
= host2wire(d
->string_len
);
676 node_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->string_len
+ 1);
678 if (node_data_len
> len
) {
681 memcpy(where
, d
->string
, d
->string_len
+ 1);
683 /* Zero padded data. */
684 pad_len
= ROUND_TO_64BIT_WORD_SIZE(d
->string_len
+ 1) - (d
->string_len
+ 1);
685 bzero(where
+ d
->string_len
+ 1, pad_len
);
688 case LAUNCH_DATA_OPAQUE
:
689 o_in_w
->opaque_size
= host2wire(d
->opaque_size
);
690 node_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
);
691 if (node_data_len
> len
) {
694 memcpy(where
, d
->opaque
, d
->opaque_size
);
696 /* Zero padded data. */
697 pad_len
= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
) - d
->opaque_size
;
698 bzero(where
+ d
->opaque_size
, pad_len
);
701 case LAUNCH_DATA_DICTIONARY
:
702 case LAUNCH_DATA_ARRAY
:
703 o_in_w
->_array_cnt
= host2wire(d
->_array_cnt
);
704 node_data_len
+= d
->_array_cnt
* sizeof(uint64_t);
705 if (node_data_len
> len
) {
709 where
+= d
->_array_cnt
* sizeof(uint64_t);
711 for (i
= 0; i
< d
->_array_cnt
; i
++) {
712 rsz
= launch_data_pack(d
->_array
[i
], where
, len
- node_data_len
, fd_where
, fd_cnt
);
717 node_data_len
+= rsz
;
724 return node_data_len
;
728 launch_data_unpack(void *data
, size_t data_size
, int *fds
, size_t fd_cnt
, size_t *data_offset
, size_t *fdoffset
)
730 launch_data_t r
= data
+ *data_offset
;
733 if ((data_size
- *data_offset
) < sizeof(struct _launch_data
))
735 *data_offset
+= sizeof(struct _launch_data
);
737 switch (big2wire(r
->type
)) {
738 case LAUNCH_DATA_DICTIONARY
:
739 case LAUNCH_DATA_ARRAY
:
740 tmpcnt
= big2wire(r
->_array_cnt
);
741 if ((data_size
- *data_offset
) < (tmpcnt
* sizeof(uint64_t))) {
745 r
->_array
= data
+ *data_offset
;
746 *data_offset
+= tmpcnt
* sizeof(uint64_t);
747 for (i
= 0; i
< tmpcnt
; i
++) {
748 r
->_array
[i
] = launch_data_unpack(data
, data_size
, fds
, fd_cnt
, data_offset
, fdoffset
);
749 if (r
->_array
[i
] == NULL
)
752 r
->_array_cnt
= tmpcnt
;
754 case LAUNCH_DATA_STRING
:
755 tmpcnt
= big2wire(r
->string_len
);
756 if ((data_size
- *data_offset
) < (tmpcnt
+ 1)) {
760 r
->string
= data
+ *data_offset
;
761 r
->string_len
= tmpcnt
;
762 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
+ 1);
764 case LAUNCH_DATA_OPAQUE
:
765 tmpcnt
= big2wire(r
->opaque_size
);
766 if ((data_size
- *data_offset
) < tmpcnt
) {
770 r
->opaque
= data
+ *data_offset
;
771 r
->opaque_size
= tmpcnt
;
772 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
);
775 if (r
->fd
!= -1 && fd_cnt
> *fdoffset
) {
776 r
->fd
= _fd(fds
[*fdoffset
]);
780 case LAUNCH_DATA_INTEGER
:
781 r
->number
= big2wire(r
->number
);
783 case LAUNCH_DATA_REAL
:
784 r
->float_num
= big2wire(r
->float_num
);
786 case LAUNCH_DATA_BOOL
:
787 r
->boolean
= big2wire(r
->boolean
);
789 case LAUNCH_DATA_ERRNO
:
790 r
->err
= big2wire(r
->err
);
791 case LAUNCH_DATA_MACHPORT
:
799 r
->type
= big2wire(r
->type
);
805 launchd_msg_send(launch_t lh
, launch_data_t d
)
807 struct launch_msg_header lmh
;
808 struct cmsghdr
*cm
= NULL
;
811 size_t sentctrllen
= 0;
814 int fd2use
= launchd_getfd(lh
);
820 memset(&mh
, 0, sizeof(mh
));
822 /* confirm that the next hack works */
823 assert((d
&& lh
->sendlen
== 0) || (!d
&& lh
->sendlen
));
826 size_t fd_slots_used
= 0;
827 size_t good_enough_size
= 10 * 1024 * 1024;
830 /* hack, see the above assert to verify "correctness" */
832 lh
->sendbuf
= malloc(good_enough_size
);
839 lh
->sendfds
= malloc(4 * 1024);
847 lh
->sendlen
= launch_data_pack(d
, lh
->sendbuf
, good_enough_size
, lh
->sendfds
, &fd_slots_used
);
849 if (lh
->sendlen
== 0) {
854 lh
->sendfdcnt
= fd_slots_used
;
856 msglen
= lh
->sendlen
+ sizeof(struct launch_msg_header
); /* type promotion to make the host2wire() macro work right */
857 lmh
.len
= host2wire(msglen
);
858 lmh
.magic
= host2wire(LAUNCH_MSG_HEADER_MAGIC
);
860 iov
[0].iov_base
= &lmh
;
861 iov
[0].iov_len
= sizeof(lmh
);
865 mh
.msg_iov
= iov
+ 1;
869 iov
[1].iov_base
= lh
->sendbuf
;
870 iov
[1].iov_len
= lh
->sendlen
;
873 if (lh
->sendfdcnt
> 0) {
874 sentctrllen
= mh
.msg_controllen
= CMSG_SPACE(lh
->sendfdcnt
* sizeof(int));
875 cm
= alloca(mh
.msg_controllen
);
878 memset(cm
, 0, mh
.msg_controllen
);
880 cm
->cmsg_len
= CMSG_LEN(lh
->sendfdcnt
* sizeof(int));
881 cm
->cmsg_level
= SOL_SOCKET
;
882 cm
->cmsg_type
= SCM_RIGHTS
;
884 memcpy(CMSG_DATA(cm
), lh
->sendfds
, lh
->sendfdcnt
* sizeof(int));
887 if ((r
= sendmsg(fd2use
, &mh
, 0)) == -1) {
892 } else if (sentctrllen
!= mh
.msg_controllen
) {
898 r
-= sizeof(struct launch_msg_header
);
902 if (lh
->sendlen
> 0) {
903 memmove(lh
->sendbuf
, lh
->sendbuf
+ r
, lh
->sendlen
);
906 lh
->sendbuf
= malloc(0);
911 lh
->sendfds
= malloc(0);
913 if (lh
->sendlen
> 0) {
924 pthread_once(&_lc_once
, launch_client_init
);
935 launch_msg_getmsgs(launch_data_t m
, void *context
)
937 launch_data_t async_resp
, *sync_resp
= context
;
939 if ((LAUNCH_DATA_DICTIONARY
== launch_data_get_type(m
)) && (async_resp
= launch_data_dict_lookup(m
, LAUNCHD_ASYNC_MSG_KEY
))) {
940 launch_data_array_set_index(_lc
->async_resp
, launch_data_copy(async_resp
), launch_data_array_get_count(_lc
->async_resp
));
942 *sync_resp
= launch_data_copy(m
);
947 launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
__attribute__((unused
)))
949 kern_return_t result
;
953 strlcpy(srvnm
, key
, sizeof(srvnm
));
955 result
= bootstrap_check_in(bootstrap_port
, srvnm
, &p
);
957 if (result
== BOOTSTRAP_SUCCESS
)
958 launch_data_set_machport(obj
, p
);
962 launch_msg(launch_data_t d
)
964 launch_data_t mps
, r
= launch_msg_internal(d
);
966 if (launch_data_get_type(d
) == LAUNCH_DATA_STRING
) {
967 if (strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) != 0)
971 if (launch_data_get_type(r
) != LAUNCH_DATA_DICTIONARY
)
973 mps
= launch_data_dict_lookup(r
, LAUNCH_JOBKEY_MACHSERVICES
);
976 launch_data_dict_iterate(mps
, launch_mach_checkin_service
, NULL
);
982 extern kern_return_t
vproc_mig_set_security_session(mach_port_t
, uuid_t
, mach_port_t
);
985 uuid_data_is_null(launch_data_t d
)
988 if (launch_data_get_type(d
) == LAUNCH_DATA_OPAQUE
&& launch_data_get_opaque_size(d
) == sizeof(uuid_t
)) {
989 uuid_t existing_uuid
;
990 memcpy(existing_uuid
, launch_data_get_opaque(d
), sizeof(uuid_t
));
992 /* A NULL UUID tells us to keep the session inherited from the parent. */
993 result
= (bool)uuid_is_null(existing_uuid
);
1000 launch_msg_internal(launch_data_t d
)
1002 launch_data_t resp
= NULL
;
1004 if (d
&& (launch_data_get_type(d
) == LAUNCH_DATA_STRING
)
1005 && (strcmp(launch_data_get_string(d
), LAUNCH_KEY_GETJOBS
) == 0)
1006 && vproc_swap_complex(NULL
, VPROC_GSK_ALLJOBS
, NULL
, &resp
) == NULL
) {
1010 pthread_once(&_lc_once
, launch_client_init
);
1017 if ((launch_data_get_type(d
) == LAUNCH_DATA_STRING
&& strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) == 0) || s_am_embedded_god
) {
1018 _lc
->l
->which
= LAUNCHD_USE_CHECKIN_FD
;
1020 _lc
->l
->which
= LAUNCHD_USE_OTHER_FD
;
1023 fd2use
= launchd_getfd(_lc
->l
);
1030 #if !TARGET_OS_EMBEDDED
1032 launch_data_t uuid_d
= NULL
;
1033 size_t jobs_that_need_sessions
= 0;
1034 if (d
&& launch_data_get_type(d
) == LAUNCH_DATA_DICTIONARY
) {
1035 launch_data_t v
= launch_data_dict_lookup(d
, LAUNCH_KEY_SUBMITJOB
);
1037 if (v
&& launch_data_get_type(v
) == LAUNCH_DATA_ARRAY
) {
1038 size_t cnt
= launch_data_array_get_count(v
);
1041 uuid_generate(uuid
);
1042 for (i
= 0; i
< cnt
; i
++) {
1043 launch_data_t ji
= launch_data_array_get_index(v
, i
);
1044 if (launch_data_get_type(ji
) == LAUNCH_DATA_DICTIONARY
) {
1045 launch_data_t existing_v
= launch_data_dict_lookup(ji
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1047 /* I really wish these were reference-counted. Sigh... */
1048 uuid_d
= launch_data_new_opaque(uuid
, sizeof(uuid
));
1049 launch_data_dict_insert(ji
, uuid_d
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1050 jobs_that_need_sessions
++;
1051 } else if (launch_data_get_type(existing_v
) == LAUNCH_DATA_OPAQUE
) {
1052 jobs_that_need_sessions
+= uuid_data_is_null(existing_v
) ? 0 : 1;
1056 } else if (v
&& launch_data_get_type(v
) == LAUNCH_DATA_DICTIONARY
) {
1057 launch_data_t existing_v
= launch_data_dict_lookup(v
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1059 uuid_generate(uuid
);
1060 uuid_d
= launch_data_new_opaque(uuid
, sizeof(uuid
));
1061 launch_data_dict_insert(v
, uuid_d
, LAUNCH_JOBKEY_SECURITYSESSIONUUID
);
1062 jobs_that_need_sessions
++;
1064 jobs_that_need_sessions
+= uuid_data_is_null(existing_v
) ? 0 : 1;
1070 pthread_mutex_lock(&_lc
->mtx
);
1072 if (d
&& launchd_msg_send(_lc
->l
, d
) == -1) {
1074 if (errno
!= EAGAIN
)
1076 } while (launchd_msg_send(_lc
->l
, NULL
) == -1);
1079 while (resp
== NULL
) {
1080 if (d
== NULL
&& launch_data_array_get_count(_lc
->async_resp
) > 0) {
1081 resp
= launch_data_array_pop_first(_lc
->async_resp
);
1084 if (launchd_msg_recv(_lc
->l
, launch_msg_getmsgs
, &resp
) == -1) {
1085 if (errno
!= EAGAIN
) {
1087 } else if (d
== NULL
) {
1094 FD_SET(fd2use
, &rfds
);
1096 select(fd2use
+ 1, &rfds
, NULL
, NULL
, NULL
);
1102 #if !TARGET_OS_EMBEDDED
1103 if (!uuid_is_null(uuid
) && resp
&& jobs_that_need_sessions
> 0) {
1104 mach_port_t session_port
= _audit_session_self();
1105 launch_data_type_t resp_type
= launch_data_get_type(resp
);
1107 bool set_session
= false;
1108 if (resp_type
== LAUNCH_DATA_ERRNO
) {
1109 set_session
= ( launch_data_get_errno(resp
) == ENEEDAUTH
);
1110 } else if (resp_type
== LAUNCH_DATA_ARRAY
) {
1114 kern_return_t kr
= KERN_FAILURE
;
1116 kr
= vproc_mig_set_security_session(bootstrap_port
, uuid
, session_port
);
1119 if (kr
== KERN_SUCCESS
) {
1120 if (resp_type
== LAUNCH_DATA_ERRNO
) {
1121 launch_data_set_errno(resp
, 0);
1124 for (i
= 0; i
< launch_data_array_get_count(resp
); i
++) {
1125 launch_data_t ri
= launch_data_array_get_index(resp
, i
);
1128 if (launch_data_get_type(ri
) == LAUNCH_DATA_ERRNO
&& (recvd_err
= launch_data_get_errno(ri
))) {
1129 launch_data_set_errno(ri
, recvd_err
== ENEEDAUTH
? 0 : recvd_err
);
1135 mach_port_deallocate(mach_task_self(), session_port
);
1139 pthread_mutex_unlock(&_lc
->mtx
);
1145 launchd_msg_recv(launch_t lh
, void (*cb
)(launch_data_t
, void *), void *context
)
1147 struct cmsghdr
*cm
= alloca(4096);
1148 launch_data_t rmsg
= NULL
;
1149 size_t data_offset
, fd_offset
;
1154 int fd2use
= launchd_getfd(lh
);
1160 memset(&mh
, 0, sizeof(mh
));
1164 lh
->recvbuf
= reallocf(lh
->recvbuf
, lh
->recvlen
+ 8*1024);
1166 iov
.iov_base
= lh
->recvbuf
+ lh
->recvlen
;
1167 iov
.iov_len
= 8*1024;
1168 mh
.msg_control
= cm
;
1169 mh
.msg_controllen
= 4096;
1171 if ((r
= recvmsg(fd2use
, &mh
, 0)) == -1)
1177 if (mh
.msg_flags
& MSG_CTRUNC
) {
1178 errno
= ECONNABORTED
;
1182 if (mh
.msg_controllen
> 0) {
1183 lh
->recvfds
= reallocf(lh
->recvfds
, lh
->recvfdcnt
* sizeof(int) + mh
.msg_controllen
- sizeof(struct cmsghdr
));
1184 memcpy(lh
->recvfds
+ lh
->recvfdcnt
, CMSG_DATA(cm
), mh
.msg_controllen
- sizeof(struct cmsghdr
));
1185 lh
->recvfdcnt
+= (mh
.msg_controllen
- sizeof(struct cmsghdr
)) / sizeof(int);
1190 while (lh
->recvlen
> 0) {
1191 struct launch_msg_header
*lmhp
= lh
->recvbuf
;
1193 data_offset
= sizeof(struct launch_msg_header
);
1196 if (lh
->recvlen
< sizeof(struct launch_msg_header
))
1197 goto need_more_data
;
1199 tmplen
= big2wire(lmhp
->len
);
1201 if (big2wire(lmhp
->magic
) != LAUNCH_MSG_HEADER_MAGIC
|| tmplen
<= sizeof(struct launch_msg_header
)) {
1206 if (lh
->recvlen
< tmplen
) {
1207 goto need_more_data
;
1210 if ((rmsg
= launch_data_unpack(lh
->recvbuf
, lh
->recvlen
, lh
->recvfds
, lh
->recvfdcnt
, &data_offset
, &fd_offset
)) == NULL
) {
1215 in_flight_msg_recv_client
= lh
;
1219 /* launchd and only launchd can call launchd_close() as a part of the callback */
1220 if (in_flight_msg_recv_client
== NULL
) {
1225 lh
->recvlen
-= data_offset
;
1226 if (lh
->recvlen
> 0) {
1227 memmove(lh
->recvbuf
, lh
->recvbuf
+ data_offset
, lh
->recvlen
);
1230 lh
->recvbuf
= malloc(0);
1233 lh
->recvfdcnt
-= fd_offset
;
1234 if (lh
->recvfdcnt
> 0) {
1235 memmove(lh
->recvfds
, lh
->recvfds
+ fd_offset
, lh
->recvfdcnt
* sizeof(int));
1238 lh
->recvfds
= malloc(0);
1251 launch_data_copy(launch_data_t o
)
1253 launch_data_t r
= launch_data_alloc(o
->type
);
1257 memcpy(r
, o
, sizeof(struct _launch_data
));
1260 case LAUNCH_DATA_DICTIONARY
:
1261 case LAUNCH_DATA_ARRAY
:
1262 r
->_array
= calloc(1, o
->_array_cnt
* sizeof(launch_data_t
));
1263 for (i
= 0; i
< o
->_array_cnt
; i
++) {
1265 r
->_array
[i
] = launch_data_copy(o
->_array
[i
]);
1268 case LAUNCH_DATA_STRING
:
1269 r
->string
= strdup(o
->string
);
1271 case LAUNCH_DATA_OPAQUE
:
1272 r
->opaque
= malloc(o
->opaque_size
);
1273 memcpy(r
->opaque
, o
->opaque
, o
->opaque_size
);
1286 fcntl(fd
, F_SETFD
, 1);
1291 launch_data_new_errno(int e
)
1293 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_ERRNO
);
1296 launch_data_set_errno(r
, e
);
1302 launch_data_new_fd(int fd
)
1304 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_FD
);
1307 launch_data_set_fd(r
, fd
);
1313 launch_data_new_machport(mach_port_t p
)
1315 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_MACHPORT
);
1318 launch_data_set_machport(r
, p
);
1324 launch_data_new_integer(long long n
)
1326 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_INTEGER
);
1329 launch_data_set_integer(r
, n
);
1335 launch_data_new_bool(bool b
)
1337 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_BOOL
);
1340 launch_data_set_bool(r
, b
);
1346 launch_data_new_real(double d
)
1348 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_REAL
);
1351 launch_data_set_real(r
, d
);
1357 launch_data_new_string(const char *s
)
1359 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_STRING
);
1364 if (!launch_data_set_string(r
, s
)) {
1365 launch_data_free(r
);
1373 launch_data_new_opaque(const void *o
, size_t os
)
1375 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_OPAQUE
);
1380 if (!launch_data_set_opaque(r
, o
, os
)) {
1381 launch_data_free(r
);
1389 load_launchd_jobs_at_loginwindow_prompt(int flags
__attribute__((unused
)), ...)
1391 _vprocmgr_init(VPROCMGR_SESSION_LOGINWINDOW
);
1395 create_and_switch_to_per_session_launchd(const char *login
__attribute__((unused
)), int flags
, ...)
1397 uid_t target_user
= geteuid() ? geteuid() : getuid();
1398 if (_vprocmgr_move_subset_to_user(target_user
, VPROCMGR_SESSION_AQUA
, flags
)) {