Deleted Added
sdiff udiff text old ( 41059 ) new ( 41086 )
full compact
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
39 * $Id: kern_exit.c,v 1.67 1998/06/05 21:44:20 dg Exp $
40 */
41
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/kernel.h>
49#include <sys/malloc.h>
50#include <sys/proc.h>
51#include <sys/pioctl.h>
52#include <sys/tty.h>
53#include <sys/wait.h>
54#include <sys/vnode.h>
55#include <sys/resourcevar.h>
56#include <sys/signalvar.h>
57#include <sys/ptrace.h>
58#include <sys/acct.h> /* for acct_process() function prototype */
59#include <sys/filedesc.h>
60#include <sys/shm.h>
61#include <sys/sem.h>
62#include <sys/aio.h>
63
64#ifdef COMPAT_43
65#include <machine/reg.h>
66#include <machine/psl.h>
67#endif
68#include <machine/limits.h> /* for UCHAR_MAX = typeof(p_priority)_MAX */
69
70#include <vm/vm.h>
71#include <vm/vm_param.h>
72#include <sys/lock.h>
73#include <vm/pmap.h>
74#include <vm/vm_map.h>
75#include <vm/vm_zone.h>
76
77static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
78
79static int wait1 __P((struct proc *, struct wait_args *, int));
80
81/*
82 * callout list for things to do at exit time
83 */
84typedef struct exit_list_element {
85 struct exit_list_element *next;
86 exitlist_fn function;
87} *ele_p;
88
89static ele_p exit_list;
90
91/*
92 * exit --
93 * Death of process.
94 */
95void
96exit(p, uap)
97 struct proc *p;
98 struct rexit_args /* {
99 int rval;
100 } */ *uap;
101{
102
103 exit1(p, W_EXITCODE(uap->rval, 0));
104 /* NOTREACHED */
105}
106
107/*
108 * Exit: deallocate address space and other resources, change proc state
109 * to zombie, and unlink proc from allproc and parent's lists. Save exit
110 * status and rusage for wait(). Check for child processes and orphan them.
111 */
112void
113exit1(p, rv)
114 register struct proc *p;
115 int rv;
116{
117 register struct proc *q, *nq;
118 register struct vmspace *vm;
119 ele_p ep = exit_list;
120
121 if (p->p_pid == 1) {
122 printf("init died (signal %d, exit %d)\n",
123 WTERMSIG(rv), WEXITSTATUS(rv));
124 panic("Going nowhere without my init!");
125 }
126
127 aio_proc_rundown(p);
128
129 /* are we a task leader? */
130 if(p == p->p_leader) {
131 struct kill_args killArgs;
132 killArgs.signum = SIGKILL;
133 q = p->p_peers;
134 while(q) {
135 killArgs.pid = q->p_pid;
136 /*
137 * The interface for kill is better
138 * than the internal signal
139 */
140 kill(p, &killArgs);
141 nq = q;
142 q = q->p_peers;
143 /*
144 * orphan the threads so we don't mess up
145 * when they call exit
146 */
147 nq->p_peers = 0;
148 nq->p_leader = nq;
149 }
150
151 /* otherwise are we a peer? */
152 } else if(p->p_peers) {
153 q = p->p_leader;
154 while(q->p_peers != p)
155 q = q->p_peers;
156 q->p_peers = p->p_peers;
157 }
158
159#ifdef PGINPROF
160 vmsizmon();
161#endif
162 STOPEVENT(p, S_EXIT, rv);
163
164 /*
165 * Check if any LKMs need anything done at process exit.
166 * e.g. SYSV IPC stuff
167 * XXX what if one of these generates an error?
168 */
169 while (ep) {
170 (*ep->function)(p);
171 ep = ep->next;
172 }
173
174 if (p->p_flag & P_PROFIL)
175 stopprofclock(p);
176 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
177 M_ZOMBIE, M_WAITOK);
178 /*
179 * If parent is waiting for us to exit or exec,
180 * P_PPWAIT is set; we will wakeup the parent below.
181 */
182 p->p_flag &= ~(P_TRACED | P_PPWAIT);
183 p->p_flag |= P_WEXIT;
184 p->p_sigignore = ~0;
185 p->p_siglist = 0;
186 if (timevalisset(&p->p_realtimer.it_value))
187 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
188
189 /*
190 * Close open files and release open-file table.
191 * This may block!
192 */
193 fdfree(p);
194
195 /*
196 * XXX Shutdown SYSV semaphores
197 */
198 semexit(p);
199
200 /* The next two chunks should probably be moved to vmspace_exit. */
201 vm = p->p_vmspace;
202 /*
203 * Release user portion of address space.
204 * This releases references to vnodes,
205 * which could cause I/O if the file has been unlinked.
206 * Need to do this early enough that we can still sleep.
207 * Can't free the entire vmspace as the kernel stack
208 * may be mapped within that space also.
209 */
210 if (vm->vm_refcnt == 1) {
211 if (vm->vm_shm)
212 shmexit(p);
213 pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS,
214 VM_MAXUSER_ADDRESS);
215 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
216 VM_MAXUSER_ADDRESS);
217 }
218
219 if (SESS_LEADER(p)) {
220 register struct session *sp = p->p_session;
221
222 if (sp->s_ttyvp) {
223 /*
224 * Controlling process.
225 * Signal foreground pgrp,
226 * drain controlling terminal
227 * and revoke access to controlling terminal.
228 */
229 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
230 if (sp->s_ttyp->t_pgrp)
231 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
232 (void) ttywait(sp->s_ttyp);
233 /*
234 * The tty could have been revoked
235 * if we blocked.
236 */
237 if (sp->s_ttyvp)
238 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
239 }
240 if (sp->s_ttyvp)
241 vrele(sp->s_ttyvp);
242 sp->s_ttyvp = NULL;
243 /*
244 * s_ttyp is not zero'd; we use this to indicate
245 * that the session once had a controlling terminal.
246 * (for logging and informational purposes)
247 */
248 }
249 sp->s_leader = NULL;
250 }
251 fixjobc(p, p->p_pgrp, 0);
252 (void)acct_process(p);
253#ifdef KTRACE
254 /*
255 * release trace file
256 */
257 p->p_traceflag = 0; /* don't trace the vrele() */
258 if (p->p_tracep)
259 vrele(p->p_tracep);
260#endif
261 /*
262 * Remove proc from allproc queue and pidhash chain.
263 * Place onto zombproc. Unlink from parent's child list.
264 */
265 LIST_REMOVE(p, p_list);
266 LIST_INSERT_HEAD(&zombproc, p, p_list);
267 p->p_stat = SZOMB;
268
269 LIST_REMOVE(p, p_hash);
270
271 q = p->p_children.lh_first;
272 if (q) /* only need this if any child is S_ZOMB */
273 wakeup((caddr_t) initproc);
274 for (; q != 0; q = nq) {
275 nq = q->p_sibling.le_next;
276 LIST_REMOVE(q, p_sibling);
277 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
278 q->p_pptr = initproc;
279 /*
280 * Traced processes are killed
281 * since their existence means someone is screwing up.
282 */
283 if (q->p_flag & P_TRACED) {
284 q->p_flag &= ~P_TRACED;
285 psignal(q, SIGKILL);
286 }
287 }
288
289 /*
290 * Save exit status and final rusage info, adding in child rusage
291 * info and self times.
292 */
293 p->p_xstat = rv;
294 *p->p_ru = p->p_stats->p_ru;
295 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
296 ruadd(p->p_ru, &p->p_stats->p_cru);
297
298 /*
299 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
300 * flag set, notify process 1 instead (and hope it will handle
301 * this situation).
302 */
303 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
304 struct proc *pp = p->p_pptr;
305 proc_reparent(p, initproc);
306 /*
307 * If this was the last child of our parent, notify
308 * parent, so in case he was wait(2)ing, he will
309 * continue.
310 */
311 if (LIST_EMPTY(&pp->p_children))
312 wakeup((caddr_t)pp);
313 }
314
315 psignal(p->p_pptr, SIGCHLD);
316 wakeup((caddr_t)p->p_pptr);
317#if defined(tahoe)
318 /* move this to cpu_exit */
319 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
320#endif
321 /*
322 * Clear curproc after we've done all operations
323 * that could block, and before tearing down the rest
324 * of the process state that might be used from clock, etc.
325 * Also, can't clear curproc while we're still runnable,
326 * as we're not on a run queue (we are current, just not
327 * a proper proc any longer!).
328 *
329 * Other substructures are freed from wait().
330 */
331 curproc = NULL;
332 if (--p->p_limit->p_refcnt == 0) {
333 FREE(p->p_limit, M_SUBPROC);
334 p->p_limit = NULL;
335 }
336
337 /*
338 * Finally, call machine-dependent code to release the remaining
339 * resources including address space, the kernel stack and pcb.
340 * The address space is released by "vmspace_free(p->p_vmspace)";
341 * This is machine-dependent, as we may have to change stacks
342 * or ensure that the current one isn't reallocated before we
343 * finish. cpu_exit will end with a call to cpu_switch(), finishing
344 * our execution (pun intended).
345 */
346 cpu_exit(p);
347}
348
349#ifdef COMPAT_43
350#if defined(hp300) || defined(luna68k)
351#include <machine/frame.h>
352#define GETPS(rp) ((struct frame *)(rp))->f_sr
353#else
354#define GETPS(rp) (rp)[PS]
355#endif
356
357int
358owait(p, uap)
359 struct proc *p;
360 register struct owait_args /* {
361 int dummy;
362 } */ *uap;
363{
364 struct wait_args w;
365
366#ifdef PSL_ALLCC
367 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
368 w.options = 0;
369 w.rusage = NULL;
370 } else {
371 w.options = p->p_md.md_regs[R0];
372 w.rusage = (struct rusage *)p->p_md.md_regs[R1];
373 }
374#else
375 w.options = 0;
376 w.rusage = NULL;
377#endif
378 w.pid = WAIT_ANY;
379 w.status = NULL;
380 return (wait1(p, &w, 1));
381}
382#endif /* COMPAT_43 */
383
384int
385wait4(p, uap)
386 struct proc *p;
387 struct wait_args *uap;
388{
389
390 return (wait1(p, uap, 0));
391}
392
393static int
394wait1(q, uap, compat)
395 register struct proc *q;
396 register struct wait_args /* {
397 int pid;
398 int *status;
399 int options;
400 struct rusage *rusage;
401 } */ *uap;
402 int compat;
403{
404 register int nfound;
405 register struct proc *p, *t;
406 int status, error;
407
408 if (uap->pid == 0)
409 uap->pid = -q->p_pgid;
410 if (uap->options &~ (WUNTRACED|WNOHANG))
411 return (EINVAL);
412loop:
413 nfound = 0;
414 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
415 if (uap->pid != WAIT_ANY &&
416 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
417 continue;
418 nfound++;
419 if (p->p_stat == SZOMB) {
420 /* charge childs scheduling cpu usage to parent */
421 if (curproc->p_pid != 1) {
422 curproc->p_estcpu = min(curproc->p_estcpu +
423 p->p_estcpu, UCHAR_MAX);
424 }
425
426 q->p_retval[0] = p->p_pid;
427#ifdef COMPAT_43
428 if (compat)
429 q->p_retval[1] = p->p_xstat;
430 else
431#endif
432 if (uap->status) {
433 status = p->p_xstat; /* convert to int */
434 if ((error = copyout((caddr_t)&status,
435 (caddr_t)uap->status, sizeof(status))))
436 return (error);
437 }
438 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
439 (caddr_t)uap->rusage, sizeof (struct rusage))))
440 return (error);
441 /*
442 * If we got the child via a ptrace 'attach',
443 * we need to give it back to the old parent.
444 */
445 if (p->p_oppid && (t = pfind(p->p_oppid))) {
446 p->p_oppid = 0;
447 proc_reparent(p, t);
448 psignal(t, SIGCHLD);
449 wakeup((caddr_t)t);
450 return (0);
451 }
452 p->p_xstat = 0;
453 ruadd(&q->p_stats->p_cru, p->p_ru);
454 FREE(p->p_ru, M_ZOMBIE);
455 p->p_ru = NULL;
456
457 /*
458 * Decrement the count of procs running with this uid.
459 */
460 (void)chgproccnt(p->p_cred->p_ruid, -1);
461
462 /*
463 * Release reference to text vnode
464 */
465 if (p->p_textvp)
466 vrele(p->p_textvp);
467
468 /*
469 * Free up credentials.
470 */
471 if (--p->p_cred->p_refcnt == 0) {
472 crfree(p->p_cred->pc_ucred);
473 FREE(p->p_cred, M_SUBPROC);
474 p->p_cred = NULL;
475 }
476
477 /*
478 * Finally finished with old proc entry.
479 * Unlink it from its process group and free it.
480 */
481 leavepgrp(p);
482 LIST_REMOVE(p, p_list); /* off zombproc */
483 LIST_REMOVE(p, p_sibling);
484
485 /*
486 * Give machine-dependent layer a chance
487 * to free anything that cpu_exit couldn't
488 * release while still running in process context.
489 */
490 cpu_wait(p);
491 zfree(proc_zone, p);
492 nprocs--;
493 return (0);
494 }
495 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
496 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
497 p->p_flag |= P_WAITED;
498 q->p_retval[0] = p->p_pid;
499#ifdef COMPAT_43
500 if (compat) {
501 q->p_retval[1] = W_STOPCODE(p->p_xstat);
502 error = 0;
503 } else
504#endif
505 if (uap->status) {
506 status = W_STOPCODE(p->p_xstat);
507 error = copyout((caddr_t)&status,
508 (caddr_t)uap->status, sizeof(status));
509 } else
510 error = 0;
511 return (error);
512 }
513 }
514 if (nfound == 0)
515 return (ECHILD);
516 if (uap->options & WNOHANG) {
517 q->p_retval[0] = 0;
518 return (0);
519 }
520 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
521 return (error);
522 goto loop;
523}
524
525/*
526 * make process 'parent' the new parent of process 'child'.
527 */
528void
529proc_reparent(child, parent)
530 register struct proc *child;
531 register struct proc *parent;
532{
533
534 if (child->p_pptr == parent)
535 return;
536
537 LIST_REMOVE(child, p_sibling);
538 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
539 child->p_pptr = parent;
540}
541
542/*
543 * The next two functions are to handle adding/deleting items on the
544 * exit callout list
545 *
546 * at_exit():
547 * Take the arguments given and put them onto the exit callout list,
548 * However first make sure that it's not already there.
549 * returns 0 on success.
550 */
551int
552at_exit(function)
553 exitlist_fn function;
554{
555 ele_p ep;
556
557 /* Be noisy if the programmer has lost track of things */
558 if (rm_at_exit(function))
559 printf("exit callout entry already present\n");
560 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
561 if (ep == NULL)
562 return (ENOMEM);
563 ep->next = exit_list;
564 ep->function = function;
565 exit_list = ep;
566 return (0);
567}
568/*
569 * Scan the exit callout list for the given items and remove them.
570 * Returns the number of items removed.
571 * Logically this can only be 0 or 1.
572 */
573int
574rm_at_exit(function)
575 exitlist_fn function;
576{
577 ele_p *epp, ep;
578 int count;
579
580 count = 0;
581 epp = &exit_list;
582 ep = *epp;
583 while (ep) {
584 if (ep->function == function) {
585 *epp = ep->next;
586 free(ep, M_TEMP);
587 count++;
588 } else {
589 epp = &ep->next;
590 }
591 ep = *epp;
592 }
593 return (count);
594}