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