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