1 /* $OpenBSD: kern_exit.c,v 1.217 2023/09/29 12:47:34 claudio Exp $ */ 2 /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/proc.h> 43 #include <sys/time.h> 44 #include <sys/resource.h> 45 #include <sys/wait.h> 46 #include <sys/vnode.h> 47 #include <sys/malloc.h> 48 #include <sys/resourcevar.h> 49 #include <sys/ptrace.h> 50 #include <sys/acct.h> 51 #include <sys/filedesc.h> 52 #include <sys/signalvar.h> 53 #include <sys/sched.h> 54 #include <sys/ktrace.h> 55 #include <sys/pool.h> 56 #include <sys/mutex.h> 57 #ifdef SYSVSEM 58 #include <sys/sem.h> 59 #endif 60 #include <sys/witness.h> 61 62 #include <sys/mount.h> 63 #include <sys/syscallargs.h> 64 65 #include <uvm/uvm_extern.h> 66 67 #include "kcov.h" 68 #if NKCOV > 0 69 #include <sys/kcov.h> 70 #endif 71 72 void proc_finish_wait(struct proc *, struct proc *); 73 void process_clear_orphan(struct process *); 74 void process_zap(struct process *); 75 void proc_free(struct proc *); 76 void unveil_destroy(struct process *ps); 77 78 /* 79 * exit -- 80 * Death of process. 81 */ 82 int 83 sys_exit(struct proc *p, void *v, register_t *retval) 84 { 85 struct sys_exit_args /* { 86 syscallarg(int) rval; 87 } */ *uap = v; 88 89 exit1(p, SCARG(uap, rval), 0, EXIT_NORMAL); 90 /* NOTREACHED */ 91 return (0); 92 } 93 94 int 95 sys___threxit(struct proc *p, void *v, register_t *retval) 96 { 97 struct sys___threxit_args /* { 98 syscallarg(pid_t *) notdead; 99 } */ *uap = v; 100 101 if (SCARG(uap, notdead) != NULL) { 102 pid_t zero = 0; 103 if (copyout(&zero, SCARG(uap, notdead), sizeof(zero))) 104 psignal(p, SIGSEGV); 105 } 106 exit1(p, 0, 0, EXIT_THREAD); 107 108 return (0); 109 } 110 111 /* 112 * Exit: deallocate address space and other resources, change proc state 113 * to zombie, and unlink proc from allproc and parent's lists. Save exit 114 * status and rusage for wait(). Check for child processes and orphan them. 115 */ 116 void 117 exit1(struct proc *p, int xexit, int xsig, int flags) 118 { 119 struct process *pr, *qr, *nqr; 120 struct rusage *rup; 121 struct timespec ts; 122 int s; 123 124 atomic_setbits_int(&p->p_flag, P_WEXIT); 125 126 pr = p->p_p; 127 128 /* single-threaded? */ 129 if (!P_HASSIBLING(p)) { 130 flags = EXIT_NORMAL; 131 } else { 132 /* nope, multi-threaded */ 133 if (flags == EXIT_NORMAL) 134 single_thread_set(p, SINGLE_EXIT); 135 else if (flags == EXIT_THREAD) 136 single_thread_check(p, 0); 137 } 138 139 if (flags == EXIT_NORMAL && !(pr->ps_flags & PS_EXITING)) { 140 if (pr->ps_pid == 1) 141 panic("init died (signal %d, exit %d)", xsig, xexit); 142 143 atomic_setbits_int(&pr->ps_flags, PS_EXITING); 144 pr->ps_xexit = xexit; 145 pr->ps_xsig = xsig; 146 147 /* 148 * If parent is waiting for us to exit or exec, PS_PPWAIT 149 * is set; we wake up the parent early to avoid deadlock. 150 */ 151 if (pr->ps_flags & PS_PPWAIT) { 152 atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT); 153 atomic_clearbits_int(&pr->ps_pptr->ps_flags, 154 PS_ISPWAIT); 155 wakeup(pr->ps_pptr); 156 } 157 } 158 159 /* unlink ourselves from the active threads */ 160 SCHED_LOCK(s); 161 TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link); 162 SCHED_UNLOCK(s); 163 164 if ((p->p_flag & P_THREAD) == 0) { 165 /* main thread gotta wait because it has the pid, et al */ 166 while (pr->ps_threadcnt > 1) 167 tsleep_nsec(&pr->ps_threads, PWAIT, "thrdeath", INFSLP); 168 } 169 170 rup = pr->ps_ru; 171 if (rup == NULL) { 172 rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO); 173 if (pr->ps_ru == NULL) { 174 pr->ps_ru = rup; 175 } else { 176 pool_put(&rusage_pool, rup); 177 rup = pr->ps_ru; 178 } 179 } 180 p->p_siglist = 0; 181 if ((p->p_flag & P_THREAD) == 0) 182 pr->ps_siglist = 0; 183 184 kqpoll_exit(); 185 186 #if NKCOV > 0 187 kcov_exit(p); 188 #endif 189 190 if ((p->p_flag & P_THREAD) == 0) { 191 if (pr->ps_flags & PS_PROFIL) 192 stopprofclock(pr); 193 194 sigio_freelist(&pr->ps_sigiolst); 195 196 /* close open files and release open-file table */ 197 fdfree(p); 198 199 cancel_all_itimers(); 200 201 timeout_del(&pr->ps_rucheck_to); 202 #ifdef SYSVSEM 203 semexit(pr); 204 #endif 205 killjobc(pr); 206 #ifdef ACCOUNTING 207 acct_process(p); 208 #endif 209 210 #ifdef KTRACE 211 /* release trace file */ 212 if (pr->ps_tracevp) 213 ktrcleartrace(pr); 214 #endif 215 216 unveil_destroy(pr); 217 218 /* 219 * If parent has the SAS_NOCLDWAIT flag set, we're not 220 * going to become a zombie. 221 */ 222 if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT) 223 atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE); 224 } 225 226 p->p_fd = NULL; /* zap the thread's copy */ 227 228 /* Release the thread's read reference of resource limit structure. */ 229 if (p->p_limit != NULL) { 230 struct plimit *limit; 231 232 limit = p->p_limit; 233 p->p_limit = NULL; 234 lim_free(limit); 235 } 236 237 /* 238 * Remove proc from pidhash chain and allproc so looking 239 * it up won't work. We will put the proc on the 240 * deadproc list later (using the p_hash member), and 241 * wake up the reaper when we do. If this is the last 242 * thread of a process that isn't PS_NOZOMBIE, we'll put 243 * the process on the zombprocess list below. 244 */ 245 /* 246 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 247 */ 248 p->p_stat = SDEAD; 249 250 LIST_REMOVE(p, p_hash); 251 LIST_REMOVE(p, p_list); 252 253 if ((p->p_flag & P_THREAD) == 0) { 254 LIST_REMOVE(pr, ps_hash); 255 LIST_REMOVE(pr, ps_list); 256 257 if ((pr->ps_flags & PS_NOZOMBIE) == 0) 258 LIST_INSERT_HEAD(&zombprocess, pr, ps_list); 259 else { 260 /* 261 * Not going to be a zombie, so it's now off all 262 * the lists scanned by ispidtaken(), so block 263 * fast reuse of the pid now. 264 */ 265 freepid(pr->ps_pid); 266 } 267 268 /* 269 * Reparent children to their original parent, in case 270 * they were being traced, or to init(8). 271 */ 272 qr = LIST_FIRST(&pr->ps_children); 273 if (qr) /* only need this if any child is S_ZOMB */ 274 wakeup(initprocess); 275 for (; qr != NULL; qr = nqr) { 276 nqr = LIST_NEXT(qr, ps_sibling); 277 /* 278 * Traced processes are killed since their 279 * existence means someone is screwing up. 280 */ 281 if (qr->ps_flags & PS_TRACED && 282 !(qr->ps_flags & PS_EXITING)) { 283 process_untrace(qr); 284 285 /* 286 * If single threading is active, 287 * direct the signal to the active 288 * thread to avoid deadlock. 289 */ 290 if (qr->ps_single) 291 ptsignal(qr->ps_single, SIGKILL, 292 STHREAD); 293 else 294 prsignal(qr, SIGKILL); 295 } else { 296 process_reparent(qr, initprocess); 297 } 298 } 299 300 /* 301 * Make sure orphans won't remember the exiting process. 302 */ 303 while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) { 304 KASSERT(qr->ps_oppid == pr->ps_pid); 305 qr->ps_oppid = 0; 306 process_clear_orphan(qr); 307 } 308 } 309 310 /* add thread's accumulated rusage into the process's total */ 311 ruadd(rup, &p->p_ru); 312 nanouptime(&ts); 313 if (timespeccmp(&ts, &curcpu()->ci_schedstate.spc_runtime, <)) 314 timespecclear(&ts); 315 else 316 timespecsub(&ts, &curcpu()->ci_schedstate.spc_runtime, &ts); 317 SCHED_LOCK(s); 318 tuagg_locked(pr, p, &ts); 319 SCHED_UNLOCK(s); 320 321 /* 322 * clear %cpu usage during swap 323 */ 324 p->p_pctcpu = 0; 325 326 if ((p->p_flag & P_THREAD) == 0) { 327 /* 328 * Final thread has died, so add on our children's rusage 329 * and calculate the total times 330 */ 331 calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL); 332 ruadd(rup, &pr->ps_cru); 333 334 /* 335 * Notify parent that we're gone. If we're not going to 336 * become a zombie, reparent to process 1 (init) so that 337 * we can wake our original parent to possibly unblock 338 * wait4() to return ECHILD. 339 */ 340 if (pr->ps_flags & PS_NOZOMBIE) { 341 struct process *ppr = pr->ps_pptr; 342 process_reparent(pr, initprocess); 343 wakeup(ppr); 344 } 345 } 346 347 /* just a thread? detach it from its process */ 348 if (p->p_flag & P_THREAD) { 349 /* scheduler_wait_hook(pr->ps_mainproc, p); XXX */ 350 if (--pr->ps_threadcnt == 1) 351 wakeup(&pr->ps_threads); 352 KASSERT(pr->ps_threadcnt > 0); 353 } 354 355 /* 356 * Other substructures are freed from reaper and wait(). 357 */ 358 359 /* 360 * Finally, call machine-dependent code to switch to a new 361 * context (possibly the idle context). Once we are no longer 362 * using the dead process's vmspace and stack, exit2() will be 363 * called to schedule those resources to be released by the 364 * reaper thread. 365 * 366 * Note that cpu_exit() will end with a call equivalent to 367 * cpu_switch(), finishing our execution (pun intended). 368 */ 369 uvmexp.swtch++; 370 cpu_exit(p); 371 panic("cpu_exit returned"); 372 } 373 374 /* 375 * Locking of this proclist is special; it's accessed in a 376 * critical section of process exit, and thus locking it can't 377 * modify interrupt state. We use a simple spin lock for this 378 * proclist. We use the p_hash member to linkup to deadproc. 379 */ 380 struct mutex deadproc_mutex = 381 MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS); 382 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 383 384 /* 385 * We are called from cpu_exit() once it is safe to schedule the 386 * dead process's resources to be freed. 387 * 388 * NOTE: One must be careful with locking in this routine. It's 389 * called from a critical section in machine-dependent code, so 390 * we should refrain from changing any interrupt state. 391 * 392 * We lock the deadproc list, place the proc on that list (using 393 * the p_hash member), and wake up the reaper. 394 */ 395 void 396 exit2(struct proc *p) 397 { 398 mtx_enter(&deadproc_mutex); 399 LIST_INSERT_HEAD(&deadproc, p, p_hash); 400 mtx_leave(&deadproc_mutex); 401 402 wakeup(&deadproc); 403 } 404 405 void 406 proc_free(struct proc *p) 407 { 408 crfree(p->p_ucred); 409 pool_put(&proc_pool, p); 410 nthreads--; 411 } 412 413 /* 414 * Process reaper. This is run by a kernel thread to free the resources 415 * of a dead process. Once the resources are free, the process becomes 416 * a zombie, and the parent is allowed to read the undead's status. 417 */ 418 void 419 reaper(void *arg) 420 { 421 struct proc *p; 422 423 KERNEL_UNLOCK(); 424 425 SCHED_ASSERT_UNLOCKED(); 426 427 for (;;) { 428 mtx_enter(&deadproc_mutex); 429 while ((p = LIST_FIRST(&deadproc)) == NULL) 430 msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper", 431 INFSLP); 432 433 /* Remove us from the deadproc list. */ 434 LIST_REMOVE(p, p_hash); 435 mtx_leave(&deadproc_mutex); 436 437 WITNESS_THREAD_EXIT(p); 438 439 KERNEL_LOCK(); 440 441 /* 442 * Free the VM resources we're still holding on to. 443 * We must do this from a valid thread because doing 444 * so may block. 445 */ 446 uvm_uarea_free(p); 447 p->p_vmspace = NULL; /* zap the thread's copy */ 448 449 if (p->p_flag & P_THREAD) { 450 /* Just a thread */ 451 proc_free(p); 452 } else { 453 struct process *pr = p->p_p; 454 455 /* Release the rest of the process's vmspace */ 456 uvm_exit(pr); 457 458 if ((pr->ps_flags & PS_NOZOMBIE) == 0) { 459 /* Process is now a true zombie. */ 460 atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE); 461 } 462 463 /* Notify listeners of our demise and clean up. */ 464 knote_processexit(pr); 465 466 if (pr->ps_flags & PS_ZOMBIE) { 467 /* Post SIGCHLD and wake up parent. */ 468 prsignal(pr->ps_pptr, SIGCHLD); 469 wakeup(pr->ps_pptr); 470 } else { 471 /* No one will wait for us, just zap it. */ 472 process_zap(pr); 473 } 474 } 475 476 KERNEL_UNLOCK(); 477 } 478 } 479 480 int 481 dowait6(struct proc *q, idtype_t idtype, id_t id, int *statusp, int options, 482 struct rusage *rusage, siginfo_t *info, register_t *retval) 483 { 484 int nfound; 485 struct process *pr; 486 struct proc *p; 487 int error; 488 489 if (info != NULL) 490 memset(info, 0, sizeof(*info)); 491 492 loop: 493 nfound = 0; 494 LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) { 495 if ((pr->ps_flags & PS_NOZOMBIE) || 496 (idtype == P_PID && id != pr->ps_pid) || 497 (idtype == P_PGID && id != pr->ps_pgid)) 498 continue; 499 500 p = pr->ps_mainproc; 501 502 nfound++; 503 if ((options & WEXITED) && (pr->ps_flags & PS_ZOMBIE)) { 504 *retval = pr->ps_pid; 505 if (info != NULL) { 506 info->si_pid = pr->ps_pid; 507 info->si_uid = pr->ps_ucred->cr_uid; 508 info->si_signo = SIGCHLD; 509 if (pr->ps_xsig == 0) { 510 info->si_code = CLD_EXITED; 511 info->si_status = pr->ps_xexit; 512 } else if (WCOREDUMP(pr->ps_xsig)) { 513 info->si_code = CLD_DUMPED; 514 info->si_status = _WSTATUS(pr->ps_xsig); 515 } else { 516 info->si_code = CLD_KILLED; 517 info->si_status = _WSTATUS(pr->ps_xsig); 518 } 519 } 520 521 if (statusp != NULL) 522 *statusp = W_EXITCODE(pr->ps_xexit, 523 pr->ps_xsig); 524 if (rusage != NULL) 525 memcpy(rusage, pr->ps_ru, sizeof(*rusage)); 526 if ((options & WNOWAIT) == 0) 527 proc_finish_wait(q, p); 528 return (0); 529 } 530 if ((options & WTRAPPED) && 531 pr->ps_flags & PS_TRACED && 532 (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single && 533 pr->ps_single->p_stat == SSTOP && 534 (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) { 535 if (single_thread_wait(pr, 0)) 536 goto loop; 537 538 if ((options & WNOWAIT) == 0) 539 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 540 541 *retval = pr->ps_pid; 542 if (info != NULL) { 543 info->si_pid = pr->ps_pid; 544 info->si_uid = pr->ps_ucred->cr_uid; 545 info->si_signo = SIGCHLD; 546 info->si_code = CLD_TRAPPED; 547 info->si_status = pr->ps_xsig; 548 } 549 550 if (statusp != NULL) 551 *statusp = W_STOPCODE(pr->ps_xsig); 552 if (rusage != NULL) 553 memset(rusage, 0, sizeof(*rusage)); 554 return (0); 555 } 556 if (p->p_stat == SSTOP && 557 (pr->ps_flags & PS_WAITED) == 0 && 558 (p->p_flag & P_SUSPSINGLE) == 0 && 559 (pr->ps_flags & PS_TRACED || 560 options & WUNTRACED)) { 561 if ((options & WNOWAIT) == 0) 562 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 563 564 *retval = pr->ps_pid; 565 if (info != 0) { 566 info->si_pid = pr->ps_pid; 567 info->si_uid = pr->ps_ucred->cr_uid; 568 info->si_signo = SIGCHLD; 569 info->si_code = CLD_STOPPED; 570 info->si_status = pr->ps_xsig; 571 } 572 573 if (statusp != NULL) 574 *statusp = W_STOPCODE(pr->ps_xsig); 575 if (rusage != NULL) 576 memset(rusage, 0, sizeof(*rusage)); 577 return (0); 578 } 579 if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 580 if ((options & WNOWAIT) == 0) 581 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 582 583 *retval = pr->ps_pid; 584 if (info != NULL) { 585 info->si_pid = pr->ps_pid; 586 info->si_uid = pr->ps_ucred->cr_uid; 587 info->si_signo = SIGCHLD; 588 info->si_code = CLD_CONTINUED; 589 info->si_status = SIGCONT; 590 } 591 592 if (statusp != NULL) 593 *statusp = _WCONTINUED; 594 if (rusage != NULL) 595 memset(rusage, 0, sizeof(*rusage)); 596 return (0); 597 } 598 } 599 /* 600 * Look in the orphans list too, to allow the parent to 601 * collect its child's exit status even if child is being 602 * debugged. 603 * 604 * Debugger detaches from the parent upon successful 605 * switch-over from parent to child. At this point due to 606 * re-parenting the parent loses the child to debugger and a 607 * wait4(2) call would report that it has no children to wait 608 * for. By maintaining a list of orphans we allow the parent 609 * to successfully wait until the child becomes a zombie. 610 */ 611 if (nfound == 0) { 612 LIST_FOREACH(pr, &q->p_p->ps_orphans, ps_orphan) { 613 if ((pr->ps_flags & PS_NOZOMBIE) || 614 (idtype == P_PID && id != pr->ps_pid) || 615 (idtype == P_PGID && id != pr->ps_pgid)) 616 continue; 617 nfound++; 618 break; 619 } 620 } 621 if (nfound == 0) 622 return (ECHILD); 623 if (options & WNOHANG) { 624 *retval = 0; 625 return (0); 626 } 627 if ((error = tsleep_nsec(q->p_p, PWAIT | PCATCH, "wait", INFSLP)) != 0) 628 return (error); 629 goto loop; 630 } 631 632 int 633 sys_wait4(struct proc *q, void *v, register_t *retval) 634 { 635 struct sys_wait4_args /* { 636 syscallarg(pid_t) pid; 637 syscallarg(int *) status; 638 syscallarg(int) options; 639 syscallarg(struct rusage *) rusage; 640 } */ *uap = v; 641 struct rusage ru; 642 pid_t pid = SCARG(uap, pid); 643 int options = SCARG(uap, options); 644 int status, error; 645 idtype_t idtype; 646 id_t id; 647 648 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WCONTINUED)) 649 return (EINVAL); 650 options |= WEXITED | WTRAPPED; 651 652 if (SCARG(uap, pid) == WAIT_MYPGRP) { 653 idtype = P_PGID; 654 id = q->p_p->ps_pgid; 655 } else if (SCARG(uap, pid) == WAIT_ANY) { 656 idtype = P_ALL; 657 id = 0; 658 } else if (pid < 0) { 659 idtype = P_PGID; 660 id = -pid; 661 } else { 662 idtype = P_PID; 663 id = pid; 664 } 665 666 error = dowait6(q, idtype, id, 667 SCARG(uap, status) ? &status : NULL, options, 668 SCARG(uap, rusage) ? &ru : NULL, NULL, retval); 669 if (error == 0 && *retval > 0 && SCARG(uap, status)) { 670 error = copyout(&status, SCARG(uap, status), sizeof(status)); 671 } 672 if (error == 0 && *retval > 0 && SCARG(uap, rusage)) { 673 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 674 #ifdef KTRACE 675 if (error == 0 && KTRPOINT(q, KTR_STRUCT)) 676 ktrrusage(q, &ru); 677 #endif 678 } 679 return (error); 680 } 681 682 int 683 sys_waitid(struct proc *q, void *v, register_t *retval) 684 { 685 struct sys_waitid_args /* { 686 syscallarg(idtype_t) idtype; 687 syscallarg(id_t) id; 688 syscallarg(siginfo_t) info; 689 syscallarg(int) options; 690 } */ *uap = v; 691 siginfo_t info; 692 idtype_t idtype = SCARG(uap, idtype); 693 int options = SCARG(uap, options); 694 int error; 695 696 if (options &~ (WSTOPPED|WCONTINUED|WEXITED|WTRAPPED|WNOHANG|WNOWAIT)) 697 return (EINVAL); 698 if ((options & (WSTOPPED|WCONTINUED|WEXITED|WTRAPPED)) == 0) 699 return (EINVAL); 700 if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID) 701 return (EINVAL); 702 703 error = dowait6(q, idtype, SCARG(uap, id), NULL, 704 options, NULL, &info, retval); 705 if (error == 0) { 706 error = copyout(&info, SCARG(uap, info), sizeof(info)); 707 #ifdef KTRACE 708 if (error == 0 && KTRPOINT(q, KTR_STRUCT)) 709 ktrsiginfo(q, &info); 710 #endif 711 } 712 if (error == 0) 713 *retval = 0; 714 return (error); 715 } 716 717 void 718 proc_finish_wait(struct proc *waiter, struct proc *p) 719 { 720 struct process *pr, *tr; 721 struct rusage *rup; 722 723 /* 724 * If we got the child via a ptrace 'attach', 725 * we need to give it back to the old parent. 726 */ 727 pr = p->p_p; 728 if (pr->ps_oppid != 0 && (pr->ps_oppid != pr->ps_pptr->ps_pid) && 729 (tr = prfind(pr->ps_oppid))) { 730 pr->ps_oppid = 0; 731 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 732 process_reparent(pr, tr); 733 prsignal(tr, SIGCHLD); 734 wakeup(tr); 735 } else { 736 scheduler_wait_hook(waiter, p); 737 rup = &waiter->p_p->ps_cru; 738 ruadd(rup, pr->ps_ru); 739 LIST_REMOVE(pr, ps_list); /* off zombprocess */ 740 freepid(pr->ps_pid); 741 process_zap(pr); 742 } 743 } 744 745 /* 746 * give process back to original parent or init(8) 747 */ 748 void 749 process_untrace(struct process *pr) 750 { 751 struct process *ppr = NULL; 752 753 KASSERT(pr->ps_flags & PS_TRACED); 754 755 if (pr->ps_oppid != 0 && 756 (pr->ps_oppid != pr->ps_pptr->ps_pid)) 757 ppr = prfind(pr->ps_oppid); 758 759 /* not being traced any more */ 760 pr->ps_oppid = 0; 761 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 762 process_reparent(pr, ppr ? ppr : initprocess); 763 } 764 765 void 766 process_clear_orphan(struct process *pr) 767 { 768 if (pr->ps_flags & PS_ORPHAN) { 769 LIST_REMOVE(pr, ps_orphan); 770 atomic_clearbits_int(&pr->ps_flags, PS_ORPHAN); 771 } 772 } 773 774 /* 775 * make process 'parent' the new parent of process 'child'. 776 */ 777 void 778 process_reparent(struct process *child, struct process *parent) 779 { 780 781 if (child->ps_pptr == parent) 782 return; 783 784 KASSERT(child->ps_oppid == 0 || 785 child->ps_oppid == child->ps_pptr->ps_pid); 786 787 LIST_REMOVE(child, ps_sibling); 788 LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling); 789 790 process_clear_orphan(child); 791 if (child->ps_flags & PS_TRACED) { 792 atomic_setbits_int(&child->ps_flags, PS_ORPHAN); 793 LIST_INSERT_HEAD(&child->ps_pptr->ps_orphans, child, ps_orphan); 794 } 795 796 child->ps_pptr = parent; 797 child->ps_ppid = parent->ps_pid; 798 } 799 800 void 801 process_zap(struct process *pr) 802 { 803 struct vnode *otvp; 804 struct proc *p = pr->ps_mainproc; 805 806 /* 807 * Finally finished with old proc entry. 808 * Unlink it from its process group and free it. 809 */ 810 leavepgrp(pr); 811 LIST_REMOVE(pr, ps_sibling); 812 process_clear_orphan(pr); 813 814 /* 815 * Decrement the count of procs running with this uid. 816 */ 817 (void)chgproccnt(pr->ps_ucred->cr_ruid, -1); 818 819 /* 820 * Release reference to text vnode 821 */ 822 otvp = pr->ps_textvp; 823 pr->ps_textvp = NULL; 824 if (otvp) 825 vrele(otvp); 826 827 KASSERT(pr->ps_threadcnt == 1); 828 if (pr->ps_ptstat != NULL) 829 free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat)); 830 pool_put(&rusage_pool, pr->ps_ru); 831 KASSERT(TAILQ_EMPTY(&pr->ps_threads)); 832 sigactsfree(pr->ps_sigacts); 833 lim_free(pr->ps_limit); 834 crfree(pr->ps_ucred); 835 pool_put(&process_pool, pr); 836 nprocesses--; 837 838 proc_free(p); 839 } 840