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