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