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