1/*
2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
30/*
31 * [SPN] Support for _POSIX_SPAWN
32 *
33 * This file contains internal data structures which are externally represented
34 * as opaque void pointers to prevent introspection.  This permits us to
35 * change the underlying implementation of the code to maintain it or to
36 * support new features, as needed, without the consumer needing to recompile
37 * their code because of structure size changes or data reorganization.
38 */
39
40#ifndef	_SYS_SPAWN_INTERNAL_H_
41#define	_SYS_SPAWN_INTERNAL_H_
42
43#include <sys/_types.h>		/* __offsetof(), __darwin_size_t */
44#include <sys/syslimits.h>	/* PATH_MAX */
45#include <sys/spawn.h>
46#include <mach/machine.h>
47#include <mach/port.h>
48#include <mach/exception_types.h>
49
50/*
51 * Allowable posix_spawn() port action types
52 */
53typedef enum {
54	PSPA_SPECIAL = 0,
55	PSPA_EXCEPTION = 1,
56	PSPA_AU_SESSION = 2,
57	PSPA_IMP_WATCHPORTS = 3,
58} pspa_t;
59
60/*
61 * Internal representation of one port to be set on posix_spawn().
62 * Currently this is limited to setting special and exception ports,
63 * but could be extended to other inheritable port types.
64 */
65typedef struct _ps_port_action {
66	pspa_t			port_type;
67	exception_mask_t	mask;
68	mach_port_name_t	new_port;
69	exception_behavior_t	behavior;
70	thread_state_flavor_t	flavor;
71	int			which;
72} _ps_port_action_t;
73
74/*
75 * A collection of port actions to take on the newly spawned process.
76 */
77typedef struct _posix_spawn_port_actions {
78	int			pspa_alloc;
79	int			pspa_count;
80	_ps_port_action_t 	pspa_actions[];
81} *_posix_spawn_port_actions_t;
82
83/*
84 * Returns size in bytes of a _posix_spawn_port_actions holding x elements.
85 */
86#define	PS_PORT_ACTIONS_SIZE(x)	\
87	__offsetof(struct _posix_spawn_port_actions, pspa_actions[(x)])
88
89#define NBINPREFS	4
90
91/*
92 * Mapping of opaque data pointer to a MAC policy (specified by name).
93 */
94typedef struct _ps_mac_policy_extension {
95	char			policyname[128];
96	union {
97		uint64_t	data;
98		void 		*datap;		/* pointer in kernel memory */
99	};
100	uint64_t		datalen;
101} _ps_mac_policy_extension_t;
102
103/*
104 * A collection of extra data passed to MAC policies for the newly spawned process.
105 */
106typedef struct _posix_spawn_mac_policy_extensions {
107	int			psmx_alloc;
108	int			psmx_count;
109	_ps_mac_policy_extension_t psmx_extensions[];
110} *_posix_spawn_mac_policy_extensions_t;
111
112/*
113 * Returns size in bytes of a _posix_spawn_mac_policy_extensions holding x elements.
114 */
115#define PS_MAC_EXTENSIONS_SIZE(x)     \
116        __offsetof(struct _posix_spawn_mac_policy_extensions, psmx_extensions[(x)])
117
118#define PS_MAC_EXTENSIONS_INIT_COUNT	2
119
120
121/*
122 * A posix_spawnattr structure contains all of the attribute elements that
123 * can be set, as well as any metadata whose validity is signalled by the
124 * presence of a bit in the flags field.  All fields are initialized to the
125 * appropriate default values by posix_spawnattr_init().
126 */
127typedef struct _posix_spawnattr {
128	short		psa_flags;		/* spawn attribute flags */
129	short 		flags_padding; 	/* get the flags to be int aligned */
130	sigset_t	psa_sigdefault;		/* signal set to default */
131	sigset_t	psa_sigmask;		/* signal set to mask */
132	pid_t		psa_pgroup;		/* pgroup to spawn into */
133	cpu_type_t	psa_binprefs[NBINPREFS];   /* cpu affinity prefs*/
134	int		psa_pcontrol;		/* process control bits on resource starvation */
135	int		psa_apptype;		/* app type and process spec behav */
136	uint64_t 	psa_cpumonitor_percent; /* CPU usage monitor percentage */
137	uint64_t 	psa_cpumonitor_interval; /* CPU usage monitor interval, in seconds */
138	/*
139	 * TODO: cleanup - see <rdar://problem/12858307>. psa_ports is a pointer,
140	 * meaning that the fields following differ in alignment between 32 and
141	 * 64-bit architectures. All pointers (existing and new) should therefore
142	 * be placed at the end; changing this now, however, would currently break
143	 * some legacy dependencies. The radar will be used to track resolution when
144	 * appropriate.
145	 */
146
147	short       psa_jetsam_flags; /* jetsam flags */
148	short		short_padding;  /* Padding for alignment issues */
149	int         psa_priority;   /* jetsam relative importance */
150	int         psa_high_water_mark; /* jetsam resident page count limit */
151	int 		int_padding;	/* Padding for alignment issues */
152	/* MAC policy-specific extensions. */
153	 _posix_spawn_port_actions_t	psa_ports; /* special/exception ports */
154	_posix_spawn_mac_policy_extensions_t psa_mac_extensions;
155} *_posix_spawnattr_t;
156
157/*
158 * Jetsam flags
159 */
160#define	POSIX_SPAWN_JETSAM_SET                      0x8000
161
162#define	POSIX_SPAWN_JETSAM_USE_EFFECTIVE_PRIORITY   0x1
163#define	POSIX_SPAWN_JETSAM_HIWATER_BACKGROUND       0x2
164
165/*
166 * Deprecated posix_spawn psa_flags values
167 *
168 * POSIX_SPAWN_OSX_TALAPP_START         0x0400
169 * POSIX_SPAWN_IOS_RESV1_APP_START      0x0400
170 * POSIX_SPAWN_IOS_APPLE_DAEMON_START   0x0800
171 * POSIX_SPAWN_IOS_APP_START            0x1000
172 * POSIX_SPAWN_OSX_WIDGET_START         0x0800
173 * POSIX_SPAWN_OSX_DBCLIENT_START       0x0800
174 * POSIX_SPAWN_OSX_RESVAPP_START        0x1000
175 */
176
177/*
178 * Deprecated posix_spawn psa_apptype values
179 *
180 * POSIX_SPAWN_PROCESS_TYPE_APPLEDAEMON             0x00000001
181 * POSIX_SPAWN_PROCESS_TYPE_UIAPP                   0x00000002
182 * POSIX_SPAWN_PROCESS_TYPE_ADAPTIVE                0x00000004
183 * POSIX_SPAWN_PROCESS_TYPE_TAL                     0x00000001
184 * POSIX_SPAWN_PROCESS_TYPE_WIDGET                  0x00000002
185 * POSIX_SPAWN_PROCESS_TYPE_DELAYIDLESLEEP          0x10000000
186 *
187 * POSIX_SPAWN_PROCESS_FLAG_IMPORTANCE_DONOR        0x00000010
188 * POSIX_SPAWN_PROCESS_FLAG_ADAPTIVE                0x00000020
189 * POSIX_SPAWN_PROCESS_FLAG_START_BACKGROUND        0x00000040
190 * POSIX_SPAWN_PROCESS_FLAG_START_LIGHT_THROTTLE    0x00000080
191 */
192
193/*
194 * posix_spawn psa_apptype process type settings.
195 * when POSIX_SPAWN_PROC_TYPE is set, old psa_apptype bits are ignored
196 */
197
198#define POSIX_SPAWN_PROCESS_TYPE_NORMAL             0x00000000
199#define POSIX_SPAWN_PROCESS_TYPE_DEFAULT            POSIX_SPAWN_PROCESS_TYPE_NORMAL
200
201#define POSIX_SPAWN_PROC_TYPE_MASK                  0x00000F00
202
203#define POSIX_SPAWN_PROC_TYPE_APP_DEFAULT           0x00000100
204#define POSIX_SPAWN_PROC_TYPE_APP_TAL               0x00000200
205
206#define POSIX_SPAWN_PROC_TYPE_DAEMON_STANDARD       0x00000300
207#define POSIX_SPAWN_PROC_TYPE_DAEMON_INTERACTIVE    0x00000400
208#define POSIX_SPAWN_PROC_TYPE_DAEMON_BACKGROUND     0x00000500
209#define POSIX_SPAWN_PROC_TYPE_DAEMON_ADAPTIVE       0x00000600
210
211/*
212 * Allowable posix_spawn() file actions
213 */
214typedef enum {
215	PSFA_OPEN = 0,
216	PSFA_CLOSE = 1,
217	PSFA_DUP2 = 2,
218	PSFA_INHERIT = 3
219} psfa_t;
220
221
222/*
223 * A posix_spawn() file action record for a single action
224 *
225 * Notes:	We carry around the full open arguments for both the open
226 *		and the close to permit the use of a single array of action
227 *		elements to be associated with a file actions object.
228 *
229 *		A possible future optimization would be to break this into
230 *		a variable sized vector list to save space (i.e. a separate
231 *		string area, allocation of least amount of path buffer per
232 *		open action, etc.).
233 *
234 * XXX:		Currently overloading psfao_oflag for PSFA_DUP2
235 */
236typedef struct _psfa_action {
237	psfa_t	psfaa_type;			/* file action type */
238	int	psfaa_filedes;			/* fd to operate on */
239	struct _psfaa_open {
240		int	psfao_oflag;		/* open flags to use */
241		mode_t	psfao_mode;		/* mode for open */
242		char	psfao_path[PATH_MAX];	/* path to open */
243	} psfaa_openargs;
244} _psfa_action_t;
245
246
247/*
248 * Internal representation of posix_spawn() file actions structure
249 *
250 * Notes:	This is implemented as a structure followed by an array of
251 *		file action records.  The psfa_act_alloc value is the number
252 *		of elements allocated in this array, and the psfa_act_count is
253 *		the number of elements currently in use (to permit some form
254 *		of preallocation, e.g. a power of 2 growth for reallocation,
255 *		etc.).
256 *
257 *		A possible future optimization would keep a size value and
258 *		a structure base reference pointer to permit copyin to the
259 *		kernel directly as a single blob, without damaging relative
260 *		internal pointer math.  It's probably better that this be a
261 *		long long rather than a true pointer, to make it invariant
262 *		for 32 vs. 64 bt programming SPIs.
263 */
264typedef struct _posix_spawn_file_actions {
265	int		psfa_act_alloc;		/* available actions space */
266	int		psfa_act_count;		/* count of defined actions */
267	_psfa_action_t	psfa_act_acts[];	/* actions array (uses c99) */
268} *_posix_spawn_file_actions_t;
269
270/*
271 * Calculate the size of a structure, given the number of elements that it is
272 * capable of containing.
273 */
274#define	PSF_ACTIONS_SIZE(x)	\
275	__offsetof(struct _posix_spawn_file_actions, psfa_act_acts[(x)])
276
277/*
278 * Initial count of actions in a struct _posix_spawn_file_actions after it is
279 * first allocated; this should be non-zero, since we expect that one would not
280 * have been allocated unless there was an intent to use it.
281 */
282#define	PSF_ACTIONS_INIT_COUNT	2
283
284/*
285 * Structure defining the true third argument to the posix_spawn() system call
286 * entry point; we wrap it and pass a descriptor so that we can know the
287 * copyin size ahead of time, and deal with copying in variant lists of things
288 * as single monolithic units, instead of many individual elements.  This is a
289 * performance optimization.
290 */
291struct _posix_spawn_args_desc {
292	__darwin_size_t		attr_size;	/* size of attributes block */
293	_posix_spawnattr_t	attrp;		/* pointer to block */
294	__darwin_size_t	file_actions_size;	/* size of file actions block */
295	_posix_spawn_file_actions_t
296				file_actions;	/* pointer to block */
297	__darwin_size_t	port_actions_size; 	/* size of port actions block */
298	_posix_spawn_port_actions_t
299				port_actions; 	/* pointer to port block */
300	__darwin_size_t mac_extensions_size;
301	_posix_spawn_mac_policy_extensions_t
302				mac_extensions;	/* pointer to policy-specific
303						 * attributes */
304
305};
306
307#ifdef KERNEL
308#include <sys/appleapiopts.h>
309#ifdef __APPLE_API_PRIVATE
310
311#if __DARWIN_ALIGN_NATURAL
312#pragma options align=natural
313#endif
314
315struct user32__posix_spawn_args_desc {
316	uint32_t		attr_size;	/* size of attributes block */
317	uint32_t		attrp;		/* pointer to block */
318	uint32_t	file_actions_size;	/* size of file actions block */
319	uint32_t		file_actions;	/* pointer to block */
320	uint32_t	port_actions_size;	/* size of port actions block */
321	uint32_t		port_actions;	/* pointer to block */
322	uint32_t	mac_extensions_size;
323	uint32_t	mac_extensions;
324};
325
326struct user__posix_spawn_args_desc {
327	user_size_t		attr_size;	/* size of attributes block */
328	user_addr_t		attrp;		/* pointer to block */
329	user_size_t	file_actions_size;	/* size of file actions block */
330	user_addr_t		file_actions;	/* pointer to block */
331	user_size_t	port_actions_size;	/* size of port actions block */
332	user_addr_t		port_actions;	/* pointer to block */
333	user_size_t	mac_extensions_size;	/* size of MAC-specific attrs. */
334	user_addr_t	mac_extensions;		/* pointer to block */
335};
336
337
338#if __DARWIN_ALIGN_NATURAL
339#pragma options align=reset
340#endif
341
342#endif	/* __APPLE_API_PRIVATE */
343#endif	/* KERNEL */
344
345#endif	/* _SYS_SPAWN_INTERNAL_H_ */
346