kern_exit.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /* $OpenBSD: kern_exit.c,v 1.149 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1989, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * (c) UNIX System Laboratories, Inc.
  7. * All or some portions of this file are derived from material licensed
  8. * to the University of California by American Telephone and Telegraph
  9. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  10. * the permission of UNIX System Laboratories, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
  37. */
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/proc.h>
  42. #include <sys/tty.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #include <sys/kernel.h>
  46. #include <sys/sysctl.h>
  47. #include <sys/wait.h>
  48. #include <sys/file.h>
  49. #include <sys/vnode.h>
  50. #include <sys/syslog.h>
  51. #include <sys/malloc.h>
  52. #include <sys/resourcevar.h>
  53. #include <sys/ptrace.h>
  54. #include <sys/acct.h>
  55. #include <sys/filedesc.h>
  56. #include <sys/signalvar.h>
  57. #include <sys/sched.h>
  58. #include <sys/ktrace.h>
  59. #include <sys/pool.h>
  60. #include <sys/mutex.h>
  61. #ifdef SYSVSEM
  62. #include <sys/sem.h>
  63. #endif
  64. #include "systrace.h"
  65. #include <dev/systrace.h>
  66. #include <sys/mount.h>
  67. #include <sys/syscallargs.h>
  68. #include <uvm/uvm_extern.h>
  69. /*
  70. * exit --
  71. * Death of process.
  72. */
  73. int
  74. sys_exit(struct proc *p, void *v, register_t *retval)
  75. {
  76. struct sys_exit_args /* {
  77. syscallarg(int) rval;
  78. } */ *uap = v;
  79. exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL);
  80. /* NOTREACHED */
  81. return (0);
  82. }
  83. int
  84. sys___threxit(struct proc *p, void *v, register_t *retval)
  85. {
  86. struct sys___threxit_args /* {
  87. syscallarg(pid_t *) notdead;
  88. } */ *uap = v;
  89. if (SCARG(uap, notdead) != NULL) {
  90. pid_t zero = 0;
  91. if (copyout(&zero, SCARG(uap, notdead), sizeof(zero)))
  92. psignal(p, SIGSEGV);
  93. }
  94. exit1(p, 0, EXIT_THREAD);
  95. return (0);
  96. }
  97. /*
  98. * Exit: deallocate address space and other resources, change proc state
  99. * to zombie, and unlink proc from allproc and parent's lists. Save exit
  100. * status and rusage for wait(). Check for child processes and orphan them.
  101. */
  102. void
  103. exit1(struct proc *p, int rv, int flags)
  104. {
  105. struct process *pr, *qr, *nqr;
  106. struct rusage *rup;
  107. struct vnode *ovp;
  108. atomic_setbits_int(&p->p_flag, P_WEXIT);
  109. pr = p->p_p;
  110. /* single-threaded? */
  111. if (TAILQ_FIRST(&pr->ps_threads) == p &&
  112. TAILQ_NEXT(p, p_thr_link) == NULL) {
  113. flags = EXIT_NORMAL;
  114. } else {
  115. /* nope, multi-threaded */
  116. if (flags == EXIT_NORMAL)
  117. single_thread_set(p, SINGLE_EXIT, 0);
  118. else if (flags == EXIT_THREAD)
  119. single_thread_check(p, 0);
  120. }
  121. if (flags == EXIT_NORMAL) {
  122. if (pr->ps_pid == 1)
  123. panic("init died (signal %d, exit %d)",
  124. WTERMSIG(rv), WEXITSTATUS(rv));
  125. atomic_setbits_int(&pr->ps_flags, PS_EXITING);
  126. pr->ps_mainproc->p_xstat = rv;
  127. /*
  128. * If parent is waiting for us to exit or exec, PS_PPWAIT
  129. * is set; we wake up the parent early to avoid deadlock.
  130. */
  131. if (pr->ps_flags & PS_PPWAIT) {
  132. atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
  133. atomic_clearbits_int(&pr->ps_pptr->ps_flags,
  134. PS_ISPWAIT);
  135. wakeup(pr->ps_pptr);
  136. }
  137. }
  138. /* unlink ourselves from the active threads */
  139. TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link);
  140. if ((p->p_flag & P_THREAD) == 0) {
  141. /* main thread gotta wait because it has the pid, et al */
  142. while (pr->ps_refcnt > 1)
  143. tsleep(&pr->ps_threads, PUSER, "thrdeath", 0);
  144. if (pr->ps_flags & PS_PROFIL)
  145. stopprofclock(pr);
  146. }
  147. rup = pr->ps_ru;
  148. if (rup == NULL) {
  149. rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO);
  150. if (pr->ps_ru == NULL) {
  151. pr->ps_ru = rup;
  152. } else {
  153. pool_put(&rusage_pool, rup);
  154. rup = pr->ps_ru;
  155. }
  156. }
  157. p->p_siglist = 0;
  158. if ((p->p_flag & P_THREAD) == 0) {
  159. /* close open files and release open-file table */
  160. fdfree(p);
  161. timeout_del(&pr->ps_realit_to);
  162. #ifdef SYSVSEM
  163. semexit(pr);
  164. #endif
  165. if (SESS_LEADER(pr)) {
  166. struct session *sp = pr->ps_session;
  167. if (sp->s_ttyvp) {
  168. /*
  169. * Controlling process.
  170. * Signal foreground pgrp,
  171. * drain controlling terminal
  172. * and revoke access to controlling terminal.
  173. */
  174. if (sp->s_ttyp->t_session == sp) {
  175. if (sp->s_ttyp->t_pgrp)
  176. pgsignal(sp->s_ttyp->t_pgrp,
  177. SIGHUP, 1);
  178. ttywait(sp->s_ttyp);
  179. /*
  180. * The tty could have been revoked
  181. * if we blocked.
  182. */
  183. if (sp->s_ttyvp)
  184. VOP_REVOKE(sp->s_ttyvp,
  185. REVOKEALL);
  186. }
  187. ovp = sp->s_ttyvp;
  188. sp->s_ttyvp = NULL;
  189. if (ovp)
  190. vrele(ovp);
  191. /*
  192. * s_ttyp is not zero'd; we use this to
  193. * indicate that the session once had a
  194. * controlling terminal. (for logging and
  195. * informational purposes)
  196. */
  197. }
  198. sp->s_leader = NULL;
  199. }
  200. fixjobc(pr, pr->ps_pgrp, 0);
  201. #ifdef ACCOUNTING
  202. acct_process(p);
  203. #endif
  204. #ifdef KTRACE
  205. /* release trace file */
  206. if (pr->ps_tracevp)
  207. ktrcleartrace(pr);
  208. #endif
  209. /*
  210. * If parent has the SAS_NOCLDWAIT flag set, we're not
  211. * going to become a zombie.
  212. */
  213. if (pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDWAIT)
  214. atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
  215. }
  216. p->p_fd = NULL; /* zap the thread's copy */
  217. #if NSYSTRACE > 0
  218. if (ISSET(p->p_flag, P_SYSTRACE))
  219. systrace_exit(p);
  220. #endif
  221. /*
  222. * If emulation has thread exit hook, call it now.
  223. */
  224. if (pr->ps_emul->e_proc_exit)
  225. (*pr->ps_emul->e_proc_exit)(p);
  226. /*
  227. * Remove proc from pidhash chain and allproc so looking
  228. * it up won't work. We will put the proc on the
  229. * deadproc list later (using the p_hash member), and
  230. * wake up the reaper when we do. If this is the last
  231. * thread of a process that isn't PS_NOZOMBIE, we'll put
  232. * the process on the zombprocess list below.
  233. */
  234. /*
  235. * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
  236. */
  237. p->p_stat = SDEAD;
  238. LIST_REMOVE(p, p_hash);
  239. LIST_REMOVE(p, p_list);
  240. if ((p->p_flag & P_THREAD) == 0) {
  241. LIST_REMOVE(pr, ps_list);
  242. if ((pr->ps_flags & PS_NOZOMBIE) == 0)
  243. LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
  244. else {
  245. /*
  246. * Not going to be a zombie, so it's now off all
  247. * the lists scanned by ispidtaken(), so block
  248. * fast reuse of the pid now.
  249. */
  250. freepid(p->p_pid);
  251. }
  252. /*
  253. * Give orphaned children to init(8).
  254. */
  255. qr = LIST_FIRST(&pr->ps_children);
  256. if (qr) /* only need this if any child is S_ZOMB */
  257. wakeup(initprocess);
  258. for (; qr != 0; qr = nqr) {
  259. nqr = LIST_NEXT(qr, ps_sibling);
  260. proc_reparent(qr, initprocess);
  261. /*
  262. * Traced processes are killed since their
  263. * existence means someone is screwing up.
  264. */
  265. if (qr->ps_flags & PS_TRACED &&
  266. !(qr->ps_flags & PS_EXITING)) {
  267. atomic_clearbits_int(&qr->ps_flags, PS_TRACED);
  268. /*
  269. * If single threading is active,
  270. * direct the signal to the active
  271. * thread to avoid deadlock.
  272. */
  273. if (qr->ps_single)
  274. ptsignal(qr->ps_single, SIGKILL,
  275. STHREAD);
  276. else
  277. prsignal(qr, SIGKILL);
  278. }
  279. }
  280. }
  281. /* add thread's accumulated rusage into the process's total */
  282. ruadd(rup, &p->p_ru);
  283. tuagg(pr, p);
  284. /*
  285. * clear %cpu usage during swap
  286. */
  287. p->p_pctcpu = 0;
  288. if ((p->p_flag & P_THREAD) == 0) {
  289. /*
  290. * Final thread has died, so add on our children's rusage
  291. * and calculate the total times
  292. */
  293. calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
  294. ruadd(rup, &pr->ps_cru);
  295. /* notify interested parties of our demise and clean up */
  296. knote_processexit(p);
  297. /*
  298. * Notify parent that we're gone. If we're not going to
  299. * become a zombie, reparent to process 1 (init) so that
  300. * we can wake our original parent to possibly unblock
  301. * wait4() to return ECHILD.
  302. */
  303. if (pr->ps_flags & PS_NOZOMBIE) {
  304. struct process *ppr = pr->ps_pptr;
  305. proc_reparent(pr, initprocess);
  306. wakeup(ppr);
  307. }
  308. /*
  309. * Release the process's signal state.
  310. */
  311. sigactsfree(pr);
  312. }
  313. /* just a thread? detach it from its process */
  314. if (p->p_flag & P_THREAD) {
  315. /* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
  316. if (--pr->ps_refcnt == 1)
  317. wakeup(&pr->ps_threads);
  318. KASSERT(pr->ps_refcnt > 0);
  319. }
  320. /*
  321. * Other substructures are freed from reaper and wait().
  322. */
  323. /*
  324. * Finally, call machine-dependent code to switch to a new
  325. * context (possibly the idle context). Once we are no longer
  326. * using the dead process's vmspace and stack, exit2() will be
  327. * called to schedule those resources to be released by the
  328. * reaper thread.
  329. *
  330. * Note that cpu_exit() will end with a call equivalent to
  331. * cpu_switch(), finishing our execution (pun intended).
  332. */
  333. uvmexp.swtch++;
  334. cpu_exit(p);
  335. panic("cpu_exit returned");
  336. }
  337. /*
  338. * Locking of this proclist is special; it's accessed in a
  339. * critical section of process exit, and thus locking it can't
  340. * modify interrupt state. We use a simple spin lock for this
  341. * proclist. We use the p_hash member to linkup to deadproc.
  342. */
  343. struct mutex deadproc_mutex = MUTEX_INITIALIZER(IPL_NONE);
  344. struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
  345. /*
  346. * We are called from cpu_exit() once it is safe to schedule the
  347. * dead process's resources to be freed.
  348. *
  349. * NOTE: One must be careful with locking in this routine. It's
  350. * called from a critical section in machine-dependent code, so
  351. * we should refrain from changing any interrupt state.
  352. *
  353. * We lock the deadproc list, place the proc on that list (using
  354. * the p_hash member), and wake up the reaper.
  355. */
  356. void
  357. exit2(struct proc *p)
  358. {
  359. mtx_enter(&deadproc_mutex);
  360. LIST_INSERT_HEAD(&deadproc, p, p_hash);
  361. mtx_leave(&deadproc_mutex);
  362. wakeup(&deadproc);
  363. }
  364. void
  365. proc_free(struct proc *p)
  366. {
  367. crfree(p->p_ucred);
  368. pool_put(&proc_pool, p);
  369. nthreads--;
  370. }
  371. /*
  372. * Process reaper. This is run by a kernel thread to free the resources
  373. * of a dead process. Once the resources are free, the process becomes
  374. * a zombie, and the parent is allowed to read the undead's status.
  375. */
  376. void
  377. reaper(void)
  378. {
  379. struct proc *p;
  380. KERNEL_UNLOCK();
  381. SCHED_ASSERT_UNLOCKED();
  382. for (;;) {
  383. mtx_enter(&deadproc_mutex);
  384. while ((p = LIST_FIRST(&deadproc)) == NULL)
  385. msleep(&deadproc, &deadproc_mutex, PVM, "reaper", 0);
  386. /* Remove us from the deadproc list. */
  387. LIST_REMOVE(p, p_hash);
  388. mtx_leave(&deadproc_mutex);
  389. KERNEL_LOCK();
  390. /*
  391. * Free the VM resources we're still holding on to.
  392. * We must do this from a valid thread because doing
  393. * so may block.
  394. */
  395. uvm_uarea_free(p);
  396. p->p_vmspace = NULL; /* zap the thread's copy */
  397. if (p->p_flag & P_THREAD) {
  398. /* Just a thread */
  399. proc_free(p);
  400. } else {
  401. struct process *pr = p->p_p;
  402. /* Release the rest of the process's vmspace */
  403. uvm_exit(pr);
  404. if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
  405. /* Process is now a true zombie. */
  406. atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
  407. prsignal(pr->ps_pptr, SIGCHLD);
  408. /* Wake up the parent so it can get exit status. */
  409. wakeup(pr->ps_pptr);
  410. } else {
  411. /* No one will wait for us. Just zap the process now */
  412. process_zap(pr);
  413. }
  414. }
  415. KERNEL_UNLOCK();
  416. }
  417. }
  418. int
  419. sys_wait4(struct proc *q, void *v, register_t *retval)
  420. {
  421. struct sys_wait4_args /* {
  422. syscallarg(pid_t) pid;
  423. syscallarg(int *) status;
  424. syscallarg(int) options;
  425. syscallarg(struct rusage *) rusage;
  426. } */ *uap = v;
  427. struct rusage ru;
  428. int status, error;
  429. error = dowait4(q, SCARG(uap, pid),
  430. SCARG(uap, status) ? &status : NULL,
  431. SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval);
  432. if (error == 0 && retval[0] > 0 && SCARG(uap, status)) {
  433. error = copyout(&status, SCARG(uap, status), sizeof(status));
  434. }
  435. if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) {
  436. error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
  437. #ifdef KTRACE
  438. if (error == 0 && KTRPOINT(q, KTR_STRUCT))
  439. ktrrusage(q, &ru);
  440. #endif
  441. }
  442. return (error);
  443. }
  444. int
  445. dowait4(struct proc *q, pid_t pid, int *statusp, int options,
  446. struct rusage *rusage, register_t *retval)
  447. {
  448. int nfound;
  449. struct process *pr;
  450. struct proc *p;
  451. int error;
  452. if (pid == 0)
  453. pid = -q->p_p->ps_pgid;
  454. if (options &~ (WUNTRACED|WNOHANG|WCONTINUED))
  455. return (EINVAL);
  456. loop:
  457. nfound = 0;
  458. LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) {
  459. p = pr->ps_mainproc;
  460. if ((pr->ps_flags & PS_NOZOMBIE) ||
  461. (pid != WAIT_ANY &&
  462. p->p_pid != pid &&
  463. pr->ps_pgid != -pid))
  464. continue;
  465. nfound++;
  466. if (pr->ps_flags & PS_ZOMBIE) {
  467. retval[0] = p->p_pid;
  468. if (statusp != NULL)
  469. *statusp = p->p_xstat; /* convert to int */
  470. if (rusage != NULL)
  471. memcpy(rusage, pr->ps_ru, sizeof(*rusage));
  472. proc_finish_wait(q, p);
  473. return (0);
  474. }
  475. if (pr->ps_flags & PS_TRACED &&
  476. (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single &&
  477. pr->ps_single->p_stat == SSTOP &&
  478. (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) {
  479. single_thread_wait(pr);
  480. atomic_setbits_int(&pr->ps_flags, PS_WAITED);
  481. retval[0] = p->p_pid;
  482. if (statusp != NULL)
  483. *statusp = W_STOPCODE(pr->ps_single->p_xstat);
  484. if (rusage != NULL)
  485. memset(rusage, 0, sizeof(*rusage));
  486. return (0);
  487. }
  488. if (p->p_stat == SSTOP &&
  489. (pr->ps_flags & PS_WAITED) == 0 &&
  490. (p->p_flag & P_SUSPSINGLE) == 0 &&
  491. (pr->ps_flags & PS_TRACED ||
  492. options & WUNTRACED)) {
  493. atomic_setbits_int(&pr->ps_flags, PS_WAITED);
  494. retval[0] = p->p_pid;
  495. if (statusp != NULL)
  496. *statusp = W_STOPCODE(p->p_xstat);
  497. if (rusage != NULL)
  498. memset(rusage, 0, sizeof(*rusage));
  499. return (0);
  500. }
  501. if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
  502. atomic_clearbits_int(&p->p_flag, P_CONTINUED);
  503. retval[0] = p->p_pid;
  504. if (statusp != NULL)
  505. *statusp = _WCONTINUED;
  506. if (rusage != NULL)
  507. memset(rusage, 0, sizeof(*rusage));
  508. return (0);
  509. }
  510. }
  511. if (nfound == 0)
  512. return (ECHILD);
  513. if (options & WNOHANG) {
  514. retval[0] = 0;
  515. return (0);
  516. }
  517. if ((error = tsleep(q->p_p, PWAIT | PCATCH, "wait", 0)) != 0)
  518. return (error);
  519. goto loop;
  520. }
  521. void
  522. proc_finish_wait(struct proc *waiter, struct proc *p)
  523. {
  524. struct process *pr, *tr;
  525. struct rusage *rup;
  526. /*
  527. * If we got the child via a ptrace 'attach',
  528. * we need to give it back to the old parent.
  529. */
  530. pr = p->p_p;
  531. if (pr->ps_oppid && (tr = prfind(pr->ps_oppid))) {
  532. atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
  533. pr->ps_oppid = 0;
  534. proc_reparent(pr, tr);
  535. prsignal(tr, SIGCHLD);
  536. wakeup(tr);
  537. } else {
  538. scheduler_wait_hook(waiter, p);
  539. p->p_xstat = 0;
  540. rup = &waiter->p_p->ps_cru;
  541. ruadd(rup, pr->ps_ru);
  542. LIST_REMOVE(pr, ps_list); /* off zombprocess */
  543. freepid(p->p_pid);
  544. process_zap(pr);
  545. }
  546. }
  547. /*
  548. * make process 'parent' the new parent of process 'child'.
  549. */
  550. void
  551. proc_reparent(struct process *child, struct process *parent)
  552. {
  553. if (child->ps_pptr == parent)
  554. return;
  555. LIST_REMOVE(child, ps_sibling);
  556. LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling);
  557. child->ps_pptr = parent;
  558. }
  559. void
  560. process_zap(struct process *pr)
  561. {
  562. struct vnode *otvp;
  563. struct proc *p = pr->ps_mainproc;
  564. /*
  565. * Finally finished with old proc entry.
  566. * Unlink it from its process group and free it.
  567. */
  568. leavepgrp(pr);
  569. LIST_REMOVE(pr, ps_sibling);
  570. /*
  571. * Decrement the count of procs running with this uid.
  572. */
  573. (void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
  574. /*
  575. * Release reference to text vnode
  576. */
  577. otvp = pr->ps_textvp;
  578. pr->ps_textvp = NULL;
  579. if (otvp)
  580. vrele(otvp);
  581. KASSERT(pr->ps_refcnt == 1);
  582. if (pr->ps_ptstat != NULL)
  583. free(pr->ps_ptstat, M_SUBPROC, 0);
  584. pool_put(&rusage_pool, pr->ps_ru);
  585. KASSERT(TAILQ_EMPTY(&pr->ps_threads));
  586. limfree(pr->ps_limit);
  587. crfree(pr->ps_ucred);
  588. pool_put(&process_pool, pr);
  589. nprocesses--;
  590. proc_free(p);
  591. }