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#include "vproc_priv.h"
25
26#include <paths.h>
27#include <dispatch/dispatch.h>
28#include <pthread.h>
29
30#if __has_include(<os/alloc_once_private.h>)
31#include <os/alloc_once_private.h>
32#if defined(OS_ALLOC_ONCE_KEY_LIBLAUNCH)
33#define _LIBLAUNCH_HAS_ALLOC_ONCE 1
34#endif
35#endif
36
37typedef struct _launch *launch_t;
38
39struct launch_globals_s {
40	// liblaunch.c
41	pthread_once_t lc_once;
42	pthread_mutex_t lc_mtx;
43	launch_t l;
44	launch_data_t async_resp;
45
46	launch_t in_flight_msg_recv_client;
47
48	int64_t s_am_embedded_god;
49
50	// libvproc.c
51	dispatch_queue_t _vproc_gone2zero_queue;
52	_vproc_transaction_callout _vproc_gone2zero_callout;
53	void *_vproc_gone2zero_ctx;
54
55	dispatch_once_t _vproc_transaction_once;
56	uint64_t _vproc_transaction_enabled;
57	dispatch_queue_t _vproc_transaction_queue;
58	int64_t _vproc_transaction_cnt;
59};
60typedef struct launch_globals_s *launch_globals_t;
61
62void _launch_init_globals(launch_globals_t globals);
63
64#if !_LIBLAUNCH_HAS_ALLOC_ONCE
65launch_globals_t _launch_globals_impl(void);
66#endif
67
68__attribute__((__pure__))
69static inline launch_globals_t
70_launch_globals(void) {
71#if _LIBLAUNCH_HAS_ALLOC_ONCE
72	return (launch_globals_t)os_alloc_once(OS_ALLOC_ONCE_KEY_LIBLAUNCH,
73					       sizeof(struct launch_globals_s),
74					       (void*)&_launch_init_globals);
75#else
76	return _launch_globals_impl();
77#endif
78}
79
80#pragma GCC visibility push(default)
81
82#define LAUNCHD_DB_PREFIX "/private/var/db/launchd.db"
83#define LAUNCHD_LOG_PREFIX "/private/var/log"
84
85#define LAUNCHD_JOB_DEFAULTS "Defaults"
86#define LAUNCHD_JOB_DEFAULTS_CACHED "CachedDefaults"
87
88launch_t launchd_fdopen(int, int);
89int launchd_getfd(launch_t);
90void launchd_close(launch_t, __typeof__(close) closefunc);
91
92launch_data_t launch_data_new_errno(int);
93bool launch_data_set_errno(launch_data_t, int);
94
95int launchd_msg_send(launch_t, launch_data_t);
96int launchd_msg_recv(launch_t, void (*)(launch_data_t, void *), void *);
97
98size_t launch_data_pack(launch_data_t d, void *where, size_t len, int *fd_where, size_t *fdslotsleft);
99launch_data_t launch_data_unpack(void *data, size_t data_size, int *fds, size_t fd_cnt, size_t *data_offset, size_t *fdoffset);
100
101#pragma GCC visibility pop
102
103#endif /*  __LAUNCH_INTERNAL_H__*/
104