123370Smckusick /* 263174Sbostic * Copyright (c) 1982, 1986, 1989, 1991, 1993 363174Sbostic * The Regents of the University of California. All rights reserved. 423370Smckusick * 544434Sbostic * %sccs.include.redist.c% 637728Smckusick * 7*64591Sbostic * @(#)kern_fork.c 8.3 (Berkeley) 09/23/93 823370Smckusick */ 912791Ssam 1057050Smckusick #include <sys/param.h> 1157050Smckusick #include <sys/systm.h> 1257050Smckusick #include <sys/map.h> 1357050Smckusick #include <sys/filedesc.h> 1457050Smckusick #include <sys/kernel.h> 1557050Smckusick #include <sys/malloc.h> 1657050Smckusick #include <sys/proc.h> 1757050Smckusick #include <sys/resourcevar.h> 1857050Smckusick #include <sys/vnode.h> 1957050Smckusick #include <sys/file.h> 2057050Smckusick #include <sys/acct.h> 2157050Smckusick #include <sys/ktrace.h> 2212791Ssam 2354922Storek struct fork_args { 2454922Storek int dummy; 2554922Storek }; 2642921Smckusick /* ARGSUSED */ 2742921Smckusick fork(p, uap, retval) 2842921Smckusick struct proc *p; 2954922Storek struct fork_args *uap; 3042921Smckusick int retval[]; 3112791Ssam { 3212791Ssam 3344404Skarels return (fork1(p, 0, retval)); 3412791Ssam } 3512791Ssam 3642921Smckusick /* ARGSUSED */ 3742921Smckusick vfork(p, uap, retval) 3842921Smckusick struct proc *p; 3954922Storek struct fork_args *uap; 4042921Smckusick int retval[]; 4112791Ssam { 4212791Ssam 4344404Skarels return (fork1(p, 1, retval)); 4412791Ssam } 4512791Ssam 4647547Skarels int nprocs = 1; /* process 0 */ 4747547Skarels 4842921Smckusick fork1(p1, isvfork, retval) 4942921Smckusick register struct proc *p1; 5042921Smckusick int isvfork, retval[]; 5112791Ssam { 5242921Smckusick register struct proc *p2; 5355400Smckusick register uid_t uid; 5453699Smckusick struct proc *newproc; 5553646Smckusick struct proc **hash; 5655400Smckusick int count; 5747547Skarels static int nextpid, pidchecked = 0; 5812791Ssam 5912791Ssam /* 6053646Smckusick * Although process entries are dynamically created, we still keep 6153646Smckusick * a global limit on the maximum number we will create. Don't allow 6255400Smckusick * a nonprivileged user to bring the system within one of the global 6355400Smckusick * limit; don't let root exceed the limit. The variable nprocs is 6455400Smckusick * the current number of processes, maxproc is the limit. 6512791Ssam */ 6655400Smckusick uid = p1->p_cred->p_ruid; 6747547Skarels if (nprocs >= maxproc || uid == 0 && nprocs >= maxproc + 1) { 6812791Ssam tablefull("proc"); 6942921Smckusick return (EAGAIN); 7012791Ssam } 7155400Smckusick /* 7255400Smckusick * Increment the count of procs running with this uid. Don't allow 7355400Smckusick * a nonprivileged user to exceed their current limit. 7455400Smckusick */ 7555400Smckusick count = chgproccnt(uid, 1); 7655400Smckusick if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { 7755400Smckusick (void)chgproccnt(uid, -1); 7847547Skarels return (EAGAIN); 7955400Smckusick } 8012791Ssam 8153646Smckusick /* Allocate new proc. */ 8253699Smckusick MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK); 8353646Smckusick 8412791Ssam /* 8553646Smckusick * Find an unused process ID. We remember a range of unused IDs 8653646Smckusick * ready to use (from nextpid+1 through pidchecked-1). 8712791Ssam */ 8847547Skarels nextpid++; 8912791Ssam retry: 9047547Skarels /* 9147547Skarels * If the process ID prototype has wrapped around, 9247547Skarels * restart somewhat above 0, as the low-numbered procs 9347547Skarels * tend to include daemons that don't exit. 9447547Skarels */ 9547547Skarels if (nextpid >= PID_MAX) { 9647547Skarels nextpid = 100; 9716528Skarels pidchecked = 0; 9812791Ssam } 9947547Skarels if (nextpid >= pidchecked) { 10016528Skarels int doingzomb = 0; 10116578Ssam 10245005Skarels pidchecked = PID_MAX; 10316528Skarels /* 10447547Skarels * Scan the active and zombie procs to check whether this pid 10516528Skarels * is in use. Remember the lowest pid that's greater 10647547Skarels * than nextpid, so we can avoid checking for a while. 10716528Skarels */ 10854756Storek p2 = (struct proc *)allproc; 10916528Skarels again: 110*64591Sbostic for (; p2 != NULL; p2 = p2->p_next) { 11153646Smckusick while (p2->p_pid == nextpid || 11247547Skarels p2->p_pgrp->pg_id == nextpid) { 11347547Skarels nextpid++; 11447547Skarels if (nextpid >= pidchecked) 11516528Skarels goto retry; 11616528Skarels } 11747547Skarels if (p2->p_pid > nextpid && pidchecked > p2->p_pid) 11847547Skarels pidchecked = p2->p_pid; 11947547Skarels if (p2->p_pgrp->pg_id > nextpid && 12047547Skarels pidchecked > p2->p_pgrp->pg_id) 12147547Skarels pidchecked = p2->p_pgrp->pg_id; 12216528Skarels } 12316528Skarels if (!doingzomb) { 12416528Skarels doingzomb = 1; 12547547Skarels p2 = zombproc; 12616528Skarels goto again; 12716528Skarels } 12812791Ssam } 12912791Ssam 13016528Skarels 13156335Smckusick /* 13256335Smckusick * Link onto allproc (this should probably be delayed). 13356335Smckusick * Heavy use of volatile here to prevent the compiler from 13456335Smckusick * rearranging code. Yes, it *is* terribly ugly, but at least 13556335Smckusick * it works. 13656335Smckusick */ 13747547Skarels nprocs++; 13853699Smckusick p2 = newproc; 13956335Smckusick #define Vp2 ((volatile struct proc *)p2) 14056335Smckusick Vp2->p_stat = SIDL; /* protect against others */ 14156335Smckusick Vp2->p_pid = nextpid; 14256335Smckusick /* 14356335Smckusick * This is really: 144*64591Sbostic * p2->p_next = allproc; 145*64591Sbostic * allproc->p_prev = &p2->p_next; 14656335Smckusick * p2->p_prev = &allproc; 14756335Smckusick * allproc = p2; 14856335Smckusick * The assignment via allproc is legal since it is never NULL. 14956335Smckusick */ 150*64591Sbostic *(volatile struct proc **)&Vp2->p_next = allproc; 15156335Smckusick *(volatile struct proc ***)&allproc->p_prev = 152*64591Sbostic (volatile struct proc **)&Vp2->p_next; 15356335Smckusick *(volatile struct proc ***)&Vp2->p_prev = &allproc; 15456335Smckusick allproc = Vp2; 15556335Smckusick #undef Vp2 156*64591Sbostic p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */ 15747547Skarels 15853646Smckusick /* Insert on the hash chain. */ 15953646Smckusick hash = &pidhash[PIDHASH(p2->p_pid)]; 16053646Smckusick p2->p_hash = *hash; 16153646Smckusick *hash = p2; 16253646Smckusick 16347547Skarels /* 16412791Ssam * Make a proc table entry for the new process. 16547547Skarels * Start by zeroing the section of proc that is zero-initialized, 16647547Skarels * then copy the section that is copied directly from the parent. 16712791Ssam */ 16847547Skarels bzero(&p2->p_startzero, 16947547Skarels (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 17047547Skarels bcopy(&p1->p_startcopy, &p2->p_startcopy, 17147547Skarels (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 17247547Skarels 17347547Skarels /* 17447547Skarels * Duplicate sub-structures as needed. 17547547Skarels * Increase reference counts on shared objects. 17648405Skarels * The p_stats and p_sigacts substructs are set in vm_fork. 17747547Skarels */ 178*64591Sbostic p2->p_flag = P_INMEM; 179*64591Sbostic if (p1->p_flag & P_PROFIL) 18054132Smckusick startprofclock(p2); 18147547Skarels MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred), 18247547Skarels M_SUBPROC, M_WAITOK); 18347547Skarels bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); 18449746Smckusick p2->p_cred->p_refcnt = 1; 18547547Skarels crhold(p1->p_ucred); 18647547Skarels 18747547Skarels p2->p_fd = fdcopy(p1); 18847547Skarels /* 18947547Skarels * If p_limit is still copy-on-write, bump refcnt, 19047547Skarels * otherwise get a copy that won't be modified. 19147547Skarels * (If PL_SHAREMOD is clear, the structure is shared 19247547Skarels * copy-on-write.) 19347547Skarels */ 19447547Skarels if (p1->p_limit->p_lflags & PL_SHAREMOD) 19547547Skarels p2->p_limit = limcopy(p1->p_limit); 19647547Skarels else { 19747547Skarels p2->p_limit = p1->p_limit; 19847547Skarels p2->p_limit->p_refcnt++; 19947547Skarels } 20047547Skarels 201*64591Sbostic if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 202*64591Sbostic p2->p_flag |= P_CONTROLT; 20345727Smckusick if (isvfork) 204*64591Sbostic p2->p_flag |= P_PPWAIT; 20547547Skarels p2->p_pgrpnxt = p1->p_pgrpnxt; 20647547Skarels p1->p_pgrpnxt = p2; 20747547Skarels p2->p_pptr = p1; 20847547Skarels p2->p_osptr = p1->p_cptr; 20947547Skarels if (p1->p_cptr) 21047547Skarels p1->p_cptr->p_ysptr = p2; 21147547Skarels p1->p_cptr = p2; 21247547Skarels #ifdef KTRACE 21312791Ssam /* 21447547Skarels * Copy traceflag and tracefile if enabled. 21547547Skarels * If not inherited, these were zeroed above. 21612791Ssam */ 21747547Skarels if (p1->p_traceflag&KTRFAC_INHERIT) { 21847547Skarels p2->p_traceflag = p1->p_traceflag; 21947547Skarels if ((p2->p_tracep = p1->p_tracep) != NULL) 22047547Skarels VREF(p2->p_tracep); 22147547Skarels } 22245727Smckusick #endif 22312791Ssam 22412791Ssam /* 22512791Ssam * This begins the section where we must prevent the parent 22612791Ssam * from being swapped. 22712791Ssam */ 228*64591Sbostic p1->p_flag |= P_NOSWAP; 22948405Skarels /* 23048405Skarels * Set return values for child before vm_fork, 23148405Skarels * so they can be copied to child stack. 23248405Skarels * We return parent pid, and mark as child in retval[1]. 23349099Skarels * NOTE: the kernel stack may be at a different location in the child 23449099Skarels * process, and thus addresses of automatic variables (including retval) 23549099Skarels * may be invalid after vm_fork returns in the child process. 23648405Skarels */ 23748405Skarels retval[0] = p1->p_pid; 23848405Skarels retval[1] = 1; 23947547Skarels if (vm_fork(p1, p2, isvfork)) { 24047547Skarels /* 24148405Skarels * Child process. Set start time and get to work. 24247547Skarels */ 24341180Smarc (void) splclock(); 24447547Skarels p2->p_stats->p_start = time; 24541180Smarc (void) spl0(); 24647547Skarels p2->p_acflag = AFORK; 24747547Skarels return (0); 24841180Smarc } 24912791Ssam 25012791Ssam /* 25112791Ssam * Make child runnable and add to run queue. 25212791Ssam */ 25347547Skarels (void) splhigh(); 25447547Skarels p2->p_stat = SRUN; 25564532Sbostic setrunqueue(p2); 25612791Ssam (void) spl0(); 25712791Ssam 25812791Ssam /* 25912791Ssam * Now can be swapped. 26012791Ssam */ 261*64591Sbostic p1->p_flag &= ~P_NOSWAP; 26212791Ssam 26312791Ssam /* 264*64591Sbostic * Preserve synchronization semantics of vfork. If waiting for 265*64591Sbostic * child to exec or exit, set P_PPWAIT on child, and sleep on our 266*64591Sbostic * proc (in case of exit). 26712791Ssam */ 26847547Skarels if (isvfork) 269*64591Sbostic while (p2->p_flag & P_PPWAIT) 270*64591Sbostic tsleep(p1, PWAIT, "ppwait", 0); 27112791Ssam 27212791Ssam /* 27348405Skarels * Return child pid to parent process, 27448405Skarels * marking us as parent via retval[1]. 27512791Ssam */ 27647547Skarels retval[0] = p2->p_pid; 27748405Skarels retval[1] = 0; 27812791Ssam return (0); 27912791Ssam } 280