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