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