1*215Sbill /* kern_proc.c 3.8 10/14/12 */ 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" 15*215Sbill #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" 2036Sbill 2136Sbill /* 2236Sbill * exec system call, with and without environments. 2336Sbill */ 2436Sbill struct execa { 2536Sbill char *fname; 2636Sbill char **argp; 2736Sbill char **envp; 2836Sbill }; 2936Sbill 3036Sbill exec() 3136Sbill { 3236Sbill ((struct execa *)u.u_ap)->envp = NULL; 3336Sbill exece(); 3436Sbill } 3536Sbill 3636Sbill exece() 3736Sbill { 3836Sbill register nc; 3936Sbill register char *cp; 4036Sbill register struct buf *bp; 4136Sbill register struct execa *uap; 4236Sbill int na, ne, ucp, ap, c; 4336Sbill struct inode *ip; 4436Sbill swblk_t bno; 4536Sbill 4636Sbill if ((ip = namei(uchar, 0)) == NULL) 4736Sbill return; 4836Sbill bno = 0; 4936Sbill bp = 0; 5036Sbill if(access(ip, IEXEC)) 5136Sbill goto bad; 5236Sbill if((ip->i_mode & IFMT) != IFREG || 5336Sbill (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { 5436Sbill u.u_error = EACCES; 5536Sbill goto bad; 5636Sbill } 5736Sbill /* 5836Sbill * Collect arguments on "file" in swap space. 5936Sbill */ 6036Sbill na = 0; 6136Sbill ne = 0; 6236Sbill nc = 0; 6336Sbill uap = (struct execa *)u.u_ap; 6436Sbill if ((bno = malloc(swapmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { 6536Sbill swkill(u.u_procp, "exece"); 6636Sbill goto bad; 6736Sbill } 6836Sbill if (bno % CLSIZE) 6936Sbill panic("execa malloc"); 7036Sbill if (uap->argp) for (;;) { 7136Sbill ap = NULL; 7236Sbill if (uap->argp) { 7336Sbill ap = fuword((caddr_t)uap->argp); 7436Sbill uap->argp++; 7536Sbill } 7636Sbill if (ap==NULL && uap->envp) { 7736Sbill uap->argp = NULL; 7836Sbill if ((ap = fuword((caddr_t)uap->envp)) == NULL) 7936Sbill break; 8036Sbill uap->envp++; 8136Sbill ne++; 8236Sbill } 8336Sbill if (ap==NULL) 8436Sbill break; 8536Sbill na++; 8636Sbill if(ap == -1) 8736Sbill u.u_error = EFAULT; 8836Sbill do { 8936Sbill if (nc >= NCARGS-1) 9036Sbill u.u_error = E2BIG; 9136Sbill if ((c = fubyte((caddr_t)ap++)) < 0) 9236Sbill u.u_error = EFAULT; 9383Sbill if (u.u_error) { 9483Sbill if (bp) 9583Sbill brelse(bp); 9683Sbill bp = 0; 9736Sbill goto badarg; 9883Sbill } 9936Sbill if ((nc&BMASK) == 0) { 10036Sbill if (bp) 10136Sbill bdwrite(bp); 102*215Sbill bp = getblk(swapdev, 103*215Sbill (daddr_t)(dbtofsb(swplo+bno)+(nc>>BSHIFT))); 10436Sbill cp = bp->b_un.b_addr; 10536Sbill } 10636Sbill nc++; 10736Sbill *cp++ = c; 10836Sbill } while (c>0); 10936Sbill } 11036Sbill if (bp) 11136Sbill bdwrite(bp); 11236Sbill bp = 0; 11336Sbill nc = (nc + NBPW-1) & ~(NBPW-1); 11436Sbill if (getxfile(ip, nc) || u.u_error) { 11536Sbill badarg: 11636Sbill for (c = 0; c < nc; c += BSIZE) 11736Sbill if (bp = baddr(swapdev, dbtofsb(swplo+bno)+(c>>BSHIFT))) { 11836Sbill bp->b_flags |= B_AGE; /* throw away */ 11936Sbill bp->b_flags &= ~B_DELWRI; /* cancel io */ 12036Sbill brelse(bp); 12136Sbill bp = 0; 12236Sbill } 12336Sbill goto bad; 12436Sbill } 12536Sbill 12636Sbill /* 12736Sbill * copy back arglist 12836Sbill */ 12936Sbill 13036Sbill ucp = USRSTACK - nc - NBPW; 13136Sbill ap = ucp - na*NBPW - 3*NBPW; 13236Sbill u.u_ar0[SP] = ap; 133132Sbill (void) suword((caddr_t)ap, na-ne); 13436Sbill nc = 0; 13536Sbill for (;;) { 13636Sbill ap += NBPW; 13736Sbill if (na==ne) { 138132Sbill (void) suword((caddr_t)ap, 0); 13936Sbill ap += NBPW; 14036Sbill } 14136Sbill if (--na < 0) 14236Sbill break; 143132Sbill (void) suword((caddr_t)ap, ucp); 14436Sbill do { 14536Sbill if ((nc&BMASK) == 0) { 14636Sbill if (bp) 14736Sbill brelse(bp); 14836Sbill bp = bread(swapdev, (daddr_t)(dbtofsb(swplo+bno)+(nc>>BSHIFT))); 14936Sbill bp->b_flags |= B_AGE; /* throw away */ 15036Sbill bp->b_flags &= ~B_DELWRI; /* cancel io */ 15136Sbill cp = bp->b_un.b_addr; 15236Sbill } 153132Sbill (void) subyte((caddr_t)ucp++, (c = *cp++)); 15436Sbill nc++; 15536Sbill } while(c&0377); 15636Sbill } 157132Sbill (void) suword((caddr_t)ap, 0); 158132Sbill (void) suword((caddr_t)ucp, 0); 15936Sbill setregs(); 16036Sbill bad: 16136Sbill if (bp) 16236Sbill brelse(bp); 16336Sbill if (bno) 16436Sbill mfree(swapmap, ctod(clrnd((int) btoc(NCARGS))), bno); 16536Sbill iput(ip); 16636Sbill } 16736Sbill 16836Sbill /* 16936Sbill * Read in and set up memory for executed file. 17036Sbill * Zero return is normal; 17136Sbill * non-zero means only the text is being replaced 17236Sbill */ 17336Sbill getxfile(ip, nargc) 17436Sbill register struct inode *ip; 17536Sbill { 17636Sbill register sep; 17736Sbill register size_t ts, ds, ss; 17836Sbill register int overlay; 17936Sbill int pagi = 0; 18036Sbill 18136Sbill /* 18236Sbill * read in first few bytes 18336Sbill * of file for segment 18436Sbill * sizes: 18536Sbill * ux_mag = 407/410/411/405 18636Sbill * 407 is plain executable 18736Sbill * 410 is RO text 18836Sbill * 411 is separated ID 18936Sbill * 405 is overlaid text 19036Sbill * 412 is demand paged plain executable (NOT IMPLEMENTED) 19136Sbill * 413 is demand paged RO text 19236Sbill */ 19336Sbill 19436Sbill u.u_base = (caddr_t)&u.u_exdata; 19536Sbill u.u_count = sizeof(u.u_exdata); 19636Sbill u.u_offset = 0; 19736Sbill u.u_segflg = 1; 19836Sbill readi(ip); 19936Sbill u.u_segflg = 0; 20036Sbill if(u.u_error) 20136Sbill goto bad; 20236Sbill if (u.u_count!=0) { 20336Sbill u.u_error = ENOEXEC; 20436Sbill goto bad; 20536Sbill } 20636Sbill sep = 0; 20736Sbill overlay = 0; 20836Sbill switch (u.u_exdata.ux_mag) { 20936Sbill 21036Sbill case 0405: 21136Sbill overlay++; 21236Sbill break; 21336Sbill 21436Sbill case 0412: 21536Sbill u.u_error = ENOEXEC; 21636Sbill goto bad; 21736Sbill 21836Sbill case 0407: 21936Sbill u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; 22036Sbill u.u_exdata.ux_tsize = 0; 22136Sbill break; 22236Sbill 22336Sbill case 0413: 22436Sbill pagi = SPAGI; 22536Sbill /* fall into ... */ 22636Sbill 22736Sbill case 0410: 22836Sbill if (u.u_exdata.ux_tsize == 0) { 22936Sbill u.u_error = ENOEXEC; 23036Sbill goto bad; 23136Sbill } 23236Sbill break; 23336Sbill 23436Sbill case 0411: 23536Sbill u.u_error = ENOEXEC; 23636Sbill goto bad; 23736Sbill 23836Sbill default: 23936Sbill u.u_error = ENOEXEC; 24036Sbill goto bad; 24136Sbill } 24236Sbill if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) { 24336Sbill u.u_error = ETXTBSY; 24436Sbill goto bad; 24536Sbill } 24636Sbill 24736Sbill /* 24836Sbill * find text and data sizes 24936Sbill * try them out for possible 25036Sbill * exceed of max sizes 25136Sbill */ 25236Sbill 25336Sbill ts = clrnd(btoc(u.u_exdata.ux_tsize)); 25436Sbill ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); 25536Sbill ss = clrnd(SSIZE + btoc(nargc)); 25636Sbill if (overlay) { 257*215Sbill if ((u.u_procp->p_flag & SPAGI) || 258*215Sbill u.u_sep==0 && ctos(ts) != ctos(u.u_tsize) || nargc) { 25936Sbill u.u_error = ENOMEM; 26036Sbill goto bad; 26136Sbill } 26236Sbill ds = u.u_dsize; 26336Sbill ss = u.u_ssize; 26436Sbill sep = u.u_sep; 26536Sbill xfree(); 26636Sbill xalloc(ip, pagi); 26736Sbill u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ 26836Sbill } else { 26936Sbill if (chksize(ts, ds, ss)) 27036Sbill goto bad; 27136Sbill u.u_cdmap = zdmap; 27236Sbill u.u_csmap = zdmap; 27336Sbill if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) 27436Sbill goto bad; 27536Sbill 27636Sbill /* 27736Sbill * At this point, committed to the new image! 27836Sbill * Release virtual memory resources of old process, and 27936Sbill * initialize the virtual memory of the new process. 28036Sbill * If we resulted from vfork(), instead wakeup our 28136Sbill * parent who will set SVFDONE when he has taken back 28236Sbill * our resources. 28336Sbill */ 28436Sbill u.u_prof.pr_scale = 0; 28536Sbill if ((u.u_procp->p_flag & SVFORK) == 0) 28636Sbill vrelvm(); 28736Sbill else { 28836Sbill u.u_procp->p_flag &= ~SVFORK; 28936Sbill u.u_procp->p_flag |= SKEEP; 29036Sbill wakeup((caddr_t)u.u_procp); 29136Sbill while ((u.u_procp->p_flag & SVFDONE) == 0) 29236Sbill sleep((caddr_t)u.u_procp, PZERO - 1); 29336Sbill u.u_procp->p_flag &= ~(SVFDONE|SKEEP); 29436Sbill } 29536Sbill u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM); 29636Sbill u.u_procp->p_flag |= pagi; 29736Sbill u.u_dmap = u.u_cdmap; 29836Sbill u.u_smap = u.u_csmap; 29936Sbill vgetvm(ts, ds, ss); 30036Sbill 30136Sbill if (pagi == 0) { 30236Sbill /* 30336Sbill * Read in data segment. 30436Sbill */ 30536Sbill u.u_base = (char *)ctob(ts); 30636Sbill u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize; 30736Sbill u.u_count = u.u_exdata.ux_dsize; 30836Sbill readi(ip); 30936Sbill } 31036Sbill xalloc(ip, pagi); 31136Sbill if (pagi && u.u_procp->p_textp) 31236Sbill vinifod((struct fpte *)dptopte(u.u_procp, 0), 31336Sbill PG_FTEXT, u.u_procp->p_textp->x_iptr, 31436Sbill 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); 31536Sbill 31636Sbill /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ 31736Sbill mtpr(TBIA,1); 31836Sbill 31936Sbill /* 32036Sbill * set SUID/SGID protections, if no tracing 32136Sbill */ 32236Sbill if ((u.u_procp->p_flag&STRC)==0) { 32336Sbill if(ip->i_mode&ISUID) 32436Sbill if(u.u_uid != 0) { 32536Sbill u.u_uid = ip->i_uid; 32636Sbill u.u_procp->p_uid = ip->i_uid; 32736Sbill } 32836Sbill if(ip->i_mode&ISGID) 32936Sbill u.u_gid = ip->i_gid; 33036Sbill } else 331173Sbill psignal(u.u_procp, SIGTRAP); 33236Sbill } 33336Sbill u.u_tsize = ts; 33436Sbill u.u_dsize = ds; 33536Sbill u.u_ssize = ss; 33636Sbill u.u_sep = sep; 33736Sbill bad: 33836Sbill return(overlay); 33936Sbill } 34036Sbill 34136Sbill /* 34236Sbill * Clear registers on exec 34336Sbill */ 34436Sbill setregs() 34536Sbill { 346173Sbill register int (**rp)(); 34736Sbill register i; 348188Sbill long sigmask; 34936Sbill 350188Sbill for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG]; 351188Sbill sigmask <<= 1, rp++) { 352188Sbill switch (*rp) { 353188Sbill 354188Sbill case SIG_IGN: 355188Sbill case SIG_DFL: 356188Sbill case SIG_HOLD: 357188Sbill continue; 358188Sbill 359188Sbill default: 360188Sbill /* 361206Sbill * Normal or deferring catch; revert to default. 362188Sbill */ 363206Sbill (void) spl6(); 364206Sbill *rp = SIG_DFL; 365188Sbill if ((int)*rp & 1) 366188Sbill u.u_procp->p_siga0 |= sigmask; 367188Sbill else 368188Sbill u.u_procp->p_siga1 &= ~sigmask; 369188Sbill if ((int)*rp & 2) 370188Sbill u.u_procp->p_siga1 |= sigmask; 371188Sbill else 372188Sbill u.u_procp->p_siga1 &= ~sigmask; 373206Sbill (void) spl0(); 374188Sbill continue; 375188Sbill } 376188Sbill } 37736Sbill /* 37836Sbill for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) 37936Sbill *rp++ = 0; 38036Sbill */ 38136Sbill u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ 38236Sbill for(i=0; i<NOFILE; i++) { 38336Sbill if (u.u_pofile[i]&EXCLOSE) { 38436Sbill closef(u.u_ofile[i]); 38536Sbill u.u_ofile[i] = NULL; 386188Sbill u.u_pofile[i] &= ~EXCLOSE; 38736Sbill } 38836Sbill } 38936Sbill /* 39036Sbill * Remember file name for accounting. 39136Sbill */ 39236Sbill u.u_acflag &= ~AFORK; 39336Sbill bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ); 39436Sbill } 39536Sbill 39636Sbill /* 39736Sbill * exit system call: 39836Sbill * pass back caller's arg 39936Sbill */ 40036Sbill rexit() 40136Sbill { 40236Sbill register struct a { 40336Sbill int rval; 40436Sbill } *uap; 40536Sbill 40636Sbill uap = (struct a *)u.u_ap; 40736Sbill exit((uap->rval & 0377) << 8); 40836Sbill } 40936Sbill 41036Sbill /* 41136Sbill * Release resources. 41236Sbill * Save u. area for parent to look at. 41336Sbill * Enter zombie state. 41436Sbill * Wake up parent and init processes, 41536Sbill * and dispose of children. 41636Sbill */ 41736Sbill exit(rv) 41836Sbill { 41936Sbill register int i; 42036Sbill register struct proc *p, *q; 42136Sbill register struct file *f; 42236Sbill register int x; 42336Sbill 42436Sbill #ifdef PGINPROF 42536Sbill vmsizmon(); 42636Sbill #endif 42736Sbill p = u.u_procp; 42836Sbill p->p_flag &= ~(STRC|SULOCK); 42936Sbill p->p_flag |= SWEXIT; 43036Sbill p->p_clktim = 0; 431188Sbill (void) spl6(); 432188Sbill if ((int)SIG_IGN & 1) 433188Sbill p->p_siga0 = ~0; 434188Sbill else 435188Sbill p->p_siga0 = 0; 436188Sbill if ((int)SIG_IGN & 2) 437188Sbill p->p_siga1 = ~0; 438188Sbill else 439206Sbill p->p_siga1 = 0; 440188Sbill (void) spl0(); 44136Sbill rate.v_pgin -= p->p_aveflt; 44236Sbill p->p_aveflt = 0; 44336Sbill for(i=0; i<NSIG; i++) 444173Sbill u.u_signal[i] = SIG_IGN; 44536Sbill /* 44636Sbill * Release virtual memory. If we resulted from 44736Sbill * a vfork(), instead give the resources back to 44836Sbill * the parent. 44936Sbill */ 45036Sbill if ((p->p_flag & SVFORK) == 0) 45136Sbill vrelvm(); 45236Sbill else { 45336Sbill p->p_flag &= ~SVFORK; 45436Sbill wakeup((caddr_t)p); 45536Sbill while ((p->p_flag & SVFDONE) == 0) 45636Sbill sleep((caddr_t)p, PZERO - 1); 45736Sbill p->p_flag &= ~SVFDONE; 45836Sbill } 45936Sbill for(i=0; i<NOFILE; i++) { 46036Sbill f = u.u_ofile[i]; 46136Sbill u.u_ofile[i] = NULL; 46236Sbill closef(f); 46336Sbill } 46436Sbill plock(u.u_cdir); 46536Sbill iput(u.u_cdir); 46636Sbill if (u.u_rdir) { 46736Sbill plock(u.u_rdir); 46836Sbill iput(u.u_rdir); 46936Sbill } 47036Sbill acct(); 47136Sbill vrelpt(u.u_procp); 47236Sbill vrelu(u.u_procp, 0); 47336Sbill multprog--; 47436Sbill spl7(); /* clock will get mad because of overlaying */ 47536Sbill p->p_stat = SZOMB; 47636Sbill i = PIDHASH(p->p_pid); 47736Sbill x = p - proc; 47836Sbill if (pidhash[i] == x) 47936Sbill pidhash[i] = p->p_idhash; 48036Sbill else { 48136Sbill for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 48236Sbill if (proc[i].p_idhash == x) { 48336Sbill proc[i].p_idhash = p->p_idhash; 48436Sbill goto done; 48536Sbill } 48636Sbill panic("exit"); 48736Sbill } 48836Sbill done: 48936Sbill ((struct xproc *)p)->xp_xstat = rv; /* overlay */ 49036Sbill ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ 49136Sbill vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); 49236Sbill for(q = &proc[0]; q < &proc[NPROC]; q++) 493188Sbill if(q->p_pptr == p) { 494188Sbill q->p_pptr = &proc[1]; 495188Sbill q->p_ppid = 1; 49636Sbill wakeup((caddr_t)&proc[1]); 497188Sbill /* 498212Sbill * Traced processes are killed 499188Sbill * since their existence means someone is screwing up. 500212Sbill * Traced processes are sent a hangup and a continue. 501212Sbill * This is designed to be ``safe'' for setuid 502212Sbill * processes since they must be willing to tolerate 503212Sbill * hangups anyways. 504188Sbill */ 505212Sbill if (q->p_flag&STRC) { 506188Sbill q->p_flag &= ~STRC; 507188Sbill psignal(q, SIGKILL); 508212Sbill } else if (q->p_stat == SSTOP) { 509212Sbill psignal(q, SIGHUP); 510212Sbill psignal(q, SIGCONT); 511188Sbill } 512*215Sbill /* 513*215Sbill * Protect this process from future 514*215Sbill * tty signals, and clear TSTP/TTIN/TTOU if pending. 515*215Sbill */ 516*215Sbill q->p_pgrp = 0; 517*215Sbill #define bit(a) (1<<(a-1)) 518*215Sbill q->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); 51936Sbill } 520188Sbill wakeup((caddr_t)p->p_pptr); 521188Sbill psignal(p->p_pptr, SIGCHLD); 52236Sbill swtch(); 52336Sbill } 52436Sbill 52536Sbill wait() 52636Sbill { 527188Sbill struct vtimes vm; 528188Sbill struct vtimes *vp; 52936Sbill 530188Sbill if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 531188Sbill wait1(0, (struct vtimes *)0); 532188Sbill return; 533188Sbill } 534188Sbill vp = (struct vtimes *)u.u_ar0[R1]; 535188Sbill wait1(u.u_ar0[R0], &vm); 536188Sbill if (u.u_error) 537188Sbill return; 538188Sbill (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); 53936Sbill } 54036Sbill 54136Sbill /* 54236Sbill * Wait system call. 54336Sbill * Search for a terminated (zombie) child, 54436Sbill * finally lay it to rest, and collect its status. 54536Sbill * Look also for stopped (traced) children, 54636Sbill * and pass back status from them. 54736Sbill */ 548188Sbill wait1(options, vp) 549188Sbill register options; 55036Sbill struct vtimes *vp; 55136Sbill { 55236Sbill register f; 55336Sbill register struct proc *p; 55436Sbill 55536Sbill f = 0; 55636Sbill loop: 55736Sbill for(p = &proc[0]; p < &proc[NPROC]; p++) 558188Sbill if(p->p_pptr == u.u_procp) { 55936Sbill f++; 56036Sbill if(p->p_stat == SZOMB) { 56136Sbill u.u_r.r_val1 = p->p_pid; 56236Sbill u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; 56336Sbill ((struct xproc *)p)->xp_xstat = 0; 56436Sbill if (vp) 56536Sbill *vp = ((struct xproc *)p)->xp_vm; 56636Sbill vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); 56736Sbill ((struct xproc *)p)->xp_vm = zvms; 56836Sbill p->p_stat = NULL; 56936Sbill p->p_pid = 0; 57036Sbill p->p_ppid = 0; 571188Sbill p->p_pptr = 0; 57236Sbill p->p_sig = 0; 573188Sbill p->p_siga0 = 0; 574188Sbill p->p_siga1 = 0; 57536Sbill p->p_pgrp = 0; 57636Sbill p->p_flag = 0; 57736Sbill p->p_wchan = 0; 578188Sbill p->p_cursig = 0; 57936Sbill return; 58036Sbill } 581188Sbill if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 582188Sbill (p->p_flag&STRC || options&WUNTRACED)) { 583188Sbill p->p_flag |= SWTED; 584188Sbill u.u_r.r_val1 = p->p_pid; 585188Sbill u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 586188Sbill return; 58736Sbill } 58836Sbill } 589188Sbill if (f==0) { 590188Sbill u.u_error = ECHILD; 591188Sbill return; 59236Sbill } 593188Sbill if (options&WNOHANG) { 594188Sbill u.u_r.r_val1 = 0; 595188Sbill return; 596188Sbill } 597206Sbill /* 598188Sbill if (setjmp(u.u_qsav)) { 599188Sbill u.u_eosys = RESTARTSYS; 600188Sbill return; 601188Sbill } 602206Sbill */ 603188Sbill sleep((caddr_t)u.u_procp, PWAIT); 604188Sbill goto loop; 60536Sbill } 60636Sbill 60736Sbill /* 60836Sbill * fork system call. 60936Sbill */ 61036Sbill fork() 61136Sbill { 61236Sbill 61336Sbill u.u_cdmap = zdmap; 61436Sbill u.u_csmap = zdmap; 61536Sbill if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { 61636Sbill u.u_r.r_val2 = 0; 61736Sbill return; 61836Sbill } 61936Sbill fork1(0); 62036Sbill } 62136Sbill 62236Sbill fork1(isvfork) 62336Sbill { 62436Sbill register struct proc *p1, *p2; 62536Sbill register a; 62636Sbill 62736Sbill a = 0; 62836Sbill p2 = NULL; 62936Sbill for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) { 63036Sbill if (p1->p_stat==NULL && p2==NULL) 63136Sbill p2 = p1; 63236Sbill else { 63336Sbill if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) 63436Sbill a++; 63536Sbill } 63636Sbill } 63736Sbill /* 63836Sbill * Disallow if 63936Sbill * No processes at all; 64036Sbill * not su and too many procs owned; or 64136Sbill * not su and would take last slot. 64236Sbill */ 64336Sbill if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) { 64436Sbill u.u_error = EAGAIN; 64536Sbill if (!isvfork) { 646132Sbill (void) vsexpand(0, &u.u_cdmap, 1); 647132Sbill (void) vsexpand(0, &u.u_csmap, 1); 64836Sbill } 64936Sbill goto out; 65036Sbill } 65136Sbill p1 = u.u_procp; 65236Sbill if(newproc(isvfork)) { 65336Sbill u.u_r.r_val1 = p1->p_pid; 65436Sbill u.u_r.r_val2 = 1; /* child */ 65536Sbill u.u_start = time; 65636Sbill u.u_acflag = AFORK; 65736Sbill return; 65836Sbill } 65936Sbill u.u_r.r_val1 = p2->p_pid; 66036Sbill 66136Sbill out: 66236Sbill u.u_r.r_val2 = 0; 66336Sbill } 66436Sbill 66536Sbill /* 66636Sbill * break system call. 66736Sbill * -- bad planning: "break" is a dirty word in C. 66836Sbill */ 66936Sbill sbreak() 67036Sbill { 67136Sbill struct a { 67236Sbill char *nsiz; 67336Sbill }; 67436Sbill register int n, d; 67536Sbill 67636Sbill /* 67736Sbill * set n to new data size 67836Sbill * set d to new-old 67936Sbill */ 68036Sbill 68136Sbill n = btoc(((struct a *)u.u_ap)->nsiz); 68236Sbill if (!u.u_sep) 68336Sbill n -= ctos(u.u_tsize) * stoc(1); 68436Sbill if (n < 0) 68536Sbill n = 0; 68636Sbill d = clrnd(n - u.u_dsize); 68736Sbill if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) 68836Sbill return; 68936Sbill if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0) 69036Sbill return; 69136Sbill expand(d, P0BR); 69236Sbill } 693