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