1 /* $NetBSD: kern_fork.c,v 1.21 1994/08/30 06:16:25 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/map.h> 46 #include <sys/filedesc.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 #include <sys/resourcevar.h> 51 #include <sys/vnode.h> 52 #include <sys/file.h> 53 #include <sys/acct.h> 54 #include <sys/ktrace.h> 55 56 /* ARGSUSED */ 57 fork(p, uap, retval) 58 struct proc *p; 59 void *uap; 60 int retval[]; 61 { 62 63 return (fork1(p, 0, retval)); 64 } 65 66 /* ARGSUSED */ 67 vfork(p, uap, retval) 68 struct proc *p; 69 void *uap; 70 int retval[]; 71 { 72 73 return (fork1(p, 1, retval)); 74 } 75 76 int nprocs = 1; /* process 0 */ 77 78 fork1(p1, isvfork, retval) 79 register struct proc *p1; 80 int isvfork, retval[]; 81 { 82 register struct proc *p2; 83 register uid_t uid; 84 struct proc *newproc; 85 struct proc **hash; 86 int count; 87 static int nextpid, pidchecked = 0; 88 89 /* 90 * Although process entries are dynamically created, we still keep 91 * a global limit on the maximum number we will create. Don't allow 92 * a nonprivileged user to use the last process; don't let root 93 * exceed the limit. The variable nprocs is the current number of 94 * processes, maxproc is the limit. 95 */ 96 uid = p1->p_cred->p_ruid; 97 if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { 98 tablefull("proc"); 99 return (EAGAIN); 100 } 101 102 /* 103 * Increment the count of procs running with this uid. Don't allow 104 * a nonprivileged user to exceed their current limit. 105 */ 106 count = chgproccnt(uid, 1); 107 if (uid != 0 && count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur) { 108 (void)chgproccnt(uid, -1); 109 return (EAGAIN); 110 } 111 112 /* Allocate new proc. */ 113 MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK); 114 115 /* 116 * Find an unused process ID. We remember a range of unused IDs 117 * ready to use (from nextpid+1 through pidchecked-1). 118 */ 119 nextpid++; 120 retry: 121 /* 122 * If the process ID prototype has wrapped around, 123 * restart somewhat above 0, as the low-numbered procs 124 * tend to include daemons that don't exit. 125 */ 126 if (nextpid >= PID_MAX) { 127 nextpid = 100; 128 pidchecked = 0; 129 } 130 if (nextpid >= pidchecked) { 131 int doingzomb = 0; 132 133 pidchecked = PID_MAX; 134 /* 135 * Scan the active and zombie procs to check whether this pid 136 * is in use. Remember the lowest pid that's greater 137 * than nextpid, so we can avoid checking for a while. 138 */ 139 p2 = allproc.lh_first; 140 again: 141 for (; p2 != 0; p2 = p2->p_list.le_next) { 142 while (p2->p_pid == nextpid || 143 p2->p_pgrp->pg_id == nextpid) { 144 nextpid++; 145 if (nextpid >= pidchecked) 146 goto retry; 147 } 148 if (p2->p_pid > nextpid && pidchecked > p2->p_pid) 149 pidchecked = p2->p_pid; 150 if (p2->p_pgrp->pg_id > nextpid && 151 pidchecked > p2->p_pgrp->pg_id) 152 pidchecked = p2->p_pgrp->pg_id; 153 } 154 if (!doingzomb) { 155 doingzomb = 1; 156 p2 = zombproc.lh_first; 157 goto again; 158 } 159 } 160 161 nprocs++; 162 p2 = newproc; 163 p2->p_stat = SIDL; /* protect against others */ 164 p2->p_pid = nextpid; 165 LIST_INSERT_HEAD(&allproc, p2, p_list); 166 p2->p_forw = p2->p_back = NULL; /* shouldn't be necessary */ 167 LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash); 168 169 /* 170 * Make a proc table entry for the new process. 171 * Start by zeroing the section of proc that is zero-initialized, 172 * then copy the section that is copied directly from the parent. 173 */ 174 bzero(&p2->p_startzero, 175 (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero)); 176 bcopy(&p1->p_startcopy, &p2->p_startcopy, 177 (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy)); 178 179 /* 180 * Duplicate sub-structures as needed. 181 * Increase reference counts on shared objects. 182 * The p_stats and p_sigacts substructs are set in vm_fork. 183 */ 184 p2->p_flag = P_INMEM; 185 p2->p_emul = p1->p_emul; 186 if (p1->p_flag & P_PROFIL) 187 startprofclock(p2); 188 MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred), 189 M_SUBPROC, M_WAITOK); 190 bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); 191 p2->p_cred->p_refcnt = 1; 192 crhold(p1->p_ucred); 193 194 /* bump references to the text vnode (for procfs) */ 195 p2->p_textvp = p1->p_textvp; 196 if (p2->p_textvp) 197 VREF(p2->p_textvp); 198 199 p2->p_fd = fdcopy(p1); 200 /* 201 * If p_limit is still copy-on-write, bump refcnt, 202 * otherwise get a copy that won't be modified. 203 * (If PL_SHAREMOD is clear, the structure is shared 204 * copy-on-write.) 205 */ 206 if (p1->p_limit->p_lflags & PL_SHAREMOD) 207 p2->p_limit = limcopy(p1->p_limit); 208 else { 209 p2->p_limit = p1->p_limit; 210 p2->p_limit->p_refcnt++; 211 } 212 213 if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) 214 p2->p_flag |= P_CONTROLT; 215 if (isvfork) 216 p2->p_flag |= P_PPWAIT; 217 LIST_INSERT_AFTER(p1, p2, p_pglist); 218 p2->p_pptr = p1; 219 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling); 220 LIST_INIT(&p2->p_children); 221 222 #ifdef KTRACE 223 /* 224 * Copy traceflag and tracefile if enabled. 225 * If not inherited, these were zeroed above. 226 */ 227 if (p1->p_traceflag&KTRFAC_INHERIT) { 228 p2->p_traceflag = p1->p_traceflag; 229 if ((p2->p_tracep = p1->p_tracep) != NULL) 230 VREF(p2->p_tracep); 231 } 232 #endif 233 234 /* 235 * This begins the section where we must prevent the parent 236 * from being swapped. 237 */ 238 p1->p_holdcnt++; 239 /* 240 * Set return values for child before vm_fork, 241 * so they can be copied to child stack. 242 * We return parent pid, and mark as child in retval[1]. 243 * NOTE: the kernel stack may be at a different location in the child 244 * process, and thus addresses of automatic variables (including retval) 245 * may be invalid after vm_fork returns in the child process. 246 */ 247 retval[0] = p1->p_pid; 248 retval[1] = 1; 249 if (vm_fork(p1, p2, isvfork)) { 250 /* 251 * Child process. Set start time and get to work. 252 */ 253 (void) splclock(); 254 p2->p_stats->p_start = time; 255 (void) spl0(); 256 p2->p_acflag = AFORK; 257 return (0); 258 } 259 260 /* 261 * Make child runnable and add to run queue. 262 */ 263 (void) splhigh(); 264 p2->p_stat = SRUN; 265 setrunqueue(p2); 266 (void) spl0(); 267 268 /* 269 * Now can be swapped. 270 */ 271 p1->p_holdcnt--; 272 273 /* 274 * Preserve synchronization semantics of vfork. If waiting for 275 * child to exec or exit, set P_PPWAIT on child, and sleep on our 276 * proc (in case of exit). 277 */ 278 if (isvfork) 279 while (p2->p_flag & P_PPWAIT) 280 tsleep(p1, PWAIT, "ppwait", 0); 281 282 /* 283 * Return child pid to parent process, 284 * marking us as parent via retval[1]. 285 */ 286 retval[0] = p2->p_pid; 287 retval[1] = 0; 288 return (0); 289 } 290