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