e1116845aa153715f7bb617cb6a5c5a698e31a3f
[platform/upstream/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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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 #include "bashjmp.h"
38
39 #include "command.h"
40 #include "general.h"
41 #include "jobs.h"
42 #include "externs.h"
43 #include "sig.h"
44 #include "error.h"
45 #include "bashtty.h"
46
47 #if defined (BUFFERED_INPUT)
48 #  include "input.h"
49 #endif
50
51 #if defined (TERMIOS_TTY_DRIVER)
52 #  include <termios.h>
53 #else
54 #  if defined (TERMIO_TTY_DRIVER)
55 #    include <termio.h>
56 #  else
57 #    include <sgtty.h>
58 #  endif /* !TERMIO_TTY_DRIVER */
59 #endif /* TERMIOS_TTY_DRIVER */
60
61 #if !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
62 /* For struct winsize on SCO */
63 /*   sys/ptem.h has winsize but needs mblk_t from sys/stream.h */
64 #  if defined (HAVE_SYS_PTEM_H) && defined (TIOCGWINSZ) && defined (SIGWINCH)
65 #    if defined (HAVE_SYS_STREAM_H)
66 #      include <sys/stream.h>
67 #    endif
68 #    include <sys/ptem.h>
69 #  endif /* HAVE_SYS_PTEM_H && TIOCGWINSZ && SIGWINCH */
70 #endif /* !STRUCT_WINSIZE_IN_SYS_IOCTL */
71
72 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
73 #  define killpg(pg, sig)               kill(-(pg),(sig))
74 #endif /* USG || _POSIX_VERSION */
75
76 #if !defined (HAVE_SIGINTERRUPT)
77 #  define siginterrupt(sig, code)
78 #endif /* USG */
79
80 #if defined (HAVE_WAITPID)
81 #  define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
82 #else
83 #  define WAITPID(pid, statusp, options) wait (statusp)
84 #endif /* !HAVE_WAITPID */
85
86 /* Return the fd from which we are actually getting input. */
87 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
88
89 #if !defined (errno)
90 extern int errno;
91 #endif /* !errno */
92
93 #if defined (READLINE)
94 extern void _rl_set_screen_size (); 
95 #endif    
96
97 extern int interactive, interactive_shell, login_shell;
98 extern int subshell_environment;
99 extern int last_command_exit_value;
100 #if defined (HAVE_POSIX_SIGNALS)
101 extern sigset_t top_level_mask;
102 #endif
103
104 pid_t last_made_pid = NO_PID;
105 pid_t last_asynchronous_pid = NO_PID;
106
107 /* Call this when you start making children. */
108 int already_making_children = 0;
109
110 /* The controlling tty for this shell. */
111 int shell_tty = -1;
112
113 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
114    exits from get_tty_state(). */
115 int check_window_size;
116
117 #if defined (HAVE_WAITPID)
118 static void reap_zombie_children ();
119 #endif
120
121 struct proc_status {
122   pid_t pid;
123   int status;   /* Exit status of PID or 128 + fatal signal number */
124 };
125
126 static struct proc_status *pid_list = (struct proc_status *)NULL;
127 static int pid_list_size;
128
129 #define PROC_BAD -1
130 #define PROC_STILL_ALIVE -2
131
132 /* Allocate new, or grow existing PID_LIST. */
133 static void
134 alloc_pid_list ()
135 {
136   register int i;
137   int old = pid_list_size;
138
139   pid_list_size += 10;
140   pid_list = (struct proc_status *)
141     xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
142
143   /* None of the newly allocated slots have process id's yet. */
144   for (i = old; i < pid_list_size; i++)
145     pid_list[i].pid = NO_PID;
146 }
147
148 /* Return the offset within the PID_LIST array of an empty slot.  This can
149    create new slots if all of the existing slots are taken. */
150 static int
151 find_proc_slot ()
152 {
153   register int i;
154
155   for (i = 0; i < pid_list_size; i++)
156     if (pid_list[i].pid == NO_PID)
157       return (i);
158
159   if (i == pid_list_size)
160     alloc_pid_list ();
161
162   return (i);
163 }
164
165 /* Return the offset within the PID_LIST array of a slot containing PID,
166    or the value NO_PID if the pid wasn't found. */
167 static int
168 find_index_by_pid (pid)
169      pid_t pid;
170 {
171   register int i;
172
173   for (i = 0; i < pid_list_size; i++)
174     if (pid_list[i].pid == pid)
175       return (i);
176
177   return (NO_PID);
178 }
179
180 /* Return the status of PID as looked up in the PID_LIST array.  A
181    return value of PROC_BAD indicates that PID wasn't found. */
182 static int
183 find_status_by_pid (pid)
184      pid_t pid;
185 {
186   int i;
187
188   i = find_index_by_pid (pid);
189   if (i == NO_PID)
190     return (PROC_BAD);
191   return (pid_list[i].status);
192 }
193
194 /* Give PID the status value STATUS in the PID_LIST array. */
195 static void
196 set_pid_status (pid, status)
197      pid_t pid;
198      WAIT status;
199 {
200   int slot;
201
202   slot = find_index_by_pid (pid);
203   if (slot == NO_PID)
204     return;
205
206   if (WIFSIGNALED (status))
207     pid_list[slot].status = 128 + WTERMSIG (status);
208   else
209     pid_list[slot].status = WEXITSTATUS (status);
210 }
211
212 static void
213 add_pid (pid)
214      pid_t pid;
215 {
216   int slot;
217
218   slot = find_proc_slot ();
219   pid_list[slot].pid = pid;
220   pid_list[slot].status = PROC_STILL_ALIVE;
221 }
222
223 int
224 cleanup_dead_jobs ()
225 {
226   register int i;
227
228 #if defined (HAVE_WAITPID)
229   reap_zombie_children ();
230 #endif
231
232   for (i = 0; i < pid_list_size; i++)
233     if (pid_list[i].status != PROC_STILL_ALIVE)
234       pid_list[i].pid = NO_PID;
235 }
236
237 /* Initialize the job control mechanism, and set up the tty stuff. */
238 initialize_job_control (force)
239      int force;
240 {
241   shell_tty = fileno (stderr);
242
243   if (interactive)
244     get_tty_state ();
245 }
246
247 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
248 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
249
250 static void
251 get_new_window_size (from_sig)
252      int from_sig;
253 {
254   struct winsize win;
255   int tty;
256
257   tty = input_tty ();
258   if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) &&
259       win.ws_row > 0 && win.ws_col > 0)
260     {
261 #if defined (aixpc)
262       shell_tty_info.c_winsize = win;   /* structure copying */
263 #endif
264       set_lines_and_columns (win.ws_row, win.ws_col);
265 #if defined (READLINE)
266       _rl_set_screen_size (win.ws_row, win.ws_col);
267 #endif
268     }
269 }
270
271 static sighandler
272 sigwinch_sighandler (sig)
273      int sig;
274 {
275 #if defined (MUST_REINSTALL_SIGHANDLERS)
276   set_signal_handler (SIGWINCH, sigwinch_sighandler);
277 #endif /* MUST_REINSTALL_SIGHANDLERS */
278   get_new_window_size (1);
279 }
280 #else
281 static void
282 get_new_window_size (from_sig)
283      int from_sig;
284 {
285 }
286 #endif /* TIOCGWINSZ && SIGWINCH */
287
288 void
289 set_sigwinch_handler ()
290 {
291 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
292   old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
293 #endif
294 }
295
296 void
297 unset_sigwinch_handler ()
298 {
299 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
300   set_signal_handler (SIGWINCH, old_winch);
301 #endif
302 }
303
304 /* Setup this shell to handle C-C, etc. */
305 void
306 initialize_job_signals ()
307 {
308   set_signal_handler (SIGINT, sigint_sighandler);
309   set_sigwinch_handler ();
310
311   /* If this is a login shell we don't wish to be disturbed by
312      stop signals. */
313   if (login_shell)
314     ignore_tty_job_signals ();
315 }
316
317 #if defined (HAVE_WAITPID)
318 /* Collect the status of all zombie children so that their system
319    resources can be deallocated. */
320 static void
321 reap_zombie_children ()
322 {
323 #if defined (WNOHANG)
324   pid_t pid;
325   WAIT status;
326
327   while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
328     set_pid_status (pid, status);
329 #endif
330 }
331 #endif /* WAITPID && WNOHANG */
332
333 /* Fork, handling errors.  Returns the pid of the newly made child, or 0.
334    COMMAND is just for remembering the name of the command; we don't do
335    anything else with it.  ASYNC_P says what to do with the tty.  If
336    non-zero, then don't give it away. */
337 pid_t
338 make_child (command, async_p)
339      char *command;
340      int async_p;
341 {
342   pid_t pid;
343 #if defined (HAVE_WAITPID)
344   int retry = 1;
345 #endif /* HAVE_WAITPID */
346
347   /* Discard saved memory. */
348   if (command)
349     free (command);
350
351   start_pipeline ();
352
353 #if defined (BUFFERED_INPUT)
354   /* If default_buffered_input is active, we are reading a script.  If
355      the command is asynchronous, we have already duplicated /dev/null
356      as fd 0, but have not changed the buffered stream corresponding to
357      the old fd 0.  We don't want to sync the stream in this case. */
358   if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
359     sync_buffered_stream (default_buffered_input);
360 #endif /* BUFFERED_INPUT */
361
362   /* Create the child, handle severe errors. */
363 #if defined (HAVE_WAITPID)
364   retry_fork:
365 #endif /* HAVE_WAITPID */
366
367   if ((pid = fork ()) < 0)
368     {
369 #if defined (HAVE_WAITPID)
370       /* Posix systems with a non-blocking waitpid () system call available
371          get another chance after zombies are reaped. */
372       if (errno == EAGAIN && retry)
373         {
374           reap_zombie_children ();
375           retry = 0;
376           goto retry_fork;
377         }
378 #endif /* HAVE_WAITPID */
379
380       sys_error ("fork");
381
382       throw_to_top_level ();
383     }
384
385   if (pid == 0)
386     {
387 #if defined (BUFFERED_INPUT)
388       unset_bash_input (0);
389 #endif /* BUFFERED_INPUT */
390
391 #if defined (HAVE_POSIX_SIGNALS)
392       /* Restore top-level signal mask. */
393       sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
394 #endif
395
396       /* Ignore INT and QUIT in asynchronous children. */
397       if (async_p)
398         last_asynchronous_pid = getpid ();
399
400       default_tty_job_signals ();
401     }
402   else
403     {
404       /* In the parent. */
405
406       last_made_pid = pid;
407
408       if (async_p)
409         last_asynchronous_pid = pid;
410
411       add_pid (pid);
412     }
413   return (pid);
414 }
415
416 void
417 ignore_tty_job_signals ()
418 {
419 #if defined (SIGTSTP)
420   set_signal_handler (SIGTSTP, SIG_IGN);
421   set_signal_handler (SIGTTIN, SIG_IGN);
422   set_signal_handler (SIGTTOU, SIG_IGN);
423 #endif
424 }
425
426 void
427 default_tty_job_signals ()
428 {
429 #if defined (SIGTSTP)
430   set_signal_handler (SIGTSTP, SIG_DFL);
431   set_signal_handler (SIGTTIN, SIG_DFL);
432   set_signal_handler (SIGTTOU, SIG_DFL);
433 #endif
434 }
435
436 /* Wait for a single pid (PID) and return its exit status. */
437 wait_for_single_pid (pid)
438      pid_t pid;
439 {
440   pid_t got_pid;
441   WAIT status;
442   int pstatus;
443
444   pstatus = find_status_by_pid (pid);
445
446   if (pstatus == PROC_BAD)
447     {
448       internal_error ("wait: pid %d is not a child of this shell", pid);
449       return (127);
450     }
451
452   if (pstatus != PROC_STILL_ALIVE)
453     return (pstatus);
454
455   siginterrupt (SIGINT, 1);
456   while ((got_pid = WAITPID (pid, &status, 0)) != pid)
457     {
458       if (got_pid < 0)
459         {
460           if (errno != EINTR && errno != ECHILD)
461             {
462               siginterrupt (SIGINT, 0);
463               sys_error ("wait");
464             }
465           break;
466         }
467       else if (got_pid > 0)
468         set_pid_status (got_pid, status);
469     }
470
471   set_pid_status (got_pid, status);
472   siginterrupt (SIGINT, 0);
473   QUIT;
474
475   if (WIFSIGNALED (status))
476     return (128 + WTERMSIG (status));
477   else
478     return (WEXITSTATUS (status));
479 }
480
481 /* Wait for all of the shell's children to exit. */
482 void
483 wait_for_background_pids ()
484 {
485   pid_t got_pid;
486   WAIT status;
487
488   /* If we aren't using job control, we let the kernel take care of the
489      bookkeeping for us.  wait () will return -1 and set errno to ECHILD
490      when there are no more unwaited-for child processes on both
491      4.2 BSD-based and System V-based systems. */
492
493   siginterrupt (SIGINT, 1);
494
495   /* Wait for ECHILD */
496   while ((got_pid = WAITPID (-1, &status, 0)) != -1)
497     set_pid_status (got_pid, status);
498
499   if (errno != EINTR && errno != ECHILD)
500     {
501       siginterrupt (SIGINT, 0);
502       sys_error("wait");
503     }
504
505   siginterrupt (SIGINT, 0);
506   QUIT;
507 }
508
509 /* Handle SIGINT while we are waiting for children in a script to exit.
510    All interrupts are effectively ignored by the shell, but allowed to
511    kill a running job. */
512 static sighandler
513 wait_sigint_handler (sig)
514      int sig;
515 {
516 #if 0
517   /* Run a trap handler if one has been defined. */
518   maybe_call_trap_handler (sig);
519 #endif
520
521   SIGRETURN (0);
522 }
523
524 /* Wait for pid (one of our children) to terminate.  This is called only
525    by the execution code in execute_cmd.c. */
526 int
527 wait_for (pid)
528      pid_t pid;
529 {
530   int return_val, pstatus;
531   pid_t got_pid;
532   WAIT status;
533   SigHandler *old_sigint_handler;
534
535   pstatus = find_status_by_pid (pid);
536
537   if (pstatus == PROC_BAD)
538     return (0);
539
540   if (pstatus != PROC_STILL_ALIVE)
541     return (pstatus);
542
543   /* If we are running a script, ignore SIGINT while we're waiting for
544      a child to exit.  The loop below does some of this, but not all. */
545   if (!interactive_shell)
546     old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
547
548   while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
549     {
550       if (got_pid < 0 && errno == ECHILD)
551         {
552 #if !defined (_POSIX_VERSION)
553           status.w_termsig = status.w_retcode = 0;
554 #else
555           status = 0;
556 #endif /* _POSIX_VERSION */
557           break;
558         }
559       else if (got_pid < 0 && errno != EINTR)
560         programming_error ("wait_for(%d): %s", pid, strerror(errno));
561       else if (got_pid > 0)
562         set_pid_status (got_pid, status);
563     }
564
565   set_pid_status (got_pid, status);
566
567 #if defined (HAVE_WAITPID)
568   if (got_pid >= 0)
569     reap_zombie_children ();
570 #endif /* HAVE_WAITPID */
571
572   if (interactive_shell == 0)
573     {
574       set_signal_handler (SIGINT, old_sigint_handler);
575       /* If the job exited because of SIGINT, make sure the shell acts as if
576          it had received one also. */
577       if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
578         {
579           if (maybe_call_trap_handler (SIGINT) == 0)
580             (*old_sigint_handler) (SIGINT);
581         }
582     }
583
584   /* Default return value. */
585   /* ``a full 8 bits of status is returned'' */
586   if (WIFSIGNALED (status))
587     return_val = 128 + WTERMSIG (status);
588   else
589     return_val = WEXITSTATUS (status);
590
591 #if !defined (DONT_REPORT_SIGPIPE)
592   if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
593         (WTERMSIG (status) != SIGINT))
594 #else
595   if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
596         (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
597 #endif
598     {
599       fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
600       if (WIFCORED (status))
601         fprintf (stderr, " (core dumped)");
602       fprintf (stderr, "\n");
603     }
604
605   if (interactive_shell && subshell_environment == 0)
606     {
607       if (WIFSIGNALED (status) || WIFSTOPPED (status))
608         set_tty_state ();
609       else
610         get_tty_state ();
611     }
612
613   return (return_val);
614 }
615
616 /* Give PID SIGNAL.  This determines what job the pid belongs to (if any).
617    If PID does belong to a job, and the job is stopped, then CONTinue the
618    job after giving it SIGNAL.  Returns -1 on failure.  If GROUP is non-null,
619    then kill the process group associated with PID. */
620 int
621 kill_pid (pid, signal, group)
622      pid_t pid;
623      int signal, group;
624 {
625   int result;
626
627   if (group)
628     result = killpg (pid, signal);
629   else
630     result = kill (pid, signal);
631
632   return (result);
633 }
634
635 #if defined (TERMIOS_TTY_DRIVER)
636 static struct termios shell_tty_info;
637 #else /* !TERMIOS_TTY_DRIVER */
638 #  if defined (TERMIO_TTY_DRIVER)
639 static struct termio shell_tty_info;
640 #  else
641 static struct sgttyb shell_tty_info;
642 #  endif /* !TERMIO_TTY_DRIVER */
643 #endif /* !TERMIOS_TTY_DRIVER */
644
645 static int got_tty_state;
646
647 /* Fill the contents of shell_tty_info with the current tty info. */
648 get_tty_state ()
649 {
650   int tty;
651
652   tty = input_tty ();
653   if (tty != -1)
654     {
655 #if defined (TERMIOS_TTY_DRIVER)
656       tcgetattr (tty, &shell_tty_info);
657 #else
658 #  if defined (TERMIO_TTY_DRIVER)
659       ioctl (tty, TCGETA, &shell_tty_info);
660 #  else
661       ioctl (tty, TIOCGETP, &shell_tty_info);
662 #  endif
663 #endif
664       got_tty_state = 1;
665       if (check_window_size)
666         get_new_window_size (0);
667     }
668 }
669
670 /* Make the current tty use the state in shell_tty_info. */
671 int
672 set_tty_state ()
673 {
674   int tty;
675
676   tty = input_tty ();
677   if (tty != -1)
678     {
679       if (got_tty_state == 0)
680         return 0;
681
682 #if defined (TERMIOS_TTY_DRIVER)
683       tcsetattr (tty, TCSADRAIN, &shell_tty_info);
684 #else
685 #  if defined (TERMIO_TTY_DRIVER)
686       ioctl (tty, TCSETAW, &shell_tty_info);  /* Wait for output, no flush */
687 #  else
688       ioctl (tty, TIOCSETN, &shell_tty_info);
689 #  endif
690 #endif
691     }
692   return 0;
693 }
694
695 /* Give the terminal to PGRP.  */
696 give_terminal_to (pgrp)
697      pid_t pgrp;
698 {
699 }
700
701 /* Stop a pipeline. */
702 stop_pipeline (async, ignore)
703      int async;
704      COMMAND *ignore;
705 {
706   already_making_children = 0;
707 }
708
709 void
710 start_pipeline ()
711 {
712   already_making_children = 1;
713 }
714
715 int
716 get_job_by_pid (pid, block)
717      pid_t pid;
718      int block;
719 {
720   int i;
721
722   i = find_index_by_pid (pid);
723   return ((i == NO_PID) ? PROC_BAD : i);
724 }
725
726 /* Print descriptive information about the job with leader pid PID. */
727 void
728 describe_pid (pid)
729      pid_t pid;
730 {
731   fprintf (stderr, "%d\n", (int) pid);
732 }
733
734 void
735 unfreeze_jobs_list ()
736 {
737 }