1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)kern_fork.c 7.23 (Berkeley) 01/10/91 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "map.h" 13 #include "user.h" 14 #include "filedesc.h" 15 #include "kernel.h" 16 #include "proc.h" 17 #include "vnode.h" 18 #include "seg.h" 19 #include "file.h" 20 #include "acct.h" 21 #include "ktrace.h" 22 23 #include "machine/reg.h" 24 #include "machine/psl.h" 25 26 /* 27 * fork system call. 28 */ 29 /* ARGSUSED */ 30 fork(p, uap, retval) 31 struct proc *p; 32 struct args *uap; 33 int retval[]; 34 { 35 int error; 36 37 return (fork1(p, 0, retval)); 38 } 39 40 /* ARGSUSED */ 41 vfork(p, uap, retval) 42 struct proc *p; 43 struct args *uap; 44 int retval[]; 45 { 46 47 return (fork1(p, 1, retval)); 48 } 49 50 fork1(p1, isvfork, retval) 51 register struct proc *p1; 52 int isvfork, retval[]; 53 { 54 register struct proc *p2; 55 register int a; 56 57 a = 0; 58 if (p1->p_uid != 0) { 59 for (p2 = allproc; p2; p2 = p2->p_nxt) 60 if (p2->p_uid == p1->p_uid) 61 a++; 62 for (p2 = zombproc; p2; p2 = p2->p_nxt) 63 if (p2->p_uid == p1->p_uid) 64 a++; 65 } 66 /* 67 * Disallow if 68 * No processes at all; 69 * not su and too many procs owned; or 70 * not su and would take last slot. 71 */ 72 p2 = freeproc; 73 if (p2==NULL) 74 tablefull("proc"); 75 if (p2 == NULL || 76 (p1->p_uid != 0 && (p2->p_nxt == NULL || a > MAXUPRC))) { 77 retval[1] = 0; 78 return (EAGAIN); 79 } 80 if (newproc(isvfork)) { 81 retval[0] = p1->p_pid; 82 retval[1] = 1; /* child */ 83 u.u_acflag = AFORK; 84 return (0); 85 } 86 retval[0] = p2->p_pid; 87 retval[1] = 0; 88 return (0); 89 } 90 91 /* 92 * Create a new process-- the internal version of 93 * sys fork. 94 * It returns 1 in the new process, 0 in the old. 95 */ 96 newproc(isvfork) 97 int isvfork; 98 { 99 register struct proc *rpp, *rip; 100 register struct file *fp; 101 static int pidchecked = 0; 102 103 /* 104 * First, just locate a slot for a process 105 * and copy the useful info from this process into it. 106 * The panic "cannot happen" because fork has already 107 * checked for the existence of a slot. 108 */ 109 mpid++; 110 retry: 111 if (mpid >= PID_MAX) { 112 mpid = 100; 113 pidchecked = 0; 114 } 115 if (mpid >= pidchecked) { 116 int doingzomb = 0; 117 118 pidchecked = PID_MAX; 119 /* 120 * Scan the proc table to check whether this pid 121 * is in use. Remember the lowest pid that's greater 122 * than mpid, so we can avoid checking for a while. 123 */ 124 rpp = allproc; 125 again: 126 for (; rpp != NULL; rpp = rpp->p_nxt) { 127 if (rpp->p_pid == mpid || rpp->p_pgrp->pg_id == mpid) { 128 mpid++; 129 if (mpid >= pidchecked) 130 goto retry; 131 } 132 if (rpp->p_pid > mpid && pidchecked > rpp->p_pid) 133 pidchecked = rpp->p_pid; 134 if (rpp->p_pgrp->pg_id > mpid && 135 pidchecked > rpp->p_pgrp->pg_id) 136 pidchecked = rpp->p_pgrp->pg_id; 137 } 138 if (!doingzomb) { 139 doingzomb = 1; 140 rpp = zombproc; 141 goto again; 142 } 143 } 144 if ((rpp = freeproc) == NULL) 145 panic("no procs"); 146 147 freeproc = rpp->p_nxt; /* off freeproc */ 148 rpp->p_nxt = allproc; /* onto allproc */ 149 rpp->p_nxt->p_prev = &rpp->p_nxt; /* (allproc is never NULL) */ 150 rpp->p_prev = &allproc; 151 allproc = rpp; 152 153 /* 154 * Make a proc table entry for the new process. 155 */ 156 rip = u.u_procp; 157 #if defined(tahoe) 158 rpp->p_ckey = rip->p_ckey; 159 rpp->p_dkey = 0; 160 #endif 161 rpp->p_stat = SIDL; 162 timerclear(&rpp->p_realtimer.it_value); 163 rpp->p_flag = SLOAD | (rip->p_flag & (SPAGV|SHPUX)); 164 if (rip->p_session->s_ttyvp != NULL && rip->p_flag & SCTTY) 165 rpp->p_flag |= SCTTY; 166 if (isvfork) 167 rpp->p_flag |= SVFORK; 168 rpp->p_ndx = rpp - proc; 169 bcopy(rip->p_comm, rpp->p_comm, MAXCOMLEN+1); 170 bcopy(rip->p_logname, rpp->p_logname, MAXLOGNAME); 171 rpp->p_uid = rip->p_uid; 172 rpp->p_ruid = rip->p_ruid; 173 rpp->p_rgid = rip->p_rgid; 174 rpp->p_pgrp = rip->p_pgrp; 175 rpp->p_pgrpnxt = rip->p_pgrpnxt; 176 rip->p_pgrpnxt = rpp; 177 rpp->p_nice = rip->p_nice; 178 rpp->p_pid = mpid; 179 rpp->p_ppid = rip->p_pid; 180 rpp->p_pptr = rip; 181 rpp->p_osptr = rip->p_cptr; 182 if (rip->p_cptr) 183 rip->p_cptr->p_ysptr = rpp; 184 rpp->p_ysptr = NULL; 185 rpp->p_cptr = NULL; 186 rip->p_cptr = rpp; 187 rpp->p_time = 0; 188 bzero((caddr_t)&rpp->p_utime, sizeof (struct timeval)); 189 bzero((caddr_t)&rpp->p_stime, sizeof (struct timeval)); 190 rpp->p_cpu = 0; 191 rpp->p_sigmask = rip->p_sigmask; 192 rpp->p_sigcatch = rip->p_sigcatch; 193 rpp->p_sigignore = rip->p_sigignore; 194 #ifdef KTRACE 195 if (rip->p_traceflag&KTRFAC_INHERIT) { 196 rpp->p_traceflag = rip->p_traceflag; 197 if ((rpp->p_tracep = rip->p_tracep) != NULL) 198 VREF(rpp->p_tracep); 199 } else { 200 rpp->p_tracep = NULL; 201 rpp->p_traceflag = 0; 202 } 203 #endif 204 rpp->p_rssize = 0; 205 rpp->p_maxrss = rip->p_maxrss; 206 rpp->p_wchan = 0; 207 rpp->p_slptime = 0; 208 rpp->p_pctcpu = 0; 209 rpp->p_cpticks = 0; 210 { 211 struct proc **hash = &pidhash[PIDHASH(rpp->p_pid)]; 212 213 rpp->p_hash = *hash; 214 *hash = rpp; 215 } 216 217 /* 218 * Increase reference counts on shared objects. 219 */ 220 rpp->p_fd = fddup(rip->p_fd, 0); 221 #ifdef SYSVSHM 222 if (rip->p_shm) 223 shmfork(rip, rpp, isvfork); 224 #endif 225 crhold(u.u_cred); 226 227 /* 228 * This begins the section where we must prevent the parent 229 * from being swapped. 230 */ 231 rip->p_flag |= SKEEP; 232 if (procdup(rpp, isvfork)) { 233 (void) splclock(); 234 u.u_start = time; 235 (void) spl0(); 236 return (1); 237 } 238 239 /* 240 * Make child runnable and add to run queue. 241 */ 242 (void) splclock(); 243 rpp->p_stat = SRUN; 244 setrq(rpp); 245 (void) spl0(); 246 247 /* 248 * Cause child to take a non-local goto as soon as it runs. 249 * On older systems this was done with SSWAP bit in proc 250 * table; on VAX we use u.u_pcb.pcb_sswap so don't need 251 * to do rpp->p_flag |= SSWAP. Actually do nothing here. 252 */ 253 /* rpp->p_flag |= SSWAP; */ 254 255 /* 256 * Now can be swapped. 257 */ 258 rip->p_flag &= ~SKEEP; 259 260 /* 261 * XXX preserve synchronization semantics of vfork 262 */ 263 if (isvfork) { 264 u.u_procp->p_flag |= SNOVM; 265 while (rpp->p_flag & SVFORK) 266 sleep((caddr_t)rpp, PZERO - 1); 267 if ((rpp->p_flag & SLOAD) == 0) 268 panic("newproc vfork"); 269 u.u_procp->p_flag &= ~SNOVM; 270 rpp->p_flag |= SVFDONE; 271 wakeup((caddr_t)rpp); 272 } 273 274 /* 275 * 0 return means parent. 276 */ 277 return (0); 278 } 279