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