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