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.10 (Berkeley) 10/27/89 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 int x; 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 /* TODO: vhangup(); */ 110 if (u.u_ttyp) { 111 u.u_ttyp->t_session = 0; 112 u.u_ttyp->t_pgid = 0; 113 } 114 } 115 VOP_LOCK(u.u_cdir); 116 vput(u.u_cdir); 117 if (u.u_rdir) { 118 VOP_LOCK(u.u_rdir); 119 vput(u.u_rdir); 120 } 121 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 122 acct(); 123 #ifdef QUOTA 124 qclean(); 125 #endif 126 crfree(u.u_cred); 127 #ifdef KTRACE 128 /* 129 * release trace file 130 */ 131 if (p->p_tracep) 132 vrele(p->p_tracep); 133 #endif 134 /* 135 /* 136 * Freeing the user structure and kernel stack 137 * for the current process: have to run a bit longer 138 * using the pages which are about to be freed... 139 * vrelu will block memory allocation by raising ipl. 140 */ 141 vrelu(u.u_procp, 0); 142 vrelpt(u.u_procp); 143 if (*p->p_prev = p->p_nxt) /* off allproc queue */ 144 p->p_nxt->p_prev = p->p_prev; 145 if (p->p_nxt = zombproc) /* onto zombproc */ 146 p->p_nxt->p_prev = &p->p_nxt; 147 p->p_prev = &zombproc; 148 zombproc = p; 149 multprog--; 150 p->p_stat = SZOMB; 151 noproc = 1; 152 i = PIDHASH(p->p_pid); 153 x = p - proc; 154 if (pidhash[i] == x) 155 pidhash[i] = p->p_idhash; 156 else { 157 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 158 if (proc[i].p_idhash == x) { 159 proc[i].p_idhash = p->p_idhash; 160 goto done; 161 } 162 panic("exit"); 163 } 164 if (p->p_pid == 1) { 165 if (p->p_dsize == 0) { 166 printf("Can't exec init (errno %d)\n", WEXITSTATUS(rv)); 167 for (;;) 168 ; 169 } else 170 panic("init died"); 171 } 172 done: 173 p->p_xstat = rv; 174 *p->p_ru = u.u_ru; 175 ruadd(p->p_ru, &u.u_cru); 176 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 177 wakeup((caddr_t)&proc[1]); 178 if (PGRP_JOBC(p)) 179 p->p_pgrp->pg_jobc--; 180 for (q = p->p_cptr; q != NULL; q = nq) { 181 if (PGRP_JOBC(q)) 182 q->p_pgrp->pg_jobc--; 183 nq = q->p_osptr; 184 if (nq != NULL) 185 nq->p_ysptr = NULL; 186 if (proc[1].p_cptr) 187 proc[1].p_cptr->p_ysptr = q; 188 q->p_osptr = proc[1].p_cptr; 189 q->p_ysptr = NULL; 190 proc[1].p_cptr = q; 191 192 q->p_pptr = &proc[1]; 193 q->p_ppid = 1; 194 /* 195 * Traced processes are killed 196 * since their existence means someone is screwing up. 197 * Stopped processes are sent a hangup and a continue. 198 * This is designed to be ``safe'' for setuid 199 * processes since they must be willing to tolerate 200 * hangups anyways. 201 */ 202 if (q->p_flag&STRC) { 203 q->p_flag &= ~STRC; 204 psignal(q, SIGKILL); 205 } else if (q->p_stat == SSTOP) { 206 psignal(q, SIGHUP); 207 psignal(q, SIGCONT); 208 } 209 /* 210 * Protect this process from future 211 * tty signals, clear TSTP/TTIN/TTOU if pending. 212 */ 213 (void) spgrp(q); 214 } 215 p->p_cptr = NULL; 216 psignal(p->p_pptr, SIGCHLD); 217 wakeup((caddr_t)p->p_pptr); 218 #if defined(tahoe) 219 dkeyrelease(p->p_dkey), p->p_dkey = 0; 220 ckeyrelease(p->p_ckey), p->p_ckey = 0; 221 u.u_pcb.pcb_savacc.faddr = (float *)NULL; 222 #endif 223 swtch(); 224 } 225 226 #ifdef COMPAT_43 227 owait() 228 { 229 register struct a { 230 int pid; 231 int *status; 232 int options; 233 struct rusage *rusage; 234 int compat; 235 } *uap = (struct a *)u.u_ap; 236 237 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 238 uap->options = 0; 239 uap->rusage = 0; 240 } else { 241 uap->options = u.u_ar0[R0]; 242 uap->rusage = (struct rusage *)u.u_ar0[R1]; 243 } 244 uap->pid = WAIT_ANY; 245 uap->status = 0; 246 uap->compat = 1; 247 u.u_error = wait1(); 248 } 249 250 wait4() 251 { 252 register struct a { 253 int pid; 254 int *status; 255 int options; 256 struct rusage *rusage; 257 int compat; 258 } *uap = (struct a *)u.u_ap; 259 260 uap->compat = 0; 261 u.u_error = wait1(); 262 } 263 #else 264 #define wait1 wait4 265 #endif 266 267 /* 268 * Wait system call. 269 * Search for a terminated (zombie) child, 270 * finally lay it to rest, and collect its status. 271 * Look also for stopped (traced) children, 272 * and pass back status from them. 273 */ 274 wait1() 275 { 276 register struct a { 277 int pid; 278 int *status; 279 int options; 280 struct rusage *rusage; 281 #ifdef COMPAT_43 282 int compat; 283 #endif 284 } *uap = (struct a *)u.u_ap; 285 register f; 286 register struct proc *p, *q; 287 int status, error; 288 289 q = u.u_procp; 290 if (uap->pid == 0) 291 uap->pid = -q->p_pgid; 292 #ifdef notyet 293 if (uap->options &~ (WUNTRACED|WNOHANG)) 294 return (EINVAL); 295 #endif 296 loop: 297 f = 0; 298 for (p = q->p_cptr; p; p = p->p_osptr) { 299 if (uap->pid != WAIT_ANY && 300 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 301 continue; 302 f++; 303 if (p->p_stat == SZOMB) { 304 u.u_r.r_val1 = p->p_pid; 305 #ifdef COMPAT_43 306 if (uap->compat) 307 u.u_r.r_val2 = p->p_xstat; 308 else 309 #endif 310 if (uap->status) { 311 status = p->p_xstat; /* convert to int */ 312 if (error = copyout((caddr_t)&status, 313 (caddr_t)uap->status, sizeof(status))) 314 return (error); 315 } 316 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 317 (caddr_t)uap->rusage, sizeof (struct rusage)))) 318 return (error); 319 pgrm(p); /* off pgrp */ 320 p->p_xstat = 0; 321 ruadd(&u.u_cru, p->p_ru); 322 FREE(p->p_ru, M_ZOMBIE); 323 p->p_ru = 0; 324 p->p_stat = NULL; 325 p->p_pid = 0; 326 p->p_ppid = 0; 327 if (*p->p_prev = p->p_nxt) /* off zombproc */ 328 p->p_nxt->p_prev = p->p_prev; 329 p->p_nxt = freeproc; /* onto freeproc */ 330 freeproc = p; 331 if (q = p->p_ysptr) 332 q->p_osptr = p->p_osptr; 333 if (q = p->p_osptr) 334 q->p_ysptr = p->p_ysptr; 335 if ((q = p->p_pptr)->p_cptr == p) 336 q->p_cptr = p->p_osptr; 337 p->p_pptr = 0; 338 p->p_ysptr = 0; 339 p->p_osptr = 0; 340 p->p_cptr = 0; 341 p->p_sig = 0; 342 p->p_sigcatch = 0; 343 p->p_sigignore = 0; 344 p->p_sigmask = 0; 345 /*p->p_pgrp = 0;*/ 346 p->p_flag = 0; 347 p->p_wchan = 0; 348 p->p_cursig = 0; 349 return (0); 350 } 351 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 352 (p->p_flag & STRC || uap->options & WUNTRACED)) { 353 p->p_flag |= SWTED; 354 u.u_r.r_val1 = p->p_pid; 355 #ifdef COMPAT_43 356 if (uap->compat) 357 u.u_r.r_val2 = W_STOPCODE(p->p_cursig); 358 else 359 #endif 360 if (uap->status) { 361 status = W_STOPCODE(p->p_cursig); 362 error = copyout((caddr_t)&status, 363 (caddr_t)uap->status, sizeof(status)); 364 } else 365 error = 0; 366 return (error); 367 } 368 } 369 if (f == 0) 370 return (ECHILD); 371 if (uap->options & WNOHANG) { 372 u.u_r.r_val1 = 0; 373 return (0); 374 } 375 if (setjmp(&u.u_qsave)) { 376 p = u.u_procp; 377 if ((u.u_sigintr & sigmask(p->p_cursig)) != 0) 378 return (EINTR); 379 u.u_eosys = RESTARTSYS; 380 return (0); 381 } 382 sleep((caddr_t)u.u_procp, PWAIT); 383 goto loop; 384 } 385