jobs.c revision 1.4 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1991 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Kenneth Almquist.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd static char sccsid[] = "@(#)jobs.c 5.1 (Berkeley) 3/7/91";
39 1.4 dpassage static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/sh/jobs.c,v 1.4 1993/04/26 06:09:17 dpassage Exp $";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #include "shell.h"
43 1.1 cgd #if JOBS
44 1.1 cgd #include "sgtty.h"
45 1.1 cgd #undef CEOF /* syntax.h redefines this */
46 1.1 cgd #endif
47 1.1 cgd #include "main.h"
48 1.1 cgd #include "parser.h"
49 1.1 cgd #include "nodes.h"
50 1.1 cgd #include "jobs.h"
51 1.1 cgd #include "options.h"
52 1.1 cgd #include "trap.h"
53 1.1 cgd #include "signames.h"
54 1.1 cgd #include "syntax.h"
55 1.1 cgd #include "input.h"
56 1.1 cgd #include "output.h"
57 1.1 cgd #include "memalloc.h"
58 1.1 cgd #include "error.h"
59 1.1 cgd #include "mystring.h"
60 1.1 cgd #include <fcntl.h>
61 1.1 cgd #include <signal.h>
62 1.1 cgd #include <errno.h>
63 1.1 cgd #ifdef BSD
64 1.1 cgd #include <sys/types.h>
65 1.1 cgd #include <sys/wait.h>
66 1.1 cgd #include <sys/time.h>
67 1.1 cgd #include <sys/resource.h>
68 1.1 cgd #endif
69 1.1 cgd
70 1.1 cgd
71 1.1 cgd
72 1.1 cgd struct job *jobtab; /* array of jobs */
73 1.1 cgd int njobs; /* size of array */
74 1.1 cgd MKINIT short backgndpid = -1; /* pid of last background process */
75 1.1 cgd #if JOBS
76 1.1 cgd int initialpgrp; /* pgrp of shell on invocation */
77 1.1 cgd short curjob; /* current job */
78 1.1 cgd #endif
79 1.1 cgd
80 1.1 cgd #ifdef __STDC__
81 1.1 cgd STATIC void restartjob(struct job *);
82 1.1 cgd STATIC struct job *getjob(char *);
83 1.1 cgd STATIC void freejob(struct job *);
84 1.1 cgd STATIC int procrunning(int);
85 1.1 cgd STATIC int dowait(int, struct job *);
86 1.1 cgd STATIC int waitproc(int, int *);
87 1.1 cgd STATIC char *commandtext(union node *);
88 1.1 cgd #else
89 1.1 cgd STATIC void restartjob();
90 1.1 cgd STATIC struct job *getjob();
91 1.1 cgd STATIC void freejob();
92 1.1 cgd STATIC int procrunning();
93 1.1 cgd STATIC int dowait();
94 1.1 cgd STATIC int waitproc();
95 1.1 cgd STATIC char *commandtext();
96 1.1 cgd #endif
97 1.1 cgd
98 1.1 cgd
99 1.1 cgd
100 1.1 cgd #if JOBS
101 1.1 cgd /*
102 1.1 cgd * Turn job control on and off.
103 1.1 cgd *
104 1.1 cgd * Note: This code assumes that the third arg to ioctl is a character
105 1.1 cgd * pointer, which is true on Berkeley systems but not System V. Since
106 1.1 cgd * System V doesn't have job control yet, this isn't a problem now.
107 1.1 cgd */
108 1.1 cgd
109 1.1 cgd MKINIT int jobctl;
110 1.1 cgd
111 1.1 cgd void
112 1.1 cgd setjobctl(on) {
113 1.1 cgd int ldisc;
114 1.1 cgd
115 1.1 cgd if (on == jobctl || rootshell == 0)
116 1.1 cgd return;
117 1.1 cgd if (on) {
118 1.1 cgd do { /* while we are in the background */
119 1.1 cgd if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) {
120 1.1 cgd out2str("ash: can't access tty; job control turned off\n");
121 1.1 cgd jflag = 0;
122 1.1 cgd return;
123 1.1 cgd }
124 1.1 cgd if (initialpgrp == -1)
125 1.1 cgd initialpgrp = getpgrp(0);
126 1.1 cgd else if (initialpgrp != getpgrp(0)) {
127 1.1 cgd killpg(initialpgrp, SIGTTIN);
128 1.1 cgd continue;
129 1.1 cgd }
130 1.1 cgd } while (0);
131 1.1 cgd if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) {
132 1.1 cgd out2str("ash: need new tty driver to run job control; job control turned off\n");
133 1.1 cgd jflag = 0;
134 1.1 cgd return;
135 1.1 cgd }
136 1.1 cgd setsignal(SIGTSTP);
137 1.1 cgd setsignal(SIGTTOU);
138 1.1 cgd setpgrp(0, rootpid);
139 1.1 cgd ioctl(2, TIOCSPGRP, (char *)&rootpid);
140 1.1 cgd } else { /* turning job control off */
141 1.1 cgd setpgrp(0, initialpgrp);
142 1.1 cgd ioctl(2, TIOCSPGRP, (char *)&initialpgrp);
143 1.1 cgd setsignal(SIGTSTP);
144 1.1 cgd setsignal(SIGTTOU);
145 1.1 cgd }
146 1.1 cgd jobctl = on;
147 1.1 cgd }
148 1.1 cgd #endif
149 1.1 cgd
150 1.1 cgd
151 1.1 cgd #ifdef mkinit
152 1.1 cgd
153 1.1 cgd SHELLPROC {
154 1.1 cgd backgndpid = -1;
155 1.1 cgd #if JOBS
156 1.1 cgd jobctl = 0;
157 1.1 cgd #endif
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd #endif
161 1.1 cgd
162 1.1 cgd
163 1.1 cgd
164 1.1 cgd #if JOBS
165 1.1 cgd fgcmd(argc, argv) char **argv; {
166 1.1 cgd struct job *jp;
167 1.1 cgd int pgrp;
168 1.1 cgd int status;
169 1.1 cgd
170 1.1 cgd jp = getjob(argv[1]);
171 1.1 cgd if (jp->jobctl == 0)
172 1.1 cgd error("job not created under job control");
173 1.1 cgd pgrp = jp->ps[0].pid;
174 1.1 cgd ioctl(2, TIOCSPGRP, (char *)&pgrp);
175 1.1 cgd restartjob(jp);
176 1.1 cgd INTOFF;
177 1.1 cgd status = waitforjob(jp);
178 1.1 cgd INTON;
179 1.1 cgd return status;
180 1.1 cgd }
181 1.1 cgd
182 1.1 cgd
183 1.1 cgd bgcmd(argc, argv) char **argv; {
184 1.1 cgd struct job *jp;
185 1.1 cgd
186 1.1 cgd do {
187 1.1 cgd jp = getjob(*++argv);
188 1.1 cgd if (jp->jobctl == 0)
189 1.1 cgd error("job not created under job control");
190 1.1 cgd restartjob(jp);
191 1.1 cgd } while (--argc > 1);
192 1.1 cgd return 0;
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd
196 1.1 cgd STATIC void
197 1.1 cgd restartjob(jp)
198 1.1 cgd struct job *jp;
199 1.1 cgd {
200 1.1 cgd struct procstat *ps;
201 1.1 cgd int i;
202 1.1 cgd
203 1.1 cgd if (jp->state == JOBDONE)
204 1.1 cgd return;
205 1.1 cgd INTOFF;
206 1.1 cgd killpg(jp->ps[0].pid, SIGCONT);
207 1.1 cgd for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
208 1.1 cgd if ((ps->status & 0377) == 0177) {
209 1.1 cgd ps->status = -1;
210 1.1 cgd jp->state = 0;
211 1.1 cgd }
212 1.1 cgd }
213 1.1 cgd INTON;
214 1.1 cgd }
215 1.1 cgd #endif
216 1.1 cgd
217 1.1 cgd
218 1.1 cgd int
219 1.1 cgd jobscmd(argc, argv) char **argv; {
220 1.1 cgd showjobs(0);
221 1.1 cgd return 0;
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd
225 1.1 cgd /*
226 1.1 cgd * Print a list of jobs. If "change" is nonzero, only print jobs whose
227 1.1 cgd * statuses have changed since the last call to showjobs.
228 1.1 cgd *
229 1.1 cgd * If the shell is interrupted in the process of creating a job, the
230 1.1 cgd * result may be a job structure containing zero processes. Such structures
231 1.1 cgd * will be freed here.
232 1.1 cgd */
233 1.1 cgd
234 1.1 cgd void
235 1.1 cgd showjobs(change) {
236 1.1 cgd int jobno;
237 1.1 cgd int procno;
238 1.1 cgd int i;
239 1.1 cgd struct job *jp;
240 1.1 cgd struct procstat *ps;
241 1.1 cgd int col;
242 1.1 cgd char s[64];
243 1.1 cgd
244 1.1 cgd TRACE(("showjobs(%d) called\n", change));
245 1.1 cgd while (dowait(0, (struct job *)NULL) > 0);
246 1.1 cgd for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
247 1.1 cgd if (! jp->used)
248 1.1 cgd continue;
249 1.1 cgd if (jp->nprocs == 0) {
250 1.1 cgd freejob(jp);
251 1.1 cgd continue;
252 1.1 cgd }
253 1.1 cgd if (change && ! jp->changed)
254 1.1 cgd continue;
255 1.1 cgd procno = jp->nprocs;
256 1.1 cgd for (ps = jp->ps ; ; ps++) { /* for each process */
257 1.1 cgd if (ps == jp->ps)
258 1.1 cgd fmtstr(s, 64, "[%d] %d ", jobno, ps->pid);
259 1.1 cgd else
260 1.1 cgd fmtstr(s, 64, " %d ", ps->pid);
261 1.1 cgd out1str(s);
262 1.1 cgd col = strlen(s);
263 1.1 cgd s[0] = '\0';
264 1.1 cgd if (ps->status == -1) {
265 1.1 cgd /* don't print anything */
266 1.1 cgd } else if ((ps->status & 0xFF) == 0) {
267 1.1 cgd fmtstr(s, 64, "Exit %d", ps->status >> 8);
268 1.1 cgd } else {
269 1.1 cgd i = ps->status;
270 1.1 cgd #if JOBS
271 1.1 cgd if ((i & 0xFF) == 0177)
272 1.1 cgd i >>= 8;
273 1.1 cgd #endif
274 1.1 cgd if ((i & 0x7F) <= MAXSIG && sigmesg[i & 0x7F])
275 1.1 cgd scopy(sigmesg[i & 0x7F], s);
276 1.1 cgd else
277 1.1 cgd fmtstr(s, 64, "Signal %d", i & 0x7F);
278 1.1 cgd if (i & 0x80)
279 1.1 cgd strcat(s, " (core dumped)");
280 1.1 cgd }
281 1.1 cgd out1str(s);
282 1.1 cgd col += strlen(s);
283 1.1 cgd do {
284 1.1 cgd out1c(' ');
285 1.1 cgd col++;
286 1.1 cgd } while (col < 30);
287 1.1 cgd out1str(ps->cmd);
288 1.1 cgd out1c('\n');
289 1.1 cgd if (--procno <= 0)
290 1.1 cgd break;
291 1.1 cgd }
292 1.1 cgd jp->changed = 0;
293 1.1 cgd if (jp->state == JOBDONE) {
294 1.1 cgd freejob(jp);
295 1.1 cgd }
296 1.1 cgd }
297 1.1 cgd }
298 1.1 cgd
299 1.1 cgd
300 1.1 cgd /*
301 1.1 cgd * Mark a job structure as unused.
302 1.1 cgd */
303 1.1 cgd
304 1.1 cgd STATIC void
305 1.1 cgd freejob(jp)
306 1.1 cgd struct job *jp;
307 1.1 cgd {
308 1.1 cgd struct procstat *ps;
309 1.1 cgd int i;
310 1.1 cgd
311 1.1 cgd INTOFF;
312 1.1 cgd for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
313 1.1 cgd if (ps->cmd != nullstr)
314 1.1 cgd ckfree(ps->cmd);
315 1.1 cgd }
316 1.1 cgd if (jp->ps != &jp->ps0)
317 1.1 cgd ckfree(jp->ps);
318 1.1 cgd jp->used = 0;
319 1.1 cgd #if JOBS
320 1.1 cgd if (curjob == jp - jobtab + 1)
321 1.1 cgd curjob = 0;
322 1.1 cgd #endif
323 1.1 cgd INTON;
324 1.1 cgd }
325 1.1 cgd
326 1.1 cgd
327 1.1 cgd
328 1.1 cgd int
329 1.1 cgd waitcmd(argc, argv) char **argv; {
330 1.1 cgd struct job *job;
331 1.1 cgd int status;
332 1.1 cgd struct job *jp;
333 1.1 cgd
334 1.1 cgd if (argc > 1) {
335 1.1 cgd job = getjob(argv[1]);
336 1.1 cgd } else {
337 1.1 cgd job = NULL;
338 1.1 cgd }
339 1.1 cgd for (;;) { /* loop until process terminated or stopped */
340 1.1 cgd if (job != NULL) {
341 1.1 cgd if (job->state) {
342 1.1 cgd status = job->ps[job->nprocs - 1].status;
343 1.1 cgd if ((status & 0xFF) == 0)
344 1.1 cgd status = status >> 8 & 0xFF;
345 1.1 cgd #if JOBS
346 1.1 cgd else if ((status & 0xFF) == 0177)
347 1.1 cgd status = (status >> 8 & 0x7F) + 128;
348 1.1 cgd #endif
349 1.1 cgd else
350 1.1 cgd status = (status & 0x7F) + 128;
351 1.1 cgd if (! iflag)
352 1.1 cgd freejob(job);
353 1.1 cgd return status;
354 1.1 cgd }
355 1.1 cgd } else {
356 1.1 cgd for (jp = jobtab ; ; jp++) {
357 1.1 cgd if (jp >= jobtab + njobs) { /* no running procs */
358 1.1 cgd return 0;
359 1.1 cgd }
360 1.1 cgd if (jp->used && jp->state == 0)
361 1.1 cgd break;
362 1.1 cgd }
363 1.1 cgd }
364 1.1 cgd dowait(1, (struct job *)NULL);
365 1.1 cgd }
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd
369 1.1 cgd
370 1.1 cgd jobidcmd(argc, argv) char **argv; {
371 1.1 cgd struct job *jp;
372 1.1 cgd int i;
373 1.1 cgd
374 1.1 cgd jp = getjob(argv[1]);
375 1.1 cgd for (i = 0 ; i < jp->nprocs ; ) {
376 1.1 cgd out1fmt("%d", jp->ps[i].pid);
377 1.1 cgd out1c(++i < jp->nprocs? ' ' : '\n');
378 1.1 cgd }
379 1.1 cgd return 0;
380 1.1 cgd }
381 1.1 cgd
382 1.1 cgd
383 1.1 cgd
384 1.1 cgd /*
385 1.1 cgd * Convert a job name to a job structure.
386 1.1 cgd */
387 1.1 cgd
388 1.1 cgd STATIC struct job *
389 1.1 cgd getjob(name)
390 1.1 cgd char *name;
391 1.1 cgd {
392 1.1 cgd int jobno;
393 1.1 cgd register struct job *jp;
394 1.1 cgd int pid;
395 1.1 cgd int i;
396 1.1 cgd
397 1.1 cgd if (name == NULL) {
398 1.1 cgd #if JOBS
399 1.1 cgd currentjob:
400 1.1 cgd if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0)
401 1.1 cgd error("No current job");
402 1.1 cgd return &jobtab[jobno - 1];
403 1.1 cgd #else
404 1.1 cgd error("No current job");
405 1.1 cgd #endif
406 1.1 cgd } else if (name[0] == '%') {
407 1.1 cgd if (is_digit(name[1])) {
408 1.1 cgd jobno = number(name + 1);
409 1.1 cgd if (jobno > 0 && jobno <= njobs
410 1.1 cgd && jobtab[jobno - 1].used != 0)
411 1.1 cgd return &jobtab[jobno - 1];
412 1.1 cgd #if JOBS
413 1.1 cgd } else if (name[1] == '%' && name[2] == '\0') {
414 1.1 cgd goto currentjob;
415 1.1 cgd #endif
416 1.1 cgd } else {
417 1.1 cgd register struct job *found = NULL;
418 1.1 cgd for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
419 1.1 cgd if (jp->used && jp->nprocs > 0
420 1.1 cgd && prefix(name + 1, jp->ps[0].cmd)) {
421 1.1 cgd if (found)
422 1.1 cgd error("%s: ambiguous", name);
423 1.1 cgd found = jp;
424 1.1 cgd }
425 1.1 cgd }
426 1.1 cgd if (found)
427 1.1 cgd return found;
428 1.1 cgd }
429 1.1 cgd } else if (is_number(name)) {
430 1.1 cgd pid = number(name);
431 1.1 cgd for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
432 1.1 cgd if (jp->used && jp->nprocs > 0
433 1.1 cgd && jp->ps[jp->nprocs - 1].pid == pid)
434 1.1 cgd return jp;
435 1.1 cgd }
436 1.1 cgd }
437 1.1 cgd error("No such job: %s", name);
438 1.1 cgd }
439 1.1 cgd
440 1.1 cgd
441 1.1 cgd
442 1.1 cgd /*
443 1.1 cgd * Return a new job structure,
444 1.1 cgd */
445 1.1 cgd
446 1.1 cgd struct job *
447 1.1 cgd makejob(node, nprocs)
448 1.1 cgd union node *node;
449 1.1 cgd {
450 1.1 cgd int i;
451 1.1 cgd struct job *jp;
452 1.1 cgd
453 1.1 cgd for (i = njobs, jp = jobtab ; ; jp++) {
454 1.1 cgd if (--i < 0) {
455 1.1 cgd INTOFF;
456 1.1 cgd if (njobs == 0) {
457 1.1 cgd jobtab = ckmalloc(4 * sizeof jobtab[0]);
458 1.1 cgd } else {
459 1.1 cgd jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
460 1.1 cgd bcopy(jobtab, jp, njobs * sizeof jp[0]);
461 1.1 cgd ckfree(jobtab);
462 1.1 cgd jobtab = jp;
463 1.1 cgd }
464 1.1 cgd jp = jobtab + njobs;
465 1.1 cgd for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0);
466 1.1 cgd INTON;
467 1.1 cgd break;
468 1.1 cgd }
469 1.1 cgd if (jp->used == 0)
470 1.1 cgd break;
471 1.1 cgd }
472 1.1 cgd INTOFF;
473 1.1 cgd jp->state = 0;
474 1.1 cgd jp->used = 1;
475 1.1 cgd jp->changed = 0;
476 1.1 cgd jp->nprocs = 0;
477 1.1 cgd #if JOBS
478 1.1 cgd jp->jobctl = jobctl;
479 1.1 cgd #endif
480 1.1 cgd if (nprocs > 1) {
481 1.1 cgd jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
482 1.1 cgd } else {
483 1.1 cgd jp->ps = &jp->ps0;
484 1.1 cgd }
485 1.1 cgd INTON;
486 1.1 cgd TRACE(("makejob(0x%x, %d) returns %%%d\n", (int)node, nprocs, jp - jobtab + 1));
487 1.1 cgd return jp;
488 1.1 cgd }
489 1.1 cgd
490 1.1 cgd
491 1.1 cgd /*
492 1.1 cgd * Fork of a subshell. If we are doing job control, give the subshell its
493 1.1 cgd * own process group. Jp is a job structure that the job is to be added to.
494 1.1 cgd * N is the command that will be evaluated by the child. Both jp and n may
495 1.1 cgd * be NULL. The mode parameter can be one of the following:
496 1.1 cgd * FORK_FG - Fork off a foreground process.
497 1.1 cgd * FORK_BG - Fork off a background process.
498 1.1 cgd * FORK_NOJOB - Like FORK_FG, but don't give the process its own
499 1.1 cgd * process group even if job control is on.
500 1.1 cgd *
501 1.1 cgd * When job control is turned off, background processes have their standard
502 1.1 cgd * input redirected to /dev/null (except for the second and later processes
503 1.1 cgd * in a pipeline).
504 1.1 cgd */
505 1.1 cgd
506 1.1 cgd int
507 1.1 cgd forkshell(jp, n, mode)
508 1.1 cgd union node *n;
509 1.1 cgd struct job *jp;
510 1.1 cgd {
511 1.1 cgd int pid;
512 1.1 cgd int pgrp;
513 1.1 cgd
514 1.1 cgd TRACE(("forkshell(%%%d, 0x%x, %d) called\n", jp - jobtab, (int)n, mode));
515 1.1 cgd INTOFF;
516 1.1 cgd pid = fork();
517 1.1 cgd if (pid == -1) {
518 1.1 cgd TRACE(("Fork failed, errno=%d\n", errno));
519 1.1 cgd INTON;
520 1.1 cgd error("Cannot fork");
521 1.1 cgd }
522 1.1 cgd if (pid == 0) {
523 1.1 cgd struct job *p;
524 1.1 cgd int wasroot;
525 1.1 cgd int i;
526 1.1 cgd
527 1.1 cgd TRACE(("Child shell %d\n", getpid()));
528 1.1 cgd wasroot = rootshell;
529 1.1 cgd rootshell = 0;
530 1.1 cgd for (i = njobs, p = jobtab ; --i >= 0 ; p++)
531 1.1 cgd if (p->used)
532 1.1 cgd freejob(p);
533 1.1 cgd closescript();
534 1.1 cgd INTON;
535 1.1 cgd clear_traps();
536 1.1 cgd #if JOBS
537 1.1 cgd jobctl = 0; /* do job control only in root shell */
538 1.1 cgd if (wasroot && mode != FORK_NOJOB && jflag) {
539 1.1 cgd if (jp == NULL || jp->nprocs == 0)
540 1.1 cgd pgrp = getpid();
541 1.1 cgd else
542 1.1 cgd pgrp = jp->ps[0].pid;
543 1.1 cgd setpgrp(0, pgrp);
544 1.1 cgd if (mode == FORK_FG) {
545 1.1 cgd /*** this causes superfluous TIOCSPGRPS ***/
546 1.1 cgd if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0)
547 1.1 cgd error("TIOCSPGRP failed, errno=%d\n", errno);
548 1.1 cgd }
549 1.1 cgd setsignal(SIGTSTP);
550 1.1 cgd setsignal(SIGTTOU);
551 1.1 cgd } else if (mode == FORK_BG) {
552 1.1 cgd ignoresig(SIGINT);
553 1.1 cgd ignoresig(SIGQUIT);
554 1.1 cgd if (jp == NULL || jp->nprocs == 0) {
555 1.1 cgd close(0);
556 1.1 cgd if (open("/dev/null", O_RDONLY) != 0)
557 1.1 cgd error("Can't open /dev/null");
558 1.1 cgd }
559 1.1 cgd }
560 1.1 cgd #else
561 1.1 cgd if (mode == FORK_BG) {
562 1.1 cgd ignoresig(SIGINT);
563 1.1 cgd ignoresig(SIGQUIT);
564 1.1 cgd if (jp == NULL || jp->nprocs == 0) {
565 1.1 cgd close(0);
566 1.1 cgd if (open("/dev/null", O_RDONLY) != 0)
567 1.1 cgd error("Can't open /dev/null");
568 1.1 cgd }
569 1.1 cgd }
570 1.1 cgd #endif
571 1.1 cgd if (wasroot && iflag) {
572 1.1 cgd setsignal(SIGINT);
573 1.1 cgd setsignal(SIGQUIT);
574 1.1 cgd setsignal(SIGTERM);
575 1.1 cgd }
576 1.1 cgd return pid;
577 1.1 cgd }
578 1.1 cgd if (rootshell && mode != FORK_NOJOB && jflag) {
579 1.1 cgd if (jp == NULL || jp->nprocs == 0)
580 1.1 cgd pgrp = pid;
581 1.1 cgd else
582 1.1 cgd pgrp = jp->ps[0].pid;
583 1.1 cgd setpgrp(pid, pgrp);
584 1.1 cgd }
585 1.1 cgd if (mode == FORK_BG)
586 1.1 cgd backgndpid = pid; /* set $! */
587 1.1 cgd if (jp) {
588 1.1 cgd struct procstat *ps = &jp->ps[jp->nprocs++];
589 1.1 cgd ps->pid = pid;
590 1.1 cgd ps->status = -1;
591 1.1 cgd ps->cmd = nullstr;
592 1.1 cgd if (iflag && rootshell && n)
593 1.1 cgd ps->cmd = commandtext(n);
594 1.1 cgd }
595 1.1 cgd INTON;
596 1.1 cgd TRACE(("In parent shell: child = %d\n", pid));
597 1.1 cgd return pid;
598 1.1 cgd }
599 1.1 cgd
600 1.1 cgd
601 1.1 cgd
602 1.1 cgd /*
603 1.1 cgd * Wait for job to finish.
604 1.1 cgd *
605 1.1 cgd * Under job control we have the problem that while a child process is
606 1.1 cgd * running interrupts generated by the user are sent to the child but not
607 1.1 cgd * to the shell. This means that an infinite loop started by an inter-
608 1.1 cgd * active user may be hard to kill. With job control turned off, an
609 1.1 cgd * interactive user may place an interactive program inside a loop. If
610 1.1 cgd * the interactive program catches interrupts, the user doesn't want
611 1.1 cgd * these interrupts to also abort the loop. The approach we take here
612 1.1 cgd * is to have the shell ignore interrupt signals while waiting for a
613 1.1 cgd * forground process to terminate, and then send itself an interrupt
614 1.1 cgd * signal if the child process was terminated by an interrupt signal.
615 1.1 cgd * Unfortunately, some programs want to do a bit of cleanup and then
616 1.1 cgd * exit on interrupt; unless these processes terminate themselves by
617 1.1 cgd * sending a signal to themselves (instead of calling exit) they will
618 1.1 cgd * confuse this approach.
619 1.1 cgd */
620 1.1 cgd
621 1.1 cgd int
622 1.1 cgd waitforjob(jp)
623 1.1 cgd register struct job *jp;
624 1.1 cgd {
625 1.1 cgd #if JOBS
626 1.1 cgd int mypgrp = getpgrp(0);
627 1.1 cgd #endif
628 1.1 cgd int status;
629 1.1 cgd int st;
630 1.1 cgd
631 1.1 cgd INTOFF;
632 1.1 cgd TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1));
633 1.1 cgd while (jp->state == 0) {
634 1.1 cgd dowait(1, jp);
635 1.1 cgd }
636 1.1 cgd #if JOBS
637 1.1 cgd if (jp->jobctl) {
638 1.1 cgd if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0)
639 1.1 cgd error("TIOCSPGRP failed, errno=%d\n", errno);
640 1.1 cgd }
641 1.1 cgd if (jp->state == JOBSTOPPED)
642 1.1 cgd curjob = jp - jobtab + 1;
643 1.1 cgd #endif
644 1.1 cgd status = jp->ps[jp->nprocs - 1].status;
645 1.1 cgd /* convert to 8 bits */
646 1.1 cgd if ((status & 0xFF) == 0)
647 1.1 cgd st = status >> 8 & 0xFF;
648 1.1 cgd #if JOBS
649 1.1 cgd else if ((status & 0xFF) == 0177)
650 1.1 cgd st = (status >> 8 & 0x7F) + 128;
651 1.1 cgd #endif
652 1.1 cgd else
653 1.1 cgd st = (status & 0x7F) + 128;
654 1.1 cgd if (! JOBS || jp->state == JOBDONE)
655 1.1 cgd freejob(jp);
656 1.1 cgd CLEAR_PENDING_INT;
657 1.1 cgd if ((status & 0x7F) == SIGINT)
658 1.1 cgd kill(getpid(), SIGINT);
659 1.1 cgd INTON;
660 1.1 cgd return st;
661 1.1 cgd }
662 1.1 cgd
663 1.1 cgd
664 1.1 cgd
665 1.1 cgd /*
666 1.1 cgd * Wait for a process to terminate.
667 1.1 cgd */
668 1.1 cgd
669 1.1 cgd STATIC int
670 1.1 cgd dowait(block, job)
671 1.1 cgd struct job *job;
672 1.1 cgd {
673 1.1 cgd int pid;
674 1.1 cgd int status;
675 1.1 cgd struct procstat *sp;
676 1.1 cgd struct job *jp;
677 1.1 cgd struct job *thisjob;
678 1.1 cgd int done;
679 1.1 cgd int stopped;
680 1.1 cgd int core;
681 1.1 cgd
682 1.1 cgd TRACE(("dowait(%d) called\n", block));
683 1.1 cgd do {
684 1.1 cgd pid = waitproc(block, &status);
685 1.1 cgd TRACE(("wait returns %d, status=%d\n", pid, status));
686 1.1 cgd } while (pid == -1 && errno == EINTR);
687 1.1 cgd if (pid <= 0)
688 1.1 cgd return pid;
689 1.1 cgd INTOFF;
690 1.1 cgd thisjob = NULL;
691 1.1 cgd for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
692 1.1 cgd if (jp->used) {
693 1.1 cgd done = 1;
694 1.1 cgd stopped = 1;
695 1.1 cgd for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
696 1.1 cgd if (sp->pid == -1)
697 1.1 cgd continue;
698 1.1 cgd if (sp->pid == pid) {
699 1.1 cgd TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status));
700 1.1 cgd sp->status = status;
701 1.1 cgd thisjob = jp;
702 1.1 cgd }
703 1.1 cgd if (sp->status == -1)
704 1.1 cgd stopped = 0;
705 1.1 cgd else if ((sp->status & 0377) == 0177)
706 1.1 cgd done = 0;
707 1.1 cgd }
708 1.1 cgd if (stopped) { /* stopped or done */
709 1.1 cgd int state = done? JOBDONE : JOBSTOPPED;
710 1.1 cgd if (jp->state != state) {
711 1.1 cgd TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
712 1.1 cgd jp->state = state;
713 1.1 cgd #if JOBS
714 1.1 cgd if (done && curjob == jp - jobtab + 1)
715 1.1 cgd curjob = 0; /* no current job */
716 1.1 cgd #endif
717 1.1 cgd }
718 1.1 cgd }
719 1.1 cgd }
720 1.1 cgd }
721 1.1 cgd INTON;
722 1.1 cgd if (! rootshell || ! iflag || (job && thisjob == job)) {
723 1.1 cgd #if JOBS
724 1.1 cgd if ((status & 0xFF) == 0177)
725 1.1 cgd status >>= 8;
726 1.1 cgd #endif
727 1.1 cgd core = status & 0x80;
728 1.1 cgd status &= 0x7F;
729 1.1 cgd if (status != 0 && status != SIGINT && status != SIGPIPE) {
730 1.1 cgd if (thisjob != job)
731 1.1 cgd outfmt(out2, "%d: ", pid);
732 1.1 cgd #if JOBS
733 1.1 cgd if (status == SIGTSTP && rootshell && iflag)
734 1.1 cgd outfmt(out2, "%%%d ", job - jobtab + 1);
735 1.1 cgd #endif
736 1.1 cgd if (status <= MAXSIG && sigmesg[status])
737 1.1 cgd out2str(sigmesg[status]);
738 1.1 cgd else
739 1.1 cgd outfmt(out2, "Signal %d", status);
740 1.1 cgd if (core)
741 1.1 cgd out2str(" - core dumped");
742 1.1 cgd out2c('\n');
743 1.1 cgd flushout(&errout);
744 1.1 cgd } else {
745 1.1 cgd TRACE(("Not printing status: status=%d\n", status));
746 1.1 cgd }
747 1.1 cgd } else {
748 1.1 cgd TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job));
749 1.1 cgd if (thisjob)
750 1.1 cgd thisjob->changed = 1;
751 1.1 cgd }
752 1.1 cgd return pid;
753 1.1 cgd }
754 1.1 cgd
755 1.1 cgd
756 1.1 cgd
757 1.1 cgd /*
758 1.1 cgd * Do a wait system call. If job control is compiled in, we accept
759 1.1 cgd * stopped processes. If block is zero, we return a value of zero
760 1.1 cgd * rather than blocking.
761 1.1 cgd *
762 1.1 cgd * System V doesn't have a non-blocking wait system call. It does
763 1.1 cgd * have a SIGCLD signal that is sent to a process when one of it's
764 1.1 cgd * children dies. The obvious way to use SIGCLD would be to install
765 1.1 cgd * a handler for SIGCLD which simply bumped a counter when a SIGCLD
766 1.1 cgd * was received, and have waitproc bump another counter when it got
767 1.1 cgd * the status of a process. Waitproc would then know that a wait
768 1.1 cgd * system call would not block if the two counters were different.
769 1.1 cgd * This approach doesn't work because if a process has children that
770 1.1 cgd * have not been waited for, System V will send it a SIGCLD when it
771 1.1 cgd * installs a signal handler for SIGCLD. What this means is that when
772 1.1 cgd * a child exits, the shell will be sent SIGCLD signals continuously
773 1.1 cgd * until is runs out of stack space, unless it does a wait call before
774 1.1 cgd * restoring the signal handler. The code below takes advantage of
775 1.1 cgd * this (mis)feature by installing a signal handler for SIGCLD and
776 1.1 cgd * then checking to see whether it was called. If there are any
777 1.1 cgd * children to be waited for, it will be.
778 1.1 cgd *
779 1.1 cgd * If neither SYSV nor BSD is defined, we don't implement nonblocking
780 1.1 cgd * waits at all. In this case, the user will not be informed when
781 1.1 cgd * a background process until the next time she runs a real program
782 1.1 cgd * (as opposed to running a builtin command or just typing return),
783 1.1 cgd * and the jobs command may give out of date information.
784 1.1 cgd */
785 1.1 cgd
786 1.1 cgd #ifdef SYSV
787 1.1 cgd STATIC int gotsigchild;
788 1.1 cgd
789 1.1 cgd STATIC int onsigchild() {
790 1.1 cgd gotsigchild = 1;
791 1.1 cgd }
792 1.1 cgd #endif
793 1.1 cgd
794 1.1 cgd
795 1.1 cgd STATIC int
796 1.1 cgd waitproc(block, status)
797 1.1 cgd int *status;
798 1.1 cgd {
799 1.1 cgd #ifdef BSD
800 1.1 cgd int flags;
801 1.1 cgd
802 1.1 cgd #if JOBS
803 1.1 cgd flags = WUNTRACED;
804 1.1 cgd #else
805 1.1 cgd flags = 0;
806 1.1 cgd #endif
807 1.1 cgd if (block == 0)
808 1.1 cgd flags |= WNOHANG;
809 1.4 dpassage return wait3((int *)status, flags, (struct rusage *)NULL);
810 1.1 cgd #else
811 1.1 cgd #ifdef SYSV
812 1.1 cgd int (*save)();
813 1.1 cgd
814 1.1 cgd if (block == 0) {
815 1.1 cgd gotsigchild = 0;
816 1.1 cgd save = signal(SIGCLD, onsigchild);
817 1.1 cgd signal(SIGCLD, save);
818 1.1 cgd if (gotsigchild == 0)
819 1.1 cgd return 0;
820 1.1 cgd }
821 1.1 cgd return wait(status);
822 1.1 cgd #else
823 1.1 cgd if (block == 0)
824 1.1 cgd return 0;
825 1.1 cgd return wait(status);
826 1.1 cgd #endif
827 1.1 cgd #endif
828 1.1 cgd }
829 1.1 cgd
830 1.1 cgd
831 1.1 cgd
832 1.1 cgd /*
833 1.1 cgd * Return a string identifying a command (to be printed by the
834 1.1 cgd * jobs command.
835 1.1 cgd */
836 1.1 cgd
837 1.1 cgd STATIC char *cmdnextc;
838 1.1 cgd STATIC int cmdnleft;
839 1.1 cgd STATIC void cmdtxt(), cmdputs();
840 1.1 cgd
841 1.1 cgd STATIC char *
842 1.1 cgd commandtext(n)
843 1.1 cgd union node *n;
844 1.1 cgd {
845 1.1 cgd char *name;
846 1.1 cgd
847 1.1 cgd cmdnextc = name = ckmalloc(50);
848 1.1 cgd cmdnleft = 50 - 4;
849 1.1 cgd cmdtxt(n);
850 1.1 cgd *cmdnextc = '\0';
851 1.1 cgd return name;
852 1.1 cgd }
853 1.1 cgd
854 1.1 cgd
855 1.1 cgd STATIC void
856 1.1 cgd cmdtxt(n)
857 1.1 cgd union node *n;
858 1.1 cgd {
859 1.1 cgd union node *np;
860 1.1 cgd struct nodelist *lp;
861 1.1 cgd char *p;
862 1.1 cgd int i;
863 1.1 cgd char s[2];
864 1.1 cgd
865 1.1 cgd switch (n->type) {
866 1.1 cgd case NSEMI:
867 1.1 cgd cmdtxt(n->nbinary.ch1);
868 1.1 cgd cmdputs("; ");
869 1.1 cgd cmdtxt(n->nbinary.ch2);
870 1.1 cgd break;
871 1.1 cgd case NAND:
872 1.1 cgd cmdtxt(n->nbinary.ch1);
873 1.1 cgd cmdputs(" && ");
874 1.1 cgd cmdtxt(n->nbinary.ch2);
875 1.1 cgd break;
876 1.1 cgd case NOR:
877 1.1 cgd cmdtxt(n->nbinary.ch1);
878 1.1 cgd cmdputs(" || ");
879 1.1 cgd cmdtxt(n->nbinary.ch2);
880 1.1 cgd break;
881 1.1 cgd case NPIPE:
882 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
883 1.1 cgd cmdtxt(lp->n);
884 1.1 cgd if (lp->next)
885 1.1 cgd cmdputs(" | ");
886 1.1 cgd }
887 1.1 cgd break;
888 1.1 cgd case NSUBSHELL:
889 1.1 cgd cmdputs("(");
890 1.1 cgd cmdtxt(n->nredir.n);
891 1.1 cgd cmdputs(")");
892 1.1 cgd break;
893 1.1 cgd case NREDIR:
894 1.1 cgd case NBACKGND:
895 1.1 cgd cmdtxt(n->nredir.n);
896 1.1 cgd break;
897 1.1 cgd case NIF:
898 1.1 cgd cmdputs("if ");
899 1.1 cgd cmdtxt(n->nif.test);
900 1.1 cgd cmdputs("; then ");
901 1.1 cgd cmdtxt(n->nif.ifpart);
902 1.1 cgd cmdputs("...");
903 1.1 cgd break;
904 1.1 cgd case NWHILE:
905 1.1 cgd cmdputs("while ");
906 1.1 cgd goto until;
907 1.1 cgd case NUNTIL:
908 1.1 cgd cmdputs("until ");
909 1.1 cgd until:
910 1.1 cgd cmdtxt(n->nbinary.ch1);
911 1.1 cgd cmdputs("; do ");
912 1.1 cgd cmdtxt(n->nbinary.ch2);
913 1.1 cgd cmdputs("; done");
914 1.1 cgd break;
915 1.1 cgd case NFOR:
916 1.1 cgd cmdputs("for ");
917 1.1 cgd cmdputs(n->nfor.var);
918 1.1 cgd cmdputs(" in ...");
919 1.1 cgd break;
920 1.1 cgd case NCASE:
921 1.1 cgd cmdputs("case ");
922 1.1 cgd cmdputs(n->ncase.expr->narg.text);
923 1.1 cgd cmdputs(" in ...");
924 1.1 cgd break;
925 1.1 cgd case NDEFUN:
926 1.1 cgd cmdputs(n->narg.text);
927 1.1 cgd cmdputs("() ...");
928 1.1 cgd break;
929 1.1 cgd case NCMD:
930 1.1 cgd for (np = n->ncmd.args ; np ; np = np->narg.next) {
931 1.1 cgd cmdtxt(np);
932 1.1 cgd if (np->narg.next)
933 1.1 cgd cmdputs(" ");
934 1.1 cgd }
935 1.1 cgd for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
936 1.1 cgd cmdputs(" ");
937 1.1 cgd cmdtxt(np);
938 1.1 cgd }
939 1.1 cgd break;
940 1.1 cgd case NARG:
941 1.1 cgd cmdputs(n->narg.text);
942 1.1 cgd break;
943 1.1 cgd case NTO:
944 1.1 cgd p = ">"; i = 1; goto redir;
945 1.1 cgd case NAPPEND:
946 1.1 cgd p = ">>"; i = 1; goto redir;
947 1.1 cgd case NTOFD:
948 1.1 cgd p = ">&"; i = 1; goto redir;
949 1.1 cgd case NFROM:
950 1.1 cgd p = "<"; i = 0; goto redir;
951 1.1 cgd case NFROMFD:
952 1.1 cgd p = "<&"; i = 0; goto redir;
953 1.1 cgd redir:
954 1.1 cgd if (n->nfile.fd != i) {
955 1.1 cgd s[0] = n->nfile.fd + '0';
956 1.1 cgd s[1] = '\0';
957 1.1 cgd cmdputs(s);
958 1.1 cgd }
959 1.1 cgd cmdputs(p);
960 1.1 cgd if (n->type == NTOFD || n->type == NFROMFD) {
961 1.1 cgd s[0] = n->ndup.dupfd + '0';
962 1.1 cgd s[1] = '\0';
963 1.1 cgd cmdputs(s);
964 1.1 cgd } else {
965 1.1 cgd cmdtxt(n->nfile.fname);
966 1.1 cgd }
967 1.1 cgd break;
968 1.1 cgd case NHERE:
969 1.1 cgd case NXHERE:
970 1.1 cgd cmdputs("<<...");
971 1.1 cgd break;
972 1.1 cgd default:
973 1.1 cgd cmdputs("???");
974 1.1 cgd break;
975 1.1 cgd }
976 1.1 cgd }
977 1.1 cgd
978 1.1 cgd
979 1.1 cgd
980 1.1 cgd STATIC void
981 1.1 cgd cmdputs(s)
982 1.1 cgd char *s;
983 1.1 cgd {
984 1.1 cgd register char *p, *q;
985 1.1 cgd register char c;
986 1.1 cgd int subtype = 0;
987 1.1 cgd
988 1.1 cgd if (cmdnleft <= 0)
989 1.1 cgd return;
990 1.1 cgd p = s;
991 1.1 cgd q = cmdnextc;
992 1.1 cgd while ((c = *p++) != '\0') {
993 1.1 cgd if (c == CTLESC)
994 1.1 cgd *q++ = *p++;
995 1.1 cgd else if (c == CTLVAR) {
996 1.1 cgd *q++ = '$';
997 1.1 cgd if (--cmdnleft > 0)
998 1.1 cgd *q++ = '{';
999 1.1 cgd subtype = *p++;
1000 1.1 cgd } else if (c == '=' && subtype != 0) {
1001 1.1 cgd *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL];
1002 1.1 cgd subtype = 0;
1003 1.1 cgd } else if (c == CTLENDVAR) {
1004 1.1 cgd *q++ = '}';
1005 1.1 cgd } else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE)
1006 1.1 cgd cmdnleft++; /* ignore it */
1007 1.1 cgd else
1008 1.1 cgd *q++ = c;
1009 1.1 cgd if (--cmdnleft <= 0) {
1010 1.1 cgd *q++ = '.';
1011 1.1 cgd *q++ = '.';
1012 1.1 cgd *q++ = '.';
1013 1.1 cgd break;
1014 1.1 cgd }
1015 1.1 cgd }
1016 1.1 cgd cmdnextc = q;
1017 1.1 cgd }
1018