xref: /onnv-gate/usr/src/lib/libc/port/threads/spawn.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52248Sraf  * Common Development and Distribution License (the "License").
62248Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
212248Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
313086Sraf #include <sys/libc_kernel.h>
320Sstevel@tonic-gate #include <sys/procset.h>
333235Sraf #include <sys/fork.h>
340Sstevel@tonic-gate #include <alloca.h>
350Sstevel@tonic-gate #include <spawn.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	ALL_POSIX_SPAWN_FLAGS			\
380Sstevel@tonic-gate 		(POSIX_SPAWN_RESETIDS |		\
390Sstevel@tonic-gate 		POSIX_SPAWN_SETPGROUP |		\
400Sstevel@tonic-gate 		POSIX_SPAWN_SETSIGDEF |		\
410Sstevel@tonic-gate 		POSIX_SPAWN_SETSIGMASK |	\
420Sstevel@tonic-gate 		POSIX_SPAWN_SETSCHEDPARAM |	\
433235Sraf 		POSIX_SPAWN_SETSCHEDULER |	\
443235Sraf 		POSIX_SPAWN_NOSIGCHLD_NP |	\
453235Sraf 		POSIX_SPAWN_WAITPID_NP)
460Sstevel@tonic-gate 
470Sstevel@tonic-gate typedef struct {
486247Sraf 	int		sa_psflags;	/* POSIX_SPAWN_* flags */
496247Sraf 	int		sa_priority;
500Sstevel@tonic-gate 	int		sa_schedpolicy;
510Sstevel@tonic-gate 	pid_t		sa_pgroup;
520Sstevel@tonic-gate 	sigset_t	sa_sigdefault;
530Sstevel@tonic-gate 	sigset_t	sa_sigmask;
540Sstevel@tonic-gate } spawn_attr_t;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate typedef struct file_attr {
570Sstevel@tonic-gate 	struct file_attr *fa_next;	/* circular list of file actions */
580Sstevel@tonic-gate 	struct file_attr *fa_prev;
590Sstevel@tonic-gate 	enum {FA_OPEN, FA_CLOSE, FA_DUP2} fa_type;
600Sstevel@tonic-gate 	uint_t		fa_pathsize;	/* size of fa_path[] array */
610Sstevel@tonic-gate 	char		*fa_path;	/* copied pathname for open() */
620Sstevel@tonic-gate 	int		fa_oflag;	/* oflag for open() */
630Sstevel@tonic-gate 	mode_t		fa_mode;	/* mode for open() */
640Sstevel@tonic-gate 	int		fa_filedes;	/* file descriptor for open()/close() */
650Sstevel@tonic-gate 	int		fa_newfiledes;	/* new file descriptor for dup2() */
660Sstevel@tonic-gate } file_attr_t;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate extern	int	__lwp_sigmask(int, const sigset_t *, sigset_t *);
690Sstevel@tonic-gate extern	int	__sigaction(int, const struct sigaction *, struct sigaction *);
700Sstevel@tonic-gate 
713086Sraf static int
720Sstevel@tonic-gate perform_flag_actions(spawn_attr_t *sap)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int sig;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (sap->sa_psflags & POSIX_SPAWN_SETSIGMASK) {
770Sstevel@tonic-gate 		(void) __lwp_sigmask(SIG_SETMASK, &sap->sa_sigmask, NULL);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (sap->sa_psflags & POSIX_SPAWN_SETSIGDEF) {
810Sstevel@tonic-gate 		struct sigaction sigdfl;
820Sstevel@tonic-gate 
836515Sraf 		(void) memset(&sigdfl, 0, sizeof (sigdfl));
840Sstevel@tonic-gate 		for (sig = 1; sig < NSIG; sig++) {
856515Sraf 			if (sigismember(&sap->sa_sigdefault, sig))
860Sstevel@tonic-gate 				(void) __sigaction(sig, &sigdfl, NULL);
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (sap->sa_psflags & POSIX_SPAWN_RESETIDS) {
916515Sraf 		if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
923086Sraf 			return (errno);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (sap->sa_psflags & POSIX_SPAWN_SETPGROUP) {
966515Sraf 		if (setpgid(0, sap->sa_pgroup) != 0)
973086Sraf 			return (errno);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (sap->sa_psflags & POSIX_SPAWN_SETSCHEDULER) {
1016247Sraf 		if (setparam(P_LWPID, P_MYID,
1026247Sraf 		    sap->sa_schedpolicy, sap->sa_priority) == -1)
1033086Sraf 			return (errno);
1040Sstevel@tonic-gate 	} else if (sap->sa_psflags & POSIX_SPAWN_SETSCHEDPARAM) {
1056247Sraf 		if (setprio(P_LWPID, P_MYID, sap->sa_priority, NULL) == -1)
1063086Sraf 			return (errno);
1070Sstevel@tonic-gate 	}
1083086Sraf 
1093086Sraf 	return (0);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1123086Sraf static int
1130Sstevel@tonic-gate perform_file_actions(file_attr_t *fap)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	file_attr_t *froot = fap;
1160Sstevel@tonic-gate 	int fd;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	do {
1190Sstevel@tonic-gate 		switch (fap->fa_type) {
1200Sstevel@tonic-gate 		case FA_OPEN:
1216515Sraf 			fd = __open(fap->fa_path,
1225891Sraf 			    fap->fa_oflag, fap->fa_mode);
1230Sstevel@tonic-gate 			if (fd < 0)
1243086Sraf 				return (errno);
1250Sstevel@tonic-gate 			if (fd != fap->fa_filedes) {
1266515Sraf 				if (__fcntl(fd, F_DUP2FD, fap->fa_filedes) < 0)
1273086Sraf 					return (errno);
1286515Sraf 				(void) __close(fd);
1290Sstevel@tonic-gate 			}
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 		case FA_CLOSE:
1326515Sraf 			if (__close(fap->fa_filedes) == -1)
1333086Sraf 				return (errno);
1340Sstevel@tonic-gate 			break;
1350Sstevel@tonic-gate 		case FA_DUP2:
1366515Sraf 			fd = __fcntl(fap->fa_filedes, F_DUP2FD,
1375891Sraf 			    fap->fa_newfiledes);
1380Sstevel@tonic-gate 			if (fd < 0)
1393086Sraf 				return (errno);
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 	} while ((fap = fap->fa_next) != froot);
1433086Sraf 
1443086Sraf 	return (0);
1453086Sraf }
1463086Sraf 
1473235Sraf static int
1483235Sraf forkflags(spawn_attr_t *sap)
1493235Sraf {
1503235Sraf 	int flags = 0;
1513235Sraf 
1523235Sraf 	if (sap != NULL) {
1533235Sraf 		if (sap->sa_psflags & POSIX_SPAWN_NOSIGCHLD_NP)
1543235Sraf 			flags |= FORK_NOSIGCHLD;
1553235Sraf 		if (sap->sa_psflags & POSIX_SPAWN_WAITPID_NP)
1563235Sraf 			flags |= FORK_WAITPID;
1573235Sraf 	}
1583235Sraf 
1593235Sraf 	return (flags);
1603235Sraf }
1613235Sraf 
1623086Sraf /*
1633086Sraf  * set_error() / get_error() are used to guarantee that the local variable
1643086Sraf  * 'error' is set correctly in memory on return from vfork() in the parent.
1653086Sraf  */
1663086Sraf 
1673086Sraf static int
1683086Sraf set_error(int *errp, int err)
1693086Sraf {
1703086Sraf 	return (*errp = err);
1713086Sraf }
1723086Sraf 
1733086Sraf static int
1743086Sraf get_error(int *errp)
1753086Sraf {
1763086Sraf 	return (*errp);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * For MT safety, do not invoke the dynamic linker after calling vfork().
1810Sstevel@tonic-gate  * If some other thread was in the dynamic linker when this thread's parent
1820Sstevel@tonic-gate  * called vfork() then the dynamic linker's lock would still be held here
1830Sstevel@tonic-gate  * (with a defunct owner) and we would deadlock ourself if we invoked it.
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  * Therefore, all of the functions we call here after returning from
186*6812Sraf  * vforkx() in the child are not and must never be exported from libc
1870Sstevel@tonic-gate  * as global symbols.  To do so would risk invoking the dynamic linker.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
191*6812Sraf posix_spawn(
1920Sstevel@tonic-gate 	pid_t *pidp,
1930Sstevel@tonic-gate 	const char *path,
1940Sstevel@tonic-gate 	const posix_spawn_file_actions_t *file_actions,
1950Sstevel@tonic-gate 	const posix_spawnattr_t *attrp,
1960Sstevel@tonic-gate 	char *const argv[],
1970Sstevel@tonic-gate 	char *const envp[])
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	spawn_attr_t *sap = attrp? attrp->__spawn_attrp : NULL;
2000Sstevel@tonic-gate 	file_attr_t *fap = file_actions? file_actions->__file_attrp : NULL;
2013086Sraf 	int error;		/* this will be set by the child */
2020Sstevel@tonic-gate 	pid_t pid;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (attrp != NULL && sap == NULL)
2050Sstevel@tonic-gate 		return (EINVAL);
2060Sstevel@tonic-gate 
207*6812Sraf 	switch (pid = vforkx(forkflags(sap))) {
2080Sstevel@tonic-gate 	case 0:			/* child */
2090Sstevel@tonic-gate 		break;
2100Sstevel@tonic-gate 	case -1:		/* parent, failure */
2110Sstevel@tonic-gate 		return (errno);
2120Sstevel@tonic-gate 	default:		/* parent, success */
2133086Sraf 		/*
2143086Sraf 		 * We don't get here until the child exec()s or exit()s
2153086Sraf 		 */
2163086Sraf 		if (pidp != NULL && get_error(&error) == 0)
2170Sstevel@tonic-gate 			*pidp = pid;
2183086Sraf 		return (get_error(&error));
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if (sap != NULL)
2223086Sraf 		if (set_error(&error, perform_flag_actions(sap)) != 0)
2236515Sraf 			_exit(_EVAPORATE);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (fap != NULL)
2263086Sraf 		if (set_error(&error, perform_file_actions(fap)) != 0)
2276515Sraf 			_exit(_EVAPORATE);
2280Sstevel@tonic-gate 
2293086Sraf 	(void) set_error(&error, 0);
2306515Sraf 	(void) execve(path, argv, envp);
2313086Sraf 	(void) set_error(&error, errno);
2326515Sraf 	_exit(_EVAPORATE);
2330Sstevel@tonic-gate 	return (0);	/* not reached */
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * Much of posix_spawnp() blatently stolen from execvp() (port/gen/execvp.c).
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate 
2405891Sraf extern int libc__xpg4;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate static const char *
2430Sstevel@tonic-gate execat(const char *s1, const char *s2, char *si)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	int cnt = PATH_MAX + 1;
2460Sstevel@tonic-gate 	char *s;
2470Sstevel@tonic-gate 	char c;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	for (s = si; (c = *s1) != '\0' && c != ':'; s1++) {
2500Sstevel@tonic-gate 		if (cnt > 0) {
2510Sstevel@tonic-gate 			*s++ = c;
2520Sstevel@tonic-gate 			cnt--;
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 	if (si != s && cnt > 0) {
2560Sstevel@tonic-gate 		*s++ = '/';
2570Sstevel@tonic-gate 		cnt--;
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	for (; (c = *s2) != '\0' && cnt > 0; s2++) {
2600Sstevel@tonic-gate 		*s++ = c;
2610Sstevel@tonic-gate 		cnt--;
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 	*s = '\0';
2640Sstevel@tonic-gate 	return (*s1? ++s1: NULL);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /* ARGSUSED */
2680Sstevel@tonic-gate int
269*6812Sraf posix_spawnp(
2700Sstevel@tonic-gate 	pid_t *pidp,
2710Sstevel@tonic-gate 	const char *file,
2720Sstevel@tonic-gate 	const posix_spawn_file_actions_t *file_actions,
2730Sstevel@tonic-gate 	const posix_spawnattr_t *attrp,
2740Sstevel@tonic-gate 	char *const argv[],
2750Sstevel@tonic-gate 	char *const envp[])
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	spawn_attr_t *sap = attrp? attrp->__spawn_attrp : NULL;
2780Sstevel@tonic-gate 	file_attr_t *fap = file_actions? file_actions->__file_attrp : NULL;
2790Sstevel@tonic-gate 	const char *pathstr = (strchr(file, '/') == NULL)? getenv("PATH") : "";
2805891Sraf 	int xpg4 = libc__xpg4;
2813086Sraf 	int error;		/* this will be set by the child */
2820Sstevel@tonic-gate 	char path[PATH_MAX+4];
2830Sstevel@tonic-gate 	const char *cp;
2840Sstevel@tonic-gate 	pid_t pid;
2850Sstevel@tonic-gate 	char **newargs;
2860Sstevel@tonic-gate 	int argc;
2870Sstevel@tonic-gate 	int i;
2880Sstevel@tonic-gate 	static const char *sun_path = "/bin/sh";
2890Sstevel@tonic-gate 	static const char *xpg4_path = "/usr/xpg4/bin/sh";
2900Sstevel@tonic-gate 	static const char *shell = "sh";
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (attrp != NULL && sap == NULL)
2930Sstevel@tonic-gate 		return (EINVAL);
2940Sstevel@tonic-gate 
2953086Sraf 	if (*file == '\0')
2963086Sraf 		return (EACCES);
2973086Sraf 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * We may need to invoke the shell with a slightly modified
3000Sstevel@tonic-gate 	 * argv[] array.  To do this we need to preallocate the array.
3010Sstevel@tonic-gate 	 * We must call alloca() before calling vfork() because doing
3020Sstevel@tonic-gate 	 * it after vfork() (in the child) would corrupt the parent.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	for (argc = 0; argv[argc] != NULL; argc++)
3050Sstevel@tonic-gate 		continue;
3060Sstevel@tonic-gate 	newargs = alloca((argc + 2) * sizeof (char *));
3070Sstevel@tonic-gate 
308*6812Sraf 	switch (pid = vforkx(forkflags(sap))) {
3090Sstevel@tonic-gate 	case 0:			/* child */
3100Sstevel@tonic-gate 		break;
3110Sstevel@tonic-gate 	case -1:		/* parent, failure */
3120Sstevel@tonic-gate 		return (errno);
3130Sstevel@tonic-gate 	default:		/* parent, success */
3143086Sraf 		/*
3153086Sraf 		 * We don't get here until the child exec()s or exit()s
3163086Sraf 		 */
3173086Sraf 		if (pidp != NULL && get_error(&error) == 0)
3180Sstevel@tonic-gate 			*pidp = pid;
3193086Sraf 		return (get_error(&error));
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (sap != NULL)
3233086Sraf 		if (set_error(&error, perform_flag_actions(sap)) != 0)
3246515Sraf 			_exit(_EVAPORATE);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (fap != NULL)
3273086Sraf 		if (set_error(&error, perform_file_actions(fap)) != 0)
3286515Sraf 			_exit(_EVAPORATE);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	if (pathstr == NULL) {
3310Sstevel@tonic-gate 		/*
3320Sstevel@tonic-gate 		 * XPG4:  pathstr is equivalent to _CS_PATH, except that
3330Sstevel@tonic-gate 		 * :/usr/sbin is appended when root, and pathstr must end
3340Sstevel@tonic-gate 		 * with a colon when not root.  Keep these paths in sync
3350Sstevel@tonic-gate 		 * with _CS_PATH in confstr.c.  Note that pathstr must end
3360Sstevel@tonic-gate 		 * with a colon when not root so that when file doesn't
3370Sstevel@tonic-gate 		 * contain '/', the last call to execat() will result in an
3380Sstevel@tonic-gate 		 * attempt to execv file from the current directory.
3390Sstevel@tonic-gate 		 */
3406515Sraf 		if (geteuid() == 0 || getuid() == 0) {
3410Sstevel@tonic-gate 			if (!xpg4)
3420Sstevel@tonic-gate 				pathstr = "/usr/sbin:/usr/ccs/bin:/usr/bin";
3430Sstevel@tonic-gate 			else
3440Sstevel@tonic-gate 				pathstr = "/usr/xpg4/bin:/usr/ccs/bin:"
3450Sstevel@tonic-gate 				    "/usr/bin:/opt/SUNWspro/bin:/usr/sbin";
3460Sstevel@tonic-gate 		} else {
3470Sstevel@tonic-gate 			if (!xpg4)
3480Sstevel@tonic-gate 				pathstr = "/usr/ccs/bin:/usr/bin:";
3490Sstevel@tonic-gate 			else
3500Sstevel@tonic-gate 				pathstr = "/usr/xpg4/bin:/usr/ccs/bin:"
3510Sstevel@tonic-gate 				    "/usr/bin:/opt/SUNWspro/bin:";
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	cp = pathstr;
3560Sstevel@tonic-gate 	do {
3570Sstevel@tonic-gate 		cp = execat(cp, file, path);
3580Sstevel@tonic-gate 		/*
3590Sstevel@tonic-gate 		 * 4025035 and 4038378
3600Sstevel@tonic-gate 		 * if a filename begins with a "-" prepend "./" so that
3610Sstevel@tonic-gate 		 * the shell can't interpret it as an option
3620Sstevel@tonic-gate 		 */
3630Sstevel@tonic-gate 		if (*path == '-') {
3640Sstevel@tonic-gate 			char *s;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 			for (s = path; *s != '\0'; s++)
3670Sstevel@tonic-gate 				continue;
3680Sstevel@tonic-gate 			for (; s >= path; s--)
3690Sstevel@tonic-gate 				*(s + 2) = *s;
3700Sstevel@tonic-gate 			path[0] = '.';
3710Sstevel@tonic-gate 			path[1] = '/';
3720Sstevel@tonic-gate 		}
3733086Sraf 		(void) set_error(&error, 0);
3746515Sraf 		(void) execve(path, argv, envp);
3753086Sraf 		if (set_error(&error, errno) == ENOEXEC) {
3760Sstevel@tonic-gate 			newargs[0] = (char *)shell;
3770Sstevel@tonic-gate 			newargs[1] = path;
3780Sstevel@tonic-gate 			for (i = 1; i <= argc; i++)
3790Sstevel@tonic-gate 				newargs[i + 1] = argv[i];
3803086Sraf 			(void) set_error(&error, 0);
3816515Sraf 			(void) execve(xpg4? xpg4_path : sun_path,
3823086Sraf 			    newargs, envp);
3833086Sraf 			(void) set_error(&error, errno);
3846515Sraf 			_exit(_EVAPORATE);
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 	} while (cp);
3876515Sraf 	_exit(_EVAPORATE);
3880Sstevel@tonic-gate 	return (0);	/* not reached */
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate int
392*6812Sraf posix_spawn_file_actions_init(
3930Sstevel@tonic-gate 	posix_spawn_file_actions_t *file_actions)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	file_actions->__file_attrp = NULL;
3960Sstevel@tonic-gate 	return (0);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate int
400*6812Sraf posix_spawn_file_actions_destroy(
4010Sstevel@tonic-gate 	posix_spawn_file_actions_t *file_actions)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	file_attr_t *froot = file_actions->__file_attrp;
4040Sstevel@tonic-gate 	file_attr_t *fap;
4050Sstevel@tonic-gate 	file_attr_t *next;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if ((fap = froot) != NULL) {
4080Sstevel@tonic-gate 		do {
4090Sstevel@tonic-gate 			next = fap->fa_next;
4100Sstevel@tonic-gate 			if (fap-> fa_type == FA_OPEN)
4110Sstevel@tonic-gate 				lfree(fap->fa_path, fap->fa_pathsize);
4120Sstevel@tonic-gate 			lfree(fap, sizeof (*fap));
4130Sstevel@tonic-gate 		} while ((fap = next) != froot);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 	file_actions->__file_attrp = NULL;
4160Sstevel@tonic-gate 	return (0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate static void
4200Sstevel@tonic-gate add_file_attr(posix_spawn_file_actions_t *file_actions, file_attr_t *fap)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	file_attr_t *froot = file_actions->__file_attrp;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (froot == NULL) {
4250Sstevel@tonic-gate 		fap->fa_next = fap->fa_prev = fap;
4260Sstevel@tonic-gate 		file_actions->__file_attrp = fap;
4270Sstevel@tonic-gate 	} else {
4280Sstevel@tonic-gate 		fap->fa_next = froot;
4290Sstevel@tonic-gate 		fap->fa_prev = froot->fa_prev;
4300Sstevel@tonic-gate 		froot->fa_prev->fa_next = fap;
4310Sstevel@tonic-gate 		froot->fa_prev = fap;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate int
436*6812Sraf posix_spawn_file_actions_addopen(
4370Sstevel@tonic-gate 	posix_spawn_file_actions_t *file_actions,
4380Sstevel@tonic-gate 	int filedes,
4390Sstevel@tonic-gate 	const char *path,
4400Sstevel@tonic-gate 	int oflag,
4410Sstevel@tonic-gate 	mode_t mode)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	file_attr_t *fap;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	if (filedes < 0)
4460Sstevel@tonic-gate 		return (EBADF);
4470Sstevel@tonic-gate 	if ((fap = lmalloc(sizeof (*fap))) == NULL)
4480Sstevel@tonic-gate 		return (ENOMEM);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	fap->fa_pathsize = strlen(path) + 1;
4510Sstevel@tonic-gate 	if ((fap->fa_path = lmalloc(fap->fa_pathsize)) == NULL) {
4520Sstevel@tonic-gate 		lfree(fap, sizeof (*fap));
4530Sstevel@tonic-gate 		return (ENOMEM);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 	(void) strcpy(fap->fa_path, path);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	fap->fa_type = FA_OPEN;
4580Sstevel@tonic-gate 	fap->fa_oflag = oflag;
4590Sstevel@tonic-gate 	fap->fa_mode = mode;
4600Sstevel@tonic-gate 	fap->fa_filedes = filedes;
4610Sstevel@tonic-gate 	add_file_attr(file_actions, fap);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	return (0);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate int
467*6812Sraf posix_spawn_file_actions_addclose(
4680Sstevel@tonic-gate 	posix_spawn_file_actions_t *file_actions,
4690Sstevel@tonic-gate 	int filedes)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	file_attr_t *fap;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if (filedes < 0)
4740Sstevel@tonic-gate 		return (EBADF);
4750Sstevel@tonic-gate 	if ((fap = lmalloc(sizeof (*fap))) == NULL)
4760Sstevel@tonic-gate 		return (ENOMEM);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	fap->fa_type = FA_CLOSE;
4790Sstevel@tonic-gate 	fap->fa_filedes = filedes;
4800Sstevel@tonic-gate 	add_file_attr(file_actions, fap);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	return (0);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate int
486*6812Sraf posix_spawn_file_actions_adddup2(
4870Sstevel@tonic-gate 	posix_spawn_file_actions_t *file_actions,
4880Sstevel@tonic-gate 	int filedes,
4890Sstevel@tonic-gate 	int newfiledes)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	file_attr_t *fap;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (filedes < 0 || newfiledes < 0)
4940Sstevel@tonic-gate 		return (EBADF);
4950Sstevel@tonic-gate 	if ((fap = lmalloc(sizeof (*fap))) == NULL)
4960Sstevel@tonic-gate 		return (ENOMEM);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	fap->fa_type = FA_DUP2;
4990Sstevel@tonic-gate 	fap->fa_filedes = filedes;
5000Sstevel@tonic-gate 	fap->fa_newfiledes = newfiledes;
5010Sstevel@tonic-gate 	add_file_attr(file_actions, fap);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	return (0);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate int
507*6812Sraf posix_spawnattr_init(
5080Sstevel@tonic-gate 	posix_spawnattr_t *attr)
5090Sstevel@tonic-gate {
5103235Sraf 	if ((attr->__spawn_attrp = lmalloc(sizeof (posix_spawnattr_t))) == NULL)
5110Sstevel@tonic-gate 		return (ENOMEM);
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Add default stuff here?
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	return (0);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate int
519*6812Sraf posix_spawnattr_destroy(
5200Sstevel@tonic-gate 	posix_spawnattr_t *attr)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (sap == NULL)
5250Sstevel@tonic-gate 		return (EINVAL);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * deallocate stuff here?
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	lfree(sap, sizeof (*sap));
5310Sstevel@tonic-gate 	attr->__spawn_attrp = NULL;
5320Sstevel@tonic-gate 	return (0);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate int
536*6812Sraf posix_spawnattr_setflags(
5370Sstevel@tonic-gate 	posix_spawnattr_t *attr,
5380Sstevel@tonic-gate 	short flags)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (sap == NULL ||
5430Sstevel@tonic-gate 	    (flags & ~ALL_POSIX_SPAWN_FLAGS))
5440Sstevel@tonic-gate 		return (EINVAL);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	sap->sa_psflags = flags;
5470Sstevel@tonic-gate 	return (0);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate int
551*6812Sraf posix_spawnattr_getflags(
5520Sstevel@tonic-gate 	const posix_spawnattr_t *attr,
5530Sstevel@tonic-gate 	short *flags)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if (sap == NULL)
5580Sstevel@tonic-gate 		return (EINVAL);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	*flags = sap->sa_psflags;
5610Sstevel@tonic-gate 	return (0);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate int
565*6812Sraf posix_spawnattr_setpgroup(
5660Sstevel@tonic-gate 	posix_spawnattr_t *attr,
5670Sstevel@tonic-gate 	pid_t pgroup)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (sap == NULL)
5720Sstevel@tonic-gate 		return (EINVAL);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	sap->sa_pgroup = pgroup;
5750Sstevel@tonic-gate 	return (0);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate int
579*6812Sraf posix_spawnattr_getpgroup(
5800Sstevel@tonic-gate 	const posix_spawnattr_t *attr,
5810Sstevel@tonic-gate 	pid_t *pgroup)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (sap == NULL)
5860Sstevel@tonic-gate 		return (EINVAL);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	*pgroup = sap->sa_pgroup;
5890Sstevel@tonic-gate 	return (0);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate int
593*6812Sraf posix_spawnattr_setschedparam(
5940Sstevel@tonic-gate 	posix_spawnattr_t *attr,
5950Sstevel@tonic-gate 	const struct sched_param *schedparam)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if (sap == NULL)
6000Sstevel@tonic-gate 		return (EINVAL);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * Check validity?
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	sap->sa_priority = schedparam->sched_priority;
6060Sstevel@tonic-gate 	return (0);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate int
610*6812Sraf posix_spawnattr_getschedparam(
6110Sstevel@tonic-gate 	const posix_spawnattr_t *attr,
6120Sstevel@tonic-gate 	struct sched_param *schedparam)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (sap == NULL)
6170Sstevel@tonic-gate 		return (EINVAL);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	schedparam->sched_priority = sap->sa_priority;
6200Sstevel@tonic-gate 	return (0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate int
624*6812Sraf posix_spawnattr_setschedpolicy(
6250Sstevel@tonic-gate 	posix_spawnattr_t *attr,
6260Sstevel@tonic-gate 	int schedpolicy)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
6290Sstevel@tonic-gate 
6306247Sraf 	if (sap == NULL || schedpolicy == SCHED_SYS)
6310Sstevel@tonic-gate 		return (EINVAL);
6320Sstevel@tonic-gate 
6336247Sraf 	/*
6346247Sraf 	 * Cache the policy information for later use
6356247Sraf 	 * by the vfork() child of posix_spawn().
6366247Sraf 	 */
6376247Sraf 	if (get_info_by_policy(schedpolicy) == NULL)
6386247Sraf 		return (errno);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	sap->sa_schedpolicy = schedpolicy;
6410Sstevel@tonic-gate 	return (0);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate int
645*6812Sraf posix_spawnattr_getschedpolicy(
6460Sstevel@tonic-gate 	const posix_spawnattr_t *attr,
6470Sstevel@tonic-gate 	int *schedpolicy)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	if (sap == NULL)
6520Sstevel@tonic-gate 		return (EINVAL);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	*schedpolicy = sap->sa_schedpolicy;
6550Sstevel@tonic-gate 	return (0);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate int
659*6812Sraf posix_spawnattr_setsigdefault(
6600Sstevel@tonic-gate 	posix_spawnattr_t *attr,
6610Sstevel@tonic-gate 	const sigset_t *sigdefault)
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	if (sap == NULL)
6660Sstevel@tonic-gate 		return (EINVAL);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	sap->sa_sigdefault = *sigdefault;
6690Sstevel@tonic-gate 	return (0);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate int
673*6812Sraf posix_spawnattr_getsigdefault(
6740Sstevel@tonic-gate 	const posix_spawnattr_t *attr,
6750Sstevel@tonic-gate 	sigset_t *sigdefault)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	if (sap == NULL)
6800Sstevel@tonic-gate 		return (EINVAL);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	*sigdefault = sap->sa_sigdefault;
6830Sstevel@tonic-gate 	return (0);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate int
687*6812Sraf posix_spawnattr_setsigmask(
6880Sstevel@tonic-gate 	posix_spawnattr_t *attr,
6890Sstevel@tonic-gate 	const sigset_t *sigmask)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (sap == NULL)
6940Sstevel@tonic-gate 		return (EINVAL);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	sap->sa_sigmask = *sigmask;
6970Sstevel@tonic-gate 	return (0);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate int
701*6812Sraf posix_spawnattr_getsigmask(
7020Sstevel@tonic-gate 	const posix_spawnattr_t *attr,
7030Sstevel@tonic-gate 	sigset_t *sigmask)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	spawn_attr_t *sap = attr->__spawn_attrp;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (sap == NULL)
7080Sstevel@tonic-gate 		return (EINVAL);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	*sigmask = sap->sa_sigmask;
7110Sstevel@tonic-gate 	return (0);
7120Sstevel@tonic-gate }
713