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