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