1fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes/*	$OpenBSD: jobs.c,v 1.43 2015/09/10 22:48:58 nicm Exp $	*/
25155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
35155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*-
4811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
5fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes *		 2012, 2013, 2014, 2015, 2016
6fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes *	mirabilos <m@mirbsd.org>
75155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
85155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Provided that these terms and disclaimer and all copyright notices
95155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * are retained or reproduced in an accompanying document, permission
105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * is granted to deal in this work without restriction, including un-
115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * limited rights to use, publicly perform, distribute, sell, modify,
125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * merge, give away, or sublicence.
135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * the utmost extent permitted by applicable law, neither express nor
165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * implied; without malicious intent or gross negligence. In no event
175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * may a licensor, author or contributor be held liable for indirect,
185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * direct, other damage, loss, or other issues arising in any way out
195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * of dealing in the work, even if advised of the possibility of such
205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * damage or existence of a defect, except proven that it results out
215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * of said person's immediate fault when using the work as intended.
225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#include "sh.h"
255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
26fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.117 2016/01/14 23:18:09 tg Exp $");
275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#if HAVE_KILLPG
295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define mksh_killpg		killpg
305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#else
315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* cross fingers and hope kill is killpg-endowed */
325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define mksh_killpg(p,s)	kill(-(p), (s))
335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Order important! */
365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PRUNNING	0
375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PEXITED		1
385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PSIGNALLED	2
395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PSTOPPED	3
405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
41c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glasertypedef struct proc Proc;
425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustruct proc {
435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Proc *next;		/* next process in pipeline (if any) */
445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	pid_t pid;		/* process id */
455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int state;
465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int status;		/* wait status */
47c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	/* process command string from vistree */
48811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	char command[256 - (ALLOC_SIZE + sizeof(Proc *) + sizeof(pid_t) +
49c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	    2 * sizeof(int))];
505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru};
515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Notify/print flag - j_print() argument */
535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_SHORT	1	/* print signals processes were killed by */
545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_MEDIUM	2	/* print [job-num] -/+ command */
555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_LONG		3	/* print [job-num] -/+ pid command */
565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JP_PGRP		4	/* print pgrp */
575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* put_job() flags */
595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PJ_ON_FRONT	0	/* at very front */
605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define PJ_PAST_STOPPED	1	/* just past any stopped jobs */
615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Job.flags values */
635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_STARTED	0x001	/* set when all processes in job are started */
645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_WAITING	0x002	/* set if j_waitj() is waiting on job */
655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_W_ASYNCNOTIFY 0x004	/* set if waiting and async notification ok */
665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_XXCOM	0x008	/* set for $(command) jobs */
675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_FG		0x010	/* running in foreground (also has tty pgrp) */
6803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#define JF_SAVEDTTY	0x020	/* j->ttystat is valid */
695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_CHANGED	0x040	/* process has changed state */
705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_KNOWN	0x080	/* $! referenced */
715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_ZOMBIE	0x100	/* known, unwaited process */
725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_REMOVE	0x200	/* flagged for removal (j_jobs()/j_noityf()) */
735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_USETTYMODE	0x400	/* tty mode saved if process exits normally */
745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JF_SAVEDTTYPGRP	0x800	/* j->saved_ttypgrp is valid */
755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querutypedef struct job Job;
775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustruct job {
785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Job *next;		/* next job in list */
795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Proc *proc_list;	/* process list */
805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Proc *last_proc;	/* last process in list */
815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	struct timeval systime;	/* system time used by job */
825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	struct timeval usrtime;	/* user time used by job */
835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	pid_t pgrp;		/* process group of job */
845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	pid_t ppid;		/* pid of process that forked job */
855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int job;		/* job number: %n */
865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int flags;		/* see JF_* */
875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	volatile int state;	/* job state */
885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int status;		/* exit status of last process */
89fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes	int age;		/* number of jobs started */
905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Coproc_id coproc_id;	/* 0 or id of coprocess output pipe */
915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
92c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	mksh_ttyst ttystat;	/* saved tty state for stopped jobs */
935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	pid_t saved_ttypgrp;	/* saved tty process group for stopped jobs */
945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru};
965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Flags for j_waitj() */
985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_NONE		0x00
995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_INTERRUPT	0x01	/* ^C will stop the wait */
1005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_ASYNCNOTIFY	0x02	/* asynchronous notification during wait ok */
1015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define JW_STOPPEDWAIT	0x04	/* wait even if job stopped */
10203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#define JW_PIPEST	0x08	/* want PIPESTATUS */
1035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Error codes for j_lookup() */
105811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#define JL_NOSUCH	0	/* no such job */
106811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#define JL_AMBIG	1	/* %foo or %?foo is ambiguous */
107811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#define JL_INVALID	2	/* non-pid, non-% job id */
1085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
109c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic const char * const lookup_msgs[] = {
1105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	"no such job",
1115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	"ambiguous",
112811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	"argument must be %job or process id"
1135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru};
1145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *job_list;		/* job list */
1165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *last_job;
1175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *async_job;
1185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic pid_t async_pid;
1195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int nzombie;		/* # of zombies owned by this process */
121fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughesstatic int njobs;		/* # of jobs started */
1225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef CHILD_MAX
1245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#define CHILD_MAX	25
1255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
1265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
12703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
1285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* held_sigchld is set if sigchld occurs before a job is completely started */
1295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic volatile sig_atomic_t held_sigchld;
13003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
1315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
1335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic struct shf	*shl_j;
1345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic bool		ttypgrp_ok;	/* set if can use tty pgrps */
1355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic pid_t		restore_ttypgrp = -1;
1365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int const	tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU };
1375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
1385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		j_set_async(Job *);
1405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		j_startjob(Job *);
1415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int		j_waitj(Job *, int, const char *);
1425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		j_sigchld(int);
1435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		j_print(Job *, int, struct shf *);
1445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job		*j_lookup(const char *, int *);
1455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job		*new_job(void);
1465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Proc		*new_proc(void);
1475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		check_job(Job *);
1485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		put_job(Job *, int);
1495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void		remove_job(Job *, const char *);
1505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int		kill_job(Job *, int);
1515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
152c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void tty_init_talking(void);
153c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void tty_init_state(void);
154c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser
1555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* initialise job control */
1565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid
1575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_init(void)
1585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
1595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
1605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	bool mflagset = Flag(FMONITOR) != 127;
1615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Flag(FMONITOR) = 0;
1635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
1645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
1665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	(void)sigemptyset(&sm_default);
1675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &sm_default, NULL);
1685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	(void)sigemptyset(&sm_sigchld);
1705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	(void)sigaddset(&sm_sigchld, SIGCHLD);
1715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	setsig(&sigtraps[SIGCHLD], j_sigchld,
1735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
17403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else
17503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* Make sure SIGCHLD isn't ignored - can do odd things under SYSV */
17603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	setsig(&sigtraps[SIGCHLD], SIG_DFL, SS_RESTORE_ORIG|SS_FORCE);
17703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
1785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
1805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!mflagset && Flag(FTALKING))
1815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		Flag(FMONITOR) = 1;
1825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/*
1845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * shl_j is used to do asynchronous notification (used in
1855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * an interrupt handler, so need a distinct shf)
1865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 */
1875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	shl_j = shf_fdopen(2, SHF_WR, NULL);
1885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (Flag(FMONITOR) || Flag(FTALKING)) {
1905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		int i;
1915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
1935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * the TF_SHELL_USES test is a kludge that lets us know if
1945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * if the signals have been changed by the shell.
1955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
1965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (i = NELEM(tt_sigs); --i >= 0; ) {
1975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			sigtraps[tt_sigs[i]].flags |= TF_SHELL_USES;
1985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* j_change() sets this to SS_RESTORE_DFL if FMONITOR */
1995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
2005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    SS_RESTORE_IGN|SS_FORCE);
2015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
2025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
2035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
204c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	/* j_change() calls tty_init_talking() and tty_init_state() */
2055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (Flag(FMONITOR))
2065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j_change();
2075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	else
2085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
209c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	  if (Flag(FTALKING)) {
210c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		tty_init_talking();
211c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		tty_init_state();
212c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	}
2135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
2145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
21503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrastatic int
21603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraproc_errorlevel(Proc *p)
21703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra{
21803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	switch (p->state) {
21903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	case PEXITED:
220fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		return ((WEXITSTATUS(p->status)) & 255);
22103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	case PSIGNALLED:
222fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		return (ksh_sigmask(WTERMSIG(p->status)));
22303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	default:
22403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return (0);
22503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	}
22603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
22703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
228737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID
229737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes/* suspend the shell */
230737fdce098f804459a925438e48dd711c31bbc9eElliott Hughesvoid
231737fdce098f804459a925438e48dd711c31bbc9eElliott Hughesj_suspend(void)
232737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes{
233737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	struct sigaction sa, osa;
234737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes
235737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	/* Restore tty and pgrp. */
236737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	if (ttypgrp_ok) {
237737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		if (tty_hasstate)
238737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			mksh_tcset(tty_fd, &tty_state);
239737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		if (restore_ttypgrp >= 0) {
240737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			if (tcsetpgrp(tty_fd, restore_ttypgrp) < 0) {
241737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				warningf(false, "%s: %s %s: %s", "j_suspend",
242737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				    "tcsetpgrp", "failed", cstrerror(errno));
243737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			} else if (setpgid(0, restore_ttypgrp) < 0) {
244737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				warningf(false, "%s: %s %s: %s", "j_suspend",
245737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				    "setpgid", "failed", cstrerror(errno));
246737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			}
247737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		}
248737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	}
249737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes
250737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	/* Suspend the shell. */
251737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	memset(&sa, 0, sizeof(sa));
252737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	sigemptyset(&sa.sa_mask);
253737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	sa.sa_handler = SIG_DFL;
254737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	sigaction(SIGTSTP, &sa, &osa);
255737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	kill(0, SIGTSTP);
256737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes
257737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	/* Back from suspend, reset signals, pgrp and tty. */
258737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	sigaction(SIGTSTP, &osa, NULL);
259737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	if (ttypgrp_ok) {
260737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		if (restore_ttypgrp >= 0) {
261737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			if (setpgid(0, kshpid) < 0) {
262737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				warningf(false, "%s: %s %s: %s", "j_suspend",
263737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				    "setpgid", "failed", cstrerror(errno));
264737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				ttypgrp_ok = false;
265737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			} else if (tcsetpgrp(tty_fd, kshpid) < 0) {
266737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				warningf(false, "%s: %s %s: %s", "j_suspend",
267737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				    "tcsetpgrp", "failed", cstrerror(errno));
268737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				ttypgrp_ok = false;
269737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			}
270737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		}
271737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		tty_init_state();
272737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes	}
273737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes}
274737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#endif
275737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes
2765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* job cleanup before shell exit */
2775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid
2785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_exit(void)
2795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
2805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* kill stopped, and possibly running, jobs */
28103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
28203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	bool killed = false;
2835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
2845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (j = job_list; j != NULL; j = j->next) {
2855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->ppid == procpid &&
2865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    (j->state == PSTOPPED ||
2875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    (j->state == PRUNNING &&
2885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    ((j->flags & JF_FG) ||
2895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid))))) {
29003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			killed = true;
2915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->pgrp == 0)
2925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				kill_job(j, SIGHUP);
2935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			else
2945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				mksh_killpg(j->pgrp, SIGHUP);
2955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
2965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->state == PSTOPPED) {
2975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (j->pgrp == 0)
2985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					kill_job(j, SIGCONT);
2995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				else
3005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					mksh_killpg(j->pgrp, SIGCONT);
3015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
3025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
3035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
3045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
3055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (killed)
3065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sleep(1);
3075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j_notify();
3085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
3095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
3105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (kshpid == procpid && restore_ttypgrp >= 0) {
3115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
3125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * Need to restore the tty pgrp to what it was when the
3135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * shell started up, so that the process that started us
3145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * will be able to access the tty when we are done.
3155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * Also need to restore our process group in case we are
3165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * about to do an exec so that both our parent and the
3175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * process we are to become will be able to access the tty.
3185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
3195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		tcsetpgrp(tty_fd, restore_ttypgrp);
3205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		setpgid(0, restore_ttypgrp);
3215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
3225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (Flag(FMONITOR)) {
3235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		Flag(FMONITOR) = 0;
3245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j_change();
3255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
3265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
3275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
3285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
3295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
3305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* turn job control on or off according to Flag(FMONITOR) */
3315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid
3325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_change(void)
3335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
3345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int i;
3355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
3365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (Flag(FMONITOR)) {
3375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		bool use_tty = Flag(FTALKING);
3385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
339c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		/* don't call mksh_tcget until we own the tty process group */
3405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (use_tty)
341c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			tty_init_talking();
3425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
3435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* no controlling tty, no SIGT* */
344c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		if ((ttypgrp_ok = (use_tty && tty_fd >= 0 && tty_devtty))) {
3455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			setsig(&sigtraps[SIGTTIN], SIG_DFL,
3465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    SS_RESTORE_ORIG|SS_FORCE);
3475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* wait to be given tty (POSIX.1, B.2, job control) */
34803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			while (/* CONSTCOND */ 1) {
3495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				pid_t ttypgrp;
3505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
3515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) {
35203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra					warningf(false, "%s: %s %s: %s",
35303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra					    "j_init", "tcgetpgrp", "failed",
354c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser					    cstrerror(errno));
3555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					ttypgrp_ok = false;
3565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					break;
3575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				}
3585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (ttypgrp == kshpgrp)
3595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					break;
3605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				kill(0, SIGTTIN);
3615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
3625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
3635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (i = NELEM(tt_sigs); --i >= 0; )
3645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
3655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    SS_RESTORE_DFL|SS_FORCE);
3665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (ttypgrp_ok && kshpgrp != kshpid) {
3675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (setpgid(0, kshpid) < 0) {
36803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				warningf(false, "%s: %s %s: %s", "j_init",
369c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				    "setpgid", "failed", cstrerror(errno));
3705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				ttypgrp_ok = false;
3715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			} else {
3725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (tcsetpgrp(tty_fd, kshpid) < 0) {
37303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra					warningf(false, "%s: %s %s: %s",
37403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra					    "j_init", "tcsetpgrp", "failed",
375c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser					    cstrerror(errno));
3765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					ttypgrp_ok = false;
3775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				} else
3785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					restore_ttypgrp = kshpgrp;
3795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				kshpgrp = kshpid;
3805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
3815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
382c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifndef MKSH_DISABLE_TTY_WARNING
3835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (use_tty && !ttypgrp_ok)
38403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			warningf(false, "%s: %s", "warning",
38503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			    "won't have full job control");
386c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
3875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else {
3885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		ttypgrp_ok = false;
3895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FTALKING))
3905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			for (i = NELEM(tt_sigs); --i >= 0; )
3915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
3925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    SS_RESTORE_IGN|SS_FORCE);
3935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		else
3945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			for (i = NELEM(tt_sigs); --i >= 0; ) {
3955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (sigtraps[tt_sigs[i]].flags &
3965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    (TF_ORIG_IGN | TF_ORIG_DFL))
3975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					setsig(&sigtraps[tt_sigs[i]],
3985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					    (sigtraps[tt_sigs[i]].flags & TF_ORIG_IGN) ?
3995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					    SIG_IGN : SIG_DFL,
4005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					    SS_RESTORE_ORIG|SS_FORCE);
4015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
4025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
403c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	tty_init_state();
4045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
4055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
4065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
40703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#if HAVE_NICE
40803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra/* run nice(3) and ignore the result */
40903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrastatic void
41003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condraksh_nice(int ness)
41103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra{
41203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#if defined(__USE_FORTIFY_LEVEL) && (__USE_FORTIFY_LEVEL > 0)
413c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	int eno;
41403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
41503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	errno = 0;
41603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* this is gonna annoy users; complain to your distro, people! */
417c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	if (nice(ness) == -1 && (eno = errno) != 0)
418c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		warningf(false, "%s: %s", "bgnice", cstrerror(eno));
41903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else
42003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	(void)nice(ness);
42103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
42203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
42303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
42403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
4255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* execute tree in child subprocess */
4265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
4275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruexchild(struct op *t, int flags,
4285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru    volatile int *xerrok,
42903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    /* used if XPCLOSE or XCCLOSE */
43003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    int close_fd)
4315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
43203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* for pipelines */
43303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	static Proc *last_proc;
4345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
43503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int rv = 0, forksleep, jwflags = JW_NONE;
43603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
4375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
43803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
43903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Proc *p;
44003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
44103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	pid_t cldpid;
44203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
44303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (flags & XPIPEST) {
44403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		flags &= ~XPIPEST;
44503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		jwflags |= JW_PIPEST;
44603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	}
4475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
4485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (flags & XEXEC)
4495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
4505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
4515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * (also done in another execute() below)
4525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
4535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (execute(t, flags & (XEXEC | XERROK), xerrok));
4545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
45503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
4565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* no SIGCHLDs while messing with job and process lists */
4575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
45803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
4595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
46003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	p = new_proc();
46103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	p->next = NULL;
46203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	p->state = PRUNNING;
46303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	p->status = 0;
46403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	p->pid = 0;
4655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
4665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* link process into jobs list */
4675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (flags & XPIPEI) {
4685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* continuing with a pipe */
4695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (!last_job)
47003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			internal_errorf("%s %d",
47103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			    "exchild: XPIPEI and no last_job - pid",
4725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    (int)procpid);
47303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j = last_job;
4745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (last_proc)
47503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			last_proc->next = p;
47603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		last_proc = p;
4775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else {
47803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* fills in j->job */
47903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j = new_job();
4805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
4815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * we don't consider XXCOMs foreground since they don't get
4825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * tty process group and we don't save or restore tty modes.
4835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
48403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->flags = (flags & XXCOM) ? JF_XXCOM :
4855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
48603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		timerclear(&j->usrtime);
48703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		timerclear(&j->systime);
48803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->state = PRUNNING;
48903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->pgrp = 0;
49003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->ppid = procpid;
49103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->age = ++njobs;
49203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->proc_list = p;
49303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j->coproc_id = 0;
49403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		last_job = j;
49503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		last_proc = p;
49603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		put_job(j, PJ_PAST_STOPPED);
4975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
4985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
49903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	vistree(p->command, sizeof(p->command), t);
5005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
5015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* create child process */
5025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	forksleep = 1;
50303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while ((cldpid = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
50403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (intrsig)
50503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			/* allow user to ^C out... */
5065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
5075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sleep(forksleep);
5085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		forksleep <<= 1;
5095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
51003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* ensure $RANDOM changes between parent and child */
511811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	rndset((unsigned long)cldpid);
51203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* fork failed? */
51303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (cldpid < 0) {
51403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		kill_job(j, SIGKILL);
51503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		remove_job(j, "fork failed");
51603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
5175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
51803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
51903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		errorf("can't fork - try again");
5205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
52103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	p->pid = cldpid ? cldpid : (procpid = getpid());
5225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
5235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
5245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* job control set up */
5255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (Flag(FMONITOR) && !(flags&XXCOM)) {
52603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		bool dotty = false;
527c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser
52803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		if (j->pgrp == 0) {
52903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			/* First process */
53003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			j->pgrp = p->pid;
53103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			dotty = true;
5325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
5335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
53403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/*
53503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		 * set pgrp in both parent and child to deal with race
5365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * condition
5375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
53803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		setpgid(p->pid, j->pgrp);
5395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (ttypgrp_ok && dotty && !(flags & XBGND))
54003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			tcsetpgrp(tty_fd, j->pgrp);
5415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
5425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
5435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
5445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* used to close pipe input fd */
54503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (close_fd >= 0 && (((flags & XPCLOSE) && cldpid) ||
54603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	    ((flags & XCCLOSE) && !cldpid)))
5475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		close(close_fd);
54803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (!cldpid) {
5495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* child */
5505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
5515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* Do this before restoring signal */
5525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (flags & XCOPROC)
5535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			coproc_cleanup(false);
5545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		cleanup_parents_env();
5555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
55603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/*
55703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		 * If FMONITOR or FTALKING is set, these signals are ignored,
5585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * if neither FMONITOR nor FTALKING are set, the signals have
5595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * their inherited values.
5605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
5615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FMONITOR) && !(flags & XXCOM)) {
5625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			for (forksleep = NELEM(tt_sigs); --forksleep >= 0; )
5635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				setsig(&sigtraps[tt_sigs[forksleep]], SIG_DFL,
5645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    SS_RESTORE_DFL|SS_FORCE);
5655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
5665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
5675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#if HAVE_NICE
5685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FBGNICE) && (flags & XBGND))
56903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			ksh_nice(4);
5705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
5715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if ((flags & XBGND)
5725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
5735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    && !Flag(FMONITOR)
5745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
5755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    ) {
5765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			setsig(&sigtraps[SIGINT], SIG_IGN,
5775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    SS_RESTORE_IGN|SS_FORCE);
5785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			setsig(&sigtraps[SIGQUIT], SIG_IGN,
5795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    SS_RESTORE_IGN|SS_FORCE);
5805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if ((!(flags & (XPIPEI | XCOPROC))) &&
5815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    ((forksleep = open("/dev/null", 0)) > 0)) {
5825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				(void)ksh_dup2(forksleep, 0, true);
5835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				close(forksleep);
5845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
5855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
58603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* in case of $(jobs) command */
58703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		remove_job(j, "child");
588811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#ifndef MKSH_NOPROSPECTOFWORK
589811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		/* remove_job needs SIGCHLD blocked still */
590811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		sigprocmask(SIG_SETMASK, &omask, NULL);
591811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser#endif
5925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		nzombie = 0;
5935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
5945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		ttypgrp_ok = false;
5955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		Flag(FMONITOR) = 0;
5965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
5975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		Flag(FTALKING) = 0;
5985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		cleartraps();
5995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* no return */
6005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		execute(t, (flags & XERROK) | XEXEC, NULL);
6015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_SMALL
6025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (t->type == TPIPE)
6035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			unwind(LLEAVE);
60403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		internal_warningf("%s: %s", "exchild", "execute() returned");
60503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		fptreef(shl_out, 8, "%s: tried to execute {\n\t%T\n}\n",
60603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		    "exchild", t);
6075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shf_flush(shl_out);
6085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
6095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		unwind(LLEAVE);
6105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* NOTREACHED */
6115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
6125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* shell (parent) stuff */
61403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (!(flags & XPIPEO)) {
61503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* last process in a job */
61603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j_startjob(j);
6175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (flags & XCOPROC) {
61803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			j->coproc_id = coproc.id;
6195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* n jobs using co-process output */
6205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			coproc.njobs++;
6215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* j using co-process input */
62203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			coproc.job = (void *)j;
6235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
6245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (flags & XBGND) {
62503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			j_set_async(j);
6265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (Flag(FTALKING)) {
62703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				shf_fprintf(shl_out, "[%d]", j->job);
62803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				for (p = j->proc_list; p; p = p->next)
6295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					shf_fprintf(shl_out, " %d",
63003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra					    (int)p->pid);
6315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_putchar('\n', shl_out);
6325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_flush(shl_out);
6335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
6345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		} else
63503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			rv = j_waitj(j, jwflags, "jw:last proc");
6365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
6375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
63803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
6395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
64003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
6415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rv);
6435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
6445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* start the last job: only used for $(command) jobs */
6465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid
6475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustartlast(void)
6485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
64903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
6505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
6515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
65303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
6545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
65503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* no need to report error - waitlast() will do it */
65603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (last_job) {
6575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* ensure it isn't removed by check_job() */
6585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		last_job->flags |= JF_WAITING;
6595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j_startjob(last_job);
6605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
66103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
6625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
66303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
6645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
6655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* wait for last job: only used for $(command) jobs */
6675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
6685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruwaitlast(void)
6695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
67003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int rv;
67103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
67203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
6735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
6745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
67603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
6775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j = last_job;
6795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!j || !(j->flags & JF_STARTED)) {
6805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (!j)
68103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			warningf(true, "%s: %s", "waitlast", "no last job");
6825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		else
68303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			internal_warningf("%s: %s", "waitlast", "not started");
68403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
6855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
68603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
68703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* not so arbitrary, non-zero value */
68803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		return (125);
6895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
6905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
69103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	rv = j_waitj(j, JW_NONE, "waitlast");
6925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
69303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
6945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
69503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
6965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
6975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rv);
6985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
6995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* wait for child, interruptable. */
7015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
7025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruwaitfor(const char *cp, int *sigp)
7035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
70403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int rv, ecode, flags = JW_INTERRUPT|JW_ASYNCNOTIFY;
70503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
70603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
7085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
71003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	*sigp = 0;
7135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (cp == NULL) {
7155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
7165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * wait for an unspecified job - always returns 0, so
7175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * don't have to worry about exited/signaled jobs
7185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
7195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j; j = j->next)
7205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* AT&T ksh will wait for stopped jobs - we don't */
7215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->ppid == procpid && j->state == PRUNNING)
7225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				break;
7235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (!j) {
72403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			sigprocmask(SIG_SETMASK, &omask, NULL);
72603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (-1);
7285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
7295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else if ((j = j_lookup(cp, &ecode))) {
7305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* don't report normal job completion */
7315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		flags &= ~JW_ASYNCNOTIFY;
7325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->ppid != procpid) {
73303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			sigprocmask(SIG_SETMASK, &omask, NULL);
73503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (-1);
7375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
7385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else {
73903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
74103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (ecode != JL_NOSUCH)
7435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
7445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (-1);
7455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
7465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* AT&T ksh will wait for stopped jobs - we don't */
7485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	rv = j_waitj(j, flags, "jw:waitfor");
7495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
75003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
75203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
75403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (rv < 0)
75503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* we were interrupted */
756fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		*sigp = ksh_sigmask(-rv);
7575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rv);
7595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
7605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* kill (built-in) a job */
7625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
7635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_kill(const char *cp, int sig)
7645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
76503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
76603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int rv = 0, ecode;
76703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
7695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
77103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
7735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if ((j = j_lookup(cp, &ecode)) == NULL) {
77403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
77603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
7775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
7785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (1);
7795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
7805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
78103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (j->pgrp == 0) {
78203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* started when !Flag(FMONITOR) */
7835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (kill_job(j, sig) < 0) {
784c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			bi_errorf("%s: %s", cp, cstrerror(errno));
7855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			rv = 1;
7865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
7875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else {
7885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
7895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP))
7905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			mksh_killpg(j->pgrp, SIGCONT);
7915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
7925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (mksh_killpg(j->pgrp, sig) < 0) {
793c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			bi_errorf("%s: %s", cp, cstrerror(errno));
7945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			rv = 1;
7955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
7965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
7975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
79803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
7995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
80003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
8015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rv);
8035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
8045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
8065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* fg and bg built-ins: called only if Flag(FMONITOR) set */
8075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
8085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_resume(const char *cp, int bg)
8095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
81003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
81103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Proc *p;
81203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int ecode, rv = 0;
81303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	bool running;
8145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
8155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
8175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if ((j = j_lookup(cp, &ecode)) == NULL) {
8195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
8205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
8215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (1);
8225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
8235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j->pgrp == 0) {
8255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
8265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		bi_errorf("job not job-controlled");
8275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (1);
8285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
8295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (bg)
8315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shprintf("[%d] ", j->job);
8325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
83303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	running = false;
8345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (p = j->proc_list; p != NULL; p = p->next) {
8355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (p->state == PSTOPPED) {
8365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			p->state = PRUNNING;
8375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			p->status = 0;
83803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			running = true;
8395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
8405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shf_puts(p->command, shl_stdout);
8415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (p->next)
8425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			shf_puts("| ", shl_stdout);
8435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
8445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	shf_putc('\n', shl_stdout);
8455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	shf_flush(shl_stdout);
8465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (running)
8475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->state = PRUNNING;
8485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	put_job(j, PJ_PAST_STOPPED);
8505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (bg)
8515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j_set_async(j);
8525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	else {
8535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* attach tty to job */
8545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->state == PRUNNING) {
8555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
856c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				mksh_tcset(tty_fd, &j->ttystat);
8575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* See comment in j_waitj regarding saved_ttypgrp. */
8585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (ttypgrp_ok &&
8595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ?
8605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    j->saved_ttypgrp : j->pgrp) < 0) {
8615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				rv = errno;
8625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (j->flags & JF_SAVEDTTY)
863c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser					mksh_tcset(tty_fd, &tty_state);
86403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				sigprocmask(SIG_SETMASK, &omask, NULL);
86503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				bi_errorf("%s %s(%d, %ld) %s: %s",
86603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    "1st", "tcsetpgrp", tty_fd,
86703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    (long)((j->flags & JF_SAVEDTTYPGRP) ?
86803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    j->saved_ttypgrp : j->pgrp), "failed",
869c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				    cstrerror(rv));
8705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				return (1);
8715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
8725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
8735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->flags |= JF_FG;
8745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->flags &= ~JF_KNOWN;
8755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j == async_job)
8765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			async_job = NULL;
8775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
8785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j->state == PRUNNING && mksh_killpg(j->pgrp, SIGCONT) < 0) {
880c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		int eno = errno;
8815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
8825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (!bg) {
8835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->flags &= ~JF_FG;
8845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
885c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				mksh_tcset(tty_fd, &tty_state);
8865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (ttypgrp_ok && tcsetpgrp(tty_fd, kshpgrp) < 0)
88703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				warningf(true, "%s %s(%d, %ld) %s: %s",
88803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    "fg: 2nd", "tcsetpgrp", tty_fd,
889c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				    (long)kshpgrp, "failed", cstrerror(errno));
8905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
8915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigprocmask(SIG_SETMASK, &omask, NULL);
892fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		bi_errorf("%s %s: %s", "can't continue job",
893c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		    cp, cstrerror(eno));
8945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (1);
8955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
8965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!bg) {
8975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (ttypgrp_ok) {
8985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP);
8995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
9005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		rv = j_waitj(j, JW_NONE, "jw:resume");
9015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
9025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
9035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rv);
9045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
9055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
9065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* are there any running or stopped jobs ? */
9085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
9095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_stopped_running(void)
9105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
91103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j;
91203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int which = 0;
9135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (j = job_list; j != NULL; j = j->next) {
9155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
9165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->ppid == procpid && j->state == PSTOPPED)
9175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			which |= 1;
9185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
9195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid &&
9205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    j->ppid == procpid && j->state == PRUNNING)
9215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			which |= 2;
9225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
9235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (which) {
9245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shellf("You have %s%s%s jobs\n",
9255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    which & 1 ? "stopped" : "",
9265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    which == 3 ? " and " : "",
9275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    which & 2 ? "running" : "");
9285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (1);
9295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
9305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (0);
9325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
9335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* list jobs for jobs built-in */
9365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruint
9375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_jobs(const char *cp, int slp,
93803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    /* 0: short, 1: long, 2: pgrp */
93903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    int nflag)
9405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
94103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j, *tmp;
94203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int how, zflag = 0;
94303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
9445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
9455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
94703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
9485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
94903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (nflag < 0) {
95003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* kludge: print zombies */
9515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		nflag = 0;
9525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		zflag = 1;
9535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
9545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (cp) {
955811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser		int ecode;
9565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if ((j = j_lookup(cp, &ecode)) == NULL) {
95803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
9595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			sigprocmask(SIG_SETMASK, &omask, NULL);
96003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
9615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
9625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (1);
9635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
9645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else
9655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j = job_list;
9665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	how = slp == 0 ? JP_MEDIUM : (slp == 1 ? JP_LONG : JP_PGRP);
9675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (; j; j = j->next) {
9685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if ((!(j->flags & JF_ZOMBIE) || zflag) &&
9695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    (!nflag || (j->flags & JF_CHANGED))) {
9705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j_print(j, how, shl_stdout);
9715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->state == PEXITED || j->state == PSIGNALLED)
9725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				j->flags |= JF_REMOVE;
9735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
9745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (cp)
9755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
9765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
9775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* Remove jobs after printing so there won't be multiple + or - jobs */
9785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (j = job_list; j; j = tmp) {
9795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		tmp = j->next;
9805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->flags & JF_REMOVE)
9815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			remove_job(j, "jobs");
9825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
98303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
9845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
98503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
9865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (0);
9875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
9885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* list jobs for top-level notification */
9905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruvoid
9915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_notify(void)
9925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
99303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j, *tmp;
99403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
9955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
9965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
9975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
99803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
9995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (j = job_list; j; j = j->next) {
10005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
10015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FMONITOR) && (j->flags & JF_CHANGED))
10025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j_print(j, JP_MEDIUM, shl_out);
10035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
100403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/*
100503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		 * Remove job after doing reports so there aren't
10065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * multiple +/- jobs.
10075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
10085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->state == PEXITED || j->state == PSIGNALLED)
10095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->flags |= JF_REMOVE;
10105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
10115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (j = job_list; j; j = tmp) {
10125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		tmp = j->next;
10135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->flags & JF_REMOVE)
10145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			remove_job(j, "notify");
10155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
10165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	shf_flush(shl_out);
101703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
10185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
101903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
10205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
10215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* Return pid of last process in last asynchronous job */
10235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querupid_t
10245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_async(void)
10255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
102603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
10275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigset_t omask;
10285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
103003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
10315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (async_job)
10335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		async_job->flags |= JF_KNOWN;
10345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
103503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
10365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	sigprocmask(SIG_SETMASK, &omask, NULL);
103703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
10385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (async_pid);
10405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
10415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
10435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Make j the last async process
10445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
10455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
10465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
10475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
10485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_set_async(Job *j)
10495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
105050012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Job *jl, *oldest;
10515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (async_job && (async_job->flags & (JF_KNOWN|JF_ZOMBIE)) == JF_ZOMBIE)
10535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		remove_job(async_job, "async");
10545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!(j->flags & JF_STARTED)) {
105503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		internal_warningf("%s: %s", "j_async", "job not started");
10565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return;
10575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
10585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	async_job = j;
10595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	async_pid = j->last_proc->pid;
10605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	while (nzombie > CHILD_MAX) {
10615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		oldest = NULL;
10625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (jl = job_list; jl; jl = jl->next)
10635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (jl != async_job && (jl->flags & JF_ZOMBIE) &&
10645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    (!oldest || jl->age < oldest->age))
10655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				oldest = jl;
10665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (!oldest) {
10675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* XXX debugging */
10685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (!(async_job->flags & JF_ZOMBIE) || nzombie != 1) {
106903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				internal_warningf("%s: bad nzombie (%d)",
107003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    "j_async", nzombie);
10715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				nzombie = 0;
10725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
10735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
10745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
10755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		remove_job(oldest, "zombie");
10765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
10775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
10785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
10805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Start a job: set STARTED, check for held signals and set j->last_proc
10815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
10825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
10835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
10845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
10855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_startjob(Job *j)
10865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
108750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Proc *p;
10885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
10895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->flags |= JF_STARTED;
10905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (p = j->proc_list; p->next; p = p->next)
10915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		;
10925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->last_proc = p;
10935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
109403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
10955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (held_sigchld) {
10965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		held_sigchld = 0;
10975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* Don't call j_sigchld() as it may remove job... */
10985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		kill(procpid, SIGCHLD);
10995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
110003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
11015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
11025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
11035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
11045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * wait for job to complete or change state
11055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
11065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
11075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
11085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int
11095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_waitj(Job *j,
111003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    /* see JW_* */
111103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra    int flags,
11125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru    const char *where)
11135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
1114b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes	Proc *p;
111503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int rv;
1116c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND
1117c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	sigset_t omask;
1118c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
11195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
11205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/*
11215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * No auto-notify on the job we are waiting on.
11225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 */
11235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->flags |= JF_WAITING;
11245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (flags & JW_ASYNCNOTIFY)
11255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->flags |= JF_W_ASYNCNOTIFY;
11265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
11275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
11285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!Flag(FMONITOR))
11295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
11305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		flags |= JW_STOPPEDWAIT;
11315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
11325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	while (j->state == PRUNNING ||
11335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    ((flags & JW_STOPPEDWAIT) && j->state == PSTOPPED)) {
113403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
1135c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND
1136c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		sigprocmask(SIG_SETMASK, &sm_default, &omask);
1137c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		pause();
1138c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		/* note that handlers may run here so they need to know */
1139c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		sigprocmask(SIG_SETMASK, &omask, NULL);
1140c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#else
11415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		sigsuspend(&sm_default);
1142c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
114303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else
114403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		j_sigchld(SIGCHLD);
114503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
11465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (fatal_trap) {
11475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			int oldf = j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY);
11485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
11495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			runtraps(TF_FATAL);
115003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			/* not reached... */
115103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			j->flags |= oldf;
11525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
11535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if ((flags & JW_INTERRUPT) && (rv = trap_pending())) {
11545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
11555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (-rv);
11565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
11575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
11585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
11595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
11605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j->flags & JF_FG) {
11615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->flags &= ~JF_FG;
11625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
11635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FMONITOR) && ttypgrp_ok && j->pgrp) {
11645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/*
11655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * Save the tty's current pgrp so it can be restored
11665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * when the job is foregrounded. This is to
11675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * deal with things like the GNU su which does
11685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * a fork/exec instead of an exec (the fork means
11695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * the execed shell gets a different pid from its
11705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * pgrp, so naturally it sets its pgrp and gets hosed
11715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * when it gets foregrounded by the parent shell which
11725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * has restored the tty's pgrp to that of the su
11735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * process).
11745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 */
11755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->state == PSTOPPED &&
11765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    (j->saved_ttypgrp = tcgetpgrp(tty_fd)) >= 0)
11775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				j->flags |= JF_SAVEDTTYPGRP;
11785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (tcsetpgrp(tty_fd, kshpgrp) < 0)
117903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				warningf(true, "%s %s(%d, %ld) %s: %s",
118003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    "j_waitj:", "tcsetpgrp", tty_fd,
1181c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				    (long)kshpgrp, "failed", cstrerror(errno));
11825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->state == PSTOPPED) {
11835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				j->flags |= JF_SAVEDTTY;
1184c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				mksh_tcget(tty_fd, &j->ttystat);
11855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
11865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
11875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
1188c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		if (tty_hasstate) {
11895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/*
11905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * Only restore tty settings if job was originally
11915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * started in the foreground. Problems can be
11925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * caused by things like 'more foobar &' which will
11935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * typically get and save the shell's vi/emacs tty
11945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * settings before setting up the tty for itself;
11955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * when more exits, it restores the 'original'
11965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * settings, and things go down hill from there...
11975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 */
11985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->state == PEXITED && j->status == 0 &&
11995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    (j->flags & JF_USETTYMODE)) {
1200c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				mksh_tcget(tty_fd, &tty_state);
12015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			} else {
1202c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser				mksh_tcset(tty_fd, &tty_state);
12035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				/*-
12045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 * Don't use tty mode if job is stopped and
12055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 * later restarted and exits. Consider
12065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 * the sequence:
12075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 *	vi foo (stopped)
12085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 *	...
12095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 *	stty something
12105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 *	...
12115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 *	fg (vi; ZZ)
12125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 * mode should be that of the stty, not what
12135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 * was before the vi started.
12145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				 */
12155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (j->state == PSTOPPED)
12165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					j->flags &= ~JF_USETTYMODE;
12175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
12185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
12195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
12205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
12215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * If it looks like user hit ^C to kill a job, pretend we got
12225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * one too to break out of for loops, etc. (AT&T ksh does this
12235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * even when not monitoring, but this doesn't make sense since
12245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * a tty generated ^C goes to the whole process group)
12255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
1226fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		if (Flag(FMONITOR) && j->state == PSIGNALLED &&
1227fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		    WIFSIGNALED(j->last_proc->status)) {
1228fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			int termsig;
1229fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes
1230fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			if ((termsig = WTERMSIG(j->last_proc->status)) > 0 &&
1231fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			    termsig < ksh_NSIG &&
1232fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			    (sigtraps[termsig].flags & TF_TTY_INTR))
1233fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes				trapsig(termsig);
12345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
12355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
12365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
12375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
12385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j_usrtime = j->usrtime;
12395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j_systime = j->systime;
12405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	rv = j->status;
12415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1242b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes	if (!(p = j->proc_list)) {
1243b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes		;	/* nothing */
1244b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes	} else if (flags & JW_PIPEST) {
124503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		uint32_t num = 0;
124603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		struct tbl *vp;
124703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
124803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		unset(vp_pipest, 1);
124903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		vp = vp_pipest;
125003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		vp->flag = DEFINED | ISSET | INTEGER | RDONLY | ARRAY | INT_U;
125103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		goto got_array;
125203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
125303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		while (p != NULL) {
125403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			{
125503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				struct tbl *vq;
125603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
125703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				/* strlen(vp_pipest->name) == 10 */
125803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				vq = alloc(offsetof(struct tbl, name[0]) + 11,
125903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				    vp_pipest->areap);
126003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				memset(vq, 0, offsetof(struct tbl, name[0]));
126103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				memcpy(vq->name, vp_pipest->name, 11);
126203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				vp->u.array = vq;
126303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				vp = vq;
126403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			}
126503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			vp->areap = vp_pipest->areap;
126603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			vp->ua.index = ++num;
126703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			vp->flag = DEFINED | ISSET | INTEGER | RDONLY |
126803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			    ARRAY | INT_U | AINDEX;
126903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra got_array:
127003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			vp->val.i = proc_errorlevel(p);
1271811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser			if (Flag(FPIPEFAIL) && vp->val.i)
1272811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser				rv = vp->val.i;
127303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			p = p->next;
127403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		}
1275b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes	} else if (Flag(FPIPEFAIL)) {
1276b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes		do {
127796b43632c2aa206ac1ec0eb70b34847d58d52633Elliott Hughes			const int i = proc_errorlevel(p);
1278737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes
1279b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes			if (i)
1280737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes				rv = i;
1281b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes		} while ((p = p->next));
128203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	}
128303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
12845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!(flags & JW_ASYNCNOTIFY)
12855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
12865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    && (!Flag(FMONITOR) || j->state != PSTOPPED)
12875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
12885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    ) {
12895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j_print(j, JP_SHORT, shl_out);
12905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shf_flush(shl_out);
12915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
12925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j->state != PSTOPPED
12935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
12945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    && (!Flag(FMONITOR) || !(flags & JW_ASYNCNOTIFY))
12955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
12965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    )
12975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		remove_job(j, where);
12985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
12995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rv);
13005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
13015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
13025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
13035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * SIGCHLD handler to reap children and update job states
13045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
13055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
13065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
13075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/* ARGSUSED */
13085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
13095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_sigchld(int sig MKSH_A_UNUSED)
13105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
1311811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	int saved_errno = errno;
13125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Job *j;
13135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	Proc *p = NULL;
13145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	pid_t pid;
13155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	int status;
13165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	struct rusage ru0, ru1;
1317c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND
1318c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	sigset_t omask;
1319c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser
1320c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	/* this handler can run while SIGCHLD is not blocked, so block it now */
1321c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
1322c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
13235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
132403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
13255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/*
13265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * Don't wait for any processes if a job is partially started.
13275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * This is so we don't do away with the process group leader
13285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * before all the processes in a pipe line are started (so the
13295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * setpgid() won't fail)
13305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 */
13315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (j = job_list; j; j = j->next)
13325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->ppid == procpid && !(j->flags & JF_STARTED)) {
13335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			held_sigchld = 1;
1334c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			goto j_sigchld_out;
13355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
133603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
13375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
13385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	getrusage(RUSAGE_CHILDREN, &ru0);
13395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	do {
134003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
1341737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		pid = waitpid(-1, &status, (WNOHANG |
134256b517d46cdf4f6ccd6b62b207110e2afc3db30bElliott Hughes#if defined(WCONTINUED) && defined(WIFCONTINUED)
1343737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		    WCONTINUED |
1344737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#endif
1345737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		    WUNTRACED));
134603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else
134703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		pid = wait(&status);
134803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
13495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
13505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
13515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * return if this would block (0) or no children
13525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * or interrupted (-1)
13535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
13545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (pid <= 0)
1355c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			goto j_sigchld_out;
13565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
13575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		getrusage(RUSAGE_CHILDREN, &ru1);
13585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
13595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* find job and process structures for this pid */
13605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j != NULL; j = j->next)
13615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			for (p = j->proc_list; p != NULL; p = p->next)
13625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (p->pid == pid)
13635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					goto found;
13645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru found:
13655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j == NULL) {
13665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* Can occur if process has kids, then execs shell
13675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			warningf(true, "bad process waited for (pid = %d)",
13685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				pid);
13695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 */
13705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			ru0 = ru1;
13715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			continue;
13725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
13735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
13745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime);
13755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime);
13765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		timeradd(&j->systime, &ru1.ru_stime, &j->systime);
13775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		timersub(&j->systime, &ru0.ru_stime, &j->systime);
13785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		ru0 = ru1;
13795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		p->status = status;
13805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
13815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (WIFSTOPPED(status))
13825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			p->state = PSTOPPED;
13835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		else
138456b517d46cdf4f6ccd6b62b207110e2afc3db30bElliott Hughes#if defined(WCONTINUED) && defined(WIFCONTINUED)
1385737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		  if (WIFCONTINUED(status)) {
1386737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			p->state = j->state = PRUNNING;
1387737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			/* skip check_job(), no-op in this case */
1388737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes			continue;
1389737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes		} else
1390737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#endif
13915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
13925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		  if (WIFSIGNALED(status))
13935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			p->state = PSIGNALLED;
13945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		else
13955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			p->state = PEXITED;
13965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
139703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* check to see if entire job is done */
139803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		check_job(j);
139903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	}
140003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#ifndef MKSH_NOPROSPECTOFWORK
140103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	    while (/* CONSTCOND */ 1);
140203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#else
140303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	    while (/* CONSTCOND */ 0);
140403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#endif
1405c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser
1406c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser j_sigchld_out:
1407c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifdef MKSH_NO_SIGSUSPEND
1408c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	sigprocmask(SIG_SETMASK, &omask, NULL);
1409c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
1410811a575c0f6a5ef00a921d14c1830ef5ae1bd796Thorsten Glaser	errno = saved_errno;
14115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
14125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
14135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
14145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Called only when a process in j has exited/stopped (ie, called only
14155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * from j_sigchld()). If no processes are running, the job status
14165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * and state are updated, asynchronous job notification is done and,
14175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * if unneeded, the job is removed.
14185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
14195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
14205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
14215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
14225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querucheck_job(Job *j)
14235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
142450012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	int jstate;
142550012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Proc *p;
14265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
14275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* XXX debugging (nasty - interrupt routine using shl_out) */
14285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (!(j->flags & JF_STARTED)) {
14295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		internal_warningf("check_job: job started (flags 0x%x)",
14305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    j->flags);
14315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return;
14325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
14335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
14345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	jstate = PRUNNING;
14355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (p=j->proc_list; p != NULL; p = p->next) {
14365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (p->state == PRUNNING)
143703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			/* some processes still running */
143803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			return;
14395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (p->state > jstate)
14405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			jstate = p->state;
14415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
14425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->state = jstate;
144303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	j->status = proc_errorlevel(j->last_proc);
14445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
14455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/*
14465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * Note when co-process dies: can't be done in j_wait() nor
14475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * remove_job() since neither may be called for non-interactive
14485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 * shells.
14495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	 */
14505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j->state == PEXITED || j->state == PSIGNALLED) {
14515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
14525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * No need to keep co-process input any more
14535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * (at least, this is what ksh93d thinks)
14545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
14555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (coproc.job == j) {
14565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			coproc.job = NULL;
14575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/*
14585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * XXX would be nice to get the closes out of here
14595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * so they aren't done in the signal handler.
14605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * Would mean a check in coproc_getfd() to
14615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * do "if job == 0 && write >= 0, close write".
14625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 */
14635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			coproc_write_close(coproc.write);
14645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
14655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* Do we need to keep the output? */
14665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->coproc_id && j->coproc_id == coproc.id &&
14675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    --coproc.njobs == 0)
14685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			coproc_readw_close(coproc.read);
14695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
14705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
14715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->flags |= JF_CHANGED;
14725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
14735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (Flag(FMONITOR) && !(j->flags & JF_XXCOM)) {
14745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
14755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * Only put stopped jobs at the front to avoid confusing
14765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * the user (don't want finished jobs effecting %+ or %-)
14775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
14785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j->state == PSTOPPED)
14795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			put_job(j, PJ_ON_FRONT);
14805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (Flag(FNOTIFY) &&
14815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    (j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY)) != JF_WAITING) {
14825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/* Look for the real file descriptor 2 */
14835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			{
14845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				struct env *ep;
14855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				int fd = 2;
14865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
14875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				for (ep = e; ep; ep = ep->oenv)
14885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					if (ep->savefd && ep->savefd[2])
14895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru						fd = ep->savefd[2];
14905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_reopen(fd, SHF_WR, shl_j);
14915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
14925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/*
14935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * Can't call j_notify() as it removes jobs. The job
14945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * must stay in the job list as j_waitj() may be
14955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * running with this job.
14965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 */
14975155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j_print(j, JP_MEDIUM, shl_j);
14985155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			shf_flush(shl_j);
14995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (!(j->flags & JF_WAITING) && j->state != PSTOPPED)
15005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				remove_job(j, "notify");
15015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
15025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
15035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
15045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (
15055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifndef MKSH_UNEMPLOYED
15065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    !Flag(FMONITOR) &&
15075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
15085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    !(j->flags & (JF_WAITING|JF_FG)) &&
15095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	    j->state != PSTOPPED) {
15105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j == async_job || (j->flags & JF_KNOWN)) {
15115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->flags |= JF_ZOMBIE;
15125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			j->job = -1;
15135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			nzombie++;
15145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		} else
15155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			remove_job(j, "checkjob");
15165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
15175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
15185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
15195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
15205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Print job status in either short, medium or long format.
15215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
15225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
15235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
15245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
15255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_print(Job *j, int how, struct shf *shf)
15265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
152750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Proc *p;
152850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	int state;
152950012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	int status;
1530fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes	bool coredumped;
153150012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	char jobchar = ' ';
153250012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	char buf[64];
15335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	const char *filler;
153450012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	int output = 0;
15355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
15365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (how == JP_PGRP) {
15375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
15385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * POSIX doesn't say what to do it there is no process
15395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * group leader (ie, !FMONITOR). We arbitrarily return
15405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * last pid (which is what $! returns).
15415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
15425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shf_fprintf(shf, "%d\n", (int)(j->pgrp ? j->pgrp :
15435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    (j->last_proc ? j->last_proc->pid : 0)));
15445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return;
15455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
15465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->flags &= ~JF_CHANGED;
15475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	filler = j->job > 10 ? "\n       " : "\n      ";
15485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j == job_list)
15495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		jobchar = '+';
15505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	else if (j == job_list->next)
15515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		jobchar = '-';
15525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
15535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (p = j->proc_list; p != NULL;) {
1554fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		coredumped = false;
15555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		switch (p->state) {
15565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		case PRUNNING:
15575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			memcpy(buf, "Running", 8);
15585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
1559fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		case PSTOPPED: {
1560fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			int stopsig = WSTOPSIG(p->status);
1561fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes
1562fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			strlcpy(buf, stopsig > 0 && stopsig < ksh_NSIG ?
1563fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			    sigtraps[stopsig].mess : "Stopped", sizeof(buf));
15645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
1565fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		}
1566fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		case PEXITED: {
1567fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			int exitstatus = (WEXITSTATUS(p->status)) & 255;
1568fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes
15695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (how == JP_SHORT)
15705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				buf[0] = '\0';
1571fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			else if (exitstatus == 0)
15725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				memcpy(buf, "Done", 5);
15735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			else
15745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_snprintf(buf, sizeof(buf), "Done (%d)",
1575fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes				    exitstatus);
15765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
1577fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		}
1578fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		case PSIGNALLED: {
1579fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			int termsig = WTERMSIG(p->status);
15805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#ifdef WCOREDUMP
15815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (WCOREDUMP(p->status))
1582fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes				coredumped = true;
15835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru#endif
15845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			/*
15855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * kludge for not reporting 'normal termination
15865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 * signals' (i.e. SIGINT, SIGPIPE)
15875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			 */
15885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (how == JP_SHORT && !coredumped &&
1589fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes			    (termsig == SIGINT || termsig == SIGPIPE)) {
15905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				buf[0] = '\0';
15915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			} else
1592fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes				strlcpy(buf, termsig > 0 && termsig < ksh_NSIG ?
1593fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes				    sigtraps[termsig].mess : "Signalled",
15945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    sizeof(buf));
15955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
1596fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes		}
1597c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		default:
1598c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser			buf[0] = '\0';
15995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
16005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (how != JP_SHORT) {
16025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (p == j->proc_list)
16035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_fprintf(shf, "[%d] %c ", j->job, jobchar);
16045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			else
160503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra				shf_puts(filler, shf);
16065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
16075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (how == JP_LONG)
16095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			shf_fprintf(shf, "%5d ", (int)p->pid);
16105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (how == JP_SHORT) {
16125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (buf[0]) {
16135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				output = 1;
16145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_fprintf(shf, "%s%s ",
16155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    buf, coredumped ? " (core dumped)" : null);
16165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
16175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		} else {
16185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			output = 1;
16195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			shf_fprintf(shf, "%-20s %s%s%s", buf, p->command,
16205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    p->next ? "|" : null,
16215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			    coredumped ? " (core dumped)" : null);
16225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
16235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		state = p->state;
16255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		status = p->status;
16265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		p = p->next;
16275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		while (p && p->state == state && p->status == status) {
16285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (how == JP_LONG)
16295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_fprintf(shf, "%s%5d %-20s %s%s", filler,
16305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    (int)p->pid, " ", p->command,
16315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    p->next ? "|" : null);
16325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			else if (how == JP_MEDIUM)
16335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				shf_fprintf(shf, " %s%s", p->command,
16345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				    p->next ? "|" : null);
16355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			p = p->next;
16365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		}
16375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
16385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (output)
16395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		shf_putc('\n', shf);
16405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
16415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
16435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Convert % sequence to job
16445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
16455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
16465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
16475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *
16485155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruj_lookup(const char *cp, int *ecodep)
16495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
165003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Job *j, *last_match;
165103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	Proc *p;
165203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	size_t len;
165303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	int job = 0;
16545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
1655b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes	if (ksh_isdigit(*cp) && getn(cp, &job)) {
16565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/* Look for last_proc->pid (what $! returns) first... */
16575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j != NULL; j = j->next)
16585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->last_proc && j->last_proc->pid == job)
16595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				return (j);
16605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		/*
16615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * ...then look for process group (this is non-POSIX,
16625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 * but should not break anything
16635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		 */
16645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j != NULL; j = j->next)
16655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->pgrp && j->pgrp == job)
16665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				return (j);
1667b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes		goto j_lookup_nosuch;
16685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
16695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (*cp != '%') {
1670b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes j_lookup_invalid:
16715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (ecodep)
16725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			*ecodep = JL_INVALID;
16735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return (NULL);
16745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
16755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	switch (*++cp) {
16765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case '\0': /* non-standard */
16775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case '+':
16785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case '%':
16795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (job_list != NULL)
16805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (job_list);
16815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
16825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case '-':
16845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (job_list != NULL && job_list->next)
16855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (job_list->next);
16865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
16875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
16885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case '0': case '1': case '2': case '3': case '4':
16895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case '5': case '6': case '7': case '8': case '9':
1690b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes		if (!getn(cp, &job))
1691b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes			goto j_lookup_invalid;
16925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j != NULL; j = j->next)
16935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (j->job == job)
16945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				return (j);
16955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
16965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
169703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* %?string */
169803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	case '?':
16995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		last_match = NULL;
17005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j != NULL; j = j->next)
17015155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			for (p = j->proc_list; p != NULL; p = p->next)
17025155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (strstr(p->command, cp+1) != NULL) {
17035155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					if (last_match) {
17045155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru						if (ecodep)
17055155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru							*ecodep = JL_AMBIG;
17065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru						return (NULL);
17075155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					}
17085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					last_match = j;
17095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				}
17105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (last_match)
17115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (last_match);
17125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
17135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
171403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* %string */
171503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	default:
17165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		len = strlen(cp);
17175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		last_match = NULL;
17185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j != NULL; j = j->next)
17195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (strncmp(cp, j->proc_list->command, len) == 0) {
17205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				if (last_match) {
17215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					if (ecodep)
17225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru						*ecodep = JL_AMBIG;
17235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru					return (NULL);
17245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				}
17255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				last_match = j;
17265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			}
17275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (last_match)
17285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			return (last_match);
17295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
17305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
1731b27ce95e41e941ad22b3dc392d8328251d3a057eElliott Hughes j_lookup_nosuch:
17325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (ecodep)
17335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		*ecodep = JL_NOSUCH;
17345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (NULL);
17355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
17365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job	*free_jobs;
17385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Proc	*free_procs;
17395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17405155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
17415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * allocate a new job and fill in the job number.
17425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
17435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
17445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
17455155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Job *
17465155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querunew_job(void)
17475155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
174850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	int i;
174950012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Job *newj, *j;
17505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (free_jobs != NULL) {
17525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		newj = free_jobs;
17535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		free_jobs = free_jobs->next;
17545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else
17555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		newj = alloc(sizeof(Job), APERM);
17565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* brute force method */
17585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (i = 1; ; i++) {
17595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (j = job_list; j && j->job != i; j = j->next)
17605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			;
17615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (j == NULL)
17625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			break;
17635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
17645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	newj->job = i;
17655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (newj);
17675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
17685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
17705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Allocate new process struct
17715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
17725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
17735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
17745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic Proc *
17755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querunew_proc(void)
17765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
177750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Proc *p;
17785155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17795155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (free_procs != NULL) {
17805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		p = free_procs;
17815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		free_procs = free_procs->next;
17825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	} else
17835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		p = alloc(sizeof(Proc), APERM);
17845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (p);
17865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
17875155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
17885155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
17895155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Take job out of job_list and put old structures into free list.
17905155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * Keeps nzombies, last_job and async_job up to date.
17915155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
17925155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
17935155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
17945155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
17955155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruremove_job(Job *j, const char *where)
17965155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
179750012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Proc *p, *tmp;
179850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Job **prev, *curr;
17995155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18005155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	prev = &job_list;
1801c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	curr = job_list;
1802c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	while (curr && curr != j) {
1803c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		prev = &curr->next;
1804c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		curr = *prev;
1805c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	}
18065155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (curr != j) {
180703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		internal_warningf("remove_job: job %s (%s)", "not found", where);
18085155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		return;
18095155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
18105155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	*prev = curr->next;
18115155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18125155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* free up proc structures */
18135155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (p = j->proc_list; p != NULL; ) {
18145155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		tmp = p;
18155155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		p = p->next;
18165155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		tmp->next = free_procs;
18175155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		free_procs = tmp;
18185155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
18195155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18205155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if ((j->flags & JF_ZOMBIE) && j->ppid == procpid)
18215155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		--nzombie;
18225155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	j->next = free_jobs;
18235155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	free_jobs = j;
18245155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18255155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j == last_job)
18265155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		last_job = NULL;
18275155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (j == async_job)
18285155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		async_job = NULL;
18295155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
18305155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18315155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
18325155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * put j in a particular location (taking it out job_list if it is there
18335155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * already)
18345155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
18355155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
18365155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
18375155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic void
18385155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queruput_job(Job *j, int where)
18395155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
184050012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Job **prev, *curr;
18415155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18425155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	/* Remove job from list (if there) */
18435155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	prev = &job_list;
18445155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	curr = job_list;
1845c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	while (curr && curr != j) {
1846c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		prev = &curr->next;
1847c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		curr = *prev;
1848c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	}
18495155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	if (curr == j)
18505155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		*prev = curr->next;
18515155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18525155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	switch (where) {
18535155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case PJ_ON_FRONT:
18545155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->next = job_list;
18555155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		job_list = j;
18565155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
18575155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18585155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	case PJ_PAST_STOPPED:
18595155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		prev = &job_list;
18605155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		curr = job_list;
18615155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		for (; curr && curr->state == PSTOPPED; prev = &curr->next,
18625155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		    curr = *prev)
18635155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			;
18645155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		j->next = curr;
18655155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		*prev = j;
18665155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		break;
18675155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	}
18685155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
18695155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18705155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru/*
18715155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * nuke a job (called when unable to start full job).
18725155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru *
18735155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru * If jobs are compiled in then this routine expects sigchld to be blocked.
18745155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru */
18755155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querustatic int
18765155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Querukill_job(Job *j, int sig)
18775155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru{
187850012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	Proc *p;
187950012061ca3ad8e8a7f88c72130a5e22d797897eElliott Hughes	int rval = 0;
18805155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru
18815155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	for (p = j->proc_list; p != NULL; p = p->next)
18825155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru		if (p->pid != 0)
18835155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru			if (kill(p->pid, sig) < 0)
18845155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru				rval = -1;
18855155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru	return (rval);
18865155f1c7438ef540d7b25eb70aa1639579795b07Jean-Baptiste Queru}
1887c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser
1888c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void
1889c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glasertty_init_talking(void)
1890c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser{
1891c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	switch (tty_init_fd()) {
1892c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	case 0:
1893c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		break;
1894c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	case 1:
1895c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifndef MKSH_DISABLE_TTY_WARNING
1896c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		warningf(false, "%s: %s %s: %s",
1897c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		    "No controlling tty", "open", "/dev/tty",
1898c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		    cstrerror(errno));
1899c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
1900c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		break;
1901c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	case 2:
1902c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#ifndef MKSH_DISABLE_TTY_WARNING
1903c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		warningf(false, "%s: %s", "can't find tty fd", cstrerror(errno));
1904c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser#endif
1905c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		break;
1906c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	case 3:
1907c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		warningf(false, "%s: %s %s: %s", "j_ttyinit",
1908c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		    "dup of tty fd", "failed", cstrerror(errno));
1909c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		break;
1910c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	case 4:
1911c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		warningf(false, "%s: %s: %s", "j_ttyinit",
1912c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		    "can't set close-on-exec flag", cstrerror(errno));
1913c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		break;
1914c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	}
1915c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser}
1916c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser
1917c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaserstatic void
1918c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glasertty_init_state(void)
1919c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser{
1920c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	if (tty_fd >= 0) {
1921c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		mksh_tcget(tty_fd, &tty_state);
1922c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser		tty_hasstate = true;
1923c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser	}
1924c2dc5def5e2273bb1d78b4ba032a3903dd0f980cThorsten Glaser}
1925