1 /* $NetBSD: jobs.c,v 1.75 2015/08/22 12:12:47 christos Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
40 __RCSID("$NetBSD: jobs.c,v 1.75 2015/08/22 12:12:47 christos Exp $");
50 #include <sys/types.h>
51 #include <sys/param.h>
55 #include <sys/resource.h>
57 #include <sys/ioctl.h>
66 #undef CEOF /* syntax.h redefines this */
85 static struct job
*jobtab
; /* array of jobs */
86 static int njobs
; /* size of array */
87 static int jobs_invalid
; /* set in child */
88 MKINIT pid_t backgndpid
= -1; /* pid of last background process */
90 int initialpgrp
; /* pgrp of shell on invocation */
91 static int curjob
= -1; /* current job */
93 #if JOBS && defined(__minix)
94 static int ttyfd
= -1;
95 #endif /* JOBS && defined(__minix) */
97 STATIC
void restartjob(struct job
*);
98 STATIC
void freejob(struct job
*);
99 STATIC
struct job
*getjob(const char *, int);
100 STATIC
int dowait(int, struct job
*);
103 STATIC
int waitproc(int, struct job
*, int *);
104 STATIC
void cmdtxt(union node
*);
105 STATIC
void cmdlist(union node
*, int);
106 STATIC
void cmdputs(const char *);
109 STATIC
int onsigchild(void);
112 #ifdef OLD_TTY_DRIVER
113 static pid_t
tcgetpgrp(int fd
);
114 static int tcsetpgrp(int fd
, pid_t pgrp
);
120 if (ioctl(fd
, TIOCGPGRP
, (char *)&pgrp
) == -1)
127 tcsetpgrp(int fd
, pid_tpgrp
)
129 return ioctl(fd
, TIOCSPGRP
, (char *)&pgrp
);
134 * Turn job control on and off.
136 * Note: This code assumes that the third arg to ioctl is a character
137 * pointer, which is true on Berkeley systems but not System V. Since
138 * System V doesn't have job control yet, this isn't a problem now.
143 #if JOBS /* Minix setjobctl is defined to empty, compilation error */
147 #ifdef OLD_TTY_DRIVER
151 if (on
== jobctl
|| rootshell
== 0)
154 #if defined(FIOCLEX) || defined(FD_CLOEXEC)
159 if ((ttyfd
= open("/dev/tty", O_RDWR
)) == -1) {
160 for (i
= 0; i
< 3; i
++) {
161 if (isatty(i
) && (ttyfd
= dup(i
)) != -1)
167 /* Move to a high fd */
168 for (i
= 10; i
> 2; i
--) {
169 if ((err
= fcntl(ttyfd
, F_DUPFD
, (1 << i
) - 1)) != -1)
177 err
= ioctl(ttyfd
, FIOCLEX
, 0);
179 err
= fcntl(ttyfd
, F_SETFD
,
180 fcntl(ttyfd
, F_GETFD
, 0) | FD_CLOEXEC
);
188 out2str("sh: Need FIOCLEX or FD_CLOEXEC to support job control");
191 do { /* while we are in the background */
192 if ((initialpgrp
= tcgetpgrp(ttyfd
)) < 0) {
194 out2str("sh: can't access tty; job control turned off\n");
198 if (initialpgrp
== -1)
199 initialpgrp
= getpgrp();
200 else if (initialpgrp
!= getpgrp()) {
206 #ifdef OLD_TTY_DRIVER
207 if (ioctl(ttyfd
, TIOCGETD
, (char *)&ldisc
) < 0
208 || ldisc
!= NTTYDISC
) {
209 out2str("sh: need new tty driver to run job control; job control turned off\n");
214 setsignal(SIGTSTP
, 0);
215 setsignal(SIGTTOU
, 0);
216 setsignal(SIGTTIN
, 0);
217 if (getpgrp() != rootpid
&& setpgid(0, rootpid
) == -1)
218 error("Cannot set process group (%s) at %d",
219 strerror(errno
), __LINE__
);
220 if (tcsetpgrp(ttyfd
, rootpid
) == -1)
221 error("Cannot set tty process group (%s) at %d",
222 strerror(errno
), __LINE__
);
223 } else { /* turning job control off */
224 if (getpgrp() != initialpgrp
&& setpgid(0, initialpgrp
) == -1)
225 error("Cannot set process group (%s) at %d",
226 strerror(errno
), __LINE__
);
227 if (tcsetpgrp(ttyfd
, initialpgrp
) == -1)
228 error("Cannot set tty process group (%s) at %d",
229 strerror(errno
), __LINE__
);
232 setsignal(SIGTSTP
, 0);
233 setsignal(SIGTTOU
, 0);
234 setsignal(SIGTTIN
, 0);
257 do_fgcmd(const char *arg_ptr
)
263 jp
= getjob(arg_ptr
, 0);
265 error("job not created under job control");
266 out1fmt("%s", jp
->ps
[0].cmd
);
267 for (i
= 1; i
< jp
->nprocs
; i
++)
268 out1fmt(" | %s", jp
->ps
[i
].cmd
);
272 for (i
= 0; i
< jp
->nprocs
; i
++)
273 if (tcsetpgrp(ttyfd
, jp
->ps
[i
].pid
) != -1)
276 if (i
>= jp
->nprocs
) {
277 error("Cannot set tty process group (%s) at %d",
278 strerror(errno
), __LINE__
);
282 status
= waitforjob(jp
);
288 fgcmd(int argc
, char **argv
)
291 return do_fgcmd(*argptr
);
295 fgcmd_percent(int argc
, char **argv
)
298 return do_fgcmd(*argv
);
302 set_curjob(struct job
*jp
, int mode
)
304 struct job
*jp1
, *jp2
;
309 /* first remove from list */
311 curjob
= jp
->prev_job
;
313 for (i
= 0; i
< njobs
; i
++) {
314 if (jobtab
[i
].prev_job
!= ji
)
316 jobtab
[i
].prev_job
= jp
->prev_job
;
321 /* Then re-insert in correct position */
323 case 0: /* job being deleted */
326 case 1: /* newly created job or backgrounded job,
327 put after all stopped jobs. */
328 if (curjob
!= -1 && jobtab
[curjob
].state
== JOBSTOPPED
) {
329 for (jp1
= jobtab
+ curjob
; ; jp1
= jp2
) {
330 if (jp1
->prev_job
== -1)
332 jp2
= jobtab
+ jp1
->prev_job
;
333 if (jp2
->state
!= JOBSTOPPED
)
336 jp
->prev_job
= jp1
->prev_job
;
341 case 2: /* newly stopped job - becomes curjob */
342 jp
->prev_job
= curjob
;
349 bgcmd(int argc
, char **argv
)
356 jp
= getjob(*argptr
, 0);
358 error("job not created under job control");
360 out1fmt("[%ld] %s", (long)(jp
- jobtab
+ 1), jp
->ps
[0].cmd
);
361 for (i
= 1; i
< jp
->nprocs
; i
++)
362 out1fmt(" | %s", jp
->ps
[i
].cmd
);
366 } while (*argptr
&& *++argptr
);
372 restartjob(struct job
*jp
)
377 if (jp
->state
== JOBDONE
)
380 for (i
= 0; i
< jp
->nprocs
; i
++)
381 if (killpg(jp
->ps
[i
].pid
, SIGCONT
) != -1)
384 error("Cannot continue job (%s)", strerror(errno
));
385 for (ps
= jp
->ps
, i
= jp
->nprocs
; --i
>= 0 ; ps
++) {
386 if (WIFSTOPPED(ps
->status
)) {
388 jp
->state
= JOBRUNNING
;
394 /* LSC: For Minix add dummy functions. */
396 fgcmd(int argc
, char **argv
)
398 error("no job control in this shell.");
403 fgcmd_percent(int argc
, char **argv
)
405 error("no job control in this shell.");
411 bgcmd(int argc
, char **argv
)
413 error("no job control in this shell.");
419 showjob(struct output
*out
, struct job
*jp
, int mode
)
428 if (mode
& SHOW_PGID
) {
429 /* just output process (group) id of pipeline */
430 outfmt(out
, "%ld\n", (long)jp
->ps
->pid
);
440 mode
|= SHOW_MULTILINE
;
442 if ((procno
> 1 && !(mode
& SHOW_MULTILINE
))
443 || (mode
& SHOW_SIGNALLED
)) {
444 /* See if we have more than one status to report */
448 int st1
= ps
->status
;
450 /* yes - need multi-line output */
451 mode
|= SHOW_MULTILINE
;
452 if (st1
== -1 || !(mode
& SHOW_SIGNALLED
) || WIFEXITED(st1
))
454 if (WIFSTOPPED(st1
) || ((st1
= WTERMSIG(st1
) & 0x7f)
455 && st1
!= SIGINT
&& st1
!= SIGPIPE
))
458 } while (ps
++, --procno
);
462 if (mode
& SHOW_SIGNALLED
&& !(mode
& SHOW_ISSIG
)) {
463 if (jp
->state
== JOBDONE
&& !(mode
& SHOW_NO_FREE
)) {
464 TRACE(("showjob: freeing job %d\n", jp
- jobtab
+ 1));
470 for (ps
= jp
->ps
; --procno
>= 0; ps
++) { /* for each process */
472 fmtstr(s
, 16, "[%ld] %c ",
473 (long)(jp
- jobtab
+ 1),
475 jp
== jobtab
+ curjob
? '+' :
476 curjob
!= -1 && jp
== jobtab
+
477 jobtab
[curjob
].prev_job
? '-' :
483 if (mode
& SHOW_PID
) {
484 fmtstr(s
+ col
, 16, "%ld ", (long)ps
->pid
);
485 col
+= strlen(s
+ col
);
487 if (ps
->status
== -1) {
488 scopy("Running", s
+ col
);
489 } else if (WIFEXITED(ps
->status
)) {
490 st
= WEXITSTATUS(ps
->status
);
492 fmtstr(s
+ col
, 16, "Done(%d)", st
);
494 fmtstr(s
+ col
, 16, "Done");
497 if (WIFSTOPPED(ps
->status
))
498 st
= WSTOPSIG(ps
->status
);
499 else /* WIFSIGNALED(ps->status) */
501 st
= WTERMSIG(ps
->status
);
503 if (st
< NSIG
&& sys_siglist
[st
])
504 scopyn(sys_siglist
[st
], s
+ col
, 32);
506 fmtstr(s
+ col
, 16, "Signal %d", st
);
507 if (WCOREDUMP(ps
->status
)) {
508 col
+= strlen(s
+ col
);
509 scopyn(" (core dumped)", s
+ col
, 64 - col
);
512 col
+= strlen(s
+ col
);
518 outstr(ps
->cmd
, out
);
519 if (mode
& SHOW_MULTILINE
) {
525 while (--procno
>= 0)
526 outfmt(out
, " | %s", (++ps
)->cmd
);
532 if (jp
->state
== JOBDONE
&& !(mode
& SHOW_NO_FREE
))
538 jobscmd(int argc
, char **argv
)
541 int sv
= jobs_invalid
;
545 while ((m
= nextopt("lp")))
552 showjob(out1
, getjob(*argptr
,0), mode
);
555 showjobs(out1
, mode
);
562 * Print a list of jobs. If "change" is nonzero, only print jobs whose
563 * statuses have changed since the last call to showjobs.
565 * If the shell is interrupted in the process of creating a job, the
566 * result may be a job structure containing zero processes. Such structures
567 * will be freed here.
571 showjobs(struct output
*out
, int mode
)
575 #if JOBS /* MINIX: gotpid. */
576 int silent
= 0, gotpid
;
581 TRACE(("showjobs(%x) called\n", mode
));
583 /* If not even one one job changed, there is nothing to do */
584 #if JOBS /* MINIX: gotpid. */
585 gotpid
= dowait(0, NULL
);
587 while (dowait(0, NULL
) > 0)
589 #if JOBS /* MINIX: #ifdef fails when JOBS = 0 */
591 * Check if we are not in our foreground group, and if not
594 if (mflag
&& gotpid
!= -1 && tcgetpgrp(ttyfd
) != getpid()) {
595 if (tcsetpgrp(ttyfd
, getpid()) == -1)
596 error("Cannot set tty process group (%s) at %d",
597 strerror(errno
), __LINE__
);
598 TRACE(("repaired tty process group\n"));
605 for (jobno
= 1, jp
= jobtab
; jobno
<= njobs
; jobno
++, jp
++) {
608 if (jp
->nprocs
== 0) {
612 if ((mode
& SHOW_CHANGED
) && !jp
->changed
)
614 if (silent
&& jp
->changed
) {
618 showjob(out
, jp
, mode
);
623 * Mark a job structure as unused.
627 freejob(struct job
*jp
)
630 if (jp
->ps
!= &jp
->ps0
) {
645 waitcmd(int argc
, char **argv
)
654 /* wait for all jobs */
659 if (jp
>= jobtab
+ njobs
) {
660 /* no running procs */
663 if (!jp
->used
|| jp
->state
!= JOBRUNNING
) {
667 if (dowait(WBLOCK
, NULL
) == -1)
668 return 128 + lastsig();
673 retval
= 127; /* XXXGCC: -Wuninitialized */
674 for (; *argptr
; argptr
++) {
675 job
= getjob(*argptr
, 1);
680 /* loop until process terminated or stopped */
681 while (job
->state
== JOBRUNNING
) {
682 if (dowait(WBLOCK
|WNOFREE
, job
) == -1)
683 return 128 + lastsig();
685 status
= job
->ps
[job
->nprocs
? job
->nprocs
- 1 : 0].status
;
686 if (WIFEXITED(status
))
687 retval
= WEXITSTATUS(status
);
689 else if (WIFSTOPPED(status
))
690 retval
= WSTOPSIG(status
) + 128;
693 /* XXX: limits number of signals */
694 retval
= WTERMSIG(status
) + 128;
705 jobidcmd(int argc
, char **argv
)
711 jp
= getjob(*argptr
, 0);
712 for (i
= 0 ; i
< jp
->nprocs
; ) {
713 out1fmt("%ld", (long)jp
->ps
[i
].pid
);
714 out1c(++i
< jp
->nprocs
? ' ' : '\n');
720 getjobpgrp(const char *name
)
724 jp
= getjob(name
, 1);
727 return -jp
->ps
[0].pid
;
731 * Convert a job name to a job structure.
735 getjob(const char *name
, int noerror
)
741 const char *err_msg
= "No such job: %s";
747 err_msg
= "No current job";
748 } else if (name
[0] == '%') {
749 if (is_number(name
+ 1)) {
750 jobno
= number(name
+ 1) - 1;
751 } else if (!name
[2]) {
758 err_msg
= "No current job";
763 jobno
= jobtab
[jobno
].prev_job
;
764 err_msg
= "No previous job";
774 for (jp
= jobtab
, i
= njobs
; --i
>= 0 ; jp
++) {
775 if (!jp
->used
|| jp
->nprocs
<= 0)
778 && strstr(jp
->ps
[0].cmd
, name
+ 2))
779 || prefix(name
+ 1, jp
->ps
[0].cmd
)) {
781 err_msg
= "%s: ambiguous";
792 } else if (is_number(name
)) {
794 for (jp
= jobtab
, i
= njobs
; --i
>= 0 ; jp
++) {
795 if (jp
->used
&& jp
->nprocs
> 0
796 && jp
->ps
[jp
->nprocs
- 1].pid
== pid
)
801 if (!jobs_invalid
&& jobno
>= 0 && jobno
< njobs
) {
807 error(err_msg
, name
);
814 * Return a new job structure,
818 makejob(union node
*node
, int nprocs
)
824 for (i
= njobs
, jp
= jobtab
; --i
>= 0 ; jp
++) {
831 for (i
= njobs
, jp
= jobtab
; ; jp
++) {
835 jobtab
= ckmalloc(4 * sizeof jobtab
[0]);
837 jp
= ckmalloc((njobs
+ 4) * sizeof jobtab
[0]);
838 memcpy(jp
, jobtab
, njobs
* sizeof jp
[0]);
839 /* Relocate `ps' pointers */
840 for (i
= 0; i
< njobs
; i
++)
841 if (jp
[i
].ps
== &jobtab
[i
].ps0
)
842 jp
[i
].ps
= &jp
[i
].ps0
;
847 for (i
= 4 ; --i
>= 0 ; )
848 jobtab
[njobs
++].used
= 0;
856 jp
->state
= JOBRUNNING
;
865 jp
->ps
= ckmalloc(nprocs
* sizeof (struct procstat
));
870 TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node
, nprocs
,
877 * Fork off a subshell. If we are doing job control, give the subshell its
878 * own process group. Jp is a job structure that the job is to be added to.
879 * N is the command that will be evaluated by the child. Both jp and n may
880 * be NULL. The mode parameter can be one of the following:
881 * FORK_FG - Fork off a foreground process.
882 * FORK_BG - Fork off a background process.
883 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
884 * process group even if job control is on.
886 * When job control is turned off, background processes have their standard
887 * input redirected to /dev/null (except for the second and later processes
892 forkshell(struct job
*jp
, union node
*n
, int mode
)
897 TRACE(("forkshell(%%%d, %p, %d) called\n", jp
- jobtab
, n
, mode
));
898 switch ((pid
= fork())) {
901 TRACE(("Fork failed, errno=%d\n", serrno
));
903 error("Cannot fork (%s)", strerror(serrno
));
906 forkchild(jp
, n
, mode
, 0);
909 return forkparent(jp
, n
, mode
, pid
);
914 forkparent(struct job
*jp
, union node
*n
, int mode
, pid_t pid
)
918 if (rootshell
&& mode
!= FORK_NOJOB
&& mflag
) {
919 if (jp
== NULL
|| jp
->nprocs
== 0)
922 pgrp
= jp
->ps
[0].pid
;
923 /* This can fail because we are doing it in the child also */
924 (void)setpgid(pid
, pgrp
);
927 backgndpid
= pid
; /* set $! */
929 struct procstat
*ps
= &jp
->ps
[jp
->nprocs
++];
933 if (/* iflag && rootshell && */ n
)
936 TRACE(("In parent shell: child = %d\n", pid
));
941 forkchild(struct job
*jp
, union node
*n
, int mode
, int vforked
)
944 #if JOBS /* LSC: for proper compilation with JOBS == 0 */
947 const char *devnull
= _PATH_DEVNULL
;
948 const char *nullerr
= "Can't open %s";
951 TRACE(("Child shell %d\n", getpid()));
955 closescript(vforked
);
956 clear_traps(vforked
);
959 jobctl
= 0; /* do job control only in root shell */
960 if (wasroot
&& mode
!= FORK_NOJOB
&& mflag
) {
961 if (jp
== NULL
|| jp
->nprocs
== 0)
964 pgrp
= jp
->ps
[0].pid
;
965 /* This can fail because we are doing it in the parent also */
966 (void)setpgid(0, pgrp
);
967 if (mode
== FORK_FG
) {
968 if (tcsetpgrp(ttyfd
, pgrp
) == -1)
969 error("Cannot set tty process group (%s) at %d",
970 strerror(errno
), __LINE__
);
972 setsignal(SIGTSTP
, vforked
);
973 setsignal(SIGTTOU
, vforked
);
974 } else if (mode
== FORK_BG
) {
975 ignoresig(SIGINT
, vforked
);
976 ignoresig(SIGQUIT
, vforked
);
977 if ((jp
== NULL
|| jp
->nprocs
== 0) &&
978 ! fd0_redirected_p ()) {
980 if (open(devnull
, O_RDONLY
) != 0)
981 error(nullerr
, devnull
);
985 if (mode
== FORK_BG
) {
986 ignoresig(SIGINT
, vforked
);
987 ignoresig(SIGQUIT
, vforked
);
988 if ((jp
== NULL
|| jp
->nprocs
== 0) &&
989 ! fd0_redirected_p ()) {
991 if (open(devnull
, O_RDONLY
) != 0)
992 error(nullerr
, devnull
);
996 if (wasroot
&& iflag
) {
997 setsignal(SIGINT
, vforked
);
998 setsignal(SIGQUIT
, vforked
);
999 setsignal(SIGTERM
, vforked
);
1007 * Wait for job to finish.
1009 * Under job control we have the problem that while a child process is
1010 * running interrupts generated by the user are sent to the child but not
1011 * to the shell. This means that an infinite loop started by an inter-
1012 * active user may be hard to kill. With job control turned off, an
1013 * interactive user may place an interactive program inside a loop. If
1014 * the interactive program catches interrupts, the user doesn't want
1015 * these interrupts to also abort the loop. The approach we take here
1016 * is to have the shell ignore interrupt signals while waiting for a
1017 * forground process to terminate, and then send itself an interrupt
1018 * signal if the child process was terminated by an interrupt signal.
1019 * Unfortunately, some programs want to do a bit of cleanup and then
1020 * exit on interrupt; unless these processes terminate themselves by
1021 * sending a signal to themselves (instead of calling exit) they will
1022 * confuse this approach.
1026 waitforjob(struct job
*jp
)
1029 int mypgrp
= getpgrp();
1035 TRACE(("waitforjob(%%%d) called\n", jp
- jobtab
+ 1));
1036 while (jp
->state
== JOBRUNNING
) {
1041 if (tcsetpgrp(ttyfd
, mypgrp
) == -1)
1042 error("Cannot set tty process group (%s) at %d",
1043 strerror(errno
), __LINE__
);
1045 if (jp
->state
== JOBSTOPPED
&& curjob
!= jp
- jobtab
)
1048 status
= jp
->ps
[jp
->nprocs
- 1].status
;
1049 /* convert to 8 bits */
1050 if (WIFEXITED(status
))
1051 st
= WEXITSTATUS(status
);
1053 else if (WIFSTOPPED(status
))
1054 st
= WSTOPSIG(status
) + 128;
1057 st
= WTERMSIG(status
) + 128;
1058 TRACE(("waitforjob: job %d, nproc %d, status %x, st %x\n",
1059 jp
- jobtab
+ 1, jp
->nprocs
, status
, st
));
1063 * This is truly gross.
1064 * If we're doing job control, then we did a TIOCSPGRP which
1065 * caused us (the shell) to no longer be in the controlling
1066 * session -- so we wouldn't have seen any ^C/SIGINT. So, we
1067 * intuit from the subprocess exit status whether a SIGINT
1068 * occurred, and if so interrupt ourselves. Yuck. - mycroft
1070 if (WIFSIGNALED(status
) && WTERMSIG(status
) == SIGINT
)
1074 if (! JOBS
|| jp
->state
== JOBDONE
)
1083 * Wait for a process to terminate.
1087 dowait(int flags
, struct job
*job
)
1091 struct procstat
*sp
;
1093 struct job
*thisjob
;
1097 TRACE(("dowait(%x) called\n", flags
));
1099 pid
= waitproc(flags
& WBLOCK
, job
, &status
);
1100 TRACE(("wait returns pid %d, status %d\n", pid
, status
));
1101 } while (pid
== -1 && errno
== EINTR
&& pendingsigs
== 0);
1106 for (jp
= jobtab
; jp
< jobtab
+ njobs
; jp
++) {
1110 for (sp
= jp
->ps
; sp
< jp
->ps
+ jp
->nprocs
; sp
++) {
1113 if (sp
->pid
== pid
) {
1114 TRACE(("Job %d: changing status of proc %d from 0x%x to 0x%x\n", jp
- jobtab
+ 1, pid
, sp
->status
, status
));
1115 sp
->status
= status
;
1118 if (sp
->status
== -1)
1120 else if (WIFSTOPPED(sp
->status
))
1123 if (stopped
) { /* stopped or done */
1124 int state
= done
? JOBDONE
: JOBSTOPPED
;
1125 if (jp
->state
!= state
) {
1126 TRACE(("Job %d: changing state from %d to %d\n", jp
- jobtab
+ 1, jp
->state
, state
));
1137 if (thisjob
&& thisjob
->state
!= JOBRUNNING
) {
1139 if (!rootshell
|| !iflag
)
1140 mode
= SHOW_SIGNALLED
;
1141 if ((job
== thisjob
&& (flags
& WNOFREE
) == 0) ||
1142 (job
!= thisjob
&& (flags
& WNOFREE
) != 0))
1143 mode
= SHOW_SIGNALLED
| SHOW_NO_FREE
;
1145 showjob(out2
, thisjob
, mode
);
1147 TRACE(("Not printing status, rootshell=%d, job=%p\n",
1149 thisjob
->changed
= 1;
1160 * Do a wait system call. If job control is compiled in, we accept
1161 * stopped processes. If block is zero, we return a value of zero
1162 * rather than blocking.
1164 * System V doesn't have a non-blocking wait system call. It does
1165 * have a SIGCLD signal that is sent to a process when one of its
1166 * children dies. The obvious way to use SIGCLD would be to install
1167 * a handler for SIGCLD which simply bumped a counter when a SIGCLD
1168 * was received, and have waitproc bump another counter when it got
1169 * the status of a process. Waitproc would then know that a wait
1170 * system call would not block if the two counters were different.
1171 * This approach doesn't work because if a process has children that
1172 * have not been waited for, System V will send it a SIGCLD when it
1173 * installs a signal handler for SIGCLD. What this means is that when
1174 * a child exits, the shell will be sent SIGCLD signals continuously
1175 * until is runs out of stack space, unless it does a wait call before
1176 * restoring the signal handler. The code below takes advantage of
1177 * this (mis)feature by installing a signal handler for SIGCLD and
1178 * then checking to see whether it was called. If there are any
1179 * children to be waited for, it will be.
1181 * If neither SYSV nor BSD is defined, we don't implement nonblocking
1182 * waits at all. In this case, the user will not be informed when
1183 * a background process until the next time she runs a real program
1184 * (as opposed to running a builtin command or just typing return),
1185 * and the jobs command may give out of date information.
1189 STATIC
int gotsigchild
;
1191 STATIC
int onsigchild() {
1198 waitproc(int block
, struct job
*jp
, int *status
)
1204 if (jp
!= NULL
&& jp
->jobctl
)
1209 return waitpid(-1, status
, flags
);
1216 save
= signal(SIGCLD
, onsigchild
);
1217 signal(SIGCLD
, save
);
1218 if (gotsigchild
== 0)
1221 return wait(status
);
1225 return wait(status
);
1231 * return 1 if there are stopped jobs, otherwise 0
1233 int job_warning
= 0;
1240 if (job_warning
|| jobs_invalid
)
1242 for (jobno
= 1, jp
= jobtab
; jobno
<= njobs
; jobno
++, jp
++) {
1245 if (jp
->state
== JOBSTOPPED
) {
1246 out2str("You have stopped jobs.\n");
1256 * Return a string identifying a command (to be printed by the
1260 STATIC
char *cmdnextc
;
1261 STATIC
int cmdnleft
;
1264 commandtext(struct procstat
*ps
, union node
*n
)
1269 if (iflag
|| mflag
|| sizeof ps
->cmd
< 100)
1270 len
= sizeof(ps
->cmd
);
1272 len
= sizeof(ps
->cmd
) / 10;
1275 if (cmdnleft
<= 0) {
1276 char *p
= ps
->cmd
+ len
- 4;
1283 TRACE(("commandtext: ps->cmd %x, end %x, left %d\n\t\"%s\"\n",
1284 ps
->cmd
, cmdnextc
, cmdnleft
, ps
->cmd
));
1289 cmdtxt(union node
*n
)
1292 struct nodelist
*lp
;
1297 if (n
== NULL
|| cmdnleft
<= 0)
1301 cmdtxt(n
->nbinary
.ch1
);
1303 cmdtxt(n
->nbinary
.ch2
);
1306 cmdtxt(n
->nbinary
.ch1
);
1308 cmdtxt(n
->nbinary
.ch2
);
1311 cmdtxt(n
->nbinary
.ch1
);
1313 cmdtxt(n
->nbinary
.ch2
);
1316 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
1324 cmdtxt(n
->nredir
.n
);
1329 cmdtxt(n
->nredir
.n
);
1333 cmdtxt(n
->nif
.test
);
1335 cmdtxt(n
->nif
.ifpart
);
1336 if (n
->nif
.elsepart
) {
1338 cmdtxt(n
->nif
.elsepart
);
1348 cmdtxt(n
->nbinary
.ch1
);
1350 cmdtxt(n
->nbinary
.ch2
);
1355 cmdputs(n
->nfor
.var
);
1357 cmdlist(n
->nfor
.args
, 1);
1359 cmdtxt(n
->nfor
.body
);
1364 cmdputs(n
->ncase
.expr
->narg
.text
);
1366 for (np
= n
->ncase
.cases
; np
; np
= np
->nclist
.next
) {
1367 cmdtxt(np
->nclist
.pattern
);
1369 cmdtxt(np
->nclist
.body
);
1375 cmdputs(n
->narg
.text
);
1376 cmdputs("() { ... }");
1379 cmdlist(n
->ncmd
.args
, 1);
1380 cmdlist(n
->ncmd
.redirect
, 0);
1383 cmdputs(n
->narg
.text
);
1386 p
= ">"; i
= 1; goto redir
;
1388 p
= ">|"; i
= 1; goto redir
;
1390 p
= ">>"; i
= 1; goto redir
;
1392 p
= ">&"; i
= 1; goto redir
;
1394 p
= "<"; i
= 0; goto redir
;
1396 p
= "<&"; i
= 0; goto redir
;
1398 p
= "<>"; i
= 0; goto redir
;
1400 if (n
->nfile
.fd
!= i
) {
1401 s
[0] = n
->nfile
.fd
+ '0';
1406 if (n
->type
== NTOFD
|| n
->type
== NFROMFD
) {
1407 s
[0] = n
->ndup
.dupfd
+ '0';
1411 cmdtxt(n
->nfile
.fname
);
1425 cmdlist(union node
*np
, int sep
)
1427 for (; np
; np
= np
->narg
.next
) {
1431 if (sep
&& np
->narg
.next
)
1438 cmdputs(const char *s
)
1440 const char *p
, *str
= 0;
1441 char c
, cc
[2] = " ";
1446 static char vstype
[16][4] = { "", "}", "-", "+", "?", "=",
1447 "#", "##", "%", "%%" };
1452 while (nleft
> 0 && (c
= *p
++) != 0) {
1459 if ((subtype
& VSTYPE
) == VSLENGTH
)
1463 if (!(subtype
& VSQUOTE
) != !(quoted
& 1)) {
1482 case CTLBACKQ
+CTLQUOTE
:
1501 str
= vstype
[subtype
& VSTYPE
];
1502 if (subtype
& VSNUL
)
1513 /* These can only happen inside quotes */
1523 } while (--nleft
> 0 && str
&& (c
= *str
++));
1526 if ((quoted
& 1) && nleft
) {