Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.126
      1 /*	$NetBSD: kern_exec.c,v 1.126 2000/11/28 12:24:34 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
      5  * Copyright (C) 1992 Wolfgang Solfrank.
      6  * Copyright (C) 1992 TooLs GmbH.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include "opt_ktrace.h"
     36 #include "opt_syscall_debug.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/filedesc.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/mount.h>
     44 #include <sys/malloc.h>
     45 #include <sys/namei.h>
     46 #include <sys/vnode.h>
     47 #include <sys/file.h>
     48 #include <sys/acct.h>
     49 #include <sys/exec.h>
     50 #include <sys/ktrace.h>
     51 #include <sys/resourcevar.h>
     52 #include <sys/wait.h>
     53 #include <sys/mman.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/stat.h>
     56 #include <sys/syscall.h>
     57 
     58 #include <sys/syscallargs.h>
     59 
     60 #include <uvm/uvm_extern.h>
     61 
     62 #include <machine/cpu.h>
     63 #include <machine/reg.h>
     64 
     65 extern char sigcode[], esigcode[];
     66 #ifdef SYSCALL_DEBUG
     67 extern const char * const syscallnames[];
     68 #endif
     69 
     70 const struct emul emul_netbsd = {
     71 	"netbsd",
     72 	NULL,
     73 	sendsig,
     74 	SYS_syscall,
     75 	SYS_MAXSYSCALL,
     76 	sysent,
     77 #ifdef SYSCALL_DEBUG
     78 	syscallnames,
     79 #else
     80 	NULL,
     81 #endif
     82 	sigcode,
     83 	esigcode,
     84 };
     85 
     86 /*
     87  * check exec:
     88  * given an "executable" described in the exec package's namei info,
     89  * see what we can do with it.
     90  *
     91  * ON ENTRY:
     92  *	exec package with appropriate namei info
     93  *	proc pointer of exec'ing proc
     94  *	NO SELF-LOCKED VNODES
     95  *
     96  * ON EXIT:
     97  *	error:	nothing held, etc.  exec header still allocated.
     98  *	ok:	filled exec package, executable's vnode (unlocked).
     99  *
    100  * EXEC SWITCH ENTRY:
    101  * 	Locked vnode to check, exec package, proc.
    102  *
    103  * EXEC SWITCH EXIT:
    104  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
    105  *	error:	destructive:
    106  *			everything deallocated execept exec header.
    107  *		non-destructive:
    108  *			error code, executable's vnode (unlocked),
    109  *			exec header unmodified.
    110  */
    111 int
    112 check_exec(struct proc *p, struct exec_package *epp)
    113 {
    114 	int error, i;
    115 	struct vnode *vp;
    116 	struct nameidata *ndp;
    117 	size_t resid;
    118 
    119 	ndp = epp->ep_ndp;
    120 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    121 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
    122 	/* first get the vnode */
    123 	if ((error = namei(ndp)) != 0)
    124 		return error;
    125 	epp->ep_vp = vp = ndp->ni_vp;
    126 
    127 	/* check access and type */
    128 	if (vp->v_type != VREG) {
    129 		error = EACCES;
    130 		goto bad1;
    131 	}
    132 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
    133 		goto bad1;
    134 
    135 	/* get attributes */
    136 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
    137 		goto bad1;
    138 
    139 	/* Check mount point */
    140 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    141 		error = EACCES;
    142 		goto bad1;
    143 	}
    144 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
    145 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    146 
    147 	/* try to open it */
    148 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
    149 		goto bad1;
    150 
    151 	/* unlock vp, since we need it unlocked from here on out. */
    152 	VOP_UNLOCK(vp, 0);
    153 
    154 	/* now we have the file, get the exec header */
    155 	uvn_attach(vp, VM_PROT_READ);
    156 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    157 			UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
    158 	if (error)
    159 		goto bad2;
    160 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    161 
    162 	/*
    163 	 * set up the vmcmds for creation of the process
    164 	 * address space
    165 	 */
    166 	error = ENOEXEC;
    167 	for (i = 0; i < nexecs && error != 0; i++) {
    168 		int newerror;
    169 
    170 		if (execsw[i].es_check == NULL)
    171 			continue;
    172 
    173 		epp->ep_esch = &execsw[i];
    174 		newerror = (*execsw[i].es_check)(p, epp);
    175 		/* make sure the first "interesting" error code is saved. */
    176 		if (!newerror || error == ENOEXEC)
    177 			error = newerror;
    178 
    179 		/* if es_check call was successful, update epp->ep_es */
    180 		if (!newerror && (epp->ep_flags & EXEC_HASES) == 0)
    181 			epp->ep_es = &execsw[i];
    182 
    183 		if (epp->ep_flags & EXEC_DESTR && error != 0)
    184 			return error;
    185 	}
    186 	if (!error) {
    187 		/* check that entry point is sane */
    188 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
    189 			error = ENOEXEC;
    190 
    191 		/* check limits */
    192 		if ((epp->ep_tsize > MAXTSIZ) ||
    193 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
    194 			error = ENOMEM;
    195 
    196 		if (!error)
    197 			return (0);
    198 	}
    199 
    200 	/*
    201 	 * free any vmspace-creation commands,
    202 	 * and release their references
    203 	 */
    204 	kill_vmcmds(&epp->ep_vmcmds);
    205 
    206 bad2:
    207 	/*
    208 	 * close and release the vnode, restore the old one, free the
    209 	 * pathname buf, and punt.
    210 	 */
    211 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    212 	VOP_CLOSE(vp, FREAD, p->p_ucred, p);
    213 	vput(vp);
    214 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    215 	return error;
    216 
    217 bad1:
    218 	/*
    219 	 * free the namei pathname buffer, and put the vnode
    220 	 * (which we don't yet have open).
    221 	 */
    222 	vput(vp);				/* was still locked */
    223 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    224 	return error;
    225 }
    226 
    227 /*
    228  * exec system call
    229  */
    230 /* ARGSUSED */
    231 int
    232 sys_execve(struct proc *p, void *v, register_t *retval)
    233 {
    234 	struct sys_execve_args /* {
    235 		syscallarg(const char *) path;
    236 		syscallarg(char * const *) argp;
    237 		syscallarg(char * const *) envp;
    238 	} */ *uap = v;
    239 	int error, i;
    240 	struct exec_package pack;
    241 	struct nameidata nid;
    242 	struct vattr attr;
    243 	struct ucred *cred = p->p_ucred;
    244 	char *argp;
    245 	char * const *cpp;
    246 	char *dp, *sp;
    247 	long argc, envc;
    248 	size_t len;
    249 	char *stack;
    250 	struct ps_strings arginfo;
    251 	struct vmspace *vm;
    252 	char **tmpfap;
    253 	int szsigcode;
    254 	struct exec_vmcmd *base_vcp = NULL;
    255 
    256 	/*
    257 	 * figure out the maximum size of an exec header, if necessary.
    258 	 * XXX should be able to keep LKM code from modifying exec switch
    259 	 * when we're still using it, but...
    260 	 */
    261 	if (exec_maxhdrsz == 0) {
    262 		for (i = 0; i < nexecs; i++)
    263 			if (execsw[i].es_check != NULL
    264 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
    265 				exec_maxhdrsz = execsw[i].es_hdrsz;
    266 	}
    267 
    268 	/* init the namei data to point the file user's program name */
    269 	/* XXX cgd 960926: why do this here?  most will be clobbered. */
    270 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    271 
    272 	/*
    273 	 * initialize the fields of the exec package.
    274 	 */
    275 	pack.ep_name = SCARG(uap, path);
    276 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
    277 	pack.ep_hdrlen = exec_maxhdrsz;
    278 	pack.ep_hdrvalid = 0;
    279 	pack.ep_ndp = &nid;
    280 	pack.ep_emul_arg = NULL;
    281 	pack.ep_vmcmds.evs_cnt = 0;
    282 	pack.ep_vmcmds.evs_used = 0;
    283 	pack.ep_vap = &attr;
    284 	pack.ep_flags = 0;
    285 
    286 	/* see if we can run it. */
    287 	if ((error = check_exec(p, &pack)) != 0)
    288 		goto freehdr;
    289 
    290 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    291 
    292 	/* allocate an argument buffer */
    293 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
    294 #ifdef DIAGNOSTIC
    295 	if (argp == (vaddr_t) 0)
    296 		panic("execve: argp == NULL");
    297 #endif
    298 	dp = argp;
    299 	argc = 0;
    300 
    301 	/* copy the fake args list, if there's one, freeing it as we go */
    302 	if (pack.ep_flags & EXEC_HASARGL) {
    303 		tmpfap = pack.ep_fa;
    304 		while (*tmpfap != NULL) {
    305 			char *cp;
    306 
    307 			cp = *tmpfap;
    308 			while (*cp)
    309 				*dp++ = *cp++;
    310 			dp++;
    311 
    312 			FREE(*tmpfap, M_EXEC);
    313 			tmpfap++; argc++;
    314 		}
    315 		FREE(pack.ep_fa, M_EXEC);
    316 		pack.ep_flags &= ~EXEC_HASARGL;
    317 	}
    318 
    319 	/* Now get argv & environment */
    320 	if (!(cpp = SCARG(uap, argp))) {
    321 		error = EINVAL;
    322 		goto bad;
    323 	}
    324 
    325 	if (pack.ep_flags & EXEC_SKIPARG)
    326 		cpp++;
    327 
    328 	while (1) {
    329 		len = argp + ARG_MAX - dp;
    330 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    331 			goto bad;
    332 		if (!sp)
    333 			break;
    334 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    335 			if (error == ENAMETOOLONG)
    336 				error = E2BIG;
    337 			goto bad;
    338 		}
    339 		dp += len;
    340 		cpp++;
    341 		argc++;
    342 	}
    343 
    344 	envc = 0;
    345 	/* environment need not be there */
    346 	if ((cpp = SCARG(uap, envp)) != NULL ) {
    347 		while (1) {
    348 			len = argp + ARG_MAX - dp;
    349 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    350 				goto bad;
    351 			if (!sp)
    352 				break;
    353 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    354 				if (error == ENAMETOOLONG)
    355 					error = E2BIG;
    356 				goto bad;
    357 			}
    358 			dp += len;
    359 			cpp++;
    360 			envc++;
    361 		}
    362 	}
    363 
    364 	dp = (char *) ALIGN(dp);
    365 
    366 	szsigcode = pack.ep_es->es_emul->e_esigcode -
    367 	    pack.ep_es->es_emul->e_sigcode;
    368 
    369 	/* Now check if args & environ fit into new stack */
    370 	if (pack.ep_flags & EXEC_32)
    371 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    372 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
    373 		    szsigcode + sizeof(struct ps_strings)) - argp;
    374 	else
    375 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    376 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
    377 		    szsigcode + sizeof(struct ps_strings)) - argp;
    378 
    379 	len = ALIGN(len);	/* make the stack "safely" aligned */
    380 
    381 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    382 		error = ENOMEM;
    383 		goto bad;
    384 	}
    385 
    386 	/* adjust "active stack depth" for process VSZ */
    387 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    388 
    389 	/*
    390 	 * Do whatever is necessary to prepare the address space
    391 	 * for remapping.  Note that this might replace the current
    392 	 * vmspace with another!
    393 	 */
    394 	uvmspace_exec(p);
    395 
    396 	/* Now map address space */
    397 	vm = p->p_vmspace;
    398 	vm->vm_taddr = (char *) pack.ep_taddr;
    399 	vm->vm_tsize = btoc(pack.ep_tsize);
    400 	vm->vm_daddr = (char *) pack.ep_daddr;
    401 	vm->vm_dsize = btoc(pack.ep_dsize);
    402 	vm->vm_ssize = btoc(pack.ep_ssize);
    403 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    404 	vm->vm_minsaddr = (char *) pack.ep_minsaddr;
    405 
    406 	/* create the new process's VM space by running the vmcmds */
    407 #ifdef DIAGNOSTIC
    408 	if (pack.ep_vmcmds.evs_used == 0)
    409 		panic("execve: no vmcmds");
    410 #endif
    411 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    412 		struct exec_vmcmd *vcp;
    413 
    414 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    415 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    416 #ifdef DIAGNOSTIC
    417 			if (base_vcp == NULL)
    418 				panic("execve: relative vmcmd with no base");
    419 			if (vcp->ev_flags & VMCMD_BASE)
    420 				panic("execve: illegal base & relative vmcmd");
    421 #endif
    422 			vcp->ev_addr += base_vcp->ev_addr;
    423 		}
    424 		error = (*vcp->ev_proc)(p, vcp);
    425 #ifdef DEBUG
    426 		if (error) {
    427 			if (i > 0)
    428 				printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", i-1,
    429 				       vcp[-1].ev_addr, vcp[-1].ev_len,
    430 				       vcp[-1].ev_offset);
    431 			printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", i,
    432 			       vcp->ev_addr, vcp->ev_len, vcp->ev_offset);
    433 		}
    434 #endif
    435 		if (vcp->ev_flags & VMCMD_BASE)
    436 			base_vcp = vcp;
    437 	}
    438 
    439 	/* free the vmspace-creation commands, and release their references */
    440 	kill_vmcmds(&pack.ep_vmcmds);
    441 
    442 	/* if an error happened, deallocate and punt */
    443 	if (error) {
    444 #ifdef DEBUG
    445 		printf("execve: vmcmd %i failed: %d\n", i-1, error);
    446 #endif
    447 		goto exec_abort;
    448 	}
    449 
    450 	/* remember information about the process */
    451 	arginfo.ps_nargvstr = argc;
    452 	arginfo.ps_nenvstr = envc;
    453 
    454 	stack = (char *) (vm->vm_minsaddr - len);
    455 	/* Now copy argc, args & environ to new stack */
    456 	if (!(*pack.ep_es->es_copyargs)(&pack, &arginfo, stack, argp)) {
    457 #ifdef DEBUG
    458 		printf("execve: copyargs failed\n");
    459 #endif
    460 		goto exec_abort;
    461 	}
    462 
    463 	/* fill process ps_strings info */
    464 	p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr
    465 		- sizeof(struct ps_strings));
    466 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    467 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    468 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    469 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    470 
    471 	/* copy out the process's ps_strings structure */
    472 	if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
    473 #ifdef DEBUG
    474 		printf("execve: ps_strings copyout failed\n");
    475 #endif
    476 		goto exec_abort;
    477 	}
    478 
    479 	/* copy out the process's signal trapoline code */
    480 	if (szsigcode) {
    481 		if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
    482 		    p->p_sigacts->ps_sigcode = (char *)p->p_psstr - szsigcode,
    483 		    szsigcode)) {
    484 #ifdef DEBUG
    485 			printf("execve: sig trampoline copyout failed\n");
    486 #endif
    487 			goto exec_abort;
    488 		}
    489 #ifdef PMAP_NEED_PROCWR
    490 		/* This is code. Let the pmap do what is needed. */
    491 		pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
    492 #endif
    493 	}
    494 
    495 	stopprofclock(p);	/* stop profiling */
    496 	fdcloseexec(p);		/* handle close on exec */
    497 	execsigs(p);		/* reset catched signals */
    498 	p->p_ctxlink = NULL;	/* reset ucontext link */
    499 
    500 	/* set command name & other accounting info */
    501 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    502 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
    503 	p->p_comm[len] = 0;
    504 	p->p_acflag &= ~AFORK;
    505 
    506 	/* record proc's vnode, for use by procfs and others */
    507         if (p->p_textvp)
    508                 vrele(p->p_textvp);
    509 	VREF(pack.ep_vp);
    510 	p->p_textvp = pack.ep_vp;
    511 
    512 	p->p_flag |= P_EXEC;
    513 	if (p->p_flag & P_PPWAIT) {
    514 		p->p_flag &= ~P_PPWAIT;
    515 		wakeup((caddr_t) p->p_pptr);
    516 	}
    517 
    518 	/*
    519 	 * deal with set[ug]id.
    520 	 * MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
    521 	 */
    522 	if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
    523 	 || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
    524 		p->p_ucred = crcopy(cred);
    525 #ifdef KTRACE
    526 		/*
    527 		 * If process is being ktraced, turn off - unless
    528 		 * root set it.
    529 		 */
    530 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
    531 			ktrderef(p);
    532 #endif
    533 		if (attr.va_mode & S_ISUID)
    534 			p->p_ucred->cr_uid = attr.va_uid;
    535 		if (attr.va_mode & S_ISGID)
    536 			p->p_ucred->cr_gid = attr.va_gid;
    537 		p_sugid(p);
    538 	} else
    539 		p->p_flag &= ~P_SUGID;
    540 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    541 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    542 
    543 	doexechooks(p);
    544 
    545 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    546 
    547 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    548 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    549 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    550 	vput(pack.ep_vp);
    551 
    552 	/* setup new registers and do misc. setup. */
    553 	(*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
    554 
    555 	if (p->p_flag & P_TRACED)
    556 		psignal(p, SIGTRAP);
    557 
    558 	free(pack.ep_hdr, M_EXEC);
    559 
    560 	/*
    561 	 * Call emulation specific exec hook. This can setup setup per-process
    562 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    563 	 *
    564 	 * If we are executing process of different emulation than the
    565 	 * original forked process, call e_proc_exit() of the old emulation
    566 	 * first, then e_proc_exec() of new emulation. If the emulation is
    567 	 * same, the exec hook code should deallocate any old emulation
    568 	 * resources held previously by this process.
    569 	 */
    570 	if (p->p_emul && p->p_emul->e_proc_exit
    571 	    && p->p_emul != pack.ep_es->es_emul)
    572 		(*p->p_emul->e_proc_exit)(p);
    573 
    574 	/*
    575 	 * Call exec hook. Emulation code may NOT store reference to anything
    576 	 * from &pack.
    577 	 */
    578         if (pack.ep_es->es_emul->e_proc_exec)
    579                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
    580 
    581 	/* update p_emul, the old value is no longer needed */
    582 	p->p_emul = pack.ep_es->es_emul;
    583 
    584 #ifdef KTRACE
    585 	if (KTRPOINT(p, KTR_EMUL))
    586 		ktremul(p);
    587 #endif
    588 
    589 	return (EJUSTRETURN);
    590 
    591 bad:
    592 	/* free the vmspace-creation commands, and release their references */
    593 	kill_vmcmds(&pack.ep_vmcmds);
    594 	/* kill any opened file descriptor, if necessary */
    595 	if (pack.ep_flags & EXEC_HASFD) {
    596 		pack.ep_flags &= ~EXEC_HASFD;
    597 		(void) fdrelease(p, pack.ep_fd);
    598 	}
    599 	/* close and put the exec'd file */
    600 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    601 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    602 	vput(pack.ep_vp);
    603 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    604 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    605 
    606 freehdr:
    607 	free(pack.ep_hdr, M_EXEC);
    608 	return error;
    609 
    610 exec_abort:
    611 	/*
    612 	 * the old process doesn't exist anymore.  exit gracefully.
    613 	 * get rid of the (new) address space we have created, if any, get rid
    614 	 * of our namei data and vnode, and exit noting failure
    615 	 */
    616 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    617 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    618 	if (pack.ep_emul_arg)
    619 		FREE(pack.ep_emul_arg, M_TEMP);
    620 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    621 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    622 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    623 	vput(pack.ep_vp);
    624 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    625 	free(pack.ep_hdr, M_EXEC);
    626 	exit1(p, W_EXITCODE(0, SIGABRT));
    627 	exit1(p, -1);
    628 
    629 	/* NOTREACHED */
    630 	return 0;
    631 }
    632 
    633 
    634 void *
    635 copyargs(struct exec_package *pack, struct ps_strings *arginfo,
    636     void *stack, void *argp)
    637 {
    638 	char **cpp = stack;
    639 	char *dp, *sp;
    640 	size_t len;
    641 	void *nullp = NULL;
    642 	long argc = arginfo->ps_nargvstr;
    643 	long envc = arginfo->ps_nenvstr;
    644 
    645 #ifdef __sparc_v9__
    646 	/* XXX Temporary hack for argc format conversion. */
    647 	argc <<= 32;
    648 #endif
    649 	if (copyout(&argc, cpp++, sizeof(argc)))
    650 		return NULL;
    651 #ifdef __sparc_v9__
    652 	/* XXX Temporary hack for argc format conversion. */
    653 	argc >>= 32;
    654 #endif
    655 
    656 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
    657 	sp = argp;
    658 
    659 	/* XXX don't copy them out, remap them! */
    660 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    661 
    662 	for (; --argc >= 0; sp += len, dp += len)
    663 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    664 		    copyoutstr(sp, dp, ARG_MAX, &len))
    665 			return NULL;
    666 
    667 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    668 		return NULL;
    669 
    670 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    671 
    672 	for (; --envc >= 0; sp += len, dp += len)
    673 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    674 		    copyoutstr(sp, dp, ARG_MAX, &len))
    675 			return NULL;
    676 
    677 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    678 		return NULL;
    679 
    680 	return cpp;
    681 }
    682