kern_exit.c revision 1.145 1 /* $NetBSD: kern_exit.c,v 1.145 2005/02/26 21:34:55 perry Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1989, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.145 2005/02/26 21:34:55 perry Exp $");
78
79 #include "opt_ktrace.h"
80 #include "opt_perfctrs.h"
81 #include "opt_systrace.h"
82 #include "opt_sysv.h"
83
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/ioctl.h>
87 #include <sys/proc.h>
88 #include <sys/tty.h>
89 #include <sys/time.h>
90 #include <sys/resource.h>
91 #include <sys/kernel.h>
92 #include <sys/ktrace.h>
93 #include <sys/proc.h>
94 #include <sys/buf.h>
95 #include <sys/wait.h>
96 #include <sys/file.h>
97 #include <sys/vnode.h>
98 #include <sys/syslog.h>
99 #include <sys/malloc.h>
100 #include <sys/pool.h>
101 #include <sys/resourcevar.h>
102 #if defined(PERFCTRS)
103 #include <sys/pmc.h>
104 #endif
105 #include <sys/ptrace.h>
106 #include <sys/acct.h>
107 #include <sys/filedesc.h>
108 #include <sys/ras.h>
109 #include <sys/signalvar.h>
110 #include <sys/sched.h>
111 #include <sys/sa.h>
112 #include <sys/savar.h>
113 #include <sys/mount.h>
114 #include <sys/syscallargs.h>
115 #include <sys/systrace.h>
116
117 #include <machine/cpu.h>
118
119 #include <uvm/uvm_extern.h>
120
121 #define DEBUG_EXIT
122
123 #ifdef DEBUG_EXIT
124 int debug_exit = 0;
125 #define DPRINTF(x) if (debug_exit) printf x
126 #else
127 #define DPRINTF(x)
128 #endif
129
130 static void lwp_exit_hook(struct lwp *, void *);
131
132 /*
133 * Fill in the appropriate signal information, and signal the parent.
134 */
135 static void
136 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
137 {
138
139 (void)memset(ksi, 0, sizeof(ksiginfo_t));
140 if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
141 if (WIFSIGNALED(p->p_xstat)) {
142 if (WCOREDUMP(p->p_xstat))
143 ksi->ksi_code = CLD_DUMPED;
144 else
145 ksi->ksi_code = CLD_KILLED;
146 } else {
147 ksi->ksi_code = CLD_EXITED;
148 }
149 }
150 /*
151 * we fill those in, even for non-SIGCHLD.
152 */
153 ksi->ksi_pid = p->p_pid;
154 ksi->ksi_uid = p->p_ucred->cr_uid;
155 ksi->ksi_status = p->p_xstat;
156 /* XXX: is this still valid? */
157 ksi->ksi_utime = p->p_ru->ru_utime.tv_sec;
158 ksi->ksi_stime = p->p_ru->ru_stime.tv_sec;
159 }
160
161 /*
162 * exit --
163 * Death of process.
164 */
165 int
166 sys_exit(struct lwp *l, void *v, register_t *retval)
167 {
168 struct sys_exit_args /* {
169 syscallarg(int) rval;
170 } */ *uap = v;
171
172 /* Don't call exit1() multiple times in the same process.*/
173 if (l->l_proc->p_flag & P_WEXIT)
174 lwp_exit(l);
175
176 exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
177 /* NOTREACHED */
178 return (0);
179 }
180
181 /*
182 * Exit: deallocate address space and other resources, change proc state
183 * to zombie, and unlink proc from allproc and parent's lists. Save exit
184 * status and rusage for wait(). Check for child processes and orphan them.
185 */
186 void
187 exit1(struct lwp *l, int rv)
188 {
189 struct proc *p, *q, *nq;
190 int s, sa;
191 struct plimit *plim;
192 struct pstats *pstats;
193 struct sigacts *ps;
194 ksiginfo_t ksi;
195 int do_psignal = 0;
196
197 p = l->l_proc;
198
199 if (__predict_false(p == initproc))
200 panic("init died (signal %d, exit %d)",
201 WTERMSIG(rv), WEXITSTATUS(rv));
202
203 p->p_flag |= P_WEXIT;
204 if (p->p_flag & P_STOPEXIT) {
205 int s;
206
207 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
208 SCHED_LOCK(s);
209 p->p_stat = SSTOP;
210 l->l_stat = LSSTOP;
211 p->p_nrlwps--;
212 mi_switch(l, NULL);
213 SCHED_ASSERT_UNLOCKED();
214 splx(s);
215 }
216
217 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
218 /*
219 * Disable scheduler activation upcalls.
220 * We're trying to get out of here.
221 */
222 sa = 0;
223 if (p->p_sa != NULL) {
224 l->l_flag &= ~L_SA;
225 #if 0
226 p->p_flag &= ~P_SA;
227 #endif
228 sa = 1;
229 }
230
231 #ifdef PGINPROF
232 vmsizmon();
233 #endif
234 if (p->p_flag & P_PROFIL)
235 stopprofclock(p);
236 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
237 /*
238 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
239 * wake up the parent early to avoid deadlock.
240 */
241 if (p->p_flag & P_PPWAIT) {
242 p->p_flag &= ~P_PPWAIT;
243 wakeup(p->p_pptr);
244 }
245 sigfillset(&p->p_sigctx.ps_sigignore);
246 sigemptyset(&p->p_sigctx.ps_siglist);
247 p->p_sigctx.ps_sigcheck = 0;
248 timers_free(p, TIMERS_ALL);
249
250 if (sa || (p->p_nlwps > 1)) {
251 exit_lwps(l);
252
253 /*
254 * Collect thread u-areas.
255 */
256 uvm_uarea_drain(FALSE);
257 }
258
259 #if defined(__HAVE_RAS)
260 ras_purgeall(p);
261 #endif
262
263 /*
264 * Close open files and release open-file table.
265 * This may block!
266 */
267 fdfree(p);
268 cwdfree(p->p_cwdi);
269 p->p_cwdi = 0;
270
271 doexithooks(p);
272
273 if (SESS_LEADER(p)) {
274 struct session *sp = p->p_session;
275 struct tty *tp;
276
277 if (sp->s_ttyvp) {
278 int s;
279 /*
280 * Controlling process.
281 * Signal foreground pgrp,
282 * drain controlling terminal
283 * and revoke access to controlling terminal.
284 */
285 tp = sp->s_ttyp;
286 s = spltty();
287 TTY_LOCK(tp);
288 if (tp->t_session == sp) {
289 if (tp->t_pgrp)
290 pgsignal(tp->t_pgrp, SIGHUP, 1);
291 /* we can't guarantee the revoke will do this */
292 tp->t_pgrp = NULL;
293 tp->t_session = NULL;
294 TTY_UNLOCK(tp);
295 splx(s);
296 SESSRELE(sp);
297 (void) ttywait(tp);
298 /*
299 * The tty could have been revoked
300 * if we blocked.
301 */
302 if (sp->s_ttyvp)
303 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
304 } else {
305 TTY_UNLOCK(tp);
306 splx(s);
307 }
308 if (sp->s_ttyvp)
309 vrele(sp->s_ttyvp);
310 sp->s_ttyvp = NULL;
311 /*
312 * s_ttyp is not zero'd; we use this to indicate
313 * that the session once had a controlling terminal.
314 * (for logging and informational purposes)
315 */
316 }
317 sp->s_leader = NULL;
318 }
319 fixjobc(p, p->p_pgrp, 0);
320 (void)acct_process(p);
321 #ifdef KTRACE
322 /*
323 * release trace file
324 */
325 ktrderef(p);
326 #endif
327 #ifdef SYSTRACE
328 systrace_sys_exit(p);
329 #endif
330 /*
331 * If emulation has process exit hook, call it now.
332 */
333 if (p->p_emul->e_proc_exit)
334 (*p->p_emul->e_proc_exit)(p);
335
336 /*
337 * Free the VM resources we're still holding on to.
338 * We must do this from a valid thread because doing
339 * so may block. This frees vmspace, which we don't
340 * need anymore. The only remaining lwp is the one
341 * we run at this moment, nothing runs in userland
342 * anymore.
343 */
344 uvm_proc_exit(p);
345
346 /*
347 * Give machine-dependent code a chance to free any
348 * MD LWP resources while we can still block. This must be done
349 * before uvm_lwp_exit(), in case these resources are in the
350 * PCB.
351 * THIS IS LAST BLOCKING OPERATION.
352 */
353 #ifndef __NO_CPU_LWP_FREE
354 cpu_lwp_free(l, 1);
355 #endif
356
357 pmap_deactivate(l);
358
359 /*
360 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
361 */
362
363 /*
364 * Save exit status and final rusage info, adding in child rusage
365 * info and self times.
366 * In order to pick up the time for the current execution, we must
367 * do this before unlinking the lwp from l_list.
368 */
369 p->p_xstat = rv;
370 *p->p_ru = p->p_stats->p_ru;
371 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
372 ruadd(p->p_ru, &p->p_stats->p_cru);
373
374 /*
375 * Notify interested parties of our demise.
376 */
377 KNOTE(&p->p_klist, NOTE_EXIT);
378
379 #if PERFCTRS
380 /*
381 * Save final PMC information in parent process & clean up.
382 */
383 if (PMC_ENABLED(p)) {
384 pmc_save_context(p);
385 pmc_accumulate(p->p_pptr, p);
386 pmc_process_exit(p);
387 }
388 #endif
389
390 s = proclist_lock_write();
391 /*
392 * Reset p_opptr pointer of all former children which got
393 * traced by another process and were reparented. We reset
394 * it to NULL here; the trace detach code then reparents
395 * the child to initproc. We only check allproc list, since
396 * eventual former children on zombproc list won't reference
397 * p_opptr anymore.
398 */
399 if (p->p_flag & P_CHTRACED) {
400 PROCLIST_FOREACH(q, &allproc) {
401 if (q->p_opptr == p)
402 q->p_opptr = NULL;
403 }
404 }
405
406 /*
407 * Give orphaned children to init(8).
408 */
409 q = LIST_FIRST(&p->p_children);
410 if (q) /* only need this if any child is SZOMB */
411 wakeup(initproc);
412 for (; q != NULL; q = nq) {
413 nq = LIST_NEXT(q, p_sibling);
414
415 /*
416 * Traced processes are killed since their existence
417 * means someone is screwing up. Since we reset the
418 * trace flags, the logic in sys_wait4() would not be
419 * triggered to reparent the process to its
420 * original parent, so we must do this here.
421 */
422 if (q->p_flag & P_TRACED) {
423 if (q->p_opptr != q->p_pptr) {
424 struct proc *t = q->p_opptr;
425 proc_reparent(q, t ? t : initproc);
426 q->p_opptr = NULL;
427 } else
428 proc_reparent(q, initproc);
429 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
430 psignal(q, SIGKILL);
431 } else {
432 proc_reparent(q, initproc);
433 }
434 }
435
436 /*
437 * Move proc from allproc to zombproc, it's now ready
438 * to be collected by parent. Remaining lwp resources
439 * will be freed in lwp_exit2() once we've switch to idle
440 * context.
441 * Changing the state to SZOMB stops it being found by pfind().
442 */
443 LIST_REMOVE(p, p_list);
444 LIST_INSERT_HEAD(&zombproc, p, p_list);
445 p->p_stat = SZOMB;
446
447 LIST_REMOVE(l, l_list);
448 LIST_REMOVE(l, l_sibling);
449 l->l_flag |= L_DETACHED|L_PROCEXIT; /* detached from proc too */
450 l->l_stat = LSDEAD;
451
452 p->p_nrlwps--;
453 p->p_nlwps--;
454
455 /* Put in front of parent's sibling list for parent to collect it */
456 q = p->p_pptr;
457 q->p_nstopchild++;
458 if (LIST_FIRST(&q->p_children) != p) {
459 /* Put child where it can be found quickly */
460 LIST_REMOVE(p, p_sibling);
461 LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
462 }
463
464 /*
465 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
466 * flag set, notify init instead (and hope it will handle
467 * this situation).
468 */
469 if (q->p_flag & P_NOCLDWAIT) {
470 proc_reparent(p, initproc);
471
472 /*
473 * If this was the last child of our parent, notify
474 * parent, so in case he was wait(2)ing, he will
475 * continue.
476 */
477 if (LIST_FIRST(&q->p_children) == NULL)
478 wakeup(q);
479 }
480
481 /*
482 * Clear curlwp after we've done all operations
483 * that could block, and before tearing down the rest
484 * of the process state that might be used from clock, etc.
485 * Also, can't clear curlwp while we're still runnable,
486 * as we're not on a run queue (we are current, just not
487 * a proper proc any longer!).
488 *
489 * Other substructures are freed from wait().
490 */
491 curlwp = NULL;
492
493 /* Delay release until after dropping the proclist lock */
494 plim = p->p_limit;
495 pstats = p->p_stats;
496 ps = p->p_sigacts;
497
498 p->p_limit = NULL;
499 p->p_stats = NULL;
500 p->p_sigacts = NULL;
501
502 /* Reload parent pointer, since p may have been reparented above */
503 q = p->p_pptr;
504
505 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) {
506 exit_psignal(p, q, &ksi);
507 do_psignal = 1;
508 }
509
510 /*
511 * Once we release the proclist lock, we shouldn't touch the
512 * process structure anymore, since it's now on the zombie
513 * list and available for collection by the parent.
514 */
515 proclist_unlock_write(s);
516
517 if (do_psignal)
518 kpsignal(q, &ksi, NULL);
519
520 /* Wake up the parent so it can get exit status. */
521 wakeup(q);
522
523 /* Release substructures */
524 sigactsfree(ps);
525 limfree(plim);
526 pstatsfree(pstats);
527
528 #ifdef DEBUG
529 /* Nothing should use the process link anymore */
530 l->l_proc = NULL;
531 #endif
532
533 /* This process no longer needs to hold the kernel lock. */
534 KERNEL_PROC_UNLOCK(l);
535
536 /*
537 * Finally, call machine-dependent code to switch to a new
538 * context (possibly the idle context). Once we are no longer
539 * using the dead lwp's stack, lwp_exit2() will be called
540 * to arrange for the resources to be released.
541 *
542 * Note that cpu_exit() will end with a call equivalent to
543 * cpu_switch(), finishing our execution (pun intended).
544 */
545
546 uvmexp.swtch++;
547 cpu_exit(l);
548 }
549
550 void
551 exit_lwps(struct lwp *l)
552 {
553 struct proc *p;
554 struct lwp *l2;
555 struct sadata_vp *vp;
556 int s, error;
557 lwpid_t waited;
558
559 p = l->l_proc;
560
561 /* XXX SMP
562 * This would be the right place to IPI any LWPs running on
563 * other processors so that they can notice the userret exit hook.
564 */
565 p->p_userret = lwp_exit_hook;
566 p->p_userret_arg = NULL;
567
568 if (p->p_sa) {
569 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
570 /*
571 * Make SA-cached LWPs normal process runnable
572 * LWPs so that they'll also self-destruct.
573 */
574 DPRINTF(("exit_lwps: Making cached LWPs of %d on VP %d runnable: ",
575 p->p_pid, vp->savp_id));
576 SCHED_LOCK(s);
577 while ((l2 = sa_getcachelwp(vp)) != 0) {
578 l2->l_priority = l2->l_usrpri;
579 setrunnable(l2);
580 DPRINTF(("%d ", l2->l_lid));
581 }
582 SCHED_UNLOCK(s);
583 DPRINTF(("\n"));
584
585 /*
586 * Clear wokenq, the LWPs on the queue will
587 * run below.
588 */
589 vp->savp_wokenq_head = NULL;
590 }
591 }
592
593 /*
594 * Interrupt LWPs in interruptable sleep, unsuspend suspended
595 * LWPs, make detached LWPs undetached (so we can wait for
596 * them) and then wait for everyone else to finish.
597 */
598 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
599 l2->l_flag &= ~(L_DETACHED|L_SA);
600
601 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
602 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
603 SCHED_LOCK(s);
604 setrunnable(l2);
605 SCHED_UNLOCK(s);
606 DPRINTF(("exit_lwps: Made %d.%d runnable\n",
607 p->p_pid, l2->l_lid));
608 }
609 }
610
611
612 while (p->p_nlwps > 1) {
613 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
614 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
615 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
616 if (error)
617 panic("exit_lwps: lwp_wait1 failed with error %d\n",
618 error);
619 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
620 }
621
622 p->p_userret = NULL;
623 }
624
625 /* Wrapper function for use in p_userret */
626 static void
627 lwp_exit_hook(struct lwp *l, void *arg)
628 {
629 KERNEL_PROC_LOCK(l);
630 lwp_exit(l);
631 }
632
633 int
634 sys_wait4(struct lwp *l, void *v, register_t *retval)
635 {
636 struct sys_wait4_args /* {
637 syscallarg(int) pid;
638 syscallarg(int *) status;
639 syscallarg(int) options;
640 syscallarg(struct rusage *) rusage;
641 } */ *uap = v;
642 struct proc *child, *parent;
643 int status, error;
644
645 parent = l->l_proc;
646
647 if (SCARG(uap, pid) == 0)
648 SCARG(uap, pid) = -parent->p_pgid;
649 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
650 return (EINVAL);
651
652 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
653 &child);
654 if (error != 0)
655 return error;
656 if (child == NULL) {
657 *retval = 0;
658 return 0;
659 }
660
661 /*
662 * Collect child u-areas.
663 */
664 uvm_uarea_drain(FALSE);
665
666 retval[0] = child->p_pid;
667
668 if (P_ZOMBIE(child)) {
669 if (SCARG(uap, status)) {
670 status = child->p_xstat; /* convert to int */
671 error = copyout(&status, SCARG(uap, status),
672 sizeof(status));
673 if (error)
674 return (error);
675 }
676 if (SCARG(uap, rusage)) {
677 error = copyout(child->p_ru, SCARG(uap, rusage),
678 sizeof(struct rusage));
679 if (error)
680 return (error);
681 }
682
683 proc_free(child);
684 return 0;
685 }
686
687 /* child state must be SSTOP */
688 if (SCARG(uap, status)) {
689 status = W_STOPCODE(child->p_xstat);
690 return copyout(&status, SCARG(uap, status), sizeof(status));
691 }
692 return 0;
693 }
694
695 /*
696 * Scan list of child processes for a child process that has stopped or
697 * exited. Used by sys_wait4 and 'compat' equivalents.
698 */
699 int
700 find_stopped_child(struct proc *parent, pid_t pid, int options,
701 struct proc **child_p)
702 {
703 struct proc *child;
704 int error;
705
706 for (;;) {
707 proclist_lock_read();
708 error = ECHILD;
709 LIST_FOREACH(child, &parent->p_children, p_sibling) {
710 if (pid >= 0) {
711 if (child->p_pid != pid) {
712 child = p_find(pid, PFIND_ZOMBIE |
713 PFIND_LOCKED);
714 if (child == NULL
715 || child->p_pptr != parent) {
716 child = NULL;
717 break;
718 }
719 }
720 } else
721 if (pid != WAIT_ANY && child->p_pgid != -pid)
722 /* child not in correct pgrp */
723 continue;
724 /*
725 * Wait for processes with p_exitsig != SIGCHLD
726 * processes only if WALTSIG is set; wait for
727 * processes with p_exitsig == SIGCHLD only
728 * if WALTSIG is clear.
729 */
730 if (((options & WALLSIG) == 0) &&
731 (options & WALTSIG ? child->p_exitsig == SIGCHLD
732 : P_EXITSIG(child) != SIGCHLD)){
733 if (child->p_pid == pid) {
734 child = NULL;
735 break;
736 }
737 continue;
738 }
739
740 error = 0;
741 if (child->p_stat == SZOMB && !(options & WNOZOMBIE))
742 break;
743
744 if (child->p_stat == SSTOP &&
745 (child->p_flag & P_WAITED) == 0 &&
746 (child->p_flag & P_TRACED || options & WUNTRACED)) {
747 if ((options & WNOWAIT) == 0) {
748 child->p_flag |= P_WAITED;
749 parent->p_nstopchild--;
750 }
751 break;
752 }
753 if (parent->p_nstopchild == 0 || child->p_pid == pid) {
754 child = NULL;
755 break;
756 }
757 }
758 proclist_unlock_read();
759 if (child != NULL || error != 0 || options & WNOHANG) {
760 *child_p = child;
761 return error;
762 }
763 error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
764 if (error != 0)
765 return error;
766 }
767 }
768
769 /*
770 * Free a process after parent has taken all the state info.
771 */
772 void
773 proc_free(struct proc *p)
774 {
775 struct proc *parent = p->p_pptr;
776 ksiginfo_t ksi;
777 int s;
778
779 KASSERT(p->p_nlwps == 0);
780 KASSERT(p->p_nzlwps == 0);
781 KASSERT(p->p_nrlwps == 0);
782 KASSERT(LIST_EMPTY(&p->p_lwps));
783
784 /*
785 * If we got the child via ptrace(2) or procfs, and
786 * the parent is different (meaning the process was
787 * attached, rather than run as a child), then we need
788 * to give it back to the old parent, and send the
789 * parent the exit signal. The rest of the cleanup
790 * will be done when the old parent waits on the child.
791 */
792 if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
793 parent = p->p_opptr;
794 if (parent == NULL)
795 parent = initproc;
796 proc_reparent(p, parent);
797 p->p_opptr = NULL;
798 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
799 if (p->p_exitsig != 0) {
800 exit_psignal(p, parent, &ksi);
801 kpsignal(parent, &ksi, NULL);
802 }
803 wakeup(parent);
804 return;
805 }
806
807 scheduler_wait_hook(parent, p);
808 p->p_xstat = 0;
809
810 ruadd(&parent->p_stats->p_cru, p->p_ru);
811
812 /*
813 * At this point we are going to start freeing the final resources.
814 * If anyone tries to access the proc structure after here they
815 * will get a shock - bits are missing.
816 * Attempt to make it hard!
817 */
818
819 p->p_stat = SIDL; /* not even a zombie any more */
820
821 pool_put(&rusage_pool, p->p_ru);
822
823 /*
824 * Finally finished with old proc entry.
825 * Unlink it from its process group and free it.
826 */
827 leavepgrp(p);
828
829 s = proclist_lock_write();
830 LIST_REMOVE(p, p_list); /* off zombproc */
831 p->p_pptr->p_nstopchild--;
832 LIST_REMOVE(p, p_sibling);
833 proclist_unlock_write(s);
834
835 /*
836 * Decrement the count of procs running with this uid.
837 */
838 (void)chgproccnt(p->p_cred->p_ruid, -1);
839
840 /*
841 * Free up credentials.
842 */
843 if (--p->p_cred->p_refcnt == 0) {
844 crfree(p->p_cred->pc_ucred);
845 pool_put(&pcred_pool, p->p_cred);
846 }
847
848 /*
849 * Release reference to text vnode
850 */
851 if (p->p_textvp)
852 vrele(p->p_textvp);
853
854 /* Release any SA state. */
855 if (p->p_sa)
856 sa_release(p);
857
858 /* Free proc structure and let pid be reallocated */
859 proc_free_mem(p);
860 }
861
862 /*
863 * make process 'parent' the new parent of process 'child'.
864 *
865 * Must be called with proclist_lock_write() held.
866 */
867 void
868 proc_reparent(struct proc *child, struct proc *parent)
869 {
870
871 if (child->p_pptr == parent)
872 return;
873
874 if (child->p_stat == SZOMB
875 || (child->p_stat == SSTOP && !(child->p_flag & P_WAITED))) {
876 child->p_pptr->p_nstopchild--;
877 parent->p_nstopchild++;
878 }
879 if (parent == initproc)
880 child->p_exitsig = SIGCHLD;
881
882 LIST_REMOVE(child, p_sibling);
883 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
884 child->p_pptr = parent;
885 }
886