xref: /original-bsd/sys/kern/kern_fork.c (revision 1e29b3fc)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_fork.c	7.37 (Berkeley) 07/07/92
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "map.h"
13 #include "filedesc.h"
14 #include "kernel.h"
15 #include "malloc.h"
16 #include "proc.h"
17 #include "resourcevar.h"
18 #include "vnode.h"
19 #include "file.h"
20 #include "acct.h"
21 #include "ktrace.h"
22 
23 /* ARGSUSED */
24 fork(p, uap, retval)
25 	struct proc *p;
26 	struct args *uap;
27 	int retval[];
28 {
29 
30 	return (fork1(p, 0, retval));
31 }
32 
33 /* ARGSUSED */
34 vfork(p, uap, retval)
35 	struct proc *p;
36 	struct args *uap;
37 	int retval[];
38 {
39 
40 	return (fork1(p, 1, retval));
41 }
42 
43 int	nprocs = 1;		/* process 0 */
44 
45 fork1(p1, isvfork, retval)
46 	register struct proc *p1;
47 	int isvfork, retval[];
48 {
49 	register struct proc *p2;
50 	register int count, uid;
51 	struct proc *newproc;
52 	struct proc **hash;
53 	static int nextpid, pidchecked = 0;
54 
55 	count = 0;
56 	if ((uid = p1->p_ucred->cr_uid) != 0) {
57 		for (p2 = (struct proc *)allproc; p2; p2 = p2->p_nxt)
58 			if (p2->p_ucred->cr_uid == uid)
59 				count++;
60 		for (p2 = zombproc; p2; p2 = p2->p_nxt)
61 			if (p2->p_ucred->cr_uid == uid)
62 				count++;
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 exceed its current limit or to bring us
68 	 * within one of the global limit; don't let root exceed the limit.
69 	 * nprocs is the current number of processes, maxproc is the limit.
70 	 */
71 	if (nprocs >= maxproc || uid == 0 && nprocs >= maxproc + 1) {
72 		tablefull("proc");
73 		return (EAGAIN);
74 	}
75 	if (count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)
76 		return (EAGAIN);
77 
78 	/* Allocate new proc. */
79 	MALLOC(newproc, struct proc *, sizeof(struct proc), M_PROC, M_WAITOK);
80 
81 	/*
82 	 * Find an unused process ID.  We remember a range of unused IDs
83 	 * ready to use (from nextpid+1 through pidchecked-1).
84 	 */
85 	nextpid++;
86 retry:
87 	/*
88 	 * If the process ID prototype has wrapped around,
89 	 * restart somewhat above 0, as the low-numbered procs
90 	 * tend to include daemons that don't exit.
91 	 */
92 	if (nextpid >= PID_MAX) {
93 		nextpid = 100;
94 		pidchecked = 0;
95 	}
96 	if (nextpid >= pidchecked) {
97 		int doingzomb = 0;
98 
99 		pidchecked = PID_MAX;
100 		/*
101 		 * Scan the active and zombie procs to check whether this pid
102 		 * is in use.  Remember the lowest pid that's greater
103 		 * than nextpid, so we can avoid checking for a while.
104 		 */
105 		p2 = (struct proc *)allproc;
106 again:
107 		for (; p2 != NULL; p2 = p2->p_nxt) {
108 			while (p2->p_pid == nextpid ||
109 			    p2->p_pgrp->pg_id == nextpid) {
110 				nextpid++;
111 				if (nextpid >= pidchecked)
112 					goto retry;
113 			}
114 			if (p2->p_pid > nextpid && pidchecked > p2->p_pid)
115 				pidchecked = p2->p_pid;
116 			if (p2->p_pgrp->pg_id > nextpid &&
117 			    pidchecked > p2->p_pgrp->pg_id)
118 				pidchecked = p2->p_pgrp->pg_id;
119 		}
120 		if (!doingzomb) {
121 			doingzomb = 1;
122 			p2 = zombproc;
123 			goto again;
124 		}
125 	}
126 
127 
128 	/* Link onto allproc (this should probably be delayed). */
129 	nprocs++;
130 	p2 = newproc;
131 	p2->p_stat = SIDL;			/* protect against others */
132 	p2->p_pid = nextpid;
133 	p2->p_nxt = (struct proc *)allproc;
134 	p2->p_nxt->p_prev = &p2->p_nxt;		/* allproc is never NULL */
135 	p2->p_prev = (struct proc **)&allproc;
136 	allproc = p2;
137 	p2->p_link = NULL;			/* shouldn't be necessary */
138 	p2->p_rlink = NULL;			/* shouldn't be necessary */
139 
140 	/* Insert on the hash chain. */
141 	hash = &pidhash[PIDHASH(p2->p_pid)];
142 	p2->p_hash = *hash;
143 	*hash = p2;
144 
145 	/*
146 	 * Make a proc table entry for the new process.
147 	 * Start by zeroing the section of proc that is zero-initialized,
148 	 * then copy the section that is copied directly from the parent.
149 	 */
150 	bzero(&p2->p_startzero,
151 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
152 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
153 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
154 
155 	/*
156 	 * Duplicate sub-structures as needed.
157 	 * Increase reference counts on shared objects.
158 	 * The p_stats and p_sigacts substructs are set in vm_fork.
159 	 */
160 	p2->p_flag = SLOAD | (p1->p_flag & SHPUX);
161 	if (p1->p_flag & SPROFIL)
162 		startprofclock(p2);
163 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
164 	    M_SUBPROC, M_WAITOK);
165 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
166 	p2->p_cred->p_refcnt = 1;
167 	crhold(p1->p_ucred);
168 
169 	p2->p_fd = fdcopy(p1);
170 	/*
171 	 * If p_limit is still copy-on-write, bump refcnt,
172 	 * otherwise get a copy that won't be modified.
173 	 * (If PL_SHAREMOD is clear, the structure is shared
174 	 * copy-on-write.)
175 	 */
176 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
177 		p2->p_limit = limcopy(p1->p_limit);
178 	else {
179 		p2->p_limit = p1->p_limit;
180 		p2->p_limit->p_refcnt++;
181 	}
182 
183 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & SCTTY)
184 		p2->p_flag |= SCTTY;
185 	if (isvfork)
186 		p2->p_flag |= SPPWAIT;
187 	p2->p_pgrpnxt = p1->p_pgrpnxt;
188 	p1->p_pgrpnxt = p2;
189 	p2->p_pptr = p1;
190 	p2->p_osptr = p1->p_cptr;
191 	if (p1->p_cptr)
192 		p1->p_cptr->p_ysptr = p2;
193 	p1->p_cptr = p2;
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 |= SKEEP;
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 	setrq(p2);
238 	(void) spl0();
239 
240 	/*
241 	 * Now can be swapped.
242 	 */
243 	p1->p_flag &= ~SKEEP;
244 
245 	/*
246 	 * Preserve synchronization semantics of vfork.
247 	 * If waiting for child to exec or exit, set SPPWAIT
248 	 * on child, and sleep on our proc (in case of exit).
249 	 */
250 	if (isvfork)
251 		while (p2->p_flag & SPPWAIT)
252 			tsleep((caddr_t)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