Home | History | Annotate | Line # | Download | only in ksh
jobs.c revision 1.7
      1  1.7       agc /*	$NetBSD: jobs.c,v 1.7 2003/06/23 11:38:58 agc Exp $	*/
      2  1.2       tls 
      3  1.1       jtc /*
      4  1.1       jtc  * Process and job control
      5  1.1       jtc  */
      6  1.1       jtc 
      7  1.1       jtc /*
      8  1.1       jtc  * Reworked/Rewritten version of Eric Gisin's/Ron Natalie's code by
      9  1.1       jtc  * Larry Bouzane (larry (at) cs.mun.ca) and hacked again by
     10  1.1       jtc  * Michael Rendell (michael (at) cs.mun.ca)
     11  1.1       jtc  *
     12  1.1       jtc  * The interface to the rest of the shell should probably be changed
     13  1.1       jtc  * to allow use of vfork() when available but that would be way too much
     14  1.1       jtc  * work :)
     15  1.1       jtc  *
     16  1.1       jtc  * Notes regarding the copious ifdefs:
     17  1.1       jtc  *	- JOB_SIGS is independent of JOBS - it is defined if there are modern
     18  1.1       jtc  *	  signal and wait routines available.  This is prefered, even when
     19  1.1       jtc  *	  JOBS is not defined, since the shell will not otherwise notice when
     20  1.1       jtc  *	  background jobs die until the shell waits for a foreground process
     21  1.1       jtc  *	  to die.
     22  1.1       jtc  *	- TTY_PGRP defined iff JOBS is defined - defined if there are tty
     23  1.1       jtc  *	  process groups
     24  1.1       jtc  *	- NEED_PGRP_SYNC defined iff JOBS is defined - see comment below
     25  1.1       jtc  */
     26  1.7       agc #include <sys/cdefs.h>
     27  1.7       agc 
     28  1.7       agc #ifndef lint
     29  1.7       agc __RCSID("$NetBSD: jobs.c,v 1.7 2003/06/23 11:38:58 agc Exp $");
     30  1.7       agc #endif
     31  1.7       agc 
     32  1.1       jtc 
     33  1.1       jtc #include "sh.h"
     34  1.1       jtc #include "ksh_stat.h"
     35  1.1       jtc #include "ksh_wait.h"
     36  1.1       jtc #include "ksh_times.h"
     37  1.1       jtc #include "tty.h"
     38  1.1       jtc 
     39  1.1       jtc /* Start of system configuration stuff */
     40  1.1       jtc 
     41  1.1       jtc /* We keep CHILD_MAX zombie processes around (exact value isn't critical) */
     42  1.1       jtc #ifndef CHILD_MAX
     43  1.1       jtc # if defined(HAVE_SYSCONF) && defined(_SC_CHILD_MAX)
     44  1.1       jtc #  define CHILD_MAX sysconf(_SC_CHILD_MAX)
     45  1.1       jtc # else /* _SC_CHILD_MAX */
     46  1.1       jtc #  ifdef _POSIX_CHILD_MAX
     47  1.1       jtc #   define CHILD_MAX	((_POSIX_CHILD_MAX) * 2)
     48  1.1       jtc #  else /* _POSIX_CHILD_MAX */
     49  1.1       jtc #   define CHILD_MAX	20
     50  1.1       jtc #  endif /* _POSIX_CHILD_MAX */
     51  1.1       jtc # endif /* _SC_CHILD_MAX */
     52  1.1       jtc #endif /* !CHILD_MAX */
     53  1.1       jtc 
     54  1.1       jtc #ifdef JOBS
     55  1.1       jtc # if defined(HAVE_TCSETPGRP) || defined(TIOCSPGRP)
     56  1.1       jtc #  define TTY_PGRP
     57  1.1       jtc # endif
     58  1.1       jtc # ifdef BSD_PGRP
     59  1.1       jtc #  define setpgid	setpgrp
     60  1.1       jtc #  define getpgID()	getpgrp(0)
     61  1.1       jtc # else
     62  1.1       jtc #  define getpgID()	getpgrp()
     63  1.1       jtc # endif
     64  1.1       jtc # if defined(TTY_PGRP) && !defined(HAVE_TCSETPGRP)
     65  1.1       jtc int tcsetpgrp ARGS((int fd, pid_t grp));
     66  1.1       jtc int tcgetpgrp ARGS((int fd));
     67  1.1       jtc 
     68  1.1       jtc int
     69  1.1       jtc tcsetpgrp(fd, grp)
     70  1.1       jtc 	int fd;
     71  1.1       jtc 	pid_t grp;
     72  1.1       jtc {
     73  1.1       jtc 	return ioctl(fd, TIOCSPGRP, &grp);
     74  1.1       jtc }
     75  1.1       jtc 
     76  1.1       jtc int
     77  1.1       jtc tcgetpgrp(fd)
     78  1.1       jtc 	int	fd;
     79  1.1       jtc {
     80  1.1       jtc 	int r, grp;
     81  1.1       jtc 
     82  1.1       jtc 	if ((r = ioctl(fd, TIOCGPGRP, &grp)) < 0)
     83  1.1       jtc 		return r;
     84  1.1       jtc 	return grp;
     85  1.1       jtc }
     86  1.1       jtc # endif /* !HAVE_TCSETPGRP && TIOCSPGRP */
     87  1.1       jtc #else /* JOBS */
     88  1.1       jtc /* These so we can use ifdef xxx instead of if defined(JOBS) && defined(xxx) */
     89  1.1       jtc # undef TTY_PGRP
     90  1.1       jtc # undef NEED_PGRP_SYNC
     91  1.1       jtc #endif /* JOBS */
     92  1.1       jtc 
     93  1.1       jtc /* End of system configuration stuff */
     94  1.1       jtc 
     95  1.1       jtc 
     96  1.1       jtc /* Order important! */
     97  1.1       jtc #define PRUNNING	0
     98  1.1       jtc #define PEXITED		1
     99  1.1       jtc #define PSIGNALLED	2
    100  1.1       jtc #define PSTOPPED	3
    101  1.1       jtc 
    102  1.1       jtc typedef struct proc	Proc;
    103  1.1       jtc struct proc {
    104  1.1       jtc 	Proc	*next;		/* next process in pipeline (if any) */
    105  1.1       jtc 	int	state;
    106  1.1       jtc 	WAIT_T	status;		/* wait status */
    107  1.1       jtc 	pid_t	pid;		/* process id */
    108  1.1       jtc 	char	command[48];	/* process command string */
    109  1.1       jtc };
    110  1.1       jtc 
    111  1.1       jtc /* Notify/print flag - j_print() argument */
    112  1.1       jtc #define JP_NONE		0	/* don't print anything */
    113  1.1       jtc #define JP_SHORT	1	/* print signals processes were killed by */
    114  1.1       jtc #define JP_MEDIUM	2	/* print [job-num] -/+ command */
    115  1.1       jtc #define JP_LONG		3	/* print [job-num] -/+ pid command */
    116  1.1       jtc #define JP_PGRP		4	/* print pgrp */
    117  1.1       jtc 
    118  1.1       jtc /* put_job() flags */
    119  1.1       jtc #define PJ_ON_FRONT	0	/* at very front */
    120  1.1       jtc #define PJ_PAST_STOPPED	1	/* just past any stopped jobs */
    121  1.1       jtc 
    122  1.1       jtc /* Job.flags values */
    123  1.1       jtc #define JF_STARTED	0x001	/* set when all processes in job are started */
    124  1.1       jtc #define JF_WAITING	0x002	/* set if j_waitj() is waiting on job */
    125  1.1       jtc #define JF_W_ASYNCNOTIFY 0x004	/* set if waiting and async notification ok */
    126  1.1       jtc #define JF_XXCOM	0x008	/* set for `command` jobs */
    127  1.1       jtc #define JF_FG		0x010	/* running in foreground (also has tty pgrp) */
    128  1.1       jtc #define JF_SAVEDTTY	0x020	/* j->ttystate is valid */
    129  1.1       jtc #define JF_CHANGED	0x040	/* process has changed state */
    130  1.1       jtc #define JF_KNOWN	0x080	/* $! referenced */
    131  1.1       jtc #define JF_ZOMBIE	0x100	/* known, unwaited process */
    132  1.1       jtc #define JF_REMOVE	0x200	/* flaged for removal (j_jobs()/j_noityf()) */
    133  1.1       jtc #define JF_USETTYMODE	0x400	/* tty mode saved if process exits normally */
    134  1.5   hubertf #define JF_SAVEDTTYPGRP	0x800	/* j->saved_ttypgrp is valid */
    135  1.1       jtc 
    136  1.1       jtc typedef struct job Job;
    137  1.1       jtc struct job {
    138  1.1       jtc 	Job	*next;		/* next job in list */
    139  1.1       jtc 	int	job;		/* job number: %n */
    140  1.1       jtc 	int	flags;		/* see JF_* */
    141  1.1       jtc 	int	state;		/* job state */
    142  1.1       jtc 	int	status;		/* exit status of last process */
    143  1.1       jtc 	pid_t	pgrp;		/* process group of job */
    144  1.1       jtc 	pid_t	ppid;		/* pid of process that forked job */
    145  1.1       jtc 	INT32	age;		/* number of jobs started */
    146  1.1       jtc 	clock_t	systime;	/* system time used by job */
    147  1.1       jtc 	clock_t	usrtime;	/* user time used by job */
    148  1.1       jtc 	Proc	*proc_list;	/* process list */
    149  1.1       jtc 	Proc	*last_proc;	/* last process in list */
    150  1.2       tls #ifdef KSH
    151  1.1       jtc 	Coproc_id coproc_id;	/* 0 or id of coprocess output pipe */
    152  1.2       tls #endif /* KSH */
    153  1.1       jtc #ifdef TTY_PGRP
    154  1.1       jtc 	TTY_state ttystate;	/* saved tty state for stopped jobs */
    155  1.5   hubertf 	pid_t	saved_ttypgrp;	/* saved tty process group for stopped jobs */
    156  1.1       jtc #endif /* TTY_PGRP */
    157  1.1       jtc };
    158  1.1       jtc 
    159  1.1       jtc /* Flags for j_waitj() */
    160  1.1       jtc #define JW_NONE		0x00
    161  1.1       jtc #define JW_INTERRUPT	0x01	/* ^C will stop the wait */
    162  1.1       jtc #define JW_ASYNCNOTIFY	0x02	/* asynchronous notification during wait ok */
    163  1.1       jtc #define JW_STOPPEDWAIT	0x04	/* wait even if job stopped */
    164  1.1       jtc 
    165  1.1       jtc /* Error codes for j_lookup() */
    166  1.1       jtc #define JL_OK		0
    167  1.1       jtc #define JL_NOSUCH	1	/* no such job */
    168  1.1       jtc #define JL_AMBIG	2	/* %foo or %?foo is ambiguous */
    169  1.1       jtc #define JL_INVALID	3	/* non-pid, non-% job id */
    170  1.1       jtc 
    171  1.1       jtc static const char	*const lookup_msgs[] = {
    172  1.1       jtc 				null,
    173  1.1       jtc 				"no such job",
    174  1.1       jtc 				"ambiguous",
    175  1.1       jtc 				"argument must be %job or process id",
    176  1.1       jtc 				(char *) 0
    177  1.1       jtc 			    };
    178  1.1       jtc clock_t	j_systime, j_usrtime;	/* user and system time of last j_waitjed job */
    179  1.1       jtc 
    180  1.1       jtc static Job		*job_list;	/* job list */
    181  1.1       jtc static Job		*last_job;
    182  1.1       jtc static Job		*async_job;
    183  1.1       jtc static pid_t		async_pid;
    184  1.1       jtc 
    185  1.1       jtc static int		nzombie;	/* # of zombies owned by this process */
    186  1.1       jtc static INT32		njobs;		/* # of jobs started */
    187  1.1       jtc static int		child_max;	/* CHILD_MAX */
    188  1.1       jtc 
    189  1.1       jtc 
    190  1.1       jtc #ifdef JOB_SIGS
    191  1.1       jtc /* held_sigchld is set if sigchld occurs before a job is completely started */
    192  1.1       jtc static int		held_sigchld;
    193  1.1       jtc #endif /* JOB_SIGS */
    194  1.1       jtc 
    195  1.1       jtc #ifdef JOBS
    196  1.1       jtc static struct shf	*shl_j;
    197  1.1       jtc #endif /* JOBS */
    198  1.1       jtc 
    199  1.1       jtc #ifdef NEED_PGRP_SYNC
    200  1.1       jtc /* On some systems, the kernel doesn't count zombie processes when checking
    201  1.1       jtc  * if a process group is valid, which can cause problems in creating the
    202  1.1       jtc  * pipeline "cmd1 | cmd2": if cmd1 can die (and go into the zombie state)
    203  1.1       jtc  * before cmd2 is started, the kernel doesn't allow the setpgid() for cmd2
    204  1.1       jtc  * to succeed.  Solution is to create a pipe between the parent and the first
    205  1.1       jtc  * process; the first process doesn't do anything until the pipe is closed
    206  1.1       jtc  * and the parent doesn't close the pipe until all the processes are started.
    207  1.1       jtc  */
    208  1.1       jtc static int		j_sync_pipe[2];
    209  1.1       jtc static int		j_sync_open;
    210  1.1       jtc #endif /* NEED_PGRP_SYNC */
    211  1.1       jtc 
    212  1.1       jtc #ifdef TTY_PGRP
    213  1.1       jtc static int		ttypgrp_ok;	/* set if can use tty pgrps */
    214  1.1       jtc static pid_t		restore_ttypgrp = -1;
    215  1.1       jtc static pid_t		our_pgrp;
    216  1.1       jtc static int const	tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU };
    217  1.1       jtc #endif /* TTY_PGRP */
    218  1.1       jtc 
    219  1.1       jtc static void		j_set_async ARGS((Job *j));
    220  1.1       jtc static void		j_startjob ARGS((Job *j));
    221  1.1       jtc static int		j_waitj ARGS((Job *j, int flags, const char *where));
    222  1.1       jtc static RETSIGTYPE	j_sigchld ARGS((int sig));
    223  1.1       jtc static void		j_print ARGS((Job *j, int how, struct shf *shf));
    224  1.1       jtc static Job		*j_lookup ARGS((const char *cp, int *ecodep));
    225  1.1       jtc static Job		*new_job ARGS((void));
    226  1.1       jtc static Proc		*new_proc ARGS((void));
    227  1.1       jtc static void		check_job ARGS((Job *j));
    228  1.1       jtc static void		put_job ARGS((Job *j, int where));
    229  1.1       jtc static void		remove_job ARGS((Job *j, const char *where));
    230  1.6  christos static int		kill_job ARGS((Job *j, int sig));
    231  1.1       jtc 
    232  1.1       jtc /* initialize job control */
    233  1.1       jtc void
    234  1.1       jtc j_init(mflagset)
    235  1.1       jtc 	int mflagset;
    236  1.1       jtc {
    237  1.1       jtc 	child_max = CHILD_MAX; /* so syscon() isn't always being called */
    238  1.1       jtc 
    239  1.1       jtc #ifdef JOB_SIGS
    240  1.1       jtc 	sigemptyset(&sm_default);
    241  1.1       jtc 	sigprocmask(SIG_SETMASK, &sm_default, (sigset_t *) 0);
    242  1.1       jtc 
    243  1.1       jtc 	sigemptyset(&sm_sigchld);
    244  1.1       jtc 	sigaddset(&sm_sigchld, SIGCHLD);
    245  1.1       jtc 
    246  1.1       jtc 	setsig(&sigtraps[SIGCHLD], j_sigchld,
    247  1.1       jtc 		SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
    248  1.1       jtc #else /* JOB_SIGS */
    249  1.1       jtc 	/* Make sure SIGCHLD isn't ignored - can do odd things under SYSV */
    250  1.1       jtc 	setsig(&sigtraps[SIGCHLD], SIG_DFL, SS_RESTORE_ORIG|SS_FORCE);
    251  1.1       jtc #endif /* JOB_SIGS */
    252  1.1       jtc 
    253  1.1       jtc #ifdef JOBS
    254  1.1       jtc 	if (!mflagset && Flag(FTALKING))
    255  1.1       jtc 		Flag(FMONITOR) = 1;
    256  1.1       jtc 
    257  1.1       jtc 	/* shl_j is used to do asynchronous notification (used in
    258  1.1       jtc 	 * an interrupt handler, so need a distinct shf)
    259  1.1       jtc 	 */
    260  1.1       jtc 	shl_j = shf_fdopen(2, SHF_WR, (struct shf *) 0);
    261  1.1       jtc 
    262  1.1       jtc # ifdef TTY_PGRP
    263  1.1       jtc 	if (Flag(FMONITOR) || Flag(FTALKING)) {
    264  1.1       jtc 		int i;
    265  1.1       jtc 
    266  1.1       jtc 		/* the TF_SHELL_USES test is a kludge that lets us know if
    267  1.1       jtc 		 * if the signals have been changed by the shell.
    268  1.1       jtc 		 */
    269  1.1       jtc 		for (i = NELEM(tt_sigs); --i >= 0; ) {
    270  1.1       jtc 			sigtraps[tt_sigs[i]].flags |= TF_SHELL_USES;
    271  1.1       jtc 			/* j_change() sets this to SS_RESTORE_DFL if FMONITOR */
    272  1.1       jtc 			setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
    273  1.1       jtc 				SS_RESTORE_IGN|SS_FORCE);
    274  1.1       jtc 		}
    275  1.1       jtc 	}
    276  1.1       jtc # endif /* TTY_PGRP */
    277  1.1       jtc 
    278  1.1       jtc 	/* j_change() calls tty_init() */
    279  1.1       jtc 	if (Flag(FMONITOR))
    280  1.1       jtc 		j_change();
    281  1.1       jtc 	else
    282  1.1       jtc #endif /* JOBS */
    283  1.1       jtc 	  if (Flag(FTALKING))
    284  1.1       jtc 		tty_init(TRUE);
    285  1.1       jtc }
    286  1.1       jtc 
    287  1.1       jtc /* job cleanup before shell exit */
    288  1.1       jtc void
    289  1.1       jtc j_exit()
    290  1.1       jtc {
    291  1.1       jtc 	/* kill stopped, and possibly running, jobs */
    292  1.1       jtc 	Job	*j;
    293  1.1       jtc 	int	killed = 0;
    294  1.1       jtc 
    295  1.1       jtc 	for (j = job_list; j != (Job *) 0; j = j->next) {
    296  1.1       jtc 		if (j->ppid == procpid
    297  1.1       jtc 		    && (j->state == PSTOPPED
    298  1.1       jtc 			|| (j->state == PRUNNING
    299  1.1       jtc 			    && ((j->flags & JF_FG)
    300  1.1       jtc 				|| (Flag(FLOGIN) && !Flag(FNOHUP)
    301  1.1       jtc 				    && procpid == kshpid)))))
    302  1.1       jtc 		{
    303  1.1       jtc 			killed = 1;
    304  1.6  christos 			if (j->pgrp == 0)
    305  1.6  christos 				kill_job(j, SIGHUP);
    306  1.6  christos 			else
    307  1.6  christos 				killpg(j->pgrp, SIGHUP);
    308  1.1       jtc #ifdef JOBS
    309  1.6  christos 			if (j->state == PSTOPPED) {
    310  1.6  christos 				if (j->pgrp == 0)
    311  1.6  christos 					kill_job(j, SIGCONT);
    312  1.6  christos 				else
    313  1.6  christos 					killpg(j->pgrp, SIGCONT);
    314  1.6  christos 			}
    315  1.1       jtc #endif /* JOBS */
    316  1.1       jtc 		}
    317  1.1       jtc 	}
    318  1.1       jtc 	if (killed)
    319  1.1       jtc 		sleep(1);
    320  1.1       jtc 	j_notify();
    321  1.1       jtc 
    322  1.1       jtc #ifdef JOBS
    323  1.1       jtc # ifdef TTY_PGRP
    324  1.1       jtc 	if (kshpid == procpid && restore_ttypgrp >= 0) {
    325  1.1       jtc 		/* Need to restore the tty pgrp to what it was when the
    326  1.1       jtc 		 * shell started up, so that the process that started us
    327  1.1       jtc 		 * will be able to access the tty when we are done.
    328  1.1       jtc 		 * Also need to restore our process group in case we are
    329  1.1       jtc 		 * about to do an exec so that both our parent and the
    330  1.1       jtc 		 * process we are to become will be able to access the tty.
    331  1.1       jtc 		 */
    332  1.1       jtc 		tcsetpgrp(tty_fd, restore_ttypgrp);
    333  1.1       jtc 		setpgid(0, restore_ttypgrp);
    334  1.1       jtc 	}
    335  1.1       jtc # endif /* TTY_PGRP */
    336  1.1       jtc 	if (Flag(FMONITOR)) {
    337  1.1       jtc 		Flag(FMONITOR) = 0;
    338  1.1       jtc 		j_change();
    339  1.1       jtc 	}
    340  1.1       jtc #endif /* JOBS */
    341  1.1       jtc }
    342  1.1       jtc 
    343  1.1       jtc #ifdef JOBS
    344  1.1       jtc /* turn job control on or off according to Flag(FMONITOR) */
    345  1.1       jtc void
    346  1.1       jtc j_change()
    347  1.1       jtc {
    348  1.1       jtc 	int i;
    349  1.1       jtc 
    350  1.1       jtc 	if (Flag(FMONITOR)) {
    351  1.1       jtc 		/* Don't call get_tty() 'til we own the tty process group */
    352  1.1       jtc 		tty_init(FALSE);
    353  1.1       jtc 
    354  1.1       jtc # ifdef TTY_PGRP
    355  1.1       jtc 		/* no controlling tty, no SIGT* */
    356  1.1       jtc 		ttypgrp_ok = tty_fd >= 0 && tty_devtty;
    357  1.1       jtc 
    358  1.1       jtc 		if (ttypgrp_ok && (our_pgrp = getpgID()) < 0) {
    359  1.1       jtc 			warningf(FALSE, "j_init: getpgrp() failed: %s",
    360  1.1       jtc 				strerror(errno));
    361  1.1       jtc 			ttypgrp_ok = 0;
    362  1.1       jtc 		}
    363  1.1       jtc 		if (ttypgrp_ok) {
    364  1.1       jtc 			setsig(&sigtraps[SIGTTIN], SIG_DFL,
    365  1.1       jtc 				SS_RESTORE_ORIG|SS_FORCE);
    366  1.1       jtc 			/* wait to be given tty (POSIX.1, B.2, job control) */
    367  1.1       jtc 			while (1) {
    368  1.1       jtc 				pid_t ttypgrp;
    369  1.1       jtc 
    370  1.1       jtc 				if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) {
    371  1.1       jtc 					warningf(FALSE,
    372  1.1       jtc 					"j_init: tcgetpgrp() failed: %s",
    373  1.1       jtc 						strerror(errno));
    374  1.1       jtc 					ttypgrp_ok = 0;
    375  1.1       jtc 					break;
    376  1.1       jtc 				}
    377  1.1       jtc 				if (ttypgrp == our_pgrp)
    378  1.1       jtc 					break;
    379  1.1       jtc 				kill(0, SIGTTIN);
    380  1.1       jtc 			}
    381  1.1       jtc 		}
    382  1.1       jtc 		for (i = NELEM(tt_sigs); --i >= 0; )
    383  1.1       jtc 			setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
    384  1.1       jtc 				SS_RESTORE_DFL|SS_FORCE);
    385  1.1       jtc 		if (ttypgrp_ok && our_pgrp != kshpid) {
    386  1.1       jtc 			if (setpgid(0, kshpid) < 0) {
    387  1.1       jtc 				warningf(FALSE,
    388  1.1       jtc 					"j_init: setpgid() failed: %s",
    389  1.1       jtc 					strerror(errno));
    390  1.1       jtc 				ttypgrp_ok = 0;
    391  1.1       jtc 			} else {
    392  1.1       jtc 				if (tcsetpgrp(tty_fd, kshpid) < 0) {
    393  1.1       jtc 					warningf(FALSE,
    394  1.1       jtc 					"j_init: tcsetpgrp() failed: %s",
    395  1.1       jtc 						strerror(errno));
    396  1.1       jtc 					ttypgrp_ok = 0;
    397  1.1       jtc 				} else
    398  1.1       jtc 					restore_ttypgrp = our_pgrp;
    399  1.1       jtc 				our_pgrp = kshpid;
    400  1.1       jtc 			}
    401  1.1       jtc 		}
    402  1.1       jtc #  if defined(NTTYDISC) && defined(TIOCSETD) && !defined(HAVE_TERMIOS_H) && !defined(HAVE_TERMIO_H)
    403  1.1       jtc 		if (ttypgrp_ok) {
    404  1.1       jtc 			int ldisc = NTTYDISC;
    405  1.1       jtc 
    406  1.1       jtc 			if (ioctl(tty_fd, TIOCSETD, &ldisc) < 0)
    407  1.1       jtc 				warningf(FALSE,
    408  1.1       jtc 				"j_init: can't set new line discipline: %s",
    409  1.1       jtc 					strerror(errno));
    410  1.1       jtc 		}
    411  1.1       jtc #  endif /* NTTYDISC && TIOCSETD */
    412  1.1       jtc 		if (!ttypgrp_ok)
    413  1.1       jtc 			warningf(FALSE, "warning: won't have full job control");
    414  1.1       jtc # endif /* TTY_PGRP */
    415  1.1       jtc 		if (tty_fd >= 0)
    416  1.1       jtc 			get_tty(tty_fd, &tty_state);
    417  1.1       jtc 	} else {
    418  1.1       jtc # ifdef TTY_PGRP
    419  1.1       jtc 		ttypgrp_ok = 0;
    420  1.1       jtc 		if (Flag(FTALKING))
    421  1.1       jtc 			for (i = NELEM(tt_sigs); --i >= 0; )
    422  1.1       jtc 				setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
    423  1.1       jtc 					SS_RESTORE_IGN|SS_FORCE);
    424  1.1       jtc 		else
    425  1.1       jtc 			for (i = NELEM(tt_sigs); --i >= 0; ) {
    426  1.1       jtc 				if (sigtraps[tt_sigs[i]].flags & (TF_ORIG_IGN
    427  1.1       jtc 							          |TF_ORIG_DFL))
    428  1.1       jtc 					setsig(&sigtraps[tt_sigs[i]],
    429  1.1       jtc 						(sigtraps[tt_sigs[i]].flags & TF_ORIG_IGN) ? SIG_IGN : SIG_DFL,
    430  1.1       jtc 						SS_RESTORE_ORIG|SS_FORCE);
    431  1.1       jtc 			}
    432  1.1       jtc # endif /* TTY_PGRP */
    433  1.1       jtc 		if (!Flag(FTALKING))
    434  1.1       jtc 			tty_close();
    435  1.1       jtc 	}
    436  1.1       jtc }
    437  1.1       jtc #endif /* JOBS */
    438  1.1       jtc 
    439  1.1       jtc /* execute tree in child subprocess */
    440  1.1       jtc int
    441  1.1       jtc exchild(t, flags, close_fd)
    442  1.1       jtc 	struct op	*t;
    443  1.1       jtc 	int		flags;
    444  1.1       jtc 	int		close_fd;	/* used if XPCLOSE or XCCLOSE */
    445  1.1       jtc {
    446  1.1       jtc 	static Proc	*last_proc;	/* for pipelines */
    447  1.1       jtc 
    448  1.1       jtc 	int		i;
    449  1.1       jtc #ifdef JOB_SIGS
    450  1.1       jtc 	sigset_t	omask;
    451  1.1       jtc #endif /* JOB_SIGS */
    452  1.1       jtc 	Proc		*p;
    453  1.1       jtc 	Job		*j;
    454  1.1       jtc 	int		rv = 0;
    455  1.1       jtc 	int		forksleep;
    456  1.1       jtc 	int		ischild;
    457  1.1       jtc 
    458  1.1       jtc 	if (flags & XEXEC)
    459  1.5   hubertf 		/* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
    460  1.5   hubertf 		 * (also done in another execute() below)
    461  1.5   hubertf 		 */
    462  1.5   hubertf 		return execute(t, flags & (XEXEC | XERROK));
    463  1.1       jtc 
    464  1.1       jtc #ifdef JOB_SIGS
    465  1.1       jtc 	/* no SIGCHLD's while messing with job and process lists */
    466  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
    467  1.1       jtc #endif /* JOB_SIGS */
    468  1.1       jtc 
    469  1.1       jtc 	p = new_proc();
    470  1.1       jtc 	p->next = (Proc *) 0;
    471  1.1       jtc 	p->state = PRUNNING;
    472  1.1       jtc 	WSTATUS(p->status) = 0;
    473  1.1       jtc 	p->pid = 0;
    474  1.1       jtc 
    475  1.1       jtc 	/* link process into jobs list */
    476  1.1       jtc 	if (flags&XPIPEI) {	/* continuing with a pipe */
    477  1.5   hubertf 		if (!last_job)
    478  1.5   hubertf 			internal_errorf(1, "exchild: XPIPEI and no last_job - pid %d", (int) procpid);
    479  1.1       jtc 		j = last_job;
    480  1.1       jtc 		last_proc->next = p;
    481  1.1       jtc 		last_proc = p;
    482  1.1       jtc 	} else {
    483  1.1       jtc #ifdef NEED_PGRP_SYNC
    484  1.2       tls 		if (j_sync_open) {	/* should never happen */
    485  1.2       tls 			j_sync_open = 0;
    486  1.1       jtc 			closepipe(j_sync_pipe);
    487  1.1       jtc 		}
    488  1.1       jtc 		/* don't do the sync pipe business if there is no pipeline */
    489  1.1       jtc 		if (flags & XPIPEO) {
    490  1.1       jtc 			openpipe(j_sync_pipe);
    491  1.1       jtc 			j_sync_open = 1;
    492  1.1       jtc 		}
    493  1.1       jtc #endif /* NEED_PGRP_SYNC */
    494  1.1       jtc 		j = new_job(); /* fills in j->job */
    495  1.1       jtc 		/* we don't consider XXCOM's foreground since they don't get
    496  1.1       jtc 		 * tty process group and we don't save or restore tty modes.
    497  1.1       jtc 		 */
    498  1.1       jtc 		j->flags = (flags & XXCOM) ? JF_XXCOM
    499  1.1       jtc 			: ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
    500  1.1       jtc 		j->usrtime = j->systime = 0;
    501  1.1       jtc 		j->state = PRUNNING;
    502  1.1       jtc 		j->pgrp = 0;
    503  1.1       jtc 		j->ppid = procpid;
    504  1.1       jtc 		j->age = ++njobs;
    505  1.1       jtc 		j->proc_list = p;
    506  1.2       tls #ifdef KSH
    507  1.1       jtc 		j->coproc_id = 0;
    508  1.2       tls #endif /* KSH */
    509  1.1       jtc 		last_job = j;
    510  1.1       jtc 		last_proc = p;
    511  1.1       jtc 		put_job(j, PJ_PAST_STOPPED);
    512  1.1       jtc 	}
    513  1.1       jtc 
    514  1.6  christos 	snptreef(p->command, sizeof(p->command), "%T", t);
    515  1.1       jtc 
    516  1.1       jtc 	/* create child process */
    517  1.1       jtc 	forksleep = 1;
    518  1.1       jtc 	while ((i = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
    519  1.1       jtc 		if (intrsig)	 /* allow user to ^C out... */
    520  1.1       jtc 			break;
    521  1.1       jtc 		sleep(forksleep);
    522  1.1       jtc 		forksleep <<= 1;
    523  1.1       jtc 	}
    524  1.1       jtc 	if (i < 0) {
    525  1.6  christos 		kill_job(j, SIGKILL);
    526  1.1       jtc 		remove_job(j, "fork failed");
    527  1.1       jtc #ifdef NEED_PGRP_SYNC
    528  1.1       jtc 		if (j_sync_open) {
    529  1.1       jtc 			closepipe(j_sync_pipe);
    530  1.1       jtc 			j_sync_open = 0;
    531  1.1       jtc 		}
    532  1.1       jtc #endif /* NEED_PGRP_SYNC */
    533  1.1       jtc #ifdef JOB_SIGS
    534  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    535  1.1       jtc #endif /* JOB_SIGS */
    536  1.1       jtc 		errorf("cannot fork - try again");
    537  1.1       jtc 	}
    538  1.1       jtc 	ischild = i == 0;
    539  1.1       jtc 	if (ischild)
    540  1.1       jtc 		p->pid = procpid = getpid();
    541  1.1       jtc 	else
    542  1.1       jtc 		p->pid = i;
    543  1.1       jtc 
    544  1.1       jtc #ifdef JOBS
    545  1.1       jtc 	/* job control set up */
    546  1.1       jtc 	if (Flag(FMONITOR) && !(flags&XXCOM)) {
    547  1.1       jtc 		int	dotty = 0;
    548  1.1       jtc # ifdef NEED_PGRP_SYNC
    549  1.2       tls 		int	first_child_sync = 0;
    550  1.1       jtc # endif /* NEED_PGRP_SYNC */
    551  1.1       jtc 
    552  1.1       jtc # ifdef NEED_PGRP_SYNC
    553  1.2       tls 		if (j_sync_open) {
    554  1.2       tls 			/*
    555  1.2       tls 			 * The Parent closes 0, keeps 1 open 'til the whole
    556  1.2       tls 			 * pipeline is started.  The First child closes 1,
    557  1.2       tls 			 * keeps 0 open (reads from it).  The remaining
    558  1.2       tls 			 * children just have to close 1 (parent has already
    559  1.2       tls 			 * closeed 0).
    560  1.2       tls 			 */
    561  1.2       tls 			if (j->pgrp == 0) { /* First process */
    562  1.1       jtc 				close(j_sync_pipe[ischild]);
    563  1.1       jtc 				j_sync_pipe[ischild] = -1;
    564  1.2       tls 				first_child_sync = ischild;
    565  1.2       tls 			} else if (ischild) {
    566  1.2       tls 				j_sync_open = 0;
    567  1.2       tls 				closepipe(j_sync_pipe);
    568  1.1       jtc 			}
    569  1.2       tls 		}
    570  1.1       jtc # endif /* NEED_PGRP_SYNC */
    571  1.2       tls 		if (j->pgrp == 0) {	/* First process */
    572  1.2       tls 			j->pgrp = p->pid;
    573  1.2       tls 			dotty = 1;
    574  1.1       jtc 		}
    575  1.1       jtc 
    576  1.1       jtc 		/* set pgrp in both parent and child to deal with race
    577  1.1       jtc 		 * condition
    578  1.1       jtc 		 */
    579  1.1       jtc 		setpgid(p->pid, j->pgrp);
    580  1.1       jtc # ifdef TTY_PGRP
    581  1.1       jtc 		/* YYY: should this be
    582  1.1       jtc 		   if (ttypgrp_ok && ischild && !(flags&XBGND))
    583  1.1       jtc 			tcsetpgrp(tty_fd, j->pgrp);
    584  1.1       jtc 		   instead? (see also YYY below)
    585  1.1       jtc 		 */
    586  1.1       jtc 		if (ttypgrp_ok && dotty && !(flags & XBGND))
    587  1.1       jtc 			tcsetpgrp(tty_fd, j->pgrp);
    588  1.1       jtc # endif /* TTY_PGRP */
    589  1.1       jtc # ifdef NEED_PGRP_SYNC
    590  1.2       tls 		if (first_child_sync) {
    591  1.2       tls 			char c;
    592  1.2       tls 			while (read(j_sync_pipe[0], &c, 1) == -1
    593  1.2       tls 			       && errno == EINTR)
    594  1.2       tls 				;
    595  1.1       jtc 			close(j_sync_pipe[0]);
    596  1.1       jtc 			j_sync_open = 0;
    597  1.1       jtc 		}
    598  1.1       jtc # endif /* NEED_PGRP_SYNC */
    599  1.1       jtc 	}
    600  1.1       jtc #endif /* JOBS */
    601  1.1       jtc 
    602  1.1       jtc 	/* used to close pipe input fd */
    603  1.5   hubertf 	if (close_fd >= 0 && (((flags & XPCLOSE) && !ischild)
    604  1.5   hubertf 			      || ((flags & XCCLOSE) && ischild)))
    605  1.1       jtc 		close(close_fd);
    606  1.1       jtc 	if (ischild) {		/* child */
    607  1.1       jtc #ifdef KSH
    608  1.1       jtc 		/* Do this before restoring signal */
    609  1.5   hubertf 		if (flags & XCOPROC)
    610  1.1       jtc 			coproc_cleanup(FALSE);
    611  1.1       jtc #endif /* KSH */
    612  1.1       jtc #ifdef JOB_SIGS
    613  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    614  1.1       jtc #endif /* JOB_SIGS */
    615  1.1       jtc 		cleanup_parents_env();
    616  1.1       jtc #ifdef TTY_PGRP
    617  1.1       jtc 		/* If FMONITOR or FTALKING is set, these signals are ignored,
    618  1.1       jtc 		 * if neither FMONITOR nor FTALKING are set, the signals have
    619  1.1       jtc 		 * their inherited values.
    620  1.1       jtc 		 */
    621  1.1       jtc 		if (Flag(FMONITOR) && !(flags & XXCOM)) {
    622  1.1       jtc 			for (i = NELEM(tt_sigs); --i >= 0; )
    623  1.1       jtc 				setsig(&sigtraps[tt_sigs[i]], SIG_DFL,
    624  1.1       jtc 					SS_RESTORE_DFL|SS_FORCE);
    625  1.1       jtc 		}
    626  1.1       jtc #endif /* TTY_PGRP */
    627  1.1       jtc #ifdef HAVE_NICE
    628  1.1       jtc 		if (Flag(FBGNICE) && (flags & XBGND))
    629  1.1       jtc 			nice(4);
    630  1.1       jtc #endif /* HAVE_NICE */
    631  1.1       jtc 		if ((flags & XBGND) && !Flag(FMONITOR)) {
    632  1.1       jtc 			setsig(&sigtraps[SIGINT], SIG_IGN,
    633  1.1       jtc 				SS_RESTORE_IGN|SS_FORCE);
    634  1.1       jtc 			setsig(&sigtraps[SIGQUIT], SIG_IGN,
    635  1.1       jtc 				SS_RESTORE_IGN|SS_FORCE);
    636  1.5   hubertf 			if (!(flags & (XPIPEI | XCOPROC))) {
    637  1.5   hubertf 				int fd = open("/dev/null", 0);
    638  1.1       jtc 				(void) ksh_dup2(fd, 0, TRUE);
    639  1.1       jtc 				close(fd);
    640  1.1       jtc 			}
    641  1.1       jtc 		}
    642  1.1       jtc 		remove_job(j, "child");	/* in case of `jobs` command */
    643  1.1       jtc 		nzombie = 0;
    644  1.1       jtc #ifdef JOBS
    645  1.1       jtc 		ttypgrp_ok = 0;
    646  1.1       jtc 		Flag(FMONITOR) = 0;
    647  1.1       jtc #endif /* JOBS */
    648  1.1       jtc 		Flag(FTALKING) = 0;
    649  1.5   hubertf #ifdef OS2
    650  1.5   hubertf 		if (tty_fd >= 0)
    651  1.5   hubertf 			flags |= XINTACT;
    652  1.5   hubertf #endif /* OS2 */
    653  1.1       jtc 		tty_close();
    654  1.1       jtc 		cleartraps();
    655  1.5   hubertf 		execute(t, (flags & XERROK) | XEXEC); /* no return */
    656  1.1       jtc 		internal_errorf(0, "exchild: execute() returned");
    657  1.1       jtc 		unwind(LLEAVE);
    658  1.1       jtc 		/* NOTREACHED */
    659  1.1       jtc 	}
    660  1.1       jtc 
    661  1.1       jtc 	/* shell (parent) stuff */
    662  1.5   hubertf 	/* Ensure next child gets a (slightly) different $RANDOM sequence */
    663  1.5   hubertf 	change_random();
    664  1.1       jtc 	if (!(flags & XPIPEO)) {	/* last process in a job */
    665  1.1       jtc #ifdef TTY_PGRP
    666  1.1       jtc 		/* YYY: Is this needed? (see also YYY above)
    667  1.1       jtc 		   if (Flag(FMONITOR) && !(flags&(XXCOM|XBGND)))
    668  1.1       jtc 			tcsetpgrp(tty_fd, j->pgrp);
    669  1.1       jtc 		*/
    670  1.1       jtc #endif /* TTY_PGRP */
    671  1.1       jtc 		j_startjob(j);
    672  1.1       jtc #ifdef KSH
    673  1.5   hubertf 		if (flags & XCOPROC) {
    674  1.1       jtc 			j->coproc_id = coproc.id;
    675  1.1       jtc 			coproc.njobs++; /* n jobs using co-process output */
    676  1.1       jtc 			coproc.job = (void *) j; /* j using co-process input */
    677  1.1       jtc 		}
    678  1.1       jtc #endif /* KSH */
    679  1.1       jtc 		if (flags & XBGND) {
    680  1.1       jtc 			j_set_async(j);
    681  1.1       jtc 			if (Flag(FTALKING)) {
    682  1.1       jtc 				shf_fprintf(shl_out, "[%d]", j->job);
    683  1.1       jtc 				for (p = j->proc_list; p; p = p->next)
    684  1.1       jtc 					shf_fprintf(shl_out, " %d", p->pid);
    685  1.1       jtc 				shf_putchar('\n', shl_out);
    686  1.1       jtc 				shf_flush(shl_out);
    687  1.1       jtc 			}
    688  1.1       jtc 		} else
    689  1.1       jtc 			rv = j_waitj(j, JW_NONE, "jw:last proc");
    690  1.1       jtc 	}
    691  1.1       jtc 
    692  1.1       jtc #ifdef JOB_SIGS
    693  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    694  1.1       jtc #endif /* JOB_SIGS */
    695  1.1       jtc 
    696  1.1       jtc 	return rv;
    697  1.1       jtc }
    698  1.1       jtc 
    699  1.1       jtc /* start the last job: only used for `command` jobs */
    700  1.1       jtc void
    701  1.1       jtc startlast()
    702  1.1       jtc {
    703  1.1       jtc #ifdef JOB_SIGS
    704  1.1       jtc 	sigset_t omask;
    705  1.1       jtc 
    706  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
    707  1.1       jtc #endif /* JOB_SIGS */
    708  1.1       jtc 
    709  1.1       jtc 	if (last_job) { /* no need to report error - waitlast() will do it */
    710  1.1       jtc 		/* ensure it isn't removed by check_job() */
    711  1.1       jtc 		last_job->flags |= JF_WAITING;
    712  1.1       jtc 		j_startjob(last_job);
    713  1.1       jtc 	}
    714  1.1       jtc #ifdef JOB_SIGS
    715  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    716  1.1       jtc #endif /* JOB_SIGS */
    717  1.1       jtc }
    718  1.1       jtc 
    719  1.1       jtc /* wait for last job: only used for `command` jobs */
    720  1.1       jtc int
    721  1.1       jtc waitlast()
    722  1.1       jtc {
    723  1.1       jtc 	int	rv;
    724  1.1       jtc 	Job	*j;
    725  1.1       jtc #ifdef JOB_SIGS
    726  1.1       jtc 	sigset_t omask;
    727  1.1       jtc 
    728  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
    729  1.1       jtc #endif /* JOB_SIGS */
    730  1.1       jtc 
    731  1.1       jtc 	j = last_job;
    732  1.1       jtc 	if (!j || !(j->flags & JF_STARTED)) {
    733  1.1       jtc 		if (!j)
    734  1.1       jtc 			warningf(TRUE, "waitlast: no last job");
    735  1.1       jtc 		else
    736  1.1       jtc 			internal_errorf(0, "waitlast: not started");
    737  1.1       jtc #ifdef JOB_SIGS
    738  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    739  1.1       jtc #endif /* JOB_SIGS */
    740  1.1       jtc 		return 125; /* not so arbitrary, non-zero value */
    741  1.1       jtc 	}
    742  1.1       jtc 
    743  1.1       jtc 	rv = j_waitj(j, JW_NONE, "jw:waitlast");
    744  1.1       jtc 
    745  1.1       jtc #ifdef JOB_SIGS
    746  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    747  1.1       jtc #endif /* JOB_SIGS */
    748  1.1       jtc 
    749  1.1       jtc 	return rv;
    750  1.1       jtc }
    751  1.1       jtc 
    752  1.1       jtc /* wait for child, interruptable. */
    753  1.1       jtc int
    754  1.1       jtc waitfor(cp, sigp)
    755  1.1       jtc 	const char *cp;
    756  1.1       jtc 	int	*sigp;
    757  1.1       jtc {
    758  1.1       jtc 	int	rv;
    759  1.1       jtc 	Job	*j;
    760  1.1       jtc 	int	ecode;
    761  1.1       jtc 	int	flags = JW_INTERRUPT|JW_ASYNCNOTIFY;
    762  1.1       jtc #ifdef JOB_SIGS
    763  1.1       jtc 	sigset_t omask;
    764  1.1       jtc 
    765  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
    766  1.1       jtc #endif /* JOB_SIGS */
    767  1.1       jtc 
    768  1.1       jtc 	*sigp = 0;
    769  1.1       jtc 
    770  1.1       jtc 	if (cp == (char *) 0) {
    771  1.1       jtc 		/* wait for an unspecified job - always returns 0, so
    772  1.1       jtc 		 * don't have to worry about exited/signaled jobs
    773  1.1       jtc 		 */
    774  1.1       jtc 		for (j = job_list; j; j = j->next)
    775  1.1       jtc 			/* at&t ksh will wait for stopped jobs - we don't */
    776  1.1       jtc 			if (j->ppid == procpid && j->state == PRUNNING)
    777  1.1       jtc 				break;
    778  1.1       jtc 		if (!j) {
    779  1.1       jtc #ifdef JOB_SIGS
    780  1.1       jtc 			sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    781  1.1       jtc #endif /* JOB_SIGS */
    782  1.1       jtc 			return -1;
    783  1.1       jtc 		}
    784  1.1       jtc 	} else if ((j = j_lookup(cp, &ecode))) {
    785  1.1       jtc 		/* don't report normal job completion */
    786  1.1       jtc 		flags &= ~JW_ASYNCNOTIFY;
    787  1.1       jtc 		if (j->ppid != procpid) {
    788  1.1       jtc #ifdef JOB_SIGS
    789  1.1       jtc 			sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    790  1.1       jtc #endif /* JOB_SIGS */
    791  1.1       jtc 			return -1;
    792  1.1       jtc 		}
    793  1.1       jtc 	} else {
    794  1.1       jtc #ifdef JOB_SIGS
    795  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    796  1.1       jtc #endif /* JOB_SIGS */
    797  1.5   hubertf 		if (ecode != JL_NOSUCH)
    798  1.5   hubertf 			bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
    799  1.5   hubertf 		return -1;
    800  1.1       jtc 	}
    801  1.1       jtc 
    802  1.1       jtc 	/* at&t ksh will wait for stopped jobs - we don't */
    803  1.1       jtc 	rv = j_waitj(j, flags, "jw:waitfor");
    804  1.1       jtc 
    805  1.1       jtc #ifdef JOB_SIGS
    806  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    807  1.1       jtc #endif /* JOB_SIGS */
    808  1.1       jtc 
    809  1.1       jtc 	if (rv < 0) /* we were interrupted */
    810  1.1       jtc 		*sigp = 128 + -rv;
    811  1.1       jtc 
    812  1.1       jtc 	return rv;
    813  1.1       jtc }
    814  1.1       jtc 
    815  1.1       jtc /* kill (built-in) a job */
    816  1.1       jtc int
    817  1.1       jtc j_kill(cp, sig)
    818  1.1       jtc 	const char *cp;
    819  1.1       jtc 	int	sig;
    820  1.1       jtc {
    821  1.1       jtc 	Job	*j;
    822  1.1       jtc 	int	rv = 0;
    823  1.1       jtc 	int	ecode;
    824  1.1       jtc #ifdef JOB_SIGS
    825  1.1       jtc 	sigset_t omask;
    826  1.1       jtc 
    827  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
    828  1.1       jtc #endif /* JOB_SIGS */
    829  1.1       jtc 
    830  1.1       jtc 	if ((j = j_lookup(cp, &ecode)) == (Job *) 0) {
    831  1.1       jtc #ifdef JOB_SIGS
    832  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    833  1.1       jtc #endif /* JOB_SIGS */
    834  1.1       jtc 		bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
    835  1.1       jtc 		return 1;
    836  1.1       jtc 	}
    837  1.1       jtc 
    838  1.1       jtc 	if (j->pgrp == 0) {	/* started when !Flag(FMONITOR) */
    839  1.6  christos 		if (kill_job(j, sig) < 0) {
    840  1.6  christos 			bi_errorf("%s: %s", cp, strerror(errno));
    841  1.6  christos 			rv = 1;
    842  1.6  christos 		}
    843  1.1       jtc 	} else {
    844  1.1       jtc #ifdef JOBS
    845  1.1       jtc 		if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP))
    846  1.1       jtc 			(void) killpg(j->pgrp, SIGCONT);
    847  1.1       jtc #endif /* JOBS */
    848  1.1       jtc 		if (killpg(j->pgrp, sig) < 0) {
    849  1.1       jtc 			bi_errorf("%s: %s", cp, strerror(errno));
    850  1.1       jtc 			rv = 1;
    851  1.1       jtc 		}
    852  1.1       jtc 	}
    853  1.1       jtc 
    854  1.1       jtc #ifdef JOB_SIGS
    855  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    856  1.1       jtc #endif /* JOB_SIGS */
    857  1.1       jtc 
    858  1.1       jtc 	return rv;
    859  1.1       jtc }
    860  1.1       jtc 
    861  1.1       jtc #ifdef JOBS
    862  1.1       jtc /* fg and bg built-ins: called only if Flag(FMONITOR) set */
    863  1.1       jtc int
    864  1.1       jtc j_resume(cp, bg)
    865  1.1       jtc 	const char *cp;
    866  1.1       jtc 	int	bg;
    867  1.1       jtc {
    868  1.1       jtc 	Job	*j;
    869  1.1       jtc 	Proc	*p;
    870  1.1       jtc 	int	ecode;
    871  1.1       jtc 	int	running;
    872  1.1       jtc 	int	rv = 0;
    873  1.1       jtc 	sigset_t omask;
    874  1.1       jtc 
    875  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
    876  1.1       jtc 
    877  1.1       jtc 	if ((j = j_lookup(cp, &ecode)) == (Job *) 0) {
    878  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    879  1.1       jtc 		bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
    880  1.1       jtc 		return 1;
    881  1.1       jtc 	}
    882  1.1       jtc 
    883  1.1       jtc 	if (j->pgrp == 0) {
    884  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    885  1.1       jtc 		bi_errorf("job not job-controlled");
    886  1.1       jtc 		return 1;
    887  1.1       jtc 	}
    888  1.1       jtc 
    889  1.1       jtc 	if (bg)
    890  1.1       jtc 		shprintf("[%d] ", j->job);
    891  1.1       jtc 
    892  1.1       jtc 	running = 0;
    893  1.1       jtc 	for (p = j->proc_list; p != (Proc *) 0; p = p->next) {
    894  1.1       jtc 		if (p->state == PSTOPPED) {
    895  1.1       jtc 			p->state = PRUNNING;
    896  1.1       jtc 			WSTATUS(p->status) = 0;
    897  1.1       jtc 			running = 1;
    898  1.1       jtc 		}
    899  1.1       jtc 		shprintf("%s%s", p->command, p->next ? "| " : null);
    900  1.1       jtc 	}
    901  1.1       jtc 	shprintf(newline);
    902  1.1       jtc 	shf_flush(shl_stdout);
    903  1.1       jtc 	if (running)
    904  1.1       jtc 		j->state = PRUNNING;
    905  1.1       jtc 
    906  1.1       jtc 	put_job(j, PJ_PAST_STOPPED);
    907  1.1       jtc 	if (bg)
    908  1.1       jtc 		j_set_async(j);
    909  1.1       jtc 	else {
    910  1.1       jtc # ifdef TTY_PGRP
    911  1.1       jtc 		/* attach tty to job */
    912  1.1       jtc 		if (j->state == PRUNNING) {
    913  1.1       jtc 			if (ttypgrp_ok && (j->flags & JF_SAVEDTTY)) {
    914  1.1       jtc 				set_tty(tty_fd, &j->ttystate, TF_NONE);
    915  1.1       jtc 			}
    916  1.5   hubertf 			/* See comment in j_waitj regarding saved_ttypgrp. */
    917  1.5   hubertf 			if (ttypgrp_ok && tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ? j->saved_ttypgrp : j->pgrp) < 0) {
    918  1.5   hubertf 				if (j->flags & JF_SAVEDTTY) {
    919  1.1       jtc 					set_tty(tty_fd, &tty_state, TF_NONE);
    920  1.5   hubertf 				}
    921  1.1       jtc 				sigprocmask(SIG_SETMASK, &omask,
    922  1.1       jtc 					(sigset_t *) 0);
    923  1.1       jtc 				bi_errorf("1st tcsetpgrp(%d, %d) failed: %s",
    924  1.5   hubertf 					tty_fd, (int) ((j->flags & JF_SAVEDTTYPGRP) ? j->saved_ttypgrp : j->pgrp), strerror(errno));
    925  1.1       jtc 				return 1;
    926  1.1       jtc 			}
    927  1.1       jtc 		}
    928  1.1       jtc # endif /* TTY_PGRP */
    929  1.1       jtc 		j->flags |= JF_FG;
    930  1.1       jtc 		j->flags &= ~JF_KNOWN;
    931  1.1       jtc 		if (j == async_job)
    932  1.1       jtc 			async_job = (Job *) 0;
    933  1.1       jtc 	}
    934  1.1       jtc 
    935  1.1       jtc 	if (j->state == PRUNNING && killpg(j->pgrp, SIGCONT) < 0) {
    936  1.1       jtc 		int	err = errno;
    937  1.1       jtc 
    938  1.1       jtc 		if (!bg) {
    939  1.1       jtc 			j->flags &= ~JF_FG;
    940  1.1       jtc # ifdef TTY_PGRP
    941  1.5   hubertf 			if (ttypgrp_ok && (j->flags & JF_SAVEDTTY)) {
    942  1.1       jtc 				set_tty(tty_fd, &tty_state, TF_NONE);
    943  1.5   hubertf 			}
    944  1.1       jtc 			if (ttypgrp_ok && tcsetpgrp(tty_fd, our_pgrp) < 0) {
    945  1.1       jtc 				warningf(TRUE,
    946  1.1       jtc 				"fg: 2nd tcsetpgrp(%d, %d) failed: %s",
    947  1.1       jtc 					tty_fd, (int) our_pgrp,
    948  1.1       jtc 					strerror(errno));
    949  1.1       jtc 			}
    950  1.1       jtc # endif /* TTY_PGRP */
    951  1.1       jtc 		}
    952  1.1       jtc 		sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    953  1.1       jtc 		bi_errorf("cannot continue job %s: %s",
    954  1.1       jtc 			cp, strerror(err));
    955  1.1       jtc 		return 1;
    956  1.1       jtc 	}
    957  1.1       jtc 	if (!bg) {
    958  1.1       jtc # ifdef TTY_PGRP
    959  1.1       jtc 		if (ttypgrp_ok) {
    960  1.5   hubertf 			j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP);
    961  1.1       jtc 		}
    962  1.1       jtc # endif /* TTY_PGRP */
    963  1.1       jtc 		rv = j_waitj(j, JW_NONE, "jw:resume");
    964  1.1       jtc 	}
    965  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
    966  1.1       jtc 	return rv;
    967  1.1       jtc }
    968  1.1       jtc #endif /* JOBS */
    969  1.1       jtc 
    970  1.1       jtc /* are there any running or stopped jobs ? */
    971  1.1       jtc int
    972  1.1       jtc j_stopped_running()
    973  1.1       jtc {
    974  1.1       jtc 	Job	*j;
    975  1.1       jtc 	int	which = 0;
    976  1.1       jtc 
    977  1.1       jtc 	for (j = job_list; j != (Job *) 0; j = j->next) {
    978  1.1       jtc #ifdef JOBS
    979  1.1       jtc 		if (j->ppid == procpid && j->state == PSTOPPED)
    980  1.1       jtc 			which |= 1;
    981  1.1       jtc #endif /* JOBS */
    982  1.1       jtc 		if (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid
    983  1.1       jtc 		    && j->ppid == procpid && j->state == PRUNNING)
    984  1.1       jtc 			which |= 2;
    985  1.1       jtc 	}
    986  1.1       jtc 	if (which) {
    987  1.1       jtc 		shellf("You have %s%s%s jobs\n",
    988  1.1       jtc 			which & 1 ? "stopped" : "",
    989  1.1       jtc 			which == 3 ? " and " : "",
    990  1.1       jtc 			which & 2 ? "running" : "");
    991  1.1       jtc 		return 1;
    992  1.1       jtc 	}
    993  1.1       jtc 
    994  1.1       jtc 	return 0;
    995  1.1       jtc }
    996  1.1       jtc 
    997  1.1       jtc /* list jobs for jobs built-in */
    998  1.1       jtc int
    999  1.1       jtc j_jobs(cp, slp, nflag)
   1000  1.1       jtc 	const char *cp;
   1001  1.1       jtc 	int	slp;		/* 0: short, 1: long, 2: pgrp */
   1002  1.1       jtc 	int	nflag;
   1003  1.1       jtc {
   1004  1.1       jtc 	Job	*j, *tmp;
   1005  1.1       jtc 	int	how;
   1006  1.1       jtc 	int	zflag = 0;
   1007  1.1       jtc #ifdef JOB_SIGS
   1008  1.1       jtc 	sigset_t omask;
   1009  1.1       jtc 
   1010  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
   1011  1.1       jtc #endif /* JOB_SIGS */
   1012  1.1       jtc 
   1013  1.1       jtc 	if (nflag < 0) { /* kludge: print zombies */
   1014  1.1       jtc 		nflag = 0;
   1015  1.1       jtc 		zflag = 1;
   1016  1.1       jtc 	}
   1017  1.1       jtc 	if (cp) {
   1018  1.1       jtc 		int	ecode;
   1019  1.1       jtc 
   1020  1.1       jtc 		if ((j = j_lookup(cp, &ecode)) == (Job *) 0) {
   1021  1.1       jtc #ifdef JOB_SIGS
   1022  1.1       jtc 			sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
   1023  1.1       jtc #endif /* JOB_SIGS */
   1024  1.1       jtc 			bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
   1025  1.1       jtc 			return 1;
   1026  1.1       jtc 		}
   1027  1.1       jtc 	} else
   1028  1.1       jtc 		j = job_list;
   1029  1.1       jtc 	how = slp == 0 ? JP_MEDIUM : (slp == 1 ? JP_LONG : JP_PGRP);
   1030  1.1       jtc 	for (; j; j = j->next) {
   1031  1.1       jtc 		if ((!(j->flags & JF_ZOMBIE) || zflag)
   1032  1.1       jtc 		    && (!nflag || (j->flags & JF_CHANGED)))
   1033  1.1       jtc 		{
   1034  1.1       jtc 			j_print(j, how, shl_stdout);
   1035  1.1       jtc 			if (j->state == PEXITED || j->state == PSIGNALLED)
   1036  1.1       jtc 				j->flags |= JF_REMOVE;
   1037  1.1       jtc 		}
   1038  1.1       jtc 		if (cp)
   1039  1.1       jtc 			break;
   1040  1.1       jtc 	}
   1041  1.1       jtc 	/* Remove jobs after printing so there won't be multiple + or - jobs */
   1042  1.1       jtc 	for (j = job_list; j; j = tmp) {
   1043  1.1       jtc 		tmp = j->next;
   1044  1.1       jtc 		if (j->flags & JF_REMOVE)
   1045  1.1       jtc 			remove_job(j, "jobs");
   1046  1.1       jtc 	}
   1047  1.1       jtc #ifdef JOB_SIGS
   1048  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
   1049  1.1       jtc #endif /* JOB_SIGS */
   1050  1.1       jtc 	return 0;
   1051  1.1       jtc }
   1052  1.1       jtc 
   1053  1.1       jtc /* list jobs for top-level notification */
   1054  1.1       jtc void
   1055  1.1       jtc j_notify()
   1056  1.1       jtc {
   1057  1.1       jtc 	Job	*j, *tmp;
   1058  1.1       jtc #ifdef JOB_SIGS
   1059  1.1       jtc 	sigset_t omask;
   1060  1.1       jtc 
   1061  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
   1062  1.1       jtc #endif /* JOB_SIGS */
   1063  1.1       jtc 	for (j = job_list; j; j = j->next) {
   1064  1.1       jtc #ifdef JOBS
   1065  1.1       jtc 		if (Flag(FMONITOR) && (j->flags & JF_CHANGED))
   1066  1.1       jtc 			j_print(j, JP_MEDIUM, shl_out);
   1067  1.1       jtc #endif /* JOBS */
   1068  1.1       jtc 		/* Remove job after doing reports so there aren't
   1069  1.1       jtc 		 * multiple +/- jobs.
   1070  1.1       jtc 		 */
   1071  1.1       jtc 		if (j->state == PEXITED || j->state == PSIGNALLED)
   1072  1.1       jtc 			j->flags |= JF_REMOVE;
   1073  1.1       jtc 	}
   1074  1.1       jtc 	for (j = job_list; j; j = tmp) {
   1075  1.1       jtc 		tmp = j->next;
   1076  1.1       jtc 		if (j->flags & JF_REMOVE)
   1077  1.1       jtc 			remove_job(j, "notify");
   1078  1.1       jtc 	}
   1079  1.1       jtc 	shf_flush(shl_out);
   1080  1.1       jtc #ifdef JOB_SIGS
   1081  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
   1082  1.1       jtc #endif /* JOB_SIGS */
   1083  1.1       jtc }
   1084  1.1       jtc 
   1085  1.1       jtc /* Return pid of last process in last asynchornous job */
   1086  1.1       jtc pid_t
   1087  1.1       jtc j_async()
   1088  1.1       jtc {
   1089  1.1       jtc #ifdef JOB_SIGS
   1090  1.1       jtc 	sigset_t omask;
   1091  1.1       jtc 
   1092  1.1       jtc 	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
   1093  1.1       jtc #endif /* JOB_SIGS */
   1094  1.1       jtc 
   1095  1.1       jtc 	if (async_job)
   1096  1.1       jtc 		async_job->flags |= JF_KNOWN;
   1097  1.1       jtc 
   1098  1.1       jtc #ifdef JOB_SIGS
   1099  1.1       jtc 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
   1100  1.1       jtc #endif /* JOB_SIGS */
   1101  1.1       jtc 
   1102  1.1       jtc 	return async_pid;
   1103  1.1       jtc }
   1104  1.1       jtc 
   1105  1.1       jtc /* Make j the last async process
   1106  1.1       jtc  *
   1107  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1108  1.1       jtc  */
   1109  1.1       jtc static void
   1110  1.1       jtc j_set_async(j)
   1111  1.1       jtc 	Job *j;
   1112  1.1       jtc {
   1113  1.1       jtc 	Job	*jl, *oldest;
   1114  1.1       jtc 
   1115  1.1       jtc 	if (async_job && (async_job->flags & (JF_KNOWN|JF_ZOMBIE)) == JF_ZOMBIE)
   1116  1.1       jtc 		remove_job(async_job, "async");
   1117  1.1       jtc 	if (!(j->flags & JF_STARTED)) {
   1118  1.1       jtc 		internal_errorf(0, "j_async: job not started");
   1119  1.1       jtc 		return;
   1120  1.1       jtc 	}
   1121  1.1       jtc 	async_job = j;
   1122  1.1       jtc 	async_pid = j->last_proc->pid;
   1123  1.1       jtc 	while (nzombie > child_max) {
   1124  1.1       jtc 		oldest = (Job *) 0;
   1125  1.1       jtc 		for (jl = job_list; jl; jl = jl->next)
   1126  1.1       jtc 			if (jl != async_job && (jl->flags & JF_ZOMBIE)
   1127  1.1       jtc 			    && (!oldest || jl->age < oldest->age))
   1128  1.1       jtc 				oldest = jl;
   1129  1.1       jtc 		if (!oldest) {
   1130  1.1       jtc 			/* XXX debugging */
   1131  1.1       jtc 			if (!(async_job->flags & JF_ZOMBIE) || nzombie != 1) {
   1132  1.1       jtc 				internal_errorf(0, "j_async: bad nzombie (%d)", nzombie);
   1133  1.1       jtc 				nzombie = 0;
   1134  1.1       jtc 			}
   1135  1.1       jtc 			break;
   1136  1.1       jtc 		}
   1137  1.1       jtc 		remove_job(oldest, "zombie");
   1138  1.1       jtc 	}
   1139  1.1       jtc }
   1140  1.1       jtc 
   1141  1.1       jtc /* Start a job: set STARTED, check for held signals and set j->last_proc
   1142  1.1       jtc  *
   1143  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1144  1.1       jtc  */
   1145  1.1       jtc static void
   1146  1.1       jtc j_startjob(j)
   1147  1.1       jtc 	Job *j;
   1148  1.1       jtc {
   1149  1.1       jtc 	Proc	*p;
   1150  1.1       jtc 
   1151  1.1       jtc 	j->flags |= JF_STARTED;
   1152  1.1       jtc 	for (p = j->proc_list; p->next; p = p->next)
   1153  1.1       jtc 		;
   1154  1.1       jtc 	j->last_proc = p;
   1155  1.1       jtc 
   1156  1.1       jtc #ifdef NEED_PGRP_SYNC
   1157  1.1       jtc 	if (j_sync_open) {
   1158  1.2       tls 		j_sync_open = 0;
   1159  1.1       jtc 		closepipe(j_sync_pipe);
   1160  1.1       jtc 	}
   1161  1.1       jtc #endif /* NEED_PGRP_SYNC */
   1162  1.1       jtc #ifdef JOB_SIGS
   1163  1.1       jtc 	if (held_sigchld) {
   1164  1.1       jtc 		held_sigchld = 0;
   1165  1.2       tls 		/* Don't call j_sigchld() as it may remove job... */
   1166  1.1       jtc 		kill(procpid, SIGCHLD);
   1167  1.1       jtc 	}
   1168  1.1       jtc #endif /* JOB_SIGS */
   1169  1.1       jtc }
   1170  1.1       jtc 
   1171  1.1       jtc /*
   1172  1.1       jtc  * wait for job to complete or change state
   1173  1.1       jtc  *
   1174  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1175  1.1       jtc  */
   1176  1.1       jtc static int
   1177  1.1       jtc j_waitj(j, flags, where)
   1178  1.1       jtc 	Job	*j;
   1179  1.1       jtc 	int	flags;		/* see JW_* */
   1180  1.1       jtc 	const char *where;
   1181  1.1       jtc {
   1182  1.1       jtc 	int	rv;
   1183  1.1       jtc 
   1184  1.1       jtc 	/*
   1185  1.1       jtc 	 * No auto-notify on the job we are waiting on.
   1186  1.1       jtc 	 */
   1187  1.1       jtc 	j->flags |= JF_WAITING;
   1188  1.1       jtc 	if (flags & JW_ASYNCNOTIFY)
   1189  1.1       jtc 		j->flags |= JF_W_ASYNCNOTIFY;
   1190  1.1       jtc 
   1191  1.1       jtc 	if (!Flag(FMONITOR))
   1192  1.1       jtc 		flags |= JW_STOPPEDWAIT;
   1193  1.1       jtc 
   1194  1.1       jtc 	while ((volatile int) j->state == PRUNNING
   1195  1.1       jtc 		|| ((flags & JW_STOPPEDWAIT)
   1196  1.1       jtc 		    && (volatile int) j->state == PSTOPPED))
   1197  1.1       jtc 	{
   1198  1.1       jtc #ifdef JOB_SIGS
   1199  1.1       jtc 		sigsuspend(&sm_default);
   1200  1.1       jtc #else /* JOB_SIGS */
   1201  1.1       jtc 		j_sigchld(SIGCHLD);
   1202  1.1       jtc #endif /* JOB_SIGS */
   1203  1.1       jtc 		if (fatal_trap) {
   1204  1.1       jtc 			int oldf = j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY);
   1205  1.1       jtc 			j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
   1206  1.1       jtc 			runtraps(TF_FATAL);
   1207  1.1       jtc 			j->flags |= oldf; /* not reached... */
   1208  1.1       jtc 		}
   1209  1.1       jtc 		if ((flags & JW_INTERRUPT) && (rv = trap_pending())) {
   1210  1.1       jtc 			j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
   1211  1.1       jtc 			return -rv;
   1212  1.1       jtc 		}
   1213  1.1       jtc 	}
   1214  1.1       jtc 	j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
   1215  1.1       jtc 
   1216  1.1       jtc 	if (j->flags & JF_FG) {
   1217  1.1       jtc 		WAIT_T	status;
   1218  1.1       jtc 
   1219  1.1       jtc 		j->flags &= ~JF_FG;
   1220  1.1       jtc #ifdef TTY_PGRP
   1221  1.1       jtc 		if (Flag(FMONITOR) && ttypgrp_ok && j->pgrp) {
   1222  1.5   hubertf 			/*
   1223  1.5   hubertf 			 * Save the tty's current pgrp so it can be restored
   1224  1.5   hubertf 			 * when the job is foregrounded.  This is to
   1225  1.5   hubertf 			 * deal with things like the GNU su which does
   1226  1.5   hubertf 			 * a fork/exec instead of an exec (the fork means
   1227  1.5   hubertf 			 * the execed shell gets a different pid from its
   1228  1.5   hubertf 			 * pgrp, so naturally it sets its pgrp and gets hosed
   1229  1.5   hubertf 			 * when it gets forgrounded by the parent shell, which
   1230  1.5   hubertf 			 * has restored the tty's pgrp to that of the su
   1231  1.5   hubertf 			 * process).
   1232  1.5   hubertf 			 */
   1233  1.5   hubertf 			if (j->state == PSTOPPED
   1234  1.5   hubertf 			    && (j->saved_ttypgrp = tcgetpgrp(tty_fd)) >= 0)
   1235  1.5   hubertf 				j->flags |= JF_SAVEDTTYPGRP;
   1236  1.1       jtc 			if (tcsetpgrp(tty_fd, our_pgrp) < 0) {
   1237  1.1       jtc 				warningf(TRUE,
   1238  1.1       jtc 				"j_waitj: tcsetpgrp(%d, %d) failed: %s",
   1239  1.1       jtc 					tty_fd, (int) our_pgrp,
   1240  1.1       jtc 					strerror(errno));
   1241  1.1       jtc 			}
   1242  1.1       jtc 			if (j->state == PSTOPPED) {
   1243  1.1       jtc 				j->flags |= JF_SAVEDTTY;
   1244  1.1       jtc 				get_tty(tty_fd, &j->ttystate);
   1245  1.1       jtc 			}
   1246  1.1       jtc 		}
   1247  1.1       jtc #endif /* TTY_PGRP */
   1248  1.1       jtc 		if (tty_fd >= 0) {
   1249  1.1       jtc 			/* Only restore tty settings if job was originally
   1250  1.1       jtc 			 * started in the foreground.  Problems can be
   1251  1.1       jtc 			 * caused by things like `more foobar &' which will
   1252  1.1       jtc 			 * typically get and save the shell's vi/emacs tty
   1253  1.1       jtc 			 * settings before setting up the tty for itself;
   1254  1.1       jtc 			 * when more exits, it restores the `original'
   1255  1.1       jtc 			 * settings, and things go down hill from there...
   1256  1.1       jtc 			 */
   1257  1.1       jtc 			if (j->state == PEXITED && j->status == 0
   1258  1.1       jtc 			    && (j->flags & JF_USETTYMODE))
   1259  1.1       jtc 			{
   1260  1.1       jtc 				get_tty(tty_fd, &tty_state);
   1261  1.1       jtc 			} else {
   1262  1.1       jtc 				set_tty(tty_fd, &tty_state,
   1263  1.1       jtc 				    (j->state == PEXITED) ? 0 : TF_MIPSKLUDGE);
   1264  1.1       jtc 				/* Don't use tty mode if job is stopped and
   1265  1.1       jtc 				 * later restarted and exits.  Consider
   1266  1.1       jtc 				 * the sequence:
   1267  1.1       jtc 				 *	vi foo (stopped)
   1268  1.1       jtc 				 *	...
   1269  1.1       jtc 				 *	stty something
   1270  1.1       jtc 				 *	...
   1271  1.1       jtc 				 *	fg (vi; ZZ)
   1272  1.1       jtc 				 * mode should be that of the stty, not what
   1273  1.1       jtc 				 * was before the vi started.
   1274  1.1       jtc 				 */
   1275  1.1       jtc 				if (j->state == PSTOPPED)
   1276  1.1       jtc 					j->flags &= ~JF_USETTYMODE;
   1277  1.1       jtc 			}
   1278  1.1       jtc 		}
   1279  1.1       jtc #ifdef JOBS
   1280  1.1       jtc 		/* If it looks like user hit ^C to kill a job, pretend we got
   1281  1.1       jtc 		 * one too to break out of for loops, etc.  (at&t ksh does this
   1282  1.1       jtc 		 * even when not monitoring, but this doesn't make sense since
   1283  1.1       jtc 		 * a tty generated ^C goes to the whole process group)
   1284  1.1       jtc 		 */
   1285  1.1       jtc 		status = j->last_proc->status;
   1286  1.1       jtc 		if (Flag(FMONITOR) && j->state == PSIGNALLED
   1287  1.1       jtc 		    && WIFSIGNALED(status)
   1288  1.1       jtc 		    && (sigtraps[WTERMSIG(status)].flags & TF_TTY_INTR))
   1289  1.1       jtc 			trapsig(WTERMSIG(status));
   1290  1.1       jtc #endif /* JOBS */
   1291  1.1       jtc 	}
   1292  1.1       jtc 
   1293  1.1       jtc 	j_usrtime = j->usrtime;
   1294  1.1       jtc 	j_systime = j->systime;
   1295  1.1       jtc 	rv = j->status;
   1296  1.1       jtc 
   1297  1.1       jtc 	if (!(flags & JW_ASYNCNOTIFY)
   1298  1.1       jtc 	    && (!Flag(FMONITOR) || j->state != PSTOPPED))
   1299  1.1       jtc 	{
   1300  1.1       jtc 		j_print(j, JP_SHORT, shl_out);
   1301  1.1       jtc 		shf_flush(shl_out);
   1302  1.1       jtc 	}
   1303  1.1       jtc 	if (j->state != PSTOPPED
   1304  1.1       jtc 	    && (!Flag(FMONITOR) || !(flags & JW_ASYNCNOTIFY)))
   1305  1.1       jtc 		remove_job(j, where);
   1306  1.1       jtc 
   1307  1.1       jtc 	return rv;
   1308  1.1       jtc }
   1309  1.1       jtc 
   1310  1.1       jtc /* SIGCHLD handler to reap children and update job states
   1311  1.1       jtc  *
   1312  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1313  1.1       jtc  */
   1314  1.1       jtc static RETSIGTYPE
   1315  1.1       jtc j_sigchld(sig)
   1316  1.1       jtc 	int	sig;
   1317  1.1       jtc {
   1318  1.1       jtc 	int		errno_ = errno;
   1319  1.1       jtc 	Job		*j;
   1320  1.1       jtc 	Proc		UNINITIALIZED(*p);
   1321  1.1       jtc 	int		pid;
   1322  1.1       jtc 	WAIT_T		status;
   1323  1.1       jtc 	struct tms	t0, t1;
   1324  1.1       jtc 
   1325  1.1       jtc #ifdef JOB_SIGS
   1326  1.1       jtc 	/* Don't wait for any processes if a job is partially started.
   1327  1.1       jtc 	 * This is so we don't do away with the process group leader
   1328  1.1       jtc 	 * before all the processes in a pipe line are started (so the
   1329  1.1       jtc 	 * setpgid() won't fail)
   1330  1.1       jtc 	 */
   1331  1.1       jtc 	for (j = job_list; j; j = j->next)
   1332  1.1       jtc 		if (j->ppid == procpid && !(j->flags & JF_STARTED)) {
   1333  1.1       jtc 			held_sigchld = 1;
   1334  1.1       jtc 			return RETSIGVAL;
   1335  1.1       jtc 		}
   1336  1.1       jtc #endif /* JOB_SIGS */
   1337  1.1       jtc 
   1338  1.1       jtc 	ksh_times(&t0);
   1339  1.1       jtc 	do {
   1340  1.1       jtc #ifdef JOB_SIGS
   1341  1.1       jtc 		pid = ksh_waitpid(-1, &status, (WNOHANG|WUNTRACED));
   1342  1.1       jtc #else /* JOB_SIGS */
   1343  1.1       jtc 		pid = wait(&status);
   1344  1.1       jtc #endif /* JOB_SIGS */
   1345  1.1       jtc 
   1346  1.1       jtc 		if (pid <= 0)	/* return if would block (0) ... */
   1347  1.1       jtc 			break;	/* ... or no children or interrupted (-1) */
   1348  1.1       jtc 
   1349  1.1       jtc 		ksh_times(&t1);
   1350  1.1       jtc 
   1351  1.1       jtc 		/* find job and process structures for this pid */
   1352  1.1       jtc 		for (j = job_list; j != (Job *) 0; j = j->next)
   1353  1.1       jtc 			for (p = j->proc_list; p != (Proc *) 0; p = p->next)
   1354  1.1       jtc 				if (p->pid == pid)
   1355  1.1       jtc 					goto found;
   1356  1.1       jtc found:
   1357  1.1       jtc 		if (j == (Job *) 0) {
   1358  1.1       jtc 			/* Can occur if process has kids, then execs shell
   1359  1.1       jtc 			warningf(TRUE, "bad process waited for (pid = %d)",
   1360  1.1       jtc 				pid);
   1361  1.1       jtc 			 */
   1362  1.1       jtc 			t0 = t1;
   1363  1.1       jtc 			continue;
   1364  1.1       jtc 		}
   1365  1.1       jtc 
   1366  1.1       jtc 		j->usrtime += t1.tms_cutime - t0.tms_cutime;
   1367  1.1       jtc 		j->systime += t1.tms_cstime - t0.tms_cstime;
   1368  1.1       jtc 		t0 = t1;
   1369  1.1       jtc 		p->status = status;
   1370  1.1       jtc #ifdef JOBS
   1371  1.1       jtc 		if (WIFSTOPPED(status))
   1372  1.1       jtc 			p->state = PSTOPPED;
   1373  1.1       jtc 		else
   1374  1.1       jtc #endif /* JOBS */
   1375  1.1       jtc 		if (WIFSIGNALED(status))
   1376  1.1       jtc 			p->state = PSIGNALLED;
   1377  1.1       jtc 		else
   1378  1.1       jtc 			p->state = PEXITED;
   1379  1.1       jtc 
   1380  1.1       jtc 		check_job(j);	/* check to see if entire job is done */
   1381  1.1       jtc 	}
   1382  1.1       jtc #ifdef JOB_SIGS
   1383  1.1       jtc 	while (1);
   1384  1.1       jtc #else /* JOB_SIGS */
   1385  1.1       jtc 	while (0);
   1386  1.1       jtc #endif /* JOB_SIGS */
   1387  1.1       jtc 
   1388  1.1       jtc 	errno = errno_;
   1389  1.1       jtc 
   1390  1.1       jtc 	return RETSIGVAL;
   1391  1.1       jtc }
   1392  1.1       jtc 
   1393  1.1       jtc /*
   1394  1.1       jtc  * Called only when a process in j has exited/stopped (ie, called only
   1395  1.2       tls  * from j_sigchld()).  If no processes are running, the job status
   1396  1.1       jtc  * and state are updated, asynchronous job notification is done and,
   1397  1.1       jtc  * if unneeded, the job is removed.
   1398  1.1       jtc  *
   1399  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1400  1.1       jtc  */
   1401  1.1       jtc static void
   1402  1.1       jtc check_job(j)
   1403  1.1       jtc 	Job	*j;
   1404  1.1       jtc {
   1405  1.1       jtc 	int	jstate;
   1406  1.1       jtc 	Proc	*p;
   1407  1.1       jtc 
   1408  1.1       jtc 	/* XXX debugging (nasty - interrupt routine using shl_out) */
   1409  1.1       jtc 	if (!(j->flags & JF_STARTED)) {
   1410  1.1       jtc 		internal_errorf(0, "check_job: job started (flags 0x%x)",
   1411  1.1       jtc 			j->flags);
   1412  1.1       jtc 		return;
   1413  1.1       jtc 	}
   1414  1.1       jtc 
   1415  1.1       jtc 	jstate = PRUNNING;
   1416  1.1       jtc 	for (p=j->proc_list; p != (Proc *) 0; p = p->next) {
   1417  1.1       jtc 		if (p->state == PRUNNING)
   1418  1.1       jtc 			return;	/* some processes still running */
   1419  1.1       jtc 		if (p->state > jstate)
   1420  1.1       jtc 			jstate = p->state;
   1421  1.1       jtc 	}
   1422  1.1       jtc 	j->state = jstate;
   1423  1.1       jtc 
   1424  1.1       jtc 	switch (j->last_proc->state) {
   1425  1.1       jtc 	case PEXITED:
   1426  1.1       jtc 		j->status = WEXITSTATUS(j->last_proc->status);
   1427  1.1       jtc 		break;
   1428  1.1       jtc 	case PSIGNALLED:
   1429  1.1       jtc 		j->status = 128 + WTERMSIG(j->last_proc->status);
   1430  1.1       jtc 		break;
   1431  1.1       jtc 	default:
   1432  1.1       jtc 		j->status = 0;
   1433  1.1       jtc 		break;
   1434  1.1       jtc 	}
   1435  1.1       jtc 
   1436  1.1       jtc #ifdef KSH
   1437  1.1       jtc 	/* Note when co-process dies: can't be done in j_wait() nor
   1438  1.1       jtc 	 * remove_job() since neither may be called for non-interactive
   1439  1.1       jtc 	 * shells.
   1440  1.1       jtc 	 */
   1441  1.1       jtc 	if (j->state == PEXITED || j->state == PSIGNALLED) {
   1442  1.1       jtc 		/* No need to keep co-process input any more
   1443  1.1       jtc 		 * (at leasst, this is what ksh93d thinks)
   1444  1.1       jtc 		 */
   1445  1.1       jtc 		if (coproc.job == j) {
   1446  1.1       jtc 			coproc.job = (void *) 0;
   1447  1.1       jtc 			/* XXX would be nice to get the closes out of here
   1448  1.1       jtc 			 * so they aren't done in the signal handler.
   1449  1.1       jtc 			 * Would mean a check in coproc_getfd() to
   1450  1.1       jtc 			 * do "if job == 0 && write >= 0, close write".
   1451  1.1       jtc 			 */
   1452  1.1       jtc 			coproc_write_close(coproc.write);
   1453  1.1       jtc 		}
   1454  1.1       jtc 		/* Do we need to keep the output? */
   1455  1.1       jtc 		if (j->coproc_id && j->coproc_id == coproc.id
   1456  1.1       jtc 		    && --coproc.njobs == 0)
   1457  1.1       jtc 			coproc_readw_close(coproc.read);
   1458  1.1       jtc 	}
   1459  1.1       jtc #endif /* KSH */
   1460  1.1       jtc 
   1461  1.1       jtc 	j->flags |= JF_CHANGED;
   1462  1.1       jtc #ifdef JOBS
   1463  1.1       jtc 	if (Flag(FMONITOR) && !(j->flags & JF_XXCOM)) {
   1464  1.1       jtc 		/* Only put stopped jobs at the front to avoid confusing
   1465  1.1       jtc 		 * the user (don't want finished jobs effecting %+ or %-)
   1466  1.1       jtc 		 */
   1467  1.1       jtc 		if (j->state == PSTOPPED)
   1468  1.1       jtc 			put_job(j, PJ_ON_FRONT);
   1469  1.1       jtc 		if (Flag(FNOTIFY)
   1470  1.1       jtc 		    && (j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY)) != JF_WAITING)
   1471  1.1       jtc 		{
   1472  1.1       jtc 			/* Look for the real file descriptor 2 */
   1473  1.1       jtc 			{
   1474  1.1       jtc 				struct env *ep;
   1475  1.1       jtc 				int fd = 2;
   1476  1.1       jtc 
   1477  1.1       jtc 				for (ep = e; ep; ep = ep->oenv)
   1478  1.1       jtc 					if (ep->savefd && ep->savefd[2])
   1479  1.1       jtc 						fd = ep->savefd[2];
   1480  1.1       jtc 				shf_reopen(fd, SHF_WR, shl_j);
   1481  1.1       jtc 			}
   1482  1.1       jtc 			/* Can't call j_notify() as it removes jobs.  The job
   1483  1.1       jtc 			 * must stay in the job list as j_waitj() may be
   1484  1.1       jtc 			 * running with this job.
   1485  1.1       jtc 			 */
   1486  1.1       jtc 			j_print(j, JP_MEDIUM, shl_j);
   1487  1.1       jtc 			shf_flush(shl_j);
   1488  1.1       jtc 			if (!(j->flags & JF_WAITING) && j->state != PSTOPPED)
   1489  1.1       jtc 				remove_job(j, "notify");
   1490  1.1       jtc 		}
   1491  1.1       jtc 	}
   1492  1.1       jtc #endif /* JOBS */
   1493  1.1       jtc 	if (!Flag(FMONITOR) && !(j->flags & (JF_WAITING|JF_FG))
   1494  1.1       jtc 	    && j->state != PSTOPPED)
   1495  1.1       jtc 	{
   1496  1.1       jtc 		if (j == async_job || (j->flags & JF_KNOWN)) {
   1497  1.1       jtc 			j->flags |= JF_ZOMBIE;
   1498  1.1       jtc 			j->job = -1;
   1499  1.1       jtc 			nzombie++;
   1500  1.1       jtc 		} else
   1501  1.1       jtc 			remove_job(j, "checkjob");
   1502  1.1       jtc 	}
   1503  1.1       jtc }
   1504  1.1       jtc 
   1505  1.1       jtc /*
   1506  1.1       jtc  * Print job status in either short, medium or long format.
   1507  1.1       jtc  *
   1508  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1509  1.1       jtc  */
   1510  1.1       jtc static void
   1511  1.1       jtc j_print(j, how, shf)
   1512  1.1       jtc 	Job		*j;
   1513  1.1       jtc 	int		how;
   1514  1.1       jtc 	struct shf	*shf;
   1515  1.1       jtc {
   1516  1.1       jtc 	Proc	*p;
   1517  1.1       jtc 	int	state;
   1518  1.1       jtc 	WAIT_T	status;
   1519  1.1       jtc 	int	coredumped;
   1520  1.1       jtc 	char	jobchar = ' ';
   1521  1.1       jtc 	char	buf[64];
   1522  1.1       jtc 	const char *filler;
   1523  1.1       jtc 	int	output = 0;
   1524  1.1       jtc 
   1525  1.1       jtc 	if (how == JP_PGRP) {
   1526  1.1       jtc 		/* POSIX doesn't say what to do it there is no process
   1527  1.1       jtc 		 * group leader (ie, !FMONITOR).  We arbitrarily return
   1528  1.1       jtc 		 * last pid (which is what $! returns).
   1529  1.1       jtc 		 */
   1530  1.1       jtc 		shf_fprintf(shf, "%d\n", j->pgrp ? j->pgrp
   1531  1.1       jtc 				: (j->last_proc ? j->last_proc->pid : 0));
   1532  1.1       jtc 		return;
   1533  1.1       jtc 	}
   1534  1.1       jtc 	j->flags &= ~JF_CHANGED;
   1535  1.1       jtc 	filler = j->job > 10 ?  "\n       " : "\n      ";
   1536  1.1       jtc 	if (j == job_list)
   1537  1.1       jtc 		jobchar = '+';
   1538  1.1       jtc 	else if (j == job_list->next)
   1539  1.1       jtc 		jobchar = '-';
   1540  1.1       jtc 
   1541  1.1       jtc 	for (p = j->proc_list; p != (Proc *) 0;) {
   1542  1.1       jtc 		coredumped = 0;
   1543  1.1       jtc 		switch (p->state) {
   1544  1.1       jtc 		case PRUNNING:
   1545  1.1       jtc 			strcpy(buf, "Running");
   1546  1.1       jtc 			break;
   1547  1.1       jtc 		case PSTOPPED:
   1548  1.1       jtc 			strcpy(buf, sigtraps[WSTOPSIG(p->status)].mess);
   1549  1.1       jtc 			break;
   1550  1.1       jtc 		case PEXITED:
   1551  1.1       jtc 			if (how == JP_SHORT)
   1552  1.1       jtc 				buf[0] = '\0';
   1553  1.1       jtc 			else if (WEXITSTATUS(p->status) == 0)
   1554  1.1       jtc 				strcpy(buf, "Done");
   1555  1.1       jtc 			else
   1556  1.1       jtc 				shf_snprintf(buf, sizeof(buf), "Done (%d)",
   1557  1.1       jtc 					WEXITSTATUS(p->status));
   1558  1.1       jtc 			break;
   1559  1.1       jtc 		case PSIGNALLED:
   1560  1.1       jtc 			if (WIFCORED(p->status))
   1561  1.1       jtc 				coredumped = 1;
   1562  1.1       jtc 			/* kludge for not reporting `normal termination signals'
   1563  1.1       jtc 			 * (ie, SIGINT, SIGPIPE)
   1564  1.1       jtc 			 */
   1565  1.1       jtc 			if (how == JP_SHORT && !coredumped
   1566  1.1       jtc 			    && (WTERMSIG(p->status) == SIGINT
   1567  1.1       jtc 				|| WTERMSIG(p->status) == SIGPIPE)) {
   1568  1.1       jtc 				buf[0] = '\0';
   1569  1.1       jtc 			} else
   1570  1.1       jtc 				strcpy(buf, sigtraps[WTERMSIG(p->status)].mess);
   1571  1.1       jtc 			break;
   1572  1.1       jtc 		}
   1573  1.1       jtc 
   1574  1.4   thorpej 		if (how != JP_SHORT) {
   1575  1.1       jtc 			if (p == j->proc_list)
   1576  1.1       jtc 				shf_fprintf(shf, "[%d] %c ", j->job, jobchar);
   1577  1.1       jtc 			else
   1578  1.1       jtc 				shf_fprintf(shf, "%s", filler);
   1579  1.4   thorpej 		}
   1580  1.1       jtc 
   1581  1.1       jtc 		if (how == JP_LONG)
   1582  1.1       jtc 			shf_fprintf(shf, "%5d ", p->pid);
   1583  1.1       jtc 
   1584  1.1       jtc 		if (how == JP_SHORT) {
   1585  1.1       jtc 			if (buf[0]) {
   1586  1.1       jtc 				output = 1;
   1587  1.1       jtc 				shf_fprintf(shf, "%s%s ",
   1588  1.1       jtc 					buf, coredumped ? " (core dumped)" : null);
   1589  1.1       jtc 			}
   1590  1.1       jtc 		} else {
   1591  1.1       jtc 			output = 1;
   1592  1.1       jtc 			shf_fprintf(shf, "%-20s %s%s%s", buf, p->command,
   1593  1.1       jtc 				p->next ? "|" : null,
   1594  1.1       jtc 				coredumped ? " (core dumped)" : null);
   1595  1.1       jtc 		}
   1596  1.1       jtc 
   1597  1.1       jtc 		state = p->state;
   1598  1.1       jtc 		status = p->status;
   1599  1.1       jtc 		p = p->next;
   1600  1.1       jtc 		while (p && p->state == state
   1601  1.1       jtc 		       && WSTATUS(p->status) == WSTATUS(status))
   1602  1.1       jtc 		{
   1603  1.1       jtc 			if (how == JP_LONG)
   1604  1.1       jtc 				shf_fprintf(shf, "%s%5d %-20s %s%s", filler, p->pid,
   1605  1.1       jtc 					space, p->command, p->next ? "|" : null);
   1606  1.1       jtc 			else if (how == JP_MEDIUM)
   1607  1.1       jtc 				shf_fprintf(shf, " %s%s", p->command,
   1608  1.1       jtc 					p->next ? "|" : null);
   1609  1.1       jtc 			p = p->next;
   1610  1.1       jtc 		}
   1611  1.1       jtc 	}
   1612  1.1       jtc 	if (output)
   1613  1.1       jtc 		shf_fprintf(shf, newline);
   1614  1.1       jtc }
   1615  1.1       jtc 
   1616  1.1       jtc /* Convert % sequence to job
   1617  1.1       jtc  *
   1618  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1619  1.1       jtc  */
   1620  1.1       jtc static Job *
   1621  1.1       jtc j_lookup(cp, ecodep)
   1622  1.1       jtc 	const char *cp;
   1623  1.1       jtc 	int	*ecodep;
   1624  1.1       jtc {
   1625  1.1       jtc 	Job		*j, *last_match;
   1626  1.1       jtc 	Proc		*p;
   1627  1.1       jtc 	int		len, job = 0;
   1628  1.1       jtc 
   1629  1.1       jtc 	if (digit(*cp)) {
   1630  1.1       jtc 		job = atoi(cp);
   1631  1.1       jtc 		/* Look for last_proc->pid (what $! returns) first... */
   1632  1.1       jtc 		for (j = job_list; j != (Job *) 0; j = j->next)
   1633  1.1       jtc 			if (j->last_proc && j->last_proc->pid == job)
   1634  1.1       jtc 				return j;
   1635  1.1       jtc 		/* ...then look for process group (this is non-POSIX),
   1636  1.1       jtc 		 * but should not break anything (so FPOSIX isn't used).
   1637  1.1       jtc 		 */
   1638  1.1       jtc 		for (j = job_list; j != (Job *) 0; j = j->next)
   1639  1.1       jtc 			if (j->pgrp && j->pgrp == job)
   1640  1.1       jtc 				return j;
   1641  1.1       jtc 		if (ecodep)
   1642  1.1       jtc 			*ecodep = JL_NOSUCH;
   1643  1.1       jtc 		return (Job *) 0;
   1644  1.1       jtc 	}
   1645  1.1       jtc 	if (*cp != '%') {
   1646  1.1       jtc 		if (ecodep)
   1647  1.1       jtc 			*ecodep = JL_INVALID;
   1648  1.1       jtc 		return (Job *) 0;
   1649  1.1       jtc 	}
   1650  1.1       jtc 	switch (*++cp) {
   1651  1.1       jtc 	  case '\0': /* non-standard */
   1652  1.1       jtc 	  case '+':
   1653  1.1       jtc 	  case '%':
   1654  1.1       jtc 		if (job_list != (Job *) 0)
   1655  1.1       jtc 			return job_list;
   1656  1.1       jtc 		break;
   1657  1.1       jtc 
   1658  1.1       jtc 	  case '-':
   1659  1.1       jtc 		if (job_list != (Job *) 0 && job_list->next)
   1660  1.1       jtc 			return job_list->next;
   1661  1.1       jtc 		break;
   1662  1.1       jtc 
   1663  1.1       jtc 	  case '0': case '1': case '2': case '3': case '4':
   1664  1.1       jtc 	  case '5': case '6': case '7': case '8': case '9':
   1665  1.1       jtc 		job = atoi(cp);
   1666  1.1       jtc 		for (j = job_list; j != (Job *) 0; j = j->next)
   1667  1.1       jtc 			if (j->job == job)
   1668  1.1       jtc 				return j;
   1669  1.1       jtc 		break;
   1670  1.1       jtc 
   1671  1.1       jtc 	  case '?':		/* %?string */
   1672  1.1       jtc 		last_match = (Job *) 0;
   1673  1.1       jtc 		for (j = job_list; j != (Job *) 0; j = j->next)
   1674  1.1       jtc 			for (p = j->proc_list; p != (Proc *) 0; p = p->next)
   1675  1.1       jtc 				if (strstr(p->command, cp+1) != (char *) 0) {
   1676  1.1       jtc 					if (last_match) {
   1677  1.1       jtc 						if (ecodep)
   1678  1.1       jtc 							*ecodep = JL_AMBIG;
   1679  1.1       jtc 						return (Job *) 0;
   1680  1.1       jtc 					}
   1681  1.1       jtc 					last_match = j;
   1682  1.1       jtc 				}
   1683  1.1       jtc 		if (last_match)
   1684  1.1       jtc 			return last_match;
   1685  1.1       jtc 		break;
   1686  1.1       jtc 
   1687  1.1       jtc 	  default:		/* %string */
   1688  1.1       jtc 		len = strlen(cp);
   1689  1.1       jtc 		last_match = (Job *) 0;
   1690  1.1       jtc 		for (j = job_list; j != (Job *) 0; j = j->next)
   1691  1.1       jtc 			if (strncmp(cp, j->proc_list->command, len) == 0) {
   1692  1.1       jtc 				if (last_match) {
   1693  1.1       jtc 					if (ecodep)
   1694  1.1       jtc 						*ecodep = JL_AMBIG;
   1695  1.1       jtc 					return (Job *) 0;
   1696  1.1       jtc 				}
   1697  1.1       jtc 				last_match = j;
   1698  1.1       jtc 			}
   1699  1.1       jtc 		if (last_match)
   1700  1.1       jtc 			return last_match;
   1701  1.1       jtc 		break;
   1702  1.1       jtc 	}
   1703  1.1       jtc 	if (ecodep)
   1704  1.1       jtc 		*ecodep = JL_NOSUCH;
   1705  1.1       jtc 	return (Job *) 0;
   1706  1.1       jtc }
   1707  1.1       jtc 
   1708  1.1       jtc static Job	*free_jobs;
   1709  1.1       jtc static Proc	*free_procs;
   1710  1.1       jtc 
   1711  1.1       jtc /* allocate a new job and fill in the job number.
   1712  1.1       jtc  *
   1713  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1714  1.1       jtc  */
   1715  1.1       jtc static Job *
   1716  1.1       jtc new_job()
   1717  1.1       jtc {
   1718  1.1       jtc 	int	i;
   1719  1.1       jtc 	Job	*newj, *j;
   1720  1.1       jtc 
   1721  1.1       jtc 	if (free_jobs != (Job *) 0) {
   1722  1.1       jtc 		newj = free_jobs;
   1723  1.1       jtc 		free_jobs = free_jobs->next;
   1724  1.1       jtc 	} else
   1725  1.1       jtc 		newj = (Job *) alloc(sizeof(Job), APERM);
   1726  1.1       jtc 
   1727  1.1       jtc 	/* brute force method */
   1728  1.1       jtc 	for (i = 1; ; i++) {
   1729  1.1       jtc 		for (j = job_list; j && j->job != i; j = j->next)
   1730  1.1       jtc 			;
   1731  1.1       jtc 		if (j == (Job *) 0)
   1732  1.1       jtc 			break;
   1733  1.1       jtc 	}
   1734  1.1       jtc 	newj->job = i;
   1735  1.1       jtc 
   1736  1.1       jtc 	return newj;
   1737  1.1       jtc }
   1738  1.1       jtc 
   1739  1.1       jtc /* Allocate new process strut
   1740  1.1       jtc  *
   1741  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1742  1.1       jtc  */
   1743  1.1       jtc static Proc *
   1744  1.1       jtc new_proc()
   1745  1.1       jtc {
   1746  1.1       jtc 	Proc	*p;
   1747  1.1       jtc 
   1748  1.1       jtc 	if (free_procs != (Proc *) 0) {
   1749  1.1       jtc 		p = free_procs;
   1750  1.1       jtc 		free_procs = free_procs->next;
   1751  1.1       jtc 	} else
   1752  1.1       jtc 		p = (Proc *) alloc(sizeof(Proc), APERM);
   1753  1.1       jtc 
   1754  1.1       jtc 	return p;
   1755  1.1       jtc }
   1756  1.1       jtc 
   1757  1.1       jtc /* Take job out of job_list and put old structures into free list.
   1758  1.1       jtc  * Keeps nzombies, last_job and async_job up to date.
   1759  1.1       jtc  *
   1760  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1761  1.1       jtc  */
   1762  1.1       jtc static void
   1763  1.1       jtc remove_job(j, where)
   1764  1.1       jtc 	Job	*j;
   1765  1.1       jtc 	const char *where;
   1766  1.1       jtc {
   1767  1.1       jtc 	Proc	*p, *tmp;
   1768  1.1       jtc 	Job	**prev, *curr;
   1769  1.1       jtc 
   1770  1.1       jtc 	prev = &job_list;
   1771  1.1       jtc 	curr = *prev;
   1772  1.1       jtc 	for (; curr != (Job *) 0 && curr != j; prev = &curr->next, curr = *prev)
   1773  1.1       jtc 		;
   1774  1.1       jtc 	if (curr != j) {
   1775  1.1       jtc 		internal_errorf(0, "remove_job: job not found (%s)", where);
   1776  1.1       jtc 		return;
   1777  1.1       jtc 	}
   1778  1.1       jtc 	*prev = curr->next;
   1779  1.1       jtc 
   1780  1.1       jtc 	/* free up proc structures */
   1781  1.1       jtc 	for (p = j->proc_list; p != (Proc *) 0; ) {
   1782  1.1       jtc 		tmp = p;
   1783  1.1       jtc 		p = p->next;
   1784  1.1       jtc 		tmp->next = free_procs;
   1785  1.1       jtc 		free_procs = tmp;
   1786  1.1       jtc 	}
   1787  1.1       jtc 
   1788  1.1       jtc 	if ((j->flags & JF_ZOMBIE) && j->ppid == procpid)
   1789  1.1       jtc 		--nzombie;
   1790  1.1       jtc 	j->next = free_jobs;
   1791  1.1       jtc 	free_jobs = j;
   1792  1.1       jtc 
   1793  1.1       jtc 	if (j == last_job)
   1794  1.1       jtc 		last_job = (Job *) 0;
   1795  1.1       jtc 	if (j == async_job)
   1796  1.1       jtc 		async_job = (Job *) 0;
   1797  1.1       jtc }
   1798  1.1       jtc 
   1799  1.1       jtc /* put j in a particular location (taking it out job_list if it is there
   1800  1.1       jtc  * already)
   1801  1.1       jtc  *
   1802  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1803  1.1       jtc  */
   1804  1.1       jtc static void
   1805  1.1       jtc put_job(j, where)
   1806  1.1       jtc 	Job	*j;
   1807  1.1       jtc 	int	where;
   1808  1.1       jtc {
   1809  1.1       jtc 	Job	**prev, *curr;
   1810  1.1       jtc 
   1811  1.1       jtc 	/* Remove job from list (if there) */
   1812  1.1       jtc 	prev = &job_list;
   1813  1.1       jtc 	curr = job_list;
   1814  1.1       jtc 	for (; curr && curr != j; prev = &curr->next, curr = *prev)
   1815  1.1       jtc 		;
   1816  1.1       jtc 	if (curr == j)
   1817  1.1       jtc 		*prev = curr->next;
   1818  1.1       jtc 
   1819  1.1       jtc 	switch (where) {
   1820  1.1       jtc 	case PJ_ON_FRONT:
   1821  1.1       jtc 		j->next = job_list;
   1822  1.1       jtc 		job_list = j;
   1823  1.1       jtc 		break;
   1824  1.1       jtc 
   1825  1.1       jtc 	case PJ_PAST_STOPPED:
   1826  1.1       jtc 		prev = &job_list;
   1827  1.1       jtc 		curr = job_list;
   1828  1.1       jtc 		for (; curr && curr->state == PSTOPPED; prev = &curr->next,
   1829  1.1       jtc 							curr = *prev)
   1830  1.1       jtc 			;
   1831  1.1       jtc 		j->next = curr;
   1832  1.1       jtc 		*prev = j;
   1833  1.1       jtc 		break;
   1834  1.1       jtc 	}
   1835  1.1       jtc }
   1836  1.1       jtc 
   1837  1.1       jtc /* nuke a job (called when unable to start full job).
   1838  1.1       jtc  *
   1839  1.1       jtc  * If jobs are compiled in then this routine expects sigchld to be blocked.
   1840  1.1       jtc  */
   1841  1.6  christos static int
   1842  1.6  christos kill_job(j, sig)
   1843  1.1       jtc 	Job	*j;
   1844  1.6  christos 	int	sig;
   1845  1.1       jtc {
   1846  1.1       jtc 	Proc	*p;
   1847  1.6  christos 	int	rval = 0;
   1848  1.1       jtc 
   1849  1.1       jtc 	for (p = j->proc_list; p != (Proc *) 0; p = p->next)
   1850  1.1       jtc 		if (p->pid != 0)
   1851  1.6  christos 			if (kill(p->pid, sig) < 0)
   1852  1.6  christos 				rval = -1;
   1853  1.6  christos 	return rval;
   1854  1.1       jtc }
   1855