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