Loading...
sys/posix_spawn.c Libc-498 Libc-825.26
--- Libc/Libc-498/sys/posix_spawn.c
+++ Libc/Libc-825.26/sys/posix_spawn.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2006-2008 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -27,6 +27,7 @@
 
 #include <sys/types.h> /* for user_size_t */
 #include <spawn.h>
+#include <spawn_private.h>
 #include <sys/spawn_internal.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -38,6 +39,9 @@
 #include <mach/port.h>
 #include <mach/exception_types.h>
 
+#if TARGET_OS_EMBEDDED
+#include <sys/kern_memorystatus.h>
+#endif
 
 /*
  * posix_spawnattr_init
@@ -98,6 +102,29 @@
 
 		/* Default is no port actions to take */
 		(*psattrp)->psa_ports = NULL;
+
+		/*
+		 * The default value of this attribute shall be an no
+		 * process control on resource starvation
+		 */
+		(*psattrp)->psa_pcontrol = 0;
+
+		/*
+		 * The default value of this attribute shall be an no
+		 * process control on resource starvation
+		 */
+		(*psattrp)->psa_apptype = 0;
+
+#if TARGET_OS_EMBEDDED
+		/* Jetsam related */
+		(*psattrp)->psa_jetsam_flags = 0;
+		(*psattrp)->psa_priority = DEFAULT_JETSAM_PRIORITY;
+		(*psattrp)->psa_high_water_mark = -1;
+#endif
+
+		/* Default is no CPU usage monitor active. */
+		(*psattrp)->psa_cpumonitor_percent = 0;
+		(*psattrp)->psa_cpumonitor_interval = 0;
 	}
 
 	return (err);
@@ -124,6 +151,8 @@
  * NOTIMP:	Allowed failures (checking NOT required):
  *		EINVAL	The value specified by attr is invalid.
  */
+static int posix_spawn_destroyportactions_np(posix_spawnattr_t *);
+
 int
 posix_spawnattr_destroy(posix_spawnattr_t *attr)
 {
@@ -357,6 +386,78 @@
 		*ocount = i;
 	return 0;
 }
+
+
+/*
+ * posix_spawnattr_getpcontrol_np
+ *
+ * Description:	Retrieve the  process control property set default according to
+ *		the spawn attribute value referenced by 'attr' and place the
+ *		result into the memory containing the control  referenced by
+ *		'pcontrol'
+ *
+ * Parameters:	attr			The spawn attributes object whose
+ *					signal set for default signals is to
+ *					be retrieved
+ *		pcontrol		A pointer to an int  to receive
+ *					the process control info
+ *
+ * Returns:	0			Success
+ *
+ * Implicit Returns:
+ *		*pcontrol (modified)	The signal set of signals to default
+ *					from the spawn attributes object
+ */
+int
+posix_spawnattr_getpcontrol_np(const posix_spawnattr_t * __restrict attr,
+		int * __restrict pcontrol)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL)
+		return EINVAL;
+
+	psattr = *(_posix_spawnattr_t *)attr;
+	*pcontrol = psattr->psa_pcontrol;
+
+	return (0);
+}
+
+/*
+ * posix_spawnattr_getapptype_np
+ *
+ * Description:	Retrieve the  process specific behaviors and app launch typea
+ *		spawn attribute value referenced by 'attr' and place the
+ *		result into the memory containing the control  referenced by
+ *		'apptype'
+ *
+ * Parameters:	attr			The spawn attributes object whose
+ *					signal set for default signals is to
+ *					be retrieved
+ *		apptype			A pointer to an int  to receive
+ *					the process control info
+ *
+ * Returns:	0			Success
+ *
+ * Implicit Returns:
+ *		*pcontrol (modified)	The signal set of signals to default
+ *					from the spawn attributes object
+ */
+int
+posix_spawnattr_getapptype_np(const posix_spawnattr_t * __restrict attr,
+		int * __restrict apptype)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL)
+		return EINVAL;
+
+	psattr = *(_posix_spawnattr_t *)attr;
+	*apptype = psattr->psa_apptype;
+
+	return (0);
+}
+
 /*
  * posix_spawnattr_setsigdefault
  *
@@ -489,12 +590,72 @@
 	return 0;
 }
 
+
+/*
+ * posix_spawnattr_setpcontrol_np
+ *
+ * Description:	Set the process control property according to
+ *		attribute value referenced by 'attr' from the memory
+ *		containing the int value 'pcontrol'
+ *
+ * Parameters:	attr			The spawn attributes object whose
+ *					signal set for default signals is to
+ *					be set
+ *		pcontrol		An int value of the process control info
+ *
+ * Returns:	0			Success
+ */
+int
+posix_spawnattr_setpcontrol_np(posix_spawnattr_t * __restrict attr,
+		const int pcontrol)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL)
+		return EINVAL;
+
+	psattr = *(_posix_spawnattr_t *)attr;
+	psattr->psa_pcontrol = pcontrol;
+
+	return (0);
+}
+
+
+/*
+ * posix_spawnattr_setapptype_np
+ *
+ * Description:	Set the process specific behaviors and app launch type
+ *		attribute value referenced by 'attr' from the memory
+ *		containing the int value 'apptype'
+ *
+ * Parameters:	attr			The spawn attributes object whose
+ *					signal set for default signals is to
+ *					be set
+ *		apptype			An int value of the apptype info
+ *
+ * Returns:	0			Success
+ */
+int
+posix_spawnattr_setapptype_np(posix_spawnattr_t * __restrict attr,
+		const int apptype)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL)
+		return EINVAL;
+
+	psattr = *(_posix_spawnattr_t *)attr;
+	psattr->psa_apptype = apptype;
+
+	return (0);
+}
+
 /*
  * posix_spawn_createportactions_np
  * Description: create a new posix_spawn_port_actions struct and link
  * 	it into the posix_spawnattr.
  */
-int
+static int
 posix_spawn_createportactions_np(posix_spawnattr_t *attr)
 {
 	_posix_spawnattr_t psattr;
@@ -519,7 +680,7 @@
  * posix_spawn_growportactions_np
  * Description: Enlarge the size of portactions if necessary 
  */
-int
+static int
 posix_spawn_growportactions_np(posix_spawnattr_t *attr)
 {
 	_posix_spawnattr_t psattr;
@@ -548,7 +709,7 @@
  * posix_spawn_destroyportactions_np
  * Description: clean up portactions struct in posix_spawnattr_t attr
  */
-int
+static int
 posix_spawn_destroyportactions_np(posix_spawnattr_t *attr)
 {
 	_posix_spawnattr_t psattr;
@@ -683,6 +844,58 @@
 	return err;
 }
 
+/*
+ * posix_spawnattr_setauditsessionport_np
+ *
+ * Description:	Set the audit session port rights attribute in the spawned task.
+ *		This is used to securely set the audit session information for
+ *		the new task.
+ *
+ * Parameters:	attr			The spawn attributes object for the
+ * 					new process
+ * 		au_sessionport		The audit session send port right
+ *
+ * Returns:	0			Success
+ */
+int    
+posix_spawnattr_setauditsessionport_np(
+		posix_spawnattr_t       *attr,
+		mach_port_t              au_sessionport)
+{
+	_posix_spawnattr_t psattr;
+	int err = 0;
+	_ps_port_action_t *action;
+	_posix_spawn_port_actions_t ports;
+
+	if (attr == NULL || *attr == NULL)
+		return EINVAL;
+
+	psattr = *(_posix_spawnattr_t *)attr;
+	ports = psattr->psa_ports;
+	/* Have any port actions been created yet? */
+	if (ports == NULL) {
+		err = posix_spawn_createportactions_np(attr);
+		if (err) 
+			return err;
+		ports = psattr->psa_ports;
+	}
+	
+	/* Is there enough room? */
+	if (ports->pspa_alloc == ports->pspa_count) {
+		err = posix_spawn_growportactions_np(attr);
+		if (err)
+			return err;
+	}
+
+	/* Add this action to next spot in array */
+	action = &ports->pspa_actions[ports->pspa_count];
+	action->port_type = PSPA_AU_SESSION;
+	action->new_port = au_sessionport;
+	
+	ports->pspa_count++;
+	return err;
+}
+
 
 /*
  * posix_spawn_file_actions_init
@@ -810,7 +1023,7 @@
  *		opened with flags 'oflag' and mode 'mode', and, if successful,
  *		return as descriptor 'filedes' to the spawned process.
  *
- * Parameters:	file_actions		File action object to add open to
+ * Parameters:	file_actions		File action object to augment
  *		filedes			fd that open is to use
  *		path			path to file to open
  *		oflag			open file flags
@@ -872,7 +1085,7 @@
  *		that will cause the file referenced by 'filedes' to be
  *		attempted to be closed in the spawned process.
  *
- * Parameters:	file_actions		File action object to add open to
+ * Parameters:	file_actions		File action object to augment
  *		filedes			fd to close
  *
  * Returns:	0			Success
@@ -922,12 +1135,12 @@
 /*
  * posix_spawn_file_actions_adddup2
  *
- * Description:	Add a dpu2 action to the object referenced by 'file_actions'
+ * Description:	Add a dup2 action to the object referenced by 'file_actions'
  *		that will cause the file referenced by 'filedes' to be
  *		attempted to be dup2'ed to the descriptor 'newfiledes' in the
  *		spawned process.
  *
- * Parameters:	file_actions		File action object to add open to
+ * Parameters:	file_actions		File action object to augment
  *		filedes			fd to dup2
  *		newfiledes		fd to dup2 it to
  *
@@ -976,6 +1189,140 @@
 	return (0);
 }
 
+/*
+ * posix_spawn_file_actions_addinherit_np
+ *
+ * Description:	Add the "inherit" action to the object referenced by
+ *		'file_actions' that will cause the file referenced by
+ *		'filedes' to continue to be available in the spawned
+ *		process via the same descriptor.
+ *
+ *		Inheritance is the normal default behaviour for
+ *		file descriptors across exec and spawn; but if the
+ *		POSIX_SPAWN_CLOEXEC_DEFAULT flag is set, the usual
+ *		default is reversed for the purposes of the spawn
+ *		invocation.  Any pre-existing descriptors that
+ *		need to be made available to the spawned process can
+ *		be marked explicitly as 'inherit' via this interface.
+ *		Otherwise they will be automatically closed.
+ *
+ *		Note that any descriptors created via the other file
+ *		actions interfaces are automatically marked as 'inherit'.
+ *
+ * Parameters:	file_actions		File action object to augment
+ *		filedes			fd to inherit.
+ *
+ * Returns:	0			Success
+ *		EBADF			The value specified by fildes is
+ *					negative or greater than or equal to
+ *					{OPEN_MAX}.
+ *		ENOMEM			Insufficient memory exists to add to
+ *					the spawn file actions object.
+ *
+ * NOTIMP:	Allowed failures (checking NOT required):
+ *		EINVAL	The value specified by file_actions is invalid.
+ */
+int
+posix_spawn_file_actions_addinherit_np(posix_spawn_file_actions_t *file_actions,
+		int filedes)
+{
+	_posix_spawn_file_actions_t *psactsp;
+	_psfa_action_t *psfileact;
+
+	if (file_actions == NULL || *file_actions == NULL)
+		return (EINVAL);
+
+	psactsp = (_posix_spawn_file_actions_t *)file_actions;
+	/* Range check; required by POSIX */
+	if (filedes < 0 || filedes >= OPEN_MAX)
+		return (EBADF);
+
+#if defined(POSIX_SPAWN_CLOEXEC_DEFAULT)	// TODO: delete this check
+	/* If we do not have enough slots, grow the structure */
+	if ((*psactsp)->psfa_act_count == (*psactsp)->psfa_act_alloc) {
+		/* need to grow file actions structure */
+		if (_posix_spawn_file_actions_grow(psactsp))
+			return (ENOMEM);
+	}
+
+	/*
+	 * Allocate next available slot and fill it out
+	 */
+	psfileact = &(*psactsp)->psfa_act_acts[(*psactsp)->psfa_act_count++];
+
+	psfileact->psfaa_type = PSFA_INHERIT;
+	psfileact->psfaa_filedes = filedes;
+#endif
+	return (0);
+}
+
+int
+posix_spawnattr_setcpumonitor(posix_spawnattr_t * __restrict attr,
+		uint64_t percent, uint64_t interval)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL || percent == 0 || percent > 100)
+		return (EINVAL);
+
+	psattr = *(_posix_spawnattr_t *)attr;
+
+	psattr->psa_cpumonitor_percent = percent;
+	psattr->psa_cpumonitor_interval = interval;
+
+	return (0);			
+}
+
+int
+posix_spawnattr_getcpumonitor(posix_spawnattr_t * __restrict attr,
+		uint64_t *percent, uint64_t *interval)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL)
+		return (EINVAL);
+
+	psattr = *(_posix_spawnattr_t *)attr;
+
+	*percent = psattr->psa_cpumonitor_percent;
+	*interval = psattr->psa_cpumonitor_interval;
+
+	return (0);
+}
+
+#if TARGET_OS_EMBEDDED
+/*
+ * posix_spawnattr_setjetsam
+ *
+ * Description:	Set jetsam attributes for the spawn attribute object
+ *		referred to by 'attr'.
+ *
+ * Parameters:	flags			The flags value to set
+ *		priority		Relative jetsam priority
+ *		high_water_mark		Value in pages; resident page
+ *					counts above this level can
+ *					result in termination
+ *
+ * Returns:	0			Success
+ */
+int
+posix_spawnattr_setjetsam(posix_spawnattr_t * __restrict attr,
+		short flags, int priority, int high_water_mark)
+{
+	_posix_spawnattr_t psattr;
+
+	if (attr == NULL || *attr == NULL)
+		return EINVAL;
+
+	psattr = *(_posix_spawnattr_t *)attr;
+	
+	psattr->psa_jetsam_flags = flags;
+	psattr->psa_priority = priority;
+	psattr->psa_high_water_mark = high_water_mark;
+
+	return (0);
+}
+#endif
 
 /*
  * posix_spawnp