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$ 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 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 463 bcopy(jobtab, jp, njobs * sizeof jp[0]); 464 ckfree(jobtab); 465 jobtab = jp; 466 } 467 jp = jobtab + njobs; 468 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 469 INTON; 470 break; 471 } 472 if (jp->used == 0) 473 break; 474 } 475 INTOFF; 476 jp->state = 0; 477 jp->used = 1; 478 jp->changed = 0; 479 jp->nprocs = 0; 480 #if JOBS 481 jp->jobctl = jobctl; 482 #endif 483 if (nprocs > 1) { 484 jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 485 } else { 486 jp->ps = &jp->ps0; 487 } 488 INTON; 489 TRACE(("makejob(0x%x, %d) returns %%%d\n", (int)node, nprocs, jp - jobtab + 1)); 490 return jp; 491 } 492 493 494 /* 495 * Fork of a subshell. If we are doing job control, give the subshell its 496 * own process group. Jp is a job structure that the job is to be added to. 497 * N is the command that will be evaluated by the child. Both jp and n may 498 * be NULL. The mode parameter can be one of the following: 499 * FORK_FG - Fork off a foreground process. 500 * FORK_BG - Fork off a background process. 501 * FORK_NOJOB - Like FORK_FG, but don't give the process its own 502 * process group even if job control is on. 503 * 504 * When job control is turned off, background processes have their standard 505 * input redirected to /dev/null (except for the second and later processes 506 * in a pipeline). 507 */ 508 509 int 510 forkshell(jp, n, mode) 511 union node *n; 512 struct job *jp; 513 { 514 int pid; 515 int pgrp; 516 517 TRACE(("forkshell(%%%d, 0x%x, %d) called\n", jp - jobtab, (int)n, mode)); 518 INTOFF; 519 pid = fork(); 520 if (pid == -1) { 521 TRACE(("Fork failed, errno=%d\n", errno)); 522 INTON; 523 error("Cannot fork"); 524 } 525 if (pid == 0) { 526 struct job *p; 527 int wasroot; 528 int i; 529 530 TRACE(("Child shell %d\n", getpid())); 531 wasroot = rootshell; 532 rootshell = 0; 533 for (i = njobs, p = jobtab ; --i >= 0 ; p++) 534 if (p->used) 535 freejob(p); 536 closescript(); 537 INTON; 538 clear_traps(); 539 #if JOBS 540 jobctl = 0; /* do job control only in root shell */ 541 if (wasroot && mode != FORK_NOJOB && mflag) { 542 if (jp == NULL || jp->nprocs == 0) 543 pgrp = getpid(); 544 else 545 pgrp = jp->ps[0].pid; 546 setpgrp(0, pgrp); 547 if (mode == FORK_FG) { 548 /*** this causes superfluous TIOCSPGRPS ***/ 549 if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 550 error("TIOCSPGRP failed, errno=%d\n", errno); 551 } 552 setsignal(SIGTSTP); 553 setsignal(SIGTTOU); 554 } else if (mode == FORK_BG) { 555 ignoresig(SIGINT); 556 ignoresig(SIGQUIT); 557 if ((jp == NULL || jp->nprocs == 0) && 558 ! fd0_redirected_p ()) { 559 close(0); 560 if (open("/dev/null", O_RDONLY) != 0) 561 error("Can't open /dev/null"); 562 } 563 } 564 #else 565 if (mode == FORK_BG) { 566 ignoresig(SIGINT); 567 ignoresig(SIGQUIT); 568 if ((jp == NULL || jp->nprocs == 0) && 569 ! fd0_redirected_p ()) { 570 close(0); 571 if (open("/dev/null", O_RDONLY) != 0) 572 error("Can't open /dev/null"); 573 } 574 } 575 #endif 576 if (wasroot && iflag) { 577 setsignal(SIGINT); 578 setsignal(SIGQUIT); 579 setsignal(SIGTERM); 580 } 581 return pid; 582 } 583 if (rootshell && mode != FORK_NOJOB && mflag) { 584 if (jp == NULL || jp->nprocs == 0) 585 pgrp = pid; 586 else 587 pgrp = jp->ps[0].pid; 588 setpgrp(pid, pgrp); 589 } 590 if (mode == FORK_BG) 591 backgndpid = pid; /* set $! */ 592 if (jp) { 593 struct procstat *ps = &jp->ps[jp->nprocs++]; 594 ps->pid = pid; 595 ps->status = -1; 596 ps->cmd = nullstr; 597 if (iflag && rootshell && n) 598 ps->cmd = commandtext(n); 599 } 600 INTON; 601 TRACE(("In parent shell: child = %d\n", pid)); 602 return pid; 603 } 604 605 606 607 /* 608 * Wait for job to finish. 609 * 610 * Under job control we have the problem that while a child process is 611 * running interrupts generated by the user are sent to the child but not 612 * to the shell. This means that an infinite loop started by an inter- 613 * active user may be hard to kill. With job control turned off, an 614 * interactive user may place an interactive program inside a loop. If 615 * the interactive program catches interrupts, the user doesn't want 616 * these interrupts to also abort the loop. The approach we take here 617 * is to have the shell ignore interrupt signals while waiting for a 618 * forground process to terminate, and then send itself an interrupt 619 * signal if the child process was terminated by an interrupt signal. 620 * Unfortunately, some programs want to do a bit of cleanup and then 621 * exit on interrupt; unless these processes terminate themselves by 622 * sending a signal to themselves (instead of calling exit) they will 623 * confuse this approach. 624 */ 625 626 int 627 waitforjob(jp) 628 register struct job *jp; 629 { 630 #if JOBS 631 int mypgrp = getpgrp(0); 632 #endif 633 int status; 634 int st; 635 636 INTOFF; 637 TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 638 while (jp->state == 0) { 639 dowait(1, jp); 640 } 641 #if JOBS 642 if (jp->jobctl) { 643 if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 644 error("TIOCSPGRP failed, errno=%d\n", errno); 645 } 646 if (jp->state == JOBSTOPPED) 647 curjob = jp - jobtab + 1; 648 #endif 649 status = jp->ps[jp->nprocs - 1].status; 650 /* convert to 8 bits */ 651 if ((status & 0xFF) == 0) 652 st = status >> 8 & 0xFF; 653 #if JOBS 654 else if ((status & 0xFF) == 0177) 655 st = (status >> 8 & 0x7F) + 128; 656 #endif 657 else 658 st = (status & 0x7F) + 128; 659 if (! JOBS || jp->state == JOBDONE) 660 freejob(jp); 661 CLEAR_PENDING_INT; 662 if ((status & 0x7F) == SIGINT) 663 kill(getpid(), SIGINT); 664 INTON; 665 return st; 666 } 667 668 669 670 /* 671 * Wait for a process to terminate. 672 */ 673 674 STATIC int 675 dowait(block, job) 676 struct job *job; 677 { 678 int pid; 679 int status; 680 struct procstat *sp; 681 struct job *jp; 682 struct job *thisjob; 683 int done; 684 int stopped; 685 int core; 686 687 TRACE(("dowait(%d) called\n", block)); 688 do { 689 pid = waitproc(block, &status); 690 TRACE(("wait returns %d, status=%d\n", pid, status)); 691 } while (pid == -1 && errno == EINTR); 692 if (pid <= 0) 693 return pid; 694 INTOFF; 695 thisjob = NULL; 696 for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 697 if (jp->used) { 698 done = 1; 699 stopped = 1; 700 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 701 if (sp->pid == -1) 702 continue; 703 if (sp->pid == pid) { 704 TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status)); 705 sp->status = status; 706 thisjob = jp; 707 } 708 if (sp->status == -1) 709 stopped = 0; 710 else if ((sp->status & 0377) == 0177) 711 done = 0; 712 } 713 if (stopped) { /* stopped or done */ 714 int state = done? JOBDONE : JOBSTOPPED; 715 if (jp->state != state) { 716 TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 717 jp->state = state; 718 #if JOBS 719 if (done && curjob == jp - jobtab + 1) 720 curjob = 0; /* no current job */ 721 #endif 722 } 723 } 724 } 725 } 726 INTON; 727 if (! rootshell || ! iflag || (job && thisjob == job)) { 728 #if JOBS 729 if ((status & 0xFF) == 0177) 730 status >>= 8; 731 #endif 732 core = status & 0x80; 733 status &= 0x7F; 734 if (status != 0 && status != SIGINT && status != SIGPIPE) { 735 if (thisjob != job) 736 outfmt(out2, "%d: ", pid); 737 #if JOBS 738 if (status == SIGTSTP && rootshell && iflag) 739 outfmt(out2, "%%%d ", job - jobtab + 1); 740 #endif 741 if (status <= MAXSIG && sigmesg[status]) 742 out2str(sigmesg[status]); 743 else 744 outfmt(out2, "Signal %d", status); 745 if (core) 746 out2str(" - core dumped"); 747 out2c('\n'); 748 flushout(&errout); 749 } else { 750 TRACE(("Not printing status: status=%d\n", status)); 751 } 752 } else { 753 TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 754 if (thisjob) 755 thisjob->changed = 1; 756 } 757 return pid; 758 } 759 760 761 762 /* 763 * Do a wait system call. If job control is compiled in, we accept 764 * stopped processes. If block is zero, we return a value of zero 765 * rather than blocking. 766 * 767 * System V doesn't have a non-blocking wait system call. It does 768 * have a SIGCLD signal that is sent to a process when one of it's 769 * children dies. The obvious way to use SIGCLD would be to install 770 * a handler for SIGCLD which simply bumped a counter when a SIGCLD 771 * was received, and have waitproc bump another counter when it got 772 * the status of a process. Waitproc would then know that a wait 773 * system call would not block if the two counters were different. 774 * This approach doesn't work because if a process has children that 775 * have not been waited for, System V will send it a SIGCLD when it 776 * installs a signal handler for SIGCLD. What this means is that when 777 * a child exits, the shell will be sent SIGCLD signals continuously 778 * until is runs out of stack space, unless it does a wait call before 779 * restoring the signal handler. The code below takes advantage of 780 * this (mis)feature by installing a signal handler for SIGCLD and 781 * then checking to see whether it was called. If there are any 782 * children to be waited for, it will be. 783 * 784 * If neither SYSV nor BSD is defined, we don't implement nonblocking 785 * waits at all. In this case, the user will not be informed when 786 * a background process until the next time she runs a real program 787 * (as opposed to running a builtin command or just typing return), 788 * and the jobs command may give out of date information. 789 */ 790 791 #ifdef SYSV 792 STATIC int gotsigchild; 793 794 STATIC int onsigchild() { 795 gotsigchild = 1; 796 } 797 #endif 798 799 800 STATIC int 801 waitproc(block, status) 802 int *status; 803 { 804 #ifdef BSD 805 int flags; 806 807 #if JOBS 808 flags = WUNTRACED; 809 #else 810 flags = 0; 811 #endif 812 if (block == 0) 813 flags |= WNOHANG; 814 return wait3(status, flags, (struct rusage *)NULL); 815 #else 816 #ifdef SYSV 817 int (*save)(); 818 819 if (block == 0) { 820 gotsigchild = 0; 821 save = signal(SIGCLD, onsigchild); 822 signal(SIGCLD, save); 823 if (gotsigchild == 0) 824 return 0; 825 } 826 return wait(status); 827 #else 828 if (block == 0) 829 return 0; 830 return wait(status); 831 #endif 832 #endif 833 } 834 835 /* 836 * return 1 if there are stopped jobs, otherwise 0 837 */ 838 int job_warning = 0; 839 int 840 stoppedjobs() 841 { 842 register int jobno; 843 register struct job *jp; 844 845 if (job_warning) 846 return (0); 847 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 848 if (jp->used == 0) 849 continue; 850 if (jp->state == JOBSTOPPED) { 851 out2str("You have stopped jobs.\n"); 852 job_warning = 2; 853 return (1); 854 } 855 } 856 857 return (0); 858 } 859 860 /* 861 * Return a string identifying a command (to be printed by the 862 * jobs command. 863 */ 864 865 STATIC char *cmdnextc; 866 STATIC int cmdnleft; 867 STATIC void cmdtxt(), cmdputs(); 868 #define MAXCMDTEXT 200 869 870 char * 871 commandtext(n) 872 union node *n; 873 { 874 char *name; 875 876 cmdnextc = name = ckmalloc(MAXCMDTEXT); 877 cmdnleft = MAXCMDTEXT - 4; 878 cmdtxt(n); 879 *cmdnextc = '\0'; 880 return name; 881 } 882 883 884 STATIC void 885 cmdtxt(n) 886 union node *n; 887 { 888 union node *np; 889 struct nodelist *lp; 890 char *p; 891 int i; 892 char s[2]; 893 894 if (n == NULL) 895 return; 896 switch (n->type) { 897 case NSEMI: 898 cmdtxt(n->nbinary.ch1); 899 cmdputs("; "); 900 cmdtxt(n->nbinary.ch2); 901 break; 902 case NAND: 903 cmdtxt(n->nbinary.ch1); 904 cmdputs(" && "); 905 cmdtxt(n->nbinary.ch2); 906 break; 907 case NOR: 908 cmdtxt(n->nbinary.ch1); 909 cmdputs(" || "); 910 cmdtxt(n->nbinary.ch2); 911 break; 912 case NPIPE: 913 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 914 cmdtxt(lp->n); 915 if (lp->next) 916 cmdputs(" | "); 917 } 918 break; 919 case NSUBSHELL: 920 cmdputs("("); 921 cmdtxt(n->nredir.n); 922 cmdputs(")"); 923 break; 924 case NREDIR: 925 case NBACKGND: 926 cmdtxt(n->nredir.n); 927 break; 928 case NIF: 929 cmdputs("if "); 930 cmdtxt(n->nif.test); 931 cmdputs("; then "); 932 cmdtxt(n->nif.ifpart); 933 cmdputs("..."); 934 break; 935 case NWHILE: 936 cmdputs("while "); 937 goto until; 938 case NUNTIL: 939 cmdputs("until "); 940 until: 941 cmdtxt(n->nbinary.ch1); 942 cmdputs("; do "); 943 cmdtxt(n->nbinary.ch2); 944 cmdputs("; done"); 945 break; 946 case NFOR: 947 cmdputs("for "); 948 cmdputs(n->nfor.var); 949 cmdputs(" in ..."); 950 break; 951 case NCASE: 952 cmdputs("case "); 953 cmdputs(n->ncase.expr->narg.text); 954 cmdputs(" in ..."); 955 break; 956 case NDEFUN: 957 cmdputs(n->narg.text); 958 cmdputs("() ..."); 959 break; 960 case NCMD: 961 for (np = n->ncmd.args ; np ; np = np->narg.next) { 962 cmdtxt(np); 963 if (np->narg.next) 964 cmdputs(" "); 965 } 966 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 967 cmdputs(" "); 968 cmdtxt(np); 969 } 970 break; 971 case NARG: 972 cmdputs(n->narg.text); 973 break; 974 case NTO: 975 p = ">"; i = 1; goto redir; 976 case NAPPEND: 977 p = ">>"; i = 1; goto redir; 978 case NTOFD: 979 p = ">&"; i = 1; goto redir; 980 case NFROM: 981 p = "<"; i = 0; goto redir; 982 case NFROMFD: 983 p = "<&"; i = 0; goto redir; 984 redir: 985 if (n->nfile.fd != i) { 986 s[0] = n->nfile.fd + '0'; 987 s[1] = '\0'; 988 cmdputs(s); 989 } 990 cmdputs(p); 991 if (n->type == NTOFD || n->type == NFROMFD) { 992 s[0] = n->ndup.dupfd + '0'; 993 s[1] = '\0'; 994 cmdputs(s); 995 } else { 996 cmdtxt(n->nfile.fname); 997 } 998 break; 999 case NHERE: 1000 case NXHERE: 1001 cmdputs("<<..."); 1002 break; 1003 default: 1004 cmdputs("???"); 1005 break; 1006 } 1007 } 1008 1009 1010 1011 STATIC void 1012 cmdputs(s) 1013 char *s; 1014 { 1015 register char *p, *q; 1016 register char c; 1017 int subtype = 0; 1018 1019 if (cmdnleft <= 0) 1020 return; 1021 p = s; 1022 q = cmdnextc; 1023 while ((c = *p++) != '\0') { 1024 if (c == CTLESC) 1025 *q++ = *p++; 1026 else if (c == CTLVAR) { 1027 *q++ = '$'; 1028 if (--cmdnleft > 0) 1029 *q++ = '{'; 1030 subtype = *p++; 1031 } else if (c == '=' && subtype != 0) { 1032 *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 1033 subtype = 0; 1034 } else if (c == CTLENDVAR) { 1035 *q++ = '}'; 1036 } else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE) 1037 cmdnleft++; /* ignore it */ 1038 else 1039 *q++ = c; 1040 if (--cmdnleft <= 0) { 1041 *q++ = '.'; 1042 *q++ = '.'; 1043 *q++ = '.'; 1044 break; 1045 } 1046 } 1047 cmdnextc = q; 1048 } 1049