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