| 1 | /* kern_proc.c 4.8 %G% */ |
| 2 | |
| 3 | #include "../h/param.h" |
| 4 | #include "../h/systm.h" |
| 5 | #include "../h/map.h" |
| 6 | #include "../h/mtpr.h" |
| 7 | #include "../h/dir.h" |
| 8 | #include "../h/user.h" |
| 9 | #include "../h/proc.h" |
| 10 | #include "../h/buf.h" |
| 11 | #include "../h/reg.h" |
| 12 | #include "../h/inode.h" |
| 13 | #include "../h/seg.h" |
| 14 | #include "../h/acct.h" |
| 15 | #include "/usr/include/wait.h" |
| 16 | #include "../h/pte.h" |
| 17 | #include "../h/vm.h" |
| 18 | #include "../h/text.h" |
| 19 | #include "../h/psl.h" |
| 20 | #include "../h/vlimit.h" |
| 21 | #include "../h/file.h" |
| 22 | |
| 23 | /* |
| 24 | * exec system call, with and without environments. |
| 25 | */ |
| 26 | struct execa { |
| 27 | char *fname; |
| 28 | char **argp; |
| 29 | char **envp; |
| 30 | }; |
| 31 | |
| 32 | exec() |
| 33 | { |
| 34 | ((struct execa *)u.u_ap)->envp = NULL; |
| 35 | exece(); |
| 36 | } |
| 37 | |
| 38 | exece() |
| 39 | { |
| 40 | register nc; |
| 41 | register char *cp; |
| 42 | register struct buf *bp; |
| 43 | register struct execa *uap; |
| 44 | int na, ne, ucp, ap, c; |
| 45 | int indir, uid, gid; |
| 46 | char *sharg; |
| 47 | struct inode *ip; |
| 48 | swblk_t bno; |
| 49 | char cfname[DIRSIZ]; |
| 50 | char cfarg[SHSIZE]; |
| 51 | |
| 52 | if ((ip = namei(uchar, 0)) == NULL) |
| 53 | return; |
| 54 | |
| 55 | bno = 0; |
| 56 | bp = 0; |
| 57 | indir = 0; |
| 58 | uid = u.u_uid; |
| 59 | gid = u.u_gid; |
| 60 | |
| 61 | if (ip->i_mode & ISUID) |
| 62 | uid = ip->i_uid; |
| 63 | if (ip->i_mode & ISGID) |
| 64 | gid = ip->i_gid; |
| 65 | |
| 66 | again: |
| 67 | if(access(ip, IEXEC)) |
| 68 | goto bad; |
| 69 | if((u.u_procp->p_flag&STRC) && access(ip, IREAD)) |
| 70 | goto bad; |
| 71 | if((ip->i_mode & IFMT) != IFREG || |
| 72 | (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { |
| 73 | u.u_error = EACCES; |
| 74 | goto bad; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Read in first few bytes of file for segment sizes, ux_mag: |
| 79 | * 407 = plain executable |
| 80 | * 410 = RO text |
| 81 | * 413 = demand paged RO text |
| 82 | * Also an ASCII line beginning with #! is |
| 83 | * the file name of a ``shell'' and arguments may be prepended |
| 84 | * to the argument list if given here. |
| 85 | * |
| 86 | * SHELL NAMES ARE LIMITED IN LENGTH. |
| 87 | * |
| 88 | * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM |
| 89 | * THE ASCII LINE. |
| 90 | */ |
| 91 | u.u_base = (caddr_t)&u.u_exdata; |
| 92 | u.u_count = sizeof(u.u_exdata); |
| 93 | u.u_offset = 0; |
| 94 | u.u_segflg = 1; |
| 95 | readi(ip); |
| 96 | u.u_segflg = 0; |
| 97 | if(u.u_error) |
| 98 | goto bad; |
| 99 | if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) |
| 100 | && u.u_exdata.ux_shell[0] != '#') { |
| 101 | u.u_error = ENOEXEC; |
| 102 | goto bad; |
| 103 | } |
| 104 | switch (u.u_exdata.ux_mag) { |
| 105 | |
| 106 | case 0407: |
| 107 | u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; |
| 108 | u.u_exdata.ux_tsize = 0; |
| 109 | break; |
| 110 | |
| 111 | case 0413: |
| 112 | case 0410: |
| 113 | if (u.u_exdata.ux_tsize == 0) { |
| 114 | u.u_error = ENOEXEC; |
| 115 | goto bad; |
| 116 | } |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | if (u.u_exdata.ux_shell[0] != '#' || |
| 121 | u.u_exdata.ux_shell[1] != '!' || |
| 122 | indir) { |
| 123 | u.u_error = ENOEXEC; |
| 124 | goto bad; |
| 125 | } |
| 126 | cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */ |
| 127 | while (cp < &u.u_exdata.ux_shell[SHSIZE]) { |
| 128 | if (*cp == '\t') |
| 129 | *cp = ' '; |
| 130 | else if (*cp == '\n') { |
| 131 | *cp = '\0'; |
| 132 | break; |
| 133 | } |
| 134 | cp++; |
| 135 | } |
| 136 | if (*cp != '\0') { |
| 137 | u.u_error = ENOEXEC; |
| 138 | goto bad; |
| 139 | } |
| 140 | cp = &u.u_exdata.ux_shell[2]; |
| 141 | while (*cp == ' ') |
| 142 | cp++; |
| 143 | u.u_dirp = cp; |
| 144 | while (*cp && *cp != ' ') |
| 145 | cp++; |
| 146 | sharg = NULL; |
| 147 | if (*cp) { |
| 148 | *cp++ = '\0'; |
| 149 | while (*cp == ' ') |
| 150 | cp++; |
| 151 | if (*cp) { |
| 152 | bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE); |
| 153 | sharg = cfarg; |
| 154 | } |
| 155 | } |
| 156 | bcopy((caddr_t)u.u_dbuf, (caddr_t)cfname, DIRSIZ); |
| 157 | indir = 1; |
| 158 | iput(ip); |
| 159 | ip = namei(schar, 0); |
| 160 | if (ip == NULL) |
| 161 | return; |
| 162 | goto again; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Collect arguments on "file" in swap space. |
| 167 | */ |
| 168 | na = 0; |
| 169 | ne = 0; |
| 170 | nc = 0; |
| 171 | uap = (struct execa *)u.u_ap; |
| 172 | if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { |
| 173 | swkill(u.u_procp, "exece"); |
| 174 | goto bad; |
| 175 | } |
| 176 | if (bno % CLSIZE) |
| 177 | panic("execa rmalloc"); |
| 178 | if (uap->argp) for (;;) { |
| 179 | ap = NULL; |
| 180 | if (na == 1 && indir) { |
| 181 | if (sharg == NULL) |
| 182 | ap = (int)uap->fname; |
| 183 | } else if (na == 2 && indir && sharg != NULL) |
| 184 | ap = (int)uap->fname; |
| 185 | else if (uap->argp) { |
| 186 | ap = fuword((caddr_t)uap->argp); |
| 187 | uap->argp++; |
| 188 | } |
| 189 | if (ap==NULL && uap->envp) { |
| 190 | uap->argp = NULL; |
| 191 | if ((ap = fuword((caddr_t)uap->envp)) == NULL) |
| 192 | break; |
| 193 | uap->envp++; |
| 194 | ne++; |
| 195 | } |
| 196 | if (ap==NULL) |
| 197 | break; |
| 198 | na++; |
| 199 | if(ap == -1) |
| 200 | u.u_error = EFAULT; |
| 201 | do { |
| 202 | if (nc >= NCARGS-1) |
| 203 | u.u_error = E2BIG; |
| 204 | if (indir && na == 2 && sharg != NULL) |
| 205 | c = *sharg++ & 0377; |
| 206 | else if ((c = fubyte((caddr_t)ap++)) < 0) |
| 207 | u.u_error = EFAULT; |
| 208 | if (u.u_error) { |
| 209 | if (bp) |
| 210 | brelse(bp); |
| 211 | bp = 0; |
| 212 | goto badarg; |
| 213 | } |
| 214 | if ((nc&BMASK) == 0) { |
| 215 | if (bp) |
| 216 | bdwrite(bp); |
| 217 | bp = getblk(argdev, |
| 218 | (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT))); |
| 219 | cp = bp->b_un.b_addr; |
| 220 | } |
| 221 | nc++; |
| 222 | *cp++ = c; |
| 223 | } while (c>0); |
| 224 | } |
| 225 | if (bp) |
| 226 | bdwrite(bp); |
| 227 | bp = 0; |
| 228 | nc = (nc + NBPW-1) & ~(NBPW-1); |
| 229 | if (indir) |
| 230 | bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ); |
| 231 | getxfile(ip, nc + (na+4)*NBPW, uid, gid); |
| 232 | if (u.u_error) { |
| 233 | badarg: |
| 234 | for (c = 0; c < nc; c += BSIZE) |
| 235 | if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) { |
| 236 | bp->b_flags |= B_AGE; /* throw away */ |
| 237 | bp->b_flags &= ~B_DELWRI; /* cancel io */ |
| 238 | brelse(bp); |
| 239 | bp = 0; |
| 240 | } |
| 241 | goto bad; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * copy back arglist |
| 246 | */ |
| 247 | |
| 248 | ucp = USRSTACK - nc - NBPW; |
| 249 | ap = ucp - na*NBPW - 3*NBPW; |
| 250 | u.u_ar0[SP] = ap; |
| 251 | (void) suword((caddr_t)ap, na-ne); |
| 252 | nc = 0; |
| 253 | for (;;) { |
| 254 | ap += NBPW; |
| 255 | if (na==ne) { |
| 256 | (void) suword((caddr_t)ap, 0); |
| 257 | ap += NBPW; |
| 258 | } |
| 259 | if (--na < 0) |
| 260 | break; |
| 261 | (void) suword((caddr_t)ap, ucp); |
| 262 | do { |
| 263 | if ((nc&BMASK) == 0) { |
| 264 | if (bp) |
| 265 | brelse(bp); |
| 266 | bp = bread(argdev, |
| 267 | (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT))); |
| 268 | bp->b_flags |= B_AGE; /* throw away */ |
| 269 | bp->b_flags &= ~B_DELWRI; /* cancel io */ |
| 270 | cp = bp->b_un.b_addr; |
| 271 | } |
| 272 | (void) subyte((caddr_t)ucp++, (c = *cp++)); |
| 273 | nc++; |
| 274 | } while(c&0377); |
| 275 | } |
| 276 | (void) suword((caddr_t)ap, 0); |
| 277 | (void) suword((caddr_t)ucp, 0); |
| 278 | setregs(); |
| 279 | bad: |
| 280 | if (bp) |
| 281 | brelse(bp); |
| 282 | if (bno) |
| 283 | rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); |
| 284 | iput(ip); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Read in and set up memory for executed file. |
| 289 | */ |
| 290 | getxfile(ip, nargc, uid, gid) |
| 291 | register struct inode *ip; |
| 292 | { |
| 293 | register size_t ts, ds, ss; |
| 294 | int pagi; |
| 295 | |
| 296 | if (u.u_exdata.ux_mag == 0413) |
| 297 | pagi = SPAGI; |
| 298 | else |
| 299 | pagi = 0; |
| 300 | |
| 301 | if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) { |
| 302 | register struct file *fp; |
| 303 | |
| 304 | for (fp = file; fp < fileNFILE; fp++) |
| 305 | if (fp->f_inode == ip && (fp->f_flag&FWRITE)) { |
| 306 | u.u_error = ETXTBSY; |
| 307 | goto bad; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * find text and data sizes |
| 313 | * try them out for possible |
| 314 | * exceed of max sizes |
| 315 | */ |
| 316 | |
| 317 | ts = clrnd(btoc(u.u_exdata.ux_tsize)); |
| 318 | ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); |
| 319 | ss = clrnd(SSIZE + btoc(nargc)); |
| 320 | if (chksize(ts, ds, ss)) |
| 321 | goto bad; |
| 322 | u.u_cdmap = zdmap; |
| 323 | u.u_csmap = zdmap; |
| 324 | if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) |
| 325 | goto bad; |
| 326 | |
| 327 | /* |
| 328 | * At this point, committed to the new image! |
| 329 | * Release virtual memory resources of old process, and |
| 330 | * initialize the virtual memory of the new process. |
| 331 | * If we resulted from vfork(), instead wakeup our |
| 332 | * parent who will set SVFDONE when he has taken back |
| 333 | * our resources. |
| 334 | */ |
| 335 | u.u_prof.pr_scale = 0; |
| 336 | if ((u.u_procp->p_flag & SVFORK) == 0) |
| 337 | vrelvm(); |
| 338 | else { |
| 339 | u.u_procp->p_flag &= ~SVFORK; |
| 340 | u.u_procp->p_flag |= SKEEP; |
| 341 | wakeup((caddr_t)u.u_procp); |
| 342 | while ((u.u_procp->p_flag & SVFDONE) == 0) |
| 343 | sleep((caddr_t)u.u_procp, PZERO - 1); |
| 344 | u.u_procp->p_flag &= ~(SVFDONE|SKEEP); |
| 345 | } |
| 346 | u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM|SNUSIG); |
| 347 | u.u_procp->p_flag |= pagi; |
| 348 | u.u_dmap = u.u_cdmap; |
| 349 | u.u_smap = u.u_csmap; |
| 350 | vgetvm(ts, ds, ss); |
| 351 | |
| 352 | if (pagi == 0) { |
| 353 | /* |
| 354 | * Read in data segment. |
| 355 | */ |
| 356 | u.u_base = (char *)ctob(ts); |
| 357 | u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize; |
| 358 | u.u_count = u.u_exdata.ux_dsize; |
| 359 | readi(ip); |
| 360 | } |
| 361 | xalloc(ip, pagi); |
| 362 | if (pagi && u.u_procp->p_textp) |
| 363 | vinifod((struct fpte *)dptopte(u.u_procp, 0), |
| 364 | PG_FTEXT, u.u_procp->p_textp->x_iptr, |
| 365 | 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); |
| 366 | |
| 367 | /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ |
| 368 | mtpr(TBIA, 0); |
| 369 | |
| 370 | /* |
| 371 | * set SUID/SGID protections, if no tracing |
| 372 | */ |
| 373 | if ((u.u_procp->p_flag&STRC)==0) { |
| 374 | #ifndef MELB |
| 375 | if(u.u_uid != 0) |
| 376 | #endif |
| 377 | { |
| 378 | u.u_uid = uid; |
| 379 | u.u_procp->p_uid = uid; |
| 380 | } |
| 381 | u.u_gid = gid; |
| 382 | } else |
| 383 | psignal(u.u_procp, SIGTRAP); |
| 384 | u.u_tsize = ts; |
| 385 | u.u_dsize = ds; |
| 386 | u.u_ssize = ss; |
| 387 | bad: |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Clear registers on exec |
| 393 | */ |
| 394 | setregs() |
| 395 | { |
| 396 | register int (**rp)(); |
| 397 | register i; |
| 398 | #ifdef UCBIPC |
| 399 | register struct port *pt; |
| 400 | #endif UCBIPC |
| 401 | long sigmask; |
| 402 | |
| 403 | for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG]; |
| 404 | sigmask <<= 1, rp++) { |
| 405 | switch (*rp) { |
| 406 | |
| 407 | case SIG_IGN: |
| 408 | case SIG_DFL: |
| 409 | case SIG_HOLD: |
| 410 | continue; |
| 411 | |
| 412 | default: |
| 413 | /* |
| 414 | * Normal or deferring catch; revert to default. |
| 415 | */ |
| 416 | (void) spl6(); |
| 417 | *rp = SIG_DFL; |
| 418 | if ((int)*rp & 1) |
| 419 | u.u_procp->p_siga0 |= sigmask; |
| 420 | else |
| 421 | u.u_procp->p_siga1 &= ~sigmask; |
| 422 | if ((int)*rp & 2) |
| 423 | u.u_procp->p_siga1 |= sigmask; |
| 424 | else |
| 425 | u.u_procp->p_siga1 &= ~sigmask; |
| 426 | (void) spl0(); |
| 427 | continue; |
| 428 | } |
| 429 | } |
| 430 | /* |
| 431 | for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) |
| 432 | *rp++ = 0; |
| 433 | */ |
| 434 | u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ |
| 435 | for(i=0; i<NOFILE; i++) { |
| 436 | if (u.u_pofile[i]&EXCLOSE) { |
| 437 | #ifndef UCBIPC |
| 438 | closef(u.u_ofile[i]); |
| 439 | u.u_ofile[i] = NULL; |
| 440 | #else UCBIPC |
| 441 | if (u.u_pofile[i]&ISPORT) { |
| 442 | pt = u.u_oport[i]; |
| 443 | if (--pt->pt_count == 0) |
| 444 | ptclose(pt); |
| 445 | u.u_pofile[i] &= ~ISPORT; |
| 446 | u.u_oport[i] = NULL; |
| 447 | } else { |
| 448 | closef(u.u_ofile[i]); |
| 449 | u.u_ofile[i] = NULL; |
| 450 | } |
| 451 | #endif UCBIPC |
| 452 | u.u_pofile[i] &= ~EXCLOSE; |
| 453 | } |
| 454 | } |
| 455 | /* |
| 456 | * Remember file name for accounting. |
| 457 | */ |
| 458 | u.u_acflag &= ~AFORK; |
| 459 | bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * exit system call: |
| 464 | * pass back caller's arg |
| 465 | */ |
| 466 | rexit() |
| 467 | { |
| 468 | register struct a { |
| 469 | int rval; |
| 470 | } *uap; |
| 471 | |
| 472 | uap = (struct a *)u.u_ap; |
| 473 | exit((uap->rval & 0377) << 8); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Release resources. |
| 478 | * Save u. area for parent to look at. |
| 479 | * Enter zombie state. |
| 480 | * Wake up parent and init processes, |
| 481 | * and dispose of children. |
| 482 | */ |
| 483 | exit(rv) |
| 484 | { |
| 485 | register int i; |
| 486 | register struct proc *p, *q; |
| 487 | register struct file *f; |
| 488 | #ifdef UCBIPC |
| 489 | register struct port *pt; |
| 490 | #endif UCBIPC |
| 491 | register int x; |
| 492 | |
| 493 | #ifdef PGINPROF |
| 494 | vmsizmon(); |
| 495 | #endif |
| 496 | p = u.u_procp; |
| 497 | p->p_flag &= ~(STRC|SULOCK); |
| 498 | p->p_flag |= SWEXIT; |
| 499 | p->p_clktim = 0; |
| 500 | (void) spl6(); |
| 501 | if ((int)SIG_IGN & 1) |
| 502 | p->p_siga0 = ~0; |
| 503 | else |
| 504 | p->p_siga0 = 0; |
| 505 | if ((int)SIG_IGN & 2) |
| 506 | p->p_siga1 = ~0; |
| 507 | else |
| 508 | p->p_siga1 = 0; |
| 509 | (void) spl0(); |
| 510 | p->p_cpticks = 0; |
| 511 | p->p_pctcpu = 0; |
| 512 | for(i=0; i<NSIG; i++) |
| 513 | u.u_signal[i] = SIG_IGN; |
| 514 | /* |
| 515 | * Release virtual memory. If we resulted from |
| 516 | * a vfork(), instead give the resources back to |
| 517 | * the parent. |
| 518 | */ |
| 519 | if ((p->p_flag & SVFORK) == 0) |
| 520 | vrelvm(); |
| 521 | else { |
| 522 | p->p_flag &= ~SVFORK; |
| 523 | wakeup((caddr_t)p); |
| 524 | while ((p->p_flag & SVFDONE) == 0) |
| 525 | sleep((caddr_t)p, PZERO - 1); |
| 526 | p->p_flag &= ~SVFDONE; |
| 527 | } |
| 528 | for(i=0; i<NOFILE; i++) { |
| 529 | #ifndef UCBIPC |
| 530 | f = u.u_ofile[i]; |
| 531 | u.u_ofile[i] = NULL; |
| 532 | closef(f); |
| 533 | #else UCBIPC |
| 534 | if (u.u_pofile[i]&ISPORT) { |
| 535 | pt = u.u_oport[i]; |
| 536 | if (--pt->pt_count == 0) |
| 537 | ptclose(pt); |
| 538 | u.u_oport[i] = NULL; |
| 539 | } else { |
| 540 | f = u.u_ofile[i]; |
| 541 | u.u_ofile[i] = NULL; |
| 542 | closef(f); |
| 543 | } |
| 544 | #endif UCBIPC |
| 545 | } |
| 546 | plock(u.u_cdir); |
| 547 | iput(u.u_cdir); |
| 548 | if (u.u_rdir) { |
| 549 | plock(u.u_rdir); |
| 550 | iput(u.u_rdir); |
| 551 | } |
| 552 | u.u_limit[LIM_FSIZE] = INFINITY; |
| 553 | acct(); |
| 554 | vrelpt(u.u_procp); |
| 555 | vrelu(u.u_procp, 0); |
| 556 | multprog--; |
| 557 | /* spl7(); /* clock will get mad because of overlaying */ |
| 558 | p->p_stat = SZOMB; |
| 559 | noproc = 1; |
| 560 | i = PIDHASH(p->p_pid); |
| 561 | x = p - proc; |
| 562 | if (pidhash[i] == x) |
| 563 | pidhash[i] = p->p_idhash; |
| 564 | else { |
| 565 | for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) |
| 566 | if (proc[i].p_idhash == x) { |
| 567 | proc[i].p_idhash = p->p_idhash; |
| 568 | goto done; |
| 569 | } |
| 570 | panic("exit"); |
| 571 | } |
| 572 | if (p->p_pid == 1) |
| 573 | panic("init died"); |
| 574 | done: |
| 575 | ((struct xproc *)p)->xp_xstat = rv; /* overlay */ |
| 576 | ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ |
| 577 | vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); |
| 578 | for(q = proc; q < procNPROC; q++) |
| 579 | if(q->p_pptr == p) { |
| 580 | q->p_pptr = &proc[1]; |
| 581 | q->p_ppid = 1; |
| 582 | wakeup((caddr_t)&proc[1]); |
| 583 | /* |
| 584 | * Traced processes are killed |
| 585 | * since their existence means someone is screwing up. |
| 586 | * Stopped processes are sent a hangup and a continue. |
| 587 | * This is designed to be ``safe'' for setuid |
| 588 | * processes since they must be willing to tolerate |
| 589 | * hangups anyways. |
| 590 | */ |
| 591 | if (q->p_flag&STRC) { |
| 592 | q->p_flag &= ~STRC; |
| 593 | psignal(q, SIGKILL); |
| 594 | } else if (q->p_stat == SSTOP) { |
| 595 | psignal(q, SIGHUP); |
| 596 | psignal(q, SIGCONT); |
| 597 | } |
| 598 | /* |
| 599 | * Protect this process from future |
| 600 | * tty signals, clear TSTP/TTIN/TTOU if pending, |
| 601 | * and set SDETACH bit on procs. |
| 602 | */ |
| 603 | (void) spgrp(q, -1); |
| 604 | } |
| 605 | wakeup((caddr_t)p->p_pptr); |
| 606 | psignal(p->p_pptr, SIGCHLD); |
| 607 | swtch(); |
| 608 | } |
| 609 | |
| 610 | wait() |
| 611 | { |
| 612 | struct vtimes vm; |
| 613 | struct vtimes *vp; |
| 614 | |
| 615 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { |
| 616 | wait1(0, (struct vtimes *)0); |
| 617 | return; |
| 618 | } |
| 619 | vp = (struct vtimes *)u.u_ar0[R1]; |
| 620 | wait1(u.u_ar0[R0], &vm); |
| 621 | if (u.u_error) |
| 622 | return; |
| 623 | (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Wait system call. |
| 628 | * Search for a terminated (zombie) child, |
| 629 | * finally lay it to rest, and collect its status. |
| 630 | * Look also for stopped (traced) children, |
| 631 | * and pass back status from them. |
| 632 | */ |
| 633 | wait1(options, vp) |
| 634 | register options; |
| 635 | struct vtimes *vp; |
| 636 | { |
| 637 | register f; |
| 638 | register struct proc *p; |
| 639 | |
| 640 | f = 0; |
| 641 | loop: |
| 642 | for(p = proc; p < procNPROC; p++) |
| 643 | if(p->p_pptr == u.u_procp) { |
| 644 | f++; |
| 645 | if(p->p_stat == SZOMB) { |
| 646 | u.u_r.r_val1 = p->p_pid; |
| 647 | u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; |
| 648 | ((struct xproc *)p)->xp_xstat = 0; |
| 649 | if (vp) |
| 650 | *vp = ((struct xproc *)p)->xp_vm; |
| 651 | vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); |
| 652 | ((struct xproc *)p)->xp_vm = zvms; |
| 653 | p->p_stat = NULL; |
| 654 | p->p_pid = 0; |
| 655 | p->p_ppid = 0; |
| 656 | p->p_pptr = 0; |
| 657 | p->p_sig = 0; |
| 658 | p->p_siga0 = 0; |
| 659 | p->p_siga1 = 0; |
| 660 | p->p_pgrp = 0; |
| 661 | p->p_flag = 0; |
| 662 | p->p_wchan = 0; |
| 663 | p->p_cursig = 0; |
| 664 | return; |
| 665 | } |
| 666 | if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && |
| 667 | (p->p_flag&STRC || options&WUNTRACED)) { |
| 668 | p->p_flag |= SWTED; |
| 669 | u.u_r.r_val1 = p->p_pid; |
| 670 | u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; |
| 671 | return; |
| 672 | } |
| 673 | } |
| 674 | if (f==0) { |
| 675 | u.u_error = ECHILD; |
| 676 | return; |
| 677 | } |
| 678 | if (options&WNOHANG) { |
| 679 | u.u_r.r_val1 = 0; |
| 680 | return; |
| 681 | } |
| 682 | if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { |
| 683 | u.u_eosys = RESTARTSYS; |
| 684 | return; |
| 685 | } |
| 686 | sleep((caddr_t)u.u_procp, PWAIT); |
| 687 | goto loop; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * fork system call. |
| 692 | */ |
| 693 | fork() |
| 694 | { |
| 695 | |
| 696 | u.u_cdmap = zdmap; |
| 697 | u.u_csmap = zdmap; |
| 698 | if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { |
| 699 | u.u_r.r_val2 = 0; |
| 700 | return; |
| 701 | } |
| 702 | fork1(0); |
| 703 | } |
| 704 | |
| 705 | fork1(isvfork) |
| 706 | { |
| 707 | register struct proc *p1, *p2; |
| 708 | register a; |
| 709 | |
| 710 | a = 0; |
| 711 | p2 = NULL; |
| 712 | for(p1 = proc; p1 < procNPROC; p1++) { |
| 713 | if (p1->p_stat==NULL && p2==NULL) |
| 714 | p2 = p1; |
| 715 | else { |
| 716 | if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) |
| 717 | a++; |
| 718 | } |
| 719 | } |
| 720 | /* |
| 721 | * Disallow if |
| 722 | * No processes at all; |
| 723 | * not su and too many procs owned; or |
| 724 | * not su and would take last slot. |
| 725 | */ |
| 726 | if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { |
| 727 | u.u_error = EAGAIN; |
| 728 | if (!isvfork) { |
| 729 | (void) vsexpand(0, &u.u_cdmap, 1); |
| 730 | (void) vsexpand(0, &u.u_csmap, 1); |
| 731 | } |
| 732 | goto out; |
| 733 | } |
| 734 | p1 = u.u_procp; |
| 735 | if(newproc(isvfork)) { |
| 736 | u.u_r.r_val1 = p1->p_pid; |
| 737 | u.u_r.r_val2 = 1; /* child */ |
| 738 | u.u_start = time; |
| 739 | u.u_acflag = AFORK; |
| 740 | return; |
| 741 | } |
| 742 | u.u_r.r_val1 = p2->p_pid; |
| 743 | |
| 744 | out: |
| 745 | u.u_r.r_val2 = 0; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * break system call. |
| 750 | * -- bad planning: "break" is a dirty word in C. |
| 751 | */ |
| 752 | sbreak() |
| 753 | { |
| 754 | struct a { |
| 755 | char *nsiz; |
| 756 | }; |
| 757 | register int n, d; |
| 758 | |
| 759 | /* |
| 760 | * set n to new data size |
| 761 | * set d to new-old |
| 762 | */ |
| 763 | |
| 764 | n = btoc(((struct a *)u.u_ap)->nsiz); |
| 765 | if (!u.u_sep) |
| 766 | n -= ctos(u.u_tsize) * stoc(1); |
| 767 | if (n < 0) |
| 768 | n = 0; |
| 769 | d = clrnd(n - u.u_dsize); |
| 770 | if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) { |
| 771 | u.u_error = ENOMEM; |
| 772 | return; |
| 773 | } |
| 774 | if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize)) |
| 775 | return; |
| 776 | if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0) |
| 777 | return; |
| 778 | expand(d, P0BR); |
| 779 | } |