1 1.300 riastrad /* $NetBSD: kern_exit.c,v 1.300 2025/03/16 15:52:18 riastradh Exp $ */ 2 1.56 thorpej 3 1.56 thorpej /*- 4 1.295 ad * Copyright (c) 1998, 1999, 2006, 2007, 2008, 2020, 2023 5 1.295 ad * The NetBSD Foundation, Inc. 6 1.56 thorpej * All rights reserved. 7 1.58 christos * 8 1.56 thorpej * This code is derived from software contributed to The NetBSD Foundation 9 1.56 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 1.165 ad * NASA Ames Research Center, and by Andrew Doran. 11 1.56 thorpej * 12 1.56 thorpej * Redistribution and use in source and binary forms, with or without 13 1.56 thorpej * modification, are permitted provided that the following conditions 14 1.56 thorpej * are met: 15 1.56 thorpej * 1. Redistributions of source code must retain the above copyright 16 1.56 thorpej * notice, this list of conditions and the following disclaimer. 17 1.58 christos * 2. Redistributions in binary form must reproduce the above copyright 18 1.56 thorpej * notice, this list of conditions and the following disclaimer in the 19 1.56 thorpej * documentation and/or other materials provided with the distribution. 20 1.58 christos * 21 1.56 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 1.56 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 1.56 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.56 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 1.56 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 1.56 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 1.56 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 1.56 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 1.56 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 1.56 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 1.56 thorpej * POSSIBILITY OF SUCH DAMAGE. 32 1.56 thorpej */ 33 1.24 cgd 34 1.24 cgd /* 35 1.24 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993 36 1.24 cgd * The Regents of the University of California. All rights reserved. 37 1.24 cgd * (c) UNIX System Laboratories, Inc. 38 1.24 cgd * All or some portions of this file are derived from material licensed 39 1.24 cgd * to the University of California by American Telephone and Telegraph 40 1.24 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with 41 1.24 cgd * the permission of UNIX System Laboratories, Inc. 42 1.24 cgd * 43 1.24 cgd * Redistribution and use in source and binary forms, with or without 44 1.24 cgd * modification, are permitted provided that the following conditions 45 1.24 cgd * are met: 46 1.24 cgd * 1. Redistributions of source code must retain the above copyright 47 1.24 cgd * notice, this list of conditions and the following disclaimer. 48 1.24 cgd * 2. Redistributions in binary form must reproduce the above copyright 49 1.24 cgd * notice, this list of conditions and the following disclaimer in the 50 1.24 cgd * documentation and/or other materials provided with the distribution. 51 1.119 agc * 3. Neither the name of the University nor the names of its contributors 52 1.24 cgd * may be used to endorse or promote products derived from this software 53 1.24 cgd * without specific prior written permission. 54 1.24 cgd * 55 1.24 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 1.24 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 1.24 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 1.24 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 1.24 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 1.24 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 1.24 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 1.24 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 1.24 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 1.24 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 1.24 cgd * SUCH DAMAGE. 66 1.24 cgd * 67 1.49 fvdl * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95 68 1.24 cgd */ 69 1.92 lukem 70 1.92 lukem #include <sys/cdefs.h> 71 1.300 riastrad __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.300 2025/03/16 15:52:18 riastradh Exp $"); 72 1.48 mrg 73 1.51 thorpej #include "opt_ktrace.h" 74 1.245 christos #include "opt_dtrace.h" 75 1.60 tron #include "opt_sysv.h" 76 1.24 cgd 77 1.24 cgd #include <sys/param.h> 78 1.299 riastrad #include <sys/types.h> 79 1.299 riastrad 80 1.299 riastrad #include <sys/acct.h> 81 1.299 riastrad #include <sys/atomic.h> 82 1.24 cgd #include <sys/buf.h> 83 1.299 riastrad #include <sys/cpu.h> 84 1.24 cgd #include <sys/file.h> 85 1.299 riastrad #include <sys/filedesc.h> 86 1.274 hannken #include <sys/fstrans.h> 87 1.299 riastrad #include <sys/ioctl.h> 88 1.299 riastrad #include <sys/kauth.h> 89 1.299 riastrad #include <sys/kernel.h> 90 1.299 riastrad #include <sys/ktrace.h> 91 1.299 riastrad #include <sys/lock.h> 92 1.299 riastrad #include <sys/lockdebug.h> 93 1.299 riastrad #include <sys/lwpctl.h> 94 1.299 riastrad #include <sys/mount.h> 95 1.53 thorpej #include <sys/pool.h> 96 1.299 riastrad #include <sys/proc.h> 97 1.299 riastrad #include <sys/psref.h> 98 1.24 cgd #include <sys/ptrace.h> 99 1.100 gmcgarry #include <sys/ras.h> 100 1.299 riastrad #include <sys/resource.h> 101 1.299 riastrad #include <sys/sched.h> 102 1.299 riastrad #include <sys/sdt.h> 103 1.36 christos #include <sys/signalvar.h> 104 1.299 riastrad #include <sys/sleepq.h> 105 1.26 cgd #include <sys/syscallargs.h> 106 1.299 riastrad #include <sys/syslog.h> 107 1.299 riastrad #include <sys/systm.h> 108 1.299 riastrad #include <sys/time.h> 109 1.299 riastrad #include <sys/tty.h> 110 1.299 riastrad #include <sys/uidinfo.h> 111 1.299 riastrad #include <sys/vnode.h> 112 1.299 riastrad #include <sys/wait.h> 113 1.24 cgd 114 1.47 mrg #include <uvm/uvm_extern.h> 115 1.47 mrg 116 1.107 thorpej #ifdef DEBUG_EXIT 117 1.107 thorpej int debug_exit = 0; 118 1.107 thorpej #define DPRINTF(x) if (debug_exit) printf x 119 1.107 thorpej #else 120 1.107 thorpej #define DPRINTF(x) 121 1.107 thorpej #endif 122 1.107 thorpej 123 1.249 christos static int find_stopped_child(struct proc *, idtype_t, id_t, int, 124 1.255 christos struct proc **, struct wrusage *, siginfo_t *); 125 1.249 christos static void proc_free(struct proc *, struct wrusage *); 126 1.177 dsl 127 1.123 christos /* 128 1.226 darran * DTrace SDT provider definitions 129 1.226 darran */ 130 1.245 christos SDT_PROVIDER_DECLARE(proc); 131 1.245 christos SDT_PROBE_DEFINE1(proc, kernel, , exit, "int"); 132 1.245 christos 133 1.226 darran /* 134 1.132 jdolecek * Fill in the appropriate signal information, and signal the parent. 135 1.123 christos */ 136 1.235 chs /* XXX noclone works around a gcc 4.5 bug on arm */ 137 1.235 chs static void __noclone 138 1.162 yamt exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi) 139 1.123 christos { 140 1.123 christos 141 1.163 yamt KSI_INIT(ksi); 142 1.140 pk if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) { 143 1.252 christos if (p->p_xsig) { 144 1.252 christos if (p->p_sflag & PS_COREDUMP) 145 1.140 pk ksi->ksi_code = CLD_DUMPED; 146 1.123 christos else 147 1.140 pk ksi->ksi_code = CLD_KILLED; 148 1.252 christos ksi->ksi_status = p->p_xsig; 149 1.123 christos } else { 150 1.140 pk ksi->ksi_code = CLD_EXITED; 151 1.252 christos ksi->ksi_status = p->p_xexit; 152 1.123 christos } 153 1.252 christos } else { 154 1.252 christos ksi->ksi_code = SI_USER; 155 1.252 christos ksi->ksi_status = p->p_xsig; 156 1.123 christos } 157 1.123 christos /* 158 1.165 ad * We fill those in, even for non-SIGCHLD. 159 1.165 ad * It's safe to access p->p_cred unlocked here. 160 1.123 christos */ 161 1.140 pk ksi->ksi_pid = p->p_pid; 162 1.156 elad ksi->ksi_uid = kauth_cred_geteuid(p->p_cred); 163 1.123 christos /* XXX: is this still valid? */ 164 1.175 dsl ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 165 1.175 dsl ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 166 1.123 christos } 167 1.94 christos 168 1.24 cgd /* 169 1.24 cgd * exit -- 170 1.24 cgd * Death of process. 171 1.24 cgd */ 172 1.31 thorpej int 173 1.195 dsl sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval) 174 1.31 thorpej { 175 1.195 dsl /* { 176 1.89 lukem syscallarg(int) rval; 177 1.195 dsl } */ 178 1.165 ad struct proc *p = l->l_proc; 179 1.24 cgd 180 1.165 ad /* Don't call exit1() multiple times in the same process. */ 181 1.204 ad mutex_enter(p->p_lock); 182 1.165 ad if (p->p_sflag & PS_WEXIT) { 183 1.204 ad mutex_exit(p->p_lock); 184 1.107 thorpej lwp_exit(l); 185 1.165 ad } 186 1.107 thorpej 187 1.165 ad /* exit1() will release the mutex. */ 188 1.253 christos exit1(l, SCARG(uap, rval), 0); 189 1.24 cgd /* NOTREACHED */ 190 1.31 thorpej return (0); 191 1.24 cgd } 192 1.24 cgd 193 1.24 cgd /* 194 1.24 cgd * Exit: deallocate address space and other resources, change proc state 195 1.24 cgd * to zombie, and unlink proc from allproc and parent's lists. Save exit 196 1.24 cgd * status and rusage for wait(). Check for child processes and orphan them. 197 1.165 ad * 198 1.204 ad * Must be called with p->p_lock held. Does not return. 199 1.24 cgd */ 200 1.31 thorpej void 201 1.253 christos exit1(struct lwp *l, int exitcode, int signo) 202 1.24 cgd { 203 1.241 riastrad struct proc *p, *child, *next_child, *old_parent, *new_parent; 204 1.201 ad struct pgrp *pgrp; 205 1.140 pk ksiginfo_t ksi; 206 1.165 ad ksiginfoq_t kq; 207 1.237 rmind int wakeinit; 208 1.24 cgd 209 1.107 thorpej p = l->l_proc; 210 1.107 thorpej 211 1.279 ad /* Verify that we hold no locks other than p->p_lock. */ 212 1.279 ad LOCKDEBUG_BARRIER(p->p_lock, 0); 213 1.285 ad 214 1.285 ad /* XXX Temporary: something is leaking kernel_lock. */ 215 1.285 ad KERNEL_UNLOCK_ALL(l, NULL); 216 1.285 ad 217 1.204 ad KASSERT(mutex_owned(p->p_lock)); 218 1.238 martin KASSERT(p->p_vmspace != NULL); 219 1.165 ad 220 1.237 rmind if (__predict_false(p == initproc)) { 221 1.252 christos panic("init died (signal %d, exit %d)", signo, exitcode); 222 1.213 wrstuden } 223 1.213 wrstuden 224 1.165 ad p->p_sflag |= PS_WEXIT; 225 1.165 ad 226 1.165 ad /* 227 1.165 ad * Force all other LWPs to exit before we do. Only then can we 228 1.165 ad * begin to tear down the rest of the process state. 229 1.165 ad */ 230 1.237 rmind if (p->p_nlwps > 1) { 231 1.165 ad exit_lwps(l); 232 1.237 rmind } 233 1.165 ad 234 1.165 ad ksiginfo_queue_init(&kq); 235 1.165 ad 236 1.165 ad /* 237 1.165 ad * If we have been asked to stop on exit, do so now. 238 1.165 ad */ 239 1.211 ad if (__predict_false(p->p_sflag & PS_STOPEXIT)) { 240 1.295 ad KASSERT(l->l_blcnt == 0); 241 1.165 ad sigclearall(p, &contsigmask, &kq); 242 1.247 pgoyette 243 1.290 ad if (!mutex_tryenter(&proc_lock)) { 244 1.247 pgoyette mutex_exit(p->p_lock); 245 1.290 ad mutex_enter(&proc_lock); 246 1.247 pgoyette mutex_enter(p->p_lock); 247 1.247 pgoyette } 248 1.165 ad p->p_waited = 0; 249 1.247 pgoyette p->p_pptr->p_nstopchild++; 250 1.248 pgoyette p->p_stat = SSTOP; 251 1.290 ad mutex_exit(&proc_lock); 252 1.165 ad lwp_lock(l); 253 1.165 ad p->p_nrlwps--; 254 1.130 atatat l->l_stat = LSSTOP; 255 1.231 rmind lwp_unlock(l); 256 1.204 ad mutex_exit(p->p_lock); 257 1.231 rmind lwp_lock(l); 258 1.278 ad spc_lock(l->l_cpu); 259 1.179 yamt mi_switch(l); 260 1.211 ad mutex_enter(p->p_lock); 261 1.211 ad } 262 1.211 ad 263 1.211 ad /* 264 1.211 ad * Bin any remaining signals and mark the process as dying so it will 265 1.241 riastrad * not be found for, e.g. signals. 266 1.211 ad */ 267 1.211 ad sigfillset(&p->p_sigctx.ps_sigignore); 268 1.211 ad sigclearall(p, NULL, &kq); 269 1.211 ad p->p_stat = SDYING; 270 1.287 thorpej 271 1.287 thorpej /* 272 1.287 thorpej * Perform any required thread cleanup. Do this early so 273 1.287 thorpej * anyone wanting to look us up by our global thread ID 274 1.287 thorpej * will fail to find us. 275 1.287 thorpej * 276 1.287 thorpej * N.B. this will unlock p->p_lock on our behalf. 277 1.287 thorpej */ 278 1.287 thorpej lwp_thread_cleanup(l); 279 1.287 thorpej 280 1.211 ad ksiginfo_queue_drain(&kq); 281 1.165 ad 282 1.193 ad /* Destroy any lwpctl info. */ 283 1.193 ad if (p->p_lwpctl != NULL) 284 1.193 ad lwp_ctl_exit(); 285 1.193 ad 286 1.165 ad /* 287 1.165 ad * Drain all remaining references that procfs, ptrace and others may 288 1.165 ad * have on the process. 289 1.165 ad */ 290 1.191 ad rw_enter(&p->p_reflock, RW_WRITER); 291 1.112 nathanw 292 1.252 christos DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid)); 293 1.107 thorpej 294 1.291 thorpej ptimers_free(p, TIMERS_ALL); 295 1.100 gmcgarry #if defined(__HAVE_RAS) 296 1.190 ad ras_purgeall(); 297 1.100 gmcgarry #endif 298 1.24 cgd 299 1.24 cgd /* 300 1.165 ad * Close open files, release open-file table and free signal 301 1.165 ad * actions. This may block! 302 1.24 cgd */ 303 1.200 ad fd_free(); 304 1.141 pk cwdfree(p->p_cwdi); 305 1.165 ad p->p_cwdi = NULL; 306 1.93 christos doexithooks(p); 307 1.165 ad sigactsfree(p->p_sigacts); 308 1.157 ad 309 1.157 ad /* 310 1.164 ad * Write out accounting data. 311 1.157 ad */ 312 1.154 christos (void)acct_process(l); 313 1.157 ad 314 1.24 cgd #ifdef KTRACE 315 1.145 perry /* 316 1.157 ad * Release trace file. 317 1.24 cgd */ 318 1.165 ad if (p->p_tracep != NULL) { 319 1.185 ad mutex_enter(&ktrace_lock); 320 1.165 ad ktrderef(p); 321 1.185 ad mutex_exit(&ktrace_lock); 322 1.165 ad } 323 1.94 christos #endif 324 1.157 ad 325 1.252 christos p->p_xexit = exitcode; 326 1.252 christos p->p_xsig = signo; 327 1.252 christos 328 1.24 cgd /* 329 1.99 manu * If emulation has process exit hook, call it now. 330 1.158 manu * Set the exit status now so that the exit hook has 331 1.158 manu * an opportunity to tweak it (COMPAT_LINUX requires 332 1.158 manu * this for thread group emulation) 333 1.99 manu */ 334 1.99 manu if (p->p_emul->e_proc_exit) 335 1.99 manu (*p->p_emul->e_proc_exit)(p); 336 1.99 manu 337 1.160 thorpej /* 338 1.140 pk * Free the VM resources we're still holding on to. 339 1.140 pk * We must do this from a valid thread because doing 340 1.140 pk * so may block. This frees vmspace, which we don't 341 1.140 pk * need anymore. The only remaining lwp is the one 342 1.140 pk * we run at this moment, nothing runs in userland 343 1.140 pk * anymore. 344 1.140 pk */ 345 1.271 christos ruspace(p); /* Update our vm resource use */ 346 1.140 pk uvm_proc_exit(p); 347 1.140 pk 348 1.140 pk /* 349 1.165 ad * Stop profiling. 350 1.140 pk */ 351 1.211 ad if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) { 352 1.165 ad mutex_spin_enter(&p->p_stmutex); 353 1.165 ad stopprofclock(p); 354 1.165 ad mutex_spin_exit(&p->p_stmutex); 355 1.165 ad } 356 1.140 pk 357 1.140 pk /* 358 1.214 yamt * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we 359 1.165 ad * wake up the parent early to avoid deadlock. We can do this once 360 1.165 ad * the VM resources are released. 361 1.140 pk */ 362 1.290 ad mutex_enter(&proc_lock); 363 1.211 ad if (p->p_lflag & PL_PPWAIT) { 364 1.276 kamil lwp_t *lp; 365 1.276 kamil 366 1.240 christos l->l_lwpctl = NULL; /* was on loan from blocked parent */ 367 1.240 christos p->p_lflag &= ~PL_PPWAIT; 368 1.276 kamil 369 1.276 kamil lp = p->p_vforklwp; 370 1.276 kamil p->p_vforklwp = NULL; 371 1.276 kamil lp->l_vforkwaiting = false; 372 1.276 kamil cv_broadcast(&lp->l_waitcv); 373 1.165 ad } 374 1.165 ad 375 1.165 ad if (SESS_LEADER(p)) { 376 1.165 ad struct vnode *vprele = NULL, *vprevoke = NULL; 377 1.165 ad struct session *sp = p->p_session; 378 1.165 ad struct tty *tp; 379 1.165 ad 380 1.165 ad if (sp->s_ttyvp) { 381 1.165 ad /* 382 1.165 ad * Controlling process. 383 1.165 ad * Signal foreground pgrp, 384 1.165 ad * drain controlling terminal 385 1.165 ad * and revoke access to controlling terminal. 386 1.165 ad */ 387 1.165 ad tp = sp->s_ttyp; 388 1.192 ad mutex_spin_enter(&tty_lock); 389 1.165 ad if (tp->t_session == sp) { 390 1.165 ad /* we can't guarantee the revoke will do this */ 391 1.201 ad pgrp = tp->t_pgrp; 392 1.165 ad tp->t_pgrp = NULL; 393 1.165 ad tp->t_session = NULL; 394 1.192 ad mutex_spin_exit(&tty_lock); 395 1.201 ad if (pgrp != NULL) { 396 1.201 ad pgsignal(pgrp, SIGHUP, 1); 397 1.201 ad } 398 1.290 ad mutex_exit(&proc_lock); 399 1.165 ad (void) ttywait(tp); 400 1.290 ad mutex_enter(&proc_lock); 401 1.165 ad 402 1.198 ad /* The tty could have been revoked. */ 403 1.165 ad vprevoke = sp->s_ttyvp; 404 1.192 ad } else 405 1.192 ad mutex_spin_exit(&tty_lock); 406 1.165 ad vprele = sp->s_ttyvp; 407 1.165 ad sp->s_ttyvp = NULL; 408 1.165 ad /* 409 1.165 ad * s_ttyp is not zero'd; we use this to indicate 410 1.165 ad * that the session once had a controlling terminal. 411 1.165 ad * (for logging and informational purposes) 412 1.165 ad */ 413 1.165 ad } 414 1.165 ad sp->s_leader = NULL; 415 1.140 pk 416 1.165 ad if (vprevoke != NULL || vprele != NULL) { 417 1.198 ad if (vprevoke != NULL) { 418 1.220 rmind /* Releases proc_lock. */ 419 1.220 rmind proc_sessrele(sp); 420 1.165 ad VOP_REVOKE(vprevoke, REVOKEALL); 421 1.198 ad } else 422 1.290 ad mutex_exit(&proc_lock); 423 1.165 ad if (vprele != NULL) 424 1.165 ad vrele(vprele); 425 1.290 ad mutex_enter(&proc_lock); 426 1.165 ad } 427 1.165 ad } 428 1.165 ad fixjobc(p, p->p_pgrp, 0); 429 1.140 pk 430 1.274 hannken /* Release fstrans private data. */ 431 1.274 hannken fstrans_lwp_dtor(l); 432 1.274 hannken 433 1.140 pk /* 434 1.171 ad * Finalize the last LWP's specificdata, as well as the 435 1.171 ad * specificdata for the proc itself. 436 1.171 ad */ 437 1.171 ad lwp_finispecific(l); 438 1.171 ad proc_finispecific(p); 439 1.171 ad 440 1.171 ad /* 441 1.128 dsl * Reset p_opptr pointer of all former children which got 442 1.128 dsl * traced by another process and were reparented. We reset 443 1.128 dsl * it to NULL here; the trace detach code then reparents 444 1.128 dsl * the child to initproc. We only check allproc list, since 445 1.128 dsl * eventual former children on zombproc list won't reference 446 1.128 dsl * p_opptr anymore. 447 1.128 dsl */ 448 1.208 ad if (__predict_false(p->p_slflag & PSL_CHTRACED)) { 449 1.241 riastrad struct proc *q; 450 1.143 yamt PROCLIST_FOREACH(q, &allproc) { 451 1.128 dsl if (q->p_opptr == p) 452 1.128 dsl q->p_opptr = NULL; 453 1.128 dsl } 454 1.262 christos PROCLIST_FOREACH(q, &zombproc) { 455 1.262 christos if (q->p_opptr == p) 456 1.262 christos q->p_opptr = NULL; 457 1.262 christos } 458 1.128 dsl } 459 1.128 dsl 460 1.128 dsl /* 461 1.72 thorpej * Give orphaned children to init(8). 462 1.72 thorpej */ 463 1.241 riastrad child = LIST_FIRST(&p->p_children); 464 1.241 riastrad wakeinit = (child != NULL); 465 1.241 riastrad for (; child != NULL; child = next_child) { 466 1.241 riastrad next_child = LIST_NEXT(child, p_sibling); 467 1.104 jdolecek 468 1.24 cgd /* 469 1.104 jdolecek * Traced processes are killed since their existence 470 1.104 jdolecek * means someone is screwing up. Since we reset the 471 1.104 jdolecek * trace flags, the logic in sys_wait4() would not be 472 1.104 jdolecek * triggered to reparent the process to its 473 1.106 jdolecek * original parent, so we must do this here. 474 1.24 cgd */ 475 1.241 riastrad if (__predict_false(child->p_slflag & PSL_TRACED)) { 476 1.204 ad mutex_enter(p->p_lock); 477 1.241 riastrad child->p_slflag &= 478 1.269 kamil ~(PSL_TRACED|PSL_SYSCALL); 479 1.204 ad mutex_exit(p->p_lock); 480 1.241 riastrad if (child->p_opptr != child->p_pptr) { 481 1.241 riastrad struct proc *t = child->p_opptr; 482 1.241 riastrad proc_reparent(child, t ? t : initproc); 483 1.241 riastrad child->p_opptr = NULL; 484 1.105 jdolecek } else 485 1.241 riastrad proc_reparent(child, initproc); 486 1.241 riastrad killproc(child, "orphaned traced process"); 487 1.165 ad } else 488 1.241 riastrad proc_reparent(child, initproc); 489 1.24 cgd } 490 1.115 dsl 491 1.115 dsl /* 492 1.165 ad * Move proc from allproc to zombproc, it's now nearly ready to be 493 1.165 ad * collected by parent. 494 1.115 dsl */ 495 1.165 ad LIST_REMOVE(l, l_list); 496 1.115 dsl LIST_REMOVE(p, p_list); 497 1.115 dsl LIST_INSERT_HEAD(&zombproc, p, p_list); 498 1.133 jdolecek 499 1.165 ad /* 500 1.165 ad * Mark the process as dead. We must do this before we signal 501 1.165 ad * the parent. 502 1.165 ad */ 503 1.165 ad p->p_stat = SDEAD; 504 1.133 jdolecek 505 1.292 thorpej /* 506 1.292 thorpej * Let anyone watching this DTrace probe know what we're 507 1.292 thorpej * on our way out. 508 1.292 thorpej */ 509 1.292 thorpej SDT_PROBE(proc, kernel, , exit, 510 1.292 thorpej ((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED : 511 1.292 thorpej (p->p_xsig ? CLD_KILLED : CLD_EXITED)), 512 1.292 thorpej 0,0,0,0); 513 1.292 thorpej 514 1.133 jdolecek /* Put in front of parent's sibling list for parent to collect it */ 515 1.241 riastrad old_parent = p->p_pptr; 516 1.241 riastrad old_parent->p_nstopchild++; 517 1.241 riastrad if (LIST_FIRST(&old_parent->p_children) != p) { 518 1.133 jdolecek /* Put child where it can be found quickly */ 519 1.133 jdolecek LIST_REMOVE(p, p_sibling); 520 1.241 riastrad LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling); 521 1.133 jdolecek } 522 1.133 jdolecek 523 1.59 christos /* 524 1.59 christos * Notify parent that we're gone. If parent has the P_NOCLDWAIT 525 1.59 christos * flag set, notify init instead (and hope it will handle 526 1.59 christos * this situation). 527 1.59 christos */ 528 1.241 riastrad if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) { 529 1.59 christos proc_reparent(p, initproc); 530 1.165 ad wakeinit = 1; 531 1.133 jdolecek 532 1.59 christos /* 533 1.59 christos * If this was the last child of our parent, notify 534 1.59 christos * parent, so in case he was wait(2)ing, he will 535 1.59 christos * continue. 536 1.59 christos */ 537 1.241 riastrad if (LIST_FIRST(&old_parent->p_children) == NULL) 538 1.241 riastrad cv_broadcast(&old_parent->p_waitcv); 539 1.59 christos } 540 1.24 cgd 541 1.140 pk /* Reload parent pointer, since p may have been reparented above */ 542 1.241 riastrad new_parent = p->p_pptr; 543 1.140 pk 544 1.269 kamil if (__predict_false(p->p_exitsig != 0)) { 545 1.241 riastrad exit_psignal(p, new_parent, &ksi); 546 1.241 riastrad kpsignal(new_parent, &ksi, NULL); 547 1.140 pk } 548 1.140 pk 549 1.175 dsl /* Calculate the final rusage info. */ 550 1.175 dsl calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, 551 1.175 dsl NULL, NULL); 552 1.140 pk 553 1.187 ad callout_destroy(&l->l_timeout_ch); 554 1.183 ad 555 1.165 ad /* 556 1.234 matt * Release any PCU resources before becoming a zombie. 557 1.234 matt */ 558 1.234 matt pcu_discard_all(l); 559 1.234 matt 560 1.292 thorpej /* 561 1.292 thorpej * Notify other processes tracking us with a knote that 562 1.292 thorpej * we're exiting. 563 1.292 thorpej * 564 1.292 thorpej * N.B. we do this here because the process is now SDEAD, 565 1.292 thorpej * and thus cannot have any more knotes attached. Also, 566 1.292 thorpej * knote_proc_exit() expects that p->p_lock is already 567 1.292 thorpej * held (and will assert so). 568 1.292 thorpej */ 569 1.298 ad mutex_enter(p->p_lock); 570 1.292 thorpej if (!SLIST_EMPTY(&p->p_klist)) { 571 1.292 thorpej knote_proc_exit(p); 572 1.292 thorpej } 573 1.292 thorpej 574 1.289 thorpej /* Free the LWP ID */ 575 1.289 thorpej proc_free_lwpid(p, l->l_lid); 576 1.287 thorpej lwp_drainrefs(l); 577 1.165 ad lwp_lock(l); 578 1.165 ad l->l_prflag &= ~LPR_DETACHED; 579 1.165 ad l->l_stat = LSZOMB; 580 1.165 ad lwp_unlock(l); 581 1.165 ad KASSERT(curlwp == l); 582 1.165 ad KASSERT(p->p_nrlwps == 1); 583 1.165 ad KASSERT(p->p_nlwps == 1); 584 1.165 ad p->p_stat = SZOMB; 585 1.165 ad p->p_nrlwps--; 586 1.165 ad p->p_nzlwps++; 587 1.165 ad p->p_ndlwps = 0; 588 1.204 ad mutex_exit(p->p_lock); 589 1.140 pk 590 1.165 ad /* 591 1.165 ad * Signal the parent to collect us, and drop the proclist lock. 592 1.191 ad * Drop debugger/procfs lock; no new references can be gained. 593 1.165 ad */ 594 1.298 ad rw_exit(&p->p_reflock); 595 1.184 ad cv_broadcast(&p->p_pptr->p_waitcv); 596 1.290 ad mutex_exit(&proc_lock); 597 1.298 ad if (wakeinit) 598 1.298 ad cv_broadcast(&initproc->p_waitcv); 599 1.140 pk 600 1.165 ad /* 601 1.165 ad * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 602 1.165 ad */ 603 1.157 ad 604 1.165 ad /* 605 1.165 ad * Give machine-dependent code a chance to free any MD LWP 606 1.165 ad * resources. This must be done before uvm_lwp_exit(), in 607 1.165 ad * case these resources are in the PCB. 608 1.165 ad */ 609 1.165 ad cpu_lwp_free(l, 1); 610 1.238 martin 611 1.279 ad /* Switch away into oblivion. */ 612 1.279 ad lwp_lock(l); 613 1.279 ad spc_lock(l->l_cpu); 614 1.279 ad mi_switch(l); 615 1.279 ad panic("exit1"); 616 1.107 thorpej } 617 1.107 thorpej 618 1.107 thorpej void 619 1.107 thorpej exit_lwps(struct lwp *l) 620 1.107 thorpej { 621 1.242 rmind proc_t *p = l->l_proc; 622 1.242 rmind lwp_t *l2; 623 1.165 ad 624 1.242 rmind retry: 625 1.242 rmind KASSERT(mutex_owned(p->p_lock)); 626 1.107 thorpej 627 1.107 thorpej /* 628 1.107 thorpej * Interrupt LWPs in interruptable sleep, unsuspend suspended 629 1.165 ad * LWPs and then wait for everyone else to finish. 630 1.107 thorpej */ 631 1.107 thorpej LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 632 1.165 ad if (l2 == l) 633 1.165 ad continue; 634 1.165 ad lwp_lock(l2); 635 1.167 pavel l2->l_flag |= LW_WEXIT; 636 1.296 ad lwp_need_userret(l2); 637 1.167 pavel if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) || 638 1.118 fvdl l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) { 639 1.277 kamil l2->l_flag &= ~LW_DBGSUSPEND; 640 1.165 ad /* setrunnable() will release the lock. */ 641 1.107 thorpej setrunnable(l2); 642 1.165 ad continue; 643 1.107 thorpej } 644 1.165 ad lwp_unlock(l2); 645 1.107 thorpej } 646 1.242 rmind 647 1.242 rmind /* 648 1.242 rmind * Wait for every LWP to exit. Note: LWPs can get suspended/slept 649 1.242 rmind * behind us or there may even be new LWPs created. Therefore, a 650 1.242 rmind * full retry is required on error. 651 1.242 rmind */ 652 1.107 thorpej while (p->p_nlwps > 1) { 653 1.242 rmind if (lwp_wait(l, 0, NULL, true)) { 654 1.150 yamt goto retry; 655 1.150 yamt } 656 1.145 perry } 657 1.107 thorpej 658 1.181 yamt KASSERT(p->p_nlwps == 1); 659 1.24 cgd } 660 1.24 cgd 661 1.259 skrll int 662 1.249 christos do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options, 663 1.249 christos struct wrusage *wru, siginfo_t *si) 664 1.177 dsl { 665 1.225 rmind proc_t *child; 666 1.225 rmind int error; 667 1.177 dsl 668 1.260 skrll 669 1.249 christos if (wru != NULL) 670 1.249 christos memset(wru, 0, sizeof(*wru)); 671 1.249 christos if (si != NULL) 672 1.249 christos memset(si, 0, sizeof(*si)); 673 1.249 christos 674 1.290 ad mutex_enter(&proc_lock); 675 1.255 christos error = find_stopped_child(curproc, idtype, id, options, &child, 676 1.249 christos wru, si); 677 1.177 dsl if (child == NULL) { 678 1.290 ad mutex_exit(&proc_lock); 679 1.177 dsl *pid = 0; 680 1.273 maxv *status = 0; 681 1.177 dsl return error; 682 1.177 dsl } 683 1.177 dsl *pid = child->p_pid; 684 1.177 dsl 685 1.177 dsl if (child->p_stat == SZOMB) { 686 1.255 christos /* Child is exiting */ 687 1.255 christos *status = P_WAITSTATUS(child); 688 1.203 ad /* proc_free() will release the proc_lock. */ 689 1.224 rmind if (options & WNOWAIT) { 690 1.290 ad mutex_exit(&proc_lock); 691 1.224 rmind } else { 692 1.249 christos proc_free(child, wru); 693 1.177 dsl } 694 1.177 dsl } else { 695 1.261 christos /* Don't mark SIGCONT if we are being stopped */ 696 1.261 christos *status = (child->p_xsig == SIGCONT && child->p_stat != SSTOP) ? 697 1.261 christos W_CONTCODE() : W_STOPCODE(child->p_xsig); 698 1.290 ad mutex_exit(&proc_lock); 699 1.177 dsl } 700 1.177 dsl return 0; 701 1.177 dsl } 702 1.177 dsl 703 1.177 dsl int 704 1.249 christos do_sys_wait(int *pid, int *status, int options, struct rusage *ru) 705 1.249 christos { 706 1.249 christos idtype_t idtype; 707 1.249 christos id_t id; 708 1.249 christos int ret; 709 1.249 christos struct wrusage wru; 710 1.249 christos 711 1.249 christos /* 712 1.249 christos * Translate the special pid values into the (idtype, pid) 713 1.249 christos * pair for wait6. The WAIT_MYPGRP case is handled by 714 1.249 christos * find_stopped_child() on its own. 715 1.249 christos */ 716 1.249 christos if (*pid == WAIT_ANY) { 717 1.249 christos idtype = P_ALL; 718 1.249 christos id = 0; 719 1.249 christos } else if (*pid < 0) { 720 1.249 christos idtype = P_PGID; 721 1.249 christos id = (id_t)-*pid; 722 1.249 christos } else { 723 1.249 christos idtype = P_PID; 724 1.249 christos id = (id_t)*pid; 725 1.249 christos } 726 1.249 christos options |= WEXITED | WTRAPPED; 727 1.249 christos ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL, 728 1.249 christos NULL); 729 1.249 christos if (ru) 730 1.249 christos *ru = wru.wru_self; 731 1.249 christos return ret; 732 1.249 christos } 733 1.249 christos 734 1.249 christos int 735 1.225 rmind sys___wait450(struct lwp *l, const struct sys___wait450_args *uap, 736 1.225 rmind register_t *retval) 737 1.31 thorpej { 738 1.195 dsl /* { 739 1.89 lukem syscallarg(int) pid; 740 1.89 lukem syscallarg(int *) status; 741 1.89 lukem syscallarg(int) options; 742 1.89 lukem syscallarg(struct rusage *) rusage; 743 1.195 dsl } */ 744 1.225 rmind int error, status, pid = SCARG(uap, pid); 745 1.225 rmind struct rusage ru; 746 1.24 cgd 747 1.225 rmind error = do_sys_wait(&pid, &status, SCARG(uap, options), 748 1.225 rmind SCARG(uap, rusage) != NULL ? &ru : NULL); 749 1.107 thorpej 750 1.195 dsl retval[0] = pid; 751 1.225 rmind if (pid == 0) { 752 1.109 dsl return error; 753 1.225 rmind } 754 1.225 rmind if (SCARG(uap, status)) { 755 1.225 rmind error = copyout(&status, SCARG(uap, status), sizeof(status)); 756 1.225 rmind } 757 1.225 rmind if (SCARG(uap, rusage) && error == 0) { 758 1.177 dsl error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 759 1.225 rmind } 760 1.177 dsl return error; 761 1.109 dsl } 762 1.109 dsl 763 1.249 christos int 764 1.249 christos sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval) 765 1.249 christos { 766 1.249 christos /* { 767 1.249 christos syscallarg(idtype_t) idtype; 768 1.249 christos syscallarg(id_t) id; 769 1.249 christos syscallarg(int *) status; 770 1.249 christos syscallarg(int) options; 771 1.249 christos syscallarg(struct wrusage *) wru; 772 1.249 christos syscallarg(siginfo_t *) si; 773 1.249 christos } */ 774 1.249 christos struct wrusage wru, *wrup; 775 1.249 christos siginfo_t si, *sip; 776 1.249 christos idtype_t idtype; 777 1.249 christos int pid; 778 1.249 christos id_t id; 779 1.249 christos int error, status; 780 1.249 christos 781 1.249 christos idtype = SCARG(uap, idtype); 782 1.249 christos id = SCARG(uap, id); 783 1.249 christos 784 1.249 christos if (SCARG(uap, wru) != NULL) 785 1.249 christos wrup = &wru; 786 1.249 christos else 787 1.249 christos wrup = NULL; 788 1.249 christos 789 1.249 christos if (SCARG(uap, info) != NULL) 790 1.249 christos sip = &si; 791 1.249 christos else 792 1.249 christos sip = NULL; 793 1.249 christos 794 1.249 christos /* 795 1.249 christos * We expect all callers of wait6() to know about WEXITED and 796 1.249 christos * WTRAPPED. 797 1.249 christos */ 798 1.249 christos error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options), 799 1.249 christos wrup, sip); 800 1.249 christos 801 1.257 christos retval[0] = pid; /* tell userland who it was */ 802 1.257 christos 803 1.257 christos #if 0 804 1.260 skrll /* 805 1.257 christos * should we copyout if there was no process, hence no useful data? 806 1.293 msaitoh * We don't for an old style wait4() (etc) but I believe 807 1.257 christos * FreeBSD does for wait6(), so a tossup... Go with FreeBSD for now. 808 1.257 christos */ 809 1.257 christos if (pid == 0) 810 1.257 christos return error; 811 1.257 christos #endif 812 1.257 christos 813 1.249 christos if (SCARG(uap, status) != NULL && error == 0) 814 1.249 christos error = copyout(&status, SCARG(uap, status), sizeof(status)); 815 1.249 christos if (SCARG(uap, wru) != NULL && error == 0) 816 1.249 christos error = copyout(&wru, SCARG(uap, wru), sizeof(wru)); 817 1.249 christos if (SCARG(uap, info) != NULL && error == 0) 818 1.249 christos error = copyout(&si, SCARG(uap, info), sizeof(si)); 819 1.249 christos return error; 820 1.249 christos } 821 1.249 christos 822 1.249 christos 823 1.251 christos /* 824 1.251 christos * Find a process that matches the provided criteria, and fill siginfo 825 1.251 christos * and resources if found. 826 1.251 christos * Returns: 827 1.251 christos * -1: Not found, abort early 828 1.251 christos * 0: Not matched 829 1.251 christos * 1: Matched, there might be more matches 830 1.251 christos * 2: This is the only match 831 1.251 christos */ 832 1.249 christos static int 833 1.265 kre match_process(const struct proc *pp, struct proc **q, idtype_t idtype, id_t id, 834 1.249 christos int options, struct wrusage *wrusage, siginfo_t *siginfo) 835 1.249 christos { 836 1.249 christos struct rusage *rup; 837 1.249 christos struct proc *p = *q; 838 1.251 christos int rv = 1; 839 1.249 christos 840 1.249 christos switch (idtype) { 841 1.249 christos case P_ALL: 842 1.297 ad mutex_enter(p->p_lock); 843 1.249 christos break; 844 1.249 christos case P_PID: 845 1.249 christos if (p->p_pid != (pid_t)id) { 846 1.249 christos p = *q = proc_find_raw((pid_t)id); 847 1.249 christos if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) { 848 1.249 christos *q = NULL; 849 1.249 christos return -1; 850 1.249 christos } 851 1.249 christos } 852 1.297 ad mutex_enter(p->p_lock); 853 1.251 christos rv++; 854 1.249 christos break; 855 1.249 christos case P_PGID: 856 1.249 christos if (p->p_pgid != (pid_t)id) 857 1.297 ad return 0; 858 1.297 ad mutex_enter(p->p_lock); 859 1.249 christos break; 860 1.249 christos case P_SID: 861 1.249 christos if (p->p_session->s_sid != (pid_t)id) 862 1.297 ad return 0; 863 1.297 ad mutex_enter(p->p_lock); 864 1.249 christos break; 865 1.249 christos case P_UID: 866 1.297 ad mutex_enter(p->p_lock); 867 1.297 ad if (kauth_cred_geteuid(p->p_cred) != (uid_t)id) { 868 1.297 ad mutex_exit(p->p_lock); 869 1.297 ad return 0; 870 1.297 ad } 871 1.249 christos break; 872 1.249 christos case P_GID: 873 1.297 ad mutex_enter(p->p_lock); 874 1.297 ad if (kauth_cred_getegid(p->p_cred) != (gid_t)id) { 875 1.297 ad mutex_exit(p->p_lock); 876 1.297 ad return 0; 877 1.297 ad } 878 1.249 christos break; 879 1.249 christos case P_CID: 880 1.249 christos case P_PSETID: 881 1.249 christos case P_CPUID: 882 1.249 christos /* XXX: Implement me */ 883 1.249 christos default: 884 1.297 ad return 0; 885 1.297 ad } 886 1.297 ad 887 1.297 ad if ((options & WEXITED) == 0 && p->p_stat == SZOMB) { 888 1.249 christos mutex_exit(p->p_lock); 889 1.249 christos return 0; 890 1.249 christos } 891 1.249 christos 892 1.249 christos if (siginfo != NULL) { 893 1.249 christos siginfo->si_errno = 0; 894 1.249 christos 895 1.249 christos /* 896 1.249 christos * SUSv4 requires that the si_signo value is always 897 1.249 christos * SIGCHLD. Obey it despite the rfork(2) interface 898 1.249 christos * allows to request other signal for child exit 899 1.249 christos * notification. 900 1.249 christos */ 901 1.249 christos siginfo->si_signo = SIGCHLD; 902 1.249 christos 903 1.249 christos /* 904 1.249 christos * This is still a rough estimate. We will fix the 905 1.249 christos * cases TRAPPED, STOPPED, and CONTINUED later. 906 1.249 christos */ 907 1.252 christos if (p->p_sflag & PS_COREDUMP) { 908 1.249 christos siginfo->si_code = CLD_DUMPED; 909 1.252 christos siginfo->si_status = p->p_xsig; 910 1.252 christos } else if (p->p_xsig) { 911 1.249 christos siginfo->si_code = CLD_KILLED; 912 1.252 christos siginfo->si_status = p->p_xsig; 913 1.249 christos } else { 914 1.249 christos siginfo->si_code = CLD_EXITED; 915 1.252 christos siginfo->si_status = p->p_xexit; 916 1.249 christos } 917 1.249 christos 918 1.249 christos siginfo->si_pid = p->p_pid; 919 1.250 christos siginfo->si_uid = kauth_cred_geteuid(p->p_cred); 920 1.250 christos siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec; 921 1.250 christos siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec; 922 1.249 christos } 923 1.249 christos 924 1.249 christos /* 925 1.249 christos * There should be no reason to limit resources usage info to 926 1.249 christos * exited processes only. A snapshot about any resources used 927 1.249 christos * by a stopped process may be exactly what is needed. 928 1.249 christos */ 929 1.249 christos if (wrusage != NULL) { 930 1.249 christos rup = &wrusage->wru_self; 931 1.249 christos *rup = p->p_stats->p_ru; 932 1.249 christos calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL); 933 1.249 christos 934 1.249 christos rup = &wrusage->wru_children; 935 1.249 christos *rup = p->p_stats->p_cru; 936 1.249 christos calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL); 937 1.249 christos } 938 1.249 christos 939 1.249 christos mutex_exit(p->p_lock); 940 1.251 christos return rv; 941 1.249 christos } 942 1.249 christos 943 1.109 dsl /* 944 1.265 kre * Determine if there are existing processes being debugged 945 1.265 kre * that used to be (and sometime later will be again) children 946 1.265 kre * of a specific parent (while matching wait criteria) 947 1.265 kre */ 948 1.265 kre static bool 949 1.265 kre debugged_child_exists(idtype_t idtype, id_t id, int options, siginfo_t *si, 950 1.265 kre const struct proc *parent) 951 1.265 kre { 952 1.265 kre struct proc *pp; 953 1.265 kre 954 1.265 kre /* 955 1.265 kre * If we are searching for a specific pid, we can optimise a little 956 1.265 kre */ 957 1.265 kre if (idtype == P_PID) { 958 1.265 kre /* 959 1.265 kre * Check the specific process to see if its real parent is us 960 1.265 kre */ 961 1.265 kre pp = proc_find_raw((pid_t)id); 962 1.265 kre if (pp != NULL && pp->p_stat != SIDL && pp->p_opptr == parent) { 963 1.265 kre /* 964 1.265 kre * using P_ALL here avoids match_process() doing the 965 1.265 kre * same work that we just did, but incorrectly for 966 1.265 kre * this scenario. 967 1.265 kre */ 968 1.265 kre if (match_process(parent, &pp, P_ALL, id, options, 969 1.265 kre NULL, si)) 970 1.265 kre return true; 971 1.265 kre } 972 1.265 kre return false; 973 1.265 kre } 974 1.265 kre 975 1.265 kre /* 976 1.265 kre * For the hard cases, just look everywhere to see if some 977 1.265 kre * stolen (reparented) process is really our lost child. 978 1.265 kre * Then check if that process could satisfy the wait conditions. 979 1.265 kre */ 980 1.265 kre 981 1.265 kre /* 982 1.265 kre * XXX inefficient, but hopefully fairly rare. 983 1.265 kre * XXX should really use a list of reparented processes. 984 1.265 kre */ 985 1.265 kre PROCLIST_FOREACH(pp, &allproc) { 986 1.265 kre if (pp->p_stat == SIDL) /* XXX impossible ?? */ 987 1.265 kre continue; 988 1.265 kre if (pp->p_opptr == parent && 989 1.265 kre match_process(parent, &pp, idtype, id, options, NULL, si)) 990 1.265 kre return true; 991 1.265 kre } 992 1.265 kre PROCLIST_FOREACH(pp, &zombproc) { 993 1.265 kre if (pp->p_stat == SIDL) /* XXX impossible ?? */ 994 1.265 kre continue; 995 1.265 kre if (pp->p_opptr == parent && 996 1.265 kre match_process(parent, &pp, idtype, id, options, NULL, si)) 997 1.265 kre return true; 998 1.265 kre } 999 1.265 kre 1000 1.265 kre return false; 1001 1.265 kre } 1002 1.265 kre 1003 1.265 kre /* 1004 1.109 dsl * Scan list of child processes for a child process that has stopped or 1005 1.109 dsl * exited. Used by sys_wait4 and 'compat' equivalents. 1006 1.165 ad * 1007 1.203 ad * Must be called with the proc_lock held, and may release while waiting. 1008 1.109 dsl */ 1009 1.177 dsl static int 1010 1.249 christos find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options, 1011 1.255 christos struct proc **child_p, struct wrusage *wru, siginfo_t *si) 1012 1.109 dsl { 1013 1.165 ad struct proc *child, *dead; 1014 1.265 kre int error; 1015 1.68 thorpej 1016 1.290 ad KASSERT(mutex_owned(&proc_lock)); 1017 1.165 ad 1018 1.266 christos if (options & ~WALLOPTS) { 1019 1.177 dsl *child_p = NULL; 1020 1.300 riastrad return SET_ERROR(EINVAL); 1021 1.177 dsl } 1022 1.177 dsl 1023 1.266 christos if ((options & WSELECTOPTS) == 0) { 1024 1.249 christos /* 1025 1.249 christos * We will be unable to find any matching processes, 1026 1.249 christos * because there are no known events to look for. 1027 1.249 christos * Prefer to return error instead of blocking 1028 1.249 christos * indefinitely. 1029 1.249 christos */ 1030 1.249 christos *child_p = NULL; 1031 1.300 riastrad return SET_ERROR(EINVAL); 1032 1.249 christos } 1033 1.249 christos 1034 1.249 christos if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1035 1.249 christos id = (id_t)parent->p_pgid; 1036 1.249 christos idtype = P_PGID; 1037 1.249 christos } 1038 1.177 dsl 1039 1.120 yamt for (;;) { 1040 1.128 dsl error = ECHILD; 1041 1.165 ad dead = NULL; 1042 1.165 ad 1043 1.109 dsl LIST_FOREACH(child, &parent->p_children, p_sibling) { 1044 1.249 christos int rv = match_process(parent, &child, idtype, id, 1045 1.249 christos options, wru, si); 1046 1.249 christos if (rv == -1) 1047 1.249 christos break; 1048 1.249 christos if (rv == 0) 1049 1.165 ad continue; 1050 1.165 ad 1051 1.109 dsl /* 1052 1.109 dsl * Wait for processes with p_exitsig != SIGCHLD 1053 1.109 dsl * processes only if WALTSIG is set; wait for 1054 1.109 dsl * processes with p_exitsig == SIGCHLD only 1055 1.109 dsl * if WALTSIG is clear. 1056 1.109 dsl */ 1057 1.109 dsl if (((options & WALLSIG) == 0) && 1058 1.109 dsl (options & WALTSIG ? child->p_exitsig == SIGCHLD 1059 1.128 dsl : P_EXITSIG(child) != SIGCHLD)){ 1060 1.251 christos if (rv == 2) { 1061 1.251 christos child = NULL; 1062 1.251 christos break; 1063 1.251 christos } 1064 1.109 dsl continue; 1065 1.128 dsl } 1066 1.109 dsl 1067 1.128 dsl error = 0; 1068 1.165 ad if ((options & WNOZOMBIE) == 0) { 1069 1.165 ad if (child->p_stat == SZOMB) 1070 1.165 ad break; 1071 1.165 ad if (child->p_stat == SDEAD) { 1072 1.165 ad /* 1073 1.165 ad * We may occasionally arrive here 1074 1.165 ad * after receiving a signal, but 1075 1.228 pooka * immediately before the child 1076 1.165 ad * process is zombified. The wait 1077 1.165 ad * will be short, so avoid returning 1078 1.165 ad * to userspace. 1079 1.165 ad */ 1080 1.165 ad dead = child; 1081 1.165 ad } 1082 1.165 ad } 1083 1.109 dsl 1084 1.250 christos if ((options & WCONTINUED) != 0 && 1085 1.258 christos child->p_xsig == SIGCONT && 1086 1.258 christos (child->p_sflag & PS_CONTINUED)) { 1087 1.250 christos if ((options & WNOWAIT) == 0) { 1088 1.258 christos child->p_sflag &= ~PS_CONTINUED; 1089 1.250 christos child->p_waited = 1; 1090 1.250 christos parent->p_nstopchild--; 1091 1.250 christos } 1092 1.250 christos if (si) { 1093 1.252 christos si->si_status = child->p_xsig; 1094 1.250 christos si->si_code = CLD_CONTINUED; 1095 1.250 christos } 1096 1.250 christos break; 1097 1.250 christos } 1098 1.250 christos 1099 1.250 christos if ((options & (WTRAPPED|WSTOPPED)) != 0 && 1100 1.249 christos child->p_stat == SSTOP && 1101 1.165 ad child->p_waited == 0 && 1102 1.250 christos ((child->p_slflag & PSL_TRACED) || 1103 1.250 christos options & (WUNTRACED|WSTOPPED))) { 1104 1.128 dsl if ((options & WNOWAIT) == 0) { 1105 1.165 ad child->p_waited = 1; 1106 1.128 dsl parent->p_nstopchild--; 1107 1.128 dsl } 1108 1.249 christos if (si) { 1109 1.252 christos si->si_status = child->p_xsig; 1110 1.260 skrll si->si_code = 1111 1.250 christos (child->p_slflag & PSL_TRACED) ? 1112 1.250 christos CLD_TRAPPED : CLD_STOPPED; 1113 1.249 christos } 1114 1.128 dsl break; 1115 1.128 dsl } 1116 1.251 christos if (parent->p_nstopchild == 0 || rv == 2) { 1117 1.128 dsl child = NULL; 1118 1.128 dsl break; 1119 1.24 cgd } 1120 1.109 dsl } 1121 1.165 ad 1122 1.265 kre /* 1123 1.265 kre * If we found nothing, but we are the bereaved parent 1124 1.265 kre * of a stolen child, look and see if that child (or 1125 1.265 kre * one of them) meets our search criteria. If so, then 1126 1.265 kre * we cannot succeed, but we can hang (wait...), 1127 1.265 kre * or if WNOHANG, return 0 instead of ECHILD 1128 1.265 kre */ 1129 1.265 kre if (child == NULL && error == ECHILD && 1130 1.265 kre (parent->p_slflag & PSL_CHTRACED) && 1131 1.265 kre debugged_child_exists(idtype, id, options, si, parent)) 1132 1.265 kre error = 0; 1133 1.265 kre 1134 1.265 kre if (child != NULL || error != 0 || 1135 1.265 kre ((options & WNOHANG) != 0 && dead == NULL)) { 1136 1.128 dsl *child_p = child; 1137 1.300 riastrad return SET_ERROR(error); 1138 1.109 dsl } 1139 1.165 ad 1140 1.165 ad /* 1141 1.165 ad * Wait for another child process to stop. 1142 1.165 ad */ 1143 1.290 ad error = cv_wait_sig(&parent->p_waitcv, &proc_lock); 1144 1.165 ad 1145 1.177 dsl if (error != 0) { 1146 1.177 dsl *child_p = NULL; 1147 1.109 dsl return error; 1148 1.177 dsl } 1149 1.109 dsl } 1150 1.109 dsl } 1151 1.109 dsl 1152 1.109 dsl /* 1153 1.165 ad * Free a process after parent has taken all the state info. Must be called 1154 1.169 ad * with the proclist lock held, and will release before returning. 1155 1.165 ad * 1156 1.165 ad * *ru is returned to the caller, and must be freed by the caller. 1157 1.109 dsl */ 1158 1.178 dsl static void 1159 1.249 christos proc_free(struct proc *p, struct wrusage *wru) 1160 1.109 dsl { 1161 1.219 rmind struct proc *parent = p->p_pptr; 1162 1.165 ad struct lwp *l; 1163 1.140 pk ksiginfo_t ksi; 1164 1.182 ad kauth_cred_t cred1, cred2; 1165 1.165 ad uid_t uid; 1166 1.24 cgd 1167 1.290 ad KASSERT(mutex_owned(&proc_lock)); 1168 1.165 ad KASSERT(p->p_nlwps == 1); 1169 1.165 ad KASSERT(p->p_nzlwps == 1); 1170 1.137 yamt KASSERT(p->p_nrlwps == 0); 1171 1.165 ad KASSERT(p->p_stat == SZOMB); 1172 1.137 yamt 1173 1.109 dsl /* 1174 1.109 dsl * If we got the child via ptrace(2) or procfs, and 1175 1.109 dsl * the parent is different (meaning the process was 1176 1.109 dsl * attached, rather than run as a child), then we need 1177 1.109 dsl * to give it back to the old parent, and send the 1178 1.109 dsl * parent the exit signal. The rest of the cleanup 1179 1.109 dsl * will be done when the old parent waits on the child. 1180 1.109 dsl */ 1181 1.219 rmind if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) { 1182 1.219 rmind mutex_enter(p->p_lock); 1183 1.269 kamil p->p_slflag &= ~(PSL_TRACED|PSL_SYSCALL); 1184 1.219 rmind mutex_exit(p->p_lock); 1185 1.219 rmind parent = (p->p_opptr == NULL) ? initproc : p->p_opptr; 1186 1.219 rmind proc_reparent(p, parent); 1187 1.219 rmind p->p_opptr = NULL; 1188 1.219 rmind if (p->p_exitsig != 0) { 1189 1.219 rmind exit_psignal(p, parent, &ksi); 1190 1.219 rmind kpsignal(parent, &ksi, NULL); 1191 1.140 pk } 1192 1.219 rmind cv_broadcast(&parent->p_waitcv); 1193 1.290 ad mutex_exit(&proc_lock); 1194 1.219 rmind return; 1195 1.109 dsl } 1196 1.109 dsl 1197 1.179 yamt sched_proc_exit(parent, p); 1198 1.202 ad 1199 1.178 dsl /* 1200 1.178 dsl * Add child times of exiting process onto its own times. 1201 1.178 dsl * This cannot be done any earlier else it might get done twice. 1202 1.178 dsl */ 1203 1.202 ad l = LIST_FIRST(&p->p_lwps); 1204 1.202 ad ruadd(&p->p_stats->p_ru, &l->l_ru); 1205 1.178 dsl ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru); 1206 1.175 dsl ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru); 1207 1.249 christos if (wru != NULL) { 1208 1.249 christos wru->wru_self = p->p_stats->p_ru; 1209 1.249 christos wru->wru_children = p->p_stats->p_cru; 1210 1.249 christos } 1211 1.252 christos p->p_xsig = 0; 1212 1.252 christos p->p_xexit = 0; 1213 1.115 dsl 1214 1.115 dsl /* 1215 1.260 skrll * At this point we are going to start freeing the final resources. 1216 1.165 ad * If anyone tries to access the proc structure after here they will 1217 1.165 ad * get a shock - bits are missing. Attempt to make it hard! We 1218 1.165 ad * don't bother with any further locking past this point. 1219 1.115 dsl */ 1220 1.165 ad p->p_stat = SIDL; /* not even a zombie any more */ 1221 1.165 ad LIST_REMOVE(p, p_list); /* off zombproc */ 1222 1.219 rmind parent->p_nstopchild--; 1223 1.165 ad LIST_REMOVE(p, p_sibling); 1224 1.115 dsl 1225 1.188 ad /* 1226 1.188 ad * Let pid be reallocated. 1227 1.188 ad */ 1228 1.230 chs proc_free_pid(p->p_pid); 1229 1.288 thorpej atomic_dec_uint(&nprocs); 1230 1.220 rmind 1231 1.220 rmind /* 1232 1.220 rmind * Unlink process from its process group. 1233 1.220 rmind * Releases the proc_lock. 1234 1.220 rmind */ 1235 1.220 rmind proc_leavepgrp(p); 1236 1.182 ad 1237 1.109 dsl /* 1238 1.188 ad * Delay release until after lwp_free. 1239 1.109 dsl */ 1240 1.182 ad cred2 = l->l_cred; 1241 1.182 ad 1242 1.182 ad /* 1243 1.188 ad * Free the last LWP's resources. 1244 1.188 ad * 1245 1.188 ad * lwp_free ensures the LWP is no longer running on another CPU. 1246 1.182 ad */ 1247 1.182 ad lwp_free(l, false, true); 1248 1.56 thorpej 1249 1.165 ad /* 1250 1.188 ad * Now no one except us can reach the process p. 1251 1.165 ad */ 1252 1.35 mycroft 1253 1.109 dsl /* 1254 1.109 dsl * Decrement the count of procs running with this uid. 1255 1.109 dsl */ 1256 1.188 ad cred1 = p->p_cred; 1257 1.188 ad uid = kauth_cred_getuid(cred1); 1258 1.165 ad (void)chgproccnt(uid, -1); 1259 1.24 cgd 1260 1.109 dsl /* 1261 1.165 ad * Release substructures. 1262 1.109 dsl */ 1263 1.188 ad 1264 1.233 rmind lim_free(p->p_limit); 1265 1.188 ad pstatsfree(p->p_stats); 1266 1.182 ad kauth_cred_free(cred1); 1267 1.182 ad kauth_cred_free(cred2); 1268 1.107 thorpej 1269 1.109 dsl /* 1270 1.109 dsl * Release reference to text vnode 1271 1.109 dsl */ 1272 1.188 ad if (p->p_textvp) 1273 1.188 ad vrele(p->p_textvp); 1274 1.270 christos kmem_strfree(p->p_path); 1275 1.188 ad 1276 1.198 ad mutex_destroy(&p->p_auxlock); 1277 1.204 ad mutex_obj_free(p->p_lock); 1278 1.188 ad mutex_destroy(&p->p_stmutex); 1279 1.188 ad cv_destroy(&p->p_waitcv); 1280 1.188 ad cv_destroy(&p->p_lwpcv); 1281 1.191 ad rw_destroy(&p->p_reflock); 1282 1.188 ad 1283 1.196 ad proc_free_mem(p); 1284 1.24 cgd } 1285 1.24 cgd 1286 1.24 cgd /* 1287 1.263 christos * Change the parent of a process for tracing purposes. 1288 1.263 christos */ 1289 1.263 christos void 1290 1.263 christos proc_changeparent(struct proc *t, struct proc *p) 1291 1.263 christos { 1292 1.263 christos SET(t->p_slflag, PSL_TRACED); 1293 1.263 christos t->p_opptr = t->p_pptr; 1294 1.263 christos if (t->p_pptr == p) 1295 1.263 christos return; 1296 1.263 christos struct proc *parent = t->p_pptr; 1297 1.263 christos 1298 1.263 christos if (parent->p_lock < t->p_lock) { 1299 1.263 christos if (!mutex_tryenter(parent->p_lock)) { 1300 1.263 christos mutex_exit(t->p_lock); 1301 1.263 christos mutex_enter(parent->p_lock); 1302 1.263 christos mutex_enter(t->p_lock); 1303 1.263 christos } 1304 1.263 christos } else if (parent->p_lock > t->p_lock) { 1305 1.263 christos mutex_enter(parent->p_lock); 1306 1.263 christos } 1307 1.263 christos parent->p_slflag |= PSL_CHTRACED; 1308 1.263 christos proc_reparent(t, p); 1309 1.263 christos if (parent->p_lock != t->p_lock) 1310 1.263 christos mutex_exit(parent->p_lock); 1311 1.263 christos } 1312 1.263 christos 1313 1.263 christos /* 1314 1.24 cgd * make process 'parent' the new parent of process 'child'. 1315 1.128 dsl * 1316 1.203 ad * Must be called with proc_lock held. 1317 1.24 cgd */ 1318 1.24 cgd void 1319 1.82 thorpej proc_reparent(struct proc *child, struct proc *parent) 1320 1.24 cgd { 1321 1.24 cgd 1322 1.290 ad KASSERT(mutex_owned(&proc_lock)); 1323 1.165 ad 1324 1.24 cgd if (child->p_pptr == parent) 1325 1.24 cgd return; 1326 1.70 thorpej 1327 1.246 pgoyette if (child->p_stat == SZOMB || child->p_stat == SDEAD || 1328 1.165 ad (child->p_stat == SSTOP && !child->p_waited)) { 1329 1.128 dsl child->p_pptr->p_nstopchild--; 1330 1.128 dsl parent->p_nstopchild++; 1331 1.128 dsl } 1332 1.267 christos if (parent == initproc) { 1333 1.70 thorpej child->p_exitsig = SIGCHLD; 1334 1.267 christos child->p_ppid = parent->p_pid; 1335 1.267 christos } 1336 1.24 cgd 1337 1.25 mycroft LIST_REMOVE(child, p_sibling); 1338 1.25 mycroft LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1339 1.24 cgd child->p_pptr = parent; 1340 1.24 cgd } 1341