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@
22 #include "liblaunch_public.h"
23 #include "liblaunch_private.h"
24 #include "liblaunch_internal.h"
26 #include <mach/mach.h>
27 #include <libkern/OSByteOrder.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/fcntl.h>
43 #include "libbootstrap_public.h"
44 #include "libvproc_public.h"
45 #include "libvproc_private.h"
46 #include "libvproc_internal.h"
48 /* __OSBogusByteSwap__() must not really exist in the symbol namespace
49 * in order for the following to generate an error at build time.
51 extern void __OSBogusByteSwap__(void);
54 ({ typeof (x) _X, _x = (x); \
55 switch (sizeof(_x)) { \
57 _X = OSSwapHostToBigInt64(_x); \
60 _X = OSSwapHostToBigInt32(_x); \
63 _X = OSSwapHostToBigInt16(_x); \
69 __OSBogusByteSwap__(); \
77 ({ typeof (x) _X, _x = (x); \
78 switch (sizeof(_x)) { \
80 _X = OSSwapBigToHostInt64(_x); \
83 _X = OSSwapBigToHostInt32(_x); \
86 _X = OSSwapBigToHostInt16(_x); \
92 __OSBogusByteSwap__(); \
99 struct launch_msg_header
{
104 #define LAUNCH_MSG_HEADER_MAGIC 0xD2FEA02366B39A41ull
106 struct _launch_data
{
111 launch_data_t
*_array
;
119 uint64_t opaque_size
;
126 uint32_t boolean
; /* We'd use 'bool' but this struct needs to be used under Rosetta, and sizeof(bool) is different between PowerPC and Intel */
143 static launch_data_t
launch_data_array_pop_first(launch_data_t where
);
144 static int _fd(int fd
);
145 static void launch_client_init(void);
146 static void launch_msg_getmsgs(launch_data_t m
, void *context
);
147 static launch_data_t
launch_msg_internal(launch_data_t d
);
148 static void launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
);
150 static pthread_once_t _lc_once
= PTHREAD_ONCE_INIT
;
152 static struct _launch_client
{
155 launch_data_t async_resp
;
159 launch_client_init(void)
161 struct sockaddr_un sun
;
162 char *where
= getenv(LAUNCHD_SOCKET_ENV
);
163 char *_launchd_fd
= getenv(LAUNCHD_TRUSTED_FD_ENV
);
167 _lc
= calloc(1, sizeof(struct _launch_client
));
172 pthread_mutex_init(&_lc
->mtx
, NULL
);
175 lfd
= strtol(_launchd_fd
, NULL
, 10);
176 if ((dfd
= dup(lfd
)) >= 0) {
182 unsetenv(LAUNCHD_TRUSTED_FD_ENV
);
185 memset(&sun
, 0, sizeof(sun
));
186 sun
.sun_family
= AF_UNIX
;
188 if (where
&& where
[0] != '\0') {
189 strncpy(sun
.sun_path
, where
, sizeof(sun
.sun_path
));
190 } else if (!getenv("SUDO_COMMAND") && _vprocmgr_getsocket(spath
) == 0) {
193 min_len
= sizeof(sun
.sun_path
) < sizeof(spath
) ? sizeof(sun
.sun_path
) : sizeof(spath
);
195 strncpy(sun
.sun_path
, spath
, min_len
);
197 strncpy(sun
.sun_path
, LAUNCHD_SOCK_PREFIX
"/sock", sizeof(sun
.sun_path
));
200 if ((lfd
= _fd(socket(AF_UNIX
, SOCK_STREAM
, 0))) == -1)
202 if (-1 == connect(lfd
, (struct sockaddr
*)&sun
, sizeof(sun
)))
205 if (!(_lc
->l
= launchd_fdopen(lfd
)))
207 if (!(_lc
->async_resp
= launch_data_alloc(LAUNCH_DATA_ARRAY
)))
213 launchd_close(_lc
->l
, close
);
222 launch_data_alloc(launch_data_type_t t
)
224 launch_data_t d
= calloc(1, sizeof(struct _launch
));
229 case LAUNCH_DATA_DICTIONARY
:
230 case LAUNCH_DATA_ARRAY
:
231 d
->_array
= malloc(0);
242 launch_data_get_type(launch_data_t d
)
248 launch_data_free(launch_data_t d
)
253 case LAUNCH_DATA_DICTIONARY
:
254 case LAUNCH_DATA_ARRAY
:
255 for (i
= 0; i
< d
->_array_cnt
; i
++)
256 launch_data_free(d
->_array
[i
]);
259 case LAUNCH_DATA_STRING
:
263 case LAUNCH_DATA_OPAQUE
:
274 launch_data_dict_get_count(launch_data_t dict
)
276 return dict
->_array_cnt
/ 2;
281 launch_data_dict_insert(launch_data_t dict
, launch_data_t what
, const char *key
)
284 launch_data_t thekey
= launch_data_alloc(LAUNCH_DATA_STRING
);
286 launch_data_set_string(thekey
, key
);
288 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
289 if (!strcasecmp(key
, dict
->_array
[i
]->string
)) {
290 launch_data_array_set_index(dict
, thekey
, i
);
291 launch_data_array_set_index(dict
, what
, i
+ 1);
295 launch_data_array_set_index(dict
, thekey
, i
);
296 launch_data_array_set_index(dict
, what
, i
+ 1);
301 launch_data_dict_lookup(launch_data_t dict
, const char *key
)
305 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
)
308 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
309 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
310 return dict
->_array
[i
+ 1];
317 launch_data_dict_remove(launch_data_t dict
, const char *key
)
321 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2) {
322 if (!strcasecmp(key
, dict
->_array
[i
]->string
))
325 if (i
== dict
->_array_cnt
)
327 launch_data_free(dict
->_array
[i
]);
328 launch_data_free(dict
->_array
[i
+ 1]);
329 memmove(dict
->_array
+ i
, dict
->_array
+ i
+ 2, (dict
->_array_cnt
- (i
+ 2)) * sizeof(launch_data_t
));
330 dict
->_array_cnt
-= 2;
335 launch_data_dict_iterate(launch_data_t dict
, void (*cb
)(launch_data_t
, const char *, void *), void *context
)
339 if (LAUNCH_DATA_DICTIONARY
!= dict
->type
)
342 for (i
= 0; i
< dict
->_array_cnt
; i
+= 2)
343 cb(dict
->_array
[i
+ 1], dict
->_array
[i
]->string
, context
);
347 launch_data_array_set_index(launch_data_t where
, launch_data_t what
, size_t ind
)
349 if ((ind
+ 1) >= where
->_array_cnt
) {
350 where
->_array
= reallocf(where
->_array
, (ind
+ 1) * sizeof(launch_data_t
));
351 memset(where
->_array
+ where
->_array_cnt
, 0, (ind
+ 1 - where
->_array_cnt
) * sizeof(launch_data_t
));
352 where
->_array_cnt
= ind
+ 1;
355 if (where
->_array
[ind
])
356 launch_data_free(where
->_array
[ind
]);
357 where
->_array
[ind
] = what
;
362 launch_data_array_get_index(launch_data_t where
, size_t ind
)
364 if (LAUNCH_DATA_ARRAY
!= where
->type
)
366 if (ind
< where
->_array_cnt
)
367 return where
->_array
[ind
];
372 launch_data_array_pop_first(launch_data_t where
)
374 launch_data_t r
= NULL
;
376 if (where
->_array_cnt
> 0) {
377 r
= where
->_array
[0];
378 memmove(where
->_array
, where
->_array
+ 1, (where
->_array_cnt
- 1) * sizeof(launch_data_t
));
385 launch_data_array_get_count(launch_data_t where
)
387 if (LAUNCH_DATA_ARRAY
!= where
->type
)
389 return where
->_array_cnt
;
393 launch_data_set_errno(launch_data_t d
, int e
)
400 launch_data_set_fd(launch_data_t d
, int fd
)
407 launch_data_set_machport(launch_data_t d
, mach_port_t p
)
414 launch_data_set_integer(launch_data_t d
, long long n
)
421 launch_data_set_bool(launch_data_t d
, bool b
)
428 launch_data_set_real(launch_data_t d
, double n
)
435 launch_data_set_string(launch_data_t d
, const char *s
)
439 d
->string
= strdup(s
);
441 d
->string_len
= strlen(d
->string
);
448 launch_data_set_opaque(launch_data_t d
, const void *o
, size_t os
)
453 d
->opaque
= malloc(os
);
455 memcpy(d
->opaque
, o
, os
);
462 launch_data_get_errno(launch_data_t d
)
468 launch_data_get_fd(launch_data_t d
)
474 launch_data_get_machport(launch_data_t d
)
480 launch_data_get_integer(launch_data_t d
)
486 launch_data_get_bool(launch_data_t d
)
492 launch_data_get_real(launch_data_t d
)
498 launch_data_get_string(launch_data_t d
)
500 if (LAUNCH_DATA_STRING
!= d
->type
)
506 launch_data_get_opaque(launch_data_t d
)
508 if (LAUNCH_DATA_OPAQUE
!= d
->type
)
514 launch_data_get_opaque_size(launch_data_t d
)
516 return d
->opaque_size
;
520 launchd_getfd(launch_t l
)
526 launchd_fdopen(int fd
)
530 c
= calloc(1, sizeof(struct _launch
));
536 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
538 if ((c
->sendbuf
= malloc(0)) == NULL
)
540 if ((c
->sendfds
= malloc(0)) == NULL
)
542 if ((c
->recvbuf
= malloc(0)) == NULL
)
544 if ((c
->recvfds
= malloc(0)) == NULL
)
563 launchd_close(launch_t lh
, typeof(close
) closefunc
)
577 #define ROUND_TO_64BIT_WORD_SIZE(x) ((x + 7) & ~7)
580 launch_data_pack(launch_data_t d
, void *where
, size_t len
, int *fd_where
, size_t *fd_cnt
)
582 launch_data_t o_in_w
= where
;
583 size_t i
, rsz
, total_data_len
= sizeof(struct _launch_data
);
585 if (total_data_len
> len
) {
589 where
+= total_data_len
;
591 o_in_w
->type
= host2big(d
->type
);
594 case LAUNCH_DATA_INTEGER
:
595 o_in_w
->number
= host2big(d
->number
);
597 case LAUNCH_DATA_REAL
:
598 o_in_w
->float_num
= host2big(d
->float_num
);
600 case LAUNCH_DATA_BOOL
:
601 o_in_w
->boolean
= host2big(d
->boolean
);
603 case LAUNCH_DATA_ERRNO
:
604 o_in_w
->err
= host2big(d
->err
);
607 o_in_w
->fd
= host2big(d
->fd
);
608 if (fd_where
&& d
->fd
!= -1) {
609 fd_where
[*fd_cnt
] = d
->fd
;
613 case LAUNCH_DATA_STRING
:
614 o_in_w
->string_len
= host2big(d
->string_len
);
615 total_data_len
+= ROUND_TO_64BIT_WORD_SIZE(strlen(d
->string
) + 1);
616 if (total_data_len
> len
) {
619 memcpy(where
, d
->string
, strlen(d
->string
) + 1);
621 case LAUNCH_DATA_OPAQUE
:
622 o_in_w
->opaque_size
= host2big(d
->opaque_size
);
623 total_data_len
+= ROUND_TO_64BIT_WORD_SIZE(d
->opaque_size
);
624 if (total_data_len
> len
) {
627 memcpy(where
, d
->opaque
, d
->opaque_size
);
629 case LAUNCH_DATA_DICTIONARY
:
630 case LAUNCH_DATA_ARRAY
:
631 o_in_w
->_array_cnt
= host2big(d
->_array_cnt
);
632 total_data_len
+= d
->_array_cnt
* sizeof(uint64_t);
633 if (total_data_len
> len
) {
637 where
+= d
->_array_cnt
* sizeof(uint64_t);
639 for (i
= 0; i
< d
->_array_cnt
; i
++) {
640 rsz
= launch_data_pack(d
->_array
[i
], where
, len
- total_data_len
, fd_where
, fd_cnt
);
645 total_data_len
+= rsz
;
652 return total_data_len
;
656 launch_data_unpack(void *data
, size_t data_size
, int *fds
, size_t fd_cnt
, size_t *data_offset
, size_t *fdoffset
)
658 launch_data_t r
= data
+ *data_offset
;
661 if ((data_size
- *data_offset
) < sizeof(struct _launch_data
))
663 *data_offset
+= sizeof(struct _launch_data
);
665 switch (big2host(r
->type
)) {
666 case LAUNCH_DATA_DICTIONARY
:
667 case LAUNCH_DATA_ARRAY
:
668 tmpcnt
= big2host(r
->_array_cnt
);
669 if ((data_size
- *data_offset
) < (tmpcnt
* sizeof(uint64_t))) {
673 r
->_array
= data
+ *data_offset
;
674 *data_offset
+= tmpcnt
* sizeof(uint64_t);
675 for (i
= 0; i
< tmpcnt
; i
++) {
676 r
->_array
[i
] = launch_data_unpack(data
, data_size
, fds
, fd_cnt
, data_offset
, fdoffset
);
677 if (r
->_array
[i
] == NULL
)
680 r
->_array_cnt
= tmpcnt
;
682 case LAUNCH_DATA_STRING
:
683 tmpcnt
= big2host(r
->string_len
);
684 if ((data_size
- *data_offset
) < (tmpcnt
+ 1)) {
688 r
->string
= data
+ *data_offset
;
689 r
->string_len
= tmpcnt
;
690 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
+ 1);
692 case LAUNCH_DATA_OPAQUE
:
693 tmpcnt
= big2host(r
->opaque_size
);
694 if ((data_size
- *data_offset
) < tmpcnt
) {
698 r
->opaque
= data
+ *data_offset
;
699 r
->opaque_size
= tmpcnt
;
700 *data_offset
+= ROUND_TO_64BIT_WORD_SIZE(tmpcnt
);
703 if (r
->fd
!= -1 && fd_cnt
> *fdoffset
) {
704 r
->fd
= _fd(fds
[*fdoffset
]);
708 case LAUNCH_DATA_INTEGER
:
709 r
->number
= big2host(r
->number
);
711 case LAUNCH_DATA_REAL
:
712 r
->float_num
= big2host(r
->float_num
);
714 case LAUNCH_DATA_BOOL
:
715 r
->boolean
= big2host(r
->boolean
);
717 case LAUNCH_DATA_ERRNO
:
718 r
->err
= big2host(r
->err
);
719 case LAUNCH_DATA_MACHPORT
:
727 r
->type
= big2host(r
->type
);
732 int launchd_msg_send(launch_t lh
, launch_data_t d
)
734 struct launch_msg_header lmh
;
735 struct cmsghdr
*cm
= NULL
;
738 size_t sentctrllen
= 0;
741 memset(&mh
, 0, sizeof(mh
));
743 /* confirm that the next hack works */
744 assert((d
&& lh
->sendlen
== 0) || (!d
&& lh
->sendlen
));
747 size_t fd_slots_used
= 0;
748 size_t good_enough_size
= 10 * 1024 * 1024;
751 /* hack, see the above assert to verify "correctness" */
753 lh
->sendbuf
= malloc(good_enough_size
);
755 lh
->sendfds
= malloc(4 * 1024);
757 lh
->sendlen
= launch_data_pack(d
, lh
->sendbuf
, good_enough_size
, lh
->sendfds
, &fd_slots_used
);
759 if (lh
->sendlen
== 0) {
764 lh
->sendfdcnt
= fd_slots_used
;
766 msglen
= lh
->sendlen
+ sizeof(struct launch_msg_header
); /* type promotion to make the host2big() macro work right */
767 lmh
.len
= host2big(msglen
);
768 lmh
.magic
= host2big(LAUNCH_MSG_HEADER_MAGIC
);
770 iov
[0].iov_base
= &lmh
;
771 iov
[0].iov_len
= sizeof(lmh
);
775 mh
.msg_iov
= iov
+ 1;
779 iov
[1].iov_base
= lh
->sendbuf
;
780 iov
[1].iov_len
= lh
->sendlen
;
783 if (lh
->sendfdcnt
> 0) {
784 sentctrllen
= mh
.msg_controllen
= CMSG_SPACE(lh
->sendfdcnt
* sizeof(int));
785 cm
= alloca(mh
.msg_controllen
);
788 memset(cm
, 0, mh
.msg_controllen
);
790 cm
->cmsg_len
= CMSG_LEN(lh
->sendfdcnt
* sizeof(int));
791 cm
->cmsg_level
= SOL_SOCKET
;
792 cm
->cmsg_type
= SCM_RIGHTS
;
794 memcpy(CMSG_DATA(cm
), lh
->sendfds
, lh
->sendfdcnt
* sizeof(int));
797 if ((r
= sendmsg(lh
->fd
, &mh
, 0)) == -1) {
802 } else if (sentctrllen
!= mh
.msg_controllen
) {
808 r
-= sizeof(struct launch_msg_header
);
812 if (lh
->sendlen
> 0) {
813 memmove(lh
->sendbuf
, lh
->sendbuf
+ r
, lh
->sendlen
);
816 lh
->sendbuf
= malloc(0);
821 lh
->sendfds
= malloc(0);
823 if (lh
->sendlen
> 0) {
835 pthread_once(&_lc_once
, launch_client_init
);
846 launch_msg_getmsgs(launch_data_t m
, void *context
)
848 launch_data_t async_resp
, *sync_resp
= context
;
850 if ((LAUNCH_DATA_DICTIONARY
== launch_data_get_type(m
)) && (async_resp
= launch_data_dict_lookup(m
, LAUNCHD_ASYNC_MSG_KEY
))) {
851 launch_data_array_set_index(_lc
->async_resp
, launch_data_copy(async_resp
), launch_data_array_get_count(_lc
->async_resp
));
853 *sync_resp
= launch_data_copy(m
);
858 launch_mach_checkin_service(launch_data_t obj
, const char *key
, void *context
__attribute__((unused
)))
860 kern_return_t result
;
864 strlcpy(srvnm
, key
, sizeof(srvnm
));
866 result
= bootstrap_check_in(bootstrap_port
, srvnm
, &p
);
868 if (result
== BOOTSTRAP_SUCCESS
)
869 launch_data_set_machport(obj
, p
);
873 launch_msg(launch_data_t d
)
875 launch_data_t mps
, r
= launch_msg_internal(d
);
877 if (launch_data_get_type(d
) == LAUNCH_DATA_STRING
) {
878 if (strcmp(launch_data_get_string(d
), LAUNCH_KEY_CHECKIN
) != 0)
882 if (launch_data_get_type(r
) != LAUNCH_DATA_DICTIONARY
)
884 mps
= launch_data_dict_lookup(r
, LAUNCH_JOBKEY_MACHSERVICES
);
887 launch_data_dict_iterate(mps
, launch_mach_checkin_service
, NULL
);
894 launch_msg_internal(launch_data_t d
)
896 launch_data_t resp
= NULL
;
898 if (d
&& (launch_data_get_type(d
) == LAUNCH_DATA_STRING
)
899 && (strcmp(launch_data_get_string(d
), LAUNCH_KEY_GETJOBS
) == 0)
900 && vproc_swap_complex(NULL
, VPROC_GSK_ALLJOBS
, NULL
, &resp
) == NULL
) {
904 pthread_once(&_lc_once
, launch_client_init
);
911 pthread_mutex_lock(&_lc
->mtx
);
913 if (d
&& launchd_msg_send(_lc
->l
, d
) == -1) {
917 } while (launchd_msg_send(_lc
->l
, NULL
) == -1);
920 while (resp
== NULL
) {
921 if (d
== NULL
&& launch_data_array_get_count(_lc
->async_resp
) > 0) {
922 resp
= launch_data_array_pop_first(_lc
->async_resp
);
925 if (launchd_msg_recv(_lc
->l
, launch_msg_getmsgs
, &resp
) == -1) {
926 if (errno
!= EAGAIN
) {
928 } else if (d
== NULL
) {
935 FD_SET(_lc
->l
->fd
, &rfds
);
937 select(_lc
->l
->fd
+ 1, &rfds
, NULL
, NULL
, NULL
);
943 pthread_mutex_unlock(&_lc
->mtx
);
948 int launchd_msg_recv(launch_t lh
, void (*cb
)(launch_data_t
, void *), void *context
)
950 struct cmsghdr
*cm
= alloca(4096);
951 launch_data_t rmsg
= NULL
;
952 size_t data_offset
, fd_offset
;
957 memset(&mh
, 0, sizeof(mh
));
961 lh
->recvbuf
= reallocf(lh
->recvbuf
, lh
->recvlen
+ 8*1024);
963 iov
.iov_base
= lh
->recvbuf
+ lh
->recvlen
;
964 iov
.iov_len
= 8*1024;
966 mh
.msg_controllen
= 4096;
968 if ((r
= recvmsg(lh
->fd
, &mh
, 0)) == -1)
974 if (mh
.msg_flags
& MSG_CTRUNC
) {
975 errno
= ECONNABORTED
;
979 if (mh
.msg_controllen
> 0) {
980 lh
->recvfds
= reallocf(lh
->recvfds
, lh
->recvfdcnt
* sizeof(int) + mh
.msg_controllen
- sizeof(struct cmsghdr
));
981 memcpy(lh
->recvfds
+ lh
->recvfdcnt
, CMSG_DATA(cm
), mh
.msg_controllen
- sizeof(struct cmsghdr
));
982 lh
->recvfdcnt
+= (mh
.msg_controllen
- sizeof(struct cmsghdr
)) / sizeof(int);
987 while (lh
->recvlen
> 0) {
988 struct launch_msg_header
*lmhp
= lh
->recvbuf
;
990 data_offset
= sizeof(struct launch_msg_header
);
993 if (lh
->recvlen
< sizeof(struct launch_msg_header
))
996 tmplen
= big2host(lmhp
->len
);
998 if (big2host(lmhp
->magic
) != LAUNCH_MSG_HEADER_MAGIC
|| tmplen
<= sizeof(struct launch_msg_header
)) {
1003 if (lh
->recvlen
< tmplen
) {
1004 goto need_more_data
;
1007 if ((rmsg
= launch_data_unpack(lh
->recvbuf
, lh
->recvlen
, lh
->recvfds
, lh
->recvfdcnt
, &data_offset
, &fd_offset
)) == NULL
) {
1014 lh
->recvlen
-= data_offset
;
1015 if (lh
->recvlen
> 0) {
1016 memmove(lh
->recvbuf
, lh
->recvbuf
+ data_offset
, lh
->recvlen
);
1019 lh
->recvbuf
= malloc(0);
1022 lh
->recvfdcnt
-= fd_offset
;
1023 if (lh
->recvfdcnt
> 0) {
1024 memmove(lh
->recvfds
, lh
->recvfds
+ fd_offset
, lh
->recvfdcnt
* sizeof(int));
1027 lh
->recvfds
= malloc(0);
1039 launch_data_t
launch_data_copy(launch_data_t o
)
1041 launch_data_t r
= launch_data_alloc(o
->type
);
1045 memcpy(r
, o
, sizeof(struct _launch_data
));
1048 case LAUNCH_DATA_DICTIONARY
:
1049 case LAUNCH_DATA_ARRAY
:
1050 r
->_array
= calloc(1, o
->_array_cnt
* sizeof(launch_data_t
));
1051 for (i
= 0; i
< o
->_array_cnt
; i
++) {
1053 r
->_array
[i
] = launch_data_copy(o
->_array
[i
]);
1056 case LAUNCH_DATA_STRING
:
1057 r
->string
= strdup(o
->string
);
1059 case LAUNCH_DATA_OPAQUE
:
1060 r
->opaque
= malloc(o
->opaque_size
);
1061 memcpy(r
->opaque
, o
->opaque
, o
->opaque_size
);
1071 launchd_batch_enable(bool b
)
1075 vproc_swap_integer(NULL
, VPROC_GSK_GLOBAL_ON_DEMAND
, &val
, NULL
);
1079 launchd_batch_query(void)
1083 if (vproc_swap_integer(NULL
, VPROC_GSK_GLOBAL_ON_DEMAND
, NULL
, &val
) == NULL
) {
1090 static int _fd(int fd
)
1093 fcntl(fd
, F_SETFD
, 1);
1097 launch_data_t
launch_data_new_errno(int e
)
1099 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_ERRNO
);
1102 launch_data_set_errno(r
, e
);
1107 launch_data_t
launch_data_new_fd(int fd
)
1109 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_FD
);
1112 launch_data_set_fd(r
, fd
);
1117 launch_data_t
launch_data_new_machport(mach_port_t p
)
1119 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_MACHPORT
);
1122 launch_data_set_machport(r
, p
);
1127 launch_data_t
launch_data_new_integer(long long n
)
1129 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_INTEGER
);
1132 launch_data_set_integer(r
, n
);
1137 launch_data_t
launch_data_new_bool(bool b
)
1139 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_BOOL
);
1142 launch_data_set_bool(r
, b
);
1147 launch_data_t
launch_data_new_real(double d
)
1149 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_REAL
);
1152 launch_data_set_real(r
, d
);
1157 launch_data_t
launch_data_new_string(const char *s
)
1159 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_STRING
);
1164 if (!launch_data_set_string(r
, s
)) {
1165 launch_data_free(r
);
1172 launch_data_t
launch_data_new_opaque(const void *o
, size_t os
)
1174 launch_data_t r
= launch_data_alloc(LAUNCH_DATA_OPAQUE
);
1179 if (!launch_data_set_opaque(r
, o
, os
)) {
1180 launch_data_free(r
);
1188 load_launchd_jobs_at_loginwindow_prompt(int flags
__attribute__((unused
)), ...)
1190 _vprocmgr_init("LoginWindow");
1194 create_and_switch_to_per_session_launchd(const char *login
__attribute__((unused
)), int flags
__attribute__((unused
)), ...)
1196 mach_port_t bezel_ui_server
;
1198 uid_t target_user
= geteuid() ? geteuid() : getuid();
1200 if (_vprocmgr_move_subset_to_user(target_user
, "Aqua")) {
1204 #define BEZEL_UI_PATH "/System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/BezelUI/BezelUIServer"
1205 #define BEZEL_UI_PLIST "/System/Library/LaunchAgents/com.apple.BezelUIServer.plist"
1206 #define BEZEL_UI_SERVICE "BezelUI"
1208 if (!(stat(BEZEL_UI_PLIST
, &sb
) == 0 && S_ISREG(sb
.st_mode
))) {
1209 if (bootstrap_create_server(bootstrap_port
, BEZEL_UI_PATH
, target_user
, true, &bezel_ui_server
) == BOOTSTRAP_SUCCESS
) {
1212 if (bootstrap_create_service(bezel_ui_server
, BEZEL_UI_SERVICE
, &srv
) == BOOTSTRAP_SUCCESS
) {
1213 mach_port_deallocate(mach_task_self(), srv
);
1216 mach_port_deallocate(mach_task_self(), bezel_ui_server
);