xref: /original-bsd/sys/kern/kern_fork.c (revision c7f94de1)
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.38 (Berkeley) 07/10/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 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 int count, uid;
54 	struct proc *newproc;
55 	struct proc **hash;
56 	static int nextpid, pidchecked = 0;
57 
58 	count = 0;
59 	if ((uid = p1->p_ucred->cr_uid) != 0) {
60 		for (p2 = (struct proc *)allproc; p2; p2 = p2->p_nxt)
61 			if (p2->p_ucred->cr_uid == uid)
62 				count++;
63 		for (p2 = zombproc; p2; p2 = p2->p_nxt)
64 			if (p2->p_ucred->cr_uid == uid)
65 				count++;
66 	}
67 	/*
68 	 * Although process entries are dynamically created, we still keep
69 	 * a global limit on the maximum number we will create.  Don't allow
70 	 * a nonprivileged user to exceed its current limit or to bring us
71 	 * within one of the global limit; don't let root exceed the limit.
72 	 * nprocs is the current number of processes, maxproc is the limit.
73 	 */
74 	if (nprocs >= maxproc || uid == 0 && nprocs >= maxproc + 1) {
75 		tablefull("proc");
76 		return (EAGAIN);
77 	}
78 	if (count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)
79 		return (EAGAIN);
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 	/* Link onto allproc (this should probably be delayed). */
132 	nprocs++;
133 	p2 = newproc;
134 	p2->p_stat = SIDL;			/* protect against others */
135 	p2->p_pid = nextpid;
136 	p2->p_nxt = (struct proc *)allproc;
137 	p2->p_nxt->p_prev = &p2->p_nxt;		/* allproc is never NULL */
138 	p2->p_prev = (struct proc **)&allproc;
139 	allproc = p2;
140 	p2->p_link = NULL;			/* shouldn't be necessary */
141 	p2->p_rlink = NULL;			/* shouldn't be necessary */
142 
143 	/* Insert on the hash chain. */
144 	hash = &pidhash[PIDHASH(p2->p_pid)];
145 	p2->p_hash = *hash;
146 	*hash = p2;
147 
148 	/*
149 	 * Make a proc table entry for the new process.
150 	 * Start by zeroing the section of proc that is zero-initialized,
151 	 * then copy the section that is copied directly from the parent.
152 	 */
153 	bzero(&p2->p_startzero,
154 	    (unsigned) ((caddr_t)&p2->p_endzero - (caddr_t)&p2->p_startzero));
155 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
156 	    (unsigned) ((caddr_t)&p2->p_endcopy - (caddr_t)&p2->p_startcopy));
157 
158 	/*
159 	 * Duplicate sub-structures as needed.
160 	 * Increase reference counts on shared objects.
161 	 * The p_stats and p_sigacts substructs are set in vm_fork.
162 	 */
163 	p2->p_flag = SLOAD | (p1->p_flag & SHPUX);
164 	if (p1->p_flag & SPROFIL)
165 		startprofclock(p2);
166 	MALLOC(p2->p_cred, struct pcred *, sizeof(struct pcred),
167 	    M_SUBPROC, M_WAITOK);
168 	bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred));
169 	p2->p_cred->p_refcnt = 1;
170 	crhold(p1->p_ucred);
171 
172 	p2->p_fd = fdcopy(p1);
173 	/*
174 	 * If p_limit is still copy-on-write, bump refcnt,
175 	 * otherwise get a copy that won't be modified.
176 	 * (If PL_SHAREMOD is clear, the structure is shared
177 	 * copy-on-write.)
178 	 */
179 	if (p1->p_limit->p_lflags & PL_SHAREMOD)
180 		p2->p_limit = limcopy(p1->p_limit);
181 	else {
182 		p2->p_limit = p1->p_limit;
183 		p2->p_limit->p_refcnt++;
184 	}
185 
186 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & SCTTY)
187 		p2->p_flag |= SCTTY;
188 	if (isvfork)
189 		p2->p_flag |= SPPWAIT;
190 	p2->p_pgrpnxt = p1->p_pgrpnxt;
191 	p1->p_pgrpnxt = p2;
192 	p2->p_pptr = p1;
193 	p2->p_osptr = p1->p_cptr;
194 	if (p1->p_cptr)
195 		p1->p_cptr->p_ysptr = p2;
196 	p1->p_cptr = p2;
197 #ifdef KTRACE
198 	/*
199 	 * Copy traceflag and tracefile if enabled.
200 	 * If not inherited, these were zeroed above.
201 	 */
202 	if (p1->p_traceflag&KTRFAC_INHERIT) {
203 		p2->p_traceflag = p1->p_traceflag;
204 		if ((p2->p_tracep = p1->p_tracep) != NULL)
205 			VREF(p2->p_tracep);
206 	}
207 #endif
208 
209 	/*
210 	 * This begins the section where we must prevent the parent
211 	 * from being swapped.
212 	 */
213 	p1->p_flag |= SKEEP;
214 	/*
215 	 * Set return values for child before vm_fork,
216 	 * so they can be copied to child stack.
217 	 * We return parent pid, and mark as child in retval[1].
218 	 * NOTE: the kernel stack may be at a different location in the child
219 	 * process, and thus addresses of automatic variables (including retval)
220 	 * may be invalid after vm_fork returns in the child process.
221 	 */
222 	retval[0] = p1->p_pid;
223 	retval[1] = 1;
224 	if (vm_fork(p1, p2, isvfork)) {
225 		/*
226 		 * Child process.  Set start time and get to work.
227 		 */
228 		(void) splclock();
229 		p2->p_stats->p_start = time;
230 		(void) spl0();
231 		p2->p_acflag = AFORK;
232 		return (0);
233 	}
234 
235 	/*
236 	 * Make child runnable and add to run queue.
237 	 */
238 	(void) splhigh();
239 	p2->p_stat = SRUN;
240 	setrq(p2);
241 	(void) spl0();
242 
243 	/*
244 	 * Now can be swapped.
245 	 */
246 	p1->p_flag &= ~SKEEP;
247 
248 	/*
249 	 * Preserve synchronization semantics of vfork.
250 	 * If waiting for child to exec or exit, set SPPWAIT
251 	 * on child, and sleep on our proc (in case of exit).
252 	 */
253 	if (isvfork)
254 		while (p2->p_flag & SPPWAIT)
255 			tsleep((caddr_t)p1, PWAIT, "ppwait", 0);
256 
257 	/*
258 	 * Return child pid to parent process,
259 	 * marking us as parent via retval[1].
260 	 */
261 	retval[0] = p2->p_pid;
262 	retval[1] = 0;
263 	return (0);
264 }
265