Commit | Line | Data |
---|---|---|
99fa88a2 | 1 | /* kern_proc.c 3.6 %H% */ |
29dd101b BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
5 | #include "../h/map.h" | |
6 | #include "../h/mtpr.h" | |
7 | #include "../h/dir.h" | |
8 | #include "../h/user.h" | |
9 | #include "../h/proc.h" | |
10 | #include "../h/buf.h" | |
11 | #include "../h/reg.h" | |
12 | #include "../h/inode.h" | |
13 | #include "../h/seg.h" | |
14 | #include "../h/acct.h" | |
bdfe5b0f | 15 | #include <wait.h> |
29dd101b BJ |
16 | #include "../h/pte.h" |
17 | #include "../h/vm.h" | |
18 | #include "../h/text.h" | |
bdfe5b0f | 19 | #include "../h/psl.h" |
29dd101b BJ |
20 | |
21 | /* | |
22 | * exec system call, with and without environments. | |
23 | */ | |
24 | struct execa { | |
25 | char *fname; | |
26 | char **argp; | |
27 | char **envp; | |
28 | }; | |
29 | ||
30 | exec() | |
31 | { | |
32 | ((struct execa *)u.u_ap)->envp = NULL; | |
33 | exece(); | |
34 | } | |
35 | ||
36 | exece() | |
37 | { | |
38 | register nc; | |
39 | register char *cp; | |
40 | register struct buf *bp; | |
41 | register struct execa *uap; | |
42 | int na, ne, ucp, ap, c; | |
43 | struct inode *ip; | |
44 | swblk_t bno; | |
45 | ||
46 | if ((ip = namei(uchar, 0)) == NULL) | |
47 | return; | |
48 | bno = 0; | |
49 | bp = 0; | |
50 | if(access(ip, IEXEC)) | |
51 | goto bad; | |
52 | if((ip->i_mode & IFMT) != IFREG || | |
53 | (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { | |
54 | u.u_error = EACCES; | |
55 | goto bad; | |
56 | } | |
57 | /* | |
58 | * Collect arguments on "file" in swap space. | |
59 | */ | |
60 | na = 0; | |
61 | ne = 0; | |
62 | nc = 0; | |
63 | uap = (struct execa *)u.u_ap; | |
64 | if ((bno = malloc(swapmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { | |
65 | swkill(u.u_procp, "exece"); | |
66 | goto bad; | |
67 | } | |
68 | if (bno % CLSIZE) | |
69 | panic("execa malloc"); | |
70 | if (uap->argp) for (;;) { | |
71 | ap = NULL; | |
72 | if (uap->argp) { | |
73 | ap = fuword((caddr_t)uap->argp); | |
74 | uap->argp++; | |
75 | } | |
76 | if (ap==NULL && uap->envp) { | |
77 | uap->argp = NULL; | |
78 | if ((ap = fuword((caddr_t)uap->envp)) == NULL) | |
79 | break; | |
80 | uap->envp++; | |
81 | ne++; | |
82 | } | |
83 | if (ap==NULL) | |
84 | break; | |
85 | na++; | |
86 | if(ap == -1) | |
87 | u.u_error = EFAULT; | |
88 | do { | |
89 | if (nc >= NCARGS-1) | |
90 | u.u_error = E2BIG; | |
91 | if ((c = fubyte((caddr_t)ap++)) < 0) | |
92 | u.u_error = EFAULT; | |
64d6118b BJ |
93 | if (u.u_error) { |
94 | if (bp) | |
95 | brelse(bp); | |
96 | bp = 0; | |
29dd101b | 97 | goto badarg; |
64d6118b | 98 | } |
29dd101b BJ |
99 | if ((nc&BMASK) == 0) { |
100 | if (bp) | |
101 | bdwrite(bp); | |
102 | bp = getblk(swapdev, (daddr_t)(dbtofsb(swplo+bno)+(nc>>BSHIFT))); | |
103 | cp = bp->b_un.b_addr; | |
104 | } | |
105 | nc++; | |
106 | *cp++ = c; | |
107 | } while (c>0); | |
108 | } | |
109 | if (bp) | |
110 | bdwrite(bp); | |
111 | bp = 0; | |
112 | nc = (nc + NBPW-1) & ~(NBPW-1); | |
113 | if (getxfile(ip, nc) || u.u_error) { | |
114 | badarg: | |
115 | for (c = 0; c < nc; c += BSIZE) | |
116 | if (bp = baddr(swapdev, dbtofsb(swplo+bno)+(c>>BSHIFT))) { | |
117 | bp->b_flags |= B_AGE; /* throw away */ | |
118 | bp->b_flags &= ~B_DELWRI; /* cancel io */ | |
119 | brelse(bp); | |
120 | bp = 0; | |
121 | } | |
122 | goto bad; | |
123 | } | |
124 | ||
125 | /* | |
126 | * copy back arglist | |
127 | */ | |
128 | ||
129 | ucp = USRSTACK - nc - NBPW; | |
130 | ap = ucp - na*NBPW - 3*NBPW; | |
131 | u.u_ar0[SP] = ap; | |
81263dba | 132 | (void) suword((caddr_t)ap, na-ne); |
29dd101b BJ |
133 | nc = 0; |
134 | for (;;) { | |
135 | ap += NBPW; | |
136 | if (na==ne) { | |
81263dba | 137 | (void) suword((caddr_t)ap, 0); |
29dd101b BJ |
138 | ap += NBPW; |
139 | } | |
140 | if (--na < 0) | |
141 | break; | |
81263dba | 142 | (void) suword((caddr_t)ap, ucp); |
29dd101b BJ |
143 | do { |
144 | if ((nc&BMASK) == 0) { | |
145 | if (bp) | |
146 | brelse(bp); | |
147 | bp = bread(swapdev, (daddr_t)(dbtofsb(swplo+bno)+(nc>>BSHIFT))); | |
148 | bp->b_flags |= B_AGE; /* throw away */ | |
149 | bp->b_flags &= ~B_DELWRI; /* cancel io */ | |
150 | cp = bp->b_un.b_addr; | |
151 | } | |
81263dba | 152 | (void) subyte((caddr_t)ucp++, (c = *cp++)); |
29dd101b BJ |
153 | nc++; |
154 | } while(c&0377); | |
155 | } | |
81263dba BJ |
156 | (void) suword((caddr_t)ap, 0); |
157 | (void) suword((caddr_t)ucp, 0); | |
29dd101b BJ |
158 | setregs(); |
159 | bad: | |
160 | if (bp) | |
161 | brelse(bp); | |
162 | if (bno) | |
163 | mfree(swapmap, ctod(clrnd((int) btoc(NCARGS))), bno); | |
164 | iput(ip); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Read in and set up memory for executed file. | |
169 | * Zero return is normal; | |
170 | * non-zero means only the text is being replaced | |
171 | */ | |
172 | getxfile(ip, nargc) | |
173 | register struct inode *ip; | |
174 | { | |
175 | register sep; | |
176 | register size_t ts, ds, ss; | |
177 | register int overlay; | |
178 | int pagi = 0; | |
179 | ||
180 | /* | |
181 | * read in first few bytes | |
182 | * of file for segment | |
183 | * sizes: | |
184 | * ux_mag = 407/410/411/405 | |
185 | * 407 is plain executable | |
186 | * 410 is RO text | |
187 | * 411 is separated ID | |
188 | * 405 is overlaid text | |
189 | * 412 is demand paged plain executable (NOT IMPLEMENTED) | |
190 | * 413 is demand paged RO text | |
191 | */ | |
192 | ||
193 | u.u_base = (caddr_t)&u.u_exdata; | |
194 | u.u_count = sizeof(u.u_exdata); | |
195 | u.u_offset = 0; | |
196 | u.u_segflg = 1; | |
197 | readi(ip); | |
198 | u.u_segflg = 0; | |
199 | if(u.u_error) | |
200 | goto bad; | |
201 | if (u.u_count!=0) { | |
202 | u.u_error = ENOEXEC; | |
203 | goto bad; | |
204 | } | |
205 | sep = 0; | |
206 | overlay = 0; | |
207 | switch (u.u_exdata.ux_mag) { | |
208 | ||
209 | case 0405: | |
210 | overlay++; | |
211 | break; | |
212 | ||
213 | case 0412: | |
214 | u.u_error = ENOEXEC; | |
215 | goto bad; | |
216 | ||
217 | case 0407: | |
218 | u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; | |
219 | u.u_exdata.ux_tsize = 0; | |
220 | break; | |
221 | ||
222 | case 0413: | |
223 | pagi = SPAGI; | |
224 | /* fall into ... */ | |
225 | ||
226 | case 0410: | |
227 | if (u.u_exdata.ux_tsize == 0) { | |
228 | u.u_error = ENOEXEC; | |
229 | goto bad; | |
230 | } | |
231 | break; | |
232 | ||
233 | case 0411: | |
234 | u.u_error = ENOEXEC; | |
235 | goto bad; | |
236 | ||
237 | default: | |
238 | u.u_error = ENOEXEC; | |
239 | goto bad; | |
240 | } | |
241 | if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) { | |
242 | u.u_error = ETXTBSY; | |
243 | goto bad; | |
244 | } | |
245 | ||
246 | /* | |
247 | * find text and data sizes | |
248 | * try them out for possible | |
249 | * exceed of max sizes | |
250 | */ | |
251 | ||
252 | ts = clrnd(btoc(u.u_exdata.ux_tsize)); | |
253 | ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); | |
254 | ss = clrnd(SSIZE + btoc(nargc)); | |
255 | if (overlay) { | |
256 | if ((u.u_procp->p_flag & SPAGI) || u.u_sep==0 && ctos(ts) != ctos(u.u_tsize) || nargc) { | |
257 | u.u_error = ENOMEM; | |
258 | goto bad; | |
259 | } | |
260 | ds = u.u_dsize; | |
261 | ss = u.u_ssize; | |
262 | sep = u.u_sep; | |
263 | xfree(); | |
264 | xalloc(ip, pagi); | |
265 | u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ | |
266 | } else { | |
267 | if (chksize(ts, ds, ss)) | |
268 | goto bad; | |
269 | u.u_cdmap = zdmap; | |
270 | u.u_csmap = zdmap; | |
271 | if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) | |
272 | goto bad; | |
273 | ||
274 | /* | |
275 | * At this point, committed to the new image! | |
276 | * Release virtual memory resources of old process, and | |
277 | * initialize the virtual memory of the new process. | |
278 | * If we resulted from vfork(), instead wakeup our | |
279 | * parent who will set SVFDONE when he has taken back | |
280 | * our resources. | |
281 | */ | |
282 | u.u_prof.pr_scale = 0; | |
283 | if ((u.u_procp->p_flag & SVFORK) == 0) | |
284 | vrelvm(); | |
285 | else { | |
286 | u.u_procp->p_flag &= ~SVFORK; | |
287 | u.u_procp->p_flag |= SKEEP; | |
288 | wakeup((caddr_t)u.u_procp); | |
289 | while ((u.u_procp->p_flag & SVFDONE) == 0) | |
290 | sleep((caddr_t)u.u_procp, PZERO - 1); | |
291 | u.u_procp->p_flag &= ~(SVFDONE|SKEEP); | |
292 | } | |
293 | u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM); | |
294 | u.u_procp->p_flag |= pagi; | |
295 | u.u_dmap = u.u_cdmap; | |
296 | u.u_smap = u.u_csmap; | |
297 | vgetvm(ts, ds, ss); | |
298 | ||
299 | if (pagi == 0) { | |
300 | /* | |
301 | * Read in data segment. | |
302 | */ | |
303 | u.u_base = (char *)ctob(ts); | |
304 | u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize; | |
305 | u.u_count = u.u_exdata.ux_dsize; | |
306 | readi(ip); | |
307 | } | |
308 | xalloc(ip, pagi); | |
309 | if (pagi && u.u_procp->p_textp) | |
310 | vinifod((struct fpte *)dptopte(u.u_procp, 0), | |
311 | PG_FTEXT, u.u_procp->p_textp->x_iptr, | |
312 | 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); | |
313 | ||
314 | /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ | |
315 | mtpr(TBIA,1); | |
316 | ||
317 | /* | |
318 | * set SUID/SGID protections, if no tracing | |
319 | */ | |
320 | if ((u.u_procp->p_flag&STRC)==0) { | |
321 | if(ip->i_mode&ISUID) | |
322 | if(u.u_uid != 0) { | |
323 | u.u_uid = ip->i_uid; | |
324 | u.u_procp->p_uid = ip->i_uid; | |
325 | } | |
326 | if(ip->i_mode&ISGID) | |
327 | u.u_gid = ip->i_gid; | |
328 | } else | |
594ebedd | 329 | psignal(u.u_procp, SIGTRAP); |
29dd101b BJ |
330 | } |
331 | u.u_tsize = ts; | |
332 | u.u_dsize = ds; | |
333 | u.u_ssize = ss; | |
334 | u.u_sep = sep; | |
335 | bad: | |
336 | return(overlay); | |
337 | } | |
338 | ||
339 | /* | |
340 | * Clear registers on exec | |
341 | */ | |
342 | setregs() | |
343 | { | |
594ebedd | 344 | register int (**rp)(); |
29dd101b | 345 | register i; |
bdfe5b0f | 346 | long sigmask; |
29dd101b | 347 | |
bdfe5b0f BJ |
348 | for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG]; |
349 | sigmask <<= 1, rp++) { | |
350 | switch (*rp) { | |
351 | ||
352 | case SIG_IGN: | |
353 | case SIG_DFL: | |
354 | case SIG_HOLD: | |
355 | continue; | |
356 | ||
357 | default: | |
358 | /* | |
99fa88a2 | 359 | * Normal or deferring catch; revert to default. |
bdfe5b0f | 360 | */ |
99fa88a2 BJ |
361 | (void) spl6(); |
362 | *rp = SIG_DFL; | |
bdfe5b0f BJ |
363 | if ((int)*rp & 1) |
364 | u.u_procp->p_siga0 |= sigmask; | |
365 | else | |
366 | u.u_procp->p_siga1 &= ~sigmask; | |
367 | if ((int)*rp & 2) | |
368 | u.u_procp->p_siga1 |= sigmask; | |
369 | else | |
370 | u.u_procp->p_siga1 &= ~sigmask; | |
99fa88a2 | 371 | (void) spl0(); |
bdfe5b0f BJ |
372 | continue; |
373 | } | |
374 | } | |
29dd101b BJ |
375 | /* |
376 | for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) | |
377 | *rp++ = 0; | |
378 | */ | |
379 | u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ | |
380 | for(i=0; i<NOFILE; i++) { | |
381 | if (u.u_pofile[i]&EXCLOSE) { | |
382 | closef(u.u_ofile[i]); | |
383 | u.u_ofile[i] = NULL; | |
bdfe5b0f | 384 | u.u_pofile[i] &= ~EXCLOSE; |
29dd101b | 385 | } |
29dd101b BJ |
386 | } |
387 | /* | |
388 | * Remember file name for accounting. | |
389 | */ | |
390 | u.u_acflag &= ~AFORK; | |
391 | bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ); | |
392 | } | |
393 | ||
394 | /* | |
395 | * exit system call: | |
396 | * pass back caller's arg | |
397 | */ | |
398 | rexit() | |
399 | { | |
400 | register struct a { | |
401 | int rval; | |
402 | } *uap; | |
403 | ||
404 | uap = (struct a *)u.u_ap; | |
405 | exit((uap->rval & 0377) << 8); | |
406 | } | |
407 | ||
408 | /* | |
409 | * Release resources. | |
410 | * Save u. area for parent to look at. | |
411 | * Enter zombie state. | |
412 | * Wake up parent and init processes, | |
413 | * and dispose of children. | |
414 | */ | |
415 | exit(rv) | |
416 | { | |
417 | register int i; | |
418 | register struct proc *p, *q; | |
419 | register struct file *f; | |
420 | register int x; | |
421 | ||
422 | #ifdef PGINPROF | |
423 | vmsizmon(); | |
424 | #endif | |
425 | p = u.u_procp; | |
426 | p->p_flag &= ~(STRC|SULOCK); | |
427 | p->p_flag |= SWEXIT; | |
428 | p->p_clktim = 0; | |
bdfe5b0f BJ |
429 | (void) spl6(); |
430 | if ((int)SIG_IGN & 1) | |
431 | p->p_siga0 = ~0; | |
432 | else | |
433 | p->p_siga0 = 0; | |
434 | if ((int)SIG_IGN & 2) | |
435 | p->p_siga1 = ~0; | |
436 | else | |
99fa88a2 | 437 | p->p_siga1 = 0; |
bdfe5b0f | 438 | (void) spl0(); |
29dd101b BJ |
439 | rate.v_pgin -= p->p_aveflt; |
440 | p->p_aveflt = 0; | |
441 | for(i=0; i<NSIG; i++) | |
594ebedd | 442 | u.u_signal[i] = SIG_IGN; |
29dd101b BJ |
443 | /* |
444 | * Release virtual memory. If we resulted from | |
445 | * a vfork(), instead give the resources back to | |
446 | * the parent. | |
447 | */ | |
448 | if ((p->p_flag & SVFORK) == 0) | |
449 | vrelvm(); | |
450 | else { | |
451 | p->p_flag &= ~SVFORK; | |
452 | wakeup((caddr_t)p); | |
453 | while ((p->p_flag & SVFDONE) == 0) | |
454 | sleep((caddr_t)p, PZERO - 1); | |
455 | p->p_flag &= ~SVFDONE; | |
456 | } | |
457 | for(i=0; i<NOFILE; i++) { | |
458 | f = u.u_ofile[i]; | |
459 | u.u_ofile[i] = NULL; | |
460 | closef(f); | |
461 | } | |
462 | plock(u.u_cdir); | |
463 | iput(u.u_cdir); | |
464 | if (u.u_rdir) { | |
465 | plock(u.u_rdir); | |
466 | iput(u.u_rdir); | |
467 | } | |
468 | acct(); | |
469 | vrelpt(u.u_procp); | |
470 | vrelu(u.u_procp, 0); | |
471 | multprog--; | |
472 | spl7(); /* clock will get mad because of overlaying */ | |
473 | p->p_stat = SZOMB; | |
474 | i = PIDHASH(p->p_pid); | |
475 | x = p - proc; | |
476 | if (pidhash[i] == x) | |
477 | pidhash[i] = p->p_idhash; | |
478 | else { | |
479 | for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) | |
480 | if (proc[i].p_idhash == x) { | |
481 | proc[i].p_idhash = p->p_idhash; | |
482 | goto done; | |
483 | } | |
484 | panic("exit"); | |
485 | } | |
486 | done: | |
487 | ((struct xproc *)p)->xp_xstat = rv; /* overlay */ | |
488 | ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ | |
489 | vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); | |
490 | for(q = &proc[0]; q < &proc[NPROC]; q++) | |
bdfe5b0f BJ |
491 | if(q->p_pptr == p) { |
492 | q->p_pptr = &proc[1]; | |
29dd101b | 493 | q->p_ppid = 1; |
bdfe5b0f BJ |
494 | wakeup((caddr_t)&proc[1]); |
495 | /* | |
496 | * Stopped or traced processes are killed | |
497 | * since their existence means someone is screwing up. | |
498 | */ | |
499 | if (q->p_stat == SSTOP || q->p_flag&STRC) { | |
500 | q->p_flag &= ~STRC; | |
501 | psignal(q, SIGKILL); | |
502 | } | |
29dd101b | 503 | } |
bdfe5b0f BJ |
504 | wakeup((caddr_t)p->p_pptr); |
505 | psignal(p->p_pptr, SIGCHLD); | |
29dd101b BJ |
506 | swtch(); |
507 | } | |
508 | ||
509 | wait() | |
510 | { | |
bdfe5b0f BJ |
511 | struct vtimes vm; |
512 | struct vtimes *vp; | |
29dd101b | 513 | |
bdfe5b0f BJ |
514 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { |
515 | wait1(0, (struct vtimes *)0); | |
516 | return; | |
517 | } | |
518 | vp = (struct vtimes *)u.u_ar0[R1]; | |
519 | wait1(u.u_ar0[R0], &vm); | |
520 | if (u.u_error) | |
521 | return; | |
522 | (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); | |
29dd101b BJ |
523 | } |
524 | ||
525 | /* | |
526 | * Wait system call. | |
527 | * Search for a terminated (zombie) child, | |
528 | * finally lay it to rest, and collect its status. | |
529 | * Look also for stopped (traced) children, | |
530 | * and pass back status from them. | |
531 | */ | |
bdfe5b0f BJ |
532 | wait1(options, vp) |
533 | register options; | |
29dd101b BJ |
534 | struct vtimes *vp; |
535 | { | |
536 | register f; | |
537 | register struct proc *p; | |
538 | ||
539 | f = 0; | |
29dd101b BJ |
540 | loop: |
541 | for(p = &proc[0]; p < &proc[NPROC]; p++) | |
bdfe5b0f | 542 | if(p->p_pptr == u.u_procp) { |
29dd101b BJ |
543 | f++; |
544 | if(p->p_stat == SZOMB) { | |
545 | u.u_r.r_val1 = p->p_pid; | |
546 | u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; | |
547 | ((struct xproc *)p)->xp_xstat = 0; | |
548 | if (vp) | |
549 | *vp = ((struct xproc *)p)->xp_vm; | |
550 | vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); | |
551 | ((struct xproc *)p)->xp_vm = zvms; | |
552 | p->p_stat = NULL; | |
553 | p->p_pid = 0; | |
554 | p->p_ppid = 0; | |
bdfe5b0f | 555 | p->p_pptr = 0; |
29dd101b | 556 | p->p_sig = 0; |
bdfe5b0f BJ |
557 | p->p_siga0 = 0; |
558 | p->p_siga1 = 0; | |
29dd101b BJ |
559 | p->p_pgrp = 0; |
560 | p->p_flag = 0; | |
561 | p->p_wchan = 0; | |
bdfe5b0f | 562 | p->p_cursig = 0; |
29dd101b BJ |
563 | return; |
564 | } | |
bdfe5b0f BJ |
565 | if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && |
566 | (p->p_flag&STRC || options&WUNTRACED)) { | |
567 | p->p_flag |= SWTED; | |
568 | u.u_r.r_val1 = p->p_pid; | |
569 | u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; | |
570 | return; | |
29dd101b BJ |
571 | } |
572 | } | |
bdfe5b0f BJ |
573 | if (f==0) { |
574 | u.u_error = ECHILD; | |
575 | return; | |
576 | } | |
577 | if (options&WNOHANG) { | |
578 | u.u_r.r_val1 = 0; | |
579 | return; | |
580 | } | |
99fa88a2 | 581 | /* |
bdfe5b0f BJ |
582 | if (setjmp(u.u_qsav)) { |
583 | u.u_eosys = RESTARTSYS; | |
584 | return; | |
29dd101b | 585 | } |
99fa88a2 | 586 | */ |
bdfe5b0f BJ |
587 | sleep((caddr_t)u.u_procp, PWAIT); |
588 | goto loop; | |
29dd101b BJ |
589 | } |
590 | ||
591 | /* | |
592 | * fork system call. | |
593 | */ | |
594 | fork() | |
595 | { | |
596 | ||
597 | u.u_cdmap = zdmap; | |
598 | u.u_csmap = zdmap; | |
599 | if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { | |
600 | u.u_r.r_val2 = 0; | |
601 | return; | |
602 | } | |
603 | fork1(0); | |
604 | } | |
605 | ||
606 | fork1(isvfork) | |
607 | { | |
608 | register struct proc *p1, *p2; | |
609 | register a; | |
610 | ||
611 | a = 0; | |
612 | p2 = NULL; | |
613 | for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) { | |
614 | if (p1->p_stat==NULL && p2==NULL) | |
615 | p2 = p1; | |
616 | else { | |
617 | if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) | |
618 | a++; | |
619 | } | |
620 | } | |
621 | /* | |
622 | * Disallow if | |
623 | * No processes at all; | |
624 | * not su and too many procs owned; or | |
625 | * not su and would take last slot. | |
626 | */ | |
627 | if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) { | |
628 | u.u_error = EAGAIN; | |
629 | if (!isvfork) { | |
81263dba BJ |
630 | (void) vsexpand(0, &u.u_cdmap, 1); |
631 | (void) vsexpand(0, &u.u_csmap, 1); | |
29dd101b BJ |
632 | } |
633 | goto out; | |
634 | } | |
635 | p1 = u.u_procp; | |
636 | if(newproc(isvfork)) { | |
637 | u.u_r.r_val1 = p1->p_pid; | |
638 | u.u_r.r_val2 = 1; /* child */ | |
639 | u.u_start = time; | |
640 | u.u_acflag = AFORK; | |
641 | return; | |
642 | } | |
643 | u.u_r.r_val1 = p2->p_pid; | |
644 | ||
645 | out: | |
646 | u.u_r.r_val2 = 0; | |
647 | } | |
648 | ||
649 | /* | |
650 | * break system call. | |
651 | * -- bad planning: "break" is a dirty word in C. | |
652 | */ | |
653 | sbreak() | |
654 | { | |
655 | struct a { | |
656 | char *nsiz; | |
657 | }; | |
658 | register int n, d; | |
659 | ||
660 | /* | |
661 | * set n to new data size | |
662 | * set d to new-old | |
663 | */ | |
664 | ||
665 | n = btoc(((struct a *)u.u_ap)->nsiz); | |
666 | if (!u.u_sep) | |
667 | n -= ctos(u.u_tsize) * stoc(1); | |
668 | if (n < 0) | |
669 | n = 0; | |
670 | d = clrnd(n - u.u_dsize); | |
671 | if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) | |
672 | return; | |
673 | if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0) | |
674 | return; | |
675 | expand(d, P0BR); | |
676 | } |