1 /*
2 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #ifndef __LAUNCH_INTERNAL_H__
22 #define __LAUNCH_INTERNAL_H__
23
24 #pragma clang diagnostic ignored "-Wcast-qual"
25
26 #include "vproc_priv.h"
27
28 #include <paths.h>
29 #include <dispatch/dispatch.h>
30 #include <pthread.h>
31
32 #if __has_include(<os/alloc_once_private.h>)
33 #include <os/alloc_once_private.h>
34 #if defined(OS_ALLOC_ONCE_KEY_LIBLAUNCH)
35 #define _LIBLAUNCH_HAS_ALLOC_ONCE 1
36 #endif
37 #endif
38
39 typedef struct _launch *launch_t;
40
41 struct launch_globals_s {
42 // liblaunch.c
43 pthread_once_t lc_once;
44 pthread_mutex_t lc_mtx;
45 launch_t l;
46 launch_data_t async_resp;
47
48 launch_t in_flight_msg_recv_client;
49
50 int64_t s_am_embedded_god;
51
52 // libvproc.c
53 dispatch_queue_t _vproc_gone2zero_queue;
54 _vproc_transaction_callout _vproc_gone2zero_callout;
55 void *_vproc_gone2zero_ctx;
56
57 dispatch_once_t _vproc_transaction_once;
58 uint64_t _vproc_transaction_enabled;
59 dispatch_queue_t _vproc_transaction_queue;
60 int64_t _vproc_transaction_cnt;
61 };
62 typedef struct launch_globals_s *launch_globals_t;
63
64 void _launch_init_globals(launch_globals_t globals);
65
66 #if !_LIBLAUNCH_HAS_ALLOC_ONCE
67 launch_globals_t _launch_globals_impl(void);
68 #endif
69
70 __attribute__((__pure__))
71 static inline launch_globals_t
_launch_globals(void)72 _launch_globals(void) {
73 #if _LIBLAUNCH_HAS_ALLOC_ONCE
74 return (launch_globals_t)os_alloc_once(OS_ALLOC_ONCE_KEY_LIBLAUNCH,
75 sizeof(struct launch_globals_s),
76 (void*)&_launch_init_globals);
77 #else
78 return _launch_globals_impl();
79 #endif
80 }
81
82 #pragma GCC visibility push(default)
83
84 #define LAUNCHD_DB_PREFIX "/var/db/launchd"
85 #define LAUNCHD_LOG_PREFIX "/var/log"
86
87 #define LAUNCHD_JOB_DEFAULTS "Defaults"
88 #define LAUNCHD_JOB_DEFAULTS_CACHED "CachedDefaults"
89
90 launch_t launchd_fdopen(int, int);
91 int launchd_getfd(launch_t);
92 void launchd_close(launch_t, __typeof__(close) closefunc);
93
94 launch_data_t launch_data_new_errno(int);
95 bool launch_data_set_errno(launch_data_t, int);
96
97 int launchd_msg_send(launch_t, launch_data_t);
98 int launchd_msg_recv(launch_t, void (*)(launch_data_t, void *), void *);
99
100 size_t launch_data_pack(launch_data_t d, uint8_t *where, size_t len, int *fd_where, size_t *fdslotsleft);
101 launch_data_t launch_data_unpack(uint8_t *data, size_t data_size, int *fds, size_t fd_cnt, size_t *data_offset, size_t *fdoffset);
102
103 #pragma GCC visibility pop
104
105 #endif /* __LAUNCH_INTERNAL_H__*/
106