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