Deleted Added
full compact
kern_exit.c (41059) kern_exit.c (41086)
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
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 $
39 * $Id: kern_exit.c,v 1.68 1998/11/10 09:16:29 peter 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 /*
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 * Reset any sigio structures pointing to us as a result of
191 * F_SETOWN with our pid.
192 */
193 funsetownlst(&p->p_sigiolst);
194
195 /*
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}
196 * Close open files and release open-file table.
197 * This may block!
198 */
199 fdfree(p);
200
201 /*
202 * XXX Shutdown SYSV semaphores
203 */
204 semexit(p);
205
206 /* The next two chunks should probably be moved to vmspace_exit. */
207 vm = p->p_vmspace;
208 /*
209 * Release user portion of address space.
210 * This releases references to vnodes,
211 * which could cause I/O if the file has been unlinked.
212 * Need to do this early enough that we can still sleep.
213 * Can't free the entire vmspace as the kernel stack
214 * may be mapped within that space also.
215 */
216 if (vm->vm_refcnt == 1) {
217 if (vm->vm_shm)
218 shmexit(p);
219 pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS,
220 VM_MAXUSER_ADDRESS);
221 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
222 VM_MAXUSER_ADDRESS);
223 }
224
225 if (SESS_LEADER(p)) {
226 register struct session *sp = p->p_session;
227
228 if (sp->s_ttyvp) {
229 /*
230 * Controlling process.
231 * Signal foreground pgrp,
232 * drain controlling terminal
233 * and revoke access to controlling terminal.
234 */
235 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
236 if (sp->s_ttyp->t_pgrp)
237 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
238 (void) ttywait(sp->s_ttyp);
239 /*
240 * The tty could have been revoked
241 * if we blocked.
242 */
243 if (sp->s_ttyvp)
244 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
245 }
246 if (sp->s_ttyvp)
247 vrele(sp->s_ttyvp);
248 sp->s_ttyvp = NULL;
249 /*
250 * s_ttyp is not zero'd; we use this to indicate
251 * that the session once had a controlling terminal.
252 * (for logging and informational purposes)
253 */
254 }
255 sp->s_leader = NULL;
256 }
257 fixjobc(p, p->p_pgrp, 0);
258 (void)acct_process(p);
259#ifdef KTRACE
260 /*
261 * release trace file
262 */
263 p->p_traceflag = 0; /* don't trace the vrele() */
264 if (p->p_tracep)
265 vrele(p->p_tracep);
266#endif
267 /*
268 * Remove proc from allproc queue and pidhash chain.
269 * Place onto zombproc. Unlink from parent's child list.
270 */
271 LIST_REMOVE(p, p_list);
272 LIST_INSERT_HEAD(&zombproc, p, p_list);
273 p->p_stat = SZOMB;
274
275 LIST_REMOVE(p, p_hash);
276
277 q = p->p_children.lh_first;
278 if (q) /* only need this if any child is S_ZOMB */
279 wakeup((caddr_t) initproc);
280 for (; q != 0; q = nq) {
281 nq = q->p_sibling.le_next;
282 LIST_REMOVE(q, p_sibling);
283 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
284 q->p_pptr = initproc;
285 /*
286 * Traced processes are killed
287 * since their existence means someone is screwing up.
288 */
289 if (q->p_flag & P_TRACED) {
290 q->p_flag &= ~P_TRACED;
291 psignal(q, SIGKILL);
292 }
293 }
294
295 /*
296 * Save exit status and final rusage info, adding in child rusage
297 * info and self times.
298 */
299 p->p_xstat = rv;
300 *p->p_ru = p->p_stats->p_ru;
301 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
302 ruadd(p->p_ru, &p->p_stats->p_cru);
303
304 /*
305 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
306 * flag set, notify process 1 instead (and hope it will handle
307 * this situation).
308 */
309 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
310 struct proc *pp = p->p_pptr;
311 proc_reparent(p, initproc);
312 /*
313 * If this was the last child of our parent, notify
314 * parent, so in case he was wait(2)ing, he will
315 * continue.
316 */
317 if (LIST_EMPTY(&pp->p_children))
318 wakeup((caddr_t)pp);
319 }
320
321 psignal(p->p_pptr, SIGCHLD);
322 wakeup((caddr_t)p->p_pptr);
323#if defined(tahoe)
324 /* move this to cpu_exit */
325 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
326#endif
327 /*
328 * Clear curproc after we've done all operations
329 * that could block, and before tearing down the rest
330 * of the process state that might be used from clock, etc.
331 * Also, can't clear curproc while we're still runnable,
332 * as we're not on a run queue (we are current, just not
333 * a proper proc any longer!).
334 *
335 * Other substructures are freed from wait().
336 */
337 curproc = NULL;
338 if (--p->p_limit->p_refcnt == 0) {
339 FREE(p->p_limit, M_SUBPROC);
340 p->p_limit = NULL;
341 }
342
343 /*
344 * Finally, call machine-dependent code to release the remaining
345 * resources including address space, the kernel stack and pcb.
346 * The address space is released by "vmspace_free(p->p_vmspace)";
347 * This is machine-dependent, as we may have to change stacks
348 * or ensure that the current one isn't reallocated before we
349 * finish. cpu_exit will end with a call to cpu_switch(), finishing
350 * our execution (pun intended).
351 */
352 cpu_exit(p);
353}
354
355#ifdef COMPAT_43
356#if defined(hp300) || defined(luna68k)
357#include <machine/frame.h>
358#define GETPS(rp) ((struct frame *)(rp))->f_sr
359#else
360#define GETPS(rp) (rp)[PS]
361#endif
362
363int
364owait(p, uap)
365 struct proc *p;
366 register struct owait_args /* {
367 int dummy;
368 } */ *uap;
369{
370 struct wait_args w;
371
372#ifdef PSL_ALLCC
373 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
374 w.options = 0;
375 w.rusage = NULL;
376 } else {
377 w.options = p->p_md.md_regs[R0];
378 w.rusage = (struct rusage *)p->p_md.md_regs[R1];
379 }
380#else
381 w.options = 0;
382 w.rusage = NULL;
383#endif
384 w.pid = WAIT_ANY;
385 w.status = NULL;
386 return (wait1(p, &w, 1));
387}
388#endif /* COMPAT_43 */
389
390int
391wait4(p, uap)
392 struct proc *p;
393 struct wait_args *uap;
394{
395
396 return (wait1(p, uap, 0));
397}
398
399static int
400wait1(q, uap, compat)
401 register struct proc *q;
402 register struct wait_args /* {
403 int pid;
404 int *status;
405 int options;
406 struct rusage *rusage;
407 } */ *uap;
408 int compat;
409{
410 register int nfound;
411 register struct proc *p, *t;
412 int status, error;
413
414 if (uap->pid == 0)
415 uap->pid = -q->p_pgid;
416 if (uap->options &~ (WUNTRACED|WNOHANG))
417 return (EINVAL);
418loop:
419 nfound = 0;
420 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
421 if (uap->pid != WAIT_ANY &&
422 p->p_pid != uap->pid && p->p_pgid != -uap->pid)
423 continue;
424 nfound++;
425 if (p->p_stat == SZOMB) {
426 /* charge childs scheduling cpu usage to parent */
427 if (curproc->p_pid != 1) {
428 curproc->p_estcpu = min(curproc->p_estcpu +
429 p->p_estcpu, UCHAR_MAX);
430 }
431
432 q->p_retval[0] = p->p_pid;
433#ifdef COMPAT_43
434 if (compat)
435 q->p_retval[1] = p->p_xstat;
436 else
437#endif
438 if (uap->status) {
439 status = p->p_xstat; /* convert to int */
440 if ((error = copyout((caddr_t)&status,
441 (caddr_t)uap->status, sizeof(status))))
442 return (error);
443 }
444 if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
445 (caddr_t)uap->rusage, sizeof (struct rusage))))
446 return (error);
447 /*
448 * If we got the child via a ptrace 'attach',
449 * we need to give it back to the old parent.
450 */
451 if (p->p_oppid && (t = pfind(p->p_oppid))) {
452 p->p_oppid = 0;
453 proc_reparent(p, t);
454 psignal(t, SIGCHLD);
455 wakeup((caddr_t)t);
456 return (0);
457 }
458 p->p_xstat = 0;
459 ruadd(&q->p_stats->p_cru, p->p_ru);
460 FREE(p->p_ru, M_ZOMBIE);
461 p->p_ru = NULL;
462
463 /*
464 * Decrement the count of procs running with this uid.
465 */
466 (void)chgproccnt(p->p_cred->p_ruid, -1);
467
468 /*
469 * Release reference to text vnode
470 */
471 if (p->p_textvp)
472 vrele(p->p_textvp);
473
474 /*
475 * Free up credentials.
476 */
477 if (--p->p_cred->p_refcnt == 0) {
478 crfree(p->p_cred->pc_ucred);
479 FREE(p->p_cred, M_SUBPROC);
480 p->p_cred = NULL;
481 }
482
483 /*
484 * Finally finished with old proc entry.
485 * Unlink it from its process group and free it.
486 */
487 leavepgrp(p);
488 LIST_REMOVE(p, p_list); /* off zombproc */
489 LIST_REMOVE(p, p_sibling);
490
491 /*
492 * Give machine-dependent layer a chance
493 * to free anything that cpu_exit couldn't
494 * release while still running in process context.
495 */
496 cpu_wait(p);
497 zfree(proc_zone, p);
498 nprocs--;
499 return (0);
500 }
501 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
502 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
503 p->p_flag |= P_WAITED;
504 q->p_retval[0] = p->p_pid;
505#ifdef COMPAT_43
506 if (compat) {
507 q->p_retval[1] = W_STOPCODE(p->p_xstat);
508 error = 0;
509 } else
510#endif
511 if (uap->status) {
512 status = W_STOPCODE(p->p_xstat);
513 error = copyout((caddr_t)&status,
514 (caddr_t)uap->status, sizeof(status));
515 } else
516 error = 0;
517 return (error);
518 }
519 }
520 if (nfound == 0)
521 return (ECHILD);
522 if (uap->options & WNOHANG) {
523 q->p_retval[0] = 0;
524 return (0);
525 }
526 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
527 return (error);
528 goto loop;
529}
530
531/*
532 * make process 'parent' the new parent of process 'child'.
533 */
534void
535proc_reparent(child, parent)
536 register struct proc *child;
537 register struct proc *parent;
538{
539
540 if (child->p_pptr == parent)
541 return;
542
543 LIST_REMOVE(child, p_sibling);
544 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
545 child->p_pptr = parent;
546}
547
548/*
549 * The next two functions are to handle adding/deleting items on the
550 * exit callout list
551 *
552 * at_exit():
553 * Take the arguments given and put them onto the exit callout list,
554 * However first make sure that it's not already there.
555 * returns 0 on success.
556 */
557int
558at_exit(function)
559 exitlist_fn function;
560{
561 ele_p ep;
562
563 /* Be noisy if the programmer has lost track of things */
564 if (rm_at_exit(function))
565 printf("exit callout entry already present\n");
566 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
567 if (ep == NULL)
568 return (ENOMEM);
569 ep->next = exit_list;
570 ep->function = function;
571 exit_list = ep;
572 return (0);
573}
574/*
575 * Scan the exit callout list for the given items and remove them.
576 * Returns the number of items removed.
577 * Logically this can only be 0 or 1.
578 */
579int
580rm_at_exit(function)
581 exitlist_fn function;
582{
583 ele_p *epp, ep;
584 int count;
585
586 count = 0;
587 epp = &exit_list;
588 ep = *epp;
589 while (ep) {
590 if (ep->function == function) {
591 *epp = ep->next;
592 free(ep, M_TEMP);
593 count++;
594 } else {
595 epp = &ep->next;
596 }
597 ep = *epp;
598 }
599 return (count);
600}