1 /* kern_proc.c 4.16 81/11/20 */ 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 0 98 if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A)) 99 #endif 100 if (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 (indir && (na == 1 || na == 2 && sharg)) 181 ap = (int)uap->fname; 182 else if (uap->argp) { 183 ap = fuword((caddr_t)uap->argp); 184 uap->argp++; 185 } 186 if (ap==NULL && uap->envp) { 187 uap->argp = NULL; 188 if ((ap = fuword((caddr_t)uap->envp)) == NULL) 189 break; 190 uap->envp++; 191 ne++; 192 } 193 if (ap==NULL) 194 break; 195 na++; 196 if (ap == -1) 197 u.u_error = EFAULT; 198 do { 199 if (nc >= NCARGS-1) 200 u.u_error = E2BIG; 201 if (indir && na == 2 && sharg != NULL) 202 c = *sharg++ & 0377; 203 else if ((c = fubyte((caddr_t)ap++)) < 0) 204 u.u_error = EFAULT; 205 if (u.u_error) { 206 if (bp) 207 brelse(bp); 208 bp = 0; 209 goto badarg; 210 } 211 if ((nc&BMASK) == 0) { 212 if (bp) 213 bdwrite(bp); 214 bp = getblk(argdev, 215 (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT))); 216 cp = bp->b_un.b_addr; 217 } 218 nc++; 219 *cp++ = c; 220 } while (c>0); 221 } 222 if (bp) 223 bdwrite(bp); 224 bp = 0; 225 nc = (nc + NBPW-1) & ~(NBPW-1); 226 if (indir) 227 bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ); 228 getxfile(ip, nc + (na+4)*NBPW, uid, gid); 229 if (u.u_error) { 230 badarg: 231 for (c = 0; c < nc; c += BSIZE) 232 if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) { 233 bp->b_flags |= B_AGE; /* throw away */ 234 bp->b_flags &= ~B_DELWRI; /* cancel io */ 235 brelse(bp); 236 bp = 0; 237 } 238 goto bad; 239 } 240 241 /* 242 * copy back arglist 243 */ 244 ucp = USRSTACK - nc - NBPW; 245 ap = ucp - na*NBPW - 3*NBPW; 246 u.u_ar0[SP] = ap; 247 (void) suword((caddr_t)ap, na-ne); 248 nc = 0; 249 for (;;) { 250 ap += NBPW; 251 if (na==ne) { 252 (void) suword((caddr_t)ap, 0); 253 ap += NBPW; 254 } 255 if (--na < 0) 256 break; 257 (void) suword((caddr_t)ap, ucp); 258 do { 259 if ((nc&BMASK) == 0) { 260 if (bp) 261 brelse(bp); 262 bp = bread(argdev, 263 (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT))); 264 bp->b_flags |= B_AGE; /* throw away */ 265 bp->b_flags &= ~B_DELWRI; /* cancel io */ 266 cp = bp->b_un.b_addr; 267 } 268 (void) subyte((caddr_t)ucp++, (c = *cp++)); 269 nc++; 270 } while(c&0377); 271 } 272 (void) suword((caddr_t)ap, 0); 273 (void) suword((caddr_t)ucp, 0); 274 setregs(); 275 bad: 276 if (bp) 277 brelse(bp); 278 if (bno) 279 rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); 280 iput(ip); 281 } 282 283 /* 284 * Read in and set up memory for executed file. 285 */ 286 getxfile(ip, nargc, uid, gid) 287 register struct inode *ip; 288 { 289 register size_t ts, ds, ss; 290 int pagi; 291 292 if (u.u_exdata.ux_mag == 0413) 293 pagi = SPAGI; 294 else 295 pagi = 0; 296 if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && 297 ip->i_count!=1) { 298 register struct file *fp; 299 300 for (fp = file; fp < fileNFILE; fp++) { 301 if (fp->f_flag & FSOCKET) 302 continue; 303 if (fp->f_inode == ip && (fp->f_flag&FWRITE)) { 304 u.u_error = ETXTBSY; 305 goto bad; 306 } 307 } 308 } 309 310 /* 311 * Compute text and data sizes and make sure not too large. 312 */ 313 ts = clrnd(btoc(u.u_exdata.ux_tsize)); 314 ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); 315 ss = clrnd(SSIZE + btoc(nargc)); 316 if (chksize(ts, ds, ss)) 317 goto bad; 318 319 /* 320 * Make sure enough space to start process. 321 */ 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|SSEQL|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 u.u_uid = uid; 375 u.u_procp->p_uid = uid; 376 u.u_gid = gid; 377 } else 378 psignal(u.u_procp, SIGTRAP); 379 u.u_tsize = ts; 380 u.u_dsize = ds; 381 u.u_ssize = ss; 382 bad: 383 return; 384 } 385 386 /* 387 * Clear registers on exec 388 */ 389 setregs() 390 { 391 register int (**rp)(); 392 register i; 393 long sigmask; 394 395 for (rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG]; 396 sigmask <<= 1, rp++) { 397 switch (*rp) { 398 399 case SIG_IGN: 400 case SIG_DFL: 401 case SIG_HOLD: 402 continue; 403 404 default: 405 /* 406 * Normal or deferring catch; revert to default. 407 */ 408 (void) spl6(); 409 *rp = SIG_DFL; 410 if ((int)*rp & 1) 411 u.u_procp->p_siga0 |= sigmask; 412 else 413 u.u_procp->p_siga1 &= ~sigmask; 414 if ((int)*rp & 2) 415 u.u_procp->p_siga1 |= sigmask; 416 else 417 u.u_procp->p_siga1 &= ~sigmask; 418 (void) spl0(); 419 continue; 420 } 421 } 422 /* 423 for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) 424 *rp++ = 0; 425 */ 426 u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ 427 for (i=0; i<NOFILE; i++) { 428 if (u.u_pofile[i]&EXCLOSE) { 429 closef(u.u_ofile[i]); 430 u.u_ofile[i] = NULL; 431 u.u_pofile[i] &= ~EXCLOSE; 432 } 433 } 434 435 /* 436 * Remember file name for accounting. 437 */ 438 u.u_acflag &= ~AFORK; 439 bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ); 440 } 441 442 /* 443 * Exit system call: pass back caller's arg 444 */ 445 rexit() 446 { 447 register struct a { 448 int rval; 449 } *uap; 450 451 uap = (struct a *)u.u_ap; 452 exit((uap->rval & 0377) << 8); 453 } 454 455 /* 456 * Release resources. 457 * Save u. area for parent to look at. 458 * Enter zombie state. 459 * Wake up parent and init processes, 460 * and dispose of children. 461 */ 462 exit(rv) 463 { 464 register int i; 465 register struct proc *p, *q; 466 register struct file *f; 467 register int x; 468 469 #ifdef PGINPROF 470 vmsizmon(); 471 #endif 472 p = u.u_procp; 473 p->p_flag &= ~(STRC|SULOCK); 474 p->p_flag |= SWEXIT; 475 p->p_clktim = 0; 476 (void) spl6(); 477 if ((int)SIG_IGN & 1) 478 p->p_siga0 = ~0; 479 else 480 p->p_siga0 = 0; 481 if ((int)SIG_IGN & 2) 482 p->p_siga1 = ~0; 483 else 484 p->p_siga1 = 0; 485 (void) spl0(); 486 p->p_cpticks = 0; 487 p->p_pctcpu = 0; 488 for (i=0; i<NSIG; i++) 489 u.u_signal[i] = SIG_IGN; 490 /* 491 * Release virtual memory. If we resulted from 492 * a vfork(), instead give the resources back to 493 * the parent. 494 */ 495 if ((p->p_flag & SVFORK) == 0) 496 vrelvm(); 497 else { 498 p->p_flag &= ~SVFORK; 499 wakeup((caddr_t)p); 500 while ((p->p_flag & SVFDONE) == 0) 501 sleep((caddr_t)p, PZERO - 1); 502 p->p_flag &= ~SVFDONE; 503 } 504 for (i=0; i<NOFILE; i++) { 505 f = u.u_ofile[i]; 506 u.u_ofile[i] = NULL; 507 closef(f); 508 } 509 ilock(u.u_cdir); 510 iput(u.u_cdir); 511 if (u.u_rdir) { 512 ilock(u.u_rdir); 513 iput(u.u_rdir); 514 } 515 u.u_limit[LIM_FSIZE] = INFINITY; 516 acct(); 517 vrelpt(u.u_procp); 518 vrelu(u.u_procp, 0); 519 multprog--; 520 p->p_stat = SZOMB; 521 noproc = 1; 522 i = PIDHASH(p->p_pid); 523 x = p - proc; 524 if (pidhash[i] == x) 525 pidhash[i] = p->p_idhash; 526 else { 527 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 528 if (proc[i].p_idhash == x) { 529 proc[i].p_idhash = p->p_idhash; 530 goto done; 531 } 532 panic("exit"); 533 } 534 if (p->p_pid == 1) 535 panic("init died"); 536 done: 537 ((struct xproc *)p)->xp_xstat = rv; /* overlay */ 538 ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ 539 vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); 540 for (q = proc; q < procNPROC; q++) 541 if (q->p_pptr == p) { 542 q->p_pptr = &proc[1]; 543 q->p_ppid = 1; 544 wakeup((caddr_t)&proc[1]); 545 /* 546 * Traced processes are killed 547 * since their existence means someone is screwing up. 548 * Stopped processes are sent a hangup and a continue. 549 * This is designed to be ``safe'' for setuid 550 * processes since they must be willing to tolerate 551 * hangups anyways. 552 */ 553 if (q->p_flag&STRC) { 554 q->p_flag &= ~STRC; 555 psignal(q, SIGKILL); 556 } else if (q->p_stat == SSTOP) { 557 psignal(q, SIGHUP); 558 psignal(q, SIGCONT); 559 } 560 /* 561 * Protect this process from future 562 * tty signals, clear TSTP/TTIN/TTOU if pending, 563 * and set SDETACH bit on procs. 564 */ 565 (void) spgrp(q, -1); 566 } 567 wakeup((caddr_t)p->p_pptr); 568 psignal(p->p_pptr, SIGCHLD); 569 swtch(); 570 } 571 572 wait() 573 { 574 struct vtimes vm; 575 struct vtimes *vp; 576 577 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 578 wait1(0, (struct vtimes *)0); 579 return; 580 } 581 vp = (struct vtimes *)u.u_ar0[R1]; 582 wait1(u.u_ar0[R0], &vm); 583 if (u.u_error) 584 return; 585 (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); 586 } 587 588 /* 589 * Wait system call. 590 * Search for a terminated (zombie) child, 591 * finally lay it to rest, and collect its status. 592 * Look also for stopped (traced) children, 593 * and pass back status from them. 594 */ 595 wait1(options, vp) 596 register options; 597 struct vtimes *vp; 598 { 599 register f; 600 register struct proc *p; 601 602 f = 0; 603 loop: 604 for (p = proc; p < procNPROC; p++) 605 if (p->p_pptr == u.u_procp) { 606 f++; 607 if (p->p_stat == SZOMB) { 608 u.u_r.r_val1 = p->p_pid; 609 u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; 610 ((struct xproc *)p)->xp_xstat = 0; 611 if (vp) 612 *vp = ((struct xproc *)p)->xp_vm; 613 vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); 614 ((struct xproc *)p)->xp_vm = zvms; 615 p->p_stat = NULL; 616 p->p_pid = 0; 617 p->p_ppid = 0; 618 p->p_pptr = 0; 619 p->p_sig = 0; 620 p->p_siga0 = 0; 621 p->p_siga1 = 0; 622 p->p_pgrp = 0; 623 p->p_flag = 0; 624 p->p_wchan = 0; 625 p->p_cursig = 0; 626 return; 627 } 628 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 629 (p->p_flag&STRC || options&WUNTRACED)) { 630 p->p_flag |= SWTED; 631 u.u_r.r_val1 = p->p_pid; 632 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 633 return; 634 } 635 } 636 if (f==0) { 637 u.u_error = ECHILD; 638 return; 639 } 640 if (options&WNOHANG) { 641 u.u_r.r_val1 = 0; 642 return; 643 } 644 if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { 645 u.u_eosys = RESTARTSYS; 646 return; 647 } 648 sleep((caddr_t)u.u_procp, PWAIT); 649 goto loop; 650 } 651 652 /* 653 * fork system call. 654 */ 655 fork() 656 { 657 658 u.u_cdmap = zdmap; 659 u.u_csmap = zdmap; 660 if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { 661 u.u_r.r_val2 = 0; 662 return; 663 } 664 fork1(0); 665 } 666 667 fork1(isvfork) 668 { 669 register struct proc *p1, *p2; 670 register a; 671 672 a = 0; 673 p2 = NULL; 674 for (p1 = proc; p1 < procNPROC; p1++) { 675 if (p1->p_stat==NULL && p2==NULL) 676 p2 = p1; 677 else { 678 if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) 679 a++; 680 } 681 } 682 /* 683 * Disallow if 684 * No processes at all; 685 * not su and too many procs owned; or 686 * not su and would take last slot. 687 */ 688 if (p2==NULL) 689 tablefull("proc"); 690 if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { 691 u.u_error = EAGAIN; 692 if (!isvfork) { 693 (void) vsexpand(0, &u.u_cdmap, 1); 694 (void) vsexpand(0, &u.u_csmap, 1); 695 } 696 goto out; 697 } 698 p1 = u.u_procp; 699 if (newproc(isvfork)) { 700 u.u_r.r_val1 = p1->p_pid; 701 u.u_r.r_val2 = 1; /* child */ 702 u.u_start = time; 703 u.u_acflag = AFORK; 704 return; 705 } 706 u.u_r.r_val1 = p2->p_pid; 707 708 out: 709 u.u_r.r_val2 = 0; 710 } 711 712 /* 713 * break system call. 714 * -- bad planning: "break" is a dirty word in C. 715 */ 716 sbreak() 717 { 718 struct a { 719 char *nsiz; 720 }; 721 register int n, d; 722 723 /* 724 * set n to new data size 725 * set d to new-old 726 */ 727 728 n = btoc(((struct a *)u.u_ap)->nsiz); 729 if (!u.u_sep) 730 n -= ctos(u.u_tsize) * stoc(1); 731 if (n < 0) 732 n = 0; 733 d = clrnd(n - u.u_dsize); 734 if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) { 735 u.u_error = ENOMEM; 736 return; 737 } 738 if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) 739 return; 740 if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0) 741 return; 742 expand(d, P0BR); 743 } 744