1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)kern_fork.c 8.3 (Berkeley) 09/23/93 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/map.h> 13 #include <sys/filedesc.h> 14 #include <sys/kernel.h> 15 #include <sys/malloc.h> 16 #include <sys/proc.h> 17 #include <sys/resourcevar.h> 18 #include <sys/vnode.h> 19 #include <sys/file.h> 20 #include <sys/acct.h> 21 #include <sys/ktrace.h> 22 23 struct fork_args { 24 int dummy; 25 }; 26 /* ARGSUSED */ 27 fork(p, uap, retval) 28 struct proc *p; 29 struct fork_args *uap; 30 int retval[]; 31 { 32 33 return (fork1(p, 0, retval)); 34 } 35 36 /* ARGSUSED */ 37 vfork(p, uap, retval) 38 struct proc *p; 39 struct fork_args *uap; 40 int retval[]; 41 { 42 43 return (fork1(p, 1, retval)); 44 } 45 46 int nprocs = 1; /* process 0 */ 47 48 fork1(p1, isvfork, retval) 49 register struct proc *p1; 50 int isvfork, retval[]; 51 { 52 register struct proc *p2; 53 register uid_t uid; 54 struct proc *newproc; 55 struct proc **hash; 56 int count; 57 static int nextpid, pidchecked = 0; 58 59 /* 60 * Although process entries are dynamically created, we still keep 61 * a global limit on the maximum number we will create. Don't allow 62 * a nonprivileged user to bring the system within one of the global 63 * limit; don't let root exceed the limit. The variable nprocs is 64 * the current number of processes, maxproc is the limit. 65 */ 66 uid = p1->p_cred->p_ruid; 67 if (nprocs >= maxproc || uid == 0 && nprocs >= maxproc + 1) { 68 tablefull("proc"); 69 return (EAGAIN); 70 } 71 /* 72 * Increment the count of procs running with this uid. Don't allow 73 * a nonprivileged user to exceed their current limit. 74 */ 75 count = chgproccnt(uid, 1); 76 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { 77 (void)chgproccnt(uid, -1); 78 return (EAGAIN); 79 } 80 81 /* Allocate new proc. */ 82 MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK); 83 84 /* 85 * Find an unused process ID. We remember a range of unused IDs 86 * ready to use (from nextpid+1 through pidchecked-1). 87 */ 88 nextpid++; 89 retry: 90 /* 91 * If the process ID prototype has wrapped around, 92 * restart somewhat above 0, as the low-numbered procs 93 * tend to include daemons that don't exit. 94 */ 95 if (nextpid >= PID_MAX) { 96 nextpid = 100; 97 pidchecked = 0; 98 } 99 if (nextpid >= pidchecked) { 100 int doingzomb = 0; 101 102 pidchecked = PID_MAX; 103 /* 104 * Scan the active and zombie procs to check whether this pid 105 * is in use. Remember the lowest pid that's greater 106 * than nextpid, so we can avoid checking for a while. 107 */ 108 p2 = (struct proc *)allproc; 109 again: 110 for (; p2 != NULL; p2 = p2->p_next) { 111 while (p2->p_pid == nextpid || 112 p2->p_pgrp->pg_id == nextpid) { 113 nextpid++; 114 if (nextpid >= pidchecked) 115 goto retry; 116 } 117 if (p2->p_pid > nextpid && pidchecked > p2->p_pid) 118 pidchecked = p2->p_pid; 119 if (p2->p_pgrp->pg_id > nextpid && 120 pidchecked > p2->p_pgrp->pg_id) 121 pidchecked = p2->p_pgrp->pg_id; 122 } 123 if (!doingzomb) { 124 doingzomb = 1; 125 p2 = zombproc; 126 goto again; 127 } 128 } 129 130 131 /* 132 * Link onto allproc (this should probably be delayed). 133 * Heavy use of volatile here to prevent the compiler from 134 * rearranging code. Yes, it *is* terribly ugly, but at least 135 * it works. 136 */ 137 nprocs++; 138 p2 = newproc; 139 #define Vp2 ((volatile struct proc *)p2) 140 Vp2->p_stat = SIDL; /* protect against others */ 141 Vp2->p_pid = nextpid; 142 /* 143 * This is really: 144 * p2->p_next = allproc; 145 * allproc->p_prev = &p2->p_next; 146 * p2->p_prev = &allproc; 147 * allproc = p2; 148 * The assignment via allproc is legal since it is never NULL. 149 */ 150 *(volatile struct proc **)&Vp2->p_next = allproc; 151 *(volatile struct proc ***)&allproc->p_prev = 152 (volatile struct proc **)&Vp2->p_next; 153 *(volatile struct proc ***)&Vp2->p_prev = &allproc; 154 allproc = Vp2; 155 #undef Vp2 156 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */ 157 158 /* Insert on the hash chain. */ 159 hash = &pidhash[PIDHASH(p2->p_pid)]; 160 p2->p_hash = *hash; 161 *hash = p2; 162 163 /* 164 * Make a proc table entry for the new process. 165 * Start by zeroing the section of proc that is zero-initialized, 166 * then copy the section that is copied directly from the parent. 167 */ 168 bzero(&p2->p_startzero, 169 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 170 bcopy(&p1->p_startcopy, &p2->p_startcopy, 171 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 172 173 /* 174 * Duplicate sub-structures as needed. 175 * Increase reference counts on shared objects. 176 * The p_stats and p_sigacts substructs are set in vm_fork. 177 */ 178 p2->p_flag = P_INMEM; 179 if (p1->p_flag & P_PROFIL) 180 startprofclock(p2); 181 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred), 182 M_SUBPROC, M_WAITOK); 183 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); 184 p2->p_cred->p_refcnt = 1; 185 crhold(p1->p_ucred); 186 187 p2->p_fd = fdcopy(p1); 188 /* 189 * If p_limit is still copy-on-write, bump refcnt, 190 * otherwise get a copy that won't be modified. 191 * (If PL_SHAREMOD is clear, the structure is shared 192 * copy-on-write.) 193 */ 194 if (p1->p_limit->p_lflags & PL_SHAREMOD) 195 p2->p_limit = limcopy(p1->p_limit); 196 else { 197 p2->p_limit = p1->p_limit; 198 p2->p_limit->p_refcnt++; 199 } 200 201 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 202 p2->p_flag |= P_CONTROLT; 203 if (isvfork) 204 p2->p_flag |= P_PPWAIT; 205 p2->p_pgrpnxt = p1->p_pgrpnxt; 206 p1->p_pgrpnxt = p2; 207 p2->p_pptr = p1; 208 p2->p_osptr = p1->p_cptr; 209 if (p1->p_cptr) 210 p1->p_cptr->p_ysptr = p2; 211 p1->p_cptr = p2; 212 #ifdef KTRACE 213 /* 214 * Copy traceflag and tracefile if enabled. 215 * If not inherited, these were zeroed above. 216 */ 217 if (p1->p_traceflag&KTRFAC_INHERIT) { 218 p2->p_traceflag = p1->p_traceflag; 219 if ((p2->p_tracep = p1->p_tracep) != NULL) 220 VREF(p2->p_tracep); 221 } 222 #endif 223 224 /* 225 * This begins the section where we must prevent the parent 226 * from being swapped. 227 */ 228 p1->p_flag |= P_NOSWAP; 229 /* 230 * Set return values for child before vm_fork, 231 * so they can be copied to child stack. 232 * We return parent pid, and mark as child in retval[1]. 233 * NOTE: the kernel stack may be at a different location in the child 234 * process, and thus addresses of automatic variables (including retval) 235 * may be invalid after vm_fork returns in the child process. 236 */ 237 retval[0] = p1->p_pid; 238 retval[1] = 1; 239 if (vm_fork(p1, p2, isvfork)) { 240 /* 241 * Child process. Set start time and get to work. 242 */ 243 (void) splclock(); 244 p2->p_stats->p_start = time; 245 (void) spl0(); 246 p2->p_acflag = AFORK; 247 return (0); 248 } 249 250 /* 251 * Make child runnable and add to run queue. 252 */ 253 (void) splhigh(); 254 p2->p_stat = SRUN; 255 setrunqueue(p2); 256 (void) spl0(); 257 258 /* 259 * Now can be swapped. 260 */ 261 p1->p_flag &= ~P_NOSWAP; 262 263 /* 264 * Preserve synchronization semantics of vfork. If waiting for 265 * child to exec or exit, set P_PPWAIT on child, and sleep on our 266 * proc (in case of exit). 267 */ 268 if (isvfork) 269 while (p2->p_flag & P_PPWAIT) 270 tsleep(p1, PWAIT, "ppwait", 0); 271 272 /* 273 * Return child pid to parent process, 274 * marking us as parent via retval[1]. 275 */ 276 retval[0] = p2->p_pid; 277 retval[1] = 0; 278 return (0); 279 } 280