kern_exit.c revision 1.164 1 1.164 ad /* $NetBSD: kern_exit.c,v 1.164 2006/12/22 08:04:01 ad Exp $ */
2 1.56 thorpej
3 1.56 thorpej /*-
4 1.72 thorpej * Copyright (c) 1998, 1999 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.58 christos * NASA Ames Research Center.
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.164 ad __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.164 2006/12/22 08:04:01 ad 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.24 cgd #include <sys/systm.h>
86 1.24 cgd #include <sys/ioctl.h>
87 1.24 cgd #include <sys/tty.h>
88 1.24 cgd #include <sys/time.h>
89 1.24 cgd #include <sys/resource.h>
90 1.24 cgd #include <sys/kernel.h>
91 1.50 christos #include <sys/ktrace.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.107 thorpej #include <sys/sa.h>
111 1.107 thorpej #include <sys/savar.h>
112 1.26 cgd #include <sys/mount.h>
113 1.26 cgd #include <sys/syscallargs.h>
114 1.94 christos #include <sys/systrace.h>
115 1.156 elad #include <sys/kauth.h>
116 1.26 cgd
117 1.24 cgd #include <machine/cpu.h>
118 1.24 cgd
119 1.47 mrg #include <uvm/uvm_extern.h>
120 1.47 mrg
121 1.107 thorpej #define DEBUG_EXIT
122 1.107 thorpej
123 1.107 thorpej #ifdef DEBUG_EXIT
124 1.107 thorpej int debug_exit = 0;
125 1.107 thorpej #define DPRINTF(x) if (debug_exit) printf x
126 1.107 thorpej #else
127 1.107 thorpej #define DPRINTF(x)
128 1.107 thorpej #endif
129 1.107 thorpej
130 1.107 thorpej static void lwp_exit_hook(struct lwp *, void *);
131 1.123 christos
132 1.123 christos /*
133 1.132 jdolecek * Fill in the appropriate signal information, and signal the parent.
134 1.123 christos */
135 1.123 christos static void
136 1.162 yamt exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
137 1.123 christos {
138 1.123 christos
139 1.163 yamt KSI_INIT(ksi);
140 1.140 pk if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
141 1.123 christos if (WIFSIGNALED(p->p_xstat)) {
142 1.123 christos if (WCOREDUMP(p->p_xstat))
143 1.140 pk ksi->ksi_code = CLD_DUMPED;
144 1.123 christos else
145 1.140 pk ksi->ksi_code = CLD_KILLED;
146 1.123 christos } else {
147 1.140 pk ksi->ksi_code = CLD_EXITED;
148 1.123 christos }
149 1.123 christos }
150 1.123 christos /*
151 1.123 christos * we fill those in, even for non-SIGCHLD.
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.140 pk ksi->ksi_utime = p->p_ru->ru_utime.tv_sec;
158 1.140 pk ksi->ksi_stime = p->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.24 cgd
172 1.107 thorpej /* Don't call exit1() multiple times in the same process.*/
173 1.107 thorpej if (l->l_proc->p_flag & P_WEXIT)
174 1.107 thorpej lwp_exit(l);
175 1.107 thorpej
176 1.107 thorpej exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
177 1.24 cgd /* NOTREACHED */
178 1.31 thorpej return (0);
179 1.24 cgd }
180 1.24 cgd
181 1.24 cgd /*
182 1.24 cgd * Exit: deallocate address space and other resources, change proc state
183 1.24 cgd * to zombie, and unlink proc from allproc and parent's lists. Save exit
184 1.24 cgd * status and rusage for wait(). Check for child processes and orphan them.
185 1.24 cgd */
186 1.31 thorpej void
187 1.107 thorpej exit1(struct lwp *l, int rv)
188 1.24 cgd {
189 1.107 thorpej struct proc *p, *q, *nq;
190 1.108 nathanw int s, sa;
191 1.140 pk struct plimit *plim;
192 1.140 pk struct pstats *pstats;
193 1.140 pk struct sigacts *ps;
194 1.140 pk ksiginfo_t ksi;
195 1.140 pk int do_psignal = 0;
196 1.24 cgd
197 1.107 thorpej p = l->l_proc;
198 1.107 thorpej
199 1.78 thorpej if (__predict_false(p == initproc))
200 1.24 cgd panic("init died (signal %d, exit %d)",
201 1.24 cgd WTERMSIG(rv), WEXITSTATUS(rv));
202 1.73 thorpej
203 1.112 nathanw p->p_flag |= P_WEXIT;
204 1.130 atatat if (p->p_flag & P_STOPEXIT) {
205 1.130 atatat sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
206 1.130 atatat SCHED_LOCK(s);
207 1.130 atatat p->p_stat = SSTOP;
208 1.130 atatat l->l_stat = LSSTOP;
209 1.130 atatat p->p_nrlwps--;
210 1.130 atatat mi_switch(l, NULL);
211 1.130 atatat SCHED_ASSERT_UNLOCKED();
212 1.130 atatat splx(s);
213 1.130 atatat }
214 1.112 nathanw
215 1.107 thorpej DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
216 1.107 thorpej /*
217 1.145 perry * Disable scheduler activation upcalls.
218 1.145 perry * We're trying to get out of here.
219 1.107 thorpej */
220 1.108 nathanw sa = 0;
221 1.110 nathanw if (p->p_sa != NULL) {
222 1.107 thorpej l->l_flag &= ~L_SA;
223 1.118 fvdl #if 0
224 1.107 thorpej p->p_flag &= ~P_SA;
225 1.118 fvdl #endif
226 1.108 nathanw sa = 1;
227 1.107 thorpej }
228 1.107 thorpej
229 1.24 cgd #ifdef PGINPROF
230 1.24 cgd vmsizmon();
231 1.24 cgd #endif
232 1.24 cgd if (p->p_flag & P_PROFIL)
233 1.24 cgd stopprofclock(p);
234 1.55 thorpej p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
235 1.24 cgd /*
236 1.37 mycroft * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
237 1.37 mycroft * wake up the parent early to avoid deadlock.
238 1.24 cgd */
239 1.37 mycroft if (p->p_flag & P_PPWAIT) {
240 1.37 mycroft p->p_flag &= ~P_PPWAIT;
241 1.126 dsl wakeup(p->p_pptr);
242 1.37 mycroft }
243 1.87 jdolecek sigfillset(&p->p_sigctx.ps_sigignore);
244 1.87 jdolecek sigemptyset(&p->p_sigctx.ps_siglist);
245 1.87 jdolecek p->p_sigctx.ps_sigcheck = 0;
246 1.107 thorpej timers_free(p, TIMERS_ALL);
247 1.107 thorpej
248 1.133 jdolecek if (sa || (p->p_nlwps > 1)) {
249 1.107 thorpej exit_lwps(l);
250 1.100 gmcgarry
251 1.133 jdolecek /*
252 1.133 jdolecek * Collect thread u-areas.
253 1.133 jdolecek */
254 1.133 jdolecek uvm_uarea_drain(FALSE);
255 1.133 jdolecek }
256 1.133 jdolecek
257 1.100 gmcgarry #if defined(__HAVE_RAS)
258 1.100 gmcgarry ras_purgeall(p);
259 1.100 gmcgarry #endif
260 1.24 cgd
261 1.24 cgd /*
262 1.24 cgd * Close open files and release open-file table.
263 1.24 cgd * This may block!
264 1.24 cgd */
265 1.154 christos fdfree(l);
266 1.141 pk cwdfree(p->p_cwdi);
267 1.144 dbj p->p_cwdi = 0;
268 1.24 cgd
269 1.93 christos doexithooks(p);
270 1.93 christos
271 1.24 cgd if (SESS_LEADER(p)) {
272 1.76 augustss struct session *sp = p->p_session;
273 1.127 dsl struct tty *tp;
274 1.24 cgd
275 1.24 cgd if (sp->s_ttyvp) {
276 1.24 cgd /*
277 1.24 cgd * Controlling process.
278 1.24 cgd * Signal foreground pgrp,
279 1.24 cgd * drain controlling terminal
280 1.24 cgd * and revoke access to controlling terminal.
281 1.24 cgd */
282 1.127 dsl tp = sp->s_ttyp;
283 1.138 dbj s = spltty();
284 1.127 dsl TTY_LOCK(tp);
285 1.127 dsl if (tp->t_session == sp) {
286 1.127 dsl if (tp->t_pgrp)
287 1.127 dsl pgsignal(tp->t_pgrp, SIGHUP, 1);
288 1.127 dsl /* we can't guarantee the revoke will do this */
289 1.127 dsl tp->t_pgrp = NULL;
290 1.127 dsl tp->t_session = NULL;
291 1.127 dsl TTY_UNLOCK(tp);
292 1.138 dbj splx(s);
293 1.127 dsl SESSRELE(sp);
294 1.127 dsl (void) ttywait(tp);
295 1.24 cgd /*
296 1.24 cgd * The tty could have been revoked
297 1.24 cgd * if we blocked.
298 1.24 cgd */
299 1.24 cgd if (sp->s_ttyvp)
300 1.49 fvdl VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
301 1.138 dbj } else {
302 1.127 dsl TTY_UNLOCK(tp);
303 1.138 dbj splx(s);
304 1.138 dbj }
305 1.24 cgd if (sp->s_ttyvp)
306 1.24 cgd vrele(sp->s_ttyvp);
307 1.24 cgd sp->s_ttyvp = NULL;
308 1.24 cgd /*
309 1.24 cgd * s_ttyp is not zero'd; we use this to indicate
310 1.24 cgd * that the session once had a controlling terminal.
311 1.24 cgd * (for logging and informational purposes)
312 1.24 cgd */
313 1.24 cgd }
314 1.24 cgd sp->s_leader = NULL;
315 1.24 cgd }
316 1.24 cgd fixjobc(p, p->p_pgrp, 0);
317 1.157 ad
318 1.157 ad /*
319 1.164 ad * Write out accounting data.
320 1.157 ad */
321 1.154 christos (void)acct_process(l);
322 1.157 ad
323 1.24 cgd #ifdef KTRACE
324 1.145 perry /*
325 1.157 ad * Release trace file.
326 1.24 cgd */
327 1.50 christos ktrderef(p);
328 1.94 christos #endif
329 1.94 christos #ifdef SYSTRACE
330 1.94 christos systrace_sys_exit(p);
331 1.24 cgd #endif
332 1.157 ad
333 1.24 cgd /*
334 1.99 manu * If emulation has process exit hook, call it now.
335 1.158 manu * Set the exit status now so that the exit hook has
336 1.158 manu * an opportunity to tweak it (COMPAT_LINUX requires
337 1.158 manu * this for thread group emulation)
338 1.99 manu */
339 1.158 manu p->p_xstat = rv;
340 1.99 manu if (p->p_emul->e_proc_exit)
341 1.99 manu (*p->p_emul->e_proc_exit)(p);
342 1.99 manu
343 1.99 manu /*
344 1.160 thorpej * Finalize the last LWP's specificdata, as well as the
345 1.160 thorpej * specificdata for the proc itself.
346 1.160 thorpej */
347 1.160 thorpej lwp_finispecific(l);
348 1.160 thorpej proc_finispecific(p);
349 1.160 thorpej
350 1.160 thorpej /*
351 1.140 pk * Free the VM resources we're still holding on to.
352 1.140 pk * We must do this from a valid thread because doing
353 1.140 pk * so may block. This frees vmspace, which we don't
354 1.140 pk * need anymore. The only remaining lwp is the one
355 1.140 pk * we run at this moment, nothing runs in userland
356 1.140 pk * anymore.
357 1.140 pk */
358 1.140 pk uvm_proc_exit(p);
359 1.140 pk
360 1.140 pk /*
361 1.140 pk * Give machine-dependent code a chance to free any
362 1.140 pk * MD LWP resources while we can still block. This must be done
363 1.145 perry * before uvm_lwp_exit(), in case these resources are in the
364 1.140 pk * PCB.
365 1.140 pk * THIS IS LAST BLOCKING OPERATION.
366 1.140 pk */
367 1.140 pk #ifndef __NO_CPU_LWP_FREE
368 1.140 pk cpu_lwp_free(l, 1);
369 1.140 pk #endif
370 1.140 pk
371 1.140 pk pmap_deactivate(l);
372 1.140 pk
373 1.140 pk /*
374 1.140 pk * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
375 1.140 pk */
376 1.140 pk
377 1.140 pk /*
378 1.158 manu * Save final rusage info, adding in child rusage info and self times.
379 1.140 pk * In order to pick up the time for the current execution, we must
380 1.140 pk * do this before unlinking the lwp from l_list.
381 1.140 pk */
382 1.140 pk *p->p_ru = p->p_stats->p_ru;
383 1.140 pk calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
384 1.140 pk ruadd(p->p_ru, &p->p_stats->p_cru);
385 1.140 pk
386 1.140 pk /*
387 1.140 pk * Notify interested parties of our demise.
388 1.140 pk */
389 1.140 pk KNOTE(&p->p_klist, NOTE_EXIT);
390 1.140 pk
391 1.140 pk #if PERFCTRS
392 1.140 pk /*
393 1.140 pk * Save final PMC information in parent process & clean up.
394 1.140 pk */
395 1.140 pk if (PMC_ENABLED(p)) {
396 1.140 pk pmc_save_context(p);
397 1.140 pk pmc_accumulate(p->p_pptr, p);
398 1.140 pk pmc_process_exit(p);
399 1.140 pk }
400 1.140 pk #endif
401 1.140 pk
402 1.140 pk s = proclist_lock_write();
403 1.140 pk /*
404 1.128 dsl * Reset p_opptr pointer of all former children which got
405 1.128 dsl * traced by another process and were reparented. We reset
406 1.128 dsl * it to NULL here; the trace detach code then reparents
407 1.128 dsl * the child to initproc. We only check allproc list, since
408 1.128 dsl * eventual former children on zombproc list won't reference
409 1.128 dsl * p_opptr anymore.
410 1.128 dsl */
411 1.128 dsl if (p->p_flag & P_CHTRACED) {
412 1.143 yamt PROCLIST_FOREACH(q, &allproc) {
413 1.128 dsl if (q->p_opptr == p)
414 1.128 dsl q->p_opptr = NULL;
415 1.128 dsl }
416 1.128 dsl }
417 1.128 dsl
418 1.128 dsl /*
419 1.72 thorpej * Give orphaned children to init(8).
420 1.72 thorpej */
421 1.101 matt q = LIST_FIRST(&p->p_children);
422 1.132 jdolecek if (q) /* only need this if any child is SZOMB */
423 1.126 dsl wakeup(initproc);
424 1.132 jdolecek for (; q != NULL; q = nq) {
425 1.101 matt nq = LIST_NEXT(q, p_sibling);
426 1.104 jdolecek
427 1.24 cgd /*
428 1.104 jdolecek * Traced processes are killed since their existence
429 1.104 jdolecek * means someone is screwing up. Since we reset the
430 1.104 jdolecek * trace flags, the logic in sys_wait4() would not be
431 1.104 jdolecek * triggered to reparent the process to its
432 1.106 jdolecek * original parent, so we must do this here.
433 1.24 cgd */
434 1.24 cgd if (q->p_flag & P_TRACED) {
435 1.104 jdolecek if (q->p_opptr != q->p_pptr) {
436 1.104 jdolecek struct proc *t = q->p_opptr;
437 1.104 jdolecek proc_reparent(q, t ? t : initproc);
438 1.104 jdolecek q->p_opptr = NULL;
439 1.105 jdolecek } else
440 1.105 jdolecek proc_reparent(q, initproc);
441 1.155 christos q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE|P_SYSCALL);
442 1.153 jdolecek killproc(q, "orphaned traced process");
443 1.104 jdolecek } else {
444 1.104 jdolecek proc_reparent(q, initproc);
445 1.24 cgd }
446 1.24 cgd }
447 1.115 dsl
448 1.115 dsl /*
449 1.133 jdolecek * Move proc from allproc to zombproc, it's now ready
450 1.133 jdolecek * to be collected by parent. Remaining lwp resources
451 1.140 pk * will be freed in lwp_exit2() once we've switch to idle
452 1.133 jdolecek * context.
453 1.133 jdolecek * Changing the state to SZOMB stops it being found by pfind().
454 1.115 dsl */
455 1.115 dsl LIST_REMOVE(p, p_list);
456 1.115 dsl LIST_INSERT_HEAD(&zombproc, p, p_list);
457 1.133 jdolecek p->p_stat = SZOMB;
458 1.133 jdolecek
459 1.115 dsl LIST_REMOVE(l, l_list);
460 1.137 yamt LIST_REMOVE(l, l_sibling);
461 1.152 yamt l->l_flag |= L_DETACHED; /* detached from proc too */
462 1.133 jdolecek l->l_stat = LSDEAD;
463 1.133 jdolecek
464 1.151 yamt KASSERT(p->p_nrlwps == 1);
465 1.151 yamt KASSERT(p->p_nlwps == 1);
466 1.133 jdolecek p->p_nrlwps--;
467 1.133 jdolecek p->p_nlwps--;
468 1.133 jdolecek
469 1.133 jdolecek /* Put in front of parent's sibling list for parent to collect it */
470 1.133 jdolecek q = p->p_pptr;
471 1.133 jdolecek q->p_nstopchild++;
472 1.133 jdolecek if (LIST_FIRST(&q->p_children) != p) {
473 1.133 jdolecek /* Put child where it can be found quickly */
474 1.133 jdolecek LIST_REMOVE(p, p_sibling);
475 1.133 jdolecek LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
476 1.133 jdolecek }
477 1.133 jdolecek
478 1.59 christos /*
479 1.59 christos * Notify parent that we're gone. If parent has the P_NOCLDWAIT
480 1.59 christos * flag set, notify init instead (and hope it will handle
481 1.59 christos * this situation).
482 1.59 christos */
483 1.146 christos if (q->p_flag & (P_NOCLDWAIT|P_CLDSIGIGN)) {
484 1.59 christos proc_reparent(p, initproc);
485 1.133 jdolecek
486 1.59 christos /*
487 1.59 christos * If this was the last child of our parent, notify
488 1.59 christos * parent, so in case he was wait(2)ing, he will
489 1.59 christos * continue.
490 1.59 christos */
491 1.140 pk if (LIST_FIRST(&q->p_children) == NULL)
492 1.140 pk wakeup(q);
493 1.59 christos }
494 1.67 thorpej
495 1.96 manu /*
496 1.107 thorpej * Clear curlwp after we've done all operations
497 1.24 cgd * that could block, and before tearing down the rest
498 1.24 cgd * of the process state that might be used from clock, etc.
499 1.107 thorpej * Also, can't clear curlwp while we're still runnable,
500 1.24 cgd * as we're not on a run queue (we are current, just not
501 1.24 cgd * a proper proc any longer!).
502 1.24 cgd *
503 1.24 cgd * Other substructures are freed from wait().
504 1.24 cgd */
505 1.107 thorpej curlwp = NULL;
506 1.140 pk
507 1.140 pk /* Delay release until after dropping the proclist lock */
508 1.140 pk plim = p->p_limit;
509 1.140 pk pstats = p->p_stats;
510 1.140 pk ps = p->p_sigacts;
511 1.140 pk
512 1.77 thorpej p->p_limit = NULL;
513 1.140 pk p->p_stats = NULL;
514 1.140 pk p->p_sigacts = NULL;
515 1.24 cgd
516 1.140 pk /* Reload parent pointer, since p may have been reparented above */
517 1.140 pk q = p->p_pptr;
518 1.140 pk
519 1.140 pk if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) {
520 1.140 pk exit_psignal(p, q, &ksi);
521 1.140 pk do_psignal = 1;
522 1.140 pk }
523 1.140 pk
524 1.140 pk /*
525 1.140 pk * Once we release the proclist lock, we shouldn't touch the
526 1.140 pk * process structure anymore, since it's now on the zombie
527 1.140 pk * list and available for collection by the parent.
528 1.140 pk */
529 1.140 pk proclist_unlock_write(s);
530 1.140 pk
531 1.140 pk if (do_psignal)
532 1.140 pk kpsignal(q, &ksi, NULL);
533 1.140 pk
534 1.140 pk /* Wake up the parent so it can get exit status. */
535 1.140 pk wakeup(q);
536 1.140 pk
537 1.140 pk /* Release substructures */
538 1.140 pk sigactsfree(ps);
539 1.140 pk limfree(plim);
540 1.140 pk pstatsfree(pstats);
541 1.85 thorpej
542 1.157 ad /* Release cached credentials. */
543 1.157 ad kauth_cred_free(l->l_cred);
544 1.157 ad
545 1.133 jdolecek #ifdef DEBUG
546 1.133 jdolecek /* Nothing should use the process link anymore */
547 1.133 jdolecek l->l_proc = NULL;
548 1.133 jdolecek #endif
549 1.133 jdolecek
550 1.140 pk /* This process no longer needs to hold the kernel lock. */
551 1.140 pk KERNEL_PROC_UNLOCK(l);
552 1.140 pk
553 1.24 cgd /*
554 1.56 thorpej * Finally, call machine-dependent code to switch to a new
555 1.56 thorpej * context (possibly the idle context). Once we are no longer
556 1.133 jdolecek * using the dead lwp's stack, lwp_exit2() will be called
557 1.133 jdolecek * to arrange for the resources to be released.
558 1.56 thorpej *
559 1.56 thorpej * Note that cpu_exit() will end with a call equivalent to
560 1.56 thorpej * cpu_switch(), finishing our execution (pun intended).
561 1.24 cgd */
562 1.107 thorpej
563 1.133 jdolecek uvmexp.swtch++;
564 1.133 jdolecek cpu_exit(l);
565 1.107 thorpej }
566 1.107 thorpej
567 1.107 thorpej void
568 1.107 thorpej exit_lwps(struct lwp *l)
569 1.107 thorpej {
570 1.107 thorpej struct proc *p;
571 1.107 thorpej struct lwp *l2;
572 1.139 cl struct sadata_vp *vp;
573 1.107 thorpej int s, error;
574 1.107 thorpej lwpid_t waited;
575 1.107 thorpej
576 1.107 thorpej p = l->l_proc;
577 1.107 thorpej
578 1.145 perry /* XXX SMP
579 1.145 perry * This would be the right place to IPI any LWPs running on
580 1.107 thorpej * other processors so that they can notice the userret exit hook.
581 1.107 thorpej */
582 1.107 thorpej p->p_userret = lwp_exit_hook;
583 1.107 thorpej p->p_userret_arg = NULL;
584 1.107 thorpej
585 1.131 cl if (p->p_sa) {
586 1.139 cl SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
587 1.139 cl /*
588 1.139 cl * Make SA-cached LWPs normal process runnable
589 1.139 cl * LWPs so that they'll also self-destruct.
590 1.139 cl */
591 1.139 cl DPRINTF(("exit_lwps: Making cached LWPs of %d on VP %d runnable: ",
592 1.139 cl p->p_pid, vp->savp_id));
593 1.139 cl SCHED_LOCK(s);
594 1.139 cl while ((l2 = sa_getcachelwp(vp)) != 0) {
595 1.139 cl l2->l_priority = l2->l_usrpri;
596 1.139 cl setrunnable(l2);
597 1.139 cl DPRINTF(("%d ", l2->l_lid));
598 1.139 cl }
599 1.139 cl SCHED_UNLOCK(s);
600 1.139 cl DPRINTF(("\n"));
601 1.139 cl
602 1.139 cl /*
603 1.139 cl * Clear wokenq, the LWPs on the queue will
604 1.139 cl * run below.
605 1.139 cl */
606 1.139 cl vp->savp_wokenq_head = NULL;
607 1.107 thorpej }
608 1.107 thorpej }
609 1.145 perry
610 1.150 yamt retry:
611 1.107 thorpej /*
612 1.107 thorpej * Interrupt LWPs in interruptable sleep, unsuspend suspended
613 1.111 skrll * LWPs, make detached LWPs undetached (so we can wait for
614 1.145 perry * them) and then wait for everyone else to finish.
615 1.107 thorpej */
616 1.107 thorpej LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
617 1.107 thorpej l2->l_flag &= ~(L_DETACHED|L_SA);
618 1.128 dsl
619 1.149 yamt SCHED_LOCK(s);
620 1.107 thorpej if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
621 1.118 fvdl l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
622 1.107 thorpej setrunnable(l2);
623 1.107 thorpej DPRINTF(("exit_lwps: Made %d.%d runnable\n",
624 1.107 thorpej p->p_pid, l2->l_lid));
625 1.107 thorpej }
626 1.149 yamt SCHED_UNLOCK(s);
627 1.107 thorpej }
628 1.107 thorpej
629 1.145 perry
630 1.107 thorpej while (p->p_nlwps > 1) {
631 1.145 perry DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
632 1.107 thorpej p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
633 1.107 thorpej error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
634 1.150 yamt if (error == EDEADLK) {
635 1.150 yamt /*
636 1.150 yamt * LWPs can get suspended/slept behind us.
637 1.150 yamt * (eg. sa_setwoken)
638 1.150 yamt * kick them again and retry.
639 1.150 yamt */
640 1.150 yamt goto retry;
641 1.150 yamt }
642 1.107 thorpej if (error)
643 1.147 christos panic("exit_lwps: lwp_wait1 failed with error %d",
644 1.107 thorpej error);
645 1.107 thorpej DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
646 1.145 perry }
647 1.107 thorpej
648 1.107 thorpej p->p_userret = NULL;
649 1.107 thorpej }
650 1.107 thorpej
651 1.107 thorpej /* Wrapper function for use in p_userret */
652 1.107 thorpej static void
653 1.162 yamt lwp_exit_hook(struct lwp *l, void *arg)
654 1.107 thorpej {
655 1.162 yamt
656 1.107 thorpej KERNEL_PROC_LOCK(l);
657 1.107 thorpej lwp_exit(l);
658 1.24 cgd }
659 1.24 cgd
660 1.24 cgd int
661 1.107 thorpej sys_wait4(struct lwp *l, void *v, register_t *retval)
662 1.31 thorpej {
663 1.76 augustss struct sys_wait4_args /* {
664 1.89 lukem syscallarg(int) pid;
665 1.89 lukem syscallarg(int *) status;
666 1.89 lukem syscallarg(int) options;
667 1.89 lukem syscallarg(struct rusage *) rusage;
668 1.31 thorpej } */ *uap = v;
669 1.109 dsl struct proc *child, *parent;
670 1.109 dsl int status, error;
671 1.24 cgd
672 1.109 dsl parent = l->l_proc;
673 1.107 thorpej
674 1.26 cgd if (SCARG(uap, pid) == 0)
675 1.109 dsl SCARG(uap, pid) = -parent->p_pgid;
676 1.109 dsl if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
677 1.24 cgd return (EINVAL);
678 1.41 mikel
679 1.109 dsl error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
680 1.109 dsl &child);
681 1.109 dsl if (error != 0)
682 1.109 dsl return error;
683 1.109 dsl if (child == NULL) {
684 1.109 dsl *retval = 0;
685 1.109 dsl return 0;
686 1.109 dsl }
687 1.109 dsl
688 1.133 jdolecek /*
689 1.133 jdolecek * Collect child u-areas.
690 1.133 jdolecek */
691 1.133 jdolecek uvm_uarea_drain(FALSE);
692 1.133 jdolecek
693 1.109 dsl retval[0] = child->p_pid;
694 1.109 dsl
695 1.142 christos if (P_ZOMBIE(child)) {
696 1.109 dsl if (SCARG(uap, status)) {
697 1.109 dsl status = child->p_xstat; /* convert to int */
698 1.126 dsl error = copyout(&status, SCARG(uap, status),
699 1.109 dsl sizeof(status));
700 1.109 dsl if (error)
701 1.109 dsl return (error);
702 1.109 dsl }
703 1.109 dsl if (SCARG(uap, rusage)) {
704 1.126 dsl error = copyout(child->p_ru, SCARG(uap, rusage),
705 1.109 dsl sizeof(struct rusage));
706 1.109 dsl if (error)
707 1.109 dsl return (error);
708 1.109 dsl }
709 1.109 dsl
710 1.109 dsl proc_free(child);
711 1.109 dsl return 0;
712 1.109 dsl }
713 1.109 dsl
714 1.109 dsl /* child state must be SSTOP */
715 1.109 dsl if (SCARG(uap, status)) {
716 1.109 dsl status = W_STOPCODE(child->p_xstat);
717 1.128 dsl return copyout(&status, SCARG(uap, status), sizeof(status));
718 1.109 dsl }
719 1.109 dsl return 0;
720 1.109 dsl }
721 1.109 dsl
722 1.109 dsl /*
723 1.109 dsl * Scan list of child processes for a child process that has stopped or
724 1.109 dsl * exited. Used by sys_wait4 and 'compat' equivalents.
725 1.109 dsl */
726 1.109 dsl int
727 1.109 dsl find_stopped_child(struct proc *parent, pid_t pid, int options,
728 1.109 dsl struct proc **child_p)
729 1.109 dsl {
730 1.109 dsl struct proc *child;
731 1.128 dsl int error;
732 1.68 thorpej
733 1.120 yamt for (;;) {
734 1.128 dsl proclist_lock_read();
735 1.128 dsl error = ECHILD;
736 1.109 dsl LIST_FOREACH(child, &parent->p_children, p_sibling) {
737 1.128 dsl if (pid >= 0) {
738 1.128 dsl if (child->p_pid != pid) {
739 1.128 dsl child = p_find(pid, PFIND_ZOMBIE |
740 1.128 dsl PFIND_LOCKED);
741 1.128 dsl if (child == NULL
742 1.128 dsl || child->p_pptr != parent) {
743 1.128 dsl child = NULL;
744 1.128 dsl break;
745 1.128 dsl }
746 1.128 dsl }
747 1.128 dsl } else
748 1.128 dsl if (pid != WAIT_ANY && child->p_pgid != -pid)
749 1.128 dsl /* child not in correct pgrp */
750 1.128 dsl continue;
751 1.109 dsl /*
752 1.109 dsl * Wait for processes with p_exitsig != SIGCHLD
753 1.109 dsl * processes only if WALTSIG is set; wait for
754 1.109 dsl * processes with p_exitsig == SIGCHLD only
755 1.109 dsl * if WALTSIG is clear.
756 1.109 dsl */
757 1.109 dsl if (((options & WALLSIG) == 0) &&
758 1.109 dsl (options & WALTSIG ? child->p_exitsig == SIGCHLD
759 1.128 dsl : P_EXITSIG(child) != SIGCHLD)){
760 1.128 dsl if (child->p_pid == pid) {
761 1.128 dsl child = NULL;
762 1.128 dsl break;
763 1.128 dsl }
764 1.109 dsl continue;
765 1.128 dsl }
766 1.109 dsl
767 1.128 dsl error = 0;
768 1.128 dsl if (child->p_stat == SZOMB && !(options & WNOZOMBIE))
769 1.128 dsl break;
770 1.109 dsl
771 1.109 dsl if (child->p_stat == SSTOP &&
772 1.109 dsl (child->p_flag & P_WAITED) == 0 &&
773 1.109 dsl (child->p_flag & P_TRACED || options & WUNTRACED)) {
774 1.128 dsl if ((options & WNOWAIT) == 0) {
775 1.109 dsl child->p_flag |= P_WAITED;
776 1.128 dsl parent->p_nstopchild--;
777 1.128 dsl }
778 1.128 dsl break;
779 1.128 dsl }
780 1.128 dsl if (parent->p_nstopchild == 0 || child->p_pid == pid) {
781 1.128 dsl child = NULL;
782 1.128 dsl break;
783 1.24 cgd }
784 1.109 dsl }
785 1.128 dsl proclist_unlock_read();
786 1.128 dsl if (child != NULL || error != 0 || options & WNOHANG) {
787 1.128 dsl *child_p = child;
788 1.128 dsl return error;
789 1.109 dsl }
790 1.126 dsl error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
791 1.109 dsl if (error != 0)
792 1.109 dsl return error;
793 1.109 dsl }
794 1.109 dsl }
795 1.109 dsl
796 1.109 dsl /*
797 1.109 dsl * Free a process after parent has taken all the state info.
798 1.109 dsl */
799 1.109 dsl void
800 1.109 dsl proc_free(struct proc *p)
801 1.109 dsl {
802 1.109 dsl struct proc *parent = p->p_pptr;
803 1.140 pk ksiginfo_t ksi;
804 1.109 dsl int s;
805 1.24 cgd
806 1.137 yamt KASSERT(p->p_nlwps == 0);
807 1.137 yamt KASSERT(p->p_nzlwps == 0);
808 1.137 yamt KASSERT(p->p_nrlwps == 0);
809 1.137 yamt KASSERT(LIST_EMPTY(&p->p_lwps));
810 1.137 yamt
811 1.109 dsl /*
812 1.109 dsl * If we got the child via ptrace(2) or procfs, and
813 1.109 dsl * the parent is different (meaning the process was
814 1.109 dsl * attached, rather than run as a child), then we need
815 1.109 dsl * to give it back to the old parent, and send the
816 1.109 dsl * parent the exit signal. The rest of the cleanup
817 1.109 dsl * will be done when the old parent waits on the child.
818 1.109 dsl */
819 1.109 dsl if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
820 1.109 dsl parent = p->p_opptr;
821 1.109 dsl if (parent == NULL)
822 1.109 dsl parent = initproc;
823 1.109 dsl proc_reparent(p, parent);
824 1.109 dsl p->p_opptr = NULL;
825 1.155 christos p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE|P_SYSCALL);
826 1.140 pk if (p->p_exitsig != 0) {
827 1.140 pk exit_psignal(p, parent, &ksi);
828 1.140 pk kpsignal(parent, &ksi, NULL);
829 1.140 pk }
830 1.126 dsl wakeup(parent);
831 1.109 dsl return;
832 1.109 dsl }
833 1.109 dsl
834 1.109 dsl scheduler_wait_hook(parent, p);
835 1.109 dsl p->p_xstat = 0;
836 1.115 dsl
837 1.109 dsl ruadd(&parent->p_stats->p_cru, p->p_ru);
838 1.115 dsl
839 1.115 dsl /*
840 1.115 dsl * At this point we are going to start freeing the final resources.
841 1.115 dsl * If anyone tries to access the proc structure after here they
842 1.115 dsl * will get a shock - bits are missing.
843 1.115 dsl * Attempt to make it hard!
844 1.115 dsl */
845 1.115 dsl
846 1.115 dsl p->p_stat = SIDL; /* not even a zombie any more */
847 1.115 dsl
848 1.109 dsl pool_put(&rusage_pool, p->p_ru);
849 1.56 thorpej
850 1.109 dsl /*
851 1.109 dsl * Finally finished with old proc entry.
852 1.109 dsl * Unlink it from its process group and free it.
853 1.109 dsl */
854 1.109 dsl leavepgrp(p);
855 1.56 thorpej
856 1.109 dsl s = proclist_lock_write();
857 1.109 dsl LIST_REMOVE(p, p_list); /* off zombproc */
858 1.128 dsl p->p_pptr->p_nstopchild--;
859 1.115 dsl LIST_REMOVE(p, p_sibling);
860 1.109 dsl proclist_unlock_write(s);
861 1.35 mycroft
862 1.109 dsl /*
863 1.109 dsl * Decrement the count of procs running with this uid.
864 1.109 dsl */
865 1.156 elad (void)chgproccnt(kauth_cred_getuid(p->p_cred), -1);
866 1.24 cgd
867 1.109 dsl /*
868 1.109 dsl * Free up credentials.
869 1.109 dsl */
870 1.156 elad kauth_cred_free(p->p_cred);
871 1.107 thorpej
872 1.109 dsl /*
873 1.109 dsl * Release reference to text vnode
874 1.109 dsl */
875 1.109 dsl if (p->p_textvp)
876 1.109 dsl vrele(p->p_textvp);
877 1.24 cgd
878 1.129 cl /* Release any SA state. */
879 1.129 cl if (p->p_sa)
880 1.129 cl sa_release(p);
881 1.109 dsl
882 1.115 dsl /* Free proc structure and let pid be reallocated */
883 1.115 dsl proc_free_mem(p);
884 1.24 cgd }
885 1.24 cgd
886 1.24 cgd /*
887 1.24 cgd * make process 'parent' the new parent of process 'child'.
888 1.128 dsl *
889 1.128 dsl * Must be called with proclist_lock_write() held.
890 1.24 cgd */
891 1.24 cgd void
892 1.82 thorpej proc_reparent(struct proc *child, struct proc *parent)
893 1.24 cgd {
894 1.24 cgd
895 1.24 cgd if (child->p_pptr == parent)
896 1.24 cgd return;
897 1.70 thorpej
898 1.128 dsl if (child->p_stat == SZOMB
899 1.128 dsl || (child->p_stat == SSTOP && !(child->p_flag & P_WAITED))) {
900 1.128 dsl child->p_pptr->p_nstopchild--;
901 1.128 dsl parent->p_nstopchild++;
902 1.128 dsl }
903 1.70 thorpej if (parent == initproc)
904 1.70 thorpej child->p_exitsig = SIGCHLD;
905 1.24 cgd
906 1.25 mycroft LIST_REMOVE(child, p_sibling);
907 1.25 mycroft LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
908 1.24 cgd child->p_pptr = parent;
909 1.24 cgd }
910