Commit | Line | Data |
---|---|---|
1d348849 | 1 | /* kern_fork.c 6.2 84/05/22 */ |
50108d5c SL |
2 | |
3 | #include "../machine/reg.h" | |
4 | #include "../machine/pte.h" | |
5 | #include "../machine/psl.h" | |
6 | ||
7 | #include "../h/param.h" | |
8 | #include "../h/systm.h" | |
9 | #include "../h/map.h" | |
10 | #include "../h/dir.h" | |
11 | #include "../h/user.h" | |
12 | #include "../h/kernel.h" | |
13 | #include "../h/proc.h" | |
14 | #include "../h/inode.h" | |
15 | #include "../h/seg.h" | |
16 | #include "../h/vm.h" | |
17 | #include "../h/text.h" | |
18 | #include "../h/file.h" | |
19 | #include "../h/acct.h" | |
20 | #include "../h/quota.h" | |
21 | ||
22 | /* | |
23 | * fork system call. | |
24 | */ | |
25 | fork() | |
26 | { | |
27 | ||
28 | u.u_cdmap = zdmap; | |
29 | u.u_csmap = zdmap; | |
30 | if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { | |
31 | u.u_r.r_val2 = 0; | |
32 | return; | |
33 | } | |
34 | fork1(0); | |
35 | } | |
36 | ||
37 | vfork() | |
38 | { | |
39 | ||
40 | fork1(1); | |
41 | } | |
42 | ||
43 | fork1(isvfork) | |
44 | int isvfork; | |
45 | { | |
46 | register struct proc *p1, *p2; | |
47 | register a; | |
48 | ||
49 | a = 0; | |
1d348849 MK |
50 | if (u.u_uid != 0) { |
51 | for (p1 = allproc; p1; p1 = p1->p_nxt) | |
52 | if (p1->p_uid == u.u_uid) | |
53 | a++; | |
54 | for (p1 = zombproc; p1; p1 = p1->p_nxt) | |
55 | if (p1->p_uid == u.u_uid) | |
50108d5c | 56 | a++; |
50108d5c SL |
57 | } |
58 | /* | |
59 | * Disallow if | |
60 | * No processes at all; | |
61 | * not su and too many procs owned; or | |
62 | * not su and would take last slot. | |
63 | */ | |
1d348849 | 64 | p2 = freeproc; |
50108d5c SL |
65 | if (p2==NULL) |
66 | tablefull("proc"); | |
1d348849 | 67 | if (p2==NULL || (u.u_uid!=0 && (p2->p_nxt == NULL || a>MAXUPRC))) { |
50108d5c SL |
68 | u.u_error = EAGAIN; |
69 | if (!isvfork) { | |
70 | (void) vsexpand(0, &u.u_cdmap, 1); | |
71 | (void) vsexpand(0, &u.u_csmap, 1); | |
72 | } | |
73 | goto out; | |
74 | } | |
75 | p1 = u.u_procp; | |
76 | if (newproc(isvfork)) { | |
77 | u.u_r.r_val1 = p1->p_pid; | |
78 | u.u_r.r_val2 = 1; /* child */ | |
79 | u.u_start = time.tv_sec; | |
80 | u.u_acflag = AFORK; | |
81 | return; | |
82 | } | |
83 | u.u_r.r_val1 = p2->p_pid; | |
84 | ||
85 | out: | |
86 | u.u_r.r_val2 = 0; | |
87 | } | |
88 | ||
89 | /* | |
90 | * Create a new process-- the internal version of | |
91 | * sys fork. | |
92 | * It returns 1 in the new process, 0 in the old. | |
93 | */ | |
94 | newproc(isvfork) | |
95 | int isvfork; | |
96 | { | |
50108d5c SL |
97 | register struct proc *rpp, *rip; |
98 | register int n; | |
99 | register struct file *fp; | |
1d348849 | 100 | static int pidchecked = 0; |
50108d5c | 101 | |
50108d5c SL |
102 | /* |
103 | * First, just locate a slot for a process | |
104 | * and copy the useful info from this process into it. | |
105 | * The panic "cannot happen" because fork has already | |
106 | * checked for the existence of a slot. | |
107 | */ | |
50108d5c | 108 | mpid++; |
1d348849 | 109 | retry: |
50108d5c | 110 | if (mpid >= 30000) { |
1d348849 MK |
111 | mpid = 100; |
112 | pidchecked = 0; | |
50108d5c | 113 | } |
1d348849 MK |
114 | if (mpid >= pidchecked) { |
115 | int doingzomb = 0; | |
116 | pidchecked = 30000; | |
117 | /* | |
118 | * Scan the proc table to check whether this pid | |
119 | * is in use. Remember the lowest pid that's greater | |
120 | * than mpid, so we can avoid checking for a while. | |
121 | */ | |
122 | rpp = allproc; | |
123 | again: | |
124 | for (; rpp != NULL; rpp = rpp->p_nxt) { | |
125 | if (rpp->p_pid==mpid || rpp->p_pgrp==mpid) { | |
126 | mpid++; | |
127 | if (mpid >= pidchecked) | |
128 | goto retry; | |
129 | } | |
130 | if ((rpp->p_pid > mpid) && (pidchecked > rpp->p_pid)) | |
131 | pidchecked = rpp->p_pid; | |
132 | if ((rpp->p_pgrp > mpid) && (pidchecked > rpp->p_pgrp)) | |
133 | pidchecked = rpp->p_pgrp; | |
134 | } | |
135 | if (!doingzomb) { | |
136 | doingzomb = 1; | |
137 | rpp = zombproc; | |
138 | goto again; | |
139 | } | |
50108d5c | 140 | } |
1d348849 | 141 | if ((rpp = freeproc) == NULL) |
50108d5c SL |
142 | panic("no procs"); |
143 | ||
1d348849 MK |
144 | freeproc = rpp->p_nxt; /* off freeproc */ |
145 | rpp->p_nxt = allproc; /* onto allproc */ | |
146 | rpp->p_nxt->p_prev = &rpp->p_nxt; /* (allproc is never NULL) */ | |
147 | rpp->p_prev = &allproc; | |
148 | allproc = rpp; | |
149 | ||
50108d5c SL |
150 | /* |
151 | * Make a proc table entry for the new process. | |
152 | */ | |
153 | rip = u.u_procp; | |
154 | #ifdef QUOTA | |
155 | rpp->p_quota = rip->p_quota; | |
156 | rpp->p_quota->q_cnt++; | |
157 | #endif | |
158 | rpp->p_stat = SIDL; | |
159 | timerclear(&rpp->p_realtimer.it_value); | |
dd012d1e | 160 | rpp->p_flag = SLOAD | (rip->p_flag & (SPAGI|SOUSIG)); |
50108d5c SL |
161 | if (isvfork) { |
162 | rpp->p_flag |= SVFORK; | |
163 | rpp->p_ndx = rip->p_ndx; | |
164 | } else | |
165 | rpp->p_ndx = rpp - proc; | |
166 | rpp->p_uid = rip->p_uid; | |
167 | rpp->p_pgrp = rip->p_pgrp; | |
168 | rpp->p_nice = rip->p_nice; | |
169 | rpp->p_textp = isvfork ? 0 : rip->p_textp; | |
170 | rpp->p_pid = mpid; | |
171 | rpp->p_ppid = rip->p_pid; | |
172 | rpp->p_pptr = rip; | |
173 | rpp->p_osptr = rip->p_cptr; | |
174 | if (rip->p_cptr) | |
175 | rip->p_cptr->p_ysptr = rpp; | |
176 | rpp->p_ysptr = NULL; | |
177 | rpp->p_cptr = NULL; | |
178 | rip->p_cptr = rpp; | |
179 | rpp->p_time = 0; | |
180 | rpp->p_cpu = 0; | |
dd012d1e SL |
181 | rpp->p_sigmask = rip->p_sigmask; |
182 | rpp->p_sigcatch = rip->p_sigcatch; | |
183 | rpp->p_sigignore = rip->p_sigignore; | |
184 | /* take along any pending signals like stops? */ | |
50108d5c SL |
185 | if (isvfork) { |
186 | rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0; | |
187 | rpp->p_szpt = clrnd(ctopt(UPAGES)); | |
188 | forkstat.cntvfork++; | |
189 | forkstat.sizvfork += rip->p_dsize + rip->p_ssize; | |
190 | } else { | |
191 | rpp->p_tsize = rip->p_tsize; | |
192 | rpp->p_dsize = rip->p_dsize; | |
193 | rpp->p_ssize = rip->p_ssize; | |
194 | rpp->p_szpt = rip->p_szpt; | |
195 | forkstat.cntfork++; | |
196 | forkstat.sizfork += rip->p_dsize + rip->p_ssize; | |
197 | } | |
198 | rpp->p_rssize = 0; | |
199 | rpp->p_maxrss = rip->p_maxrss; | |
200 | rpp->p_wchan = 0; | |
201 | rpp->p_slptime = 0; | |
202 | rpp->p_pctcpu = 0; | |
203 | rpp->p_cpticks = 0; | |
204 | n = PIDHASH(rpp->p_pid); | |
1d348849 | 205 | rpp->p_idhash = pidhash[n]; |
50108d5c SL |
206 | pidhash[n] = rpp - proc; |
207 | multprog++; | |
208 | ||
209 | /* | |
210 | * Increase reference counts on shared objects. | |
211 | */ | |
212 | for (n = 0; n < NOFILE; n++) { | |
213 | fp = u.u_ofile[n]; | |
214 | if (fp == NULL) | |
215 | continue; | |
216 | fp->f_count++; | |
50108d5c SL |
217 | } |
218 | u.u_cdir->i_count++; | |
219 | if (u.u_rdir) | |
220 | u.u_rdir->i_count++; | |
221 | ||
222 | /* | |
50108d5c SL |
223 | * This begins the section where we must prevent the parent |
224 | * from being swapped. | |
225 | */ | |
226 | rip->p_flag |= SKEEP; | |
227 | if (procdup(rpp, isvfork)) | |
228 | return (1); | |
229 | ||
230 | /* | |
231 | * Make child runnable and add to run queue. | |
232 | */ | |
233 | (void) spl6(); | |
234 | rpp->p_stat = SRUN; | |
235 | setrq(rpp); | |
236 | (void) spl0(); | |
237 | ||
238 | /* | |
239 | * Cause child to take a non-local goto as soon as it runs. | |
240 | * On older systems this was done with SSWAP bit in proc | |
241 | * table; on VAX we use u.u_pcb.pcb_sswap so don't need | |
242 | * to do rpp->p_flag |= SSWAP. Actually do nothing here. | |
243 | */ | |
244 | /* rpp->p_flag |= SSWAP; */ | |
245 | ||
246 | /* | |
247 | * Now can be swapped. | |
248 | */ | |
249 | rip->p_flag &= ~SKEEP; | |
250 | ||
251 | /* | |
252 | * If vfork make chain from parent process to child | |
253 | * (where virtal memory is temporarily). Wait for | |
254 | * child to finish, steal virtual memory back, | |
255 | * and wakeup child to let it die. | |
256 | */ | |
257 | if (isvfork) { | |
258 | u.u_procp->p_xlink = rpp; | |
259 | u.u_procp->p_flag |= SNOVM; | |
260 | while (rpp->p_flag & SVFORK) | |
261 | sleep((caddr_t)rpp, PZERO - 1); | |
262 | if ((rpp->p_flag & SLOAD) == 0) | |
263 | panic("newproc vfork"); | |
264 | uaccess(rpp, Vfmap, &vfutl); | |
265 | u.u_procp->p_xlink = 0; | |
266 | vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap); | |
267 | u.u_procp->p_flag &= ~SNOVM; | |
268 | rpp->p_ndx = rpp - proc; | |
269 | rpp->p_flag |= SVFDONE; | |
270 | wakeup((caddr_t)rpp); | |
271 | } | |
272 | ||
273 | /* | |
274 | * 0 return means parent. | |
275 | */ | |
276 | return (0); | |
277 | } |