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