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