jobs.c revision 155301
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)jobs.c	8.5 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/jobs.c 155301 2006-02-04 14:37:50Z schweikh $");
40
41#include <fcntl.h>
42#include <signal.h>
43#include <errno.h>
44#include <paths.h>
45#include <unistd.h>
46#include <stdlib.h>
47#include <sys/param.h>
48#include <sys/wait.h>
49#include <sys/time.h>
50#include <sys/resource.h>
51#include <paths.h>
52#include <sys/ioctl.h>
53
54#include "shell.h"
55#if JOBS
56#include <termios.h>
57#undef CEOF			/* syntax.h redefines this */
58#endif
59#include "redir.h"
60#include "show.h"
61#include "main.h"
62#include "parser.h"
63#include "nodes.h"
64#include "jobs.h"
65#include "options.h"
66#include "trap.h"
67#include "syntax.h"
68#include "input.h"
69#include "output.h"
70#include "memalloc.h"
71#include "error.h"
72#include "mystring.h"
73
74
75STATIC struct job *jobtab;	/* array of jobs */
76STATIC int njobs;		/* size of array */
77MKINIT pid_t backgndpid = -1;	/* pid of last background process */
78#if JOBS
79STATIC struct job *jobmru;	/* most recently used job list */
80STATIC pid_t initialpgrp;	/* pgrp of shell on invocation */
81#endif
82int in_waitcmd = 0;		/* are we in waitcmd()? */
83int in_dowait = 0;		/* are we in dowait()? */
84volatile sig_atomic_t breakwaitcmd = 0;	/* should wait be terminated? */
85static int ttyfd = -1;
86
87#if JOBS
88STATIC void restartjob(struct job *);
89#endif
90STATIC void freejob(struct job *);
91STATIC struct job *getjob(char *);
92STATIC pid_t dowait(int, struct job *);
93STATIC pid_t waitproc(int, int *);
94STATIC void cmdtxt(union node *);
95STATIC void cmdputs(char *);
96#if JOBS
97STATIC void setcurjob(struct job *);
98STATIC void deljob(struct job *);
99STATIC struct job *getcurjob(struct job *);
100#endif
101STATIC void showjob(struct job *, pid_t, int, int);
102
103
104/*
105 * Turn job control on and off.
106 */
107
108MKINIT int jobctl;
109
110#if JOBS
111void
112setjobctl(int on)
113{
114	int i;
115
116	if (on == jobctl || rootshell == 0)
117		return;
118	if (on) {
119		if (ttyfd != -1)
120			close(ttyfd);
121		if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
122			i = 0;
123			while (i <= 2 && !isatty(i))
124				i++;
125			if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
126				goto out;
127		}
128		if (ttyfd < 10) {
129			/*
130			 * Keep our TTY file descriptor out of the way of
131			 * the user's redirections.
132			 */
133			if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
134				close(ttyfd);
135				ttyfd = -1;
136				goto out;
137			}
138			close(ttyfd);
139			ttyfd = i;
140		}
141		if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
142			close(ttyfd);
143			ttyfd = -1;
144			goto out;
145		}
146		do { /* while we are in the background */
147			initialpgrp = tcgetpgrp(ttyfd);
148			if (initialpgrp < 0) {
149out:				out2str("sh: can't access tty; job control turned off\n");
150				mflag = 0;
151				return;
152			}
153			if (initialpgrp == -1)
154				initialpgrp = getpgrp();
155			else if (initialpgrp != getpgrp()) {
156				killpg(0, SIGTTIN);
157				continue;
158			}
159		} while (0);
160		setsignal(SIGTSTP);
161		setsignal(SIGTTOU);
162		setsignal(SIGTTIN);
163		setpgid(0, rootpid);
164		tcsetpgrp(ttyfd, rootpid);
165	} else { /* turning job control off */
166		setpgid(0, initialpgrp);
167		tcsetpgrp(ttyfd, initialpgrp);
168		close(ttyfd);
169		ttyfd = -1;
170		setsignal(SIGTSTP);
171		setsignal(SIGTTOU);
172		setsignal(SIGTTIN);
173	}
174	jobctl = on;
175}
176#endif
177
178
179#ifdef mkinit
180INCLUDE <sys/types.h>
181INCLUDE <stdlib.h>
182
183SHELLPROC {
184	backgndpid = -1;
185#if JOBS
186	jobctl = 0;
187#endif
188}
189
190#endif
191
192
193
194#if JOBS
195int
196fgcmd(int argc __unused, char **argv)
197{
198	struct job *jp;
199	pid_t pgrp;
200	int status;
201
202	jp = getjob(argv[1]);
203	if (jp->jobctl == 0)
204		error("job not created under job control");
205	out1str(jp->ps[0].cmd);
206	out1c('\n');
207	flushout(&output);
208	pgrp = jp->ps[0].pid;
209	tcsetpgrp(ttyfd, pgrp);
210	restartjob(jp);
211	jp->foreground = 1;
212	INTOFF;
213	status = waitforjob(jp, (int *)NULL);
214	INTON;
215	return status;
216}
217
218
219int
220bgcmd(int argc, char **argv)
221{
222	char s[64];
223	struct job *jp;
224
225	do {
226		jp = getjob(*++argv);
227		if (jp->jobctl == 0)
228			error("job not created under job control");
229		if (jp->state == JOBDONE)
230			continue;
231		restartjob(jp);
232		jp->foreground = 0;
233		fmtstr(s, 64, "[%td] ", jp - jobtab + 1);
234		out1str(s);
235		out1str(jp->ps[0].cmd);
236		out1c('\n');
237	} while (--argc > 1);
238	return 0;
239}
240
241
242STATIC void
243restartjob(struct job *jp)
244{
245	struct procstat *ps;
246	int i;
247
248	if (jp->state == JOBDONE)
249		return;
250	setcurjob(jp);
251	INTOFF;
252	killpg(jp->ps[0].pid, SIGCONT);
253	for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
254		if (WIFSTOPPED(ps->status)) {
255			ps->status = -1;
256			jp->state = 0;
257		}
258	}
259	INTON;
260}
261#endif
262
263
264int
265jobscmd(int argc, char *argv[])
266{
267	char *id;
268	int ch, sformat, lformat;
269
270	optind = optreset = 1;
271	opterr = 0;
272	sformat = lformat = 0;
273	while ((ch = getopt(argc, argv, "ls")) != -1) {
274		switch (ch) {
275		case 'l':
276			lformat = 1;
277			break;
278		case 's':
279			sformat = 1;
280			break;
281		case '?':
282		default:
283			error("unknown option: -%c", optopt);
284		}
285	}
286	argc -= optind;
287	argv += optind;
288
289	if (argc == 0)
290		showjobs(0, sformat, lformat);
291	else
292		while ((id = *argv++) != NULL)
293			showjob(getjob(id), 0, sformat, lformat);
294
295	return (0);
296}
297
298STATIC void
299showjob(struct job *jp, pid_t pid, int sformat, int lformat)
300{
301	char s[64];
302	struct procstat *ps;
303	struct job *j;
304	int col, curr, i, jobno, prev, procno;
305	char c;
306
307	procno = jp->nprocs;
308	jobno = jp - jobtab + 1;
309	curr = prev = 0;
310#if JOBS
311	if ((j = getcurjob(NULL)) != NULL) {
312		curr = j - jobtab + 1;
313		if ((j = getcurjob(j)) != NULL)
314			prev = j - jobtab + 1;
315	}
316#endif
317	for (ps = jp->ps ; ; ps++) {	/* for each process */
318		if (sformat) {
319			out1fmt("%d\n", (int)ps->pid);
320			goto skip;
321		}
322		if (!lformat && ps != jp->ps && pid == 0)
323			goto skip;
324		if (pid != 0 && pid != ps->pid)
325			goto skip;
326		if (jobno == curr && ps == jp->ps)
327			c = '+';
328		else if (jobno == prev && ps == jp->ps)
329			c = '-';
330		else
331			c = ' ';
332		if (ps == jp->ps)
333			fmtstr(s, 64, "[%d] %c ", jobno, c);
334		else
335			fmtstr(s, 64, "    %c ", c);
336		out1str(s);
337		col = strlen(s);
338		if (lformat) {
339			fmtstr(s, 64, "%d ", (int)ps->pid);
340			out1str(s);
341			col += strlen(s);
342		}
343		s[0] = '\0';
344		if (ps != jp->ps) {
345			*s = '\0';
346		} else if (ps->status == -1) {
347			strcpy(s, "Running");
348		} else if (WIFEXITED(ps->status)) {
349			if (WEXITSTATUS(ps->status) == 0)
350				strcpy(s, "Done");
351			else
352				fmtstr(s, 64, "Done (%d)",
353				    WEXITSTATUS(ps->status));
354		} else {
355#if JOBS
356			if (WIFSTOPPED(ps->status))
357				i = WSTOPSIG(ps->status);
358			else
359#endif
360				i = WTERMSIG(ps->status);
361			if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F])
362				scopy(sys_siglist[i & 0x7F], s);
363			else
364				fmtstr(s, 64, "Signal %d", i & 0x7F);
365			if (WCOREDUMP(ps->status))
366				strcat(s, " (core dumped)");
367		}
368		out1str(s);
369		col += strlen(s);
370		do {
371			out1c(' ');
372			col++;
373		} while (col < 30);
374		out1str(ps->cmd);
375		out1c('\n');
376skip:		if (--procno <= 0)
377			break;
378	}
379}
380
381/*
382 * Print a list of jobs.  If "change" is nonzero, only print jobs whose
383 * statuses have changed since the last call to showjobs.
384 *
385 * If the shell is interrupted in the process of creating a job, the
386 * result may be a job structure containing zero processes.  Such structures
387 * will be freed here.
388 */
389
390void
391showjobs(int change, int sformat, int lformat)
392{
393	int jobno;
394	struct job *jp;
395
396	TRACE(("showjobs(%d) called\n", change));
397	while (dowait(0, (struct job *)NULL) > 0);
398	for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
399		if (! jp->used)
400			continue;
401		if (jp->nprocs == 0) {
402			freejob(jp);
403			continue;
404		}
405		if (change && ! jp->changed)
406			continue;
407		showjob(jp, 0, sformat, lformat);
408		jp->changed = 0;
409		if (jp->state == JOBDONE) {
410			freejob(jp);
411		}
412	}
413}
414
415
416/*
417 * Mark a job structure as unused.
418 */
419
420STATIC void
421freejob(struct job *jp)
422{
423	struct procstat *ps;
424	int i;
425
426	INTOFF;
427	for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
428		if (ps->cmd != nullstr)
429			ckfree(ps->cmd);
430	}
431	if (jp->ps != &jp->ps0)
432		ckfree(jp->ps);
433	jp->used = 0;
434#if JOBS
435	deljob(jp);
436#endif
437	INTON;
438}
439
440
441
442int
443waitcmd(int argc, char **argv)
444{
445	struct job *job;
446	int status, retval;
447	struct job *jp;
448
449	if (argc > 1) {
450		job = getjob(argv[1]);
451	} else {
452		job = NULL;
453	}
454
455	/*
456	 * Loop until a process is terminated or stopped, or a SIGINT is
457	 * received.
458	 */
459
460	in_waitcmd++;
461	do {
462		if (job != NULL) {
463			if (job->state) {
464				status = job->ps[job->nprocs - 1].status;
465				if (WIFEXITED(status))
466					retval = WEXITSTATUS(status);
467#if JOBS
468				else if (WIFSTOPPED(status))
469					retval = WSTOPSIG(status) + 128;
470#endif
471				else
472					retval = WTERMSIG(status) + 128;
473				if (! iflag)
474					freejob(job);
475				in_waitcmd--;
476				return retval;
477			}
478		} else {
479			for (jp = jobtab ; ; jp++) {
480				if (jp >= jobtab + njobs) {	/* no running procs */
481					in_waitcmd--;
482					return 0;
483				}
484				if (jp->used && jp->state == 0)
485					break;
486			}
487		}
488	} while (dowait(1, (struct job *)NULL) != -1);
489	in_waitcmd--;
490
491	return 0;
492}
493
494
495
496int
497jobidcmd(int argc __unused, char **argv)
498{
499	struct job *jp;
500	int i;
501
502	jp = getjob(argv[1]);
503	for (i = 0 ; i < jp->nprocs ; ) {
504		out1fmt("%d", (int)jp->ps[i].pid);
505		out1c(++i < jp->nprocs? ' ' : '\n');
506	}
507	return 0;
508}
509
510
511
512/*
513 * Convert a job name to a job structure.
514 */
515
516STATIC struct job *
517getjob(char *name)
518{
519	int jobno;
520	struct job *found, *jp;
521	pid_t pid;
522	int i;
523
524	if (name == NULL) {
525#if JOBS
526currentjob:	if ((jp = getcurjob(NULL)) == NULL)
527			error("No current job");
528		return (jp);
529#else
530		error("No current job");
531#endif
532	} else if (name[0] == '%') {
533		if (is_digit(name[1])) {
534			jobno = number(name + 1);
535			if (jobno > 0 && jobno <= njobs
536			 && jobtab[jobno - 1].used != 0)
537				return &jobtab[jobno - 1];
538#if JOBS
539		} else if (name[1] == '%' && name[2] == '\0') {
540			goto currentjob;
541		} else if (name[1] == '+' && name[2] == '\0') {
542			goto currentjob;
543		} else if (name[1] == '-' && name[2] == '\0') {
544			if ((jp = getcurjob(NULL)) == NULL ||
545			    (jp = getcurjob(jp)) == NULL)
546				error("No previous job");
547			return (jp);
548#endif
549		} else if (name[1] == '?') {
550			found = NULL;
551			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
552				if (jp->used && jp->nprocs > 0
553				 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
554					if (found)
555						error("%s: ambiguous", name);
556					found = jp;
557				}
558			}
559			if (found != NULL)
560				return (found);
561		} else {
562			found = NULL;
563			for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
564				if (jp->used && jp->nprocs > 0
565				 && prefix(name + 1, jp->ps[0].cmd)) {
566					if (found)
567						error("%s: ambiguous", name);
568					found = jp;
569				}
570			}
571			if (found)
572				return found;
573		}
574	} else if (is_number(name)) {
575		pid = (pid_t)number(name);
576		for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
577			if (jp->used && jp->nprocs > 0
578			 && jp->ps[jp->nprocs - 1].pid == pid)
579				return jp;
580		}
581	}
582	error("No such job: %s", name);
583	/*NOTREACHED*/
584	return NULL;
585}
586
587
588
589/*
590 * Return a new job structure,
591 */
592
593struct job *
594makejob(union node *node __unused, int nprocs)
595{
596	int i;
597	struct job *jp;
598
599	for (i = njobs, jp = jobtab ; ; jp++) {
600		if (--i < 0) {
601			INTOFF;
602			if (njobs == 0) {
603				jobtab = ckmalloc(4 * sizeof jobtab[0]);
604#if JOBS
605				jobmru = NULL;
606#endif
607			} else {
608				jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
609				memcpy(jp, jobtab, njobs * sizeof jp[0]);
610#if JOBS
611				/* Relocate `next' pointers and list head */
612				if (jobmru != NULL)
613					jobmru = &jp[jobmru - jobtab];
614				for (i = 0; i < njobs; i++)
615					if (jp[i].next != NULL)
616						jp[i].next = &jp[jp[i].next -
617						    jobtab];
618#endif
619				/* Relocate `ps' pointers */
620				for (i = 0; i < njobs; i++)
621					if (jp[i].ps == &jobtab[i].ps0)
622						jp[i].ps = &jp[i].ps0;
623				ckfree(jobtab);
624				jobtab = jp;
625			}
626			jp = jobtab + njobs;
627			for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
628			INTON;
629			break;
630		}
631		if (jp->used == 0)
632			break;
633	}
634	INTOFF;
635	jp->state = 0;
636	jp->used = 1;
637	jp->changed = 0;
638	jp->nprocs = 0;
639	jp->foreground = 0;
640#if JOBS
641	jp->jobctl = jobctl;
642	jp->next = NULL;
643#endif
644	if (nprocs > 1) {
645		jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
646	} else {
647		jp->ps = &jp->ps0;
648	}
649	INTON;
650	TRACE(("makejob(%p, %d) returns %%%d\n", (void *)node, nprocs,
651	    jp - jobtab + 1));
652	return jp;
653}
654
655#if JOBS
656STATIC void
657setcurjob(struct job *cj)
658{
659	struct job *jp, *prev;
660
661	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
662		if (jp == cj) {
663			if (prev != NULL)
664				prev->next = jp->next;
665			else
666				jobmru = jp->next;
667			jp->next = jobmru;
668			jobmru = cj;
669			return;
670		}
671	}
672	cj->next = jobmru;
673	jobmru = cj;
674}
675
676STATIC void
677deljob(struct job *j)
678{
679	struct job *jp, *prev;
680
681	for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
682		if (jp == j) {
683			if (prev != NULL)
684				prev->next = jp->next;
685			else
686				jobmru = jp->next;
687			return;
688		}
689	}
690}
691
692/*
693 * Return the most recently used job that isn't `nj', and preferably one
694 * that is stopped.
695 */
696STATIC struct job *
697getcurjob(struct job *nj)
698{
699	struct job *jp;
700
701	/* Try to find a stopped one.. */
702	for (jp = jobmru; jp != NULL; jp = jp->next)
703		if (jp->used && jp != nj && jp->state == JOBSTOPPED)
704			return (jp);
705	/* Otherwise the most recently used job that isn't `nj' */
706	for (jp = jobmru; jp != NULL; jp = jp->next)
707		if (jp->used && jp != nj)
708			return (jp);
709
710	return (NULL);
711}
712
713#endif
714
715/*
716 * Fork of a subshell.  If we are doing job control, give the subshell its
717 * own process group.  Jp is a job structure that the job is to be added to.
718 * N is the command that will be evaluated by the child.  Both jp and n may
719 * be NULL.  The mode parameter can be one of the following:
720 *	FORK_FG - Fork off a foreground process.
721 *	FORK_BG - Fork off a background process.
722 *	FORK_NOJOB - Like FORK_FG, but don't give the process its own
723 *		     process group even if job control is on.
724 *
725 * When job control is turned off, background processes have their standard
726 * input redirected to /dev/null (except for the second and later processes
727 * in a pipeline).
728 */
729
730pid_t
731forkshell(struct job *jp, union node *n, int mode)
732{
733	pid_t pid;
734	pid_t pgrp;
735
736	TRACE(("forkshell(%%%d, %p, %d) called\n", jp - jobtab, (void *)n,
737	    mode));
738	INTOFF;
739	flushall();
740	pid = fork();
741	if (pid == -1) {
742		TRACE(("Fork failed, errno=%d\n", errno));
743		INTON;
744		error("Cannot fork: %s", strerror(errno));
745	}
746	if (pid == 0) {
747		struct job *p;
748		int wasroot;
749		int i;
750
751		TRACE(("Child shell %d\n", (int)getpid()));
752		wasroot = rootshell;
753		rootshell = 0;
754		closescript();
755		INTON;
756		clear_traps();
757#if JOBS
758		jobctl = 0;		/* do job control only in root shell */
759		if (wasroot && mode != FORK_NOJOB && mflag) {
760			if (jp == NULL || jp->nprocs == 0)
761				pgrp = getpid();
762			else
763				pgrp = jp->ps[0].pid;
764			if (setpgid(0, pgrp) == 0 && mode == FORK_FG) {
765				/*** this causes superfluous TIOCSPGRPS ***/
766				if (tcsetpgrp(ttyfd, pgrp) < 0)
767					error("tcsetpgrp failed, errno=%d", errno);
768			}
769			setsignal(SIGTSTP);
770			setsignal(SIGTTOU);
771		} else if (mode == FORK_BG) {
772			ignoresig(SIGINT);
773			ignoresig(SIGQUIT);
774			if ((jp == NULL || jp->nprocs == 0) &&
775			    ! fd0_redirected_p ()) {
776				close(0);
777				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
778					error("Can't open %s: %s",
779					    _PATH_DEVNULL, strerror(errno));
780			}
781		}
782#else
783		if (mode == FORK_BG) {
784			ignoresig(SIGINT);
785			ignoresig(SIGQUIT);
786			if ((jp == NULL || jp->nprocs == 0) &&
787			    ! fd0_redirected_p ()) {
788				close(0);
789				if (open(_PATH_DEVNULL, O_RDONLY) != 0)
790					error("Can't open %s: %s",
791					    _PATH_DEVNULL, strerror(errno));
792			}
793		}
794#endif
795		INTOFF;
796		for (i = njobs, p = jobtab ; --i >= 0 ; p++)
797			if (p->used)
798				freejob(p);
799		INTON;
800		if (wasroot && iflag) {
801			setsignal(SIGINT);
802			setsignal(SIGQUIT);
803			setsignal(SIGTERM);
804		}
805		return pid;
806	}
807	if (rootshell && mode != FORK_NOJOB && mflag) {
808		if (jp == NULL || jp->nprocs == 0)
809			pgrp = pid;
810		else
811			pgrp = jp->ps[0].pid;
812		setpgid(pid, pgrp);
813	}
814	if (mode == FORK_BG)
815		backgndpid = pid;		/* set $! */
816	if (jp) {
817		struct procstat *ps = &jp->ps[jp->nprocs++];
818		ps->pid = pid;
819		ps->status = -1;
820		ps->cmd = nullstr;
821		if (iflag && rootshell && n)
822			ps->cmd = commandtext(n);
823		jp->foreground = mode == FORK_FG;
824#if JOBS
825		setcurjob(jp);
826#endif
827	}
828	INTON;
829	TRACE(("In parent shell:  child = %d\n", (int)pid));
830	return pid;
831}
832
833
834
835/*
836 * Wait for job to finish.
837 *
838 * Under job control we have the problem that while a child process is
839 * running interrupts generated by the user are sent to the child but not
840 * to the shell.  This means that an infinite loop started by an inter-
841 * active user may be hard to kill.  With job control turned off, an
842 * interactive user may place an interactive program inside a loop.  If
843 * the interactive program catches interrupts, the user doesn't want
844 * these interrupts to also abort the loop.  The approach we take here
845 * is to have the shell ignore interrupt signals while waiting for a
846 * foreground process to terminate, and then send itself an interrupt
847 * signal if the child process was terminated by an interrupt signal.
848 * Unfortunately, some programs want to do a bit of cleanup and then
849 * exit on interrupt; unless these processes terminate themselves by
850 * sending a signal to themselves (instead of calling exit) they will
851 * confuse this approach.
852 */
853
854int
855waitforjob(struct job *jp, int *origstatus)
856{
857#if JOBS
858	pid_t mypgrp = getpgrp();
859#endif
860	int status;
861	int st;
862
863	INTOFF;
864	TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
865	while (jp->state == 0)
866		if (dowait(1, jp) == -1)
867			dotrap();
868#if JOBS
869	if (jp->jobctl) {
870		if (tcsetpgrp(ttyfd, mypgrp) < 0)
871			error("tcsetpgrp failed, errno=%d\n", errno);
872	}
873	if (jp->state == JOBSTOPPED)
874		setcurjob(jp);
875#endif
876	status = jp->ps[jp->nprocs - 1].status;
877	if (origstatus != NULL)
878		*origstatus = status;
879	/* convert to 8 bits */
880	if (WIFEXITED(status))
881		st = WEXITSTATUS(status);
882#if JOBS
883	else if (WIFSTOPPED(status))
884		st = WSTOPSIG(status) + 128;
885#endif
886	else
887		st = WTERMSIG(status) + 128;
888	if (! JOBS || jp->state == JOBDONE)
889		freejob(jp);
890	if (int_pending()) {
891		if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
892			kill(getpid(), SIGINT);
893		else
894			CLEAR_PENDING_INT;
895	}
896	INTON;
897	return st;
898}
899
900
901
902/*
903 * Wait for a process to terminate.
904 */
905
906STATIC pid_t
907dowait(int block, struct job *job)
908{
909	pid_t pid;
910	int status;
911	struct procstat *sp;
912	struct job *jp;
913	struct job *thisjob;
914	int done;
915	int stopped;
916	int sig;
917	int i;
918
919	in_dowait++;
920	TRACE(("dowait(%d) called\n", block));
921	do {
922		pid = waitproc(block, &status);
923		TRACE(("wait returns %d, status=%d\n", (int)pid, status));
924	} while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) ||
925		 (pid > 0 && WIFSTOPPED(status) && !iflag));
926	in_dowait--;
927	if (pid == -1 && errno == ECHILD && job != NULL)
928		job->state = JOBDONE;
929	if (breakwaitcmd != 0) {
930		breakwaitcmd = 0;
931		if (pid <= 0)
932			return -1;
933	}
934	if (pid <= 0)
935		return pid;
936	INTOFF;
937	thisjob = NULL;
938	for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
939		if (jp->used) {
940			done = 1;
941			stopped = 1;
942			for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
943				if (sp->pid == -1)
944					continue;
945				if (sp->pid == pid) {
946					TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
947						   (int)pid, sp->status,
948						   status));
949					sp->status = status;
950					thisjob = jp;
951				}
952				if (sp->status == -1)
953					stopped = 0;
954				else if (WIFSTOPPED(sp->status))
955					done = 0;
956			}
957			if (stopped) {		/* stopped or done */
958				int state = done? JOBDONE : JOBSTOPPED;
959				if (jp->state != state) {
960					TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
961					jp->state = state;
962#if JOBS
963					if (done)
964						deljob(jp);
965#endif
966				}
967			}
968		}
969	}
970	INTON;
971	if (! rootshell || ! iflag || (job && thisjob == job)) {
972#if JOBS
973		if (WIFSTOPPED(status))
974			sig = WSTOPSIG(status);
975		else
976#endif
977		{
978			if (WIFEXITED(status))
979				sig = 0;
980			else
981				sig = WTERMSIG(status);
982		}
983		if (sig != 0 && sig != SIGINT && sig != SIGPIPE) {
984			if (!mflag ||
985			    (thisjob->foreground && !WIFSTOPPED(status))) {
986				i = WTERMSIG(status);
987				if ((i & 0x7F) < sys_nsig && sys_siglist[i & 0x7F])
988					out1str(sys_siglist[i & 0x7F]);
989				else
990					out1fmt("Signal %d", i & 0x7F);
991				if (WCOREDUMP(status))
992					out1str(" (core dumped)");
993				out1c('\n');
994			} else
995				showjob(thisjob, pid, 0, 0);
996		}
997	} else {
998		TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
999		if (thisjob)
1000			thisjob->changed = 1;
1001	}
1002	return pid;
1003}
1004
1005
1006
1007/*
1008 * Do a wait system call.  If job control is compiled in, we accept
1009 * stopped processes.  If block is zero, we return a value of zero
1010 * rather than blocking.
1011 */
1012STATIC pid_t
1013waitproc(int block, int *status)
1014{
1015	int flags;
1016
1017#if JOBS
1018	flags = WUNTRACED;
1019#else
1020	flags = 0;
1021#endif
1022	if (block == 0)
1023		flags |= WNOHANG;
1024	return wait3(status, flags, (struct rusage *)NULL);
1025}
1026
1027/*
1028 * return 1 if there are stopped jobs, otherwise 0
1029 */
1030int job_warning = 0;
1031int
1032stoppedjobs(void)
1033{
1034	int jobno;
1035	struct job *jp;
1036
1037	if (job_warning)
1038		return (0);
1039	for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1040		if (jp->used == 0)
1041			continue;
1042		if (jp->state == JOBSTOPPED) {
1043			out2str("You have stopped jobs.\n");
1044			job_warning = 2;
1045			return (1);
1046		}
1047	}
1048
1049	return (0);
1050}
1051
1052/*
1053 * Return a string identifying a command (to be printed by the
1054 * jobs command.
1055 */
1056
1057STATIC char *cmdnextc;
1058STATIC int cmdnleft;
1059#define MAXCMDTEXT	200
1060
1061char *
1062commandtext(union node *n)
1063{
1064	char *name;
1065
1066	cmdnextc = name = ckmalloc(MAXCMDTEXT);
1067	cmdnleft = MAXCMDTEXT - 4;
1068	cmdtxt(n);
1069	*cmdnextc = '\0';
1070	return name;
1071}
1072
1073
1074STATIC void
1075cmdtxt(union node *n)
1076{
1077	union node *np;
1078	struct nodelist *lp;
1079	char *p;
1080	int i;
1081	char s[2];
1082
1083	if (n == NULL)
1084		return;
1085	switch (n->type) {
1086	case NSEMI:
1087		cmdtxt(n->nbinary.ch1);
1088		cmdputs("; ");
1089		cmdtxt(n->nbinary.ch2);
1090		break;
1091	case NAND:
1092		cmdtxt(n->nbinary.ch1);
1093		cmdputs(" && ");
1094		cmdtxt(n->nbinary.ch2);
1095		break;
1096	case NOR:
1097		cmdtxt(n->nbinary.ch1);
1098		cmdputs(" || ");
1099		cmdtxt(n->nbinary.ch2);
1100		break;
1101	case NPIPE:
1102		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1103			cmdtxt(lp->n);
1104			if (lp->next)
1105				cmdputs(" | ");
1106		}
1107		break;
1108	case NSUBSHELL:
1109		cmdputs("(");
1110		cmdtxt(n->nredir.n);
1111		cmdputs(")");
1112		break;
1113	case NREDIR:
1114	case NBACKGND:
1115		cmdtxt(n->nredir.n);
1116		break;
1117	case NIF:
1118		cmdputs("if ");
1119		cmdtxt(n->nif.test);
1120		cmdputs("; then ");
1121		cmdtxt(n->nif.ifpart);
1122		cmdputs("...");
1123		break;
1124	case NWHILE:
1125		cmdputs("while ");
1126		goto until;
1127	case NUNTIL:
1128		cmdputs("until ");
1129until:
1130		cmdtxt(n->nbinary.ch1);
1131		cmdputs("; do ");
1132		cmdtxt(n->nbinary.ch2);
1133		cmdputs("; done");
1134		break;
1135	case NFOR:
1136		cmdputs("for ");
1137		cmdputs(n->nfor.var);
1138		cmdputs(" in ...");
1139		break;
1140	case NCASE:
1141		cmdputs("case ");
1142		cmdputs(n->ncase.expr->narg.text);
1143		cmdputs(" in ...");
1144		break;
1145	case NDEFUN:
1146		cmdputs(n->narg.text);
1147		cmdputs("() ...");
1148		break;
1149	case NCMD:
1150		for (np = n->ncmd.args ; np ; np = np->narg.next) {
1151			cmdtxt(np);
1152			if (np->narg.next)
1153				cmdputs(" ");
1154		}
1155		for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1156			cmdputs(" ");
1157			cmdtxt(np);
1158		}
1159		break;
1160	case NARG:
1161		cmdputs(n->narg.text);
1162		break;
1163	case NTO:
1164		p = ">";  i = 1;  goto redir;
1165	case NAPPEND:
1166		p = ">>";  i = 1;  goto redir;
1167	case NTOFD:
1168		p = ">&";  i = 1;  goto redir;
1169	case NCLOBBER:
1170		p = ">|"; i = 1; goto redir;
1171	case NFROM:
1172		p = "<";  i = 0;  goto redir;
1173	case NFROMTO:
1174		p = "<>";  i = 0;  goto redir;
1175	case NFROMFD:
1176		p = "<&";  i = 0;  goto redir;
1177redir:
1178		if (n->nfile.fd != i) {
1179			s[0] = n->nfile.fd + '0';
1180			s[1] = '\0';
1181			cmdputs(s);
1182		}
1183		cmdputs(p);
1184		if (n->type == NTOFD || n->type == NFROMFD) {
1185			if (n->ndup.dupfd >= 0)
1186				s[0] = n->ndup.dupfd + '0';
1187			else
1188				s[0] = '-';
1189			s[1] = '\0';
1190			cmdputs(s);
1191		} else {
1192			cmdtxt(n->nfile.fname);
1193		}
1194		break;
1195	case NHERE:
1196	case NXHERE:
1197		cmdputs("<<...");
1198		break;
1199	default:
1200		cmdputs("???");
1201		break;
1202	}
1203}
1204
1205
1206
1207STATIC void
1208cmdputs(char *s)
1209{
1210	char *p, *q;
1211	char c;
1212	int subtype = 0;
1213
1214	if (cmdnleft <= 0)
1215		return;
1216	p = s;
1217	q = cmdnextc;
1218	while ((c = *p++) != '\0') {
1219		if (c == CTLESC)
1220			*q++ = *p++;
1221		else if (c == CTLVAR) {
1222			*q++ = '$';
1223			if (--cmdnleft > 0)
1224				*q++ = '{';
1225			subtype = *p++;
1226		} else if (c == '=' && subtype != 0) {
1227			*q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
1228			subtype = 0;
1229		} else if (c == CTLENDVAR) {
1230			*q++ = '}';
1231		} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE)
1232			cmdnleft++;		/* ignore it */
1233		else
1234			*q++ = c;
1235		if (--cmdnleft <= 0) {
1236			*q++ = '.';
1237			*q++ = '.';
1238			*q++ = '.';
1239			break;
1240		}
1241	}
1242	cmdnextc = q;
1243}
1244