kern_exit.c revision 1.174 1 /* $NetBSD: kern_exit.c,v 1.174 2007/04/30 14:44:30 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2006, 2007 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, and by Andrew Doran.
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.174 2007/04/30 14:44:30 rmind 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/aio.h>
86 #include <sys/systm.h>
87 #include <sys/ioctl.h>
88 #include <sys/tty.h>
89 #include <sys/time.h>
90 #include <sys/resource.h>
91 #include <sys/kernel.h>
92 #include <sys/proc.h>
93 #include <sys/buf.h>
94 #include <sys/wait.h>
95 #include <sys/file.h>
96 #include <sys/vnode.h>
97 #include <sys/syslog.h>
98 #include <sys/malloc.h>
99 #include <sys/pool.h>
100 #include <sys/resourcevar.h>
101 #if defined(PERFCTRS)
102 #include <sys/pmc.h>
103 #endif
104 #include <sys/ptrace.h>
105 #include <sys/acct.h>
106 #include <sys/filedesc.h>
107 #include <sys/ras.h>
108 #include <sys/signalvar.h>
109 #include <sys/sched.h>
110 #include <sys/mount.h>
111 #include <sys/syscallargs.h>
112 #include <sys/systrace.h>
113 #include <sys/kauth.h>
114 #include <sys/sleepq.h>
115 #include <sys/lockdebug.h>
116 #include <sys/ktrace.h>
117
118 #include <machine/cpu.h>
119
120 #include <uvm/uvm_extern.h>
121
122 #define DEBUG_EXIT
123
124 #ifdef DEBUG_EXIT
125 int debug_exit = 0;
126 #define DPRINTF(x) if (debug_exit) printf x
127 #else
128 #define DPRINTF(x)
129 #endif
130
131 /*
132 * Fill in the appropriate signal information, and signal the parent.
133 */
134 static void
135 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
136 {
137
138 KSI_INIT(ksi);
139 if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
140 if (WIFSIGNALED(p->p_xstat)) {
141 if (WCOREDUMP(p->p_xstat))
142 ksi->ksi_code = CLD_DUMPED;
143 else
144 ksi->ksi_code = CLD_KILLED;
145 } else {
146 ksi->ksi_code = CLD_EXITED;
147 }
148 }
149 /*
150 * We fill those in, even for non-SIGCHLD.
151 * It's safe to access p->p_cred unlocked here.
152 */
153 ksi->ksi_pid = p->p_pid;
154 ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
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 struct proc *p = l->l_proc;
172
173 /* Don't call exit1() multiple times in the same process. */
174 mutex_enter(&p->p_smutex);
175 if (p->p_sflag & PS_WEXIT) {
176 mutex_exit(&p->p_smutex);
177 lwp_exit(l);
178 }
179
180 /* exit1() will release the mutex. */
181 exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
182 /* NOTREACHED */
183 return (0);
184 }
185
186 /*
187 * Exit: deallocate address space and other resources, change proc state
188 * to zombie, and unlink proc from allproc and parent's lists. Save exit
189 * status and rusage for wait(). Check for child processes and orphan them.
190 *
191 * Must be called with p->p_smutex held. Does not return.
192 */
193 void
194 exit1(struct lwp *l, int rv)
195 {
196 struct proc *p, *q, *nq;
197 int s;
198 ksiginfo_t ksi;
199 ksiginfoq_t kq;
200 int wakeinit;
201
202 p = l->l_proc;
203
204 KASSERT(mutex_owned(&p->p_smutex));
205
206 if (__predict_false(p == initproc))
207 panic("init died (signal %d, exit %d)",
208 WTERMSIG(rv), WEXITSTATUS(rv));
209
210 p->p_sflag |= PS_WEXIT;
211
212 /*
213 * Force all other LWPs to exit before we do. Only then can we
214 * begin to tear down the rest of the process state.
215 */
216 if (p->p_nlwps > 1)
217 exit_lwps(l);
218
219 ksiginfo_queue_init(&kq);
220
221 /*
222 * If we have been asked to stop on exit, do so now.
223 */
224 if (p->p_sflag & PS_STOPEXIT) {
225 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
226 sigclearall(p, &contsigmask, &kq);
227 p->p_waited = 0;
228 mb_write();
229 p->p_stat = SSTOP;
230 lwp_lock(l);
231 p->p_nrlwps--;
232 l->l_stat = LSSTOP;
233 mutex_exit(&p->p_smutex);
234 mi_switch(l, NULL);
235 KERNEL_LOCK(l->l_biglocks, l);
236 } else
237 mutex_exit(&p->p_smutex);
238
239 /* Destroy all AIO works */
240 aio_exit(p);
241
242 /*
243 * Drain all remaining references that procfs, ptrace and others may
244 * have on the process.
245 */
246 mutex_enter(&p->p_mutex);
247 proc_drainrefs(p);
248 mutex_exit(&p->p_mutex);
249
250 /*
251 * Bin any remaining signals and mark the process as dying so it will
252 * not be found for, e.g. signals.
253 */
254 mutex_enter(&p->p_smutex);
255 sigfillset(&p->p_sigctx.ps_sigignore);
256 sigclearall(p, NULL, &kq);
257 p->p_stat = SDYING;
258 mutex_exit(&p->p_smutex);
259 ksiginfo_queue_drain(&kq);
260
261 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
262
263 #ifdef PGINPROF
264 vmsizmon();
265 #endif
266 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
267 timers_free(p, TIMERS_ALL);
268 #if defined(__HAVE_RAS)
269 ras_purgeall(p);
270 #endif
271
272 /*
273 * Close open files, release open-file table and free signal
274 * actions. This may block!
275 */
276 fdfree(l);
277 cwdfree(p->p_cwdi);
278 p->p_cwdi = NULL;
279 doexithooks(p);
280 sigactsfree(p->p_sigacts);
281
282 /*
283 * Write out accounting data.
284 */
285 (void)acct_process(l);
286
287 #ifdef KTRACE
288 /*
289 * Release trace file.
290 */
291 if (p->p_tracep != NULL) {
292 mutex_enter(&ktrace_mutex);
293 ktrderef(p);
294 mutex_exit(&ktrace_mutex);
295 }
296 #endif
297 #ifdef SYSTRACE
298 systrace_sys_exit(p);
299 #endif
300
301 /*
302 * If emulation has process exit hook, call it now.
303 * Set the exit status now so that the exit hook has
304 * an opportunity to tweak it (COMPAT_LINUX requires
305 * this for thread group emulation)
306 */
307 p->p_xstat = rv;
308 if (p->p_emul->e_proc_exit)
309 (*p->p_emul->e_proc_exit)(p);
310
311 /* Collect child u-areas. */
312 uvm_uarea_drain(false);
313
314 /*
315 * Free the VM resources we're still holding on to.
316 * We must do this from a valid thread because doing
317 * so may block. This frees vmspace, which we don't
318 * need anymore. The only remaining lwp is the one
319 * we run at this moment, nothing runs in userland
320 * anymore.
321 */
322 uvm_proc_exit(p);
323
324 /*
325 * Stop profiling.
326 */
327 if ((p->p_stflag & PST_PROFIL) != 0) {
328 mutex_spin_enter(&p->p_stmutex);
329 stopprofclock(p);
330 mutex_spin_exit(&p->p_stmutex);
331 }
332
333 /*
334 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
335 * wake up the parent early to avoid deadlock. We can do this once
336 * the VM resources are released.
337 */
338 mutex_enter(&proclist_lock);
339
340 mutex_enter(&p->p_smutex);
341 if (p->p_sflag & PS_PPWAIT) {
342 p->p_sflag &= ~PS_PPWAIT;
343 cv_signal(&p->p_pptr->p_waitcv);
344 }
345 mutex_exit(&p->p_smutex);
346
347 if (SESS_LEADER(p)) {
348 struct vnode *vprele = NULL, *vprevoke = NULL;
349 struct session *sp = p->p_session;
350 struct tty *tp;
351
352 if (sp->s_ttyvp) {
353 /*
354 * Controlling process.
355 * Signal foreground pgrp,
356 * drain controlling terminal
357 * and revoke access to controlling terminal.
358 */
359 tp = sp->s_ttyp;
360 s = spltty();
361 TTY_LOCK(tp);
362 if (tp->t_session == sp) {
363 if (tp->t_pgrp) {
364 mutex_enter(&proclist_mutex);
365 pgsignal(tp->t_pgrp, SIGHUP, 1);
366 mutex_exit(&proclist_mutex);
367 }
368 /* we can't guarantee the revoke will do this */
369 tp->t_pgrp = NULL;
370 tp->t_session = NULL;
371 TTY_UNLOCK(tp);
372 splx(s);
373 SESSRELE(sp);
374 mutex_exit(&proclist_lock);
375 (void) ttywait(tp);
376 mutex_enter(&proclist_lock);
377
378 /*
379 * The tty could have been revoked
380 * if we blocked.
381 */
382 vprevoke = sp->s_ttyvp;
383 } else {
384 TTY_UNLOCK(tp);
385 splx(s);
386 }
387 vprele = sp->s_ttyvp;
388 sp->s_ttyvp = NULL;
389 /*
390 * s_ttyp is not zero'd; we use this to indicate
391 * that the session once had a controlling terminal.
392 * (for logging and informational purposes)
393 */
394 }
395 sp->s_leader = NULL;
396
397 if (vprevoke != NULL || vprele != NULL) {
398 mutex_exit(&proclist_lock);
399 if (vprevoke != NULL)
400 VOP_REVOKE(vprevoke, REVOKEALL);
401 if (vprele != NULL)
402 vrele(vprele);
403 mutex_enter(&proclist_lock);
404 }
405 }
406 mutex_enter(&proclist_mutex);
407 fixjobc(p, p->p_pgrp, 0);
408 mutex_exit(&proclist_mutex);
409
410 /*
411 * Finalize the last LWP's specificdata, as well as the
412 * specificdata for the proc itself.
413 */
414 lwp_finispecific(l);
415 proc_finispecific(p);
416
417 /*
418 * Notify interested parties of our demise.
419 */
420 KNOTE(&p->p_klist, NOTE_EXIT);
421
422 #if PERFCTRS
423 /*
424 * Save final PMC information in parent process & clean up.
425 */
426 if (PMC_ENABLED(p)) {
427 pmc_save_context(p);
428 pmc_accumulate(p->p_pptr, p);
429 pmc_process_exit(p);
430 }
431 #endif
432
433 /*
434 * Reset p_opptr pointer of all former children which got
435 * traced by another process and were reparented. We reset
436 * it to NULL here; the trace detach code then reparents
437 * the child to initproc. We only check allproc list, since
438 * eventual former children on zombproc list won't reference
439 * p_opptr anymore.
440 */
441 if (p->p_slflag & PSL_CHTRACED) {
442 PROCLIST_FOREACH(q, &allproc) {
443 if (q->p_opptr == p)
444 q->p_opptr = NULL;
445 }
446 }
447
448 /*
449 * Give orphaned children to init(8).
450 */
451 q = LIST_FIRST(&p->p_children);
452 wakeinit = (q != NULL);
453 for (; q != NULL; q = nq) {
454 nq = LIST_NEXT(q, p_sibling);
455
456 /*
457 * Traced processes are killed since their existence
458 * means someone is screwing up. Since we reset the
459 * trace flags, the logic in sys_wait4() would not be
460 * triggered to reparent the process to its
461 * original parent, so we must do this here.
462 */
463 if (q->p_slflag & PSL_TRACED) {
464 mutex_enter(&p->p_smutex);
465 q->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
466 mutex_exit(&p->p_smutex);
467 if (q->p_opptr != q->p_pptr) {
468 struct proc *t = q->p_opptr;
469 proc_reparent(q, t ? t : initproc);
470 q->p_opptr = NULL;
471 } else
472 proc_reparent(q, initproc);
473 killproc(q, "orphaned traced process");
474 } else
475 proc_reparent(q, initproc);
476 }
477
478 /*
479 * Move proc from allproc to zombproc, it's now nearly ready to be
480 * collected by parent.
481 */
482 mutex_enter(&proclist_mutex);
483 LIST_REMOVE(l, l_list);
484 LIST_REMOVE(p, p_list);
485 LIST_INSERT_HEAD(&zombproc, p, p_list);
486
487 /*
488 * Mark the process as dead. We must do this before we signal
489 * the parent.
490 */
491 p->p_stat = SDEAD;
492
493 /* Put in front of parent's sibling list for parent to collect it */
494 q = p->p_pptr;
495 q->p_nstopchild++;
496 if (LIST_FIRST(&q->p_children) != p) {
497 /* Put child where it can be found quickly */
498 LIST_REMOVE(p, p_sibling);
499 LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
500 }
501 mutex_exit(&proclist_mutex);
502
503 /*
504 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
505 * flag set, notify init instead (and hope it will handle
506 * this situation).
507 */
508 mutex_enter(&q->p_mutex);
509 if (q->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
510 proc_reparent(p, initproc);
511 wakeinit = 1;
512
513 /*
514 * If this was the last child of our parent, notify
515 * parent, so in case he was wait(2)ing, he will
516 * continue.
517 */
518 if (LIST_FIRST(&q->p_children) == NULL)
519 cv_signal(&q->p_waitcv);
520 }
521 mutex_exit(&q->p_mutex);
522
523 /* Reload parent pointer, since p may have been reparented above */
524 q = p->p_pptr;
525
526 if ((p->p_slflag & PSL_FSTRACE) == 0 && p->p_exitsig != 0) {
527 exit_psignal(p, q, &ksi);
528 mutex_enter(&proclist_mutex);
529 kpsignal(q, &ksi, NULL);
530 mutex_exit(&proclist_mutex);
531 }
532
533 /*
534 * Save final rusage info, adding in child rusage info and self
535 * times. It's OK to call caclru() unlocked here.
536 */
537 *p->p_ru = p->p_stats->p_ru;
538 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL, NULL);
539 ruadd(p->p_ru, &p->p_stats->p_cru);
540
541 if (wakeinit)
542 cv_signal(&initproc->p_waitcv);
543
544 /*
545 * Remaining lwp resources will be freed in lwp_exit2() once we've
546 * switch to idle context; at that point, we will be marked as a
547 * full blown zombie.
548 *
549 * XXXSMP disable preemption.
550 */
551 mutex_enter(&p->p_smutex);
552 lwp_drainrefs(l);
553 lwp_lock(l);
554 l->l_prflag &= ~LPR_DETACHED;
555 l->l_stat = LSZOMB;
556 lwp_unlock(l);
557 KASSERT(curlwp == l);
558 KASSERT(p->p_nrlwps == 1);
559 KASSERT(p->p_nlwps == 1);
560 p->p_stat = SZOMB;
561 p->p_nrlwps--;
562 p->p_nzlwps++;
563 p->p_ndlwps = 0;
564 mutex_exit(&p->p_smutex);
565
566 /*
567 * Signal the parent to collect us, and drop the proclist lock.
568 */
569 cv_signal(&p->p_pptr->p_waitcv);
570 mutex_exit(&proclist_lock);
571
572 /* Verify that we hold no locks other than the kernel lock. */
573 #ifdef MULTIPROCESSOR
574 LOCKDEBUG_BARRIER(&kernel_lock, 0);
575 #else
576 LOCKDEBUG_BARRIER(NULL, 0);
577 #endif
578
579 /*
580 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
581 */
582
583 /*
584 * Give machine-dependent code a chance to free any MD LWP
585 * resources. This must be done before uvm_lwp_exit(), in
586 * case these resources are in the PCB.
587 */
588 #ifndef __NO_CPU_LWP_FREE
589 cpu_lwp_free(l, 1);
590 #endif
591 pmap_deactivate(l);
592
593 /* This process no longer needs to hold the kernel lock. */
594 #ifdef notyet
595 /* XXXSMP hold in lwp_userret() */
596 KERNEL_UNLOCK_LAST(l);
597 #else
598 KERNEL_UNLOCK_ALL(l, NULL);
599 #endif
600
601 /*
602 * Finally, call machine-dependent code to switch to a new
603 * context (possibly the idle context). Once we are no longer
604 * using the dead lwp's stack, lwp_exit2() will be called.
605 *
606 * Note that cpu_exit() will end with a call equivalent to
607 * cpu_switch(), finishing our execution (pun intended).
608 */
609 uvmexp.swtch++; /* XXXSMP unlocked */
610 cpu_exit(l);
611 }
612
613 void
614 exit_lwps(struct lwp *l)
615 {
616 struct proc *p;
617 struct lwp *l2;
618 int error;
619 lwpid_t waited;
620 #if defined(MULTIPROCESSOR)
621 int nlocks;
622 #endif
623
624 KERNEL_UNLOCK_ALL(l, &nlocks);
625
626 p = l->l_proc;
627
628 retry:
629 /*
630 * Interrupt LWPs in interruptable sleep, unsuspend suspended
631 * LWPs and then wait for everyone else to finish.
632 */
633 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
634 if (l2 == l)
635 continue;
636 lwp_lock(l2);
637 l2->l_flag |= LW_WEXIT;
638 if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
639 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
640 /* setrunnable() will release the lock. */
641 setrunnable(l2);
642 DPRINTF(("exit_lwps: Made %d.%d runnable\n",
643 p->p_pid, l2->l_lid));
644 continue;
645 }
646 lwp_unlock(l2);
647 }
648 while (p->p_nlwps > 1) {
649 DPRINTF(("exit_lwps: waiting for %d LWPs (%d zombies)\n",
650 p->p_nlwps, p->p_nzlwps));
651 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
652 if (p->p_nlwps == 1)
653 break;
654 if (error == EDEADLK) {
655 /*
656 * LWPs can get suspended/slept behind us.
657 * (eg. sa_setwoken)
658 * kick them again and retry.
659 */
660 goto retry;
661 }
662 if (error)
663 panic("exit_lwps: lwp_wait1 failed with error %d",
664 error);
665 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
666 }
667
668 KERNEL_LOCK(nlocks, l);
669 }
670
671 int
672 sys_wait4(struct lwp *l, void *v, register_t *retval)
673 {
674 struct sys_wait4_args /* {
675 syscallarg(int) pid;
676 syscallarg(int *) status;
677 syscallarg(int) options;
678 syscallarg(struct rusage *) rusage;
679 } */ *uap = v;
680 struct proc *child, *parent;
681 int status, error;
682 struct rusage ru;
683
684 parent = l->l_proc;
685
686 if (SCARG(uap, pid) == 0)
687 SCARG(uap, pid) = -parent->p_pgid;
688 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
689 return (EINVAL);
690
691 mutex_enter(&proclist_lock);
692
693 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
694 &child, &status);
695 if (error != 0) {
696 mutex_exit(&proclist_lock);
697 return error;
698 }
699 if (child == NULL) {
700 mutex_exit(&proclist_lock);
701 *retval = 0;
702 return 0;
703 }
704
705 retval[0] = child->p_pid;
706
707 if (P_ZOMBIE(child)) {
708 KERNEL_LOCK(1, l); /* XXXSMP */
709 /* proc_free() will release the proclist_lock. */
710 proc_free(child, (SCARG(uap, rusage) == NULL ? NULL : &ru));
711 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
712
713 if (SCARG(uap, rusage))
714 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
715 if (error == 0 && SCARG(uap, status))
716 error = copyout(&status, SCARG(uap, status),
717 sizeof(status));
718
719 return error;
720 }
721
722 mutex_exit(&proclist_lock);
723
724 /* Child state must have been SSTOP. */
725 if (SCARG(uap, status)) {
726 status = W_STOPCODE(status);
727 return copyout(&status, SCARG(uap, status), sizeof(status));
728 }
729
730 return 0;
731 }
732
733 /*
734 * Scan list of child processes for a child process that has stopped or
735 * exited. Used by sys_wait4 and 'compat' equivalents.
736 *
737 * Must be called with the proclist_lock held, and may release
738 * while waiting.
739 */
740 int
741 find_stopped_child(struct proc *parent, pid_t pid, int options,
742 struct proc **child_p, int *status_p)
743 {
744 struct proc *child, *dead;
745 int error;
746
747 KASSERT(mutex_owned(&proclist_lock));
748
749 for (;;) {
750 error = ECHILD;
751 dead = NULL;
752
753 mutex_enter(&proclist_mutex);
754 LIST_FOREACH(child, &parent->p_children, p_sibling) {
755 if (pid >= 0) {
756 if (child->p_pid != pid) {
757 child = p_find(pid, PFIND_ZOMBIE |
758 PFIND_LOCKED);
759 if (child == NULL ||
760 child->p_pptr != parent) {
761 child = NULL;
762 break;
763 }
764 }
765 } else if (pid != WAIT_ANY && child->p_pgid != -pid) {
766 /* Child not in correct pgrp */
767 continue;
768 }
769
770 /*
771 * Wait for processes with p_exitsig != SIGCHLD
772 * processes only if WALTSIG is set; wait for
773 * processes with p_exitsig == SIGCHLD only
774 * if WALTSIG is clear.
775 */
776 if (((options & WALLSIG) == 0) &&
777 (options & WALTSIG ? child->p_exitsig == SIGCHLD
778 : P_EXITSIG(child) != SIGCHLD)){
779 if (child->p_pid == pid) {
780 child = NULL;
781 break;
782 }
783 continue;
784 }
785
786 error = 0;
787 if ((options & WNOZOMBIE) == 0) {
788 if (child->p_stat == SZOMB)
789 break;
790 if (child->p_stat == SDEAD) {
791 /*
792 * We may occasionally arrive here
793 * after receiving a signal, but
794 * immediatley before the child
795 * process is zombified. The wait
796 * will be short, so avoid returning
797 * to userspace.
798 */
799 dead = child;
800 }
801 }
802
803 if (child->p_stat == SSTOP &&
804 child->p_waited == 0 &&
805 (child->p_slflag & PSL_TRACED ||
806 options & WUNTRACED)) {
807 if ((options & WNOWAIT) == 0) {
808 child->p_waited = 1;
809 parent->p_nstopchild--;
810 }
811 break;
812 }
813 if (parent->p_nstopchild == 0 || child->p_pid == pid) {
814 child = NULL;
815 break;
816 }
817 }
818
819 if (child != NULL || error != 0 ||
820 ((options & WNOHANG) != 0 && dead == NULL)) {
821 if (child != NULL)
822 *status_p = child->p_xstat;
823 mutex_exit(&proclist_mutex);
824 *child_p = child;
825 return error;
826 }
827
828 /*
829 * Wait for another child process to stop.
830 */
831 mutex_exit(&proclist_lock);
832 error = cv_wait_sig(&parent->p_waitcv, &proclist_mutex);
833 mutex_exit(&proclist_mutex);
834 mutex_enter(&proclist_lock);
835
836 if (error != 0)
837 return error;
838 }
839 }
840
841 /*
842 * Free a process after parent has taken all the state info. Must be called
843 * with the proclist lock held, and will release before returning.
844 *
845 * *ru is returned to the caller, and must be freed by the caller.
846 */
847 void
848 proc_free(struct proc *p, struct rusage *caller_ru)
849 {
850 struct plimit *plim;
851 struct pstats *pstats;
852 struct rusage *ru;
853 struct proc *parent;
854 struct lwp *l;
855 ksiginfo_t ksi;
856 kauth_cred_t cred;
857 struct vnode *vp;
858 uid_t uid;
859
860 KASSERT(mutex_owned(&proclist_lock));
861 KASSERT(p->p_nlwps == 1);
862 KASSERT(p->p_nzlwps == 1);
863 KASSERT(p->p_nrlwps == 0);
864 KASSERT(p->p_stat == SZOMB);
865
866 if (caller_ru != NULL)
867 memcpy(caller_ru, p->p_ru, sizeof(*caller_ru));
868
869 /*
870 * If we got the child via ptrace(2) or procfs, and
871 * the parent is different (meaning the process was
872 * attached, rather than run as a child), then we need
873 * to give it back to the old parent, and send the
874 * parent the exit signal. The rest of the cleanup
875 * will be done when the old parent waits on the child.
876 */
877 if ((p->p_slflag & PSL_TRACED) != 0) {
878 parent = p->p_pptr;
879 if (p->p_opptr != parent){
880 mutex_enter(&p->p_smutex);
881 p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
882 mutex_exit(&p->p_smutex);
883 parent = p->p_opptr;
884 if (parent == NULL)
885 parent = initproc;
886 proc_reparent(p, parent);
887 p->p_opptr = NULL;
888 if (p->p_exitsig != 0) {
889 exit_psignal(p, parent, &ksi);
890 mutex_enter(&proclist_mutex);
891 kpsignal(parent, &ksi, NULL);
892 mutex_exit(&proclist_mutex);
893 }
894 cv_signal(&parent->p_waitcv);
895 mutex_exit(&proclist_lock);
896 return;
897 }
898 }
899
900 /*
901 * Finally finished with old proc entry. Unlink it from its process
902 * group.
903 */
904 leavepgrp(p);
905
906 parent = p->p_pptr;
907 scheduler_wait_hook(parent, p);
908 ruadd(&parent->p_stats->p_cru, p->p_ru);
909 p->p_xstat = 0;
910
911 /*
912 * At this point we are going to start freeing the final resources.
913 * If anyone tries to access the proc structure after here they will
914 * get a shock - bits are missing. Attempt to make it hard! We
915 * don't bother with any further locking past this point.
916 */
917 mutex_enter(&proclist_mutex);
918 p->p_stat = SIDL; /* not even a zombie any more */
919 LIST_REMOVE(p, p_list); /* off zombproc */
920 parent = p->p_pptr;
921 p->p_pptr->p_nstopchild--;
922 mutex_exit(&proclist_mutex);
923 LIST_REMOVE(p, p_sibling);
924
925 uid = kauth_cred_getuid(p->p_cred);
926 vp = p->p_textvp;
927 cred = p->p_cred;
928 ru = p->p_ru;
929 l = LIST_FIRST(&p->p_lwps);
930
931 mutex_destroy(&p->p_rasmutex);
932 mutex_destroy(&p->p_mutex);
933 mutex_destroy(&p->p_stmutex);
934 mutex_destroy(&p->p_smutex);
935 cv_destroy(&p->p_waitcv);
936 cv_destroy(&p->p_lwpcv);
937 cv_destroy(&p->p_refcv);
938
939 /*
940 * Delay release until after dropping the proclist lock.
941 */
942 plim = p->p_limit;
943 pstats = p->p_stats;
944
945 /*
946 * Free the proc structure and let pid be reallocated. This will
947 * release the proclist_lock.
948 */
949 proc_free_mem(p);
950
951 /*
952 * Decrement the count of procs running with this uid.
953 */
954 (void)chgproccnt(uid, -1);
955
956 /*
957 * Release substructures.
958 */
959 limfree(plim);
960 pstatsfree(pstats);
961 kauth_cred_free(cred);
962 kauth_cred_free(l->l_cred);
963
964 /*
965 * Release reference to text vnode
966 */
967 if (vp)
968 vrele(vp);
969
970 /*
971 * Free the last LWP's resources.
972 */
973 lwp_free(l, false, true);
974
975 /*
976 * Collect child u-areas.
977 */
978 uvm_uarea_drain(false);
979 pool_put(&rusage_pool, ru);
980 }
981
982 /*
983 * make process 'parent' the new parent of process 'child'.
984 *
985 * Must be called with proclist_lock lock held.
986 */
987 void
988 proc_reparent(struct proc *child, struct proc *parent)
989 {
990
991 KASSERT(mutex_owned(&proclist_lock));
992
993 if (child->p_pptr == parent)
994 return;
995
996 mutex_enter(&proclist_mutex);
997 if (child->p_stat == SZOMB ||
998 (child->p_stat == SSTOP && !child->p_waited)) {
999 child->p_pptr->p_nstopchild--;
1000 parent->p_nstopchild++;
1001 }
1002 mutex_exit(&proclist_mutex);
1003 if (parent == initproc)
1004 child->p_exitsig = SIGCHLD;
1005
1006 LIST_REMOVE(child, p_sibling);
1007 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1008 child->p_pptr = parent;
1009 }
1010