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