jobs.c revision 1.25 1 /* $NetBSD: jobs.c,v 1.25 1998/02/04 23:49:25 thorpej 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.25 1998/02/04 23:49:25 thorpej 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, "%%%ld ",
814 (long)(job - jobtab + 1));
815 #endif
816 if (sig < NSIG && sys_siglist[sig])
817 out2str(sys_siglist[sig]);
818 else
819 outfmt(out2, "Signal %d", sig);
820 if (core)
821 out2str(" - core dumped");
822 out2c('\n');
823 flushout(&errout);
824 } else {
825 TRACE(("Not printing status: status=%d, sig=%d\n",
826 status, sig));
827 }
828 } else {
829 TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
830 if (thisjob)
831 thisjob->changed = 1;
832 }
833 return pid;
834 }
835
836
837
838 /*
839 * Do a wait system call. If job control is compiled in, we accept
840 * stopped processes. If block is zero, we return a value of zero
841 * rather than blocking.
842 *
843 * System V doesn't have a non-blocking wait system call. It does
844 * have a SIGCLD signal that is sent to a process when one of it's
845 * children dies. The obvious way to use SIGCLD would be to install
846 * a handler for SIGCLD which simply bumped a counter when a SIGCLD
847 * was received, and have waitproc bump another counter when it got
848 * the status of a process. Waitproc would then know that a wait
849 * system call would not block if the two counters were different.
850 * This approach doesn't work because if a process has children that
851 * have not been waited for, System V will send it a SIGCLD when it
852 * installs a signal handler for SIGCLD. What this means is that when
853 * a child exits, the shell will be sent SIGCLD signals continuously
854 * until is runs out of stack space, unless it does a wait call before
855 * restoring the signal handler. The code below takes advantage of
856 * this (mis)feature by installing a signal handler for SIGCLD and
857 * then checking to see whether it was called. If there are any
858 * children to be waited for, it will be.
859 *
860 * If neither SYSV nor BSD is defined, we don't implement nonblocking
861 * waits at all. In this case, the user will not be informed when
862 * a background process until the next time she runs a real program
863 * (as opposed to running a builtin command or just typing return),
864 * and the jobs command may give out of date information.
865 */
866
867 #ifdef SYSV
868 STATIC int gotsigchild;
869
870 STATIC int onsigchild() {
871 gotsigchild = 1;
872 }
873 #endif
874
875
876 STATIC int
877 waitproc(block, status)
878 int block;
879 int *status;
880 {
881 #ifdef BSD
882 int flags;
883
884 #if JOBS
885 flags = WUNTRACED;
886 #else
887 flags = 0;
888 #endif
889 if (block == 0)
890 flags |= WNOHANG;
891 return wait3(status, flags, (struct rusage *)NULL);
892 #else
893 #ifdef SYSV
894 int (*save)();
895
896 if (block == 0) {
897 gotsigchild = 0;
898 save = signal(SIGCLD, onsigchild);
899 signal(SIGCLD, save);
900 if (gotsigchild == 0)
901 return 0;
902 }
903 return wait(status);
904 #else
905 if (block == 0)
906 return 0;
907 return wait(status);
908 #endif
909 #endif
910 }
911
912 /*
913 * return 1 if there are stopped jobs, otherwise 0
914 */
915 int job_warning = 0;
916 int
917 stoppedjobs()
918 {
919 int jobno;
920 struct job *jp;
921
922 if (job_warning)
923 return (0);
924 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
925 if (jp->used == 0)
926 continue;
927 if (jp->state == JOBSTOPPED) {
928 out2str("You have stopped jobs.\n");
929 job_warning = 2;
930 return (1);
931 }
932 }
933
934 return (0);
935 }
936
937 /*
938 * Return a string identifying a command (to be printed by the
939 * jobs command.
940 */
941
942 STATIC char *cmdnextc;
943 STATIC int cmdnleft;
944 #define MAXCMDTEXT 200
945
946 char *
947 commandtext(n)
948 union node *n;
949 {
950 char *name;
951
952 cmdnextc = name = ckmalloc(MAXCMDTEXT);
953 cmdnleft = MAXCMDTEXT - 4;
954 cmdtxt(n);
955 *cmdnextc = '\0';
956 return name;
957 }
958
959
960 STATIC void
961 cmdtxt(n)
962 union node *n;
963 {
964 union node *np;
965 struct nodelist *lp;
966 char *p;
967 int i;
968 char s[2];
969
970 if (n == NULL)
971 return;
972 switch (n->type) {
973 case NSEMI:
974 cmdtxt(n->nbinary.ch1);
975 cmdputs("; ");
976 cmdtxt(n->nbinary.ch2);
977 break;
978 case NAND:
979 cmdtxt(n->nbinary.ch1);
980 cmdputs(" && ");
981 cmdtxt(n->nbinary.ch2);
982 break;
983 case NOR:
984 cmdtxt(n->nbinary.ch1);
985 cmdputs(" || ");
986 cmdtxt(n->nbinary.ch2);
987 break;
988 case NPIPE:
989 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
990 cmdtxt(lp->n);
991 if (lp->next)
992 cmdputs(" | ");
993 }
994 break;
995 case NSUBSHELL:
996 cmdputs("(");
997 cmdtxt(n->nredir.n);
998 cmdputs(")");
999 break;
1000 case NREDIR:
1001 case NBACKGND:
1002 cmdtxt(n->nredir.n);
1003 break;
1004 case NIF:
1005 cmdputs("if ");
1006 cmdtxt(n->nif.test);
1007 cmdputs("; then ");
1008 cmdtxt(n->nif.ifpart);
1009 cmdputs("...");
1010 break;
1011 case NWHILE:
1012 cmdputs("while ");
1013 goto until;
1014 case NUNTIL:
1015 cmdputs("until ");
1016 until:
1017 cmdtxt(n->nbinary.ch1);
1018 cmdputs("; do ");
1019 cmdtxt(n->nbinary.ch2);
1020 cmdputs("; done");
1021 break;
1022 case NFOR:
1023 cmdputs("for ");
1024 cmdputs(n->nfor.var);
1025 cmdputs(" in ...");
1026 break;
1027 case NCASE:
1028 cmdputs("case ");
1029 cmdputs(n->ncase.expr->narg.text);
1030 cmdputs(" in ...");
1031 break;
1032 case NDEFUN:
1033 cmdputs(n->narg.text);
1034 cmdputs("() ...");
1035 break;
1036 case NCMD:
1037 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1038 cmdtxt(np);
1039 if (np->narg.next)
1040 cmdputs(" ");
1041 }
1042 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1043 cmdputs(" ");
1044 cmdtxt(np);
1045 }
1046 break;
1047 case NARG:
1048 cmdputs(n->narg.text);
1049 break;
1050 case NTO:
1051 p = ">"; i = 1; goto redir;
1052 case NAPPEND:
1053 p = ">>"; i = 1; goto redir;
1054 case NTOFD:
1055 p = ">&"; i = 1; goto redir;
1056 case NFROM:
1057 p = "<"; i = 0; goto redir;
1058 case NFROMFD:
1059 p = "<&"; i = 0; goto redir;
1060 redir:
1061 if (n->nfile.fd != i) {
1062 s[0] = n->nfile.fd + '0';
1063 s[1] = '\0';
1064 cmdputs(s);
1065 }
1066 cmdputs(p);
1067 if (n->type == NTOFD || n->type == NFROMFD) {
1068 s[0] = n->ndup.dupfd + '0';
1069 s[1] = '\0';
1070 cmdputs(s);
1071 } else {
1072 cmdtxt(n->nfile.fname);
1073 }
1074 break;
1075 case NHERE:
1076 case NXHERE:
1077 cmdputs("<<...");
1078 break;
1079 default:
1080 cmdputs("???");
1081 break;
1082 }
1083 }
1084
1085
1086
1087 STATIC void
1088 cmdputs(s)
1089 char *s;
1090 {
1091 char *p, *q;
1092 char c;
1093 int subtype = 0;
1094
1095 if (cmdnleft <= 0)
1096 return;
1097 p = s;
1098 q = cmdnextc;
1099 while ((c = *p++) != '\0') {
1100 if (c == CTLESC)
1101 *q++ = *p++;
1102 else if (c == CTLVAR) {
1103 *q++ = '$';
1104 if (--cmdnleft > 0)
1105 *q++ = '{';
1106 subtype = *p++;
1107 } else if (c == '=' && subtype != 0) {
1108 *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
1109 subtype = 0;
1110 } else if (c == CTLENDVAR) {
1111 *q++ = '}';
1112 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
1113 cmdnleft++; /* ignore it */
1114 else
1115 *q++ = c;
1116 if (--cmdnleft <= 0) {
1117 *q++ = '.';
1118 *q++ = '.';
1119 *q++ = '.';
1120 break;
1121 }
1122 }
1123 cmdnextc = q;
1124 }
1125