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