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