xref: /xnu-8020.140.41/libsyscall/wrappers/spawn/posix_spawn.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * Copyright (c) 2006-2012 Apple Inc. All rights reserved.
3  *
4  * @APPLE_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. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 /*
25  * [SPN] Support for _POSIX_SPAWN
26  */
27 
28 #define CONFIG_MEMORYSTATUS 1 // <rdar://problem/13604997>
29 #include <sys/types.h> /* for user_size_t */
30 #include <spawn.h>
31 #include <spawn_private.h>
32 #include <sys/spawn_internal.h>
33 #include <sys/process_policy.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <limits.h>     /* for OPEN_MAX, PATH_MAX */
37 #include <string.h>
38 #include <strings.h>
39 #include <mach/port.h>
40 #include <mach/mach_param.h> /* for TASK_PORT_REGISTER_MAX */
41 #include <mach/exception_types.h>
42 #include <mach/coalition.h> /* for COALITION_TYPE_MAX */
43 #include <sys/kern_memorystatus.h>
44 
45 /*
46  * Actual syscall wrappers.
47  */
48 extern int __posix_spawn(pid_t * __restrict, const char * __restrict,
49     struct _posix_spawn_args_desc *, char *const argv[__restrict],
50     char *const envp[__restrict]);
51 extern int __execve(const char *fname, char * const *argp, char * const *envp);
52 
53 /*
54  * Function pointers that are NULL in Libsyscall_static, and get populated with
55  * the real implementations in Libsyscall_dynamic from _libkernel_init.c when
56  * os_feature_enabled(Libsystem, posix_spawn_filtering) is on.
57  *
58  * Since launchd cannot read feature flags during process startup (data volume
59  * is not mounted yet), it reads them later and sets these function pointers via
60  * Libsystem and __libkernel_init_after_boot_tasks.
61  *
62  * Usually NULL. Always NULL on customer installs.
63  */
64 __attribute__((visibility("hidden")))
65 bool (*posix_spawn_with_filter)(pid_t *pid, const char *fname,
66     char * const *argp, char * const *envp, struct _posix_spawn_args_desc *adp,
67     int *ret);
68 
69 __attribute__((visibility("hidden")))
70 int (*execve_with_filter)(const char *fname, char * const *argp,
71     char * const *envp);
72 
73 __attribute__((visibility("hidden")))
74 void
__posix_spawnattr_init(struct _posix_spawnattr * psattrp)75 __posix_spawnattr_init(struct _posix_spawnattr *psattrp)
76 {
77 	/*
78 	 * The default value of this attribute shall be as if no
79 	 * flags were set
80 	 */
81 	psattrp->psa_flags = 0;
82 
83 	/*
84 	 * The default value of this attribute shall be an empty
85 	 * signal set
86 	 */
87 	psattrp->psa_sigdefault = 0;
88 
89 	/* The default value of this attribute is unspecified */
90 	psattrp->psa_sigmask = 0;
91 
92 	/* The default value of this attribute shall be zero */
93 	psattrp->psa_pgroup = 0;     /* doesn't matter */
94 
95 	/* Default is no binary preferences, i.e. use normal grading */
96 	memset(psattrp->psa_binprefs, 0,
97 	    sizeof(psattrp->psa_binprefs));
98 	memset(psattrp->psa_subcpuprefs, 0xff /* CPU_SUBTYPE_ANY */,
99 	    sizeof(psattrp->psa_subcpuprefs));
100 
101 	/* Default is no port actions to take */
102 	psattrp->psa_ports = NULL;
103 
104 	/*
105 	 * The default value of this attribute shall be an no
106 	 * process control on resource starvation
107 	 */
108 	psattrp->psa_pcontrol = 0;
109 
110 	/*
111 	 * Initializing the alignment paddings.
112 	 */
113 
114 	psattrp->short_padding = 0;
115 	psattrp->flags_padding = 0;
116 
117 	/* Default is no new apptype requested */
118 	psattrp->psa_apptype = POSIX_SPAWN_PROCESS_TYPE_DEFAULT;
119 
120 	/* Jetsam related */
121 	psattrp->psa_jetsam_flags = 0;
122 	psattrp->psa_priority = -1;
123 	psattrp->psa_memlimit_active = -1;
124 	psattrp->psa_memlimit_inactive = -1;
125 
126 	/* Default is no thread limit */
127 	psattrp->psa_thread_limit = 0;
128 
129 	/* Default is no CPU usage monitor active. */
130 	psattrp->psa_cpumonitor_percent = 0;
131 	psattrp->psa_cpumonitor_interval = 0;
132 
133 	/* Default is no MAC policy extensions. */
134 	psattrp->psa_mac_extensions = NULL;
135 
136 	/* Default is to inherit parent's coalition(s) */
137 	psattrp->psa_coalition_info = NULL;
138 
139 	psattrp->psa_persona_info = NULL;
140 
141 	psattrp->psa_posix_cred_info = NULL;
142 
143 	/*
144 	 * old coalition field
145 	 * For backwards compatibility reasons, we set this to 1
146 	 * which is the first valid coalition id. This will allow
147 	 * newer user space code to properly spawn processes on
148 	 * older kernels
149 	 * (they will just all end up in the same coalition).
150 	 */
151 	psattrp->psa_reserved = 1;
152 
153 	/* Default is no new clamp */
154 	psattrp->psa_qos_clamp = POSIX_SPAWN_PROC_CLAMP_NONE;
155 
156 	/* Default is no change to role */
157 	psattrp->psa_darwin_role = POSIX_SPAWN_DARWIN_ROLE_NONE;
158 
159 	psattrp->psa_max_addr = 0;
160 
161 	psattrp->psa_no_smt = false;
162 	psattrp->psa_tecs = false;
163 
164 	/* Default is no subsystem root path */
165 	psattrp->psa_subsystem_root_path = NULL;
166 
167 	/* Default is no platform given */
168 	psattrp->psa_platform = 0;
169 
170 	/* Default is no option */
171 	psattrp->psa_options = PSA_OPTION_NONE;
172 
173 	/* Default is no port limit */
174 	psattrp->psa_port_soft_limit = 0;
175 	psattrp->psa_port_hard_limit = 0;
176 
177 	/* Default is no file descriptor limit */
178 	psattrp->psa_filedesc_soft_limit = 0;
179 	psattrp->psa_filedesc_hard_limit = 0;
180 }
181 
182 /*
183  * posix_spawnattr_init
184  *
185  * Description:	Initialize a spawn attributes object attr with default values
186  *
187  * Parameters:	attr			The spawn attributes object to be
188  *					initialized
189  *
190  * Returns:	0			Success
191  *		ENOMEM			Insufficient memory exists to
192  *					initialize the spawn attributes object.
193  *
194  * Note:	As an implementation detail, the externally visibily type
195  *		posix_spawnattr_t is defined to be a void *, and initialization
196  *		involves allocation of a memory object.  Subsequent changes to
197  *		the spawn attributes may result in reallocation under the
198  *		covers.
199  *
200  *		Reinitialization of an already initialized spawn attributes
201  *		object will result in memory being leaked.  Because spawn
202  *		attributes are not required to be used in conjunction with a
203  *		static initializer, there is no way to distinguish a spawn
204  *		attribute with stack garbage from one that's been initialized.
205  *		This is arguably an API design error.
206  */
207 int
posix_spawnattr_init(posix_spawnattr_t * attr)208 posix_spawnattr_init(posix_spawnattr_t *attr)
209 {
210 	_posix_spawnattr_t *psattrp = (_posix_spawnattr_t *)attr;
211 	int     err = 0;
212 
213 	if ((*psattrp = (_posix_spawnattr_t)malloc(sizeof(struct _posix_spawnattr))) == NULL) {
214 		err = ENOMEM;
215 	} else {
216 		__posix_spawnattr_init(*psattrp);
217 	}
218 
219 	return err;
220 }
221 
222 
223 /*
224  * posix_spawnattr_destroy
225  *
226  * Description:	Destroy a spawn attributes object that was previously
227  *		initialized via posix_spawnattr_init() by freeing any
228  *		memory associated with it and setting it to an invalid value.
229  *
230  * Parameters:	attr			The spawn attributes object to be
231  *					destroyed.
232  *
233  * Returns:	0			Success
234  *
235  * Notes:	The destroyed spawn attribute results in the void * pointer
236  *		being set to NULL; subsequent use without reinitialization
237  *		will result in explicit program failure (rather than merely
238  *		"undefined behaviour").
239  *
240  * NOTIMP:	Allowed failures (checking NOT required):
241  *		EINVAL	The value specified by attr is invalid.
242  */
243 static int posix_spawn_destroyportactions_np(posix_spawnattr_t *);
244 static int posix_spawn_destroycoalition_info_np(posix_spawnattr_t *);
245 static int posix_spawn_destroypersona_info_np(posix_spawnattr_t *);
246 static int posix_spawn_destroyposix_cred_info_np(posix_spawnattr_t *);
247 static int posix_spawn_destroymacpolicy_info_np(posix_spawnattr_t *);
248 static int posix_spawn_destroysubsystem_root_path_np(posix_spawnattr_t *);
249 
250 int
posix_spawnattr_destroy(posix_spawnattr_t * attr)251 posix_spawnattr_destroy(posix_spawnattr_t *attr)
252 {
253 	_posix_spawnattr_t psattr;
254 
255 	if (attr == NULL || *attr == NULL) {
256 		return EINVAL;
257 	}
258 
259 	psattr = *(_posix_spawnattr_t *)attr;
260 	posix_spawn_destroyportactions_np(attr);
261 	posix_spawn_destroycoalition_info_np(attr);
262 	posix_spawn_destroypersona_info_np(attr);
263 	posix_spawn_destroyposix_cred_info_np(attr);
264 	posix_spawn_destroymacpolicy_info_np(attr);
265 	posix_spawn_destroysubsystem_root_path_np(attr);
266 
267 	free(psattr);
268 	*attr = NULL;
269 
270 	return 0;
271 }
272 
273 
274 /*
275  * posix_spawnattr_setflags
276  *
277  * Description:	Set the spawn flags attribute for the spawn attribute object
278  *		referred to by 'attr'.
279  *
280  * Parameters:	attr			The spawn attributes object whose flags
281  *					are to be set
282  *		flags			The flags value to set
283  *
284  * Returns:	0			Success
285  *
286  * NOTIMP:	Allowed failures (checking NOT required):
287  *		EINVAL	The value specified by attr is invalid.
288  *		EINVAL	The value of the attribute being set is not valid.
289  */
290 int
posix_spawnattr_setflags(posix_spawnattr_t * attr,short flags)291 posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags)
292 {
293 	_posix_spawnattr_t psattr;
294 
295 	if (attr == NULL || *attr == NULL) {
296 		return EINVAL;
297 	}
298 
299 	psattr = *(_posix_spawnattr_t *)attr;
300 	psattr->psa_flags = flags;
301 
302 	return 0;
303 }
304 
305 
306 /*
307  * posix_spawnattr_getflags
308  *
309  * Description:	Retrieve the spawn attributes flag for the spawn attributes
310  *		object referenced by 'attr' and place them in the memory
311  *		location referenced by 'flagsp'
312  *
313  * Parameters:	attr			The spawn attributes object whose flags
314  *					are to be retrieved
315  *		flagsp			A pointer to a short value to receive
316  *					the flags
317  *
318  * Returns:	0			Success
319  *
320  * Implicit Returns:
321  *		*flagps (modified)	The flags value from the spawn
322  *					attributes object
323  *
324  * NOTIMP:	Allowed failures (checking NOT required):
325  *		EINVAL	The value specified by attr is invalid.
326  *		EINVAL	The value of the attribute being set is not valid.
327  */
328 int
posix_spawnattr_getflags(const posix_spawnattr_t * __restrict attr,short * __restrict flagsp)329 posix_spawnattr_getflags(const posix_spawnattr_t * __restrict attr,
330     short * __restrict flagsp)
331 {
332 	_posix_spawnattr_t psattr;
333 
334 	if (attr == NULL || *attr == NULL) {
335 		return EINVAL;
336 	}
337 
338 	psattr = *(_posix_spawnattr_t *)attr;
339 	*flagsp = psattr->psa_flags;
340 
341 	return 0;
342 }
343 
344 
345 /*
346  * posix_spawnattr_getsigdefault
347  *
348  * Description:	Retrieve the set of signals to be set to default according to
349  *		the spawn attribute value referenced by 'attr' and place the
350  *		result into the memory containing the sigset_t referenced by
351  *		'sigdefault'
352  *
353  * Parameters:	attr			The spawn attributes object whose
354  *					signal set for default signals is to
355  *					be retrieved
356  *		sigdefault		A pointer to the sigset_t to receive
357  *					the signal set
358  *
359  * Returns:	0			Success
360  *
361  * Implicit Returns:
362  *		*sigdefault (modified)	The signal set of signals to default
363  *					from the spawn attributes object
364  */
365 int
posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict attr,sigset_t * __restrict sigdefault)366 posix_spawnattr_getsigdefault(const posix_spawnattr_t * __restrict attr,
367     sigset_t * __restrict sigdefault)
368 {
369 	_posix_spawnattr_t psattr;
370 
371 	if (attr == NULL || *attr == NULL) {
372 		return EINVAL;
373 	}
374 
375 	psattr = *(_posix_spawnattr_t *)attr;
376 	*sigdefault = psattr->psa_sigdefault;
377 
378 	return 0;
379 }
380 
381 
382 /*
383  * posix_spawnattr_getpgroup
384  *
385  * Description:	Obtain the value of the spawn process group attribute from the
386  *		spawn attributes object referenced by 'attr' and place the
387  *		results in the memory location referenced by 'pgroup'
388  *
389  * Parameters:	attr			The spawn attributes object whose
390  *					process group information is to be
391  *					retrieved
392  *		pgroup			A pointer to the pid_t to receive the
393  *					process group
394  *
395  * Returns:	0			Success
396  *
397  * Implicit Returns:
398  *		*pgroup (modified)	The process group information from the
399  *					spawn attributes object
400  */
401 int
posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict attr,pid_t * __restrict pgroup)402 posix_spawnattr_getpgroup(const posix_spawnattr_t * __restrict attr,
403     pid_t * __restrict pgroup)
404 {
405 	_posix_spawnattr_t psattr;
406 
407 	if (attr == NULL || *attr == NULL) {
408 		return EINVAL;
409 	}
410 
411 	psattr = *(_posix_spawnattr_t *)attr;
412 	*pgroup = psattr->psa_pgroup;
413 
414 	return 0;
415 }
416 
417 
418 /*
419  * posix_spawnattr_getsigmask
420  *
421  * Description:	Obtain the value of the spawn signal mask attribute from the
422  *		spawn attributes object referenced by 'attr' and place the
423  *		result into the memory containing the sigset_t referenced by
424  *		'sigmask'
425  *
426  * Parameters:	attr			The spawn attributes object whose
427  *					signal set for masked signals is to
428  *					be retrieved
429  *		sigmask		A pointer to the sigset_t to receive
430  *					the signal set
431  *
432  * Returns:	0			Success
433  *
434  * Implicit Returns:
435  *		*sigmask (modified)	The signal set of signals to mask
436  *					from the spawn attributes object
437  */
438 int
posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict attr,sigset_t * __restrict sigmask)439 posix_spawnattr_getsigmask(const posix_spawnattr_t * __restrict attr,
440     sigset_t * __restrict sigmask)
441 {
442 	_posix_spawnattr_t psattr;
443 
444 	if (attr == NULL || *attr == NULL) {
445 		return EINVAL;
446 	}
447 
448 	psattr = *(_posix_spawnattr_t *)attr;
449 	*sigmask = psattr->psa_sigmask;
450 
451 	return 0;
452 }
453 
454 /*
455  * posix_spawnattr_getbinpref_np
456  *
457  * Description:	Obtain the value of the spawn binary preferences attribute from
458  *              the spawn attributes object referenced by 'attr' and place the
459  *		result into the memory referenced by 'pref'.
460  *
461  * Parameters:	attr			The spawn attributes object whose
462  *					binary preferences are to be retrieved
463  *		count			The size of the cpu_type_t array
464  *		pref			An array of cpu types
465  *		ocount			The actual number copied
466  *
467  * Returns:	0			No binary preferences found
468  *              > 0			The number of cpu types (less than
469  *                                      count) copied over from 'attr'.
470  *
471  * Implicit Returns:
472  *		*pref (modified)	The binary preferences array
473  *					from the spawn attributes object
474  */
475 int
posix_spawnattr_getbinpref_np(const posix_spawnattr_t * __restrict attr,size_t count,cpu_type_t * pref,size_t * __restrict ocount)476 posix_spawnattr_getbinpref_np(const posix_spawnattr_t * __restrict attr,
477     size_t count, cpu_type_t *pref, size_t * __restrict ocount)
478 {
479 	_posix_spawnattr_t psattr;
480 	int i = 0;
481 
482 	if (attr == NULL || *attr == NULL || pref == NULL) {
483 		return EINVAL;
484 	}
485 
486 	psattr = *(_posix_spawnattr_t *)attr;
487 	for (i = 0; i < count && i < NBINPREFS; i++) {
488 		pref[i] = psattr->psa_binprefs[i];
489 	}
490 
491 	if (ocount) {
492 		*ocount = i;
493 	}
494 	return 0;
495 }
496 
497 /*
498  * posix_spawnattr_getarchpref_np
499  *
500  * Description:	Obtain the value of the spawn binary preferences attribute from
501  *              the spawn attributes object referenced by 'attr' and place the
502  *		result into the memory referenced by 'pref' and 'subpref'.
503  *
504  * Parameters:	attr			The spawn attributes object whose
505  *					binary preferences are to be retrieved
506  *		count			The size of the cpu_type_t array
507  *		pref			An array of cpu types
508  *		subpref			An array of subcpu types
509  *		ocount			The actual number copied
510  *
511  * Returns:	0			No cpu/subcpu preferences found
512  *              > 0			The number of types (less than
513  *                                      count) copied over from 'attr'.
514  *
515  * Implicit Returns:
516  *		*pref (modified)	The cpu preferences array
517  *					from the spawn attributes object
518  *		*subpref (modified)	The subcpu preferences array
519  *					from the spawn attributes object
520  */
521 int
posix_spawnattr_getarchpref_np(const posix_spawnattr_t * __restrict attr,size_t count,cpu_type_t * pref,cpu_subtype_t * subpref,size_t * __restrict ocount)522 posix_spawnattr_getarchpref_np(const posix_spawnattr_t * __restrict attr,
523     size_t count, cpu_type_t *pref, cpu_subtype_t *subpref, size_t * __restrict ocount)
524 {
525 	_posix_spawnattr_t psattr;
526 	int i = 0;
527 
528 	if (attr == NULL || *attr == NULL || pref == NULL || subpref == NULL) {
529 		return EINVAL;
530 	}
531 
532 	psattr = *(_posix_spawnattr_t *)attr;
533 	for (i = 0; i < count && i < NBINPREFS; i++) {
534 		pref[i] = psattr->psa_binprefs[i];
535 		subpref[i] = psattr->psa_subcpuprefs[i];
536 	}
537 
538 	if (ocount) {
539 		*ocount = i;
540 	}
541 	return 0;
542 }
543 
544 
545 /*
546  * posix_spawnattr_getpcontrol_np
547  *
548  * Description:	Retrieve the  process control property set default according to
549  *		the spawn attribute value referenced by 'attr' and place the
550  *		result into the memory containing the control  referenced by
551  *		'pcontrol'
552  *
553  * Parameters:	attr			The spawn attributes object whose
554  *					signal set for default signals is to
555  *					be retrieved
556  *		pcontrol		A pointer to an int  to receive
557  *					the process control info
558  *
559  * Returns:	0			Success
560  *
561  * Implicit Returns:
562  *		*pcontrol (modified)	The signal set of signals to default
563  *					from the spawn attributes object
564  */
565 int
posix_spawnattr_getpcontrol_np(const posix_spawnattr_t * __restrict attr,int * __restrict pcontrol)566 posix_spawnattr_getpcontrol_np(const posix_spawnattr_t * __restrict attr,
567     int * __restrict pcontrol)
568 {
569 	_posix_spawnattr_t psattr;
570 
571 	if (attr == NULL || *attr == NULL) {
572 		return EINVAL;
573 	}
574 
575 	psattr = *(_posix_spawnattr_t *)attr;
576 	*pcontrol = psattr->psa_pcontrol;
577 
578 	return 0;
579 }
580 
581 /*
582  * posix_spawnattr_getprocesstype_np
583  *
584  * Description:	Retrieve the  process specific behaviors and app launch types
585  *		spawn attribute value referenced by 'attr' and place the
586  *		result into the memory containing the control  referenced by
587  *		'proctype'
588  *
589  * Parameters:	attr			The spawn attributes object whose
590  *					signal set for default signals is to
591  *					be retrieved
592  *		proctype		A pointer to an int  to receive
593  *					the process type info
594  *
595  * Returns:	0			Success
596  *
597  * Implicit Returns:
598  *		*proctype (modified)	The process type set to value
599  *					from the spawn attributes object
600  */
601 int
posix_spawnattr_getprocesstype_np(const posix_spawnattr_t * __restrict attr,int * __restrict proctype)602 posix_spawnattr_getprocesstype_np(const posix_spawnattr_t * __restrict attr,
603     int * __restrict proctype)
604 {
605 	_posix_spawnattr_t psattr;
606 
607 	if (attr == NULL || *attr == NULL) {
608 		return EINVAL;
609 	}
610 
611 	psattr = *(_posix_spawnattr_t *)attr;
612 	*proctype = psattr->psa_apptype;
613 
614 	return 0;
615 }
616 /*
617  * posix_spawnattr_setsigdefault
618  *
619  * Description:	Set the set of signals to be set to default for the spawn
620  *		attribute value referenced by 'attr' from the memory
621  *		containing the sigset_t referenced by 'sigdefault'
622  *
623  * Parameters:	attr			The spawn attributes object whose
624  *					signal set for default signals is to
625  *					be set
626  *		sigdefault		A pointer to the sigset_t from which to
627  *					obtain the signal set
628  *
629  * Returns:	0			Success
630  */
631 int
posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict attr,const sigset_t * __restrict sigdefault)632 posix_spawnattr_setsigdefault(posix_spawnattr_t * __restrict attr,
633     const sigset_t * __restrict sigdefault)
634 {
635 	_posix_spawnattr_t psattr;
636 
637 	if (attr == NULL || *attr == NULL) {
638 		return EINVAL;
639 	}
640 
641 	psattr = *(_posix_spawnattr_t *)attr;
642 	psattr->psa_sigdefault = *sigdefault;
643 
644 	return 0;
645 }
646 
647 
648 /*
649  * posix_spawnattr_setpgroup
650  *
651  * Description:	Set the value of the spawn process group attribute for the
652  *		spawn attributes object referenced by 'attr' from the value
653  *		of 'pgroup'
654  *
655  * Parameters:	attr			The spawn attributes object for which
656  *					the process group information is to be
657  *					set
658  *		pgroup			The process group to set
659  *
660  * Returns:	0			Success
661  */
662 int
posix_spawnattr_setpgroup(posix_spawnattr_t * attr,pid_t pgroup)663 posix_spawnattr_setpgroup(posix_spawnattr_t * attr, pid_t pgroup)
664 {
665 	_posix_spawnattr_t psattr;
666 
667 	if (attr == NULL || *attr == NULL) {
668 		return EINVAL;
669 	}
670 
671 	psattr = *(_posix_spawnattr_t *)attr;
672 	psattr->psa_pgroup = pgroup;
673 
674 	return 0;
675 }
676 
677 
678 /*
679  * posix_spawnattr_setsigmask
680  *
681  * Description:	Set the set of signals to be masked for the spawn attribute
682  *		value referenced by 'attr' from the memory containing the
683  *		sigset_t referenced by 'sigmask'
684  *
685  * Parameters:	attr			The spawn attributes object whose
686  *					signal set for masked signals is to
687  *					be set
688  *		sigmask		A pointer to the sigset_t from which to
689  *					obtain the signal set
690  *
691  * Returns:	0			Success
692  */
693 int
posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict attr,const sigset_t * __restrict sigmask)694 posix_spawnattr_setsigmask(posix_spawnattr_t * __restrict attr,
695     const sigset_t * __restrict sigmask)
696 {
697 	_posix_spawnattr_t psattr;
698 
699 	if (attr == NULL || *attr == NULL) {
700 		return EINVAL;
701 	}
702 
703 	psattr = *(_posix_spawnattr_t *)attr;
704 	psattr->psa_sigmask = *sigmask;
705 
706 	return 0;
707 }
708 
709 
710 /*
711  * posix_spawnattr_setbinpref_np
712  *
713  * Description:	Set the universal binary preferences for the spawn attribute
714  *		value referenced by 'attr' from the memory containing the
715  *		cpu_type_t array referenced by 'pref', size of 'count'
716  *
717  * Parameters:	attr			The spawn attributes object whose
718  *                                      binary preferences are to be set
719  *              count			Size of the array pointed to by 'pref'
720  *              pref			cpu_type_t array of binary preferences
721  *		ocount			The actual number copied
722  *
723  * Returns:	0			No preferences copied
724  *              > 0			Number of preferences copied
725  *
726  * Note:	The posix_spawnattr_t currently only holds four cpu_type_t's.
727  *              If the caller provides more preferences than this limit, they
728  *              will be ignored, as reflected in the return value.
729  */
730 int
posix_spawnattr_setbinpref_np(posix_spawnattr_t * __restrict attr,size_t count,cpu_type_t * pref,size_t * __restrict ocount)731 posix_spawnattr_setbinpref_np(posix_spawnattr_t * __restrict attr,
732     size_t count, cpu_type_t *pref, size_t * __restrict ocount)
733 {
734 	_posix_spawnattr_t psattr;
735 	int i = 0;
736 
737 	if (attr == NULL || *attr == NULL || pref == NULL) {
738 		return EINVAL;
739 	}
740 
741 	psattr = *(_posix_spawnattr_t *)attr;
742 	for (i = 0; i < count && i < NBINPREFS; i++) {
743 		psattr->psa_binprefs[i] = pref[i];
744 		psattr->psa_subcpuprefs[i] = CPU_SUBTYPE_ANY;
745 	}
746 
747 	/* return number of binprefs copied over */
748 	if (ocount) {
749 		*ocount = i;
750 	}
751 
752 	for (; i < NBINPREFS; i++) {
753 		psattr->psa_binprefs[i] = 0;
754 		psattr->psa_subcpuprefs[i] = CPU_SUBTYPE_ANY;
755 	}
756 
757 	return 0;
758 }
759 
760 /*
761  * posix_spawnattr_setarchpref_np
762  *
763  * Description:	Set the universal binary preferences for the spawn attribute
764  *		value referenced by 'attr' from the memory containing the
765  *		cpu_type_t array referenced by 'pref', the cpu_subtype_t array
766  *		referenced by 'subpref' and size of 'count'
767  *
768  * Parameters:	attr			The spawn attributes object whose
769  *                                      binary preferences are to be set
770  *              count			Size of the array pointed to by 'pref'
771  *              pref			cpu_type_t array of cpu binary preferences
772  *              subpref			cpu_subtype_t array of subcpu binary preferences
773  *		ocount			The actual number copied
774  *
775  * Returns:	0			No preferences copied
776  *              > 0			Number of preferences copied
777  *
778  * Note:	The posix_spawnattr_t currently only holds four
779  *              cpu_type_t/cpu_subtype_t pairs.
780  *              If the caller provides more preferences than this limit, they
781  *              will be ignored, as reflected in the return value.
782  */
783 int
posix_spawnattr_setarchpref_np(posix_spawnattr_t * __restrict attr,size_t count,cpu_type_t * pref,cpu_subtype_t * subpref,size_t * __restrict ocount)784 posix_spawnattr_setarchpref_np(posix_spawnattr_t * __restrict attr,
785     size_t count, cpu_type_t *pref, cpu_subtype_t *subpref,
786     size_t * __restrict ocount)
787 {
788 	_posix_spawnattr_t psattr;
789 	int i = 0;
790 
791 	if (attr == NULL || *attr == NULL || pref == NULL || subpref == NULL) {
792 		return EINVAL;
793 	}
794 
795 	psattr = *(_posix_spawnattr_t *)attr;
796 	for (i = 0; i < count && i < NBINPREFS; i++) {
797 		psattr->psa_binprefs[i] = pref[i];
798 		psattr->psa_subcpuprefs[i] = subpref[i];
799 	}
800 
801 	/* return number of binprefs copied over */
802 	if (ocount) {
803 		*ocount = i;
804 	}
805 
806 	for (; i < NBINPREFS; i++) {
807 		psattr->psa_binprefs[i] = 0;
808 		psattr->psa_subcpuprefs[i] = CPU_SUBTYPE_ANY;
809 	}
810 
811 	return 0;
812 }
813 
814 /*
815  * posix_spawnattr_setpcontrol_np
816  *
817  * Description:	Set the process control property according to
818  *		attribute value referenced by 'attr' from the memory
819  *		containing the int value 'pcontrol'
820  *
821  * Parameters:	attr			The spawn attributes object whose
822  *					signal set for default signals is to
823  *					be set
824  *		pcontrol		An int value of the process control info
825  *
826  * Returns:	0			Success
827  */
828 int
posix_spawnattr_setpcontrol_np(posix_spawnattr_t * __restrict attr,const int pcontrol)829 posix_spawnattr_setpcontrol_np(posix_spawnattr_t * __restrict attr,
830     const int pcontrol)
831 {
832 	_posix_spawnattr_t psattr;
833 
834 	if (attr == NULL || *attr == NULL) {
835 		return EINVAL;
836 	}
837 
838 	psattr = *(_posix_spawnattr_t *)attr;
839 	psattr->psa_pcontrol = pcontrol;
840 
841 	return 0;
842 }
843 
844 
845 /*
846  * posix_spawnattr_setprocesstype_np
847  *
848  * Description:	Set the process specific behaviors and app launch type
849  *		attribute value referenced by 'attr' from the memory
850  *		containing the int value 'proctype'
851  *
852  * Parameters:	attr			The spawn attributes object whose
853  *					signal set for default signals is to
854  *					be set
855  *		proctype		An int value of the process type info
856  *
857  * Returns:	0			Success
858  */
859 int
posix_spawnattr_setprocesstype_np(posix_spawnattr_t * __restrict attr,const int proctype)860 posix_spawnattr_setprocesstype_np(posix_spawnattr_t * __restrict attr,
861     const int proctype)
862 {
863 	_posix_spawnattr_t psattr;
864 
865 	if (attr == NULL || *attr == NULL) {
866 		return EINVAL;
867 	}
868 
869 	psattr = *(_posix_spawnattr_t *)attr;
870 	psattr->psa_apptype = proctype;
871 
872 	return 0;
873 }
874 
875 /*
876  * posix_spawn_createportactions_np
877  * Description: create a new posix_spawn_port_actions struct and link
878  *      it into the posix_spawnattr.
879  */
880 static int
posix_spawn_createportactions_np(posix_spawnattr_t * attr)881 posix_spawn_createportactions_np(posix_spawnattr_t *attr)
882 {
883 	_posix_spawnattr_t psattr;
884 	_posix_spawn_port_actions_t acts;
885 
886 	if (attr == NULL || *attr == NULL) {
887 		return EINVAL;
888 	}
889 
890 	psattr = *(_posix_spawnattr_t *)attr;
891 	acts = (_posix_spawn_port_actions_t)malloc(PS_PORT_ACTIONS_SIZE(2));
892 	if (acts == NULL) {
893 		return ENOMEM;
894 	}
895 
896 	acts->pspa_alloc = 2;
897 	acts->pspa_count = 0;
898 
899 	psattr->psa_ports = acts;
900 	return 0;
901 }
902 
903 /*
904  * posix_spawn_growportactions_np
905  * Description: Enlarge the size of portactions if necessary
906  */
907 static int
posix_spawn_growportactions_np(posix_spawnattr_t * attr)908 posix_spawn_growportactions_np(posix_spawnattr_t *attr)
909 {
910 	_posix_spawnattr_t psattr;
911 	_posix_spawn_port_actions_t acts;
912 
913 	if (attr == NULL || *attr == NULL) {
914 		return EINVAL;
915 	}
916 
917 	psattr = *(_posix_spawnattr_t *)attr;
918 	acts = psattr->psa_ports;
919 	if (acts == NULL) {
920 		return EINVAL;
921 	}
922 
923 	/* Double number of port actions allocated for */
924 	int newnum = 0;
925 	if (os_mul_overflow(acts->pspa_alloc, 2, &newnum)) {
926 		return ENOMEM;
927 	}
928 	size_t newsize = PS_PORT_ACTIONS_SIZE(newnum);
929 	if (newsize == 0) {
930 		return ENOMEM;
931 	}
932 
933 	acts = realloc(acts, newsize);
934 	if (acts == NULL) {
935 		return ENOMEM;
936 	}
937 
938 	acts->pspa_alloc = newnum;
939 	psattr->psa_ports = acts;
940 	return 0;
941 }
942 
943 /*
944  * posix_spawn_destroyportactions_np
945  * Description: clean up portactions struct in posix_spawnattr_t attr
946  */
947 static int
posix_spawn_destroyportactions_np(posix_spawnattr_t * attr)948 posix_spawn_destroyportactions_np(posix_spawnattr_t *attr)
949 {
950 	_posix_spawnattr_t psattr;
951 	_posix_spawn_port_actions_t acts;
952 
953 	if (attr == NULL || *attr == NULL) {
954 		return EINVAL;
955 	}
956 
957 	psattr = *(_posix_spawnattr_t *)attr;
958 	acts = psattr->psa_ports;
959 	if (acts == NULL) {
960 		return EINVAL;
961 	}
962 
963 	free(acts);
964 	return 0;
965 }
966 
967 /*
968  * posix_spawn_destroycoalition_info_np
969  * Description: clean up coalition_info struct in posix_spawnattr_t attr
970  */
971 static int
posix_spawn_destroycoalition_info_np(posix_spawnattr_t * attr)972 posix_spawn_destroycoalition_info_np(posix_spawnattr_t *attr)
973 {
974 	_posix_spawnattr_t psattr;
975 	struct _posix_spawn_coalition_info *coal_info;
976 
977 	if (attr == NULL || *attr == NULL) {
978 		return EINVAL;
979 	}
980 
981 	psattr = *(_posix_spawnattr_t *)attr;
982 	coal_info = psattr->psa_coalition_info;
983 	if (coal_info == NULL) {
984 		return EINVAL;
985 	}
986 
987 	psattr->psa_coalition_info = NULL;
988 	free(coal_info);
989 	return 0;
990 }
991 
992 /*
993  * posix_spawn_destroypersona_info_np
994  * Description: clean up persona_info struct in posix_spawnattr_t attr
995  */
996 static int
posix_spawn_destroypersona_info_np(posix_spawnattr_t * attr)997 posix_spawn_destroypersona_info_np(posix_spawnattr_t *attr)
998 {
999 	_posix_spawnattr_t psattr;
1000 	struct _posix_spawn_persona_info *persona;
1001 
1002 	if (attr == NULL || *attr == NULL) {
1003 		return EINVAL;
1004 	}
1005 
1006 	psattr = *(_posix_spawnattr_t *)attr;
1007 	persona = psattr->psa_persona_info;
1008 	if (persona == NULL) {
1009 		return EINVAL;
1010 	}
1011 
1012 	psattr->psa_persona_info = NULL;
1013 	free(persona);
1014 	return 0;
1015 }
1016 
1017 /*
1018  * posix_spawn_destroyposix_cred_info_np
1019  * Description: clean up posix_cred_info struct in posix_spawnattr_t attr
1020  */
1021 static int
posix_spawn_destroyposix_cred_info_np(posix_spawnattr_t * attr)1022 posix_spawn_destroyposix_cred_info_np(posix_spawnattr_t *attr)
1023 {
1024 	_posix_spawnattr_t psattr;
1025 	struct _posix_spawn_posix_cred_info *pspci;
1026 
1027 	if (attr == NULL || *attr == NULL) {
1028 		return EINVAL;
1029 	}
1030 
1031 	psattr = *(_posix_spawnattr_t *)attr;
1032 	pspci = psattr->psa_posix_cred_info;
1033 	if (pspci == NULL) {
1034 		return EINVAL;
1035 	}
1036 
1037 	psattr->psa_posix_cred_info = NULL;
1038 	free(pspci);
1039 	return 0;
1040 }
1041 
1042 /*
1043  * posix_spawn_set_subsystem_root_path
1044  * Description: Set path as the subsystem root path for attr; clears if NULL
1045  */
1046 int
posix_spawnattr_set_subsystem_root_path_np(posix_spawnattr_t * attr,char * path)1047 posix_spawnattr_set_subsystem_root_path_np(posix_spawnattr_t *attr, char *path)
1048 {
1049 	_posix_spawnattr_t psattr;
1050 	char * buf = NULL;
1051 	char * old_buf;
1052 	size_t bytes;
1053 
1054 	if (attr == NULL || *attr == NULL) {
1055 		return EINVAL;
1056 	}
1057 
1058 	psattr = *(_posix_spawnattr_t *)attr;
1059 
1060 	if (path) {
1061 		buf = malloc(MAXPATHLEN);
1062 
1063 		if (buf == NULL) {
1064 			return ENOMEM;
1065 		}
1066 
1067 		bytes = strlcpy(buf, path, MAXPATHLEN);
1068 
1069 		if (bytes >= MAXPATHLEN) {
1070 			free(buf);
1071 			return ENAMETOOLONG;
1072 		}
1073 	}
1074 
1075 	old_buf = psattr->psa_subsystem_root_path;
1076 	psattr->psa_subsystem_root_path = buf;
1077 
1078 	free(old_buf);
1079 
1080 	return 0;
1081 }
1082 
1083 /*
1084  * posix_spawn_destroy_subsystem_root_path_np
1085  * Description: clean up subsystem_root_path string in posix_spawnattr_t attr
1086  */
1087 static int
posix_spawn_destroysubsystem_root_path_np(posix_spawnattr_t * attr)1088 posix_spawn_destroysubsystem_root_path_np(posix_spawnattr_t *attr)
1089 {
1090 	_posix_spawnattr_t psattr;
1091 	char * subsystem_root_path;
1092 
1093 	if (attr == NULL || *attr == NULL) {
1094 		return EINVAL;
1095 	}
1096 
1097 	psattr = *(_posix_spawnattr_t *)attr;
1098 	subsystem_root_path = psattr->psa_subsystem_root_path;
1099 
1100 	if (subsystem_root_path == NULL) {
1101 		return EINVAL;
1102 	}
1103 
1104 	psattr->psa_subsystem_root_path = NULL;
1105 	free(subsystem_root_path);
1106 	return 0;
1107 }
1108 
1109 /*
1110  * posix_spawnattr_set_platform_np
1111  * Description: sets the platform in posix_spawnattr_t attr
1112  *
1113  * To be implemented.
1114  */
1115 int
posix_spawnattr_set_platform_np(posix_spawnattr_t * attr,int platform,uint32_t flags)1116 posix_spawnattr_set_platform_np(posix_spawnattr_t *attr, int platform, uint32_t flags)
1117 {
1118 	_posix_spawnattr_t psattr;
1119 
1120 	if (attr == NULL || *attr == NULL) {
1121 		return EINVAL;
1122 	}
1123 
1124 	psattr = *(_posix_spawnattr_t *)attr;
1125 	psattr->psa_platform = platform;
1126 
1127 	(void)flags;
1128 	return 0;
1129 }
1130 
1131 /*
1132  * posix_spawnattr_disable_ptr_auth_a_keys_np
1133  * Description: Set flag to disable A keys for Ptr Auth
1134  */
1135 int
posix_spawnattr_disable_ptr_auth_a_keys_np(posix_spawnattr_t * attr,uint32_t flags)1136 posix_spawnattr_disable_ptr_auth_a_keys_np(posix_spawnattr_t *attr, uint32_t flags)
1137 {
1138 	_posix_spawnattr_t psattr;
1139 
1140 	if (attr == NULL || *attr == NULL) {
1141 		return EINVAL;
1142 	}
1143 
1144 	psattr = *(_posix_spawnattr_t *)attr;
1145 
1146 	psattr->psa_options |= PSA_OPTION_PLUGIN_HOST_DISABLE_A_KEYS;
1147 	(void)flags;
1148 	return 0;
1149 }
1150 
1151 /*
1152  * posix_spawnattr_set_alt_rosetta_np
1153  * Description: Set flag to use alternative Rosetta runtime
1154  */
1155 int
posix_spawnattr_set_alt_rosetta_np(posix_spawnattr_t * attr,uint32_t flags)1156 posix_spawnattr_set_alt_rosetta_np(posix_spawnattr_t *attr, uint32_t flags)
1157 {
1158 	_posix_spawnattr_t psattr;
1159 
1160 	if (attr == NULL || *attr == NULL) {
1161 		return EINVAL;
1162 	}
1163 
1164 	psattr = *(_posix_spawnattr_t *)attr;
1165 
1166 	psattr->psa_options |= PSA_OPTION_ALT_ROSETTA;
1167 	(void)flags;
1168 	return 0;
1169 }
1170 
1171 /*
1172  * posix_spawn_appendportaction_np
1173  * Description: append a port action, grow the array if necessary
1174  */
1175 static int
posix_spawn_appendportaction_np(posix_spawnattr_t * attr,_ps_port_action_t * act)1176 posix_spawn_appendportaction_np(posix_spawnattr_t *attr, _ps_port_action_t *act)
1177 {
1178 	_posix_spawnattr_t psattr;
1179 	_posix_spawn_port_actions_t acts;
1180 
1181 	if (attr == NULL || *attr == NULL || act == NULL) {
1182 		return EINVAL;
1183 	}
1184 
1185 	psattr = *(_posix_spawnattr_t *)attr;
1186 	acts = psattr->psa_ports;
1187 
1188 	// Have any port actions been created yet?
1189 	if (acts == NULL) {
1190 		int err = posix_spawn_createportactions_np(attr);
1191 		if (err) {
1192 			return err;
1193 		}
1194 		acts = psattr->psa_ports;
1195 	}
1196 
1197 	// Is there enough room?
1198 	if (acts->pspa_alloc == acts->pspa_count) {
1199 		int err = posix_spawn_growportactions_np(attr);
1200 		if (err) {
1201 			return err;
1202 		}
1203 		acts = psattr->psa_ports;
1204 	}
1205 
1206 	// Add this action to next spot in array
1207 	acts->pspa_actions[acts->pspa_count] = *act;
1208 	acts->pspa_count++;
1209 
1210 	return 0;
1211 }
1212 
1213 /*
1214  * posix_spawnattr_setspecialport_np
1215  *
1216  * Description:	Set a new value for a mach special port in the spawned task.
1217  *
1218  * Parameters:	attr			The spawn attributes object for the
1219  *                                      new process
1220  *              new_port		The new value for the special port
1221  *              which			The particular port to be set
1222  *                                      (see task_set_special_port for details)
1223  *
1224  * Returns:	0			Success
1225  *              ENOMEM			Couldn't allocate memory
1226  */
1227 int
posix_spawnattr_setspecialport_np(posix_spawnattr_t * attr,mach_port_t new_port,int which)1228 posix_spawnattr_setspecialport_np(
1229 	posix_spawnattr_t *attr,
1230 	mach_port_t      new_port,
1231 	int             which)
1232 {
1233 	_ps_port_action_t action = {
1234 		.port_type = PSPA_SPECIAL,
1235 		.new_port = new_port,
1236 		.which = which,
1237 	};
1238 	return posix_spawn_appendportaction_np(attr, &action);
1239 }
1240 
1241 /*
1242  * posix_spawnattr_setexceptionports_np
1243  *
1244  * Description:	Set a new port for a set of exception ports in the spawned task.
1245  *
1246  * Parameters:	attr			The spawn attributes object for the
1247  *                                      new process
1248  *              mask			A bitfield indicating which exceptions
1249  *                                      to associate the port with
1250  *              new_port		The new value for the exception port
1251  *              behavior		The default behavior for the port
1252  *              flavor			The default flavor for the port
1253  *                                      (see task_set_exception_ports)
1254  *
1255  * Returns:	0			Success
1256  */
1257 int
posix_spawnattr_setexceptionports_np(posix_spawnattr_t * attr,exception_mask_t mask,mach_port_t new_port,exception_behavior_t behavior,thread_state_flavor_t flavor)1258 posix_spawnattr_setexceptionports_np(
1259 	posix_spawnattr_t       *attr,
1260 	exception_mask_t        mask,
1261 	mach_port_t              new_port,
1262 	exception_behavior_t    behavior,
1263 	thread_state_flavor_t   flavor)
1264 {
1265 	_ps_port_action_t action = {
1266 		.port_type = PSPA_EXCEPTION,
1267 		.mask = mask,
1268 		.new_port = new_port,
1269 		.behavior = behavior,
1270 		.flavor = flavor,
1271 	};
1272 	return posix_spawn_appendportaction_np(attr, &action);
1273 }
1274 
1275 /*
1276  * posix_spawnattr_setauditsessionport_np
1277  *
1278  * Description:	Set the audit session port rights attribute in the spawned task.
1279  *		This is used to securely set the audit session information for
1280  *		the new task.
1281  *
1282  * Parameters:	attr			The spawn attributes object for the
1283  *                                      new process
1284  *              au_sessionport		The audit session send port right
1285  *
1286  * Returns:	0			Success
1287  */
1288 int
posix_spawnattr_setauditsessionport_np(posix_spawnattr_t * attr,mach_port_t au_sessionport)1289 posix_spawnattr_setauditsessionport_np(
1290 	posix_spawnattr_t       *attr,
1291 	mach_port_t              au_sessionport)
1292 {
1293 	_ps_port_action_t action = {
1294 		.port_type = PSPA_AU_SESSION,
1295 		.new_port = au_sessionport,
1296 	};
1297 	return posix_spawn_appendportaction_np(attr, &action);
1298 }
1299 
1300 
1301 /*
1302  * posix_spawn_file_actions_init
1303  *
1304  * Description:	Initialize a spawn file actions object attr with default values
1305  *
1306  * Parameters:	file_actions		The spawn file actions object to be
1307  *					initialized
1308  *
1309  * Returns:	0			Success
1310  *		ENOMEM			Insufficient memory exists to
1311  *					initialize the spawn file actions
1312  *					object.
1313  *
1314  * Note:	As an implementation detail, the externally visibily type
1315  *		posix_spawn_file_actions_t is defined to be a void *, and
1316  *		initialization involves allocation of a memory object.
1317  *		Subsequent changes to the spawn file actions may result in
1318  *		reallocation under the covers.
1319  *
1320  *		Reinitialization of an already initialized spawn file actions
1321  *		object will result in memory being leaked.  Because spawn
1322  *		file actions are not required to be used in conjunction with a
1323  *		static initializer, there is no way to distinguish a spawn
1324  *		file actions with stack garbage from one that's been
1325  *		initialized.  This is arguably an API design error.
1326  */
1327 int
posix_spawn_file_actions_init(posix_spawn_file_actions_t * file_actions)1328 posix_spawn_file_actions_init(posix_spawn_file_actions_t *file_actions)
1329 {
1330 	_posix_spawn_file_actions_t *psactsp = (_posix_spawn_file_actions_t *)file_actions;
1331 	int     err = 0;
1332 
1333 	if ((*psactsp = (_posix_spawn_file_actions_t)malloc(PSF_ACTIONS_SIZE(PSF_ACTIONS_INIT_COUNT))) == NULL) {
1334 		err = ENOMEM;
1335 	} else {
1336 		(*psactsp)->psfa_act_alloc = PSF_ACTIONS_INIT_COUNT;
1337 		(*psactsp)->psfa_act_count = 0;
1338 	}
1339 
1340 	return err;
1341 }
1342 
1343 
1344 /*
1345  * posix_spawn_file_actions_destroy
1346  *
1347  * Description:	Destroy a spawn file actions object that was previously
1348  *		initialized via posix_spawn_file_actions_init() by freeing any
1349  *		memory associated with it and setting it to an invalid value.
1350  *
1351  * Parameters:	attr			The spawn file actions object to be
1352  *					destroyed.
1353  *
1354  * Returns:	0			Success
1355  *
1356  * Notes:	The destroyed spawn file actions results in the void * pointer
1357  *		being set to NULL; subsequent use without reinitialization
1358  *		will result in explicit program failure (rather than merely
1359  *		"undefined behaviour").
1360  *
1361  * NOTIMP:	Allowed failures (checking NOT required):
1362  *		EINVAL	The value specified by file_actions is invalid.
1363  */
1364 int
posix_spawn_file_actions_destroy(posix_spawn_file_actions_t * file_actions)1365 posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *file_actions)
1366 {
1367 	_posix_spawn_file_actions_t psacts;
1368 
1369 	if (file_actions == NULL || *file_actions == NULL) {
1370 		return EINVAL;
1371 	}
1372 
1373 	psacts = *(_posix_spawn_file_actions_t *)file_actions;
1374 	free(psacts);
1375 	*file_actions = NULL;
1376 
1377 	return 0;
1378 }
1379 
1380 
1381 /*
1382  * _posix_spawn_file_actions_grow
1383  *
1384  * Description:	Grow the available list of file actions associated with the
1385  *		pointer to the structure provided; replace the contents of the
1386  *		pointer as a side effect.
1387  *
1388  * Parameters:	psactsp			Pointer to _posix_spawn_file_actions_t
1389  *					to grow
1390  *
1391  * Returns:	0			Success
1392  *		ENOMEM			Insufficient memory for operation
1393  *
1394  * Notes:	This code is common to all posix_spawn_file_actions_*()
1395  *		functions, since we use a naieve data structure implementation
1396  *		at present.  Future optimization will likely change this.
1397  */
1398 static int
_posix_spawn_file_actions_grow(_posix_spawn_file_actions_t * psactsp)1399 _posix_spawn_file_actions_grow(_posix_spawn_file_actions_t *psactsp)
1400 {
1401 	int newnum = 0;
1402 	if (os_mul_overflow((*psactsp)->psfa_act_alloc, 2, &newnum)) {
1403 		return ENOMEM;
1404 	}
1405 
1406 	size_t newsize = PSF_ACTIONS_SIZE(newnum);
1407 	if (newsize == 0) {
1408 		return ENOMEM;
1409 	}
1410 
1411 	/*
1412 	 * XXX may want to impose an administrative limit here; POSIX does
1413 	 * XXX not provide for an administrative error return in this case,
1414 	 * XXX so it's probably acceptable to just fail catastrophically
1415 	 * XXX instead of implementing one.
1416 	 */
1417 	_posix_spawn_file_actions_t new_psacts;
1418 	if ((new_psacts = (_posix_spawn_file_actions_t)realloc((*psactsp), newsize)) == NULL) {
1419 		return ENOMEM;
1420 	}
1421 	new_psacts->psfa_act_alloc = newnum;
1422 	*psactsp = new_psacts;
1423 
1424 	return 0;
1425 }
1426 
1427 
1428 /*
1429  * posix_spawn_file_actions_addopen
1430  *
1431  * Description:	Add an open action to the object referenced by 'file_actions'
1432  *		that will cause the file named by 'path' to be attempted to be
1433  *		opened with flags 'oflag' and mode 'mode', and, if successful,
1434  *		return as descriptor 'filedes' to the spawned process.
1435  *
1436  * Parameters:	file_actions		File action object to augment
1437  *		filedes			fd that open is to use
1438  *		path			path to file to open
1439  *		oflag			open file flags
1440  *		mode			open file mode
1441  *
1442  * Returns:	0			Success
1443  *		EBADF			The value specified by fildes is
1444  *					negative or greater than or equal to
1445  *					{OPEN_MAX}.
1446  *		ENOMEM			Insufficient memory exists to add to
1447  *					the spawn file actions object.
1448  *
1449  * NOTIMP:	Allowed failures (checking NOT required):
1450  *		EINVAL	The value specified by file_actions is invalid.
1451  */
1452 int
posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict file_actions,int filedes,const char * __restrict path,int oflag,mode_t mode)1453 posix_spawn_file_actions_addopen(
1454 	posix_spawn_file_actions_t * __restrict file_actions,
1455 	int filedes, const char * __restrict path, int oflag,
1456 	mode_t mode)
1457 {
1458 	_posix_spawn_file_actions_t *psactsp;
1459 	_psfa_action_t *psfileact;
1460 
1461 	if (file_actions == NULL || *file_actions == NULL) {
1462 		return EINVAL;
1463 	}
1464 
1465 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1466 	/* Range check; required by POSIX */
1467 	if (filedes < 0 || filedes >= OPEN_MAX) {
1468 		return EBADF;
1469 	}
1470 
1471 	/* If we do not have enough slots, grow the structure */
1472 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1473 		/* need to grow file actions structure */
1474 		if (_posix_spawn_file_actions_grow(psactsp)) {
1475 			return ENOMEM;
1476 		}
1477 	}
1478 
1479 	/*
1480 	 * Allocate next available slot and fill it out
1481 	 */
1482 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1483 
1484 	psfileact->psfaa_type = PSFA_OPEN;
1485 	psfileact->psfaa_filedes = filedes;
1486 	psfileact->psfaa_openargs.psfao_oflag = oflag;
1487 	psfileact->psfaa_openargs.psfao_mode = mode;
1488 	strlcpy(psfileact->psfaa_openargs.psfao_path, path, PATH_MAX);
1489 
1490 	return 0;
1491 }
1492 
1493 
1494 /*
1495  * posix_spawn_file_actions_addclose
1496  *
1497  * Description:	Add a close action to the object referenced by 'file_actions'
1498  *		that will cause the file referenced by 'filedes' to be
1499  *		attempted to be closed in the spawned process.
1500  *
1501  * Parameters:	file_actions		File action object to augment
1502  *		filedes			fd to close
1503  *
1504  * Returns:	0			Success
1505  *		EBADF			The value specified by fildes is
1506  *					negative or greater than or equal to
1507  *					{OPEN_MAX}.
1508  *		ENOMEM			Insufficient memory exists to add to
1509  *					the spawn file actions object.
1510  *
1511  * NOTIMP:	Allowed failures (checking NOT required):
1512  *		EINVAL	The value specified by file_actions is invalid.
1513  */
1514 int
posix_spawn_file_actions_addclose(posix_spawn_file_actions_t * file_actions,int filedes)1515 posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *file_actions,
1516     int filedes)
1517 {
1518 	_posix_spawn_file_actions_t *psactsp;
1519 	_psfa_action_t *psfileact;
1520 
1521 	if (file_actions == NULL || *file_actions == NULL) {
1522 		return EINVAL;
1523 	}
1524 
1525 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1526 	/* Range check; required by POSIX */
1527 	if (filedes < 0 || filedes >= OPEN_MAX) {
1528 		return EBADF;
1529 	}
1530 
1531 	/* If we do not have enough slots, grow the structure */
1532 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1533 		/* need to grow file actions structure */
1534 		if (_posix_spawn_file_actions_grow(psactsp)) {
1535 			return ENOMEM;
1536 		}
1537 	}
1538 
1539 	/*
1540 	 * Allocate next available slot and fill it out
1541 	 */
1542 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1543 
1544 	psfileact->psfaa_type = PSFA_CLOSE;
1545 	psfileact->psfaa_filedes = filedes;
1546 
1547 	return 0;
1548 }
1549 
1550 
1551 /*
1552  * posix_spawn_file_actions_adddup2
1553  *
1554  * Description:	Add a dup2 action to the object referenced by 'file_actions'
1555  *		that will cause the file referenced by 'filedes' to be
1556  *		attempted to be dup2'ed to the descriptor 'newfiledes' in the
1557  *		spawned process.
1558  *
1559  * Parameters:	file_actions		File action object to augment
1560  *		filedes			fd to dup2
1561  *		newfiledes		fd to dup2 it to
1562  *
1563  * Returns:	0			Success
1564  *		EBADF			The value specified by either fildes
1565  *					or by newfiledes is negative or greater
1566  *					than or equal to {OPEN_MAX}.
1567  *		ENOMEM			Insufficient memory exists to add to
1568  *					the spawn file actions object.
1569  *
1570  * NOTIMP:	Allowed failures (checking NOT required):
1571  *		EINVAL	The value specified by file_actions is invalid.
1572  */
1573 int
posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t * file_actions,int filedes,int newfiledes)1574 posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *file_actions,
1575     int filedes, int newfiledes)
1576 {
1577 	_posix_spawn_file_actions_t *psactsp;
1578 	_psfa_action_t *psfileact;
1579 
1580 	if (file_actions == NULL || *file_actions == NULL) {
1581 		return EINVAL;
1582 	}
1583 
1584 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1585 	/* Range check; required by POSIX */
1586 	if (filedes < 0 || filedes >= OPEN_MAX ||
1587 	    newfiledes < 0 || newfiledes >= OPEN_MAX) {
1588 		return EBADF;
1589 	}
1590 
1591 	/* If we do not have enough slots, grow the structure */
1592 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1593 		/* need to grow file actions structure */
1594 		if (_posix_spawn_file_actions_grow(psactsp)) {
1595 			return ENOMEM;
1596 		}
1597 	}
1598 
1599 	/*
1600 	 * Allocate next available slot and fill it out
1601 	 */
1602 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1603 
1604 	psfileact->psfaa_type = PSFA_DUP2;
1605 	psfileact->psfaa_filedes = filedes;
1606 	psfileact->psfaa_dup2args.psfad_newfiledes = newfiledes;
1607 
1608 	return 0;
1609 }
1610 
1611 /*
1612  * posix_spawn_file_actions_add_fileportdup2_np
1613  *
1614  * Description:	Add a dup2 action to the object referenced by 'file_actions'
1615  *		that will cause the file referenced by 'fileport' to be
1616  *		attempted to be dup2'ed to the descriptor 'newfiledes' in the
1617  *		spawned process.
1618  *
1619  * Parameters:	file_actions		File action object to augment
1620  *		filedes			fileport to dup2
1621  *		newfiledes		fd to dup2 it to
1622  *
1623  * Returns:	0			Success
1624  *		EBADF			fileport isn't a valid port, or the
1625  *					value specified by newfiledes is
1626  *					negative or greater than or equal to
1627  *					{OPEN_MAX}.
1628  *		ENOMEM			Insufficient memory exists to add to
1629  *					the spawn file actions object.
1630  *
1631  * NOTIMP:	Allowed failures (checking NOT required):
1632  *		EINVAL	The value specified by file_actions is invalid.
1633  */
1634 int
posix_spawn_file_actions_add_fileportdup2_np(posix_spawn_file_actions_t * file_actions,mach_port_t fileport,int newfiledes)1635 posix_spawn_file_actions_add_fileportdup2_np(
1636 	posix_spawn_file_actions_t *file_actions,
1637 	mach_port_t fileport, int newfiledes)
1638 {
1639 	_posix_spawn_file_actions_t *psactsp;
1640 	_psfa_action_t *psfileact;
1641 
1642 	if (file_actions == NULL || *file_actions == NULL) {
1643 		return EINVAL;
1644 	}
1645 
1646 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1647 	/* Range check; required by POSIX */
1648 	if (!MACH_PORT_VALID(fileport) ||
1649 	    newfiledes < 0 || newfiledes >= OPEN_MAX) {
1650 		return EBADF;
1651 	}
1652 
1653 	/* If we do not have enough slots, grow the structure */
1654 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1655 		/* need to grow file actions structure */
1656 		if (_posix_spawn_file_actions_grow(psactsp)) {
1657 			return ENOMEM;
1658 		}
1659 	}
1660 
1661 	/*
1662 	 * Allocate next available slot and fill it out
1663 	 */
1664 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1665 
1666 	psfileact->psfaa_type = PSFA_FILEPORT_DUP2;
1667 	psfileact->psfaa_fileport = fileport;
1668 	psfileact->psfaa_dup2args.psfad_newfiledes = newfiledes;
1669 
1670 	return 0;
1671 }
1672 
1673 /*
1674  * posix_spawn_file_actions_addinherit_np
1675  *
1676  * Description:	Add the "inherit" action to the object referenced by
1677  *		'file_actions' that will cause the file referenced by
1678  *		'filedes' to continue to be available in the spawned
1679  *		process via the same descriptor.
1680  *
1681  *		Inheritance is the normal default behaviour for
1682  *		file descriptors across exec and spawn; but if the
1683  *		POSIX_SPAWN_CLOEXEC_DEFAULT flag is set, the usual
1684  *		default is reversed for the purposes of the spawn
1685  *		invocation.  Any pre-existing descriptors that
1686  *		need to be made available to the spawned process can
1687  *		be marked explicitly as 'inherit' via this interface.
1688  *		Otherwise they will be automatically closed.
1689  *
1690  *		Note that any descriptors created via the other file
1691  *		actions interfaces are automatically marked as 'inherit'.
1692  *
1693  * Parameters:	file_actions		File action object to augment
1694  *		filedes			fd to inherit.
1695  *
1696  * Returns:	0			Success
1697  *		EBADF			The value specified by fildes is
1698  *					negative or greater than or equal to
1699  *					{OPEN_MAX}.
1700  *		ENOMEM			Insufficient memory exists to add to
1701  *					the spawn file actions object.
1702  *
1703  * NOTIMP:	Allowed failures (checking NOT required):
1704  *		EINVAL	The value specified by file_actions is invalid.
1705  */
1706 int
posix_spawn_file_actions_addinherit_np(posix_spawn_file_actions_t * file_actions,int filedes)1707 posix_spawn_file_actions_addinherit_np(posix_spawn_file_actions_t *file_actions,
1708     int filedes)
1709 {
1710 	_posix_spawn_file_actions_t *psactsp;
1711 	_psfa_action_t *psfileact;
1712 
1713 	if (file_actions == NULL || *file_actions == NULL) {
1714 		return EINVAL;
1715 	}
1716 
1717 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1718 	/* Range check; required by POSIX */
1719 	if (filedes < 0 || filedes >= OPEN_MAX) {
1720 		return EBADF;
1721 	}
1722 
1723 #if defined(POSIX_SPAWN_CLOEXEC_DEFAULT)        // TODO: delete this check
1724 	/* If we do not have enough slots, grow the structure */
1725 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1726 		/* need to grow file actions structure */
1727 		if (_posix_spawn_file_actions_grow(psactsp)) {
1728 			return ENOMEM;
1729 		}
1730 	}
1731 
1732 	/*
1733 	 * Allocate next available slot and fill it out
1734 	 */
1735 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1736 
1737 	psfileact->psfaa_type = PSFA_INHERIT;
1738 	psfileact->psfaa_filedes = filedes;
1739 #endif
1740 	return 0;
1741 }
1742 
1743 
1744 /*
1745  * posix_spawn_file_actions_addchdir_np
1746  *
1747  * Description:	Add a chdir action to the object referenced by 'file_actions'
1748  *		that will cause the current working directory to attempt to be changed
1749  *      to that referenced by 'path' in the spawned process.
1750  *
1751  * Parameters:	file_actions		File action object to augment
1752  *		path			path of the desired working directory
1753  *
1754  * Returns:	0			Success
1755  *		ENOMEM			Insufficient memory exists to add to
1756  *					the spawn file actions object.
1757  *		ENAMETOOLONG	The supplied path exceeded PATH_MAX.
1758  *
1759  * NOTIMP:	Allowed failures (checking NOT required):
1760  *		EINVAL	The value specified by file_actions is invalid.
1761  */
1762 int
posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t * __restrict file_actions,const char * __restrict path)1763 posix_spawn_file_actions_addchdir_np(
1764 	posix_spawn_file_actions_t * __restrict file_actions,
1765 	const char * __restrict path)
1766 {
1767 	_posix_spawn_file_actions_t *psactsp;
1768 	_psfa_action_t *psfileact;
1769 
1770 	if (file_actions == NULL || *file_actions == NULL) {
1771 		return EINVAL;
1772 	}
1773 
1774 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1775 
1776 	/* If we do not have enough slots, grow the structure */
1777 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1778 		/* need to grow file actions structure */
1779 		if (_posix_spawn_file_actions_grow(psactsp)) {
1780 			return ENOMEM;
1781 		}
1782 	}
1783 
1784 	/*
1785 	 * Allocate next available slot and fill it out
1786 	 */
1787 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1788 
1789 	psfileact->psfaa_type = PSFA_CHDIR;
1790 	if (strlcpy(psfileact->psfaa_chdirargs.psfac_path, path, PATH_MAX) >= PATH_MAX) {
1791 		(*psactsp)->psfa_act_count--;
1792 		return ENAMETOOLONG;
1793 	}
1794 
1795 	return 0;
1796 }
1797 
1798 
1799 /*
1800  * posix_spawn_file_actions_fchdir_np
1801  *
1802  * Description:	Add a fchdir action to the object referenced by 'file_actions'
1803  *		that will cause the current working directory to attempt to be changed
1804  *      to that referenced by the descriptor 'filedes' in the spawned process.
1805  *
1806  * Parameters:	file_actions		File action object to augment
1807  *		filedes			fd to chdir to
1808  *
1809  * Returns:	0			Success
1810  *		EBADF			The value specified by either fildes is negative or
1811  *                              greater than or equal to {OPEN_MAX}.
1812  *		ENOMEM			Insufficient memory exists to add to
1813  *					the spawn file actions object.
1814  *
1815  * NOTIMP:	Allowed failures (checking NOT required):
1816  *		EINVAL	The value specified by file_actions is invalid.
1817  */
1818 int
posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t * file_actions,int filedes)1819 posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t *file_actions,
1820     int filedes)
1821 {
1822 	_posix_spawn_file_actions_t *psactsp;
1823 	_psfa_action_t *psfileact;
1824 
1825 	if (file_actions == NULL || *file_actions == NULL) {
1826 		return EINVAL;
1827 	}
1828 
1829 	psactsp = (_posix_spawn_file_actions_t *)file_actions;
1830 	/* Range check; in spirit of POSIX */
1831 	if (filedes < 0 || filedes >= OPEN_MAX) {
1832 		return EBADF;
1833 	}
1834 
1835 	/* If we do not have enough slots, grow the structure */
1836 	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
1837 		/* need to grow file actions structure */
1838 		if (_posix_spawn_file_actions_grow(psactsp)) {
1839 			return ENOMEM;
1840 		}
1841 	}
1842 
1843 	/*
1844 	 * Allocate next available slot and fill it out
1845 	 */
1846 	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
1847 
1848 	psfileact->psfaa_type = PSFA_FCHDIR;
1849 	psfileact->psfaa_filedes = filedes;
1850 
1851 	return 0;
1852 }
1853 
1854 int
posix_spawnattr_setcpumonitor_default(posix_spawnattr_t * __restrict attr)1855 posix_spawnattr_setcpumonitor_default(posix_spawnattr_t * __restrict attr)
1856 {
1857 	return posix_spawnattr_setcpumonitor(attr, PROC_POLICY_CPUMON_DEFAULTS, 0);
1858 }
1859 
1860 int
posix_spawnattr_setcpumonitor(posix_spawnattr_t * __restrict attr,uint64_t percent,uint64_t interval)1861 posix_spawnattr_setcpumonitor(posix_spawnattr_t * __restrict attr,
1862     uint64_t percent, uint64_t interval)
1863 {
1864 	_posix_spawnattr_t psattr;
1865 
1866 	if (attr == NULL || *attr == NULL) {
1867 		return EINVAL;
1868 	}
1869 
1870 	psattr = *(_posix_spawnattr_t *)attr;
1871 
1872 	psattr->psa_cpumonitor_percent = percent;
1873 	psattr->psa_cpumonitor_interval = interval;
1874 
1875 	return 0;
1876 }
1877 
1878 int
posix_spawnattr_getcpumonitor(posix_spawnattr_t * __restrict attr,uint64_t * percent,uint64_t * interval)1879 posix_spawnattr_getcpumonitor(posix_spawnattr_t * __restrict attr,
1880     uint64_t *percent, uint64_t *interval)
1881 {
1882 	_posix_spawnattr_t psattr;
1883 
1884 	if (attr == NULL || *attr == NULL) {
1885 		return EINVAL;
1886 	}
1887 
1888 	psattr = *(_posix_spawnattr_t *)attr;
1889 
1890 	*percent = psattr->psa_cpumonitor_percent;
1891 	*interval = psattr->psa_cpumonitor_interval;
1892 
1893 	return 0;
1894 }
1895 
1896 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
1897 /*
1898  * posix_spawnattr_setjetsam
1899  *
1900  * Description:	Set jetsam attributes for the spawn attribute object
1901  *		referred to by 'attr'.
1902  *
1903  * Parameters:	flags			The flags value to set
1904  *		priority		Relative jetsam priority
1905  *		memlimit		Value in megabytes; a memory footprint
1906  *					above this level may result in termination.
1907  *					Implies both active and inactive limits.
1908  *
1909  * Returns:	0			Success
1910  *
1911  * Note: to be deprecated (not available on desktop)
1912  *
1913  */
1914 int
posix_spawnattr_setjetsam(posix_spawnattr_t * __restrict attr,short flags,int priority,int memlimit)1915 posix_spawnattr_setjetsam(posix_spawnattr_t * __restrict attr,
1916     short flags, int priority, int memlimit)
1917 {
1918 	short flags_ext = flags;
1919 
1920 	if (flags & POSIX_SPAWN_JETSAM_MEMLIMIT_FATAL) {
1921 		flags_ext |= POSIX_SPAWN_JETSAM_MEMLIMIT_ACTIVE_FATAL;
1922 		flags_ext |= POSIX_SPAWN_JETSAM_MEMLIMIT_INACTIVE_FATAL;
1923 	} else {
1924 		flags_ext &= ~POSIX_SPAWN_JETSAM_MEMLIMIT_ACTIVE_FATAL;
1925 		flags_ext &= ~POSIX_SPAWN_JETSAM_MEMLIMIT_INACTIVE_FATAL;
1926 	}
1927 
1928 	return posix_spawnattr_setjetsam_ext(attr, flags_ext, priority, memlimit, memlimit);
1929 }
1930 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
1931 
1932 /*
1933  * posix_spawnattr_setjetsam_ext
1934  *
1935  * Description:	Set jetsam attributes for the spawn attribute object
1936  *		referred to by 'attr'.
1937  *
1938  * Parameters:	flags			The flags value to set
1939  *		priority		Relative jetsam priority
1940  *		memlimit_active		Value in megabytes; memory footprint
1941  *					above this level while process is
1942  *					active may result in termination.
1943  *		memlimit_inactive	Value in megabytes; memory footprint
1944  *					above this level while process is
1945  *					inactive may result in termination.
1946  *
1947  * Returns:	0			Success
1948  */
1949 int
posix_spawnattr_setjetsam_ext(posix_spawnattr_t * __restrict attr,short flags,int priority,int memlimit_active,int memlimit_inactive)1950 posix_spawnattr_setjetsam_ext(posix_spawnattr_t * __restrict attr,
1951     short flags, int priority, int memlimit_active, int memlimit_inactive)
1952 {
1953 	_posix_spawnattr_t psattr;
1954 
1955 	if (attr == NULL || *attr == NULL) {
1956 		return EINVAL;
1957 	}
1958 
1959 	psattr = *(_posix_spawnattr_t *)attr;
1960 
1961 	psattr->psa_jetsam_flags = flags;
1962 	psattr->psa_jetsam_flags |= POSIX_SPAWN_JETSAM_SET;
1963 	psattr->psa_priority = priority;
1964 	psattr->psa_memlimit_active = memlimit_active;
1965 	psattr->psa_memlimit_inactive = memlimit_inactive;
1966 
1967 	return 0;
1968 }
1969 
1970 int
posix_spawnattr_set_threadlimit_ext(posix_spawnattr_t * __restrict attr,int thread_limit)1971 posix_spawnattr_set_threadlimit_ext(posix_spawnattr_t * __restrict attr,
1972     int thread_limit)
1973 {
1974 	_posix_spawnattr_t psattr;
1975 
1976 	if (attr == NULL || *attr == NULL) {
1977 		return EINVAL;
1978 	}
1979 
1980 	psattr = *(_posix_spawnattr_t *)attr;
1981 
1982 	psattr->psa_thread_limit = thread_limit;
1983 
1984 	return 0;
1985 }
1986 
1987 
1988 /*
1989  * posix_spawnattr_set_importancewatch_port_np
1990  *
1991  * Description:	Mark ports referred to by these rights
1992  *              to boost the new task instead of their current task
1993  *              for the spawn attribute object referred to by 'attr'.
1994  *              Ports must be valid at posix_spawn time.  They will NOT be
1995  *              consumed by the kernel, so they must be deallocated after the spawn returns.
1996  *              (If you are SETEXEC-ing, they are cleaned up by the exec operation).
1997  *
1998  *              The maximum number of watch ports allowed is defined by POSIX_SPAWN_IMPORTANCE_PORT_COUNT.
1999  *
2000  * Parameters:	count           Number of ports in portarray
2001  *              portarray       Array of rights
2002  *
2003  * Returns:     0       Success
2004  *              EINVAL  Bad port count
2005  *              ENOMEM  Insufficient memory exists to add to
2006  *                      the spawn port actions object.
2007  */
2008 int
posix_spawnattr_set_importancewatch_port_np(posix_spawnattr_t * __restrict attr,int count,mach_port_t portarray[])2009 posix_spawnattr_set_importancewatch_port_np(posix_spawnattr_t * __restrict attr,
2010     int count, mach_port_t portarray[])
2011 {
2012 	int err = 0, i;
2013 
2014 	if (count < 0 || count > POSIX_SPAWN_IMPORTANCE_PORT_COUNT) {
2015 		return EINVAL;
2016 	}
2017 
2018 	for (i = 0; i < count; i++) {
2019 		_ps_port_action_t action = {
2020 			.port_type = PSPA_IMP_WATCHPORTS,
2021 			.new_port = portarray[i],
2022 		};
2023 		err = posix_spawn_appendportaction_np(attr, &action);
2024 		if (err) {
2025 			break;
2026 		}
2027 	}
2028 	return err;
2029 }
2030 
2031 int
posix_spawnattr_set_registered_ports_np(posix_spawnattr_t * __restrict attr,mach_port_t portarray[],uint32_t count)2032 posix_spawnattr_set_registered_ports_np(posix_spawnattr_t * __restrict attr,
2033     mach_port_t portarray[], uint32_t count)
2034 {
2035 	int err = 0;
2036 
2037 	if (count > TASK_PORT_REGISTER_MAX) {
2038 		return EINVAL;
2039 	}
2040 
2041 	for (uint32_t i = 0; i < count; i++) {
2042 		_ps_port_action_t action = {
2043 			.port_type = PSPA_REGISTERED_PORTS,
2044 			.new_port = portarray[i],
2045 		};
2046 		err = posix_spawn_appendportaction_np(attr, &action);
2047 		if (err) {
2048 			break;
2049 		}
2050 	}
2051 	return err;
2052 }
2053 
2054 int
posix_spawnattr_set_ptrauth_task_port_np(posix_spawnattr_t * __restrict attr,mach_port_t port)2055 posix_spawnattr_set_ptrauth_task_port_np(posix_spawnattr_t * __restrict attr,
2056     mach_port_t port)
2057 {
2058 	int err = 0;
2059 
2060 	_ps_port_action_t action = {
2061 		.port_type = PSPA_PTRAUTH_TASK_PORT,
2062 		.new_port = port,
2063 	};
2064 
2065 	err = posix_spawn_appendportaction_np(attr, &action);
2066 	return err;
2067 }
2068 
2069 static
2070 _ps_mac_policy_extension_t *
posix_spawnattr_macpolicyinfo_lookup(_posix_spawn_mac_policy_extensions_t psmx,const char * policyname)2071 posix_spawnattr_macpolicyinfo_lookup(_posix_spawn_mac_policy_extensions_t psmx, const char *policyname)
2072 {
2073 	int i;
2074 
2075 	if (psmx == NULL) {
2076 		return NULL;
2077 	}
2078 
2079 	for (i = 0; i < psmx->psmx_count; i++) {
2080 		_ps_mac_policy_extension_t *extension = &psmx->psmx_extensions[i];
2081 		if (strcmp(extension->policyname, policyname) == 0) {
2082 			return extension;
2083 		}
2084 	}
2085 	return NULL;
2086 }
2087 
2088 int
posix_spawnattr_getmacpolicyinfo_np(const posix_spawnattr_t * __restrict attr,const char * policyname,void ** datap,size_t * datalenp)2089 posix_spawnattr_getmacpolicyinfo_np(const posix_spawnattr_t * __restrict attr,
2090     const char *policyname, void **datap, size_t *datalenp)
2091 {
2092 	_posix_spawnattr_t psattr;
2093 	_ps_mac_policy_extension_t *extension;
2094 
2095 	if (attr == NULL || *attr == NULL || policyname == NULL || datap == NULL) {
2096 		return EINVAL;
2097 	}
2098 
2099 	psattr = *(_posix_spawnattr_t *)attr;
2100 	extension = posix_spawnattr_macpolicyinfo_lookup(psattr->psa_mac_extensions, policyname);
2101 	if (extension == NULL) {
2102 		return ESRCH;
2103 	}
2104 	*datap = (void *)(uintptr_t)extension->data;
2105 	if (datalenp != NULL) {
2106 		*datalenp = (size_t)extension->datalen;
2107 	}
2108 	return 0;
2109 }
2110 
2111 int
posix_spawnattr_setmacpolicyinfo_np(posix_spawnattr_t * __restrict attr,const char * policyname,void * data,size_t datalen)2112 posix_spawnattr_setmacpolicyinfo_np(posix_spawnattr_t * __restrict attr,
2113     const char *policyname, void *data, size_t datalen)
2114 {
2115 	_posix_spawnattr_t psattr;
2116 	_posix_spawn_mac_policy_extensions_t psmx;
2117 	_ps_mac_policy_extension_t *extension;
2118 
2119 	if (attr == NULL || *attr == NULL || policyname == NULL) {
2120 		return EINVAL;
2121 	}
2122 
2123 	psattr = *(_posix_spawnattr_t *)attr;
2124 	psmx = psattr->psa_mac_extensions;
2125 	extension = posix_spawnattr_macpolicyinfo_lookup(psattr->psa_mac_extensions, policyname);
2126 	if (extension != NULL) {
2127 		extension->data = (uintptr_t)data;
2128 		extension->datalen = datalen;
2129 		return 0;
2130 	} else if (psmx == NULL) {
2131 		psmx = psattr->psa_mac_extensions = malloc(PS_MAC_EXTENSIONS_SIZE(PS_MAC_EXTENSIONS_INIT_COUNT));
2132 		if (psmx == NULL) {
2133 			return ENOMEM;
2134 		}
2135 		psmx->psmx_alloc = PS_MAC_EXTENSIONS_INIT_COUNT;
2136 		psmx->psmx_count = 0;
2137 	} else if (psmx->psmx_count == psmx->psmx_alloc) {
2138 		int newnum = 0;
2139 		if (os_mul_overflow(psmx->psmx_alloc, 2, &newnum)) {
2140 			return ENOMEM;
2141 		}
2142 		size_t extsize = PS_MAC_EXTENSIONS_SIZE(newnum);
2143 		if (extsize == 0) {
2144 			return ENOMEM;
2145 		}
2146 		psmx = psattr->psa_mac_extensions = reallocf(psmx, extsize);
2147 		if (psmx == NULL) {
2148 			return ENOMEM;
2149 		}
2150 		psmx->psmx_alloc = newnum;
2151 	}
2152 	extension = &psmx->psmx_extensions[psmx->psmx_count];
2153 	strlcpy(extension->policyname, policyname, sizeof(extension->policyname));
2154 	extension->data = (uintptr_t)data;
2155 	extension->datalen = datalen;
2156 	psmx->psmx_count += 1;
2157 	return 0;
2158 }
2159 
2160 /*
2161  * posix_spawn_destroymacpolicy_info_np
2162  * Description: cleanup the macpolicy struct in posix_spawnattr_t attr
2163  */
2164 static int
posix_spawn_destroymacpolicy_info_np(posix_spawnattr_t * attr)2165 posix_spawn_destroymacpolicy_info_np(posix_spawnattr_t *attr)
2166 {
2167 	_posix_spawnattr_t psattr;
2168 	_posix_spawn_mac_policy_extensions_t psmx;
2169 
2170 	if (attr == NULL || *attr == NULL) {
2171 		return EINVAL;
2172 	}
2173 
2174 	psattr = *(_posix_spawnattr_t *)attr;
2175 	psmx = psattr->psa_mac_extensions;
2176 	if (psmx == NULL) {
2177 		return EINVAL;
2178 	}
2179 
2180 	psattr->psa_mac_extensions = NULL;
2181 	free(psmx);
2182 	return 0;
2183 }
2184 
2185 int
posix_spawnattr_setcoalition_np(const posix_spawnattr_t * __restrict attr,uint64_t coalitionid,int type,int role)2186 posix_spawnattr_setcoalition_np(const posix_spawnattr_t * __restrict attr,
2187     uint64_t coalitionid, int type, int role)
2188 {
2189 	_posix_spawnattr_t psattr;
2190 	struct _posix_spawn_coalition_info *coal_info;
2191 
2192 	if (attr == NULL || *attr == NULL) {
2193 		return EINVAL;
2194 	}
2195 	if (type < 0 || type > COALITION_TYPE_MAX) {
2196 		return EINVAL;
2197 	}
2198 
2199 	psattr = *(_posix_spawnattr_t *)attr;
2200 
2201 	coal_info = psattr->psa_coalition_info;
2202 	if (!coal_info) {
2203 		coal_info = (struct _posix_spawn_coalition_info *)malloc(sizeof(*coal_info));
2204 		if (!coal_info) {
2205 			return ENOMEM;
2206 		}
2207 		memset(coal_info, 0, sizeof(*coal_info));
2208 		psattr->psa_coalition_info = coal_info;
2209 	}
2210 
2211 	coal_info->psci_info[type].psci_id   = coalitionid;
2212 	coal_info->psci_info[type].psci_role = role;
2213 
2214 	return 0;
2215 }
2216 
2217 
2218 int
posix_spawnattr_set_qos_clamp_np(const posix_spawnattr_t * __restrict attr,uint64_t qos_clamp)2219 posix_spawnattr_set_qos_clamp_np(const posix_spawnattr_t * __restrict attr, uint64_t qos_clamp)
2220 {
2221 	_posix_spawnattr_t psattr;
2222 
2223 	if (attr == NULL || *attr == NULL) {
2224 		return EINVAL;
2225 	}
2226 
2227 	if (qos_clamp >= POSIX_SPAWN_PROC_CLAMP_LAST) {
2228 		return EINVAL;
2229 	}
2230 
2231 	psattr = *(_posix_spawnattr_t *)attr;
2232 	psattr->psa_qos_clamp = qos_clamp;
2233 
2234 	return 0;
2235 }
2236 
2237 int
posix_spawnattr_get_qos_clamp_np(const posix_spawnattr_t * __restrict attr,uint64_t * __restrict qos_clampp)2238 posix_spawnattr_get_qos_clamp_np(const posix_spawnattr_t * __restrict attr, uint64_t * __restrict qos_clampp)
2239 {
2240 	_posix_spawnattr_t psattr;
2241 
2242 	if (attr == NULL || *attr == NULL) {
2243 		return EINVAL;
2244 	}
2245 
2246 	psattr = *(_posix_spawnattr_t *)attr;
2247 	*qos_clampp = psattr->psa_qos_clamp;
2248 
2249 	return 0;
2250 }
2251 
2252 int
posix_spawnattr_set_darwin_role_np(const posix_spawnattr_t * __restrict attr,uint64_t darwin_role)2253 posix_spawnattr_set_darwin_role_np(const posix_spawnattr_t * __restrict attr, uint64_t darwin_role)
2254 {
2255 	_posix_spawnattr_t psattr;
2256 
2257 	if (attr == NULL || *attr == NULL) {
2258 		return EINVAL;
2259 	}
2260 
2261 	psattr = *(_posix_spawnattr_t *)attr;
2262 	psattr->psa_darwin_role = darwin_role;
2263 
2264 	return 0;
2265 }
2266 
2267 int
posix_spawnattr_get_darwin_role_np(const posix_spawnattr_t * __restrict attr,uint64_t * __restrict darwin_rolep)2268 posix_spawnattr_get_darwin_role_np(const posix_spawnattr_t * __restrict attr, uint64_t * __restrict darwin_rolep)
2269 {
2270 	_posix_spawnattr_t psattr;
2271 
2272 	if (attr == NULL || *attr == NULL) {
2273 		return EINVAL;
2274 	}
2275 
2276 	psattr = *(_posix_spawnattr_t *)attr;
2277 	*darwin_rolep = psattr->psa_darwin_role;
2278 
2279 	return 0;
2280 }
2281 
2282 
2283 int
posix_spawnattr_set_persona_np(const posix_spawnattr_t * __restrict attr,uid_t persona_id,uint32_t flags)2284 posix_spawnattr_set_persona_np(const posix_spawnattr_t * __restrict attr, uid_t persona_id, uint32_t flags)
2285 {
2286 	_posix_spawnattr_t psattr;
2287 	struct _posix_spawn_persona_info *persona;
2288 
2289 	if (attr == NULL || *attr == NULL) {
2290 		return EINVAL;
2291 	}
2292 
2293 	if (flags & ~POSIX_SPAWN_PERSONA_ALL_FLAGS) {
2294 		return EINVAL;
2295 	}
2296 
2297 	psattr = *(_posix_spawnattr_t *)attr;
2298 
2299 	persona = psattr->psa_persona_info;
2300 	if (!persona) {
2301 		persona = (struct _posix_spawn_persona_info *)malloc(sizeof(*persona));
2302 		if (!persona) {
2303 			return ENOMEM;
2304 		}
2305 		persona->pspi_uid = 0;
2306 		persona->pspi_gid = 0;
2307 		persona->pspi_ngroups = 0;
2308 		persona->pspi_groups[0] = 0;
2309 		persona->pspi_gmuid = 0;
2310 
2311 		psattr->psa_persona_info = persona;
2312 	}
2313 
2314 	persona->pspi_id = persona_id;
2315 	persona->pspi_flags = flags;
2316 
2317 	return 0;
2318 }
2319 
2320 int
posix_spawnattr_set_persona_uid_np(const posix_spawnattr_t * __restrict attr,uid_t uid)2321 posix_spawnattr_set_persona_uid_np(const posix_spawnattr_t * __restrict attr, uid_t uid)
2322 {
2323 	_posix_spawnattr_t psattr;
2324 	struct _posix_spawn_persona_info *persona;
2325 
2326 	if (attr == NULL || *attr == NULL) {
2327 		return EINVAL;
2328 	}
2329 
2330 	psattr = *(_posix_spawnattr_t *)attr;
2331 	persona = psattr->psa_persona_info;
2332 	if (!persona) {
2333 		return EINVAL;
2334 	}
2335 
2336 	if (!(persona->pspi_flags & (POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE | POSIX_SPAWN_PERSONA_FLAGS_VERIFY))) {
2337 		return EINVAL;
2338 	}
2339 
2340 	persona->pspi_uid = uid;
2341 
2342 	persona->pspi_flags |= POSIX_SPAWN_PERSONA_UID;
2343 
2344 	return 0;
2345 }
2346 
2347 int
posix_spawnattr_set_persona_gid_np(const posix_spawnattr_t * __restrict attr,gid_t gid)2348 posix_spawnattr_set_persona_gid_np(const posix_spawnattr_t * __restrict attr, gid_t gid)
2349 {
2350 	_posix_spawnattr_t psattr;
2351 	struct _posix_spawn_persona_info *persona;
2352 
2353 	if (attr == NULL || *attr == NULL) {
2354 		return EINVAL;
2355 	}
2356 
2357 	psattr = *(_posix_spawnattr_t *)attr;
2358 	persona = psattr->psa_persona_info;
2359 	if (!persona) {
2360 		return EINVAL;
2361 	}
2362 
2363 	if (!(persona->pspi_flags & (POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE | POSIX_SPAWN_PERSONA_FLAGS_VERIFY))) {
2364 		return EINVAL;
2365 	}
2366 
2367 	persona->pspi_gid = gid;
2368 
2369 	persona->pspi_flags |= POSIX_SPAWN_PERSONA_GID;
2370 
2371 	return 0;
2372 }
2373 
2374 int
posix_spawnattr_set_persona_groups_np(const posix_spawnattr_t * __restrict attr,int ngroups,gid_t * gidarray,uid_t gmuid)2375 posix_spawnattr_set_persona_groups_np(const posix_spawnattr_t * __restrict attr, int ngroups, gid_t *gidarray, uid_t gmuid)
2376 {
2377 	_posix_spawnattr_t psattr;
2378 	struct _posix_spawn_persona_info *persona;
2379 
2380 	if (attr == NULL || *attr == NULL) {
2381 		return EINVAL;
2382 	}
2383 
2384 	if (gidarray == NULL) {
2385 		return EINVAL;
2386 	}
2387 
2388 	if (ngroups > NGROUPS || ngroups < 0) {
2389 		return EINVAL;
2390 	}
2391 
2392 	psattr = *(_posix_spawnattr_t *)attr;
2393 	persona = psattr->psa_persona_info;
2394 	if (!persona) {
2395 		return EINVAL;
2396 	}
2397 
2398 	if (!(persona->pspi_flags & (POSIX_SPAWN_PERSONA_FLAGS_OVERRIDE | POSIX_SPAWN_PERSONA_FLAGS_VERIFY))) {
2399 		return EINVAL;
2400 	}
2401 
2402 	persona->pspi_ngroups = ngroups;
2403 	for (int i = 0; i < ngroups; i++) {
2404 		persona->pspi_groups[i] = gidarray[i];
2405 	}
2406 
2407 	persona->pspi_gmuid = gmuid;
2408 
2409 	persona->pspi_flags |= POSIX_SPAWN_PERSONA_GROUPS;
2410 
2411 	return 0;
2412 }
2413 
2414 int
posix_spawnattr_set_max_addr_np(const posix_spawnattr_t * __restrict attr,uint64_t max_addr)2415 posix_spawnattr_set_max_addr_np(const posix_spawnattr_t * __restrict attr, uint64_t max_addr)
2416 {
2417 	_posix_spawnattr_t psattr;
2418 
2419 	if (attr == NULL || *attr == NULL) {
2420 		return EINVAL;
2421 	}
2422 
2423 	psattr = *(_posix_spawnattr_t *)attr;
2424 	psattr->psa_max_addr = max_addr;
2425 
2426 	return 0;
2427 }
2428 
2429 int
posix_spawnattr_setnosmt_np(const posix_spawnattr_t * __restrict attr)2430 posix_spawnattr_setnosmt_np(const posix_spawnattr_t * __restrict attr)
2431 {
2432 	_posix_spawnattr_t psattr;
2433 
2434 	if (attr == NULL || *attr == NULL) {
2435 		return EINVAL;
2436 	}
2437 
2438 	psattr = *(_posix_spawnattr_t *)attr;
2439 	psattr->psa_no_smt = true;
2440 
2441 	return 0;
2442 }
2443 
2444 int
posix_spawnattr_set_csm_np(const posix_spawnattr_t * __restrict attr,uint32_t flags)2445 posix_spawnattr_set_csm_np(const posix_spawnattr_t * __restrict attr, uint32_t flags)
2446 {
2447 	_posix_spawnattr_t psattr;
2448 
2449 	if (attr == NULL || *attr == NULL) {
2450 		return EINVAL;
2451 	}
2452 
2453 	const uint32_t mask = POSIX_SPAWN_NP_CSM_ALL | POSIX_SPAWN_NP_CSM_TECS | POSIX_SPAWN_NP_CSM_NOSMT;
2454 	if ((flags & ~mask) != 0) {
2455 		return EINVAL;
2456 	}
2457 
2458 	psattr = *(_posix_spawnattr_t *)attr;
2459 
2460 	if (flags & (POSIX_SPAWN_NP_CSM_TECS | POSIX_SPAWN_NP_CSM_ALL)) {
2461 		psattr->psa_tecs = true;
2462 	}
2463 	if (flags & (POSIX_SPAWN_NP_CSM_NOSMT | POSIX_SPAWN_NP_CSM_ALL)) {
2464 		psattr->psa_no_smt = true;
2465 	}
2466 
2467 	return 0;
2468 }
2469 
2470 static struct _posix_spawn_posix_cred_info *
_posix_spawnattr_get_posix_creds_info(_posix_spawnattr_t psattr)2471 _posix_spawnattr_get_posix_creds_info(_posix_spawnattr_t psattr)
2472 {
2473 	struct _posix_spawn_posix_cred_info *pspci = psattr->psa_posix_cred_info;
2474 
2475 	if (pspci == NULL) {
2476 		pspci = malloc(sizeof(struct _posix_spawn_posix_cred_info));
2477 		if (pspci != NULL) {
2478 			pspci->pspci_flags = 0;
2479 			pspci->pspci_uid = 0;
2480 			pspci->pspci_gid = 0;
2481 			pspci->pspci_ngroups = 0;
2482 			pspci->pspci_groups[0] = 0;
2483 			pspci->pspci_gmuid = 0;
2484 			pspci->pspci_login[0] = '\0';
2485 			psattr->psa_posix_cred_info = pspci;
2486 		}
2487 	}
2488 	return pspci;
2489 }
2490 
2491 int
posix_spawnattr_set_uid_np(const posix_spawnattr_t * attr,uid_t uid)2492 posix_spawnattr_set_uid_np(const posix_spawnattr_t *attr, uid_t uid)
2493 {
2494 	struct _posix_spawn_posix_cred_info *pspci;
2495 
2496 	if (attr == NULL || *attr == NULL) {
2497 		return EINVAL;
2498 	}
2499 
2500 	pspci = _posix_spawnattr_get_posix_creds_info(*(_posix_spawnattr_t *)attr);
2501 	if (pspci == NULL) {
2502 		return ENOMEM;
2503 	}
2504 
2505 	pspci->pspci_uid = uid;
2506 
2507 	pspci->pspci_flags |= POSIX_SPAWN_POSIX_CRED_UID;
2508 
2509 	return 0;
2510 }
2511 
2512 int
posix_spawnattr_set_gid_np(const posix_spawnattr_t * attr,gid_t gid)2513 posix_spawnattr_set_gid_np(const posix_spawnattr_t *attr, gid_t gid)
2514 {
2515 	struct _posix_spawn_posix_cred_info *pspci;
2516 
2517 	if (attr == NULL || *attr == NULL) {
2518 		return EINVAL;
2519 	}
2520 
2521 	pspci = _posix_spawnattr_get_posix_creds_info(*(_posix_spawnattr_t *)attr);
2522 	if (pspci == NULL) {
2523 		return ENOMEM;
2524 	}
2525 
2526 	pspci->pspci_gid = gid;
2527 
2528 	pspci->pspci_flags |= POSIX_SPAWN_POSIX_CRED_GID;
2529 
2530 	return 0;
2531 }
2532 
2533 int
posix_spawnattr_set_groups_np(const posix_spawnattr_t * attr,int ngroups,gid_t * gidarray,uid_t gmuid)2534 posix_spawnattr_set_groups_np(const posix_spawnattr_t *attr,
2535     int ngroups, gid_t *gidarray, uid_t gmuid)
2536 {
2537 	struct _posix_spawn_posix_cred_info *pspci;
2538 
2539 	if (attr == NULL || *attr == NULL) {
2540 		return EINVAL;
2541 	}
2542 
2543 	if (gidarray == NULL) {
2544 		return EINVAL;
2545 	}
2546 
2547 	if (ngroups > NGROUPS || ngroups < 0) {
2548 		return EINVAL;
2549 	}
2550 
2551 	pspci = _posix_spawnattr_get_posix_creds_info(*(_posix_spawnattr_t *)attr);
2552 	if (pspci == NULL) {
2553 		return ENOMEM;
2554 	}
2555 
2556 	pspci->pspci_ngroups = ngroups;
2557 	for (int i = 0; i < ngroups; i++) {
2558 		pspci->pspci_groups[i] = gidarray[i];
2559 	}
2560 
2561 	pspci->pspci_gmuid = gmuid;
2562 
2563 	pspci->pspci_flags |= POSIX_SPAWN_POSIX_CRED_GROUPS;
2564 
2565 	return 0;
2566 }
2567 
2568 int
posix_spawnattr_set_login_np(const posix_spawnattr_t * attr,const char * login)2569 posix_spawnattr_set_login_np(const posix_spawnattr_t *attr, const char *login)
2570 {
2571 	struct _posix_spawn_posix_cred_info *pspci;
2572 
2573 	if (attr == NULL || *attr == NULL) {
2574 		return EINVAL;
2575 	}
2576 
2577 	if (strlen(login) > MAXLOGNAME) {
2578 		return ERANGE;
2579 	}
2580 
2581 	pspci = _posix_spawnattr_get_posix_creds_info(*(_posix_spawnattr_t *)attr);
2582 	if (pspci == NULL) {
2583 		return ENOMEM;
2584 	}
2585 
2586 	strlcpy(pspci->pspci_login, login, sizeof(pspci->pspci_login));
2587 
2588 	pspci->pspci_flags |= POSIX_SPAWN_POSIX_CRED_LOGIN;
2589 
2590 	return 0;
2591 }
2592 
2593 int
posix_spawnattr_set_portlimits_ext(posix_spawnattr_t * __restrict attr,uint32_t port_soft_limit,uint32_t port_hard_limit)2594 posix_spawnattr_set_portlimits_ext(posix_spawnattr_t * __restrict attr,
2595     uint32_t port_soft_limit, uint32_t port_hard_limit)
2596 {
2597 	_posix_spawnattr_t psattr;
2598 
2599 	if (attr == NULL || *attr == NULL) {
2600 		return EINVAL;
2601 	}
2602 
2603 	psattr = *(_posix_spawnattr_t *)attr;
2604 
2605 	psattr->psa_port_soft_limit = port_soft_limit;
2606 	psattr->psa_port_hard_limit = port_hard_limit;
2607 
2608 	return 0;
2609 }
2610 
2611 int
posix_spawnattr_set_filedesclimit_ext(posix_spawnattr_t * __restrict attr,uint32_t filedesc_soft_limit,uint32_t filedesc_hard_limit)2612 posix_spawnattr_set_filedesclimit_ext(posix_spawnattr_t * __restrict attr,
2613     uint32_t filedesc_soft_limit, uint32_t filedesc_hard_limit)
2614 {
2615 	_posix_spawnattr_t psattr;
2616 
2617 	if (attr == NULL || *attr == NULL) {
2618 		return EINVAL;
2619 	}
2620 
2621 	psattr = *(_posix_spawnattr_t *)attr;
2622 
2623 	psattr->psa_filedesc_soft_limit = filedesc_soft_limit;
2624 	psattr->psa_filedesc_hard_limit = filedesc_hard_limit;
2625 
2626 	return 0;
2627 }
2628 
2629 /*
2630  * posix_spawnattr_set_jetsam_ttr_np
2631  *
2632  * Description: Pass data regarding recent relaunch behavior when jetsammed for the process.
2633  *              The recent history is effectively converted into a histogram and the highest
2634  *              frequency bucket defines the "type" of the process. The type is passed along
2635  *              to the jetsam code as part of psa_jetsam_flags.
2636  *
2637  * Parameters:	count           Number of entries in the ttrs_millis array
2638  *              ttrs_millis     Array of raw data for relaunch behavior
2639  *
2640  * Returns:     0       Success
2641  *              EINVAL  Bad attr pointer or empty data array
2642  */
2643 int
posix_spawnattr_set_jetsam_ttr_np(const posix_spawnattr_t * __restrict attr,uint32_t count,uint32_t * ttrs_millis)2644 posix_spawnattr_set_jetsam_ttr_np(const posix_spawnattr_t * __restrict attr, uint32_t count, uint32_t *ttrs_millis)
2645 {
2646 	_posix_spawnattr_t psattr;
2647 
2648 	/*
2649 	 * Define the bucketizing policy which would be used to generate the histogram. These
2650 	 * values are based on looking at data from various Avg. Joanna runs.
2651 	 */
2652 	static const uint32_t relaunch_buckets_msecs[POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_BUCKETS] = {
2653 		5000,
2654 		10000,
2655 		UINT32_MAX
2656 	};
2657 	static const uint32_t relaunch_jetsam_flags[POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_BUCKETS] = {
2658 		POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_HIGH,
2659 		POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MED,
2660 		POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_LOW
2661 	};
2662 
2663 	/* Make sure the attr pointer is valid */
2664 	if (attr == NULL || *attr == NULL) {
2665 		return EINVAL;
2666 	}
2667 
2668 	/* Make sure the count of entries is non-zero */
2669 	if (count == 0) {
2670 		return EINVAL;
2671 	}
2672 
2673 	psattr = *(_posix_spawnattr_t *)attr;
2674 
2675 	/* Generate a histogram based on the relaunch data while maintaining highest frequency bucket info */
2676 	int relaunch_histogram[POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_BUCKETS] = {0};
2677 	int max_frequency = -1;
2678 	int highest_frequency_bucket = -1;
2679 
2680 	for (uint32_t i = 0; i < count; i++) {
2681 		/* For each data point passed in via launchd, find the bucket it lands in */
2682 		for (uint32_t bucket = 0; bucket < POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_BUCKETS; bucket++) {
2683 			if (ttrs_millis[i] <= relaunch_buckets_msecs[bucket]) {
2684 				relaunch_histogram[bucket]++;
2685 
2686 				/* Check if the bucket is the highest frequency bucket now */
2687 				if (relaunch_histogram[bucket] > max_frequency) {
2688 					max_frequency = relaunch_histogram[bucket];
2689 					highest_frequency_bucket = bucket;
2690 				}
2691 				break;
2692 			}
2693 		}
2694 	}
2695 	psattr->psa_jetsam_flags |= relaunch_jetsam_flags[highest_frequency_bucket];
2696 	return 0;
2697 }
2698 
2699 /*
2700  * posix_spawn
2701  *
2702  * Description:	Create a new process from the process image corresponding to
2703  *		the supplied 'path' argument.
2704  *
2705  * Parameters:	pid				Pointer to pid_t to receive the
2706  *						PID of the spawned process, if
2707  *						successful and 'pid' != NULL
2708  *		path				Path of image file to spawn
2709  *		file_actions			spawn file actions object which
2710  *						describes file actions to be
2711  *						performed during the spawn
2712  *		attrp				spawn attributes object which
2713  *						describes attributes to be
2714  *						applied during the spawn
2715  *		argv				argument vector array; NULL
2716  *						terminated
2717  *		envp				environment vector array; NULL
2718  *						terminated
2719  *
2720  * Returns:	0				Success
2721  *		!0				An errno value indicating the
2722  *						cause of the failure to spawn
2723  *
2724  * Notes:	Unlike other system calls, the return value of this system
2725  *		call is expected to either be a 0 or an errno, rather than a
2726  *		0 or a -1, with the 'errno' variable being set.
2727  */
2728 int
posix_spawn(pid_t * __restrict pid,const char * __restrict path,const posix_spawn_file_actions_t * file_actions,const posix_spawnattr_t * __restrict attrp,char * const argv[__restrict],char * const envp[__restrict])2729 posix_spawn(pid_t * __restrict pid, const char * __restrict path,
2730     const posix_spawn_file_actions_t *file_actions,
2731     const posix_spawnattr_t * __restrict attrp,
2732     char *const argv[__restrict], char *const envp[__restrict])
2733 {
2734 	int saveerrno = errno;
2735 	int ret = 0;
2736 	struct _posix_spawn_args_desc ad;
2737 	struct _posix_spawn_args_desc *adp = NULL;
2738 	/*
2739 	 * Only do extra work if we have file actions or attributes to push
2740 	 * down.  We use a descriptor to push this information down, since we
2741 	 * want to have size information, which will let us (1) preallocate a
2742 	 * single chunk of memory for the copyin(), and (2) allow us to do a
2743 	 * single copyin() per attributes or file actions as a monlithic block.
2744 	 *
2745 	 * Note:	A future implementation may attempt to do the same
2746 	 *		thing for the argv/envp data, which could potentially
2747 	 *		result in a performance improvement due to increased
2748 	 *		kernel efficiency, even though it would mean copying
2749 	 *		the data in user space.
2750 	 */
2751 	if ((file_actions != NULL && (*file_actions != NULL) && (*(_posix_spawn_file_actions_t *)file_actions)->psfa_act_count > 0) || attrp != NULL) {
2752 		memset(&ad, 0, sizeof(ad));
2753 		adp = &ad;
2754 		if (attrp != NULL && *attrp != NULL) {
2755 			_posix_spawnattr_t psattr = *(_posix_spawnattr_t *)attrp;
2756 			ad.attr_size = sizeof(struct _posix_spawnattr);
2757 			ad.attrp = psattr;
2758 
2759 			if (psattr->psa_ports != NULL) {
2760 				size_t psact_size = PS_PORT_ACTIONS_SIZE(psattr->psa_ports->pspa_count);
2761 				if (psact_size == 0 && psattr->psa_ports->pspa_count != 0) {
2762 					errno = EINVAL;
2763 					ret = -1;
2764 					goto out;
2765 				}
2766 				ad.port_actions = psattr->psa_ports;
2767 				ad.port_actions_size = psact_size;
2768 			}
2769 			if (psattr->psa_mac_extensions != NULL) {
2770 				size_t macext_size = PS_MAC_EXTENSIONS_SIZE(psattr->psa_mac_extensions->psmx_count);
2771 				if (macext_size == 0 && psattr->psa_mac_extensions->psmx_count != 0) {
2772 					errno = EINVAL;
2773 					ret = -1;
2774 					goto out;
2775 				}
2776 				ad.mac_extensions = psattr->psa_mac_extensions;
2777 				ad.mac_extensions_size = macext_size;
2778 			}
2779 			if (psattr->psa_coalition_info != NULL) {
2780 				ad.coal_info_size = sizeof(struct _posix_spawn_coalition_info);
2781 				ad.coal_info = psattr->psa_coalition_info;
2782 			}
2783 			if (psattr->psa_persona_info != NULL) {
2784 				ad.persona_info_size = sizeof(struct _posix_spawn_persona_info);
2785 				ad.persona_info = psattr->psa_persona_info;
2786 			}
2787 			if (psattr->psa_posix_cred_info != NULL) {
2788 				ad.posix_cred_info_size = sizeof(struct _posix_spawn_posix_cred_info);
2789 				ad.posix_cred_info = psattr->psa_posix_cred_info;
2790 			}
2791 			if (psattr->psa_subsystem_root_path != NULL) {
2792 				ad.subsystem_root_path_size = MAXPATHLEN;
2793 				ad.subsystem_root_path = psattr->psa_subsystem_root_path;
2794 			}
2795 		}
2796 		if (file_actions != NULL && *file_actions != NULL) {
2797 			_posix_spawn_file_actions_t psactsp =
2798 			    *(_posix_spawn_file_actions_t *)file_actions;
2799 
2800 			if (psactsp->psfa_act_count > 0) {
2801 				size_t fa_size = PSF_ACTIONS_SIZE(psactsp->psfa_act_count);
2802 				if (fa_size == 0 && psactsp->psfa_act_count != 0) {
2803 					errno = EINVAL;
2804 					ret = -1;
2805 					goto out;
2806 				}
2807 				ad.file_actions_size = fa_size;
2808 				ad.file_actions = psactsp;
2809 			}
2810 		}
2811 	}
2812 
2813 	if (!posix_spawn_with_filter ||
2814 	    !posix_spawn_with_filter(pid, path, argv, envp, adp, &ret)) {
2815 		ret = __posix_spawn(pid, path, adp, argv, envp);
2816 	}
2817 
2818 out:
2819 	if (ret < 0) {
2820 		ret = errno;
2821 	}
2822 	errno = saveerrno;
2823 	return ret;
2824 }
2825 
2826 int
execve(const char * fname,char * const * argp,char * const * envp)2827 execve(const char *fname, char * const *argp, char * const *envp)
2828 {
2829 	int ret;
2830 	if (execve_with_filter) {
2831 		/* Noinline slow path to avoid a large stack frame in the common case */
2832 		return execve_with_filter(fname, argp, envp);
2833 	}
2834 
2835 	ret = __execve(fname, argp, envp);
2836 	return ret;
2837 }
2838