1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)kern_exit.c 7.17 (Berkeley) 04/05/90 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "map.h" 23 #include "user.h" 24 #include "kernel.h" 25 #include "proc.h" 26 #include "buf.h" 27 #include "wait.h" 28 #include "vm.h" 29 #include "file.h" 30 #include "vnode.h" 31 #include "ioctl.h" 32 #include "tty.h" 33 #include "syslog.h" 34 #include "malloc.h" 35 36 #include "machine/reg.h" 37 #ifdef COMPAT_43 38 #include "machine/psl.h" 39 #endif 40 41 /* 42 * Exit system call: pass back caller's arg 43 */ 44 rexit() 45 { 46 struct a { 47 int rval; 48 } *uap; 49 50 uap = (struct a *)u.u_ap; 51 exit(W_EXITCODE(uap->rval, 0)); 52 } 53 54 /* 55 * Release resources. 56 * Save u. area for parent to look at. 57 * Enter zombie state. 58 * Wake up parent and init processes, 59 * and dispose of children. 60 */ 61 exit(rv) 62 int rv; 63 { 64 register int i; 65 register struct proc *p, *q, *nq; 66 register struct proc **pp; 67 68 #ifdef PGINPROF 69 vmsizmon(); 70 #endif 71 p = u.u_procp; 72 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 73 M_ZOMBIE, M_WAITOK); 74 p->p_flag &= ~(STRC|SULOCK); 75 p->p_flag |= SWEXIT; 76 p->p_sigignore = ~0; 77 p->p_sig = 0; 78 p->p_cpticks = 0; 79 p->p_pctcpu = 0; 80 for (i = 0; i < NSIG; i++) 81 u.u_signal[i] = SIG_IGN; 82 untimeout(realitexpire, (caddr_t)p); 83 /* 84 * Release virtual memory. If we resulted from 85 * a vfork(), instead give the resources back to 86 * the parent. 87 */ 88 if ((p->p_flag & SVFORK) == 0) 89 vrelvm(); 90 else { 91 p->p_flag &= ~SVFORK; 92 wakeup((caddr_t)p); 93 while ((p->p_flag & SVFDONE) == 0) 94 sleep((caddr_t)p, PZERO - 1); 95 p->p_flag &= ~SVFDONE; 96 } 97 for (i = 0; i <= u.u_lastfile; i++) { 98 struct file *f; 99 100 f = u.u_ofile[i]; 101 if (f) { 102 u.u_ofile[i] = NULL; 103 u.u_pofile[i] = 0; 104 (void) closef(f); 105 } 106 } 107 if (SESS_LEADER(p)) { 108 p->p_session->s_leader = 0; 109 if (p->p_session->s_ttyvp) { 110 vgoneall(p->p_session->s_ttyvp); 111 vrele(p->p_session->s_ttyvp); 112 p->p_session->s_ttyvp = NULL; 113 /* 114 * s_ttyp is not zero'd; we use this to indicate 115 * that the session once had a controlling terminal. 116 */ 117 } 118 } 119 VOP_LOCK(u.u_cdir); 120 vput(u.u_cdir); 121 if (u.u_rdir) { 122 VOP_LOCK(u.u_rdir); 123 vput(u.u_rdir); 124 } 125 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 126 acct(); 127 #ifdef QUOTA 128 qclean(); 129 #endif 130 crfree(u.u_cred); 131 #ifdef KTRACE 132 /* 133 * release trace file 134 */ 135 if (p->p_tracep) 136 vrele(p->p_tracep); 137 #endif 138 /* 139 /* 140 * Freeing the user structure and kernel stack 141 * for the current process: have to run a bit longer 142 * using the pages which are about to be freed... 143 * vrelu will block memory allocation by raising ipl. 144 */ 145 vrelu(u.u_procp, 0); 146 vrelpt(u.u_procp); 147 if (*p->p_prev = p->p_nxt) /* off allproc queue */ 148 p->p_nxt->p_prev = p->p_prev; 149 if (p->p_nxt = zombproc) /* onto zombproc */ 150 p->p_nxt->p_prev = &p->p_nxt; 151 p->p_prev = &zombproc; 152 zombproc = p; 153 multprog--; 154 p->p_stat = SZOMB; 155 noproc = 1; 156 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) 157 if (*pp == p) { 158 *pp = p->p_hash; 159 goto done; 160 } 161 panic("exit"); 162 done: 163 if (p->p_pid == 1) { 164 if (p->p_dsize == 0) { 165 printf("Can't exec init (errno %d)\n", WEXITSTATUS(rv)); 166 for (;;) 167 ; 168 } else 169 panic("init died"); 170 } 171 p->p_xstat = rv; 172 *p->p_ru = u.u_ru; 173 i = splclock(); 174 p->p_ru->ru_stime = p->p_stime; 175 p->p_ru->ru_utime = p->p_utime; 176 splx(i); 177 ruadd(p->p_ru, &u.u_cru); 178 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 179 wakeup((caddr_t)&proc[1]); 180 if (PGRP_JOBC(p)) 181 p->p_pgrp->pg_jobc--; 182 for (q = p->p_cptr; q != NULL; q = nq) { 183 if (PGRP_JOBC(q)) 184 q->p_pgrp->pg_jobc--; 185 nq = q->p_osptr; 186 if (nq != NULL) 187 nq->p_ysptr = NULL; 188 if (proc[1].p_cptr) 189 proc[1].p_cptr->p_ysptr = q; 190 q->p_osptr = proc[1].p_cptr; 191 q->p_ysptr = NULL; 192 proc[1].p_cptr = q; 193 194 q->p_pptr = &proc[1]; 195 q->p_ppid = 1; 196 /* 197 * Traced processes are killed 198 * since their existence means someone is screwing up. 199 * Stopped processes are sent a hangup and a continue. 200 * This is designed to be ``safe'' for setuid 201 * processes since they must be willing to tolerate 202 * hangups anyways. 203 */ 204 if (q->p_flag&STRC) { 205 q->p_flag &= ~STRC; 206 psignal(q, SIGKILL); 207 } else if (q->p_stat == SSTOP) { 208 psignal(q, SIGHUP); 209 psignal(q, SIGCONT); 210 } 211 /* 212 * Protect this process from future 213 * tty signals, clear TSTP/TTIN/TTOU if pending. 214 */ 215 (void) spgrp(q); 216 } 217 p->p_cptr = NULL; 218 psignal(p->p_pptr, SIGCHLD); 219 wakeup((caddr_t)p->p_pptr); 220 #if defined(tahoe) 221 dkeyrelease(p->p_dkey), p->p_dkey = 0; 222 ckeyrelease(p->p_ckey), p->p_ckey = 0; 223 u.u_pcb.pcb_savacc.faddr = (float *)NULL; 224 #endif 225 swtch(); 226 } 227 228 #ifdef COMPAT_43 229 owait() 230 { 231 register struct a { 232 int pid; 233 int *status; 234 int options; 235 struct rusage *rusage; 236 int compat; 237 } *uap = (struct a *)u.u_ap; 238 239 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 240 uap->options = 0; 241 uap->rusage = 0; 242 } else { 243 uap->options = u.u_ar0[R0]; 244 uap->rusage = (struct rusage *)u.u_ar0[R1]; 245 } 246 uap->pid = WAIT_ANY; 247 uap->status = 0; 248 uap->compat = 1; 249 u.u_error = wait1(); 250 } 251 252 wait4() 253 { 254 register struct a { 255 int pid; 256 int *status; 257 int options; 258 struct rusage *rusage; 259 int compat; 260 } *uap = (struct a *)u.u_ap; 261 262 uap->compat = 0; 263 u.u_error = wait1(); 264 } 265 #else 266 #define wait1 wait4 267 #endif 268 269 /* 270 * Wait system call. 271 * Search for a terminated (zombie) child, 272 * finally lay it to rest, and collect its status. 273 * Look also for stopped (traced) children, 274 * and pass back status from them. 275 */ 276 wait1() 277 { 278 register struct a { 279 int pid; 280 int *status; 281 int options; 282 struct rusage *rusage; 283 #ifdef COMPAT_43 284 int compat; 285 #endif 286 } *uap = (struct a *)u.u_ap; 287 register f; 288 register struct proc *p, *q; 289 int status, error; 290 291 q = u.u_procp; 292 if (uap->pid == 0) 293 uap->pid = -q->p_pgid; 294 #ifdef notyet 295 if (uap->options &~ (WUNTRACED|WNOHANG)) 296 return (EINVAL); 297 #endif 298 loop: 299 f = 0; 300 for (p = q->p_cptr; p; p = p->p_osptr) { 301 if (uap->pid != WAIT_ANY && 302 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 303 continue; 304 f++; 305 if (p->p_stat == SZOMB) { 306 u.u_r.r_val1 = p->p_pid; 307 #ifdef COMPAT_43 308 if (uap->compat) 309 u.u_r.r_val2 = p->p_xstat; 310 else 311 #endif 312 if (uap->status) { 313 status = p->p_xstat; /* convert to int */ 314 if (error = copyout((caddr_t)&status, 315 (caddr_t)uap->status, sizeof(status))) 316 return (error); 317 } 318 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 319 (caddr_t)uap->rusage, sizeof (struct rusage)))) 320 return (error); 321 pgrm(p); /* off pgrp */ 322 p->p_xstat = 0; 323 ruadd(&u.u_cru, p->p_ru); 324 FREE(p->p_ru, M_ZOMBIE); 325 p->p_ru = 0; 326 p->p_stat = NULL; 327 p->p_pid = 0; 328 p->p_ppid = 0; 329 if (*p->p_prev = p->p_nxt) /* off zombproc */ 330 p->p_nxt->p_prev = p->p_prev; 331 p->p_nxt = freeproc; /* onto freeproc */ 332 freeproc = p; 333 if (q = p->p_ysptr) 334 q->p_osptr = p->p_osptr; 335 if (q = p->p_osptr) 336 q->p_ysptr = p->p_ysptr; 337 if ((q = p->p_pptr)->p_cptr == p) 338 q->p_cptr = p->p_osptr; 339 p->p_pptr = 0; 340 p->p_ysptr = 0; 341 p->p_osptr = 0; 342 p->p_cptr = 0; 343 p->p_sig = 0; 344 p->p_sigcatch = 0; 345 p->p_sigignore = 0; 346 p->p_sigmask = 0; 347 /*p->p_pgrp = 0;*/ 348 p->p_flag = 0; 349 p->p_wchan = 0; 350 p->p_cursig = 0; 351 return (0); 352 } 353 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 354 (p->p_flag & STRC || uap->options & WUNTRACED)) { 355 p->p_flag |= SWTED; 356 u.u_r.r_val1 = p->p_pid; 357 #ifdef COMPAT_43 358 if (uap->compat) { 359 u.u_r.r_val2 = W_STOPCODE(p->p_cursig); 360 error = 0; 361 } else 362 #endif 363 if (uap->status) { 364 status = W_STOPCODE(p->p_cursig); 365 error = copyout((caddr_t)&status, 366 (caddr_t)uap->status, sizeof(status)); 367 } else 368 error = 0; 369 return (error); 370 } 371 } 372 if (f == 0) 373 return (ECHILD); 374 if (uap->options & WNOHANG) { 375 u.u_r.r_val1 = 0; 376 return (0); 377 } 378 if (error = tsleep((caddr_t)u.u_procp, PWAIT | PCATCH, "wait", 0)) 379 return (error); 380 goto loop; 381 } 382