kern_exit.c revision 1.249 1 /* $NetBSD: kern_exit.c,v 1.249 2016/04/02 20:38:40 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2006, 2007, 2008 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1989, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.249 2016/04/02 20:38:40 christos Exp $");
71
72 #include "opt_ktrace.h"
73 #include "opt_dtrace.h"
74 #include "opt_perfctrs.h"
75 #include "opt_sysv.h"
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/ioctl.h>
80 #include <sys/tty.h>
81 #include <sys/time.h>
82 #include <sys/resource.h>
83 #include <sys/kernel.h>
84 #include <sys/proc.h>
85 #include <sys/buf.h>
86 #include <sys/wait.h>
87 #include <sys/file.h>
88 #include <sys/vnode.h>
89 #include <sys/syslog.h>
90 #include <sys/pool.h>
91 #include <sys/uidinfo.h>
92 #if defined(PERFCTRS)
93 #include <sys/pmc.h>
94 #endif
95 #include <sys/ptrace.h>
96 #include <sys/acct.h>
97 #include <sys/filedesc.h>
98 #include <sys/ras.h>
99 #include <sys/signalvar.h>
100 #include <sys/sched.h>
101 #include <sys/mount.h>
102 #include <sys/syscallargs.h>
103 #include <sys/kauth.h>
104 #include <sys/sleepq.h>
105 #include <sys/lockdebug.h>
106 #include <sys/ktrace.h>
107 #include <sys/cpu.h>
108 #include <sys/lwpctl.h>
109 #include <sys/atomic.h>
110 #include <sys/sdt.h>
111
112 #include <uvm/uvm_extern.h>
113
114 #ifdef DEBUG_EXIT
115 int debug_exit = 0;
116 #define DPRINTF(x) if (debug_exit) printf x
117 #else
118 #define DPRINTF(x)
119 #endif
120
121 static int find_stopped_child(struct proc *, idtype_t, id_t, int,
122 struct proc **, int *, struct wrusage *, siginfo_t *);
123 static void proc_free(struct proc *, struct wrusage *);
124
125 /*
126 * DTrace SDT provider definitions
127 */
128 SDT_PROVIDER_DECLARE(proc);
129 SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
130
131 /*
132 * Fill in the appropriate signal information, and signal the parent.
133 */
134 /* XXX noclone works around a gcc 4.5 bug on arm */
135 static void __noclone
136 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
137 {
138
139 KSI_INIT(ksi);
140 if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
141 if (WIFSIGNALED(p->p_xstat)) {
142 if (WCOREDUMP(p->p_xstat))
143 ksi->ksi_code = CLD_DUMPED;
144 else
145 ksi->ksi_code = CLD_KILLED;
146 } else {
147 ksi->ksi_code = CLD_EXITED;
148 }
149 }
150 /*
151 * We fill those in, even for non-SIGCHLD.
152 * It's safe to access p->p_cred unlocked here.
153 */
154 ksi->ksi_pid = p->p_pid;
155 ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
156 ksi->ksi_status = p->p_xstat;
157 /* XXX: is this still valid? */
158 ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
159 ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
160 }
161
162 /*
163 * exit --
164 * Death of process.
165 */
166 int
167 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
168 {
169 /* {
170 syscallarg(int) rval;
171 } */
172 struct proc *p = l->l_proc;
173
174 /* Don't call exit1() multiple times in the same process. */
175 mutex_enter(p->p_lock);
176 if (p->p_sflag & PS_WEXIT) {
177 mutex_exit(p->p_lock);
178 lwp_exit(l);
179 }
180
181 /* exit1() will release the mutex. */
182 exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
183 /* NOTREACHED */
184 return (0);
185 }
186
187 /*
188 * Exit: deallocate address space and other resources, change proc state
189 * to zombie, and unlink proc from allproc and parent's lists. Save exit
190 * status and rusage for wait(). Check for child processes and orphan them.
191 *
192 * Must be called with p->p_lock held. Does not return.
193 */
194 void
195 exit1(struct lwp *l, int rv)
196 {
197 struct proc *p, *child, *next_child, *old_parent, *new_parent;
198 struct pgrp *pgrp;
199 ksiginfo_t ksi;
200 ksiginfoq_t kq;
201 int wakeinit;
202
203 p = l->l_proc;
204
205 KASSERT(mutex_owned(p->p_lock));
206 KASSERT(p->p_vmspace != NULL);
207
208 if (__predict_false(p == initproc)) {
209 panic("init died (signal %d, exit %d)",
210 WTERMSIG(rv), WEXITSTATUS(rv));
211 }
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
223 ksiginfo_queue_init(&kq);
224
225 /*
226 * If we have been asked to stop on exit, do so now.
227 */
228 if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
229 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
230 sigclearall(p, &contsigmask, &kq);
231
232 if (!mutex_tryenter(proc_lock)) {
233 mutex_exit(p->p_lock);
234 mutex_enter(proc_lock);
235 mutex_enter(p->p_lock);
236 }
237 p->p_waited = 0;
238 p->p_pptr->p_nstopchild++;
239 p->p_stat = SSTOP;
240 mutex_exit(proc_lock);
241 lwp_lock(l);
242 p->p_nrlwps--;
243 l->l_stat = LSSTOP;
244 lwp_unlock(l);
245 mutex_exit(p->p_lock);
246 lwp_lock(l);
247 mi_switch(l);
248 KERNEL_LOCK(l->l_biglocks, l);
249 mutex_enter(p->p_lock);
250 }
251
252 /*
253 * Bin any remaining signals and mark the process as dying so it will
254 * not be found for, e.g. signals.
255 */
256 sigfillset(&p->p_sigctx.ps_sigignore);
257 sigclearall(p, NULL, &kq);
258 p->p_stat = SDYING;
259 mutex_exit(p->p_lock);
260 ksiginfo_queue_drain(&kq);
261
262 /* Destroy any lwpctl info. */
263 if (p->p_lwpctl != NULL)
264 lwp_ctl_exit();
265
266 /*
267 * Drain all remaining references that procfs, ptrace and others may
268 * have on the process.
269 */
270 rw_enter(&p->p_reflock, RW_WRITER);
271
272 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
273
274 timers_free(p, TIMERS_ALL);
275 #if defined(__HAVE_RAS)
276 ras_purgeall();
277 #endif
278
279 /*
280 * Close open files, release open-file table and free signal
281 * actions. This may block!
282 */
283 fd_free();
284 cwdfree(p->p_cwdi);
285 p->p_cwdi = NULL;
286 doexithooks(p);
287 sigactsfree(p->p_sigacts);
288
289 /*
290 * Write out accounting data.
291 */
292 (void)acct_process(l);
293
294 #ifdef KTRACE
295 /*
296 * Release trace file.
297 */
298 if (p->p_tracep != NULL) {
299 mutex_enter(&ktrace_lock);
300 ktrderef(p);
301 mutex_exit(&ktrace_lock);
302 }
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 /*
316 * Free the VM resources we're still holding on to.
317 * We must do this from a valid thread because doing
318 * so may block. This frees vmspace, which we don't
319 * need anymore. The only remaining lwp is the one
320 * we run at this moment, nothing runs in userland
321 * anymore.
322 */
323 uvm_proc_exit(p);
324
325 /*
326 * Stop profiling.
327 */
328 if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
329 mutex_spin_enter(&p->p_stmutex);
330 stopprofclock(p);
331 mutex_spin_exit(&p->p_stmutex);
332 }
333
334 /*
335 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we
336 * wake up the parent early to avoid deadlock. We can do this once
337 * the VM resources are released.
338 */
339 mutex_enter(proc_lock);
340 if (p->p_lflag & PL_PPWAIT) {
341 #if 0
342 lwp_t *lp;
343
344 l->l_lwpctl = NULL; /* was on loan from blocked parent */
345 p->p_lflag &= ~PL_PPWAIT;
346
347 lp = p->p_vforklwp;
348 p->p_vforklwp = NULL;
349 lp->l_pflag &= ~LP_VFORKWAIT; /* XXX */
350 cv_broadcast(&lp->l_waitcv);
351 #else
352 l->l_lwpctl = NULL; /* was on loan from blocked parent */
353 p->p_lflag &= ~PL_PPWAIT;
354 cv_broadcast(&p->p_pptr->p_waitcv);
355 #endif
356 }
357
358 if (SESS_LEADER(p)) {
359 struct vnode *vprele = NULL, *vprevoke = NULL;
360 struct session *sp = p->p_session;
361 struct tty *tp;
362
363 if (sp->s_ttyvp) {
364 /*
365 * Controlling process.
366 * Signal foreground pgrp,
367 * drain controlling terminal
368 * and revoke access to controlling terminal.
369 */
370 tp = sp->s_ttyp;
371 mutex_spin_enter(&tty_lock);
372 if (tp->t_session == sp) {
373 /* we can't guarantee the revoke will do this */
374 pgrp = tp->t_pgrp;
375 tp->t_pgrp = NULL;
376 tp->t_session = NULL;
377 mutex_spin_exit(&tty_lock);
378 if (pgrp != NULL) {
379 pgsignal(pgrp, SIGHUP, 1);
380 }
381 mutex_exit(proc_lock);
382 (void) ttywait(tp);
383 mutex_enter(proc_lock);
384
385 /* The tty could have been revoked. */
386 vprevoke = sp->s_ttyvp;
387 } else
388 mutex_spin_exit(&tty_lock);
389 vprele = sp->s_ttyvp;
390 sp->s_ttyvp = NULL;
391 /*
392 * s_ttyp is not zero'd; we use this to indicate
393 * that the session once had a controlling terminal.
394 * (for logging and informational purposes)
395 */
396 }
397 sp->s_leader = NULL;
398
399 if (vprevoke != NULL || vprele != NULL) {
400 if (vprevoke != NULL) {
401 /* Releases proc_lock. */
402 proc_sessrele(sp);
403 VOP_REVOKE(vprevoke, REVOKEALL);
404 } else
405 mutex_exit(proc_lock);
406 if (vprele != NULL)
407 vrele(vprele);
408 mutex_enter(proc_lock);
409 }
410 }
411 fixjobc(p, p->p_pgrp, 0);
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 SDT_PROBE(proc, kernel, , exit,
426 (WCOREDUMP(rv) ? CLD_DUMPED :
427 (WIFSIGNALED(rv) ? CLD_KILLED : CLD_EXITED)),
428 0,0,0,0);
429
430 #if PERFCTRS
431 /*
432 * Save final PMC information in parent process & clean up.
433 */
434 if (PMC_ENABLED(p)) {
435 pmc_save_context(p);
436 pmc_accumulate(p->p_pptr, p);
437 pmc_process_exit(p);
438 }
439 #endif
440
441 /*
442 * Reset p_opptr pointer of all former children which got
443 * traced by another process and were reparented. We reset
444 * it to NULL here; the trace detach code then reparents
445 * the child to initproc. We only check allproc list, since
446 * eventual former children on zombproc list won't reference
447 * p_opptr anymore.
448 */
449 if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
450 struct proc *q;
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 child = LIST_FIRST(&p->p_children);
461 wakeinit = (child != NULL);
462 for (; child != NULL; child = next_child) {
463 next_child = LIST_NEXT(child, 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 (__predict_false(child->p_slflag & PSL_TRACED)) {
473 mutex_enter(p->p_lock);
474 child->p_slflag &=
475 ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
476 mutex_exit(p->p_lock);
477 if (child->p_opptr != child->p_pptr) {
478 struct proc *t = child->p_opptr;
479 proc_reparent(child, t ? t : initproc);
480 child->p_opptr = NULL;
481 } else
482 proc_reparent(child, initproc);
483 killproc(child, "orphaned traced process");
484 } else
485 proc_reparent(child, initproc);
486 }
487
488 /*
489 * Move proc from allproc to zombproc, it's now nearly ready to be
490 * collected by parent.
491 */
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 old_parent = p->p_pptr;
504 old_parent->p_nstopchild++;
505 if (LIST_FIRST(&old_parent->p_children) != p) {
506 /* Put child where it can be found quickly */
507 LIST_REMOVE(p, p_sibling);
508 LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling);
509 }
510
511 /*
512 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
513 * flag set, notify init instead (and hope it will handle
514 * this situation).
515 */
516 if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
517 proc_reparent(p, initproc);
518 wakeinit = 1;
519
520 /*
521 * If this was the last child of our parent, notify
522 * parent, so in case he was wait(2)ing, he will
523 * continue.
524 */
525 if (LIST_FIRST(&old_parent->p_children) == NULL)
526 cv_broadcast(&old_parent->p_waitcv);
527 }
528
529 /* Reload parent pointer, since p may have been reparented above */
530 new_parent = p->p_pptr;
531
532 if (__predict_false((p->p_slflag & PSL_FSTRACE) == 0 &&
533 p->p_exitsig != 0)) {
534 exit_psignal(p, new_parent, &ksi);
535 kpsignal(new_parent, &ksi, NULL);
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 * Release any PCU resources before becoming a zombie.
549 */
550 pcu_discard_all(l);
551
552 mutex_enter(p->p_lock);
553 /* Free the linux lwp id */
554 if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid)
555 proc_free_pid(l->l_lid);
556 lwp_drainrefs(l);
557 lwp_lock(l);
558 l->l_prflag &= ~LPR_DETACHED;
559 l->l_stat = LSZOMB;
560 lwp_unlock(l);
561 KASSERT(curlwp == l);
562 KASSERT(p->p_nrlwps == 1);
563 KASSERT(p->p_nlwps == 1);
564 p->p_stat = SZOMB;
565 p->p_nrlwps--;
566 p->p_nzlwps++;
567 p->p_ndlwps = 0;
568 mutex_exit(p->p_lock);
569
570 /*
571 * Signal the parent to collect us, and drop the proclist lock.
572 * Drop debugger/procfs lock; no new references can be gained.
573 */
574 cv_broadcast(&p->p_pptr->p_waitcv);
575 rw_exit(&p->p_reflock);
576 mutex_exit(proc_lock);
577
578 /* Verify that we hold no locks other than the kernel lock. */
579 LOCKDEBUG_BARRIER(&kernel_lock, 0);
580
581 /*
582 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
583 */
584
585 /*
586 * Give machine-dependent code a chance to free any MD LWP
587 * resources. This must be done before uvm_lwp_exit(), in
588 * case these resources are in the PCB.
589 */
590 cpu_lwp_free(l, 1);
591
592 pmap_deactivate(l);
593
594 /* This process no longer needs to hold the kernel lock. */
595 #ifdef notyet
596 /* XXXSMP hold in lwp_userret() */
597 KERNEL_UNLOCK_LAST(l);
598 #else
599 KERNEL_UNLOCK_ALL(l, NULL);
600 #endif
601
602 lwp_exit_switchaway(l);
603 }
604
605 void
606 exit_lwps(struct lwp *l)
607 {
608 proc_t *p = l->l_proc;
609 lwp_t *l2;
610 int nlocks;
611
612 KERNEL_UNLOCK_ALL(l, &nlocks);
613 retry:
614 KASSERT(mutex_owned(p->p_lock));
615
616 /*
617 * Interrupt LWPs in interruptable sleep, unsuspend suspended
618 * LWPs and then wait for everyone else to finish.
619 */
620 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
621 if (l2 == l)
622 continue;
623 lwp_lock(l2);
624 l2->l_flag |= LW_WEXIT;
625 if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
626 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
627 /* setrunnable() will release the lock. */
628 setrunnable(l2);
629 continue;
630 }
631 lwp_unlock(l2);
632 }
633
634 /*
635 * Wait for every LWP to exit. Note: LWPs can get suspended/slept
636 * behind us or there may even be new LWPs created. Therefore, a
637 * full retry is required on error.
638 */
639 while (p->p_nlwps > 1) {
640 if (lwp_wait(l, 0, NULL, true)) {
641 goto retry;
642 }
643 }
644
645 KERNEL_LOCK(nlocks, l);
646 KASSERT(p->p_nlwps == 1);
647 }
648
649 static int
650 do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options,
651 struct wrusage *wru, siginfo_t *si)
652 {
653 proc_t *child;
654 int error;
655
656
657 if (wru != NULL)
658 memset(wru, 0, sizeof(*wru));
659 if (si != NULL)
660 memset(si, 0, sizeof(*si));
661
662 mutex_enter(proc_lock);
663 error = find_stopped_child(curproc, idtype, id, options, &child, status,
664 wru, si);
665 if (child == NULL) {
666 mutex_exit(proc_lock);
667 *pid = 0;
668 return error;
669 }
670 *pid = child->p_pid;
671
672 if (child->p_stat == SZOMB) {
673 /* proc_free() will release the proc_lock. */
674 if (options & WNOWAIT) {
675 mutex_exit(proc_lock);
676 } else {
677 proc_free(child, wru);
678 }
679 } else {
680 /* Child state must have been SSTOP. */
681 mutex_exit(proc_lock);
682 *status = W_STOPCODE(*status);
683 }
684 return 0;
685 }
686
687 int
688 do_sys_wait(int *pid, int *status, int options, struct rusage *ru)
689 {
690 idtype_t idtype;
691 id_t id;
692 int ret;
693 struct wrusage wru;
694
695 /*
696 * Translate the special pid values into the (idtype, pid)
697 * pair for wait6. The WAIT_MYPGRP case is handled by
698 * find_stopped_child() on its own.
699 */
700 if (*pid == WAIT_ANY) {
701 idtype = P_ALL;
702 id = 0;
703 } else if (*pid < 0) {
704 idtype = P_PGID;
705 id = (id_t)-*pid;
706 } else {
707 idtype = P_PID;
708 id = (id_t)*pid;
709 }
710 options |= WEXITED | WTRAPPED;
711 ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL,
712 NULL);
713 if (ru)
714 *ru = wru.wru_self;
715 return ret;
716 }
717
718 int
719 sys___wait450(struct lwp *l, const struct sys___wait450_args *uap,
720 register_t *retval)
721 {
722 /* {
723 syscallarg(int) pid;
724 syscallarg(int *) status;
725 syscallarg(int) options;
726 syscallarg(struct rusage *) rusage;
727 } */
728 int error, status, pid = SCARG(uap, pid);
729 struct rusage ru;
730
731 error = do_sys_wait(&pid, &status, SCARG(uap, options),
732 SCARG(uap, rusage) != NULL ? &ru : NULL);
733
734 retval[0] = pid;
735 if (pid == 0) {
736 return error;
737 }
738 if (SCARG(uap, status)) {
739 error = copyout(&status, SCARG(uap, status), sizeof(status));
740 }
741 if (SCARG(uap, rusage) && error == 0) {
742 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
743 }
744 return error;
745 }
746
747 int
748 sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval)
749 {
750 /* {
751 syscallarg(idtype_t) idtype;
752 syscallarg(id_t) id;
753 syscallarg(int *) status;
754 syscallarg(int) options;
755 syscallarg(struct wrusage *) wru;
756 syscallarg(siginfo_t *) si;
757 } */
758 struct wrusage wru, *wrup;
759 siginfo_t si, *sip;
760 idtype_t idtype;
761 int pid;
762 id_t id;
763 int error, status;
764
765 idtype = SCARG(uap, idtype);
766 id = SCARG(uap, id);
767
768 if (SCARG(uap, wru) != NULL)
769 wrup = &wru;
770 else
771 wrup = NULL;
772
773 if (SCARG(uap, info) != NULL)
774 sip = &si;
775 else
776 sip = NULL;
777
778 /*
779 * We expect all callers of wait6() to know about WEXITED and
780 * WTRAPPED.
781 */
782 error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options),
783 wrup, sip);
784
785 if (SCARG(uap, status) != NULL && error == 0)
786 error = copyout(&status, SCARG(uap, status), sizeof(status));
787 if (SCARG(uap, wru) != NULL && error == 0)
788 error = copyout(&wru, SCARG(uap, wru), sizeof(wru));
789 if (SCARG(uap, info) != NULL && error == 0)
790 error = copyout(&si, SCARG(uap, info), sizeof(si));
791 return error;
792 }
793
794
795 static int
796 match_process(struct proc *pp, struct proc **q, idtype_t idtype, id_t id,
797 int options, struct wrusage *wrusage, siginfo_t *siginfo)
798 {
799 struct rusage *rup;
800 struct proc *p = *q;
801
802 mutex_enter(p->p_lock);
803 switch (idtype) {
804 case P_ALL:
805 break;
806 case P_PID:
807 if (p->p_pid != (pid_t)id) {
808 mutex_exit(p->p_lock);
809 p = *q = proc_find_raw((pid_t)id);
810 if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) {
811 *q = NULL;
812 return -1;
813 }
814 mutex_enter(p->p_lock);
815 }
816 break;
817 case P_PGID:
818 if (p->p_pgid != (pid_t)id)
819 goto out;
820 break;
821 case P_SID:
822 if (p->p_session->s_sid != (pid_t)id)
823 goto out;
824 break;
825 case P_UID:
826 if (kauth_cred_getuid(p->p_cred) != (uid_t)id)
827 goto out;
828 break;
829 case P_GID:
830 if (kauth_cred_getgid(p->p_cred) != (gid_t)id)
831 goto out;
832 break;
833 case P_CID:
834 case P_PSETID:
835 case P_CPUID:
836 /* XXX: Implement me */
837 default:
838 out:
839 mutex_exit(p->p_lock);
840 return 0;
841 }
842
843 if ((options & WEXITED) == 0 && p->p_stat == SZOMB)
844 goto out;
845
846 if (siginfo != NULL) {
847 siginfo->si_errno = 0;
848
849 /*
850 * SUSv4 requires that the si_signo value is always
851 * SIGCHLD. Obey it despite the rfork(2) interface
852 * allows to request other signal for child exit
853 * notification.
854 */
855 siginfo->si_signo = SIGCHLD;
856
857 /*
858 * This is still a rough estimate. We will fix the
859 * cases TRAPPED, STOPPED, and CONTINUED later.
860 */
861 if (WCOREDUMP(p->p_xstat)) {
862 siginfo->si_code = CLD_DUMPED;
863 siginfo->si_status = WTERMSIG(p->p_xstat);
864 } else if (WIFSIGNALED(p->p_xstat)) {
865 siginfo->si_code = CLD_KILLED;
866 siginfo->si_status = WTERMSIG(p->p_xstat);
867 } else {
868 siginfo->si_code = CLD_EXITED;
869 siginfo->si_status = WEXITSTATUS(p->p_xstat);
870 }
871
872 siginfo->si_pid = p->p_pid;
873 siginfo->si_uid = kauth_cred_getuid(p->p_cred);
874
875 /*
876 * The si_addr field would be useful additional
877 * detail, but apparently the PC value may be lost
878 * when we reach this point. bzero() above sets
879 * siginfo->si_addr to NULL.
880 */
881 }
882
883 /*
884 * There should be no reason to limit resources usage info to
885 * exited processes only. A snapshot about any resources used
886 * by a stopped process may be exactly what is needed.
887 */
888 if (wrusage != NULL) {
889 rup = &wrusage->wru_self;
890 *rup = p->p_stats->p_ru;
891 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
892
893 rup = &wrusage->wru_children;
894 *rup = p->p_stats->p_cru;
895 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
896 }
897
898 mutex_exit(p->p_lock);
899 return 1;
900 }
901
902 /*
903 * Scan list of child processes for a child process that has stopped or
904 * exited. Used by sys_wait4 and 'compat' equivalents.
905 *
906 * Must be called with the proc_lock held, and may release while waiting.
907 */
908 static int
909 find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options,
910 struct proc **child_p, int *status_p, struct wrusage *wru, siginfo_t *si)
911 {
912 struct proc *child, *dead;
913 int error;
914
915 KASSERT(mutex_owned(proc_lock));
916
917 if (options & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG|WTRAPPED|WEXITED|
918 WNOWAIT|WCONTINUED)
919 && !(options & WOPTSCHECKED)) {
920 *child_p = NULL;
921 return EINVAL;
922 }
923
924 if ((options & (WEXITED|WUNTRACED|WCONTINUED|WTRAPPED)) == 0) {
925 /*
926 * We will be unable to find any matching processes,
927 * because there are no known events to look for.
928 * Prefer to return error instead of blocking
929 * indefinitely.
930 */
931 *child_p = NULL;
932 return EINVAL;
933 }
934
935 if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
936 mutex_enter(parent->p_lock);
937 id = (id_t)parent->p_pgid;
938 mutex_exit(parent->p_lock);
939 idtype = P_PGID;
940 }
941
942 for (;;) {
943 error = ECHILD;
944 dead = NULL;
945
946 LIST_FOREACH(child, &parent->p_children, p_sibling) {
947 int rv = match_process(parent, &child, idtype, id,
948 options, wru, si);
949 if (rv == -1)
950 break;
951 if (rv == 0)
952 continue;
953
954 /*
955 * Wait for processes with p_exitsig != SIGCHLD
956 * processes only if WALTSIG is set; wait for
957 * processes with p_exitsig == SIGCHLD only
958 * if WALTSIG is clear.
959 */
960 if (((options & WALLSIG) == 0) &&
961 (options & WALTSIG ? child->p_exitsig == SIGCHLD
962 : P_EXITSIG(child) != SIGCHLD)){
963 continue;
964 }
965
966 error = 0;
967 if ((options & WNOZOMBIE) == 0) {
968 if (child->p_stat == SZOMB)
969 break;
970 if (child->p_stat == SDEAD) {
971 /*
972 * We may occasionally arrive here
973 * after receiving a signal, but
974 * immediately before the child
975 * process is zombified. The wait
976 * will be short, so avoid returning
977 * to userspace.
978 */
979 dead = child;
980 }
981 }
982
983 if ((options & WTRAPPED) != 0 &&
984 child->p_stat == SSTOP &&
985 child->p_waited == 0 &&
986 (child->p_slflag & PSL_TRACED ||
987 options & WUNTRACED)) {
988 if ((options & WNOWAIT) == 0) {
989 child->p_waited = 1;
990 parent->p_nstopchild--;
991 }
992 if (si) {
993 si->si_status = child->p_xstat;
994 si->si_code = CLD_TRAPPED;
995 }
996 break;
997 }
998 if (parent->p_nstopchild == 0) {
999 child = NULL;
1000 break;
1001 }
1002 }
1003 /* XXX: WCONTINUED? */
1004
1005 if (child != NULL || error != 0 ||
1006 ((options & WNOHANG) != 0 && dead == NULL)) {
1007 if (child != NULL) {
1008 *status_p = child->p_xstat;
1009 }
1010 *child_p = child;
1011 return error;
1012 }
1013
1014 /*
1015 * Wait for another child process to stop.
1016 */
1017 error = cv_wait_sig(&parent->p_waitcv, proc_lock);
1018
1019 if (error != 0) {
1020 *child_p = NULL;
1021 return error;
1022 }
1023 }
1024 }
1025
1026 /*
1027 * Free a process after parent has taken all the state info. Must be called
1028 * with the proclist lock held, and will release before returning.
1029 *
1030 * *ru is returned to the caller, and must be freed by the caller.
1031 */
1032 static void
1033 proc_free(struct proc *p, struct wrusage *wru)
1034 {
1035 struct proc *parent = p->p_pptr;
1036 struct lwp *l;
1037 ksiginfo_t ksi;
1038 kauth_cred_t cred1, cred2;
1039 uid_t uid;
1040
1041 KASSERT(mutex_owned(proc_lock));
1042 KASSERT(p->p_nlwps == 1);
1043 KASSERT(p->p_nzlwps == 1);
1044 KASSERT(p->p_nrlwps == 0);
1045 KASSERT(p->p_stat == SZOMB);
1046
1047 /*
1048 * If we got the child via ptrace(2) or procfs, and
1049 * the parent is different (meaning the process was
1050 * attached, rather than run as a child), then we need
1051 * to give it back to the old parent, and send the
1052 * parent the exit signal. The rest of the cleanup
1053 * will be done when the old parent waits on the child.
1054 */
1055 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
1056 mutex_enter(p->p_lock);
1057 p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
1058 mutex_exit(p->p_lock);
1059 parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
1060 proc_reparent(p, parent);
1061 p->p_opptr = NULL;
1062 if (p->p_exitsig != 0) {
1063 exit_psignal(p, parent, &ksi);
1064 kpsignal(parent, &ksi, NULL);
1065 }
1066 cv_broadcast(&parent->p_waitcv);
1067 mutex_exit(proc_lock);
1068 return;
1069 }
1070
1071 sched_proc_exit(parent, p);
1072
1073 /*
1074 * Add child times of exiting process onto its own times.
1075 * This cannot be done any earlier else it might get done twice.
1076 */
1077 l = LIST_FIRST(&p->p_lwps);
1078 p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
1079 p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
1080 ruadd(&p->p_stats->p_ru, &l->l_ru);
1081 ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
1082 ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
1083 if (wru != NULL) {
1084 wru->wru_self = p->p_stats->p_ru;
1085 wru->wru_children = p->p_stats->p_cru;
1086 }
1087 p->p_xstat = 0;
1088
1089 /*
1090 * At this point we are going to start freeing the final resources.
1091 * If anyone tries to access the proc structure after here they will
1092 * get a shock - bits are missing. Attempt to make it hard! We
1093 * don't bother with any further locking past this point.
1094 */
1095 p->p_stat = SIDL; /* not even a zombie any more */
1096 LIST_REMOVE(p, p_list); /* off zombproc */
1097 parent->p_nstopchild--;
1098 LIST_REMOVE(p, p_sibling);
1099
1100 /*
1101 * Let pid be reallocated.
1102 */
1103 proc_free_pid(p->p_pid);
1104
1105 /*
1106 * Unlink process from its process group.
1107 * Releases the proc_lock.
1108 */
1109 proc_leavepgrp(p);
1110
1111 /*
1112 * Delay release until after lwp_free.
1113 */
1114 cred2 = l->l_cred;
1115
1116 /*
1117 * Free the last LWP's resources.
1118 *
1119 * lwp_free ensures the LWP is no longer running on another CPU.
1120 */
1121 lwp_free(l, false, true);
1122
1123 /*
1124 * Now no one except us can reach the process p.
1125 */
1126
1127 /*
1128 * Decrement the count of procs running with this uid.
1129 */
1130 cred1 = p->p_cred;
1131 uid = kauth_cred_getuid(cred1);
1132 (void)chgproccnt(uid, -1);
1133
1134 /*
1135 * Release substructures.
1136 */
1137
1138 lim_free(p->p_limit);
1139 pstatsfree(p->p_stats);
1140 kauth_cred_free(cred1);
1141 kauth_cred_free(cred2);
1142
1143 /*
1144 * Release reference to text vnode
1145 */
1146 if (p->p_textvp)
1147 vrele(p->p_textvp);
1148
1149 mutex_destroy(&p->p_auxlock);
1150 mutex_obj_free(p->p_lock);
1151 mutex_destroy(&p->p_stmutex);
1152 cv_destroy(&p->p_waitcv);
1153 cv_destroy(&p->p_lwpcv);
1154 rw_destroy(&p->p_reflock);
1155
1156 proc_free_mem(p);
1157 }
1158
1159 /*
1160 * make process 'parent' the new parent of process 'child'.
1161 *
1162 * Must be called with proc_lock held.
1163 */
1164 void
1165 proc_reparent(struct proc *child, struct proc *parent)
1166 {
1167
1168 KASSERT(mutex_owned(proc_lock));
1169
1170 if (child->p_pptr == parent)
1171 return;
1172
1173 if (child->p_stat == SZOMB || child->p_stat == SDEAD ||
1174 (child->p_stat == SSTOP && !child->p_waited)) {
1175 child->p_pptr->p_nstopchild--;
1176 parent->p_nstopchild++;
1177 }
1178 if (parent == initproc)
1179 child->p_exitsig = SIGCHLD;
1180
1181 LIST_REMOVE(child, p_sibling);
1182 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1183 child->p_pptr = parent;
1184 child->p_ppid = parent->p_pid;
1185 }
1186