1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 46d2d3e875SBruce Evans #include <sys/sysproto.h> 47df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 48df8bae1dSRodney W. Grimes #include <sys/kernel.h> 49c76e95c3SPeter Wemm #include <sys/sysctl.h> 5019284646SJohn Baldwin #include <sys/lock.h> 51df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5235e0e5b3SJohn Baldwin #include <sys/mutex.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 55a7b124c3SJohn Baldwin #include <sys/syscall.h> 56df8bae1dSRodney W. Grimes #include <sys/vnode.h> 57df8bae1dSRodney W. Grimes #include <sys/acct.h> 580384fff8SJason Evans #include <sys/ktr.h> 59df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 60a7b124c3SJohn Baldwin #include <sys/kthread.h> 61b71fec07SBruce Evans #include <sys/unistd.h> 6275c13541SPoul-Henning Kamp #include <sys/jail.h> 6357934cd3SJohn Baldwin #include <sys/sx.h> 64df8bae1dSRodney W. Grimes 65d93f860cSPoul-Henning Kamp #include <vm/vm.h> 66dabee6feSPeter Wemm #include <vm/pmap.h> 67dabee6feSPeter Wemm #include <vm/vm_map.h> 68efeaf95aSDavid Greenman #include <vm/vm_extern.h> 692d8acc0fSJohn Dyson #include <vm/vm_zone.h> 70d93f860cSPoul-Henning Kamp 715d22597fSHajimu UMEMOTO #include <sys/vmmeter.h> 72dc9c271aSJulian Elischer #include <sys/user.h> 7388c5ea45SJulian Elischer 7493efcae8SPoul-Henning Kamp static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback"); 7593efcae8SPoul-Henning Kamp 76fed06968SJulian Elischer /* 77e0d898b4SJulian Elischer * These are the stuctures used to create a callout list for things to do 78e0d898b4SJulian Elischer * when forking a process 79fed06968SJulian Elischer */ 8093efcae8SPoul-Henning Kamp struct forklist { 81fed06968SJulian Elischer forklist_fn function; 82e3975643SJake Burkholder TAILQ_ENTRY(forklist) next; 8393efcae8SPoul-Henning Kamp }; 84fed06968SJulian Elischer 8557934cd3SJohn Baldwin static struct sx fork_list_lock; 8657934cd3SJohn Baldwin 87e3975643SJake Burkholder TAILQ_HEAD(forklist_head, forklist); 8893efcae8SPoul-Henning Kamp static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list); 89fed06968SJulian Elischer 90d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 91ad7507e2SSteven Wallace struct fork_args { 92ad7507e2SSteven Wallace int dummy; 93ad7507e2SSteven Wallace }; 94d2d3e875SBruce Evans #endif 95ad7507e2SSteven Wallace 9657934cd3SJohn Baldwin static void 9757934cd3SJohn Baldwin init_fork_list(void *data __unused) 9857934cd3SJohn Baldwin { 9957934cd3SJohn Baldwin 10057934cd3SJohn Baldwin sx_init(&fork_list_lock, "fork list"); 10157934cd3SJohn Baldwin } 10257934cd3SJohn Baldwin SYSINIT(fork_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_fork_list, NULL); 10357934cd3SJohn Baldwin 104116734c4SMatthew Dillon /* 105116734c4SMatthew Dillon * MPSAFE 106116734c4SMatthew Dillon */ 107df8bae1dSRodney W. Grimes /* ARGSUSED */ 10826f9a767SRodney W. Grimes int 109b40ce416SJulian Elischer fork(td, uap) 110b40ce416SJulian Elischer struct thread *td; 111df8bae1dSRodney W. Grimes struct fork_args *uap; 112df8bae1dSRodney W. Grimes { 113df8abd0bSPeter Wemm int error; 114df8abd0bSPeter Wemm struct proc *p2; 115be67169aSBruce Evans 116116734c4SMatthew Dillon mtx_lock(&Giant); 117b40ce416SJulian Elischer error = fork1(td, RFFDG | RFPROC, &p2); 118df8abd0bSPeter Wemm if (error == 0) { 119b40ce416SJulian Elischer td->td_retval[0] = p2->p_pid; 120b40ce416SJulian Elischer td->td_retval[1] = 0; 121df8abd0bSPeter Wemm } 122116734c4SMatthew Dillon mtx_unlock(&Giant); 123df8abd0bSPeter Wemm return error; 124df8bae1dSRodney W. Grimes } 125df8bae1dSRodney W. Grimes 126116734c4SMatthew Dillon /* 127116734c4SMatthew Dillon * MPSAFE 128116734c4SMatthew Dillon */ 129df8bae1dSRodney W. Grimes /* ARGSUSED */ 13026f9a767SRodney W. Grimes int 131b40ce416SJulian Elischer vfork(td, uap) 132b40ce416SJulian Elischer struct thread *td; 133dabee6feSPeter Wemm struct vfork_args *uap; 134df8bae1dSRodney W. Grimes { 135df8abd0bSPeter Wemm int error; 136df8abd0bSPeter Wemm struct proc *p2; 137be67169aSBruce Evans 138116734c4SMatthew Dillon mtx_lock(&Giant); 139b40ce416SJulian Elischer error = fork1(td, RFFDG | RFPROC | RFPPWAIT | RFMEM, &p2); 140df8abd0bSPeter Wemm if (error == 0) { 141b40ce416SJulian Elischer td->td_retval[0] = p2->p_pid; 142b40ce416SJulian Elischer td->td_retval[1] = 0; 143df8abd0bSPeter Wemm } 144116734c4SMatthew Dillon mtx_unlock(&Giant); 145df8abd0bSPeter Wemm return error; 146df8bae1dSRodney W. Grimes } 147df8bae1dSRodney W. Grimes 148116734c4SMatthew Dillon /* 149116734c4SMatthew Dillon * MPSAFE 150116734c4SMatthew Dillon */ 151dabee6feSPeter Wemm int 152b40ce416SJulian Elischer rfork(td, uap) 153b40ce416SJulian Elischer struct thread *td; 154dabee6feSPeter Wemm struct rfork_args *uap; 155dabee6feSPeter Wemm { 156df8abd0bSPeter Wemm int error; 157df8abd0bSPeter Wemm struct proc *p2; 158be67169aSBruce Evans 159885ccc61SJohn Baldwin /* Don't allow kernel only flags. */ 160885ccc61SJohn Baldwin if ((uap->flags & RFKERNELONLY) != 0) 161885ccc61SJohn Baldwin return (EINVAL); 162116734c4SMatthew Dillon mtx_lock(&Giant); 163885ccc61SJohn Baldwin error = fork1(td, uap->flags, &p2); 164df8abd0bSPeter Wemm if (error == 0) { 165b40ce416SJulian Elischer td->td_retval[0] = p2 ? p2->p_pid : 0; 166b40ce416SJulian Elischer td->td_retval[1] = 0; 167df8abd0bSPeter Wemm } 168116734c4SMatthew Dillon mtx_unlock(&Giant); 169df8abd0bSPeter Wemm return error; 170dabee6feSPeter Wemm } 171dabee6feSPeter Wemm 172dabee6feSPeter Wemm 173df8bae1dSRodney W. Grimes int nprocs = 1; /* process 0 */ 1748f7e4eb5SDag-Erling Smørgrav int lastpid = 0; 1758f7e4eb5SDag-Erling Smørgrav SYSCTL_INT(_kern, OID_AUTO, lastpid, CTLFLAG_RD, &lastpid, 0, 176d941d475SRobert Watson "Last used PID"); 177df8bae1dSRodney W. Grimes 178bb6a234eSPeter Wemm /* 1798f7e4eb5SDag-Erling Smørgrav * Random component to lastpid generation. We mix in a random factor to make 180bb6a234eSPeter Wemm * it a little harder to predict. We sanity check the modulus value to avoid 181bb6a234eSPeter Wemm * doing it in critical paths. Don't let it be too small or we pointlessly 182bb6a234eSPeter Wemm * waste randomness entropy, and don't let it be impossibly large. Using a 183bb6a234eSPeter Wemm * modulus that is too big causes a LOT more process table scans and slows 184bb6a234eSPeter Wemm * down fork processing as the pidchecked caching is defeated. 185bb6a234eSPeter Wemm */ 186ee3fd601SDan Moschuk static int randompid = 0; 187bb6a234eSPeter Wemm 188bb6a234eSPeter Wemm static int 18982d9ae4eSPoul-Henning Kamp sysctl_kern_randompid(SYSCTL_HANDLER_ARGS) 190bb6a234eSPeter Wemm { 191bb6a234eSPeter Wemm int error, pid; 192bb6a234eSPeter Wemm 193bb6a234eSPeter Wemm pid = randompid; 194bb6a234eSPeter Wemm error = sysctl_handle_int(oidp, &pid, 0, req); 195bb6a234eSPeter Wemm if (error || !req->newptr) 196bb6a234eSPeter Wemm return (error); 197bb6a234eSPeter Wemm if (pid < 0 || pid > PID_MAX - 100) /* out of range */ 198bb6a234eSPeter Wemm pid = PID_MAX - 100; 199bb6a234eSPeter Wemm else if (pid < 2) /* NOP */ 200bb6a234eSPeter Wemm pid = 0; 201bb6a234eSPeter Wemm else if (pid < 100) /* Make it reasonable */ 202bb6a234eSPeter Wemm pid = 100; 203bb6a234eSPeter Wemm randompid = pid; 204bb6a234eSPeter Wemm return (error); 205bb6a234eSPeter Wemm } 206bb6a234eSPeter Wemm 207bb6a234eSPeter Wemm SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW, 208bb6a234eSPeter Wemm 0, 0, sysctl_kern_randompid, "I", "Random PID modulus"); 209ee3fd601SDan Moschuk 210b40ce416SJulian Elischer #if 0 211b40ce416SJulian Elischer void 212b40ce416SJulian Elischer kse_init(struct kse *kse1, struct kse *kse2) 213b40ce416SJulian Elischer { 214b40ce416SJulian Elischer } 215b40ce416SJulian Elischer 216b40ce416SJulian Elischer void 217b40ce416SJulian Elischer thread_init(struct thread *thread1, struct thread *thread2) 218b40ce416SJulian Elischer { 219b40ce416SJulian Elischer } 220b40ce416SJulian Elischer 221b40ce416SJulian Elischer void 222b40ce416SJulian Elischer ksegrp_init(struct ksegrp *ksegrp1, struct ksegrp *ksegrp2) 223b40ce416SJulian Elischer { 224b40ce416SJulian Elischer } 225b40ce416SJulian Elischer #endif 226b40ce416SJulian Elischer 22774b2192aSJohn Dyson int 228b40ce416SJulian Elischer fork1(td, flags, procp) 229b40ce416SJulian Elischer struct thread *td; /* parent proc */ 2300e3eb7eeSSujal Patel int flags; 2310384fff8SJason Evans struct proc **procp; /* child proc */ 232df8bae1dSRodney W. Grimes { 233df8abd0bSPeter Wemm struct proc *p2, *pptr; 234df8abd0bSPeter Wemm uid_t uid; 235df8bae1dSRodney W. Grimes struct proc *newproc; 2360384fff8SJason Evans int trypid; 237c6362551SAlfred Perlstein int ok; 23851068190SWolfram Schneider static int pidchecked = 0; 23993efcae8SPoul-Henning Kamp struct forklist *ep; 2405641ae5dSJohn Baldwin struct filedesc *fd; 241b40ce416SJulian Elischer struct proc *p1 = td->td_proc; 2425856e12eSJohn Dyson 2430cddd8f0SMatthew Dillon GIANT_REQUIRED; 2440cddd8f0SMatthew Dillon 2450384fff8SJason Evans /* Can't copy and clear */ 2460e3eb7eeSSujal Patel if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG)) 247dabee6feSPeter Wemm return (EINVAL); 248df8bae1dSRodney W. Grimes 249df8bae1dSRodney W. Grimes /* 2505856e12eSJohn Dyson * Here we don't create a new process, but we divorce 2515856e12eSJohn Dyson * certain parts of a process from itself. 2525856e12eSJohn Dyson */ 2535856e12eSJohn Dyson if ((flags & RFPROC) == 0) { 254b40ce416SJulian Elischer vm_forkproc(td, 0, flags); 2555856e12eSJohn Dyson 2565856e12eSJohn Dyson /* 2575856e12eSJohn Dyson * Close all file descriptors. 2585856e12eSJohn Dyson */ 2595856e12eSJohn Dyson if (flags & RFCFDG) { 2605856e12eSJohn Dyson struct filedesc *fdtmp; 261b40ce416SJulian Elischer fdtmp = fdinit(td); /* XXXKSE */ 2625641ae5dSJohn Baldwin PROC_LOCK(p1); 263b40ce416SJulian Elischer fdfree(td); /* XXXKSE */ 2645856e12eSJohn Dyson p1->p_fd = fdtmp; 2655641ae5dSJohn Baldwin PROC_UNLOCK(p1); 2665856e12eSJohn Dyson } 2675856e12eSJohn Dyson 2685856e12eSJohn Dyson /* 2695856e12eSJohn Dyson * Unshare file descriptors (from parent.) 2705856e12eSJohn Dyson */ 2715856e12eSJohn Dyson if (flags & RFFDG) { 272426da3bcSAlfred Perlstein FILEDESC_LOCK(p1->p_fd); 2735856e12eSJohn Dyson if (p1->p_fd->fd_refcnt > 1) { 2745856e12eSJohn Dyson struct filedesc *newfd; 275426da3bcSAlfred Perlstein 276b40ce416SJulian Elischer newfd = fdcopy(td); 277426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p1->p_fd); 2785641ae5dSJohn Baldwin PROC_LOCK(p1); 279b40ce416SJulian Elischer fdfree(td); 2805856e12eSJohn Dyson p1->p_fd = newfd; 2815641ae5dSJohn Baldwin PROC_UNLOCK(p1); 282426da3bcSAlfred Perlstein } else 283426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p1->p_fd); 2845856e12eSJohn Dyson } 2851943af61SPeter Wemm *procp = NULL; 2865856e12eSJohn Dyson return (0); 2875856e12eSJohn Dyson } 2885856e12eSJohn Dyson 2895856e12eSJohn Dyson /* 290df8bae1dSRodney W. Grimes * Although process entries are dynamically created, we still keep 291df8bae1dSRodney W. Grimes * a global limit on the maximum number we will create. Don't allow 292df8bae1dSRodney W. Grimes * a nonprivileged user to use the last process; don't let root 293df8bae1dSRodney W. Grimes * exceed the limit. The variable nprocs is the current number of 294df8bae1dSRodney W. Grimes * processes, maxproc is the limit. 295df8bae1dSRodney W. Grimes */ 296b1fc0ec1SRobert Watson uid = p1->p_ucred->cr_ruid; 297df8bae1dSRodney W. Grimes if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { 298df8bae1dSRodney W. Grimes tablefull("proc"); 299df8bae1dSRodney W. Grimes return (EAGAIN); 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes /* 302ef5dc8a9SJohn Dyson * Increment the nprocs resource before blocking can occur. There 303ef5dc8a9SJohn Dyson * are hard-limits as to the number of processes that can run. 304ef5dc8a9SJohn Dyson */ 305ef5dc8a9SJohn Dyson nprocs++; 306ef5dc8a9SJohn Dyson 307ef5dc8a9SJohn Dyson /* 308df8bae1dSRodney W. Grimes * Increment the count of procs running with this uid. Don't allow 309df8bae1dSRodney W. Grimes * a nonprivileged user to exceed their current limit. 310df8bae1dSRodney W. Grimes */ 311b1fc0ec1SRobert Watson ok = chgproccnt(p1->p_ucred->cr_ruidinfo, 1, 31242fd51ceSDon Lewis (uid != 0) ? p1->p_rlimit[RLIMIT_NPROC].rlim_cur : 0); 31342fd51ceSDon Lewis if (!ok) { 314ef5dc8a9SJohn Dyson /* 315ef5dc8a9SJohn Dyson * Back out the process count 316ef5dc8a9SJohn Dyson */ 317ef5dc8a9SJohn Dyson nprocs--; 318df8bae1dSRodney W. Grimes return (EAGAIN); 319df8bae1dSRodney W. Grimes } 320df8bae1dSRodney W. Grimes 321df8bae1dSRodney W. Grimes /* Allocate new proc. */ 3222d8acc0fSJohn Dyson newproc = zalloc(proc_zone); 323df8bae1dSRodney W. Grimes 324df8bae1dSRodney W. Grimes /* 3252c1011f7SJohn Dyson * Setup linkage for kernel based threading 3262c1011f7SJohn Dyson */ 3272c1011f7SJohn Dyson if((flags & RFTHREAD) != 0) { 3282c1011f7SJohn Dyson newproc->p_peers = p1->p_peers; 3292c1011f7SJohn Dyson p1->p_peers = newproc; 3302c1011f7SJohn Dyson newproc->p_leader = p1->p_leader; 3312c1011f7SJohn Dyson } else { 332a7b124c3SJohn Baldwin newproc->p_peers = NULL; 3332c1011f7SJohn Dyson newproc->p_leader = newproc; 3342c1011f7SJohn Dyson } 3352c1011f7SJohn Dyson 336d4da2dbaSAlan Cox newproc->p_vmspace = NULL; 337d4da2dbaSAlan Cox 3382c1011f7SJohn Dyson /* 339df8bae1dSRodney W. Grimes * Find an unused process ID. We remember a range of unused IDs 3408f7e4eb5SDag-Erling Smørgrav * ready to use (from lastpid+1 through pidchecked-1). 3410384fff8SJason Evans * 3420384fff8SJason Evans * If RFHIGHPID is set (used during system boot), do not allocate 3430384fff8SJason Evans * low-numbered pids. 344df8bae1dSRodney W. Grimes */ 3451005a129SJohn Baldwin sx_xlock(&allproc_lock); 3468f7e4eb5SDag-Erling Smørgrav trypid = lastpid + 1; 3470384fff8SJason Evans if (flags & RFHIGHPID) { 3480384fff8SJason Evans if (trypid < 10) { 3490384fff8SJason Evans trypid = 10; 3500384fff8SJason Evans } 3510384fff8SJason Evans } else { 352bb6a234eSPeter Wemm if (randompid) 3530384fff8SJason Evans trypid += arc4random() % randompid; 3540384fff8SJason Evans } 355df8bae1dSRodney W. Grimes retry: 356df8bae1dSRodney W. Grimes /* 357df8bae1dSRodney W. Grimes * If the process ID prototype has wrapped around, 358df8bae1dSRodney W. Grimes * restart somewhat above 0, as the low-numbered procs 359df8bae1dSRodney W. Grimes * tend to include daemons that don't exit. 360df8bae1dSRodney W. Grimes */ 3610384fff8SJason Evans if (trypid >= PID_MAX) { 3620384fff8SJason Evans trypid = trypid % PID_MAX; 3630384fff8SJason Evans if (trypid < 100) 3640384fff8SJason Evans trypid += 100; 365df8bae1dSRodney W. Grimes pidchecked = 0; 366df8bae1dSRodney W. Grimes } 3670384fff8SJason Evans if (trypid >= pidchecked) { 368df8bae1dSRodney W. Grimes int doingzomb = 0; 369df8bae1dSRodney W. Grimes 370df8bae1dSRodney W. Grimes pidchecked = PID_MAX; 371df8bae1dSRodney W. Grimes /* 372df8bae1dSRodney W. Grimes * Scan the active and zombie procs to check whether this pid 373df8bae1dSRodney W. Grimes * is in use. Remember the lowest pid that's greater 3740384fff8SJason Evans * than trypid, so we can avoid checking for a while. 375df8bae1dSRodney W. Grimes */ 3762e3c8fcbSPoul-Henning Kamp p2 = LIST_FIRST(&allproc); 377df8bae1dSRodney W. Grimes again: 378a7b124c3SJohn Baldwin for (; p2 != NULL; p2 = LIST_NEXT(p2, p_list)) { 3790384fff8SJason Evans while (p2->p_pid == trypid || 3800384fff8SJason Evans p2->p_pgrp->pg_id == trypid || 3810384fff8SJason Evans p2->p_session->s_sid == trypid) { 3820384fff8SJason Evans trypid++; 3830384fff8SJason Evans if (trypid >= pidchecked) 384df8bae1dSRodney W. Grimes goto retry; 385df8bae1dSRodney W. Grimes } 3860384fff8SJason Evans if (p2->p_pid > trypid && pidchecked > p2->p_pid) 387df8bae1dSRodney W. Grimes pidchecked = p2->p_pid; 3880384fff8SJason Evans if (p2->p_pgrp->pg_id > trypid && 389df8bae1dSRodney W. Grimes pidchecked > p2->p_pgrp->pg_id) 390df8bae1dSRodney W. Grimes pidchecked = p2->p_pgrp->pg_id; 3910384fff8SJason Evans if (p2->p_session->s_sid > trypid && 392643a8daaSDon Lewis pidchecked > p2->p_session->s_sid) 393643a8daaSDon Lewis pidchecked = p2->p_session->s_sid; 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes if (!doingzomb) { 396df8bae1dSRodney W. Grimes doingzomb = 1; 3972e3c8fcbSPoul-Henning Kamp p2 = LIST_FIRST(&zombproc); 398df8bae1dSRodney W. Grimes goto again; 399df8bae1dSRodney W. Grimes } 400df8bae1dSRodney W. Grimes } 401df8bae1dSRodney W. Grimes 402df8bae1dSRodney W. Grimes /* 4038f7e4eb5SDag-Erling Smørgrav * RFHIGHPID does not mess with the lastpid counter during boot. 4040384fff8SJason Evans */ 4050384fff8SJason Evans if (flags & RFHIGHPID) 4060384fff8SJason Evans pidchecked = 0; 4070384fff8SJason Evans else 4088f7e4eb5SDag-Erling Smørgrav lastpid = trypid; 4090384fff8SJason Evans 410553629ebSJake Burkholder p2 = newproc; 411553629ebSJake Burkholder p2->p_stat = SIDL; /* protect against others */ 412553629ebSJake Burkholder p2->p_pid = trypid; 413553629ebSJake Burkholder LIST_INSERT_HEAD(&allproc, p2, p_list); 414553629ebSJake Burkholder LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 4151005a129SJohn Baldwin sx_xunlock(&allproc_lock); 416553629ebSJake Burkholder 4170384fff8SJason Evans /* 418df8bae1dSRodney W. Grimes * Make a proc table entry for the new process. 419df8bae1dSRodney W. Grimes * Start by zeroing the section of proc that is zero-initialized, 420df8bae1dSRodney W. Grimes * then copy the section that is copied directly from the parent. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes bzero(&p2->p_startzero, 423df8bae1dSRodney W. Grimes (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 424b40ce416SJulian Elischer bzero(&p2->p_kse.ke_startzero, 425b40ce416SJulian Elischer (unsigned) ((caddr_t)&p2->p_kse.ke_endzero 426b40ce416SJulian Elischer - (caddr_t)&p2->p_kse.ke_startzero)); 427b40ce416SJulian Elischer bzero(&p2->p_thread.td_startzero, 428b40ce416SJulian Elischer (unsigned) ((caddr_t)&p2->p_thread.td_endzero 429b40ce416SJulian Elischer - (caddr_t)&p2->p_thread.td_startzero)); 430b40ce416SJulian Elischer bzero(&p2->p_ksegrp.kg_startzero, 431b40ce416SJulian Elischer (unsigned) ((caddr_t)&p2->p_ksegrp.kg_endzero 432b40ce416SJulian Elischer - (caddr_t)&p2->p_ksegrp.kg_startzero)); 43357934cd3SJohn Baldwin PROC_LOCK(p1); 434df8bae1dSRodney W. Grimes bcopy(&p1->p_startcopy, &p2->p_startcopy, 435df8bae1dSRodney W. Grimes (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 436b40ce416SJulian Elischer 437b40ce416SJulian Elischer bcopy(&p1->p_kse.ke_startcopy, &p2->p_kse.ke_startcopy, 438b40ce416SJulian Elischer (unsigned) ((caddr_t)&p2->p_kse.ke_endcopy 439b40ce416SJulian Elischer - (caddr_t)&p2->p_kse.ke_startcopy)); 440b40ce416SJulian Elischer 441b40ce416SJulian Elischer bcopy(&p1->p_thread.td_startcopy, &p2->p_thread.td_startcopy, 442b40ce416SJulian Elischer (unsigned) ((caddr_t)&p2->p_thread.td_endcopy 443b40ce416SJulian Elischer - (caddr_t)&p2->p_thread.td_startcopy)); 444b40ce416SJulian Elischer 445b40ce416SJulian Elischer bcopy(&p1->p_ksegrp.kg_startcopy, &p2->p_ksegrp.kg_startcopy, 446b40ce416SJulian Elischer (unsigned) ((caddr_t)&p2->p_ksegrp.kg_endcopy 447b40ce416SJulian Elischer - (caddr_t)&p2->p_ksegrp.kg_startcopy)); 44857934cd3SJohn Baldwin PROC_UNLOCK(p1); 449df8bae1dSRodney W. Grimes 450b40ce416SJulian Elischer /* 451b40ce416SJulian Elischer * XXXKSE Theoretically only the running thread would get copied 452b40ce416SJulian Elischer * Others in the kernel would be 'aborted' in the child. 453b40ce416SJulian Elischer * i.e return E*something* 454b40ce416SJulian Elischer */ 455b40ce416SJulian Elischer proc_linkup(p2); 456b40ce416SJulian Elischer 4574971f62aSJohn Baldwin mtx_init(&p2->p_mtx, "process lock", MTX_DEF); 45857934cd3SJohn Baldwin PROC_LOCK(p2); 459b40ce416SJulian Elischer /* note.. XXXKSE no pcb or u-area yet */ 4602244ea07SJohn Dyson 461df8bae1dSRodney W. Grimes /* 462df8bae1dSRodney W. Grimes * Duplicate sub-structures as needed. 463df8bae1dSRodney W. Grimes * Increase reference counts on shared objects. 464eb30c1c0SPeter Wemm * The p_stats and p_sigacts substructs are set in vm_forkproc. 465df8bae1dSRodney W. Grimes */ 466a7b124c3SJohn Baldwin p2->p_flag = 0; 4679ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 468a7b124c3SJohn Baldwin p2->p_sflag = PS_INMEM; 469a7b124c3SJohn Baldwin if (p1->p_sflag & PS_PROFIL) 470df8bae1dSRodney W. Grimes startprofclock(p2); 4719ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 47257934cd3SJohn Baldwin PROC_LOCK(p1); 473bd78ceceSJohn Baldwin p2->p_ucred = crhold(p1->p_ucred); 4748e2e767bSJohn Baldwin p2->p_thread.td_ucred = crhold(p2->p_ucred); /* XXXKSE */ 475df8bae1dSRodney W. Grimes 476b9df5231SPoul-Henning Kamp if (p2->p_args) 477b9df5231SPoul-Henning Kamp p2->p_args->ar_ref++; 478b9df5231SPoul-Henning Kamp 4796626c604SJulian Elischer if (flags & RFSIGSHARE) { 480dc9c271aSJulian Elischer p2->p_procsig = p1->p_procsig; 4816626c604SJulian Elischer p2->p_procsig->ps_refcnt++; 482b40ce416SJulian Elischer if (p1->p_sigacts == &p1->p_uarea->u_sigacts) { 483dc9c271aSJulian Elischer struct sigacts *newsigacts; 484dc9c271aSJulian Elischer 48557934cd3SJohn Baldwin PROC_UNLOCK(p1); 48657934cd3SJohn Baldwin PROC_UNLOCK(p2); 487dc9c271aSJulian Elischer /* Create the shared sigacts structure */ 488df8abd0bSPeter Wemm MALLOC(newsigacts, struct sigacts *, 489df8abd0bSPeter Wemm sizeof(struct sigacts), M_SUBPROC, M_WAITOK); 49057934cd3SJohn Baldwin PROC_LOCK(p2); 49157934cd3SJohn Baldwin PROC_LOCK(p1); 492df8abd0bSPeter Wemm /* 493df8abd0bSPeter Wemm * Set p_sigacts to the new shared structure. 494df8abd0bSPeter Wemm * Note that this is updating p1->p_sigacts at the 495df8abd0bSPeter Wemm * same time, since p_sigacts is just a pointer to 496df8abd0bSPeter Wemm * the shared p_procsig->ps_sigacts. 497dc9c271aSJulian Elischer */ 498dc9c271aSJulian Elischer p2->p_sigacts = newsigacts; 499b40ce416SJulian Elischer *p2->p_sigacts = p1->p_uarea->u_sigacts; 500dc9c271aSJulian Elischer } 5016626c604SJulian Elischer } else { 50257934cd3SJohn Baldwin PROC_UNLOCK(p1); 50357934cd3SJohn Baldwin PROC_UNLOCK(p2); 504dc9c271aSJulian Elischer MALLOC(p2->p_procsig, struct procsig *, sizeof(struct procsig), 505dc9c271aSJulian Elischer M_SUBPROC, M_WAITOK); 50657934cd3SJohn Baldwin PROC_LOCK(p2); 50757934cd3SJohn Baldwin PROC_LOCK(p1); 508df8abd0bSPeter Wemm bcopy(p1->p_procsig, p2->p_procsig, sizeof(*p2->p_procsig)); 509dc9c271aSJulian Elischer p2->p_procsig->ps_refcnt = 1; 510eb30c1c0SPeter Wemm p2->p_sigacts = NULL; /* finished in vm_forkproc() */ 5116626c604SJulian Elischer } 5124ac9ae70SJulian Elischer if (flags & RFLINUXTHPN) 5136626c604SJulian Elischer p2->p_sigparent = SIGUSR1; 5144ac9ae70SJulian Elischer else 5154ac9ae70SJulian Elischer p2->p_sigparent = SIGCHLD; 51688c5ea45SJulian Elischer 517df8bae1dSRodney W. Grimes /* bump references to the text vnode (for procfs) */ 518df8bae1dSRodney W. Grimes p2->p_textvp = p1->p_textvp; 5195641ae5dSJohn Baldwin PROC_UNLOCK(p1); 5205641ae5dSJohn Baldwin PROC_UNLOCK(p2); 521df8bae1dSRodney W. Grimes if (p2->p_textvp) 522df8bae1dSRodney W. Grimes VREF(p2->p_textvp); 523df8bae1dSRodney W. Grimes 5240e3eb7eeSSujal Patel if (flags & RFCFDG) 525b40ce416SJulian Elischer fd = fdinit(td); 526426da3bcSAlfred Perlstein else if (flags & RFFDG) { 527426da3bcSAlfred Perlstein FILEDESC_LOCK(p1->p_fd); 528b40ce416SJulian Elischer fd = fdcopy(td); 529426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p1->p_fd); 530426da3bcSAlfred Perlstein } else 5315641ae5dSJohn Baldwin fd = fdshare(p1); 5325641ae5dSJohn Baldwin PROC_LOCK(p2); 5335641ae5dSJohn Baldwin p2->p_fd = fd; 534dabee6feSPeter Wemm 535df8bae1dSRodney W. Grimes /* 536df8bae1dSRodney W. Grimes * If p_limit is still copy-on-write, bump refcnt, 537df8bae1dSRodney W. Grimes * otherwise get a copy that won't be modified. 538df8bae1dSRodney W. Grimes * (If PL_SHAREMOD is clear, the structure is shared 539df8bae1dSRodney W. Grimes * copy-on-write.) 540df8bae1dSRodney W. Grimes */ 5415641ae5dSJohn Baldwin PROC_LOCK(p1); 542df8bae1dSRodney W. Grimes if (p1->p_limit->p_lflags & PL_SHAREMOD) 543df8bae1dSRodney W. Grimes p2->p_limit = limcopy(p1->p_limit); 544df8bae1dSRodney W. Grimes else { 545df8bae1dSRodney W. Grimes p2->p_limit = p1->p_limit; 546df8bae1dSRodney W. Grimes p2->p_limit->p_refcnt++; 547df8bae1dSRodney W. Grimes } 548df8bae1dSRodney W. Grimes 54970e534e7SDavid Greenman /* 55057934cd3SJohn Baldwin * Preserve some more flags in subprocess. PS_PROFIL has already 551be67169aSBruce Evans * been preserved. 55270e534e7SDavid Greenman */ 5533b26be6aSAkinori MUSHA p2->p_flag |= p1->p_flag & (P_SUGID | P_ALTSTACK); 554df8bae1dSRodney W. Grimes if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 555df8bae1dSRodney W. Grimes p2->p_flag |= P_CONTROLT; 5560e3eb7eeSSujal Patel if (flags & RFPPWAIT) 557df8bae1dSRodney W. Grimes p2->p_flag |= P_PPWAIT; 558be67169aSBruce Evans 559b75356e1SJeffrey Hsu LIST_INSERT_AFTER(p1, p2, p_pglist); 56057934cd3SJohn Baldwin PROC_UNLOCK(p1); 56157934cd3SJohn Baldwin PROC_UNLOCK(p2); 5620e3eb7eeSSujal Patel 5630e3eb7eeSSujal Patel /* 5640e3eb7eeSSujal Patel * Attach the new process to its parent. 5650e3eb7eeSSujal Patel * 5660e3eb7eeSSujal Patel * If RFNOWAIT is set, the newly created process becomes a child 5670e3eb7eeSSujal Patel * of init. This effectively disassociates the child from the 5680e3eb7eeSSujal Patel * parent. 5690e3eb7eeSSujal Patel */ 5700e3eb7eeSSujal Patel if (flags & RFNOWAIT) 5710e3eb7eeSSujal Patel pptr = initproc; 5720e3eb7eeSSujal Patel else 5730e3eb7eeSSujal Patel pptr = p1; 5741005a129SJohn Baldwin sx_xlock(&proctree_lock); 57557934cd3SJohn Baldwin PROC_LOCK(p2); 5760e3eb7eeSSujal Patel p2->p_pptr = pptr; 57757934cd3SJohn Baldwin PROC_UNLOCK(p2); 5780e3eb7eeSSujal Patel LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling); 5791005a129SJohn Baldwin sx_xunlock(&proctree_lock); 58057934cd3SJohn Baldwin PROC_LOCK(p2); 581b75356e1SJeffrey Hsu LIST_INIT(&p2->p_children); 582b40ce416SJulian Elischer LIST_INIT(&p2->p_thread.td_contested); /* XXXKSE only 1 thread? */ 583b75356e1SJeffrey Hsu 5844f559836SJake Burkholder callout_init(&p2->p_itcallout, 0); 585b40ce416SJulian Elischer callout_init(&p2->p_thread.td_slpcallout, 1); /* XXXKSE */ 5864f559836SJake Burkholder 58757934cd3SJohn Baldwin PROC_LOCK(p1); 588df8bae1dSRodney W. Grimes #ifdef KTRACE 589df8bae1dSRodney W. Grimes /* 59079deba82SMatthew Dillon * Copy traceflag and tracefile if enabled. If not inherited, 59179deba82SMatthew Dillon * these were zeroed above but we still could have a trace race 59279deba82SMatthew Dillon * so make sure p2's p_tracep is NULL. 593df8bae1dSRodney W. Grimes */ 59479deba82SMatthew Dillon if ((p1->p_traceflag & KTRFAC_INHERIT) && p2->p_tracep == NULL) { 595df8bae1dSRodney W. Grimes p2->p_traceflag = p1->p_traceflag; 5965641ae5dSJohn Baldwin if ((p2->p_tracep = p1->p_tracep) != NULL) { 5975641ae5dSJohn Baldwin PROC_UNLOCK(p1); 5985641ae5dSJohn Baldwin PROC_UNLOCK(p2); 599df8bae1dSRodney W. Grimes VREF(p2->p_tracep); 6005641ae5dSJohn Baldwin PROC_LOCK(p2); 6015641ae5dSJohn Baldwin PROC_LOCK(p1); 6025641ae5dSJohn Baldwin } 603df8bae1dSRodney W. Grimes } 604df8bae1dSRodney W. Grimes #endif 605df8bae1dSRodney W. Grimes 606df8bae1dSRodney W. Grimes /* 6070d2afceeSDavid Greenman * set priority of child to be that of parent 608b40ce416SJulian Elischer * XXXKSE hey! copying the estcpu seems dodgy.. should split it.. 6090d2afceeSDavid Greenman */ 6105641ae5dSJohn Baldwin mtx_lock_spin(&sched_lock); 611b40ce416SJulian Elischer p2->p_ksegrp.kg_estcpu = p1->p_ksegrp.kg_estcpu; 6125641ae5dSJohn Baldwin mtx_unlock_spin(&sched_lock); 6130d2afceeSDavid Greenman 6140d2afceeSDavid Greenman /* 615df8bae1dSRodney W. Grimes * This begins the section where we must prevent the parent 616df8bae1dSRodney W. Grimes * from being swapped. 617df8bae1dSRodney W. Grimes */ 61857934cd3SJohn Baldwin _PHOLD(p1); 61957934cd3SJohn Baldwin PROC_UNLOCK(p1); 62057934cd3SJohn Baldwin PROC_UNLOCK(p2); 6210d2afceeSDavid Greenman 622df8bae1dSRodney W. Grimes /* 623a2a1c95cSPeter Wemm * Finish creating the child process. It will return via a different 624a2a1c95cSPeter Wemm * execution path later. (ie: directly into user mode) 625dabee6feSPeter Wemm */ 626b40ce416SJulian Elischer vm_forkproc(td, p2, flags); 627df8bae1dSRodney W. Grimes 6285d22597fSHajimu UMEMOTO if (flags == (RFFDG | RFPROC)) { 6295d22597fSHajimu UMEMOTO cnt.v_forks++; 6305d22597fSHajimu UMEMOTO cnt.v_forkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 6315d22597fSHajimu UMEMOTO } else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) { 6325d22597fSHajimu UMEMOTO cnt.v_vforks++; 6335d22597fSHajimu UMEMOTO cnt.v_vforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 6345d22597fSHajimu UMEMOTO } else if (p1 == &proc0) { 6355d22597fSHajimu UMEMOTO cnt.v_kthreads++; 6365d22597fSHajimu UMEMOTO cnt.v_kthreadpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 6375d22597fSHajimu UMEMOTO } else { 6385d22597fSHajimu UMEMOTO cnt.v_rforks++; 6395d22597fSHajimu UMEMOTO cnt.v_rforkpages += p2->p_vmspace->vm_dsize + p2->p_vmspace->vm_ssize; 6405d22597fSHajimu UMEMOTO } 6415d22597fSHajimu UMEMOTO 642df8bae1dSRodney W. Grimes /* 643e9189611SPeter Wemm * Both processes are set up, now check if any loadable modules want 644e0d898b4SJulian Elischer * to adjust anything. 645fed06968SJulian Elischer * What if they have an error? XXX 646fed06968SJulian Elischer */ 64757934cd3SJohn Baldwin sx_slock(&fork_list_lock); 64893efcae8SPoul-Henning Kamp TAILQ_FOREACH(ep, &fork_list, next) { 649fed06968SJulian Elischer (*ep->function)(p1, p2, flags); 650fed06968SJulian Elischer } 65157934cd3SJohn Baldwin sx_sunlock(&fork_list_lock); 652fed06968SJulian Elischer 653fed06968SJulian Elischer /* 6540384fff8SJason Evans * If RFSTOPPED not requested, make child runnable and add to 6550384fff8SJason Evans * run queue. 656df8bae1dSRodney W. Grimes */ 657a2a1c95cSPeter Wemm microtime(&(p2->p_stats->p_start)); 658a2a1c95cSPeter Wemm p2->p_acflag = AFORK; 6590384fff8SJason Evans if ((flags & RFSTOPPED) == 0) { 6609ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 661df8bae1dSRodney W. Grimes p2->p_stat = SRUN; 662b40ce416SJulian Elischer setrunqueue(&p2->p_thread); 6639ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 6640384fff8SJason Evans } 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * Now can be swapped. 668df8bae1dSRodney W. Grimes */ 66957934cd3SJohn Baldwin PROC_LOCK(p1); 67057934cd3SJohn Baldwin _PRELE(p1); 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes /* 673cb679c38SJonathan Lemon * tell any interested parties about the new process 674cb679c38SJonathan Lemon */ 675cb679c38SJonathan Lemon KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); 67657934cd3SJohn Baldwin PROC_UNLOCK(p1); 677cb679c38SJonathan Lemon 678cb679c38SJonathan Lemon /* 679df8bae1dSRodney W. Grimes * Preserve synchronization semantics of vfork. If waiting for 680df8bae1dSRodney W. Grimes * child to exec or exit, set P_PPWAIT on child, and sleep on our 681df8bae1dSRodney W. Grimes * proc (in case of exit). 682df8bae1dSRodney W. Grimes */ 68357934cd3SJohn Baldwin PROC_LOCK(p2); 684df8bae1dSRodney W. Grimes while (p2->p_flag & P_PPWAIT) 68557934cd3SJohn Baldwin msleep(p1, &p2->p_mtx, PWAIT, "ppwait", 0); 68657934cd3SJohn Baldwin PROC_UNLOCK(p2); 687df8bae1dSRodney W. Grimes 688df8bae1dSRodney W. Grimes /* 689df8abd0bSPeter Wemm * Return child proc pointer to parent. 690df8bae1dSRodney W. Grimes */ 691df8abd0bSPeter Wemm *procp = p2; 692df8bae1dSRodney W. Grimes return (0); 693df8bae1dSRodney W. Grimes } 694fed06968SJulian Elischer 695e0d898b4SJulian Elischer /* 696e0d898b4SJulian Elischer * The next two functionms are general routines to handle adding/deleting 697e0d898b4SJulian Elischer * items on the fork callout list. 698e0d898b4SJulian Elischer * 699e0d898b4SJulian Elischer * at_fork(): 700e0d898b4SJulian Elischer * Take the arguments given and put them onto the fork callout list, 701fed06968SJulian Elischer * However first make sure that it's not already there. 702e0d898b4SJulian Elischer * Returns 0 on success or a standard error number. 703fed06968SJulian Elischer */ 70493efcae8SPoul-Henning Kamp 705fed06968SJulian Elischer int 706eb776aeaSBruce Evans at_fork(function) 707eb776aeaSBruce Evans forklist_fn function; 708fed06968SJulian Elischer { 70993efcae8SPoul-Henning Kamp struct forklist *ep; 710e0d898b4SJulian Elischer 71193efcae8SPoul-Henning Kamp #ifdef INVARIANTS 712e0d898b4SJulian Elischer /* let the programmer know if he's been stupid */ 713e0d898b4SJulian Elischer if (rm_at_fork(function)) 71493efcae8SPoul-Henning Kamp printf("WARNING: fork callout entry (%p) already present\n", 71593efcae8SPoul-Henning Kamp function); 71693efcae8SPoul-Henning Kamp #endif 71793efcae8SPoul-Henning Kamp ep = malloc(sizeof(*ep), M_ATFORK, M_NOWAIT); 718e0d898b4SJulian Elischer if (ep == NULL) 719e0d898b4SJulian Elischer return (ENOMEM); 720fed06968SJulian Elischer ep->function = function; 72157934cd3SJohn Baldwin sx_xlock(&fork_list_lock); 72293efcae8SPoul-Henning Kamp TAILQ_INSERT_TAIL(&fork_list, ep, next); 72357934cd3SJohn Baldwin sx_xunlock(&fork_list_lock); 724e0d898b4SJulian Elischer return (0); 725fed06968SJulian Elischer } 726e0d898b4SJulian Elischer 727fed06968SJulian Elischer /* 72893efcae8SPoul-Henning Kamp * Scan the exit callout list for the given item and remove it.. 72993efcae8SPoul-Henning Kamp * Returns the number of items removed (0 or 1) 730fed06968SJulian Elischer */ 73193efcae8SPoul-Henning Kamp 732fed06968SJulian Elischer int 733eb776aeaSBruce Evans rm_at_fork(function) 734eb776aeaSBruce Evans forklist_fn function; 735fed06968SJulian Elischer { 73693efcae8SPoul-Henning Kamp struct forklist *ep; 737fed06968SJulian Elischer 73857934cd3SJohn Baldwin sx_xlock(&fork_list_lock); 73993efcae8SPoul-Henning Kamp TAILQ_FOREACH(ep, &fork_list, next) { 740fed06968SJulian Elischer if (ep->function == function) { 74193efcae8SPoul-Henning Kamp TAILQ_REMOVE(&fork_list, ep, next); 74257934cd3SJohn Baldwin sx_xunlock(&fork_list_lock); 74393efcae8SPoul-Henning Kamp free(ep, M_ATFORK); 74493efcae8SPoul-Henning Kamp return(1); 745fed06968SJulian Elischer } 746fed06968SJulian Elischer } 74757934cd3SJohn Baldwin sx_xunlock(&fork_list_lock); 74893efcae8SPoul-Henning Kamp return (0); 749fed06968SJulian Elischer } 750a7b124c3SJohn Baldwin 751a7b124c3SJohn Baldwin /* 752a7b124c3SJohn Baldwin * Handle the return of a child process from fork1(). This function 753a7b124c3SJohn Baldwin * is called from the MD fork_trampoline() entry point. 754a7b124c3SJohn Baldwin */ 755a7b124c3SJohn Baldwin void 756a7b124c3SJohn Baldwin fork_exit(callout, arg, frame) 7578865286bSJohn Baldwin void (*callout)(void *, struct trapframe *); 758a7b124c3SJohn Baldwin void *arg; 7592a36ec35SJohn Baldwin struct trapframe *frame; 760a7b124c3SJohn Baldwin { 761b40ce416SJulian Elischer struct thread *td = curthread; 762b40ce416SJulian Elischer struct proc *p = td->td_proc; 76357934cd3SJohn Baldwin 764201b0ea8SJohn Baldwin td->td_kse->ke_oncpu = PCPU_GET(cpuid); 7655813dc03SJohn Baldwin /* 7665813dc03SJohn Baldwin * Setup the sched_lock state so that we can release it. 7675813dc03SJohn Baldwin */ 768b40ce416SJulian Elischer sched_lock.mtx_lock = (uintptr_t)td; 7695813dc03SJohn Baldwin sched_lock.mtx_recurse = 0; 7707e1f6dfeSJohn Baldwin td->td_critnest = 1; 7717e1f6dfeSJohn Baldwin td->td_savecrit = CRITICAL_FORK; 772201b0ea8SJohn Baldwin CTR3(KTR_PROC, "fork_exit: new proc %p (pid %d, %s)", p, p->p_pid, 773201b0ea8SJohn Baldwin p->p_comm); 774a7b124c3SJohn Baldwin if (PCPU_GET(switchtime.tv_sec) == 0) 775a7b124c3SJohn Baldwin microuptime(PCPU_PTR(switchtime)); 776a7b124c3SJohn Baldwin PCPU_SET(switchticks, ticks); 777201b0ea8SJohn Baldwin mtx_unlock_spin(&sched_lock); 778a7b124c3SJohn Baldwin 779a7b124c3SJohn Baldwin /* 780a7b124c3SJohn Baldwin * cpu_set_fork_handler intercepts this function call to 781a7b124c3SJohn Baldwin * have this call a non-return function to stay in kernel mode. 782a7b124c3SJohn Baldwin * initproc has its own fork handler, but it does return. 783a7b124c3SJohn Baldwin */ 7845813dc03SJohn Baldwin KASSERT(callout != NULL, ("NULL callout in fork_exit")); 7858865286bSJohn Baldwin callout(arg, frame); 786a7b124c3SJohn Baldwin 787a7b124c3SJohn Baldwin /* 788a7b124c3SJohn Baldwin * Check if a kernel thread misbehaved and returned from its main 789a7b124c3SJohn Baldwin * function. 790a7b124c3SJohn Baldwin */ 79157934cd3SJohn Baldwin PROC_LOCK(p); 792a7b124c3SJohn Baldwin if (p->p_flag & P_KTHREAD) { 79357934cd3SJohn Baldwin PROC_UNLOCK(p); 7949ed346baSBosko Milekic mtx_lock(&Giant); 795a7b124c3SJohn Baldwin printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n", 796a7b124c3SJohn Baldwin p->p_comm, p->p_pid); 797a7b124c3SJohn Baldwin kthread_exit(0); 798a7b124c3SJohn Baldwin } 79957934cd3SJohn Baldwin PROC_UNLOCK(p); 8008e2e767bSJohn Baldwin mtx_lock(&Giant); 8018e2e767bSJohn Baldwin crfree(td->td_ucred); 8028e2e767bSJohn Baldwin mtx_unlock(&Giant); 8038e2e767bSJohn Baldwin td->td_ucred = NULL; 804a7b124c3SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 805a7b124c3SJohn Baldwin } 806a7b124c3SJohn Baldwin 807a7b124c3SJohn Baldwin /* 808a7b124c3SJohn Baldwin * Simplified back end of syscall(), used when returning from fork() 809a7b124c3SJohn Baldwin * directly into user mode. Giant is not held on entry, and must not 810a7b124c3SJohn Baldwin * be held on return. This function is passed in to fork_exit() as the 811a7b124c3SJohn Baldwin * first parameter and is called when returning to a new userland process. 812a7b124c3SJohn Baldwin */ 813a7b124c3SJohn Baldwin void 814b40ce416SJulian Elischer fork_return(td, frame) 815b40ce416SJulian Elischer struct thread *td; 816a7b124c3SJohn Baldwin struct trapframe *frame; 817a7b124c3SJohn Baldwin { 818a7b124c3SJohn Baldwin 819b40ce416SJulian Elischer userret(td, frame, 0); 820a7b124c3SJohn Baldwin #ifdef KTRACE 821b40ce416SJulian Elischer if (KTRPOINT(td->td_proc, KTR_SYSRET)) { 822b40ce416SJulian Elischer ktrsysret(td->td_proc->p_tracep, SYS_fork, 0, 0); 823a7b124c3SJohn Baldwin } 824a7b124c3SJohn Baldwin #endif 825a7b124c3SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 826a7b124c3SJohn Baldwin } 827