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