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