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