jobs.c revision 213811
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)jobs.c	8.5 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/jobs.c 213811 2010-10-13 22:18:03Z obrien $");
401556Srgrimes
41213775Sjhb#include <sys/ioctl.h>
42213775Sjhb#include <sys/param.h>
43213775Sjhb#include <sys/resource.h>
44213775Sjhb#include <sys/stddef.h>
45213775Sjhb#include <sys/time.h>
46213775Sjhb#include <sys/wait.h>
47213775Sjhb#include <errno.h>
4817987Speter#include <fcntl.h>
49213775Sjhb#include <paths.h>
5017987Speter#include <signal.h>
51213775Sjhb#include <stdlib.h>
5217987Speter#include <unistd.h>
5317987Speter
541556Srgrimes#include "shell.h"
551556Srgrimes#if JOBS
5617987Speter#include <termios.h>
571556Srgrimes#undef CEOF			/* syntax.h redefines this */
581556Srgrimes#endif
5917987Speter#include "redir.h"
6017987Speter#include "show.h"
611556Srgrimes#include "main.h"
621556Srgrimes#include "parser.h"
631556Srgrimes#include "nodes.h"
641556Srgrimes#include "jobs.h"
651556Srgrimes#include "options.h"
661556Srgrimes#include "trap.h"
671556Srgrimes#include "syntax.h"
681556Srgrimes#include "input.h"
691556Srgrimes#include "output.h"
701556Srgrimes#include "memalloc.h"
711556Srgrimes#include "error.h"
721556Srgrimes#include "mystring.h"
731556Srgrimes
741556Srgrimes
75213760Sobrienstatic struct job *jobtab;	/* array of jobs */
76213760Sobrienstatic int njobs;		/* size of array */
7728346SsteveMKINIT pid_t backgndpid = -1;	/* pid of last background process */
78209600SjillesMKINIT struct job *bgjob = NULL; /* last background process */
791556Srgrimes#if JOBS
80213760Sobrienstatic struct job *jobmru;	/* most recently used job list */
81213760Sobrienstatic pid_t initialpgrp;	/* pgrp of shell on invocation */
821556Srgrimes#endif
8338536Scracauerint in_waitcmd = 0;		/* are we in waitcmd()? */
8438950Scracauerint in_dowait = 0;		/* are we in dowait()? */
8538536Scracauervolatile sig_atomic_t breakwaitcmd = 0;	/* should wait be terminated? */
8699762Stjrstatic int ttyfd = -1;
871556Srgrimes
8820425Ssteve#if JOBS
89213811Sobrienstatic void restartjob(struct job *);
9020425Ssteve#endif
91213811Sobrienstatic void freejob(struct job *);
92213811Sobrienstatic struct job *getjob(char *);
93213811Sobrienstatic pid_t dowait(int, struct job *);
94213811Sobrienstatic pid_t waitproc(int, int *);
95213811Sobrienstatic void checkzombies(void);
96213811Sobrienstatic void cmdtxt(union node *);
97213811Sobrienstatic void cmdputs(const char *);
9897659Stjr#if JOBS
99213811Sobrienstatic void setcurjob(struct job *);
100213811Sobrienstatic void deljob(struct job *);
101213811Sobrienstatic struct job *getcurjob(struct job *);
10297659Stjr#endif
103213811Sobrienstatic void showjob(struct job *, pid_t, int);
1041556Srgrimes
1051556Srgrimes
1061556Srgrimes/*
1071556Srgrimes * Turn job control on and off.
1081556Srgrimes */
1091556Srgrimes
1101556SrgrimesMKINIT int jobctl;
1111556Srgrimes
11220425Ssteve#if JOBS
1131556Srgrimesvoid
11490111Simpsetjobctl(int on)
11517987Speter{
11699762Stjr	int i;
1171556Srgrimes
1181556Srgrimes	if (on == jobctl || rootshell == 0)
1191556Srgrimes		return;
1201556Srgrimes	if (on) {
12199762Stjr		if (ttyfd != -1)
12299762Stjr			close(ttyfd);
12399762Stjr		if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
12499762Stjr			i = 0;
12599762Stjr			while (i <= 2 && !isatty(i))
12699762Stjr				i++;
127109927Stjr			if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
12899762Stjr				goto out;
12999762Stjr		}
130109927Stjr		if (ttyfd < 10) {
131109927Stjr			/*
132109927Stjr			 * Keep our TTY file descriptor out of the way of
133109927Stjr			 * the user's redirections.
134109927Stjr			 */
135109927Stjr			if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
136109927Stjr				close(ttyfd);
137109927Stjr				ttyfd = -1;
138109927Stjr				goto out;
139109927Stjr			}
140109927Stjr			close(ttyfd);
141109927Stjr			ttyfd = i;
142109927Stjr		}
143103223Snectar		if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
14499762Stjr			close(ttyfd);
14599762Stjr			ttyfd = -1;
14699762Stjr			goto out;
14799762Stjr		}
1481556Srgrimes		do { /* while we are in the background */
14999762Stjr			initialpgrp = tcgetpgrp(ttyfd);
15020425Ssteve			if (initialpgrp < 0) {
151199629Sjillesout:				out2fmt_flush("sh: can't access tty; job control turned off\n");
1521556Srgrimes				mflag = 0;
1531556Srgrimes				return;
1541556Srgrimes			}
1551556Srgrimes			if (initialpgrp == -1)
15617987Speter				initialpgrp = getpgrp();
15717987Speter			else if (initialpgrp != getpgrp()) {
15899762Stjr				killpg(0, SIGTTIN);
1591556Srgrimes				continue;
1601556Srgrimes			}
1611556Srgrimes		} while (0);
1621556Srgrimes		setsignal(SIGTSTP);
1631556Srgrimes		setsignal(SIGTTOU);
1641556Srgrimes		setsignal(SIGTTIN);
16517987Speter		setpgid(0, rootpid);
16699762Stjr		tcsetpgrp(ttyfd, rootpid);
1671556Srgrimes	} else { /* turning job control off */
16817987Speter		setpgid(0, initialpgrp);
16999762Stjr		tcsetpgrp(ttyfd, initialpgrp);
17099762Stjr		close(ttyfd);
17199762Stjr		ttyfd = -1;
1721556Srgrimes		setsignal(SIGTSTP);
1731556Srgrimes		setsignal(SIGTTOU);
1741556Srgrimes		setsignal(SIGTTIN);
1751556Srgrimes	}
1761556Srgrimes	jobctl = on;
1771556Srgrimes}
17820425Ssteve#endif
1791556Srgrimes
1801556Srgrimes
1811556Srgrimes#ifdef mkinit
18228346SsteveINCLUDE <sys/types.h>
18317987SpeterINCLUDE <stdlib.h>
1841556Srgrimes
1851556SrgrimesSHELLPROC {
1861556Srgrimes	backgndpid = -1;
187209600Sjilles	bgjob = NULL;
1881556Srgrimes#if JOBS
1891556Srgrimes	jobctl = 0;
1901556Srgrimes#endif
1911556Srgrimes}
1921556Srgrimes
1931556Srgrimes#endif
1941556Srgrimes
1951556Srgrimes
1961556Srgrimes
1971556Srgrimes#if JOBS
19817987Speterint
19990111Simpfgcmd(int argc __unused, char **argv)
20017987Speter{
2011556Srgrimes	struct job *jp;
202100308Stjr	pid_t pgrp;
2031556Srgrimes	int status;
2041556Srgrimes
2051556Srgrimes	jp = getjob(argv[1]);
2061556Srgrimes	if (jp->jobctl == 0)
2071556Srgrimes		error("job not created under job control");
20896933Stjr	out1str(jp->ps[0].cmd);
20996933Stjr	out1c('\n');
21096933Stjr	flushout(&output);
2111556Srgrimes	pgrp = jp->ps[0].pid;
21299762Stjr	tcsetpgrp(ttyfd, pgrp);
2131556Srgrimes	restartjob(jp);
214100305Stjr	jp->foreground = 1;
2151556Srgrimes	INTOFF;
21645916Scracauer	status = waitforjob(jp, (int *)NULL);
2171556Srgrimes	INTON;
2181556Srgrimes	return status;
2191556Srgrimes}
2201556Srgrimes
2211556Srgrimes
22217987Speterint
22390111Simpbgcmd(int argc, char **argv)
22417987Speter{
22596933Stjr	char s[64];
2261556Srgrimes	struct job *jp;
2271556Srgrimes
2281556Srgrimes	do {
2291556Srgrimes		jp = getjob(*++argv);
2301556Srgrimes		if (jp->jobctl == 0)
2311556Srgrimes			error("job not created under job control");
23296933Stjr		if (jp->state == JOBDONE)
23396933Stjr			continue;
2341556Srgrimes		restartjob(jp);
235100305Stjr		jp->foreground = 0;
236104275Smux		fmtstr(s, 64, "[%td] ", jp - jobtab + 1);
23796933Stjr		out1str(s);
23896933Stjr		out1str(jp->ps[0].cmd);
23996933Stjr		out1c('\n');
2401556Srgrimes	} while (--argc > 1);
2411556Srgrimes	return 0;
2421556Srgrimes}
2431556Srgrimes
2441556Srgrimes
245213811Sobrienstatic void
24690111Simprestartjob(struct job *jp)
24717987Speter{
2481556Srgrimes	struct procstat *ps;
2491556Srgrimes	int i;
2501556Srgrimes
2511556Srgrimes	if (jp->state == JOBDONE)
2521556Srgrimes		return;
25397660Stjr	setcurjob(jp);
2541556Srgrimes	INTOFF;
2551556Srgrimes	killpg(jp->ps[0].pid, SIGCONT);
2561556Srgrimes	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
25726104Ssteve		if (WIFSTOPPED(ps->status)) {
2581556Srgrimes			ps->status = -1;
2591556Srgrimes			jp->state = 0;
2601556Srgrimes		}
2611556Srgrimes	}
2621556Srgrimes	INTON;
2631556Srgrimes}
2641556Srgrimes#endif
2651556Srgrimes
2661556Srgrimes
2671556Srgrimesint
26897669Stjrjobscmd(int argc, char *argv[])
26917987Speter{
27097669Stjr	char *id;
271163085Sstefanf	int ch, mode;
27297669Stjr
27397669Stjr	optind = optreset = 1;
274100663Stjr	opterr = 0;
275163085Sstefanf	mode = SHOWJOBS_DEFAULT;
276163085Sstefanf	while ((ch = getopt(argc, argv, "lps")) != -1) {
27797669Stjr		switch (ch) {
27897669Stjr		case 'l':
279163085Sstefanf			mode = SHOWJOBS_VERBOSE;
28097669Stjr			break;
281163085Sstefanf		case 'p':
282163085Sstefanf			mode = SHOWJOBS_PGIDS;
283163085Sstefanf			break;
28497669Stjr		case 's':
285163085Sstefanf			mode = SHOWJOBS_PIDS;
28697669Stjr			break;
28797669Stjr		case '?':
28897669Stjr		default:
28997669Stjr			error("unknown option: -%c", optopt);
29097669Stjr		}
29197669Stjr	}
29297669Stjr	argc -= optind;
29397669Stjr	argv += optind;
29497669Stjr
29597669Stjr	if (argc == 0)
296163085Sstefanf		showjobs(0, mode);
29797669Stjr	else
29897669Stjr		while ((id = *argv++) != NULL)
299163085Sstefanf			showjob(getjob(id), 0, mode);
30097669Stjr
30197669Stjr	return (0);
3021556Srgrimes}
3031556Srgrimes
304213811Sobrienstatic void
305163085Sstefanfshowjob(struct job *jp, pid_t pid, int mode)
30697663Stjr{
30797663Stjr	char s[64];
30897663Stjr	struct procstat *ps;
30997669Stjr	struct job *j;
31097669Stjr	int col, curr, i, jobno, prev, procno;
311163085Sstefanf	pid_t ppid;
31297669Stjr	char c;
3131556Srgrimes
314163085Sstefanf	procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
31597663Stjr	jobno = jp - jobtab + 1;
31697669Stjr	curr = prev = 0;
31797669Stjr#if JOBS
31897669Stjr	if ((j = getcurjob(NULL)) != NULL) {
31997669Stjr		curr = j - jobtab + 1;
32097669Stjr		if ((j = getcurjob(j)) != NULL)
32197669Stjr			prev = j - jobtab + 1;
32297669Stjr	}
32397669Stjr#endif
32497663Stjr	for (ps = jp->ps ; ; ps++) {	/* for each process */
325163085Sstefanf		if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
326163085Sstefanf			ppid = (mode == SHOWJOBS_PIDS) ? ps->pid :
327163085Sstefanf			    getpgid(ps->pid);
328163085Sstefanf			out1fmt("%d\n", (int)ppid);
32997669Stjr			goto skip;
33097669Stjr		}
331163085Sstefanf		if (mode != SHOWJOBS_VERBOSE && ps != jp->ps && pid == 0)
33297669Stjr			goto skip;
33397822Stjr		if (pid != 0 && pid != ps->pid)
33497822Stjr			goto skip;
33597819Stjr		if (jobno == curr && ps == jp->ps)
33697669Stjr			c = '+';
33797819Stjr		else if (jobno == prev && ps == jp->ps)
33897669Stjr			c = '-';
33997669Stjr		else
34097669Stjr			c = ' ';
34197663Stjr		if (ps == jp->ps)
34297669Stjr			fmtstr(s, 64, "[%d] %c ", jobno, c);
34397663Stjr		else
34497816Stjr			fmtstr(s, 64, "    %c ", c);
34597663Stjr		out1str(s);
34697663Stjr		col = strlen(s);
347163085Sstefanf		if (mode == SHOWJOBS_VERBOSE) {
348100308Stjr			fmtstr(s, 64, "%d ", (int)ps->pid);
34997669Stjr			out1str(s);
35097669Stjr			col += strlen(s);
35197669Stjr		}
35297663Stjr		s[0] = '\0';
35397819Stjr		if (ps != jp->ps) {
35497819Stjr			*s = '\0';
35597819Stjr		} else if (ps->status == -1) {
35697669Stjr			strcpy(s, "Running");
35797663Stjr		} else if (WIFEXITED(ps->status)) {
35897820Stjr			if (WEXITSTATUS(ps->status) == 0)
35997820Stjr				strcpy(s, "Done");
36097820Stjr			else
36197820Stjr				fmtstr(s, 64, "Done (%d)",
36297820Stjr				    WEXITSTATUS(ps->status));
36397663Stjr		} else {
36497663Stjr#if JOBS
365155301Sschweikh			if (WIFSTOPPED(ps->status))
36697663Stjr				i = WSTOPSIG(ps->status);
36797663Stjr			else
36897663Stjr#endif
36997663Stjr				i = WTERMSIG(ps->status);
370125155Snjl			if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F])
37197663Stjr				scopy(sys_siglist[i & 0x7F], s);
37297663Stjr			else
37397663Stjr				fmtstr(s, 64, "Signal %d", i & 0x7F);
37497663Stjr			if (WCOREDUMP(ps->status))
37597663Stjr				strcat(s, " (core dumped)");
37697663Stjr		}
37797663Stjr		out1str(s);
37897663Stjr		col += strlen(s);
37997663Stjr		do {
38097663Stjr			out1c(' ');
38197663Stjr			col++;
38297663Stjr		} while (col < 30);
38397663Stjr		out1str(ps->cmd);
38497663Stjr		out1c('\n');
38597669Stjrskip:		if (--procno <= 0)
38697663Stjr			break;
38797663Stjr	}
38897663Stjr}
38997663Stjr
3901556Srgrimes/*
3911556Srgrimes * Print a list of jobs.  If "change" is nonzero, only print jobs whose
3921556Srgrimes * statuses have changed since the last call to showjobs.
3931556Srgrimes *
3941556Srgrimes * If the shell is interrupted in the process of creating a job, the
3951556Srgrimes * result may be a job structure containing zero processes.  Such structures
3961556Srgrimes * will be freed here.
3971556Srgrimes */
3981556Srgrimes
3991556Srgrimesvoid
400163085Sstefanfshowjobs(int change, int mode)
40117987Speter{
4021556Srgrimes	int jobno;
4031556Srgrimes	struct job *jp;
4041556Srgrimes
4051556Srgrimes	TRACE(("showjobs(%d) called\n", change));
406208489Sjilles	checkzombies();
4071556Srgrimes	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
4081556Srgrimes		if (! jp->used)
4091556Srgrimes			continue;
4101556Srgrimes		if (jp->nprocs == 0) {
4111556Srgrimes			freejob(jp);
4121556Srgrimes			continue;
4131556Srgrimes		}
4141556Srgrimes		if (change && ! jp->changed)
4151556Srgrimes			continue;
416163085Sstefanf		showjob(jp, 0, mode);
4171556Srgrimes		jp->changed = 0;
418209600Sjilles		/* Hack: discard jobs for which $! has not been referenced
419209600Sjilles		 * in interactive mode when they terminate.
420209600Sjilles		 */
421209600Sjilles		if (jp->state == JOBDONE && !jp->remembered &&
422209600Sjilles				(iflag || jp != bgjob)) {
4231556Srgrimes			freejob(jp);
4241556Srgrimes		}
4251556Srgrimes	}
4261556Srgrimes}
4271556Srgrimes
4281556Srgrimes
4291556Srgrimes/*
4301556Srgrimes * Mark a job structure as unused.
4311556Srgrimes */
4321556Srgrimes
433213811Sobrienstatic void
43490111Simpfreejob(struct job *jp)
43590111Simp{
4361556Srgrimes	struct procstat *ps;
4371556Srgrimes	int i;
4381556Srgrimes
4391556Srgrimes	INTOFF;
440209600Sjilles	if (bgjob == jp)
441209600Sjilles		bgjob = NULL;
4421556Srgrimes	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
4431556Srgrimes		if (ps->cmd != nullstr)
4441556Srgrimes			ckfree(ps->cmd);
4451556Srgrimes	}
4461556Srgrimes	if (jp->ps != &jp->ps0)
4471556Srgrimes		ckfree(jp->ps);
4481556Srgrimes	jp->used = 0;
4491556Srgrimes#if JOBS
45097659Stjr	deljob(jp);
4511556Srgrimes#endif
4521556Srgrimes	INTON;
4531556Srgrimes}
4541556Srgrimes
4551556Srgrimes
4561556Srgrimes
4571556Srgrimesint
45890111Simpwaitcmd(int argc, char **argv)
45917987Speter{
4601556Srgrimes	struct job *job;
46126104Ssteve	int status, retval;
4621556Srgrimes	struct job *jp;
4631556Srgrimes
4641556Srgrimes	if (argc > 1) {
4651556Srgrimes		job = getjob(argv[1]);
4661556Srgrimes	} else {
4671556Srgrimes		job = NULL;
4681556Srgrimes	}
46938536Scracauer
47038536Scracauer	/*
47138536Scracauer	 * Loop until a process is terminated or stopped, or a SIGINT is
47238536Scracauer	 * received.
47338536Scracauer	 */
47438536Scracauer
47538521Scracauer	in_waitcmd++;
47638536Scracauer	do {
4771556Srgrimes		if (job != NULL) {
4781556Srgrimes			if (job->state) {
4791556Srgrimes				status = job->ps[job->nprocs - 1].status;
48026104Ssteve				if (WIFEXITED(status))
48126104Ssteve					retval = WEXITSTATUS(status);
4821556Srgrimes#if JOBS
48326104Ssteve				else if (WIFSTOPPED(status))
48426104Ssteve					retval = WSTOPSIG(status) + 128;
4851556Srgrimes#endif
4861556Srgrimes				else
48726104Ssteve					retval = WTERMSIG(status) + 128;
488209600Sjilles				if (! iflag || ! job->changed)
4891556Srgrimes					freejob(job);
490209600Sjilles				else {
491209600Sjilles					job->remembered = 0;
492209600Sjilles					if (job == bgjob)
493209600Sjilles						bgjob = NULL;
494209600Sjilles				}
49538536Scracauer				in_waitcmd--;
49626104Ssteve				return retval;
4971556Srgrimes			}
4981556Srgrimes		} else {
499209600Sjilles			for (jp = jobtab ; jp < jobtab + njobs; jp++)
500209600Sjilles				if (jp->used && jp->state == JOBDONE) {
501209600Sjilles					if (! iflag || ! jp->changed)
502209600Sjilles						freejob(jp);
503209600Sjilles					else {
504209600Sjilles						jp->remembered = 0;
505209600Sjilles						if (jp == bgjob)
506209600Sjilles							bgjob = NULL;
507209600Sjilles					}
508209600Sjilles				}
5091556Srgrimes			for (jp = jobtab ; ; jp++) {
5101556Srgrimes				if (jp >= jobtab + njobs) {	/* no running procs */
51138536Scracauer					in_waitcmd--;
5121556Srgrimes					return 0;
5131556Srgrimes				}
5141556Srgrimes				if (jp->used && jp->state == 0)
5151556Srgrimes					break;
5161556Srgrimes			}
5171556Srgrimes		}
51838521Scracauer	} while (dowait(1, (struct job *)NULL) != -1);
51938521Scracauer	in_waitcmd--;
52038521Scracauer
52138521Scracauer	return 0;
5221556Srgrimes}
5231556Srgrimes
5241556Srgrimes
5251556Srgrimes
52617987Speterint
52790111Simpjobidcmd(int argc __unused, char **argv)
52817987Speter{
5291556Srgrimes	struct job *jp;
5301556Srgrimes	int i;
5311556Srgrimes
5321556Srgrimes	jp = getjob(argv[1]);
5331556Srgrimes	for (i = 0 ; i < jp->nprocs ; ) {
534100308Stjr		out1fmt("%d", (int)jp->ps[i].pid);
5351556Srgrimes		out1c(++i < jp->nprocs? ' ' : '\n');
5361556Srgrimes	}
5371556Srgrimes	return 0;
5381556Srgrimes}
5391556Srgrimes
5401556Srgrimes
5411556Srgrimes
5421556Srgrimes/*
5431556Srgrimes * Convert a job name to a job structure.
5441556Srgrimes */
5451556Srgrimes
546213811Sobrienstatic struct job *
54790111Simpgetjob(char *name)
54890111Simp{
5491556Srgrimes	int jobno;
55097688Stjr	struct job *found, *jp;
551100308Stjr	pid_t pid;
5521556Srgrimes	int i;
5531556Srgrimes
5541556Srgrimes	if (name == NULL) {
5551556Srgrimes#if JOBS
55697659Stjrcurrentjob:	if ((jp = getcurjob(NULL)) == NULL)
5571556Srgrimes			error("No current job");
55897659Stjr		return (jp);
5591556Srgrimes#else
5601556Srgrimes		error("No current job");
5611556Srgrimes#endif
5621556Srgrimes	} else if (name[0] == '%') {
5631556Srgrimes		if (is_digit(name[1])) {
5641556Srgrimes			jobno = number(name + 1);
5651556Srgrimes			if (jobno > 0 && jobno <= njobs
5661556Srgrimes			 && jobtab[jobno - 1].used != 0)
5671556Srgrimes				return &jobtab[jobno - 1];
5681556Srgrimes#if JOBS
5691556Srgrimes		} else if (name[1] == '%' && name[2] == '\0') {
5701556Srgrimes			goto currentjob;
57197688Stjr		} else if (name[1] == '+' && name[2] == '\0') {
57297688Stjr			goto currentjob;
57397688Stjr		} else if (name[1] == '-' && name[2] == '\0') {
57497688Stjr			if ((jp = getcurjob(NULL)) == NULL ||
57597688Stjr			    (jp = getcurjob(jp)) == NULL)
57697688Stjr				error("No previous job");
57797688Stjr			return (jp);
5781556Srgrimes#endif
57997688Stjr		} else if (name[1] == '?') {
58097688Stjr			found = NULL;
58197688Stjr			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
58297688Stjr				if (jp->used && jp->nprocs > 0
58397688Stjr				 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
58497688Stjr					if (found)
58597688Stjr						error("%s: ambiguous", name);
58697688Stjr					found = jp;
58797688Stjr				}
58897688Stjr			}
58997688Stjr			if (found != NULL)
59097688Stjr				return (found);
5911556Srgrimes		} else {
59297688Stjr			found = NULL;
5931556Srgrimes			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
5941556Srgrimes				if (jp->used && jp->nprocs > 0
5951556Srgrimes				 && prefix(name + 1, jp->ps[0].cmd)) {
5961556Srgrimes					if (found)
5971556Srgrimes						error("%s: ambiguous", name);
5981556Srgrimes					found = jp;
5991556Srgrimes				}
6001556Srgrimes			}
6011556Srgrimes			if (found)
6021556Srgrimes				return found;
6031556Srgrimes		}
6041556Srgrimes	} else if (is_number(name)) {
605100308Stjr		pid = (pid_t)number(name);
6061556Srgrimes		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
6071556Srgrimes			if (jp->used && jp->nprocs > 0
6081556Srgrimes			 && jp->ps[jp->nprocs - 1].pid == pid)
6091556Srgrimes				return jp;
6101556Srgrimes		}
6111556Srgrimes	}
6121556Srgrimes	error("No such job: %s", name);
61317987Speter	/*NOTREACHED*/
61417987Speter	return NULL;
6151556Srgrimes}
6161556Srgrimes
6171556Srgrimes
6181556Srgrimes
6191556Srgrimes/*
6201556Srgrimes * Return a new job structure,
6211556Srgrimes */
6221556Srgrimes
6231556Srgrimesstruct job *
62490111Simpmakejob(union node *node __unused, int nprocs)
62517987Speter{
6261556Srgrimes	int i;
6271556Srgrimes	struct job *jp;
6281556Srgrimes
6291556Srgrimes	for (i = njobs, jp = jobtab ; ; jp++) {
6301556Srgrimes		if (--i < 0) {
6311556Srgrimes			INTOFF;
6321556Srgrimes			if (njobs == 0) {
6331556Srgrimes				jobtab = ckmalloc(4 * sizeof jobtab[0]);
63497664Stjr#if JOBS
63597659Stjr				jobmru = NULL;
63697664Stjr#endif
6371556Srgrimes			} else {
6381556Srgrimes				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
63917987Speter				memcpy(jp, jobtab, njobs * sizeof jp[0]);
64097659Stjr#if JOBS
64197659Stjr				/* Relocate `next' pointers and list head */
64299760Stjr				if (jobmru != NULL)
64399760Stjr					jobmru = &jp[jobmru - jobtab];
64497659Stjr				for (i = 0; i < njobs; i++)
64597659Stjr					if (jp[i].next != NULL)
64697659Stjr						jp[i].next = &jp[jp[i].next -
64797659Stjr						    jobtab];
64897659Stjr#endif
649209600Sjilles				if (bgjob != NULL)
650209600Sjilles					bgjob = &jp[bgjob - jobtab];
65120425Ssteve				/* Relocate `ps' pointers */
65220425Ssteve				for (i = 0; i < njobs; i++)
65320425Ssteve					if (jp[i].ps == &jobtab[i].ps0)
65420425Ssteve						jp[i].ps = &jp[i].ps0;
6551556Srgrimes				ckfree(jobtab);
6561556Srgrimes				jobtab = jp;
6571556Srgrimes			}
6581556Srgrimes			jp = jobtab + njobs;
6591556Srgrimes			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
6601556Srgrimes			INTON;
6611556Srgrimes			break;
6621556Srgrimes		}
6631556Srgrimes		if (jp->used == 0)
6641556Srgrimes			break;
6651556Srgrimes	}
6661556Srgrimes	INTOFF;
6671556Srgrimes	jp->state = 0;
6681556Srgrimes	jp->used = 1;
6691556Srgrimes	jp->changed = 0;
6701556Srgrimes	jp->nprocs = 0;
671100305Stjr	jp->foreground = 0;
672209600Sjilles	jp->remembered = 0;
6731556Srgrimes#if JOBS
6741556Srgrimes	jp->jobctl = jobctl;
67597659Stjr	jp->next = NULL;
6761556Srgrimes#endif
6771556Srgrimes	if (nprocs > 1) {
6781556Srgrimes		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
6791556Srgrimes	} else {
6801556Srgrimes		jp->ps = &jp->ps0;
6811556Srgrimes	}
6821556Srgrimes	INTON;
683213775Sjhb	TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
68417987Speter	    jp - jobtab + 1));
6851556Srgrimes	return jp;
6868855Srgrimes}
6871556Srgrimes
68897659Stjr#if JOBS
689213811Sobrienstatic void
69097659Stjrsetcurjob(struct job *cj)
69197659Stjr{
69297659Stjr	struct job *jp, *prev;
6931556Srgrimes
69497659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
69597659Stjr		if (jp == cj) {
69697659Stjr			if (prev != NULL)
69797659Stjr				prev->next = jp->next;
69897659Stjr			else
69997659Stjr				jobmru = jp->next;
70097659Stjr			jp->next = jobmru;
70197659Stjr			jobmru = cj;
70297659Stjr			return;
70397659Stjr		}
70497659Stjr	}
70597659Stjr	cj->next = jobmru;
70697659Stjr	jobmru = cj;
70797659Stjr}
70897659Stjr
709213811Sobrienstatic void
71097659Stjrdeljob(struct job *j)
71197659Stjr{
71297659Stjr	struct job *jp, *prev;
71397659Stjr
71497659Stjr	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
71597659Stjr		if (jp == j) {
71697659Stjr			if (prev != NULL)
71797659Stjr				prev->next = jp->next;
71897659Stjr			else
71997659Stjr				jobmru = jp->next;
72097659Stjr			return;
72197659Stjr		}
72297659Stjr	}
72397659Stjr}
72497659Stjr
7251556Srgrimes/*
72697659Stjr * Return the most recently used job that isn't `nj', and preferably one
72797659Stjr * that is stopped.
72897659Stjr */
729213811Sobrienstatic struct job *
73097659Stjrgetcurjob(struct job *nj)
73197659Stjr{
73297659Stjr	struct job *jp;
73397659Stjr
73497659Stjr	/* Try to find a stopped one.. */
73597659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
73697659Stjr		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
73797659Stjr			return (jp);
73897659Stjr	/* Otherwise the most recently used job that isn't `nj' */
73997659Stjr	for (jp = jobmru; jp != NULL; jp = jp->next)
74097659Stjr		if (jp->used && jp != nj)
74197659Stjr			return (jp);
74297659Stjr
74397659Stjr	return (NULL);
74497659Stjr}
74597659Stjr
74697659Stjr#endif
74797659Stjr
74897659Stjr/*
7491556Srgrimes * Fork of a subshell.  If we are doing job control, give the subshell its
7501556Srgrimes * own process group.  Jp is a job structure that the job is to be added to.
7511556Srgrimes * N is the command that will be evaluated by the child.  Both jp and n may
7521556Srgrimes * be NULL.  The mode parameter can be one of the following:
7531556Srgrimes *	FORK_FG - Fork off a foreground process.
7541556Srgrimes *	FORK_BG - Fork off a background process.
7551556Srgrimes *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
7561556Srgrimes *		     process group even if job control is on.
7571556Srgrimes *
7581556Srgrimes * When job control is turned off, background processes have their standard
7591556Srgrimes * input redirected to /dev/null (except for the second and later processes
7601556Srgrimes * in a pipeline).
7611556Srgrimes */
7621556Srgrimes
763100308Stjrpid_t
76490111Simpforkshell(struct job *jp, union node *n, int mode)
76517987Speter{
766100308Stjr	pid_t pid;
767100308Stjr	pid_t pgrp;
7681556Srgrimes
769213775Sjhb	TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
77017987Speter	    mode));
7711556Srgrimes	INTOFF;
772208489Sjilles	if (mode == FORK_BG)
773208489Sjilles		checkzombies();
774112341Stjr	flushall();
7751556Srgrimes	pid = fork();
7761556Srgrimes	if (pid == -1) {
7771556Srgrimes		TRACE(("Fork failed, errno=%d\n", errno));
7781556Srgrimes		INTON;
77953891Scracauer		error("Cannot fork: %s", strerror(errno));
7801556Srgrimes	}
7811556Srgrimes	if (pid == 0) {
7821556Srgrimes		struct job *p;
7831556Srgrimes		int wasroot;
7841556Srgrimes		int i;
7851556Srgrimes
786100308Stjr		TRACE(("Child shell %d\n", (int)getpid()));
7871556Srgrimes		wasroot = rootshell;
7881556Srgrimes		rootshell = 0;
789200998Sjilles		handler = &main_handler;
7901556Srgrimes		closescript();
7911556Srgrimes		INTON;
7921556Srgrimes		clear_traps();
7931556Srgrimes#if JOBS
7941556Srgrimes		jobctl = 0;		/* do job control only in root shell */
7951556Srgrimes		if (wasroot && mode != FORK_NOJOB && mflag) {
7961556Srgrimes			if (jp == NULL || jp->nprocs == 0)
7971556Srgrimes				pgrp = getpid();
7981556Srgrimes			else
7991556Srgrimes				pgrp = jp->ps[0].pid;
80021352Ssteve			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
8011556Srgrimes				/*** this causes superfluous TIOCSPGRPS ***/
80299762Stjr				if (tcsetpgrp(ttyfd, pgrp) < 0)
80320425Ssteve					error("tcsetpgrp failed, errno=%d", errno);
8041556Srgrimes			}
8051556Srgrimes			setsignal(SIGTSTP);
8061556Srgrimes			setsignal(SIGTTOU);
8071556Srgrimes		} else if (mode == FORK_BG) {
8081556Srgrimes			ignoresig(SIGINT);
8091556Srgrimes			ignoresig(SIGQUIT);
8101556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
8111556Srgrimes			    ! fd0_redirected_p ()) {
8121556Srgrimes				close(0);
81369793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
81469793Sobrien					error("Can't open %s: %s",
81569793Sobrien					    _PATH_DEVNULL, strerror(errno));
8161556Srgrimes			}
8171556Srgrimes		}
8181556Srgrimes#else
8191556Srgrimes		if (mode == FORK_BG) {
8201556Srgrimes			ignoresig(SIGINT);
8211556Srgrimes			ignoresig(SIGQUIT);
8221556Srgrimes			if ((jp == NULL || jp->nprocs == 0) &&
8231556Srgrimes			    ! fd0_redirected_p ()) {
8241556Srgrimes				close(0);
82569793Sobrien				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
826155301Sschweikh					error("Can't open %s: %s",
82769793Sobrien					    _PATH_DEVNULL, strerror(errno));
8281556Srgrimes			}
8291556Srgrimes		}
8301556Srgrimes#endif
831102051Stjr		INTOFF;
832102051Stjr		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
833102051Stjr			if (p->used)
834102051Stjr				freejob(p);
835102051Stjr		INTON;
8361556Srgrimes		if (wasroot && iflag) {
8371556Srgrimes			setsignal(SIGINT);
8381556Srgrimes			setsignal(SIGQUIT);
8391556Srgrimes			setsignal(SIGTERM);
8401556Srgrimes		}
8411556Srgrimes		return pid;
8421556Srgrimes	}
8431556Srgrimes	if (rootshell && mode != FORK_NOJOB && mflag) {
8441556Srgrimes		if (jp == NULL || jp->nprocs == 0)
8451556Srgrimes			pgrp = pid;
8461556Srgrimes		else
8471556Srgrimes			pgrp = jp->ps[0].pid;
84817987Speter		setpgid(pid, pgrp);
8491556Srgrimes	}
850209600Sjilles	if (mode == FORK_BG) {
851209600Sjilles		if (bgjob != NULL && bgjob->state == JOBDONE &&
852209600Sjilles		    !bgjob->remembered && !iflag)
853209600Sjilles			freejob(bgjob);
8541556Srgrimes		backgndpid = pid;		/* set $! */
855209600Sjilles		bgjob = jp;
856209600Sjilles	}
8571556Srgrimes	if (jp) {
8581556Srgrimes		struct procstat *ps = &jp->ps[jp->nprocs++];
8591556Srgrimes		ps->pid = pid;
8601556Srgrimes		ps->status = -1;
8611556Srgrimes		ps->cmd = nullstr;
8621556Srgrimes		if (iflag && rootshell && n)
8631556Srgrimes			ps->cmd = commandtext(n);
864100305Stjr		jp->foreground = mode == FORK_FG;
86597659Stjr#if JOBS
86697659Stjr		setcurjob(jp);
86797659Stjr#endif
8681556Srgrimes	}
8691556Srgrimes	INTON;
870100308Stjr	TRACE(("In parent shell:  child = %d\n", (int)pid));
8711556Srgrimes	return pid;
8721556Srgrimes}
8731556Srgrimes
8741556Srgrimes
8751556Srgrimes
8761556Srgrimes/*
8771556Srgrimes * Wait for job to finish.
8781556Srgrimes *
8791556Srgrimes * Under job control we have the problem that while a child process is
8801556Srgrimes * running interrupts generated by the user are sent to the child but not
8811556Srgrimes * to the shell.  This means that an infinite loop started by an inter-
8821556Srgrimes * active user may be hard to kill.  With job control turned off, an
8831556Srgrimes * interactive user may place an interactive program inside a loop.  If
8841556Srgrimes * the interactive program catches interrupts, the user doesn't want
8851556Srgrimes * these interrupts to also abort the loop.  The approach we take here
8861556Srgrimes * is to have the shell ignore interrupt signals while waiting for a
88746684Skris * foreground process to terminate, and then send itself an interrupt
8881556Srgrimes * signal if the child process was terminated by an interrupt signal.
8891556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then
8901556Srgrimes * exit on interrupt; unless these processes terminate themselves by
8911556Srgrimes * sending a signal to themselves (instead of calling exit) they will
8921556Srgrimes * confuse this approach.
8931556Srgrimes */
8941556Srgrimes
8951556Srgrimesint
89690111Simpwaitforjob(struct job *jp, int *origstatus)
89745916Scracauer{
8981556Srgrimes#if JOBS
899100308Stjr	pid_t mypgrp = getpgrp();
900208881Sjilles	int propagate_int = jp->jobctl && jp->foreground;
9011556Srgrimes#endif
9021556Srgrimes	int status;
9031556Srgrimes	int st;
9041556Srgrimes
9051556Srgrimes	INTOFF;
906213775Sjhb	TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
90738950Scracauer	while (jp->state == 0)
90838950Scracauer		if (dowait(1, jp) == -1)
90938950Scracauer			dotrap();
9101556Srgrimes#if JOBS
9111556Srgrimes	if (jp->jobctl) {
91299762Stjr		if (tcsetpgrp(ttyfd, mypgrp) < 0)
91320425Ssteve			error("tcsetpgrp failed, errno=%d\n", errno);
9141556Srgrimes	}
9151556Srgrimes	if (jp->state == JOBSTOPPED)
91697659Stjr		setcurjob(jp);
9171556Srgrimes#endif
9181556Srgrimes	status = jp->ps[jp->nprocs - 1].status;
91945916Scracauer	if (origstatus != NULL)
92045916Scracauer		*origstatus = status;
9211556Srgrimes	/* convert to 8 bits */
92226104Ssteve	if (WIFEXITED(status))
92326104Ssteve		st = WEXITSTATUS(status);
9241556Srgrimes#if JOBS
92526104Ssteve	else if (WIFSTOPPED(status))
92626104Ssteve		st = WSTOPSIG(status) + 128;
9271556Srgrimes#endif
9281556Srgrimes	else
92926104Ssteve		st = WTERMSIG(status) + 128;
9301556Srgrimes	if (! JOBS || jp->state == JOBDONE)
9311556Srgrimes		freejob(jp);
93238521Scracauer	if (int_pending()) {
93338521Scracauer		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
93438521Scracauer			kill(getpid(), SIGINT);
93538521Scracauer		else
93638521Scracauer			CLEAR_PENDING_INT;
93738521Scracauer	}
938208881Sjilles#if JOBS
939208881Sjilles	else if (rootshell && iflag && propagate_int &&
940208881Sjilles			WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
941208881Sjilles		kill(getpid(), SIGINT);
942208881Sjilles#endif
9431556Srgrimes	INTON;
9441556Srgrimes	return st;
9451556Srgrimes}
9461556Srgrimes
9471556Srgrimes
9481556Srgrimes
9491556Srgrimes/*
9501556Srgrimes * Wait for a process to terminate.
9511556Srgrimes */
9521556Srgrimes
953213811Sobrienstatic pid_t
95490111Simpdowait(int block, struct job *job)
95517987Speter{
956100308Stjr	pid_t pid;
9571556Srgrimes	int status;
9581556Srgrimes	struct procstat *sp;
9591556Srgrimes	struct job *jp;
9601556Srgrimes	struct job *thisjob;
9611556Srgrimes	int done;
9621556Srgrimes	int stopped;
96326104Ssteve	int sig;
964100305Stjr	int i;
9651556Srgrimes
96638950Scracauer	in_dowait++;
9671556Srgrimes	TRACE(("dowait(%d) called\n", block));
9681556Srgrimes	do {
9691556Srgrimes		pid = waitproc(block, &status);
970100308Stjr		TRACE(("wait returns %d, status=%d\n", (int)pid, status));
97172086Scracauer	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
972125501Scracauer		 (pid > 0 && WIFSTOPPED(status) && !iflag));
97338950Scracauer	in_dowait--;
974153417Smaxim	if (pid == -1 && errno == ECHILD && job != NULL)
975153417Smaxim		job->state = JOBDONE;
97638521Scracauer	if (breakwaitcmd != 0) {
97738521Scracauer		breakwaitcmd = 0;
978138312Smaxim		if (pid <= 0)
979138312Smaxim			return -1;
98038521Scracauer	}
9811556Srgrimes	if (pid <= 0)
9821556Srgrimes		return pid;
9831556Srgrimes	INTOFF;
9841556Srgrimes	thisjob = NULL;
9851556Srgrimes	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
9861556Srgrimes		if (jp->used) {
9871556Srgrimes			done = 1;
9881556Srgrimes			stopped = 1;
9891556Srgrimes			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
9901556Srgrimes				if (sp->pid == -1)
9911556Srgrimes					continue;
9921556Srgrimes				if (sp->pid == pid) {
99326104Ssteve					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
994100308Stjr						   (int)pid, sp->status,
995100308Stjr						   status));
9961556Srgrimes					sp->status = status;
9971556Srgrimes					thisjob = jp;
9981556Srgrimes				}
9991556Srgrimes				if (sp->status == -1)
10001556Srgrimes					stopped = 0;
100126104Ssteve				else if (WIFSTOPPED(sp->status))
10021556Srgrimes					done = 0;
10031556Srgrimes			}
10041556Srgrimes			if (stopped) {		/* stopped or done */
10051556Srgrimes				int state = done? JOBDONE : JOBSTOPPED;
10061556Srgrimes				if (jp->state != state) {
1007213775Sjhb					TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
10081556Srgrimes					jp->state = state;
1009209600Sjilles					if (jp != job) {
1010209600Sjilles						if (done && !jp->remembered &&
1011209600Sjilles						    !iflag && jp != bgjob)
1012209600Sjilles							freejob(jp);
10131556Srgrimes#if JOBS
1014209600Sjilles						else if (done)
1015209600Sjilles							deljob(jp);
10161556Srgrimes#endif
1017209600Sjilles					}
10181556Srgrimes				}
10191556Srgrimes			}
10201556Srgrimes		}
10211556Srgrimes	}
10221556Srgrimes	INTON;
10231556Srgrimes	if (! rootshell || ! iflag || (job && thisjob == job)) {
10241556Srgrimes#if JOBS
102526104Ssteve		if (WIFSTOPPED(status))
102626104Ssteve			sig = WSTOPSIG(status);
102726104Ssteve		else
10281556Srgrimes#endif
102997663Stjr		{
103026104Ssteve			if (WIFEXITED(status))
103126104Ssteve				sig = 0;
103226104Ssteve			else
103326104Ssteve				sig = WTERMSIG(status);
10341556Srgrimes		}
1035100305Stjr		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
1036107846Stjr			if (!mflag ||
1037107846Stjr			    (thisjob->foreground && !WIFSTOPPED(status))) {
1038102007Stjr				i = WTERMSIG(status);
1039125155Snjl				if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F])
1040100305Stjr					out1str(sys_siglist[i & 0x7F]);
1041100305Stjr				else
1042100305Stjr					out1fmt("Signal %d", i & 0x7F);
1043100305Stjr				if (WCOREDUMP(status))
1044100305Stjr					out1str(" (core dumped)");
1045100305Stjr				out1c('\n');
1046100305Stjr			} else
1047163085Sstefanf				showjob(thisjob, pid, SHOWJOBS_DEFAULT);
1048100305Stjr		}
10491556Srgrimes	} else {
1050109627Stjr		TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
10511556Srgrimes		if (thisjob)
10521556Srgrimes			thisjob->changed = 1;
10531556Srgrimes	}
10541556Srgrimes	return pid;
10551556Srgrimes}
10561556Srgrimes
10571556Srgrimes
10581556Srgrimes
10591556Srgrimes/*
10601556Srgrimes * Do a wait system call.  If job control is compiled in, we accept
10611556Srgrimes * stopped processes.  If block is zero, we return a value of zero
10621556Srgrimes * rather than blocking.
10631556Srgrimes */
1064213811Sobrienstatic pid_t
106590111Simpwaitproc(int block, int *status)
106617987Speter{
10671556Srgrimes	int flags;
10681556Srgrimes
10691556Srgrimes#if JOBS
10701556Srgrimes	flags = WUNTRACED;
10711556Srgrimes#else
10721556Srgrimes	flags = 0;
10731556Srgrimes#endif
10741556Srgrimes	if (block == 0)
10751556Srgrimes		flags |= WNOHANG;
10761556Srgrimes	return wait3(status, flags, (struct rusage *)NULL);
10771556Srgrimes}
10781556Srgrimes
10791556Srgrimes/*
10801556Srgrimes * return 1 if there are stopped jobs, otherwise 0
10811556Srgrimes */
10821556Srgrimesint job_warning = 0;
10831556Srgrimesint
108490111Simpstoppedjobs(void)
10851556Srgrimes{
108625222Ssteve	int jobno;
108725222Ssteve	struct job *jp;
10881556Srgrimes
10891556Srgrimes	if (job_warning)
10901556Srgrimes		return (0);
10911556Srgrimes	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
10921556Srgrimes		if (jp->used == 0)
10931556Srgrimes			continue;
10941556Srgrimes		if (jp->state == JOBSTOPPED) {
1095199629Sjilles			out2fmt_flush("You have stopped jobs.\n");
10961556Srgrimes			job_warning = 2;
10971556Srgrimes			return (1);
10981556Srgrimes		}
10991556Srgrimes	}
11001556Srgrimes
11011556Srgrimes	return (0);
11021556Srgrimes}
11031556Srgrimes
1104208489Sjilles
1105213811Sobrienstatic void
1106208489Sjillescheckzombies(void)
1107208489Sjilles{
1108208489Sjilles	while (njobs > 0 && dowait(0, NULL) > 0)
1109208489Sjilles		;
1110208489Sjilles}
1111208489Sjilles
1112208489Sjilles
1113209600Sjillesint
1114209600Sjillesbackgndpidset(void)
1115209600Sjilles{
1116209600Sjilles	return backgndpid != -1;
1117209600Sjilles}
1118209600Sjilles
1119209600Sjilles
1120209600Sjillespid_t
1121209600Sjillesbackgndpidval(void)
1122209600Sjilles{
1123209600Sjilles	if (bgjob != NULL)
1124209600Sjilles		bgjob->remembered = 1;
1125209600Sjilles	return backgndpid;
1126209600Sjilles}
1127209600Sjilles
11281556Srgrimes/*
11291556Srgrimes * Return a string identifying a command (to be printed by the
11301556Srgrimes * jobs command.
11311556Srgrimes */
11321556Srgrimes
1133213760Sobrienstatic char *cmdnextc;
1134213760Sobrienstatic int cmdnleft;
11351556Srgrimes#define MAXCMDTEXT	200
11361556Srgrimes
11371556Srgrimeschar *
113890111Simpcommandtext(union node *n)
113990111Simp{
11401556Srgrimes	char *name;
11411556Srgrimes
11421556Srgrimes	cmdnextc = name = ckmalloc(MAXCMDTEXT);
11431556Srgrimes	cmdnleft = MAXCMDTEXT - 4;
11441556Srgrimes	cmdtxt(n);
11451556Srgrimes	*cmdnextc = '\0';
11461556Srgrimes	return name;
11471556Srgrimes}
11481556Srgrimes
11491556Srgrimes
1150213811Sobrienstatic void
115190111Simpcmdtxt(union node *n)
115290111Simp{
11531556Srgrimes	union node *np;
11541556Srgrimes	struct nodelist *lp;
1155201053Sjilles	const char *p;
11561556Srgrimes	int i;
11571556Srgrimes	char s[2];
11581556Srgrimes
11591556Srgrimes	if (n == NULL)
11601556Srgrimes		return;
11611556Srgrimes	switch (n->type) {
11621556Srgrimes	case NSEMI:
11631556Srgrimes		cmdtxt(n->nbinary.ch1);
11641556Srgrimes		cmdputs("; ");
11651556Srgrimes		cmdtxt(n->nbinary.ch2);
11661556Srgrimes		break;
11671556Srgrimes	case NAND:
11681556Srgrimes		cmdtxt(n->nbinary.ch1);
11691556Srgrimes		cmdputs(" && ");
11701556Srgrimes		cmdtxt(n->nbinary.ch2);
11711556Srgrimes		break;
11721556Srgrimes	case NOR:
11731556Srgrimes		cmdtxt(n->nbinary.ch1);
11741556Srgrimes		cmdputs(" || ");
11751556Srgrimes		cmdtxt(n->nbinary.ch2);
11761556Srgrimes		break;
11771556Srgrimes	case NPIPE:
11781556Srgrimes		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
11791556Srgrimes			cmdtxt(lp->n);
11801556Srgrimes			if (lp->next)
11811556Srgrimes				cmdputs(" | ");
11821556Srgrimes		}
11831556Srgrimes		break;
11841556Srgrimes	case NSUBSHELL:
11851556Srgrimes		cmdputs("(");
11861556Srgrimes		cmdtxt(n->nredir.n);
11871556Srgrimes		cmdputs(")");
11881556Srgrimes		break;
11891556Srgrimes	case NREDIR:
11901556Srgrimes	case NBACKGND:
11911556Srgrimes		cmdtxt(n->nredir.n);
11921556Srgrimes		break;
11931556Srgrimes	case NIF:
11941556Srgrimes		cmdputs("if ");
11951556Srgrimes		cmdtxt(n->nif.test);
11961556Srgrimes		cmdputs("; then ");
11971556Srgrimes		cmdtxt(n->nif.ifpart);
11981556Srgrimes		cmdputs("...");
11991556Srgrimes		break;
12001556Srgrimes	case NWHILE:
12011556Srgrimes		cmdputs("while ");
12021556Srgrimes		goto until;
12031556Srgrimes	case NUNTIL:
12041556Srgrimes		cmdputs("until ");
12051556Srgrimesuntil:
12061556Srgrimes		cmdtxt(n->nbinary.ch1);
12071556Srgrimes		cmdputs("; do ");
12081556Srgrimes		cmdtxt(n->nbinary.ch2);
12091556Srgrimes		cmdputs("; done");
12101556Srgrimes		break;
12111556Srgrimes	case NFOR:
12121556Srgrimes		cmdputs("for ");
12131556Srgrimes		cmdputs(n->nfor.var);
12141556Srgrimes		cmdputs(" in ...");
12151556Srgrimes		break;
12161556Srgrimes	case NCASE:
12171556Srgrimes		cmdputs("case ");
12181556Srgrimes		cmdputs(n->ncase.expr->narg.text);
12191556Srgrimes		cmdputs(" in ...");
12201556Srgrimes		break;
12211556Srgrimes	case NDEFUN:
12221556Srgrimes		cmdputs(n->narg.text);
12231556Srgrimes		cmdputs("() ...");
12241556Srgrimes		break;
12251556Srgrimes	case NCMD:
12261556Srgrimes		for (np = n->ncmd.args ; np ; np = np->narg.next) {
12271556Srgrimes			cmdtxt(np);
12281556Srgrimes			if (np->narg.next)
12291556Srgrimes				cmdputs(" ");
12301556Srgrimes		}
12311556Srgrimes		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
12321556Srgrimes			cmdputs(" ");
12331556Srgrimes			cmdtxt(np);
12341556Srgrimes		}
12351556Srgrimes		break;
12361556Srgrimes	case NARG:
12371556Srgrimes		cmdputs(n->narg.text);
12381556Srgrimes		break;
12391556Srgrimes	case NTO:
12401556Srgrimes		p = ">";  i = 1;  goto redir;
12411556Srgrimes	case NAPPEND:
12421556Srgrimes		p = ">>";  i = 1;  goto redir;
12431556Srgrimes	case NTOFD:
12441556Srgrimes		p = ">&";  i = 1;  goto redir;
124596922Stjr	case NCLOBBER:
124696922Stjr		p = ">|"; i = 1; goto redir;
12471556Srgrimes	case NFROM:
12481556Srgrimes		p = "<";  i = 0;  goto redir;
124966612Sbrian	case NFROMTO:
125066612Sbrian		p = "<>";  i = 0;  goto redir;
12511556Srgrimes	case NFROMFD:
12521556Srgrimes		p = "<&";  i = 0;  goto redir;
12531556Srgrimesredir:
12541556Srgrimes		if (n->nfile.fd != i) {
12551556Srgrimes			s[0] = n->nfile.fd + '0';
12561556Srgrimes			s[1] = '\0';
12571556Srgrimes			cmdputs(s);
12581556Srgrimes		}
12591556Srgrimes		cmdputs(p);
12601556Srgrimes		if (n->type == NTOFD || n->type == NFROMFD) {
126199634Stjr			if (n->ndup.dupfd >= 0)
126299634Stjr				s[0] = n->ndup.dupfd + '0';
126399634Stjr			else
126499634Stjr				s[0] = '-';
12651556Srgrimes			s[1] = '\0';
12661556Srgrimes			cmdputs(s);
12671556Srgrimes		} else {
12681556Srgrimes			cmdtxt(n->nfile.fname);
12691556Srgrimes		}
12701556Srgrimes		break;
12711556Srgrimes	case NHERE:
12721556Srgrimes	case NXHERE:
12731556Srgrimes		cmdputs("<<...");
12741556Srgrimes		break;
12751556Srgrimes	default:
12761556Srgrimes		cmdputs("???");
12771556Srgrimes		break;
12781556Srgrimes	}
12791556Srgrimes}
12801556Srgrimes
12811556Srgrimes
12821556Srgrimes
1283213811Sobrienstatic void
1284201053Sjillescmdputs(const char *s)
128590111Simp{
1286201053Sjilles	const char *p;
1287201053Sjilles	char *q;
128825222Ssteve	char c;
12891556Srgrimes	int subtype = 0;
12901556Srgrimes
12911556Srgrimes	if (cmdnleft <= 0)
12921556Srgrimes		return;
12931556Srgrimes	p = s;
12941556Srgrimes	q = cmdnextc;
12951556Srgrimes	while ((c = *p++) != '\0') {
12961556Srgrimes		if (c == CTLESC)
12971556Srgrimes			*q++ = *p++;
12981556Srgrimes		else if (c == CTLVAR) {
12991556Srgrimes			*q++ = '$';
13001556Srgrimes			if (--cmdnleft > 0)
13011556Srgrimes				*q++ = '{';
13021556Srgrimes			subtype = *p++;
13031556Srgrimes		} else if (c == '=' && subtype != 0) {
13041556Srgrimes			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
13051556Srgrimes			subtype = 0;
13061556Srgrimes		} else if (c == CTLENDVAR) {
13071556Srgrimes			*q++ = '}';
130818954Ssteve		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
13091556Srgrimes			cmdnleft++;		/* ignore it */
13101556Srgrimes		else
13111556Srgrimes			*q++ = c;
13121556Srgrimes		if (--cmdnleft <= 0) {
13131556Srgrimes			*q++ = '.';
13141556Srgrimes			*q++ = '.';
13151556Srgrimes			*q++ = '.';
13161556Srgrimes			break;
13171556Srgrimes		}
13181556Srgrimes	}
13191556Srgrimes	cmdnextc = q;
13201556Srgrimes}
1321