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