1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $Id: jobs.c,v 1.3 1995/05/30 00:07:18 rgrimes Exp $ 37 */ 38 39 #ifndef lint 40 static char sccsid[] = "@(#)jobs.c 8.1 (Berkeley) 5/31/93"; 41 #endif /* not lint */ 42 43 #include "shell.h" 44 #if JOBS 45 #include "sgtty.h" 46 #undef CEOF /* syntax.h redefines this */ 47 #endif 48 #include "main.h" 49 #include "parser.h" 50 #include "nodes.h" 51 #include "jobs.h" 52 #include "options.h" 53 #include "trap.h" 54 #include "signames.h" 55 #include "syntax.h" 56 #include "input.h" 57 #include "output.h" 58 #include "memalloc.h" 59 #include "error.h" 60 #include "mystring.h" 61 #include <fcntl.h> 62 #include <signal.h> 63 #include <errno.h> 64 #ifdef BSD 65 #include <sys/types.h> 66 #include <sys/wait.h> 67 #include <sys/time.h> 68 #include <sys/resource.h> 69 #endif 70 71 72 73 struct job *jobtab; /* array of jobs */ 74 int njobs; /* size of array */ 75 MKINIT short backgndpid = -1; /* pid of last background process */ 76 #if JOBS 77 int initialpgrp; /* pgrp of shell on invocation */ 78 short curjob; /* current job */ 79 #endif 80 81 #ifdef __STDC__ 82 STATIC void restartjob(struct job *); 83 STATIC struct job *getjob(char *); 84 STATIC void freejob(struct job *); 85 STATIC int procrunning(int); 86 STATIC int dowait(int, struct job *); 87 STATIC int waitproc(int, int *); 88 #else 89 STATIC void restartjob(); 90 STATIC struct job *getjob(); 91 STATIC void freejob(); 92 STATIC int procrunning(); 93 STATIC int dowait(); 94 STATIC int waitproc(); 95 #endif 96 97 98 99 /* 100 * Turn job control on and off. 101 * 102 * Note: This code assumes that the third arg to ioctl is a character 103 * pointer, which is true on Berkeley systems but not System V. Since 104 * System V doesn't have job control yet, this isn't a problem now. 105 */ 106 107 MKINIT int jobctl; 108 109 void 110 setjobctl(on) { 111 #ifdef OLD_TTY_DRIVER 112 int ldisc; 113 #endif 114 115 if (on == jobctl || rootshell == 0) 116 return; 117 if (on) { 118 do { /* while we are in the background */ 119 if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { 120 out2str("sh: can't access tty; job control turned off\n"); 121 mflag = 0; 122 return; 123 } 124 if (initialpgrp == -1) 125 initialpgrp = getpgrp(0); 126 else if (initialpgrp != getpgrp(0)) { 127 killpg(initialpgrp, SIGTTIN); 128 continue; 129 } 130 } while (0); 131 #ifdef OLD_TTY_DRIVER 132 if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { 133 out2str("sh: need new tty driver to run job control; job control turned off\n"); 134 mflag = 0; 135 return; 136 } 137 #endif 138 setsignal(SIGTSTP); 139 setsignal(SIGTTOU); 140 setsignal(SIGTTIN); 141 setpgrp(0, rootpid); 142 ioctl(2, TIOCSPGRP, (char *)&rootpid); 143 } else { /* turning job control off */ 144 setpgrp(0, initialpgrp); 145 ioctl(2, TIOCSPGRP, (char *)&initialpgrp); 146 setsignal(SIGTSTP); 147 setsignal(SIGTTOU); 148 setsignal(SIGTTIN); 149 } 150 jobctl = on; 151 } 152 153 154 #ifdef mkinit 155 156 SHELLPROC { 157 backgndpid = -1; 158 #if JOBS 159 jobctl = 0; 160 #endif 161 } 162 163 #endif 164 165 166 167 #if JOBS 168 fgcmd(argc, argv) char **argv; { 169 struct job *jp; 170 int pgrp; 171 int status; 172 173 jp = getjob(argv[1]); 174 if (jp->jobctl == 0) 175 error("job not created under job control"); 176 pgrp = jp->ps[0].pid; 177 ioctl(2, TIOCSPGRP, (char *)&pgrp); 178 restartjob(jp); 179 INTOFF; 180 status = waitforjob(jp); 181 INTON; 182 return status; 183 } 184 185 186 bgcmd(argc, argv) char **argv; { 187 struct job *jp; 188 189 do { 190 jp = getjob(*++argv); 191 if (jp->jobctl == 0) 192 error("job not created under job control"); 193 restartjob(jp); 194 } while (--argc > 1); 195 return 0; 196 } 197 198 199 STATIC void 200 restartjob(jp) 201 struct job *jp; 202 { 203 struct procstat *ps; 204 int i; 205 206 if (jp->state == JOBDONE) 207 return; 208 INTOFF; 209 killpg(jp->ps[0].pid, SIGCONT); 210 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 211 if ((ps->status & 0377) == 0177) { 212 ps->status = -1; 213 jp->state = 0; 214 } 215 } 216 INTON; 217 } 218 #endif 219 220 221 int 222 jobscmd(argc, argv) char **argv; { 223 showjobs(0); 224 return 0; 225 } 226 227 228 /* 229 * Print a list of jobs. If "change" is nonzero, only print jobs whose 230 * statuses have changed since the last call to showjobs. 231 * 232 * If the shell is interrupted in the process of creating a job, the 233 * result may be a job structure containing zero processes. Such structures 234 * will be freed here. 235 */ 236 237 void 238 showjobs(change) { 239 int jobno; 240 int procno; 241 int i; 242 struct job *jp; 243 struct procstat *ps; 244 int col; 245 char s[64]; 246 247 TRACE(("showjobs(%d) called\n", change)); 248 while (dowait(0, (struct job *)NULL) > 0); 249 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 250 if (! jp->used) 251 continue; 252 if (jp->nprocs == 0) { 253 freejob(jp); 254 continue; 255 } 256 if (change && ! jp->changed) 257 continue; 258 procno = jp->nprocs; 259 for (ps = jp->ps ; ; ps++) { /* for each process */ 260 if (ps == jp->ps) 261 fmtstr(s, 64, "[%d] %d ", jobno, ps->pid); 262 else 263 fmtstr(s, 64, " %d ", ps->pid); 264 out1str(s); 265 col = strlen(s); 266 s[0] = '\0'; 267 if (ps->status == -1) { 268 /* don't print anything */ 269 } else if ((ps->status & 0xFF) == 0) { 270 fmtstr(s, 64, "Exit %d", ps->status >> 8); 271 } else { 272 i = ps->status; 273 #if JOBS 274 if ((i & 0xFF) == 0177) 275 i >>= 8; 276 #endif 277 if ((i & 0x7F) <= MAXSIG && sigmesg[i & 0x7F]) 278 scopy(sigmesg[i & 0x7F], s); 279 else 280 fmtstr(s, 64, "Signal %d", i & 0x7F); 281 if (i & 0x80) 282 strcat(s, " (core dumped)"); 283 } 284 out1str(s); 285 col += strlen(s); 286 do { 287 out1c(' '); 288 col++; 289 } while (col < 30); 290 out1str(ps->cmd); 291 out1c('\n'); 292 if (--procno <= 0) 293 break; 294 } 295 jp->changed = 0; 296 if (jp->state == JOBDONE) { 297 freejob(jp); 298 } 299 } 300 } 301 302 303 /* 304 * Mark a job structure as unused. 305 */ 306 307 STATIC void 308 freejob(jp) 309 struct job *jp; 310 { 311 struct procstat *ps; 312 int i; 313 314 INTOFF; 315 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 316 if (ps->cmd != nullstr) 317 ckfree(ps->cmd); 318 } 319 if (jp->ps != &jp->ps0) 320 ckfree(jp->ps); 321 jp->used = 0; 322 #if JOBS 323 if (curjob == jp - jobtab + 1) 324 curjob = 0; 325 #endif 326 INTON; 327 } 328 329 330 331 int 332 waitcmd(argc, argv) char **argv; { 333 struct job *job; 334 int status; 335 struct job *jp; 336 337 if (argc > 1) { 338 job = getjob(argv[1]); 339 } else { 340 job = NULL; 341 } 342 for (;;) { /* loop until process terminated or stopped */ 343 if (job != NULL) { 344 if (job->state) { 345 status = job->ps[job->nprocs - 1].status; 346 if ((status & 0xFF) == 0) 347 status = status >> 8 & 0xFF; 348 #if JOBS 349 else if ((status & 0xFF) == 0177) 350 status = (status >> 8 & 0x7F) + 128; 351 #endif 352 else 353 status = (status & 0x7F) + 128; 354 if (! iflag) 355 freejob(job); 356 return status; 357 } 358 } else { 359 for (jp = jobtab ; ; jp++) { 360 if (jp >= jobtab + njobs) { /* no running procs */ 361 return 0; 362 } 363 if (jp->used && jp->state == 0) 364 break; 365 } 366 } 367 dowait(1, (struct job *)NULL); 368 } 369 } 370 371 372 373 jobidcmd(argc, argv) char **argv; { 374 struct job *jp; 375 int i; 376 377 jp = getjob(argv[1]); 378 for (i = 0 ; i < jp->nprocs ; ) { 379 out1fmt("%d", jp->ps[i].pid); 380 out1c(++i < jp->nprocs? ' ' : '\n'); 381 } 382 return 0; 383 } 384 385 386 387 /* 388 * Convert a job name to a job structure. 389 */ 390 391 STATIC struct job * 392 getjob(name) 393 char *name; 394 { 395 int jobno; 396 register struct job *jp; 397 int pid; 398 int i; 399 400 if (name == NULL) { 401 #if JOBS 402 currentjob: 403 if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) 404 error("No current job"); 405 return &jobtab[jobno - 1]; 406 #else 407 error("No current job"); 408 #endif 409 } else if (name[0] == '%') { 410 if (is_digit(name[1])) { 411 jobno = number(name + 1); 412 if (jobno > 0 && jobno <= njobs 413 && jobtab[jobno - 1].used != 0) 414 return &jobtab[jobno - 1]; 415 #if JOBS 416 } else if (name[1] == '%' && name[2] == '\0') { 417 goto currentjob; 418 #endif 419 } else { 420 register struct job *found = NULL; 421 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 422 if (jp->used && jp->nprocs > 0 423 && prefix(name + 1, jp->ps[0].cmd)) { 424 if (found) 425 error("%s: ambiguous", name); 426 found = jp; 427 } 428 } 429 if (found) 430 return found; 431 } 432 } else if (is_number(name)) { 433 pid = number(name); 434 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 435 if (jp->used && jp->nprocs > 0 436 && jp->ps[jp->nprocs - 1].pid == pid) 437 return jp; 438 } 439 } 440 error("No such job: %s", name); 441 } 442 443 444 445 /* 446 * Return a new job structure, 447 */ 448 449 struct job * 450 makejob(node, nprocs) 451 union node *node; 452 { 453 int i; 454 struct job *jp; 455 456 for (i = njobs, jp = jobtab ; ; jp++) { 457 if (--i < 0) { 458 INTOFF; 459 if (njobs == 0) { 460 jobtab = ckmalloc(4 * sizeof jobtab[0]); 461 } else { 462 struct job *ojp; 463 464 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 465 for (i = njobs, ojp = jobtab; --i >= 0; 466 jp++, ojp++) 467 if (ojp->ps == &ojp->ps0) 468 ojp->ps = &jp->ps0; 469 jp -= njobs; 470 bcopy(jobtab, jp, njobs * sizeof jp[0]); 471 ckfree(jobtab); 472 jobtab = jp; 473 } 474 jp = jobtab + njobs; 475 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 476 INTON; 477 break; 478 } 479 if (jp->used == 0) 480 break; 481 } 482 INTOFF; 483 jp->state = 0; 484 jp->used = 1; 485 jp->changed = 0; 486 jp->nprocs = 0; 487 #if JOBS 488 jp->jobctl = jobctl; 489 #endif 490 if (nprocs > 1) { 491 jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 492 } else { 493 jp->ps = &jp->ps0; 494 } 495 INTON; 496 TRACE(("makejob(0x%x, %d) returns %%%d\n", (int)node, nprocs, jp - jobtab + 1)); 497 return jp; 498 } 499 500 501 /* 502 * Fork of a subshell. If we are doing job control, give the subshell its 503 * own process group. Jp is a job structure that the job is to be added to. 504 * N is the command that will be evaluated by the child. Both jp and n may 505 * be NULL. The mode parameter can be one of the following: 506 * FORK_FG - Fork off a foreground process. 507 * FORK_BG - Fork off a background process. 508 * FORK_NOJOB - Like FORK_FG, but don't give the process its own 509 * process group even if job control is on. 510 * 511 * When job control is turned off, background processes have their standard 512 * input redirected to /dev/null (except for the second and later processes 513 * in a pipeline). 514 */ 515 516 int 517 forkshell(jp, n, mode) 518 union node *n; 519 struct job *jp; 520 { 521 int pid; 522 int pgrp; 523 524 TRACE(("forkshell(%%%d, 0x%x, %d) called\n", jp - jobtab, (int)n, mode)); 525 INTOFF; 526 pid = fork(); 527 if (pid == -1) { 528 TRACE(("Fork failed, errno=%d\n", errno)); 529 INTON; 530 error("Cannot fork"); 531 } 532 if (pid == 0) { 533 struct job *p; 534 int wasroot; 535 int i; 536 537 TRACE(("Child shell %d\n", getpid())); 538 wasroot = rootshell; 539 rootshell = 0; 540 for (i = njobs, p = jobtab ; --i >= 0 ; p++) 541 if (p->used) 542 freejob(p); 543 closescript(); 544 INTON; 545 clear_traps(); 546 #if JOBS 547 jobctl = 0; /* do job control only in root shell */ 548 if (wasroot && mode != FORK_NOJOB && mflag) { 549 if (jp == NULL || jp->nprocs == 0) 550 pgrp = getpid(); 551 else 552 pgrp = jp->ps[0].pid; 553 setpgrp(0, pgrp); 554 if (mode == FORK_FG) { 555 /*** this causes superfluous TIOCSPGRPS ***/ 556 if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 557 error("TIOCSPGRP failed, errno=%d\n", errno); 558 } 559 setsignal(SIGTSTP); 560 setsignal(SIGTTOU); 561 } else if (mode == FORK_BG) { 562 ignoresig(SIGINT); 563 ignoresig(SIGQUIT); 564 if ((jp == NULL || jp->nprocs == 0) && 565 ! fd0_redirected_p ()) { 566 close(0); 567 if (open("/dev/null", O_RDONLY) != 0) 568 error("Can't open /dev/null"); 569 } 570 } 571 #else 572 if (mode == FORK_BG) { 573 ignoresig(SIGINT); 574 ignoresig(SIGQUIT); 575 if ((jp == NULL || jp->nprocs == 0) && 576 ! fd0_redirected_p ()) { 577 close(0); 578 if (open("/dev/null", O_RDONLY) != 0) 579 error("Can't open /dev/null"); 580 } 581 } 582 #endif 583 if (wasroot && iflag) { 584 setsignal(SIGINT); 585 setsignal(SIGQUIT); 586 setsignal(SIGTERM); 587 } 588 return pid; 589 } 590 if (rootshell && mode != FORK_NOJOB && mflag) { 591 if (jp == NULL || jp->nprocs == 0) 592 pgrp = pid; 593 else 594 pgrp = jp->ps[0].pid; 595 setpgrp(pid, pgrp); 596 } 597 if (mode == FORK_BG) 598 backgndpid = pid; /* set $! */ 599 if (jp) { 600 struct procstat *ps = &jp->ps[jp->nprocs++]; 601 ps->pid = pid; 602 ps->status = -1; 603 ps->cmd = nullstr; 604 if (iflag && rootshell && n) 605 ps->cmd = commandtext(n); 606 } 607 INTON; 608 TRACE(("In parent shell: child = %d\n", pid)); 609 return pid; 610 } 611 612 613 614 /* 615 * Wait for job to finish. 616 * 617 * Under job control we have the problem that while a child process is 618 * running interrupts generated by the user are sent to the child but not 619 * to the shell. This means that an infinite loop started by an inter- 620 * active user may be hard to kill. With job control turned off, an 621 * interactive user may place an interactive program inside a loop. If 622 * the interactive program catches interrupts, the user doesn't want 623 * these interrupts to also abort the loop. The approach we take here 624 * is to have the shell ignore interrupt signals while waiting for a 625 * forground process to terminate, and then send itself an interrupt 626 * signal if the child process was terminated by an interrupt signal. 627 * Unfortunately, some programs want to do a bit of cleanup and then 628 * exit on interrupt; unless these processes terminate themselves by 629 * sending a signal to themselves (instead of calling exit) they will 630 * confuse this approach. 631 */ 632 633 int 634 waitforjob(jp) 635 register struct job *jp; 636 { 637 #if JOBS 638 int mypgrp = getpgrp(0); 639 #endif 640 int status; 641 int st; 642 643 INTOFF; 644 TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 645 while (jp->state == 0) { 646 dowait(1, jp); 647 } 648 #if JOBS 649 if (jp->jobctl) { 650 if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 651 error("TIOCSPGRP failed, errno=%d\n", errno); 652 } 653 if (jp->state == JOBSTOPPED) 654 curjob = jp - jobtab + 1; 655 #endif 656 status = jp->ps[jp->nprocs - 1].status; 657 /* convert to 8 bits */ 658 if ((status & 0xFF) == 0) 659 st = status >> 8 & 0xFF; 660 #if JOBS 661 else if ((status & 0xFF) == 0177) 662 st = (status >> 8 & 0x7F) + 128; 663 #endif 664 else 665 st = (status & 0x7F) + 128; 666 if (! JOBS || jp->state == JOBDONE) 667 freejob(jp); 668 CLEAR_PENDING_INT; 669 if ((status & 0x7F) == SIGINT) 670 kill(getpid(), SIGINT); 671 INTON; 672 return st; 673 } 674 675 676 677 /* 678 * Wait for a process to terminate. 679 */ 680 681 STATIC int 682 dowait(block, job) 683 struct job *job; 684 { 685 int pid; 686 int status; 687 struct procstat *sp; 688 struct job *jp; 689 struct job *thisjob; 690 int done; 691 int stopped; 692 int core; 693 694 TRACE(("dowait(%d) called\n", block)); 695 do { 696 pid = waitproc(block, &status); 697 TRACE(("wait returns %d, status=%d\n", pid, status)); 698 } while (pid == -1 && errno == EINTR); 699 if (pid <= 0) 700 return pid; 701 INTOFF; 702 thisjob = NULL; 703 for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 704 if (jp->used) { 705 done = 1; 706 stopped = 1; 707 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 708 if (sp->pid == -1) 709 continue; 710 if (sp->pid == pid) { 711 TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status)); 712 sp->status = status; 713 thisjob = jp; 714 } 715 if (sp->status == -1) 716 stopped = 0; 717 else if ((sp->status & 0377) == 0177) 718 done = 0; 719 } 720 if (stopped) { /* stopped or done */ 721 int state = done? JOBDONE : JOBSTOPPED; 722 if (jp->state != state) { 723 TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 724 jp->state = state; 725 #if JOBS 726 if (done && curjob == jp - jobtab + 1) 727 curjob = 0; /* no current job */ 728 #endif 729 } 730 } 731 } 732 } 733 INTON; 734 if (! rootshell || ! iflag || (job && thisjob == job)) { 735 #if JOBS 736 if ((status & 0xFF) == 0177) 737 status >>= 8; 738 #endif 739 core = status & 0x80; 740 status &= 0x7F; 741 if (status != 0 && status != SIGINT && status != SIGPIPE) { 742 if (thisjob != job) 743 outfmt(out2, "%d: ", pid); 744 #if JOBS 745 if (status == SIGTSTP && rootshell && iflag) 746 outfmt(out2, "%%%d ", job - jobtab + 1); 747 #endif 748 if (status <= MAXSIG && sigmesg[status]) 749 out2str(sigmesg[status]); 750 else 751 outfmt(out2, "Signal %d", status); 752 if (core) 753 out2str(" - core dumped"); 754 out2c('\n'); 755 flushout(&errout); 756 } else { 757 TRACE(("Not printing status: status=%d\n", status)); 758 } 759 } else { 760 TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 761 if (thisjob) 762 thisjob->changed = 1; 763 } 764 return pid; 765 } 766 767 768 769 /* 770 * Do a wait system call. If job control is compiled in, we accept 771 * stopped processes. If block is zero, we return a value of zero 772 * rather than blocking. 773 * 774 * System V doesn't have a non-blocking wait system call. It does 775 * have a SIGCLD signal that is sent to a process when one of it's 776 * children dies. The obvious way to use SIGCLD would be to install 777 * a handler for SIGCLD which simply bumped a counter when a SIGCLD 778 * was received, and have waitproc bump another counter when it got 779 * the status of a process. Waitproc would then know that a wait 780 * system call would not block if the two counters were different. 781 * This approach doesn't work because if a process has children that 782 * have not been waited for, System V will send it a SIGCLD when it 783 * installs a signal handler for SIGCLD. What this means is that when 784 * a child exits, the shell will be sent SIGCLD signals continuously 785 * until is runs out of stack space, unless it does a wait call before 786 * restoring the signal handler. The code below takes advantage of 787 * this (mis)feature by installing a signal handler for SIGCLD and 788 * then checking to see whether it was called. If there are any 789 * children to be waited for, it will be. 790 * 791 * If neither SYSV nor BSD is defined, we don't implement nonblocking 792 * waits at all. In this case, the user will not be informed when 793 * a background process until the next time she runs a real program 794 * (as opposed to running a builtin command or just typing return), 795 * and the jobs command may give out of date information. 796 */ 797 798 #ifdef SYSV 799 STATIC int gotsigchild; 800 801 STATIC int onsigchild() { 802 gotsigchild = 1; 803 } 804 #endif 805 806 807 STATIC int 808 waitproc(block, status) 809 int *status; 810 { 811 #ifdef BSD 812 int flags; 813 814 #if JOBS 815 flags = WUNTRACED; 816 #else 817 flags = 0; 818 #endif 819 if (block == 0) 820 flags |= WNOHANG; 821 return wait3(status, flags, (struct rusage *)NULL); 822 #else 823 #ifdef SYSV 824 int (*save)(); 825 826 if (block == 0) { 827 gotsigchild = 0; 828 save = signal(SIGCLD, onsigchild); 829 signal(SIGCLD, save); 830 if (gotsigchild == 0) 831 return 0; 832 } 833 return wait(status); 834 #else 835 if (block == 0) 836 return 0; 837 return wait(status); 838 #endif 839 #endif 840 } 841 842 /* 843 * return 1 if there are stopped jobs, otherwise 0 844 */ 845 int job_warning = 0; 846 int 847 stoppedjobs() 848 { 849 register int jobno; 850 register struct job *jp; 851 852 if (job_warning) 853 return (0); 854 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 855 if (jp->used == 0) 856 continue; 857 if (jp->state == JOBSTOPPED) { 858 out2str("You have stopped jobs.\n"); 859 job_warning = 2; 860 return (1); 861 } 862 } 863 864 return (0); 865 } 866 867 /* 868 * Return a string identifying a command (to be printed by the 869 * jobs command. 870 */ 871 872 STATIC char *cmdnextc; 873 STATIC int cmdnleft; 874 STATIC void cmdtxt(), cmdputs(); 875 #define MAXCMDTEXT 200 876 877 char * 878 commandtext(n) 879 union node *n; 880 { 881 char *name; 882 883 cmdnextc = name = ckmalloc(MAXCMDTEXT); 884 cmdnleft = MAXCMDTEXT - 4; 885 cmdtxt(n); 886 *cmdnextc = '\0'; 887 return name; 888 } 889 890 891 STATIC void 892 cmdtxt(n) 893 union node *n; 894 { 895 union node *np; 896 struct nodelist *lp; 897 char *p; 898 int i; 899 char s[2]; 900 901 if (n == NULL) 902 return; 903 switch (n->type) { 904 case NSEMI: 905 cmdtxt(n->nbinary.ch1); 906 cmdputs("; "); 907 cmdtxt(n->nbinary.ch2); 908 break; 909 case NAND: 910 cmdtxt(n->nbinary.ch1); 911 cmdputs(" && "); 912 cmdtxt(n->nbinary.ch2); 913 break; 914 case NOR: 915 cmdtxt(n->nbinary.ch1); 916 cmdputs(" || "); 917 cmdtxt(n->nbinary.ch2); 918 break; 919 case NPIPE: 920 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 921 cmdtxt(lp->n); 922 if (lp->next) 923 cmdputs(" | "); 924 } 925 break; 926 case NSUBSHELL: 927 cmdputs("("); 928 cmdtxt(n->nredir.n); 929 cmdputs(")"); 930 break; 931 case NREDIR: 932 case NBACKGND: 933 cmdtxt(n->nredir.n); 934 break; 935 case NIF: 936 cmdputs("if "); 937 cmdtxt(n->nif.test); 938 cmdputs("; then "); 939 cmdtxt(n->nif.ifpart); 940 cmdputs("..."); 941 break; 942 case NWHILE: 943 cmdputs("while "); 944 goto until; 945 case NUNTIL: 946 cmdputs("until "); 947 until: 948 cmdtxt(n->nbinary.ch1); 949 cmdputs("; do "); 950 cmdtxt(n->nbinary.ch2); 951 cmdputs("; done"); 952 break; 953 case NFOR: 954 cmdputs("for "); 955 cmdputs(n->nfor.var); 956 cmdputs(" in ..."); 957 break; 958 case NCASE: 959 cmdputs("case "); 960 cmdputs(n->ncase.expr->narg.text); 961 cmdputs(" in ..."); 962 break; 963 case NDEFUN: 964 cmdputs(n->narg.text); 965 cmdputs("() ..."); 966 break; 967 case NCMD: 968 for (np = n->ncmd.args ; np ; np = np->narg.next) { 969 cmdtxt(np); 970 if (np->narg.next) 971 cmdputs(" "); 972 } 973 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 974 cmdputs(" "); 975 cmdtxt(np); 976 } 977 break; 978 case NARG: 979 cmdputs(n->narg.text); 980 break; 981 case NTO: 982 p = ">"; i = 1; goto redir; 983 case NAPPEND: 984 p = ">>"; i = 1; goto redir; 985 case NTOFD: 986 p = ">&"; i = 1; goto redir; 987 case NFROM: 988 p = "<"; i = 0; goto redir; 989 case NFROMFD: 990 p = "<&"; i = 0; goto redir; 991 redir: 992 if (n->nfile.fd != i) { 993 s[0] = n->nfile.fd + '0'; 994 s[1] = '\0'; 995 cmdputs(s); 996 } 997 cmdputs(p); 998 if (n->type == NTOFD || n->type == NFROMFD) { 999 s[0] = n->ndup.dupfd + '0'; 1000 s[1] = '\0'; 1001 cmdputs(s); 1002 } else { 1003 cmdtxt(n->nfile.fname); 1004 } 1005 break; 1006 case NHERE: 1007 case NXHERE: 1008 cmdputs("<<..."); 1009 break; 1010 default: 1011 cmdputs("???"); 1012 break; 1013 } 1014 } 1015 1016 1017 1018 STATIC void 1019 cmdputs(s) 1020 char *s; 1021 { 1022 register char *p, *q; 1023 register char c; 1024 int subtype = 0; 1025 1026 if (cmdnleft <= 0) 1027 return; 1028 p = s; 1029 q = cmdnextc; 1030 while ((c = *p++) != '\0') { 1031 if (c == CTLESC) 1032 *q++ = *p++; 1033 else if (c == CTLVAR) { 1034 *q++ = '$'; 1035 if (--cmdnleft > 0) 1036 *q++ = '{'; 1037 subtype = *p++; 1038 } else if (c == '=' && subtype != 0) { 1039 *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 1040 subtype = 0; 1041 } else if (c == CTLENDVAR) { 1042 *q++ = '}'; 1043 } else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE) 1044 cmdnleft++; /* ignore it */ 1045 else 1046 *q++ = c; 1047 if (--cmdnleft <= 0) { 1048 *q++ = '.'; 1049 *q++ = '.'; 1050 *q++ = '.'; 1051 break; 1052 } 1053 } 1054 cmdnextc = q; 1055 } 1056