]> git.ipfire.org Git - thirdparty/bash.git/blob - nojobs.c
commit bash-20161230 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 #include "builtins/common.h"
53
54 #define DEFAULT_CHILD_MAX 32
55
56 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
57 # define killpg(pg, sig) kill(-(pg),(sig))
58 #endif /* USG || _POSIX_VERSION */
59
60 #if !defined (HAVE_SIGINTERRUPT) && !defined (HAVE_POSIX_SIGNALS)
61 # define siginterrupt(sig, code)
62 #endif /* !HAVE_SIGINTERRUPT && !HAVE_POSIX_SIGNALS */
63
64 #if defined (HAVE_WAITPID)
65 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
66 #else
67 # define WAITPID(pid, statusp, options) wait (statusp)
68 #endif /* !HAVE_WAITPID */
69
70 /* Return the fd from which we are actually getting input. */
71 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
72
73 #if !defined (errno)
74 extern int errno;
75 #endif /* !errno */
76
77 #if defined (HAVE_POSIX_SIGNALS)
78 extern sigset_t top_level_mask;
79 #endif
80
81 extern void set_original_signal __P((int, SigHandler *));
82
83 volatile pid_t last_made_pid = NO_PID;
84 volatile pid_t last_asynchronous_pid = NO_PID;
85
86 static int queue_sigchld, waiting_for_child; /* dummy declarations */
87
88 /* Call this when you start making children. */
89 int already_making_children = 0;
90
91 /* The controlling tty for this shell. */
92 int shell_tty = -1;
93
94 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
95 exits from get_tty_state(). */
96 int check_window_size = CHECKWINSIZE_DEFAULT;
97
98 /* We don't have job control. */
99 int job_control = 0;
100
101 int running_in_background = 0; /* can't tell without job control */
102
103 /* STATUS and FLAGS are only valid if pid != NO_PID
104 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
105 struct proc_status {
106 pid_t pid;
107 int status; /* Exit status of PID or 128 + fatal signal number */
108 int flags;
109 };
110
111 /* Values for proc_status.flags */
112 #define PROC_RUNNING 0x01
113 #define PROC_NOTIFIED 0x02
114 #define PROC_ASYNC 0x04
115 #define PROC_SIGNALED 0x10
116
117 /* Return values from find_status_by_pid */
118 #define PROC_BAD -1
119 #define PROC_STILL_ALIVE -2
120
121 static struct proc_status *pid_list = (struct proc_status *)NULL;
122 static int pid_list_size;
123 static int wait_sigint_received;
124
125 static long child_max = -1L;
126
127 static void alloc_pid_list __P((void));
128 static int find_proc_slot __P((pid_t));
129 static int find_index_by_pid __P((pid_t));
130 static int find_status_by_pid __P((pid_t));
131 static int process_exit_status __P((WAIT));
132 static int find_termsig_by_pid __P((pid_t));
133 static int get_termsig __P((WAIT));
134 static void set_pid_status __P((pid_t, WAIT));
135 static void set_pid_flags __P((pid_t, int));
136 static void unset_pid_flags __P((pid_t, int));
137 static int get_pid_flags __P((pid_t));
138 static void add_pid __P((pid_t, int));
139 static void mark_dead_jobs_as_notified __P((int));
140
141 static sighandler wait_sigint_handler __P((int));
142 static char *j_strsignal __P((int));
143
144 #if defined (HAVE_WAITPID)
145 static void reap_zombie_children __P((void));
146 #endif
147
148 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
149 static int siginterrupt __P((int, int));
150 #endif
151
152 static void restore_sigint_handler __P((void));
153
154 /* Allocate new, or grow existing PID_LIST. */
155 static void
156 alloc_pid_list ()
157 {
158 register int i;
159 int old = pid_list_size;
160
161 pid_list_size += 10;
162 pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
163
164 /* None of the newly allocated slots have process id's yet. */
165 for (i = old; i < pid_list_size; i++)
166 {
167 pid_list[i].pid = NO_PID;
168 pid_list[i].status = pid_list[i].flags = 0;
169 }
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].pid != NO_PID &&
399 (pid_list[i].flags & PROC_RUNNING) == 0 &&
400 (pid_list[i].flags & PROC_NOTIFIED))
401 pid_list[i].pid = NO_PID;
402 }
403
404 #if defined (COPROCESS_SUPPORT)
405 coproc_reap ();
406 #endif
407
408 return 0;
409 }
410
411 void
412 reap_dead_jobs ()
413 {
414 mark_dead_jobs_as_notified (0);
415 cleanup_dead_jobs ();
416 }
417
418 /* Initialize the job control mechanism, and set up the tty stuff. */
419 int
420 initialize_job_control (force)
421 int force;
422 {
423 shell_tty = fileno (stderr);
424
425 if (interactive)
426 get_tty_state ();
427 return 0;
428 }
429
430 /* Setup this shell to handle C-C, etc. */
431 void
432 initialize_job_signals ()
433 {
434 set_signal_handler (SIGINT, sigint_sighandler);
435
436 /* If this is a login shell we don't wish to be disturbed by
437 stop signals. */
438 if (login_shell)
439 ignore_tty_job_signals ();
440 }
441
442 #if defined (HAVE_WAITPID)
443 /* Collect the status of all zombie children so that their system
444 resources can be deallocated. */
445 static void
446 reap_zombie_children ()
447 {
448 # if defined (WNOHANG)
449 pid_t pid;
450 WAIT status;
451
452 CHECK_TERMSIG;
453 CHECK_WAIT_INTR;
454 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
455 set_pid_status (pid, status);
456 # endif /* WNOHANG */
457 CHECK_TERMSIG;
458 CHECK_WAIT_INTR;
459 }
460 #endif /* WAITPID */
461
462 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
463
464 #if !defined (SA_RESTART)
465 # define SA_RESTART 0
466 #endif
467
468 static int
469 siginterrupt (sig, flag)
470 int sig, flag;
471 {
472 struct sigaction act;
473
474 sigaction (sig, (struct sigaction *)NULL, &act);
475
476 if (flag)
477 act.sa_flags &= ~SA_RESTART;
478 else
479 act.sa_flags |= SA_RESTART;
480
481 return (sigaction (sig, &act, (struct sigaction *)NULL));
482 }
483 #endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS */
484
485 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
486 COMMAND is just for remembering the name of the command; we don't do
487 anything else with it. ASYNC_P says what to do with the tty. If
488 non-zero, then don't give it away. */
489 pid_t
490 make_child (command, async_p)
491 char *command;
492 int async_p;
493 {
494 pid_t pid;
495 int forksleep;
496
497 /* Discard saved memory. */
498 if (command)
499 free (command);
500
501 start_pipeline ();
502
503 #if defined (BUFFERED_INPUT)
504 /* If default_buffered_input is active, we are reading a script. If
505 the command is asynchronous, we have already duplicated /dev/null
506 as fd 0, but have not changed the buffered stream corresponding to
507 the old fd 0. We don't want to sync the stream in this case. */
508 if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
509 sync_buffered_stream (default_buffered_input);
510 #endif /* BUFFERED_INPUT */
511
512 /* XXX - block SIGTERM here and unblock in child after fork resets the
513 set of pending signals? */
514 RESET_SIGTERM;
515
516 /* Create the child, handle severe errors. Retry on EAGAIN. */
517 forksleep = 1;
518 while ((pid = fork ()) < 0 && errno == EAGAIN && forksleep < FORKSLEEP_MAX)
519 {
520 sys_error ("fork: retry");
521 RESET_SIGTERM;
522
523 #if defined (HAVE_WAITPID)
524 /* Posix systems with a non-blocking waitpid () system call available
525 get another chance after zombies are reaped. */
526 reap_zombie_children ();
527 if (forksleep > 1 && sleep (forksleep) != 0)
528 break;
529 #else
530 if (sleep (forksleep) != 0)
531 break;
532 #endif /* HAVE_WAITPID */
533 forksleep <<= 1;
534 }
535
536 if (pid != 0)
537 RESET_SIGTERM;
538
539 if (pid < 0)
540 {
541 sys_error ("fork");
542 last_command_exit_value = EX_NOEXEC;
543 throw_to_top_level ();
544 }
545
546 if (pid == 0)
547 {
548 #if defined (BUFFERED_INPUT)
549 unset_bash_input (0);
550 #endif /* BUFFERED_INPUT */
551
552 CLRINTERRUPT; /* XXX - children have their own interrupt state */
553
554 #if defined (HAVE_POSIX_SIGNALS)
555 /* Restore top-level signal mask. */
556 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
557 #endif
558
559 #if 0
560 /* Ignore INT and QUIT in asynchronous children. */
561 if (async_p)
562 last_asynchronous_pid = getpid ();
563 #endif
564
565 default_tty_job_signals ();
566 }
567 else
568 {
569 /* In the parent. */
570
571 last_made_pid = pid;
572
573 if (async_p)
574 last_asynchronous_pid = pid;
575
576 add_pid (pid, async_p);
577 }
578 return (pid);
579 }
580
581 void
582 ignore_tty_job_signals ()
583 {
584 #if defined (SIGTSTP)
585 set_signal_handler (SIGTSTP, SIG_IGN);
586 set_signal_handler (SIGTTIN, SIG_IGN);
587 set_signal_handler (SIGTTOU, SIG_IGN);
588 #endif
589 }
590
591 void
592 default_tty_job_signals ()
593 {
594 #if defined (SIGTSTP)
595 if (signal_is_trapped (SIGTSTP) == 0 && signal_is_hard_ignored (SIGTSTP))
596 set_signal_handler (SIGTSTP, SIG_IGN);
597 else
598 set_signal_handler (SIGTSTP, SIG_DFL);
599 if (signal_is_trapped (SIGTTIN) == 0 && signal_is_hard_ignored (SIGTTIN))
600 set_signal_handler (SIGTTIN, SIG_IGN);
601 else
602 set_signal_handler (SIGTTIN, SIG_DFL);
603 if (signal_is_trapped (SIGTTOU) == 0 && signal_is_hard_ignored (SIGTTOU))
604 set_signal_handler (SIGTTOU, SIG_IGN);
605 else
606 set_signal_handler (SIGTTOU, SIG_DFL);
607 #endif
608 }
609
610 /* Called once in a parent process. */
611 void
612 get_original_tty_job_signals ()
613 {
614 static int fetched = 0;
615
616 if (fetched == 0)
617 {
618 #if defined (SIGTSTP)
619 if (interactive_shell)
620 {
621 set_original_signal (SIGTSTP, SIG_DFL);
622 set_original_signal (SIGTTIN, SIG_DFL);
623 set_original_signal (SIGTTOU, SIG_DFL);
624 }
625 else
626 {
627 get_original_signal (SIGTSTP);
628 get_original_signal (SIGTTIN);
629 get_original_signal (SIGTTOU);
630 }
631 #endif
632 fetched = 1;
633 }
634 }
635
636 /* Wait for a single pid (PID) and return its exit status. Called by
637 the wait builtin. */
638 int
639 wait_for_single_pid (pid, flags)
640 pid_t pid;
641 int flags;
642 {
643 pid_t got_pid;
644 WAIT status;
645 int pstatus;
646
647 pstatus = find_status_by_pid (pid);
648
649 if (pstatus == PROC_BAD)
650 {
651 internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid);
652 return (127);
653 }
654
655 if (pstatus != PROC_STILL_ALIVE)
656 {
657 if (pstatus > 128)
658 last_command_exit_signal = find_termsig_by_pid (pid);
659 return (pstatus);
660 }
661
662 siginterrupt (SIGINT, 1);
663 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
664 {
665 CHECK_TERMSIG;
666 CHECK_WAIT_INTR;
667 if (got_pid < 0)
668 {
669 if (errno != EINTR && errno != ECHILD)
670 {
671 siginterrupt (SIGINT, 0);
672 sys_error ("wait");
673 }
674 break;
675 }
676 else if (got_pid > 0)
677 set_pid_status (got_pid, status);
678 }
679
680 if (got_pid > 0)
681 {
682 set_pid_status (got_pid, status);
683 set_pid_flags (got_pid, PROC_NOTIFIED);
684 }
685
686 siginterrupt (SIGINT, 0);
687 QUIT;
688
689 return (got_pid > 0 ? process_exit_status (status) : -1);
690 }
691
692 /* Wait for all of the shell's children to exit. Called by the `wait'
693 builtin. */
694 void
695 wait_for_background_pids ()
696 {
697 pid_t got_pid;
698 WAIT status;
699
700 /* If we aren't using job control, we let the kernel take care of the
701 bookkeeping for us. wait () will return -1 and set errno to ECHILD
702 when there are no more unwaited-for child processes on both
703 4.2 BSD-based and System V-based systems. */
704
705 siginterrupt (SIGINT, 1);
706
707 /* Wait for ECHILD */
708 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
709 set_pid_status (got_pid, status);
710
711 if (errno != EINTR && errno != ECHILD)
712 {
713 siginterrupt (SIGINT, 0);
714 sys_error("wait");
715 }
716
717 siginterrupt (SIGINT, 0);
718 QUIT;
719
720 mark_dead_jobs_as_notified (1);
721 cleanup_dead_jobs ();
722 }
723
724 void
725 wait_sigint_cleanup ()
726 {
727 }
728
729 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
730 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
731 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
732
733 static void
734 restore_sigint_handler ()
735 {
736 if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
737 {
738 set_signal_handler (SIGINT, old_sigint_handler);
739 old_sigint_handler = INVALID_SIGNAL_HANDLER;
740 }
741 }
742
743 /* Handle SIGINT while we are waiting for children in a script to exit.
744 All interrupts are effectively ignored by the shell, but allowed to
745 kill a running job. */
746 static sighandler
747 wait_sigint_handler (sig)
748 int sig;
749 {
750 SigHandler *sigint_handler;
751
752 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
753 what POSIX.2 says (see builtins/wait.def for more info). */
754 if (this_shell_builtin && this_shell_builtin == wait_builtin &&
755 signal_is_trapped (SIGINT) &&
756 ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
757 {
758 last_command_exit_value = 128+SIGINT;
759 restore_sigint_handler ();
760 interrupt_immediately = 0;
761 trap_handler (SIGINT); /* set pending_traps[SIGINT] */
762 wait_signal_received = SIGINT;
763 SIGRETURN (0);
764 }
765
766 if (interrupt_immediately)
767 {
768 last_command_exit_value = EXECUTION_FAILURE;
769 restore_sigint_handler ();
770 ADDINTERRUPT;
771 QUIT;
772 }
773
774 wait_sigint_received = 1;
775
776 SIGRETURN (0);
777 }
778
779 static char *
780 j_strsignal (s)
781 int s;
782 {
783 static char retcode_name_buffer[64] = { '\0' };
784 char *x;
785
786 x = strsignal (s);
787 if (x == 0)
788 {
789 x = retcode_name_buffer;
790 sprintf (x, "Signal %d", s);
791 }
792 return x;
793 }
794
795 /* Wait for pid (one of our children) to terminate. This is called only
796 by the execution code in execute_cmd.c. */
797 int
798 wait_for (pid)
799 pid_t pid;
800 {
801 int return_val, pstatus;
802 pid_t got_pid;
803 WAIT status;
804
805 pstatus = find_status_by_pid (pid);
806
807 if (pstatus == PROC_BAD)
808 return (0);
809
810 if (pstatus != PROC_STILL_ALIVE)
811 {
812 if (pstatus > 128)
813 last_command_exit_signal = find_termsig_by_pid (pid);
814 return (pstatus);
815 }
816
817 /* If we are running a script, ignore SIGINT while we're waiting for
818 a child to exit. The loop below does some of this, but not all. */
819 wait_sigint_received = 0;
820 if (interactive_shell == 0)
821 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
822
823 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
824 {
825 CHECK_TERMSIG;
826 CHECK_WAIT_INTR;
827 if (got_pid < 0 && errno == ECHILD)
828 {
829 #if !defined (_POSIX_VERSION)
830 status.w_termsig = status.w_retcode = 0;
831 #else
832 status = 0;
833 #endif /* _POSIX_VERSION */
834 break;
835 }
836 else if (got_pid < 0 && errno != EINTR)
837 programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno));
838 else if (got_pid > 0)
839 set_pid_status (got_pid, status);
840 }
841
842 if (got_pid > 0)
843 set_pid_status (got_pid, status);
844
845 #if defined (HAVE_WAITPID)
846 if (got_pid >= 0)
847 reap_zombie_children ();
848 #endif /* HAVE_WAITPID */
849
850 CHECK_TERMSIG;
851 CHECK_WAIT_INTR;
852
853 if (interactive_shell == 0)
854 {
855 SigHandler *temp_handler;
856
857 temp_handler = old_sigint_handler;
858 restore_sigint_handler ();
859
860 /* If the job exited because of SIGINT, make sure the shell acts as if
861 it had received one also. */
862 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
863 {
864
865 if (maybe_call_trap_handler (SIGINT) == 0)
866 {
867 if (temp_handler == SIG_DFL)
868 termsig_handler (SIGINT);
869 else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN)
870 (*temp_handler) (SIGINT);
871 }
872 }
873 }
874
875 /* Default return value. */
876 /* ``a full 8 bits of status is returned'' */
877 return_val = process_exit_status (status);
878 last_command_exit_signal = get_termsig (status);
879
880 #if defined (DONT_REPORT_SIGPIPE) && defined (DONT_REPORT_SIGTERM)
881 # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE && (x) != SIGTERM)
882 #elif !defined (DONT_REPORT_SIGPIPE) && !defined (DONT_REPORT_SIGTERM)
883 # define REPORTSIG(x) ((x) != SIGINT)
884 #elif defined (DONT_REPORT_SIGPIPE)
885 # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE)
886 #else
887 # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGTERM)
888 #endif
889
890 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) && REPORTSIG(WTERMSIG (status)))
891 {
892 fprintf (stderr, "%s", j_strsignal (WTERMSIG (status)));
893 if (WIFCORED (status))
894 fprintf (stderr, _(" (core dumped)"));
895 fprintf (stderr, "\n");
896 }
897
898 if (interactive_shell && subshell_environment == 0)
899 {
900 if (WIFSIGNALED (status) || WIFSTOPPED (status))
901 set_tty_state ();
902 else
903 get_tty_state ();
904 }
905 else if (interactive_shell == 0 && subshell_environment == 0 && check_window_size)
906 get_new_window_size (0, (int *)0, (int *)0);
907
908 return (return_val);
909 }
910
911 /* Send PID SIGNAL. Returns -1 on failure, 0 on success. If GROUP is non-zero,
912 or PID is less than -1, then kill the process group associated with PID. */
913 int
914 kill_pid (pid, signal, group)
915 pid_t pid;
916 int signal, group;
917 {
918 int result;
919
920 if (pid < -1)
921 {
922 pid = -pid;
923 group = 1;
924 }
925 result = group ? killpg (pid, signal) : kill (pid, signal);
926 return (result);
927 }
928
929 static TTYSTRUCT shell_tty_info;
930 static int got_tty_state;
931
932 /* Fill the contents of shell_tty_info with the current tty info. */
933 int
934 get_tty_state ()
935 {
936 int tty;
937
938 tty = input_tty ();
939 if (tty != -1)
940 {
941 ttgetattr (tty, &shell_tty_info);
942 got_tty_state = 1;
943 if (check_window_size)
944 get_new_window_size (0, (int *)0, (int *)0);
945 }
946 return 0;
947 }
948
949 /* Make the current tty use the state in shell_tty_info. */
950 int
951 set_tty_state ()
952 {
953 int tty;
954
955 tty = input_tty ();
956 if (tty != -1)
957 {
958 if (got_tty_state == 0)
959 return 0;
960 ttsetattr (tty, &shell_tty_info);
961 }
962 return 0;
963 }
964
965 /* Give the terminal to PGRP. */
966 int
967 give_terminal_to (pgrp, force)
968 pid_t pgrp;
969 int force;
970 {
971 return 0;
972 }
973
974 /* Stop a pipeline. */
975 int
976 stop_pipeline (async, ignore)
977 int async;
978 COMMAND *ignore;
979 {
980 already_making_children = 0;
981 return 0;
982 }
983
984 void
985 start_pipeline ()
986 {
987 already_making_children = 1;
988 }
989
990 void
991 stop_making_children ()
992 {
993 already_making_children = 0;
994 }
995
996 /* The name is kind of a misnomer, but it's what the job control code uses. */
997 void
998 without_job_control ()
999 {
1000 stop_making_children ();
1001 last_made_pid = NO_PID; /* XXX */
1002 }
1003
1004 int
1005 get_job_by_pid (pid, block)
1006 pid_t pid;
1007 int block;
1008 {
1009 int i;
1010
1011 i = find_index_by_pid (pid);
1012 return ((i == NO_PID) ? PROC_BAD : i);
1013 }
1014
1015 /* Print descriptive information about the job with leader pid PID. */
1016 void
1017 describe_pid (pid)
1018 pid_t pid;
1019 {
1020 fprintf (stderr, "%ld\n", (long) pid);
1021 }
1022
1023 int
1024 freeze_jobs_list ()
1025 {
1026 return 0;
1027 }
1028
1029 void
1030 unfreeze_jobs_list ()
1031 {
1032 }
1033
1034 int
1035 count_all_jobs ()
1036 {
1037 return 0;
1038 }