xref: /csrg-svn/bin/sh/jobs.c (revision 68934)
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  *
8*68934Sbostic  * %sccs.include.redist.c%
947121Sbostic  */
1047121Sbostic 
1147121Sbostic #ifndef lint
12*68934Sbostic static char sccsid[] = "@(#)jobs.c	8.3 (Berkeley) 04/27/95";
1347121Sbostic #endif /* not lint */
1447121Sbostic 
1547121Sbostic #include "shell.h"
1647121Sbostic #if JOBS
1747121Sbostic #include "sgtty.h"
1847121Sbostic #undef CEOF			/* syntax.h redefines this */
1947121Sbostic #endif
2047121Sbostic #include "main.h"
2147121Sbostic #include "parser.h"
2247121Sbostic #include "nodes.h"
2347121Sbostic #include "jobs.h"
2447121Sbostic #include "options.h"
2547121Sbostic #include "trap.h"
2647121Sbostic #include "syntax.h"
2747121Sbostic #include "input.h"
2847121Sbostic #include "output.h"
2947121Sbostic #include "memalloc.h"
3047121Sbostic #include "error.h"
3147121Sbostic #include "mystring.h"
3247121Sbostic #include <fcntl.h>
3347121Sbostic #include <signal.h>
3447121Sbostic #include <errno.h>
3568928Sbostic #include <unistd.h>
3647121Sbostic #ifdef BSD
3747121Sbostic #include <sys/types.h>
3847121Sbostic #include <sys/wait.h>
3947121Sbostic #include <sys/time.h>
4047121Sbostic #include <sys/resource.h>
4147121Sbostic #endif
4247121Sbostic 
4347121Sbostic 
4447121Sbostic 
4547121Sbostic struct job *jobtab;		/* array of jobs */
4647121Sbostic int njobs;			/* size of array */
4747121Sbostic MKINIT short backgndpid = -1;	/* pid of last background process */
4847121Sbostic #if JOBS
4947121Sbostic int initialpgrp;		/* pgrp of shell on invocation */
5047121Sbostic short curjob;			/* current job */
5147121Sbostic #endif
5247121Sbostic 
5347121Sbostic #ifdef __STDC__
5447121Sbostic STATIC void restartjob(struct job *);
5547121Sbostic STATIC struct job *getjob(char *);
5647121Sbostic STATIC void freejob(struct job *);
5747121Sbostic STATIC int procrunning(int);
5847121Sbostic STATIC int dowait(int, struct job *);
5947121Sbostic STATIC int waitproc(int, int *);
6047121Sbostic #else
6147121Sbostic STATIC void restartjob();
6247121Sbostic STATIC struct job *getjob();
6347121Sbostic STATIC void freejob();
6447121Sbostic STATIC int procrunning();
6547121Sbostic STATIC int dowait();
6647121Sbostic STATIC int waitproc();
6747121Sbostic #endif
6847121Sbostic 
6947121Sbostic 
7056573Sleres 
7147121Sbostic /*
7247121Sbostic  * Turn job control on and off.
7347121Sbostic  *
7447121Sbostic  * Note:  This code assumes that the third arg to ioctl is a character
7547121Sbostic  * pointer, which is true on Berkeley systems but not System V.  Since
7647121Sbostic  * System V doesn't have job control yet, this isn't a problem now.
7747121Sbostic  */
7847121Sbostic 
7947121Sbostic MKINIT int jobctl;
8047121Sbostic 
8147121Sbostic void
8247121Sbostic setjobctl(on) {
8356563Smarc #ifdef OLD_TTY_DRIVER
8447121Sbostic 	int ldisc;
8556563Smarc #endif
8647121Sbostic 
8747121Sbostic 	if (on == jobctl || rootshell == 0)
8847121Sbostic 		return;
8947121Sbostic 	if (on) {
9047121Sbostic 		do { /* while we are in the background */
9147121Sbostic 			if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
9255230Smarc 				out2str("sh: can't access tty; job control turned off\n");
9355230Smarc 				mflag = 0;
9447121Sbostic 				return;
9547121Sbostic 			}
9647121Sbostic 			if (initialpgrp == -1)
97*68934Sbostic 				initialpgrp = getpgrp();
98*68934Sbostic 			else if (initialpgrp != getpgrp()) {
9947121Sbostic 				killpg(initialpgrp, SIGTTIN);
10047121Sbostic 				continue;
10147121Sbostic 			}
10247121Sbostic 		} while (0);
10355230Smarc #ifdef OLD_TTY_DRIVER
10447121Sbostic 		if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
10555230Smarc 			out2str("sh: need new tty driver to run job control; job control turned off\n");
10655230Smarc 			mflag = 0;
10747121Sbostic 			return;
10847121Sbostic 		}
10955230Smarc #endif
11047121Sbostic 		setsignal(SIGTSTP);
11147121Sbostic 		setsignal(SIGTTOU);
11254318Smarc 		setsignal(SIGTTIN);
11347121Sbostic 		setpgrp(0, rootpid);
11447121Sbostic 		ioctl(2, TIOCSPGRP, (char *)&rootpid);
11547121Sbostic 	} else { /* turning job control off */
11647121Sbostic 		setpgrp(0, initialpgrp);
11747121Sbostic 		ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
11847121Sbostic 		setsignal(SIGTSTP);
11947121Sbostic 		setsignal(SIGTTOU);
12054318Smarc 		setsignal(SIGTTIN);
12147121Sbostic 	}
12247121Sbostic 	jobctl = on;
12347121Sbostic }
12447121Sbostic 
12547121Sbostic 
12647121Sbostic #ifdef mkinit
12747121Sbostic 
12847121Sbostic SHELLPROC {
12947121Sbostic 	backgndpid = -1;
13047121Sbostic #if JOBS
13147121Sbostic 	jobctl = 0;
13247121Sbostic #endif
13347121Sbostic }
13447121Sbostic 
13547121Sbostic #endif
13647121Sbostic 
13747121Sbostic 
13847121Sbostic 
13947121Sbostic #if JOBS
14047121Sbostic fgcmd(argc, argv)  char **argv; {
14147121Sbostic 	struct job *jp;
14247121Sbostic 	int pgrp;
14347121Sbostic 	int status;
14447121Sbostic 
14547121Sbostic 	jp = getjob(argv[1]);
14647121Sbostic 	if (jp->jobctl == 0)
14747121Sbostic 		error("job not created under job control");
14847121Sbostic 	pgrp = jp->ps[0].pid;
14947121Sbostic 	ioctl(2, TIOCSPGRP, (char *)&pgrp);
15047121Sbostic 	restartjob(jp);
15147121Sbostic 	INTOFF;
15247121Sbostic 	status = waitforjob(jp);
15347121Sbostic 	INTON;
15447121Sbostic 	return status;
15547121Sbostic }
15647121Sbostic 
15747121Sbostic 
15847121Sbostic bgcmd(argc, argv)  char **argv; {
15947121Sbostic 	struct job *jp;
16047121Sbostic 
16147121Sbostic 	do {
16247121Sbostic 		jp = getjob(*++argv);
16347121Sbostic 		if (jp->jobctl == 0)
16447121Sbostic 			error("job not created under job control");
16547121Sbostic 		restartjob(jp);
16647121Sbostic 	} while (--argc > 1);
16747121Sbostic 	return 0;
16847121Sbostic }
16947121Sbostic 
17047121Sbostic 
17147121Sbostic STATIC void
17247121Sbostic restartjob(jp)
17347121Sbostic 	struct job *jp;
17447121Sbostic 	{
17547121Sbostic 	struct procstat *ps;
17647121Sbostic 	int i;
17747121Sbostic 
17847121Sbostic 	if (jp->state == JOBDONE)
17947121Sbostic 		return;
18047121Sbostic 	INTOFF;
18147121Sbostic 	killpg(jp->ps[0].pid, SIGCONT);
18247121Sbostic 	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
18347121Sbostic 		if ((ps->status & 0377) == 0177) {
18447121Sbostic 			ps->status = -1;
18547121Sbostic 			jp->state = 0;
18647121Sbostic 		}
18747121Sbostic 	}
18847121Sbostic 	INTON;
18947121Sbostic }
19047121Sbostic #endif
19147121Sbostic 
19247121Sbostic 
19347121Sbostic int
19447121Sbostic jobscmd(argc, argv)  char **argv; {
19547121Sbostic 	showjobs(0);
19647121Sbostic 	return 0;
19747121Sbostic }
19847121Sbostic 
19947121Sbostic 
20047121Sbostic /*
20147121Sbostic  * Print a list of jobs.  If "change" is nonzero, only print jobs whose
20247121Sbostic  * statuses have changed since the last call to showjobs.
20347121Sbostic  *
20447121Sbostic  * If the shell is interrupted in the process of creating a job, the
20547121Sbostic  * result may be a job structure containing zero processes.  Such structures
20647121Sbostic  * will be freed here.
20747121Sbostic  */
20847121Sbostic 
20947121Sbostic void
21047121Sbostic showjobs(change) {
21147121Sbostic 	int jobno;
21247121Sbostic 	int procno;
21347121Sbostic 	int i;
21447121Sbostic 	struct job *jp;
21547121Sbostic 	struct procstat *ps;
21647121Sbostic 	int col;
21747121Sbostic 	char s[64];
21847121Sbostic 
21947121Sbostic 	TRACE(("showjobs(%d) called\n", change));
22047121Sbostic 	while (dowait(0, (struct job *)NULL) > 0);
22147121Sbostic 	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
22247121Sbostic 		if (! jp->used)
22347121Sbostic 			continue;
22447121Sbostic 		if (jp->nprocs == 0) {
22547121Sbostic 			freejob(jp);
22647121Sbostic 			continue;
22747121Sbostic 		}
22847121Sbostic 		if (change && ! jp->changed)
22947121Sbostic 			continue;
23047121Sbostic 		procno = jp->nprocs;
23147121Sbostic 		for (ps = jp->ps ; ; ps++) {	/* for each process */
23247121Sbostic 			if (ps == jp->ps)
23347121Sbostic 				fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
23447121Sbostic 			else
23547121Sbostic 				fmtstr(s, 64, "    %d ", ps->pid);
23647121Sbostic 			out1str(s);
23747121Sbostic 			col = strlen(s);
23847121Sbostic 			s[0] = '\0';
23947121Sbostic 			if (ps->status == -1) {
24047121Sbostic 				/* don't print anything */
24147121Sbostic 			} else if ((ps->status & 0xFF) == 0) {
24247121Sbostic 				fmtstr(s, 64, "Exit %d", ps->status >> 8);
24347121Sbostic 			} else {
24447121Sbostic 				i = ps->status;
24547121Sbostic #if JOBS
24647121Sbostic 				if ((i & 0xFF) == 0177)
24747121Sbostic 					i >>= 8;
24847121Sbostic #endif
24968928Sbostic 				if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F])
25068928Sbostic 					scopy(sys_siglist[i & 0x7F], s);
25147121Sbostic 				else
25247121Sbostic 					fmtstr(s, 64, "Signal %d", i & 0x7F);
25347121Sbostic 				if (i & 0x80)
25447121Sbostic 					strcat(s, " (core dumped)");
25547121Sbostic 			}
25647121Sbostic 			out1str(s);
25747121Sbostic 			col += strlen(s);
25847121Sbostic 			do {
25947121Sbostic 				out1c(' ');
26047121Sbostic 				col++;
26147121Sbostic 			} while (col < 30);
26247121Sbostic 			out1str(ps->cmd);
26347121Sbostic 			out1c('\n');
26447121Sbostic 			if (--procno <= 0)
26547121Sbostic 				break;
26647121Sbostic 		}
26747121Sbostic 		jp->changed = 0;
26847121Sbostic 		if (jp->state == JOBDONE) {
26947121Sbostic 			freejob(jp);
27047121Sbostic 		}
27147121Sbostic 	}
27247121Sbostic }
27347121Sbostic 
27447121Sbostic 
27547121Sbostic /*
27647121Sbostic  * Mark a job structure as unused.
27747121Sbostic  */
27847121Sbostic 
27947121Sbostic STATIC void
28047121Sbostic freejob(jp)
28147121Sbostic 	struct job *jp;
28247121Sbostic 	{
28347121Sbostic 	struct procstat *ps;
28447121Sbostic 	int i;
28547121Sbostic 
28647121Sbostic 	INTOFF;
28747121Sbostic 	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
28847121Sbostic 		if (ps->cmd != nullstr)
28947121Sbostic 			ckfree(ps->cmd);
29047121Sbostic 	}
29147121Sbostic 	if (jp->ps != &jp->ps0)
29247121Sbostic 		ckfree(jp->ps);
29347121Sbostic 	jp->used = 0;
29447121Sbostic #if JOBS
29547121Sbostic 	if (curjob == jp - jobtab + 1)
29647121Sbostic 		curjob = 0;
29747121Sbostic #endif
29847121Sbostic 	INTON;
29947121Sbostic }
30047121Sbostic 
30147121Sbostic 
30247121Sbostic 
30347121Sbostic int
30447121Sbostic waitcmd(argc, argv)  char **argv; {
30547121Sbostic 	struct job *job;
30647121Sbostic 	int status;
30747121Sbostic 	struct job *jp;
30847121Sbostic 
30947121Sbostic 	if (argc > 1) {
31047121Sbostic 		job = getjob(argv[1]);
31147121Sbostic 	} else {
31247121Sbostic 		job = NULL;
31347121Sbostic 	}
31447121Sbostic 	for (;;) {	/* loop until process terminated or stopped */
31547121Sbostic 		if (job != NULL) {
31647121Sbostic 			if (job->state) {
31747121Sbostic 				status = job->ps[job->nprocs - 1].status;
31847121Sbostic 				if ((status & 0xFF) == 0)
31947121Sbostic 					status = status >> 8 & 0xFF;
32047121Sbostic #if JOBS
32147121Sbostic 				else if ((status & 0xFF) == 0177)
32247121Sbostic 					status = (status >> 8 & 0x7F) + 128;
32347121Sbostic #endif
32447121Sbostic 				else
32547121Sbostic 					status = (status & 0x7F) + 128;
32647121Sbostic 				if (! iflag)
32747121Sbostic 					freejob(job);
32847121Sbostic 				return status;
32947121Sbostic 			}
33047121Sbostic 		} else {
33147121Sbostic 			for (jp = jobtab ; ; jp++) {
33247121Sbostic 				if (jp >= jobtab + njobs) {	/* no running procs */
33347121Sbostic 					return 0;
33447121Sbostic 				}
33547121Sbostic 				if (jp->used && jp->state == 0)
33647121Sbostic 					break;
33747121Sbostic 			}
33847121Sbostic 		}
33947121Sbostic 		dowait(1, (struct job *)NULL);
34047121Sbostic 	}
34147121Sbostic }
34247121Sbostic 
34347121Sbostic 
34447121Sbostic 
34547121Sbostic jobidcmd(argc, argv)  char **argv; {
34647121Sbostic 	struct job *jp;
34747121Sbostic 	int i;
34847121Sbostic 
34947121Sbostic 	jp = getjob(argv[1]);
35047121Sbostic 	for (i = 0 ; i < jp->nprocs ; ) {
35147121Sbostic 		out1fmt("%d", jp->ps[i].pid);
35247121Sbostic 		out1c(++i < jp->nprocs? ' ' : '\n');
35347121Sbostic 	}
35447121Sbostic 	return 0;
35547121Sbostic }
35647121Sbostic 
35747121Sbostic 
35847121Sbostic 
35947121Sbostic /*
36047121Sbostic  * Convert a job name to a job structure.
36147121Sbostic  */
36247121Sbostic 
36347121Sbostic STATIC struct job *
36447121Sbostic getjob(name)
36547121Sbostic 	char *name;
36647121Sbostic 	{
36747121Sbostic 	int jobno;
36847121Sbostic 	register struct job *jp;
36947121Sbostic 	int pid;
37047121Sbostic 	int i;
37147121Sbostic 
37247121Sbostic 	if (name == NULL) {
37347121Sbostic #if JOBS
37447121Sbostic currentjob:
37547121Sbostic 		if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
37647121Sbostic 			error("No current job");
37747121Sbostic 		return &jobtab[jobno - 1];
37847121Sbostic #else
37947121Sbostic 		error("No current job");
38047121Sbostic #endif
38147121Sbostic 	} else if (name[0] == '%') {
38247121Sbostic 		if (is_digit(name[1])) {
38347121Sbostic 			jobno = number(name + 1);
38447121Sbostic 			if (jobno > 0 && jobno <= njobs
38547121Sbostic 			 && jobtab[jobno - 1].used != 0)
38647121Sbostic 				return &jobtab[jobno - 1];
38747121Sbostic #if JOBS
38847121Sbostic 		} else if (name[1] == '%' && name[2] == '\0') {
38947121Sbostic 			goto currentjob;
39047121Sbostic #endif
39147121Sbostic 		} else {
39247121Sbostic 			register struct job *found = NULL;
39347121Sbostic 			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
39447121Sbostic 				if (jp->used && jp->nprocs > 0
39547121Sbostic 				 && prefix(name + 1, jp->ps[0].cmd)) {
39647121Sbostic 					if (found)
39747121Sbostic 						error("%s: ambiguous", name);
39847121Sbostic 					found = jp;
39947121Sbostic 				}
40047121Sbostic 			}
40147121Sbostic 			if (found)
40247121Sbostic 				return found;
40347121Sbostic 		}
40447121Sbostic 	} else if (is_number(name)) {
40547121Sbostic 		pid = number(name);
40647121Sbostic 		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
40747121Sbostic 			if (jp->used && jp->nprocs > 0
40847121Sbostic 			 && jp->ps[jp->nprocs - 1].pid == pid)
40947121Sbostic 				return jp;
41047121Sbostic 		}
41147121Sbostic 	}
41247121Sbostic 	error("No such job: %s", name);
41347121Sbostic }
41447121Sbostic 
41547121Sbostic 
41647121Sbostic 
41747121Sbostic /*
41847121Sbostic  * Return a new job structure,
41947121Sbostic  */
42047121Sbostic 
42147121Sbostic struct job *
42247121Sbostic makejob(node, nprocs)
42347121Sbostic 	union node *node;
42447121Sbostic 	{
42547121Sbostic 	int i;
42647121Sbostic 	struct job *jp;
42747121Sbostic 
42847121Sbostic 	for (i = njobs, jp = jobtab ; ; jp++) {
42947121Sbostic 		if (--i < 0) {
43047121Sbostic 			INTOFF;
43147121Sbostic 			if (njobs == 0) {
43247121Sbostic 				jobtab = ckmalloc(4 * sizeof jobtab[0]);
43347121Sbostic 			} else {
43447121Sbostic 				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
43547121Sbostic 				bcopy(jobtab, jp, njobs * sizeof jp[0]);
43647121Sbostic 				ckfree(jobtab);
43747121Sbostic 				jobtab = jp;
43847121Sbostic 			}
43947121Sbostic 			jp = jobtab + njobs;
44047121Sbostic 			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
44147121Sbostic 			INTON;
44247121Sbostic 			break;
44347121Sbostic 		}
44447121Sbostic 		if (jp->used == 0)
44547121Sbostic 			break;
44647121Sbostic 	}
44747121Sbostic 	INTOFF;
44847121Sbostic 	jp->state = 0;
44947121Sbostic 	jp->used = 1;
45047121Sbostic 	jp->changed = 0;
45147121Sbostic 	jp->nprocs = 0;
45247121Sbostic #if JOBS
45347121Sbostic 	jp->jobctl = jobctl;
45447121Sbostic #endif
45547121Sbostic 	if (nprocs > 1) {
45647121Sbostic 		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
45747121Sbostic 	} else {
45847121Sbostic 		jp->ps = &jp->ps0;
45947121Sbostic 	}
46047121Sbostic 	INTON;
46147121Sbostic 	TRACE(("makejob(0x%x, %d) returns %%%d\n", (int)node, nprocs, jp - jobtab + 1));
46247121Sbostic 	return jp;
46347121Sbostic }
46447121Sbostic 
46547121Sbostic 
46647121Sbostic /*
46747121Sbostic  * Fork of a subshell.  If we are doing job control, give the subshell its
46847121Sbostic  * own process group.  Jp is a job structure that the job is to be added to.
46947121Sbostic  * N is the command that will be evaluated by the child.  Both jp and n may
47047121Sbostic  * be NULL.  The mode parameter can be one of the following:
47147121Sbostic  *	FORK_FG - Fork off a foreground process.
47247121Sbostic  *	FORK_BG - Fork off a background process.
47347121Sbostic  *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
47447121Sbostic  *		     process group even if job control is on.
47547121Sbostic  *
47647121Sbostic  * When job control is turned off, background processes have their standard
47747121Sbostic  * input redirected to /dev/null (except for the second and later processes
47847121Sbostic  * in a pipeline).
47947121Sbostic  */
48047121Sbostic 
48147121Sbostic int
48247121Sbostic forkshell(jp, n, mode)
48347121Sbostic 	union node *n;
48447121Sbostic 	struct job *jp;
48547121Sbostic 	{
48647121Sbostic 	int pid;
48747121Sbostic 	int pgrp;
48847121Sbostic 
48947121Sbostic 	TRACE(("forkshell(%%%d, 0x%x, %d) called\n", jp - jobtab, (int)n, mode));
49047121Sbostic 	INTOFF;
49147121Sbostic 	pid = fork();
49247121Sbostic 	if (pid == -1) {
49347121Sbostic 		TRACE(("Fork failed, errno=%d\n", errno));
49447121Sbostic 		INTON;
49547121Sbostic 		error("Cannot fork");
49647121Sbostic 	}
49747121Sbostic 	if (pid == 0) {
49847121Sbostic 		struct job *p;
49947121Sbostic 		int wasroot;
50047121Sbostic 		int i;
50147121Sbostic 
50247121Sbostic 		TRACE(("Child shell %d\n", getpid()));
50347121Sbostic 		wasroot = rootshell;
50447121Sbostic 		rootshell = 0;
50547121Sbostic 		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
50647121Sbostic 			if (p->used)
50747121Sbostic 				freejob(p);
50847121Sbostic 		closescript();
50947121Sbostic 		INTON;
51047121Sbostic 		clear_traps();
51147121Sbostic #if JOBS
51247121Sbostic 		jobctl = 0;		/* do job control only in root shell */
51355230Smarc 		if (wasroot && mode != FORK_NOJOB && mflag) {
51447121Sbostic 			if (jp == NULL || jp->nprocs == 0)
51547121Sbostic 				pgrp = getpid();
51647121Sbostic 			else
51747121Sbostic 				pgrp = jp->ps[0].pid;
51847121Sbostic 			setpgrp(0, pgrp);
51947121Sbostic 			if (mode == FORK_FG) {
52047121Sbostic 				/*** this causes superfluous TIOCSPGRPS ***/
52147121Sbostic 				if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
52247121Sbostic 					error("TIOCSPGRP failed, errno=%d\n", errno);
52347121Sbostic 			}
52447121Sbostic 			setsignal(SIGTSTP);
52547121Sbostic 			setsignal(SIGTTOU);
52647121Sbostic 		} else if (mode == FORK_BG) {
52747121Sbostic 			ignoresig(SIGINT);
52847121Sbostic 			ignoresig(SIGQUIT);
52960296Smarc 			if ((jp == NULL || jp->nprocs == 0) &&
53060296Smarc 			    ! fd0_redirected_p ()) {
53147121Sbostic 				close(0);
53247121Sbostic 				if (open("/dev/null", O_RDONLY) != 0)
53347121Sbostic 					error("Can't open /dev/null");
53447121Sbostic 			}
53547121Sbostic 		}
53647121Sbostic #else
53747121Sbostic 		if (mode == FORK_BG) {
53847121Sbostic 			ignoresig(SIGINT);
53947121Sbostic 			ignoresig(SIGQUIT);
54060296Smarc 			if ((jp == NULL || jp->nprocs == 0) &&
54160296Smarc 			    ! fd0_redirected_p ()) {
54247121Sbostic 				close(0);
54347121Sbostic 				if (open("/dev/null", O_RDONLY) != 0)
54447121Sbostic 					error("Can't open /dev/null");
54547121Sbostic 			}
54647121Sbostic 		}
54747121Sbostic #endif
54847121Sbostic 		if (wasroot && iflag) {
54947121Sbostic 			setsignal(SIGINT);
55047121Sbostic 			setsignal(SIGQUIT);
55147121Sbostic 			setsignal(SIGTERM);
55247121Sbostic 		}
55347121Sbostic 		return pid;
55447121Sbostic 	}
55555230Smarc 	if (rootshell && mode != FORK_NOJOB && mflag) {
55647121Sbostic 		if (jp == NULL || jp->nprocs == 0)
55747121Sbostic 			pgrp = pid;
55847121Sbostic 		else
55947121Sbostic 			pgrp = jp->ps[0].pid;
56047121Sbostic 		setpgrp(pid, pgrp);
56147121Sbostic 	}
56247121Sbostic 	if (mode == FORK_BG)
56347121Sbostic 		backgndpid = pid;		/* set $! */
56447121Sbostic 	if (jp) {
56547121Sbostic 		struct procstat *ps = &jp->ps[jp->nprocs++];
56647121Sbostic 		ps->pid = pid;
56747121Sbostic 		ps->status = -1;
56847121Sbostic 		ps->cmd = nullstr;
56947121Sbostic 		if (iflag && rootshell && n)
57047121Sbostic 			ps->cmd = commandtext(n);
57147121Sbostic 	}
57247121Sbostic 	INTON;
57347121Sbostic 	TRACE(("In parent shell:  child = %d\n", pid));
57447121Sbostic 	return pid;
57547121Sbostic }
57647121Sbostic 
57747121Sbostic 
57847121Sbostic 
57947121Sbostic /*
58047121Sbostic  * Wait for job to finish.
58147121Sbostic  *
58247121Sbostic  * Under job control we have the problem that while a child process is
58347121Sbostic  * running interrupts generated by the user are sent to the child but not
58447121Sbostic  * to the shell.  This means that an infinite loop started by an inter-
58547121Sbostic  * active user may be hard to kill.  With job control turned off, an
58647121Sbostic  * interactive user may place an interactive program inside a loop.  If
58747121Sbostic  * the interactive program catches interrupts, the user doesn't want
58847121Sbostic  * these interrupts to also abort the loop.  The approach we take here
58947121Sbostic  * is to have the shell ignore interrupt signals while waiting for a
59047121Sbostic  * forground process to terminate, and then send itself an interrupt
59147121Sbostic  * signal if the child process was terminated by an interrupt signal.
59247121Sbostic  * Unfortunately, some programs want to do a bit of cleanup and then
59347121Sbostic  * exit on interrupt; unless these processes terminate themselves by
59447121Sbostic  * sending a signal to themselves (instead of calling exit) they will
59547121Sbostic  * confuse this approach.
59647121Sbostic  */
59747121Sbostic 
59847121Sbostic int
59947121Sbostic waitforjob(jp)
60047121Sbostic 	register struct job *jp;
60147121Sbostic 	{
60247121Sbostic #if JOBS
603*68934Sbostic 	int mypgrp = getpgrp();
60447121Sbostic #endif
60547121Sbostic 	int status;
60647121Sbostic 	int st;
60747121Sbostic 
60847121Sbostic 	INTOFF;
60947121Sbostic 	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
61047121Sbostic 	while (jp->state == 0) {
61147121Sbostic 		dowait(1, jp);
61247121Sbostic 	}
61347121Sbostic #if JOBS
61447121Sbostic 	if (jp->jobctl) {
61547121Sbostic 		if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
61647121Sbostic 			error("TIOCSPGRP failed, errno=%d\n", errno);
61747121Sbostic 	}
61847121Sbostic 	if (jp->state == JOBSTOPPED)
61947121Sbostic 		curjob = jp - jobtab + 1;
62047121Sbostic #endif
62147121Sbostic 	status = jp->ps[jp->nprocs - 1].status;
62247121Sbostic 	/* convert to 8 bits */
62347121Sbostic 	if ((status & 0xFF) == 0)
62447121Sbostic 		st = status >> 8 & 0xFF;
62547121Sbostic #if JOBS
62647121Sbostic 	else if ((status & 0xFF) == 0177)
62747121Sbostic 		st = (status >> 8 & 0x7F) + 128;
62847121Sbostic #endif
62947121Sbostic 	else
63047121Sbostic 		st = (status & 0x7F) + 128;
63147121Sbostic 	if (! JOBS || jp->state == JOBDONE)
63247121Sbostic 		freejob(jp);
63347121Sbostic 	CLEAR_PENDING_INT;
63447121Sbostic 	if ((status & 0x7F) == SIGINT)
63547121Sbostic 		kill(getpid(), SIGINT);
63647121Sbostic 	INTON;
63747121Sbostic 	return st;
63847121Sbostic }
63947121Sbostic 
64047121Sbostic 
64147121Sbostic 
64247121Sbostic /*
64347121Sbostic  * Wait for a process to terminate.
64447121Sbostic  */
64547121Sbostic 
64647121Sbostic STATIC int
64747121Sbostic dowait(block, job)
64847121Sbostic 	struct job *job;
64947121Sbostic 	{
65047121Sbostic 	int pid;
65147121Sbostic 	int status;
65247121Sbostic 	struct procstat *sp;
65347121Sbostic 	struct job *jp;
65447121Sbostic 	struct job *thisjob;
65547121Sbostic 	int done;
65647121Sbostic 	int stopped;
65747121Sbostic 	int core;
65847121Sbostic 
65947121Sbostic 	TRACE(("dowait(%d) called\n", block));
66047121Sbostic 	do {
66147121Sbostic 		pid = waitproc(block, &status);
66247121Sbostic 		TRACE(("wait returns %d, status=%d\n", pid, status));
66347121Sbostic 	} while (pid == -1 && errno == EINTR);
66447121Sbostic 	if (pid <= 0)
66547121Sbostic 		return pid;
66647121Sbostic 	INTOFF;
66747121Sbostic 	thisjob = NULL;
66847121Sbostic 	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
66947121Sbostic 		if (jp->used) {
67047121Sbostic 			done = 1;
67147121Sbostic 			stopped = 1;
67247121Sbostic 			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
67347121Sbostic 				if (sp->pid == -1)
67447121Sbostic 					continue;
67547121Sbostic 				if (sp->pid == pid) {
67647121Sbostic 					TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status));
67747121Sbostic 					sp->status = status;
67847121Sbostic 					thisjob = jp;
67947121Sbostic 				}
68047121Sbostic 				if (sp->status == -1)
68147121Sbostic 					stopped = 0;
68247121Sbostic 				else if ((sp->status & 0377) == 0177)
68347121Sbostic 					done = 0;
68447121Sbostic 			}
68547121Sbostic 			if (stopped) {		/* stopped or done */
68647121Sbostic 				int state = done? JOBDONE : JOBSTOPPED;
68747121Sbostic 				if (jp->state != state) {
68847121Sbostic 					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
68947121Sbostic 					jp->state = state;
69047121Sbostic #if JOBS
69147121Sbostic 					if (done && curjob == jp - jobtab + 1)
69247121Sbostic 						curjob = 0;		/* no current job */
69347121Sbostic #endif
69447121Sbostic 				}
69547121Sbostic 			}
69647121Sbostic 		}
69747121Sbostic 	}
69847121Sbostic 	INTON;
69947121Sbostic 	if (! rootshell || ! iflag || (job && thisjob == job)) {
70047121Sbostic #if JOBS
70147121Sbostic 		if ((status & 0xFF) == 0177)
70247121Sbostic 			status >>= 8;
70347121Sbostic #endif
70447121Sbostic 		core = status & 0x80;
70547121Sbostic 		status &= 0x7F;
70647121Sbostic 		if (status != 0 && status != SIGINT && status != SIGPIPE) {
70747121Sbostic 			if (thisjob != job)
70847121Sbostic 				outfmt(out2, "%d: ", pid);
70947121Sbostic #if JOBS
71047121Sbostic 			if (status == SIGTSTP && rootshell && iflag)
71147121Sbostic 				outfmt(out2, "%%%d ", job - jobtab + 1);
71247121Sbostic #endif
71368928Sbostic 			if (status < NSIG && sys_siglist[status])
71468928Sbostic 				out2str(sys_siglist[status]);
71547121Sbostic 			else
71647121Sbostic 				outfmt(out2, "Signal %d", status);
71747121Sbostic 			if (core)
71847121Sbostic 				out2str(" - core dumped");
71947121Sbostic 			out2c('\n');
72047121Sbostic 			flushout(&errout);
72147121Sbostic 		} else {
72247121Sbostic 			TRACE(("Not printing status: status=%d\n", status));
72347121Sbostic 		}
72447121Sbostic 	} else {
72547121Sbostic 		TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
72647121Sbostic 		if (thisjob)
72747121Sbostic 			thisjob->changed = 1;
72847121Sbostic 	}
72947121Sbostic 	return pid;
73047121Sbostic }
73147121Sbostic 
73247121Sbostic 
73347121Sbostic 
73447121Sbostic /*
73547121Sbostic  * Do a wait system call.  If job control is compiled in, we accept
73647121Sbostic  * stopped processes.  If block is zero, we return a value of zero
73747121Sbostic  * rather than blocking.
73847121Sbostic  *
73947121Sbostic  * System V doesn't have a non-blocking wait system call.  It does
74047121Sbostic  * have a SIGCLD signal that is sent to a process when one of it's
74147121Sbostic  * children dies.  The obvious way to use SIGCLD would be to install
74247121Sbostic  * a handler for SIGCLD which simply bumped a counter when a SIGCLD
74347121Sbostic  * was received, and have waitproc bump another counter when it got
74447121Sbostic  * the status of a process.  Waitproc would then know that a wait
74547121Sbostic  * system call would not block if the two counters were different.
74647121Sbostic  * This approach doesn't work because if a process has children that
74747121Sbostic  * have not been waited for, System V will send it a SIGCLD when it
74847121Sbostic  * installs a signal handler for SIGCLD.  What this means is that when
74947121Sbostic  * a child exits, the shell will be sent SIGCLD signals continuously
75047121Sbostic  * until is runs out of stack space, unless it does a wait call before
75147121Sbostic  * restoring the signal handler.  The code below takes advantage of
75247121Sbostic  * this (mis)feature by installing a signal handler for SIGCLD and
75347121Sbostic  * then checking to see whether it was called.  If there are any
75447121Sbostic  * children to be waited for, it will be.
75547121Sbostic  *
75647121Sbostic  * If neither SYSV nor BSD is defined, we don't implement nonblocking
75747121Sbostic  * waits at all.  In this case, the user will not be informed when
75847121Sbostic  * a background process until the next time she runs a real program
75947121Sbostic  * (as opposed to running a builtin command or just typing return),
76047121Sbostic  * and the jobs command may give out of date information.
76147121Sbostic  */
76247121Sbostic 
76347121Sbostic #ifdef SYSV
76447121Sbostic STATIC int gotsigchild;
76547121Sbostic 
76647121Sbostic STATIC int onsigchild() {
76747121Sbostic 	gotsigchild = 1;
76847121Sbostic }
76947121Sbostic #endif
77047121Sbostic 
77147121Sbostic 
77247121Sbostic STATIC int
77347121Sbostic waitproc(block, status)
77447121Sbostic 	int *status;
77547121Sbostic 	{
77647121Sbostic #ifdef BSD
77747121Sbostic 	int flags;
77847121Sbostic 
77947121Sbostic #if JOBS
78047121Sbostic 	flags = WUNTRACED;
78147121Sbostic #else
78247121Sbostic 	flags = 0;
78347121Sbostic #endif
78447121Sbostic 	if (block == 0)
78547121Sbostic 		flags |= WNOHANG;
78655230Smarc 	return wait3(status, flags, (struct rusage *)NULL);
78747121Sbostic #else
78847121Sbostic #ifdef SYSV
78947121Sbostic 	int (*save)();
79047121Sbostic 
79147121Sbostic 	if (block == 0) {
79247121Sbostic 		gotsigchild = 0;
79347121Sbostic 		save = signal(SIGCLD, onsigchild);
79447121Sbostic 		signal(SIGCLD, save);
79547121Sbostic 		if (gotsigchild == 0)
79647121Sbostic 			return 0;
79747121Sbostic 	}
79847121Sbostic 	return wait(status);
79947121Sbostic #else
80047121Sbostic 	if (block == 0)
80147121Sbostic 		return 0;
80247121Sbostic 	return wait(status);
80347121Sbostic #endif
80447121Sbostic #endif
80547121Sbostic }
80647121Sbostic 
80755277Smarc /*
80855277Smarc  * return 1 if there are stopped jobs, otherwise 0
80955277Smarc  */
81055277Smarc int job_warning = 0;
81155277Smarc int
81256573Sleres stoppedjobs()
81355277Smarc {
81456563Smarc 	register int jobno;
81556563Smarc 	register struct job *jp;
81647121Sbostic 
81755277Smarc 	if (job_warning)
81855277Smarc 		return (0);
81956563Smarc 	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
82056563Smarc 		if (jp->used == 0)
82155277Smarc 			continue;
82256563Smarc 		if (jp->state == JOBSTOPPED) {
82355277Smarc 			out2str("You have stopped jobs.\n");
82455277Smarc 			job_warning = 2;
82555277Smarc 			return (1);
82655277Smarc 		}
82755277Smarc 	}
82847121Sbostic 
82955277Smarc 	return (0);
83055277Smarc }
83155277Smarc 
83247121Sbostic /*
83347121Sbostic  * Return a string identifying a command (to be printed by the
83447121Sbostic  * jobs command.
83547121Sbostic  */
83647121Sbostic 
83747121Sbostic STATIC char *cmdnextc;
83847121Sbostic STATIC int cmdnleft;
83947121Sbostic STATIC void cmdtxt(), cmdputs();
84055297Smarc #define MAXCMDTEXT	200
84147121Sbostic 
84255297Smarc char *
84347121Sbostic commandtext(n)
84447121Sbostic 	union node *n;
84547121Sbostic 	{
84647121Sbostic 	char *name;
84747121Sbostic 
84855297Smarc 	cmdnextc = name = ckmalloc(MAXCMDTEXT);
84955297Smarc 	cmdnleft = MAXCMDTEXT - 4;
85047121Sbostic 	cmdtxt(n);
85147121Sbostic 	*cmdnextc = '\0';
85247121Sbostic 	return name;
85347121Sbostic }
85447121Sbostic 
85547121Sbostic 
85647121Sbostic STATIC void
85747121Sbostic cmdtxt(n)
85847121Sbostic 	union node *n;
85947121Sbostic 	{
86047121Sbostic 	union node *np;
86147121Sbostic 	struct nodelist *lp;
86247121Sbostic 	char *p;
86347121Sbostic 	int i;
86447121Sbostic 	char s[2];
86547121Sbostic 
86655230Smarc 	if (n == NULL)
86755230Smarc 		return;
86847121Sbostic 	switch (n->type) {
86947121Sbostic 	case NSEMI:
87047121Sbostic 		cmdtxt(n->nbinary.ch1);
87147121Sbostic 		cmdputs("; ");
87247121Sbostic 		cmdtxt(n->nbinary.ch2);
87347121Sbostic 		break;
87447121Sbostic 	case NAND:
87547121Sbostic 		cmdtxt(n->nbinary.ch1);
87647121Sbostic 		cmdputs(" && ");
87747121Sbostic 		cmdtxt(n->nbinary.ch2);
87847121Sbostic 		break;
87947121Sbostic 	case NOR:
88047121Sbostic 		cmdtxt(n->nbinary.ch1);
88147121Sbostic 		cmdputs(" || ");
88247121Sbostic 		cmdtxt(n->nbinary.ch2);
88347121Sbostic 		break;
88447121Sbostic 	case NPIPE:
88547121Sbostic 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
88647121Sbostic 			cmdtxt(lp->n);
88747121Sbostic 			if (lp->next)
88847121Sbostic 				cmdputs(" | ");
88947121Sbostic 		}
89047121Sbostic 		break;
89147121Sbostic 	case NSUBSHELL:
89247121Sbostic 		cmdputs("(");
89347121Sbostic 		cmdtxt(n->nredir.n);
89447121Sbostic 		cmdputs(")");
89547121Sbostic 		break;
89647121Sbostic 	case NREDIR:
89747121Sbostic 	case NBACKGND:
89847121Sbostic 		cmdtxt(n->nredir.n);
89947121Sbostic 		break;
90047121Sbostic 	case NIF:
90147121Sbostic 		cmdputs("if ");
90247121Sbostic 		cmdtxt(n->nif.test);
90347121Sbostic 		cmdputs("; then ");
90447121Sbostic 		cmdtxt(n->nif.ifpart);
90547121Sbostic 		cmdputs("...");
90647121Sbostic 		break;
90747121Sbostic 	case NWHILE:
90847121Sbostic 		cmdputs("while ");
90947121Sbostic 		goto until;
91047121Sbostic 	case NUNTIL:
91147121Sbostic 		cmdputs("until ");
91247121Sbostic until:
91347121Sbostic 		cmdtxt(n->nbinary.ch1);
91447121Sbostic 		cmdputs("; do ");
91547121Sbostic 		cmdtxt(n->nbinary.ch2);
91647121Sbostic 		cmdputs("; done");
91747121Sbostic 		break;
91847121Sbostic 	case NFOR:
91947121Sbostic 		cmdputs("for ");
92047121Sbostic 		cmdputs(n->nfor.var);
92147121Sbostic 		cmdputs(" in ...");
92247121Sbostic 		break;
92347121Sbostic 	case NCASE:
92447121Sbostic 		cmdputs("case ");
92547121Sbostic 		cmdputs(n->ncase.expr->narg.text);
92647121Sbostic 		cmdputs(" in ...");
92747121Sbostic 		break;
92847121Sbostic 	case NDEFUN:
92947121Sbostic 		cmdputs(n->narg.text);
93047121Sbostic 		cmdputs("() ...");
93147121Sbostic 		break;
93247121Sbostic 	case NCMD:
93347121Sbostic 		for (np = n->ncmd.args ; np ; np = np->narg.next) {
93447121Sbostic 			cmdtxt(np);
93547121Sbostic 			if (np->narg.next)
93647121Sbostic 				cmdputs(" ");
93747121Sbostic 		}
93847121Sbostic 		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
93947121Sbostic 			cmdputs(" ");
94047121Sbostic 			cmdtxt(np);
94147121Sbostic 		}
94247121Sbostic 		break;
94347121Sbostic 	case NARG:
94447121Sbostic 		cmdputs(n->narg.text);
94547121Sbostic 		break;
94647121Sbostic 	case NTO:
94747121Sbostic 		p = ">";  i = 1;  goto redir;
94847121Sbostic 	case NAPPEND:
94947121Sbostic 		p = ">>";  i = 1;  goto redir;
95047121Sbostic 	case NTOFD:
95147121Sbostic 		p = ">&";  i = 1;  goto redir;
95247121Sbostic 	case NFROM:
95347121Sbostic 		p = "<";  i = 0;  goto redir;
95447121Sbostic 	case NFROMFD:
95547121Sbostic 		p = "<&";  i = 0;  goto redir;
95647121Sbostic redir:
95747121Sbostic 		if (n->nfile.fd != i) {
95847121Sbostic 			s[0] = n->nfile.fd + '0';
95947121Sbostic 			s[1] = '\0';
96047121Sbostic 			cmdputs(s);
96147121Sbostic 		}
96247121Sbostic 		cmdputs(p);
96347121Sbostic 		if (n->type == NTOFD || n->type == NFROMFD) {
96447121Sbostic 			s[0] = n->ndup.dupfd + '0';
96547121Sbostic 			s[1] = '\0';
96647121Sbostic 			cmdputs(s);
96747121Sbostic 		} else {
96847121Sbostic 			cmdtxt(n->nfile.fname);
96947121Sbostic 		}
97047121Sbostic 		break;
97147121Sbostic 	case NHERE:
97247121Sbostic 	case NXHERE:
97347121Sbostic 		cmdputs("<<...");
97447121Sbostic 		break;
97547121Sbostic 	default:
97647121Sbostic 		cmdputs("???");
97747121Sbostic 		break;
97847121Sbostic 	}
97947121Sbostic }
98047121Sbostic 
98147121Sbostic 
98247121Sbostic 
98347121Sbostic STATIC void
98447121Sbostic cmdputs(s)
98547121Sbostic 	char *s;
98647121Sbostic 	{
98747121Sbostic 	register char *p, *q;
98847121Sbostic 	register char c;
98947121Sbostic 	int subtype = 0;
99047121Sbostic 
99147121Sbostic 	if (cmdnleft <= 0)
99247121Sbostic 		return;
99347121Sbostic 	p = s;
99447121Sbostic 	q = cmdnextc;
99547121Sbostic 	while ((c = *p++) != '\0') {
99647121Sbostic 		if (c == CTLESC)
99747121Sbostic 			*q++ = *p++;
99847121Sbostic 		else if (c == CTLVAR) {
99947121Sbostic 			*q++ = '$';
100047121Sbostic 			if (--cmdnleft > 0)
100147121Sbostic 				*q++ = '{';
100247121Sbostic 			subtype = *p++;
100347121Sbostic 		} else if (c == '=' && subtype != 0) {
100447121Sbostic 			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
100547121Sbostic 			subtype = 0;
100647121Sbostic 		} else if (c == CTLENDVAR) {
100747121Sbostic 			*q++ = '}';
100847121Sbostic 		} else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE)
100947121Sbostic 			cmdnleft++;		/* ignore it */
101047121Sbostic 		else
101147121Sbostic 			*q++ = c;
101247121Sbostic 		if (--cmdnleft <= 0) {
101347121Sbostic 			*q++ = '.';
101447121Sbostic 			*q++ = '.';
101547121Sbostic 			*q++ = '.';
101647121Sbostic 			break;
101747121Sbostic 		}
101847121Sbostic 	}
101947121Sbostic 	cmdnextc = q;
102047121Sbostic }
1021