Annotation of src/sys/kern/kern_exec.c, Revision 1.127
1.127 ! jdolecek 1: /* $NetBSD: kern_exec.c,v 1.126 2000/11/28 12:24:34 mrg Exp $ */
1.55 cgd 2:
3: /*-
1.77 cgd 4: * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
1.55 cgd 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: */
1.89 mrg 34:
1.92 thorpej 35: #include "opt_ktrace.h"
1.124 jdolecek 36: #include "opt_syscall_debug.h"
1.55 cgd 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>
1.124 jdolecek 56: #include <sys/syscall.h>
1.55 cgd 57:
1.56 cgd 58: #include <sys/syscallargs.h>
1.55 cgd 59:
1.88 mrg 60: #include <uvm/uvm_extern.h>
61:
1.55 cgd 62: #include <machine/cpu.h>
63: #include <machine/reg.h>
64:
1.124 jdolecek 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",
1.127 ! jdolecek 72: NULL, /* emulation path */
1.124 jdolecek 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:
1.55 cgd 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.
1.77 cgd 99: * ok: filled exec package, executable's vnode (unlocked).
1.55 cgd 100: *
101: * EXEC SWITCH ENTRY:
102: * Locked vnode to check, exec package, proc.
103: *
104: * EXEC SWITCH EXIT:
1.77 cgd 105: * ok: return 0, filled exec package, executable's vnode (unlocked).
1.55 cgd 106: * error: destructive:
107: * everything deallocated execept exec header.
1.76 cgd 108: * non-destructive:
1.77 cgd 109: * error code, executable's vnode (unlocked),
1.76 cgd 110: * exec header unmodified.
1.55 cgd 111: */
112: int
1.118 thorpej 113: check_exec(struct proc *p, struct exec_package *epp)
1.55 cgd 114: {
115: int error, i;
116: struct vnode *vp;
117: struct nameidata *ndp;
1.93 thorpej 118: size_t resid;
1.55 cgd 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 */
1.74 christos 124: if ((error = namei(ndp)) != 0)
1.55 cgd 125: return error;
126: epp->ep_vp = vp = ndp->ni_vp;
127:
1.84 mycroft 128: /* check access and type */
1.55 cgd 129: if (vp->v_type != VREG) {
1.81 kleink 130: error = EACCES;
1.55 cgd 131: goto bad1;
132: }
1.84 mycroft 133: if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
134: goto bad1;
1.55 cgd 135:
136: /* get attributes */
1.74 christos 137: if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
1.55 cgd 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))
1.83 mycroft 146: epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
1.55 cgd 147:
148: /* try to open it */
1.74 christos 149: if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
1.55 cgd 150: goto bad1;
151:
1.99 wrstuden 152: /* unlock vp, since we need it unlocked from here on out. */
1.90 fvdl 153: VOP_UNLOCK(vp, 0);
1.77 cgd 154:
1.55 cgd 155: /* now we have the file, get the exec header */
1.125 chs 156: uvn_attach(vp, VM_PROT_READ);
1.74 christos 157: error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
1.77 cgd 158: UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
1.74 christos 159: if (error)
1.55 cgd 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++) {
1.68 cgd 169: int newerror;
170:
171: if (execsw[i].es_check == NULL)
172: continue;
173:
1.124 jdolecek 174: epp->ep_esch = &execsw[i];
1.68 cgd 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;
1.124 jdolecek 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:
1.55 cgd 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: /*
1.99 wrstuden 209: * close and release the vnode, restore the old one, free the
1.55 cgd 210: * pathname buf, and punt.
211: */
1.99 wrstuden 212: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.77 cgd 213: VOP_CLOSE(vp, FREAD, p->p_ucred, p);
1.99 wrstuden 214: vput(vp);
1.120 thorpej 215: PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
1.55 cgd 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: */
1.77 cgd 223: vput(vp); /* was still locked */
1.120 thorpej 224: PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
1.55 cgd 225: return error;
226: }
227:
228: /*
229: * exec system call
230: */
231: /* ARGSUSED */
1.75 christos 232: int
1.118 thorpej 233: sys_execve(struct proc *p, void *v, register_t *retval)
1.71 thorpej 234: {
1.108 augustss 235: struct sys_execve_args /* {
1.78 cgd 236: syscallarg(const char *) path;
237: syscallarg(char * const *) argp;
238: syscallarg(char * const *) envp;
1.71 thorpej 239: } */ *uap = v;
1.55 cgd 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;
1.78 cgd 246: char * const *cpp;
247: char *dp, *sp;
1.63 cgd 248: long argc, envc;
1.64 mycroft 249: size_t len;
1.55 cgd 250: char *stack;
251: struct ps_strings arginfo;
1.86 thorpej 252: struct vmspace *vm;
1.55 cgd 253: char **tmpfap;
1.65 fvdl 254: int szsigcode;
1.114 matt 255: struct exec_vmcmd *base_vcp = NULL;
1.55 cgd 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 */
1.77 cgd 270: /* XXX cgd 960926: why do this here? most will be clobbered. */
1.56 cgd 271: NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1.55 cgd 272:
273: /*
274: * initialize the fields of the exec package.
275: */
1.56 cgd 276: pack.ep_name = SCARG(uap, path);
1.119 thorpej 277: pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
1.55 cgd 278: pack.ep_hdrlen = exec_maxhdrsz;
279: pack.ep_hdrvalid = 0;
280: pack.ep_ndp = &nid;
1.67 christos 281: pack.ep_emul_arg = NULL;
1.55 cgd 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. */
1.74 christos 288: if ((error = check_exec(p, &pack)) != 0)
1.55 cgd 289: goto freehdr;
290:
291: /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
292:
293: /* allocate an argument buffer */
1.88 mrg 294: argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
1.55 cgd 295: #ifdef DIAGNOSTIC
1.95 eeh 296: if (argp == (vaddr_t) 0)
1.55 cgd 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++;
1.74 christos 311: dp++;
1.55 cgd 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 */
1.56 cgd 321: if (!(cpp = SCARG(uap, argp))) {
1.55 cgd 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;
1.74 christos 331: if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
1.55 cgd 332: goto bad;
333: if (!sp)
334: break;
1.74 christos 335: if ((error = copyinstr(sp, dp, len, &len)) != 0) {
1.55 cgd 336: if (error == ENAMETOOLONG)
337: error = E2BIG;
338: goto bad;
339: }
340: dp += len;
341: cpp++;
342: argc++;
343: }
344:
345: envc = 0;
1.74 christos 346: /* environment need not be there */
347: if ((cpp = SCARG(uap, envp)) != NULL ) {
1.55 cgd 348: while (1) {
349: len = argp + ARG_MAX - dp;
1.74 christos 350: if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
1.55 cgd 351: goto bad;
352: if (!sp)
353: break;
1.74 christos 354: if ((error = copyinstr(sp, dp, len, &len)) != 0) {
1.55 cgd 355: if (error == ENAMETOOLONG)
356: error = E2BIG;
357: goto bad;
358: }
359: dp += len;
360: cpp++;
361: envc++;
362: }
363: }
1.61 mycroft 364:
365: dp = (char *) ALIGN(dp);
1.55 cgd 366:
1.126 mrg 367: szsigcode = pack.ep_es->es_emul->e_esigcode -
368: pack.ep_es->es_emul->e_sigcode;
1.65 fvdl 369:
1.55 cgd 370: /* Now check if args & environ fit into new stack */
1.105 eeh 371: if (pack.ep_flags & EXEC_32)
1.126 mrg 372: len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
373: sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
374: szsigcode + sizeof(struct ps_strings)) - argp;
1.105 eeh 375: else
1.126 mrg 376: len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
377: sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
378: szsigcode + sizeof(struct ps_strings)) - argp;
1.67 christos 379:
1.55 cgd 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:
1.86 thorpej 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: */
1.88 mrg 395: uvmspace_exec(p);
1.55 cgd 396:
397: /* Now map address space */
1.86 thorpej 398: vm = p->p_vmspace;
1.55 cgd 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;
1.121 eeh 405: vm->vm_minsaddr = (char *) pack.ep_minsaddr;
1.55 cgd 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];
1.114 matt 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: }
1.55 cgd 425: error = (*vcp->ev_proc)(p, vcp);
1.111 matt 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
1.114 matt 436: if (vcp->ev_flags & VMCMD_BASE)
437: base_vcp = vcp;
1.55 cgd 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 */
1.111 matt 444: if (error) {
445: #ifdef DEBUG
446: printf("execve: vmcmd %i failed: %d\n", i-1, error);
447: #endif
1.55 cgd 448: goto exec_abort;
1.111 matt 449: }
1.55 cgd 450:
451: /* remember information about the process */
452: arginfo.ps_nargvstr = argc;
453: arginfo.ps_nenvstr = envc;
454:
1.121 eeh 455: stack = (char *) (vm->vm_minsaddr - len);
1.55 cgd 456: /* Now copy argc, args & environ to new stack */
1.124 jdolecek 457: if (!(*pack.ep_es->es_copyargs)(&pack, &arginfo, stack, argp)) {
1.111 matt 458: #ifdef DEBUG
459: printf("execve: copyargs failed\n");
460: #endif
1.55 cgd 461: goto exec_abort;
1.111 matt 462: }
1.55 cgd 463:
1.121 eeh 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:
1.55 cgd 472: /* copy out the process's ps_strings structure */
1.121 eeh 473: if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
1.111 matt 474: #ifdef DEBUG
475: printf("execve: ps_strings copyout failed\n");
476: #endif
1.55 cgd 477: goto exec_abort;
1.111 matt 478: }
1.109 simonb 479:
1.55 cgd 480: /* copy out the process's signal trapoline code */
1.96 mycroft 481: if (szsigcode) {
1.124 jdolecek 482: if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
1.121 eeh 483: p->p_sigacts->ps_sigcode = (char *)p->p_psstr - szsigcode,
1.111 matt 484: szsigcode)) {
485: #ifdef DEBUG
486: printf("execve: sig trampoline copyout failed\n");
487: #endif
1.97 kleink 488: goto exec_abort;
1.111 matt 489: }
1.104 is 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
1.96 mycroft 494: }
1.55 cgd 495:
1.102 ross 496: stopprofclock(p); /* stop profiling */
1.55 cgd 497: fdcloseexec(p); /* handle close on exec */
498: execsigs(p); /* reset catched signals */
1.98 kleink 499: p->p_ctxlink = NULL; /* reset ucontext link */
1.55 cgd 500:
501: /* set command name & other accounting info */
502: len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
1.94 perry 503: memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
1.55 cgd 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.
1.101 cgd 521: * MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
1.55 cgd 522: */
1.83 mycroft 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)){
1.55 cgd 525: p->p_ucred = crcopy(cred);
526: #ifdef KTRACE
527: /*
528: * If process is being ktraced, turn off - unless
529: * root set it.
530: */
1.91 christos 531: if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
532: ktrderef(p);
1.55 cgd 533: #endif
1.83 mycroft 534: if (attr.va_mode & S_ISUID)
1.55 cgd 535: p->p_ucred->cr_uid = attr.va_uid;
1.83 mycroft 536: if (attr.va_mode & S_ISGID)
1.55 cgd 537: p->p_ucred->cr_gid = attr.va_gid;
1.103 bouyer 538: p_sugid(p);
1.79 mrg 539: } else
540: p->p_flag &= ~P_SUGID;
1.55 cgd 541: p->p_cred->p_svuid = p->p_ucred->cr_uid;
542: p->p_cred->p_svgid = p->p_ucred->cr_gid;
1.107 fvdl 543:
544: doexechooks(p);
1.55 cgd 545:
1.95 eeh 546: uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
1.55 cgd 547:
1.120 thorpej 548: PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1.99 wrstuden 549: vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
1.55 cgd 550: VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
1.99 wrstuden 551: vput(pack.ep_vp);
1.55 cgd 552:
553: /* setup new registers and do misc. setup. */
1.124 jdolecek 554: (*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
1.55 cgd 555:
556: if (p->p_flag & P_TRACED)
557: psignal(p, SIGTRAP);
558:
1.122 jdolecek 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: */
1.124 jdolecek 571: if (p->p_emul && p->p_emul->e_proc_exit
572: && p->p_emul != pack.ep_es->es_emul)
1.122 jdolecek 573: (*p->p_emul->e_proc_exit)(p);
574:
1.123 jdolecek 575: /*
576: * Call exec hook. Emulation code may NOT store reference to anything
577: * from &pack.
578: */
1.124 jdolecek 579: if (pack.ep_es->es_emul->e_proc_exec)
580: (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
1.122 jdolecek 581:
582: /* update p_emul, the old value is no longer needed */
1.124 jdolecek 583: p->p_emul = pack.ep_es->es_emul;
1.70 christos 584:
585: #ifdef KTRACE
586: if (KTRPOINT(p, KTR_EMUL))
1.110 sommerfe 587: ktremul(p);
1.70 christos 588: #endif
1.85 mycroft 589:
590: return (EJUSTRETURN);
1.55 cgd 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;
1.66 mycroft 598: (void) fdrelease(p, pack.ep_fd);
1.55 cgd 599: }
600: /* close and put the exec'd file */
1.99 wrstuden 601: vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
1.55 cgd 602: VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
1.99 wrstuden 603: vput(pack.ep_vp);
1.120 thorpej 604: PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1.95 eeh 605: uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
1.55 cgd 606:
607: freehdr:
1.119 thorpej 608: free(pack.ep_hdr, M_EXEC);
1.55 cgd 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: */
1.88 mrg 617: uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
618: VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
1.73 mycroft 619: if (pack.ep_emul_arg)
1.124 jdolecek 620: FREE(pack.ep_emul_arg, M_TEMP);
1.120 thorpej 621: PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1.99 wrstuden 622: vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
1.55 cgd 623: VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
1.99 wrstuden 624: vput(pack.ep_vp);
1.95 eeh 625: uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
1.119 thorpej 626: free(pack.ep_hdr, M_EXEC);
1.55 cgd 627: exit1(p, W_EXITCODE(0, SIGABRT));
628: exit1(p, -1);
629:
630: /* NOTREACHED */
631: return 0;
1.67 christos 632: }
633:
634:
635: void *
1.118 thorpej 636: copyargs(struct exec_package *pack, struct ps_strings *arginfo,
637: void *stack, void *argp)
1.67 christos 638: {
639: char **cpp = stack;
640: char *dp, *sp;
641: size_t len;
642: void *nullp = NULL;
1.116 mycroft 643: long argc = arginfo->ps_nargvstr;
644: long envc = arginfo->ps_nenvstr;
1.67 christos 645:
1.116 mycroft 646: #ifdef __sparc_v9__
647: /* XXX Temporary hack for argc format conversion. */
1.117 mycroft 648: argc <<= 32;
1.116 mycroft 649: #endif
1.67 christos 650: if (copyout(&argc, cpp++, sizeof(argc)))
651: return NULL;
1.117 mycroft 652: #ifdef __sparc_v9__
653: /* XXX Temporary hack for argc format conversion. */
654: argc >>= 32;
655: #endif
1.67 christos 656:
1.124 jdolecek 657: dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
1.67 christos 658: sp = argp;
659:
660: /* XXX don't copy them out, remap them! */
1.69 mycroft 661: arginfo->ps_argvstr = cpp; /* remember location of argv for later */
1.67 christos 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:
1.69 mycroft 671: arginfo->ps_envstr = cpp; /* remember location of envp for later */
1.67 christos 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;
1.55 cgd 682: }
CVSweb <webmaster@jp.NetBSD.org>