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