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