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