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