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