1 /* kern_proc.c 4.24 82/03/27 */ 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, 1)) == 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, 1); 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 u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8)); 376 } else 377 psignal(u.u_procp, SIGTRAP); 378 u.u_tsize = ts; 379 u.u_dsize = ds; 380 u.u_ssize = ss; 381 bad: 382 return; 383 } 384 385 /* 386 * Clear registers on exec 387 */ 388 setregs() 389 { 390 register int (**rp)(); 391 register i; 392 long sigmask; 393 394 for (rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG]; 395 sigmask <<= 1, rp++) { 396 switch (*rp) { 397 398 case SIG_IGN: 399 case SIG_DFL: 400 case SIG_HOLD: 401 continue; 402 403 default: 404 /* 405 * Normal or deferring catch; revert to default. 406 */ 407 (void) spl6(); 408 *rp = SIG_DFL; 409 if ((int)*rp & 1) 410 u.u_procp->p_siga0 |= sigmask; 411 else 412 u.u_procp->p_siga0 &= ~sigmask; 413 if ((int)*rp & 2) 414 u.u_procp->p_siga1 |= sigmask; 415 else 416 u.u_procp->p_siga1 &= ~sigmask; 417 (void) spl0(); 418 continue; 419 } 420 } 421 /* 422 for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) 423 *rp++ = 0; 424 */ 425 u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ 426 for (i=0; i<NOFILE; i++) { 427 if (u.u_pofile[i]&EXCLOSE) { 428 closef(u.u_ofile[i], 1); 429 u.u_ofile[i] = NULL; 430 u.u_pofile[i] &= ~EXCLOSE; 431 } 432 } 433 434 /* 435 * Remember file name for accounting. 436 */ 437 u.u_acflag &= ~AFORK; 438 bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ); 439 } 440 441 /* 442 * Exit system call: pass back caller's arg 443 */ 444 rexit() 445 { 446 register struct a { 447 int rval; 448 } *uap; 449 450 uap = (struct a *)u.u_ap; 451 exit((uap->rval & 0377) << 8); 452 } 453 454 /* 455 * Release resources. 456 * Save u. area for parent to look at. 457 * Enter zombie state. 458 * Wake up parent and init processes, 459 * and dispose of children. 460 */ 461 exit(rv) 462 { 463 register int i; 464 register struct proc *p, *q; 465 register struct file *f; 466 register int x; 467 468 #ifdef PGINPROF 469 vmsizmon(); 470 #endif 471 p = u.u_procp; 472 p->p_flag &= ~(STRC|SULOCK); 473 p->p_flag |= SWEXIT; 474 p->p_clktim = 0; 475 (void) spl6(); 476 if ((int)SIG_IGN & 1) 477 p->p_siga0 = ~0; 478 else 479 p->p_siga0 = 0; 480 if ((int)SIG_IGN & 2) 481 p->p_siga1 = ~0; 482 else 483 p->p_siga1 = 0; 484 (void) spl0(); 485 p->p_cpticks = 0; 486 p->p_pctcpu = 0; 487 for (i=0; i<NSIG; i++) 488 u.u_signal[i] = SIG_IGN; 489 /* 490 * Release virtual memory. If we resulted from 491 * a vfork(), instead give the resources back to 492 * the parent. 493 */ 494 if ((p->p_flag & SVFORK) == 0) 495 vrelvm(); 496 else { 497 p->p_flag &= ~SVFORK; 498 wakeup((caddr_t)p); 499 while ((p->p_flag & SVFDONE) == 0) 500 sleep((caddr_t)p, PZERO - 1); 501 p->p_flag &= ~SVFDONE; 502 } 503 for (i=0; i<NOFILE; i++) { 504 f = u.u_ofile[i]; 505 u.u_ofile[i] = NULL; 506 closef(f, 1); 507 } 508 ilock(u.u_cdir); 509 iput(u.u_cdir); 510 if (u.u_rdir) { 511 ilock(u.u_rdir); 512 iput(u.u_rdir); 513 } 514 u.u_limit[LIM_FSIZE] = INFINITY; 515 acct(); 516 vrelpt(u.u_procp); 517 vrelu(u.u_procp, 0); 518 (void) spl5(); /* hack for mem alloc race XXX */ 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 */ 564 (void) spgrp(q, -1); 565 } 566 wakeup((caddr_t)p->p_pptr); 567 psignal(p->p_pptr, SIGCHLD); 568 swtch(); 569 } 570 571 wait() 572 { 573 struct vtimes vm; 574 struct vtimes *vp; 575 576 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 577 wait1(0, (struct vtimes *)0); 578 return; 579 } 580 vp = (struct vtimes *)u.u_ar0[R1]; 581 wait1(u.u_ar0[R0], &vm); 582 if (u.u_error) 583 return; 584 (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); 585 } 586 587 /* 588 * Wait system call. 589 * Search for a terminated (zombie) child, 590 * finally lay it to rest, and collect its status. 591 * Look also for stopped (traced) children, 592 * and pass back status from them. 593 */ 594 wait1(options, vp) 595 register options; 596 struct vtimes *vp; 597 { 598 register f; 599 register struct proc *p; 600 601 f = 0; 602 loop: 603 for (p = proc; p < procNPROC; p++) 604 if (p->p_pptr == u.u_procp) { 605 f++; 606 if (p->p_stat == SZOMB) { 607 u.u_r.r_val1 = p->p_pid; 608 u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; 609 ((struct xproc *)p)->xp_xstat = 0; 610 if (vp) 611 *vp = ((struct xproc *)p)->xp_vm; 612 vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); 613 ((struct xproc *)p)->xp_vm = zvms; 614 p->p_stat = NULL; 615 p->p_pid = 0; 616 p->p_ppid = 0; 617 p->p_pptr = 0; 618 p->p_sig = 0; 619 p->p_siga0 = 0; 620 p->p_siga1 = 0; 621 p->p_pgrp = 0; 622 p->p_flag = 0; 623 p->p_wchan = 0; 624 p->p_cursig = 0; 625 return; 626 } 627 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 628 (p->p_flag&STRC || options&WUNTRACED)) { 629 p->p_flag |= SWTED; 630 u.u_r.r_val1 = p->p_pid; 631 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 632 return; 633 } 634 } 635 if (f==0) { 636 u.u_error = ECHILD; 637 return; 638 } 639 if (options&WNOHANG) { 640 u.u_r.r_val1 = 0; 641 return; 642 } 643 if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { 644 u.u_eosys = RESTARTSYS; 645 return; 646 } 647 sleep((caddr_t)u.u_procp, PWAIT); 648 goto loop; 649 } 650 651 /* 652 * fork system call. 653 */ 654 fork() 655 { 656 657 u.u_cdmap = zdmap; 658 u.u_csmap = zdmap; 659 if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { 660 u.u_r.r_val2 = 0; 661 return; 662 } 663 fork1(0); 664 } 665 666 fork1(isvfork) 667 { 668 register struct proc *p1, *p2; 669 register a; 670 671 a = 0; 672 p2 = NULL; 673 for (p1 = proc; p1 < procNPROC; p1++) { 674 if (p1->p_stat==NULL && p2==NULL) 675 p2 = p1; 676 else { 677 if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) 678 a++; 679 } 680 } 681 /* 682 * Disallow if 683 * No processes at all; 684 * not su and too many procs owned; or 685 * not su and would take last slot. 686 */ 687 if (p2==NULL) 688 tablefull("proc"); 689 if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { 690 u.u_error = EAGAIN; 691 if (!isvfork) { 692 (void) vsexpand(0, &u.u_cdmap, 1); 693 (void) vsexpand(0, &u.u_csmap, 1); 694 } 695 goto out; 696 } 697 p1 = u.u_procp; 698 if (newproc(isvfork)) { 699 u.u_r.r_val1 = p1->p_pid; 700 u.u_r.r_val2 = 1; /* child */ 701 u.u_start = time; 702 u.u_acflag = AFORK; 703 return; 704 } 705 u.u_r.r_val1 = p2->p_pid; 706 707 out: 708 u.u_r.r_val2 = 0; 709 } 710 711 /* 712 * break system call. 713 * -- bad planning: "break" is a dirty word in C. 714 */ 715 sbreak() 716 { 717 struct a { 718 char *nsiz; 719 }; 720 register int n, d; 721 722 /* 723 * set n to new data size 724 * set d to new-old 725 */ 726 727 n = btoc(((struct a *)u.u_ap)->nsiz); 728 if (!u.u_sep) 729 n -= ctos(u.u_tsize) * stoc(1); 730 if (n < 0) 731 n = 0; 732 d = clrnd(n - u.u_dsize); 733 if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) { 734 u.u_error = ENOMEM; 735 return; 736 } 737 if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) 738 return; 739 if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0) 740 return; 741 expand(d, P0BR); 742 } 743