]> git.ipfire.org Git - thirdparty/bash.git/blob - nojobs.c
commit bash-20161108 snapshot
[thirdparty/bash.git] / nojobs.c
1 /* nojobs.c - functions that make children, remember them, and handle their termination. */
2
3 /* This file works under BSD, System V, minix, and Posix systems. It does
4 not implement job control. */
5
6 /* Copyright (C) 1987-2011 Free Software Foundation, Inc.
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
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 Bash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "config.h"
25
26 #include "bashtypes.h"
27 #include "filecntl.h"
28
29 #if defined (HAVE_UNISTD_H)
30 # include <unistd.h>
31 #endif
32
33 #include <stdio.h>
34 #include <signal.h>
35 #include <errno.h>
36
37 #if defined (BUFFERED_INPUT)
38 # include "input.h"
39 #endif
40
41 /* Need to include this up here for *_TTY_DRIVER definitions. */
42 #include "shtty.h"
43
44 #include "bashintl.h"
45
46 #include "shell.h"
47 #include "jobs.h"
48 #include "execute_cmd.h"
49 #include "trap.h"
50
51 #include "builtins/builtext.h" /* for wait_builtin */
52
53 #define DEFAULT_CHILD_MAX 32
54
55 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
56 # define killpg(pg, sig) kill(-(pg),(sig))
57 #endif /* USG || _POSIX_VERSION */
58
59 #if !defined (HAVE_SIGINTERRUPT) && !defined (HAVE_POSIX_SIGNALS)
60 # define siginterrupt(sig, code)
61 #endif /* !HAVE_SIGINTERRUPT && !HAVE_POSIX_SIGNALS */
62
63 #if defined (HAVE_WAITPID)
64 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
65 #else
66 # define WAITPID(pid, statusp, options) wait (statusp)
67 #endif /* !HAVE_WAITPID */
68
69 /* Return the fd from which we are actually getting input. */
70 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
71
72 #if !defined (errno)
73 extern int errno;
74 #endif /* !errno */
75
76 extern int interactive, interactive_shell, login_shell;
77 extern int subshell_environment;
78 extern int last_command_exit_value, last_command_exit_signal;
79 extern int interrupt_immediately;
80 extern sh_builtin_func_t *this_shell_builtin;
81 #if defined (HAVE_POSIX_SIGNALS)
82 extern sigset_t top_level_mask;
83 #endif
84 extern procenv_t wait_intr_buf;
85 extern int wait_intr_flag;
86 extern int wait_signal_received;
87
88 extern void set_original_signal __P((int, SigHandler *));
89
90 volatile pid_t last_made_pid = NO_PID;
91 volatile pid_t last_asynchronous_pid = NO_PID;
92
93 static int queue_sigchld, waiting_for_child; /* dummy declarations */
94
95 /* Call this when you start making children. */
96 int already_making_children = 0;
97
98 /* The controlling tty for this shell. */
99 int shell_tty = -1;
100
101 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
102 exits from get_tty_state(). */
103 int check_window_size = CHECKWINSIZE_DEFAULT;
104
105 /* We don't have job control. */
106 int job_control = 0;
107
108 int running_in_background = 0; /* can't tell without job control */
109
110 /* STATUS and FLAGS are only valid if pid != NO_PID
111 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
112 struct proc_status {
113 pid_t pid;
114 int status; /* Exit status of PID or 128 + fatal signal number */
115 int flags;
116 };
117
118 /* Values for proc_status.flags */
119 #define PROC_RUNNING 0x01
120 #define PROC_NOTIFIED 0x02
121 #define PROC_ASYNC 0x04
122 #define PROC_SIGNALED 0x10
123
124 /* Return values from find_status_by_pid */
125 #define PROC_BAD -1
126 #define PROC_STILL_ALIVE -2
127
128 static struct proc_status *pid_list = (struct proc_status *)NULL;
129 static int pid_list_size;
130 static int wait_sigint_received;
131
132 static long child_max = -1L;
133
134 static void alloc_pid_list __P((void));
135 static int find_proc_slot __P((pid_t));
136 static int find_index_by_pid __P((pid_t));
137 static int find_status_by_pid __P((pid_t));
138 static int process_exit_status __P((WAIT));
139 static int find_termsig_by_pid __P((pid_t));
140 static int get_termsig __P((WAIT));
141 static void set_pid_status __P((pid_t, WAIT));
142 static void set_pid_flags __P((pid_t, int));
143 static void unset_pid_flags __P((pid_t, int));
144 static int get_pid_flags __P((pid_t));
145 static void add_pid __P((pid_t, int));
146 static void mark_dead_jobs_as_notified __P((int));
147
148 static sighandler wait_sigint_handler __P((int));
149 static char *j_strsignal __P((int));
150
151 #if defined (HAVE_WAITPID)
152 static void reap_zombie_children __P((void));
153 #endif
154
155 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
156 static int siginterrupt __P((int, int));
157 #endif
158
159 static void restore_sigint_handler __P((void));
160
161 /* Allocate new, or grow existing PID_LIST. */
162 static void
163 alloc_pid_list ()
164 {
165 register int i;
166 int old = pid_list_size;
167
168 pid_list_size += 10;
169 pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
170
171 /* None of the newly allocated slots have process id's yet. */
172 for (i = old; i < pid_list_size; i++)
173 {
174 pid_list[i].pid = NO_PID;
175 pid_list[i].status = pid_list[i].flags = 0;
176 }
177 }
178
179 /* Return the offset within the PID_LIST array of an empty slot. This can
180 create new slots if all of the existing slots are taken. */
181 static int
182 find_proc_slot (pid)
183 pid_t pid;
184 {
185 register int i;
186
187 for (i = 0; i < pid_list_size; i++)
188 if (pid_list[i].pid == NO_PID || pid_list[i].pid == pid)
189 return (i);
190
191 if (i == pid_list_size)
192 alloc_pid_list ();
193
194 return (i);
195 }
196
197 /* Return the offset within the PID_LIST array of a slot containing PID,
198 or the value NO_PID if the pid wasn't found. */
199 static int
200 find_index_by_pid (pid)
201 pid_t pid;
202 {
203 register int i;
204
205 for (i = 0; i < pid_list_size; i++)
206 if (pid_list[i].pid == pid)
207 return (i);
208
209 return (NO_PID);
210 }
211
212 /* Return the status of PID as looked up in the PID_LIST array. A
213 return value of PROC_BAD indicates that PID wasn't found. */
214 static int
215 find_status_by_pid (pid)
216 pid_t pid;
217 {
218 int i;
219
220 i = find_index_by_pid (pid);
221 if (i == NO_PID)
222 return (PROC_BAD);
223 if (pid_list[i].flags & PROC_RUNNING)
224 return (PROC_STILL_ALIVE);
225 return (pid_list[i].status);
226 }
227
228 static int
229 process_exit_status (status)
230 WAIT status;
231 {
232 if (WIFSIGNALED (status))
233 return (128 + WTERMSIG (status));
234 else
235 return (WEXITSTATUS (status));
236 }
237
238 /* Return the status of PID as looked up in the PID_LIST array. A
239 return value of PROC_BAD indicates that PID wasn't found. */
240 static int
241 find_termsig_by_pid (pid)
242 pid_t pid;
243 {
244 int i;
245
246 i = find_index_by_pid (pid);
247 if (i == NO_PID)
248 return (0);
249 if (pid_list[i].flags & PROC_RUNNING)
250 return (0);
251 return (get_termsig ((WAIT)pid_list[i].status));
252 }
253
254 /* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS. If STATUS is -1, look
255 up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately
256 depending on its flags and exit status. */
257 static int
258 get_termsig (status)
259 WAIT status;
260 {
261 if (WIFSTOPPED (status) == 0 && WIFSIGNALED (status))
262 return (WTERMSIG (status));
263 else
264 return (0);
265 }
266
267 /* Give PID the status value STATUS in the PID_LIST array. */
268 static void
269 set_pid_status (pid, status)
270 pid_t pid;
271 WAIT status;
272 {
273 int slot;
274
275 #if defined (COPROCESS_SUPPORT)
276 coproc_pidchk (pid, status);
277 #endif
278
279 slot = find_index_by_pid (pid);
280 if (slot == NO_PID)
281 return;
282
283 pid_list[slot].status = process_exit_status (status);
284 pid_list[slot].flags &= ~PROC_RUNNING;
285 if (WIFSIGNALED (status))
286 pid_list[slot].flags |= PROC_SIGNALED;
287 /* If it's not a background process, mark it as notified so it gets
288 cleaned up. */
289 if ((pid_list[slot].flags & PROC_ASYNC) == 0)
290 pid_list[slot].flags |= PROC_NOTIFIED;
291 }
292
293 /* Give PID the flags FLAGS in the PID_LIST array. */
294 static void
295 set_pid_flags (pid, flags)
296 pid_t pid;
297 int flags;
298 {
299 int slot;
300
301 slot = find_index_by_pid (pid);
302 if (slot == NO_PID)
303 return;
304
305 pid_list[slot].flags |= flags;
306 }
307
308 /* Unset FLAGS for PID in the pid list */
309 static void
310 unset_pid_flags (pid, flags)
311 pid_t pid;
312 int flags;
313 {
314 int slot;
315
316 slot = find_index_by_pid (pid);
317 if (slot == NO_PID)
318 return;
319
320 pid_list[slot].flags &= ~flags;
321 }
322
323 /* Return the flags corresponding to PID in the PID_LIST array. */
324 static int
325 get_pid_flags (pid)
326 pid_t pid;
327 {
328 int slot;
329
330 slot = find_index_by_pid (pid);
331 if (slot == NO_PID)
332 return 0;
333
334 return (pid_list[slot].flags);
335 }
336
337 static void
338 add_pid (pid, async)
339 pid_t pid;
340 int async;
341 {
342 int slot;
343
344 slot = find_proc_slot (pid);
345
346 pid_list[slot].pid = pid;
347 pid_list[slot].status = -1;
348 pid_list[slot].flags = PROC_RUNNING;
349 if (async)
350 pid_list[slot].flags |= PROC_ASYNC;
351 }
352
353 static void
354 mark_dead_jobs_as_notified (force)
355 int force;
356 {
357 register int i, ndead;
358
359 /* first, count the number of non-running async jobs if FORCE == 0 */
360 for (i = ndead = 0; force == 0 && i < pid_list_size; i++)
361 {
362 if (pid_list[i].pid == NO_PID)
363 continue;
364 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
365 (pid_list[i].flags & PROC_ASYNC))
366 ndead++;
367 }
368
369 if (child_max < 0)
370 child_max = getmaxchild ();
371 if (child_max < 0)
372 child_max = DEFAULT_CHILD_MAX;
373
374 if (force == 0 && ndead <= child_max)
375 return;
376
377 /* If FORCE == 0, we just mark as many non-running async jobs as notified
378 to bring us under the CHILD_MAX limit. */
379 for (i = 0; i < pid_list_size; i++)
380 {
381 if (pid_list[i].pid == NO_PID)
382 continue;
383 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
384 pid_list[i].pid != last_asynchronous_pid)
385 {
386 pid_list[i].flags |= PROC_NOTIFIED;
387 if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= child_max)
388 break;
389 }
390 }
391 }
392
393 /* Remove all dead, notified jobs from the pid_list. */
394 int
395 cleanup_dead_jobs ()
396 {
397 register int i;
398
399 #if defined (HAVE_WAITPID)
400 reap_zombie_children ();
401 #endif
402
403 for (i = 0; i < pid_list_size; i++)
404 {
405 if (pid_list[i].pid != NO_PID &&
406 (pid_list[i].flags & PROC_RUNNING) == 0 &&
407 (pid_list[i].flags & PROC_NOTIFIED))
408 pid_list[i].pid = NO_PID;
409 }
410
411 #if defined (COPROCESS_SUPPORT)
412 coproc_reap ();
413 #endif
414
415 return 0;
416 }
417
418 void
419 reap_dead_jobs ()
420 {
421 mark_dead_jobs_as_notified (0);
422 cleanup_dead_jobs ();
423 }
424
425 /* Initialize the job control mechanism, and set up the tty stuff. */
426 int
427 initialize_job_control (force)
428 int force;
429 {
430 shell_tty = fileno (stderr);
431
432 if (interactive)
433 get_tty_state ();
434 return 0;
435 }
436
437 /* Setup this shell to handle C-C, etc. */
438 void
439 initialize_job_signals ()
440 {
441 set_signal_handler (SIGINT, sigint_sighandler);
442
443 /* If this is a login shell we don't wish to be disturbed by
444 stop signals. */
445 if (login_shell)
446 ignore_tty_job_signals ();
447 }
448
449 #if defined (HAVE_WAITPID)
450 /* Collect the status of all zombie children so that their system
451 resources can be deallocated. */
452 static void
453 reap_zombie_children ()
454 {
455 # if defined (WNOHANG)
456 pid_t pid;
457 WAIT status;
458
459 CHECK_TERMSIG;
460 CHECK_WAIT_INTR;
461 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
462 set_pid_status (pid, status);
463 # endif /* WNOHANG */
464 CHECK_TERMSIG;
465 CHECK_WAIT_INTR;
466 }
467 #endif /* WAITPID */
468
469 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
470
471 #if !defined (SA_RESTART)
472 # define SA_RESTART 0
473 #endif
474
475 static int
476 siginterrupt (sig, flag)
477 int sig, flag;
478 {
479 struct sigaction act;
480
481 sigaction (sig, (struct sigaction *)NULL, &act);
482
483 if (flag)
484 act.sa_flags &= ~SA_RESTART;
485 else
486 act.sa_flags |= SA_RESTART;
487
488 return (sigaction (sig, &act, (struct sigaction *)NULL));
489 }
490 #endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS */
491
492 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
493 COMMAND is just for remembering the name of the command; we don't do
494 anything else with it. ASYNC_P says what to do with the tty. If
495 non-zero, then don't give it away. */
496 pid_t
497 make_child (command, async_p)
498 char *command;
499 int async_p;
500 {
501 pid_t pid;
502 int forksleep;
503
504 /* Discard saved memory. */
505 if (command)
506 free (command);
507
508 start_pipeline ();
509
510 #if defined (BUFFERED_INPUT)
511 /* If default_buffered_input is active, we are reading a script. If
512 the command is asynchronous, we have already duplicated /dev/null
513 as fd 0, but have not changed the buffered stream corresponding to
514 the old fd 0. We don't want to sync the stream in this case. */
515 if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
516 sync_buffered_stream (default_buffered_input);
517 #endif /* BUFFERED_INPUT */
518
519 /* XXX - block SIGTERM here and unblock in child after fork resets the
520 set of pending signals? */
521 RESET_SIGTERM;
522
523 /* Create the child, handle severe errors. Retry on EAGAIN. */
524 forksleep = 1;
525 while ((pid = fork ()) < 0 && errno == EAGAIN && forksleep < FORKSLEEP_MAX)
526 {
527 sys_error ("fork: retry");
528 RESET_SIGTERM;
529
530 #if defined (HAVE_WAITPID)
531 /* Posix systems with a non-blocking waitpid () system call available
532 get another chance after zombies are reaped. */
533 reap_zombie_children ();
534 if (forksleep > 1 && sleep (forksleep) != 0)
535 break;
536 #else
537 if (sleep (forksleep) != 0)
538 break;
539 #endif /* HAVE_WAITPID */
540 forksleep <<= 1;
541 }
542
543 if (pid != 0)
544 RESET_SIGTERM;
545
546 if (pid < 0)
547 {
548 sys_error ("fork");
549 last_command_exit_value = EX_NOEXEC;
550 throw_to_top_level ();
551 }
552
553 if (pid == 0)
554 {
555 #if defined (BUFFERED_INPUT)
556 unset_bash_input (0);
557 #endif /* BUFFERED_INPUT */
558
559 CLRINTERRUPT; /* XXX - children have their own interrupt state */
560
561 #if defined (HAVE_POSIX_SIGNALS)
562 /* Restore top-level signal mask. */
563 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
564 #endif
565
566 #if 0
567 /* Ignore INT and QUIT in asynchronous children. */
568 if (async_p)
569 last_asynchronous_pid = getpid ();
570 #endif
571
572 default_tty_job_signals ();
573 }
574 else
575 {
576 /* In the parent. */
577
578 last_made_pid = pid;
579
580 if (async_p)
581 last_asynchronous_pid = pid;
582
583 add_pid (pid, async_p);
584 }
585 return (pid);
586 }
587
588 void
589 ignore_tty_job_signals ()
590 {
591 #if defined (SIGTSTP)
592 set_signal_handler (SIGTSTP, SIG_IGN);
593 set_signal_handler (SIGTTIN, SIG_IGN);
594 set_signal_handler (SIGTTOU, SIG_IGN);
595 #endif
596 }
597
598 void
599 default_tty_job_signals ()
600 {
601 #if defined (SIGTSTP)
602 if (signal_is_trapped (SIGTSTP) == 0 && signal_is_hard_ignored (SIGTSTP))
603 set_signal_handler (SIGTSTP, SIG_IGN);
604 else
605 set_signal_handler (SIGTSTP, SIG_DFL);
606 if (signal_is_trapped (SIGTTIN) == 0 && signal_is_hard_ignored (SIGTTIN))
607 set_signal_handler (SIGTTIN, SIG_IGN);
608 else
609 set_signal_handler (SIGTTIN, SIG_DFL);
610 if (signal_is_trapped (SIGTTOU) == 0 && signal_is_hard_ignored (SIGTTOU))
611 set_signal_handler (SIGTTOU, SIG_IGN);
612 else
613 set_signal_handler (SIGTTOU, SIG_DFL);
614 #endif
615 }
616
617 /* Called once in a parent process. */
618 void
619 get_original_tty_job_signals ()
620 {
621 static int fetched = 0;
622
623 if (fetched == 0)
624 {
625 #if defined (SIGTSTP)
626 if (interactive_shell)
627 {
628 set_original_signal (SIGTSTP, SIG_DFL);
629 set_original_signal (SIGTTIN, SIG_DFL);
630 set_original_signal (SIGTTOU, SIG_DFL);
631 }
632 else
633 {
634 get_original_signal (SIGTSTP);
635 get_original_signal (SIGTTIN);
636 get_original_signal (SIGTTOU);
637 }
638 #endif
639 fetched = 1;
640 }
641 }
642
643 /* Wait for a single pid (PID) and return its exit status. Called by
644 the wait builtin. */
645 int
646 wait_for_single_pid (pid, flags)
647 pid_t pid;
648 int flags;
649 {
650 pid_t got_pid;
651 WAIT status;
652 int pstatus;
653
654 pstatus = find_status_by_pid (pid);
655
656 if (pstatus == PROC_BAD)
657 {
658 internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid);
659 return (127);
660 }
661
662 if (pstatus != PROC_STILL_ALIVE)
663 {
664 if (pstatus > 128)
665 last_command_exit_signal = find_termsig_by_pid (pid);
666 return (pstatus);
667 }
668
669 siginterrupt (SIGINT, 1);
670 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
671 {
672 CHECK_TERMSIG;
673 CHECK_WAIT_INTR;
674 if (got_pid < 0)
675 {
676 if (errno != EINTR && errno != ECHILD)
677 {
678 siginterrupt (SIGINT, 0);
679 sys_error ("wait");
680 }
681 break;
682 }
683 else if (got_pid > 0)
684 set_pid_status (got_pid, status);
685 }
686
687 if (got_pid > 0)
688 {
689 set_pid_status (got_pid, status);
690 set_pid_flags (got_pid, PROC_NOTIFIED);
691 }
692
693 siginterrupt (SIGINT, 0);
694 QUIT;
695
696 return (got_pid > 0 ? process_exit_status (status) : -1);
697 }
698
699 /* Wait for all of the shell's children to exit. Called by the `wait'
700 builtin. */
701 void
702 wait_for_background_pids ()
703 {
704 pid_t got_pid;
705 WAIT status;
706
707 /* If we aren't using job control, we let the kernel take care of the
708 bookkeeping for us. wait () will return -1 and set errno to ECHILD
709 when there are no more unwaited-for child processes on both
710 4.2 BSD-based and System V-based systems. */
711
712 siginterrupt (SIGINT, 1);
713
714 /* Wait for ECHILD */
715 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
716 set_pid_status (got_pid, status);
717
718 if (errno != EINTR && errno != ECHILD)
719 {
720 siginterrupt (SIGINT, 0);
721 sys_error("wait");
722 }
723
724 siginterrupt (SIGINT, 0);
725 QUIT;
726
727 mark_dead_jobs_as_notified (1);
728 cleanup_dead_jobs ();
729 }
730
731 void
732 wait_sigint_cleanup ()
733 {
734 }
735
736 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
737 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
738 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
739
740 static void
741 restore_sigint_handler ()
742 {
743 if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
744 {
745 set_signal_handler (SIGINT, old_sigint_handler);
746 old_sigint_handler = INVALID_SIGNAL_HANDLER;
747 }
748 }
749
750 /* Handle SIGINT while we are waiting for children in a script to exit.
751 All interrupts are effectively ignored by the shell, but allowed to
752 kill a running job. */
753 static sighandler
754 wait_sigint_handler (sig)
755 int sig;
756 {
757 SigHandler *sigint_handler;
758
759 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
760 what POSIX.2 says (see builtins/wait.def for more info). */
761 if (this_shell_builtin && this_shell_builtin == wait_builtin &&
762 signal_is_trapped (SIGINT) &&
763 ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
764 {
765 last_command_exit_value = 128+SIGINT;
766 restore_sigint_handler ();
767 interrupt_immediately = 0;
768 trap_handler (SIGINT); /* set pending_traps[SIGINT] */
769 wait_signal_received = SIGINT;
770 SIGRETURN (0);
771 }
772
773 if (interrupt_immediately)
774 {
775 last_command_exit_value = EXECUTION_FAILURE;
776 restore_sigint_handler ();
777 ADDINTERRUPT;
778 QUIT;
779 }
780
781 wait_sigint_received = 1;
782
783 SIGRETURN (0);
784 }
785
786 static char *
787 j_strsignal (s)
788 int s;
789 {
790 static char retcode_name_buffer[64] = { '\0' };
791 char *x;
792
793 x = strsignal (s);
794 if (x == 0)
795 {
796 x = retcode_name_buffer;
797 sprintf (x, "Signal %d", s);
798 }
799 return x;
800 }
801
802 /* Wait for pid (one of our children) to terminate. This is called only
803 by the execution code in execute_cmd.c. */
804 int
805 wait_for (pid)
806 pid_t pid;
807 {
808 int return_val, pstatus;
809 pid_t got_pid;
810 WAIT status;
811
812 pstatus = find_status_by_pid (pid);
813
814 if (pstatus == PROC_BAD)
815 return (0);
816
817 if (pstatus != PROC_STILL_ALIVE)
818 {
819 if (pstatus > 128)
820 last_command_exit_signal = find_termsig_by_pid (pid);
821 return (pstatus);
822 }
823
824 /* If we are running a script, ignore SIGINT while we're waiting for
825 a child to exit. The loop below does some of this, but not all. */
826 wait_sigint_received = 0;
827 if (interactive_shell == 0)
828 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
829
830 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
831 {
832 CHECK_TERMSIG;
833 CHECK_WAIT_INTR;
834 if (got_pid < 0 && errno == ECHILD)
835 {
836 #if !defined (_POSIX_VERSION)
837 status.w_termsig = status.w_retcode = 0;
838 #else
839 status = 0;
840 #endif /* _POSIX_VERSION */
841 break;
842 }
843 else if (got_pid < 0 && errno != EINTR)
844 programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno));
845 else if (got_pid > 0)
846 set_pid_status (got_pid, status);
847 }
848
849 if (got_pid > 0)
850 set_pid_status (got_pid, status);
851
852 #if defined (HAVE_WAITPID)
853 if (got_pid >= 0)
854 reap_zombie_children ();
855 #endif /* HAVE_WAITPID */
856
857 CHECK_TERMSIG;
858 CHECK_WAIT_INTR;
859
860 if (interactive_shell == 0)
861 {
862 SigHandler *temp_handler;
863
864 temp_handler = old_sigint_handler;
865 restore_sigint_handler ();
866
867 /* If the job exited because of SIGINT, make sure the shell acts as if
868 it had received one also. */
869 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
870 {
871
872 if (maybe_call_trap_handler (SIGINT) == 0)
873 {
874 if (temp_handler == SIG_DFL)
875 termsig_handler (SIGINT);
876 else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN)
877 (*temp_handler) (SIGINT);
878 }
879 }
880 }
881
882 /* Default return value. */
883 /* ``a full 8 bits of status is returned'' */
884 return_val = process_exit_status (status);
885 last_command_exit_signal = get_termsig (status);
886
887 #if defined (DONT_REPORT_SIGPIPE) && defined (DONT_REPORT_SIGTERM)
888 # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE && (x) != SIGTERM)
889 #elif !defined (DONT_REPORT_SIGPIPE) && !defined (DONT_REPORT_SIGTERM)
890 # define REPORTSIG(x) ((x) != SIGINT)
891 #elif defined (DONT_REPORT_SIGPIPE)
892 # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE)
893 #else
894 # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGTERM)
895 #endif
896
897 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) && REPORTSIG(WTERMSIG (status)))
898 {
899 fprintf (stderr, "%s", j_strsignal (WTERMSIG (status)));
900 if (WIFCORED (status))
901 fprintf (stderr, _(" (core dumped)"));
902 fprintf (stderr, "\n");
903 }
904
905 if (interactive_shell && subshell_environment == 0)
906 {
907 if (WIFSIGNALED (status) || WIFSTOPPED (status))
908 set_tty_state ();
909 else
910 get_tty_state ();
911 }
912 else if (interactive_shell == 0 && subshell_environment == 0 && check_window_size)
913 get_new_window_size (0, (int *)0, (int *)0);
914
915 return (return_val);
916 }
917
918 /* Send PID SIGNAL. Returns -1 on failure, 0 on success. If GROUP is non-zero,
919 or PID is less than -1, then kill the process group associated with PID. */
920 int
921 kill_pid (pid, signal, group)
922 pid_t pid;
923 int signal, group;
924 {
925 int result;
926
927 if (pid < -1)
928 {
929 pid = -pid;
930 group = 1;
931 }
932 result = group ? killpg (pid, signal) : kill (pid, signal);
933 return (result);
934 }
935
936 static TTYSTRUCT shell_tty_info;
937 static int got_tty_state;
938
939 /* Fill the contents of shell_tty_info with the current tty info. */
940 int
941 get_tty_state ()
942 {
943 int tty;
944
945 tty = input_tty ();
946 if (tty != -1)
947 {
948 ttgetattr (tty, &shell_tty_info);
949 got_tty_state = 1;
950 if (check_window_size)
951 get_new_window_size (0, (int *)0, (int *)0);
952 }
953 return 0;
954 }
955
956 /* Make the current tty use the state in shell_tty_info. */
957 int
958 set_tty_state ()
959 {
960 int tty;
961
962 tty = input_tty ();
963 if (tty != -1)
964 {
965 if (got_tty_state == 0)
966 return 0;
967 ttsetattr (tty, &shell_tty_info);
968 }
969 return 0;
970 }
971
972 /* Give the terminal to PGRP. */
973 int
974 give_terminal_to (pgrp, force)
975 pid_t pgrp;
976 int force;
977 {
978 return 0;
979 }
980
981 /* Stop a pipeline. */
982 int
983 stop_pipeline (async, ignore)
984 int async;
985 COMMAND *ignore;
986 {
987 already_making_children = 0;
988 return 0;
989 }
990
991 void
992 start_pipeline ()
993 {
994 already_making_children = 1;
995 }
996
997 void
998 stop_making_children ()
999 {
1000 already_making_children = 0;
1001 }
1002
1003 /* The name is kind of a misnomer, but it's what the job control code uses. */
1004 void
1005 without_job_control ()
1006 {
1007 stop_making_children ();
1008 last_made_pid = NO_PID; /* XXX */
1009 }
1010
1011 int
1012 get_job_by_pid (pid, block)
1013 pid_t pid;
1014 int block;
1015 {
1016 int i;
1017
1018 i = find_index_by_pid (pid);
1019 return ((i == NO_PID) ? PROC_BAD : i);
1020 }
1021
1022 /* Print descriptive information about the job with leader pid PID. */
1023 void
1024 describe_pid (pid)
1025 pid_t pid;
1026 {
1027 fprintf (stderr, "%ld\n", (long) pid);
1028 }
1029
1030 int
1031 freeze_jobs_list ()
1032 {
1033 return 0;
1034 }
1035
1036 void
1037 unfreeze_jobs_list ()
1038 {
1039 }
1040
1041 int
1042 count_all_jobs ()
1043 {
1044 return 0;
1045 }