1*7497Sroot /* kern_proc.c 4.28 82/07/24 */ 236Sbill 336Sbill #include "../h/param.h" 436Sbill #include "../h/systm.h" 536Sbill #include "../h/map.h" 636Sbill #include "../h/mtpr.h" 736Sbill #include "../h/dir.h" 836Sbill #include "../h/user.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" 15215Sbill #include "/usr/include/wait.h" 1636Sbill #include "../h/pte.h" 1736Sbill #include "../h/vm.h" 1836Sbill #include "../h/text.h" 19188Sbill #include "../h/psl.h" 20879Sbill #include "../h/vlimit.h" 21890Sbill #include "../h/file.h" 227488Skre #include "../h/quota.h" 23*7497Sroot #include "../h/descrip.h" 2436Sbill 2536Sbill /* 2636Sbill * exec system call, with and without environments. 2736Sbill */ 2836Sbill struct execa { 2936Sbill char *fname; 3036Sbill char **argp; 3136Sbill char **envp; 3236Sbill }; 3336Sbill 3436Sbill exec() 3536Sbill { 3636Sbill ((struct execa *)u.u_ap)->envp = NULL; 3736Sbill exece(); 3836Sbill } 3936Sbill 4036Sbill exece() 4136Sbill { 4236Sbill register nc; 4336Sbill register char *cp; 4436Sbill register struct buf *bp; 4536Sbill register struct execa *uap; 4636Sbill int na, ne, ucp, ap, c; 472301Skre int indir, uid, gid; 482301Skre char *sharg; 4936Sbill struct inode *ip; 5036Sbill swblk_t bno; 516568Smckusic char cfname[MAXNAMLEN + 1]; 522301Skre char cfarg[SHSIZE]; 5336Sbill 545991Swnj if ((ip = namei(uchar, 0, 1)) == NULL) 5536Sbill return; 5636Sbill bno = 0; 5736Sbill bp = 0; 582301Skre indir = 0; 592301Skre uid = u.u_uid; 602301Skre gid = u.u_gid; 612301Skre if (ip->i_mode & ISUID) 622301Skre uid = ip->i_uid; 632301Skre if (ip->i_mode & ISGID) 642301Skre gid = ip->i_gid; 652301Skre 662301Skre again: 674827Swnj if (access(ip, IEXEC)) 6836Sbill goto bad; 694827Swnj if ((u.u_procp->p_flag&STRC) && access(ip, IREAD)) 702330Swnj goto bad; 714827Swnj if ((ip->i_mode & IFMT) != IFREG || 7236Sbill (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { 7336Sbill u.u_error = EACCES; 7436Sbill goto bad; 7536Sbill } 762301Skre 7736Sbill /* 782301Skre * Read in first few bytes of file for segment sizes, ux_mag: 792301Skre * 407 = plain executable 802301Skre * 410 = RO text 812301Skre * 413 = demand paged RO text 822301Skre * Also an ASCII line beginning with #! is 832301Skre * the file name of a ``shell'' and arguments may be prepended 842301Skre * to the argument list if given here. 852301Skre * 862301Skre * SHELL NAMES ARE LIMITED IN LENGTH. 872301Skre * 882301Skre * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM 892301Skre * THE ASCII LINE. 902301Skre */ 912301Skre u.u_base = (caddr_t)&u.u_exdata; 922301Skre u.u_count = sizeof(u.u_exdata); 932301Skre u.u_offset = 0; 942301Skre u.u_segflg = 1; 952301Skre readi(ip); 962301Skre u.u_segflg = 0; 974827Swnj if (u.u_error) 982301Skre goto bad; 994982Swnj if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) && 1004982Swnj u.u_exdata.ux_shell[0] != '#') { 1012301Skre u.u_error = ENOEXEC; 1022301Skre goto bad; 1032301Skre } 1042301Skre switch (u.u_exdata.ux_mag) { 1052301Skre 1062301Skre case 0407: 1072301Skre u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; 1082301Skre u.u_exdata.ux_tsize = 0; 1092301Skre break; 1102301Skre 1112301Skre case 0413: 1122301Skre case 0410: 1132301Skre if (u.u_exdata.ux_tsize == 0) { 1142301Skre u.u_error = ENOEXEC; 1152301Skre goto bad; 1162301Skre } 1172301Skre break; 1182301Skre 1192301Skre default: 1202301Skre if (u.u_exdata.ux_shell[0] != '#' || 1212301Skre u.u_exdata.ux_shell[1] != '!' || 1222301Skre indir) { 1232301Skre u.u_error = ENOEXEC; 1242301Skre goto bad; 1252301Skre } 1262301Skre cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */ 1272301Skre while (cp < &u.u_exdata.ux_shell[SHSIZE]) { 1282301Skre if (*cp == '\t') 1292301Skre *cp = ' '; 1302301Skre else if (*cp == '\n') { 1312301Skre *cp = '\0'; 1322301Skre break; 1332301Skre } 1342301Skre cp++; 1352301Skre } 1362301Skre if (*cp != '\0') { 1372301Skre u.u_error = ENOEXEC; 1382301Skre goto bad; 1392301Skre } 1402301Skre cp = &u.u_exdata.ux_shell[2]; 1412301Skre while (*cp == ' ') 1422301Skre cp++; 1432301Skre u.u_dirp = cp; 1442301Skre while (*cp && *cp != ' ') 1452301Skre cp++; 1462301Skre sharg = NULL; 1472301Skre if (*cp) { 1482301Skre *cp++ = '\0'; 1492301Skre while (*cp == ' ') 1502301Skre cp++; 1512301Skre if (*cp) { 1522301Skre bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE); 1532301Skre sharg = cfarg; 1542301Skre } 1552301Skre } 1566568Smckusic bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname, 1576568Smckusic u.u_dent.d_namlen + 1); 1582301Skre indir = 1; 1592301Skre iput(ip); 1605991Swnj ip = namei(schar, 0, 1); 1612301Skre if (ip == NULL) 1622301Skre return; 1632301Skre goto again; 1642301Skre } 1652301Skre 1662301Skre /* 16736Sbill * Collect arguments on "file" in swap space. 16836Sbill */ 16936Sbill na = 0; 17036Sbill ne = 0; 17136Sbill nc = 0; 17236Sbill uap = (struct execa *)u.u_ap; 1732780Swnj if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { 17436Sbill swkill(u.u_procp, "exece"); 17536Sbill goto bad; 17636Sbill } 17736Sbill if (bno % CLSIZE) 1782780Swnj panic("execa rmalloc"); 17936Sbill if (uap->argp) for (;;) { 18036Sbill ap = NULL; 1813621Sroot if (indir && (na == 1 || na == 2 && sharg)) 1822301Skre ap = (int)uap->fname; 1832301Skre else if (uap->argp) { 18436Sbill ap = fuword((caddr_t)uap->argp); 18536Sbill uap->argp++; 18636Sbill } 18736Sbill if (ap==NULL && uap->envp) { 18836Sbill uap->argp = NULL; 18936Sbill if ((ap = fuword((caddr_t)uap->envp)) == NULL) 19036Sbill break; 19136Sbill uap->envp++; 19236Sbill ne++; 19336Sbill } 1946568Smckusic if (ap == NULL) 19536Sbill break; 19636Sbill na++; 1974827Swnj if (ap == -1) 19836Sbill u.u_error = EFAULT; 19936Sbill do { 20036Sbill if (nc >= NCARGS-1) 20136Sbill u.u_error = E2BIG; 2022301Skre if (indir && na == 2 && sharg != NULL) 2032301Skre c = *sharg++ & 0377; 2042301Skre else if ((c = fubyte((caddr_t)ap++)) < 0) 20536Sbill u.u_error = EFAULT; 20683Sbill if (u.u_error) { 20783Sbill if (bp) 20883Sbill brelse(bp); 20983Sbill bp = 0; 21036Sbill goto badarg; 21183Sbill } 2126568Smckusic if (nc % (CLSIZE*NBPG) == 0) { 21336Sbill if (bp) 21436Sbill bdwrite(bp); 2156568Smckusic bp = getblk(argdev, bno + nc / NBPG, 2166568Smckusic CLSIZE*NBPG); 21736Sbill cp = bp->b_un.b_addr; 21836Sbill } 21936Sbill nc++; 22036Sbill *cp++ = c; 2216568Smckusic } while (c > 0); 22236Sbill } 22336Sbill if (bp) 22436Sbill bdwrite(bp); 22536Sbill bp = 0; 22636Sbill nc = (nc + NBPW-1) & ~(NBPW-1); 2276568Smckusic if (indir) { 2286568Smckusic u.u_dent.d_namlen = strlen(cfname); 2296568Smckusic bcopy((caddr_t)cfname, (caddr_t)u.u_dent.d_name, 2306568Smckusic u.u_dent.d_namlen + 1); 2316568Smckusic } 2322301Skre getxfile(ip, nc + (na+4)*NBPW, uid, gid); 233912Sbill if (u.u_error) { 23436Sbill badarg: 2356568Smckusic for (c = 0; c < nc; c += CLSIZE*NBPG) 2366568Smckusic if (bp = baddr(argdev, bno + c / NBPG, CLSIZE*NBPG)) { 23736Sbill bp->b_flags |= B_AGE; /* throw away */ 23836Sbill bp->b_flags &= ~B_DELWRI; /* cancel io */ 23936Sbill brelse(bp); 24036Sbill bp = 0; 24136Sbill } 24236Sbill goto bad; 24336Sbill } 24436Sbill 24536Sbill /* 24636Sbill * copy back arglist 24736Sbill */ 24836Sbill ucp = USRSTACK - nc - NBPW; 24936Sbill ap = ucp - na*NBPW - 3*NBPW; 25036Sbill u.u_ar0[SP] = ap; 251132Sbill (void) suword((caddr_t)ap, na-ne); 25236Sbill nc = 0; 25336Sbill for (;;) { 25436Sbill ap += NBPW; 25536Sbill if (na==ne) { 256132Sbill (void) suword((caddr_t)ap, 0); 25736Sbill ap += NBPW; 25836Sbill } 25936Sbill if (--na < 0) 26036Sbill break; 261132Sbill (void) suword((caddr_t)ap, ucp); 26236Sbill do { 2636568Smckusic if (nc % (CLSIZE*NBPG) == 0) { 26436Sbill if (bp) 26536Sbill brelse(bp); 2666568Smckusic bp = bread(argdev, bno + nc / NBPG, 2676568Smckusic CLSIZE*NBPG); 26836Sbill bp->b_flags |= B_AGE; /* throw away */ 26936Sbill bp->b_flags &= ~B_DELWRI; /* cancel io */ 27036Sbill cp = bp->b_un.b_addr; 27136Sbill } 272132Sbill (void) subyte((caddr_t)ucp++, (c = *cp++)); 27336Sbill nc++; 27436Sbill } while(c&0377); 27536Sbill } 276132Sbill (void) suword((caddr_t)ap, 0); 277132Sbill (void) suword((caddr_t)ucp, 0); 27836Sbill setregs(); 27936Sbill bad: 28036Sbill if (bp) 28136Sbill brelse(bp); 28236Sbill if (bno) 2832780Swnj rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); 28436Sbill iput(ip); 28536Sbill } 28636Sbill 28736Sbill /* 28836Sbill * Read in and set up memory for executed file. 28936Sbill */ 2902301Skre getxfile(ip, nargc, uid, gid) 29136Sbill register struct inode *ip; 29236Sbill { 29336Sbill register size_t ts, ds, ss; 2942301Skre int pagi; 29536Sbill 2962301Skre if (u.u_exdata.ux_mag == 0413) 29736Sbill pagi = SPAGI; 2982301Skre else 2992301Skre pagi = 0; 3004827Swnj if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && 3014827Swnj ip->i_count!=1) { 302890Sbill register struct file *fp; 303890Sbill 3044827Swnj for (fp = file; fp < fileNFILE; fp++) { 305*7497Sroot if (fp->f_type == DTYPE_FILE && 306*7497Sroot fp->f_inode == ip && (fp->f_flag&FWRITE)) { 307890Sbill u.u_error = ETXTBSY; 308890Sbill goto bad; 309890Sbill } 3104827Swnj } 31136Sbill } 31236Sbill 31336Sbill /* 3144827Swnj * Compute text and data sizes and make sure not too large. 31536Sbill */ 31636Sbill ts = clrnd(btoc(u.u_exdata.ux_tsize)); 31736Sbill ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); 31836Sbill ss = clrnd(SSIZE + btoc(nargc)); 319912Sbill if (chksize(ts, ds, ss)) 320912Sbill goto bad; 3214827Swnj 3224827Swnj /* 3234827Swnj * Make sure enough space to start process. 3244827Swnj */ 325912Sbill u.u_cdmap = zdmap; 326912Sbill u.u_csmap = zdmap; 327912Sbill if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) 328912Sbill goto bad; 32936Sbill 330912Sbill /* 331912Sbill * At this point, committed to the new image! 332912Sbill * Release virtual memory resources of old process, and 333912Sbill * initialize the virtual memory of the new process. 334912Sbill * If we resulted from vfork(), instead wakeup our 335912Sbill * parent who will set SVFDONE when he has taken back 336912Sbill * our resources. 337912Sbill */ 338912Sbill u.u_prof.pr_scale = 0; 339912Sbill if ((u.u_procp->p_flag & SVFORK) == 0) 340912Sbill vrelvm(); 341912Sbill else { 342912Sbill u.u_procp->p_flag &= ~SVFORK; 343912Sbill u.u_procp->p_flag |= SKEEP; 344912Sbill wakeup((caddr_t)u.u_procp); 345912Sbill while ((u.u_procp->p_flag & SVFDONE) == 0) 346912Sbill sleep((caddr_t)u.u_procp, PZERO - 1); 347912Sbill u.u_procp->p_flag &= ~(SVFDONE|SKEEP); 348912Sbill } 3493592Swnj u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG); 350912Sbill u.u_procp->p_flag |= pagi; 351912Sbill u.u_dmap = u.u_cdmap; 352912Sbill u.u_smap = u.u_csmap; 353912Sbill vgetvm(ts, ds, ss); 354912Sbill 355912Sbill if (pagi == 0) { 35636Sbill /* 357912Sbill * Read in data segment. 35836Sbill */ 359912Sbill u.u_base = (char *)ctob(ts); 360912Sbill u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize; 361912Sbill u.u_count = u.u_exdata.ux_dsize; 362912Sbill readi(ip); 363912Sbill } 364912Sbill xalloc(ip, pagi); 365912Sbill if (pagi && u.u_procp->p_textp) 366912Sbill vinifod((struct fpte *)dptopte(u.u_procp, 0), 367912Sbill PG_FTEXT, u.u_procp->p_textp->x_iptr, 368912Sbill 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); 36936Sbill 370912Sbill /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ 371912Sbill mtpr(TBIA, 0); 37236Sbill 373912Sbill /* 374912Sbill * set SUID/SGID protections, if no tracing 375912Sbill */ 376912Sbill if ((u.u_procp->p_flag&STRC)==0) { 3774827Swnj u.u_uid = uid; 3784827Swnj u.u_procp->p_uid = uid; 3792301Skre u.u_gid = gid; 3805856Swnj u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8)); 381912Sbill } else 382912Sbill psignal(u.u_procp, SIGTRAP); 38336Sbill u.u_tsize = ts; 38436Sbill u.u_dsize = ds; 38536Sbill u.u_ssize = ss; 38636Sbill bad: 387912Sbill return; 38836Sbill } 38936Sbill 39036Sbill /* 39136Sbill * Clear registers on exec 39236Sbill */ 39336Sbill setregs() 39436Sbill { 395173Sbill register int (**rp)(); 39636Sbill register i; 397188Sbill long sigmask; 39836Sbill 3996464Swnj for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG]; 400188Sbill sigmask <<= 1, rp++) { 401188Sbill switch (*rp) { 402188Sbill 403188Sbill case SIG_IGN: 404188Sbill case SIG_DFL: 405188Sbill case SIG_HOLD: 406188Sbill continue; 407188Sbill 408188Sbill default: 409188Sbill /* 410206Sbill * Normal or deferring catch; revert to default. 411188Sbill */ 412206Sbill (void) spl6(); 413206Sbill *rp = SIG_DFL; 414188Sbill if ((int)*rp & 1) 415188Sbill u.u_procp->p_siga0 |= sigmask; 416188Sbill else 4176327Swnj u.u_procp->p_siga0 &= ~sigmask; 418188Sbill if ((int)*rp & 2) 419188Sbill u.u_procp->p_siga1 |= sigmask; 420188Sbill else 421188Sbill u.u_procp->p_siga1 &= ~sigmask; 422206Sbill (void) spl0(); 423188Sbill continue; 424188Sbill } 425188Sbill } 42636Sbill /* 4274827Swnj for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) 42836Sbill *rp++ = 0; 42936Sbill */ 43036Sbill u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ 4314827Swnj for (i=0; i<NOFILE; i++) { 43236Sbill if (u.u_pofile[i]&EXCLOSE) { 4335581Swnj closef(u.u_ofile[i], 1); 43436Sbill u.u_ofile[i] = NULL; 435188Sbill u.u_pofile[i] &= ~EXCLOSE; 43636Sbill } 43736Sbill } 4384827Swnj 43936Sbill /* 44036Sbill * Remember file name for accounting. 44136Sbill */ 44236Sbill u.u_acflag &= ~AFORK; 4436568Smckusic bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm, 4446568Smckusic u.u_dent.d_namlen + 1); 44536Sbill } 44636Sbill 44736Sbill /* 4484827Swnj * Exit system call: pass back caller's arg 44936Sbill */ 45036Sbill rexit() 45136Sbill { 45236Sbill register struct a { 45336Sbill int rval; 45436Sbill } *uap; 45536Sbill 45636Sbill uap = (struct a *)u.u_ap; 45736Sbill exit((uap->rval & 0377) << 8); 45836Sbill } 45936Sbill 46036Sbill /* 46136Sbill * Release resources. 46236Sbill * Save u. area for parent to look at. 46336Sbill * Enter zombie state. 46436Sbill * Wake up parent and init processes, 46536Sbill * and dispose of children. 46636Sbill */ 46736Sbill exit(rv) 46836Sbill { 46936Sbill register int i; 47036Sbill register struct proc *p, *q; 47136Sbill register struct file *f; 47236Sbill register int x; 47336Sbill 47436Sbill #ifdef PGINPROF 47536Sbill vmsizmon(); 47636Sbill #endif 47736Sbill p = u.u_procp; 47836Sbill p->p_flag &= ~(STRC|SULOCK); 47936Sbill p->p_flag |= SWEXIT; 48036Sbill p->p_clktim = 0; 481188Sbill (void) spl6(); 482188Sbill if ((int)SIG_IGN & 1) 483188Sbill p->p_siga0 = ~0; 484188Sbill else 485188Sbill p->p_siga0 = 0; 486188Sbill if ((int)SIG_IGN & 2) 487188Sbill p->p_siga1 = ~0; 488188Sbill else 489206Sbill p->p_siga1 = 0; 490188Sbill (void) spl0(); 4911399Sbill p->p_cpticks = 0; 4921399Sbill p->p_pctcpu = 0; 4934827Swnj for (i=0; i<NSIG; i++) 494173Sbill u.u_signal[i] = SIG_IGN; 49536Sbill /* 49636Sbill * Release virtual memory. If we resulted from 49736Sbill * a vfork(), instead give the resources back to 49836Sbill * the parent. 49936Sbill */ 50036Sbill if ((p->p_flag & SVFORK) == 0) 50136Sbill vrelvm(); 50236Sbill else { 50336Sbill p->p_flag &= ~SVFORK; 50436Sbill wakeup((caddr_t)p); 50536Sbill while ((p->p_flag & SVFDONE) == 0) 50636Sbill sleep((caddr_t)p, PZERO - 1); 50736Sbill p->p_flag &= ~SVFDONE; 50836Sbill } 5094827Swnj for (i=0; i<NOFILE; i++) { 51036Sbill f = u.u_ofile[i]; 51136Sbill u.u_ofile[i] = NULL; 5125581Swnj closef(f, 1); 51336Sbill } 5144827Swnj ilock(u.u_cdir); 51536Sbill iput(u.u_cdir); 51636Sbill if (u.u_rdir) { 5174827Swnj ilock(u.u_rdir); 51836Sbill iput(u.u_rdir); 51936Sbill } 520363Sbill u.u_limit[LIM_FSIZE] = INFINITY; 52136Sbill acct(); 5227488Skre #ifdef QUOTA 5237488Skre qclean(); 5247488Skre #endif 52536Sbill vrelpt(u.u_procp); 52636Sbill vrelu(u.u_procp, 0); 5275630Swnj (void) spl5(); /* hack for mem alloc race XXX */ 52836Sbill multprog--; 529931Sbill p->p_stat = SZOMB; 530307Sbill noproc = 1; 53136Sbill i = PIDHASH(p->p_pid); 53236Sbill x = p - proc; 53336Sbill if (pidhash[i] == x) 53436Sbill pidhash[i] = p->p_idhash; 53536Sbill else { 53636Sbill for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 53736Sbill if (proc[i].p_idhash == x) { 53836Sbill proc[i].p_idhash = p->p_idhash; 53936Sbill goto done; 54036Sbill } 54136Sbill panic("exit"); 54236Sbill } 5431409Sbill if (p->p_pid == 1) 5441409Sbill panic("init died"); 54536Sbill done: 54636Sbill ((struct xproc *)p)->xp_xstat = rv; /* overlay */ 54736Sbill ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ 54836Sbill vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); 5494827Swnj for (q = proc; q < procNPROC; q++) 5504827Swnj if (q->p_pptr == p) { 5517488Skre if (q->p_osptr) 5527488Skre q->p_osptr->p_ysptr = q->p_ysptr; 5537488Skre if (q->p_ysptr) 5547488Skre q->p_ysptr->p_osptr = q->p_osptr; 5557488Skre if (proc[1].p_cptr) 5567488Skre proc[1].p_cptr->p_ysptr = q; 5577488Skre q->p_osptr = proc[1].p_cptr; 5587488Skre q->p_ysptr = NULL; 5597488Skre proc[1].p_cptr = q; 5607488Skre 561188Sbill q->p_pptr = &proc[1]; 562188Sbill q->p_ppid = 1; 56336Sbill wakeup((caddr_t)&proc[1]); 564188Sbill /* 565212Sbill * Traced processes are killed 566188Sbill * since their existence means someone is screwing up. 567354Sbill * Stopped processes are sent a hangup and a continue. 568212Sbill * This is designed to be ``safe'' for setuid 569212Sbill * processes since they must be willing to tolerate 570212Sbill * hangups anyways. 571188Sbill */ 572212Sbill if (q->p_flag&STRC) { 573188Sbill q->p_flag &= ~STRC; 574188Sbill psignal(q, SIGKILL); 575212Sbill } else if (q->p_stat == SSTOP) { 576212Sbill psignal(q, SIGHUP); 577212Sbill psignal(q, SIGCONT); 578188Sbill } 579215Sbill /* 580215Sbill * Protect this process from future 5815619Swnj * tty signals, clear TSTP/TTIN/TTOU if pending. 582215Sbill */ 5831788Sbill (void) spgrp(q, -1); 58436Sbill } 5856464Swnj psignal(p->p_pptr, SIGCHLD); 586188Sbill wakeup((caddr_t)p->p_pptr); 58736Sbill swtch(); 58836Sbill } 58936Sbill 59036Sbill wait() 59136Sbill { 592188Sbill struct vtimes vm; 593188Sbill struct vtimes *vp; 59436Sbill 595188Sbill if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 596188Sbill wait1(0, (struct vtimes *)0); 597188Sbill return; 598188Sbill } 599188Sbill vp = (struct vtimes *)u.u_ar0[R1]; 600188Sbill wait1(u.u_ar0[R0], &vm); 601188Sbill if (u.u_error) 602188Sbill return; 603188Sbill (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); 60436Sbill } 60536Sbill 60636Sbill /* 60736Sbill * Wait system call. 60836Sbill * Search for a terminated (zombie) child, 60936Sbill * finally lay it to rest, and collect its status. 61036Sbill * Look also for stopped (traced) children, 61136Sbill * and pass back status from them. 61236Sbill */ 613188Sbill wait1(options, vp) 614188Sbill register options; 61536Sbill struct vtimes *vp; 61636Sbill { 61736Sbill register f; 6187488Skre register struct proc *p, *q; 61936Sbill 62036Sbill f = 0; 62136Sbill loop: 6224827Swnj for (p = proc; p < procNPROC; p++) 6234827Swnj if (p->p_pptr == u.u_procp) { 62436Sbill f++; 6254827Swnj if (p->p_stat == SZOMB) { 62636Sbill u.u_r.r_val1 = p->p_pid; 62736Sbill u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; 62836Sbill ((struct xproc *)p)->xp_xstat = 0; 62936Sbill if (vp) 63036Sbill *vp = ((struct xproc *)p)->xp_vm; 63136Sbill vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); 63236Sbill ((struct xproc *)p)->xp_vm = zvms; 63336Sbill p->p_stat = NULL; 63436Sbill p->p_pid = 0; 63536Sbill p->p_ppid = 0; 6367488Skre if (q = p->p_ysptr) 6377488Skre q->p_osptr = p->p_osptr; 6387488Skre if (q = p->p_osptr) 6397488Skre q->p_ysptr = p->p_ysptr; 6407488Skre if ((q = p->p_pptr)->p_cptr == p) 6417488Skre q->p_cptr = p->p_osptr; 642188Sbill p->p_pptr = 0; 6437488Skre p->p_ysptr = 0; 6447488Skre p->p_osptr = 0; 6457488Skre p->p_cptr = 0; 64636Sbill p->p_sig = 0; 647188Sbill p->p_siga0 = 0; 648188Sbill p->p_siga1 = 0; 64936Sbill p->p_pgrp = 0; 65036Sbill p->p_flag = 0; 65136Sbill p->p_wchan = 0; 652188Sbill p->p_cursig = 0; 65336Sbill return; 65436Sbill } 655188Sbill if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 656188Sbill (p->p_flag&STRC || options&WUNTRACED)) { 657188Sbill p->p_flag |= SWTED; 658188Sbill u.u_r.r_val1 = p->p_pid; 659188Sbill u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 660188Sbill return; 66136Sbill } 66236Sbill } 663188Sbill if (f==0) { 664188Sbill u.u_error = ECHILD; 665188Sbill return; 66636Sbill } 667188Sbill if (options&WNOHANG) { 668188Sbill u.u_r.r_val1 = 0; 669188Sbill return; 670188Sbill } 671912Sbill if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { 672188Sbill u.u_eosys = RESTARTSYS; 673188Sbill return; 674188Sbill } 675188Sbill sleep((caddr_t)u.u_procp, PWAIT); 676188Sbill goto loop; 67736Sbill } 67836Sbill 67936Sbill /* 68036Sbill * fork system call. 68136Sbill */ 68236Sbill fork() 68336Sbill { 68436Sbill 68536Sbill u.u_cdmap = zdmap; 68636Sbill u.u_csmap = zdmap; 68736Sbill if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { 68836Sbill u.u_r.r_val2 = 0; 68936Sbill return; 69036Sbill } 69136Sbill fork1(0); 69236Sbill } 69336Sbill 69436Sbill fork1(isvfork) 69536Sbill { 69636Sbill register struct proc *p1, *p2; 6977488Skre #ifndef QUOTA 69836Sbill register a; 69936Sbill 70036Sbill a = 0; 7017488Skre #else 7027488Skre if (u.u_quota != NOQUOT && u.u_quota->q_plim && 7037488Skre u.u_quota->q_cnt >= u.u_quota->q_plim) { 7047488Skre u.u_error = EPROCLIM; 7057488Skre return; 7067488Skre } 7077488Skre #endif 70836Sbill p2 = NULL; 7094827Swnj for (p1 = proc; p1 < procNPROC; p1++) { 7107488Skre #ifdef QUOTA 7117488Skre if (p1->p_stat == NULL) { 7127488Skre p2 = p1; 7137488Skre break; 7147488Skre } 7157488Skre #else 71636Sbill if (p1->p_stat==NULL && p2==NULL) 71736Sbill p2 = p1; 71836Sbill else { 71936Sbill if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) 72036Sbill a++; 72136Sbill } 7227488Skre #endif 72336Sbill } 72436Sbill /* 72536Sbill * Disallow if 72636Sbill * No processes at all; 72736Sbill * not su and too many procs owned; or 72836Sbill * not su and would take last slot. 72936Sbill */ 7302938Swnj if (p2==NULL) 7312938Swnj tablefull("proc"); 7327488Skre #ifdef QUOTA 7337488Skre if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) { 7347488Skre #else 7352741Swnj if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { 7367488Skre #endif 73736Sbill u.u_error = EAGAIN; 73836Sbill if (!isvfork) { 739132Sbill (void) vsexpand(0, &u.u_cdmap, 1); 740132Sbill (void) vsexpand(0, &u.u_csmap, 1); 74136Sbill } 74236Sbill goto out; 74336Sbill } 74436Sbill p1 = u.u_procp; 7454827Swnj if (newproc(isvfork)) { 74636Sbill u.u_r.r_val1 = p1->p_pid; 74736Sbill u.u_r.r_val2 = 1; /* child */ 74836Sbill u.u_start = time; 74936Sbill u.u_acflag = AFORK; 7507488Skre #ifdef QUOTA 7517488Skre u.u_qflags &= ~QUF_LOGIN; 7527488Skre #endif 75336Sbill return; 75436Sbill } 75536Sbill u.u_r.r_val1 = p2->p_pid; 75636Sbill 75736Sbill out: 75836Sbill u.u_r.r_val2 = 0; 75936Sbill } 76036Sbill 761*7497Sroot spgrp(top, npgrp) 762*7497Sroot register struct proc *top; 763*7497Sroot { 764*7497Sroot register struct proc *pp, *p; 765*7497Sroot int f = 0; 766*7497Sroot 767*7497Sroot for (p = top; npgrp == -1 || u.u_uid == p->p_uid || 768*7497Sroot !u.u_uid || inferior(p); p = pp) { 769*7497Sroot if (npgrp == -1) { 770*7497Sroot #define bit(a) (1<<(a-1)) 771*7497Sroot p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); 772*7497Sroot } else 773*7497Sroot p->p_pgrp = npgrp; 774*7497Sroot f++; 775*7497Sroot /* 776*7497Sroot * Search for children. 777*7497Sroot */ 778*7497Sroot for (pp = proc; pp < procNPROC; pp++) 779*7497Sroot if (pp->p_pptr == p) 780*7497Sroot goto cont; 781*7497Sroot /* 782*7497Sroot * Search for siblings. 783*7497Sroot */ 784*7497Sroot for (; p != top; p = p->p_pptr) 785*7497Sroot for (pp = p + 1; pp < procNPROC; pp++) 786*7497Sroot if (pp->p_pptr == p->p_pptr) 787*7497Sroot goto cont; 788*7497Sroot break; 789*7497Sroot cont: 790*7497Sroot ; 791*7497Sroot } 792*7497Sroot return (f); 793*7497Sroot } 794*7497Sroot 79536Sbill /* 796*7497Sroot * Is p an inferior of the current process? 79736Sbill */ 798*7497Sroot inferior(p) 799*7497Sroot register struct proc *p; 80036Sbill { 80136Sbill 802*7497Sroot for (; p != u.u_procp; p = p->p_pptr) 803*7497Sroot if (p->p_ppid == 0) 804*7497Sroot return (0); 805*7497Sroot return (1); 80636Sbill } 807