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