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