1 /* $OpenBSD: kern_exit.c,v 1.223 2024/07/08 09:15:05 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 123 atomic_setbits_int(&p->p_flag, P_WEXIT); 124 125 pr = p->p_p; 126 127 /* single-threaded? */ 128 if (!P_HASSIBLING(p)) { 129 flags = EXIT_NORMAL; 130 } else { 131 /* nope, multi-threaded */ 132 if (flags == EXIT_NORMAL) 133 single_thread_set(p, SINGLE_EXIT); 134 } 135 136 if (flags == EXIT_NORMAL && !(pr->ps_flags & PS_EXITING)) { 137 if (pr->ps_pid == 1) 138 panic("init died (signal %d, exit %d)", xsig, xexit); 139 140 atomic_setbits_int(&pr->ps_flags, PS_EXITING); 141 pr->ps_xexit = xexit; 142 pr->ps_xsig = xsig; 143 144 /* 145 * If parent is waiting for us to exit or exec, PS_PPWAIT 146 * is set; we wake up the parent early to avoid deadlock. 147 */ 148 if (pr->ps_flags & PS_PPWAIT) { 149 atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT); 150 atomic_clearbits_int(&pr->ps_pptr->ps_flags, 151 PS_ISPWAIT); 152 wakeup(pr->ps_pptr); 153 } 154 } 155 156 /* unlink ourselves from the active threads */ 157 mtx_enter(&pr->ps_mtx); 158 TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link); 159 pr->ps_threadcnt--; 160 pr->ps_exitcnt++; 161 162 /* 163 * if somebody else wants to take us to single threaded mode, 164 * count ourselves out. 165 */ 166 if (pr->ps_single) { 167 if (--pr->ps_singlecnt == 0) 168 wakeup(&pr->ps_singlecnt); 169 } 170 171 if ((p->p_flag & P_THREAD) == 0) { 172 /* main thread gotta wait because it has the pid, et al */ 173 while (pr->ps_threadcnt + pr->ps_exitcnt > 1) 174 msleep_nsec(&pr->ps_threads, &pr->ps_mtx, PWAIT, 175 "thrdeath", INFSLP); 176 } 177 mtx_leave(&pr->ps_mtx); 178 179 rup = pr->ps_ru; 180 if (rup == NULL) { 181 rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO); 182 if (pr->ps_ru == NULL) { 183 pr->ps_ru = rup; 184 } else { 185 pool_put(&rusage_pool, rup); 186 rup = pr->ps_ru; 187 } 188 } 189 p->p_siglist = 0; 190 if ((p->p_flag & P_THREAD) == 0) 191 pr->ps_siglist = 0; 192 193 kqpoll_exit(); 194 195 #if NKCOV > 0 196 kcov_exit(p); 197 #endif 198 199 if ((p->p_flag & P_THREAD) == 0) { 200 if (pr->ps_flags & PS_PROFIL) 201 stopprofclock(pr); 202 203 sigio_freelist(&pr->ps_sigiolst); 204 205 /* close open files and release open-file table */ 206 fdfree(p); 207 208 cancel_all_itimers(); 209 210 timeout_del(&pr->ps_rucheck_to); 211 #ifdef SYSVSEM 212 semexit(pr); 213 #endif 214 killjobc(pr); 215 #ifdef ACCOUNTING 216 acct_process(p); 217 #endif 218 219 #ifdef KTRACE 220 /* release trace file */ 221 if (pr->ps_tracevp) 222 ktrcleartrace(pr); 223 #endif 224 225 unveil_destroy(pr); 226 227 free(pr->ps_pin.pn_pins, M_PINSYSCALL, 228 pr->ps_pin.pn_npins * sizeof(u_int)); 229 free(pr->ps_libcpin.pn_pins, M_PINSYSCALL, 230 pr->ps_libcpin.pn_npins * sizeof(u_int)); 231 232 /* 233 * If parent has the SAS_NOCLDWAIT flag set, we're not 234 * going to become a zombie. 235 */ 236 if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT) 237 atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE); 238 } 239 240 p->p_fd = NULL; /* zap the thread's copy */ 241 242 /* Release the thread's read reference of resource limit structure. */ 243 if (p->p_limit != NULL) { 244 struct plimit *limit; 245 246 limit = p->p_limit; 247 p->p_limit = NULL; 248 lim_free(limit); 249 } 250 251 /* 252 * Remove proc from pidhash chain and allproc so looking 253 * it up won't work. We will put the proc on the 254 * deadproc list later (using the p_hash member), and 255 * wake up the reaper when we do. If this is the last 256 * thread of a process that isn't PS_NOZOMBIE, we'll put 257 * the process on the zombprocess list below. 258 */ 259 /* 260 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 261 */ 262 p->p_stat = SDEAD; 263 264 LIST_REMOVE(p, p_hash); 265 LIST_REMOVE(p, p_list); 266 267 if ((p->p_flag & P_THREAD) == 0) { 268 LIST_REMOVE(pr, ps_hash); 269 LIST_REMOVE(pr, ps_list); 270 271 if ((pr->ps_flags & PS_NOZOMBIE) == 0) 272 LIST_INSERT_HEAD(&zombprocess, pr, ps_list); 273 else { 274 /* 275 * Not going to be a zombie, so it's now off all 276 * the lists scanned by ispidtaken(), so block 277 * fast reuse of the pid now. 278 */ 279 freepid(pr->ps_pid); 280 } 281 282 /* 283 * Reparent children to their original parent, in case 284 * they were being traced, or to init(8). 285 */ 286 qr = LIST_FIRST(&pr->ps_children); 287 if (qr) /* only need this if any child is S_ZOMB */ 288 wakeup(initprocess); 289 for (; qr != NULL; qr = nqr) { 290 nqr = LIST_NEXT(qr, ps_sibling); 291 /* 292 * Traced processes are killed since their 293 * existence means someone is screwing up. 294 */ 295 if (qr->ps_flags & PS_TRACED && 296 !(qr->ps_flags & PS_EXITING)) { 297 process_untrace(qr); 298 299 /* 300 * If single threading is active, 301 * direct the signal to the active 302 * thread to avoid deadlock. 303 */ 304 if (qr->ps_single) 305 ptsignal(qr->ps_single, SIGKILL, 306 STHREAD); 307 else 308 prsignal(qr, SIGKILL); 309 } else { 310 process_reparent(qr, initprocess); 311 } 312 } 313 314 /* 315 * Make sure orphans won't remember the exiting process. 316 */ 317 while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) { 318 KASSERT(qr->ps_oppid == pr->ps_pid); 319 qr->ps_oppid = 0; 320 process_clear_orphan(qr); 321 } 322 } 323 324 /* add thread's accumulated rusage into the process's total */ 325 ruadd(rup, &p->p_ru); 326 nanouptime(&ts); 327 if (timespeccmp(&ts, &curcpu()->ci_schedstate.spc_runtime, <)) 328 timespecclear(&ts); 329 else 330 timespecsub(&ts, &curcpu()->ci_schedstate.spc_runtime, &ts); 331 SCHED_LOCK(); 332 tuagg_locked(pr, p, &ts); 333 SCHED_UNLOCK(); 334 335 /* 336 * clear %cpu usage during swap 337 */ 338 p->p_pctcpu = 0; 339 340 if ((p->p_flag & P_THREAD) == 0) { 341 /* 342 * Final thread has died, so add on our children's rusage 343 * and calculate the total times 344 */ 345 calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL); 346 ruadd(rup, &pr->ps_cru); 347 348 /* 349 * Notify parent that we're gone. If we're not going to 350 * become a zombie, reparent to process 1 (init) so that 351 * we can wake our original parent to possibly unblock 352 * wait4() to return ECHILD. 353 */ 354 if (pr->ps_flags & PS_NOZOMBIE) { 355 struct process *ppr = pr->ps_pptr; 356 process_reparent(pr, initprocess); 357 wakeup(ppr); 358 } 359 } 360 361 /* just a thread? detach it from its process */ 362 if (p->p_flag & P_THREAD) { 363 /* scheduler_wait_hook(pr->ps_mainproc, p); XXX */ 364 mtx_enter(&pr->ps_mtx); 365 pr->ps_exitcnt--; 366 if (pr->ps_threadcnt + pr->ps_exitcnt == 1) 367 wakeup(&pr->ps_threads); 368 mtx_leave(&pr->ps_mtx); 369 } 370 371 /* 372 * Other substructures are freed from reaper and wait(). 373 */ 374 375 /* 376 * Finally, call machine-dependent code to switch to a new 377 * context (possibly the idle context). Once we are no longer 378 * using the dead process's vmspace and stack, exit2() will be 379 * called to schedule those resources to be released by the 380 * reaper thread. 381 * 382 * Note that cpu_exit() will end with a call equivalent to 383 * cpu_switch(), finishing our execution (pun intended). 384 */ 385 uvmexp.swtch++; 386 cpu_exit(p); 387 panic("cpu_exit returned"); 388 } 389 390 /* 391 * Locking of this proclist is special; it's accessed in a 392 * critical section of process exit, and thus locking it can't 393 * modify interrupt state. We use a simple spin lock for this 394 * proclist. We use the p_hash member to linkup to deadproc. 395 */ 396 struct mutex deadproc_mutex = 397 MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS); 398 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 399 400 /* 401 * We are called from sched_idle() once it is safe to schedule the 402 * dead process's resources to be freed. So this is not allowed to sleep. 403 * 404 * We lock the deadproc list, place the proc on that list (using 405 * the p_hash member), and wake up the reaper. 406 */ 407 void 408 exit2(struct proc *p) 409 { 410 mtx_enter(&deadproc_mutex); 411 LIST_INSERT_HEAD(&deadproc, p, p_hash); 412 mtx_leave(&deadproc_mutex); 413 414 wakeup(&deadproc); 415 } 416 417 void 418 proc_free(struct proc *p) 419 { 420 crfree(p->p_ucred); 421 pool_put(&proc_pool, p); 422 nthreads--; 423 } 424 425 /* 426 * Process reaper. This is run by a kernel thread to free the resources 427 * of a dead process. Once the resources are free, the process becomes 428 * a zombie, and the parent is allowed to read the undead's status. 429 */ 430 void 431 reaper(void *arg) 432 { 433 struct proc *p; 434 435 KERNEL_UNLOCK(); 436 437 SCHED_ASSERT_UNLOCKED(); 438 439 for (;;) { 440 mtx_enter(&deadproc_mutex); 441 while ((p = LIST_FIRST(&deadproc)) == NULL) 442 msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper", 443 INFSLP); 444 445 /* Remove us from the deadproc list. */ 446 LIST_REMOVE(p, p_hash); 447 mtx_leave(&deadproc_mutex); 448 449 WITNESS_THREAD_EXIT(p); 450 451 KERNEL_LOCK(); 452 453 /* 454 * Free the VM resources we're still holding on to. 455 * We must do this from a valid thread because doing 456 * so may block. 457 */ 458 uvm_uarea_free(p); 459 p->p_vmspace = NULL; /* zap the thread's copy */ 460 461 if (p->p_flag & P_THREAD) { 462 /* Just a thread */ 463 proc_free(p); 464 } else { 465 struct process *pr = p->p_p; 466 467 /* Release the rest of the process's vmspace */ 468 uvm_exit(pr); 469 470 if ((pr->ps_flags & PS_NOZOMBIE) == 0) { 471 /* Process is now a true zombie. */ 472 atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE); 473 } 474 475 /* Notify listeners of our demise and clean up. */ 476 knote_processexit(pr); 477 478 if (pr->ps_flags & PS_ZOMBIE) { 479 /* Post SIGCHLD and wake up parent. */ 480 prsignal(pr->ps_pptr, SIGCHLD); 481 wakeup(pr->ps_pptr); 482 } else { 483 /* No one will wait for us, just zap it. */ 484 process_zap(pr); 485 } 486 } 487 488 KERNEL_UNLOCK(); 489 } 490 } 491 492 int 493 dowait6(struct proc *q, idtype_t idtype, id_t id, int *statusp, int options, 494 struct rusage *rusage, siginfo_t *info, register_t *retval) 495 { 496 int nfound; 497 struct process *pr; 498 struct proc *p; 499 int error; 500 501 if (info != NULL) 502 memset(info, 0, sizeof(*info)); 503 504 loop: 505 nfound = 0; 506 LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) { 507 if ((pr->ps_flags & PS_NOZOMBIE) || 508 (idtype == P_PID && id != pr->ps_pid) || 509 (idtype == P_PGID && id != pr->ps_pgid)) 510 continue; 511 512 p = pr->ps_mainproc; 513 514 nfound++; 515 if ((options & WEXITED) && (pr->ps_flags & PS_ZOMBIE)) { 516 *retval = pr->ps_pid; 517 if (info != NULL) { 518 info->si_pid = pr->ps_pid; 519 info->si_uid = pr->ps_ucred->cr_uid; 520 info->si_signo = SIGCHLD; 521 if (pr->ps_xsig == 0) { 522 info->si_code = CLD_EXITED; 523 info->si_status = pr->ps_xexit; 524 } else if (WCOREDUMP(pr->ps_xsig)) { 525 info->si_code = CLD_DUMPED; 526 info->si_status = _WSTATUS(pr->ps_xsig); 527 } else { 528 info->si_code = CLD_KILLED; 529 info->si_status = _WSTATUS(pr->ps_xsig); 530 } 531 } 532 533 if (statusp != NULL) 534 *statusp = W_EXITCODE(pr->ps_xexit, 535 pr->ps_xsig); 536 if (rusage != NULL) 537 memcpy(rusage, pr->ps_ru, sizeof(*rusage)); 538 if ((options & WNOWAIT) == 0) 539 proc_finish_wait(q, p); 540 return (0); 541 } 542 if ((options & WTRAPPED) && 543 pr->ps_flags & PS_TRACED && 544 (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single && 545 pr->ps_single->p_stat == SSTOP && 546 (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) { 547 if (single_thread_wait(pr, 0)) 548 goto loop; 549 550 if ((options & WNOWAIT) == 0) 551 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 552 553 *retval = pr->ps_pid; 554 if (info != NULL) { 555 info->si_pid = pr->ps_pid; 556 info->si_uid = pr->ps_ucred->cr_uid; 557 info->si_signo = SIGCHLD; 558 info->si_code = CLD_TRAPPED; 559 info->si_status = pr->ps_xsig; 560 } 561 562 if (statusp != NULL) 563 *statusp = W_STOPCODE(pr->ps_xsig); 564 if (rusage != NULL) 565 memset(rusage, 0, sizeof(*rusage)); 566 return (0); 567 } 568 if (p->p_stat == SSTOP && 569 (pr->ps_flags & PS_WAITED) == 0 && 570 (p->p_flag & P_SUSPSINGLE) == 0 && 571 (pr->ps_flags & PS_TRACED || 572 options & WUNTRACED)) { 573 if ((options & WNOWAIT) == 0) 574 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 575 576 *retval = pr->ps_pid; 577 if (info != 0) { 578 info->si_pid = pr->ps_pid; 579 info->si_uid = pr->ps_ucred->cr_uid; 580 info->si_signo = SIGCHLD; 581 info->si_code = CLD_STOPPED; 582 info->si_status = pr->ps_xsig; 583 } 584 585 if (statusp != NULL) 586 *statusp = W_STOPCODE(pr->ps_xsig); 587 if (rusage != NULL) 588 memset(rusage, 0, sizeof(*rusage)); 589 return (0); 590 } 591 if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 592 if ((options & WNOWAIT) == 0) 593 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 594 595 *retval = pr->ps_pid; 596 if (info != NULL) { 597 info->si_pid = pr->ps_pid; 598 info->si_uid = pr->ps_ucred->cr_uid; 599 info->si_signo = SIGCHLD; 600 info->si_code = CLD_CONTINUED; 601 info->si_status = SIGCONT; 602 } 603 604 if (statusp != NULL) 605 *statusp = _WCONTINUED; 606 if (rusage != NULL) 607 memset(rusage, 0, sizeof(*rusage)); 608 return (0); 609 } 610 } 611 /* 612 * Look in the orphans list too, to allow the parent to 613 * collect its child's exit status even if child is being 614 * debugged. 615 * 616 * Debugger detaches from the parent upon successful 617 * switch-over from parent to child. At this point due to 618 * re-parenting the parent loses the child to debugger and a 619 * wait4(2) call would report that it has no children to wait 620 * for. By maintaining a list of orphans we allow the parent 621 * to successfully wait until the child becomes a zombie. 622 */ 623 if (nfound == 0) { 624 LIST_FOREACH(pr, &q->p_p->ps_orphans, ps_orphan) { 625 if ((pr->ps_flags & PS_NOZOMBIE) || 626 (idtype == P_PID && id != pr->ps_pid) || 627 (idtype == P_PGID && id != pr->ps_pgid)) 628 continue; 629 nfound++; 630 break; 631 } 632 } 633 if (nfound == 0) 634 return (ECHILD); 635 if (options & WNOHANG) { 636 *retval = 0; 637 return (0); 638 } 639 if ((error = tsleep_nsec(q->p_p, PWAIT | PCATCH, "wait", INFSLP)) != 0) 640 return (error); 641 goto loop; 642 } 643 644 int 645 sys_wait4(struct proc *q, void *v, register_t *retval) 646 { 647 struct sys_wait4_args /* { 648 syscallarg(pid_t) pid; 649 syscallarg(int *) status; 650 syscallarg(int) options; 651 syscallarg(struct rusage *) rusage; 652 } */ *uap = v; 653 struct rusage ru; 654 pid_t pid = SCARG(uap, pid); 655 int options = SCARG(uap, options); 656 int status, error; 657 idtype_t idtype; 658 id_t id; 659 660 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WCONTINUED)) 661 return (EINVAL); 662 options |= WEXITED | WTRAPPED; 663 664 if (SCARG(uap, pid) == WAIT_MYPGRP) { 665 idtype = P_PGID; 666 id = q->p_p->ps_pgid; 667 } else if (SCARG(uap, pid) == WAIT_ANY) { 668 idtype = P_ALL; 669 id = 0; 670 } else if (pid < 0) { 671 idtype = P_PGID; 672 id = -pid; 673 } else { 674 idtype = P_PID; 675 id = pid; 676 } 677 678 error = dowait6(q, idtype, id, 679 SCARG(uap, status) ? &status : NULL, options, 680 SCARG(uap, rusage) ? &ru : NULL, NULL, retval); 681 if (error == 0 && *retval > 0 && SCARG(uap, status)) { 682 error = copyout(&status, SCARG(uap, status), sizeof(status)); 683 } 684 if (error == 0 && *retval > 0 && SCARG(uap, rusage)) { 685 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 686 #ifdef KTRACE 687 if (error == 0 && KTRPOINT(q, KTR_STRUCT)) 688 ktrrusage(q, &ru); 689 #endif 690 } 691 return (error); 692 } 693 694 int 695 sys_waitid(struct proc *q, void *v, register_t *retval) 696 { 697 struct sys_waitid_args /* { 698 syscallarg(idtype_t) idtype; 699 syscallarg(id_t) id; 700 syscallarg(siginfo_t) info; 701 syscallarg(int) options; 702 } */ *uap = v; 703 siginfo_t info; 704 idtype_t idtype = SCARG(uap, idtype); 705 int options = SCARG(uap, options); 706 int error; 707 708 if (options &~ (WSTOPPED|WCONTINUED|WEXITED|WTRAPPED|WNOHANG|WNOWAIT)) 709 return (EINVAL); 710 if ((options & (WSTOPPED|WCONTINUED|WEXITED|WTRAPPED)) == 0) 711 return (EINVAL); 712 if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID) 713 return (EINVAL); 714 715 error = dowait6(q, idtype, SCARG(uap, id), NULL, 716 options, NULL, &info, retval); 717 if (error == 0) { 718 error = copyout(&info, SCARG(uap, info), sizeof(info)); 719 #ifdef KTRACE 720 if (error == 0 && KTRPOINT(q, KTR_STRUCT)) 721 ktrsiginfo(q, &info); 722 #endif 723 } 724 if (error == 0) 725 *retval = 0; 726 return (error); 727 } 728 729 void 730 proc_finish_wait(struct proc *waiter, struct proc *p) 731 { 732 struct process *pr, *tr; 733 struct rusage *rup; 734 735 /* 736 * If we got the child via a ptrace 'attach', 737 * we need to give it back to the old parent. 738 */ 739 pr = p->p_p; 740 if (pr->ps_oppid != 0 && (pr->ps_oppid != pr->ps_pptr->ps_pid) && 741 (tr = prfind(pr->ps_oppid))) { 742 pr->ps_oppid = 0; 743 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 744 process_reparent(pr, tr); 745 prsignal(tr, SIGCHLD); 746 wakeup(tr); 747 } else { 748 scheduler_wait_hook(waiter, p); 749 rup = &waiter->p_p->ps_cru; 750 ruadd(rup, pr->ps_ru); 751 LIST_REMOVE(pr, ps_list); /* off zombprocess */ 752 freepid(pr->ps_pid); 753 process_zap(pr); 754 } 755 } 756 757 /* 758 * give process back to original parent or init(8) 759 */ 760 void 761 process_untrace(struct process *pr) 762 { 763 struct process *ppr = NULL; 764 765 KASSERT(pr->ps_flags & PS_TRACED); 766 767 if (pr->ps_oppid != 0 && 768 (pr->ps_oppid != pr->ps_pptr->ps_pid)) 769 ppr = prfind(pr->ps_oppid); 770 771 /* not being traced any more */ 772 pr->ps_oppid = 0; 773 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 774 process_reparent(pr, ppr ? ppr : initprocess); 775 } 776 777 void 778 process_clear_orphan(struct process *pr) 779 { 780 if (pr->ps_flags & PS_ORPHAN) { 781 LIST_REMOVE(pr, ps_orphan); 782 atomic_clearbits_int(&pr->ps_flags, PS_ORPHAN); 783 } 784 } 785 786 /* 787 * make process 'parent' the new parent of process 'child'. 788 */ 789 void 790 process_reparent(struct process *child, struct process *parent) 791 { 792 793 if (child->ps_pptr == parent) 794 return; 795 796 KASSERT(child->ps_oppid == 0 || 797 child->ps_oppid == child->ps_pptr->ps_pid); 798 799 LIST_REMOVE(child, ps_sibling); 800 LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling); 801 802 process_clear_orphan(child); 803 if (child->ps_flags & PS_TRACED) { 804 atomic_setbits_int(&child->ps_flags, PS_ORPHAN); 805 LIST_INSERT_HEAD(&child->ps_pptr->ps_orphans, child, ps_orphan); 806 } 807 808 child->ps_pptr = parent; 809 child->ps_ppid = parent->ps_pid; 810 } 811 812 void 813 process_zap(struct process *pr) 814 { 815 struct vnode *otvp; 816 struct proc *p = pr->ps_mainproc; 817 818 /* 819 * Finally finished with old proc entry. 820 * Unlink it from its process group and free it. 821 */ 822 leavepgrp(pr); 823 LIST_REMOVE(pr, ps_sibling); 824 process_clear_orphan(pr); 825 826 /* 827 * Decrement the count of procs running with this uid. 828 */ 829 (void)chgproccnt(pr->ps_ucred->cr_ruid, -1); 830 831 /* 832 * Release reference to text vnode 833 */ 834 otvp = pr->ps_textvp; 835 pr->ps_textvp = NULL; 836 if (otvp) 837 vrele(otvp); 838 839 KASSERT(pr->ps_threadcnt == 0); 840 KASSERT(pr->ps_exitcnt == 1); 841 if (pr->ps_ptstat != NULL) 842 free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat)); 843 pool_put(&rusage_pool, pr->ps_ru); 844 KASSERT(TAILQ_EMPTY(&pr->ps_threads)); 845 sigactsfree(pr->ps_sigacts); 846 lim_free(pr->ps_limit); 847 crfree(pr->ps_ucred); 848 pool_put(&process_pool, pr); 849 nprocesses--; 850 851 proc_free(p); 852 } 853