Commit | Line | Data |
---|---|---|
bdfe5b0f | 1 | /* kern_proc.c 3.5 %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 | /* | |
359 | * Normal or deferring catch. | |
360 | * If deferred, now hold it, else | |
361 | * revert to default. | |
362 | */ | |
363 | if (SIGISDEFER(*rp)) | |
364 | *rp = SIG_HOLD; | |
365 | else | |
366 | *rp = SIG_DFL; | |
367 | if ((int)*rp & 1) | |
368 | u.u_procp->p_siga0 |= sigmask; | |
369 | else | |
370 | u.u_procp->p_siga1 &= ~sigmask; | |
371 | if ((int)*rp & 2) | |
372 | u.u_procp->p_siga1 |= sigmask; | |
373 | else | |
374 | u.u_procp->p_siga1 &= ~sigmask; | |
375 | continue; | |
376 | } | |
377 | } | |
29dd101b BJ |
378 | /* |
379 | for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) | |
380 | *rp++ = 0; | |
381 | */ | |
382 | u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ | |
383 | for(i=0; i<NOFILE; i++) { | |
384 | if (u.u_pofile[i]&EXCLOSE) { | |
385 | closef(u.u_ofile[i]); | |
386 | u.u_ofile[i] = NULL; | |
bdfe5b0f | 387 | u.u_pofile[i] &= ~EXCLOSE; |
29dd101b | 388 | } |
29dd101b BJ |
389 | } |
390 | /* | |
391 | * Remember file name for accounting. | |
392 | */ | |
393 | u.u_acflag &= ~AFORK; | |
394 | bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ); | |
395 | } | |
396 | ||
397 | /* | |
398 | * exit system call: | |
399 | * pass back caller's arg | |
400 | */ | |
401 | rexit() | |
402 | { | |
403 | register struct a { | |
404 | int rval; | |
405 | } *uap; | |
406 | ||
407 | uap = (struct a *)u.u_ap; | |
408 | exit((uap->rval & 0377) << 8); | |
409 | } | |
410 | ||
411 | /* | |
412 | * Release resources. | |
413 | * Save u. area for parent to look at. | |
414 | * Enter zombie state. | |
415 | * Wake up parent and init processes, | |
416 | * and dispose of children. | |
417 | */ | |
418 | exit(rv) | |
419 | { | |
420 | register int i; | |
421 | register struct proc *p, *q; | |
422 | register struct file *f; | |
423 | register int x; | |
424 | ||
425 | #ifdef PGINPROF | |
426 | vmsizmon(); | |
427 | #endif | |
428 | p = u.u_procp; | |
429 | p->p_flag &= ~(STRC|SULOCK); | |
430 | p->p_flag |= SWEXIT; | |
431 | p->p_clktim = 0; | |
bdfe5b0f BJ |
432 | (void) spl6(); |
433 | if ((int)SIG_IGN & 1) | |
434 | p->p_siga0 = ~0; | |
435 | else | |
436 | p->p_siga0 = 0; | |
437 | if ((int)SIG_IGN & 2) | |
438 | p->p_siga1 = ~0; | |
439 | else | |
440 | p->p_siga0 = 0; | |
441 | (void) spl0(); | |
29dd101b BJ |
442 | rate.v_pgin -= p->p_aveflt; |
443 | p->p_aveflt = 0; | |
444 | for(i=0; i<NSIG; i++) | |
594ebedd | 445 | u.u_signal[i] = SIG_IGN; |
29dd101b BJ |
446 | /* |
447 | * Release virtual memory. If we resulted from | |
448 | * a vfork(), instead give the resources back to | |
449 | * the parent. | |
450 | */ | |
451 | if ((p->p_flag & SVFORK) == 0) | |
452 | vrelvm(); | |
453 | else { | |
454 | p->p_flag &= ~SVFORK; | |
455 | wakeup((caddr_t)p); | |
456 | while ((p->p_flag & SVFDONE) == 0) | |
457 | sleep((caddr_t)p, PZERO - 1); | |
458 | p->p_flag &= ~SVFDONE; | |
459 | } | |
460 | for(i=0; i<NOFILE; i++) { | |
461 | f = u.u_ofile[i]; | |
462 | u.u_ofile[i] = NULL; | |
463 | closef(f); | |
464 | } | |
465 | plock(u.u_cdir); | |
466 | iput(u.u_cdir); | |
467 | if (u.u_rdir) { | |
468 | plock(u.u_rdir); | |
469 | iput(u.u_rdir); | |
470 | } | |
471 | acct(); | |
472 | vrelpt(u.u_procp); | |
473 | vrelu(u.u_procp, 0); | |
474 | multprog--; | |
475 | spl7(); /* clock will get mad because of overlaying */ | |
476 | p->p_stat = SZOMB; | |
477 | i = PIDHASH(p->p_pid); | |
478 | x = p - proc; | |
479 | if (pidhash[i] == x) | |
480 | pidhash[i] = p->p_idhash; | |
481 | else { | |
482 | for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) | |
483 | if (proc[i].p_idhash == x) { | |
484 | proc[i].p_idhash = p->p_idhash; | |
485 | goto done; | |
486 | } | |
487 | panic("exit"); | |
488 | } | |
489 | done: | |
490 | ((struct xproc *)p)->xp_xstat = rv; /* overlay */ | |
491 | ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ | |
492 | vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); | |
493 | for(q = &proc[0]; q < &proc[NPROC]; q++) | |
bdfe5b0f BJ |
494 | if(q->p_pptr == p) { |
495 | q->p_pptr = &proc[1]; | |
29dd101b | 496 | q->p_ppid = 1; |
bdfe5b0f BJ |
497 | wakeup((caddr_t)&proc[1]); |
498 | /* | |
499 | * Stopped or traced processes are killed | |
500 | * since their existence means someone is screwing up. | |
501 | */ | |
502 | if (q->p_stat == SSTOP || q->p_flag&STRC) { | |
503 | q->p_flag &= ~STRC; | |
504 | psignal(q, SIGKILL); | |
505 | } | |
29dd101b | 506 | } |
bdfe5b0f BJ |
507 | wakeup((caddr_t)p->p_pptr); |
508 | psignal(p->p_pptr, SIGCHLD); | |
29dd101b BJ |
509 | swtch(); |
510 | } | |
511 | ||
512 | wait() | |
513 | { | |
bdfe5b0f BJ |
514 | struct vtimes vm; |
515 | struct vtimes *vp; | |
29dd101b | 516 | |
bdfe5b0f BJ |
517 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { |
518 | wait1(0, (struct vtimes *)0); | |
519 | return; | |
520 | } | |
521 | vp = (struct vtimes *)u.u_ar0[R1]; | |
522 | wait1(u.u_ar0[R0], &vm); | |
523 | if (u.u_error) | |
524 | return; | |
525 | (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); | |
29dd101b BJ |
526 | } |
527 | ||
528 | /* | |
529 | * Wait system call. | |
530 | * Search for a terminated (zombie) child, | |
531 | * finally lay it to rest, and collect its status. | |
532 | * Look also for stopped (traced) children, | |
533 | * and pass back status from them. | |
534 | */ | |
bdfe5b0f BJ |
535 | wait1(options, vp) |
536 | register options; | |
29dd101b BJ |
537 | struct vtimes *vp; |
538 | { | |
539 | register f; | |
540 | register struct proc *p; | |
541 | ||
542 | f = 0; | |
29dd101b BJ |
543 | loop: |
544 | for(p = &proc[0]; p < &proc[NPROC]; p++) | |
bdfe5b0f | 545 | if(p->p_pptr == u.u_procp) { |
29dd101b BJ |
546 | f++; |
547 | if(p->p_stat == SZOMB) { | |
548 | u.u_r.r_val1 = p->p_pid; | |
549 | u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; | |
550 | ((struct xproc *)p)->xp_xstat = 0; | |
551 | if (vp) | |
552 | *vp = ((struct xproc *)p)->xp_vm; | |
553 | vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); | |
554 | ((struct xproc *)p)->xp_vm = zvms; | |
555 | p->p_stat = NULL; | |
556 | p->p_pid = 0; | |
557 | p->p_ppid = 0; | |
bdfe5b0f | 558 | p->p_pptr = 0; |
29dd101b | 559 | p->p_sig = 0; |
bdfe5b0f BJ |
560 | p->p_siga0 = 0; |
561 | p->p_siga1 = 0; | |
29dd101b BJ |
562 | p->p_pgrp = 0; |
563 | p->p_flag = 0; | |
564 | p->p_wchan = 0; | |
bdfe5b0f | 565 | p->p_cursig = 0; |
29dd101b BJ |
566 | return; |
567 | } | |
bdfe5b0f BJ |
568 | if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && |
569 | (p->p_flag&STRC || options&WUNTRACED)) { | |
570 | p->p_flag |= SWTED; | |
571 | u.u_r.r_val1 = p->p_pid; | |
572 | u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; | |
573 | return; | |
29dd101b BJ |
574 | } |
575 | } | |
bdfe5b0f BJ |
576 | if (f==0) { |
577 | u.u_error = ECHILD; | |
578 | return; | |
579 | } | |
580 | if (options&WNOHANG) { | |
581 | u.u_r.r_val1 = 0; | |
582 | return; | |
583 | } | |
584 | if (setjmp(u.u_qsav)) { | |
585 | u.u_eosys = RESTARTSYS; | |
586 | return; | |
29dd101b | 587 | } |
bdfe5b0f BJ |
588 | sleep((caddr_t)u.u_procp, PWAIT); |
589 | goto loop; | |
29dd101b BJ |
590 | } |
591 | ||
592 | /* | |
593 | * fork system call. | |
594 | */ | |
595 | fork() | |
596 | { | |
597 | ||
598 | u.u_cdmap = zdmap; | |
599 | u.u_csmap = zdmap; | |
600 | if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { | |
601 | u.u_r.r_val2 = 0; | |
602 | return; | |
603 | } | |
604 | fork1(0); | |
605 | } | |
606 | ||
607 | fork1(isvfork) | |
608 | { | |
609 | register struct proc *p1, *p2; | |
610 | register a; | |
611 | ||
612 | a = 0; | |
613 | p2 = NULL; | |
614 | for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) { | |
615 | if (p1->p_stat==NULL && p2==NULL) | |
616 | p2 = p1; | |
617 | else { | |
618 | if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) | |
619 | a++; | |
620 | } | |
621 | } | |
622 | /* | |
623 | * Disallow if | |
624 | * No processes at all; | |
625 | * not su and too many procs owned; or | |
626 | * not su and would take last slot. | |
627 | */ | |
628 | if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) { | |
629 | u.u_error = EAGAIN; | |
630 | if (!isvfork) { | |
81263dba BJ |
631 | (void) vsexpand(0, &u.u_cdmap, 1); |
632 | (void) vsexpand(0, &u.u_csmap, 1); | |
29dd101b BJ |
633 | } |
634 | goto out; | |
635 | } | |
636 | p1 = u.u_procp; | |
637 | if(newproc(isvfork)) { | |
638 | u.u_r.r_val1 = p1->p_pid; | |
639 | u.u_r.r_val2 = 1; /* child */ | |
640 | u.u_start = time; | |
641 | u.u_acflag = AFORK; | |
642 | return; | |
643 | } | |
644 | u.u_r.r_val1 = p2->p_pid; | |
645 | ||
646 | out: | |
647 | u.u_r.r_val2 = 0; | |
648 | } | |
649 | ||
650 | /* | |
651 | * break system call. | |
652 | * -- bad planning: "break" is a dirty word in C. | |
653 | */ | |
654 | sbreak() | |
655 | { | |
656 | struct a { | |
657 | char *nsiz; | |
658 | }; | |
659 | register int n, d; | |
660 | ||
661 | /* | |
662 | * set n to new data size | |
663 | * set d to new-old | |
664 | */ | |
665 | ||
666 | n = btoc(((struct a *)u.u_ap)->nsiz); | |
667 | if (!u.u_sep) | |
668 | n -= ctos(u.u_tsize) * stoc(1); | |
669 | if (n < 0) | |
670 | n = 0; | |
671 | d = clrnd(n - u.u_dsize); | |
672 | if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) | |
673 | return; | |
674 | if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0) | |
675 | return; | |
676 | expand(d, P0BR); | |
677 | } |