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