kern_exit.c revision 1.253 1 /* $NetBSD: kern_exit.c,v 1.253 2016/04/04 23:07:06 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.253 2016/04/04 23:07:06 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 (p->p_xsig) {
142 if (p->p_sflag & PS_COREDUMP)
143 ksi->ksi_code = CLD_DUMPED;
144 else
145 ksi->ksi_code = CLD_KILLED;
146 ksi->ksi_status = p->p_xsig;
147 } else {
148 ksi->ksi_code = CLD_EXITED;
149 ksi->ksi_status = p->p_xexit;
150 }
151 } else {
152 ksi->ksi_code = SI_USER;
153 ksi->ksi_status = p->p_xsig;
154 }
155 /*
156 * We fill those in, even for non-SIGCHLD.
157 * It's safe to access p->p_cred unlocked here.
158 */
159 ksi->ksi_pid = p->p_pid;
160 ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
161 /* XXX: is this still valid? */
162 ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
163 ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
164 }
165
166 /*
167 * exit --
168 * Death of process.
169 */
170 int
171 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
172 {
173 /* {
174 syscallarg(int) rval;
175 } */
176 struct proc *p = l->l_proc;
177
178 /* Don't call exit1() multiple times in the same process. */
179 mutex_enter(p->p_lock);
180 if (p->p_sflag & PS_WEXIT) {
181 mutex_exit(p->p_lock);
182 lwp_exit(l);
183 }
184
185 /* exit1() will release the mutex. */
186 exit1(l, SCARG(uap, rval), 0);
187 /* NOTREACHED */
188 return (0);
189 }
190
191 /*
192 * Exit: deallocate address space and other resources, change proc state
193 * to zombie, and unlink proc from allproc and parent's lists. Save exit
194 * status and rusage for wait(). Check for child processes and orphan them.
195 *
196 * Must be called with p->p_lock held. Does not return.
197 */
198 void
199 exit1(struct lwp *l, int exitcode, int signo)
200 {
201 struct proc *p, *child, *next_child, *old_parent, *new_parent;
202 struct pgrp *pgrp;
203 ksiginfo_t ksi;
204 ksiginfoq_t kq;
205 int wakeinit;
206
207 p = l->l_proc;
208
209 KASSERT(mutex_owned(p->p_lock));
210 KASSERT(p->p_vmspace != NULL);
211
212 if (__predict_false(p == initproc)) {
213 panic("init died (signal %d, exit %d)", signo, exitcode);
214 }
215
216 p->p_sflag |= PS_WEXIT;
217
218 /*
219 * Force all other LWPs to exit before we do. Only then can we
220 * begin to tear down the rest of the process state.
221 */
222 if (p->p_nlwps > 1) {
223 exit_lwps(l);
224 }
225
226 ksiginfo_queue_init(&kq);
227
228 /*
229 * If we have been asked to stop on exit, do so now.
230 */
231 if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
232 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
233 sigclearall(p, &contsigmask, &kq);
234
235 if (!mutex_tryenter(proc_lock)) {
236 mutex_exit(p->p_lock);
237 mutex_enter(proc_lock);
238 mutex_enter(p->p_lock);
239 }
240 p->p_waited = 0;
241 p->p_pptr->p_nstopchild++;
242 p->p_stat = SSTOP;
243 mutex_exit(proc_lock);
244 lwp_lock(l);
245 p->p_nrlwps--;
246 l->l_stat = LSSTOP;
247 lwp_unlock(l);
248 mutex_exit(p->p_lock);
249 lwp_lock(l);
250 mi_switch(l);
251 KERNEL_LOCK(l->l_biglocks, l);
252 mutex_enter(p->p_lock);
253 }
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 sigfillset(&p->p_sigctx.ps_sigignore);
260 sigclearall(p, NULL, &kq);
261 p->p_stat = SDYING;
262 mutex_exit(p->p_lock);
263 ksiginfo_queue_drain(&kq);
264
265 /* Destroy any lwpctl info. */
266 if (p->p_lwpctl != NULL)
267 lwp_ctl_exit();
268
269 /*
270 * Drain all remaining references that procfs, ptrace and others may
271 * have on the process.
272 */
273 rw_enter(&p->p_reflock, RW_WRITER);
274
275 DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid));
276
277 timers_free(p, TIMERS_ALL);
278 #if defined(__HAVE_RAS)
279 ras_purgeall();
280 #endif
281
282 /*
283 * Close open files, release open-file table and free signal
284 * actions. This may block!
285 */
286 fd_free();
287 cwdfree(p->p_cwdi);
288 p->p_cwdi = NULL;
289 doexithooks(p);
290 sigactsfree(p->p_sigacts);
291
292 /*
293 * Write out accounting data.
294 */
295 (void)acct_process(l);
296
297 #ifdef KTRACE
298 /*
299 * Release trace file.
300 */
301 if (p->p_tracep != NULL) {
302 mutex_enter(&ktrace_lock);
303 ktrderef(p);
304 mutex_exit(&ktrace_lock);
305 }
306 #endif
307
308 p->p_xexit = exitcode;
309 p->p_xsig = signo;
310
311 /*
312 * If emulation has process exit hook, call it now.
313 * Set the exit status now so that the exit hook has
314 * an opportunity to tweak it (COMPAT_LINUX requires
315 * this for thread group emulation)
316 */
317 if (p->p_emul->e_proc_exit)
318 (*p->p_emul->e_proc_exit)(p);
319
320 /*
321 * Free the VM resources we're still holding on to.
322 * We must do this from a valid thread because doing
323 * so may block. This frees vmspace, which we don't
324 * need anymore. The only remaining lwp is the one
325 * we run at this moment, nothing runs in userland
326 * anymore.
327 */
328 uvm_proc_exit(p);
329
330 /*
331 * Stop profiling.
332 */
333 if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
334 mutex_spin_enter(&p->p_stmutex);
335 stopprofclock(p);
336 mutex_spin_exit(&p->p_stmutex);
337 }
338
339 /*
340 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we
341 * wake up the parent early to avoid deadlock. We can do this once
342 * the VM resources are released.
343 */
344 mutex_enter(proc_lock);
345 if (p->p_lflag & PL_PPWAIT) {
346 #if 0
347 lwp_t *lp;
348
349 l->l_lwpctl = NULL; /* was on loan from blocked parent */
350 p->p_lflag &= ~PL_PPWAIT;
351
352 lp = p->p_vforklwp;
353 p->p_vforklwp = NULL;
354 lp->l_pflag &= ~LP_VFORKWAIT; /* XXX */
355 cv_broadcast(&lp->l_waitcv);
356 #else
357 l->l_lwpctl = NULL; /* was on loan from blocked parent */
358 p->p_lflag &= ~PL_PPWAIT;
359 cv_broadcast(&p->p_pptr->p_waitcv);
360 #endif
361 }
362
363 if (SESS_LEADER(p)) {
364 struct vnode *vprele = NULL, *vprevoke = NULL;
365 struct session *sp = p->p_session;
366 struct tty *tp;
367
368 if (sp->s_ttyvp) {
369 /*
370 * Controlling process.
371 * Signal foreground pgrp,
372 * drain controlling terminal
373 * and revoke access to controlling terminal.
374 */
375 tp = sp->s_ttyp;
376 mutex_spin_enter(&tty_lock);
377 if (tp->t_session == sp) {
378 /* we can't guarantee the revoke will do this */
379 pgrp = tp->t_pgrp;
380 tp->t_pgrp = NULL;
381 tp->t_session = NULL;
382 mutex_spin_exit(&tty_lock);
383 if (pgrp != NULL) {
384 pgsignal(pgrp, SIGHUP, 1);
385 }
386 mutex_exit(proc_lock);
387 (void) ttywait(tp);
388 mutex_enter(proc_lock);
389
390 /* The tty could have been revoked. */
391 vprevoke = sp->s_ttyvp;
392 } else
393 mutex_spin_exit(&tty_lock);
394 vprele = sp->s_ttyvp;
395 sp->s_ttyvp = NULL;
396 /*
397 * s_ttyp is not zero'd; we use this to indicate
398 * that the session once had a controlling terminal.
399 * (for logging and informational purposes)
400 */
401 }
402 sp->s_leader = NULL;
403
404 if (vprevoke != NULL || vprele != NULL) {
405 if (vprevoke != NULL) {
406 /* Releases proc_lock. */
407 proc_sessrele(sp);
408 VOP_REVOKE(vprevoke, REVOKEALL);
409 } else
410 mutex_exit(proc_lock);
411 if (vprele != NULL)
412 vrele(vprele);
413 mutex_enter(proc_lock);
414 }
415 }
416 fixjobc(p, p->p_pgrp, 0);
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 SDT_PROBE(proc, kernel, , exit,
431 ((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED :
432 (p->p_xsig ? CLD_KILLED : CLD_EXITED)),
433 0,0,0,0);
434
435 #if PERFCTRS
436 /*
437 * Save final PMC information in parent process & clean up.
438 */
439 if (PMC_ENABLED(p)) {
440 pmc_save_context(p);
441 pmc_accumulate(p->p_pptr, p);
442 pmc_process_exit(p);
443 }
444 #endif
445
446 /*
447 * Reset p_opptr pointer of all former children which got
448 * traced by another process and were reparented. We reset
449 * it to NULL here; the trace detach code then reparents
450 * the child to initproc. We only check allproc list, since
451 * eventual former children on zombproc list won't reference
452 * p_opptr anymore.
453 */
454 if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
455 struct proc *q;
456 PROCLIST_FOREACH(q, &allproc) {
457 if (q->p_opptr == p)
458 q->p_opptr = NULL;
459 }
460 }
461
462 /*
463 * Give orphaned children to init(8).
464 */
465 child = LIST_FIRST(&p->p_children);
466 wakeinit = (child != NULL);
467 for (; child != NULL; child = next_child) {
468 next_child = LIST_NEXT(child, p_sibling);
469
470 /*
471 * Traced processes are killed since their existence
472 * means someone is screwing up. Since we reset the
473 * trace flags, the logic in sys_wait4() would not be
474 * triggered to reparent the process to its
475 * original parent, so we must do this here.
476 */
477 if (__predict_false(child->p_slflag & PSL_TRACED)) {
478 mutex_enter(p->p_lock);
479 child->p_slflag &=
480 ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
481 mutex_exit(p->p_lock);
482 if (child->p_opptr != child->p_pptr) {
483 struct proc *t = child->p_opptr;
484 proc_reparent(child, t ? t : initproc);
485 child->p_opptr = NULL;
486 } else
487 proc_reparent(child, initproc);
488 killproc(child, "orphaned traced process");
489 } else
490 proc_reparent(child, initproc);
491 }
492
493 /*
494 * Move proc from allproc to zombproc, it's now nearly ready to be
495 * collected by parent.
496 */
497 LIST_REMOVE(l, l_list);
498 LIST_REMOVE(p, p_list);
499 LIST_INSERT_HEAD(&zombproc, p, p_list);
500
501 /*
502 * Mark the process as dead. We must do this before we signal
503 * the parent.
504 */
505 p->p_stat = SDEAD;
506
507 /* Put in front of parent's sibling list for parent to collect it */
508 old_parent = p->p_pptr;
509 old_parent->p_nstopchild++;
510 if (LIST_FIRST(&old_parent->p_children) != p) {
511 /* Put child where it can be found quickly */
512 LIST_REMOVE(p, p_sibling);
513 LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling);
514 }
515
516 /*
517 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
518 * flag set, notify init instead (and hope it will handle
519 * this situation).
520 */
521 if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
522 proc_reparent(p, initproc);
523 wakeinit = 1;
524
525 /*
526 * If this was the last child of our parent, notify
527 * parent, so in case he was wait(2)ing, he will
528 * continue.
529 */
530 if (LIST_FIRST(&old_parent->p_children) == NULL)
531 cv_broadcast(&old_parent->p_waitcv);
532 }
533
534 /* Reload parent pointer, since p may have been reparented above */
535 new_parent = p->p_pptr;
536
537 if (__predict_false((p->p_slflag & PSL_FSTRACE) == 0 &&
538 p->p_exitsig != 0)) {
539 exit_psignal(p, new_parent, &ksi);
540 kpsignal(new_parent, &ksi, NULL);
541 }
542
543 /* Calculate the final rusage info. */
544 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
545 NULL, NULL);
546
547 if (wakeinit)
548 cv_broadcast(&initproc->p_waitcv);
549
550 callout_destroy(&l->l_timeout_ch);
551
552 /*
553 * Release any PCU resources before becoming a zombie.
554 */
555 pcu_discard_all(l);
556
557 mutex_enter(p->p_lock);
558 /* Free the linux lwp id */
559 if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid)
560 proc_free_pid(l->l_lid);
561 lwp_drainrefs(l);
562 lwp_lock(l);
563 l->l_prflag &= ~LPR_DETACHED;
564 l->l_stat = LSZOMB;
565 lwp_unlock(l);
566 KASSERT(curlwp == l);
567 KASSERT(p->p_nrlwps == 1);
568 KASSERT(p->p_nlwps == 1);
569 p->p_stat = SZOMB;
570 p->p_nrlwps--;
571 p->p_nzlwps++;
572 p->p_ndlwps = 0;
573 mutex_exit(p->p_lock);
574
575 /*
576 * Signal the parent to collect us, and drop the proclist lock.
577 * Drop debugger/procfs lock; no new references can be gained.
578 */
579 cv_broadcast(&p->p_pptr->p_waitcv);
580 rw_exit(&p->p_reflock);
581 mutex_exit(proc_lock);
582
583 /* Verify that we hold no locks other than the kernel lock. */
584 LOCKDEBUG_BARRIER(&kernel_lock, 0);
585
586 /*
587 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
588 */
589
590 /*
591 * Give machine-dependent code a chance to free any MD LWP
592 * resources. This must be done before uvm_lwp_exit(), in
593 * case these resources are in the PCB.
594 */
595 cpu_lwp_free(l, 1);
596
597 pmap_deactivate(l);
598
599 /* This process no longer needs to hold the kernel lock. */
600 #ifdef notyet
601 /* XXXSMP hold in lwp_userret() */
602 KERNEL_UNLOCK_LAST(l);
603 #else
604 KERNEL_UNLOCK_ALL(l, NULL);
605 #endif
606
607 lwp_exit_switchaway(l);
608 }
609
610 void
611 exit_lwps(struct lwp *l)
612 {
613 proc_t *p = l->l_proc;
614 lwp_t *l2;
615 int nlocks;
616
617 KERNEL_UNLOCK_ALL(l, &nlocks);
618 retry:
619 KASSERT(mutex_owned(p->p_lock));
620
621 /*
622 * Interrupt LWPs in interruptable sleep, unsuspend suspended
623 * LWPs and then wait for everyone else to finish.
624 */
625 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
626 if (l2 == l)
627 continue;
628 lwp_lock(l2);
629 l2->l_flag |= LW_WEXIT;
630 if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
631 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
632 /* setrunnable() will release the lock. */
633 setrunnable(l2);
634 continue;
635 }
636 lwp_unlock(l2);
637 }
638
639 /*
640 * Wait for every LWP to exit. Note: LWPs can get suspended/slept
641 * behind us or there may even be new LWPs created. Therefore, a
642 * full retry is required on error.
643 */
644 while (p->p_nlwps > 1) {
645 if (lwp_wait(l, 0, NULL, true)) {
646 goto retry;
647 }
648 }
649
650 KERNEL_LOCK(nlocks, l);
651 KASSERT(p->p_nlwps == 1);
652 }
653
654 static int
655 do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options,
656 struct wrusage *wru, siginfo_t *si)
657 {
658 proc_t *child;
659 int error;
660
661
662 if (wru != NULL)
663 memset(wru, 0, sizeof(*wru));
664 if (si != NULL)
665 memset(si, 0, sizeof(*si));
666
667 mutex_enter(proc_lock);
668 error = find_stopped_child(curproc, idtype, id, options, &child, status,
669 wru, si);
670 if (child == NULL) {
671 mutex_exit(proc_lock);
672 *pid = 0;
673 return error;
674 }
675 *pid = child->p_pid;
676
677 if (child->p_stat == SZOMB) {
678 /* proc_free() will release the proc_lock. */
679 if (options & WNOWAIT) {
680 mutex_exit(proc_lock);
681 } else {
682 proc_free(child, wru);
683 }
684 } else {
685 /* Child state must have been SSTOP. */
686 mutex_exit(proc_lock);
687 *status = W_STOPCODE(*status);
688 }
689 return 0;
690 }
691
692 int
693 do_sys_wait(int *pid, int *status, int options, struct rusage *ru)
694 {
695 idtype_t idtype;
696 id_t id;
697 int ret;
698 struct wrusage wru;
699
700 /*
701 * Translate the special pid values into the (idtype, pid)
702 * pair for wait6. The WAIT_MYPGRP case is handled by
703 * find_stopped_child() on its own.
704 */
705 if (*pid == WAIT_ANY) {
706 idtype = P_ALL;
707 id = 0;
708 } else if (*pid < 0) {
709 idtype = P_PGID;
710 id = (id_t)-*pid;
711 } else {
712 idtype = P_PID;
713 id = (id_t)*pid;
714 }
715 options |= WEXITED | WTRAPPED;
716 ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL,
717 NULL);
718 if (ru)
719 *ru = wru.wru_self;
720 return ret;
721 }
722
723 int
724 sys___wait450(struct lwp *l, const struct sys___wait450_args *uap,
725 register_t *retval)
726 {
727 /* {
728 syscallarg(int) pid;
729 syscallarg(int *) status;
730 syscallarg(int) options;
731 syscallarg(struct rusage *) rusage;
732 } */
733 int error, status, pid = SCARG(uap, pid);
734 struct rusage ru;
735
736 error = do_sys_wait(&pid, &status, SCARG(uap, options),
737 SCARG(uap, rusage) != NULL ? &ru : NULL);
738
739 retval[0] = pid;
740 if (pid == 0) {
741 return error;
742 }
743 if (SCARG(uap, status)) {
744 error = copyout(&status, SCARG(uap, status), sizeof(status));
745 }
746 if (SCARG(uap, rusage) && error == 0) {
747 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
748 }
749 return error;
750 }
751
752 int
753 sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval)
754 {
755 /* {
756 syscallarg(idtype_t) idtype;
757 syscallarg(id_t) id;
758 syscallarg(int *) status;
759 syscallarg(int) options;
760 syscallarg(struct wrusage *) wru;
761 syscallarg(siginfo_t *) si;
762 } */
763 struct wrusage wru, *wrup;
764 siginfo_t si, *sip;
765 idtype_t idtype;
766 int pid;
767 id_t id;
768 int error, status;
769
770 idtype = SCARG(uap, idtype);
771 id = SCARG(uap, id);
772
773 if (SCARG(uap, wru) != NULL)
774 wrup = &wru;
775 else
776 wrup = NULL;
777
778 if (SCARG(uap, info) != NULL)
779 sip = &si;
780 else
781 sip = NULL;
782
783 /*
784 * We expect all callers of wait6() to know about WEXITED and
785 * WTRAPPED.
786 */
787 error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options),
788 wrup, sip);
789
790 if (SCARG(uap, status) != NULL && error == 0)
791 error = copyout(&status, SCARG(uap, status), sizeof(status));
792 if (SCARG(uap, wru) != NULL && error == 0)
793 error = copyout(&wru, SCARG(uap, wru), sizeof(wru));
794 if (SCARG(uap, info) != NULL && error == 0)
795 error = copyout(&si, SCARG(uap, info), sizeof(si));
796 return error;
797 }
798
799
800 /*
801 * Find a process that matches the provided criteria, and fill siginfo
802 * and resources if found.
803 * Returns:
804 * -1: Not found, abort early
805 * 0: Not matched
806 * 1: Matched, there might be more matches
807 * 2: This is the only match
808 */
809 static int
810 match_process(struct proc *pp, struct proc **q, idtype_t idtype, id_t id,
811 int options, struct wrusage *wrusage, siginfo_t *siginfo)
812 {
813 struct rusage *rup;
814 struct proc *p = *q;
815 int rv = 1;
816
817 mutex_enter(p->p_lock);
818 switch (idtype) {
819 case P_ALL:
820 break;
821 case P_PID:
822 if (p->p_pid != (pid_t)id) {
823 mutex_exit(p->p_lock);
824 p = *q = proc_find_raw((pid_t)id);
825 if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) {
826 *q = NULL;
827 return -1;
828 }
829 mutex_enter(p->p_lock);
830 }
831 rv++;
832 break;
833 case P_PGID:
834 if (p->p_pgid != (pid_t)id)
835 goto out;
836 break;
837 case P_SID:
838 if (p->p_session->s_sid != (pid_t)id)
839 goto out;
840 break;
841 case P_UID:
842 if (kauth_cred_geteuid(p->p_cred) != (uid_t)id)
843 goto out;
844 break;
845 case P_GID:
846 if (kauth_cred_getegid(p->p_cred) != (gid_t)id)
847 goto out;
848 break;
849 case P_CID:
850 case P_PSETID:
851 case P_CPUID:
852 /* XXX: Implement me */
853 default:
854 out:
855 mutex_exit(p->p_lock);
856 return 0;
857 }
858
859 if ((options & WEXITED) == 0 && p->p_stat == SZOMB)
860 goto out;
861
862 if (siginfo != NULL) {
863 siginfo->si_errno = 0;
864
865 /*
866 * SUSv4 requires that the si_signo value is always
867 * SIGCHLD. Obey it despite the rfork(2) interface
868 * allows to request other signal for child exit
869 * notification.
870 */
871 siginfo->si_signo = SIGCHLD;
872
873 /*
874 * This is still a rough estimate. We will fix the
875 * cases TRAPPED, STOPPED, and CONTINUED later.
876 */
877 if (p->p_sflag & PS_COREDUMP) {
878 siginfo->si_code = CLD_DUMPED;
879 siginfo->si_status = p->p_xsig;
880 } else if (p->p_xsig) {
881 siginfo->si_code = CLD_KILLED;
882 siginfo->si_status = p->p_xsig;
883 } else {
884 siginfo->si_code = CLD_EXITED;
885 siginfo->si_status = p->p_xexit;
886 }
887
888 siginfo->si_pid = p->p_pid;
889 siginfo->si_uid = kauth_cred_geteuid(p->p_cred);
890 siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
891 siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
892 }
893
894 /*
895 * There should be no reason to limit resources usage info to
896 * exited processes only. A snapshot about any resources used
897 * by a stopped process may be exactly what is needed.
898 */
899 if (wrusage != NULL) {
900 rup = &wrusage->wru_self;
901 *rup = p->p_stats->p_ru;
902 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
903
904 rup = &wrusage->wru_children;
905 *rup = p->p_stats->p_cru;
906 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
907 }
908
909 mutex_exit(p->p_lock);
910 return rv;
911 }
912
913 /*
914 * Scan list of child processes for a child process that has stopped or
915 * exited. Used by sys_wait4 and 'compat' equivalents.
916 *
917 * Must be called with the proc_lock held, and may release while waiting.
918 */
919 static int
920 find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options,
921 struct proc **child_p, int *status_p, struct wrusage *wru, siginfo_t *si)
922 {
923 struct proc *child, *dead;
924 int error;
925
926 KASSERT(mutex_owned(proc_lock));
927
928 if (options & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG|WTRAPPED|WEXITED|
929 WNOWAIT|WCONTINUED)
930 && !(options & WOPTSCHECKED)) {
931 *child_p = NULL;
932 return EINVAL;
933 }
934
935 if ((options & (WEXITED|WUNTRACED|WCONTINUED|WTRAPPED)) == 0) {
936 /*
937 * We will be unable to find any matching processes,
938 * because there are no known events to look for.
939 * Prefer to return error instead of blocking
940 * indefinitely.
941 */
942 *child_p = NULL;
943 return EINVAL;
944 }
945
946 if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
947 mutex_enter(parent->p_lock);
948 id = (id_t)parent->p_pgid;
949 mutex_exit(parent->p_lock);
950 idtype = P_PGID;
951 }
952
953 for (;;) {
954 error = ECHILD;
955 dead = NULL;
956
957 LIST_FOREACH(child, &parent->p_children, p_sibling) {
958 int rv = match_process(parent, &child, idtype, id,
959 options, wru, si);
960 if (rv == -1)
961 break;
962 if (rv == 0)
963 continue;
964
965 /*
966 * Wait for processes with p_exitsig != SIGCHLD
967 * processes only if WALTSIG is set; wait for
968 * processes with p_exitsig == SIGCHLD only
969 * if WALTSIG is clear.
970 */
971 if (((options & WALLSIG) == 0) &&
972 (options & WALTSIG ? child->p_exitsig == SIGCHLD
973 : P_EXITSIG(child) != SIGCHLD)){
974 if (rv == 2) {
975 child = NULL;
976 break;
977 }
978 continue;
979 }
980
981 error = 0;
982 if ((options & WNOZOMBIE) == 0) {
983 if (child->p_stat == SZOMB)
984 break;
985 if (child->p_stat == SDEAD) {
986 /*
987 * We may occasionally arrive here
988 * after receiving a signal, but
989 * immediately before the child
990 * process is zombified. The wait
991 * will be short, so avoid returning
992 * to userspace.
993 */
994 dead = child;
995 }
996 }
997
998 if ((options & WCONTINUED) != 0 &&
999 child->p_xsig == SIGCONT) {
1000 if ((options & WNOWAIT) == 0) {
1001 child->p_waited = 1;
1002 parent->p_nstopchild--;
1003 }
1004 if (si) {
1005 si->si_status = child->p_xsig;
1006 si->si_code = CLD_CONTINUED;
1007 }
1008 break;
1009 }
1010
1011 if ((options & (WTRAPPED|WSTOPPED)) != 0 &&
1012 child->p_stat == SSTOP &&
1013 child->p_waited == 0 &&
1014 ((child->p_slflag & PSL_TRACED) ||
1015 options & (WUNTRACED|WSTOPPED))) {
1016 if ((options & WNOWAIT) == 0) {
1017 child->p_waited = 1;
1018 parent->p_nstopchild--;
1019 }
1020 if (si) {
1021 si->si_status = child->p_xsig;
1022 si->si_code =
1023 (child->p_slflag & PSL_TRACED) ?
1024 CLD_TRAPPED : CLD_STOPPED;
1025 }
1026 break;
1027 }
1028 if (parent->p_nstopchild == 0 || rv == 2) {
1029 child = NULL;
1030 break;
1031 }
1032 }
1033 /* XXX: WCONTINUED? */
1034
1035 if (child != NULL || error != 0 ||
1036 ((options & WNOHANG) != 0 && dead == NULL)) {
1037 if (child != NULL) {
1038 *status_p = child->p_xsig;
1039 }
1040 *child_p = child;
1041 return error;
1042 }
1043
1044 /*
1045 * Wait for another child process to stop.
1046 */
1047 error = cv_wait_sig(&parent->p_waitcv, proc_lock);
1048
1049 if (error != 0) {
1050 *child_p = NULL;
1051 return error;
1052 }
1053 }
1054 }
1055
1056 /*
1057 * Free a process after parent has taken all the state info. Must be called
1058 * with the proclist lock held, and will release before returning.
1059 *
1060 * *ru is returned to the caller, and must be freed by the caller.
1061 */
1062 static void
1063 proc_free(struct proc *p, struct wrusage *wru)
1064 {
1065 struct proc *parent = p->p_pptr;
1066 struct lwp *l;
1067 ksiginfo_t ksi;
1068 kauth_cred_t cred1, cred2;
1069 uid_t uid;
1070
1071 KASSERT(mutex_owned(proc_lock));
1072 KASSERT(p->p_nlwps == 1);
1073 KASSERT(p->p_nzlwps == 1);
1074 KASSERT(p->p_nrlwps == 0);
1075 KASSERT(p->p_stat == SZOMB);
1076
1077 /*
1078 * If we got the child via ptrace(2) or procfs, and
1079 * the parent is different (meaning the process was
1080 * attached, rather than run as a child), then we need
1081 * to give it back to the old parent, and send the
1082 * parent the exit signal. The rest of the cleanup
1083 * will be done when the old parent waits on the child.
1084 */
1085 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
1086 mutex_enter(p->p_lock);
1087 p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
1088 mutex_exit(p->p_lock);
1089 parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
1090 proc_reparent(p, parent);
1091 p->p_opptr = NULL;
1092 if (p->p_exitsig != 0) {
1093 exit_psignal(p, parent, &ksi);
1094 kpsignal(parent, &ksi, NULL);
1095 }
1096 cv_broadcast(&parent->p_waitcv);
1097 mutex_exit(proc_lock);
1098 return;
1099 }
1100
1101 sched_proc_exit(parent, p);
1102
1103 /*
1104 * Add child times of exiting process onto its own times.
1105 * This cannot be done any earlier else it might get done twice.
1106 */
1107 l = LIST_FIRST(&p->p_lwps);
1108 p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
1109 p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
1110 ruadd(&p->p_stats->p_ru, &l->l_ru);
1111 ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
1112 ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
1113 if (wru != NULL) {
1114 wru->wru_self = p->p_stats->p_ru;
1115 wru->wru_children = p->p_stats->p_cru;
1116 }
1117 p->p_xsig = 0;
1118 p->p_xexit = 0;
1119
1120 /*
1121 * At this point we are going to start freeing the final resources.
1122 * If anyone tries to access the proc structure after here they will
1123 * get a shock - bits are missing. Attempt to make it hard! We
1124 * don't bother with any further locking past this point.
1125 */
1126 p->p_stat = SIDL; /* not even a zombie any more */
1127 LIST_REMOVE(p, p_list); /* off zombproc */
1128 parent->p_nstopchild--;
1129 LIST_REMOVE(p, p_sibling);
1130
1131 /*
1132 * Let pid be reallocated.
1133 */
1134 proc_free_pid(p->p_pid);
1135
1136 /*
1137 * Unlink process from its process group.
1138 * Releases the proc_lock.
1139 */
1140 proc_leavepgrp(p);
1141
1142 /*
1143 * Delay release until after lwp_free.
1144 */
1145 cred2 = l->l_cred;
1146
1147 /*
1148 * Free the last LWP's resources.
1149 *
1150 * lwp_free ensures the LWP is no longer running on another CPU.
1151 */
1152 lwp_free(l, false, true);
1153
1154 /*
1155 * Now no one except us can reach the process p.
1156 */
1157
1158 /*
1159 * Decrement the count of procs running with this uid.
1160 */
1161 cred1 = p->p_cred;
1162 uid = kauth_cred_getuid(cred1);
1163 (void)chgproccnt(uid, -1);
1164
1165 /*
1166 * Release substructures.
1167 */
1168
1169 lim_free(p->p_limit);
1170 pstatsfree(p->p_stats);
1171 kauth_cred_free(cred1);
1172 kauth_cred_free(cred2);
1173
1174 /*
1175 * Release reference to text vnode
1176 */
1177 if (p->p_textvp)
1178 vrele(p->p_textvp);
1179
1180 mutex_destroy(&p->p_auxlock);
1181 mutex_obj_free(p->p_lock);
1182 mutex_destroy(&p->p_stmutex);
1183 cv_destroy(&p->p_waitcv);
1184 cv_destroy(&p->p_lwpcv);
1185 rw_destroy(&p->p_reflock);
1186
1187 proc_free_mem(p);
1188 }
1189
1190 /*
1191 * make process 'parent' the new parent of process 'child'.
1192 *
1193 * Must be called with proc_lock held.
1194 */
1195 void
1196 proc_reparent(struct proc *child, struct proc *parent)
1197 {
1198
1199 KASSERT(mutex_owned(proc_lock));
1200
1201 if (child->p_pptr == parent)
1202 return;
1203
1204 if (child->p_stat == SZOMB || child->p_stat == SDEAD ||
1205 (child->p_stat == SSTOP && !child->p_waited)) {
1206 child->p_pptr->p_nstopchild--;
1207 parent->p_nstopchild++;
1208 }
1209 if (parent == initproc)
1210 child->p_exitsig = SIGCHLD;
1211
1212 LIST_REMOVE(child, p_sibling);
1213 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1214 child->p_pptr = parent;
1215 child->p_ppid = parent->p_pid;
1216 }
1217