xref: /original-bsd/sys/kern/kern_fork.c (revision 3705696b)
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.1 (Berkeley) 06/10/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_nxt) {
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_nxt = allproc;
145 	 *	allproc->p_prev = &p2->p_nxt;
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_nxt = allproc;
151 	*(volatile struct proc ***)&allproc->p_prev =
152 	    (volatile struct proc **)&Vp2->p_nxt;
153 	*(volatile struct proc ***)&Vp2->p_prev = &allproc;
154 	allproc = Vp2;
155 #undef Vp2
156 	p2->p_link = NULL;			/* shouldn't be necessary */
157 	p2->p_rlink = NULL;			/* shouldn't be necessary */
158 
159 	/* Insert on the hash chain. */
160 	hash = &pidhash[PIDHASH(p2->p_pid)];
161 	p2->p_hash = *hash;
162 	*hash = p2;
163 
164 	/*
165 	 * Make a proc table entry for the new process.
166 	 * Start by zeroing the section of proc that is zero-initialized,
167 	 * then copy the section that is copied directly from the parent.
168 	 */
169 	bzero(&p2->p_startzero,
170 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
171 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
172 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
173 
174 	/*
175 	 * Duplicate sub-structures as needed.
176 	 * Increase reference counts on shared objects.
177 	 * The p_stats and p_sigacts substructs are set in vm_fork.
178 	 */
179 	p2->p_flag = SLOAD;
180 	if (p1->p_flag & SPROFIL)
181 		startprofclock(p2);
182 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
183 	    M_SUBPROC, M_WAITOK);
184 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
185 	p2->p_cred->p_refcnt = 1;
186 	crhold(p1->p_ucred);
187 
188 	p2->p_fd = fdcopy(p1);
189 	/*
190 	 * If p_limit is still copy-on-write, bump refcnt,
191 	 * otherwise get a copy that won't be modified.
192 	 * (If PL_SHAREMOD is clear, the structure is shared
193 	 * copy-on-write.)
194 	 */
195 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
196 		p2->p_limit = limcopy(p1->p_limit);
197 	else {
198 		p2->p_limit = p1->p_limit;
199 		p2->p_limit->p_refcnt++;
200 	}
201 
202 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & SCTTY)
203 		p2->p_flag |= SCTTY;
204 	if (isvfork)
205 		p2->p_flag |= SPPWAIT;
206 	p2->p_pgrpnxt = p1->p_pgrpnxt;
207 	p1->p_pgrpnxt = p2;
208 	p2->p_pptr = p1;
209 	p2->p_osptr = p1->p_cptr;
210 	if (p1->p_cptr)
211 		p1->p_cptr->p_ysptr = p2;
212 	p1->p_cptr = p2;
213 #ifdef KTRACE
214 	/*
215 	 * Copy traceflag and tracefile if enabled.
216 	 * If not inherited, these were zeroed above.
217 	 */
218 	if (p1->p_traceflag&KTRFAC_INHERIT) {
219 		p2->p_traceflag = p1->p_traceflag;
220 		if ((p2->p_tracep = p1->p_tracep) != NULL)
221 			VREF(p2->p_tracep);
222 	}
223 #endif
224 
225 	/*
226 	 * This begins the section where we must prevent the parent
227 	 * from being swapped.
228 	 */
229 	p1->p_flag |= SKEEP;
230 	/*
231 	 * Set return values for child before vm_fork,
232 	 * so they can be copied to child stack.
233 	 * We return parent pid, and mark as child in retval[1].
234 	 * NOTE: the kernel stack may be at a different location in the child
235 	 * process, and thus addresses of automatic variables (including retval)
236 	 * may be invalid after vm_fork returns in the child process.
237 	 */
238 	retval[0] = p1->p_pid;
239 	retval[1] = 1;
240 	if (vm_fork(p1, p2, isvfork)) {
241 		/*
242 		 * Child process.  Set start time and get to work.
243 		 */
244 		(void) splclock();
245 		p2->p_stats->p_start = time;
246 		(void) spl0();
247 		p2->p_acflag = AFORK;
248 		return (0);
249 	}
250 
251 	/*
252 	 * Make child runnable and add to run queue.
253 	 */
254 	(void) splhigh();
255 	p2->p_stat = SRUN;
256 	setrq(p2);
257 	(void) spl0();
258 
259 	/*
260 	 * Now can be swapped.
261 	 */
262 	p1->p_flag &= ~SKEEP;
263 
264 	/*
265 	 * Preserve synchronization semantics of vfork.
266 	 * If waiting for child to exec or exit, set SPPWAIT
267 	 * on child, and sleep on our proc (in case of exit).
268 	 */
269 	if (isvfork)
270 		while (p2->p_flag & SPPWAIT)
271 			tsleep((caddr_t)p1, PWAIT, "ppwait", 0);
272 
273 	/*
274 	 * Return child pid to parent process,
275 	 * marking us as parent via retval[1].
276 	 */
277 	retval[0] = p2->p_pid;
278 	retval[1] = 0;
279 	return (0);
280 }
281