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