147121Sbostic /*-
260698Sbostic * Copyright (c) 1991, 1993
360698Sbostic * The Regents of the University of California. All rights reserved.
447121Sbostic *
547121Sbostic * This code is derived from software contributed to Berkeley by
647121Sbostic * Kenneth Almquist.
747121Sbostic *
868934Sbostic * %sccs.include.redist.c%
947121Sbostic */
1047121Sbostic
1147121Sbostic #ifndef lint
12*69272Schristos static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 05/04/95";
1347121Sbostic #endif /* not lint */
1447121Sbostic
15*69272Schristos #include <fcntl.h>
16*69272Schristos #include <signal.h>
17*69272Schristos #include <errno.h>
18*69272Schristos #include <unistd.h>
19*69272Schristos #include <stdlib.h>
20*69272Schristos #include <sys/types.h>
21*69272Schristos #include <sys/param.h>
22*69272Schristos #ifdef BSD
23*69272Schristos #include <sys/wait.h>
24*69272Schristos #include <sys/time.h>
25*69272Schristos #include <sys/resource.h>
26*69272Schristos #endif
27*69272Schristos
2847121Sbostic #include "shell.h"
2947121Sbostic #if JOBS
3047121Sbostic #include "sgtty.h"
3147121Sbostic #undef CEOF /* syntax.h redefines this */
3247121Sbostic #endif
33*69272Schristos #include "redir.h"
34*69272Schristos #include "show.h"
3547121Sbostic #include "main.h"
3647121Sbostic #include "parser.h"
3747121Sbostic #include "nodes.h"
3847121Sbostic #include "jobs.h"
3947121Sbostic #include "options.h"
4047121Sbostic #include "trap.h"
4147121Sbostic #include "syntax.h"
4247121Sbostic #include "input.h"
4347121Sbostic #include "output.h"
4447121Sbostic #include "memalloc.h"
4547121Sbostic #include "error.h"
4647121Sbostic #include "mystring.h"
4747121Sbostic
4847121Sbostic
4947121Sbostic struct job *jobtab; /* array of jobs */
5047121Sbostic int njobs; /* size of array */
5147121Sbostic MKINIT short backgndpid = -1; /* pid of last background process */
5247121Sbostic #if JOBS
5347121Sbostic int initialpgrp; /* pgrp of shell on invocation */
5447121Sbostic short curjob; /* current job */
5547121Sbostic #endif
5647121Sbostic
57*69272Schristos STATIC void restartjob __P((struct job *));
58*69272Schristos STATIC void freejob __P((struct job *));
59*69272Schristos STATIC struct job *getjob __P((char *));
60*69272Schristos STATIC int dowait __P((int, struct job *));
61*69272Schristos STATIC int onsigchild __P((void));
62*69272Schristos STATIC int waitproc __P((int, int *));
63*69272Schristos STATIC void cmdtxt __P((union node *));
64*69272Schristos STATIC void cmdputs __P((char *));
6547121Sbostic
6647121Sbostic
6747121Sbostic /*
6847121Sbostic * Turn job control on and off.
6947121Sbostic *
7047121Sbostic * Note: This code assumes that the third arg to ioctl is a character
7147121Sbostic * pointer, which is true on Berkeley systems but not System V. Since
7247121Sbostic * System V doesn't have job control yet, this isn't a problem now.
7347121Sbostic */
7447121Sbostic
7547121Sbostic MKINIT int jobctl;
7647121Sbostic
7747121Sbostic void
setjobctl(on)78*69272Schristos setjobctl(on)
79*69272Schristos int on;
80*69272Schristos {
8156563Smarc #ifdef OLD_TTY_DRIVER
8247121Sbostic int ldisc;
8356563Smarc #endif
8447121Sbostic
8547121Sbostic if (on == jobctl || rootshell == 0)
8647121Sbostic return;
8747121Sbostic if (on) {
8847121Sbostic do { /* while we are in the background */
8947121Sbostic if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
9055230Smarc out2str("sh: can't access tty; job control turned off\n");
9155230Smarc mflag = 0;
9247121Sbostic return;
9347121Sbostic }
9447121Sbostic if (initialpgrp == -1)
9568934Sbostic initialpgrp = getpgrp();
9668934Sbostic else if (initialpgrp != getpgrp()) {
9747121Sbostic killpg(initialpgrp, SIGTTIN);
9847121Sbostic continue;
9947121Sbostic }
10047121Sbostic } while (0);
10155230Smarc #ifdef OLD_TTY_DRIVER
10247121Sbostic if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
10355230Smarc out2str("sh: need new tty driver to run job control; job control turned off\n");
10455230Smarc mflag = 0;
10547121Sbostic return;
10647121Sbostic }
10755230Smarc #endif
10847121Sbostic setsignal(SIGTSTP);
10947121Sbostic setsignal(SIGTTOU);
11054318Smarc setsignal(SIGTTIN);
11168989Sbostic setpgid(0, rootpid);
11247121Sbostic ioctl(2, TIOCSPGRP, (char *)&rootpid);
11347121Sbostic } else { /* turning job control off */
11468989Sbostic setpgid(0, initialpgrp);
11547121Sbostic ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
11647121Sbostic setsignal(SIGTSTP);
11747121Sbostic setsignal(SIGTTOU);
11854318Smarc setsignal(SIGTTIN);
11947121Sbostic }
12047121Sbostic jobctl = on;
12147121Sbostic }
12247121Sbostic
12347121Sbostic
12447121Sbostic #ifdef mkinit
125*69272Schristos INCLUDE <stdlib.h>
12647121Sbostic
12747121Sbostic SHELLPROC {
12847121Sbostic backgndpid = -1;
12947121Sbostic #if JOBS
13047121Sbostic jobctl = 0;
13147121Sbostic #endif
13247121Sbostic }
13347121Sbostic
13447121Sbostic #endif
13547121Sbostic
13647121Sbostic
13747121Sbostic
13847121Sbostic #if JOBS
139*69272Schristos int
fgcmd(argc,argv)140*69272Schristos fgcmd(argc, argv)
141*69272Schristos int argc;
142*69272Schristos char **argv;
143*69272Schristos {
14447121Sbostic struct job *jp;
14547121Sbostic int pgrp;
14647121Sbostic int status;
14747121Sbostic
14847121Sbostic jp = getjob(argv[1]);
14947121Sbostic if (jp->jobctl == 0)
15047121Sbostic error("job not created under job control");
15147121Sbostic pgrp = jp->ps[0].pid;
15247121Sbostic ioctl(2, TIOCSPGRP, (char *)&pgrp);
15347121Sbostic restartjob(jp);
15447121Sbostic INTOFF;
15547121Sbostic status = waitforjob(jp);
15647121Sbostic INTON;
15747121Sbostic return status;
15847121Sbostic }
15947121Sbostic
16047121Sbostic
161*69272Schristos int
bgcmd(argc,argv)162*69272Schristos bgcmd(argc, argv)
163*69272Schristos int argc;
164*69272Schristos char **argv;
165*69272Schristos {
16647121Sbostic struct job *jp;
16747121Sbostic
16847121Sbostic do {
16947121Sbostic jp = getjob(*++argv);
17047121Sbostic if (jp->jobctl == 0)
17147121Sbostic error("job not created under job control");
17247121Sbostic restartjob(jp);
17347121Sbostic } while (--argc > 1);
17447121Sbostic return 0;
17547121Sbostic }
17647121Sbostic
17747121Sbostic
17847121Sbostic STATIC void
restartjob(jp)17947121Sbostic restartjob(jp)
18047121Sbostic struct job *jp;
181*69272Schristos {
18247121Sbostic struct procstat *ps;
18347121Sbostic int i;
18447121Sbostic
18547121Sbostic if (jp->state == JOBDONE)
18647121Sbostic return;
18747121Sbostic INTOFF;
18847121Sbostic killpg(jp->ps[0].pid, SIGCONT);
18947121Sbostic for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
19047121Sbostic if ((ps->status & 0377) == 0177) {
19147121Sbostic ps->status = -1;
19247121Sbostic jp->state = 0;
19347121Sbostic }
19447121Sbostic }
19547121Sbostic INTON;
19647121Sbostic }
19747121Sbostic #endif
19847121Sbostic
19947121Sbostic
20047121Sbostic int
jobscmd(argc,argv)201*69272Schristos jobscmd(argc, argv)
202*69272Schristos int argc;
203*69272Schristos char **argv;
204*69272Schristos {
20547121Sbostic showjobs(0);
20647121Sbostic return 0;
20747121Sbostic }
20847121Sbostic
20947121Sbostic
21047121Sbostic /*
21147121Sbostic * Print a list of jobs. If "change" is nonzero, only print jobs whose
21247121Sbostic * statuses have changed since the last call to showjobs.
21347121Sbostic *
21447121Sbostic * If the shell is interrupted in the process of creating a job, the
21547121Sbostic * result may be a job structure containing zero processes. Such structures
21647121Sbostic * will be freed here.
21747121Sbostic */
21847121Sbostic
21947121Sbostic void
showjobs(change)220*69272Schristos showjobs(change)
221*69272Schristos int change;
222*69272Schristos {
22347121Sbostic int jobno;
22447121Sbostic int procno;
22547121Sbostic int i;
22647121Sbostic struct job *jp;
22747121Sbostic struct procstat *ps;
22847121Sbostic int col;
22947121Sbostic char s[64];
23047121Sbostic
23147121Sbostic TRACE(("showjobs(%d) called\n", change));
23247121Sbostic while (dowait(0, (struct job *)NULL) > 0);
23347121Sbostic for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
23447121Sbostic if (! jp->used)
23547121Sbostic continue;
23647121Sbostic if (jp->nprocs == 0) {
23747121Sbostic freejob(jp);
23847121Sbostic continue;
23947121Sbostic }
24047121Sbostic if (change && ! jp->changed)
24147121Sbostic continue;
24247121Sbostic procno = jp->nprocs;
24347121Sbostic for (ps = jp->ps ; ; ps++) { /* for each process */
24447121Sbostic if (ps == jp->ps)
24547121Sbostic fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
24647121Sbostic else
24747121Sbostic fmtstr(s, 64, " %d ", ps->pid);
24847121Sbostic out1str(s);
24947121Sbostic col = strlen(s);
25047121Sbostic s[0] = '\0';
25147121Sbostic if (ps->status == -1) {
25247121Sbostic /* don't print anything */
25347121Sbostic } else if ((ps->status & 0xFF) == 0) {
25447121Sbostic fmtstr(s, 64, "Exit %d", ps->status >> 8);
25547121Sbostic } else {
25647121Sbostic i = ps->status;
25747121Sbostic #if JOBS
25847121Sbostic if ((i & 0xFF) == 0177)
25947121Sbostic i >>= 8;
26047121Sbostic #endif
26168928Sbostic if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
26268928Sbostic scopy(sys_siglist[i & 0x7F], s);
26347121Sbostic else
26447121Sbostic fmtstr(s, 64, "Signal %d", i & 0x7F);
26547121Sbostic if (i & 0x80)
26647121Sbostic strcat(s, " (core dumped)");
26747121Sbostic }
26847121Sbostic out1str(s);
26947121Sbostic col += strlen(s);
27047121Sbostic do {
27147121Sbostic out1c(' ');
27247121Sbostic col++;
27347121Sbostic } while (col < 30);
27447121Sbostic out1str(ps->cmd);
27547121Sbostic out1c('\n');
27647121Sbostic if (--procno <= 0)
27747121Sbostic break;
27847121Sbostic }
27947121Sbostic jp->changed = 0;
28047121Sbostic if (jp->state == JOBDONE) {
28147121Sbostic freejob(jp);
28247121Sbostic }
28347121Sbostic }
28447121Sbostic }
28547121Sbostic
28647121Sbostic
28747121Sbostic /*
28847121Sbostic * Mark a job structure as unused.
28947121Sbostic */
29047121Sbostic
29147121Sbostic STATIC void
freejob(jp)29247121Sbostic freejob(jp)
29347121Sbostic struct job *jp;
29447121Sbostic {
29547121Sbostic struct procstat *ps;
29647121Sbostic int i;
29747121Sbostic
29847121Sbostic INTOFF;
29947121Sbostic for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
30047121Sbostic if (ps->cmd != nullstr)
30147121Sbostic ckfree(ps->cmd);
30247121Sbostic }
30347121Sbostic if (jp->ps != &jp->ps0)
30447121Sbostic ckfree(jp->ps);
30547121Sbostic jp->used = 0;
30647121Sbostic #if JOBS
30747121Sbostic if (curjob == jp - jobtab + 1)
30847121Sbostic curjob = 0;
30947121Sbostic #endif
31047121Sbostic INTON;
31147121Sbostic }
31247121Sbostic
31347121Sbostic
31447121Sbostic
31547121Sbostic int
waitcmd(argc,argv)316*69272Schristos waitcmd(argc, argv)
317*69272Schristos int argc;
318*69272Schristos char **argv;
319*69272Schristos {
32047121Sbostic struct job *job;
32147121Sbostic int status;
32247121Sbostic struct job *jp;
32347121Sbostic
32447121Sbostic if (argc > 1) {
32547121Sbostic job = getjob(argv[1]);
32647121Sbostic } else {
32747121Sbostic job = NULL;
32847121Sbostic }
32947121Sbostic for (;;) { /* loop until process terminated or stopped */
33047121Sbostic if (job != NULL) {
33147121Sbostic if (job->state) {
33247121Sbostic status = job->ps[job->nprocs - 1].status;
33347121Sbostic if ((status & 0xFF) == 0)
33447121Sbostic status = status >> 8 & 0xFF;
33547121Sbostic #if JOBS
33647121Sbostic else if ((status & 0xFF) == 0177)
33747121Sbostic status = (status >> 8 & 0x7F) + 128;
33847121Sbostic #endif
33947121Sbostic else
34047121Sbostic status = (status & 0x7F) + 128;
34147121Sbostic if (! iflag)
34247121Sbostic freejob(job);
34347121Sbostic return status;
34447121Sbostic }
34547121Sbostic } else {
34647121Sbostic for (jp = jobtab ; ; jp++) {
34747121Sbostic if (jp >= jobtab + njobs) { /* no running procs */
34847121Sbostic return 0;
34947121Sbostic }
35047121Sbostic if (jp->used && jp->state == 0)
35147121Sbostic break;
35247121Sbostic }
35347121Sbostic }
35447121Sbostic dowait(1, (struct job *)NULL);
35547121Sbostic }
35647121Sbostic }
35747121Sbostic
35847121Sbostic
35947121Sbostic
360*69272Schristos int
jobidcmd(argc,argv)361*69272Schristos jobidcmd(argc, argv)
362*69272Schristos int argc;
363*69272Schristos char **argv;
364*69272Schristos {
36547121Sbostic struct job *jp;
36647121Sbostic int i;
36747121Sbostic
36847121Sbostic jp = getjob(argv[1]);
36947121Sbostic for (i = 0 ; i < jp->nprocs ; ) {
37047121Sbostic out1fmt("%d", jp->ps[i].pid);
37147121Sbostic out1c(++i < jp->nprocs? ' ' : '\n');
37247121Sbostic }
37347121Sbostic return 0;
37447121Sbostic }
37547121Sbostic
37647121Sbostic
37747121Sbostic
37847121Sbostic /*
37947121Sbostic * Convert a job name to a job structure.
38047121Sbostic */
38147121Sbostic
38247121Sbostic STATIC struct job *
getjob(name)38347121Sbostic getjob(name)
38447121Sbostic char *name;
38547121Sbostic {
38647121Sbostic int jobno;
38747121Sbostic register struct job *jp;
38847121Sbostic int pid;
38947121Sbostic int i;
39047121Sbostic
39147121Sbostic if (name == NULL) {
39247121Sbostic #if JOBS
39347121Sbostic currentjob:
39447121Sbostic if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
39547121Sbostic error("No current job");
39647121Sbostic return &jobtab[jobno - 1];
39747121Sbostic #else
39847121Sbostic error("No current job");
39947121Sbostic #endif
40047121Sbostic } else if (name[0] == '%') {
40147121Sbostic if (is_digit(name[1])) {
40247121Sbostic jobno = number(name + 1);
40347121Sbostic if (jobno > 0 && jobno <= njobs
40447121Sbostic && jobtab[jobno - 1].used != 0)
40547121Sbostic return &jobtab[jobno - 1];
40647121Sbostic #if JOBS
40747121Sbostic } else if (name[1] == '%' && name[2] == '\0') {
40847121Sbostic goto currentjob;
40947121Sbostic #endif
41047121Sbostic } else {
41147121Sbostic register struct job *found = NULL;
41247121Sbostic for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
41347121Sbostic if (jp->used && jp->nprocs > 0
41447121Sbostic && prefix(name + 1, jp->ps[0].cmd)) {
41547121Sbostic if (found)
41647121Sbostic error("%s: ambiguous", name);
41747121Sbostic found = jp;
41847121Sbostic }
41947121Sbostic }
42047121Sbostic if (found)
42147121Sbostic return found;
42247121Sbostic }
42347121Sbostic } else if (is_number(name)) {
42447121Sbostic pid = number(name);
42547121Sbostic for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
42647121Sbostic if (jp->used && jp->nprocs > 0
42747121Sbostic && jp->ps[jp->nprocs - 1].pid == pid)
42847121Sbostic return jp;
42947121Sbostic }
43047121Sbostic }
43147121Sbostic error("No such job: %s", name);
432*69272Schristos /*NOTREACHED*/
433*69272Schristos return NULL;
43447121Sbostic }
43547121Sbostic
43647121Sbostic
43747121Sbostic
43847121Sbostic /*
43947121Sbostic * Return a new job structure,
44047121Sbostic */
44147121Sbostic
44247121Sbostic struct job *
makejob(node,nprocs)44347121Sbostic makejob(node, nprocs)
44447121Sbostic union node *node;
445*69272Schristos int nprocs;
446*69272Schristos {
44747121Sbostic int i;
44847121Sbostic struct job *jp;
44947121Sbostic
45047121Sbostic for (i = njobs, jp = jobtab ; ; jp++) {
45147121Sbostic if (--i < 0) {
45247121Sbostic INTOFF;
45347121Sbostic if (njobs == 0) {
45447121Sbostic jobtab = ckmalloc(4 * sizeof jobtab[0]);
45547121Sbostic } else {
45647121Sbostic jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
457*69272Schristos memcpy(jp, jobtab, njobs * sizeof jp[0]);
45847121Sbostic ckfree(jobtab);
45947121Sbostic jobtab = jp;
46047121Sbostic }
46147121Sbostic jp = jobtab + njobs;
46247121Sbostic for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
46347121Sbostic INTON;
46447121Sbostic break;
46547121Sbostic }
46647121Sbostic if (jp->used == 0)
46747121Sbostic break;
46847121Sbostic }
46947121Sbostic INTOFF;
47047121Sbostic jp->state = 0;
47147121Sbostic jp->used = 1;
47247121Sbostic jp->changed = 0;
47347121Sbostic jp->nprocs = 0;
47447121Sbostic #if JOBS
47547121Sbostic jp->jobctl = jobctl;
47647121Sbostic #endif
47747121Sbostic if (nprocs > 1) {
47847121Sbostic jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
47947121Sbostic } else {
48047121Sbostic jp->ps = &jp->ps0;
48147121Sbostic }
48247121Sbostic INTON;
483*69272Schristos TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
484*69272Schristos jp - jobtab + 1));
48547121Sbostic return jp;
48647121Sbostic }
48747121Sbostic
48847121Sbostic
48947121Sbostic /*
49047121Sbostic * Fork of a subshell. If we are doing job control, give the subshell its
49147121Sbostic * own process group. Jp is a job structure that the job is to be added to.
49247121Sbostic * N is the command that will be evaluated by the child. Both jp and n may
49347121Sbostic * be NULL. The mode parameter can be one of the following:
49447121Sbostic * FORK_FG - Fork off a foreground process.
49547121Sbostic * FORK_BG - Fork off a background process.
49647121Sbostic * FORK_NOJOB - Like FORK_FG, but don't give the process its own
49747121Sbostic * process group even if job control is on.
49847121Sbostic *
49947121Sbostic * When job control is turned off, background processes have their standard
50047121Sbostic * input redirected to /dev/null (except for the second and later processes
50147121Sbostic * in a pipeline).
50247121Sbostic */
50347121Sbostic
50447121Sbostic int
forkshell(jp,n,mode)50547121Sbostic forkshell(jp, n, mode)
50647121Sbostic union node *n;
50747121Sbostic struct job *jp;
508*69272Schristos int mode;
509*69272Schristos {
51047121Sbostic int pid;
51147121Sbostic int pgrp;
51247121Sbostic
513*69272Schristos TRACE(("forkshell(%%%d, 0x%lx, %d) called\n", jp - jobtab, (long)n,
514*69272Schristos mode));
51547121Sbostic INTOFF;
51647121Sbostic pid = fork();
51747121Sbostic if (pid == -1) {
51847121Sbostic TRACE(("Fork failed, errno=%d\n", errno));
51947121Sbostic INTON;
52047121Sbostic error("Cannot fork");
52147121Sbostic }
52247121Sbostic if (pid == 0) {
52347121Sbostic struct job *p;
52447121Sbostic int wasroot;
52547121Sbostic int i;
52647121Sbostic
52747121Sbostic TRACE(("Child shell %d\n", getpid()));
52847121Sbostic wasroot = rootshell;
52947121Sbostic rootshell = 0;
53047121Sbostic for (i = njobs, p = jobtab ; --i >= 0 ; p++)
53147121Sbostic if (p->used)
53247121Sbostic freejob(p);
53347121Sbostic closescript();
53447121Sbostic INTON;
53547121Sbostic clear_traps();
53647121Sbostic #if JOBS
53747121Sbostic jobctl = 0; /* do job control only in root shell */
53855230Smarc if (wasroot && mode != FORK_NOJOB && mflag) {
53947121Sbostic if (jp == NULL || jp->nprocs == 0)
54047121Sbostic pgrp = getpid();
54147121Sbostic else
54247121Sbostic pgrp = jp->ps[0].pid;
54368989Sbostic setpgid(0, pgrp);
54447121Sbostic if (mode == FORK_FG) {
54547121Sbostic /*** this causes superfluous TIOCSPGRPS ***/
54647121Sbostic if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
54747121Sbostic error("TIOCSPGRP failed, errno=%d\n", errno);
54847121Sbostic }
54947121Sbostic setsignal(SIGTSTP);
55047121Sbostic setsignal(SIGTTOU);
55147121Sbostic } else if (mode == FORK_BG) {
55247121Sbostic ignoresig(SIGINT);
55347121Sbostic ignoresig(SIGQUIT);
55460296Smarc if ((jp == NULL || jp->nprocs == 0) &&
55560296Smarc ! fd0_redirected_p ()) {
55647121Sbostic close(0);
55747121Sbostic if (open("/dev/null", O_RDONLY) != 0)
55847121Sbostic error("Can't open /dev/null");
55947121Sbostic }
56047121Sbostic }
56147121Sbostic #else
56247121Sbostic if (mode == FORK_BG) {
56347121Sbostic ignoresig(SIGINT);
56447121Sbostic ignoresig(SIGQUIT);
56560296Smarc if ((jp == NULL || jp->nprocs == 0) &&
56660296Smarc ! fd0_redirected_p ()) {
56747121Sbostic close(0);
56847121Sbostic if (open("/dev/null", O_RDONLY) != 0)
56947121Sbostic error("Can't open /dev/null");
57047121Sbostic }
57147121Sbostic }
57247121Sbostic #endif
57347121Sbostic if (wasroot && iflag) {
57447121Sbostic setsignal(SIGINT);
57547121Sbostic setsignal(SIGQUIT);
57647121Sbostic setsignal(SIGTERM);
57747121Sbostic }
57847121Sbostic return pid;
57947121Sbostic }
58055230Smarc if (rootshell && mode != FORK_NOJOB && mflag) {
58147121Sbostic if (jp == NULL || jp->nprocs == 0)
58247121Sbostic pgrp = pid;
58347121Sbostic else
58447121Sbostic pgrp = jp->ps[0].pid;
58568989Sbostic setpgid(pid, pgrp);
58647121Sbostic }
58747121Sbostic if (mode == FORK_BG)
58847121Sbostic backgndpid = pid; /* set $! */
58947121Sbostic if (jp) {
59047121Sbostic struct procstat *ps = &jp->ps[jp->nprocs++];
59147121Sbostic ps->pid = pid;
59247121Sbostic ps->status = -1;
59347121Sbostic ps->cmd = nullstr;
59447121Sbostic if (iflag && rootshell && n)
59547121Sbostic ps->cmd = commandtext(n);
59647121Sbostic }
59747121Sbostic INTON;
59847121Sbostic TRACE(("In parent shell: child = %d\n", pid));
59947121Sbostic return pid;
60047121Sbostic }
60147121Sbostic
60247121Sbostic
60347121Sbostic
60447121Sbostic /*
60547121Sbostic * Wait for job to finish.
60647121Sbostic *
60747121Sbostic * Under job control we have the problem that while a child process is
60847121Sbostic * running interrupts generated by the user are sent to the child but not
60947121Sbostic * to the shell. This means that an infinite loop started by an inter-
61047121Sbostic * active user may be hard to kill. With job control turned off, an
61147121Sbostic * interactive user may place an interactive program inside a loop. If
61247121Sbostic * the interactive program catches interrupts, the user doesn't want
61347121Sbostic * these interrupts to also abort the loop. The approach we take here
61447121Sbostic * is to have the shell ignore interrupt signals while waiting for a
61547121Sbostic * forground process to terminate, and then send itself an interrupt
61647121Sbostic * signal if the child process was terminated by an interrupt signal.
61747121Sbostic * Unfortunately, some programs want to do a bit of cleanup and then
61847121Sbostic * exit on interrupt; unless these processes terminate themselves by
61947121Sbostic * sending a signal to themselves (instead of calling exit) they will
62047121Sbostic * confuse this approach.
62147121Sbostic */
62247121Sbostic
62347121Sbostic int
waitforjob(jp)62447121Sbostic waitforjob(jp)
62547121Sbostic register struct job *jp;
62647121Sbostic {
62747121Sbostic #if JOBS
62868934Sbostic int mypgrp = getpgrp();
62947121Sbostic #endif
63047121Sbostic int status;
63147121Sbostic int st;
63247121Sbostic
63347121Sbostic INTOFF;
63447121Sbostic TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
63547121Sbostic while (jp->state == 0) {
63647121Sbostic dowait(1, jp);
63747121Sbostic }
63847121Sbostic #if JOBS
63947121Sbostic if (jp->jobctl) {
64047121Sbostic if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
64147121Sbostic error("TIOCSPGRP failed, errno=%d\n", errno);
64247121Sbostic }
64347121Sbostic if (jp->state == JOBSTOPPED)
64447121Sbostic curjob = jp - jobtab + 1;
64547121Sbostic #endif
64647121Sbostic status = jp->ps[jp->nprocs - 1].status;
64747121Sbostic /* convert to 8 bits */
64847121Sbostic if ((status & 0xFF) == 0)
64947121Sbostic st = status >> 8 & 0xFF;
65047121Sbostic #if JOBS
65147121Sbostic else if ((status & 0xFF) == 0177)
65247121Sbostic st = (status >> 8 & 0x7F) + 128;
65347121Sbostic #endif
65447121Sbostic else
65547121Sbostic st = (status & 0x7F) + 128;
65647121Sbostic if (! JOBS || jp->state == JOBDONE)
65747121Sbostic freejob(jp);
65847121Sbostic CLEAR_PENDING_INT;
65947121Sbostic if ((status & 0x7F) == SIGINT)
66047121Sbostic kill(getpid(), SIGINT);
66147121Sbostic INTON;
66247121Sbostic return st;
66347121Sbostic }
66447121Sbostic
66547121Sbostic
66647121Sbostic
66747121Sbostic /*
66847121Sbostic * Wait for a process to terminate.
66947121Sbostic */
67047121Sbostic
67147121Sbostic STATIC int
dowait(block,job)67247121Sbostic dowait(block, job)
673*69272Schristos int block;
67447121Sbostic struct job *job;
675*69272Schristos {
67647121Sbostic int pid;
67747121Sbostic int status;
67847121Sbostic struct procstat *sp;
67947121Sbostic struct job *jp;
68047121Sbostic struct job *thisjob;
68147121Sbostic int done;
68247121Sbostic int stopped;
68347121Sbostic int core;
68447121Sbostic
68547121Sbostic TRACE(("dowait(%d) called\n", block));
68647121Sbostic do {
68747121Sbostic pid = waitproc(block, &status);
68847121Sbostic TRACE(("wait returns %d, status=%d\n", pid, status));
68947121Sbostic } while (pid == -1 && errno == EINTR);
69047121Sbostic if (pid <= 0)
69147121Sbostic return pid;
69247121Sbostic INTOFF;
69347121Sbostic thisjob = NULL;
69447121Sbostic for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
69547121Sbostic if (jp->used) {
69647121Sbostic done = 1;
69747121Sbostic stopped = 1;
69847121Sbostic for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
69947121Sbostic if (sp->pid == -1)
70047121Sbostic continue;
70147121Sbostic if (sp->pid == pid) {
70247121Sbostic TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status));
70347121Sbostic sp->status = status;
70447121Sbostic thisjob = jp;
70547121Sbostic }
70647121Sbostic if (sp->status == -1)
70747121Sbostic stopped = 0;
70847121Sbostic else if ((sp->status & 0377) == 0177)
70947121Sbostic done = 0;
71047121Sbostic }
71147121Sbostic if (stopped) { /* stopped or done */
71247121Sbostic int state = done? JOBDONE : JOBSTOPPED;
71347121Sbostic if (jp->state != state) {
71447121Sbostic TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
71547121Sbostic jp->state = state;
71647121Sbostic #if JOBS
71747121Sbostic if (done && curjob == jp - jobtab + 1)
71847121Sbostic curjob = 0; /* no current job */
71947121Sbostic #endif
72047121Sbostic }
72147121Sbostic }
72247121Sbostic }
72347121Sbostic }
72447121Sbostic INTON;
72547121Sbostic if (! rootshell || ! iflag || (job && thisjob == job)) {
72647121Sbostic #if JOBS
72747121Sbostic if ((status & 0xFF) == 0177)
72847121Sbostic status >>= 8;
72947121Sbostic #endif
73047121Sbostic core = status & 0x80;
73147121Sbostic status &= 0x7F;
73247121Sbostic if (status != 0 && status != SIGINT && status != SIGPIPE) {
73347121Sbostic if (thisjob != job)
73447121Sbostic outfmt(out2, "%d: ", pid);
73547121Sbostic #if JOBS
73647121Sbostic if (status == SIGTSTP && rootshell && iflag)
73747121Sbostic outfmt(out2, "%%%d ", job - jobtab + 1);
73847121Sbostic #endif
73968928Sbostic if (status < NSIG && sys_siglist[status])
74068928Sbostic out2str(sys_siglist[status]);
74147121Sbostic else
74247121Sbostic outfmt(out2, "Signal %d", status);
74347121Sbostic if (core)
74447121Sbostic out2str(" - core dumped");
74547121Sbostic out2c('\n');
74647121Sbostic flushout(&errout);
74747121Sbostic } else {
74847121Sbostic TRACE(("Not printing status: status=%d\n", status));
74947121Sbostic }
75047121Sbostic } else {
75147121Sbostic TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
75247121Sbostic if (thisjob)
75347121Sbostic thisjob->changed = 1;
75447121Sbostic }
75547121Sbostic return pid;
75647121Sbostic }
75747121Sbostic
75847121Sbostic
75947121Sbostic
76047121Sbostic /*
76147121Sbostic * Do a wait system call. If job control is compiled in, we accept
76247121Sbostic * stopped processes. If block is zero, we return a value of zero
76347121Sbostic * rather than blocking.
76447121Sbostic *
76547121Sbostic * System V doesn't have a non-blocking wait system call. It does
76647121Sbostic * have a SIGCLD signal that is sent to a process when one of it's
76747121Sbostic * children dies. The obvious way to use SIGCLD would be to install
76847121Sbostic * a handler for SIGCLD which simply bumped a counter when a SIGCLD
76947121Sbostic * was received, and have waitproc bump another counter when it got
77047121Sbostic * the status of a process. Waitproc would then know that a wait
77147121Sbostic * system call would not block if the two counters were different.
77247121Sbostic * This approach doesn't work because if a process has children that
77347121Sbostic * have not been waited for, System V will send it a SIGCLD when it
77447121Sbostic * installs a signal handler for SIGCLD. What this means is that when
77547121Sbostic * a child exits, the shell will be sent SIGCLD signals continuously
77647121Sbostic * until is runs out of stack space, unless it does a wait call before
77747121Sbostic * restoring the signal handler. The code below takes advantage of
77847121Sbostic * this (mis)feature by installing a signal handler for SIGCLD and
77947121Sbostic * then checking to see whether it was called. If there are any
78047121Sbostic * children to be waited for, it will be.
78147121Sbostic *
78247121Sbostic * If neither SYSV nor BSD is defined, we don't implement nonblocking
78347121Sbostic * waits at all. In this case, the user will not be informed when
78447121Sbostic * a background process until the next time she runs a real program
78547121Sbostic * (as opposed to running a builtin command or just typing return),
78647121Sbostic * and the jobs command may give out of date information.
78747121Sbostic */
78847121Sbostic
78947121Sbostic #ifdef SYSV
79047121Sbostic STATIC int gotsigchild;
79147121Sbostic
onsigchild()79247121Sbostic STATIC int onsigchild() {
79347121Sbostic gotsigchild = 1;
79447121Sbostic }
79547121Sbostic #endif
79647121Sbostic
79747121Sbostic
79847121Sbostic STATIC int
waitproc(block,status)79947121Sbostic waitproc(block, status)
800*69272Schristos int block;
80147121Sbostic int *status;
802*69272Schristos {
80347121Sbostic #ifdef BSD
80447121Sbostic int flags;
80547121Sbostic
80647121Sbostic #if JOBS
80747121Sbostic flags = WUNTRACED;
80847121Sbostic #else
80947121Sbostic flags = 0;
81047121Sbostic #endif
81147121Sbostic if (block == 0)
81247121Sbostic flags |= WNOHANG;
81355230Smarc return wait3(status, flags, (struct rusage *)NULL);
81447121Sbostic #else
81547121Sbostic #ifdef SYSV
81647121Sbostic int (*save)();
81747121Sbostic
81847121Sbostic if (block == 0) {
81947121Sbostic gotsigchild = 0;
82047121Sbostic save = signal(SIGCLD, onsigchild);
82147121Sbostic signal(SIGCLD, save);
82247121Sbostic if (gotsigchild == 0)
82347121Sbostic return 0;
82447121Sbostic }
82547121Sbostic return wait(status);
82647121Sbostic #else
82747121Sbostic if (block == 0)
82847121Sbostic return 0;
82947121Sbostic return wait(status);
83047121Sbostic #endif
83147121Sbostic #endif
83247121Sbostic }
83347121Sbostic
83455277Smarc /*
83555277Smarc * return 1 if there are stopped jobs, otherwise 0
83655277Smarc */
83755277Smarc int job_warning = 0;
83855277Smarc int
stoppedjobs()83956573Sleres stoppedjobs()
84055277Smarc {
84156563Smarc register int jobno;
84256563Smarc register struct job *jp;
84347121Sbostic
84455277Smarc if (job_warning)
84555277Smarc return (0);
84656563Smarc for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
84756563Smarc if (jp->used == 0)
84855277Smarc continue;
84956563Smarc if (jp->state == JOBSTOPPED) {
85055277Smarc out2str("You have stopped jobs.\n");
85155277Smarc job_warning = 2;
85255277Smarc return (1);
85355277Smarc }
85455277Smarc }
85547121Sbostic
85655277Smarc return (0);
85755277Smarc }
85855277Smarc
85947121Sbostic /*
86047121Sbostic * Return a string identifying a command (to be printed by the
86147121Sbostic * jobs command.
86247121Sbostic */
86347121Sbostic
86447121Sbostic STATIC char *cmdnextc;
86547121Sbostic STATIC int cmdnleft;
86647121Sbostic STATIC void cmdtxt(), cmdputs();
86755297Smarc #define MAXCMDTEXT 200
86847121Sbostic
86955297Smarc char *
commandtext(n)87047121Sbostic commandtext(n)
87147121Sbostic union node *n;
87247121Sbostic {
87347121Sbostic char *name;
87447121Sbostic
87555297Smarc cmdnextc = name = ckmalloc(MAXCMDTEXT);
87655297Smarc cmdnleft = MAXCMDTEXT - 4;
87747121Sbostic cmdtxt(n);
87847121Sbostic *cmdnextc = '\0';
87947121Sbostic return name;
88047121Sbostic }
88147121Sbostic
88247121Sbostic
88347121Sbostic STATIC void
cmdtxt(n)88447121Sbostic cmdtxt(n)
88547121Sbostic union node *n;
88647121Sbostic {
88747121Sbostic union node *np;
88847121Sbostic struct nodelist *lp;
88947121Sbostic char *p;
89047121Sbostic int i;
89147121Sbostic char s[2];
89247121Sbostic
89355230Smarc if (n == NULL)
89455230Smarc return;
89547121Sbostic switch (n->type) {
89647121Sbostic case NSEMI:
89747121Sbostic cmdtxt(n->nbinary.ch1);
89847121Sbostic cmdputs("; ");
89947121Sbostic cmdtxt(n->nbinary.ch2);
90047121Sbostic break;
90147121Sbostic case NAND:
90247121Sbostic cmdtxt(n->nbinary.ch1);
90347121Sbostic cmdputs(" && ");
90447121Sbostic cmdtxt(n->nbinary.ch2);
90547121Sbostic break;
90647121Sbostic case NOR:
90747121Sbostic cmdtxt(n->nbinary.ch1);
90847121Sbostic cmdputs(" || ");
90947121Sbostic cmdtxt(n->nbinary.ch2);
91047121Sbostic break;
91147121Sbostic case NPIPE:
91247121Sbostic for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
91347121Sbostic cmdtxt(lp->n);
91447121Sbostic if (lp->next)
91547121Sbostic cmdputs(" | ");
91647121Sbostic }
91747121Sbostic break;
91847121Sbostic case NSUBSHELL:
91947121Sbostic cmdputs("(");
92047121Sbostic cmdtxt(n->nredir.n);
92147121Sbostic cmdputs(")");
92247121Sbostic break;
92347121Sbostic case NREDIR:
92447121Sbostic case NBACKGND:
92547121Sbostic cmdtxt(n->nredir.n);
92647121Sbostic break;
92747121Sbostic case NIF:
92847121Sbostic cmdputs("if ");
92947121Sbostic cmdtxt(n->nif.test);
93047121Sbostic cmdputs("; then ");
93147121Sbostic cmdtxt(n->nif.ifpart);
93247121Sbostic cmdputs("...");
93347121Sbostic break;
93447121Sbostic case NWHILE:
93547121Sbostic cmdputs("while ");
93647121Sbostic goto until;
93747121Sbostic case NUNTIL:
93847121Sbostic cmdputs("until ");
93947121Sbostic until:
94047121Sbostic cmdtxt(n->nbinary.ch1);
94147121Sbostic cmdputs("; do ");
94247121Sbostic cmdtxt(n->nbinary.ch2);
94347121Sbostic cmdputs("; done");
94447121Sbostic break;
94547121Sbostic case NFOR:
94647121Sbostic cmdputs("for ");
94747121Sbostic cmdputs(n->nfor.var);
94847121Sbostic cmdputs(" in ...");
94947121Sbostic break;
95047121Sbostic case NCASE:
95147121Sbostic cmdputs("case ");
95247121Sbostic cmdputs(n->ncase.expr->narg.text);
95347121Sbostic cmdputs(" in ...");
95447121Sbostic break;
95547121Sbostic case NDEFUN:
95647121Sbostic cmdputs(n->narg.text);
95747121Sbostic cmdputs("() ...");
95847121Sbostic break;
95947121Sbostic case NCMD:
96047121Sbostic for (np = n->ncmd.args ; np ; np = np->narg.next) {
96147121Sbostic cmdtxt(np);
96247121Sbostic if (np->narg.next)
96347121Sbostic cmdputs(" ");
96447121Sbostic }
96547121Sbostic for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
96647121Sbostic cmdputs(" ");
96747121Sbostic cmdtxt(np);
96847121Sbostic }
96947121Sbostic break;
97047121Sbostic case NARG:
97147121Sbostic cmdputs(n->narg.text);
97247121Sbostic break;
97347121Sbostic case NTO:
97447121Sbostic p = ">"; i = 1; goto redir;
97547121Sbostic case NAPPEND:
97647121Sbostic p = ">>"; i = 1; goto redir;
97747121Sbostic case NTOFD:
97847121Sbostic p = ">&"; i = 1; goto redir;
97947121Sbostic case NFROM:
98047121Sbostic p = "<"; i = 0; goto redir;
98147121Sbostic case NFROMFD:
98247121Sbostic p = "<&"; i = 0; goto redir;
98347121Sbostic redir:
98447121Sbostic if (n->nfile.fd != i) {
98547121Sbostic s[0] = n->nfile.fd + '0';
98647121Sbostic s[1] = '\0';
98747121Sbostic cmdputs(s);
98847121Sbostic }
98947121Sbostic cmdputs(p);
99047121Sbostic if (n->type == NTOFD || n->type == NFROMFD) {
99147121Sbostic s[0] = n->ndup.dupfd + '0';
99247121Sbostic s[1] = '\0';
99347121Sbostic cmdputs(s);
99447121Sbostic } else {
99547121Sbostic cmdtxt(n->nfile.fname);
99647121Sbostic }
99747121Sbostic break;
99847121Sbostic case NHERE:
99947121Sbostic case NXHERE:
100047121Sbostic cmdputs("<<...");
100147121Sbostic break;
100247121Sbostic default:
100347121Sbostic cmdputs("???");
100447121Sbostic break;
100547121Sbostic }
100647121Sbostic }
100747121Sbostic
100847121Sbostic
100947121Sbostic
101047121Sbostic STATIC void
cmdputs(s)101147121Sbostic cmdputs(s)
101247121Sbostic char *s;
101347121Sbostic {
101447121Sbostic register char *p, *q;
101547121Sbostic register char c;
101647121Sbostic int subtype = 0;
101747121Sbostic
101847121Sbostic if (cmdnleft <= 0)
101947121Sbostic return;
102047121Sbostic p = s;
102147121Sbostic q = cmdnextc;
102247121Sbostic while ((c = *p++) != '\0') {
102347121Sbostic if (c == CTLESC)
102447121Sbostic *q++ = *p++;
102547121Sbostic else if (c == CTLVAR) {
102647121Sbostic *q++ = '$';
102747121Sbostic if (--cmdnleft > 0)
102847121Sbostic *q++ = '{';
102947121Sbostic subtype = *p++;
103047121Sbostic } else if (c == '=' && subtype != 0) {
103147121Sbostic *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
103247121Sbostic subtype = 0;
103347121Sbostic } else if (c == CTLENDVAR) {
103447121Sbostic *q++ = '}';
103547121Sbostic } else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE)
103647121Sbostic cmdnleft++; /* ignore it */
103747121Sbostic else
103847121Sbostic *q++ = c;
103947121Sbostic if (--cmdnleft <= 0) {
104047121Sbostic *q++ = '.';
104147121Sbostic *q++ = '.';
104247121Sbostic *q++ = '.';
104347121Sbostic break;
104447121Sbostic }
104547121Sbostic }
104647121Sbostic cmdnextc = q;
104747121Sbostic }
1048