]>
Commit | Line | Data |
---|---|---|
3185942a | 1 | /* jobs.c - functions that make children, remember them, and handle their termination. */ |
726f6388 | 2 | |
ccc6cda3 JA |
3 | /* This file works with both POSIX and BSD systems. It implements job |
4 | control. */ | |
726f6388 | 5 | |
d233b485 | 6 | /* Copyright (C) 1989-2017 Free Software Foundation, Inc. |
726f6388 JA |
7 | |
8 | This file is part of GNU Bash, the Bourne Again SHell. | |
9 | ||
3185942a JA |
10 | Bash is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation, either version 3 of the License, or | |
13 | (at your option) any later version. | |
726f6388 | 14 | |
3185942a JA |
15 | Bash is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
726f6388 | 19 | |
3185942a JA |
20 | You should have received a copy of the GNU General Public License |
21 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
726f6388 | 23 | |
726f6388 JA |
24 | #include "config.h" |
25 | ||
726f6388 JA |
26 | #include "bashtypes.h" |
27 | #include "trap.h" | |
28 | #include <stdio.h> | |
29 | #include <signal.h> | |
30 | #include <errno.h> | |
31 | ||
ccc6cda3 JA |
32 | #if defined (HAVE_UNISTD_H) |
33 | # include <unistd.h> | |
34 | #endif | |
726f6388 | 35 | |
f73dda09 | 36 | #include "posixtime.h" |
ccc6cda3 | 37 | |
cce855bc | 38 | #if defined (HAVE_SYS_RESOURCE_H) && defined (HAVE_WAIT3) && !defined (_POSIX_VERSION) && !defined (RLIMTYPE) |
ccc6cda3 | 39 | # include <sys/resource.h> |
cce855bc | 40 | #endif /* !_POSIX_VERSION && HAVE_SYS_RESOURCE_H && HAVE_WAIT3 && !RLIMTYPE */ |
726f6388 | 41 | |
b80f6443 JA |
42 | #if defined (HAVE_SYS_FILE_H) |
43 | # include <sys/file.h> | |
44 | #endif | |
45 | ||
726f6388 JA |
46 | #include "filecntl.h" |
47 | #include <sys/ioctl.h> | |
ac50fbac | 48 | #if defined (HAVE_SYS_PARAM_H) |
726f6388 | 49 | #include <sys/param.h> |
ac50fbac | 50 | #endif |
726f6388 JA |
51 | |
52 | #if defined (BUFFERED_INPUT) | |
53 | # include "input.h" | |
54 | #endif | |
55 | ||
ccc6cda3 | 56 | /* Need to include this up here for *_TTY_DRIVER definitions. */ |
bb70624e | 57 | #include "shtty.h" |
726f6388 JA |
58 | |
59 | /* Define this if your output is getting swallowed. It's a no-op on | |
60 | machines with the termio or termios tty drivers. */ | |
61 | /* #define DRAIN_OUTPUT */ | |
62 | ||
726f6388 | 63 | /* For the TIOCGPGRP and TIOCSPGRP ioctl parameters on HP-UX */ |
726f6388 JA |
64 | #if defined (hpux) && !defined (TERMIOS_TTY_DRIVER) |
65 | # include <bsdtty.h> | |
66 | #endif /* hpux && !TERMIOS_TTY_DRIVER */ | |
67 | ||
68 | #include "bashansi.h" | |
b80f6443 | 69 | #include "bashintl.h" |
726f6388 | 70 | #include "shell.h" |
d233b485 | 71 | #include "parser.h" |
726f6388 | 72 | #include "jobs.h" |
3185942a | 73 | #include "execute_cmd.h" |
ccc6cda3 | 74 | #include "flags.h" |
726f6388 | 75 | |
d233b485 CR |
76 | #include "typemax.h" |
77 | ||
726f6388 JA |
78 | #include "builtins/builtext.h" |
79 | #include "builtins/common.h" | |
80 | ||
a0c0a00f CR |
81 | #if defined (READLINE) |
82 | # include <readline/readline.h> | |
83 | #endif | |
84 | ||
726f6388 JA |
85 | #if !defined (errno) |
86 | extern int errno; | |
87 | #endif /* !errno */ | |
88 | ||
495aee44 CR |
89 | #if !defined (HAVE_KILLPG) |
90 | extern int killpg __P((pid_t, int)); | |
91 | #endif | |
92 | ||
ac50fbac CR |
93 | #if !DEFAULT_CHILD_MAX |
94 | # define DEFAULT_CHILD_MAX 32 | |
95 | #endif | |
96 | ||
97 | #if !MAX_CHILD_MAX | |
d233b485 | 98 | # define MAX_CHILD_MAX 32768 |
ac50fbac CR |
99 | #endif |
100 | ||
0628567a JA |
101 | #if !defined (DEBUG) |
102 | #define MAX_JOBS_IN_ARRAY 4096 /* production */ | |
103 | #else | |
104 | #define MAX_JOBS_IN_ARRAY 128 /* testing */ | |
105 | #endif | |
106 | ||
a0c0a00f CR |
107 | /* XXX for now */ |
108 | #define PIDSTAT_TABLE_SZ 4096 | |
109 | #define BGPIDS_TABLE_SZ 512 | |
110 | ||
0628567a JA |
111 | /* Flag values for second argument to delete_job */ |
112 | #define DEL_WARNSTOPPED 1 /* warn about deleting stopped jobs */ | |
113 | #define DEL_NOBGPID 2 /* don't add pgrp leader to bgpids */ | |
b72432fd | 114 | |
ccc6cda3 JA |
115 | /* Take care of system dependencies that must be handled when waiting for |
116 | children. The arguments to the WAITPID macro match those to the Posix.1 | |
117 | waitpid() function. */ | |
118 | ||
119 | #if defined (ultrix) && defined (mips) && defined (_POSIX_VERSION) | |
120 | # define WAITPID(pid, statusp, options) \ | |
121 | wait3 ((union wait *)statusp, options, (struct rusage *)0) | |
122 | #else | |
123 | # if defined (_POSIX_VERSION) || defined (HAVE_WAITPID) | |
124 | # define WAITPID(pid, statusp, options) \ | |
125 | waitpid ((pid_t)pid, statusp, options) | |
126 | # else | |
127 | # if defined (HAVE_WAIT3) | |
128 | # define WAITPID(pid, statusp, options) \ | |
129 | wait3 (statusp, options, (struct rusage *)0) | |
130 | # else | |
131 | # define WAITPID(pid, statusp, options) \ | |
132 | wait3 (statusp, options, (int *)0) | |
133 | # endif /* HAVE_WAIT3 */ | |
134 | # endif /* !_POSIX_VERSION && !HAVE_WAITPID*/ | |
135 | #endif /* !(Ultrix && mips && _POSIX_VERSION) */ | |
136 | ||
137 | /* getpgrp () varies between systems. Even systems that claim to be | |
138 | Posix.1 compatible lie sometimes (Ultrix, SunOS4, apollo). */ | |
139 | #if defined (GETPGRP_VOID) | |
140 | # define getpgid(p) getpgrp () | |
141 | #else | |
142 | # define getpgid(p) getpgrp (p) | |
143 | #endif /* !GETPGRP_VOID */ | |
144 | ||
145 | /* If the system needs it, REINSTALL_SIGCHLD_HANDLER will reinstall the | |
146 | handler for SIGCHLD. */ | |
147 | #if defined (MUST_REINSTALL_SIGHANDLERS) | |
148 | # define REINSTALL_SIGCHLD_HANDLER signal (SIGCHLD, sigchld_handler) | |
149 | #else | |
150 | # define REINSTALL_SIGCHLD_HANDLER | |
151 | #endif /* !MUST_REINSTALL_SIGHANDLERS */ | |
152 | ||
f73dda09 | 153 | /* Some systems let waitpid(2) tell callers about stopped children. */ |
95732b49 JA |
154 | #if !defined (WCONTINUED) || defined (WCONTINUED_BROKEN) |
155 | # undef WCONTINUED | |
f73dda09 | 156 | # define WCONTINUED 0 |
b80f6443 JA |
157 | #endif |
158 | #if !defined (WIFCONTINUED) | |
f73dda09 JA |
159 | # define WIFCONTINUED(s) (0) |
160 | #endif | |
161 | ||
ccc6cda3 JA |
162 | /* The number of additional slots to allocate when we run out. */ |
163 | #define JOB_SLOTS 8 | |
164 | ||
f73dda09 JA |
165 | typedef int sh_job_map_func_t __P((JOB *, int, int, int)); |
166 | ||
726f6388 | 167 | /* Variables used here but defined in other files. */ |
726f6388 | 168 | extern sigset_t top_level_mask; |
bb70624e | 169 | extern WORD_LIST *subst_assign_varlist; |
726f6388 | 170 | |
a0c0a00f CR |
171 | extern SigHandler **original_signals; |
172 | ||
173 | extern void set_original_signal __P((int, SigHandler *)); | |
174 | ||
95732b49 JA |
175 | static struct jobstats zerojs = { -1L, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NO_JOB, NO_JOB, 0, 0 }; |
176 | struct jobstats js = { -1L, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NO_JOB, NO_JOB, 0, 0 }; | |
177 | ||
a0c0a00f | 178 | ps_index_t pidstat_table[PIDSTAT_TABLE_SZ]; |
95732b49 JA |
179 | struct bgpids bgpids = { 0, 0, 0 }; |
180 | ||
726f6388 JA |
181 | /* The array of known jobs. */ |
182 | JOB **jobs = (JOB **)NULL; | |
183 | ||
95732b49 | 184 | #if 0 |
726f6388 JA |
185 | /* The number of slots currently allocated to JOBS. */ |
186 | int job_slots = 0; | |
95732b49 | 187 | #endif |
726f6388 | 188 | |
726f6388 JA |
189 | /* The controlling tty for this shell. */ |
190 | int shell_tty = -1; | |
191 | ||
192 | /* The shell's process group. */ | |
193 | pid_t shell_pgrp = NO_PID; | |
194 | ||
195 | /* The terminal's process group. */ | |
196 | pid_t terminal_pgrp = NO_PID; | |
197 | ||
198 | /* The process group of the shell's parent. */ | |
199 | pid_t original_pgrp = NO_PID; | |
200 | ||
201 | /* The process group of the pipeline currently being made. */ | |
202 | pid_t pipeline_pgrp = (pid_t)0; | |
203 | ||
204 | #if defined (PGRP_PIPE) | |
205 | /* Pipes which each shell uses to communicate with the process group leader | |
206 | until all of the processes in a pipeline have been started. Then the | |
207 | process leader is allowed to continue. */ | |
208 | int pgrp_pipe[2] = { -1, -1 }; | |
209 | #endif | |
ccc6cda3 | 210 | |
726f6388 | 211 | /* Last child made by the shell. */ |
ac50fbac | 212 | volatile pid_t last_made_pid = NO_PID; |
726f6388 JA |
213 | |
214 | /* Pid of the last asynchronous child. */ | |
ac50fbac | 215 | volatile pid_t last_asynchronous_pid = NO_PID; |
726f6388 JA |
216 | |
217 | /* The pipeline currently being built. */ | |
218 | PROCESS *the_pipeline = (PROCESS *)NULL; | |
219 | ||
220 | /* If this is non-zero, do job control. */ | |
221 | int job_control = 1; | |
222 | ||
a0c0a00f CR |
223 | /* Are we running in background? (terminal_pgrp != shell_pgrp) */ |
224 | int running_in_background = 0; | |
225 | ||
726f6388 JA |
226 | /* Call this when you start making children. */ |
227 | int already_making_children = 0; | |
228 | ||
ccc6cda3 JA |
229 | /* If this is non-zero, $LINES and $COLUMNS are reset after every process |
230 | exits from get_tty_state(). */ | |
ac50fbac | 231 | int check_window_size = CHECKWINSIZE_DEFAULT; |
ccc6cda3 | 232 | |
a0c0a00f CR |
233 | PROCESS *last_procsub_child = (PROCESS *)NULL; |
234 | ||
726f6388 | 235 | /* Functions local to this file. */ |
f73dda09 | 236 | |
d233b485 CR |
237 | void debug_print_pgrps (void); |
238 | ||
f73dda09 JA |
239 | static sighandler wait_sigint_handler __P((int)); |
240 | static sighandler sigchld_handler __P((int)); | |
f73dda09 JA |
241 | static sighandler sigcont_sighandler __P((int)); |
242 | static sighandler sigstop_sighandler __P((int)); | |
243 | ||
244 | static int waitchld __P((pid_t, int)); | |
245 | ||
7117c2d2 | 246 | static PROCESS *find_pipeline __P((pid_t, int, int *)); |
95732b49 | 247 | static PROCESS *find_process __P((pid_t, int, int *)); |
f73dda09 JA |
248 | |
249 | static char *current_working_directory __P((void)); | |
250 | static char *job_working_directory __P((void)); | |
b80f6443 | 251 | static char *j_strsignal __P((int)); |
f73dda09 JA |
252 | static char *printable_job_status __P((int, PROCESS *, int)); |
253 | ||
95732b49 | 254 | static PROCESS *find_last_proc __P((int, int)); |
7117c2d2 | 255 | static pid_t find_last_pid __P((int, int)); |
f73dda09 JA |
256 | |
257 | static int set_new_line_discipline __P((int)); | |
258 | static int map_over_jobs __P((sh_job_map_func_t *, int, int)); | |
259 | static int job_last_stopped __P((int)); | |
260 | static int job_last_running __P((int)); | |
261 | static int most_recent_job_in_state __P((int, JOB_STATE)); | |
95732b49 | 262 | static int find_job __P((pid_t, int, PROCESS **)); |
f73dda09 JA |
263 | static int print_job __P((JOB *, int, int, int)); |
264 | static int process_exit_status __P((WAIT)); | |
b80f6443 | 265 | static int process_exit_signal __P((WAIT)); |
f73dda09 JA |
266 | static int set_job_status_and_cleanup __P((int)); |
267 | ||
f1be666c | 268 | static WAIT job_signal_status __P((int)); |
f73dda09 JA |
269 | static WAIT raw_job_exit_status __P((int)); |
270 | ||
271 | static void notify_of_job_status __P((void)); | |
95732b49 | 272 | static void reset_job_indices __P((void)); |
f73dda09 | 273 | static void cleanup_dead_jobs __P((void)); |
95732b49 JA |
274 | static int processes_in_job __P((int)); |
275 | static void realloc_jobs_list __P((void)); | |
7117c2d2 | 276 | static int compact_jobs_list __P((int)); |
f73dda09 JA |
277 | static void add_process __P((char *, pid_t)); |
278 | static void print_pipeline __P((PROCESS *, int, int, FILE *)); | |
279 | static void pretty_print_job __P((int, int, FILE *)); | |
280 | static void set_current_job __P((int)); | |
281 | static void reset_current __P((void)); | |
282 | static void set_job_running __P((int)); | |
283 | static void setjstatus __P((int)); | |
3185942a | 284 | static int maybe_give_terminal_to __P((pid_t, pid_t, int)); |
f73dda09 JA |
285 | static void mark_all_jobs_as_dead __P((void)); |
286 | static void mark_dead_jobs_as_notified __P((int)); | |
287 | static void restore_sigint_handler __P((void)); | |
726f6388 | 288 | #if defined (PGRP_PIPE) |
f73dda09 | 289 | static void pipe_read __P((int *)); |
726f6388 JA |
290 | #endif |
291 | ||
a0c0a00f CR |
292 | /* Hash table manipulation */ |
293 | ||
294 | static ps_index_t *pshash_getbucket __P((pid_t)); | |
295 | static void pshash_delindex __P((ps_index_t)); | |
296 | ||
297 | /* Saved background process status management */ | |
95732b49 JA |
298 | static struct pidstat *bgp_add __P((pid_t, int)); |
299 | static int bgp_delete __P((pid_t)); | |
300 | static void bgp_clear __P((void)); | |
301 | static int bgp_search __P((pid_t)); | |
a0c0a00f | 302 | |
d233b485 CR |
303 | static struct pipeline_saver *alloc_pipeline_saver __P((void)); |
304 | ||
a0c0a00f CR |
305 | static ps_index_t bgp_getindex __P((void)); |
306 | static void bgp_resize __P((void)); /* XXX */ | |
95732b49 | 307 | |
7117c2d2 JA |
308 | #if defined (ARRAY_VARS) |
309 | static int *pstatuses; /* list of pipeline statuses */ | |
310 | static int statsize; | |
311 | #endif | |
312 | ||
313 | /* Used to synchronize between wait_for and other functions and the SIGCHLD | |
314 | signal handler. */ | |
ccc6cda3 | 315 | static int sigchld; |
7117c2d2 JA |
316 | static int queue_sigchld; |
317 | ||
318 | #define QUEUE_SIGCHLD(os) (os) = sigchld, queue_sigchld++ | |
319 | ||
320 | #define UNQUEUE_SIGCHLD(os) \ | |
321 | do { \ | |
322 | queue_sigchld--; \ | |
323 | if (queue_sigchld == 0 && os != sigchld) \ | |
324 | waitchld (-1, 0); \ | |
325 | } while (0) | |
326 | ||
327 | static SigHandler *old_tstp, *old_ttou, *old_ttin; | |
328 | static SigHandler *old_cont = (SigHandler *)SIG_DFL; | |
329 | ||
ccc6cda3 | 330 | /* A place to temporarily save the current pipeline. */ |
a0c0a00f | 331 | static struct pipeline_saver *saved_pipeline; |
ccc6cda3 | 332 | static int saved_already_making_children; |
726f6388 JA |
333 | |
334 | /* Set this to non-zero whenever you don't want the jobs list to change at | |
335 | all: no jobs deleted and no status change notifications. This is used, | |
336 | for example, when executing SIGCHLD traps, which may run arbitrary | |
337 | commands. */ | |
ccc6cda3 | 338 | static int jobs_list_frozen; |
726f6388 | 339 | |
28ef6c31 JA |
340 | static char retcode_name_buffer[64]; |
341 | ||
726f6388 JA |
342 | #if !defined (_POSIX_VERSION) |
343 | ||
344 | /* These are definitions to map POSIX 1003.1 functions onto existing BSD | |
345 | library functions and system calls. */ | |
346 | #define setpgid(pid, pgrp) setpgrp (pid, pgrp) | |
347 | #define tcsetpgrp(fd, pgrp) ioctl ((fd), TIOCSPGRP, &(pgrp)) | |
348 | ||
349 | pid_t | |
350 | tcgetpgrp (fd) | |
351 | int fd; | |
352 | { | |
353 | pid_t pgrp; | |
354 | ||
355 | /* ioctl will handle setting errno correctly. */ | |
356 | if (ioctl (fd, TIOCGPGRP, &pgrp) < 0) | |
357 | return (-1); | |
358 | return (pgrp); | |
359 | } | |
360 | ||
726f6388 JA |
361 | #endif /* !_POSIX_VERSION */ |
362 | ||
0628567a | 363 | /* Initialize the global job stats structure and other bookkeeping variables */ |
95732b49 JA |
364 | void |
365 | init_job_stats () | |
366 | { | |
367 | js = zerojs; | |
368 | } | |
369 | ||
726f6388 JA |
370 | /* Return the working directory for the current process. Unlike |
371 | job_working_directory, this does not call malloc (), nor do any | |
372 | of the functions it calls. This is so that it can safely be called | |
373 | from a signal handler. */ | |
374 | static char * | |
375 | current_working_directory () | |
376 | { | |
377 | char *dir; | |
ccc6cda3 | 378 | static char d[PATH_MAX]; |
726f6388 JA |
379 | |
380 | dir = get_string_value ("PWD"); | |
381 | ||
ccc6cda3 | 382 | if (dir == 0 && the_current_working_directory && no_symbolic_links) |
726f6388 JA |
383 | dir = the_current_working_directory; |
384 | ||
ccc6cda3 | 385 | if (dir == 0) |
726f6388 | 386 | { |
ccc6cda3 | 387 | dir = getcwd (d, sizeof(d)); |
726f6388 JA |
388 | if (dir) |
389 | dir = d; | |
390 | } | |
391 | ||
ccc6cda3 | 392 | return (dir == 0) ? "<unknown>" : dir; |
726f6388 JA |
393 | } |
394 | ||
395 | /* Return the working directory for the current process. */ | |
396 | static char * | |
397 | job_working_directory () | |
398 | { | |
399 | char *dir; | |
400 | ||
401 | dir = get_string_value ("PWD"); | |
402 | if (dir) | |
403 | return (savestring (dir)); | |
404 | ||
405 | dir = get_working_directory ("job-working-directory"); | |
406 | if (dir) | |
407 | return (dir); | |
408 | ||
409 | return (savestring ("<unknown>")); | |
410 | } | |
411 | ||
412 | void | |
413 | making_children () | |
414 | { | |
415 | if (already_making_children) | |
416 | return; | |
417 | ||
418 | already_making_children = 1; | |
419 | start_pipeline (); | |
420 | } | |
421 | ||
422 | void | |
423 | stop_making_children () | |
424 | { | |
425 | already_making_children = 0; | |
426 | } | |
427 | ||
428 | void | |
429 | cleanup_the_pipeline () | |
430 | { | |
95732b49 JA |
431 | PROCESS *disposer; |
432 | sigset_t set, oset; | |
433 | ||
434 | BLOCK_CHILD (set, oset); | |
435 | disposer = the_pipeline; | |
436 | the_pipeline = (PROCESS *)NULL; | |
437 | UNBLOCK_CHILD (oset); | |
438 | ||
439 | if (disposer) | |
440 | discard_pipeline (disposer); | |
726f6388 JA |
441 | } |
442 | ||
2965eca9 CR |
443 | void |
444 | discard_last_procsub_child () | |
445 | { | |
446 | PROCESS *disposer; | |
447 | sigset_t set, oset; | |
448 | ||
449 | BLOCK_CHILD (set, oset); | |
450 | disposer = last_procsub_child; | |
451 | last_procsub_child = (PROCESS *)NULL; | |
452 | UNBLOCK_CHILD (oset); | |
453 | ||
454 | if (disposer) | |
455 | discard_pipeline (disposer); | |
456 | } | |
457 | ||
d233b485 | 458 | static struct pipeline_saver * |
a0c0a00f CR |
459 | alloc_pipeline_saver () |
460 | { | |
461 | struct pipeline_saver *ret; | |
462 | ||
463 | ret = (struct pipeline_saver *)xmalloc (sizeof (struct pipeline_saver)); | |
464 | ret->pipeline = 0; | |
465 | ret->next = 0; | |
466 | return ret; | |
467 | } | |
468 | ||
ccc6cda3 JA |
469 | void |
470 | save_pipeline (clear) | |
471 | int clear; | |
472 | { | |
a0c0a00f CR |
473 | sigset_t set, oset; |
474 | struct pipeline_saver *saver; | |
475 | ||
476 | BLOCK_CHILD (set, oset); | |
477 | saver = alloc_pipeline_saver (); | |
478 | saver->pipeline = the_pipeline; | |
479 | saver->next = saved_pipeline; | |
480 | saved_pipeline = saver; | |
ccc6cda3 JA |
481 | if (clear) |
482 | the_pipeline = (PROCESS *)NULL; | |
95732b49 | 483 | saved_already_making_children = already_making_children; |
a0c0a00f | 484 | UNBLOCK_CHILD (oset); |
ccc6cda3 JA |
485 | } |
486 | ||
a0c0a00f | 487 | PROCESS * |
ccc6cda3 JA |
488 | restore_pipeline (discard) |
489 | int discard; | |
490 | { | |
491 | PROCESS *old_pipeline; | |
a0c0a00f CR |
492 | sigset_t set, oset; |
493 | struct pipeline_saver *saver; | |
ccc6cda3 | 494 | |
a0c0a00f | 495 | BLOCK_CHILD (set, oset); |
ccc6cda3 | 496 | old_pipeline = the_pipeline; |
a0c0a00f CR |
497 | the_pipeline = saved_pipeline->pipeline; |
498 | saver = saved_pipeline; | |
499 | saved_pipeline = saved_pipeline->next; | |
500 | free (saver); | |
ccc6cda3 | 501 | already_making_children = saved_already_making_children; |
a0c0a00f CR |
502 | UNBLOCK_CHILD (oset); |
503 | ||
89a92869 | 504 | if (discard && old_pipeline) |
a0c0a00f CR |
505 | { |
506 | discard_pipeline (old_pipeline); | |
507 | return ((PROCESS *)NULL); | |
508 | } | |
509 | return old_pipeline; | |
ccc6cda3 JA |
510 | } |
511 | ||
726f6388 JA |
512 | /* Start building a pipeline. */ |
513 | void | |
514 | start_pipeline () | |
515 | { | |
516 | if (the_pipeline) | |
517 | { | |
ccc6cda3 | 518 | cleanup_the_pipeline (); |
726f6388 JA |
519 | pipeline_pgrp = 0; |
520 | #if defined (PGRP_PIPE) | |
3185942a | 521 | sh_closepipe (pgrp_pipe); |
726f6388 JA |
522 | #endif |
523 | } | |
524 | ||
525 | #if defined (PGRP_PIPE) | |
526 | if (job_control) | |
527 | { | |
528 | if (pipe (pgrp_pipe) == -1) | |
3185942a | 529 | sys_error (_("start_pipeline: pgrp pipe")); |
726f6388 JA |
530 | } |
531 | #endif | |
532 | } | |
533 | ||
534 | /* Stop building a pipeline. Install the process list in the job array. | |
535 | This returns the index of the newly installed job. | |
536 | DEFERRED is a command structure to be executed upon satisfactory | |
537 | execution exit of this pipeline. */ | |
538 | int | |
539 | stop_pipeline (async, deferred) | |
540 | int async; | |
541 | COMMAND *deferred; | |
542 | { | |
543 | register int i, j; | |
ccc6cda3 | 544 | JOB *newjob; |
726f6388 JA |
545 | sigset_t set, oset; |
546 | ||
547 | BLOCK_CHILD (set, oset); | |
548 | ||
549 | #if defined (PGRP_PIPE) | |
550 | /* The parent closes the process group synchronization pipe. */ | |
3185942a | 551 | sh_closepipe (pgrp_pipe); |
726f6388 | 552 | #endif |
ccc6cda3 | 553 | |
726f6388 JA |
554 | cleanup_dead_jobs (); |
555 | ||
95732b49 | 556 | if (js.j_jobslots == 0) |
726f6388 | 557 | { |
95732b49 JA |
558 | js.j_jobslots = JOB_SLOTS; |
559 | jobs = (JOB **)xmalloc (js.j_jobslots * sizeof (JOB *)); | |
726f6388 JA |
560 | |
561 | /* Now blank out these new entries. */ | |
95732b49 | 562 | for (i = 0; i < js.j_jobslots; i++) |
726f6388 | 563 | jobs[i] = (JOB *)NULL; |
95732b49 JA |
564 | |
565 | js.j_firstj = js.j_lastj = js.j_njobs = 0; | |
726f6388 JA |
566 | } |
567 | ||
568 | /* Scan from the last slot backward, looking for the next free one. */ | |
b80f6443 | 569 | /* XXX - revisit this interactive assumption */ |
95732b49 | 570 | /* XXX - this way for now */ |
726f6388 JA |
571 | if (interactive) |
572 | { | |
95732b49 | 573 | for (i = js.j_jobslots; i; i--) |
726f6388 JA |
574 | if (jobs[i - 1]) |
575 | break; | |
576 | } | |
577 | else | |
578 | { | |
95732b49 JA |
579 | #if 0 |
580 | /* This wraps around, but makes it inconvenient to extend the array */ | |
581 | for (i = js.j_lastj+1; i != js.j_lastj; i++) | |
582 | { | |
583 | if (i >= js.j_jobslots) | |
584 | i = 0; | |
585 | if (jobs[i] == 0) | |
586 | break; | |
587 | } | |
588 | if (i == js.j_lastj) | |
589 | i = js.j_jobslots; | |
590 | #else | |
591 | /* This doesn't wrap around yet. */ | |
592 | for (i = js.j_lastj ? js.j_lastj + 1 : js.j_lastj; i < js.j_jobslots; i++) | |
ccc6cda3 | 593 | if (jobs[i] == 0) |
726f6388 | 594 | break; |
95732b49 | 595 | #endif |
726f6388 JA |
596 | } |
597 | ||
598 | /* Do we need more room? */ | |
7117c2d2 JA |
599 | |
600 | /* First try compaction */ | |
95732b49 | 601 | if ((interactive_shell == 0 || subshell_environment) && i == js.j_jobslots && js.j_jobslots >= MAX_JOBS_IN_ARRAY) |
7117c2d2 JA |
602 | i = compact_jobs_list (0); |
603 | ||
604 | /* If we can't compact, reallocate */ | |
95732b49 | 605 | if (i == js.j_jobslots) |
726f6388 | 606 | { |
95732b49 JA |
607 | js.j_jobslots += JOB_SLOTS; |
608 | jobs = (JOB **)xrealloc (jobs, (js.j_jobslots * sizeof (JOB *))); | |
726f6388 | 609 | |
95732b49 | 610 | for (j = i; j < js.j_jobslots; j++) |
726f6388 JA |
611 | jobs[j] = (JOB *)NULL; |
612 | } | |
613 | ||
614 | /* Add the current pipeline to the job list. */ | |
615 | if (the_pipeline) | |
616 | { | |
617 | register PROCESS *p; | |
95732b49 | 618 | int any_running, any_stopped, n; |
726f6388 JA |
619 | |
620 | newjob = (JOB *)xmalloc (sizeof (JOB)); | |
621 | ||
95732b49 | 622 | for (n = 1, p = the_pipeline; p->next != the_pipeline; n++, p = p->next) |
ccc6cda3 | 623 | ; |
726f6388 JA |
624 | p->next = (PROCESS *)NULL; |
625 | newjob->pipe = REVERSE_LIST (the_pipeline, PROCESS *); | |
ccc6cda3 JA |
626 | for (p = newjob->pipe; p->next; p = p->next) |
627 | ; | |
726f6388 JA |
628 | p->next = newjob->pipe; |
629 | ||
630 | the_pipeline = (PROCESS *)NULL; | |
631 | newjob->pgrp = pipeline_pgrp; | |
632 | pipeline_pgrp = 0; | |
633 | ||
634 | newjob->flags = 0; | |
d233b485 CR |
635 | if (pipefail_opt) |
636 | newjob->flags |= J_PIPEFAIL; | |
726f6388 JA |
637 | |
638 | /* Flag to see if in another pgrp. */ | |
639 | if (job_control) | |
640 | newjob->flags |= J_JOBCONTROL; | |
641 | ||
642 | /* Set the state of this pipeline. */ | |
ccc6cda3 | 643 | p = newjob->pipe; |
95732b49 | 644 | any_running = any_stopped = 0; |
ccc6cda3 JA |
645 | do |
646 | { | |
95732b49 JA |
647 | any_running |= PRUNNING (p); |
648 | any_stopped |= PSTOPPED (p); | |
ccc6cda3 JA |
649 | p = p->next; |
650 | } | |
651 | while (p != newjob->pipe); | |
726f6388 | 652 | |
95732b49 | 653 | newjob->state = any_running ? JRUNNING : (any_stopped ? JSTOPPED : JDEAD); |
726f6388 JA |
654 | newjob->wd = job_working_directory (); |
655 | newjob->deferred = deferred; | |
656 | ||
f73dda09 | 657 | newjob->j_cleanup = (sh_vptrfunc_t *)NULL; |
ccc6cda3 JA |
658 | newjob->cleanarg = (PTR_T) NULL; |
659 | ||
726f6388 | 660 | jobs[i] = newjob; |
ccc6cda3 JA |
661 | if (newjob->state == JDEAD && (newjob->flags & J_FOREGROUND)) |
662 | setjstatus (i); | |
95732b49 JA |
663 | if (newjob->state == JDEAD) |
664 | { | |
665 | js.c_reaped += n; /* wouldn't have been done since this was not part of a job */ | |
666 | js.j_ndead++; | |
667 | } | |
668 | js.c_injobs += n; | |
669 | ||
670 | js.j_lastj = i; | |
671 | js.j_njobs++; | |
726f6388 | 672 | } |
ccc6cda3 JA |
673 | else |
674 | newjob = (JOB *)NULL; | |
726f6388 | 675 | |
95732b49 JA |
676 | if (newjob) |
677 | js.j_lastmade = newjob; | |
678 | ||
726f6388 JA |
679 | if (async) |
680 | { | |
681 | if (newjob) | |
95732b49 JA |
682 | { |
683 | newjob->flags &= ~J_FOREGROUND; | |
684 | newjob->flags |= J_ASYNC; | |
685 | js.j_lastasync = newjob; | |
686 | } | |
726f6388 JA |
687 | reset_current (); |
688 | } | |
689 | else | |
690 | { | |
691 | if (newjob) | |
692 | { | |
693 | newjob->flags |= J_FOREGROUND; | |
694 | /* | |
d233b485 | 695 | * !!!!! NOTE !!!!! (chet@po.cwru.edu) |
726f6388 JA |
696 | * |
697 | * The currently-accepted job control wisdom says to set the | |
698 | * terminal's process group n+1 times in an n-step pipeline: | |
699 | * once in the parent and once in each child. This is where | |
700 | * the parent gives it away. | |
701 | * | |
0628567a | 702 | * Don't give the terminal away if this shell is an asynchronous |
a0c0a00f CR |
703 | * subshell or if we're a (presumably non-interactive) shell running |
704 | * in the background. | |
0628567a | 705 | * |
726f6388 | 706 | */ |
a0c0a00f | 707 | if (job_control && newjob->pgrp && (subshell_environment&SUBSHELL_ASYNC) == 0 && running_in_background == 0) |
3185942a | 708 | maybe_give_terminal_to (shell_pgrp, newjob->pgrp, 0); |
726f6388 JA |
709 | } |
710 | } | |
711 | ||
712 | stop_making_children (); | |
713 | UNBLOCK_CHILD (oset); | |
495aee44 | 714 | return (newjob ? i : js.j_current); |
95732b49 JA |
715 | } |
716 | ||
717 | /* Functions to manage the list of exited background pids whose status has | |
a0c0a00f | 718 | been saved. |
95732b49 | 719 | |
a0c0a00f CR |
720 | pidstat_table: |
721 | ||
722 | The current implementation is a hash table using a single (separate) arena | |
723 | for storage that can be allocated and freed as a unit. The size of the hash | |
724 | table is a multiple of PIDSTAT_TABLE_SZ (4096) and multiple PIDs that hash | |
725 | to the same value are chained through the bucket_next and bucket_prev | |
726 | pointers (basically coalesced hashing for collision resolution). | |
727 | ||
728 | bgpids.storage: | |
729 | ||
730 | All pid/status storage is done using the circular buffer bgpids.storage. | |
731 | This must contain at least js.c_childmax entries. The circular buffer is | |
732 | used to supply the ordered list Posix requires ("the last CHILD_MAX | |
733 | processes"). To avoid searching the entire storage table for a given PID, | |
734 | the hash table (pidstat_table) holds pointers into the storage arena and | |
735 | uses a doubly-linked list of cells (bucket_next/bucket_prev, also pointers | |
736 | into the arena) to implement collision resolution. */ | |
737 | ||
738 | /* The number of elements in bgpids.storage always has to be > js.c_childmax for | |
739 | the circular buffer to work right. */ | |
740 | static void | |
741 | bgp_resize () | |
742 | { | |
d233b485 | 743 | ps_index_t nsize, nsize_cur, nsize_max; |
a0c0a00f CR |
744 | ps_index_t psi; |
745 | ||
746 | if (bgpids.nalloc == 0) | |
747 | { | |
748 | /* invalidate hash table when bgpids table is reallocated */ | |
749 | for (psi = 0; psi < PIDSTAT_TABLE_SZ; psi++) | |
750 | pidstat_table[psi] = NO_PIDSTAT; | |
751 | nsize = BGPIDS_TABLE_SZ; /* should be power of 2 */ | |
752 | bgpids.head = 0; | |
753 | } | |
754 | else | |
755 | nsize = bgpids.nalloc; | |
756 | ||
d233b485 CR |
757 | nsize_max = TYPE_MAXIMUM (ps_index_t); |
758 | nsize_cur = (ps_index_t)js.c_childmax; | |
759 | if (nsize_cur < 0) /* overflow */ | |
760 | nsize_cur = MAX_CHILD_MAX; | |
761 | ||
762 | while (nsize > 0 && nsize < nsize_cur) /* > 0 should catch overflow */ | |
763 | nsize <<= 1; | |
764 | if (nsize > nsize_max || nsize <= 0) /* overflow? */ | |
765 | nsize = nsize_max; | |
766 | if (nsize > MAX_CHILD_MAX) | |
767 | nsize = nsize_max = MAX_CHILD_MAX; /* hard cap */ | |
a0c0a00f | 768 | |
d233b485 | 769 | if (bgpids.nalloc < nsize_cur && bgpids.nalloc < nsize_max) |
a0c0a00f CR |
770 | { |
771 | bgpids.storage = (struct pidstat *)xrealloc (bgpids.storage, nsize * sizeof (struct pidstat)); | |
772 | ||
773 | for (psi = bgpids.nalloc; psi < nsize; psi++) | |
774 | bgpids.storage[psi].pid = NO_PID; | |
775 | ||
776 | bgpids.nalloc = nsize; | |
777 | ||
778 | } | |
779 | else if (bgpids.head >= bgpids.nalloc) /* wrap around */ | |
780 | bgpids.head = 0; | |
781 | } | |
782 | ||
783 | static ps_index_t | |
784 | bgp_getindex () | |
785 | { | |
d233b485 | 786 | if (bgpids.nalloc < (ps_index_t)js.c_childmax || bgpids.head >= bgpids.nalloc) |
a0c0a00f CR |
787 | bgp_resize (); |
788 | ||
789 | pshash_delindex (bgpids.head); /* XXX - clear before reusing */ | |
790 | return bgpids.head++; | |
791 | } | |
792 | ||
793 | static ps_index_t * | |
794 | pshash_getbucket (pid) | |
95732b49 | 795 | pid_t pid; |
95732b49 | 796 | { |
a0c0a00f | 797 | unsigned long hash; /* XXX - u_bits32_t */ |
95732b49 | 798 | |
a0c0a00f CR |
799 | hash = pid * 0x9e370001UL; |
800 | return (&pidstat_table[hash % PIDSTAT_TABLE_SZ]); | |
726f6388 JA |
801 | } |
802 | ||
95732b49 JA |
803 | static struct pidstat * |
804 | bgp_add (pid, status) | |
805 | pid_t pid; | |
806 | int status; | |
807 | { | |
a0c0a00f | 808 | ps_index_t *bucket, psi; |
95732b49 JA |
809 | struct pidstat *ps; |
810 | ||
354efb96 CR |
811 | /* bucket == existing chain of pids hashing to same value |
812 | psi = where were going to put this pid/status */ | |
813 | ||
814 | bucket = pshash_getbucket (pid); /* index into pidstat_table */ | |
815 | psi = bgp_getindex (); /* bgpids.head, index into storage */ | |
816 | ||
817 | /* XXX - what if psi == *bucket? */ | |
818 | if (psi == *bucket) | |
819 | { | |
820 | #ifdef DEBUG | |
821 | internal_warning ("hashed pid %d (pid %d) collides with bgpids.head, skipping", psi, pid); | |
822 | #endif | |
823 | bgpids.storage[psi].pid = NO_PID; /* make sure */ | |
824 | psi = bgp_getindex (); /* skip to next one */ | |
825 | } | |
826 | ||
a0c0a00f CR |
827 | ps = &bgpids.storage[psi]; |
828 | ||
829 | ps->pid = pid; | |
830 | ps->status = status; | |
831 | ps->bucket_next = *bucket; | |
832 | ps->bucket_prev = NO_PIDSTAT; | |
95732b49 | 833 | |
95732b49 JA |
834 | bgpids.npid++; |
835 | ||
a0c0a00f | 836 | #if 0 |
95732b49 JA |
837 | if (bgpids.npid > js.c_childmax) |
838 | bgp_prune (); | |
a0c0a00f CR |
839 | #endif |
840 | ||
841 | if (ps->bucket_next != NO_PIDSTAT) | |
842 | bgpids.storage[ps->bucket_next].bucket_prev = psi; | |
843 | ||
844 | *bucket = psi; /* set chain head in hash table */ | |
95732b49 JA |
845 | |
846 | return ps; | |
847 | } | |
848 | ||
a0c0a00f CR |
849 | static void |
850 | pshash_delindex (psi) | |
851 | ps_index_t psi; | |
852 | { | |
853 | struct pidstat *ps; | |
354efb96 | 854 | ps_index_t *bucket; |
a0c0a00f CR |
855 | |
856 | ps = &bgpids.storage[psi]; | |
857 | if (ps->pid == NO_PID) | |
858 | return; | |
859 | ||
354efb96 | 860 | if (ps->bucket_next != NO_PIDSTAT) |
a0c0a00f | 861 | bgpids.storage[ps->bucket_next].bucket_prev = ps->bucket_prev; |
354efb96 | 862 | if (ps->bucket_prev != NO_PIDSTAT) |
a0c0a00f CR |
863 | bgpids.storage[ps->bucket_prev].bucket_next = ps->bucket_next; |
864 | else | |
354efb96 CR |
865 | { |
866 | bucket = pshash_getbucket (ps->pid); | |
867 | *bucket = ps->bucket_next; /* deleting chain head in hash table */ | |
868 | } | |
869 | ||
d233b485 | 870 | /* clear out this cell, in case it gets reused. */ |
354efb96 CR |
871 | ps->pid = NO_PID; |
872 | ps->bucket_next = ps->bucket_prev = NO_PIDSTAT; | |
a0c0a00f CR |
873 | } |
874 | ||
95732b49 JA |
875 | static int |
876 | bgp_delete (pid) | |
877 | pid_t pid; | |
878 | { | |
354efb96 | 879 | ps_index_t psi, orig_psi; |
95732b49 | 880 | |
a0c0a00f CR |
881 | if (bgpids.storage == 0 || bgpids.nalloc == 0 || bgpids.npid == 0) |
882 | return 0; | |
95732b49 | 883 | |
a0c0a00f | 884 | /* Search chain using hash to find bucket in pidstat_table */ |
354efb96 CR |
885 | for (orig_psi = psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next) |
886 | { | |
887 | if (bgpids.storage[psi].pid == pid) | |
888 | break; | |
889 | if (orig_psi == bgpids.storage[psi].bucket_next) /* catch reported bug */ | |
890 | { | |
891 | internal_warning ("bgp_delete: LOOP: psi (%d) == storage[psi].bucket_next", psi); | |
892 | return 0; | |
893 | } | |
894 | } | |
a0c0a00f CR |
895 | |
896 | if (psi == NO_PIDSTAT) | |
95732b49 JA |
897 | return 0; /* not found */ |
898 | ||
899 | #if defined (DEBUG) | |
900 | itrace("bgp_delete: deleting %d", pid); | |
901 | #endif | |
902 | ||
a0c0a00f | 903 | pshash_delindex (psi); /* hash table management */ |
95732b49 JA |
904 | |
905 | bgpids.npid--; | |
95732b49 JA |
906 | return 1; |
907 | } | |
908 | ||
909 | /* Clear out the list of saved statuses */ | |
910 | static void | |
911 | bgp_clear () | |
912 | { | |
a0c0a00f CR |
913 | if (bgpids.storage == 0 || bgpids.nalloc == 0) |
914 | return; | |
915 | ||
916 | free (bgpids.storage); | |
917 | ||
918 | bgpids.storage = 0; | |
919 | bgpids.nalloc = 0; | |
920 | bgpids.head = 0; | |
95732b49 | 921 | |
95732b49 JA |
922 | bgpids.npid = 0; |
923 | } | |
924 | ||
925 | /* Search for PID in the list of saved background pids; return its status if | |
a0c0a00f CR |
926 | found. If not found, return -1. We hash to the right spot in pidstat_table |
927 | and follow the bucket chain to the end. */ | |
95732b49 JA |
928 | static int |
929 | bgp_search (pid) | |
930 | pid_t pid; | |
931 | { | |
354efb96 | 932 | ps_index_t psi, orig_psi; |
a0c0a00f CR |
933 | |
934 | if (bgpids.storage == 0 || bgpids.nalloc == 0 || bgpids.npid == 0) | |
935 | return -1; | |
936 | ||
937 | /* Search chain using hash to find bucket in pidstat_table */ | |
354efb96 CR |
938 | for (orig_psi = psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next) |
939 | { | |
940 | if (bgpids.storage[psi].pid == pid) | |
941 | return (bgpids.storage[psi].status); | |
942 | if (orig_psi == bgpids.storage[psi].bucket_next) /* catch reported bug */ | |
943 | { | |
944 | internal_warning ("bgp_search: LOOP: psi (%d) == storage[psi].bucket_next", psi); | |
945 | return -1; | |
946 | } | |
947 | } | |
95732b49 | 948 | |
95732b49 JA |
949 | return -1; |
950 | } | |
951 | ||
a0c0a00f | 952 | #if 0 |
95732b49 JA |
953 | static void |
954 | bgp_prune () | |
955 | { | |
a0c0a00f | 956 | return; |
95732b49 | 957 | } |
a0c0a00f | 958 | #endif |
3185942a | 959 | |
95732b49 JA |
960 | /* Reset the values of js.j_lastj and js.j_firstj after one or both have |
961 | been deleted. The caller should check whether js.j_njobs is 0 before | |
962 | calling this. This wraps around, but the rest of the code does not. At | |
963 | this point, it should not matter. */ | |
964 | static void | |
965 | reset_job_indices () | |
966 | { | |
967 | int old; | |
968 | ||
969 | if (jobs[js.j_firstj] == 0) | |
970 | { | |
971 | old = js.j_firstj++; | |
f1be666c JA |
972 | if (old >= js.j_jobslots) |
973 | old = js.j_jobslots - 1; | |
95732b49 JA |
974 | while (js.j_firstj != old) |
975 | { | |
976 | if (js.j_firstj >= js.j_jobslots) | |
977 | js.j_firstj = 0; | |
f1be666c | 978 | if (jobs[js.j_firstj] || js.j_firstj == old) /* needed if old == 0 */ |
95732b49 JA |
979 | break; |
980 | js.j_firstj++; | |
981 | } | |
982 | if (js.j_firstj == old) | |
983 | js.j_firstj = js.j_lastj = js.j_njobs = 0; | |
984 | } | |
985 | if (jobs[js.j_lastj] == 0) | |
986 | { | |
987 | old = js.j_lastj--; | |
f1be666c JA |
988 | if (old < 0) |
989 | old = 0; | |
95732b49 JA |
990 | while (js.j_lastj != old) |
991 | { | |
992 | if (js.j_lastj < 0) | |
993 | js.j_lastj = js.j_jobslots - 1; | |
f1be666c | 994 | if (jobs[js.j_lastj] || js.j_lastj == old) /* needed if old == js.j_jobslots */ |
95732b49 JA |
995 | break; |
996 | js.j_lastj--; | |
997 | } | |
998 | if (js.j_lastj == old) | |
999 | js.j_firstj = js.j_lastj = js.j_njobs = 0; | |
1000 | } | |
1001 | } | |
1002 | ||
726f6388 JA |
1003 | /* Delete all DEAD jobs that the user had received notification about. */ |
1004 | static void | |
1005 | cleanup_dead_jobs () | |
1006 | { | |
1007 | register int i; | |
7117c2d2 | 1008 | int os; |
d233b485 | 1009 | PROCESS *discard; |
726f6388 | 1010 | |
95732b49 | 1011 | if (js.j_jobslots == 0 || jobs_list_frozen) |
726f6388 JA |
1012 | return; |
1013 | ||
7117c2d2 | 1014 | QUEUE_SIGCHLD(os); |
726f6388 | 1015 | |
0628567a | 1016 | /* XXX could use js.j_firstj and js.j_lastj here */ |
95732b49 JA |
1017 | for (i = 0; i < js.j_jobslots; i++) |
1018 | { | |
1019 | #if defined (DEBUG) | |
1020 | if (i < js.j_firstj && jobs[i]) | |
1021 | itrace("cleanup_dead_jobs: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
1022 | if (i > js.j_lastj && jobs[i]) |
1023 | itrace("cleanup_dead_jobs: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 | 1024 | #endif |
726f6388 | 1025 | |
95732b49 JA |
1026 | if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i)) |
1027 | delete_job (i, 0); | |
1028 | } | |
17345e5a | 1029 | |
a0c0a00f CR |
1030 | #if defined (PROCESS_SUBSTITUTION) |
1031 | if (last_procsub_child && last_procsub_child->running == PS_DONE) | |
1032 | { | |
1033 | bgp_add (last_procsub_child->pid, process_exit_status (last_procsub_child->status)); /* XXX */ | |
d233b485 | 1034 | discard = last_procsub_child; |
a0c0a00f | 1035 | last_procsub_child = (PROCESS *)NULL; |
d233b485 | 1036 | discard_pipeline (discard); |
a0c0a00f CR |
1037 | } |
1038 | #endif | |
1039 | ||
17345e5a JA |
1040 | #if defined (COPROCESS_SUPPORT) |
1041 | coproc_reap (); | |
1042 | #endif | |
1043 | ||
7117c2d2 JA |
1044 | UNQUEUE_SIGCHLD(os); |
1045 | } | |
1046 | ||
7117c2d2 | 1047 | static int |
95732b49 | 1048 | processes_in_job (job) |
3185942a | 1049 | int job; |
7117c2d2 | 1050 | { |
95732b49 JA |
1051 | int nproc; |
1052 | register PROCESS *p; | |
7117c2d2 | 1053 | |
95732b49 JA |
1054 | nproc = 0; |
1055 | p = jobs[job]->pipe; | |
1056 | do | |
1057 | { | |
1058 | p = p->next; | |
1059 | nproc++; | |
1060 | } | |
1061 | while (p != jobs[job]->pipe); | |
7117c2d2 | 1062 | |
95732b49 JA |
1063 | return nproc; |
1064 | } | |
7117c2d2 | 1065 | |
0628567a JA |
1066 | static void |
1067 | delete_old_job (pid) | |
1068 | pid_t pid; | |
1069 | { | |
1070 | PROCESS *p; | |
1071 | int job; | |
1072 | ||
1073 | job = find_job (pid, 0, &p); | |
1074 | if (job != NO_JOB) | |
1075 | { | |
1076 | #ifdef DEBUG | |
1077 | itrace ("delete_old_job: found pid %d in job %d with state %d", pid, job, jobs[job]->state); | |
1078 | #endif | |
1079 | if (JOBSTATE (job) == JDEAD) | |
1080 | delete_job (job, DEL_NOBGPID); | |
1081 | else | |
1082 | { | |
ac50fbac CR |
1083 | #ifdef DEBUG |
1084 | internal_warning (_("forked pid %d appears in running job %d"), pid, job+1); | |
1085 | #endif | |
0628567a JA |
1086 | if (p) |
1087 | p->pid = 0; | |
1088 | } | |
1089 | } | |
1090 | } | |
1091 | ||
95732b49 JA |
1092 | /* Reallocate and compress the jobs list. This returns with a jobs array |
1093 | whose size is a multiple of JOB_SLOTS and can hold the current number of | |
1094 | jobs. Heuristics are used to minimize the number of new reallocs. */ | |
1095 | static void | |
1096 | realloc_jobs_list () | |
1097 | { | |
1098 | sigset_t set, oset; | |
0628567a | 1099 | int nsize, i, j, ncur, nprev; |
95732b49 | 1100 | JOB **nlist; |
7117c2d2 | 1101 | |
0628567a | 1102 | ncur = nprev = NO_JOB; |
95732b49 JA |
1103 | nsize = ((js.j_njobs + JOB_SLOTS - 1) / JOB_SLOTS); |
1104 | nsize *= JOB_SLOTS; | |
1105 | i = js.j_njobs % JOB_SLOTS; | |
1106 | if (i == 0 || i > (JOB_SLOTS >> 1)) | |
1107 | nsize += JOB_SLOTS; | |
7117c2d2 | 1108 | |
95732b49 | 1109 | BLOCK_CHILD (set, oset); |
0628567a JA |
1110 | nlist = (js.j_jobslots == nsize) ? jobs : (JOB **) xmalloc (nsize * sizeof (JOB *)); |
1111 | ||
3185942a | 1112 | js.c_reaped = js.j_ndead = 0; |
95732b49 | 1113 | for (i = j = 0; i < js.j_jobslots; i++) |
7117c2d2 | 1114 | if (jobs[i]) |
0628567a JA |
1115 | { |
1116 | if (i == js.j_current) | |
1117 | ncur = j; | |
1118 | if (i == js.j_previous) | |
1119 | nprev = j; | |
1120 | nlist[j++] = jobs[i]; | |
3185942a JA |
1121 | if (jobs[i]->state == JDEAD) |
1122 | { | |
1123 | js.j_ndead++; | |
1124 | js.c_reaped += processes_in_job (i); | |
1125 | } | |
0628567a JA |
1126 | } |
1127 | ||
ac50fbac | 1128 | #if 0 |
0628567a JA |
1129 | itrace ("realloc_jobs_list: resize jobs list from %d to %d", js.j_jobslots, nsize); |
1130 | itrace ("realloc_jobs_list: j_lastj changed from %d to %d", js.j_lastj, (j > 0) ? j - 1 : 0); | |
3185942a JA |
1131 | itrace ("realloc_jobs_list: j_njobs changed from %d to %d", js.j_njobs, j); |
1132 | itrace ("realloc_jobs_list: js.j_ndead %d js.c_reaped %d", js.j_ndead, js.c_reaped); | |
0628567a | 1133 | #endif |
7117c2d2 | 1134 | |
95732b49 | 1135 | js.j_firstj = 0; |
0628567a JA |
1136 | js.j_lastj = (j > 0) ? j - 1 : 0; |
1137 | js.j_njobs = j; | |
95732b49 | 1138 | js.j_jobslots = nsize; |
7117c2d2 | 1139 | |
0628567a JA |
1140 | /* Zero out remaining slots in new jobs list */ |
1141 | for ( ; j < nsize; j++) | |
1142 | nlist[j] = (JOB *)NULL; | |
1143 | ||
1144 | if (jobs != nlist) | |
1145 | { | |
1146 | free (jobs); | |
1147 | jobs = nlist; | |
1148 | } | |
1149 | ||
1150 | if (ncur != NO_JOB) | |
1151 | js.j_current = ncur; | |
1152 | if (nprev != NO_JOB) | |
1153 | js.j_previous = nprev; | |
1154 | ||
1155 | /* Need to reset these */ | |
1156 | if (js.j_current == NO_JOB || js.j_previous == NO_JOB || js.j_current > js.j_lastj || js.j_previous > js.j_lastj) | |
1157 | reset_current (); | |
1158 | ||
ac50fbac | 1159 | #if 0 |
0628567a JA |
1160 | itrace ("realloc_jobs_list: reset js.j_current (%d) and js.j_previous (%d)", js.j_current, js.j_previous); |
1161 | #endif | |
7117c2d2 | 1162 | |
726f6388 | 1163 | UNBLOCK_CHILD (oset); |
95732b49 JA |
1164 | } |
1165 | ||
ac50fbac | 1166 | /* Compact the jobs list by removing dead jobs. Assume that we have filled |
95732b49 JA |
1167 | the jobs array to some predefined maximum. Called when the shell is not |
1168 | the foreground process (subshell_environment != 0). Returns the first | |
1169 | available slot in the compacted list. If that value is js.j_jobslots, then | |
0628567a | 1170 | the list needs to be reallocated. The jobs array may be in new memory if |
95732b49 JA |
1171 | this returns > 0 and < js.j_jobslots. FLAGS is reserved for future use. */ |
1172 | static int | |
1173 | compact_jobs_list (flags) | |
1174 | int flags; | |
1175 | { | |
1176 | if (js.j_jobslots == 0 || jobs_list_frozen) | |
1177 | return js.j_jobslots; | |
1178 | ||
1179 | reap_dead_jobs (); | |
1180 | realloc_jobs_list (); | |
7117c2d2 | 1181 | |
ac50fbac | 1182 | #if 0 |
f1be666c JA |
1183 | itrace("compact_jobs_list: returning %d", (js.j_lastj || jobs[js.j_lastj]) ? js.j_lastj + 1 : 0); |
1184 | #endif | |
1185 | ||
1186 | return ((js.j_lastj || jobs[js.j_lastj]) ? js.j_lastj + 1 : 0); | |
726f6388 JA |
1187 | } |
1188 | ||
1189 | /* Delete the job at INDEX from the job list. Must be called | |
1190 | with SIGCHLD blocked. */ | |
1191 | void | |
0628567a JA |
1192 | delete_job (job_index, dflags) |
1193 | int job_index, dflags; | |
726f6388 JA |
1194 | { |
1195 | register JOB *temp; | |
95732b49 | 1196 | PROCESS *proc; |
0628567a | 1197 | int ndel; |
726f6388 | 1198 | |
95732b49 | 1199 | if (js.j_jobslots == 0 || jobs_list_frozen) |
726f6388 JA |
1200 | return; |
1201 | ||
0628567a | 1202 | if ((dflags & DEL_WARNSTOPPED) && subshell_environment == 0 && STOPPED (job_index)) |
b80f6443 | 1203 | internal_warning (_("deleting stopped job %d with process group %ld"), job_index+1, (long)jobs[job_index]->pgrp); |
726f6388 | 1204 | temp = jobs[job_index]; |
0628567a JA |
1205 | if (temp == 0) |
1206 | return; | |
726f6388 | 1207 | |
a0c0a00f | 1208 | if ((dflags & DEL_NOBGPID) == 0 && (temp->flags & (J_ASYNC|J_FOREGROUND)) == J_ASYNC) |
0628567a JA |
1209 | { |
1210 | proc = find_last_proc (job_index, 0); | |
0628567a JA |
1211 | if (proc) |
1212 | bgp_add (proc->pid, process_exit_status (proc->status)); | |
1213 | } | |
95732b49 | 1214 | |
726f6388 | 1215 | jobs[job_index] = (JOB *)NULL; |
95732b49 JA |
1216 | if (temp == js.j_lastmade) |
1217 | js.j_lastmade = 0; | |
1218 | else if (temp == js.j_lastasync) | |
1219 | js.j_lastasync = 0; | |
1220 | ||
726f6388 | 1221 | free (temp->wd); |
95732b49 JA |
1222 | ndel = discard_pipeline (temp->pipe); |
1223 | ||
1224 | js.c_injobs -= ndel; | |
1225 | if (temp->state == JDEAD) | |
1226 | { | |
1227 | js.c_reaped -= ndel; | |
1228 | js.j_ndead--; | |
1229 | if (js.c_reaped < 0) | |
1230 | { | |
1231 | #ifdef DEBUG | |
1232 | itrace("delete_job (%d pgrp %d): js.c_reaped (%d) < 0 ndel = %d js.j_ndead = %d", job_index, temp->pgrp, js.c_reaped, ndel, js.j_ndead); | |
1233 | #endif | |
1234 | js.c_reaped = 0; | |
1235 | } | |
1236 | } | |
726f6388 JA |
1237 | |
1238 | if (temp->deferred) | |
1239 | dispose_command (temp->deferred); | |
1240 | ||
1241 | free (temp); | |
95732b49 JA |
1242 | |
1243 | js.j_njobs--; | |
1244 | if (js.j_njobs == 0) | |
1245 | js.j_firstj = js.j_lastj = 0; | |
1246 | else if (jobs[js.j_firstj] == 0 || jobs[js.j_lastj] == 0) | |
1247 | reset_job_indices (); | |
f1be666c JA |
1248 | |
1249 | if (job_index == js.j_current || job_index == js.j_previous) | |
1250 | reset_current (); | |
726f6388 JA |
1251 | } |
1252 | ||
ccc6cda3 JA |
1253 | /* Must be called with SIGCHLD blocked. */ |
1254 | void | |
1255 | nohup_job (job_index) | |
1256 | int job_index; | |
1257 | { | |
1258 | register JOB *temp; | |
1259 | ||
95732b49 | 1260 | if (js.j_jobslots == 0) |
cce855bc JA |
1261 | return; |
1262 | ||
d166f048 | 1263 | if (temp = jobs[job_index]) |
ccc6cda3 JA |
1264 | temp->flags |= J_NOHUP; |
1265 | } | |
1266 | ||
726f6388 | 1267 | /* Get rid of the data structure associated with a process chain. */ |
a0c0a00f | 1268 | int |
726f6388 JA |
1269 | discard_pipeline (chain) |
1270 | register PROCESS *chain; | |
1271 | { | |
1272 | register PROCESS *this, *next; | |
95732b49 | 1273 | int n; |
726f6388 JA |
1274 | |
1275 | this = chain; | |
95732b49 | 1276 | n = 0; |
726f6388 JA |
1277 | do |
1278 | { | |
1279 | next = this->next; | |
ccc6cda3 | 1280 | FREE (this->command); |
726f6388 | 1281 | free (this); |
95732b49 | 1282 | n++; |
726f6388 JA |
1283 | this = next; |
1284 | } | |
1285 | while (this != chain); | |
95732b49 JA |
1286 | |
1287 | return n; | |
726f6388 JA |
1288 | } |
1289 | ||
1290 | /* Add this process to the chain being built in the_pipeline. | |
1291 | NAME is the command string that will be exec'ed later. | |
1292 | PID is the process id of the child. */ | |
1293 | static void | |
1294 | add_process (name, pid) | |
1295 | char *name; | |
1296 | pid_t pid; | |
1297 | { | |
ccc6cda3 | 1298 | PROCESS *t, *p; |
726f6388 | 1299 | |
95732b49 JA |
1300 | #if defined (RECYCLES_PIDS) |
1301 | int j; | |
1302 | p = find_process (pid, 0, &j); | |
1303 | if (p) | |
1304 | { | |
1305 | # ifdef DEBUG | |
1306 | if (j == NO_JOB) | |
3185942a | 1307 | internal_warning (_("add_process: process %5ld (%s) in the_pipeline"), (long)p->pid, p->command); |
95732b49 JA |
1308 | # endif |
1309 | if (PALIVE (p)) | |
3185942a | 1310 | internal_warning (_("add_process: pid %5ld (%s) marked as still alive"), (long)p->pid, p->command); |
95732b49 JA |
1311 | p->running = PS_RECYCLED; /* mark as recycled */ |
1312 | } | |
1313 | #endif | |
1314 | ||
ccc6cda3 | 1315 | t = (PROCESS *)xmalloc (sizeof (PROCESS)); |
726f6388 JA |
1316 | t->next = the_pipeline; |
1317 | t->pid = pid; | |
1318 | WSTATUS (t->status) = 0; | |
7117c2d2 | 1319 | t->running = PS_RUNNING; |
726f6388 JA |
1320 | t->command = name; |
1321 | the_pipeline = t; | |
1322 | ||
ccc6cda3 | 1323 | if (t->next == 0) |
726f6388 JA |
1324 | t->next = t; |
1325 | else | |
1326 | { | |
ccc6cda3 | 1327 | p = t->next; |
726f6388 JA |
1328 | while (p->next != t->next) |
1329 | p = p->next; | |
1330 | p->next = t; | |
1331 | } | |
1332 | } | |
1333 | ||
495aee44 CR |
1334 | /* Create a (dummy) PROCESS with NAME, PID, and STATUS, and make it the last |
1335 | process in jobs[JID]->pipe. Used by the lastpipe code. */ | |
1336 | void | |
1337 | append_process (name, pid, status, jid) | |
1338 | char *name; | |
1339 | pid_t pid; | |
1340 | int status; | |
1341 | int jid; | |
1342 | { | |
1343 | PROCESS *t, *p; | |
1344 | ||
1345 | t = (PROCESS *)xmalloc (sizeof (PROCESS)); | |
1346 | t->next = (PROCESS *)NULL; | |
1347 | t->pid = pid; | |
1348 | /* set process exit status using offset discovered by configure */ | |
1349 | t->status = (status & 0xff) << WEXITSTATUS_OFFSET; | |
1350 | t->running = PS_DONE; | |
1351 | t->command = name; | |
1352 | ||
1353 | js.c_reaped++; /* XXX */ | |
1354 | ||
1355 | for (p = jobs[jid]->pipe; p->next != jobs[jid]->pipe; p = p->next) | |
1356 | ; | |
1357 | p->next = t; | |
1358 | t->next = jobs[jid]->pipe; | |
1359 | } | |
1360 | ||
726f6388 JA |
1361 | #if 0 |
1362 | /* Take the last job and make it the first job. Must be called with | |
1363 | SIGCHLD blocked. */ | |
ccc6cda3 | 1364 | int |
726f6388 JA |
1365 | rotate_the_pipeline () |
1366 | { | |
1367 | PROCESS *p; | |
1368 | ||
1369 | if (the_pipeline->next == the_pipeline) | |
1370 | return; | |
1371 | for (p = the_pipeline; p->next != the_pipeline; p = p->next) | |
1372 | ; | |
1373 | the_pipeline = p; | |
1374 | } | |
1375 | ||
1376 | /* Reverse the order of the processes in the_pipeline. Must be called with | |
1377 | SIGCHLD blocked. */ | |
ccc6cda3 | 1378 | int |
726f6388 JA |
1379 | reverse_the_pipeline () |
1380 | { | |
1381 | PROCESS *p, *n; | |
1382 | ||
1383 | if (the_pipeline->next == the_pipeline) | |
1384 | return; | |
1385 | ||
1386 | for (p = the_pipeline; p->next != the_pipeline; p = p->next) | |
1387 | ; | |
1388 | p->next = (PROCESS *)NULL; | |
1389 | ||
1390 | n = REVERSE_LIST (the_pipeline, PROCESS *); | |
1391 | ||
1392 | the_pipeline = n; | |
1393 | for (p = the_pipeline; p->next; p = p->next) | |
1394 | ; | |
1395 | p->next = the_pipeline; | |
1396 | } | |
1397 | #endif | |
1398 | ||
1399 | /* Map FUNC over the list of jobs. If FUNC returns non-zero, | |
1400 | then it is time to stop mapping, and that is the return value | |
1401 | for map_over_jobs. FUNC is called with a JOB, arg1, arg2, | |
1402 | and INDEX. */ | |
1403 | static int | |
1404 | map_over_jobs (func, arg1, arg2) | |
f73dda09 | 1405 | sh_job_map_func_t *func; |
726f6388 JA |
1406 | int arg1, arg2; |
1407 | { | |
1408 | register int i; | |
1409 | int result; | |
1410 | sigset_t set, oset; | |
1411 | ||
95732b49 | 1412 | if (js.j_jobslots == 0) |
28ef6c31 JA |
1413 | return 0; |
1414 | ||
726f6388 | 1415 | BLOCK_CHILD (set, oset); |
726f6388 | 1416 | |
95732b49 JA |
1417 | /* XXX could use js.j_firstj here */ |
1418 | for (i = result = 0; i < js.j_jobslots; i++) | |
726f6388 | 1419 | { |
95732b49 JA |
1420 | #if defined (DEBUG) |
1421 | if (i < js.j_firstj && jobs[i]) | |
1422 | itrace("map_over_jobs: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
1423 | if (i > js.j_lastj && jobs[i]) |
1424 | itrace("map_over_jobs: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 | 1425 | #endif |
726f6388 JA |
1426 | if (jobs[i]) |
1427 | { | |
1428 | result = (*func)(jobs[i], arg1, arg2, i); | |
1429 | if (result) | |
1430 | break; | |
1431 | } | |
1432 | } | |
1433 | ||
1434 | UNBLOCK_CHILD (oset); | |
ccc6cda3 | 1435 | |
726f6388 JA |
1436 | return (result); |
1437 | } | |
1438 | ||
1439 | /* Cause all the jobs in the current pipeline to exit. */ | |
1440 | void | |
1441 | terminate_current_pipeline () | |
1442 | { | |
1443 | if (pipeline_pgrp && pipeline_pgrp != shell_pgrp) | |
1444 | { | |
1445 | killpg (pipeline_pgrp, SIGTERM); | |
1446 | killpg (pipeline_pgrp, SIGCONT); | |
1447 | } | |
1448 | } | |
1449 | ||
1450 | /* Cause all stopped jobs to exit. */ | |
1451 | void | |
1452 | terminate_stopped_jobs () | |
1453 | { | |
1454 | register int i; | |
1455 | ||
95732b49 JA |
1456 | /* XXX could use js.j_firstj here */ |
1457 | for (i = 0; i < js.j_jobslots; i++) | |
726f6388 | 1458 | { |
ccc6cda3 | 1459 | if (jobs[i] && STOPPED (i)) |
726f6388 JA |
1460 | { |
1461 | killpg (jobs[i]->pgrp, SIGTERM); | |
1462 | killpg (jobs[i]->pgrp, SIGCONT); | |
1463 | } | |
1464 | } | |
1465 | } | |
1466 | ||
ccc6cda3 JA |
1467 | /* Cause all jobs, running or stopped, to receive a hangup signal. If |
1468 | a job is marked J_NOHUP, don't send the SIGHUP. */ | |
726f6388 JA |
1469 | void |
1470 | hangup_all_jobs () | |
1471 | { | |
1472 | register int i; | |
1473 | ||
95732b49 JA |
1474 | /* XXX could use js.j_firstj here */ |
1475 | for (i = 0; i < js.j_jobslots; i++) | |
726f6388 JA |
1476 | { |
1477 | if (jobs[i]) | |
1478 | { | |
0628567a JA |
1479 | if (jobs[i]->flags & J_NOHUP) |
1480 | continue; | |
1481 | killpg (jobs[i]->pgrp, SIGHUP); | |
ccc6cda3 | 1482 | if (STOPPED (i)) |
726f6388 JA |
1483 | killpg (jobs[i]->pgrp, SIGCONT); |
1484 | } | |
1485 | } | |
1486 | } | |
1487 | ||
1488 | void | |
1489 | kill_current_pipeline () | |
1490 | { | |
1491 | stop_making_children (); | |
1492 | start_pipeline (); | |
1493 | } | |
1494 | ||
1495 | /* Return the pipeline that PID belongs to. Note that the pipeline | |
95732b49 JA |
1496 | doesn't have to belong to a job. Must be called with SIGCHLD blocked. |
1497 | If JOBP is non-null, return the index of the job containing PID. */ | |
726f6388 | 1498 | static PROCESS * |
95732b49 | 1499 | find_pipeline (pid, alive_only, jobp) |
726f6388 | 1500 | pid_t pid; |
95732b49 | 1501 | int alive_only; |
7117c2d2 | 1502 | int *jobp; /* index into jobs list or NO_JOB */ |
726f6388 JA |
1503 | { |
1504 | int job; | |
95732b49 | 1505 | PROCESS *p; |
726f6388 JA |
1506 | |
1507 | /* See if this process is in the pipeline that we are building. */ | |
7117c2d2 JA |
1508 | if (jobp) |
1509 | *jobp = NO_JOB; | |
726f6388 JA |
1510 | if (the_pipeline) |
1511 | { | |
ccc6cda3 | 1512 | p = the_pipeline; |
726f6388 JA |
1513 | do |
1514 | { | |
95732b49 JA |
1515 | /* Return it if we found it. Don't ever return a recycled pid. */ |
1516 | if (p->pid == pid && ((alive_only == 0 && PRECYCLED(p) == 0) || PALIVE(p))) | |
1517 | return (p); | |
726f6388 JA |
1518 | |
1519 | p = p->next; | |
1520 | } | |
1521 | while (p != the_pipeline); | |
1522 | } | |
a0c0a00f CR |
1523 | /* Now look in the last process substitution pipeline, since that sets $! */ |
1524 | if (last_procsub_child) | |
1525 | { | |
1526 | p = last_procsub_child; | |
1527 | do | |
1528 | { | |
1529 | /* Return it if we found it. Don't ever return a recycled pid. */ | |
1530 | if (p->pid == pid && ((alive_only == 0 && PRECYCLED(p) == 0) || PALIVE(p))) | |
1531 | return (p); | |
1532 | ||
1533 | p = p->next; | |
1534 | } | |
1535 | while (p != last_procsub_child); | |
1536 | } | |
726f6388 | 1537 | |
95732b49 | 1538 | job = find_job (pid, alive_only, &p); |
7117c2d2 JA |
1539 | if (jobp) |
1540 | *jobp = job; | |
ccc6cda3 | 1541 | return (job == NO_JOB) ? (PROCESS *)NULL : jobs[job]->pipe; |
726f6388 JA |
1542 | } |
1543 | ||
95732b49 JA |
1544 | /* Return the PROCESS * describing PID. If JOBP is non-null return the index |
1545 | into the jobs array of the job containing PID. Must be called with | |
1546 | SIGCHLD blocked. */ | |
1547 | static PROCESS * | |
1548 | find_process (pid, alive_only, jobp) | |
1549 | pid_t pid; | |
1550 | int alive_only; | |
1551 | int *jobp; /* index into jobs list or NO_JOB */ | |
1552 | { | |
1553 | PROCESS *p; | |
1554 | ||
1555 | p = find_pipeline (pid, alive_only, jobp); | |
1556 | while (p && p->pid != pid) | |
1557 | p = p->next; | |
1558 | return p; | |
1559 | } | |
1560 | ||
726f6388 JA |
1561 | /* Return the job index that PID belongs to, or NO_JOB if it doesn't |
1562 | belong to any job. Must be called with SIGCHLD blocked. */ | |
1563 | static int | |
95732b49 | 1564 | find_job (pid, alive_only, procp) |
726f6388 | 1565 | pid_t pid; |
95732b49 JA |
1566 | int alive_only; |
1567 | PROCESS **procp; | |
726f6388 JA |
1568 | { |
1569 | register int i; | |
95732b49 | 1570 | PROCESS *p; |
726f6388 | 1571 | |
0628567a | 1572 | /* XXX could use js.j_firstj here, and should check js.j_lastj */ |
95732b49 | 1573 | for (i = 0; i < js.j_jobslots; i++) |
726f6388 | 1574 | { |
95732b49 JA |
1575 | #if defined (DEBUG) |
1576 | if (i < js.j_firstj && jobs[i]) | |
1577 | itrace("find_job: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
1578 | if (i > js.j_lastj && jobs[i]) |
1579 | itrace("find_job: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 | 1580 | #endif |
726f6388 JA |
1581 | if (jobs[i]) |
1582 | { | |
1583 | p = jobs[i]->pipe; | |
1584 | ||
1585 | do | |
1586 | { | |
95732b49 | 1587 | if (p->pid == pid && ((alive_only == 0 && PRECYCLED(p) == 0) || PALIVE(p))) |
7117c2d2 | 1588 | { |
95732b49 JA |
1589 | if (procp) |
1590 | *procp = p; | |
1591 | return (i); | |
7117c2d2 | 1592 | } |
726f6388 JA |
1593 | |
1594 | p = p->next; | |
1595 | } | |
1596 | while (p != jobs[i]->pipe); | |
1597 | } | |
1598 | } | |
1599 | ||
1600 | return (NO_JOB); | |
1601 | } | |
1602 | ||
d166f048 JA |
1603 | /* Find a job given a PID. If BLOCK is non-zero, block SIGCHLD as |
1604 | required by find_job. */ | |
1605 | int | |
1606 | get_job_by_pid (pid, block) | |
1607 | pid_t pid; | |
1608 | int block; | |
1609 | { | |
1610 | int job; | |
1611 | sigset_t set, oset; | |
1612 | ||
1613 | if (block) | |
1614 | BLOCK_CHILD (set, oset); | |
7117c2d2 | 1615 | |
95732b49 | 1616 | job = find_job (pid, 0, NULL); |
7117c2d2 | 1617 | |
d166f048 JA |
1618 | if (block) |
1619 | UNBLOCK_CHILD (oset); | |
1620 | ||
1621 | return job; | |
1622 | } | |
1623 | ||
726f6388 JA |
1624 | /* Print descriptive information about the job with leader pid PID. */ |
1625 | void | |
1626 | describe_pid (pid) | |
1627 | pid_t pid; | |
1628 | { | |
1629 | int job; | |
1630 | sigset_t set, oset; | |
1631 | ||
1632 | BLOCK_CHILD (set, oset); | |
1633 | ||
95732b49 | 1634 | job = find_job (pid, 0, NULL); |
726f6388 JA |
1635 | |
1636 | if (job != NO_JOB) | |
95732b49 | 1637 | fprintf (stderr, "[%d] %ld\n", job + 1, (long)pid); |
726f6388 | 1638 | else |
b80f6443 | 1639 | programming_error (_("describe_pid: %ld: no such pid"), (long)pid); |
726f6388 JA |
1640 | |
1641 | UNBLOCK_CHILD (oset); | |
1642 | } | |
1643 | ||
b80f6443 JA |
1644 | static char * |
1645 | j_strsignal (s) | |
1646 | int s; | |
1647 | { | |
1648 | char *x; | |
1649 | ||
1650 | x = strsignal (s); | |
1651 | if (x == 0) | |
1652 | { | |
1653 | x = retcode_name_buffer; | |
a0c0a00f | 1654 | snprintf (x, sizeof(retcode_name_buffer), _("Signal %d"), s); |
b80f6443 JA |
1655 | } |
1656 | return x; | |
1657 | } | |
1658 | ||
28ef6c31 JA |
1659 | static char * |
1660 | printable_job_status (j, p, format) | |
1661 | int j; | |
1662 | PROCESS *p; | |
1663 | int format; | |
1664 | { | |
1665 | static char *temp; | |
1666 | int es; | |
1667 | ||
3185942a | 1668 | temp = _("Done"); |
28ef6c31 JA |
1669 | |
1670 | if (STOPPED (j) && format == 0) | |
1671 | { | |
1672 | if (posixly_correct == 0 || p == 0 || (WIFSTOPPED (p->status) == 0)) | |
3185942a | 1673 | temp = _("Stopped"); |
28ef6c31 JA |
1674 | else |
1675 | { | |
1676 | temp = retcode_name_buffer; | |
a0c0a00f | 1677 | snprintf (temp, sizeof(retcode_name_buffer), _("Stopped(%s)"), signal_name (WSTOPSIG (p->status))); |
28ef6c31 JA |
1678 | } |
1679 | } | |
1680 | else if (RUNNING (j)) | |
3185942a | 1681 | temp = _("Running"); |
28ef6c31 JA |
1682 | else |
1683 | { | |
1684 | if (WIFSTOPPED (p->status)) | |
b80f6443 | 1685 | temp = j_strsignal (WSTOPSIG (p->status)); |
28ef6c31 | 1686 | else if (WIFSIGNALED (p->status)) |
b80f6443 | 1687 | temp = j_strsignal (WTERMSIG (p->status)); |
28ef6c31 JA |
1688 | else if (WIFEXITED (p->status)) |
1689 | { | |
1690 | temp = retcode_name_buffer; | |
1691 | es = WEXITSTATUS (p->status); | |
1692 | if (es == 0) | |
a0c0a00f CR |
1693 | { |
1694 | strncpy (temp, _("Done"), sizeof (retcode_name_buffer) - 1); | |
1695 | temp[sizeof (retcode_name_buffer) - 1] = '\0'; | |
1696 | } | |
28ef6c31 | 1697 | else if (posixly_correct) |
a0c0a00f | 1698 | snprintf (temp, sizeof(retcode_name_buffer), _("Done(%d)"), es); |
28ef6c31 | 1699 | else |
a0c0a00f | 1700 | snprintf (temp, sizeof(retcode_name_buffer), _("Exit %d"), es); |
28ef6c31 JA |
1701 | } |
1702 | else | |
3185942a | 1703 | temp = _("Unknown status"); |
28ef6c31 JA |
1704 | } |
1705 | ||
1706 | return temp; | |
1707 | } | |
1708 | ||
726f6388 JA |
1709 | /* This is the way to print out information on a job if you |
1710 | know the index. FORMAT is: | |
1711 | ||
1712 | JLIST_NORMAL) [1]+ Running emacs | |
1713 | JLIST_LONG ) [1]+ 2378 Running emacs | |
1714 | -1 ) [1]+ 2378 emacs | |
1715 | ||
1716 | JLIST_NORMAL) [1]+ Stopped ls | more | |
1717 | JLIST_LONG ) [1]+ 2369 Stopped ls | |
1718 | 2367 | more | |
1719 | JLIST_PID_ONLY) | |
1720 | Just list the pid of the process group leader (really | |
1721 | the process group). | |
1722 | JLIST_CHANGED_ONLY) | |
1723 | Use format JLIST_NORMAL, but list only jobs about which | |
1724 | the user has not been notified. */ | |
ccc6cda3 JA |
1725 | |
1726 | /* Print status for pipeline P. If JOB_INDEX is >= 0, it is the index into | |
1727 | the JOBS array corresponding to this pipeline. FORMAT is as described | |
1728 | above. Must be called with SIGCHLD blocked. | |
1729 | ||
1730 | If you're printing a pipeline that's not in the jobs array, like the | |
1731 | current pipeline as it's being created, pass -1 for JOB_INDEX */ | |
726f6388 | 1732 | static void |
ccc6cda3 JA |
1733 | print_pipeline (p, job_index, format, stream) |
1734 | PROCESS *p; | |
726f6388 JA |
1735 | int job_index, format; |
1736 | FILE *stream; | |
1737 | { | |
ccc6cda3 JA |
1738 | PROCESS *first, *last, *show; |
1739 | int es, name_padding; | |
28ef6c31 | 1740 | char *temp; |
726f6388 | 1741 | |
ccc6cda3 JA |
1742 | if (p == 0) |
1743 | return; | |
726f6388 | 1744 | |
ccc6cda3 | 1745 | first = last = p; |
726f6388 JA |
1746 | while (last->next != first) |
1747 | last = last->next; | |
1748 | ||
726f6388 JA |
1749 | for (;;) |
1750 | { | |
1751 | if (p != first) | |
1752 | fprintf (stream, format ? " " : " |"); | |
1753 | ||
ccc6cda3 | 1754 | if (format != JLIST_STANDARD) |
f73dda09 | 1755 | fprintf (stream, "%5ld", (long)p->pid); |
726f6388 JA |
1756 | |
1757 | fprintf (stream, " "); | |
1758 | ||
ccc6cda3 | 1759 | if (format > -1 && job_index >= 0) |
726f6388 | 1760 | { |
ccc6cda3 | 1761 | show = format ? p : last; |
28ef6c31 | 1762 | temp = printable_job_status (job_index, show, format); |
726f6388 JA |
1763 | |
1764 | if (p != first) | |
1765 | { | |
1766 | if (format) | |
1767 | { | |
1768 | if (show->running == first->running && | |
1769 | WSTATUS (show->status) == WSTATUS (first->status)) | |
1770 | temp = ""; | |
1771 | } | |
1772 | else | |
1773 | temp = (char *)NULL; | |
1774 | } | |
1775 | ||
1776 | if (temp) | |
1777 | { | |
726f6388 JA |
1778 | fprintf (stream, "%s", temp); |
1779 | ||
ccc6cda3 JA |
1780 | es = STRLEN (temp); |
1781 | if (es == 0) | |
28ef6c31 | 1782 | es = 2; /* strlen ("| ") */ |
ccc6cda3 | 1783 | name_padding = LONGEST_SIGNAL_DESC - es; |
726f6388 JA |
1784 | |
1785 | fprintf (stream, "%*s", name_padding, ""); | |
1786 | ||
f73dda09 JA |
1787 | if ((WIFSTOPPED (show->status) == 0) && |
1788 | (WIFCONTINUED (show->status) == 0) && | |
1789 | WIFCORED (show->status)) | |
3185942a | 1790 | fprintf (stream, _("(core dumped) ")); |
726f6388 JA |
1791 | } |
1792 | } | |
1793 | ||
1794 | if (p != first && format) | |
1795 | fprintf (stream, "| "); | |
1796 | ||
1797 | if (p->command) | |
1798 | fprintf (stream, "%s", p->command); | |
1799 | ||
ccc6cda3 | 1800 | if (p == last && job_index >= 0) |
726f6388 | 1801 | { |
ccc6cda3 | 1802 | temp = current_working_directory (); |
726f6388 | 1803 | |
ccc6cda3 | 1804 | if (RUNNING (job_index) && (IS_FOREGROUND (job_index) == 0)) |
726f6388 JA |
1805 | fprintf (stream, " &"); |
1806 | ||
ccc6cda3 | 1807 | if (strcmp (temp, jobs[job_index]->wd) != 0) |
726f6388 | 1808 | fprintf (stream, |
3185942a | 1809 | _(" (wd: %s)"), polite_directory_format (jobs[job_index]->wd)); |
726f6388 JA |
1810 | } |
1811 | ||
1812 | if (format || (p == last)) | |
cce855bc JA |
1813 | { |
1814 | /* We need to add a CR only if this is an interactive shell, and | |
1815 | we're reporting the status of a completed job asynchronously. | |
1816 | We can't really check whether this particular job is being | |
1817 | reported asynchronously, so just add the CR if the shell is | |
1818 | currently interactive and asynchronous notification is enabled. */ | |
1819 | if (asynchronous_notification && interactive) | |
1820 | fprintf (stream, "\r\n"); | |
1821 | else | |
1822 | fprintf (stream, "\n"); | |
1823 | } | |
726f6388 JA |
1824 | |
1825 | if (p == last) | |
1826 | break; | |
1827 | p = p->next; | |
1828 | } | |
726f6388 | 1829 | fflush (stream); |
ccc6cda3 JA |
1830 | } |
1831 | ||
7117c2d2 JA |
1832 | /* Print information to STREAM about jobs[JOB_INDEX] according to FORMAT. |
1833 | Must be called with SIGCHLD blocked or queued with queue_sigchld */ | |
ccc6cda3 JA |
1834 | static void |
1835 | pretty_print_job (job_index, format, stream) | |
1836 | int job_index, format; | |
1837 | FILE *stream; | |
1838 | { | |
1839 | register PROCESS *p; | |
ccc6cda3 JA |
1840 | |
1841 | /* Format only pid information about the process group leader? */ | |
1842 | if (format == JLIST_PID_ONLY) | |
1843 | { | |
f73dda09 | 1844 | fprintf (stream, "%ld\n", (long)jobs[job_index]->pipe->pid); |
ccc6cda3 JA |
1845 | return; |
1846 | } | |
1847 | ||
1848 | if (format == JLIST_CHANGED_ONLY) | |
1849 | { | |
1850 | if (IS_NOTIFIED (job_index)) | |
7117c2d2 | 1851 | return; |
ccc6cda3 JA |
1852 | format = JLIST_STANDARD; |
1853 | } | |
1854 | ||
1855 | if (format != JLIST_NONINTERACTIVE) | |
1856 | fprintf (stream, "[%d]%c ", job_index + 1, | |
95732b49 JA |
1857 | (job_index == js.j_current) ? '+': |
1858 | (job_index == js.j_previous) ? '-' : ' '); | |
ccc6cda3 JA |
1859 | |
1860 | if (format == JLIST_NONINTERACTIVE) | |
1861 | format = JLIST_LONG; | |
1862 | ||
1863 | p = jobs[job_index]->pipe; | |
1864 | ||
cce855bc JA |
1865 | print_pipeline (p, job_index, format, stream); |
1866 | ||
ccc6cda3 JA |
1867 | /* We have printed information about this job. When the job's |
1868 | status changes, waitchld () sets the notification flag to 0. */ | |
1869 | jobs[job_index]->flags |= J_NOTIFIED; | |
726f6388 JA |
1870 | } |
1871 | ||
ccc6cda3 JA |
1872 | static int |
1873 | print_job (job, format, state, job_index) | |
1874 | JOB *job; | |
1875 | int format, state, job_index; | |
1876 | { | |
1877 | if (state == -1 || (JOB_STATE)state == job->state) | |
1878 | pretty_print_job (job_index, format, stdout); | |
1879 | return (0); | |
1880 | } | |
1881 | ||
1882 | void | |
726f6388 JA |
1883 | list_one_job (job, format, ignore, job_index) |
1884 | JOB *job; | |
1885 | int format, ignore, job_index; | |
1886 | { | |
7117c2d2 | 1887 | pretty_print_job (job_index, format, stdout); |
ccc6cda3 JA |
1888 | } |
1889 | ||
1890 | void | |
1891 | list_stopped_jobs (format) | |
1892 | int format; | |
1893 | { | |
1894 | cleanup_dead_jobs (); | |
1895 | map_over_jobs (print_job, format, (int)JSTOPPED); | |
1896 | } | |
1897 | ||
1898 | void | |
1899 | list_running_jobs (format) | |
1900 | int format; | |
1901 | { | |
1902 | cleanup_dead_jobs (); | |
1903 | map_over_jobs (print_job, format, (int)JRUNNING); | |
726f6388 JA |
1904 | } |
1905 | ||
1906 | /* List jobs. If FORMAT is non-zero, then the long form of the information | |
1907 | is printed, else just a short version. */ | |
1908 | void | |
ccc6cda3 | 1909 | list_all_jobs (format) |
726f6388 JA |
1910 | int format; |
1911 | { | |
1912 | cleanup_dead_jobs (); | |
ccc6cda3 | 1913 | map_over_jobs (print_job, format, -1); |
726f6388 JA |
1914 | } |
1915 | ||
1916 | /* Fork, handling errors. Returns the pid of the newly made child, or 0. | |
1917 | COMMAND is just for remembering the name of the command; we don't do | |
1918 | anything else with it. ASYNC_P says what to do with the tty. If | |
1919 | non-zero, then don't give it away. */ | |
1920 | pid_t | |
1921 | make_child (command, async_p) | |
1922 | char *command; | |
1923 | int async_p; | |
1924 | { | |
3185942a | 1925 | int forksleep; |
726f6388 JA |
1926 | sigset_t set, oset; |
1927 | pid_t pid; | |
1928 | ||
ac50fbac CR |
1929 | /* XXX - block SIGTERM here and unblock in child after fork resets the |
1930 | set of pending signals? */ | |
726f6388 JA |
1931 | sigemptyset (&set); |
1932 | sigaddset (&set, SIGCHLD); | |
1933 | sigaddset (&set, SIGINT); | |
1934 | sigemptyset (&oset); | |
1935 | sigprocmask (SIG_BLOCK, &set, &oset); | |
1936 | ||
1937 | making_children (); | |
1938 | ||
3185942a JA |
1939 | forksleep = 1; |
1940 | ||
726f6388 JA |
1941 | #if defined (BUFFERED_INPUT) |
1942 | /* If default_buffered_input is active, we are reading a script. If | |
1943 | the command is asynchronous, we have already duplicated /dev/null | |
1944 | as fd 0, but have not changed the buffered stream corresponding to | |
1945 | the old fd 0. We don't want to sync the stream in this case. */ | |
1946 | if (default_buffered_input != -1 && | |
1947 | (!async_p || default_buffered_input > 0)) | |
1948 | sync_buffered_stream (default_buffered_input); | |
1949 | #endif /* BUFFERED_INPUT */ | |
1950 | ||
ac50fbac CR |
1951 | RESET_SIGTERM; |
1952 | ||
3185942a JA |
1953 | /* Create the child, handle severe errors. Retry on EAGAIN. */ |
1954 | while ((pid = fork ()) < 0 && errno == EAGAIN && forksleep < FORKSLEEP_MAX) | |
1955 | { | |
495aee44 | 1956 | /* bash-4.2 */ |
a0c0a00f | 1957 | sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); |
0001803f CR |
1958 | /* If we can't create any children, try to reap some dead ones. */ |
1959 | waitchld (-1, 0); | |
495aee44 | 1960 | |
a0c0a00f | 1961 | errno = EAGAIN; /* restore errno */ |
3185942a | 1962 | sys_error ("fork: retry"); |
ac50fbac CR |
1963 | RESET_SIGTERM; |
1964 | ||
3185942a JA |
1965 | if (sleep (forksleep) != 0) |
1966 | break; | |
1967 | forksleep <<= 1; | |
a0c0a00f CR |
1968 | |
1969 | if (interrupt_state) | |
1970 | break; | |
1971 | sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL); | |
3185942a JA |
1972 | } |
1973 | ||
ac50fbac CR |
1974 | if (pid != 0) |
1975 | RESET_SIGTERM; | |
1976 | ||
3185942a | 1977 | if (pid < 0) |
726f6388 | 1978 | { |
ccc6cda3 | 1979 | sys_error ("fork"); |
726f6388 JA |
1980 | |
1981 | /* Kill all of the processes in the current pipeline. */ | |
1982 | terminate_current_pipeline (); | |
1983 | ||
1984 | /* Discard the current pipeline, if any. */ | |
1985 | if (the_pipeline) | |
1986 | kill_current_pipeline (); | |
1987 | ||
0001803f | 1988 | last_command_exit_value = EX_NOEXEC; |
726f6388 JA |
1989 | throw_to_top_level (); /* Reset signals, etc. */ |
1990 | } | |
1991 | ||
1992 | if (pid == 0) | |
1993 | { | |
1994 | /* In the child. Give this child the right process group, set the | |
1995 | signals to the default state for a new process. */ | |
b72432fd | 1996 | pid_t mypid; |
726f6388 | 1997 | |
d233b485 CR |
1998 | /* If this ends up being changed to modify or use `command' in the |
1999 | child process, go back and change callers who free `command' in | |
2000 | the child process when this returns. */ | |
b72432fd | 2001 | mypid = getpid (); |
726f6388 JA |
2002 | #if defined (BUFFERED_INPUT) |
2003 | /* Close default_buffered_input if it's > 0. We don't close it if it's | |
2004 | 0 because that's the file descriptor used when redirecting input, | |
2005 | and it's wrong to close the file in that case. */ | |
cce855bc | 2006 | unset_bash_input (0); |
726f6388 JA |
2007 | #endif /* BUFFERED_INPUT */ |
2008 | ||
a0c0a00f CR |
2009 | CLRINTERRUPT; /* XXX - children have their own interrupt state */ |
2010 | ||
726f6388 JA |
2011 | /* Restore top-level signal mask. */ |
2012 | sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL); | |
2013 | ||
2014 | if (job_control) | |
2015 | { | |
2016 | /* All processes in this pipeline belong in the same | |
2017 | process group. */ | |
2018 | ||
ccc6cda3 | 2019 | if (pipeline_pgrp == 0) /* This is the first child. */ |
b72432fd | 2020 | pipeline_pgrp = mypid; |
726f6388 JA |
2021 | |
2022 | /* Check for running command in backquotes. */ | |
2023 | if (pipeline_pgrp == shell_pgrp) | |
ccc6cda3 | 2024 | ignore_tty_job_signals (); |
726f6388 | 2025 | else |
ccc6cda3 | 2026 | default_tty_job_signals (); |
726f6388 JA |
2027 | |
2028 | /* Set the process group before trying to mess with the terminal's | |
2029 | process group. This is mandated by POSIX. */ | |
2030 | /* This is in accordance with the Posix 1003.1 standard, | |
2031 | section B.7.2.4, which says that trying to set the terminal | |
2032 | process group with tcsetpgrp() to an unused pgrp value (like | |
2033 | this would have for the first child) is an error. Section | |
2034 | B.4.3.3, p. 237 also covers this, in the context of job control | |
2035 | shells. */ | |
b72432fd | 2036 | if (setpgid (mypid, pipeline_pgrp) < 0) |
3185942a | 2037 | sys_error (_("child setpgid (%ld to %ld)"), (long)mypid, (long)pipeline_pgrp); |
b80f6443 JA |
2038 | |
2039 | /* By convention (and assumption above), if | |
2040 | pipeline_pgrp == shell_pgrp, we are making a child for | |
2041 | command substitution. | |
2042 | In this case, we don't want to give the terminal to the | |
2043 | shell's process group (we could be in the middle of a | |
2044 | pipeline, for example). */ | |
a0c0a00f | 2045 | if (async_p == 0 && pipeline_pgrp != shell_pgrp && ((subshell_environment&(SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0) && running_in_background == 0) |
b80f6443 | 2046 | give_terminal_to (pipeline_pgrp, 0); |
726f6388 JA |
2047 | |
2048 | #if defined (PGRP_PIPE) | |
b80f6443 JA |
2049 | if (pipeline_pgrp == mypid) |
2050 | pipe_read (pgrp_pipe); | |
726f6388 JA |
2051 | #endif |
2052 | } | |
2053 | else /* Without job control... */ | |
2054 | { | |
ccc6cda3 | 2055 | if (pipeline_pgrp == 0) |
726f6388 JA |
2056 | pipeline_pgrp = shell_pgrp; |
2057 | ||
2058 | /* If these signals are set to SIG_DFL, we encounter the curious | |
2059 | situation of an interactive ^Z to a running process *working* | |
2060 | and stopping the process, but being unable to do anything with | |
2061 | that process to change its state. On the other hand, if they | |
2062 | are set to SIG_IGN, jobs started from scripts do not stop when | |
2063 | the shell running the script gets a SIGTSTP and stops. */ | |
2064 | ||
ccc6cda3 | 2065 | default_tty_job_signals (); |
726f6388 JA |
2066 | } |
2067 | ||
2068 | #if defined (PGRP_PIPE) | |
2069 | /* Release the process group pipe, since our call to setpgid () | |
3185942a JA |
2070 | is done. The last call to sh_closepipe is done in stop_pipeline. */ |
2071 | sh_closepipe (pgrp_pipe); | |
726f6388 JA |
2072 | #endif /* PGRP_PIPE */ |
2073 | ||
3185942a JA |
2074 | #if 0 |
2075 | /* Don't set last_asynchronous_pid in the child */ | |
726f6388 | 2076 | if (async_p) |
3185942a JA |
2077 | last_asynchronous_pid = mypid; /* XXX */ |
2078 | else | |
2079 | #endif | |
95732b49 | 2080 | #if defined (RECYCLES_PIDS) |
3185942a | 2081 | if (last_asynchronous_pid == mypid) |
95732b49 JA |
2082 | /* Avoid pid aliasing. 1 seems like a safe, unusual pid value. */ |
2083 | last_asynchronous_pid = 1; | |
2084 | #endif | |
726f6388 JA |
2085 | } |
2086 | else | |
2087 | { | |
2088 | /* In the parent. Remember the pid of the child just created | |
2089 | as the proper pgrp if this is the first child. */ | |
2090 | ||
2091 | if (job_control) | |
2092 | { | |
ccc6cda3 | 2093 | if (pipeline_pgrp == 0) |
726f6388 JA |
2094 | { |
2095 | pipeline_pgrp = pid; | |
2096 | /* Don't twiddle terminal pgrps in the parent! This is the bug, | |
2097 | not the good thing of twiddling them in the child! */ | |
28ef6c31 | 2098 | /* give_terminal_to (pipeline_pgrp, 0); */ |
726f6388 JA |
2099 | } |
2100 | /* This is done on the recommendation of the Rationale section of | |
2101 | the POSIX 1003.1 standard, where it discusses job control and | |
2102 | shells. It is done to avoid possible race conditions. (Ref. | |
2103 | 1003.1 Rationale, section B.4.3.3, page 236). */ | |
2104 | setpgid (pid, pipeline_pgrp); | |
2105 | } | |
2106 | else | |
2107 | { | |
ccc6cda3 | 2108 | if (pipeline_pgrp == 0) |
726f6388 JA |
2109 | pipeline_pgrp = shell_pgrp; |
2110 | } | |
2111 | ||
2112 | /* Place all processes into the jobs array regardless of the | |
2113 | state of job_control. */ | |
2114 | add_process (command, pid); | |
2115 | ||
2116 | if (async_p) | |
2117 | last_asynchronous_pid = pid; | |
95732b49 JA |
2118 | #if defined (RECYCLES_PIDS) |
2119 | else if (last_asynchronous_pid == pid) | |
2120 | /* Avoid pid aliasing. 1 seems like a safe, unusual pid value. */ | |
2121 | last_asynchronous_pid = 1; | |
2122 | #endif | |
2123 | ||
ac50fbac CR |
2124 | /* Delete the saved status for any job containing this PID in case it's |
2125 | been reused. */ | |
2126 | delete_old_job (pid); | |
0628567a | 2127 | |
ac50fbac CR |
2128 | /* Perform the check for pid reuse unconditionally. Some systems reuse |
2129 | PIDs before giving a process CHILD_MAX/_SC_CHILD_MAX unique ones. */ | |
2130 | bgp_delete (pid); /* new process, discard any saved status */ | |
726f6388 JA |
2131 | |
2132 | last_made_pid = pid; | |
2133 | ||
95732b49 JA |
2134 | /* keep stats */ |
2135 | js.c_totforked++; | |
2136 | js.c_living++; | |
2137 | ||
2138 | /* Unblock SIGINT and SIGCHLD unless creating a pipeline, in which case | |
2139 | SIGCHLD remains blocked until all commands in the pipeline have been | |
2140 | created. */ | |
726f6388 JA |
2141 | sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); |
2142 | } | |
2143 | ||
2144 | return (pid); | |
2145 | } | |
2146 | ||
7117c2d2 | 2147 | /* These two functions are called only in child processes. */ |
ccc6cda3 JA |
2148 | void |
2149 | ignore_tty_job_signals () | |
2150 | { | |
2151 | set_signal_handler (SIGTSTP, SIG_IGN); | |
2152 | set_signal_handler (SIGTTIN, SIG_IGN); | |
2153 | set_signal_handler (SIGTTOU, SIG_IGN); | |
2154 | } | |
2155 | ||
a0c0a00f CR |
2156 | /* Reset the tty-generated job control signals to SIG_DFL unless that signal |
2157 | was ignored at entry to the shell, in which case we need to set it to | |
2158 | SIG_IGN in the child. We can't rely on resetting traps, since the hard | |
2159 | ignored signals can't be trapped. */ | |
ccc6cda3 JA |
2160 | void |
2161 | default_tty_job_signals () | |
2162 | { | |
a0c0a00f CR |
2163 | if (signal_is_trapped (SIGTSTP) == 0 && signal_is_hard_ignored (SIGTSTP)) |
2164 | set_signal_handler (SIGTSTP, SIG_IGN); | |
2165 | else | |
2166 | set_signal_handler (SIGTSTP, SIG_DFL); | |
2167 | ||
2168 | if (signal_is_trapped (SIGTTIN) == 0 && signal_is_hard_ignored (SIGTTIN)) | |
2169 | set_signal_handler (SIGTTIN, SIG_IGN); | |
2170 | else | |
2171 | set_signal_handler (SIGTTIN, SIG_DFL); | |
2172 | ||
2173 | if (signal_is_trapped (SIGTTOU) == 0 && signal_is_hard_ignored (SIGTTOU)) | |
2174 | set_signal_handler (SIGTTOU, SIG_IGN); | |
2175 | else | |
2176 | set_signal_handler (SIGTTOU, SIG_DFL); | |
2177 | } | |
2178 | ||
2179 | /* Called once in a parent process. */ | |
2180 | void | |
2181 | get_original_tty_job_signals () | |
2182 | { | |
2183 | static int fetched = 0; | |
2184 | ||
2185 | if (fetched == 0) | |
2186 | { | |
2187 | if (interactive_shell) | |
2188 | { | |
2189 | set_original_signal (SIGTSTP, SIG_DFL); | |
2190 | set_original_signal (SIGTTIN, SIG_DFL); | |
2191 | set_original_signal (SIGTTOU, SIG_DFL); | |
2192 | } | |
2193 | else | |
2194 | { | |
2195 | get_original_signal (SIGTSTP); | |
2196 | get_original_signal (SIGTTIN); | |
2197 | get_original_signal (SIGTTOU); | |
2198 | } | |
2199 | fetched = 1; | |
2200 | } | |
ccc6cda3 JA |
2201 | } |
2202 | ||
726f6388 JA |
2203 | /* When we end a job abnormally, or if we stop a job, we set the tty to the |
2204 | state kept in here. When a job ends normally, we set the state in here | |
2205 | to the state of the tty. */ | |
2206 | ||
28ef6c31 JA |
2207 | static TTYSTRUCT shell_tty_info; |
2208 | ||
726f6388 | 2209 | #if defined (NEW_TTY_DRIVER) |
726f6388 JA |
2210 | static struct tchars shell_tchars; |
2211 | static struct ltchars shell_ltchars; | |
2212 | #endif /* NEW_TTY_DRIVER */ | |
2213 | ||
726f6388 JA |
2214 | #if defined (NEW_TTY_DRIVER) && defined (DRAIN_OUTPUT) |
2215 | /* Since the BSD tty driver does not allow us to change the tty modes | |
2216 | while simultaneously waiting for output to drain and preserving | |
2217 | typeahead, we have to drain the output ourselves before calling | |
2218 | ioctl. We cheat by finding the length of the output queue, and | |
2219 | using select to wait for an appropriate length of time. This is | |
2220 | a hack, and should be labeled as such (it's a hastily-adapted | |
2221 | mutation of a `usleep' implementation). It's only reason for | |
2222 | existing is the flaw in the BSD tty driver. */ | |
2223 | ||
2224 | static int ttspeeds[] = | |
2225 | { | |
2226 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, | |
2227 | 1800, 2400, 4800, 9600, 19200, 38400 | |
2228 | }; | |
2229 | ||
2230 | static void | |
2231 | draino (fd, ospeed) | |
2232 | int fd, ospeed; | |
2233 | { | |
2234 | register int delay = ttspeeds[ospeed]; | |
2235 | int n; | |
2236 | ||
2237 | if (!delay) | |
2238 | return; | |
2239 | ||
2240 | while ((ioctl (fd, TIOCOUTQ, &n) == 0) && n) | |
2241 | { | |
2242 | if (n > (delay / 100)) | |
2243 | { | |
2244 | struct timeval tv; | |
2245 | ||
2246 | n *= 10; /* 2 bits more for conservativeness. */ | |
2247 | tv.tv_sec = n / delay; | |
2248 | tv.tv_usec = ((n % delay) * 1000000) / delay; | |
2249 | select (fd, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv); | |
2250 | } | |
2251 | else | |
2252 | break; | |
2253 | } | |
2254 | } | |
2255 | #endif /* NEW_TTY_DRIVER && DRAIN_OUTPUT */ | |
2256 | ||
2257 | /* Return the fd from which we are actually getting input. */ | |
2258 | #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr) | |
2259 | ||
2260 | /* Fill the contents of shell_tty_info with the current tty info. */ | |
ccc6cda3 | 2261 | int |
726f6388 JA |
2262 | get_tty_state () |
2263 | { | |
ccc6cda3 | 2264 | int tty; |
726f6388 | 2265 | |
ccc6cda3 | 2266 | tty = input_tty (); |
726f6388 JA |
2267 | if (tty != -1) |
2268 | { | |
2269 | #if defined (NEW_TTY_DRIVER) | |
2270 | ioctl (tty, TIOCGETP, &shell_tty_info); | |
2271 | ioctl (tty, TIOCGETC, &shell_tchars); | |
2272 | ioctl (tty, TIOCGLTC, &shell_ltchars); | |
2273 | #endif /* NEW_TTY_DRIVER */ | |
2274 | ||
2275 | #if defined (TERMIO_TTY_DRIVER) | |
2276 | ioctl (tty, TCGETA, &shell_tty_info); | |
2277 | #endif /* TERMIO_TTY_DRIVER */ | |
2278 | ||
2279 | #if defined (TERMIOS_TTY_DRIVER) | |
2280 | if (tcgetattr (tty, &shell_tty_info) < 0) | |
2281 | { | |
2282 | #if 0 | |
2283 | /* Only print an error message if we're really interactive at | |
2284 | this time. */ | |
2285 | if (interactive) | |
3185942a | 2286 | sys_error ("[%ld: %d (%d)] tcgetattr", (long)getpid (), shell_level, tty); |
726f6388 JA |
2287 | #endif |
2288 | return -1; | |
2289 | } | |
2290 | #endif /* TERMIOS_TTY_DRIVER */ | |
ccc6cda3 | 2291 | if (check_window_size) |
95732b49 | 2292 | get_new_window_size (0, (int *)0, (int *)0); |
726f6388 JA |
2293 | } |
2294 | return 0; | |
2295 | } | |
2296 | ||
2297 | /* Make the current tty use the state in shell_tty_info. */ | |
ccc6cda3 | 2298 | int |
726f6388 JA |
2299 | set_tty_state () |
2300 | { | |
d166f048 | 2301 | int tty; |
726f6388 | 2302 | |
d166f048 | 2303 | tty = input_tty (); |
726f6388 JA |
2304 | if (tty != -1) |
2305 | { | |
2306 | #if defined (NEW_TTY_DRIVER) | |
2307 | # if defined (DRAIN_OUTPUT) | |
2308 | draino (tty, shell_tty_info.sg_ospeed); | |
2309 | # endif /* DRAIN_OUTPUT */ | |
2310 | ioctl (tty, TIOCSETN, &shell_tty_info); | |
2311 | ioctl (tty, TIOCSETC, &shell_tchars); | |
2312 | ioctl (tty, TIOCSLTC, &shell_ltchars); | |
2313 | #endif /* NEW_TTY_DRIVER */ | |
2314 | ||
2315 | #if defined (TERMIO_TTY_DRIVER) | |
2316 | ioctl (tty, TCSETAW, &shell_tty_info); | |
2317 | #endif /* TERMIO_TTY_DRIVER */ | |
2318 | ||
2319 | #if defined (TERMIOS_TTY_DRIVER) | |
2320 | if (tcsetattr (tty, TCSADRAIN, &shell_tty_info) < 0) | |
2321 | { | |
2322 | /* Only print an error message if we're really interactive at | |
2323 | this time. */ | |
2324 | if (interactive) | |
3185942a | 2325 | sys_error ("[%ld: %d (%d)] tcsetattr", (long)getpid (), shell_level, tty); |
726f6388 JA |
2326 | return -1; |
2327 | } | |
2328 | #endif /* TERMIOS_TTY_DRIVER */ | |
2329 | } | |
2330 | return 0; | |
2331 | } | |
2332 | ||
95732b49 | 2333 | /* Given an index into the jobs array JOB, return the PROCESS struct of the last |
726f6388 | 2334 | process in that job's pipeline. This is the one whose exit status |
7117c2d2 | 2335 | counts. Must be called with SIGCHLD blocked or queued. */ |
95732b49 JA |
2336 | static PROCESS * |
2337 | find_last_proc (job, block) | |
726f6388 | 2338 | int job; |
7117c2d2 | 2339 | int block; |
726f6388 JA |
2340 | { |
2341 | register PROCESS *p; | |
7117c2d2 JA |
2342 | sigset_t set, oset; |
2343 | ||
2344 | if (block) | |
2345 | BLOCK_CHILD (set, oset); | |
726f6388 JA |
2346 | |
2347 | p = jobs[job]->pipe; | |
0628567a | 2348 | while (p && p->next != jobs[job]->pipe) |
726f6388 JA |
2349 | p = p->next; |
2350 | ||
7117c2d2 JA |
2351 | if (block) |
2352 | UNBLOCK_CHILD (oset); | |
cce855bc | 2353 | |
95732b49 | 2354 | return (p); |
cce855bc JA |
2355 | } |
2356 | ||
95732b49 JA |
2357 | static pid_t |
2358 | find_last_pid (job, block) | |
2359 | int job; | |
2360 | int block; | |
2361 | { | |
2362 | PROCESS *p; | |
2363 | ||
2364 | p = find_last_proc (job, block); | |
2365 | /* Possible race condition here. */ | |
2366 | return p->pid; | |
2367 | } | |
2368 | ||
726f6388 JA |
2369 | /* Wait for a particular child of the shell to finish executing. |
2370 | This low-level function prints an error message if PID is not | |
7117c2d2 JA |
2371 | a child of this shell. It returns -1 if it fails, or whatever |
2372 | wait_for returns otherwise. If the child is not found in the | |
d233b485 CR |
2373 | jobs table, it returns 127. If FLAGS doesn't include JWAIT_PERROR, |
2374 | we suppress the error message if PID isn't found. */ | |
a0c0a00f | 2375 | |
726f6388 | 2376 | int |
a0c0a00f | 2377 | wait_for_single_pid (pid, flags) |
726f6388 | 2378 | pid_t pid; |
a0c0a00f | 2379 | int flags; |
726f6388 JA |
2380 | { |
2381 | register PROCESS *child; | |
ccc6cda3 | 2382 | sigset_t set, oset; |
d233b485 | 2383 | int r, job, alive; |
726f6388 | 2384 | |
ccc6cda3 | 2385 | BLOCK_CHILD (set, oset); |
7117c2d2 | 2386 | child = find_pipeline (pid, 0, (int *)NULL); |
ccc6cda3 | 2387 | UNBLOCK_CHILD (oset); |
726f6388 | 2388 | |
95732b49 JA |
2389 | if (child == 0) |
2390 | { | |
2391 | r = bgp_search (pid); | |
2392 | if (r >= 0) | |
2393 | return r; | |
2394 | } | |
2395 | ||
ccc6cda3 | 2396 | if (child == 0) |
726f6388 | 2397 | { |
d233b485 | 2398 | if (flags & JWAIT_PERROR) |
a0c0a00f | 2399 | internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid); |
726f6388 JA |
2400 | return (127); |
2401 | } | |
2402 | ||
d233b485 CR |
2403 | alive = 0; |
2404 | do | |
2405 | { | |
2406 | r = wait_for (pid); | |
2407 | if ((flags & JWAIT_FORCE) == 0) | |
2408 | break; | |
2409 | ||
2410 | BLOCK_CHILD (set, oset); | |
2411 | alive = PALIVE (child); | |
2412 | UNBLOCK_CHILD (oset); | |
2413 | } | |
2414 | while (alive); | |
cce855bc | 2415 | |
b72432fd JA |
2416 | /* POSIX.2: if we just waited for a job, we can remove it from the jobs |
2417 | table. */ | |
2418 | BLOCK_CHILD (set, oset); | |
95732b49 | 2419 | job = find_job (pid, 0, NULL); |
b72432fd JA |
2420 | if (job != NO_JOB && jobs[job] && DEADJOB (job)) |
2421 | jobs[job]->flags |= J_NOTIFIED; | |
2422 | UNBLOCK_CHILD (oset); | |
cce855bc | 2423 | |
95732b49 JA |
2424 | /* If running in posix mode, remove the job from the jobs table immediately */ |
2425 | if (posixly_correct) | |
2426 | { | |
2427 | cleanup_dead_jobs (); | |
2428 | bgp_delete (pid); | |
2429 | } | |
2430 | ||
cce855bc | 2431 | return r; |
726f6388 JA |
2432 | } |
2433 | ||
495aee44 | 2434 | /* Wait for all of the background processes started by this shell to finish. */ |
726f6388 JA |
2435 | void |
2436 | wait_for_background_pids () | |
2437 | { | |
d233b485 CR |
2438 | register int i, r; |
2439 | int any_stopped, check_async; | |
ccc6cda3 JA |
2440 | sigset_t set, oset; |
2441 | pid_t pid; | |
726f6388 | 2442 | |
d233b485 | 2443 | for (any_stopped = 0, check_async = 1;;) |
ccc6cda3 | 2444 | { |
726f6388 JA |
2445 | BLOCK_CHILD (set, oset); |
2446 | ||
7117c2d2 | 2447 | /* find first running job; if none running in foreground, break */ |
0628567a | 2448 | /* XXX could use js.j_firstj and js.j_lastj here */ |
95732b49 JA |
2449 | for (i = 0; i < js.j_jobslots; i++) |
2450 | { | |
2451 | #if defined (DEBUG) | |
2452 | if (i < js.j_firstj && jobs[i]) | |
2453 | itrace("wait_for_background_pids: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
2454 | if (i > js.j_lastj && jobs[i]) |
2455 | itrace("wait_for_background_pids: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 | 2456 | #endif |
d233b485 CR |
2457 | if (jobs[i] && STOPPED (i)) |
2458 | { | |
2459 | builtin_warning ("job %d[%d] stopped", i+1, find_last_pid (i, 0)); | |
2460 | any_stopped = 1; | |
2461 | } | |
2462 | ||
95732b49 JA |
2463 | if (jobs[i] && RUNNING (i) && IS_FOREGROUND (i) == 0) |
2464 | break; | |
2465 | } | |
2466 | if (i == js.j_jobslots) | |
726f6388 JA |
2467 | { |
2468 | UNBLOCK_CHILD (oset); | |
2469 | break; | |
2470 | } | |
2471 | ||
7117c2d2 JA |
2472 | /* now wait for the last pid in that job. */ |
2473 | pid = find_last_pid (i, 0); | |
2474 | UNBLOCK_CHILD (oset); | |
2475 | QUIT; | |
2476 | errno = 0; /* XXX */ | |
d233b485 CR |
2477 | r = wait_for_single_pid (pid, JWAIT_PERROR); |
2478 | if (r == -1 && errno == ECHILD) | |
7117c2d2 JA |
2479 | { |
2480 | /* If we're mistaken about job state, compensate. */ | |
d233b485 CR |
2481 | check_async = 0; |
2482 | mark_all_jobs_as_dead (); | |
7117c2d2 | 2483 | } |
726f6388 | 2484 | } |
b72432fd | 2485 | |
d233b485 CR |
2486 | #if defined (PROCESS_SUBSTITUTION) |
2487 | if (last_procsub_child && last_procsub_child->pid != NO_PID) | |
2488 | r = wait_for (last_procsub_child->pid); | |
2489 | wait_procsubs (); | |
2490 | reap_procsubs (); | |
2491 | #if 1 | |
2492 | /* We don't want to wait indefinitely if we have stopped children. */ | |
2493 | /* XXX - should add a loop that goes through the list of process | |
2494 | substitutions and waits for each proc in turn before this code. */ | |
2495 | if (any_stopped == 0) | |
2496 | { | |
2497 | /* Check whether or not we have any unreaped children. */ | |
2498 | while ((r = wait_for (ANY_PID)) >= 0) | |
2499 | { | |
2500 | QUIT; | |
2501 | CHECK_WAIT_INTR; | |
2502 | } | |
2503 | } | |
2504 | #endif | |
2505 | #endif | |
2506 | ||
b72432fd JA |
2507 | /* POSIX.2 says the shell can discard the statuses of all completed jobs if |
2508 | `wait' is called with no arguments. */ | |
2509 | mark_dead_jobs_as_notified (1); | |
2510 | cleanup_dead_jobs (); | |
95732b49 | 2511 | bgp_clear (); |
726f6388 JA |
2512 | } |
2513 | ||
2514 | /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */ | |
2515 | #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids | |
2516 | static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER; | |
2517 | ||
ac50fbac CR |
2518 | static int wait_sigint_received; |
2519 | static int child_caught_sigint; | |
2520 | static int waiting_for_child; | |
2521 | ||
a0c0a00f CR |
2522 | /* Clean up state after longjmp to wait_intr_buf */ |
2523 | void | |
2524 | wait_sigint_cleanup () | |
2525 | { | |
2526 | queue_sigchld = 0; | |
2527 | waiting_for_child = 0; | |
2528 | } | |
2529 | ||
726f6388 JA |
2530 | static void |
2531 | restore_sigint_handler () | |
2532 | { | |
2533 | if (old_sigint_handler != INVALID_SIGNAL_HANDLER) | |
2534 | { | |
2535 | set_signal_handler (SIGINT, old_sigint_handler); | |
2536 | old_sigint_handler = INVALID_SIGNAL_HANDLER; | |
ac50fbac | 2537 | waiting_for_child = 0; |
726f6388 JA |
2538 | } |
2539 | } | |
2540 | ||
726f6388 JA |
2541 | /* Handle SIGINT while we are waiting for children in a script to exit. |
2542 | The `wait' builtin should be interruptible, but all others should be | |
2543 | effectively ignored (i.e. not cause the shell to exit). */ | |
2544 | static sighandler | |
2545 | wait_sigint_handler (sig) | |
2546 | int sig; | |
2547 | { | |
bb70624e JA |
2548 | SigHandler *sigint_handler; |
2549 | ||
726f6388 JA |
2550 | if (interrupt_immediately || |
2551 | (this_shell_builtin && this_shell_builtin == wait_builtin)) | |
2552 | { | |
ac50fbac | 2553 | last_command_exit_value = 128+SIGINT; |
726f6388 | 2554 | restore_sigint_handler (); |
bb70624e | 2555 | /* If we got a SIGINT while in `wait', and SIGINT is trapped, do |
28ef6c31 | 2556 | what POSIX.2 says (see builtins/wait.def for more info). */ |
bb70624e JA |
2557 | if (this_shell_builtin && this_shell_builtin == wait_builtin && |
2558 | signal_is_trapped (SIGINT) && | |
2559 | ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler)) | |
2560 | { | |
bb70624e | 2561 | trap_handler (SIGINT); /* set pending_traps[SIGINT] */ |
7117c2d2 | 2562 | wait_signal_received = SIGINT; |
a0c0a00f | 2563 | if (interrupt_immediately && wait_intr_flag) |
ac50fbac CR |
2564 | { |
2565 | interrupt_immediately = 0; | |
a0c0a00f | 2566 | sh_longjmp (wait_intr_buf, 1); |
ac50fbac CR |
2567 | } |
2568 | else | |
2569 | /* Let CHECK_WAIT_INTR handle it in wait_for/waitchld */ | |
2570 | SIGRETURN (0); | |
bb70624e | 2571 | } |
ac50fbac CR |
2572 | else if (interrupt_immediately) |
2573 | { | |
2574 | ADDINTERRUPT; | |
2575 | QUIT; | |
2576 | } | |
2577 | else /* wait_builtin but signal not trapped, treat as interrupt */ | |
2578 | kill (getpid (), SIGINT); | |
726f6388 JA |
2579 | } |
2580 | ||
ccc6cda3 JA |
2581 | /* XXX - should this be interrupt_state? If it is, the shell will act |
2582 | as if it got the SIGINT interrupt. */ | |
ac50fbac CR |
2583 | if (waiting_for_child) |
2584 | wait_sigint_received = 1; | |
2585 | else | |
2586 | { | |
2587 | last_command_exit_value = 128+SIGINT; | |
2588 | restore_sigint_handler (); | |
2589 | kill (getpid (), SIGINT); | |
2590 | } | |
ccc6cda3 | 2591 | |
726f6388 JA |
2592 | /* Otherwise effectively ignore the SIGINT and allow the running job to |
2593 | be killed. */ | |
ccc6cda3 | 2594 | SIGRETURN (0); |
726f6388 JA |
2595 | } |
2596 | ||
b80f6443 JA |
2597 | static int |
2598 | process_exit_signal (status) | |
2599 | WAIT status; | |
2600 | { | |
2601 | return (WIFSIGNALED (status) ? WTERMSIG (status) : 0); | |
2602 | } | |
2603 | ||
726f6388 JA |
2604 | static int |
2605 | process_exit_status (status) | |
2606 | WAIT status; | |
2607 | { | |
2608 | if (WIFSIGNALED (status)) | |
2609 | return (128 + WTERMSIG (status)); | |
ccc6cda3 | 2610 | else if (WIFSTOPPED (status) == 0) |
726f6388 JA |
2611 | return (WEXITSTATUS (status)); |
2612 | else | |
2613 | return (EXECUTION_SUCCESS); | |
2614 | } | |
2615 | ||
f1be666c JA |
2616 | static WAIT |
2617 | job_signal_status (job) | |
2618 | int job; | |
2619 | { | |
2620 | register PROCESS *p; | |
2621 | WAIT s; | |
2622 | ||
2623 | p = jobs[job]->pipe; | |
2624 | do | |
2625 | { | |
2626 | s = p->status; | |
2627 | if (WIFSIGNALED(s) || WIFSTOPPED(s)) | |
2628 | break; | |
2629 | p = p->next; | |
2630 | } | |
2631 | while (p != jobs[job]->pipe); | |
2632 | ||
2633 | return s; | |
2634 | } | |
2635 | ||
28ef6c31 JA |
2636 | /* Return the exit status of the last process in the pipeline for job JOB. |
2637 | This is the exit status of the entire job. */ | |
2638 | static WAIT | |
bb70624e | 2639 | raw_job_exit_status (job) |
f73dda09 | 2640 | int job; |
ccc6cda3 JA |
2641 | { |
2642 | register PROCESS *p; | |
b80f6443 | 2643 | int fail; |
3185942a | 2644 | WAIT ret; |
b80f6443 | 2645 | |
d233b485 | 2646 | #if 0 |
b80f6443 | 2647 | if (pipefail_opt) |
d233b485 CR |
2648 | #else |
2649 | if (jobs[job]->flags & J_PIPEFAIL) | |
2650 | #endif | |
b80f6443 JA |
2651 | { |
2652 | fail = 0; | |
eb873671 JA |
2653 | p = jobs[job]->pipe; |
2654 | do | |
2655 | { | |
3185942a JA |
2656 | if (WSTATUS (p->status) != EXECUTION_SUCCESS) |
2657 | fail = WSTATUS(p->status); | |
eb873671 JA |
2658 | p = p->next; |
2659 | } | |
2660 | while (p != jobs[job]->pipe); | |
3185942a JA |
2661 | WSTATUS (ret) = fail; |
2662 | return ret; | |
b80f6443 JA |
2663 | } |
2664 | ||
ccc6cda3 JA |
2665 | for (p = jobs[job]->pipe; p->next != jobs[job]->pipe; p = p->next) |
2666 | ; | |
bb70624e JA |
2667 | return (p->status); |
2668 | } | |
2669 | ||
28ef6c31 JA |
2670 | /* Return the exit status of job JOB. This is the exit status of the last |
2671 | (rightmost) process in the job's pipeline, modified if the job was killed | |
2672 | by a signal or stopped. */ | |
495aee44 | 2673 | int |
bb70624e JA |
2674 | job_exit_status (job) |
2675 | int job; | |
2676 | { | |
2677 | return (process_exit_status (raw_job_exit_status (job))); | |
ccc6cda3 JA |
2678 | } |
2679 | ||
495aee44 | 2680 | int |
b80f6443 JA |
2681 | job_exit_signal (job) |
2682 | int job; | |
2683 | { | |
2684 | return (process_exit_signal (raw_job_exit_status (job))); | |
2685 | } | |
2686 | ||
ccc6cda3 JA |
2687 | #define FIND_CHILD(pid, child) \ |
2688 | do \ | |
2689 | { \ | |
7117c2d2 | 2690 | child = find_pipeline (pid, 0, (int *)NULL); \ |
ccc6cda3 JA |
2691 | if (child == 0) \ |
2692 | { \ | |
28ef6c31 | 2693 | give_terminal_to (shell_pgrp, 0); \ |
ccc6cda3 | 2694 | UNBLOCK_CHILD (oset); \ |
b80f6443 | 2695 | internal_error (_("wait_for: No record of process %ld"), (long)pid); \ |
ccc6cda3 JA |
2696 | restore_sigint_handler (); \ |
2697 | return (termination_state = 127); \ | |
2698 | } \ | |
2699 | } \ | |
2700 | while (0) | |
2701 | ||
bb70624e JA |
2702 | /* Wait for pid (one of our children) to terminate, then |
2703 | return the termination state. Returns 127 if PID is not found in | |
2704 | the jobs table. Returns -1 if waitchld() returns -1, indicating | |
2705 | that there are no unwaited-for child processes. */ | |
726f6388 JA |
2706 | int |
2707 | wait_for (pid) | |
2708 | pid_t pid; | |
2709 | { | |
28ef6c31 JA |
2710 | int job, termination_state, r; |
2711 | WAIT s; | |
726f6388 JA |
2712 | register PROCESS *child; |
2713 | sigset_t set, oset; | |
2714 | ||
2715 | /* In the case that this code is interrupted, and we longjmp () out of it, | |
2716 | we are relying on the code in throw_to_top_level () to restore the | |
2717 | top-level signal mask. */ | |
ac50fbac | 2718 | child = 0; |
726f6388 JA |
2719 | BLOCK_CHILD (set, oset); |
2720 | ||
2721 | /* Ignore interrupts while waiting for a job run without job control | |
2722 | to finish. We don't want the shell to exit if an interrupt is | |
2723 | received, only if one of the jobs run is killed via SIGINT. If | |
ccc6cda3 | 2724 | job control is not set, the job will be run in the same pgrp as |
f1be666c JA |
2725 | the shell, and the shell will see any signals the job gets. In |
2726 | fact, we want this set every time the waiting shell and the waited- | |
2727 | for process are in the same process group, including command | |
2728 | substitution. */ | |
726f6388 JA |
2729 | |
2730 | /* This is possibly a race condition -- should it go in stop_pipeline? */ | |
ac50fbac | 2731 | wait_sigint_received = child_caught_sigint = 0; |
f1be666c | 2732 | if (job_control == 0 || (subshell_environment&SUBSHELL_COMSUB)) |
0628567a | 2733 | { |
36f89ff1 CR |
2734 | SigHandler *temp_sigint_handler; |
2735 | ||
2736 | temp_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler); | |
2737 | if (temp_sigint_handler == wait_sigint_handler) | |
2738 | { | |
2739 | #if defined (DEBUG) | |
2740 | internal_warning ("wait_for: recursively setting old_sigint_handler to wait_sigint_handler: running_trap = %d", running_trap); | |
2741 | #endif | |
2742 | } | |
2743 | else | |
2744 | old_sigint_handler = temp_sigint_handler; | |
ac50fbac | 2745 | waiting_for_child = 0; |
0628567a JA |
2746 | if (old_sigint_handler == SIG_IGN) |
2747 | set_signal_handler (SIGINT, old_sigint_handler); | |
2748 | } | |
726f6388 JA |
2749 | |
2750 | termination_state = last_command_exit_value; | |
2751 | ||
ccc6cda3 | 2752 | if (interactive && job_control == 0) |
726f6388 | 2753 | QUIT; |
0001803f CR |
2754 | /* Check for terminating signals and exit the shell if we receive one */ |
2755 | CHECK_TERMSIG; | |
726f6388 | 2756 | |
ac50fbac CR |
2757 | /* Check for a trapped signal interrupting the wait builtin and jump out */ |
2758 | CHECK_WAIT_INTR; | |
2759 | ||
ccc6cda3 JA |
2760 | /* If we say wait_for (), then we have a record of this child somewhere. |
2761 | If it and none of its peers are running, don't call waitchld(). */ | |
726f6388 | 2762 | |
ccc6cda3 JA |
2763 | job = NO_JOB; |
2764 | do | |
726f6388 | 2765 | { |
ac50fbac CR |
2766 | if (pid != ANY_PID) |
2767 | FIND_CHILD (pid, child); | |
726f6388 | 2768 | |
ccc6cda3 | 2769 | /* If this child is part of a job, then we are really waiting for the |
28ef6c31 JA |
2770 | job to finish. Otherwise, we are waiting for the child to finish. |
2771 | We check for JDEAD in case the job state has been set by waitchld | |
2772 | after receipt of a SIGCHLD. */ | |
d233b485 | 2773 | if (job == NO_JOB && pid != ANY_PID) /* XXX -- && pid != ANY_PID ? */ |
95732b49 | 2774 | job = find_job (pid, 0, NULL); |
726f6388 | 2775 | |
28ef6c31 JA |
2776 | /* waitchld() takes care of setting the state of the job. If the job |
2777 | has already exited before this is called, sigchld_handler will have | |
2778 | called waitchld and the state will be set to JDEAD. */ | |
726f6388 | 2779 | |
ac50fbac | 2780 | if (pid == ANY_PID || PRUNNING(child) || (job != NO_JOB && RUNNING (job))) |
ccc6cda3 JA |
2781 | { |
2782 | #if defined (WAITPID_BROKEN) /* SCOv4 */ | |
2783 | sigset_t suspend_set; | |
2784 | sigemptyset (&suspend_set); | |
2785 | sigsuspend (&suspend_set); | |
726f6388 | 2786 | #else /* !WAITPID_BROKEN */ |
ccc6cda3 JA |
2787 | # if defined (MUST_UNBLOCK_CHLD) |
2788 | struct sigaction act, oact; | |
2789 | sigset_t nullset, chldset; | |
2790 | ||
a0c0a00f | 2791 | queue_sigchld = 1; |
ccc6cda3 JA |
2792 | sigemptyset (&nullset); |
2793 | sigemptyset (&chldset); | |
2794 | sigprocmask (SIG_SETMASK, &nullset, &chldset); | |
2795 | act.sa_handler = SIG_DFL; | |
2796 | sigemptyset (&act.sa_mask); | |
2797 | sigemptyset (&oact.sa_mask); | |
2798 | act.sa_flags = 0; | |
ac50fbac CR |
2799 | # if defined (SA_RESTART) |
2800 | act.sa_flags |= SA_RESTART; | |
726f6388 | 2801 | # endif |
ac50fbac CR |
2802 | sigaction (SIGCHLD, &act, &oact); |
2803 | # endif /* MUST_UNBLOCK_CHLD */ | |
7117c2d2 | 2804 | queue_sigchld = 1; |
ac50fbac CR |
2805 | waiting_for_child++; |
2806 | r = waitchld (pid, 1); /* XXX */ | |
2807 | waiting_for_child--; | |
2808 | #if 0 | |
2809 | itrace("wait_for: blocking wait for %d returns %d child = %p", (int)pid, r, child); | |
2810 | #endif | |
ccc6cda3 JA |
2811 | # if defined (MUST_UNBLOCK_CHLD) |
2812 | sigaction (SIGCHLD, &oact, (struct sigaction *)NULL); | |
2813 | sigprocmask (SIG_SETMASK, &chldset, (sigset_t *)NULL); | |
726f6388 | 2814 | # endif |
7117c2d2 | 2815 | queue_sigchld = 0; |
bb70624e JA |
2816 | if (r == -1 && errno == ECHILD && this_shell_builtin == wait_builtin) |
2817 | { | |
2818 | termination_state = -1; | |
d233b485 CR |
2819 | /* XXX - restore sigint handler here */ |
2820 | restore_sigint_handler (); | |
bb70624e JA |
2821 | goto wait_for_return; |
2822 | } | |
28ef6c31 JA |
2823 | |
2824 | /* If child is marked as running, but waitpid() returns -1/ECHILD, | |
2825 | there is something wrong. Somewhere, wait should have returned | |
2826 | that child's pid. Mark the child as not running and the job, | |
2827 | if it exists, as JDEAD. */ | |
2828 | if (r == -1 && errno == ECHILD) | |
2829 | { | |
ac50fbac CR |
2830 | if (child) |
2831 | { | |
2832 | child->running = PS_DONE; | |
2833 | WSTATUS (child->status) = 0; /* XXX -- can't find true status */ | |
2834 | } | |
0628567a | 2835 | js.c_living = 0; /* no living child processes */ |
28ef6c31 | 2836 | if (job != NO_JOB) |
95732b49 JA |
2837 | { |
2838 | jobs[job]->state = JDEAD; | |
2839 | js.c_reaped++; | |
2840 | js.j_ndead++; | |
2841 | } | |
ac50fbac CR |
2842 | if (pid == ANY_PID) |
2843 | { | |
2844 | termination_state = -1; | |
2845 | break; | |
2846 | } | |
28ef6c31 | 2847 | } |
ccc6cda3 JA |
2848 | #endif /* WAITPID_BROKEN */ |
2849 | } | |
2850 | ||
2851 | /* If the shell is interactive, and job control is disabled, see | |
2852 | if the foreground process has died due to SIGINT and jump out | |
2853 | of the wait loop if it has. waitchld has already restored the | |
2854 | old SIGINT signal handler. */ | |
2855 | if (interactive && job_control == 0) | |
2856 | QUIT; | |
0001803f CR |
2857 | /* Check for terminating signals and exit the shell if we receive one */ |
2858 | CHECK_TERMSIG; | |
ac50fbac CR |
2859 | |
2860 | /* Check for a trapped signal interrupting the wait builtin and jump out */ | |
2861 | CHECK_WAIT_INTR; | |
2862 | ||
2863 | if (pid == ANY_PID) | |
d233b485 CR |
2864 | { |
2865 | /* XXX - could set child but we don't have a handle on what waitchld | |
2866 | reaps. Leave termination_state alone. */ | |
2867 | restore_sigint_handler (); | |
2868 | goto wait_for_return; | |
2869 | } | |
726f6388 | 2870 | } |
95732b49 | 2871 | while (PRUNNING (child) || (job != NO_JOB && RUNNING (job))); |
726f6388 | 2872 | |
ac50fbac CR |
2873 | /* Restore the original SIGINT signal handler before we return. */ |
2874 | restore_sigint_handler (); | |
2875 | ||
726f6388 JA |
2876 | /* The exit state of the command is either the termination state of the |
2877 | child, or the termination state of the job. If a job, the status | |
b80f6443 JA |
2878 | of the last child in the pipeline is the significant one. If the command |
2879 | or job was terminated by a signal, note that value also. */ | |
2880 | termination_state = (job != NO_JOB) ? job_exit_status (job) | |
a0c0a00f | 2881 | : (child ? process_exit_status (child->status) : EXECUTION_SUCCESS); |
b80f6443 | 2882 | last_command_exit_signal = (job != NO_JOB) ? job_exit_signal (job) |
a0c0a00f | 2883 | : (child ? process_exit_signal (child->status) : 0); |
726f6388 | 2884 | |
95732b49 | 2885 | /* XXX */ |
a0c0a00f | 2886 | if ((job != NO_JOB && JOBSTATE (job) == JSTOPPED) || (child && WIFSTOPPED (child->status))) |
95732b49 JA |
2887 | termination_state = 128 + WSTOPSIG (child->status); |
2888 | ||
ccc6cda3 | 2889 | if (job == NO_JOB || IS_JOBCONTROL (job)) |
b72432fd JA |
2890 | { |
2891 | /* XXX - under what circumstances is a job not present in the jobs | |
2892 | table (job == NO_JOB)? | |
2893 | 1. command substitution | |
2894 | ||
2895 | In the case of command substitution, at least, it's probably not | |
2896 | the right thing to give the terminal to the shell's process group, | |
2897 | even though there is code in subst.c:command_substitute to work | |
2898 | around it. | |
2899 | ||
2900 | Things that don't: | |
2901 | $PROMPT_COMMAND execution | |
2902 | process substitution | |
2903 | */ | |
2904 | #if 0 | |
2905 | if (job == NO_JOB) | |
f73dda09 | 2906 | itrace("wait_for: job == NO_JOB, giving the terminal to shell_pgrp (%ld)", (long)shell_pgrp); |
b72432fd | 2907 | #endif |
a0c0a00f CR |
2908 | /* Don't modify terminal pgrp if we are running in background or a |
2909 | subshell. Make sure subst.c:command_substitute uses the same | |
2910 | conditions to determine whether or not it should undo this and | |
2911 | give the terminal to pipeline_pgrp. */ | |
2912 | ||
2913 | if (running_in_background == 0 && (subshell_environment&(SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0) | |
2914 | give_terminal_to (shell_pgrp, 0); | |
b72432fd | 2915 | } |
726f6388 JA |
2916 | |
2917 | /* If the command did not exit cleanly, or the job is just | |
2918 | being stopped, then reset the tty state back to what it | |
2919 | was before this command. Reset the tty state and notify | |
2920 | the user of the job termination only if the shell is | |
2921 | interactive. Clean up any dead jobs in either case. */ | |
2922 | if (job != NO_JOB) | |
2923 | { | |
ccc6cda3 | 2924 | if (interactive_shell && subshell_environment == 0) |
726f6388 | 2925 | { |
bb70624e JA |
2926 | /* This used to use `child->status'. That's wrong, however, for |
2927 | pipelines. `child' is the first process in the pipeline. It's | |
2928 | likely that the process we want to check for abnormal termination | |
2929 | or stopping is the last process in the pipeline, especially if | |
2930 | it's long-lived and the first process is short-lived. Since we | |
2931 | know we have a job here, we can check all the processes in this | |
2932 | job's pipeline and see if one of them stopped or terminated due | |
2933 | to a signal. We might want to change this later to just check | |
2934 | the last process in the pipeline. If no process exits due to a | |
2935 | signal, S is left as the status of the last job in the pipeline. */ | |
f1be666c | 2936 | s = job_signal_status (job); |
bb70624e JA |
2937 | |
2938 | if (WIFSIGNALED (s) || WIFSTOPPED (s)) | |
e8ce775d JA |
2939 | { |
2940 | set_tty_state (); | |
28ef6c31 | 2941 | |
bb70624e JA |
2942 | /* If the current job was stopped or killed by a signal, and |
2943 | the user has requested it, get a possibly new window size */ | |
95732b49 JA |
2944 | if (check_window_size && (job == js.j_current || IS_FOREGROUND (job))) |
2945 | get_new_window_size (0, (int *)0, (int *)0); | |
e8ce775d | 2946 | } |
726f6388 | 2947 | else |
a0c0a00f CR |
2948 | #if defined (READLINE) |
2949 | /* We don't want to do this if we are running a process during | |
2950 | programmable completion. */ | |
2951 | if (RL_ISSTATE (RL_STATE_COMPLETING) == 0) | |
2952 | #endif | |
726f6388 JA |
2953 | get_tty_state (); |
2954 | ||
2955 | /* If job control is enabled, the job was started with job | |
2956 | control, the job was the foreground job, and it was killed | |
2957 | by SIGINT, then print a newline to compensate for the kernel | |
2958 | printing the ^C without a trailing newline. */ | |
ccc6cda3 | 2959 | if (job_control && IS_JOBCONTROL (job) && IS_FOREGROUND (job) && |
bb70624e | 2960 | WIFSIGNALED (s) && WTERMSIG (s) == SIGINT) |
726f6388 | 2961 | { |
ccc6cda3 JA |
2962 | /* If SIGINT is not trapped and the shell is in a for, while, |
2963 | or until loop, act as if the shell received SIGINT as | |
2964 | well, so the loop can be broken. This doesn't call the | |
2965 | SIGINT signal handler; maybe it should. */ | |
3185942a | 2966 | if (signal_is_trapped (SIGINT) == 0 && (loop_level || (shell_compatibility_level > 32 && executing_list))) |
ccc6cda3 | 2967 | ADDINTERRUPT; |
a0c0a00f CR |
2968 | /* Call any SIGINT trap handler if the shell is running a loop, so |
2969 | the loop can be broken. This seems more useful and matches the | |
2970 | behavior when the shell is running a builtin command in a loop | |
2971 | when it is interrupted. Change ADDINTERRUPT to | |
2972 | trap_handler (SIGINT) to run the trap without interrupting the | |
2973 | loop. */ | |
2974 | else if (signal_is_trapped (SIGINT) && loop_level) | |
2975 | ADDINTERRUPT; | |
d233b485 CR |
2976 | /* If an interactive shell with job control enabled is sourcing |
2977 | a file, allow the interrupt to terminate the file sourcing. */ | |
2978 | else if (interactive_shell && signal_is_trapped (SIGINT) == 0 && sourcelevel) | |
2979 | ADDINTERRUPT; | |
ccc6cda3 | 2980 | else |
726f6388 | 2981 | { |
ccc6cda3 JA |
2982 | putchar ('\n'); |
2983 | fflush (stdout); | |
726f6388 JA |
2984 | } |
2985 | } | |
726f6388 | 2986 | } |
495aee44 | 2987 | else if ((subshell_environment & (SUBSHELL_COMSUB|SUBSHELL_PIPE)) && wait_sigint_received) |
f1be666c JA |
2988 | { |
2989 | /* If waiting for a job in a subshell started to do command | |
495aee44 CR |
2990 | substitution or to run a pipeline element that consists of |
2991 | something like a while loop or a for loop, simulate getting | |
2992 | and being killed by the SIGINT to pass the status back to our | |
2993 | parent. */ | |
ac50fbac | 2994 | if (child_caught_sigint == 0 && signal_is_trapped (SIGINT) == 0) |
f1be666c JA |
2995 | { |
2996 | UNBLOCK_CHILD (oset); | |
f1be666c JA |
2997 | old_sigint_handler = set_signal_handler (SIGINT, SIG_DFL); |
2998 | if (old_sigint_handler == SIG_IGN) | |
2999 | restore_sigint_handler (); | |
3000 | else | |
3001 | kill (getpid (), SIGINT); | |
3002 | } | |
3003 | } | |
d233b485 CR |
3004 | else if (interactive_shell == 0 && subshell_environment == 0 && IS_FOREGROUND (job)) |
3005 | { | |
3006 | s = job_signal_status (job); | |
3007 | ||
3008 | /* If we are non-interactive, but job control is enabled, and the job | |
3009 | died due to SIGINT, pretend we got the SIGINT */ | |
3010 | if (job_control && IS_JOBCONTROL (job) && WIFSIGNALED (s) && WTERMSIG (s) == SIGINT) | |
3011 | { | |
3012 | ADDINTERRUPT; /* For now */ | |
3013 | } | |
3014 | ||
3015 | if (check_window_size) | |
3016 | get_new_window_size (0, (int *)0, (int *)0); | |
3017 | } | |
28ef6c31 | 3018 | |
7117c2d2 JA |
3019 | /* Moved here from set_job_status_and_cleanup, which is in the SIGCHLD |
3020 | signal handler path */ | |
3021 | if (DEADJOB (job) && IS_FOREGROUND (job) /*&& subshell_environment == 0*/) | |
3022 | setjstatus (job); | |
3023 | ||
28ef6c31 JA |
3024 | /* If this job is dead, notify the user of the status. If the shell |
3025 | is interactive, this will display a message on the terminal. If | |
3026 | the shell is not interactive, make sure we turn on the notify bit | |
3027 | so we don't get an unwanted message about the job's termination, | |
3028 | and so delete_job really clears the slot in the jobs table. */ | |
3029 | notify_and_cleanup (); | |
726f6388 | 3030 | } |
ccc6cda3 | 3031 | |
bb70624e JA |
3032 | wait_for_return: |
3033 | ||
726f6388 JA |
3034 | UNBLOCK_CHILD (oset); |
3035 | ||
726f6388 JA |
3036 | return (termination_state); |
3037 | } | |
3038 | ||
bb70624e JA |
3039 | /* Wait for the last process in the pipeline for JOB. Returns whatever |
3040 | wait_for returns: the last process's termination state or -1 if there | |
d233b485 CR |
3041 | are no unwaited-for child processes or an error occurs. If FLAGS |
3042 | includes JWAIT_FORCE, we wait for the job to terminate, no just change | |
3043 | state */ | |
726f6388 | 3044 | int |
d233b485 CR |
3045 | wait_for_job (job, flags) |
3046 | int job, flags; | |
726f6388 | 3047 | { |
ccc6cda3 | 3048 | pid_t pid; |
d233b485 | 3049 | int r, state; |
cce855bc JA |
3050 | sigset_t set, oset; |
3051 | ||
3052 | BLOCK_CHILD(set, oset); | |
d233b485 CR |
3053 | state = JOBSTATE (job); |
3054 | if (state == JSTOPPED) | |
b80f6443 | 3055 | internal_warning (_("wait_for_job: job %d is stopped"), job+1); |
ccc6cda3 | 3056 | |
7117c2d2 JA |
3057 | pid = find_last_pid (job, 0); |
3058 | UNBLOCK_CHILD(oset); | |
d233b485 CR |
3059 | |
3060 | do | |
3061 | { | |
3062 | r = wait_for (pid); | |
3063 | if (r == -1 && errno == ECHILD) | |
3064 | mark_all_jobs_as_dead (); | |
3065 | ||
3066 | if ((flags & JWAIT_FORCE) == 0) | |
3067 | break; | |
3068 | ||
3069 | BLOCK_CHILD (set, oset); | |
3070 | state = (job != NO_JOB && jobs[job]) ? JOBSTATE (job) : JDEAD; | |
3071 | UNBLOCK_CHILD (oset); | |
3072 | } | |
3073 | while (state != JDEAD); | |
cce855bc | 3074 | |
b72432fd JA |
3075 | /* POSIX.2: we can remove the job from the jobs table if we just waited |
3076 | for it. */ | |
3077 | BLOCK_CHILD (set, oset); | |
3078 | if (job != NO_JOB && jobs[job] && DEADJOB (job)) | |
3079 | jobs[job]->flags |= J_NOTIFIED; | |
3080 | UNBLOCK_CHILD (oset); | |
cce855bc JA |
3081 | |
3082 | return r; | |
726f6388 JA |
3083 | } |
3084 | ||
ac50fbac CR |
3085 | /* Wait for any background job started by this shell to finish. Very |
3086 | similar to wait_for_background_pids(). Returns the exit status of | |
3087 | the next exiting job, -1 if there are no background jobs. The caller | |
3088 | is responsible for translating -1 into the right return value. */ | |
3089 | int | |
d233b485 CR |
3090 | wait_for_any_job (flags) |
3091 | int flags; | |
ac50fbac CR |
3092 | { |
3093 | pid_t pid; | |
d233b485 | 3094 | int i, r; |
ac50fbac CR |
3095 | sigset_t set, oset; |
3096 | ||
3097 | if (jobs_list_frozen) | |
3098 | return -1; | |
3099 | ||
3100 | /* First see if there are any unnotified dead jobs that we can report on */ | |
3101 | BLOCK_CHILD (set, oset); | |
3102 | for (i = 0; i < js.j_jobslots; i++) | |
3103 | { | |
3104 | if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i) == 0) | |
3105 | { | |
3106 | return_job: | |
3107 | r = job_exit_status (i); | |
3108 | notify_of_job_status (); /* XXX */ | |
3109 | delete_job (i, 0); | |
3110 | #if defined (COPROCESS_SUPPORT) | |
3111 | coproc_reap (); | |
3112 | #endif | |
3113 | UNBLOCK_CHILD (oset); | |
3114 | return r; | |
3115 | } | |
3116 | } | |
3117 | UNBLOCK_CHILD (oset); | |
3118 | ||
3119 | /* At this point, we have no dead jobs in the jobs table. Wait until we | |
3120 | get one, even if it takes multiple pids exiting. */ | |
d233b485 | 3121 | for (;;) |
ac50fbac CR |
3122 | { |
3123 | /* Make sure there is a background job to wait for */ | |
3124 | BLOCK_CHILD (set, oset); | |
3125 | for (i = 0; i < js.j_jobslots; i++) | |
3126 | if (jobs[i] && RUNNING (i) && IS_FOREGROUND (i) == 0) | |
3127 | break; | |
3128 | if (i == js.j_jobslots) | |
3129 | { | |
3130 | UNBLOCK_CHILD (oset); | |
3131 | return -1; | |
3132 | } | |
3133 | ||
3134 | UNBLOCK_CHILD (oset); | |
3135 | ||
3136 | QUIT; | |
3137 | CHECK_TERMSIG; | |
3138 | CHECK_WAIT_INTR; | |
3139 | ||
3140 | errno = 0; | |
3141 | r = wait_for (ANY_PID); /* special sentinel value for wait_for */ | |
3142 | if (r == -1 && errno == ECHILD) | |
3143 | mark_all_jobs_as_dead (); | |
3144 | ||
3145 | /* Now we see if we have any dead jobs and return the first one */ | |
3146 | BLOCK_CHILD (set, oset); | |
3147 | for (i = 0; i < js.j_jobslots; i++) | |
3148 | if (jobs[i] && DEADJOB (i)) | |
3149 | goto return_job; | |
3150 | UNBLOCK_CHILD (oset); | |
3151 | } | |
3152 | ||
3153 | return -1; | |
3154 | } | |
3155 | ||
726f6388 JA |
3156 | /* Print info about dead jobs, and then delete them from the list |
3157 | of known jobs. This does not actually delete jobs when the | |
3158 | shell is not interactive, because the dead jobs are not marked | |
3159 | as notified. */ | |
3160 | void | |
3161 | notify_and_cleanup () | |
3162 | { | |
ccc6cda3 | 3163 | if (jobs_list_frozen) |
726f6388 JA |
3164 | return; |
3165 | ||
28ef6c31 | 3166 | if (interactive || interactive_shell == 0 || sourcelevel) |
726f6388 JA |
3167 | notify_of_job_status (); |
3168 | ||
3169 | cleanup_dead_jobs (); | |
3170 | } | |
3171 | ||
3172 | /* Make dead jobs disappear from the jobs array without notification. | |
3173 | This is used when the shell is not interactive. */ | |
3174 | void | |
3175 | reap_dead_jobs () | |
3176 | { | |
b72432fd | 3177 | mark_dead_jobs_as_notified (0); |
726f6388 JA |
3178 | cleanup_dead_jobs (); |
3179 | } | |
3180 | ||
3181 | /* Return the next closest (chronologically) job to JOB which is in | |
3182 | STATE. STATE can be JSTOPPED, JRUNNING. NO_JOB is returned if | |
3183 | there is no next recent job. */ | |
3184 | static int | |
3185 | most_recent_job_in_state (job, state) | |
3186 | int job; | |
3187 | JOB_STATE state; | |
3188 | { | |
3189 | register int i, result; | |
3190 | sigset_t set, oset; | |
3191 | ||
3192 | BLOCK_CHILD (set, oset); | |
7117c2d2 | 3193 | |
ccc6cda3 | 3194 | for (result = NO_JOB, i = job - 1; i >= 0; i--) |
726f6388 | 3195 | { |
ccc6cda3 | 3196 | if (jobs[i] && (JOBSTATE (i) == state)) |
726f6388 | 3197 | { |
ccc6cda3 JA |
3198 | result = i; |
3199 | break; | |
726f6388 JA |
3200 | } |
3201 | } | |
7117c2d2 | 3202 | |
726f6388 | 3203 | UNBLOCK_CHILD (oset); |
ccc6cda3 | 3204 | |
726f6388 JA |
3205 | return (result); |
3206 | } | |
3207 | ||
3208 | /* Return the newest *stopped* job older than JOB, or NO_JOB if not | |
3209 | found. */ | |
3210 | static int | |
28ef6c31 | 3211 | job_last_stopped (job) |
726f6388 JA |
3212 | int job; |
3213 | { | |
3214 | return (most_recent_job_in_state (job, JSTOPPED)); | |
3215 | } | |
3216 | ||
3217 | /* Return the newest *running* job older than JOB, or NO_JOB if not | |
3218 | found. */ | |
3219 | static int | |
28ef6c31 | 3220 | job_last_running (job) |
726f6388 JA |
3221 | int job; |
3222 | { | |
3223 | return (most_recent_job_in_state (job, JRUNNING)); | |
3224 | } | |
3225 | ||
3226 | /* Make JOB be the current job, and make previous be useful. Must be | |
3227 | called with SIGCHLD blocked. */ | |
3228 | static void | |
3229 | set_current_job (job) | |
3230 | int job; | |
3231 | { | |
ccc6cda3 | 3232 | int candidate; |
726f6388 | 3233 | |
95732b49 | 3234 | if (js.j_current != job) |
726f6388 | 3235 | { |
95732b49 JA |
3236 | js.j_previous = js.j_current; |
3237 | js.j_current = job; | |
726f6388 JA |
3238 | } |
3239 | ||
95732b49 JA |
3240 | /* First choice for previous job is the old current job. */ |
3241 | if (js.j_previous != js.j_current && | |
3242 | js.j_previous != NO_JOB && | |
3243 | jobs[js.j_previous] && | |
3244 | STOPPED (js.j_previous)) | |
726f6388 JA |
3245 | return; |
3246 | ||
3247 | /* Second choice: Newest stopped job that is older than | |
3248 | the current job. */ | |
ccc6cda3 | 3249 | candidate = NO_JOB; |
95732b49 | 3250 | if (STOPPED (js.j_current)) |
726f6388 | 3251 | { |
95732b49 | 3252 | candidate = job_last_stopped (js.j_current); |
726f6388 JA |
3253 | |
3254 | if (candidate != NO_JOB) | |
3255 | { | |
95732b49 | 3256 | js.j_previous = candidate; |
726f6388 JA |
3257 | return; |
3258 | } | |
3259 | } | |
3260 | ||
3261 | /* If we get here, there is either only one stopped job, in which case it is | |
3262 | the current job and the previous job should be set to the newest running | |
3263 | job, or there are only running jobs and the previous job should be set to | |
3264 | the newest running job older than the current job. We decide on which | |
95732b49 | 3265 | alternative to use based on whether or not JOBSTATE(js.j_current) is |
726f6388 JA |
3266 | JSTOPPED. */ |
3267 | ||
95732b49 JA |
3268 | candidate = RUNNING (js.j_current) ? job_last_running (js.j_current) |
3269 | : job_last_running (js.j_jobslots); | |
726f6388 JA |
3270 | |
3271 | if (candidate != NO_JOB) | |
3272 | { | |
95732b49 | 3273 | js.j_previous = candidate; |
726f6388 JA |
3274 | return; |
3275 | } | |
3276 | ||
3277 | /* There is only a single job, and it is both `+' and `-'. */ | |
95732b49 | 3278 | js.j_previous = js.j_current; |
726f6388 JA |
3279 | } |
3280 | ||
3281 | /* Make current_job be something useful, if it isn't already. */ | |
3282 | ||
3283 | /* Here's the deal: The newest non-running job should be `+', and the | |
3284 | next-newest non-running job should be `-'. If there is only a single | |
95732b49 | 3285 | stopped job, the js.j_previous is the newest non-running job. If there |
726f6388 JA |
3286 | are only running jobs, the newest running job is `+' and the |
3287 | next-newest running job is `-'. Must be called with SIGCHLD blocked. */ | |
ccc6cda3 | 3288 | |
726f6388 JA |
3289 | static void |
3290 | reset_current () | |
3291 | { | |
ccc6cda3 | 3292 | int candidate; |
726f6388 | 3293 | |
95732b49 JA |
3294 | if (js.j_jobslots && js.j_current != NO_JOB && jobs[js.j_current] && STOPPED (js.j_current)) |
3295 | candidate = js.j_current; | |
726f6388 JA |
3296 | else |
3297 | { | |
ccc6cda3 JA |
3298 | candidate = NO_JOB; |
3299 | ||
3300 | /* First choice: the previous job. */ | |
95732b49 JA |
3301 | if (js.j_previous != NO_JOB && jobs[js.j_previous] && STOPPED (js.j_previous)) |
3302 | candidate = js.j_previous; | |
726f6388 JA |
3303 | |
3304 | /* Second choice: the most recently stopped job. */ | |
3305 | if (candidate == NO_JOB) | |
95732b49 | 3306 | candidate = job_last_stopped (js.j_jobslots); |
726f6388 | 3307 | |
ccc6cda3 | 3308 | /* Third choice: the newest running job. */ |
726f6388 | 3309 | if (candidate == NO_JOB) |
95732b49 | 3310 | candidate = job_last_running (js.j_jobslots); |
726f6388 JA |
3311 | } |
3312 | ||
3313 | /* If we found a job to use, then use it. Otherwise, there | |
3314 | are no jobs period. */ | |
3315 | if (candidate != NO_JOB) | |
3316 | set_current_job (candidate); | |
3317 | else | |
95732b49 | 3318 | js.j_current = js.j_previous = NO_JOB; |
726f6388 JA |
3319 | } |
3320 | ||
d166f048 JA |
3321 | /* Set up the job structures so we know the job and its processes are |
3322 | all running. */ | |
3323 | static void | |
3324 | set_job_running (job) | |
3325 | int job; | |
3326 | { | |
3327 | register PROCESS *p; | |
3328 | ||
3329 | /* Each member of the pipeline is now running. */ | |
3330 | p = jobs[job]->pipe; | |
3331 | ||
3332 | do | |
3333 | { | |
3334 | if (WIFSTOPPED (p->status)) | |
7117c2d2 | 3335 | p->running = PS_RUNNING; /* XXX - could be PS_STOPPED */ |
d166f048 JA |
3336 | p = p->next; |
3337 | } | |
3338 | while (p != jobs[job]->pipe); | |
3339 | ||
3340 | /* This means that the job is running. */ | |
3341 | JOBSTATE (job) = JRUNNING; | |
3342 | } | |
3343 | ||
726f6388 JA |
3344 | /* Start a job. FOREGROUND if non-zero says to do that. Otherwise, |
3345 | start the job in the background. JOB is a zero-based index into | |
3346 | JOBS. Returns -1 if it is unable to start a job, and the return | |
3347 | status of the job otherwise. */ | |
3348 | int | |
3349 | start_job (job, foreground) | |
3350 | int job, foreground; | |
3351 | { | |
3352 | register PROCESS *p; | |
3353 | int already_running; | |
3354 | sigset_t set, oset; | |
95732b49 | 3355 | char *wd, *s; |
28ef6c31 | 3356 | static TTYSTRUCT save_stty; |
726f6388 JA |
3357 | |
3358 | BLOCK_CHILD (set, oset); | |
726f6388 | 3359 | |
ccc6cda3 | 3360 | if (DEADJOB (job)) |
726f6388 | 3361 | { |
b80f6443 | 3362 | internal_error (_("%s: job has terminated"), this_command_name); |
726f6388 JA |
3363 | UNBLOCK_CHILD (oset); |
3364 | return (-1); | |
3365 | } | |
3366 | ||
ccc6cda3 JA |
3367 | already_running = RUNNING (job); |
3368 | ||
3369 | if (foreground == 0 && already_running) | |
726f6388 | 3370 | { |
b80f6443 | 3371 | internal_error (_("%s: job %d already in background"), this_command_name, job + 1); |
726f6388 | 3372 | UNBLOCK_CHILD (oset); |
95732b49 | 3373 | return (0); /* XPG6/SUSv3 says this is not an error */ |
726f6388 JA |
3374 | } |
3375 | ||
3376 | wd = current_working_directory (); | |
3377 | ||
3378 | /* You don't know about the state of this job. Do you? */ | |
3379 | jobs[job]->flags &= ~J_NOTIFIED; | |
3380 | ||
3381 | if (foreground) | |
3382 | { | |
3383 | set_current_job (job); | |
3384 | jobs[job]->flags |= J_FOREGROUND; | |
3385 | } | |
3386 | ||
3387 | /* Tell the outside world what we're doing. */ | |
3388 | p = jobs[job]->pipe; | |
3389 | ||
ccc6cda3 | 3390 | if (foreground == 0) |
95732b49 JA |
3391 | { |
3392 | /* POSIX.2 says `bg' doesn't give any indication about current or | |
3393 | previous job. */ | |
3394 | if (posixly_correct == 0) | |
3395 | s = (job == js.j_current) ? "+ ": ((job == js.j_previous) ? "- " : " "); | |
3396 | else | |
3397 | s = " "; | |
3398 | printf ("[%d]%s", job + 1, s); | |
3399 | } | |
726f6388 JA |
3400 | |
3401 | do | |
3402 | { | |
95732b49 | 3403 | printf ("%s%s", |
726f6388 JA |
3404 | p->command ? p->command : "", |
3405 | p->next != jobs[job]->pipe? " | " : ""); | |
3406 | p = p->next; | |
3407 | } | |
3408 | while (p != jobs[job]->pipe); | |
3409 | ||
ccc6cda3 | 3410 | if (foreground == 0) |
95732b49 | 3411 | printf (" &"); |
726f6388 JA |
3412 | |
3413 | if (strcmp (wd, jobs[job]->wd) != 0) | |
95732b49 | 3414 | printf (" (wd: %s)", polite_directory_format (jobs[job]->wd)); |
726f6388 | 3415 | |
95732b49 | 3416 | printf ("\n"); |
726f6388 JA |
3417 | |
3418 | /* Run the job. */ | |
ccc6cda3 | 3419 | if (already_running == 0) |
d166f048 | 3420 | set_job_running (job); |
726f6388 JA |
3421 | |
3422 | /* Save the tty settings before we start the job in the foreground. */ | |
3423 | if (foreground) | |
3424 | { | |
3425 | get_tty_state (); | |
3426 | save_stty = shell_tty_info; | |
ccc6cda3 JA |
3427 | /* Give the terminal to this job. */ |
3428 | if (IS_JOBCONTROL (job)) | |
28ef6c31 | 3429 | give_terminal_to (jobs[job]->pgrp, 0); |
726f6388 JA |
3430 | } |
3431 | else | |
3432 | jobs[job]->flags &= ~J_FOREGROUND; | |
3433 | ||
3434 | /* If the job is already running, then don't bother jump-starting it. */ | |
ccc6cda3 | 3435 | if (already_running == 0) |
726f6388 JA |
3436 | { |
3437 | jobs[job]->flags |= J_NOTIFIED; | |
3438 | killpg (jobs[job]->pgrp, SIGCONT); | |
3439 | } | |
3440 | ||
726f6388 JA |
3441 | if (foreground) |
3442 | { | |
ccc6cda3 | 3443 | pid_t pid; |
0628567a | 3444 | int st; |
726f6388 | 3445 | |
7117c2d2 JA |
3446 | pid = find_last_pid (job, 0); |
3447 | UNBLOCK_CHILD (oset); | |
0628567a | 3448 | st = wait_for (pid); |
726f6388 JA |
3449 | shell_tty_info = save_stty; |
3450 | set_tty_state (); | |
0628567a | 3451 | return (st); |
726f6388 JA |
3452 | } |
3453 | else | |
3454 | { | |
726f6388 JA |
3455 | reset_current (); |
3456 | UNBLOCK_CHILD (oset); | |
3457 | return (0); | |
3458 | } | |
3459 | } | |
3460 | ||
3461 | /* Give PID SIGNAL. This determines what job the pid belongs to (if any). | |
3462 | If PID does belong to a job, and the job is stopped, then CONTinue the | |
3463 | job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null, | |
3464 | then kill the process group associated with PID. */ | |
3465 | int | |
3466 | kill_pid (pid, sig, group) | |
3467 | pid_t pid; | |
3468 | int sig, group; | |
3469 | { | |
3470 | register PROCESS *p; | |
95732b49 | 3471 | int job, result, negative; |
726f6388 JA |
3472 | sigset_t set, oset; |
3473 | ||
95732b49 JA |
3474 | if (pid < -1) |
3475 | { | |
3476 | pid = -pid; | |
3477 | group = negative = 1; | |
3478 | } | |
3479 | else | |
3480 | negative = 0; | |
3481 | ||
ccc6cda3 | 3482 | result = EXECUTION_SUCCESS; |
726f6388 JA |
3483 | if (group) |
3484 | { | |
f73dda09 | 3485 | BLOCK_CHILD (set, oset); |
7117c2d2 | 3486 | p = find_pipeline (pid, 0, &job); |
f73dda09 | 3487 | |
726f6388 JA |
3488 | if (job != NO_JOB) |
3489 | { | |
3490 | jobs[job]->flags &= ~J_NOTIFIED; | |
3491 | ||
3492 | /* Kill process in backquotes or one started without job control? */ | |
95732b49 JA |
3493 | |
3494 | /* If we're passed a pid < -1, just call killpg and see what happens */ | |
3495 | if (negative && jobs[job]->pgrp == shell_pgrp) | |
3496 | result = killpg (pid, sig); | |
3497 | /* If we're killing using job control notification, for example, | |
3498 | without job control active, we have to do things ourselves. */ | |
3499 | else if (jobs[job]->pgrp == shell_pgrp) | |
726f6388 JA |
3500 | { |
3501 | p = jobs[job]->pipe; | |
726f6388 JA |
3502 | do |
3503 | { | |
95732b49 JA |
3504 | if (PALIVE (p) == 0) |
3505 | continue; /* avoid pid recycling problem */ | |
726f6388 | 3506 | kill (p->pid, sig); |
95732b49 | 3507 | if (PEXITED (p) && (sig == SIGTERM || sig == SIGHUP)) |
726f6388 JA |
3508 | kill (p->pid, SIGCONT); |
3509 | p = p->next; | |
3510 | } | |
95732b49 | 3511 | while (p != jobs[job]->pipe); |
726f6388 JA |
3512 | } |
3513 | else | |
3514 | { | |
3515 | result = killpg (jobs[job]->pgrp, sig); | |
ccc6cda3 | 3516 | if (p && STOPPED (job) && (sig == SIGTERM || sig == SIGHUP)) |
726f6388 | 3517 | killpg (jobs[job]->pgrp, SIGCONT); |
d166f048 JA |
3518 | /* If we're continuing a stopped job via kill rather than bg or |
3519 | fg, emulate the `bg' behavior. */ | |
3520 | if (p && STOPPED (job) && (sig == SIGCONT)) | |
3521 | { | |
3522 | set_job_running (job); | |
3523 | jobs[job]->flags &= ~J_FOREGROUND; | |
3524 | jobs[job]->flags |= J_NOTIFIED; | |
3525 | } | |
726f6388 JA |
3526 | } |
3527 | } | |
3528 | else | |
3529 | result = killpg (pid, sig); | |
f73dda09 JA |
3530 | |
3531 | UNBLOCK_CHILD (oset); | |
726f6388 JA |
3532 | } |
3533 | else | |
3534 | result = kill (pid, sig); | |
3535 | ||
726f6388 JA |
3536 | return (result); |
3537 | } | |
3538 | ||
ccc6cda3 JA |
3539 | /* sigchld_handler () flushes at least one of the children that we are |
3540 | waiting for. It gets run when we have gotten a SIGCHLD signal. */ | |
726f6388 | 3541 | static sighandler |
ccc6cda3 | 3542 | sigchld_handler (sig) |
726f6388 JA |
3543 | int sig; |
3544 | { | |
bb70624e | 3545 | int n, oerrno; |
ccc6cda3 | 3546 | |
bb70624e | 3547 | oerrno = errno; |
726f6388 JA |
3548 | REINSTALL_SIGCHLD_HANDLER; |
3549 | sigchld++; | |
ccc6cda3 | 3550 | n = 0; |
7117c2d2 | 3551 | if (queue_sigchld == 0) |
ccc6cda3 | 3552 | n = waitchld (-1, 0); |
bb70624e | 3553 | errno = oerrno; |
ccc6cda3 | 3554 | SIGRETURN (n); |
726f6388 | 3555 | } |
ccc6cda3 JA |
3556 | |
3557 | /* waitchld() reaps dead or stopped children. It's called by wait_for and | |
bb70624e JA |
3558 | sigchld_handler, and runs until there aren't any children terminating any |
3559 | more. | |
ccc6cda3 | 3560 | If BLOCK is 1, this is to be a blocking wait for a single child, although |
bb70624e JA |
3561 | an arriving SIGCHLD could cause the wait to be non-blocking. It returns |
3562 | the number of children reaped, or -1 if there are no unwaited-for child | |
3563 | processes. */ | |
726f6388 | 3564 | static int |
ccc6cda3 JA |
3565 | waitchld (wpid, block) |
3566 | pid_t wpid; | |
3567 | int block; | |
726f6388 JA |
3568 | { |
3569 | WAIT status; | |
3570 | PROCESS *child; | |
3571 | pid_t pid; | |
d233b485 | 3572 | int ind; |
ac50fbac | 3573 | |
28ef6c31 | 3574 | int call_set_current, last_stopped_job, job, children_exited, waitpid_flags; |
95732b49 | 3575 | static int wcontinued = WCONTINUED; /* run-time fix for glibc problem */ |
ccc6cda3 JA |
3576 | |
3577 | call_set_current = children_exited = 0; | |
3578 | last_stopped_job = NO_JOB; | |
726f6388 JA |
3579 | |
3580 | do | |
3581 | { | |
d166f048 | 3582 | /* We don't want to be notified about jobs stopping if job control |
28ef6c31 | 3583 | is not active. XXX - was interactive_shell instead of job_control */ |
d166f048 | 3584 | waitpid_flags = (job_control && subshell_environment == 0) |
95732b49 | 3585 | ? (WUNTRACED|wcontinued) |
ccc6cda3 JA |
3586 | : 0; |
3587 | if (sigchld || block == 0) | |
3588 | waitpid_flags |= WNOHANG; | |
ac50fbac | 3589 | |
0001803f | 3590 | /* Check for terminating signals and exit the shell if we receive one */ |
0628567a | 3591 | CHECK_TERMSIG; |
ac50fbac CR |
3592 | /* Check for a trapped signal interrupting the wait builtin and jump out */ |
3593 | CHECK_WAIT_INTR; | |
3185942a | 3594 | |
0001803f CR |
3595 | if (block == 1 && queue_sigchld == 0 && (waitpid_flags & WNOHANG) == 0) |
3596 | { | |
3597 | internal_warning (_("waitchld: turning on WNOHANG to avoid indefinite block")); | |
3598 | waitpid_flags |= WNOHANG; | |
3599 | } | |
3600 | ||
ccc6cda3 | 3601 | pid = WAITPID (-1, &status, waitpid_flags); |
f73dda09 | 3602 | |
ac50fbac CR |
3603 | #if 0 |
3604 | if (wpid != -1 && block) | |
3605 | itrace("waitchld: blocking waitpid returns %d", pid); | |
a0c0a00f CR |
3606 | #endif |
3607 | #if 0 | |
3608 | if (wpid != -1) | |
3609 | itrace("waitchld: %s waitpid returns %d", block?"blocking":"non-blocking", pid); | |
ac50fbac | 3610 | #endif |
95732b49 JA |
3611 | /* WCONTINUED may be rejected by waitpid as invalid even when defined */ |
3612 | if (wcontinued && pid < 0 && errno == EINVAL) | |
3613 | { | |
3614 | wcontinued = 0; | |
3615 | continue; /* jump back to the test and retry without WCONTINUED */ | |
3616 | } | |
3617 | ||
ccc6cda3 JA |
3618 | /* The check for WNOHANG is to make sure we decrement sigchld only |
3619 | if it was non-zero before we called waitpid. */ | |
3620 | if (sigchld > 0 && (waitpid_flags & WNOHANG)) | |
3621 | sigchld--; | |
ac50fbac | 3622 | |
bb70624e JA |
3623 | /* If waitpid returns -1 with errno == ECHILD, there are no more |
3624 | unwaited-for child processes of this shell. */ | |
3625 | if (pid < 0 && errno == ECHILD) | |
3626 | { | |
3627 | if (children_exited == 0) | |
3628 | return -1; | |
3629 | else | |
3630 | break; | |
3631 | } | |
ccc6cda3 | 3632 | |
ac50fbac | 3633 | #if 0 |
a0c0a00f | 3634 | itrace("waitchld: waitpid returns %d block = %d children_exited = %d", pid, block, children_exited); |
ac50fbac | 3635 | #endif |
bb70624e JA |
3636 | /* If waitpid returns 0, there are running children. If it returns -1, |
3637 | the only other error POSIX says it can return is EINTR. */ | |
0628567a | 3638 | CHECK_TERMSIG; |
ac50fbac CR |
3639 | CHECK_WAIT_INTR; |
3640 | ||
3641 | /* If waitpid returns -1/EINTR and the shell saw a SIGINT, then we | |
3642 | assume the child has blocked or handled SIGINT. In that case, we | |
3643 | require the child to actually die due to SIGINT to act on the | |
3644 | SIGINT we received; otherwise we assume the child handled it and | |
3645 | let it go. */ | |
3646 | if (pid < 0 && errno == EINTR && wait_sigint_received) | |
3647 | child_caught_sigint = 1; | |
3648 | ||
ccc6cda3 JA |
3649 | if (pid <= 0) |
3650 | continue; /* jumps right to the test */ | |
3651 | ||
a0c0a00f CR |
3652 | /* Linux kernels appear to signal the parent but not interrupt the |
3653 | waitpid() (or restart it even without SA_RESTART) on SIGINT, so if | |
3654 | we saw a SIGINT and the process exited or died due to some other | |
3655 | signal, assume the child caught the SIGINT. */ | |
3656 | if (wait_sigint_received && (WIFSIGNALED (status) == 0 || WTERMSIG (status) != SIGINT)) | |
3657 | child_caught_sigint = 1; | |
3658 | ||
ac50fbac CR |
3659 | /* If the child process did die due to SIGINT, forget our assumption |
3660 | that it caught or otherwise handled it. */ | |
3661 | if (WIFSIGNALED (status) && WTERMSIG (status) == SIGINT) | |
3662 | child_caught_sigint = 0; | |
3663 | ||
f73dda09 JA |
3664 | /* children_exited is used to run traps on SIGCHLD. We don't want to |
3665 | run the trap if a process is just being continued. */ | |
3666 | if (WIFCONTINUED(status) == 0) | |
0628567a JA |
3667 | { |
3668 | children_exited++; | |
3669 | js.c_living--; | |
3670 | } | |
ccc6cda3 JA |
3671 | |
3672 | /* Locate our PROCESS for this pid. */ | |
95732b49 | 3673 | child = find_process (pid, 1, &job); /* want living procs only */ |
ccc6cda3 | 3674 | |
3185942a | 3675 | #if defined (COPROCESS_SUPPORT) |
ac50fbac | 3676 | coproc_pidchk (pid, WSTATUS(status)); |
3185942a JA |
3677 | #endif |
3678 | ||
d233b485 CR |
3679 | #if defined (PROCESS_SUBSTITUTION) |
3680 | /* Only manipulate the list of process substitutions while SIGCHLD | |
3681 | is blocked. */ | |
3682 | if ((ind = find_procsub_child (pid)) >= 0) | |
3683 | set_procsub_status (ind, pid, WSTATUS (status)); | |
3684 | /* XXX - save in bgpids list? */ | |
3685 | #endif | |
3686 | ||
ccc6cda3 JA |
3687 | /* It is not an error to have a child terminate that we did |
3688 | not have a record of. This child could have been part of | |
3689 | a pipeline in backquote substitution. Even so, I'm not | |
3690 | sure child is ever non-zero. */ | |
3691 | if (child == 0) | |
3185942a JA |
3692 | { |
3693 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
3694 | js.c_reaped++; | |
3695 | continue; | |
3696 | } | |
ccc6cda3 | 3697 | |
f73dda09 | 3698 | /* Remember status, and whether or not the process is running. */ |
ccc6cda3 | 3699 | child->status = status; |
7117c2d2 | 3700 | child->running = WIFCONTINUED(status) ? PS_RUNNING : PS_DONE; |
ccc6cda3 | 3701 | |
95732b49 JA |
3702 | if (PEXITED (child)) |
3703 | { | |
3704 | js.c_totreaped++; | |
3705 | if (job != NO_JOB) | |
3706 | js.c_reaped++; | |
3707 | } | |
a0c0a00f | 3708 | |
ccc6cda3 | 3709 | if (job == NO_JOB) |
28ef6c31 | 3710 | continue; |
726f6388 | 3711 | |
28ef6c31 | 3712 | call_set_current += set_job_status_and_cleanup (job); |
726f6388 | 3713 | |
28ef6c31 JA |
3714 | if (STOPPED (job)) |
3715 | last_stopped_job = job; | |
3716 | else if (DEADJOB (job) && last_stopped_job == job) | |
3717 | last_stopped_job = NO_JOB; | |
726f6388 | 3718 | } |
ccc6cda3 | 3719 | while ((sigchld || block == 0) && pid > (pid_t)0); |
726f6388 JA |
3720 | |
3721 | /* If a job was running and became stopped, then set the current | |
3722 | job. Otherwise, don't change a thing. */ | |
3723 | if (call_set_current) | |
bb70624e JA |
3724 | { |
3725 | if (last_stopped_job != NO_JOB) | |
3726 | set_current_job (last_stopped_job); | |
3727 | else | |
3728 | reset_current (); | |
3729 | } | |
726f6388 JA |
3730 | |
3731 | /* Call a SIGCHLD trap handler for each child that exits, if one is set. */ | |
d233b485 | 3732 | if (children_exited && |
a0c0a00f | 3733 | (signal_is_trapped (SIGCHLD) || trap_list[SIGCHLD] == (char *)IMPOSSIBLE_TRAP_HANDLER) && |
726f6388 | 3734 | trap_list[SIGCHLD] != (char *)IGNORE_SIG) |
3185942a | 3735 | { |
0001803f | 3736 | if (posixly_correct && this_shell_builtin && this_shell_builtin == wait_builtin) |
3185942a JA |
3737 | { |
3738 | interrupt_immediately = 0; | |
d836e8a2 CR |
3739 | /* This was trap_handler (SIGCHLD) but that can lose traps if |
3740 | children_exited > 1 */ | |
3741 | queue_sigchld_trap (children_exited); | |
3185942a | 3742 | wait_signal_received = SIGCHLD; |
ac50fbac CR |
3743 | /* If we're in a signal handler, let CHECK_WAIT_INTR pick it up; |
3744 | run_pending_traps will call run_sigchld_trap later */ | |
a0c0a00f CR |
3745 | if (sigchld == 0 && wait_intr_flag) |
3746 | sh_longjmp (wait_intr_buf, 1); | |
3185942a | 3747 | } |
ac50fbac CR |
3748 | /* If not in posix mode and not executing the wait builtin, queue the |
3749 | signal for later handling. Run the trap immediately if we are | |
3750 | executing the wait builtin, but don't break out of `wait'. */ | |
3751 | else if (sigchld) /* called from signal handler */ | |
3752 | queue_sigchld_trap (children_exited); | |
a0c0a00f CR |
3753 | else if (signal_in_progress (SIGCHLD)) |
3754 | queue_sigchld_trap (children_exited); | |
3755 | else if (trap_list[SIGCHLD] == (char *)IMPOSSIBLE_TRAP_HANDLER) | |
3756 | queue_sigchld_trap (children_exited); | |
ac50fbac CR |
3757 | else if (running_trap) |
3758 | queue_sigchld_trap (children_exited); | |
3759 | else if (this_shell_builtin == wait_builtin) | |
3760 | run_sigchld_trap (children_exited); /* XXX */ | |
3761 | else | |
3762 | queue_sigchld_trap (children_exited); | |
3185942a | 3763 | } |
726f6388 JA |
3764 | |
3765 | /* We have successfully recorded the useful information about this process | |
3766 | that has just changed state. If we notify asynchronously, and the job | |
3767 | that this process belongs to is no longer running, then notify the user | |
3768 | of that fact now. */ | |
3769 | if (asynchronous_notification && interactive) | |
3770 | notify_of_job_status (); | |
3771 | ||
ccc6cda3 | 3772 | return (children_exited); |
726f6388 JA |
3773 | } |
3774 | ||
28ef6c31 JA |
3775 | /* Set the status of JOB and perform any necessary cleanup if the job is |
3776 | marked as JDEAD. | |
3777 | ||
3778 | Currently, the cleanup activity is restricted to handling any SIGINT | |
3779 | received while waiting for a foreground job to finish. */ | |
3780 | static int | |
3781 | set_job_status_and_cleanup (job) | |
3782 | int job; | |
3783 | { | |
3784 | PROCESS *child; | |
3785 | int tstatus, job_state, any_stopped, any_tstped, call_set_current; | |
3786 | SigHandler *temp_handler; | |
3787 | ||
3788 | child = jobs[job]->pipe; | |
3789 | jobs[job]->flags &= ~J_NOTIFIED; | |
3790 | ||
3791 | call_set_current = 0; | |
3792 | ||
3793 | /* | |
3794 | * COMPUTE JOB STATUS | |
3795 | */ | |
3796 | ||
3797 | /* If all children are not running, but any of them is stopped, then | |
3798 | the job is stopped, not dead. */ | |
3799 | job_state = any_stopped = any_tstped = 0; | |
3800 | do | |
3801 | { | |
95732b49 JA |
3802 | job_state |= PRUNNING (child); |
3803 | #if 0 | |
3804 | if (PEXITED (child) && (WIFSTOPPED (child->status))) | |
3805 | #else | |
3806 | /* Only checking for WIFSTOPPED now, not for PS_DONE */ | |
3807 | if (PSTOPPED (child)) | |
3808 | #endif | |
28ef6c31 JA |
3809 | { |
3810 | any_stopped = 1; | |
ac50fbac | 3811 | any_tstped |= job_control && (WSTOPSIG (child->status) == SIGTSTP); |
28ef6c31 JA |
3812 | } |
3813 | child = child->next; | |
3814 | } | |
3815 | while (child != jobs[job]->pipe); | |
3816 | ||
3817 | /* If job_state != 0, the job is still running, so don't bother with | |
f73dda09 JA |
3818 | setting the process exit status and job state unless we're |
3819 | transitioning from stopped to running. */ | |
3820 | if (job_state != 0 && JOBSTATE(job) != JSTOPPED) | |
28ef6c31 JA |
3821 | return 0; |
3822 | ||
3823 | /* | |
3824 | * SET JOB STATUS | |
3825 | */ | |
3826 | ||
3827 | /* The job is either stopped or dead. Set the state of the job accordingly. */ | |
3828 | if (any_stopped) | |
3829 | { | |
3830 | jobs[job]->state = JSTOPPED; | |
3831 | jobs[job]->flags &= ~J_FOREGROUND; | |
3832 | call_set_current++; | |
3833 | /* Suspending a job with SIGTSTP breaks all active loops. */ | |
3834 | if (any_tstped && loop_level) | |
3835 | breaking = loop_level; | |
3836 | } | |
f73dda09 JA |
3837 | else if (job_state != 0) /* was stopped, now running */ |
3838 | { | |
3839 | jobs[job]->state = JRUNNING; | |
3840 | call_set_current++; | |
3841 | } | |
28ef6c31 JA |
3842 | else |
3843 | { | |
3844 | jobs[job]->state = JDEAD; | |
95732b49 | 3845 | js.j_ndead++; |
28ef6c31 | 3846 | |
7117c2d2 | 3847 | #if 0 |
28ef6c31 JA |
3848 | if (IS_FOREGROUND (job)) |
3849 | setjstatus (job); | |
7117c2d2 | 3850 | #endif |
28ef6c31 JA |
3851 | |
3852 | /* If this job has a cleanup function associated with it, call it | |
3853 | with `cleanarg' as the single argument, then set the function | |
3854 | pointer to NULL so it is not inadvertently called twice. The | |
3855 | cleanup function is responsible for deallocating cleanarg. */ | |
3856 | if (jobs[job]->j_cleanup) | |
3857 | { | |
3858 | (*jobs[job]->j_cleanup) (jobs[job]->cleanarg); | |
f73dda09 | 3859 | jobs[job]->j_cleanup = (sh_vptrfunc_t *)NULL; |
28ef6c31 JA |
3860 | } |
3861 | } | |
3862 | ||
3863 | /* | |
3864 | * CLEANUP | |
3865 | * | |
3866 | * Currently, we just do special things if we got a SIGINT while waiting | |
3867 | * for a foreground job to complete | |
3868 | */ | |
3869 | ||
95732b49 | 3870 | if (JOBSTATE (job) == JDEAD) |
28ef6c31 JA |
3871 | { |
3872 | /* If we're running a shell script and we get a SIGINT with a | |
3873 | SIGINT trap handler, but the foreground job handles it and | |
3874 | does not exit due to SIGINT, run the trap handler but do not | |
3875 | otherwise act as if we got the interrupt. */ | |
3876 | if (wait_sigint_received && interactive_shell == 0 && | |
ac50fbac | 3877 | child_caught_sigint && IS_FOREGROUND (job) && |
28ef6c31 JA |
3878 | signal_is_trapped (SIGINT)) |
3879 | { | |
7117c2d2 | 3880 | int old_frozen; |
28ef6c31 JA |
3881 | wait_sigint_received = 0; |
3882 | last_command_exit_value = process_exit_status (child->status); | |
3883 | ||
7117c2d2 | 3884 | old_frozen = jobs_list_frozen; |
28ef6c31 JA |
3885 | jobs_list_frozen = 1; |
3886 | tstatus = maybe_call_trap_handler (SIGINT); | |
7117c2d2 | 3887 | jobs_list_frozen = old_frozen; |
28ef6c31 JA |
3888 | } |
3889 | ||
3890 | /* If the foreground job is killed by SIGINT when job control is not | |
3891 | active, we need to perform some special handling. | |
3892 | ||
3893 | The check of wait_sigint_received is a way to determine if the | |
3894 | SIGINT came from the keyboard (in which case the shell has already | |
3895 | seen it, and wait_sigint_received is non-zero, because keyboard | |
3896 | signals are sent to process groups) or via kill(2) to the foreground | |
3897 | process by another process (or itself). If the shell did receive the | |
a0c0a00f CR |
3898 | SIGINT, it needs to perform normal SIGINT processing. XXX - should |
3899 | this change its behavior depending on whether the last command in an | |
3900 | pipeline exited due to SIGINT, or any process in the pipeline? Right | |
3901 | now it does this if any process in the pipeline exits due to SIGINT. */ | |
ac50fbac CR |
3902 | else if (wait_sigint_received && |
3903 | child_caught_sigint == 0 && | |
28ef6c31 JA |
3904 | IS_FOREGROUND (job) && IS_JOBCONTROL (job) == 0) |
3905 | { | |
7117c2d2 JA |
3906 | int old_frozen; |
3907 | ||
28ef6c31 JA |
3908 | wait_sigint_received = 0; |
3909 | ||
3910 | /* If SIGINT is trapped, set the exit status so that the trap | |
3911 | handler can see it. */ | |
3912 | if (signal_is_trapped (SIGINT)) | |
3913 | last_command_exit_value = process_exit_status (child->status); | |
3914 | ||
3915 | /* If the signal is trapped, let the trap handler get it no matter | |
3916 | what and simply return if the trap handler returns. | |
3917 | maybe_call_trap_handler() may cause dead jobs to be removed from | |
3918 | the job table because of a call to execute_command. We work | |
3919 | around this by setting JOBS_LIST_FROZEN. */ | |
7117c2d2 | 3920 | old_frozen = jobs_list_frozen; |
28ef6c31 JA |
3921 | jobs_list_frozen = 1; |
3922 | tstatus = maybe_call_trap_handler (SIGINT); | |
7117c2d2 | 3923 | jobs_list_frozen = old_frozen; |
28ef6c31 JA |
3924 | if (tstatus == 0 && old_sigint_handler != INVALID_SIGNAL_HANDLER) |
3925 | { | |
3926 | /* wait_sigint_handler () has already seen SIGINT and | |
3927 | allowed the wait builtin to jump out. We need to | |
3928 | call the original SIGINT handler, if necessary. If | |
3929 | the original handler is SIG_DFL, we need to resend | |
3930 | the signal to ourselves. */ | |
3931 | ||
3932 | temp_handler = old_sigint_handler; | |
3933 | ||
3934 | /* Bogus. If we've reset the signal handler as the result | |
3935 | of a trap caught on SIGINT, then old_sigint_handler | |
3936 | will point to trap_handler, which now knows nothing about | |
3937 | SIGINT (if we reset the sighandler to the default). | |
3938 | In this case, we have to fix things up. What a crock. */ | |
3939 | if (temp_handler == trap_handler && signal_is_trapped (SIGINT) == 0) | |
3940 | temp_handler = trap_to_sighandler (SIGINT); | |
a0c0a00f | 3941 | restore_sigint_handler (); |
95732b49 | 3942 | if (temp_handler == SIG_DFL) |
ac50fbac | 3943 | termsig_handler (SIGINT); /* XXX */ |
95732b49 JA |
3944 | else if (temp_handler != SIG_IGN) |
3945 | (*temp_handler) (SIGINT); | |
28ef6c31 JA |
3946 | } |
3947 | } | |
3948 | } | |
3949 | ||
3950 | return call_set_current; | |
3951 | } | |
3952 | ||
3953 | /* Build the array of values for the $PIPESTATUS variable from the set of | |
3954 | exit statuses of all processes in the job J. */ | |
3955 | static void | |
3956 | setjstatus (j) | |
3957 | int j; | |
3958 | { | |
3959 | #if defined (ARRAY_VARS) | |
3960 | register int i; | |
3961 | register PROCESS *p; | |
3962 | ||
3963 | for (i = 1, p = jobs[j]->pipe; p->next != jobs[j]->pipe; p = p->next, i++) | |
3964 | ; | |
3965 | i++; | |
7117c2d2 | 3966 | if (statsize < i) |
28ef6c31 JA |
3967 | { |
3968 | pstatuses = (int *)xrealloc (pstatuses, i * sizeof (int)); | |
3969 | statsize = i; | |
3970 | } | |
3971 | i = 0; | |
3972 | p = jobs[j]->pipe; | |
3973 | do | |
3974 | { | |
3975 | pstatuses[i++] = process_exit_status (p->status); | |
3976 | p = p->next; | |
3977 | } | |
3978 | while (p != jobs[j]->pipe); | |
3979 | ||
3980 | pstatuses[i] = -1; /* sentinel */ | |
7117c2d2 | 3981 | set_pipestatus_array (pstatuses, i); |
28ef6c31 JA |
3982 | #endif |
3983 | } | |
3984 | ||
3185942a | 3985 | void |
28ef6c31 JA |
3986 | run_sigchld_trap (nchild) |
3987 | int nchild; | |
3988 | { | |
3989 | char *trap_command; | |
3990 | int i; | |
3991 | ||
3992 | /* Turn off the trap list during the call to parse_and_execute () | |
3993 | to avoid potentially infinite recursive calls. Preserve the | |
3994 | values of last_command_exit_value, last_made_pid, and the_pipeline | |
3995 | around the execution of the trap commands. */ | |
3996 | trap_command = savestring (trap_list[SIGCHLD]); | |
3997 | ||
3998 | begin_unwind_frame ("SIGCHLD trap"); | |
3999 | unwind_protect_int (last_command_exit_value); | |
b80f6443 | 4000 | unwind_protect_int (last_command_exit_signal); |
f73dda09 | 4001 | unwind_protect_var (last_made_pid); |
28ef6c31 JA |
4002 | unwind_protect_int (interrupt_immediately); |
4003 | unwind_protect_int (jobs_list_frozen); | |
4004 | unwind_protect_pointer (the_pipeline); | |
4005 | unwind_protect_pointer (subst_assign_varlist); | |
4a741ca4 | 4006 | unwind_protect_pointer (this_shell_builtin); |
a0c0a00f | 4007 | unwind_protect_pointer (temporary_env); |
28ef6c31 JA |
4008 | |
4009 | /* We have to add the commands this way because they will be run | |
4010 | in reverse order of adding. We don't want maybe_set_sigchld_trap () | |
4011 | to reference freed memory. */ | |
b80f6443 JA |
4012 | add_unwind_protect (xfree, trap_command); |
4013 | add_unwind_protect (maybe_set_sigchld_trap, trap_command); | |
28ef6c31 JA |
4014 | |
4015 | subst_assign_varlist = (WORD_LIST *)NULL; | |
4016 | the_pipeline = (PROCESS *)NULL; | |
a0c0a00f | 4017 | temporary_env = 0; /* traps should not run with temporary env */ |
28ef6c31 | 4018 | |
ac50fbac CR |
4019 | running_trap = SIGCHLD + 1; |
4020 | ||
3185942a | 4021 | set_impossible_sigchld_trap (); |
28ef6c31 JA |
4022 | jobs_list_frozen = 1; |
4023 | for (i = 0; i < nchild; i++) | |
4024 | { | |
ac50fbac | 4025 | #if 0 |
28ef6c31 | 4026 | interrupt_immediately = 1; |
ac50fbac | 4027 | #endif |
b80f6443 | 4028 | parse_and_execute (savestring (trap_command), "trap", SEVAL_NOHIST|SEVAL_RESETLINE); |
28ef6c31 JA |
4029 | } |
4030 | ||
4031 | run_unwind_frame ("SIGCHLD trap"); | |
ac50fbac | 4032 | running_trap = 0; |
28ef6c31 JA |
4033 | } |
4034 | ||
726f6388 JA |
4035 | /* Function to call when you want to notify people of changes |
4036 | in job status. This prints out all jobs which are pending | |
4037 | notification to stderr, and marks those printed as already | |
4038 | notified, thus making them candidates for cleanup. */ | |
4039 | static void | |
4040 | notify_of_job_status () | |
4041 | { | |
4042 | register int job, termsig; | |
4043 | char *dir; | |
4044 | sigset_t set, oset; | |
ccc6cda3 | 4045 | WAIT s; |
726f6388 | 4046 | |
95732b49 | 4047 | if (jobs == 0 || js.j_jobslots == 0) |
28ef6c31 JA |
4048 | return; |
4049 | ||
7117c2d2 JA |
4050 | if (old_ttou != 0) |
4051 | { | |
4052 | sigemptyset (&set); | |
4053 | sigaddset (&set, SIGCHLD); | |
4054 | sigaddset (&set, SIGTTOU); | |
4055 | sigemptyset (&oset); | |
4056 | sigprocmask (SIG_BLOCK, &set, &oset); | |
4057 | } | |
4058 | else | |
4059 | queue_sigchld++; | |
726f6388 | 4060 | |
95732b49 JA |
4061 | /* XXX could use js.j_firstj here */ |
4062 | for (job = 0, dir = (char *)NULL; job < js.j_jobslots; job++) | |
726f6388 | 4063 | { |
ccc6cda3 | 4064 | if (jobs[job] && IS_NOTIFIED (job) == 0) |
726f6388 | 4065 | { |
bb70624e | 4066 | s = raw_job_exit_status (job); |
726f6388 JA |
4067 | termsig = WTERMSIG (s); |
4068 | ||
b72432fd JA |
4069 | /* POSIX.2 says we have to hang onto the statuses of at most the |
4070 | last CHILD_MAX background processes if the shell is running a | |
95732b49 JA |
4071 | script. If the shell is running a script, either from a file |
4072 | or standard input, don't print anything unless the job was | |
4073 | killed by a signal. */ | |
b72432fd JA |
4074 | if (startup_state == 0 && WIFSIGNALED (s) == 0 && |
4075 | ((DEADJOB (job) && IS_FOREGROUND (job) == 0) || STOPPED (job))) | |
4076 | continue; | |
4077 | ||
b80f6443 JA |
4078 | /* If job control is disabled, don't print the status messages. |
4079 | Mark dead jobs as notified so that they get cleaned up. If | |
4080 | startup_state == 2 and subshell_environment has the | |
4081 | SUBSHELL_COMSUB bit turned on, we were started to run a command | |
d233b485 CR |
4082 | substitution, so don't print anything. |
4083 | Otherwise, if the shell is not interactive, POSIX says that `jobs' | |
4084 | is the only way to notify of job status. */ | |
4085 | #if 1 | |
b80f6443 JA |
4086 | if ((job_control == 0 && interactive_shell) || |
4087 | (startup_state == 2 && (subshell_environment & SUBSHELL_COMSUB))) | |
d233b485 CR |
4088 | #else /* TAG:bash-5.1 */ |
4089 | if ((job_control == 0 && interactive_shell) || | |
4090 | (startup_state == 2 && (subshell_environment & SUBSHELL_COMSUB)) || | |
4091 | (startup_state == 2 && posixly_correct && (subshell_environment & SUBSHELL_COMSUB) == 0)) | |
b80f6443 | 4092 | #endif |
726f6388 | 4093 | { |
cce855bc JA |
4094 | /* POSIX.2 compatibility: if the shell is not interactive, |
4095 | hang onto the job corresponding to the last asynchronous | |
4096 | pid until the user has been notified of its status or does | |
4097 | a `wait'. */ | |
7117c2d2 | 4098 | if (DEADJOB (job) && (interactive_shell || (find_last_pid (job, 0) != last_asynchronous_pid))) |
726f6388 JA |
4099 | jobs[job]->flags |= J_NOTIFIED; |
4100 | continue; | |
4101 | } | |
4102 | ||
ccc6cda3 JA |
4103 | /* Print info on jobs that are running in the background, |
4104 | and on foreground jobs that were killed by anything | |
d166f048 | 4105 | except SIGINT (and possibly SIGPIPE). */ |
726f6388 JA |
4106 | switch (JOBSTATE (job)) |
4107 | { | |
726f6388 | 4108 | case JDEAD: |
ccc6cda3 | 4109 | if (interactive_shell == 0 && termsig && WIFSIGNALED (s) && |
cce855bc | 4110 | termsig != SIGINT && |
ac50fbac CR |
4111 | #if defined (DONT_REPORT_SIGTERM) |
4112 | termsig != SIGTERM && | |
4113 | #endif | |
cce855bc JA |
4114 | #if defined (DONT_REPORT_SIGPIPE) |
4115 | termsig != SIGPIPE && | |
4116 | #endif | |
ccc6cda3 | 4117 | signal_is_trapped (termsig) == 0) |
28ef6c31 | 4118 | { |
b80f6443 | 4119 | /* Don't print `0' for a line number. */ |
3185942a | 4120 | fprintf (stderr, _("%s: line %d: "), get_name_for_error (), (line_number == 0) ? 1 : line_number); |
ccc6cda3 JA |
4121 | pretty_print_job (job, JLIST_NONINTERACTIVE, stderr); |
4122 | } | |
4123 | else if (IS_FOREGROUND (job)) | |
726f6388 | 4124 | { |
d166f048 | 4125 | #if !defined (DONT_REPORT_SIGPIPE) |
726f6388 | 4126 | if (termsig && WIFSIGNALED (s) && termsig != SIGINT) |
d166f048 JA |
4127 | #else |
4128 | if (termsig && WIFSIGNALED (s) && termsig != SIGINT && termsig != SIGPIPE) | |
4129 | #endif | |
726f6388 | 4130 | { |
b80f6443 | 4131 | fprintf (stderr, "%s", j_strsignal (termsig)); |
726f6388 JA |
4132 | |
4133 | if (WIFCORED (s)) | |
3185942a | 4134 | fprintf (stderr, _(" (core dumped)")); |
726f6388 JA |
4135 | |
4136 | fprintf (stderr, "\n"); | |
4137 | } | |
4138 | } | |
95732b49 | 4139 | else if (job_control) /* XXX job control test added */ |
726f6388 | 4140 | { |
ccc6cda3 | 4141 | if (dir == 0) |
726f6388 | 4142 | dir = current_working_directory (); |
ccc6cda3 | 4143 | pretty_print_job (job, JLIST_STANDARD, stderr); |
726f6388 JA |
4144 | if (dir && strcmp (dir, jobs[job]->wd) != 0) |
4145 | fprintf (stderr, | |
3185942a | 4146 | _("(wd now: %s)\n"), polite_directory_format (dir)); |
726f6388 JA |
4147 | } |
4148 | ||
4149 | jobs[job]->flags |= J_NOTIFIED; | |
4150 | break; | |
4151 | ||
4152 | case JSTOPPED: | |
4153 | fprintf (stderr, "\n"); | |
ccc6cda3 | 4154 | if (dir == 0) |
726f6388 | 4155 | dir = current_working_directory (); |
ccc6cda3 | 4156 | pretty_print_job (job, JLIST_STANDARD, stderr); |
726f6388 JA |
4157 | if (dir && (strcmp (dir, jobs[job]->wd) != 0)) |
4158 | fprintf (stderr, | |
3185942a | 4159 | _("(wd now: %s)\n"), polite_directory_format (dir)); |
726f6388 JA |
4160 | jobs[job]->flags |= J_NOTIFIED; |
4161 | break; | |
4162 | ||
4163 | case JRUNNING: | |
4164 | case JMIXED: | |
4165 | break; | |
4166 | ||
4167 | default: | |
4168 | programming_error ("notify_of_job_status"); | |
4169 | } | |
4170 | } | |
4171 | } | |
7117c2d2 JA |
4172 | if (old_ttou != 0) |
4173 | sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); | |
4174 | else | |
4175 | queue_sigchld--; | |
726f6388 JA |
4176 | } |
4177 | ||
726f6388 | 4178 | /* Initialize the job control mechanism, and set up the tty stuff. */ |
ccc6cda3 | 4179 | int |
d166f048 JA |
4180 | initialize_job_control (force) |
4181 | int force; | |
726f6388 | 4182 | { |
3185942a | 4183 | pid_t t; |
d233b485 | 4184 | int t_errno, tty_sigs; |
3185942a JA |
4185 | |
4186 | t_errno = -1; | |
726f6388 JA |
4187 | shell_pgrp = getpgid (0); |
4188 | ||
4189 | if (shell_pgrp == -1) | |
4190 | { | |
3185942a | 4191 | sys_error (_("initialize_job_control: getpgrp failed")); |
726f6388 JA |
4192 | exit (1); |
4193 | } | |
4194 | ||
ac50fbac CR |
4195 | /* We can only have job control if we are interactive unless we force it. */ |
4196 | if (interactive == 0 && force == 0) | |
726f6388 JA |
4197 | { |
4198 | job_control = 0; | |
4199 | original_pgrp = NO_PID; | |
d166f048 | 4200 | shell_tty = fileno (stderr); |
a0c0a00f | 4201 | terminal_pgrp = tcgetpgrp (shell_tty); /* for checking later */ |
726f6388 JA |
4202 | } |
4203 | else | |
4204 | { | |
3185942a JA |
4205 | shell_tty = -1; |
4206 | ||
4207 | /* If forced_interactive is set, we skip the normal check that stderr | |
4208 | is attached to a tty, so we need to check here. If it's not, we | |
4209 | need to see whether we have a controlling tty by opening /dev/tty, | |
4210 | since trying to use job control tty pgrp manipulations on a non-tty | |
4211 | is going to fail. */ | |
4212 | if (forced_interactive && isatty (fileno (stderr)) == 0) | |
4213 | shell_tty = open ("/dev/tty", O_RDWR|O_NONBLOCK); | |
4214 | ||
726f6388 JA |
4215 | /* Get our controlling terminal. If job_control is set, or |
4216 | interactive is set, then this is an interactive shell no | |
ccc6cda3 | 4217 | matter where fd 2 is directed. */ |
3185942a JA |
4218 | if (shell_tty == -1) |
4219 | shell_tty = dup (fileno (stderr)); /* fd 2 */ | |
726f6388 | 4220 | |
ac50fbac CR |
4221 | if (shell_tty != -1) |
4222 | shell_tty = move_to_high_fd (shell_tty, 1, -1); | |
726f6388 | 4223 | |
726f6388 | 4224 | /* Compensate for a bug in systems that compiled the BSD |
ccc6cda3 | 4225 | rlogind with DEBUG defined, like NeXT and Alliant. */ |
726f6388 JA |
4226 | if (shell_pgrp == 0) |
4227 | { | |
4228 | shell_pgrp = getpid (); | |
4229 | setpgid (0, shell_pgrp); | |
a0c0a00f CR |
4230 | if (shell_tty != -1) |
4231 | tcsetpgrp (shell_tty, shell_pgrp); | |
726f6388 | 4232 | } |
726f6388 | 4233 | |
d233b485 | 4234 | tty_sigs = 0; |
726f6388 JA |
4235 | while ((terminal_pgrp = tcgetpgrp (shell_tty)) != -1) |
4236 | { | |
4237 | if (shell_pgrp != terminal_pgrp) | |
4238 | { | |
7117c2d2 | 4239 | SigHandler *ottin; |
ccc6cda3 | 4240 | |
d233b485 | 4241 | CHECK_TERMSIG; |
a0c0a00f | 4242 | ottin = set_signal_handler (SIGTTIN, SIG_DFL); |
726f6388 | 4243 | kill (0, SIGTTIN); |
7117c2d2 | 4244 | set_signal_handler (SIGTTIN, ottin); |
d233b485 CR |
4245 | if (tty_sigs++ > 16) |
4246 | { | |
4247 | sys_error (_("initialize_job_control: no job control in background")); | |
4248 | job_control = 0; | |
4249 | original_pgrp = terminal_pgrp; /* for eventual give_terminal_to */ | |
4250 | goto just_bail; | |
4251 | } | |
726f6388 JA |
4252 | continue; |
4253 | } | |
4254 | break; | |
4255 | } | |
4256 | ||
3185942a JA |
4257 | if (terminal_pgrp == -1) |
4258 | t_errno = errno; | |
4259 | ||
ccc6cda3 | 4260 | /* Make sure that we are using the new line discipline. */ |
726f6388 JA |
4261 | if (set_new_line_discipline (shell_tty) < 0) |
4262 | { | |
3185942a | 4263 | sys_error (_("initialize_job_control: line discipline")); |
726f6388 JA |
4264 | job_control = 0; |
4265 | } | |
4266 | else | |
4267 | { | |
4268 | original_pgrp = shell_pgrp; | |
4269 | shell_pgrp = getpid (); | |
4270 | ||
4271 | if ((original_pgrp != shell_pgrp) && (setpgid (0, shell_pgrp) < 0)) | |
4272 | { | |
3185942a | 4273 | sys_error (_("initialize_job_control: setpgid")); |
726f6388 JA |
4274 | shell_pgrp = original_pgrp; |
4275 | } | |
4276 | ||
4277 | job_control = 1; | |
ccc6cda3 JA |
4278 | |
4279 | /* If (and only if) we just set our process group to our pid, | |
4280 | thereby becoming a process group leader, and the terminal | |
4281 | is not in the same process group as our (new) process group, | |
4282 | then set the terminal's process group to our (new) process | |
4283 | group. If that fails, set our process group back to what it | |
4284 | was originally (so we can still read from the terminal) and | |
4285 | turn off job control. */ | |
4286 | if (shell_pgrp != original_pgrp && shell_pgrp != terminal_pgrp) | |
4287 | { | |
28ef6c31 | 4288 | if (give_terminal_to (shell_pgrp, 0) < 0) |
ccc6cda3 | 4289 | { |
3185942a | 4290 | t_errno = errno; |
28ef6c31 JA |
4291 | setpgid (0, original_pgrp); |
4292 | shell_pgrp = original_pgrp; | |
ac50fbac CR |
4293 | errno = t_errno; |
4294 | sys_error (_("cannot set terminal process group (%d)"), shell_pgrp); | |
28ef6c31 | 4295 | job_control = 0; |
ccc6cda3 JA |
4296 | } |
4297 | } | |
3185942a JA |
4298 | |
4299 | if (job_control && ((t = tcgetpgrp (shell_tty)) == -1 || t != shell_pgrp)) | |
4300 | { | |
4301 | if (t_errno != -1) | |
4302 | errno = t_errno; | |
4303 | sys_error (_("cannot set terminal process group (%d)"), t); | |
4304 | job_control = 0; | |
4305 | } | |
726f6388 JA |
4306 | } |
4307 | if (job_control == 0) | |
b80f6443 | 4308 | internal_error (_("no job control in this shell")); |
726f6388 JA |
4309 | } |
4310 | ||
d233b485 | 4311 | just_bail: |
a0c0a00f CR |
4312 | running_in_background = terminal_pgrp != shell_pgrp; |
4313 | ||
726f6388 JA |
4314 | if (shell_tty != fileno (stderr)) |
4315 | SET_CLOSE_ON_EXEC (shell_tty); | |
4316 | ||
ccc6cda3 | 4317 | set_signal_handler (SIGCHLD, sigchld_handler); |
726f6388 JA |
4318 | |
4319 | change_flag ('m', job_control ? '-' : '+'); | |
4320 | ||
4321 | if (interactive) | |
4322 | get_tty_state (); | |
ccc6cda3 | 4323 | |
95732b49 JA |
4324 | if (js.c_childmax < 0) |
4325 | js.c_childmax = getmaxchild (); | |
4326 | if (js.c_childmax < 0) | |
4327 | js.c_childmax = DEFAULT_CHILD_MAX; | |
4328 | ||
a0c0a00f CR |
4329 | #if 0 |
4330 | if (js.c_childmax > MAX_CHILD_MAX) | |
4331 | js.c_childmax = MAX_CHILD_MAX; | |
4332 | #endif | |
4333 | ||
726f6388 JA |
4334 | return job_control; |
4335 | } | |
4336 | ||
28ef6c31 JA |
4337 | #ifdef DEBUG |
4338 | void | |
4339 | debug_print_pgrps () | |
4340 | { | |
f73dda09 JA |
4341 | itrace("original_pgrp = %ld shell_pgrp = %ld terminal_pgrp = %ld", |
4342 | (long)original_pgrp, (long)shell_pgrp, (long)terminal_pgrp); | |
4343 | itrace("tcgetpgrp(%d) -> %ld, getpgid(0) -> %ld", | |
4344 | shell_tty, (long)tcgetpgrp (shell_tty), (long)getpgid(0)); | |
28ef6c31 JA |
4345 | } |
4346 | #endif | |
4347 | ||
726f6388 JA |
4348 | /* Set the line discipline to the best this system has to offer. |
4349 | Return -1 if this is not possible. */ | |
4350 | static int | |
4351 | set_new_line_discipline (tty) | |
4352 | int tty; | |
4353 | { | |
4354 | #if defined (NEW_TTY_DRIVER) | |
4355 | int ldisc; | |
4356 | ||
4357 | if (ioctl (tty, TIOCGETD, &ldisc) < 0) | |
4358 | return (-1); | |
4359 | ||
4360 | if (ldisc != NTTYDISC) | |
4361 | { | |
4362 | ldisc = NTTYDISC; | |
4363 | ||
4364 | if (ioctl (tty, TIOCSETD, &ldisc) < 0) | |
4365 | return (-1); | |
4366 | } | |
4367 | return (0); | |
4368 | #endif /* NEW_TTY_DRIVER */ | |
4369 | ||
4370 | #if defined (TERMIO_TTY_DRIVER) | |
ccc6cda3 | 4371 | # if defined (TERMIO_LDISC) && (NTTYDISC) |
726f6388 JA |
4372 | if (ioctl (tty, TCGETA, &shell_tty_info) < 0) |
4373 | return (-1); | |
4374 | ||
4375 | if (shell_tty_info.c_line != NTTYDISC) | |
4376 | { | |
4377 | shell_tty_info.c_line = NTTYDISC; | |
4378 | if (ioctl (tty, TCSETAW, &shell_tty_info) < 0) | |
4379 | return (-1); | |
4380 | } | |
ccc6cda3 | 4381 | # endif /* TERMIO_LDISC && NTTYDISC */ |
726f6388 JA |
4382 | return (0); |
4383 | #endif /* TERMIO_TTY_DRIVER */ | |
4384 | ||
4385 | #if defined (TERMIOS_TTY_DRIVER) | |
ccc6cda3 | 4386 | # if defined (TERMIOS_LDISC) && defined (NTTYDISC) |
726f6388 JA |
4387 | if (tcgetattr (tty, &shell_tty_info) < 0) |
4388 | return (-1); | |
4389 | ||
4390 | if (shell_tty_info.c_line != NTTYDISC) | |
4391 | { | |
4392 | shell_tty_info.c_line = NTTYDISC; | |
4393 | if (tcsetattr (tty, TCSADRAIN, &shell_tty_info) < 0) | |
4394 | return (-1); | |
4395 | } | |
ccc6cda3 | 4396 | # endif /* TERMIOS_LDISC && NTTYDISC */ |
726f6388 JA |
4397 | return (0); |
4398 | #endif /* TERMIOS_TTY_DRIVER */ | |
4399 | ||
4400 | #if !defined (NEW_TTY_DRIVER) && !defined (TERMIO_TTY_DRIVER) && !defined (TERMIOS_TTY_DRIVER) | |
4401 | return (-1); | |
4402 | #endif | |
4403 | } | |
4404 | ||
726f6388 JA |
4405 | /* Setup this shell to handle C-C, etc. */ |
4406 | void | |
4407 | initialize_job_signals () | |
4408 | { | |
4409 | if (interactive) | |
4410 | { | |
4411 | set_signal_handler (SIGINT, sigint_sighandler); | |
4412 | set_signal_handler (SIGTSTP, SIG_IGN); | |
4413 | set_signal_handler (SIGTTOU, SIG_IGN); | |
4414 | set_signal_handler (SIGTTIN, SIG_IGN); | |
726f6388 JA |
4415 | } |
4416 | else if (job_control) | |
4417 | { | |
28ef6c31 | 4418 | old_tstp = set_signal_handler (SIGTSTP, sigstop_sighandler); |
28ef6c31 | 4419 | old_ttin = set_signal_handler (SIGTTIN, sigstop_sighandler); |
7117c2d2 | 4420 | old_ttou = set_signal_handler (SIGTTOU, sigstop_sighandler); |
726f6388 | 4421 | } |
a0c0a00f | 4422 | /* Leave disposition unmodified for non-interactive shells without job |
726f6388 JA |
4423 | control. */ |
4424 | } | |
4425 | ||
4426 | /* Here we handle CONT signals. */ | |
4427 | static sighandler | |
28ef6c31 | 4428 | sigcont_sighandler (sig) |
726f6388 JA |
4429 | int sig; |
4430 | { | |
4431 | initialize_job_signals (); | |
4432 | set_signal_handler (SIGCONT, old_cont); | |
4433 | kill (getpid (), SIGCONT); | |
4434 | ||
ccc6cda3 | 4435 | SIGRETURN (0); |
726f6388 JA |
4436 | } |
4437 | ||
4438 | /* Here we handle stop signals while we are running not as a login shell. */ | |
4439 | static sighandler | |
28ef6c31 | 4440 | sigstop_sighandler (sig) |
726f6388 JA |
4441 | int sig; |
4442 | { | |
4443 | set_signal_handler (SIGTSTP, old_tstp); | |
4444 | set_signal_handler (SIGTTOU, old_ttou); | |
4445 | set_signal_handler (SIGTTIN, old_ttin); | |
4446 | ||
28ef6c31 | 4447 | old_cont = set_signal_handler (SIGCONT, sigcont_sighandler); |
726f6388 | 4448 | |
28ef6c31 | 4449 | give_terminal_to (shell_pgrp, 0); |
726f6388 JA |
4450 | |
4451 | kill (getpid (), sig); | |
4452 | ||
ccc6cda3 | 4453 | SIGRETURN (0); |
726f6388 JA |
4454 | } |
4455 | ||
4456 | /* Give the terminal to PGRP. */ | |
ccc6cda3 | 4457 | int |
28ef6c31 | 4458 | give_terminal_to (pgrp, force) |
726f6388 | 4459 | pid_t pgrp; |
28ef6c31 | 4460 | int force; |
726f6388 JA |
4461 | { |
4462 | sigset_t set, oset; | |
3185942a | 4463 | int r, e; |
726f6388 | 4464 | |
ccc6cda3 | 4465 | r = 0; |
28ef6c31 | 4466 | if (job_control || force) |
726f6388 JA |
4467 | { |
4468 | sigemptyset (&set); | |
4469 | sigaddset (&set, SIGTTOU); | |
4470 | sigaddset (&set, SIGTTIN); | |
4471 | sigaddset (&set, SIGTSTP); | |
4472 | sigaddset (&set, SIGCHLD); | |
4473 | sigemptyset (&oset); | |
4474 | sigprocmask (SIG_BLOCK, &set, &oset); | |
4475 | ||
4476 | if (tcsetpgrp (shell_tty, pgrp) < 0) | |
4477 | { | |
4478 | /* Maybe we should print an error message? */ | |
ccc6cda3 | 4479 | #if 0 |
f73dda09 JA |
4480 | sys_error ("tcsetpgrp(%d) failed: pid %ld to pgrp %ld", |
4481 | shell_tty, (long)getpid(), (long)pgrp); | |
ccc6cda3 | 4482 | #endif |
726f6388 | 4483 | r = -1; |
3185942a | 4484 | e = errno; |
726f6388 JA |
4485 | } |
4486 | else | |
4487 | terminal_pgrp = pgrp; | |
726f6388 JA |
4488 | sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); |
4489 | } | |
4490 | ||
3185942a JA |
4491 | if (r == -1) |
4492 | errno = e; | |
4493 | ||
726f6388 JA |
4494 | return r; |
4495 | } | |
4496 | ||
3185942a JA |
4497 | /* Give terminal to NPGRP iff it's currently owned by OPGRP. FLAGS are the |
4498 | flags to pass to give_terminal_to(). */ | |
4499 | static int | |
4500 | maybe_give_terminal_to (opgrp, npgrp, flags) | |
4501 | pid_t opgrp, npgrp; | |
4502 | int flags; | |
4503 | { | |
4504 | int tpgrp; | |
4505 | ||
4506 | tpgrp = tcgetpgrp (shell_tty); | |
4507 | if (tpgrp < 0 && errno == ENOTTY) | |
4508 | return -1; | |
4509 | if (tpgrp == npgrp) | |
4510 | { | |
4511 | terminal_pgrp = npgrp; | |
4512 | return 0; | |
4513 | } | |
4514 | else if (tpgrp != opgrp) | |
4515 | { | |
4516 | #if defined (DEBUG) | |
a0c0a00f | 4517 | internal_warning ("%d: maybe_give_terminal_to: terminal pgrp == %d shell pgrp = %d new pgrp = %d in_background = %d", (int)getpid(), tpgrp, opgrp, npgrp, running_in_background); |
3185942a JA |
4518 | #endif |
4519 | return -1; | |
4520 | } | |
4521 | else | |
4522 | return (give_terminal_to (npgrp, flags)); | |
4523 | } | |
4524 | ||
726f6388 JA |
4525 | /* Clear out any jobs in the job array. This is intended to be used by |
4526 | children of the shell, who should not have any job structures as baggage | |
4527 | when they start executing (forking subshells for parenthesized execution | |
cce855bc JA |
4528 | and functions with pipes are the two that spring to mind). If RUNNING_ONLY |
4529 | is nonzero, only running jobs are removed from the table. */ | |
d166f048 | 4530 | void |
cce855bc JA |
4531 | delete_all_jobs (running_only) |
4532 | int running_only; | |
726f6388 JA |
4533 | { |
4534 | register int i; | |
4535 | sigset_t set, oset; | |
4536 | ||
ccc6cda3 JA |
4537 | BLOCK_CHILD (set, oset); |
4538 | ||
95732b49 JA |
4539 | /* XXX - need to set j_lastj, j_firstj appropriately if running_only != 0. */ |
4540 | if (js.j_jobslots) | |
726f6388 | 4541 | { |
95732b49 | 4542 | js.j_current = js.j_previous = NO_JOB; |
726f6388 | 4543 | |
95732b49 JA |
4544 | /* XXX could use js.j_firstj here */ |
4545 | for (i = 0; i < js.j_jobslots; i++) | |
4546 | { | |
4547 | #if defined (DEBUG) | |
4548 | if (i < js.j_firstj && jobs[i]) | |
4549 | itrace("delete_all_jobs: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
4550 | if (i > js.j_lastj && jobs[i]) |
4551 | itrace("delete_all_jobs: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 JA |
4552 | #endif |
4553 | if (jobs[i] && (running_only == 0 || (running_only && RUNNING(i)))) | |
a0c0a00f CR |
4554 | /* We don't want to add any of these pids to bgpids. If running_only |
4555 | is non-zero, we don't want to add running jobs to the list. | |
4556 | If we are interested in all jobs, not just running jobs, and | |
4557 | we are going to clear the bgpids list below (bgp_clear()), we | |
4558 | don't need to bother. */ | |
4559 | delete_job (i, DEL_WARNSTOPPED|DEL_NOBGPID); | |
95732b49 | 4560 | } |
cce855bc JA |
4561 | if (running_only == 0) |
4562 | { | |
4563 | free ((char *)jobs); | |
95732b49 JA |
4564 | js.j_jobslots = 0; |
4565 | js.j_firstj = js.j_lastj = js.j_njobs = 0; | |
cce855bc | 4566 | } |
726f6388 | 4567 | } |
ccc6cda3 | 4568 | |
95732b49 JA |
4569 | if (running_only == 0) |
4570 | bgp_clear (); | |
4571 | ||
ccc6cda3 | 4572 | UNBLOCK_CHILD (oset); |
726f6388 JA |
4573 | } |
4574 | ||
d166f048 | 4575 | /* Mark all jobs in the job array so that they don't get a SIGHUP when the |
cce855bc | 4576 | shell gets one. If RUNNING_ONLY is nonzero, mark only running jobs. */ |
d166f048 | 4577 | void |
cce855bc JA |
4578 | nohup_all_jobs (running_only) |
4579 | int running_only; | |
d166f048 JA |
4580 | { |
4581 | register int i; | |
4582 | sigset_t set, oset; | |
4583 | ||
4584 | BLOCK_CHILD (set, oset); | |
4585 | ||
95732b49 | 4586 | if (js.j_jobslots) |
d166f048 | 4587 | { |
95732b49 JA |
4588 | /* XXX could use js.j_firstj here */ |
4589 | for (i = 0; i < js.j_jobslots; i++) | |
cce855bc | 4590 | if (jobs[i] && (running_only == 0 || (running_only && RUNNING(i)))) |
d166f048 JA |
4591 | nohup_job (i); |
4592 | } | |
4593 | ||
4594 | UNBLOCK_CHILD (oset); | |
4595 | } | |
4596 | ||
bb70624e JA |
4597 | int |
4598 | count_all_jobs () | |
4599 | { | |
4600 | int i, n; | |
4601 | sigset_t set, oset; | |
4602 | ||
95732b49 | 4603 | /* This really counts all non-dead jobs. */ |
bb70624e | 4604 | BLOCK_CHILD (set, oset); |
95732b49 JA |
4605 | /* XXX could use js.j_firstj here */ |
4606 | for (i = n = 0; i < js.j_jobslots; i++) | |
4607 | { | |
4608 | #if defined (DEBUG) | |
4609 | if (i < js.j_firstj && jobs[i]) | |
4610 | itrace("count_all_jobs: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
4611 | if (i > js.j_lastj && jobs[i]) |
4612 | itrace("count_all_jobs: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 JA |
4613 | #endif |
4614 | if (jobs[i] && DEADJOB(i) == 0) | |
4615 | n++; | |
4616 | } | |
bb70624e JA |
4617 | UNBLOCK_CHILD (oset); |
4618 | return n; | |
4619 | } | |
4620 | ||
4621 | static void | |
4622 | mark_all_jobs_as_dead () | |
4623 | { | |
4624 | register int i; | |
4625 | sigset_t set, oset; | |
4626 | ||
95732b49 | 4627 | if (js.j_jobslots == 0) |
7117c2d2 | 4628 | return; |
bb70624e | 4629 | |
7117c2d2 | 4630 | BLOCK_CHILD (set, oset); |
bb70624e | 4631 | |
95732b49 JA |
4632 | /* XXX could use js.j_firstj here */ |
4633 | for (i = 0; i < js.j_jobslots; i++) | |
7117c2d2 | 4634 | if (jobs[i]) |
95732b49 JA |
4635 | { |
4636 | jobs[i]->state = JDEAD; | |
4637 | js.j_ndead++; | |
4638 | } | |
7117c2d2 JA |
4639 | |
4640 | UNBLOCK_CHILD (oset); | |
bb70624e JA |
4641 | } |
4642 | ||
726f6388 | 4643 | /* Mark all dead jobs as notified, so delete_job () cleans them out |
b72432fd JA |
4644 | of the job table properly. POSIX.2 says we need to save the |
4645 | status of the last CHILD_MAX jobs, so we count the number of dead | |
4646 | jobs and mark only enough as notified to save CHILD_MAX statuses. */ | |
726f6388 | 4647 | static void |
b72432fd JA |
4648 | mark_dead_jobs_as_notified (force) |
4649 | int force; | |
726f6388 | 4650 | { |
95732b49 | 4651 | register int i, ndead, ndeadproc; |
726f6388 JA |
4652 | sigset_t set, oset; |
4653 | ||
95732b49 | 4654 | if (js.j_jobslots == 0) |
7117c2d2 | 4655 | return; |
b72432fd | 4656 | |
7117c2d2 | 4657 | BLOCK_CHILD (set, oset); |
b72432fd | 4658 | |
7117c2d2 JA |
4659 | /* If FORCE is non-zero, we don't have to keep CHILD_MAX statuses |
4660 | around; just run through the array. */ | |
4661 | if (force) | |
4662 | { | |
95732b49 JA |
4663 | /* XXX could use js.j_firstj here */ |
4664 | for (i = 0; i < js.j_jobslots; i++) | |
28ef6c31 | 4665 | { |
7117c2d2 JA |
4666 | if (jobs[i] && DEADJOB (i) && (interactive_shell || (find_last_pid (i, 0) != last_asynchronous_pid))) |
4667 | jobs[i]->flags |= J_NOTIFIED; | |
28ef6c31 | 4668 | } |
7117c2d2 JA |
4669 | UNBLOCK_CHILD (oset); |
4670 | return; | |
4671 | } | |
4672 | ||
95732b49 JA |
4673 | /* Mark enough dead jobs as notified to keep CHILD_MAX processes left in the |
4674 | array with the corresponding not marked as notified. This is a better | |
4675 | way to avoid pid aliasing and reuse problems than keeping the POSIX- | |
4676 | mandated CHILD_MAX jobs around. delete_job() takes care of keeping the | |
4677 | bgpids list regulated. */ | |
7117c2d2 JA |
4678 | |
4679 | /* Count the number of dead jobs */ | |
95732b49 JA |
4680 | /* XXX could use js.j_firstj here */ |
4681 | for (i = ndead = ndeadproc = 0; i < js.j_jobslots; i++) | |
7117c2d2 | 4682 | { |
95732b49 JA |
4683 | #if defined (DEBUG) |
4684 | if (i < js.j_firstj && jobs[i]) | |
4685 | itrace("mark_dead_jobs_as_notified: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
4686 | if (i > js.j_lastj && jobs[i]) |
4687 | itrace("mark_dead_jobs_as_notified: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 | 4688 | #endif |
7117c2d2 | 4689 | if (jobs[i] && DEADJOB (i)) |
95732b49 JA |
4690 | { |
4691 | ndead++; | |
4692 | ndeadproc += processes_in_job (i); | |
4693 | } | |
7117c2d2 JA |
4694 | } |
4695 | ||
95732b49 | 4696 | #ifdef DEBUG |
ac50fbac | 4697 | # if 0 |
95732b49 JA |
4698 | if (ndeadproc != js.c_reaped) |
4699 | itrace("mark_dead_jobs_as_notified: ndeadproc (%d) != js.c_reaped (%d)", ndeadproc, js.c_reaped); | |
ac50fbac | 4700 | # endif |
95732b49 JA |
4701 | if (ndead != js.j_ndead) |
4702 | itrace("mark_dead_jobs_as_notified: ndead (%d) != js.j_ndead (%d)", ndead, js.j_ndead); | |
4703 | #endif | |
4704 | ||
4705 | if (js.c_childmax < 0) | |
4706 | js.c_childmax = getmaxchild (); | |
4707 | if (js.c_childmax < 0) | |
4708 | js.c_childmax = DEFAULT_CHILD_MAX; | |
726f6388 | 4709 | |
a0c0a00f CR |
4710 | #if 0 |
4711 | if (js.c_childmax > MAX_CHILD_MAX) | |
4712 | js.c_childmax = MAX_CHILD_MAX; | |
4713 | #endif | |
4714 | ||
95732b49 JA |
4715 | /* Don't do anything if the number of dead processes is less than CHILD_MAX |
4716 | and we're not forcing a cleanup. */ | |
4717 | if (ndeadproc <= js.c_childmax) | |
7117c2d2 | 4718 | { |
726f6388 | 4719 | UNBLOCK_CHILD (oset); |
7117c2d2 | 4720 | return; |
726f6388 | 4721 | } |
7117c2d2 | 4722 | |
95732b49 JA |
4723 | #if 0 |
4724 | itrace("mark_dead_jobs_as_notified: child_max = %d ndead = %d ndeadproc = %d", js.c_childmax, ndead, ndeadproc); | |
4725 | #endif | |
4726 | ||
7117c2d2 JA |
4727 | /* Mark enough dead jobs as notified that we keep CHILD_MAX jobs in |
4728 | the list. This isn't exactly right yet; changes need to be made | |
4729 | to stop_pipeline so we don't mark the newer jobs after we've | |
95732b49 JA |
4730 | created CHILD_MAX slots in the jobs array. This needs to be |
4731 | integrated with a way to keep the jobs array from growing without | |
4732 | bound. Maybe we wrap back around to 0 after we reach some max | |
4733 | limit, and there are sufficient job slots free (keep track of total | |
4734 | size of jobs array (js.j_jobslots) and running count of number of jobs | |
4735 | in jobs array. Then keep a job index corresponding to the `oldest job' | |
4736 | and start this loop there, wrapping around as necessary. In effect, | |
4737 | we turn the list into a circular buffer. */ | |
4738 | /* XXX could use js.j_firstj here */ | |
4739 | for (i = 0; i < js.j_jobslots; i++) | |
7117c2d2 JA |
4740 | { |
4741 | if (jobs[i] && DEADJOB (i) && (interactive_shell || (find_last_pid (i, 0) != last_asynchronous_pid))) | |
4742 | { | |
95732b49 JA |
4743 | #if defined (DEBUG) |
4744 | if (i < js.j_firstj && jobs[i]) | |
4745 | itrace("mark_dead_jobs_as_notified: job %d non-null before js.j_firstj (%d)", i, js.j_firstj); | |
0628567a JA |
4746 | if (i > js.j_lastj && jobs[i]) |
4747 | itrace("mark_dead_jobs_as_notified: job %d non-null after js.j_lastj (%d)", i, js.j_lastj); | |
95732b49 JA |
4748 | #endif |
4749 | /* If marking this job as notified would drop us down below | |
4750 | child_max, don't mark it so we can keep at least child_max | |
4751 | statuses. XXX -- need to check what Posix actually says | |
4752 | about keeping statuses. */ | |
4753 | if ((ndeadproc -= processes_in_job (i)) <= js.c_childmax) | |
7117c2d2 | 4754 | break; |
95732b49 | 4755 | jobs[i]->flags |= J_NOTIFIED; |
7117c2d2 JA |
4756 | } |
4757 | } | |
4758 | ||
4759 | UNBLOCK_CHILD (oset); | |
726f6388 JA |
4760 | } |
4761 | ||
ccc6cda3 | 4762 | /* Here to allow other parts of the shell (like the trap stuff) to |
495aee44 | 4763 | freeze and unfreeze the jobs list. */ |
a0c0a00f | 4764 | int |
495aee44 CR |
4765 | freeze_jobs_list () |
4766 | { | |
a0c0a00f CR |
4767 | int o; |
4768 | ||
4769 | o = jobs_list_frozen; | |
495aee44 | 4770 | jobs_list_frozen = 1; |
a0c0a00f | 4771 | return o; |
495aee44 CR |
4772 | } |
4773 | ||
ccc6cda3 JA |
4774 | void |
4775 | unfreeze_jobs_list () | |
4776 | { | |
4777 | jobs_list_frozen = 0; | |
4778 | } | |
4779 | ||
d233b485 CR |
4780 | void |
4781 | set_jobs_list_frozen (s) | |
4782 | int s; | |
4783 | { | |
4784 | jobs_list_frozen = s; | |
4785 | } | |
4786 | ||
726f6388 JA |
4787 | /* Allow or disallow job control to take place. Returns the old value |
4788 | of job_control. */ | |
4789 | int | |
4790 | set_job_control (arg) | |
4791 | int arg; | |
4792 | { | |
4793 | int old; | |
4794 | ||
4795 | old = job_control; | |
4796 | job_control = arg; | |
b80f6443 | 4797 | |
a0c0a00f CR |
4798 | if (terminal_pgrp == NO_PID) |
4799 | terminal_pgrp = tcgetpgrp (shell_tty); | |
4800 | ||
4801 | running_in_background = (terminal_pgrp != shell_pgrp); | |
4802 | ||
4803 | #if 0 | |
4804 | if (interactive_shell == 0 && running_in_background == 0 && job_control != old) | |
4805 | { | |
4806 | if (job_control) | |
4807 | initialize_job_signals (); | |
4808 | else | |
4809 | default_tty_job_signals (); | |
4810 | } | |
4811 | #endif | |
4812 | ||
b80f6443 JA |
4813 | /* If we're turning on job control, reset pipeline_pgrp so make_child will |
4814 | put new child processes into the right pgrp */ | |
4815 | if (job_control != old && job_control) | |
4816 | pipeline_pgrp = 0; | |
4817 | ||
726f6388 JA |
4818 | return (old); |
4819 | } | |
4820 | ||
4821 | /* Turn off all traces of job control. This is run by children of the shell | |
4822 | which are going to do shellsy things, like wait (), etc. */ | |
4823 | void | |
4824 | without_job_control () | |
4825 | { | |
4826 | stop_making_children (); | |
4827 | start_pipeline (); | |
95732b49 | 4828 | #if defined (PGRP_PIPE) |
3185942a | 4829 | sh_closepipe (pgrp_pipe); |
95732b49 | 4830 | #endif |
cce855bc | 4831 | delete_all_jobs (0); |
726f6388 JA |
4832 | set_job_control (0); |
4833 | } | |
4834 | ||
4835 | /* If this shell is interactive, terminate all stopped jobs and | |
4836 | restore the original terminal process group. This is done | |
4837 | before the `exec' builtin calls shell_execve. */ | |
4838 | void | |
4839 | end_job_control () | |
4840 | { | |
d233b485 | 4841 | if (job_control) |
726f6388 JA |
4842 | { |
4843 | terminate_stopped_jobs (); | |
4844 | ||
4845 | if (original_pgrp >= 0) | |
28ef6c31 | 4846 | give_terminal_to (original_pgrp, 1); |
726f6388 JA |
4847 | } |
4848 | ||
4849 | if (original_pgrp >= 0) | |
4850 | setpgid (0, original_pgrp); | |
4851 | } | |
4852 | ||
4853 | /* Restart job control by closing shell tty and reinitializing. This is | |
4854 | called after an exec fails in an interactive shell and we do not exit. */ | |
4855 | void | |
4856 | restart_job_control () | |
4857 | { | |
4858 | if (shell_tty != -1) | |
4859 | close (shell_tty); | |
d166f048 | 4860 | initialize_job_control (0); |
726f6388 JA |
4861 | } |
4862 | ||
ac50fbac CR |
4863 | void |
4864 | set_maxchild (nchild) | |
4865 | int nchild; | |
4866 | { | |
4867 | static int lmaxchild = -1; | |
4868 | ||
4869 | if (lmaxchild < 0) | |
4870 | lmaxchild = getmaxchild (); | |
4871 | if (lmaxchild < 0) | |
4872 | lmaxchild = DEFAULT_CHILD_MAX; | |
4873 | ||
4874 | /* Clamp value we set. Minimum is what Posix requires, maximum is defined | |
4875 | above as MAX_CHILD_MAX. */ | |
4876 | if (nchild < lmaxchild) | |
4877 | nchild = lmaxchild; | |
4878 | else if (nchild > MAX_CHILD_MAX) | |
4879 | nchild = MAX_CHILD_MAX; | |
4880 | ||
4881 | js.c_childmax = nchild; | |
4882 | } | |
4883 | ||
726f6388 JA |
4884 | /* Set the handler to run when the shell receives a SIGCHLD signal. */ |
4885 | void | |
4886 | set_sigchld_handler () | |
4887 | { | |
ccc6cda3 | 4888 | set_signal_handler (SIGCHLD, sigchld_handler); |
726f6388 JA |
4889 | } |
4890 | ||
4891 | #if defined (PGRP_PIPE) | |
4892 | /* Read from the read end of a pipe. This is how the process group leader | |
4893 | blocks until all of the processes in a pipeline have been made. */ | |
4894 | static void | |
4895 | pipe_read (pp) | |
4896 | int *pp; | |
4897 | { | |
4898 | char ch; | |
4899 | ||
4900 | if (pp[1] >= 0) | |
4901 | { | |
4902 | close (pp[1]); | |
4903 | pp[1] = -1; | |
4904 | } | |
4905 | ||
4906 | if (pp[0] >= 0) | |
4907 | { | |
4908 | while (read (pp[0], &ch, 1) == -1 && errno == EINTR) | |
bb70624e | 4909 | ; |
726f6388 JA |
4910 | } |
4911 | } | |
4912 | ||
726f6388 | 4913 | /* Functional interface closes our local-to-job-control pipes. */ |
ccc6cda3 | 4914 | void |
726f6388 JA |
4915 | close_pgrp_pipe () |
4916 | { | |
3185942a | 4917 | sh_closepipe (pgrp_pipe); |
726f6388 JA |
4918 | } |
4919 | ||
89a92869 CR |
4920 | void |
4921 | save_pgrp_pipe (p, clear) | |
4922 | int *p; | |
4923 | int clear; | |
4924 | { | |
4925 | p[0] = pgrp_pipe[0]; | |
4926 | p[1] = pgrp_pipe[1]; | |
4927 | if (clear) | |
4928 | pgrp_pipe[0] = pgrp_pipe[1] = -1; | |
4929 | } | |
4930 | ||
4931 | void | |
4932 | restore_pgrp_pipe (p) | |
4933 | int *p; | |
4934 | { | |
4935 | pgrp_pipe[0] = p[0]; | |
4936 | pgrp_pipe[1] = p[1]; | |
4937 | } | |
4938 | ||
726f6388 | 4939 | #endif /* PGRP_PIPE */ |