1*9558Ssam /* kern_proc.c 4.49 82/12/05 */ 236Sbill 336Sbill #include "../h/param.h" 436Sbill #include "../h/systm.h" 536Sbill #include "../h/map.h" 636Sbill #include "../h/dir.h" 736Sbill #include "../h/user.h" 88030Sroot #include "../h/kernel.h" 936Sbill #include "../h/proc.h" 1036Sbill #include "../h/buf.h" 1136Sbill #include "../h/reg.h" 1236Sbill #include "../h/inode.h" 1336Sbill #include "../h/seg.h" 1436Sbill #include "../h/acct.h" 158968Sroot #include <wait.h> 1636Sbill #include "../h/pte.h" 1736Sbill #include "../h/vm.h" 1836Sbill #include "../h/text.h" 19188Sbill #include "../h/psl.h" 20890Sbill #include "../h/file.h" 217488Skre #include "../h/quota.h" 227497Sroot #include "../h/descrip.h" 237713Sroot #include "../h/uio.h" 248030Sroot #include "../h/mbuf.h" 259159Ssam #include "../h/nami.h" 2636Sbill 278030Sroot gethostid() 288030Sroot { 298030Sroot 308099Sroot u.u_r.r_val1 = hostid; 318030Sroot } 328030Sroot 338030Sroot sethostid() 348030Sroot { 358099Sroot struct a { 368099Sroot int hostid; 378099Sroot } *uap = (struct a *)u.u_ap; 388030Sroot 398099Sroot if (suser()) 408099Sroot hostid = uap->hostid; 418030Sroot } 428030Sroot 438099Sroot gethostname() 448099Sroot { 458099Sroot register struct a { 468099Sroot char *hostname; 478099Sroot int len; 488099Sroot } *uap = (struct a *)u.u_ap; 498099Sroot register u_int len; 508099Sroot 518099Sroot len = uap->len; 528099Sroot if (len > hostnamelen) 538099Sroot len = hostnamelen; 548099Sroot if (copyout((caddr_t)hostname, (caddr_t)uap->hostname, len)) 558099Sroot u.u_error = EFAULT; 568099Sroot } 578099Sroot 588099Sroot sethostname() 598099Sroot { 608099Sroot register struct a { 618099Sroot char *hostname; 628099Sroot u_int len; 638099Sroot } *uap = (struct a *)u.u_ap; 648099Sroot 658099Sroot if (!suser()) 668099Sroot return; 678099Sroot if (uap->len > sizeof (hostname) - 1) { 688099Sroot u.u_error = EINVAL; 698099Sroot return; 708099Sroot } 718099Sroot hostnamelen = uap->len; 728099Sroot if (copyin((caddr_t)uap->hostname, hostname, uap->len)) 738099Sroot u.u_error = EFAULT; 748099Sroot hostname[hostnamelen] = 0; 758099Sroot } 768099Sroot 7736Sbill /* 7836Sbill * exec system call, with and without environments. 7936Sbill */ 8036Sbill struct execa { 8136Sbill char *fname; 8236Sbill char **argp; 8336Sbill char **envp; 8436Sbill }; 8536Sbill 868030Sroot execv() 8736Sbill { 8836Sbill ((struct execa *)u.u_ap)->envp = NULL; 898030Sroot execve(); 9036Sbill } 9136Sbill 928030Sroot execve() 9336Sbill { 9436Sbill register nc; 9536Sbill register char *cp; 9636Sbill register struct buf *bp; 9736Sbill register struct execa *uap; 9836Sbill int na, ne, ucp, ap, c; 992301Skre int indir, uid, gid; 1002301Skre char *sharg; 10136Sbill struct inode *ip; 10236Sbill swblk_t bno; 1036568Smckusic char cfname[MAXNAMLEN + 1]; 1042301Skre char cfarg[SHSIZE]; 1057748Sroot int resid; 10636Sbill 1079159Ssam if ((ip = namei(uchar, LOOKUP, 1)) == NULL) 10836Sbill return; 10936Sbill bno = 0; 11036Sbill bp = 0; 1112301Skre indir = 0; 1122301Skre uid = u.u_uid; 1132301Skre gid = u.u_gid; 1142301Skre if (ip->i_mode & ISUID) 1152301Skre uid = ip->i_uid; 1162301Skre if (ip->i_mode & ISGID) 1172301Skre gid = ip->i_gid; 1182301Skre 1192301Skre again: 1204827Swnj if (access(ip, IEXEC)) 12136Sbill goto bad; 1224827Swnj if ((u.u_procp->p_flag&STRC) && access(ip, IREAD)) 1232330Swnj goto bad; 1244827Swnj if ((ip->i_mode & IFMT) != IFREG || 12536Sbill (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { 12636Sbill u.u_error = EACCES; 12736Sbill goto bad; 12836Sbill } 1292301Skre 13036Sbill /* 1312301Skre * Read in first few bytes of file for segment sizes, ux_mag: 1322301Skre * 407 = plain executable 1332301Skre * 410 = RO text 1342301Skre * 413 = demand paged RO text 1352301Skre * Also an ASCII line beginning with #! is 1362301Skre * the file name of a ``shell'' and arguments may be prepended 1372301Skre * to the argument list if given here. 1382301Skre * 1392301Skre * SHELL NAMES ARE LIMITED IN LENGTH. 1402301Skre * 1412301Skre * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM 1422301Skre * THE ASCII LINE. 1432301Skre */ 1447816Sroot u.u_error = rdwri(UIO_READ, ip, (caddr_t)&u.u_exdata, sizeof (u.u_exdata), 1457748Sroot 0, 1, &resid); 1464827Swnj if (u.u_error) 1472301Skre goto bad; 1487748Sroot u.u_count = resid; 1497816Sroot #ifndef lint 1504982Swnj if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) && 1514982Swnj u.u_exdata.ux_shell[0] != '#') { 1522301Skre u.u_error = ENOEXEC; 1532301Skre goto bad; 1542301Skre } 1557816Sroot #endif 1562301Skre switch (u.u_exdata.ux_mag) { 1572301Skre 1582301Skre case 0407: 1592301Skre u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; 1602301Skre u.u_exdata.ux_tsize = 0; 1612301Skre break; 1622301Skre 1632301Skre case 0413: 1642301Skre case 0410: 1652301Skre if (u.u_exdata.ux_tsize == 0) { 1662301Skre u.u_error = ENOEXEC; 1672301Skre goto bad; 1682301Skre } 1692301Skre break; 1702301Skre 1712301Skre default: 1722301Skre if (u.u_exdata.ux_shell[0] != '#' || 1732301Skre u.u_exdata.ux_shell[1] != '!' || 1742301Skre indir) { 1752301Skre u.u_error = ENOEXEC; 1762301Skre goto bad; 1772301Skre } 1782301Skre cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */ 1792301Skre while (cp < &u.u_exdata.ux_shell[SHSIZE]) { 1802301Skre if (*cp == '\t') 1812301Skre *cp = ' '; 1822301Skre else if (*cp == '\n') { 1832301Skre *cp = '\0'; 1842301Skre break; 1852301Skre } 1862301Skre cp++; 1872301Skre } 1882301Skre if (*cp != '\0') { 1892301Skre u.u_error = ENOEXEC; 1902301Skre goto bad; 1912301Skre } 1922301Skre cp = &u.u_exdata.ux_shell[2]; 1932301Skre while (*cp == ' ') 1942301Skre cp++; 1952301Skre u.u_dirp = cp; 1962301Skre while (*cp && *cp != ' ') 1972301Skre cp++; 1982301Skre sharg = NULL; 1992301Skre if (*cp) { 2002301Skre *cp++ = '\0'; 2012301Skre while (*cp == ' ') 2022301Skre cp++; 2032301Skre if (*cp) { 2042301Skre bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE); 2052301Skre sharg = cfarg; 2062301Skre } 2072301Skre } 2086568Smckusic bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname, 2097816Sroot (unsigned)(u.u_dent.d_namlen + 1)); 2102301Skre indir = 1; 2112301Skre iput(ip); 2129159Ssam ip = namei(schar, LOOKUP, 1); 2132301Skre if (ip == NULL) 2142301Skre return; 2152301Skre goto again; 2162301Skre } 2172301Skre 2182301Skre /* 21936Sbill * Collect arguments on "file" in swap space. 22036Sbill */ 22136Sbill na = 0; 22236Sbill ne = 0; 22336Sbill nc = 0; 22436Sbill uap = (struct execa *)u.u_ap; 2258665S if ((bno = rmalloc(argmap, (long)ctod(clrnd((int)btoc(NCARGS))))) == 0) { 22636Sbill swkill(u.u_procp, "exece"); 22736Sbill goto bad; 22836Sbill } 22936Sbill if (bno % CLSIZE) 2302780Swnj panic("execa rmalloc"); 23136Sbill if (uap->argp) for (;;) { 23236Sbill ap = NULL; 2333621Sroot if (indir && (na == 1 || na == 2 && sharg)) 2342301Skre ap = (int)uap->fname; 2352301Skre else if (uap->argp) { 23636Sbill ap = fuword((caddr_t)uap->argp); 23736Sbill uap->argp++; 23836Sbill } 23936Sbill if (ap==NULL && uap->envp) { 24036Sbill uap->argp = NULL; 24136Sbill if ((ap = fuword((caddr_t)uap->envp)) == NULL) 24236Sbill break; 24336Sbill uap->envp++; 24436Sbill ne++; 24536Sbill } 2466568Smckusic if (ap == NULL) 24736Sbill break; 24836Sbill na++; 2494827Swnj if (ap == -1) 25036Sbill u.u_error = EFAULT; 25136Sbill do { 25236Sbill if (nc >= NCARGS-1) 25336Sbill u.u_error = E2BIG; 2542301Skre if (indir && na == 2 && sharg != NULL) 2552301Skre c = *sharg++ & 0377; 2562301Skre else if ((c = fubyte((caddr_t)ap++)) < 0) 25736Sbill u.u_error = EFAULT; 25883Sbill if (u.u_error) { 25983Sbill if (bp) 26083Sbill brelse(bp); 26183Sbill bp = 0; 26236Sbill goto badarg; 26383Sbill } 2646568Smckusic if (nc % (CLSIZE*NBPG) == 0) { 26536Sbill if (bp) 26636Sbill bdwrite(bp); 2678968Sroot bp = getblk(argdev, bno + ctod(nc / NBPG), 2686568Smckusic CLSIZE*NBPG); 26936Sbill cp = bp->b_un.b_addr; 27036Sbill } 27136Sbill nc++; 27236Sbill *cp++ = c; 2736568Smckusic } while (c > 0); 27436Sbill } 27536Sbill if (bp) 27636Sbill bdwrite(bp); 27736Sbill bp = 0; 27836Sbill nc = (nc + NBPW-1) & ~(NBPW-1); 2796568Smckusic if (indir) { 2806568Smckusic u.u_dent.d_namlen = strlen(cfname); 2816568Smckusic bcopy((caddr_t)cfname, (caddr_t)u.u_dent.d_name, 2827816Sroot (unsigned)(u.u_dent.d_namlen + 1)); 2836568Smckusic } 2842301Skre getxfile(ip, nc + (na+4)*NBPW, uid, gid); 285912Sbill if (u.u_error) { 28636Sbill badarg: 2879006Sroot for (c = 0; c < nc; c += CLSIZE*NBPG) { 2889006Sroot bp = baddr(argdev, bno + ctod(c / NBPG), CLSIZE*NBPG); 2898968Sroot if (bp) { 29036Sbill bp->b_flags |= B_AGE; /* throw away */ 29136Sbill bp->b_flags &= ~B_DELWRI; /* cancel io */ 29236Sbill brelse(bp); 29336Sbill bp = 0; 29436Sbill } 2959006Sroot } 29636Sbill goto bad; 29736Sbill } 29836Sbill 29936Sbill /* 30036Sbill * copy back arglist 30136Sbill */ 30236Sbill ucp = USRSTACK - nc - NBPW; 30336Sbill ap = ucp - na*NBPW - 3*NBPW; 30436Sbill u.u_ar0[SP] = ap; 305132Sbill (void) suword((caddr_t)ap, na-ne); 30636Sbill nc = 0; 30736Sbill for (;;) { 30836Sbill ap += NBPW; 30936Sbill if (na==ne) { 310132Sbill (void) suword((caddr_t)ap, 0); 31136Sbill ap += NBPW; 31236Sbill } 31336Sbill if (--na < 0) 31436Sbill break; 315132Sbill (void) suword((caddr_t)ap, ucp); 31636Sbill do { 3176568Smckusic if (nc % (CLSIZE*NBPG) == 0) { 31836Sbill if (bp) 31936Sbill brelse(bp); 3206568Smckusic bp = bread(argdev, bno + nc / NBPG, 3216568Smckusic CLSIZE*NBPG); 32236Sbill bp->b_flags |= B_AGE; /* throw away */ 32336Sbill bp->b_flags &= ~B_DELWRI; /* cancel io */ 32436Sbill cp = bp->b_un.b_addr; 32536Sbill } 326132Sbill (void) subyte((caddr_t)ucp++, (c = *cp++)); 32736Sbill nc++; 32836Sbill } while(c&0377); 32936Sbill } 330132Sbill (void) suword((caddr_t)ap, 0); 331132Sbill (void) suword((caddr_t)ucp, 0); 33236Sbill setregs(); 33336Sbill bad: 33436Sbill if (bp) 33536Sbill brelse(bp); 33636Sbill if (bno) 3378816Sroot rmfree(argmap, (long)ctod(clrnd((int) btoc(NCARGS))), bno); 33836Sbill iput(ip); 33936Sbill } 34036Sbill 34136Sbill /* 34236Sbill * Read in and set up memory for executed file. 34336Sbill */ 3442301Skre getxfile(ip, nargc, uid, gid) 34536Sbill register struct inode *ip; 34636Sbill { 34736Sbill register size_t ts, ds, ss; 3482301Skre int pagi; 34936Sbill 3502301Skre if (u.u_exdata.ux_mag == 0413) 35136Sbill pagi = SPAGI; 3522301Skre else 3532301Skre pagi = 0; 3544827Swnj if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && 3554827Swnj ip->i_count!=1) { 356890Sbill register struct file *fp; 357890Sbill 3584827Swnj for (fp = file; fp < fileNFILE; fp++) { 3597497Sroot if (fp->f_type == DTYPE_FILE && 3607497Sroot fp->f_inode == ip && (fp->f_flag&FWRITE)) { 361890Sbill u.u_error = ETXTBSY; 362890Sbill goto bad; 363890Sbill } 3644827Swnj } 36536Sbill } 36636Sbill 36736Sbill /* 3684827Swnj * Compute text and data sizes and make sure not too large. 36936Sbill */ 37036Sbill ts = clrnd(btoc(u.u_exdata.ux_tsize)); 37136Sbill ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); 37236Sbill ss = clrnd(SSIZE + btoc(nargc)); 373912Sbill if (chksize(ts, ds, ss)) 374912Sbill goto bad; 3754827Swnj 3764827Swnj /* 3774827Swnj * Make sure enough space to start process. 3784827Swnj */ 379912Sbill u.u_cdmap = zdmap; 380912Sbill u.u_csmap = zdmap; 381912Sbill if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) 382912Sbill goto bad; 38336Sbill 384912Sbill /* 385912Sbill * At this point, committed to the new image! 386912Sbill * Release virtual memory resources of old process, and 387912Sbill * initialize the virtual memory of the new process. 388912Sbill * If we resulted from vfork(), instead wakeup our 389912Sbill * parent who will set SVFDONE when he has taken back 390912Sbill * our resources. 391912Sbill */ 392912Sbill if ((u.u_procp->p_flag & SVFORK) == 0) 393912Sbill vrelvm(); 394912Sbill else { 395912Sbill u.u_procp->p_flag &= ~SVFORK; 396912Sbill u.u_procp->p_flag |= SKEEP; 397912Sbill wakeup((caddr_t)u.u_procp); 398912Sbill while ((u.u_procp->p_flag & SVFDONE) == 0) 399912Sbill sleep((caddr_t)u.u_procp, PZERO - 1); 400912Sbill u.u_procp->p_flag &= ~(SVFDONE|SKEEP); 401912Sbill } 4023592Swnj u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG); 403912Sbill u.u_procp->p_flag |= pagi; 404912Sbill u.u_dmap = u.u_cdmap; 405912Sbill u.u_smap = u.u_csmap; 406912Sbill vgetvm(ts, ds, ss); 407912Sbill 4087748Sroot if (pagi == 0) 4097816Sroot u.u_error = 4107816Sroot rdwri(UIO_READ, ip, 4118968Sroot (char *)ctob(dptov(u.u_procp, 0)), 4128968Sroot (int)u.u_exdata.ux_dsize, 4137816Sroot (int)(sizeof(u.u_exdata)+u.u_exdata.ux_tsize), 4147816Sroot 0, (int *)0); 415912Sbill xalloc(ip, pagi); 416912Sbill if (pagi && u.u_procp->p_textp) 417912Sbill vinifod((struct fpte *)dptopte(u.u_procp, 0), 418912Sbill PG_FTEXT, u.u_procp->p_textp->x_iptr, 4198665S (long)(1 + ts/CLSIZE), (int)btoc(u.u_exdata.ux_dsize)); 42036Sbill 4218968Sroot #ifdef vax 422912Sbill /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ 4238443Sroot #include "../vax/mtpr.h" /* XXX */ 424912Sbill mtpr(TBIA, 0); 4258968Sroot #endif 42636Sbill 4277530Sroot if (u.u_error) 4287530Sroot swkill(u.u_procp, "i/o error mapping pages"); 429912Sbill /* 430912Sbill * set SUID/SGID protections, if no tracing 431912Sbill */ 432912Sbill if ((u.u_procp->p_flag&STRC)==0) { 4334827Swnj u.u_uid = uid; 4344827Swnj u.u_procp->p_uid = uid; 4352301Skre u.u_gid = gid; 4368665S (void) entergroup(gid); 437912Sbill } else 438912Sbill psignal(u.u_procp, SIGTRAP); 43936Sbill u.u_tsize = ts; 44036Sbill u.u_dsize = ds; 44136Sbill u.u_ssize = ss; 44236Sbill bad: 443912Sbill return; 44436Sbill } 44536Sbill 44636Sbill /* 44736Sbill * Clear registers on exec 44836Sbill */ 44936Sbill setregs() 45036Sbill { 451173Sbill register int (**rp)(); 45236Sbill register i; 453188Sbill long sigmask; 45436Sbill 4556464Swnj for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG]; 456188Sbill sigmask <<= 1, rp++) { 457188Sbill switch (*rp) { 458188Sbill 459188Sbill case SIG_IGN: 460188Sbill case SIG_DFL: 461188Sbill case SIG_HOLD: 462188Sbill continue; 463188Sbill 464188Sbill default: 465188Sbill /* 466206Sbill * Normal or deferring catch; revert to default. 467188Sbill */ 468206Sbill (void) spl6(); 469206Sbill *rp = SIG_DFL; 470188Sbill if ((int)*rp & 1) 471188Sbill u.u_procp->p_siga0 |= sigmask; 472188Sbill else 4736327Swnj u.u_procp->p_siga0 &= ~sigmask; 474188Sbill if ((int)*rp & 2) 475188Sbill u.u_procp->p_siga1 |= sigmask; 476188Sbill else 477188Sbill u.u_procp->p_siga1 &= ~sigmask; 478206Sbill (void) spl0(); 479188Sbill continue; 480188Sbill } 481188Sbill } 4828968Sroot #ifdef vax 48336Sbill /* 4844827Swnj for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) 48536Sbill *rp++ = 0; 48636Sbill */ 4878968Sroot u.u_ar0[PC] = u.u_exdata.ux_entloc+2; 4888968Sroot #endif 4898968Sroot #ifdef sun 4908968Sroot { register struct regs *r = (struct regs *)u.u_ar0; 4918968Sroot for (i = 0; i < 8; i++) { 4928968Sroot r->r_dreg[i] = 0; 4938968Sroot if (&r->r_areg[i] != &r->r_sp) 4948968Sroot r->r_areg[i] = 0; 4958968Sroot } 4968968Sroot r->r_sr = PSL_USERSET; 4978968Sroot r->r_pc = u.u_exdata.ux_entloc; 4988968Sroot } 4998968Sroot #endif 5004827Swnj for (i=0; i<NOFILE; i++) { 50136Sbill if (u.u_pofile[i]&EXCLOSE) { 5027697Ssam closef(u.u_ofile[i], 1, u.u_pofile[i]); 50336Sbill u.u_ofile[i] = NULL; 5047697Ssam u.u_pofile[i] = 0; 50536Sbill } 5068968Sroot #ifdef SUNMMAP 5078968Sroot u.u_pofile[i] &= ~UF_MAPPED; 5088968Sroot #endif 50936Sbill } 5104827Swnj 51136Sbill /* 51236Sbill * Remember file name for accounting. 51336Sbill */ 51436Sbill u.u_acflag &= ~AFORK; 5156568Smckusic bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm, 5167816Sroot (unsigned)(u.u_dent.d_namlen + 1)); 5178968Sroot #ifdef sun 5188968Sroot u.u_eosys = REALLYRETURN; 5198968Sroot #endif 52036Sbill } 52136Sbill 52236Sbill /* 5234827Swnj * Exit system call: pass back caller's arg 52436Sbill */ 52536Sbill rexit() 52636Sbill { 52736Sbill register struct a { 52836Sbill int rval; 52936Sbill } *uap; 53036Sbill 53136Sbill uap = (struct a *)u.u_ap; 53236Sbill exit((uap->rval & 0377) << 8); 53336Sbill } 53436Sbill 53536Sbill /* 53636Sbill * Release resources. 53736Sbill * Save u. area for parent to look at. 53836Sbill * Enter zombie state. 53936Sbill * Wake up parent and init processes, 54036Sbill * and dispose of children. 54136Sbill */ 54236Sbill exit(rv) 54336Sbill { 54436Sbill register int i; 54536Sbill register struct proc *p, *q; 54636Sbill register int x; 5478827Sroot struct mbuf *m = m_getclr(M_WAIT); 54836Sbill 54936Sbill #ifdef PGINPROF 55036Sbill vmsizmon(); 55136Sbill #endif 55236Sbill p = u.u_procp; 55336Sbill p->p_flag &= ~(STRC|SULOCK); 55436Sbill p->p_flag |= SWEXIT; 555188Sbill (void) spl6(); 556188Sbill if ((int)SIG_IGN & 1) 557188Sbill p->p_siga0 = ~0; 558188Sbill else 559188Sbill p->p_siga0 = 0; 560188Sbill if ((int)SIG_IGN & 2) 561188Sbill p->p_siga1 = ~0; 562188Sbill else 563206Sbill p->p_siga1 = 0; 564188Sbill (void) spl0(); 5651399Sbill p->p_cpticks = 0; 5661399Sbill p->p_pctcpu = 0; 5674827Swnj for (i=0; i<NSIG; i++) 568173Sbill u.u_signal[i] = SIG_IGN; 5698623Sroot untimeout(realitexpire, (caddr_t)p); 57036Sbill /* 57136Sbill * Release virtual memory. If we resulted from 57236Sbill * a vfork(), instead give the resources back to 57336Sbill * the parent. 57436Sbill */ 57536Sbill if ((p->p_flag & SVFORK) == 0) 57636Sbill vrelvm(); 57736Sbill else { 57836Sbill p->p_flag &= ~SVFORK; 57936Sbill wakeup((caddr_t)p); 58036Sbill while ((p->p_flag & SVFDONE) == 0) 58136Sbill sleep((caddr_t)p, PZERO - 1); 58236Sbill p->p_flag &= ~SVFDONE; 58336Sbill } 5847697Ssam for (i = 0; i < NOFILE; i++) { 585*9558Ssam struct file *f; 586*9558Ssam int p; 587*9558Ssam 58836Sbill f = u.u_ofile[i]; 58936Sbill u.u_ofile[i] = NULL; 590*9558Ssam p = u.u_pofile[i]; 5917697Ssam u.u_pofile[i] = 0; 592*9558Ssam closef(f, 1, p); 59336Sbill } 5944827Swnj ilock(u.u_cdir); 59536Sbill iput(u.u_cdir); 59636Sbill if (u.u_rdir) { 5974827Swnj ilock(u.u_rdir); 59836Sbill iput(u.u_rdir); 59936Sbill } 6008030Sroot u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 60136Sbill acct(); 6027488Skre #ifdef QUOTA 6037488Skre qclean(); 6047488Skre #endif 6058968Sroot #ifdef sun 6068968Sroot ctxfree(&u); 6078968Sroot #endif 60836Sbill vrelpt(u.u_procp); 60936Sbill vrelu(u.u_procp, 0); 6105630Swnj (void) spl5(); /* hack for mem alloc race XXX */ 61136Sbill multprog--; 612931Sbill p->p_stat = SZOMB; 613307Sbill noproc = 1; 61436Sbill i = PIDHASH(p->p_pid); 61536Sbill x = p - proc; 61636Sbill if (pidhash[i] == x) 61736Sbill pidhash[i] = p->p_idhash; 61836Sbill else { 61936Sbill for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 62036Sbill if (proc[i].p_idhash == x) { 62136Sbill proc[i].p_idhash = p->p_idhash; 62236Sbill goto done; 62336Sbill } 62436Sbill panic("exit"); 62536Sbill } 6261409Sbill if (p->p_pid == 1) 6271409Sbill panic("init died"); 62836Sbill done: 6298030Sroot p->p_xstat = rv; 630*9558Ssam if (m == 0) 631*9558Ssam panic("exit: m_getclr"); 6328827Sroot p->p_ru = mtod(m, struct rusage *); 6338030Sroot *p->p_ru = u.u_ru; 6348030Sroot ruadd(p->p_ru, &u.u_cru); 6354827Swnj for (q = proc; q < procNPROC; q++) 6364827Swnj if (q->p_pptr == p) { 6377488Skre if (q->p_osptr) 6387488Skre q->p_osptr->p_ysptr = q->p_ysptr; 6397488Skre if (q->p_ysptr) 6407488Skre q->p_ysptr->p_osptr = q->p_osptr; 6417488Skre if (proc[1].p_cptr) 6427488Skre proc[1].p_cptr->p_ysptr = q; 6437488Skre q->p_osptr = proc[1].p_cptr; 6447488Skre q->p_ysptr = NULL; 6457488Skre proc[1].p_cptr = q; 6467488Skre 647188Sbill q->p_pptr = &proc[1]; 648188Sbill q->p_ppid = 1; 64936Sbill wakeup((caddr_t)&proc[1]); 650188Sbill /* 651212Sbill * Traced processes are killed 652188Sbill * since their existence means someone is screwing up. 653354Sbill * Stopped processes are sent a hangup and a continue. 654212Sbill * This is designed to be ``safe'' for setuid 655212Sbill * processes since they must be willing to tolerate 656212Sbill * hangups anyways. 657188Sbill */ 658212Sbill if (q->p_flag&STRC) { 659188Sbill q->p_flag &= ~STRC; 660188Sbill psignal(q, SIGKILL); 661212Sbill } else if (q->p_stat == SSTOP) { 662212Sbill psignal(q, SIGHUP); 663212Sbill psignal(q, SIGCONT); 664188Sbill } 665215Sbill /* 666215Sbill * Protect this process from future 6675619Swnj * tty signals, clear TSTP/TTIN/TTOU if pending. 668215Sbill */ 6691788Sbill (void) spgrp(q, -1); 67036Sbill } 6716464Swnj psignal(p->p_pptr, SIGCHLD); 672188Sbill wakeup((caddr_t)p->p_pptr); 67336Sbill swtch(); 67436Sbill } 67536Sbill 6768151Sroot #include <vtimes.h> 6778151Sroot 6788099Sroot owait() 67936Sbill { 6808151Sroot struct rusage ru; 6818151Sroot struct vtimes *vtp, avt; 68236Sbill 683188Sbill if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 6848030Sroot wait1(0, (struct rusage *)0); 685188Sbill return; 686188Sbill } 6878151Sroot vtp = (struct vtimes *)u.u_ar0[R1]; 6888030Sroot wait1(u.u_ar0[R0], &ru); 689188Sbill if (u.u_error) 690188Sbill return; 6918151Sroot getvtimes(&ru, &avt); 6928151Sroot (void) copyout((caddr_t)&avt, (caddr_t)vtp, sizeof (struct vtimes)); 69336Sbill } 69436Sbill 69536Sbill /* 69636Sbill * Wait system call. 69736Sbill * Search for a terminated (zombie) child, 69836Sbill * finally lay it to rest, and collect its status. 69936Sbill * Look also for stopped (traced) children, 70036Sbill * and pass back status from them. 70136Sbill */ 7028030Sroot wait1(options, ru) 7038030Sroot register int options; 7048030Sroot struct rusage *ru; 70536Sbill { 70636Sbill register f; 7077488Skre register struct proc *p, *q; 70836Sbill 70936Sbill f = 0; 71036Sbill loop: 7114827Swnj for (p = proc; p < procNPROC; p++) 7124827Swnj if (p->p_pptr == u.u_procp) { 71336Sbill f++; 7144827Swnj if (p->p_stat == SZOMB) { 71536Sbill u.u_r.r_val1 = p->p_pid; 7168030Sroot u.u_r.r_val2 = p->p_xstat; 7178030Sroot p->p_xstat = 0; 7188030Sroot if (ru) 7198030Sroot *ru = *p->p_ru; 7208030Sroot ruadd(&u.u_cru, p->p_ru); 7218665S (void) m_free(dtom(p->p_ru)); 7228030Sroot p->p_ru = 0; 72336Sbill p->p_stat = NULL; 72436Sbill p->p_pid = 0; 72536Sbill p->p_ppid = 0; 7267488Skre if (q = p->p_ysptr) 7277488Skre q->p_osptr = p->p_osptr; 7287488Skre if (q = p->p_osptr) 7297488Skre q->p_ysptr = p->p_ysptr; 7307488Skre if ((q = p->p_pptr)->p_cptr == p) 7317488Skre q->p_cptr = p->p_osptr; 732188Sbill p->p_pptr = 0; 7337488Skre p->p_ysptr = 0; 7347488Skre p->p_osptr = 0; 7357488Skre p->p_cptr = 0; 73636Sbill p->p_sig = 0; 737188Sbill p->p_siga0 = 0; 738188Sbill p->p_siga1 = 0; 73936Sbill p->p_pgrp = 0; 74036Sbill p->p_flag = 0; 74136Sbill p->p_wchan = 0; 742188Sbill p->p_cursig = 0; 74336Sbill return; 74436Sbill } 745188Sbill if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 746188Sbill (p->p_flag&STRC || options&WUNTRACED)) { 747188Sbill p->p_flag |= SWTED; 748188Sbill u.u_r.r_val1 = p->p_pid; 749188Sbill u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 750188Sbill return; 75136Sbill } 75236Sbill } 753188Sbill if (f==0) { 754188Sbill u.u_error = ECHILD; 755188Sbill return; 75636Sbill } 757188Sbill if (options&WNOHANG) { 758188Sbill u.u_r.r_val1 = 0; 759188Sbill return; 760188Sbill } 7618116Sroot if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) { 762188Sbill u.u_eosys = RESTARTSYS; 763188Sbill return; 764188Sbill } 765188Sbill sleep((caddr_t)u.u_procp, PWAIT); 766188Sbill goto loop; 76736Sbill } 76836Sbill 76936Sbill /* 77036Sbill * fork system call. 77136Sbill */ 77236Sbill fork() 77336Sbill { 77436Sbill 77536Sbill u.u_cdmap = zdmap; 77636Sbill u.u_csmap = zdmap; 77736Sbill if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { 77836Sbill u.u_r.r_val2 = 0; 77936Sbill return; 78036Sbill } 78136Sbill fork1(0); 78236Sbill } 78336Sbill 78436Sbill fork1(isvfork) 78536Sbill { 78636Sbill register struct proc *p1, *p2; 7877488Skre #ifndef QUOTA 78836Sbill register a; 78936Sbill 79036Sbill a = 0; 7917488Skre #else 7927488Skre if (u.u_quota != NOQUOT && u.u_quota->q_plim && 7937488Skre u.u_quota->q_cnt >= u.u_quota->q_plim) { 7947488Skre u.u_error = EPROCLIM; 7957488Skre return; 7967488Skre } 7977488Skre #endif 79836Sbill p2 = NULL; 7994827Swnj for (p1 = proc; p1 < procNPROC; p1++) { 8007488Skre #ifdef QUOTA 8017488Skre if (p1->p_stat == NULL) { 8027488Skre p2 = p1; 8037488Skre break; 8047488Skre } 8057488Skre #else 80636Sbill if (p1->p_stat==NULL && p2==NULL) 80736Sbill p2 = p1; 80836Sbill else { 80936Sbill if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) 81036Sbill a++; 81136Sbill } 8127488Skre #endif 81336Sbill } 81436Sbill /* 81536Sbill * Disallow if 81636Sbill * No processes at all; 81736Sbill * not su and too many procs owned; or 81836Sbill * not su and would take last slot. 81936Sbill */ 8202938Swnj if (p2==NULL) 8212938Swnj tablefull("proc"); 8227488Skre #ifdef QUOTA 8237488Skre if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) { 8247488Skre #else 8252741Swnj if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { 8267488Skre #endif 82736Sbill u.u_error = EAGAIN; 82836Sbill if (!isvfork) { 829132Sbill (void) vsexpand(0, &u.u_cdmap, 1); 830132Sbill (void) vsexpand(0, &u.u_csmap, 1); 83136Sbill } 83236Sbill goto out; 83336Sbill } 83436Sbill p1 = u.u_procp; 8354827Swnj if (newproc(isvfork)) { 83636Sbill u.u_r.r_val1 = p1->p_pid; 83736Sbill u.u_r.r_val2 = 1; /* child */ 8388030Sroot u.u_start = time.tv_sec; 83936Sbill u.u_acflag = AFORK; 8407488Skre #ifdef QUOTA 8417488Skre u.u_qflags &= ~QUF_LOGIN; 8427488Skre #endif 84336Sbill return; 84436Sbill } 84536Sbill u.u_r.r_val1 = p2->p_pid; 84636Sbill 84736Sbill out: 84836Sbill u.u_r.r_val2 = 0; 84936Sbill } 85036Sbill 8517497Sroot spgrp(top, npgrp) 8527497Sroot register struct proc *top; 8537497Sroot { 8547497Sroot register struct proc *pp, *p; 8557497Sroot int f = 0; 8567497Sroot 8577497Sroot for (p = top; npgrp == -1 || u.u_uid == p->p_uid || 8587497Sroot !u.u_uid || inferior(p); p = pp) { 8597497Sroot if (npgrp == -1) { 8607497Sroot #define bit(a) (1<<(a-1)) 8617497Sroot p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); 8627497Sroot } else 8637497Sroot p->p_pgrp = npgrp; 8647497Sroot f++; 8657497Sroot /* 8667497Sroot * Search for children. 8677497Sroot */ 8687497Sroot for (pp = proc; pp < procNPROC; pp++) 8697497Sroot if (pp->p_pptr == p) 8707497Sroot goto cont; 8717497Sroot /* 8727497Sroot * Search for siblings. 8737497Sroot */ 8747497Sroot for (; p != top; p = p->p_pptr) 8757497Sroot for (pp = p + 1; pp < procNPROC; pp++) 8767497Sroot if (pp->p_pptr == p->p_pptr) 8777497Sroot goto cont; 8787497Sroot break; 8797497Sroot cont: 8807497Sroot ; 8817497Sroot } 8827497Sroot return (f); 8837497Sroot } 8847497Sroot 88536Sbill /* 8867497Sroot * Is p an inferior of the current process? 88736Sbill */ 8887497Sroot inferior(p) 8897816Sroot register struct proc *p; 89036Sbill { 89136Sbill 8927497Sroot for (; p != u.u_procp; p = p->p_pptr) 8937497Sroot if (p->p_ppid == 0) 8947497Sroot return (0); 8957497Sroot return (1); 89636Sbill } 8977816Sroot 8987816Sroot struct proc * 8997816Sroot pfind(pid) 9007816Sroot int pid; 9017816Sroot { 9027816Sroot register struct proc *p; 9037816Sroot 9047816Sroot for (p = &proc[pidhash[PIDHASH(pid)]]; p != &proc[0]; p = &proc[p->p_idhash]) 9057816Sroot if (p->p_pid == pid) 9067816Sroot return (p); 9077816Sroot return ((struct proc *)0); 9087816Sroot } 9098099Sroot 9108099Sroot /* 9118099Sroot * Create a new process-- the internal version of 9128099Sroot * sys fork. 9138099Sroot * It returns 1 in the new process, 0 in the old. 9148099Sroot */ 9158099Sroot newproc(isvfork) 9168099Sroot int isvfork; 9178099Sroot { 9188099Sroot register struct proc *p; 9198099Sroot register struct proc *rpp, *rip; 9208099Sroot register int n; 9218099Sroot register struct file *fp; 9228099Sroot 9238099Sroot p = NULL; 9248099Sroot /* 9258099Sroot * First, just locate a slot for a process 9268099Sroot * and copy the useful info from this process into it. 9278099Sroot * The panic "cannot happen" because fork has already 9288099Sroot * checked for the existence of a slot. 9298099Sroot */ 9308099Sroot retry: 9318099Sroot mpid++; 9328099Sroot if (mpid >= 30000) { 9338099Sroot mpid = 0; 9348099Sroot goto retry; 9358099Sroot } 9368099Sroot for (rpp = proc; rpp < procNPROC; rpp++) { 9378099Sroot if (rpp->p_stat == NULL && p==NULL) 9388099Sroot p = rpp; 9398099Sroot if (rpp->p_pid==mpid || rpp->p_pgrp==mpid) 9408099Sroot goto retry; 9418099Sroot } 9428099Sroot if ((rpp = p) == NULL) 9438099Sroot panic("no procs"); 9448099Sroot 9458099Sroot /* 9468099Sroot * Make a proc table entry for the new process. 9478099Sroot */ 9488099Sroot rip = u.u_procp; 9498099Sroot #ifdef QUOTA 9508099Sroot (rpp->p_quota = rip->p_quota)->q_cnt++; 9518099Sroot #endif 9528099Sroot rpp->p_stat = SIDL; 9538099Sroot timerclear(&rpp->p_realtimer.it_value); 9548099Sroot rpp->p_flag = SLOAD | (rip->p_flag & (SPAGI|SNUSIG)); 9558099Sroot if (isvfork) { 9568099Sroot rpp->p_flag |= SVFORK; 9578099Sroot rpp->p_ndx = rip->p_ndx; 9588099Sroot } else 9598099Sroot rpp->p_ndx = rpp - proc; 9608099Sroot rpp->p_uid = rip->p_uid; 9618099Sroot rpp->p_pgrp = rip->p_pgrp; 9628099Sroot rpp->p_nice = rip->p_nice; 9638099Sroot rpp->p_textp = isvfork ? 0 : rip->p_textp; 9648099Sroot rpp->p_pid = mpid; 9658099Sroot rpp->p_ppid = rip->p_pid; 9668099Sroot rpp->p_pptr = rip; 9678099Sroot rpp->p_osptr = rip->p_cptr; 9688099Sroot if (rip->p_cptr) 9698099Sroot rip->p_cptr->p_ysptr = rpp; 9708099Sroot rpp->p_ysptr = NULL; 9718099Sroot rpp->p_cptr = NULL; 9728099Sroot rip->p_cptr = rpp; 9738099Sroot rpp->p_time = 0; 9748099Sroot rpp->p_cpu = 0; 9758099Sroot rpp->p_siga0 = rip->p_siga0; 9768099Sroot rpp->p_siga1 = rip->p_siga1; 9778099Sroot /* take along any pending signals, like stops? */ 9788099Sroot if (isvfork) { 9798099Sroot rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0; 9808099Sroot rpp->p_szpt = clrnd(ctopt(UPAGES)); 9818099Sroot forkstat.cntvfork++; 9828099Sroot forkstat.sizvfork += rip->p_dsize + rip->p_ssize; 9838099Sroot } else { 9848099Sroot rpp->p_tsize = rip->p_tsize; 9858099Sroot rpp->p_dsize = rip->p_dsize; 9868099Sroot rpp->p_ssize = rip->p_ssize; 9878099Sroot rpp->p_szpt = rip->p_szpt; 9888099Sroot forkstat.cntfork++; 9898099Sroot forkstat.sizfork += rip->p_dsize + rip->p_ssize; 9908099Sroot } 9918099Sroot rpp->p_rssize = 0; 9928099Sroot rpp->p_maxrss = rip->p_maxrss; 9938099Sroot rpp->p_wchan = 0; 9948099Sroot rpp->p_slptime = 0; 9958099Sroot rpp->p_pctcpu = 0; 9968099Sroot rpp->p_cpticks = 0; 9978099Sroot n = PIDHASH(rpp->p_pid); 9988099Sroot p->p_idhash = pidhash[n]; 9998099Sroot pidhash[n] = rpp - proc; 10008099Sroot multprog++; 10018099Sroot 10028099Sroot /* 10038099Sroot * Increase reference counts on shared objects. 10048099Sroot */ 10058099Sroot for (n = 0; n < NOFILE; n++) { 10068099Sroot fp = u.u_ofile[n]; 10078099Sroot if (fp == NULL) 10088099Sroot continue; 10098099Sroot fp->f_count++; 10109159Ssam if (u.u_pofile[n]&SHLOCK) 10119159Ssam fp->f_inode->i_shlockc++; 10129159Ssam if (u.u_pofile[n]&EXLOCK) 10139159Ssam fp->f_inode->i_exlockc++; 10148099Sroot } 10158099Sroot u.u_cdir->i_count++; 10168099Sroot if (u.u_rdir) 10178099Sroot u.u_rdir->i_count++; 10188099Sroot 10198099Sroot /* 10208099Sroot * Partially simulate the environment 10218099Sroot * of the new process so that when it is actually 10228099Sroot * created (by copying) it will look right. 10238099Sroot * This begins the section where we must prevent the parent 10248099Sroot * from being swapped. 10258099Sroot */ 10268099Sroot rip->p_flag |= SKEEP; 10278099Sroot if (procdup(rpp, isvfork)) 10288099Sroot return (1); 10298099Sroot 10308099Sroot /* 10318099Sroot * Make child runnable and add to run queue. 10328099Sroot */ 10338099Sroot (void) spl6(); 10348099Sroot rpp->p_stat = SRUN; 10358099Sroot setrq(rpp); 10368099Sroot (void) spl0(); 10378099Sroot 10388099Sroot /* 10398099Sroot * Cause child to take a non-local goto as soon as it runs. 10408099Sroot * On older systems this was done with SSWAP bit in proc 10418099Sroot * table; on VAX we use u.u_pcb.pcb_sswap so don't need 10428099Sroot * to do rpp->p_flag |= SSWAP. Actually do nothing here. 10438099Sroot */ 10448099Sroot /* rpp->p_flag |= SSWAP; */ 10458099Sroot 10468099Sroot /* 10478099Sroot * Now can be swapped. 10488099Sroot */ 10498099Sroot rip->p_flag &= ~SKEEP; 10508099Sroot 10518099Sroot /* 10528099Sroot * If vfork make chain from parent process to child 10538099Sroot * (where virtal memory is temporarily). Wait for 10548099Sroot * child to finish, steal virtual memory back, 10558099Sroot * and wakeup child to let it die. 10568099Sroot */ 10578099Sroot if (isvfork) { 10588099Sroot u.u_procp->p_xlink = rpp; 10598099Sroot u.u_procp->p_flag |= SNOVM; 10608099Sroot while (rpp->p_flag & SVFORK) 10618099Sroot sleep((caddr_t)rpp, PZERO - 1); 10628099Sroot if ((rpp->p_flag & SLOAD) == 0) 10638099Sroot panic("newproc vfork"); 10648099Sroot uaccess(rpp, Vfmap, &vfutl); 10658099Sroot u.u_procp->p_xlink = 0; 10668099Sroot vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap); 10678099Sroot u.u_procp->p_flag &= ~SNOVM; 10688099Sroot rpp->p_ndx = rpp - proc; 10698099Sroot rpp->p_flag |= SVFDONE; 10708099Sroot wakeup((caddr_t)rpp); 10718099Sroot } 10728099Sroot 10738099Sroot /* 10748099Sroot * 0 return means parent. 10758099Sroot */ 10768099Sroot return (0); 10778099Sroot } 1078