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