123369Smckusick /* 237728Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337728Smckusick * All rights reserved. 423369Smckusick * 537728Smckusick * Redistribution and use in source and binary forms are permitted 637728Smckusick * provided that the above copyright notice and this paragraph are 737728Smckusick * duplicated in all such forms and that any documentation, 837728Smckusick * advertising materials, and other materials related to such 937728Smckusick * distribution and use acknowledge that the software was developed 1037728Smckusick * by the University of California, Berkeley. The name of the 1137728Smckusick * University may not be used to endorse or promote products derived 1237728Smckusick * from this software without specific prior written permission. 1337728Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437728Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537728Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637728Smckusick * 17*39410Skarels * @(#)kern_exit.c 7.10 (Berkeley) 10/27/89 1823369Smckusick */ 1912790Ssam 2017090Sbloom #include "param.h" 2117090Sbloom #include "systm.h" 2217090Sbloom #include "map.h" 2317090Sbloom #include "user.h" 2417090Sbloom #include "kernel.h" 2517090Sbloom #include "proc.h" 2617090Sbloom #include "buf.h" 2717090Sbloom #include "wait.h" 2817090Sbloom #include "vm.h" 2917090Sbloom #include "file.h" 3037728Smckusick #include "vnode.h" 31*39410Skarels #include "ioctl.h" 3237328Skarels #include "tty.h" 3318638Ssam #include "syslog.h" 3432018Smckusick #include "malloc.h" 3512790Ssam 3637520Smckusick #include "machine/reg.h" 3737328Skarels #ifdef COMPAT_43 3837520Smckusick #include "machine/psl.h" 3937328Skarels #endif 4037328Skarels 4112790Ssam /* 4212790Ssam * Exit system call: pass back caller's arg 4312790Ssam */ 4412790Ssam rexit() 4512790Ssam { 46*39410Skarels struct a { 4712790Ssam int rval; 4812790Ssam } *uap; 4912790Ssam 5012790Ssam uap = (struct a *)u.u_ap; 51*39410Skarels exit(W_EXITCODE(uap->rval, 0)); 5212790Ssam } 5312790Ssam 5412790Ssam /* 5512790Ssam * Release resources. 5612790Ssam * Save u. area for parent to look at. 5712790Ssam * Enter zombie state. 5812790Ssam * Wake up parent and init processes, 5912790Ssam * and dispose of children. 6012790Ssam */ 6112790Ssam exit(rv) 6238930Skarels int rv; 6312790Ssam { 6412790Ssam register int i; 6516527Skarels register struct proc *p, *q, *nq; 6612790Ssam register int x; 6712790Ssam 6812790Ssam #ifdef PGINPROF 6912790Ssam vmsizmon(); 7012790Ssam #endif 7112790Ssam p = u.u_procp; 7232018Smckusick MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 7332018Smckusick M_ZOMBIE, M_WAITOK); 7412790Ssam p->p_flag &= ~(STRC|SULOCK); 7512790Ssam p->p_flag |= SWEXIT; 7612882Ssam p->p_sigignore = ~0; 77*39410Skarels p->p_sig = 0; 7812790Ssam p->p_cpticks = 0; 7912790Ssam p->p_pctcpu = 0; 8012882Ssam for (i = 0; i < NSIG; i++) 8112790Ssam u.u_signal[i] = SIG_IGN; 8212790Ssam untimeout(realitexpire, (caddr_t)p); 8312790Ssam /* 8412790Ssam * Release virtual memory. If we resulted from 8512790Ssam * a vfork(), instead give the resources back to 8612790Ssam * the parent. 8712790Ssam */ 8812790Ssam if ((p->p_flag & SVFORK) == 0) 8912790Ssam vrelvm(); 9012790Ssam else { 9112790Ssam p->p_flag &= ~SVFORK; 9212790Ssam wakeup((caddr_t)p); 9312790Ssam while ((p->p_flag & SVFDONE) == 0) 9412790Ssam sleep((caddr_t)p, PZERO - 1); 9512790Ssam p->p_flag &= ~SVFDONE; 9612790Ssam } 9721105Skarels for (i = 0; i <= u.u_lastfile; i++) { 9812790Ssam struct file *f; 9912790Ssam 10012790Ssam f = u.u_ofile[i]; 10121105Skarels if (f) { 10221105Skarels u.u_ofile[i] = NULL; 10321105Skarels u.u_pofile[i] = 0; 10439351Smckusick (void) closef(f); 10521105Skarels } 10612790Ssam } 10737328Skarels if (SESS_LEADER(p)) { 10837328Skarels p->p_session->s_leader = 0; 10937328Skarels /* TODO: vhangup(); */ 11037328Skarels if (u.u_ttyp) { 11137328Skarels u.u_ttyp->t_session = 0; 11237328Skarels u.u_ttyp->t_pgid = 0; 11337328Skarels } 11437328Skarels } 11537728Smckusick VOP_LOCK(u.u_cdir); 11637728Smckusick vput(u.u_cdir); 11712790Ssam if (u.u_rdir) { 11837728Smckusick VOP_LOCK(u.u_rdir); 11937728Smckusick vput(u.u_rdir); 12012790Ssam } 12112790Ssam u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 12212790Ssam acct(); 12312790Ssam #ifdef QUOTA 12412790Ssam qclean(); 12512790Ssam #endif 12637728Smckusick crfree(u.u_cred); 12737328Skarels #ifdef KTRACE 12837328Skarels /* 12937328Skarels * release trace file 13037328Skarels */ 13137328Skarels if (p->p_tracep) 13237728Smckusick vrele(p->p_tracep); 13337328Skarels #endif 13426823Skarels /* 13537328Skarels /* 13626823Skarels * Freeing the user structure and kernel stack 13726823Skarels * for the current process: have to run a bit longer 13826823Skarels * using the pages which are about to be freed... 13926823Skarels * vrelu will block memory allocation by raising ipl. 14026823Skarels */ 14126823Skarels vrelu(u.u_procp, 0); 14212790Ssam vrelpt(u.u_procp); 14316527Skarels if (*p->p_prev = p->p_nxt) /* off allproc queue */ 14416527Skarels p->p_nxt->p_prev = p->p_prev; 14516527Skarels if (p->p_nxt = zombproc) /* onto zombproc */ 14616527Skarels p->p_nxt->p_prev = &p->p_nxt; 14716527Skarels p->p_prev = &zombproc; 14816527Skarels zombproc = p; 14912790Ssam multprog--; 15012790Ssam p->p_stat = SZOMB; 15112790Ssam noproc = 1; 15212790Ssam i = PIDHASH(p->p_pid); 15312790Ssam x = p - proc; 15412790Ssam if (pidhash[i] == x) 15512790Ssam pidhash[i] = p->p_idhash; 15612790Ssam else { 15712790Ssam for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 15812790Ssam if (proc[i].p_idhash == x) { 15912790Ssam proc[i].p_idhash = p->p_idhash; 16012790Ssam goto done; 16112790Ssam } 16212790Ssam panic("exit"); 16312790Ssam } 16421105Skarels if (p->p_pid == 1) { 16521105Skarels if (p->p_dsize == 0) { 166*39410Skarels printf("Can't exec init (errno %d)\n", WEXITSTATUS(rv)); 16721105Skarels for (;;) 16821105Skarels ; 16921105Skarels } else 17021105Skarels panic("init died"); 17121105Skarels } 17212790Ssam done: 17312790Ssam p->p_xstat = rv; 17421105Skarels *p->p_ru = u.u_ru; 17521105Skarels ruadd(p->p_ru, &u.u_cru); 17616527Skarels if (p->p_cptr) /* only need this if any child is S_ZOMB */ 17716527Skarels wakeup((caddr_t)&proc[1]); 17837328Skarels if (PGRP_JOBC(p)) 17937328Skarels p->p_pgrp->pg_jobc--; 18016527Skarels for (q = p->p_cptr; q != NULL; q = nq) { 18137328Skarels if (PGRP_JOBC(q)) 18237328Skarels q->p_pgrp->pg_jobc--; 18316527Skarels nq = q->p_osptr; 18416527Skarels if (nq != NULL) 18516527Skarels nq->p_ysptr = NULL; 18616527Skarels if (proc[1].p_cptr) 18716527Skarels proc[1].p_cptr->p_ysptr = q; 18816527Skarels q->p_osptr = proc[1].p_cptr; 18916527Skarels q->p_ysptr = NULL; 19016527Skarels proc[1].p_cptr = q; 19112790Ssam 19216527Skarels q->p_pptr = &proc[1]; 19316527Skarels q->p_ppid = 1; 19416527Skarels /* 19516527Skarels * Traced processes are killed 19616527Skarels * since their existence means someone is screwing up. 19716527Skarels * Stopped processes are sent a hangup and a continue. 19816527Skarels * This is designed to be ``safe'' for setuid 19916527Skarels * processes since they must be willing to tolerate 20016527Skarels * hangups anyways. 20116527Skarels */ 20216527Skarels if (q->p_flag&STRC) { 20316527Skarels q->p_flag &= ~STRC; 20416527Skarels psignal(q, SIGKILL); 20516527Skarels } else if (q->p_stat == SSTOP) { 20616527Skarels psignal(q, SIGHUP); 20716527Skarels psignal(q, SIGCONT); 20812790Ssam } 20916527Skarels /* 21016527Skarels * Protect this process from future 21116527Skarels * tty signals, clear TSTP/TTIN/TTOU if pending. 21216527Skarels */ 21325385Skarels (void) spgrp(q); 21416527Skarels } 21517540Skarels p->p_cptr = NULL; 21612790Ssam psignal(p->p_pptr, SIGCHLD); 21712790Ssam wakeup((caddr_t)p->p_pptr); 21829946Skarels #if defined(tahoe) 21929946Skarels dkeyrelease(p->p_dkey), p->p_dkey = 0; 22029946Skarels ckeyrelease(p->p_ckey), p->p_ckey = 0; 22129946Skarels u.u_pcb.pcb_savacc.faddr = (float *)NULL; 22229946Skarels #endif 22312790Ssam swtch(); 22412790Ssam } 22512790Ssam 22637328Skarels #ifdef COMPAT_43 22737328Skarels owait() 22812790Ssam { 229*39410Skarels register struct a { 23037328Skarels int pid; 231*39410Skarels int *status; 23237328Skarels int options; 23337328Skarels struct rusage *rusage; 234*39410Skarels int compat; 23537328Skarels } *uap = (struct a *)u.u_ap; 23612790Ssam 23712790Ssam if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 23838930Skarels uap->options = 0; 23937328Skarels uap->rusage = 0; 24037328Skarels } else { 24138930Skarels uap->options = u.u_ar0[R0]; 24237328Skarels uap->rusage = (struct rusage *)u.u_ar0[R1]; 24312790Ssam } 24437328Skarels uap->pid = WAIT_ANY; 24537328Skarels uap->status = 0; 246*39410Skarels uap->compat = 1; 247*39410Skarels u.u_error = wait1(); 24812790Ssam } 24912790Ssam 25037328Skarels wait4() 25137328Skarels { 252*39410Skarels register struct a { 253*39410Skarels int pid; 254*39410Skarels int *status; 255*39410Skarels int options; 256*39410Skarels struct rusage *rusage; 257*39410Skarels int compat; 258*39410Skarels } *uap = (struct a *)u.u_ap; 259*39410Skarels 260*39410Skarels uap->compat = 0; 261*39410Skarels u.u_error = wait1(); 26237328Skarels } 263*39410Skarels #else 264*39410Skarels #define wait1 wait4 26537328Skarels #endif 26637328Skarels 26712790Ssam /* 26812790Ssam * Wait system call. 26912790Ssam * Search for a terminated (zombie) child, 27012790Ssam * finally lay it to rest, and collect its status. 27112790Ssam * Look also for stopped (traced) children, 27212790Ssam * and pass back status from them. 27312790Ssam */ 274*39410Skarels wait1() 27512790Ssam { 27637328Skarels register struct a { 27737328Skarels int pid; 278*39410Skarels int *status; 27937328Skarels int options; 28037328Skarels struct rusage *rusage; 281*39410Skarels #ifdef COMPAT_43 282*39410Skarels int compat; 283*39410Skarels #endif 28437328Skarels } *uap = (struct a *)u.u_ap; 28512790Ssam register f; 28612790Ssam register struct proc *p, *q; 287*39410Skarels int status, error; 28812790Ssam 28937328Skarels q = u.u_procp; 29037328Skarels if (uap->pid == 0) 29137328Skarels uap->pid = -q->p_pgid; 29238930Skarels #ifdef notyet 293*39410Skarels if (uap->options &~ (WUNTRACED|WNOHANG)) 294*39410Skarels return (EINVAL); 29538930Skarels #endif 29612790Ssam loop: 29738930Skarels f = 0; 29816527Skarels for (p = q->p_cptr; p; p = p->p_osptr) { 29937328Skarels if (uap->pid != WAIT_ANY && 30037328Skarels p->p_pid != uap->pid && p->p_pgid != -uap->pid) 30137328Skarels continue; 30212790Ssam f++; 30312790Ssam if (p->p_stat == SZOMB) { 30412790Ssam u.u_r.r_val1 = p->p_pid; 30537328Skarels #ifdef COMPAT_43 306*39410Skarels if (uap->compat) 30737328Skarels u.u_r.r_val2 = p->p_xstat; 30837328Skarels else 30937328Skarels #endif 31037328Skarels if (uap->status) { 311*39410Skarels status = p->p_xstat; /* convert to int */ 312*39410Skarels if (error = copyout((caddr_t)&status, 31337328Skarels (caddr_t)uap->status, sizeof(status))) 314*39410Skarels return (error); 31537328Skarels } 316*39410Skarels if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 317*39410Skarels (caddr_t)uap->rusage, sizeof (struct rusage)))) 318*39410Skarels return (error); 319*39410Skarels pgrm(p); /* off pgrp */ 32012790Ssam p->p_xstat = 0; 32137328Skarels ruadd(&u.u_cru, p->p_ru); 32237328Skarels FREE(p->p_ru, M_ZOMBIE); 32337328Skarels p->p_ru = 0; 32412790Ssam p->p_stat = NULL; 32512790Ssam p->p_pid = 0; 32612790Ssam p->p_ppid = 0; 32716527Skarels if (*p->p_prev = p->p_nxt) /* off zombproc */ 32816527Skarels p->p_nxt->p_prev = p->p_prev; 32916527Skarels p->p_nxt = freeproc; /* onto freeproc */ 33016527Skarels freeproc = p; 33112790Ssam if (q = p->p_ysptr) 33212790Ssam q->p_osptr = p->p_osptr; 33312790Ssam if (q = p->p_osptr) 33412790Ssam q->p_ysptr = p->p_ysptr; 33512790Ssam if ((q = p->p_pptr)->p_cptr == p) 33612790Ssam q->p_cptr = p->p_osptr; 33712790Ssam p->p_pptr = 0; 33812790Ssam p->p_ysptr = 0; 33912790Ssam p->p_osptr = 0; 34012790Ssam p->p_cptr = 0; 34112790Ssam p->p_sig = 0; 34212882Ssam p->p_sigcatch = 0; 34312882Ssam p->p_sigignore = 0; 34412882Ssam p->p_sigmask = 0; 34537328Skarels /*p->p_pgrp = 0;*/ 34612790Ssam p->p_flag = 0; 34712790Ssam p->p_wchan = 0; 34812790Ssam p->p_cursig = 0; 349*39410Skarels return (0); 35012790Ssam } 35137328Skarels if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 35237328Skarels (p->p_flag & STRC || uap->options & WUNTRACED)) { 35312790Ssam p->p_flag |= SWTED; 35412790Ssam u.u_r.r_val1 = p->p_pid; 35537328Skarels #ifdef COMPAT_43 356*39410Skarels if (uap->compat) 357*39410Skarels u.u_r.r_val2 = W_STOPCODE(p->p_cursig); 35837328Skarels else 35937328Skarels #endif 36037328Skarels if (uap->status) { 361*39410Skarels status = W_STOPCODE(p->p_cursig); 362*39410Skarels error = copyout((caddr_t)&status, 36337328Skarels (caddr_t)uap->status, sizeof(status)); 364*39410Skarels } else 365*39410Skarels error = 0; 366*39410Skarels return (error); 36712790Ssam } 36812790Ssam } 369*39410Skarels if (f == 0) 370*39410Skarels return (ECHILD); 37137328Skarels if (uap->options & WNOHANG) { 37212790Ssam u.u_r.r_val1 = 0; 373*39410Skarels return (0); 37412790Ssam } 37538930Skarels if (setjmp(&u.u_qsave)) { 37618310Smckusick p = u.u_procp; 377*39410Skarels if ((u.u_sigintr & sigmask(p->p_cursig)) != 0) 378*39410Skarels return (EINTR); 37912790Ssam u.u_eosys = RESTARTSYS; 380*39410Skarels return (0); 38112790Ssam } 38212790Ssam sleep((caddr_t)u.u_procp, PWAIT); 38312790Ssam goto loop; 38412790Ssam } 385