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