/*
 * $QNXLicenseC:  
 * Copyright 2007, QNX Software Systems. All Rights Reserved.
 *
 * You must obtain a written license from and pay applicable license fees to QNX 
 * Software Systems before you may reproduce, modify or distribute this software, 
 * or any work that includes all or part of this software.   Free development 
 * licenses are available for evaluation and non-commercial purposes.  For more 
 * information visit http://licensing.qnx.com or email licensing@qnx.com.
 *  
 * This file may contain contributions from others.  Please review this entire 
 * file for other proprietary rights or license notices, as well as the QNX 
 * Development Suite License Guide at http://licensing.qnx.com/license-guide/ 
 * for other information.
 * $
*/

#include <unistd.h>		// determines whether _POSIX_SPAWN is defined or not
#ifdef _POSIX_SPAWN

#include "posix_spawnattr.h"


/*******************************************************************************
 * get/setcred
 * 
 * Gets/Sets the uid and gid of the child process
 *
 * Normally, the credentials (ie. user and group id) of a process spawned 
 * with posix_spawn()/posix_spawnp() will inherit the credentials of the caller.
 * The posix_spawnattr_setcred() function can be used to set the credentials of 
 * a spawned process to something other than that of the callers credentials. In
 * order to accomplish this, the caller must have an effective uid of root at the
 * time the posix_spawn()/posix_spawnp() call is made otherwise EPERM will be
 * returned from the posix_spawn() call. A effective uid of root is not required
 * to set the credentials to the same values as the caller, which would have the
 * same effect as not calling this function at all.
 * 
 * If the criteria to posix_spawn() is met by the caller, both the real and
 * effective uid/gid of the spawned process will be set to the specified
 * credentials however the setuid/setgid mode of the spawned image takes
 * precedence over the effective uid/gid of the spawned process in the same
 * fashion as described for POSIX_SPAWN_RESETIDS (ie. setuid/setgid of the spawned
 * image will cause the spawned process to inherit the uid/gid of the image as
 * its effective uid/gid respectively. The real uid/gid will be as specified in
 * the credentials).
 * 
 * Note:
 * The credentials of the spawned process take effect before the process actually
 * runs and so will govern its access to specific resources. This may result in a
 * failure in the spawned process asynchronous to a successful
 * posix_spawn()/posix_spawnp() call.
 * 
 * The credentials established with the posix_spawnattr_setcred() will have no
 * effect unless the POSIX_SPAWNATTR_SETCRED flag has also been set. EPERM will
 * not be returned by posix_spawn()/posix_spawnp() if the POSIX_SPAWNATTR_SETCRED
 * flag is not set.
 * 
 * If both POSIX_SPAWN_SETCRED and POSIX_SPAWN_RESETIDS are set,
 * POSIX_SPAWN_SETCRED will take precedence
 * 
 * Returns:
 * 		EOK on success
 * 		EINVAL for any invalid parameter
*/
int posix_spawnattr_setcred(posix_spawnattr_t *attrp, uid_t uid, gid_t gid)
{
	if (!valid_attrp(attrp)) {
		return EINVAL;
	} else {
		_posix_spawnattr_t *_attrp = GET_ATTRP(attrp);

		if (_attrp == NULL) {
			if ((_attrp = _posix_spawnattr_t_extend(NULL, 0, 0)) == NULL) return ENOMEM;
			SET_ATTRP(attrp, _attrp);
		}
		_attrp->cred.uid = uid;	
		_attrp->cred.gid = gid;	
		return EOK;
	}
}

#endif	/* _POSIX_SPAWN */

