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