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