jobs.c revision 1.57 1 1.57 christos /* $NetBSD: jobs.c,v 1.57 2003/01/27 12:54:08 christos Exp $ */
2 1.15 cgd
3 1.1 cgd /*-
4 1.8 jtc * Copyright (c) 1991, 1993
5 1.8 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.22 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.15 cgd #if 0
42 1.16 christos static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
43 1.15 cgd #else
44 1.57 christos __RCSID("$NetBSD: jobs.c,v 1.57 2003/01/27 12:54:08 christos Exp $");
45 1.15 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.16 christos #include <fcntl.h>
49 1.16 christos #include <signal.h>
50 1.16 christos #include <errno.h>
51 1.16 christos #include <unistd.h>
52 1.16 christos #include <stdlib.h>
53 1.26 fair #include <paths.h>
54 1.16 christos #include <sys/types.h>
55 1.16 christos #include <sys/param.h>
56 1.16 christos #ifdef BSD
57 1.16 christos #include <sys/wait.h>
58 1.16 christos #include <sys/time.h>
59 1.16 christos #include <sys/resource.h>
60 1.16 christos #endif
61 1.19 christos #include <sys/ioctl.h>
62 1.16 christos
63 1.1 cgd #include "shell.h"
64 1.1 cgd #if JOBS
65 1.19 christos #if OLD_TTY_DRIVER
66 1.1 cgd #include "sgtty.h"
67 1.19 christos #else
68 1.19 christos #include <termios.h>
69 1.19 christos #endif
70 1.1 cgd #undef CEOF /* syntax.h redefines this */
71 1.1 cgd #endif
72 1.16 christos #include "redir.h"
73 1.16 christos #include "show.h"
74 1.1 cgd #include "main.h"
75 1.1 cgd #include "parser.h"
76 1.1 cgd #include "nodes.h"
77 1.1 cgd #include "jobs.h"
78 1.1 cgd #include "options.h"
79 1.1 cgd #include "trap.h"
80 1.1 cgd #include "syntax.h"
81 1.1 cgd #include "input.h"
82 1.1 cgd #include "output.h"
83 1.1 cgd #include "memalloc.h"
84 1.1 cgd #include "error.h"
85 1.1 cgd #include "mystring.h"
86 1.1 cgd
87 1.1 cgd
88 1.55 christos static struct job *jobtab; /* array of jobs */
89 1.55 christos static int njobs; /* size of array */
90 1.55 christos static int jobs_invalid; /* set in child */
91 1.55 christos MKINIT pid_t backgndpid = -1; /* pid of last background process */
92 1.1 cgd #if JOBS
93 1.1 cgd int initialpgrp; /* pgrp of shell on invocation */
94 1.55 christos static int curjob = -1; /* current job */
95 1.1 cgd #endif
96 1.44 christos static int ttyfd = -1;
97 1.1 cgd
98 1.55 christos STATIC void restartjob(struct job *);
99 1.55 christos STATIC void freejob(struct job *);
100 1.55 christos STATIC struct job *getjob(const char *, int);
101 1.55 christos STATIC int dowait(int, struct job *);
102 1.55 christos STATIC int onsigchild(void);
103 1.55 christos STATIC int waitproc(int, struct job *, int *);
104 1.55 christos STATIC void cmdtxt(union node *);
105 1.55 christos STATIC void cmdlist(union node *, int);
106 1.55 christos STATIC void cmdputs(const char *);
107 1.1 cgd
108 1.44 christos #ifdef OLD_TTY_DRIVER
109 1.55 christos static pid_t tcgetpgrp(int fd);
110 1.55 christos static int tcsetpgrp(int fd, pid_t pgrp);
111 1.44 christos
112 1.44 christos static pid_t
113 1.55 christos tcgetpgrp(int fd)
114 1.44 christos {
115 1.44 christos pid_t pgrp;
116 1.44 christos if (ioctl(fd, TIOCGPGRP, (char *)&pgrp) == -1)
117 1.44 christos return -1;
118 1.44 christos else
119 1.44 christos return pgrp;
120 1.44 christos }
121 1.44 christos
122 1.44 christos static int
123 1.55 christos tcsetpgrp(int fd, pid_tpgrp)
124 1.44 christos {
125 1.44 christos return ioctl(fd, TIOCSPGRP, (char *)&pgrp);
126 1.44 christos }
127 1.44 christos #endif
128 1.8 jtc
129 1.1 cgd /*
130 1.1 cgd * Turn job control on and off.
131 1.1 cgd *
132 1.1 cgd * Note: This code assumes that the third arg to ioctl is a character
133 1.1 cgd * pointer, which is true on Berkeley systems but not System V. Since
134 1.1 cgd * System V doesn't have job control yet, this isn't a problem now.
135 1.1 cgd */
136 1.1 cgd
137 1.1 cgd MKINIT int jobctl;
138 1.1 cgd
139 1.1 cgd void
140 1.55 christos setjobctl(int on)
141 1.13 cgd {
142 1.8 jtc #ifdef OLD_TTY_DRIVER
143 1.1 cgd int ldisc;
144 1.8 jtc #endif
145 1.1 cgd
146 1.1 cgd if (on == jobctl || rootshell == 0)
147 1.1 cgd return;
148 1.1 cgd if (on) {
149 1.44 christos #if defined(FIOCLEX) || defined(FD_CLOEXEC)
150 1.44 christos int err;
151 1.57 christos int i;
152 1.44 christos if (ttyfd != -1)
153 1.44 christos close(ttyfd);
154 1.45 christos if ((ttyfd = open("/dev/tty", O_RDWR)) == -1) {
155 1.45 christos for (i = 0; i < 3; i++) {
156 1.45 christos if (isatty(i) && (ttyfd = dup(i)) != -1)
157 1.45 christos break;
158 1.45 christos }
159 1.45 christos if (i == 3)
160 1.45 christos goto out;
161 1.45 christos }
162 1.57 christos /* Move to a high fd */
163 1.57 christos for (i = 10; i > 2; i--) {
164 1.57 christos if ((err = fcntl(ttyfd, F_DUPFD, (1 << i) - 1)) != -1)
165 1.57 christos break;
166 1.57 christos }
167 1.57 christos if (err != -1) {
168 1.57 christos close(ttyfd);
169 1.57 christos ttyfd = err;
170 1.57 christos }
171 1.44 christos #ifdef FIOCLEX
172 1.44 christos err = ioctl(ttyfd, FIOCLEX, 0);
173 1.44 christos #elif FD_CLOEXEC
174 1.57 christos err = fcntl(ttyfd, FD_SETFD,
175 1.57 christos fcntl(ttyfd, F_GETFD, 0) | FD_CLOEXEC);
176 1.44 christos #endif
177 1.44 christos if (err == -1) {
178 1.44 christos close(ttyfd);
179 1.44 christos ttyfd = -1;
180 1.44 christos goto out;
181 1.44 christos }
182 1.18 mycroft #else
183 1.44 christos out2str("sh: Need FIOCLEX or FD_CLOEXEC to support job control");
184 1.44 christos goto out;
185 1.18 mycroft #endif
186 1.44 christos do { /* while we are in the background */
187 1.44 christos if ((initialpgrp = tcgetpgrp(ttyfd)) < 0) {
188 1.44 christos out:
189 1.8 jtc out2str("sh: can't access tty; job control turned off\n");
190 1.8 jtc mflag = 0;
191 1.1 cgd return;
192 1.1 cgd }
193 1.1 cgd if (initialpgrp == -1)
194 1.10 jtc initialpgrp = getpgrp();
195 1.10 jtc else if (initialpgrp != getpgrp()) {
196 1.44 christos killpg(0, SIGTTIN);
197 1.1 cgd continue;
198 1.1 cgd }
199 1.1 cgd } while (0);
200 1.44 christos
201 1.8 jtc #ifdef OLD_TTY_DRIVER
202 1.44 christos if (ioctl(ttyfd, TIOCGETD, (char *)&ldisc) < 0
203 1.44 christos || ldisc != NTTYDISC) {
204 1.8 jtc out2str("sh: need new tty driver to run job control; job control turned off\n");
205 1.8 jtc mflag = 0;
206 1.1 cgd return;
207 1.1 cgd }
208 1.8 jtc #endif
209 1.47 christos setsignal(SIGTSTP, 0);
210 1.47 christos setsignal(SIGTTOU, 0);
211 1.47 christos setsignal(SIGTTIN, 0);
212 1.47 christos if (getpgid(0) != rootpid && setpgid(0, rootpid) == -1)
213 1.47 christos error("Cannot set process group (%s) at %d",
214 1.47 christos strerror(errno), __LINE__);
215 1.47 christos if (tcsetpgrp(ttyfd, rootpid) == -1)
216 1.47 christos error("Cannot set tty process group (%s) at %d",
217 1.47 christos strerror(errno), __LINE__);
218 1.1 cgd } else { /* turning job control off */
219 1.47 christos if (getpgid(0) != initialpgrp && setpgid(0, initialpgrp) == -1)
220 1.47 christos error("Cannot set process group (%s) at %d",
221 1.47 christos strerror(errno), __LINE__);
222 1.47 christos if (tcsetpgrp(ttyfd, initialpgrp) == -1)
223 1.47 christos error("Cannot set tty process group (%s) at %d",
224 1.47 christos strerror(errno), __LINE__);
225 1.44 christos close(ttyfd);
226 1.44 christos ttyfd = -1;
227 1.47 christos setsignal(SIGTSTP, 0);
228 1.47 christos setsignal(SIGTTOU, 0);
229 1.47 christos setsignal(SIGTTIN, 0);
230 1.1 cgd }
231 1.1 cgd jobctl = on;
232 1.1 cgd }
233 1.1 cgd
234 1.1 cgd
235 1.1 cgd #ifdef mkinit
236 1.16 christos INCLUDE <stdlib.h>
237 1.1 cgd
238 1.1 cgd SHELLPROC {
239 1.1 cgd backgndpid = -1;
240 1.1 cgd #if JOBS
241 1.1 cgd jobctl = 0;
242 1.1 cgd #endif
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd #endif
246 1.1 cgd
247 1.1 cgd
248 1.1 cgd
249 1.1 cgd #if JOBS
250 1.13 cgd int
251 1.55 christos fgcmd(int argc, char **argv)
252 1.13 cgd {
253 1.1 cgd struct job *jp;
254 1.47 christos int i;
255 1.1 cgd int status;
256 1.1 cgd
257 1.55 christos nextopt("");
258 1.55 christos jp = getjob(*argptr, 0);
259 1.1 cgd if (jp->jobctl == 0)
260 1.1 cgd error("job not created under job control");
261 1.55 christos out1fmt("%s", jp->ps[0].cmd);
262 1.55 christos for (i = 1; i < jp->nprocs; i++)
263 1.55 christos out1fmt(" | %s", jp->ps[i].cmd );
264 1.55 christos out1c('\n');
265 1.55 christos flushall();
266 1.47 christos
267 1.48 christos for (i = 0; i < jp->nprocs; i++)
268 1.47 christos if (tcsetpgrp(ttyfd, jp->ps[i].pid) != -1)
269 1.47 christos break;
270 1.47 christos
271 1.48 christos if (i >= jp->nprocs) {
272 1.47 christos error("Cannot set tty process group (%s) at %d",
273 1.47 christos strerror(errno), __LINE__);
274 1.47 christos }
275 1.1 cgd restartjob(jp);
276 1.1 cgd INTOFF;
277 1.1 cgd status = waitforjob(jp);
278 1.1 cgd INTON;
279 1.1 cgd return status;
280 1.1 cgd }
281 1.1 cgd
282 1.55 christos static void
283 1.55 christos set_curjob(struct job *jp, int mode)
284 1.55 christos {
285 1.55 christos struct job *jp1, *jp2;
286 1.55 christos int i, ji;
287 1.55 christos
288 1.55 christos ji = jp - jobtab;
289 1.55 christos
290 1.55 christos /* first remove from list */
291 1.55 christos if (ji == curjob)
292 1.55 christos curjob = jp->prev_job;
293 1.55 christos else {
294 1.55 christos for (i = 0; i < njobs; i++) {
295 1.55 christos if (jobtab[i].prev_job != ji)
296 1.55 christos continue;
297 1.55 christos jobtab[i].prev_job = jp->prev_job;
298 1.55 christos break;
299 1.55 christos }
300 1.55 christos }
301 1.55 christos
302 1.55 christos /* Then re-insert in correct position */
303 1.55 christos switch (mode) {
304 1.55 christos case 0: /* job being deleted */
305 1.55 christos jp->prev_job = -1;
306 1.55 christos break;
307 1.55 christos case 1: /* newly created job or backgrounded job,
308 1.55 christos put after all stopped jobs. */
309 1.55 christos if (curjob != -1 && jobtab[curjob].state == JOBSTOPPED) {
310 1.55 christos for (jp1 = jobtab + curjob; ; jp1 = jp2) {
311 1.55 christos if (jp1->prev_job == -1)
312 1.55 christos break;
313 1.55 christos jp2 = jobtab + jp1->prev_job;
314 1.55 christos if (jp2->state != JOBSTOPPED)
315 1.55 christos break;
316 1.55 christos }
317 1.55 christos jp->prev_job = jp1->prev_job;
318 1.55 christos jp1->prev_job = ji;
319 1.55 christos break;
320 1.55 christos }
321 1.55 christos /* FALLTHROUGH */
322 1.55 christos case 2: /* newly stopped job - becomes curjob */
323 1.55 christos jp->prev_job = curjob;
324 1.55 christos curjob = ji;
325 1.55 christos break;
326 1.55 christos }
327 1.55 christos }
328 1.1 cgd
329 1.13 cgd int
330 1.55 christos bgcmd(int argc, char **argv)
331 1.13 cgd {
332 1.1 cgd struct job *jp;
333 1.55 christos int i;
334 1.1 cgd
335 1.55 christos nextopt("");
336 1.1 cgd do {
337 1.55 christos jp = getjob(*argptr, 0);
338 1.1 cgd if (jp->jobctl == 0)
339 1.1 cgd error("job not created under job control");
340 1.55 christos set_curjob(jp, 1);
341 1.56 agc out1fmt("[%ld] %s", (long)(jp - jobtab + 1), jp->ps[0].cmd);
342 1.55 christos for (i = 1; i < jp->nprocs; i++)
343 1.55 christos out1fmt(" | %s", jp->ps[i].cmd );
344 1.55 christos out1c('\n');
345 1.55 christos flushall();
346 1.1 cgd restartjob(jp);
347 1.55 christos } while (*argptr && *++argptr);
348 1.1 cgd return 0;
349 1.1 cgd }
350 1.1 cgd
351 1.1 cgd
352 1.1 cgd STATIC void
353 1.55 christos restartjob(struct job *jp)
354 1.13 cgd {
355 1.1 cgd struct procstat *ps;
356 1.1 cgd int i;
357 1.1 cgd
358 1.1 cgd if (jp->state == JOBDONE)
359 1.1 cgd return;
360 1.1 cgd INTOFF;
361 1.48 christos for (i = 0; i < jp->nprocs; i++)
362 1.47 christos if (killpg(jp->ps[i].pid, SIGCONT) != -1)
363 1.47 christos break;
364 1.48 christos if (i >= jp->nprocs)
365 1.47 christos error("Cannot continue job (%s)", strerror(errno));
366 1.1 cgd for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
367 1.23 christos if (WIFSTOPPED(ps->status)) {
368 1.1 cgd ps->status = -1;
369 1.55 christos jp->state = JOBRUNNING;
370 1.1 cgd }
371 1.1 cgd }
372 1.1 cgd INTON;
373 1.1 cgd }
374 1.1 cgd #endif
375 1.1 cgd
376 1.55 christos static void
377 1.55 christos showjob(struct output *out, struct job *jp, int mode)
378 1.55 christos {
379 1.55 christos int procno;
380 1.55 christos int st;
381 1.55 christos struct procstat *ps;
382 1.55 christos int col;
383 1.55 christos char s[64];
384 1.55 christos
385 1.55 christos #if JOBS
386 1.55 christos if (mode & SHOW_PGID) {
387 1.55 christos /* just output process (group) id of pipeline */
388 1.55 christos outfmt(out, "%ld\n", (long)jp->ps->pid);
389 1.55 christos return;
390 1.55 christos }
391 1.55 christos #endif
392 1.55 christos
393 1.55 christos procno = jp->nprocs;
394 1.55 christos if (!procno)
395 1.55 christos return;
396 1.55 christos
397 1.55 christos if (mode & SHOW_PID)
398 1.55 christos mode |= SHOW_MULTILINE;
399 1.55 christos
400 1.55 christos if ((procno > 1 && !(mode & SHOW_MULTILINE))
401 1.55 christos || (mode & SHOW_SIGNALLED)) {
402 1.55 christos /* See if we have more than one status to report */
403 1.55 christos ps = jp->ps;
404 1.55 christos st = ps->status;
405 1.55 christos do {
406 1.55 christos int st1 = ps->status;
407 1.55 christos if (st1 != st)
408 1.55 christos /* yes - need multi-line output */
409 1.55 christos mode |= SHOW_MULTILINE;
410 1.55 christos if (st1 == -1 || !(mode & SHOW_SIGNALLED) || WIFEXITED(st1))
411 1.55 christos continue;
412 1.55 christos if (WIFSTOPPED(st1) || ((st1 = WTERMSIG(st1) & 0x7f)
413 1.55 christos && st1 != SIGINT && st1 != SIGPIPE))
414 1.55 christos mode |= SHOW_ISSIG;
415 1.55 christos
416 1.55 christos } while (ps++, --procno);
417 1.55 christos procno = jp->nprocs;
418 1.55 christos }
419 1.55 christos
420 1.55 christos if (mode & SHOW_SIGNALLED && !(mode & SHOW_ISSIG)) {
421 1.55 christos if (jp->state == JOBDONE && !(mode & SHOW_NO_FREE)) {
422 1.55 christos TRACE(("showjob: freeing job %d\n", jp - jobtab + 1));
423 1.55 christos freejob(jp);
424 1.55 christos }
425 1.55 christos return;
426 1.55 christos }
427 1.55 christos
428 1.55 christos for (ps = jp->ps; --procno >= 0; ps++) { /* for each process */
429 1.55 christos if (ps == jp->ps)
430 1.56 agc fmtstr(s, 16, "[%ld] %c ",
431 1.56 agc (long)(jp - jobtab + 1),
432 1.55 christos #if JOBS
433 1.55 christos jp == jobtab + curjob ? '+' :
434 1.55 christos curjob != -1 && jp == jobtab +
435 1.55 christos jobtab[curjob].prev_job ? '-' :
436 1.55 christos #endif
437 1.55 christos ' ');
438 1.55 christos else
439 1.55 christos fmtstr(s, 16, " " );
440 1.55 christos col = strlen(s);
441 1.55 christos if (mode & SHOW_PID) {
442 1.55 christos fmtstr(s + col, 16, "%ld ", (long)ps->pid);
443 1.55 christos col += strlen(s + col);
444 1.55 christos }
445 1.55 christos if (ps->status == -1) {
446 1.55 christos scopy("Running", s + col);
447 1.55 christos } else if (WIFEXITED(ps->status)) {
448 1.55 christos st = WEXITSTATUS(ps->status);
449 1.55 christos if (st)
450 1.55 christos fmtstr(s + col, 16, "Done(%d)", st);
451 1.55 christos else
452 1.55 christos fmtstr(s + col, 16, "Done");
453 1.55 christos } else {
454 1.55 christos #if JOBS
455 1.55 christos if (WIFSTOPPED(ps->status))
456 1.55 christos st = WSTOPSIG(ps->status);
457 1.55 christos else /* WIFSIGNALED(ps->status) */
458 1.55 christos #endif
459 1.55 christos st = WTERMSIG(ps->status);
460 1.55 christos st &= 0x7f;
461 1.55 christos if (st < NSIG && sys_siglist[st])
462 1.55 christos scopyn(sys_siglist[st], s + col, 32);
463 1.55 christos else
464 1.55 christos fmtstr(s + col, 16, "Signal %d", st);
465 1.55 christos if (WCOREDUMP(ps->status)) {
466 1.55 christos col += strlen(s + col);
467 1.55 christos scopyn(" (core dumped)", s + col, 64 - col);
468 1.55 christos }
469 1.55 christos }
470 1.55 christos col += strlen(s + col);
471 1.55 christos outstr(s, out);
472 1.55 christos do {
473 1.55 christos outc(' ', out);
474 1.55 christos col++;
475 1.55 christos } while (col < 30);
476 1.55 christos outstr(ps->cmd, out);
477 1.55 christos if (mode & SHOW_MULTILINE) {
478 1.55 christos if (procno > 0) {
479 1.55 christos outc(' ', out);
480 1.55 christos outc('|', out);
481 1.55 christos }
482 1.55 christos } else {
483 1.55 christos while (--procno >= 0)
484 1.55 christos outfmt(out, " | %s", (++ps)->cmd );
485 1.55 christos }
486 1.55 christos outc('\n', out);
487 1.55 christos }
488 1.55 christos flushout(out);
489 1.55 christos jp->changed = 0;
490 1.55 christos if (jp->state == JOBDONE && !(mode & SHOW_NO_FREE))
491 1.55 christos freejob(jp);
492 1.55 christos }
493 1.55 christos
494 1.1 cgd
495 1.1 cgd int
496 1.55 christos jobscmd(int argc, char **argv)
497 1.13 cgd {
498 1.55 christos int mode, m;
499 1.55 christos int sv = jobs_invalid;
500 1.55 christos
501 1.55 christos jobs_invalid = 0;
502 1.55 christos mode = 0;
503 1.55 christos while ((m = nextopt("lp")))
504 1.55 christos if (m == 'l')
505 1.55 christos mode = SHOW_PID;
506 1.55 christos else
507 1.55 christos mode = SHOW_PGID;
508 1.55 christos if (*argptr)
509 1.55 christos do
510 1.55 christos showjob(out1, getjob(*argptr,0), mode);
511 1.55 christos while (*++argptr);
512 1.55 christos else
513 1.55 christos showjobs(out1, mode);
514 1.55 christos jobs_invalid = sv;
515 1.1 cgd return 0;
516 1.1 cgd }
517 1.1 cgd
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * Print a list of jobs. If "change" is nonzero, only print jobs whose
521 1.1 cgd * statuses have changed since the last call to showjobs.
522 1.1 cgd *
523 1.1 cgd * If the shell is interrupted in the process of creating a job, the
524 1.1 cgd * result may be a job structure containing zero processes. Such structures
525 1.1 cgd * will be freed here.
526 1.1 cgd */
527 1.1 cgd
528 1.1 cgd void
529 1.55 christos showjobs(struct output *out, int mode)
530 1.13 cgd {
531 1.1 cgd int jobno;
532 1.1 cgd struct job *jp;
533 1.47 christos int silent = 0, gotpid;
534 1.1 cgd
535 1.55 christos TRACE(("showjobs(%x) called\n", mode));
536 1.47 christos
537 1.47 christos /* If not even one one job changed, there is nothing to do */
538 1.47 christos gotpid = dowait(0, NULL);
539 1.47 christos while (dowait(0, NULL) > 0)
540 1.47 christos continue;
541 1.47 christos #ifdef JOBS
542 1.47 christos /*
543 1.47 christos * Check if we are not in our foreground group, and if not
544 1.47 christos * put us in it.
545 1.47 christos */
546 1.54 christos if (mflag && gotpid != -1 && tcgetpgrp(ttyfd) != getpid()) {
547 1.47 christos if (tcsetpgrp(ttyfd, getpid()) == -1)
548 1.47 christos error("Cannot set tty process group (%s) at %d",
549 1.47 christos strerror(errno), __LINE__);
550 1.47 christos TRACE(("repaired tty process group\n"));
551 1.47 christos silent = 1;
552 1.47 christos }
553 1.47 christos #endif
554 1.55 christos if (jobs_invalid)
555 1.55 christos return;
556 1.55 christos
557 1.1 cgd for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
558 1.55 christos if (!jp->used)
559 1.1 cgd continue;
560 1.1 cgd if (jp->nprocs == 0) {
561 1.1 cgd freejob(jp);
562 1.1 cgd continue;
563 1.1 cgd }
564 1.55 christos if ((mode & SHOW_CHANGED) && !jp->changed)
565 1.1 cgd continue;
566 1.47 christos if (silent && jp->changed) {
567 1.47 christos jp->changed = 0;
568 1.47 christos continue;
569 1.47 christos }
570 1.55 christos showjob(out, jp, mode);
571 1.1 cgd }
572 1.1 cgd }
573 1.1 cgd
574 1.1 cgd /*
575 1.1 cgd * Mark a job structure as unused.
576 1.1 cgd */
577 1.1 cgd
578 1.1 cgd STATIC void
579 1.55 christos freejob(struct job *jp)
580 1.55 christos {
581 1.1 cgd INTOFF;
582 1.41 christos if (jp->ps != &jp->ps0) {
583 1.1 cgd ckfree(jp->ps);
584 1.41 christos jp->ps = &jp->ps0;
585 1.41 christos }
586 1.41 christos jp->nprocs = 0;
587 1.1 cgd jp->used = 0;
588 1.1 cgd #if JOBS
589 1.55 christos set_curjob(jp, 0);
590 1.1 cgd #endif
591 1.1 cgd INTON;
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd
595 1.1 cgd
596 1.1 cgd int
597 1.55 christos waitcmd(int argc, char **argv)
598 1.13 cgd {
599 1.1 cgd struct job *job;
600 1.23 christos int status, retval;
601 1.1 cgd struct job *jp;
602 1.1 cgd
603 1.55 christos nextopt("");
604 1.55 christos
605 1.55 christos if (!*argptr) {
606 1.55 christos /* wait for all jobs */
607 1.55 christos jp = jobtab;
608 1.55 christos if (jobs_invalid)
609 1.55 christos return 0;
610 1.55 christos for (;;) {
611 1.55 christos if (jp >= jobtab + njobs) {
612 1.55 christos /* no running procs */
613 1.55 christos return 0;
614 1.55 christos }
615 1.55 christos if (!jp->used || jp->state != JOBRUNNING) {
616 1.55 christos jp++;
617 1.55 christos continue;
618 1.55 christos }
619 1.55 christos if (dowait(1, (struct job *)NULL) == -1)
620 1.55 christos return 128 + SIGINT;
621 1.55 christos jp = jobtab;
622 1.55 christos }
623 1.1 cgd }
624 1.55 christos
625 1.55 christos for (; *argptr; argptr++) {
626 1.55 christos job = getjob(*argptr, 1);
627 1.55 christos if (!job) {
628 1.55 christos retval = 127;
629 1.55 christos continue;
630 1.55 christos }
631 1.55 christos /* loop until process terminated or stopped */
632 1.55 christos while (job->state == JOBRUNNING) {
633 1.55 christos if (dowait(1, (struct job *)NULL) == -1)
634 1.55 christos return 128 + SIGINT;
635 1.55 christos }
636 1.55 christos status = job->ps[job->nprocs - 1].status;
637 1.55 christos if (WIFEXITED(status))
638 1.55 christos retval = WEXITSTATUS(status);
639 1.1 cgd #if JOBS
640 1.55 christos else if (WIFSTOPPED(status))
641 1.55 christos retval = WSTOPSIG(status) + 128;
642 1.1 cgd #endif
643 1.55 christos else {
644 1.55 christos /* XXX: limits number of signals */
645 1.55 christos retval = WTERMSIG(status) + 128;
646 1.1 cgd }
647 1.55 christos if (!iflag)
648 1.55 christos freejob(job);
649 1.1 cgd }
650 1.55 christos return retval;
651 1.1 cgd }
652 1.1 cgd
653 1.1 cgd
654 1.1 cgd
655 1.13 cgd int
656 1.55 christos jobidcmd(int argc, char **argv)
657 1.13 cgd {
658 1.1 cgd struct job *jp;
659 1.1 cgd int i;
660 1.1 cgd
661 1.55 christos nextopt("");
662 1.55 christos jp = getjob(*argptr, 0);
663 1.1 cgd for (i = 0 ; i < jp->nprocs ; ) {
664 1.27 christos out1fmt("%ld", (long)jp->ps[i].pid);
665 1.55 christos out1c(++i < jp->nprocs ? ' ' : '\n');
666 1.1 cgd }
667 1.1 cgd return 0;
668 1.1 cgd }
669 1.1 cgd
670 1.55 christos int
671 1.55 christos getjobpgrp(const char *name)
672 1.55 christos {
673 1.55 christos struct job *jp;
674 1.1 cgd
675 1.55 christos jp = getjob(name, 1);
676 1.55 christos if (jp == 0)
677 1.55 christos return 0;
678 1.55 christos return -jp->ps[0].pid;
679 1.55 christos }
680 1.1 cgd
681 1.1 cgd /*
682 1.1 cgd * Convert a job name to a job structure.
683 1.1 cgd */
684 1.1 cgd
685 1.1 cgd STATIC struct job *
686 1.55 christos getjob(const char *name, int noerror)
687 1.55 christos {
688 1.55 christos int jobno = -1;
689 1.21 tls struct job *jp;
690 1.1 cgd int pid;
691 1.1 cgd int i;
692 1.55 christos const char *err_msg = "No such job: %s";
693 1.55 christos
694 1.1 cgd if (name == NULL) {
695 1.1 cgd #if JOBS
696 1.55 christos jobno = curjob;
697 1.1 cgd #endif
698 1.55 christos err_msg = "No current job";
699 1.1 cgd } else if (name[0] == '%') {
700 1.55 christos if (is_number(name + 1)) {
701 1.55 christos jobno = number(name + 1) - 1;
702 1.55 christos } else if (!name[2]) {
703 1.55 christos switch (name[1]) {
704 1.1 cgd #if JOBS
705 1.55 christos case 0:
706 1.55 christos case '+':
707 1.55 christos case '%':
708 1.55 christos jobno = curjob;
709 1.55 christos err_msg = "No current job";
710 1.55 christos break;
711 1.55 christos case '-':
712 1.55 christos jobno = curjob;
713 1.55 christos if (jobno != -1)
714 1.55 christos jobno = jobtab[jobno].prev_job;
715 1.55 christos err_msg = "No previous job";
716 1.55 christos break;
717 1.1 cgd #endif
718 1.55 christos default:
719 1.55 christos goto check_pattern;
720 1.55 christos }
721 1.1 cgd } else {
722 1.55 christos struct job *found;
723 1.55 christos check_pattern:
724 1.55 christos found = NULL;
725 1.1 cgd for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
726 1.55 christos if (!jp->used || jp->nprocs <= 0)
727 1.55 christos continue;
728 1.55 christos if ((name[1] == '?'
729 1.55 christos && strstr(jp->ps[0].cmd, name + 2))
730 1.55 christos || prefix(name + 1, jp->ps[0].cmd)) {
731 1.55 christos if (found) {
732 1.55 christos err_msg = "%s: ambiguous";
733 1.55 christos found = 0;
734 1.55 christos break;
735 1.55 christos }
736 1.1 cgd found = jp;
737 1.1 cgd }
738 1.1 cgd }
739 1.1 cgd if (found)
740 1.1 cgd return found;
741 1.1 cgd }
742 1.55 christos
743 1.1 cgd } else if (is_number(name)) {
744 1.1 cgd pid = number(name);
745 1.1 cgd for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
746 1.1 cgd if (jp->used && jp->nprocs > 0
747 1.1 cgd && jp->ps[jp->nprocs - 1].pid == pid)
748 1.1 cgd return jp;
749 1.1 cgd }
750 1.1 cgd }
751 1.55 christos
752 1.55 christos if (!jobs_invalid && jobno >= 0 && jobno < njobs) {
753 1.55 christos jp = jobtab + jobno;
754 1.55 christos if (jp->used)
755 1.55 christos return jp;
756 1.55 christos }
757 1.55 christos if (!noerror)
758 1.55 christos error(err_msg, name);
759 1.55 christos return 0;
760 1.1 cgd }
761 1.1 cgd
762 1.1 cgd
763 1.1 cgd
764 1.1 cgd /*
765 1.1 cgd * Return a new job structure,
766 1.1 cgd */
767 1.1 cgd
768 1.1 cgd struct job *
769 1.55 christos makejob(union node *node, int nprocs)
770 1.13 cgd {
771 1.1 cgd int i;
772 1.1 cgd struct job *jp;
773 1.1 cgd
774 1.55 christos if (jobs_invalid) {
775 1.55 christos for (i = njobs, jp = jobtab ; --i >= 0 ; jp++) {
776 1.55 christos if (jp->used)
777 1.55 christos freejob(jp);
778 1.55 christos }
779 1.55 christos jobs_invalid = 0;
780 1.55 christos }
781 1.55 christos
782 1.1 cgd for (i = njobs, jp = jobtab ; ; jp++) {
783 1.1 cgd if (--i < 0) {
784 1.1 cgd INTOFF;
785 1.1 cgd if (njobs == 0) {
786 1.1 cgd jobtab = ckmalloc(4 * sizeof jobtab[0]);
787 1.1 cgd } else {
788 1.1 cgd jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
789 1.12 mycroft memcpy(jp, jobtab, njobs * sizeof jp[0]);
790 1.17 pk /* Relocate `ps' pointers */
791 1.17 pk for (i = 0; i < njobs; i++)
792 1.17 pk if (jp[i].ps == &jobtab[i].ps0)
793 1.17 pk jp[i].ps = &jp[i].ps0;
794 1.1 cgd ckfree(jobtab);
795 1.1 cgd jobtab = jp;
796 1.1 cgd }
797 1.1 cgd jp = jobtab + njobs;
798 1.1 cgd for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
799 1.1 cgd INTON;
800 1.1 cgd break;
801 1.1 cgd }
802 1.1 cgd if (jp->used == 0)
803 1.1 cgd break;
804 1.1 cgd }
805 1.1 cgd INTOFF;
806 1.55 christos jp->state = JOBRUNNING;
807 1.1 cgd jp->used = 1;
808 1.1 cgd jp->changed = 0;
809 1.1 cgd jp->nprocs = 0;
810 1.1 cgd #if JOBS
811 1.1 cgd jp->jobctl = jobctl;
812 1.55 christos set_curjob(jp, 1);
813 1.1 cgd #endif
814 1.1 cgd if (nprocs > 1) {
815 1.1 cgd jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
816 1.1 cgd } else {
817 1.1 cgd jp->ps = &jp->ps0;
818 1.1 cgd }
819 1.1 cgd INTON;
820 1.14 cgd TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
821 1.14 cgd jp - jobtab + 1));
822 1.1 cgd return jp;
823 1.19 christos }
824 1.1 cgd
825 1.1 cgd
826 1.1 cgd /*
827 1.37 lukem * Fork off a subshell. If we are doing job control, give the subshell its
828 1.1 cgd * own process group. Jp is a job structure that the job is to be added to.
829 1.1 cgd * N is the command that will be evaluated by the child. Both jp and n may
830 1.1 cgd * be NULL. The mode parameter can be one of the following:
831 1.1 cgd * FORK_FG - Fork off a foreground process.
832 1.1 cgd * FORK_BG - Fork off a background process.
833 1.1 cgd * FORK_NOJOB - Like FORK_FG, but don't give the process its own
834 1.1 cgd * process group even if job control is on.
835 1.1 cgd *
836 1.1 cgd * When job control is turned off, background processes have their standard
837 1.1 cgd * input redirected to /dev/null (except for the second and later processes
838 1.1 cgd * in a pipeline).
839 1.1 cgd */
840 1.1 cgd
841 1.1 cgd int
842 1.55 christos forkshell(struct job *jp, union node *n, int mode)
843 1.13 cgd {
844 1.1 cgd int pid;
845 1.1 cgd
846 1.42 simonb TRACE(("forkshell(%%%d, %p, %d) called\n", jp - jobtab, n, mode));
847 1.47 christos switch ((pid = fork())) {
848 1.47 christos case -1:
849 1.44 christos TRACE(("Fork failed, errno=%d", errno));
850 1.1 cgd INTON;
851 1.1 cgd error("Cannot fork");
852 1.47 christos break;
853 1.47 christos case 0:
854 1.51 christos forkchild(jp, n, mode, 0);
855 1.47 christos return 0;
856 1.47 christos default:
857 1.51 christos return forkparent(jp, n, mode, pid);
858 1.1 cgd }
859 1.47 christos }
860 1.47 christos
861 1.47 christos int
862 1.55 christos forkparent(struct job *jp, union node *n, int mode, pid_t pid)
863 1.47 christos {
864 1.53 mycroft int pgrp;
865 1.1 cgd
866 1.53 mycroft if (rootshell && mode != FORK_NOJOB && mflag) {
867 1.53 mycroft if (jp == NULL || jp->nprocs == 0)
868 1.53 mycroft pgrp = pid;
869 1.53 mycroft else
870 1.53 mycroft pgrp = jp->ps[0].pid;
871 1.53 mycroft /* This can fail because we are doing it in the child also */
872 1.53 mycroft (void)setpgid(pid, pgrp);
873 1.53 mycroft }
874 1.1 cgd if (mode == FORK_BG)
875 1.1 cgd backgndpid = pid; /* set $! */
876 1.1 cgd if (jp) {
877 1.1 cgd struct procstat *ps = &jp->ps[jp->nprocs++];
878 1.1 cgd ps->pid = pid;
879 1.1 cgd ps->status = -1;
880 1.55 christos ps->cmd[0] = 0;
881 1.55 christos #ifndef SMALL
882 1.55 christos if (/* iflag && rootshell && */ n)
883 1.55 christos commandtext(ps, n);
884 1.55 christos #endif
885 1.1 cgd }
886 1.1 cgd TRACE(("In parent shell: child = %d\n", pid));
887 1.1 cgd return pid;
888 1.1 cgd }
889 1.1 cgd
890 1.47 christos void
891 1.55 christos forkchild(struct job *jp, union node *n, int mode, int vforked)
892 1.47 christos {
893 1.47 christos int wasroot;
894 1.47 christos int pgrp;
895 1.47 christos const char *devnull = _PATH_DEVNULL;
896 1.47 christos const char *nullerr = "Can't open %s";
897 1.47 christos
898 1.51 christos wasroot = rootshell;
899 1.47 christos TRACE(("Child shell %d\n", getpid()));
900 1.55 christos if (!vforked)
901 1.51 christos rootshell = 0;
902 1.55 christos
903 1.47 christos closescript(vforked);
904 1.47 christos clear_traps(vforked);
905 1.47 christos #if JOBS
906 1.47 christos if (!vforked)
907 1.47 christos jobctl = 0; /* do job control only in root shell */
908 1.47 christos if (wasroot && mode != FORK_NOJOB && mflag) {
909 1.47 christos if (jp == NULL || jp->nprocs == 0)
910 1.47 christos pgrp = getpid();
911 1.47 christos else
912 1.47 christos pgrp = jp->ps[0].pid;
913 1.53 mycroft /* This can fail because we are doing it in the parent also */
914 1.53 mycroft (void)setpgid(0, pgrp);
915 1.47 christos if (mode == FORK_FG) {
916 1.47 christos if (tcsetpgrp(ttyfd, pgrp) == -1)
917 1.47 christos error("Cannot set tty process group (%s) at %d",
918 1.47 christos strerror(errno), __LINE__);
919 1.47 christos }
920 1.47 christos setsignal(SIGTSTP, vforked);
921 1.47 christos setsignal(SIGTTOU, vforked);
922 1.47 christos } else if (mode == FORK_BG) {
923 1.47 christos ignoresig(SIGINT, vforked);
924 1.47 christos ignoresig(SIGQUIT, vforked);
925 1.47 christos if ((jp == NULL || jp->nprocs == 0) &&
926 1.47 christos ! fd0_redirected_p ()) {
927 1.47 christos close(0);
928 1.47 christos if (open(devnull, O_RDONLY) != 0)
929 1.47 christos error(nullerr, devnull);
930 1.47 christos }
931 1.47 christos }
932 1.47 christos #else
933 1.47 christos if (mode == FORK_BG) {
934 1.47 christos ignoresig(SIGINT, vforked);
935 1.47 christos ignoresig(SIGQUIT, vforked);
936 1.47 christos if ((jp == NULL || jp->nprocs == 0) &&
937 1.47 christos ! fd0_redirected_p ()) {
938 1.47 christos close(0);
939 1.47 christos if (open(devnull, O_RDONLY) != 0)
940 1.47 christos error(nullerr, devnull);
941 1.47 christos }
942 1.47 christos }
943 1.47 christos #endif
944 1.47 christos if (wasroot && iflag) {
945 1.47 christos setsignal(SIGINT, vforked);
946 1.47 christos setsignal(SIGQUIT, vforked);
947 1.47 christos setsignal(SIGTERM, vforked);
948 1.47 christos }
949 1.55 christos
950 1.55 christos if (!vforked)
951 1.55 christos jobs_invalid = 1;
952 1.47 christos }
953 1.47 christos
954 1.1 cgd /*
955 1.1 cgd * Wait for job to finish.
956 1.1 cgd *
957 1.1 cgd * Under job control we have the problem that while a child process is
958 1.1 cgd * running interrupts generated by the user are sent to the child but not
959 1.1 cgd * to the shell. This means that an infinite loop started by an inter-
960 1.1 cgd * active user may be hard to kill. With job control turned off, an
961 1.1 cgd * interactive user may place an interactive program inside a loop. If
962 1.1 cgd * the interactive program catches interrupts, the user doesn't want
963 1.1 cgd * these interrupts to also abort the loop. The approach we take here
964 1.1 cgd * is to have the shell ignore interrupt signals while waiting for a
965 1.1 cgd * forground process to terminate, and then send itself an interrupt
966 1.1 cgd * signal if the child process was terminated by an interrupt signal.
967 1.1 cgd * Unfortunately, some programs want to do a bit of cleanup and then
968 1.1 cgd * exit on interrupt; unless these processes terminate themselves by
969 1.1 cgd * sending a signal to themselves (instead of calling exit) they will
970 1.1 cgd * confuse this approach.
971 1.1 cgd */
972 1.1 cgd
973 1.1 cgd int
974 1.55 christos waitforjob(struct job *jp)
975 1.55 christos {
976 1.1 cgd #if JOBS
977 1.10 jtc int mypgrp = getpgrp();
978 1.1 cgd #endif
979 1.1 cgd int status;
980 1.1 cgd int st;
981 1.1 cgd
982 1.1 cgd INTOFF;
983 1.1 cgd TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
984 1.55 christos while (jp->state == JOBRUNNING) {
985 1.1 cgd dowait(1, jp);
986 1.1 cgd }
987 1.1 cgd #if JOBS
988 1.1 cgd if (jp->jobctl) {
989 1.47 christos if (tcsetpgrp(ttyfd, mypgrp) == -1)
990 1.47 christos error("Cannot set tty process group (%s) at %d",
991 1.47 christos strerror(errno), __LINE__);
992 1.1 cgd }
993 1.55 christos if (jp->state == JOBSTOPPED && curjob != jp - jobtab)
994 1.55 christos set_curjob(jp, 2);
995 1.1 cgd #endif
996 1.1 cgd status = jp->ps[jp->nprocs - 1].status;
997 1.1 cgd /* convert to 8 bits */
998 1.23 christos if (WIFEXITED(status))
999 1.23 christos st = WEXITSTATUS(status);
1000 1.1 cgd #if JOBS
1001 1.23 christos else if (WIFSTOPPED(status))
1002 1.23 christos st = WSTOPSIG(status) + 128;
1003 1.1 cgd #endif
1004 1.1 cgd else
1005 1.23 christos st = WTERMSIG(status) + 128;
1006 1.55 christos TRACE(("waitforjob: job %d, nproc %d, status %x, st %x\n",
1007 1.55 christos jp - jobtab + 1, jp->nprocs, status, st ));
1008 1.32 mycroft #if JOBS
1009 1.32 mycroft if (jp->jobctl) {
1010 1.32 mycroft /*
1011 1.32 mycroft * This is truly gross.
1012 1.32 mycroft * If we're doing job control, then we did a TIOCSPGRP which
1013 1.32 mycroft * caused us (the shell) to no longer be in the controlling
1014 1.32 mycroft * session -- so we wouldn't have seen any ^C/SIGINT. So, we
1015 1.32 mycroft * intuit from the subprocess exit status whether a SIGINT
1016 1.40 wiz * occurred, and if so interrupt ourselves. Yuck. - mycroft
1017 1.32 mycroft */
1018 1.32 mycroft if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1019 1.32 mycroft raise(SIGINT);
1020 1.32 mycroft }
1021 1.32 mycroft #endif
1022 1.1 cgd if (! JOBS || jp->state == JOBDONE)
1023 1.1 cgd freejob(jp);
1024 1.1 cgd INTON;
1025 1.1 cgd return st;
1026 1.1 cgd }
1027 1.1 cgd
1028 1.1 cgd
1029 1.1 cgd
1030 1.1 cgd /*
1031 1.1 cgd * Wait for a process to terminate.
1032 1.1 cgd */
1033 1.1 cgd
1034 1.1 cgd STATIC int
1035 1.55 christos dowait(int block, struct job *job)
1036 1.13 cgd {
1037 1.1 cgd int pid;
1038 1.1 cgd int status;
1039 1.1 cgd struct procstat *sp;
1040 1.1 cgd struct job *jp;
1041 1.1 cgd struct job *thisjob;
1042 1.1 cgd int done;
1043 1.1 cgd int stopped;
1044 1.43 christos extern volatile char gotsig[];
1045 1.1 cgd
1046 1.1 cgd TRACE(("dowait(%d) called\n", block));
1047 1.1 cgd do {
1048 1.38 christos pid = waitproc(block, job, &status);
1049 1.55 christos TRACE(("wait returns pid %d, status %d\n", pid, status));
1050 1.43 christos } while (pid == -1 && errno == EINTR && gotsig[SIGINT - 1] == 0);
1051 1.1 cgd if (pid <= 0)
1052 1.1 cgd return pid;
1053 1.1 cgd INTOFF;
1054 1.1 cgd thisjob = NULL;
1055 1.1 cgd for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1056 1.1 cgd if (jp->used) {
1057 1.1 cgd done = 1;
1058 1.1 cgd stopped = 1;
1059 1.1 cgd for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1060 1.1 cgd if (sp->pid == -1)
1061 1.1 cgd continue;
1062 1.1 cgd if (sp->pid == pid) {
1063 1.55 christos TRACE(("Job %d: changing status of proc %d from 0x%x to 0x%x\n", jp - jobtab + 1, pid, sp->status, status));
1064 1.1 cgd sp->status = status;
1065 1.1 cgd thisjob = jp;
1066 1.1 cgd }
1067 1.1 cgd if (sp->status == -1)
1068 1.1 cgd stopped = 0;
1069 1.23 christos else if (WIFSTOPPED(sp->status))
1070 1.1 cgd done = 0;
1071 1.1 cgd }
1072 1.1 cgd if (stopped) { /* stopped or done */
1073 1.55 christos int state = done ? JOBDONE : JOBSTOPPED;
1074 1.1 cgd if (jp->state != state) {
1075 1.1 cgd TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1076 1.1 cgd jp->state = state;
1077 1.1 cgd #if JOBS
1078 1.55 christos if (done)
1079 1.55 christos set_curjob(jp, 0);
1080 1.1 cgd #endif
1081 1.1 cgd }
1082 1.1 cgd }
1083 1.1 cgd }
1084 1.1 cgd }
1085 1.23 christos
1086 1.55 christos if (thisjob && thisjob->state != JOBRUNNING) {
1087 1.55 christos int mode = 0;
1088 1.55 christos if (!rootshell || !iflag)
1089 1.55 christos mode = SHOW_SIGNALLED;
1090 1.55 christos if (job == thisjob)
1091 1.55 christos mode = SHOW_SIGNALLED | SHOW_NO_FREE;
1092 1.55 christos if (mode)
1093 1.55 christos showjob(out2, thisjob, mode);
1094 1.55 christos else {
1095 1.55 christos TRACE(("Not printing status, rootshell=%d, job=%p\n",
1096 1.55 christos rootshell, job));
1097 1.55 christos thisjob->changed = 1;
1098 1.1 cgd }
1099 1.1 cgd }
1100 1.55 christos
1101 1.47 christos INTON;
1102 1.1 cgd return pid;
1103 1.1 cgd }
1104 1.1 cgd
1105 1.1 cgd
1106 1.1 cgd
1107 1.1 cgd /*
1108 1.1 cgd * Do a wait system call. If job control is compiled in, we accept
1109 1.1 cgd * stopped processes. If block is zero, we return a value of zero
1110 1.1 cgd * rather than blocking.
1111 1.1 cgd *
1112 1.1 cgd * System V doesn't have a non-blocking wait system call. It does
1113 1.1 cgd * have a SIGCLD signal that is sent to a process when one of it's
1114 1.1 cgd * children dies. The obvious way to use SIGCLD would be to install
1115 1.1 cgd * a handler for SIGCLD which simply bumped a counter when a SIGCLD
1116 1.1 cgd * was received, and have waitproc bump another counter when it got
1117 1.1 cgd * the status of a process. Waitproc would then know that a wait
1118 1.1 cgd * system call would not block if the two counters were different.
1119 1.1 cgd * This approach doesn't work because if a process has children that
1120 1.1 cgd * have not been waited for, System V will send it a SIGCLD when it
1121 1.1 cgd * installs a signal handler for SIGCLD. What this means is that when
1122 1.1 cgd * a child exits, the shell will be sent SIGCLD signals continuously
1123 1.1 cgd * until is runs out of stack space, unless it does a wait call before
1124 1.1 cgd * restoring the signal handler. The code below takes advantage of
1125 1.1 cgd * this (mis)feature by installing a signal handler for SIGCLD and
1126 1.1 cgd * then checking to see whether it was called. If there are any
1127 1.1 cgd * children to be waited for, it will be.
1128 1.1 cgd *
1129 1.1 cgd * If neither SYSV nor BSD is defined, we don't implement nonblocking
1130 1.1 cgd * waits at all. In this case, the user will not be informed when
1131 1.1 cgd * a background process until the next time she runs a real program
1132 1.1 cgd * (as opposed to running a builtin command or just typing return),
1133 1.1 cgd * and the jobs command may give out of date information.
1134 1.1 cgd */
1135 1.1 cgd
1136 1.1 cgd #ifdef SYSV
1137 1.1 cgd STATIC int gotsigchild;
1138 1.1 cgd
1139 1.1 cgd STATIC int onsigchild() {
1140 1.1 cgd gotsigchild = 1;
1141 1.1 cgd }
1142 1.1 cgd #endif
1143 1.1 cgd
1144 1.1 cgd
1145 1.1 cgd STATIC int
1146 1.55 christos waitproc(int block, struct job *jp, int *status)
1147 1.13 cgd {
1148 1.1 cgd #ifdef BSD
1149 1.38 christos int flags = 0;
1150 1.1 cgd
1151 1.1 cgd #if JOBS
1152 1.39 assar if (jp != NULL && jp->jobctl)
1153 1.38 christos flags |= WUNTRACED;
1154 1.1 cgd #endif
1155 1.1 cgd if (block == 0)
1156 1.1 cgd flags |= WNOHANG;
1157 1.8 jtc return wait3(status, flags, (struct rusage *)NULL);
1158 1.1 cgd #else
1159 1.1 cgd #ifdef SYSV
1160 1.1 cgd int (*save)();
1161 1.1 cgd
1162 1.1 cgd if (block == 0) {
1163 1.1 cgd gotsigchild = 0;
1164 1.1 cgd save = signal(SIGCLD, onsigchild);
1165 1.1 cgd signal(SIGCLD, save);
1166 1.1 cgd if (gotsigchild == 0)
1167 1.1 cgd return 0;
1168 1.1 cgd }
1169 1.1 cgd return wait(status);
1170 1.1 cgd #else
1171 1.1 cgd if (block == 0)
1172 1.1 cgd return 0;
1173 1.1 cgd return wait(status);
1174 1.1 cgd #endif
1175 1.1 cgd #endif
1176 1.1 cgd }
1177 1.1 cgd
1178 1.8 jtc /*
1179 1.8 jtc * return 1 if there are stopped jobs, otherwise 0
1180 1.8 jtc */
1181 1.8 jtc int job_warning = 0;
1182 1.8 jtc int
1183 1.55 christos stoppedjobs(void)
1184 1.8 jtc {
1185 1.21 tls int jobno;
1186 1.21 tls struct job *jp;
1187 1.8 jtc
1188 1.55 christos if (job_warning || jobs_invalid)
1189 1.8 jtc return (0);
1190 1.8 jtc for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1191 1.8 jtc if (jp->used == 0)
1192 1.8 jtc continue;
1193 1.8 jtc if (jp->state == JOBSTOPPED) {
1194 1.8 jtc out2str("You have stopped jobs.\n");
1195 1.8 jtc job_warning = 2;
1196 1.8 jtc return (1);
1197 1.8 jtc }
1198 1.8 jtc }
1199 1.1 cgd
1200 1.8 jtc return (0);
1201 1.8 jtc }
1202 1.1 cgd
1203 1.1 cgd /*
1204 1.1 cgd * Return a string identifying a command (to be printed by the
1205 1.55 christos * jobs command).
1206 1.1 cgd */
1207 1.1 cgd
1208 1.1 cgd STATIC char *cmdnextc;
1209 1.1 cgd STATIC int cmdnleft;
1210 1.1 cgd
1211 1.55 christos void
1212 1.55 christos commandtext(struct procstat *ps, union node *n)
1213 1.55 christos {
1214 1.55 christos int len;
1215 1.1 cgd
1216 1.55 christos cmdnextc = ps->cmd;
1217 1.55 christos if (iflag || mflag)
1218 1.55 christos len = sizeof(ps->cmd);
1219 1.55 christos else
1220 1.55 christos len = sizeof(ps->cmd) / 10;
1221 1.55 christos cmdnleft = len;
1222 1.1 cgd cmdtxt(n);
1223 1.55 christos if (cmdnleft <= 0) {
1224 1.55 christos char *p = ps->cmd + len - 4;
1225 1.55 christos p[0] = '.';
1226 1.55 christos p[1] = '.';
1227 1.55 christos p[2] = '.';
1228 1.55 christos p[3] = 0;
1229 1.55 christos } else
1230 1.55 christos *cmdnextc = '\0';
1231 1.55 christos TRACE(("commandtext: ps->cmd %x, end %x, left %d\n\t\"%s\"\n",
1232 1.55 christos ps->cmd, cmdnextc, cmdnleft, ps->cmd));
1233 1.1 cgd }
1234 1.1 cgd
1235 1.1 cgd
1236 1.1 cgd STATIC void
1237 1.55 christos cmdtxt(union node *n)
1238 1.55 christos {
1239 1.1 cgd union node *np;
1240 1.1 cgd struct nodelist *lp;
1241 1.31 christos const char *p;
1242 1.1 cgd int i;
1243 1.1 cgd char s[2];
1244 1.1 cgd
1245 1.55 christos if (n == NULL || cmdnleft <= 0)
1246 1.8 jtc return;
1247 1.1 cgd switch (n->type) {
1248 1.1 cgd case NSEMI:
1249 1.1 cgd cmdtxt(n->nbinary.ch1);
1250 1.1 cgd cmdputs("; ");
1251 1.1 cgd cmdtxt(n->nbinary.ch2);
1252 1.1 cgd break;
1253 1.1 cgd case NAND:
1254 1.1 cgd cmdtxt(n->nbinary.ch1);
1255 1.1 cgd cmdputs(" && ");
1256 1.1 cgd cmdtxt(n->nbinary.ch2);
1257 1.1 cgd break;
1258 1.1 cgd case NOR:
1259 1.1 cgd cmdtxt(n->nbinary.ch1);
1260 1.1 cgd cmdputs(" || ");
1261 1.1 cgd cmdtxt(n->nbinary.ch2);
1262 1.1 cgd break;
1263 1.1 cgd case NPIPE:
1264 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1265 1.1 cgd cmdtxt(lp->n);
1266 1.1 cgd if (lp->next)
1267 1.1 cgd cmdputs(" | ");
1268 1.1 cgd }
1269 1.1 cgd break;
1270 1.1 cgd case NSUBSHELL:
1271 1.1 cgd cmdputs("(");
1272 1.1 cgd cmdtxt(n->nredir.n);
1273 1.1 cgd cmdputs(")");
1274 1.1 cgd break;
1275 1.1 cgd case NREDIR:
1276 1.1 cgd case NBACKGND:
1277 1.1 cgd cmdtxt(n->nredir.n);
1278 1.1 cgd break;
1279 1.1 cgd case NIF:
1280 1.1 cgd cmdputs("if ");
1281 1.1 cgd cmdtxt(n->nif.test);
1282 1.1 cgd cmdputs("; then ");
1283 1.1 cgd cmdtxt(n->nif.ifpart);
1284 1.55 christos if (n->nif.elsepart) {
1285 1.55 christos cmdputs("; else ");
1286 1.55 christos cmdtxt(n->nif.elsepart);
1287 1.55 christos }
1288 1.55 christos cmdputs("; fi");
1289 1.1 cgd break;
1290 1.1 cgd case NWHILE:
1291 1.1 cgd cmdputs("while ");
1292 1.1 cgd goto until;
1293 1.1 cgd case NUNTIL:
1294 1.1 cgd cmdputs("until ");
1295 1.1 cgd until:
1296 1.1 cgd cmdtxt(n->nbinary.ch1);
1297 1.1 cgd cmdputs("; do ");
1298 1.1 cgd cmdtxt(n->nbinary.ch2);
1299 1.1 cgd cmdputs("; done");
1300 1.1 cgd break;
1301 1.1 cgd case NFOR:
1302 1.1 cgd cmdputs("for ");
1303 1.1 cgd cmdputs(n->nfor.var);
1304 1.55 christos cmdputs(" in ");
1305 1.55 christos cmdlist(n->nfor.args, 1);
1306 1.55 christos cmdputs("; do ");
1307 1.55 christos cmdtxt(n->nfor.body);
1308 1.55 christos cmdputs("; done");
1309 1.1 cgd break;
1310 1.1 cgd case NCASE:
1311 1.1 cgd cmdputs("case ");
1312 1.1 cgd cmdputs(n->ncase.expr->narg.text);
1313 1.55 christos cmdputs(" in ");
1314 1.55 christos for (np = n->ncase.cases; np; np = np->nclist.next) {
1315 1.55 christos cmdtxt(np->nclist.pattern);
1316 1.55 christos cmdputs(") ");
1317 1.55 christos cmdtxt(np->nclist.body);
1318 1.55 christos cmdputs(";; ");
1319 1.55 christos }
1320 1.55 christos cmdputs("esac");
1321 1.1 cgd break;
1322 1.1 cgd case NDEFUN:
1323 1.1 cgd cmdputs(n->narg.text);
1324 1.55 christos cmdputs("() { ... }");
1325 1.1 cgd break;
1326 1.1 cgd case NCMD:
1327 1.55 christos cmdlist(n->ncmd.args, 1);
1328 1.55 christos cmdlist(n->ncmd.redirect, 0);
1329 1.1 cgd break;
1330 1.1 cgd case NARG:
1331 1.1 cgd cmdputs(n->narg.text);
1332 1.1 cgd break;
1333 1.1 cgd case NTO:
1334 1.1 cgd p = ">"; i = 1; goto redir;
1335 1.46 christos case NCLOBBER:
1336 1.46 christos p = ">|"; i = 1; goto redir;
1337 1.1 cgd case NAPPEND:
1338 1.1 cgd p = ">>"; i = 1; goto redir;
1339 1.1 cgd case NTOFD:
1340 1.1 cgd p = ">&"; i = 1; goto redir;
1341 1.1 cgd case NFROM:
1342 1.1 cgd p = "<"; i = 0; goto redir;
1343 1.1 cgd case NFROMFD:
1344 1.1 cgd p = "<&"; i = 0; goto redir;
1345 1.29 christos case NFROMTO:
1346 1.29 christos p = "<>"; i = 0; goto redir;
1347 1.1 cgd redir:
1348 1.1 cgd if (n->nfile.fd != i) {
1349 1.1 cgd s[0] = n->nfile.fd + '0';
1350 1.1 cgd s[1] = '\0';
1351 1.1 cgd cmdputs(s);
1352 1.1 cgd }
1353 1.1 cgd cmdputs(p);
1354 1.1 cgd if (n->type == NTOFD || n->type == NFROMFD) {
1355 1.1 cgd s[0] = n->ndup.dupfd + '0';
1356 1.1 cgd s[1] = '\0';
1357 1.1 cgd cmdputs(s);
1358 1.1 cgd } else {
1359 1.1 cgd cmdtxt(n->nfile.fname);
1360 1.1 cgd }
1361 1.1 cgd break;
1362 1.1 cgd case NHERE:
1363 1.1 cgd case NXHERE:
1364 1.1 cgd cmdputs("<<...");
1365 1.1 cgd break;
1366 1.1 cgd default:
1367 1.1 cgd cmdputs("???");
1368 1.1 cgd break;
1369 1.1 cgd }
1370 1.1 cgd }
1371 1.1 cgd
1372 1.55 christos STATIC void
1373 1.55 christos cmdlist(union node *np, int sep)
1374 1.55 christos {
1375 1.55 christos for (; np; np = np->narg.next) {
1376 1.55 christos if (!sep)
1377 1.55 christos cmdputs(" ");
1378 1.55 christos cmdtxt(np);
1379 1.55 christos if (sep && np->narg.next)
1380 1.55 christos cmdputs(" ");
1381 1.55 christos }
1382 1.55 christos }
1383 1.1 cgd
1384 1.1 cgd
1385 1.1 cgd STATIC void
1386 1.55 christos cmdputs(const char *s)
1387 1.55 christos {
1388 1.55 christos const char *p, *str = 0;
1389 1.55 christos char c, cc[2] = " ";
1390 1.55 christos char *nextc;
1391 1.55 christos int nleft;
1392 1.1 cgd int subtype = 0;
1393 1.55 christos int quoted = 0;
1394 1.55 christos static char vstype[16][4] = { "", "}", "-", "+", "?", "=",
1395 1.55 christos "#", "##", "%", "%%" };
1396 1.1 cgd
1397 1.1 cgd p = s;
1398 1.55 christos nextc = cmdnextc;
1399 1.55 christos nleft = cmdnleft;
1400 1.55 christos while (nleft > 0 && (c = *p++) != 0) {
1401 1.55 christos switch (c) {
1402 1.55 christos case CTLESC:
1403 1.55 christos c = *p++;
1404 1.55 christos break;
1405 1.55 christos case CTLVAR:
1406 1.1 cgd subtype = *p++;
1407 1.55 christos if ((subtype & VSTYPE) == VSLENGTH)
1408 1.55 christos str = "${#";
1409 1.55 christos else
1410 1.55 christos str = "${";
1411 1.55 christos if (!(subtype & VSQUOTE) != !(quoted & 1)) {
1412 1.55 christos quoted ^= 1;
1413 1.55 christos c = '"';
1414 1.55 christos } else
1415 1.55 christos c = *str++;
1416 1.55 christos break;
1417 1.55 christos case CTLENDVAR:
1418 1.55 christos if (quoted & 1) {
1419 1.55 christos c = '"';
1420 1.55 christos str = "}";
1421 1.55 christos } else
1422 1.55 christos c = '}';
1423 1.55 christos quoted >>= 1;
1424 1.1 cgd subtype = 0;
1425 1.55 christos break;
1426 1.55 christos case CTLBACKQ:
1427 1.55 christos c = '$';
1428 1.55 christos str = "(...)";
1429 1.55 christos break;
1430 1.55 christos case CTLBACKQ+CTLQUOTE:
1431 1.55 christos c = '"';
1432 1.55 christos str = "$(...)\"";
1433 1.55 christos break;
1434 1.55 christos case CTLARI:
1435 1.55 christos c = '$';
1436 1.55 christos str = "((";
1437 1.55 christos break;
1438 1.55 christos case CTLENDARI:
1439 1.55 christos c = ')';
1440 1.55 christos str = ")";
1441 1.55 christos break;
1442 1.55 christos case CTLQUOTEMARK:
1443 1.55 christos quoted ^= 1;
1444 1.55 christos c = '"';
1445 1.55 christos break;
1446 1.55 christos case '=':
1447 1.55 christos if (subtype == 0)
1448 1.55 christos break;
1449 1.55 christos str = vstype[subtype & VSTYPE];
1450 1.55 christos if (subtype & VSNUL)
1451 1.55 christos c = ':';
1452 1.55 christos else
1453 1.55 christos c = *str++;
1454 1.55 christos if (c != '}')
1455 1.55 christos quoted <<= 1;
1456 1.55 christos break;
1457 1.55 christos case '\'':
1458 1.55 christos case '\\':
1459 1.55 christos case '"':
1460 1.55 christos case '$':
1461 1.55 christos /* These can only happen inside quotes */
1462 1.55 christos cc[0] = c;
1463 1.55 christos str = cc;
1464 1.55 christos c = '\\';
1465 1.55 christos break;
1466 1.55 christos default:
1467 1.1 cgd break;
1468 1.1 cgd }
1469 1.55 christos do {
1470 1.55 christos *nextc++ = c;
1471 1.55 christos } while (--nleft > 0 && str && (c = *str++));
1472 1.55 christos str = 0;
1473 1.55 christos }
1474 1.55 christos if ((quoted & 1) && nleft) {
1475 1.55 christos *nextc++ = '"';
1476 1.55 christos nleft--;
1477 1.1 cgd }
1478 1.55 christos cmdnleft = nleft;
1479 1.55 christos cmdnextc = nextc;
1480 1.1 cgd }
1481