Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.61
      1  1.61  mycroft /*	$NetBSD: kern_exec.c,v 1.61 1995/02/22 01:39:56 mycroft Exp $	*/
      2  1.55      cgd 
      3  1.55      cgd /*-
      4  1.55      cgd  * Copyright (C) 1993, 1994 Christopher G. Demetriou
      5  1.55      cgd  * Copyright (C) 1992 Wolfgang Solfrank.
      6  1.55      cgd  * Copyright (C) 1992 TooLs GmbH.
      7  1.55      cgd  * All rights reserved.
      8  1.55      cgd  *
      9  1.55      cgd  * Redistribution and use in source and binary forms, with or without
     10  1.55      cgd  * modification, are permitted provided that the following conditions
     11  1.55      cgd  * are met:
     12  1.55      cgd  * 1. Redistributions of source code must retain the above copyright
     13  1.55      cgd  *    notice, this list of conditions and the following disclaimer.
     14  1.55      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.55      cgd  *    notice, this list of conditions and the following disclaimer in the
     16  1.55      cgd  *    documentation and/or other materials provided with the distribution.
     17  1.55      cgd  * 3. All advertising materials mentioning features or use of this software
     18  1.55      cgd  *    must display the following acknowledgement:
     19  1.55      cgd  *	This product includes software developed by TooLs GmbH.
     20  1.55      cgd  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  1.55      cgd  *    derived from this software without specific prior written permission.
     22  1.55      cgd  *
     23  1.55      cgd  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  1.55      cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.55      cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.55      cgd  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  1.55      cgd  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  1.55      cgd  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  1.55      cgd  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  1.55      cgd  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  1.55      cgd  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  1.55      cgd  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.55      cgd  */
     34  1.55      cgd 
     35  1.55      cgd #include <sys/param.h>
     36  1.55      cgd #include <sys/systm.h>
     37  1.55      cgd #include <sys/filedesc.h>
     38  1.55      cgd #include <sys/kernel.h>
     39  1.55      cgd #include <sys/proc.h>
     40  1.55      cgd #include <sys/mount.h>
     41  1.55      cgd #include <sys/malloc.h>
     42  1.55      cgd #include <sys/namei.h>
     43  1.55      cgd #include <sys/vnode.h>
     44  1.55      cgd #include <sys/file.h>
     45  1.55      cgd #include <sys/acct.h>
     46  1.55      cgd #include <sys/exec.h>
     47  1.55      cgd #include <sys/ktrace.h>
     48  1.55      cgd #include <sys/resourcevar.h>
     49  1.55      cgd #include <sys/wait.h>
     50  1.55      cgd #include <sys/mman.h>
     51  1.55      cgd #include <sys/signalvar.h>
     52  1.55      cgd #include <sys/stat.h>
     53  1.55      cgd 
     54  1.56      cgd #include <sys/syscallargs.h>
     55  1.56      cgd 
     56  1.55      cgd #include <vm/vm.h>
     57  1.55      cgd #include <vm/vm_kern.h>
     58  1.55      cgd 
     59  1.55      cgd #include <machine/cpu.h>
     60  1.55      cgd #include <machine/reg.h>
     61  1.55      cgd 
     62  1.55      cgd #ifdef COPY_SIGCODE
     63  1.55      cgd extern char sigcode[], esigcode[];
     64  1.55      cgd 
     65  1.55      cgd #define	szsigcode	(esigcode - sigcode)
     66  1.55      cgd #else
     67  1.55      cgd #define	szsigcode	0
     68  1.55      cgd #endif
     69  1.55      cgd 
     70  1.55      cgd /*
     71  1.55      cgd  * check exec:
     72  1.55      cgd  * given an "executable" described in the exec package's namei info,
     73  1.55      cgd  * see what we can do with it.
     74  1.55      cgd  *
     75  1.55      cgd  * ON ENTRY:
     76  1.55      cgd  *	exec package with appropriate namei info
     77  1.55      cgd  *	proc pointer of exec'ing proc
     78  1.55      cgd  *	NO SELF-LOCKED VNODES
     79  1.55      cgd  *
     80  1.55      cgd  * ON EXIT:
     81  1.55      cgd  *	error:	nothing held, etc.  exec header still allocated.
     82  1.55      cgd  *	ok:	filled exec package, one locked vnode.
     83  1.55      cgd  *
     84  1.55      cgd  * EXEC SWITCH ENTRY:
     85  1.55      cgd  * 	Locked vnode to check, exec package, proc.
     86  1.55      cgd  *
     87  1.55      cgd  * EXEC SWITCH EXIT:
     88  1.55      cgd  *	ok:	return 0, filled exec package, one locked vnode.
     89  1.55      cgd  *	error:	destructive:
     90  1.55      cgd  *			everything deallocated execept exec header.
     91  1.55      cgd  *		non-descructive:
     92  1.55      cgd  *			error code, locked vnode, exec header unmodified
     93  1.55      cgd  */
     94  1.55      cgd int
     95  1.55      cgd check_exec(p, epp)
     96  1.55      cgd 	struct proc *p;
     97  1.55      cgd 	struct exec_package *epp;
     98  1.55      cgd {
     99  1.55      cgd 	int error, i;
    100  1.55      cgd 	struct vnode *vp;
    101  1.55      cgd 	char *cp, *ep, *name;
    102  1.55      cgd 	struct nameidata *ndp;
    103  1.55      cgd 	int resid;
    104  1.55      cgd 
    105  1.55      cgd 	ndp = epp->ep_ndp;
    106  1.55      cgd 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    107  1.55      cgd 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
    108  1.55      cgd 	/* first get the vnode */
    109  1.55      cgd 	if (error = namei(ndp))
    110  1.55      cgd 		return error;
    111  1.55      cgd 	epp->ep_vp = vp = ndp->ni_vp;
    112  1.55      cgd 
    113  1.55      cgd 	/* check for regular file */
    114  1.55      cgd 	if (vp->v_type != VREG) {
    115  1.55      cgd 		error = EACCES;
    116  1.55      cgd 		goto bad1;
    117  1.55      cgd 	}
    118  1.55      cgd 
    119  1.55      cgd 	/* get attributes */
    120  1.55      cgd 	if (error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p))
    121  1.55      cgd 		goto bad1;
    122  1.55      cgd 
    123  1.55      cgd 	/* Check mount point */
    124  1.55      cgd 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    125  1.55      cgd 		error = EACCES;
    126  1.55      cgd 		goto bad1;
    127  1.55      cgd 	}
    128  1.55      cgd 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
    129  1.55      cgd 		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
    130  1.55      cgd 
    131  1.55      cgd 	/* check access.  for root we have to see if any exec bit on */
    132  1.55      cgd 	if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p))
    133  1.55      cgd 		goto bad1;
    134  1.55      cgd 	if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
    135  1.55      cgd 		error = EACCES;
    136  1.55      cgd 		goto bad1;
    137  1.55      cgd 	}
    138  1.55      cgd 
    139  1.55      cgd 	/* try to open it */
    140  1.55      cgd 	if (error = VOP_OPEN(vp, FREAD, p->p_ucred, p))
    141  1.55      cgd 		goto bad1;
    142  1.55      cgd 
    143  1.55      cgd 	/* now we have the file, get the exec header */
    144  1.55      cgd 	if (error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    145  1.55      cgd 	    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p))
    146  1.55      cgd 		goto bad2;
    147  1.55      cgd 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    148  1.55      cgd 
    149  1.55      cgd 	/*
    150  1.55      cgd 	 * set up the vmcmds for creation of the process
    151  1.55      cgd 	 * address space
    152  1.55      cgd 	 */
    153  1.55      cgd 	error = ENOEXEC;
    154  1.55      cgd 	for (i = 0; i < nexecs && error != 0; i++) {
    155  1.55      cgd 		if (execsw[i].es_check != NULL)
    156  1.55      cgd 			error = (*execsw[i].es_check)(p, epp);
    157  1.55      cgd 		if (epp->ep_flags & EXEC_DESTR && error != 0)
    158  1.55      cgd 			return error;
    159  1.55      cgd 	}
    160  1.55      cgd 	if (!error) {
    161  1.55      cgd 		/* check that entry point is sane */
    162  1.55      cgd 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
    163  1.55      cgd 			error = ENOEXEC;
    164  1.55      cgd 
    165  1.55      cgd 		/* check limits */
    166  1.55      cgd 		if ((epp->ep_tsize > MAXTSIZ) ||
    167  1.55      cgd 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
    168  1.55      cgd 			error = ENOMEM;
    169  1.55      cgd 
    170  1.55      cgd 		if (!error)
    171  1.55      cgd 			return (0);
    172  1.55      cgd 	}
    173  1.55      cgd 
    174  1.55      cgd 	/*
    175  1.55      cgd 	 * free any vmspace-creation commands,
    176  1.55      cgd 	 * and release their references
    177  1.55      cgd 	 */
    178  1.55      cgd 	kill_vmcmds(&epp->ep_vmcmds);
    179  1.55      cgd 
    180  1.55      cgd bad2:
    181  1.55      cgd 	/*
    182  1.55      cgd 	 * unlock and close the vnode, restore the old one, free the
    183  1.55      cgd 	 * pathname buf, and punt.
    184  1.55      cgd 	 */
    185  1.55      cgd 	VOP_UNLOCK(vp);
    186  1.55      cgd 	vn_close(vp, FREAD, p->p_ucred, p);
    187  1.55      cgd 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
    188  1.55      cgd 	return error;
    189  1.55      cgd 
    190  1.55      cgd bad1:
    191  1.55      cgd 	/*
    192  1.55      cgd 	 * free the namei pathname buffer, and put the vnode
    193  1.55      cgd 	 * (which we don't yet have open).
    194  1.55      cgd 	 */
    195  1.55      cgd 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
    196  1.55      cgd 	vput(vp);
    197  1.55      cgd 	return error;
    198  1.55      cgd }
    199  1.55      cgd 
    200  1.55      cgd /*
    201  1.55      cgd  * exec system call
    202  1.55      cgd  */
    203  1.55      cgd /* ARGSUSED */
    204  1.55      cgd execve(p, uap, retval)
    205  1.55      cgd 	register struct proc *p;
    206  1.56      cgd 	register struct execve_args /* {
    207  1.56      cgd 		syscallarg(char *) path;
    208  1.56      cgd 		syscallarg(char * *) argp;
    209  1.56      cgd 		syscallarg(char * *) envp;
    210  1.56      cgd 	} */ *uap;
    211  1.56      cgd 	register_t *retval;
    212  1.55      cgd {
    213  1.55      cgd 	int error, i;
    214  1.55      cgd 	struct exec_package pack;
    215  1.55      cgd 	struct nameidata nid;
    216  1.55      cgd 	struct vattr attr;
    217  1.55      cgd 	struct ucred *cred = p->p_ucred;
    218  1.55      cgd 	char *argp;
    219  1.55      cgd 	char **cpp, *dp, *sp, *np;
    220  1.55      cgd 	int argc, envc, len;
    221  1.55      cgd 	char *stack;
    222  1.55      cgd 	struct ps_strings arginfo;
    223  1.55      cgd 	struct vmspace *vm = p->p_vmspace;
    224  1.55      cgd 	char **tmpfap;
    225  1.55      cgd 
    226  1.55      cgd 	/*
    227  1.55      cgd 	 * figure out the maximum size of an exec header, if necessary.
    228  1.55      cgd 	 * XXX should be able to keep LKM code from modifying exec switch
    229  1.55      cgd 	 * when we're still using it, but...
    230  1.55      cgd 	 */
    231  1.55      cgd 	if (exec_maxhdrsz == 0) {
    232  1.55      cgd 		for (i = 0; i < nexecs; i++)
    233  1.55      cgd 			if (execsw[i].es_check != NULL
    234  1.55      cgd 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
    235  1.55      cgd 				exec_maxhdrsz = execsw[i].es_hdrsz;
    236  1.55      cgd 	}
    237  1.55      cgd 
    238  1.55      cgd 	/* init the namei data to point the file user's program name */
    239  1.56      cgd 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    240  1.55      cgd 
    241  1.55      cgd 	/*
    242  1.55      cgd 	 * initialize the fields of the exec package.
    243  1.55      cgd 	 */
    244  1.56      cgd 	pack.ep_name = SCARG(uap, path);
    245  1.55      cgd 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
    246  1.55      cgd 	pack.ep_hdrlen = exec_maxhdrsz;
    247  1.55      cgd 	pack.ep_hdrvalid = 0;
    248  1.55      cgd 	pack.ep_ndp = &nid;
    249  1.57  deraadt 	pack.ep_setup = NULL;		/* assume no setup function */
    250  1.57  deraadt 	pack.ep_setup_arg = NULL;
    251  1.57  deraadt 	pack.ep_setup_arglen = 0;
    252  1.55      cgd 	pack.ep_vmcmds.evs_cnt = 0;
    253  1.55      cgd 	pack.ep_vmcmds.evs_used = 0;
    254  1.55      cgd 	pack.ep_vap = &attr;
    255  1.55      cgd 	pack.ep_emul = EMUL_NETBSD;
    256  1.55      cgd 	pack.ep_flags = 0;
    257  1.55      cgd 
    258  1.55      cgd 	/* see if we can run it. */
    259  1.55      cgd 	if (error = check_exec(p, &pack))
    260  1.55      cgd 		goto freehdr;
    261  1.55      cgd 
    262  1.55      cgd 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    263  1.55      cgd 
    264  1.55      cgd 	/* allocate an argument buffer */
    265  1.55      cgd 	argp = (char *) kmem_alloc_wait(exec_map, NCARGS);
    266  1.55      cgd #ifdef DIAGNOSTIC
    267  1.55      cgd 	if (argp == (vm_offset_t) 0)
    268  1.55      cgd 		panic("execve: argp == NULL");
    269  1.55      cgd #endif
    270  1.55      cgd 	dp = argp;
    271  1.55      cgd 	argc = 0;
    272  1.55      cgd 
    273  1.55      cgd 	/* copy the fake args list, if there's one, freeing it as we go */
    274  1.55      cgd 	if (pack.ep_flags & EXEC_HASARGL) {
    275  1.55      cgd 		tmpfap = pack.ep_fa;
    276  1.55      cgd 		while (*tmpfap != NULL) {
    277  1.55      cgd 			char *cp;
    278  1.55      cgd 
    279  1.55      cgd 			cp = *tmpfap;
    280  1.55      cgd 			while (*cp)
    281  1.55      cgd 				*dp++ = *cp++;
    282  1.55      cgd 			*dp++;
    283  1.55      cgd 
    284  1.55      cgd 			FREE(*tmpfap, M_EXEC);
    285  1.55      cgd 			tmpfap++; argc++;
    286  1.55      cgd 		}
    287  1.55      cgd 		FREE(pack.ep_fa, M_EXEC);
    288  1.55      cgd 		pack.ep_flags &= ~EXEC_HASARGL;
    289  1.55      cgd 	}
    290  1.55      cgd 
    291  1.55      cgd 	/* Now get argv & environment */
    292  1.56      cgd 	if (!(cpp = SCARG(uap, argp))) {
    293  1.55      cgd 		error = EINVAL;
    294  1.55      cgd 		goto bad;
    295  1.55      cgd 	}
    296  1.55      cgd 
    297  1.55      cgd 	if (pack.ep_flags & EXEC_SKIPARG)
    298  1.55      cgd 		cpp++;
    299  1.55      cgd 
    300  1.55      cgd 	while (1) {
    301  1.55      cgd 		len = argp + ARG_MAX - dp;
    302  1.55      cgd 		if (error = copyin(cpp, &sp, sizeof(sp)))
    303  1.55      cgd 			goto bad;
    304  1.55      cgd 		if (!sp)
    305  1.55      cgd 			break;
    306  1.55      cgd 		if (error = copyinstr(sp, dp, len, (u_int *) & len)) {
    307  1.55      cgd 			if (error == ENAMETOOLONG)
    308  1.55      cgd 				error = E2BIG;
    309  1.55      cgd 			goto bad;
    310  1.55      cgd 		}
    311  1.55      cgd 		dp += len;
    312  1.55      cgd 		cpp++;
    313  1.55      cgd 		argc++;
    314  1.55      cgd 	}
    315  1.55      cgd 
    316  1.55      cgd 	envc = 0;
    317  1.56      cgd 	if (cpp = SCARG(uap, envp)) {	/* environment need not be there */
    318  1.55      cgd 		while (1) {
    319  1.55      cgd 			len = argp + ARG_MAX - dp;
    320  1.55      cgd 			if (error = copyin(cpp, &sp, sizeof(sp)))
    321  1.55      cgd 				goto bad;
    322  1.55      cgd 			if (!sp)
    323  1.55      cgd 				break;
    324  1.55      cgd 			if (error = copyinstr(sp, dp, len, (u_int *) & len)) {
    325  1.55      cgd 				if (error == ENAMETOOLONG)
    326  1.55      cgd 					error = E2BIG;
    327  1.55      cgd 				goto bad;
    328  1.55      cgd 			}
    329  1.55      cgd 			dp += len;
    330  1.55      cgd 			cpp++;
    331  1.55      cgd 			envc++;
    332  1.55      cgd 		}
    333  1.55      cgd 	}
    334  1.61  mycroft 
    335  1.61  mycroft 	dp = (char *) ALIGN(dp);
    336  1.55      cgd 
    337  1.55      cgd 	/* Now check if args & environ fit into new stack */
    338  1.57  deraadt 	len = ((argc + envc + 2 + pack.ep_setup_arglen) * sizeof(char *) +
    339  1.57  deraadt 	    sizeof(int) + dp + STACKGAPLEN + szsigcode +
    340  1.57  deraadt 	    sizeof(struct ps_strings)) - argp;
    341  1.55      cgd 	len = ALIGN(len);	/* make the stack "safely" aligned */
    342  1.55      cgd 
    343  1.55      cgd 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    344  1.55      cgd 		error = ENOMEM;
    345  1.55      cgd 		goto bad;
    346  1.55      cgd 	}
    347  1.55      cgd 
    348  1.55      cgd 	/* adjust "active stack depth" for process VSZ */
    349  1.55      cgd 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    350  1.55      cgd 
    351  1.55      cgd 	/* Unmap old program */
    352  1.55      cgd #ifdef sparc
    353  1.55      cgd 	kill_user_windows(p);		/* before stack addresses go away */
    354  1.55      cgd #endif
    355  1.55      cgd 	/* Kill shared memory and unmap old program */
    356  1.55      cgd #ifdef SYSVSHM
    357  1.55      cgd 	if (vm->vm_shm)
    358  1.55      cgd 		shmexit(p);
    359  1.55      cgd #endif
    360  1.55      cgd 	vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    361  1.55      cgd 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    362  1.55      cgd 
    363  1.55      cgd 	/* Now map address space */
    364  1.55      cgd 	vm->vm_taddr = (char *) pack.ep_taddr;
    365  1.55      cgd 	vm->vm_tsize = btoc(pack.ep_tsize);
    366  1.55      cgd 	vm->vm_daddr = (char *) pack.ep_daddr;
    367  1.55      cgd 	vm->vm_dsize = btoc(pack.ep_dsize);
    368  1.55      cgd 	vm->vm_ssize = btoc(pack.ep_ssize);
    369  1.55      cgd 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    370  1.55      cgd 
    371  1.55      cgd 	/* create the new process's VM space by running the vmcmds */
    372  1.55      cgd #ifdef DIAGNOSTIC
    373  1.55      cgd 	if (pack.ep_vmcmds.evs_used == 0)
    374  1.55      cgd 		panic("execve: no vmcmds");
    375  1.55      cgd #endif
    376  1.55      cgd 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    377  1.55      cgd 		struct exec_vmcmd *vcp;
    378  1.55      cgd 
    379  1.55      cgd 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    380  1.55      cgd 		error = (*vcp->ev_proc)(p, vcp);
    381  1.55      cgd 	}
    382  1.55      cgd 
    383  1.55      cgd 	/* free the vmspace-creation commands, and release their references */
    384  1.55      cgd 	kill_vmcmds(&pack.ep_vmcmds);
    385  1.55      cgd 
    386  1.55      cgd 	/* if an error happened, deallocate and punt */
    387  1.55      cgd 	if (error)
    388  1.55      cgd 		goto exec_abort;
    389  1.55      cgd 
    390  1.55      cgd 	/* remember information about the process */
    391  1.55      cgd 	arginfo.ps_nargvstr = argc;
    392  1.55      cgd 	arginfo.ps_nenvstr = envc;
    393  1.55      cgd 
    394  1.55      cgd 	/* Now copy argc, args & environ to new stack */
    395  1.55      cgd 	stack = (char *) (USRSTACK - len);
    396  1.55      cgd 	cpp = (char **) stack;
    397  1.55      cgd 
    398  1.55      cgd 	if (copyout(&argc, cpp++, sizeof(argc)))
    399  1.55      cgd 		goto exec_abort;
    400  1.57  deraadt 	dp = (char *) (cpp + argc + envc + 2 + pack.ep_setup_arglen);
    401  1.59  mycroft 	sp = argp;
    402  1.59  mycroft 	np = 0;
    403  1.55      cgd 
    404  1.55      cgd 	/* XXX don't copy them out, remap them! */
    405  1.55      cgd 	arginfo.ps_argvstr = dp; /* remember location of argv for later */
    406  1.59  mycroft 	for (; --argc >= 0; sp += len, dp += len) {
    407  1.60  mycroft 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    408  1.60  mycroft 		    copyoutstr(sp, dp, ARG_MAX, &len))
    409  1.55      cgd 			goto exec_abort;
    410  1.55      cgd 	}
    411  1.55      cgd 	if (copyout(&np, cpp++, sizeof(np)))
    412  1.55      cgd 		goto exec_abort;
    413  1.55      cgd 
    414  1.59  mycroft 	arginfo.ps_envstr = dp;	/* remember location of envp for later */
    415  1.55      cgd 	for (; --envc >= 0; sp += len, dp += len) {
    416  1.60  mycroft 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    417  1.60  mycroft 		    copyoutstr(sp, dp, ARG_MAX, &len))
    418  1.55      cgd 			goto exec_abort;
    419  1.55      cgd 	}
    420  1.57  deraadt 	if (copyout(&np, cpp++, sizeof(np)))
    421  1.55      cgd 		goto exec_abort;
    422  1.55      cgd 
    423  1.57  deraadt 	if (pack.ep_setup != NULL)
    424  1.57  deraadt 		(*pack.ep_setup)(EXEC_SETUP_ADDARGS, p, &pack, cpp);
    425  1.57  deraadt 
    426  1.55      cgd 	/* copy out the process's ps_strings structure */
    427  1.55      cgd 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
    428  1.55      cgd 		goto exec_abort;
    429  1.55      cgd 
    430  1.55      cgd #ifdef COPY_SIGCODE
    431  1.55      cgd 	/* copy out the process's signal trapoline code */
    432  1.55      cgd 	if (copyout((char *) sigcode, ((char *) PS_STRINGS) - szsigcode,
    433  1.55      cgd 		szsigcode)) {
    434  1.55      cgd 		goto exec_abort;
    435  1.55      cgd 	}
    436  1.55      cgd #endif
    437  1.55      cgd 
    438  1.55      cgd 	fdcloseexec(p);		/* handle close on exec */
    439  1.55      cgd 	execsigs(p);		/* reset catched signals */
    440  1.55      cgd 
    441  1.55      cgd 	/* set command name & other accounting info */
    442  1.55      cgd 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    443  1.55      cgd 	bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
    444  1.55      cgd 	p->p_comm[len] = 0;
    445  1.55      cgd 	p->p_acflag &= ~AFORK;
    446  1.55      cgd 
    447  1.55      cgd 	/* record proc's vnode, for use by procfs and others */
    448  1.55      cgd         if (p->p_textvp)
    449  1.55      cgd                 vrele(p->p_textvp);
    450  1.55      cgd 	VREF(pack.ep_vp);
    451  1.55      cgd 	p->p_textvp = pack.ep_vp;
    452  1.55      cgd 
    453  1.55      cgd 	p->p_flag |= P_EXEC;
    454  1.55      cgd 	if (p->p_flag & P_PPWAIT) {
    455  1.55      cgd 		p->p_flag &= ~P_PPWAIT;
    456  1.55      cgd 		wakeup((caddr_t) p->p_pptr);
    457  1.55      cgd 	}
    458  1.55      cgd 
    459  1.55      cgd 	/*
    460  1.55      cgd 	 * deal with set[ug]id.
    461  1.55      cgd 	 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id.
    462  1.55      cgd 	 */
    463  1.55      cgd 	p->p_flag &= ~P_SUGID;
    464  1.55      cgd 	if (((attr.va_mode & VSUID) != 0 &&
    465  1.55      cgd 	    p->p_ucred->cr_uid != attr.va_uid)
    466  1.55      cgd 	    || (attr.va_mode & VSGID) != 0 &&
    467  1.55      cgd 	    p->p_ucred->cr_gid != attr.va_gid) {
    468  1.55      cgd 		p->p_ucred = crcopy(cred);
    469  1.55      cgd #ifdef KTRACE
    470  1.55      cgd 		/*
    471  1.55      cgd 		 * If process is being ktraced, turn off - unless
    472  1.55      cgd 		 * root set it.
    473  1.55      cgd 		 */
    474  1.55      cgd 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
    475  1.55      cgd 			vrele(p->p_tracep);
    476  1.55      cgd 			p->p_tracep = NULL;
    477  1.55      cgd 			p->p_traceflag = 0;
    478  1.55      cgd 		}
    479  1.55      cgd #endif
    480  1.55      cgd 		if (attr.va_mode & VSUID)
    481  1.55      cgd 			p->p_ucred->cr_uid = attr.va_uid;
    482  1.55      cgd 		if (attr.va_mode & VSGID)
    483  1.55      cgd 			p->p_ucred->cr_gid = attr.va_gid;
    484  1.55      cgd 		p->p_flag |= P_SUGID;
    485  1.55      cgd 	}
    486  1.55      cgd 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    487  1.55      cgd 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    488  1.55      cgd 
    489  1.55      cgd 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    490  1.55      cgd 
    491  1.55      cgd 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    492  1.55      cgd 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    493  1.55      cgd 	vput(pack.ep_vp);
    494  1.55      cgd 
    495  1.55      cgd 	/* setup new registers and do misc. setup. */
    496  1.55      cgd 	setregs(p, pack.ep_entry, (u_long) stack, retval);
    497  1.55      cgd 	if (pack.ep_setup != NULL)
    498  1.57  deraadt 		 (*pack.ep_setup)(EXEC_SETUP_FINISH, p, &pack, NULL);
    499  1.55      cgd 
    500  1.55      cgd 	if (p->p_flag & P_TRACED)
    501  1.55      cgd 		psignal(p, SIGTRAP);
    502  1.55      cgd 
    503  1.55      cgd 	p->p_emul = pack.ep_emul;
    504  1.55      cgd 	FREE(pack.ep_hdr, M_EXEC);
    505  1.55      cgd 	return 0;
    506  1.55      cgd 
    507  1.55      cgd bad:
    508  1.57  deraadt 	if (pack.ep_setup != NULL)
    509  1.57  deraadt 		(*pack.ep_setup)(EXEC_SETUP_CLEANUP, p, &pack, dp);
    510  1.55      cgd 	/* free the vmspace-creation commands, and release their references */
    511  1.55      cgd 	kill_vmcmds(&pack.ep_vmcmds);
    512  1.55      cgd 	/* kill any opened file descriptor, if necessary */
    513  1.55      cgd 	if (pack.ep_flags & EXEC_HASFD) {
    514  1.55      cgd 		pack.ep_flags &= ~EXEC_HASFD;
    515  1.58  mycroft 		(void) fdclose(p, pack.ep_fd);
    516  1.55      cgd 	}
    517  1.55      cgd 	/* close and put the exec'd file */
    518  1.55      cgd 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    519  1.55      cgd 	vput(pack.ep_vp);
    520  1.55      cgd 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    521  1.55      cgd 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    522  1.55      cgd 
    523  1.55      cgd freehdr:
    524  1.55      cgd 	FREE(pack.ep_hdr, M_EXEC);
    525  1.55      cgd 	return error;
    526  1.55      cgd 
    527  1.55      cgd exec_abort:
    528  1.55      cgd 	/*
    529  1.55      cgd 	 * the old process doesn't exist anymore.  exit gracefully.
    530  1.55      cgd 	 * get rid of the (new) address space we have created, if any, get rid
    531  1.55      cgd 	 * of our namei data and vnode, and exit noting failure
    532  1.55      cgd 	 */
    533  1.55      cgd 	vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    534  1.55      cgd 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    535  1.55      cgd 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
    536  1.55      cgd 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    537  1.55      cgd 	vput(pack.ep_vp);
    538  1.55      cgd 	kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS);
    539  1.55      cgd 	FREE(pack.ep_hdr, M_EXEC);
    540  1.55      cgd 	exit1(p, W_EXITCODE(0, SIGABRT));
    541  1.55      cgd 	exit1(p, -1);
    542  1.55      cgd 
    543  1.55      cgd 	/* NOTREACHED */
    544  1.55      cgd 	return 0;
    545  1.55      cgd }
    546