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