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