kern_exit.c revision 1.175 1 1.175 dsl /* $NetBSD: kern_exit.c,v 1.175 2007/04/30 20:11:41 dsl Exp $ */
2 1.56 thorpej
3 1.56 thorpej /*-
4 1.165 ad * Copyright (c) 1998, 1999, 2006, 2007 The NetBSD Foundation, Inc.
5 1.56 thorpej * All rights reserved.
6 1.58 christos *
7 1.56 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.56 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.165 ad * NASA Ames Research Center, and by Andrew Doran.
10 1.56 thorpej *
11 1.56 thorpej * Redistribution and use in source and binary forms, with or without
12 1.56 thorpej * modification, are permitted provided that the following conditions
13 1.56 thorpej * are met:
14 1.56 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.56 thorpej * notice, this list of conditions and the following disclaimer.
16 1.58 christos * 2. Redistributions in binary form must reproduce the above copyright
17 1.56 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.56 thorpej * documentation and/or other materials provided with the distribution.
19 1.56 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.56 thorpej * must display the following acknowledgement:
21 1.58 christos * This product includes software developed by the NetBSD
22 1.58 christos * Foundation, Inc. and its contributors.
23 1.56 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.56 thorpej * contributors may be used to endorse or promote products derived
25 1.56 thorpej * from this software without specific prior written permission.
26 1.58 christos *
27 1.56 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.56 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.56 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.56 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.56 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.56 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.56 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.56 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.56 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.56 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.56 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.56 thorpej */
39 1.24 cgd
40 1.24 cgd /*
41 1.24 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993
42 1.24 cgd * The Regents of the University of California. All rights reserved.
43 1.24 cgd * (c) UNIX System Laboratories, Inc.
44 1.24 cgd * All or some portions of this file are derived from material licensed
45 1.24 cgd * to the University of California by American Telephone and Telegraph
46 1.24 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 1.24 cgd * the permission of UNIX System Laboratories, Inc.
48 1.24 cgd *
49 1.24 cgd * Redistribution and use in source and binary forms, with or without
50 1.24 cgd * modification, are permitted provided that the following conditions
51 1.24 cgd * are met:
52 1.24 cgd * 1. Redistributions of source code must retain the above copyright
53 1.24 cgd * notice, this list of conditions and the following disclaimer.
54 1.24 cgd * 2. Redistributions in binary form must reproduce the above copyright
55 1.24 cgd * notice, this list of conditions and the following disclaimer in the
56 1.24 cgd * documentation and/or other materials provided with the distribution.
57 1.119 agc * 3. Neither the name of the University nor the names of its contributors
58 1.24 cgd * may be used to endorse or promote products derived from this software
59 1.24 cgd * without specific prior written permission.
60 1.24 cgd *
61 1.24 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 1.24 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 1.24 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 1.24 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 1.24 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 1.24 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 1.24 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 1.24 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 1.24 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.24 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.24 cgd * SUCH DAMAGE.
72 1.24 cgd *
73 1.49 fvdl * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95
74 1.24 cgd */
75 1.92 lukem
76 1.92 lukem #include <sys/cdefs.h>
77 1.175 dsl __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.175 2007/04/30 20:11:41 dsl Exp $");
78 1.48 mrg
79 1.51 thorpej #include "opt_ktrace.h"
80 1.97 briggs #include "opt_perfctrs.h"
81 1.94 christos #include "opt_systrace.h"
82 1.60 tron #include "opt_sysv.h"
83 1.24 cgd
84 1.24 cgd #include <sys/param.h>
85 1.174 rmind #include <sys/aio.h>
86 1.24 cgd #include <sys/systm.h>
87 1.24 cgd #include <sys/ioctl.h>
88 1.24 cgd #include <sys/tty.h>
89 1.24 cgd #include <sys/time.h>
90 1.24 cgd #include <sys/resource.h>
91 1.24 cgd #include <sys/kernel.h>
92 1.24 cgd #include <sys/proc.h>
93 1.24 cgd #include <sys/buf.h>
94 1.24 cgd #include <sys/wait.h>
95 1.24 cgd #include <sys/file.h>
96 1.24 cgd #include <sys/vnode.h>
97 1.24 cgd #include <sys/syslog.h>
98 1.24 cgd #include <sys/malloc.h>
99 1.53 thorpej #include <sys/pool.h>
100 1.24 cgd #include <sys/resourcevar.h>
101 1.98 briggs #if defined(PERFCTRS)
102 1.97 briggs #include <sys/pmc.h>
103 1.98 briggs #endif
104 1.24 cgd #include <sys/ptrace.h>
105 1.29 cgd #include <sys/acct.h>
106 1.36 christos #include <sys/filedesc.h>
107 1.100 gmcgarry #include <sys/ras.h>
108 1.36 christos #include <sys/signalvar.h>
109 1.64 ross #include <sys/sched.h>
110 1.26 cgd #include <sys/mount.h>
111 1.26 cgd #include <sys/syscallargs.h>
112 1.94 christos #include <sys/systrace.h>
113 1.156 elad #include <sys/kauth.h>
114 1.165 ad #include <sys/sleepq.h>
115 1.165 ad #include <sys/lockdebug.h>
116 1.165 ad #include <sys/ktrace.h>
117 1.26 cgd
118 1.24 cgd #include <machine/cpu.h>
119 1.24 cgd
120 1.47 mrg #include <uvm/uvm_extern.h>
121 1.47 mrg
122 1.107 thorpej #define DEBUG_EXIT
123 1.107 thorpej
124 1.107 thorpej #ifdef DEBUG_EXIT
125 1.107 thorpej int debug_exit = 0;
126 1.107 thorpej #define DPRINTF(x) if (debug_exit) printf x
127 1.107 thorpej #else
128 1.107 thorpej #define DPRINTF(x)
129 1.107 thorpej #endif
130 1.107 thorpej
131 1.123 christos /*
132 1.132 jdolecek * Fill in the appropriate signal information, and signal the parent.
133 1.123 christos */
134 1.123 christos static void
135 1.162 yamt exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
136 1.123 christos {
137 1.123 christos
138 1.163 yamt KSI_INIT(ksi);
139 1.140 pk if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
140 1.123 christos if (WIFSIGNALED(p->p_xstat)) {
141 1.123 christos if (WCOREDUMP(p->p_xstat))
142 1.140 pk ksi->ksi_code = CLD_DUMPED;
143 1.123 christos else
144 1.140 pk ksi->ksi_code = CLD_KILLED;
145 1.123 christos } else {
146 1.140 pk ksi->ksi_code = CLD_EXITED;
147 1.123 christos }
148 1.123 christos }
149 1.123 christos /*
150 1.165 ad * We fill those in, even for non-SIGCHLD.
151 1.165 ad * It's safe to access p->p_cred unlocked here.
152 1.123 christos */
153 1.140 pk ksi->ksi_pid = p->p_pid;
154 1.156 elad ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
155 1.140 pk ksi->ksi_status = p->p_xstat;
156 1.123 christos /* XXX: is this still valid? */
157 1.175 dsl ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
158 1.175 dsl ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
159 1.123 christos }
160 1.94 christos
161 1.24 cgd /*
162 1.24 cgd * exit --
163 1.24 cgd * Death of process.
164 1.24 cgd */
165 1.31 thorpej int
166 1.162 yamt sys_exit(struct lwp *l, void *v, register_t *retval)
167 1.31 thorpej {
168 1.33 mycroft struct sys_exit_args /* {
169 1.89 lukem syscallarg(int) rval;
170 1.31 thorpej } */ *uap = v;
171 1.165 ad struct proc *p = l->l_proc;
172 1.24 cgd
173 1.165 ad /* Don't call exit1() multiple times in the same process. */
174 1.165 ad mutex_enter(&p->p_smutex);
175 1.165 ad if (p->p_sflag & PS_WEXIT) {
176 1.165 ad mutex_exit(&p->p_smutex);
177 1.107 thorpej lwp_exit(l);
178 1.165 ad }
179 1.107 thorpej
180 1.165 ad /* exit1() will release the mutex. */
181 1.107 thorpej exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
182 1.24 cgd /* NOTREACHED */
183 1.31 thorpej return (0);
184 1.24 cgd }
185 1.24 cgd
186 1.24 cgd /*
187 1.24 cgd * Exit: deallocate address space and other resources, change proc state
188 1.24 cgd * to zombie, and unlink proc from allproc and parent's lists. Save exit
189 1.24 cgd * status and rusage for wait(). Check for child processes and orphan them.
190 1.165 ad *
191 1.165 ad * Must be called with p->p_smutex held. Does not return.
192 1.24 cgd */
193 1.31 thorpej void
194 1.107 thorpej exit1(struct lwp *l, int rv)
195 1.24 cgd {
196 1.107 thorpej struct proc *p, *q, *nq;
197 1.165 ad int s;
198 1.140 pk ksiginfo_t ksi;
199 1.165 ad ksiginfoq_t kq;
200 1.165 ad int wakeinit;
201 1.24 cgd
202 1.107 thorpej p = l->l_proc;
203 1.107 thorpej
204 1.172 ad KASSERT(mutex_owned(&p->p_smutex));
205 1.165 ad
206 1.78 thorpej if (__predict_false(p == initproc))
207 1.24 cgd panic("init died (signal %d, exit %d)",
208 1.24 cgd WTERMSIG(rv), WEXITSTATUS(rv));
209 1.73 thorpej
210 1.165 ad p->p_sflag |= PS_WEXIT;
211 1.165 ad
212 1.165 ad /*
213 1.165 ad * Force all other LWPs to exit before we do. Only then can we
214 1.165 ad * begin to tear down the rest of the process state.
215 1.165 ad */
216 1.165 ad if (p->p_nlwps > 1)
217 1.165 ad exit_lwps(l);
218 1.165 ad
219 1.165 ad ksiginfo_queue_init(&kq);
220 1.165 ad
221 1.165 ad /*
222 1.165 ad * If we have been asked to stop on exit, do so now.
223 1.165 ad */
224 1.165 ad if (p->p_sflag & PS_STOPEXIT) {
225 1.165 ad KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
226 1.165 ad sigclearall(p, &contsigmask, &kq);
227 1.165 ad p->p_waited = 0;
228 1.165 ad mb_write();
229 1.130 atatat p->p_stat = SSTOP;
230 1.165 ad lwp_lock(l);
231 1.165 ad p->p_nrlwps--;
232 1.130 atatat l->l_stat = LSSTOP;
233 1.165 ad mutex_exit(&p->p_smutex);
234 1.130 atatat mi_switch(l, NULL);
235 1.165 ad KERNEL_LOCK(l->l_biglocks, l);
236 1.165 ad } else
237 1.165 ad mutex_exit(&p->p_smutex);
238 1.165 ad
239 1.174 rmind /* Destroy all AIO works */
240 1.174 rmind aio_exit(p);
241 1.174 rmind
242 1.165 ad /*
243 1.165 ad * Drain all remaining references that procfs, ptrace and others may
244 1.165 ad * have on the process.
245 1.165 ad */
246 1.165 ad mutex_enter(&p->p_mutex);
247 1.165 ad proc_drainrefs(p);
248 1.165 ad mutex_exit(&p->p_mutex);
249 1.112 nathanw
250 1.107 thorpej /*
251 1.165 ad * Bin any remaining signals and mark the process as dying so it will
252 1.165 ad * not be found for, e.g. signals.
253 1.107 thorpej */
254 1.165 ad mutex_enter(&p->p_smutex);
255 1.165 ad sigfillset(&p->p_sigctx.ps_sigignore);
256 1.165 ad sigclearall(p, NULL, &kq);
257 1.165 ad p->p_stat = SDYING;
258 1.165 ad mutex_exit(&p->p_smutex);
259 1.165 ad ksiginfo_queue_drain(&kq);
260 1.165 ad
261 1.165 ad DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
262 1.107 thorpej
263 1.24 cgd #ifdef PGINPROF
264 1.24 cgd vmsizmon();
265 1.24 cgd #endif
266 1.107 thorpej timers_free(p, TIMERS_ALL);
267 1.100 gmcgarry #if defined(__HAVE_RAS)
268 1.100 gmcgarry ras_purgeall(p);
269 1.100 gmcgarry #endif
270 1.24 cgd
271 1.24 cgd /*
272 1.165 ad * Close open files, release open-file table and free signal
273 1.165 ad * actions. This may block!
274 1.24 cgd */
275 1.154 christos fdfree(l);
276 1.141 pk cwdfree(p->p_cwdi);
277 1.165 ad p->p_cwdi = NULL;
278 1.93 christos doexithooks(p);
279 1.165 ad sigactsfree(p->p_sigacts);
280 1.157 ad
281 1.157 ad /*
282 1.164 ad * Write out accounting data.
283 1.157 ad */
284 1.154 christos (void)acct_process(l);
285 1.157 ad
286 1.24 cgd #ifdef KTRACE
287 1.145 perry /*
288 1.157 ad * Release trace file.
289 1.24 cgd */
290 1.165 ad if (p->p_tracep != NULL) {
291 1.165 ad mutex_enter(&ktrace_mutex);
292 1.165 ad ktrderef(p);
293 1.165 ad mutex_exit(&ktrace_mutex);
294 1.165 ad }
295 1.94 christos #endif
296 1.94 christos #ifdef SYSTRACE
297 1.94 christos systrace_sys_exit(p);
298 1.24 cgd #endif
299 1.157 ad
300 1.24 cgd /*
301 1.99 manu * If emulation has process exit hook, call it now.
302 1.158 manu * Set the exit status now so that the exit hook has
303 1.158 manu * an opportunity to tweak it (COMPAT_LINUX requires
304 1.158 manu * this for thread group emulation)
305 1.99 manu */
306 1.158 manu p->p_xstat = rv;
307 1.99 manu if (p->p_emul->e_proc_exit)
308 1.99 manu (*p->p_emul->e_proc_exit)(p);
309 1.99 manu
310 1.165 ad /* Collect child u-areas. */
311 1.168 thorpej uvm_uarea_drain(false);
312 1.165 ad
313 1.160 thorpej /*
314 1.140 pk * Free the VM resources we're still holding on to.
315 1.140 pk * We must do this from a valid thread because doing
316 1.140 pk * so may block. This frees vmspace, which we don't
317 1.140 pk * need anymore. The only remaining lwp is the one
318 1.140 pk * we run at this moment, nothing runs in userland
319 1.140 pk * anymore.
320 1.140 pk */
321 1.140 pk uvm_proc_exit(p);
322 1.140 pk
323 1.140 pk /*
324 1.165 ad * Stop profiling.
325 1.140 pk */
326 1.165 ad if ((p->p_stflag & PST_PROFIL) != 0) {
327 1.165 ad mutex_spin_enter(&p->p_stmutex);
328 1.165 ad stopprofclock(p);
329 1.165 ad mutex_spin_exit(&p->p_stmutex);
330 1.165 ad }
331 1.140 pk
332 1.140 pk /*
333 1.165 ad * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
334 1.165 ad * wake up the parent early to avoid deadlock. We can do this once
335 1.165 ad * the VM resources are released.
336 1.140 pk */
337 1.169 ad mutex_enter(&proclist_lock);
338 1.165 ad
339 1.165 ad mutex_enter(&p->p_smutex);
340 1.165 ad if (p->p_sflag & PS_PPWAIT) {
341 1.165 ad p->p_sflag &= ~PS_PPWAIT;
342 1.173 ad cv_signal(&p->p_pptr->p_waitcv);
343 1.165 ad }
344 1.165 ad mutex_exit(&p->p_smutex);
345 1.165 ad
346 1.165 ad if (SESS_LEADER(p)) {
347 1.165 ad struct vnode *vprele = NULL, *vprevoke = NULL;
348 1.165 ad struct session *sp = p->p_session;
349 1.165 ad struct tty *tp;
350 1.165 ad
351 1.165 ad if (sp->s_ttyvp) {
352 1.165 ad /*
353 1.165 ad * Controlling process.
354 1.165 ad * Signal foreground pgrp,
355 1.165 ad * drain controlling terminal
356 1.165 ad * and revoke access to controlling terminal.
357 1.165 ad */
358 1.165 ad tp = sp->s_ttyp;
359 1.165 ad s = spltty();
360 1.165 ad TTY_LOCK(tp);
361 1.165 ad if (tp->t_session == sp) {
362 1.165 ad if (tp->t_pgrp) {
363 1.165 ad mutex_enter(&proclist_mutex);
364 1.165 ad pgsignal(tp->t_pgrp, SIGHUP, 1);
365 1.165 ad mutex_exit(&proclist_mutex);
366 1.165 ad }
367 1.165 ad /* we can't guarantee the revoke will do this */
368 1.165 ad tp->t_pgrp = NULL;
369 1.165 ad tp->t_session = NULL;
370 1.165 ad TTY_UNLOCK(tp);
371 1.165 ad splx(s);
372 1.165 ad SESSRELE(sp);
373 1.169 ad mutex_exit(&proclist_lock);
374 1.165 ad (void) ttywait(tp);
375 1.169 ad mutex_enter(&proclist_lock);
376 1.165 ad
377 1.165 ad /*
378 1.165 ad * The tty could have been revoked
379 1.165 ad * if we blocked.
380 1.165 ad */
381 1.165 ad vprevoke = sp->s_ttyvp;
382 1.165 ad } else {
383 1.165 ad TTY_UNLOCK(tp);
384 1.165 ad splx(s);
385 1.165 ad }
386 1.165 ad vprele = sp->s_ttyvp;
387 1.165 ad sp->s_ttyvp = NULL;
388 1.165 ad /*
389 1.165 ad * s_ttyp is not zero'd; we use this to indicate
390 1.165 ad * that the session once had a controlling terminal.
391 1.165 ad * (for logging and informational purposes)
392 1.165 ad */
393 1.165 ad }
394 1.165 ad sp->s_leader = NULL;
395 1.140 pk
396 1.165 ad if (vprevoke != NULL || vprele != NULL) {
397 1.169 ad mutex_exit(&proclist_lock);
398 1.165 ad if (vprevoke != NULL)
399 1.165 ad VOP_REVOKE(vprevoke, REVOKEALL);
400 1.165 ad if (vprele != NULL)
401 1.165 ad vrele(vprele);
402 1.169 ad mutex_enter(&proclist_lock);
403 1.165 ad }
404 1.165 ad }
405 1.165 ad mutex_enter(&proclist_mutex);
406 1.165 ad fixjobc(p, p->p_pgrp, 0);
407 1.165 ad mutex_exit(&proclist_mutex);
408 1.140 pk
409 1.140 pk /*
410 1.171 ad * Finalize the last LWP's specificdata, as well as the
411 1.171 ad * specificdata for the proc itself.
412 1.171 ad */
413 1.171 ad lwp_finispecific(l);
414 1.171 ad proc_finispecific(p);
415 1.171 ad
416 1.171 ad /*
417 1.140 pk * Notify interested parties of our demise.
418 1.140 pk */
419 1.140 pk KNOTE(&p->p_klist, NOTE_EXIT);
420 1.140 pk
421 1.140 pk #if PERFCTRS
422 1.140 pk /*
423 1.140 pk * Save final PMC information in parent process & clean up.
424 1.140 pk */
425 1.140 pk if (PMC_ENABLED(p)) {
426 1.140 pk pmc_save_context(p);
427 1.140 pk pmc_accumulate(p->p_pptr, p);
428 1.140 pk pmc_process_exit(p);
429 1.140 pk }
430 1.140 pk #endif
431 1.140 pk
432 1.140 pk /*
433 1.128 dsl * Reset p_opptr pointer of all former children which got
434 1.128 dsl * traced by another process and were reparented. We reset
435 1.128 dsl * it to NULL here; the trace detach code then reparents
436 1.128 dsl * the child to initproc. We only check allproc list, since
437 1.128 dsl * eventual former children on zombproc list won't reference
438 1.128 dsl * p_opptr anymore.
439 1.128 dsl */
440 1.165 ad if (p->p_slflag & PSL_CHTRACED) {
441 1.143 yamt PROCLIST_FOREACH(q, &allproc) {
442 1.128 dsl if (q->p_opptr == p)
443 1.128 dsl q->p_opptr = NULL;
444 1.128 dsl }
445 1.128 dsl }
446 1.128 dsl
447 1.128 dsl /*
448 1.72 thorpej * Give orphaned children to init(8).
449 1.72 thorpej */
450 1.101 matt q = LIST_FIRST(&p->p_children);
451 1.165 ad wakeinit = (q != NULL);
452 1.132 jdolecek for (; q != NULL; q = nq) {
453 1.101 matt nq = LIST_NEXT(q, p_sibling);
454 1.104 jdolecek
455 1.24 cgd /*
456 1.104 jdolecek * Traced processes are killed since their existence
457 1.104 jdolecek * means someone is screwing up. Since we reset the
458 1.104 jdolecek * trace flags, the logic in sys_wait4() would not be
459 1.104 jdolecek * triggered to reparent the process to its
460 1.106 jdolecek * original parent, so we must do this here.
461 1.24 cgd */
462 1.165 ad if (q->p_slflag & PSL_TRACED) {
463 1.165 ad mutex_enter(&p->p_smutex);
464 1.165 ad q->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
465 1.165 ad mutex_exit(&p->p_smutex);
466 1.104 jdolecek if (q->p_opptr != q->p_pptr) {
467 1.104 jdolecek struct proc *t = q->p_opptr;
468 1.104 jdolecek proc_reparent(q, t ? t : initproc);
469 1.104 jdolecek q->p_opptr = NULL;
470 1.105 jdolecek } else
471 1.105 jdolecek proc_reparent(q, initproc);
472 1.153 jdolecek killproc(q, "orphaned traced process");
473 1.165 ad } else
474 1.104 jdolecek proc_reparent(q, initproc);
475 1.24 cgd }
476 1.115 dsl
477 1.115 dsl /*
478 1.165 ad * Move proc from allproc to zombproc, it's now nearly ready to be
479 1.165 ad * collected by parent.
480 1.115 dsl */
481 1.165 ad mutex_enter(&proclist_mutex);
482 1.165 ad LIST_REMOVE(l, l_list);
483 1.115 dsl LIST_REMOVE(p, p_list);
484 1.115 dsl LIST_INSERT_HEAD(&zombproc, p, p_list);
485 1.133 jdolecek
486 1.165 ad /*
487 1.165 ad * Mark the process as dead. We must do this before we signal
488 1.165 ad * the parent.
489 1.165 ad */
490 1.165 ad p->p_stat = SDEAD;
491 1.133 jdolecek
492 1.133 jdolecek /* Put in front of parent's sibling list for parent to collect it */
493 1.133 jdolecek q = p->p_pptr;
494 1.133 jdolecek q->p_nstopchild++;
495 1.133 jdolecek if (LIST_FIRST(&q->p_children) != p) {
496 1.133 jdolecek /* Put child where it can be found quickly */
497 1.133 jdolecek LIST_REMOVE(p, p_sibling);
498 1.133 jdolecek LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
499 1.133 jdolecek }
500 1.165 ad mutex_exit(&proclist_mutex);
501 1.133 jdolecek
502 1.59 christos /*
503 1.59 christos * Notify parent that we're gone. If parent has the P_NOCLDWAIT
504 1.59 christos * flag set, notify init instead (and hope it will handle
505 1.59 christos * this situation).
506 1.59 christos */
507 1.165 ad mutex_enter(&q->p_mutex);
508 1.167 pavel if (q->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
509 1.59 christos proc_reparent(p, initproc);
510 1.165 ad wakeinit = 1;
511 1.133 jdolecek
512 1.59 christos /*
513 1.59 christos * If this was the last child of our parent, notify
514 1.59 christos * parent, so in case he was wait(2)ing, he will
515 1.59 christos * continue.
516 1.59 christos */
517 1.140 pk if (LIST_FIRST(&q->p_children) == NULL)
518 1.173 ad cv_signal(&q->p_waitcv);
519 1.59 christos }
520 1.165 ad mutex_exit(&q->p_mutex);
521 1.24 cgd
522 1.140 pk /* Reload parent pointer, since p may have been reparented above */
523 1.140 pk q = p->p_pptr;
524 1.140 pk
525 1.165 ad if ((p->p_slflag & PSL_FSTRACE) == 0 && p->p_exitsig != 0) {
526 1.140 pk exit_psignal(p, q, &ksi);
527 1.165 ad mutex_enter(&proclist_mutex);
528 1.165 ad kpsignal(q, &ksi, NULL);
529 1.165 ad mutex_exit(&proclist_mutex);
530 1.140 pk }
531 1.140 pk
532 1.175 dsl /* Calculate the final rusage info. */
533 1.175 dsl calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
534 1.175 dsl NULL, NULL);
535 1.140 pk
536 1.165 ad if (wakeinit)
537 1.173 ad cv_signal(&initproc->p_waitcv);
538 1.165 ad
539 1.165 ad /*
540 1.165 ad * Remaining lwp resources will be freed in lwp_exit2() once we've
541 1.165 ad * switch to idle context; at that point, we will be marked as a
542 1.165 ad * full blown zombie.
543 1.165 ad *
544 1.165 ad * XXXSMP disable preemption.
545 1.165 ad */
546 1.165 ad mutex_enter(&p->p_smutex);
547 1.165 ad lwp_drainrefs(l);
548 1.165 ad lwp_lock(l);
549 1.165 ad l->l_prflag &= ~LPR_DETACHED;
550 1.165 ad l->l_stat = LSZOMB;
551 1.165 ad lwp_unlock(l);
552 1.165 ad KASSERT(curlwp == l);
553 1.165 ad KASSERT(p->p_nrlwps == 1);
554 1.165 ad KASSERT(p->p_nlwps == 1);
555 1.165 ad p->p_stat = SZOMB;
556 1.165 ad p->p_nrlwps--;
557 1.165 ad p->p_nzlwps++;
558 1.165 ad p->p_ndlwps = 0;
559 1.165 ad mutex_exit(&p->p_smutex);
560 1.140 pk
561 1.165 ad /*
562 1.165 ad * Signal the parent to collect us, and drop the proclist lock.
563 1.165 ad */
564 1.173 ad cv_signal(&p->p_pptr->p_waitcv);
565 1.169 ad mutex_exit(&proclist_lock);
566 1.140 pk
567 1.165 ad /* Verify that we hold no locks other than the kernel lock. */
568 1.165 ad #ifdef MULTIPROCESSOR
569 1.165 ad LOCKDEBUG_BARRIER(&kernel_lock, 0);
570 1.165 ad #else
571 1.165 ad LOCKDEBUG_BARRIER(NULL, 0);
572 1.165 ad #endif
573 1.85 thorpej
574 1.165 ad /*
575 1.165 ad * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
576 1.165 ad */
577 1.157 ad
578 1.165 ad /*
579 1.165 ad * Give machine-dependent code a chance to free any MD LWP
580 1.165 ad * resources. This must be done before uvm_lwp_exit(), in
581 1.165 ad * case these resources are in the PCB.
582 1.165 ad */
583 1.165 ad #ifndef __NO_CPU_LWP_FREE
584 1.165 ad cpu_lwp_free(l, 1);
585 1.133 jdolecek #endif
586 1.165 ad pmap_deactivate(l);
587 1.133 jdolecek
588 1.140 pk /* This process no longer needs to hold the kernel lock. */
589 1.165 ad #ifdef notyet
590 1.165 ad /* XXXSMP hold in lwp_userret() */
591 1.165 ad KERNEL_UNLOCK_LAST(l);
592 1.165 ad #else
593 1.165 ad KERNEL_UNLOCK_ALL(l, NULL);
594 1.165 ad #endif
595 1.140 pk
596 1.24 cgd /*
597 1.56 thorpej * Finally, call machine-dependent code to switch to a new
598 1.56 thorpej * context (possibly the idle context). Once we are no longer
599 1.165 ad * using the dead lwp's stack, lwp_exit2() will be called.
600 1.56 thorpej *
601 1.56 thorpej * Note that cpu_exit() will end with a call equivalent to
602 1.56 thorpej * cpu_switch(), finishing our execution (pun intended).
603 1.24 cgd */
604 1.165 ad uvmexp.swtch++; /* XXXSMP unlocked */
605 1.133 jdolecek cpu_exit(l);
606 1.107 thorpej }
607 1.107 thorpej
608 1.107 thorpej void
609 1.107 thorpej exit_lwps(struct lwp *l)
610 1.107 thorpej {
611 1.107 thorpej struct proc *p;
612 1.107 thorpej struct lwp *l2;
613 1.165 ad int error;
614 1.165 ad lwpid_t waited;
615 1.165 ad #if defined(MULTIPROCESSOR)
616 1.165 ad int nlocks;
617 1.165 ad #endif
618 1.165 ad
619 1.165 ad KERNEL_UNLOCK_ALL(l, &nlocks);
620 1.107 thorpej
621 1.107 thorpej p = l->l_proc;
622 1.107 thorpej
623 1.165 ad retry:
624 1.107 thorpej /*
625 1.107 thorpej * Interrupt LWPs in interruptable sleep, unsuspend suspended
626 1.165 ad * LWPs and then wait for everyone else to finish.
627 1.107 thorpej */
628 1.107 thorpej LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
629 1.165 ad if (l2 == l)
630 1.165 ad continue;
631 1.165 ad lwp_lock(l2);
632 1.167 pavel l2->l_flag |= LW_WEXIT;
633 1.167 pavel if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
634 1.118 fvdl l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
635 1.165 ad /* setrunnable() will release the lock. */
636 1.107 thorpej setrunnable(l2);
637 1.107 thorpej DPRINTF(("exit_lwps: Made %d.%d runnable\n",
638 1.107 thorpej p->p_pid, l2->l_lid));
639 1.165 ad continue;
640 1.107 thorpej }
641 1.165 ad lwp_unlock(l2);
642 1.107 thorpej }
643 1.107 thorpej while (p->p_nlwps > 1) {
644 1.165 ad DPRINTF(("exit_lwps: waiting for %d LWPs (%d zombies)\n",
645 1.165 ad p->p_nlwps, p->p_nzlwps));
646 1.107 thorpej error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
647 1.165 ad if (p->p_nlwps == 1)
648 1.165 ad break;
649 1.150 yamt if (error == EDEADLK) {
650 1.150 yamt /*
651 1.150 yamt * LWPs can get suspended/slept behind us.
652 1.150 yamt * (eg. sa_setwoken)
653 1.150 yamt * kick them again and retry.
654 1.150 yamt */
655 1.150 yamt goto retry;
656 1.150 yamt }
657 1.107 thorpej if (error)
658 1.147 christos panic("exit_lwps: lwp_wait1 failed with error %d",
659 1.107 thorpej error);
660 1.107 thorpej DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
661 1.145 perry }
662 1.107 thorpej
663 1.165 ad KERNEL_LOCK(nlocks, l);
664 1.24 cgd }
665 1.24 cgd
666 1.24 cgd int
667 1.107 thorpej sys_wait4(struct lwp *l, void *v, register_t *retval)
668 1.31 thorpej {
669 1.76 augustss struct sys_wait4_args /* {
670 1.89 lukem syscallarg(int) pid;
671 1.89 lukem syscallarg(int *) status;
672 1.89 lukem syscallarg(int) options;
673 1.89 lukem syscallarg(struct rusage *) rusage;
674 1.31 thorpej } */ *uap = v;
675 1.109 dsl struct proc *child, *parent;
676 1.109 dsl int status, error;
677 1.166 ad struct rusage ru;
678 1.24 cgd
679 1.109 dsl parent = l->l_proc;
680 1.107 thorpej
681 1.26 cgd if (SCARG(uap, pid) == 0)
682 1.109 dsl SCARG(uap, pid) = -parent->p_pgid;
683 1.109 dsl if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
684 1.24 cgd return (EINVAL);
685 1.41 mikel
686 1.169 ad mutex_enter(&proclist_lock);
687 1.165 ad
688 1.109 dsl error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
689 1.165 ad &child, &status);
690 1.165 ad if (error != 0) {
691 1.169 ad mutex_exit(&proclist_lock);
692 1.109 dsl return error;
693 1.165 ad }
694 1.109 dsl if (child == NULL) {
695 1.169 ad mutex_exit(&proclist_lock);
696 1.109 dsl *retval = 0;
697 1.109 dsl return 0;
698 1.109 dsl }
699 1.109 dsl
700 1.109 dsl retval[0] = child->p_pid;
701 1.109 dsl
702 1.142 christos if (P_ZOMBIE(child)) {
703 1.165 ad KERNEL_LOCK(1, l); /* XXXSMP */
704 1.165 ad /* proc_free() will release the proclist_lock. */
705 1.166 ad proc_free(child, (SCARG(uap, rusage) == NULL ? NULL : &ru));
706 1.166 ad KERNEL_UNLOCK_ONE(l); /* XXXSMP */
707 1.165 ad
708 1.165 ad if (SCARG(uap, rusage))
709 1.166 ad error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
710 1.165 ad if (error == 0 && SCARG(uap, status))
711 1.126 dsl error = copyout(&status, SCARG(uap, status),
712 1.165 ad sizeof(status));
713 1.109 dsl
714 1.165 ad return error;
715 1.109 dsl }
716 1.109 dsl
717 1.169 ad mutex_exit(&proclist_lock);
718 1.165 ad
719 1.165 ad /* Child state must have been SSTOP. */
720 1.109 dsl if (SCARG(uap, status)) {
721 1.165 ad status = W_STOPCODE(status);
722 1.128 dsl return copyout(&status, SCARG(uap, status), sizeof(status));
723 1.109 dsl }
724 1.165 ad
725 1.109 dsl return 0;
726 1.109 dsl }
727 1.109 dsl
728 1.109 dsl /*
729 1.109 dsl * Scan list of child processes for a child process that has stopped or
730 1.109 dsl * exited. Used by sys_wait4 and 'compat' equivalents.
731 1.165 ad *
732 1.169 ad * Must be called with the proclist_lock held, and may release
733 1.165 ad * while waiting.
734 1.109 dsl */
735 1.109 dsl int
736 1.109 dsl find_stopped_child(struct proc *parent, pid_t pid, int options,
737 1.165 ad struct proc **child_p, int *status_p)
738 1.109 dsl {
739 1.165 ad struct proc *child, *dead;
740 1.128 dsl int error;
741 1.68 thorpej
742 1.169 ad KASSERT(mutex_owned(&proclist_lock));
743 1.165 ad
744 1.120 yamt for (;;) {
745 1.128 dsl error = ECHILD;
746 1.165 ad dead = NULL;
747 1.165 ad
748 1.165 ad mutex_enter(&proclist_mutex);
749 1.109 dsl LIST_FOREACH(child, &parent->p_children, p_sibling) {
750 1.128 dsl if (pid >= 0) {
751 1.128 dsl if (child->p_pid != pid) {
752 1.128 dsl child = p_find(pid, PFIND_ZOMBIE |
753 1.165 ad PFIND_LOCKED);
754 1.165 ad if (child == NULL ||
755 1.165 ad child->p_pptr != parent) {
756 1.128 dsl child = NULL;
757 1.128 dsl break;
758 1.128 dsl }
759 1.128 dsl }
760 1.165 ad } else if (pid != WAIT_ANY && child->p_pgid != -pid) {
761 1.165 ad /* Child not in correct pgrp */
762 1.165 ad continue;
763 1.165 ad }
764 1.165 ad
765 1.109 dsl /*
766 1.109 dsl * Wait for processes with p_exitsig != SIGCHLD
767 1.109 dsl * processes only if WALTSIG is set; wait for
768 1.109 dsl * processes with p_exitsig == SIGCHLD only
769 1.109 dsl * if WALTSIG is clear.
770 1.109 dsl */
771 1.109 dsl if (((options & WALLSIG) == 0) &&
772 1.109 dsl (options & WALTSIG ? child->p_exitsig == SIGCHLD
773 1.128 dsl : P_EXITSIG(child) != SIGCHLD)){
774 1.128 dsl if (child->p_pid == pid) {
775 1.128 dsl child = NULL;
776 1.128 dsl break;
777 1.128 dsl }
778 1.109 dsl continue;
779 1.128 dsl }
780 1.109 dsl
781 1.128 dsl error = 0;
782 1.165 ad if ((options & WNOZOMBIE) == 0) {
783 1.165 ad if (child->p_stat == SZOMB)
784 1.165 ad break;
785 1.165 ad if (child->p_stat == SDEAD) {
786 1.165 ad /*
787 1.165 ad * We may occasionally arrive here
788 1.165 ad * after receiving a signal, but
789 1.165 ad * immediatley before the child
790 1.165 ad * process is zombified. The wait
791 1.165 ad * will be short, so avoid returning
792 1.165 ad * to userspace.
793 1.165 ad */
794 1.165 ad dead = child;
795 1.165 ad }
796 1.165 ad }
797 1.109 dsl
798 1.109 dsl if (child->p_stat == SSTOP &&
799 1.165 ad child->p_waited == 0 &&
800 1.165 ad (child->p_slflag & PSL_TRACED ||
801 1.165 ad options & WUNTRACED)) {
802 1.128 dsl if ((options & WNOWAIT) == 0) {
803 1.165 ad child->p_waited = 1;
804 1.128 dsl parent->p_nstopchild--;
805 1.128 dsl }
806 1.128 dsl break;
807 1.128 dsl }
808 1.128 dsl if (parent->p_nstopchild == 0 || child->p_pid == pid) {
809 1.128 dsl child = NULL;
810 1.128 dsl break;
811 1.24 cgd }
812 1.109 dsl }
813 1.165 ad
814 1.165 ad if (child != NULL || error != 0 ||
815 1.165 ad ((options & WNOHANG) != 0 && dead == NULL)) {
816 1.165 ad if (child != NULL)
817 1.165 ad *status_p = child->p_xstat;
818 1.165 ad mutex_exit(&proclist_mutex);
819 1.128 dsl *child_p = child;
820 1.128 dsl return error;
821 1.109 dsl }
822 1.165 ad
823 1.165 ad /*
824 1.165 ad * Wait for another child process to stop.
825 1.165 ad */
826 1.169 ad mutex_exit(&proclist_lock);
827 1.165 ad error = cv_wait_sig(&parent->p_waitcv, &proclist_mutex);
828 1.165 ad mutex_exit(&proclist_mutex);
829 1.169 ad mutex_enter(&proclist_lock);
830 1.165 ad
831 1.109 dsl if (error != 0)
832 1.109 dsl return error;
833 1.109 dsl }
834 1.109 dsl }
835 1.109 dsl
836 1.109 dsl /*
837 1.165 ad * Free a process after parent has taken all the state info. Must be called
838 1.169 ad * with the proclist lock held, and will release before returning.
839 1.165 ad *
840 1.165 ad * *ru is returned to the caller, and must be freed by the caller.
841 1.109 dsl */
842 1.109 dsl void
843 1.166 ad proc_free(struct proc *p, struct rusage *caller_ru)
844 1.109 dsl {
845 1.165 ad struct plimit *plim;
846 1.165 ad struct pstats *pstats;
847 1.165 ad struct proc *parent;
848 1.165 ad struct lwp *l;
849 1.140 pk ksiginfo_t ksi;
850 1.165 ad kauth_cred_t cred;
851 1.165 ad struct vnode *vp;
852 1.165 ad uid_t uid;
853 1.24 cgd
854 1.169 ad KASSERT(mutex_owned(&proclist_lock));
855 1.165 ad KASSERT(p->p_nlwps == 1);
856 1.165 ad KASSERT(p->p_nzlwps == 1);
857 1.137 yamt KASSERT(p->p_nrlwps == 0);
858 1.165 ad KASSERT(p->p_stat == SZOMB);
859 1.137 yamt
860 1.166 ad if (caller_ru != NULL)
861 1.175 dsl memcpy(caller_ru, &p->p_stats->p_ru, sizeof(*caller_ru));
862 1.166 ad
863 1.109 dsl /*
864 1.109 dsl * If we got the child via ptrace(2) or procfs, and
865 1.109 dsl * the parent is different (meaning the process was
866 1.109 dsl * attached, rather than run as a child), then we need
867 1.109 dsl * to give it back to the old parent, and send the
868 1.109 dsl * parent the exit signal. The rest of the cleanup
869 1.109 dsl * will be done when the old parent waits on the child.
870 1.109 dsl */
871 1.165 ad if ((p->p_slflag & PSL_TRACED) != 0) {
872 1.165 ad parent = p->p_pptr;
873 1.165 ad if (p->p_opptr != parent){
874 1.165 ad mutex_enter(&p->p_smutex);
875 1.165 ad p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
876 1.165 ad mutex_exit(&p->p_smutex);
877 1.165 ad parent = p->p_opptr;
878 1.165 ad if (parent == NULL)
879 1.165 ad parent = initproc;
880 1.165 ad proc_reparent(p, parent);
881 1.165 ad p->p_opptr = NULL;
882 1.165 ad if (p->p_exitsig != 0) {
883 1.165 ad exit_psignal(p, parent, &ksi);
884 1.165 ad mutex_enter(&proclist_mutex);
885 1.165 ad kpsignal(parent, &ksi, NULL);
886 1.165 ad mutex_exit(&proclist_mutex);
887 1.165 ad }
888 1.173 ad cv_signal(&parent->p_waitcv);
889 1.169 ad mutex_exit(&proclist_lock);
890 1.165 ad return;
891 1.140 pk }
892 1.109 dsl }
893 1.109 dsl
894 1.165 ad /*
895 1.165 ad * Finally finished with old proc entry. Unlink it from its process
896 1.165 ad * group.
897 1.165 ad */
898 1.165 ad leavepgrp(p);
899 1.165 ad
900 1.165 ad parent = p->p_pptr;
901 1.109 dsl scheduler_wait_hook(parent, p);
902 1.175 dsl ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
903 1.175 dsl ruadd(&parent->p_stats->p_cru, &p->p_stats->p_cru);
904 1.109 dsl p->p_xstat = 0;
905 1.115 dsl
906 1.115 dsl /*
907 1.165 ad * At this point we are going to start freeing the final resources.
908 1.165 ad * If anyone tries to access the proc structure after here they will
909 1.165 ad * get a shock - bits are missing. Attempt to make it hard! We
910 1.165 ad * don't bother with any further locking past this point.
911 1.115 dsl */
912 1.165 ad mutex_enter(&proclist_mutex);
913 1.165 ad p->p_stat = SIDL; /* not even a zombie any more */
914 1.165 ad LIST_REMOVE(p, p_list); /* off zombproc */
915 1.165 ad parent = p->p_pptr;
916 1.165 ad p->p_pptr->p_nstopchild--;
917 1.165 ad mutex_exit(&proclist_mutex);
918 1.165 ad LIST_REMOVE(p, p_sibling);
919 1.115 dsl
920 1.165 ad uid = kauth_cred_getuid(p->p_cred);
921 1.165 ad vp = p->p_textvp;
922 1.165 ad cred = p->p_cred;
923 1.165 ad l = LIST_FIRST(&p->p_lwps);
924 1.165 ad
925 1.165 ad mutex_destroy(&p->p_rasmutex);
926 1.165 ad mutex_destroy(&p->p_mutex);
927 1.165 ad mutex_destroy(&p->p_stmutex);
928 1.165 ad mutex_destroy(&p->p_smutex);
929 1.165 ad cv_destroy(&p->p_waitcv);
930 1.165 ad cv_destroy(&p->p_lwpcv);
931 1.165 ad cv_destroy(&p->p_refcv);
932 1.56 thorpej
933 1.109 dsl /*
934 1.165 ad * Delay release until after dropping the proclist lock.
935 1.109 dsl */
936 1.165 ad plim = p->p_limit;
937 1.165 ad pstats = p->p_stats;
938 1.56 thorpej
939 1.165 ad /*
940 1.165 ad * Free the proc structure and let pid be reallocated. This will
941 1.165 ad * release the proclist_lock.
942 1.165 ad */
943 1.165 ad proc_free_mem(p);
944 1.35 mycroft
945 1.109 dsl /*
946 1.109 dsl * Decrement the count of procs running with this uid.
947 1.109 dsl */
948 1.165 ad (void)chgproccnt(uid, -1);
949 1.24 cgd
950 1.109 dsl /*
951 1.165 ad * Release substructures.
952 1.109 dsl */
953 1.165 ad limfree(plim);
954 1.165 ad pstatsfree(pstats);
955 1.165 ad kauth_cred_free(cred);
956 1.165 ad kauth_cred_free(l->l_cred);
957 1.107 thorpej
958 1.109 dsl /*
959 1.109 dsl * Release reference to text vnode
960 1.109 dsl */
961 1.165 ad if (vp)
962 1.165 ad vrele(vp);
963 1.24 cgd
964 1.165 ad /*
965 1.165 ad * Free the last LWP's resources.
966 1.165 ad */
967 1.172 ad lwp_free(l, false, true);
968 1.165 ad
969 1.165 ad /*
970 1.165 ad * Collect child u-areas.
971 1.165 ad */
972 1.168 thorpej uvm_uarea_drain(false);
973 1.24 cgd }
974 1.24 cgd
975 1.24 cgd /*
976 1.24 cgd * make process 'parent' the new parent of process 'child'.
977 1.128 dsl *
978 1.169 ad * Must be called with proclist_lock lock held.
979 1.24 cgd */
980 1.24 cgd void
981 1.82 thorpej proc_reparent(struct proc *child, struct proc *parent)
982 1.24 cgd {
983 1.24 cgd
984 1.169 ad KASSERT(mutex_owned(&proclist_lock));
985 1.165 ad
986 1.24 cgd if (child->p_pptr == parent)
987 1.24 cgd return;
988 1.70 thorpej
989 1.165 ad mutex_enter(&proclist_mutex);
990 1.165 ad if (child->p_stat == SZOMB ||
991 1.165 ad (child->p_stat == SSTOP && !child->p_waited)) {
992 1.128 dsl child->p_pptr->p_nstopchild--;
993 1.128 dsl parent->p_nstopchild++;
994 1.128 dsl }
995 1.165 ad mutex_exit(&proclist_mutex);
996 1.70 thorpej if (parent == initproc)
997 1.70 thorpej child->p_exitsig = SIGCHLD;
998 1.24 cgd
999 1.25 mycroft LIST_REMOVE(child, p_sibling);
1000 1.25 mycroft LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1001 1.24 cgd child->p_pptr = parent;
1002 1.24 cgd }
1003