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