1 /* The thing that makes children, remembers them, and contains wait loops. */
3 /* This file works with both POSIX and BSD systems. It implements job
6 /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
8 This file is part of GNU Bash, the Bourne Again SHell.
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
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
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. */
26 #include "bashtypes.h"
32 #if defined (HAVE_UNISTD_H)
36 #if defined (HAVE_SYS_TIME_H)
37 # include <sys/time.h>
40 #if defined (HAVE_SYS_RESOURCE_H) && defined (HAVE_WAIT3) && !defined (_POSIX_VERSION)
41 # include <sys/resource.h>
42 #endif /* !_POSIX_VERSION && HAVE_SYS_RESOURCE_H && HAVE_WAIT3 */
46 #include <sys/ioctl.h>
47 #include <sys/param.h>
49 #if defined (BUFFERED_INPUT)
53 /* Need to include this up here for *_TTY_DRIVER definitions. */
56 /* Define this if your output is getting swallowed. It's a no-op on
57 machines with the termio or termios tty drivers. */
58 /* #define DRAIN_OUTPUT */
60 /* The _POSIX_SOURCE define is to avoid multiple symbol definitions
61 between sys/ioctl.h and termios.h. Ditto for the test against SunOS4
62 and the undefining of several symbols. */
63 #if defined (TERMIOS_TTY_DRIVER)
64 # if (defined (SunOS4) || defined (SunOS5)) && !defined (_POSIX_SOURCE)
65 # define _POSIX_SOURCE
73 #else /* !TERMIOS_TTY_DRIVER */
74 # if defined (TERMIO_TTY_DRIVER)
79 #endif /* !TERMIOS_TTY_DRIVER */
81 /* For the TIOCGPGRP and TIOCSPGRP ioctl parameters on HP-UX */
82 #if defined (hpux) && !defined (TERMIOS_TTY_DRIVER)
84 #endif /* hpux && !TERMIOS_TTY_DRIVER */
86 #if !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
87 /* For struct winsize on SCO */
88 /* sys/ptem.h has winsize but needs mblk_t from sys/stream.h */
89 # if defined (HAVE_SYS_PTEM_H) && defined (TIOCGWINSZ) && defined (SIGWINCH)
90 # if defined (HAVE_SYS_STREAM_H)
91 # include <sys/stream.h>
93 # include <sys/ptem.h>
94 # endif /* HAVE_SYS_PTEM_H && TIOCGWINSZ && SIGWINCH */
95 #endif /* !STRUCT_WINSIZE_IN_SYS_IOCTL */
103 #include "builtins/builtext.h"
104 #include "builtins/common.h"
110 /* Take care of system dependencies that must be handled when waiting for
111 children. The arguments to the WAITPID macro match those to the Posix.1
112 waitpid() function. */
114 #if defined (ultrix) && defined (mips) && defined (_POSIX_VERSION)
115 # define WAITPID(pid, statusp, options) \
116 wait3 ((union wait *)statusp, options, (struct rusage *)0)
118 # if defined (_POSIX_VERSION) || defined (HAVE_WAITPID)
119 # define WAITPID(pid, statusp, options) \
120 waitpid ((pid_t)pid, statusp, options)
122 # if defined (HAVE_WAIT3)
123 # define WAITPID(pid, statusp, options) \
124 wait3 (statusp, options, (struct rusage *)0)
126 # define WAITPID(pid, statusp, options) \
127 wait3 (statusp, options, (int *)0)
128 # endif /* HAVE_WAIT3 */
129 # endif /* !_POSIX_VERSION && !HAVE_WAITPID*/
130 #endif /* !(Ultrix && mips && _POSIX_VERSION) */
132 /* getpgrp () varies between systems. Even systems that claim to be
133 Posix.1 compatible lie sometimes (Ultrix, SunOS4, apollo). */
134 #if defined (GETPGRP_VOID)
135 # define getpgid(p) getpgrp ()
137 # define getpgid(p) getpgrp (p)
138 #endif /* !GETPGRP_VOID */
140 /* If the system needs it, REINSTALL_SIGCHLD_HANDLER will reinstall the
141 handler for SIGCHLD. */
142 #if defined (MUST_REINSTALL_SIGHANDLERS)
143 # define REINSTALL_SIGCHLD_HANDLER signal (SIGCHLD, sigchld_handler)
145 # define REINSTALL_SIGCHLD_HANDLER
146 #endif /* !MUST_REINSTALL_SIGHANDLERS */
148 /* The number of additional slots to allocate when we run out. */
151 #if defined (READLINE)
152 extern void _rl_set_screen_size ();
155 /* Variables used here but defined in other files. */
156 extern int interactive, interactive_shell, asynchronous_notification;
157 extern int startup_state, subshell_environment, line_number;
158 extern int posixly_correct, no_symbolic_links, shell_level;
159 extern int interrupt_immediately, last_command_exit_value;
160 extern int loop_level, breaking;
161 extern Function *this_shell_builtin;
162 extern char *shell_name, *this_command_name;
163 extern sigset_t top_level_mask;
165 #if defined (ARRAY_VARS)
166 static int *pstatuses; /* list of pipeline statuses */
168 static void set_pipestatus_array ();
170 static void setjstatus ();
171 static void get_new_window_size ();
173 /* The array of known jobs. */
174 JOB **jobs = (JOB **)NULL;
176 /* The number of slots currently allocated to JOBS. */
179 /* The controlling tty for this shell. */
182 /* The shell's process group. */
183 pid_t shell_pgrp = NO_PID;
185 /* The terminal's process group. */
186 pid_t terminal_pgrp = NO_PID;
188 /* The process group of the shell's parent. */
189 pid_t original_pgrp = NO_PID;
191 /* The process group of the pipeline currently being made. */
192 pid_t pipeline_pgrp = (pid_t)0;
194 #if defined (PGRP_PIPE)
195 /* Pipes which each shell uses to communicate with the process group leader
196 until all of the processes in a pipeline have been started. Then the
197 process leader is allowed to continue. */
198 int pgrp_pipe[2] = { -1, -1 };
201 /* The job which is current; i.e. the one that `%+' stands for. */
202 int current_job = NO_JOB;
204 /* The previous job; i.e. the one that `%-' stands for. */
205 int previous_job = NO_JOB;
207 /* Last child made by the shell. */
208 pid_t last_made_pid = NO_PID;
210 /* Pid of the last asynchronous child. */
211 pid_t last_asynchronous_pid = NO_PID;
213 /* The pipeline currently being built. */
214 PROCESS *the_pipeline = (PROCESS *)NULL;
216 /* If this is non-zero, do job control. */
219 /* Call this when you start making children. */
220 int already_making_children = 0;
222 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
223 exits from get_tty_state(). */
224 int check_window_size;
226 /* Functions local to this file. */
227 static sighandler sigchld_handler ();
228 static int waitchld ();
229 static PROCESS *find_pipeline ();
230 static char *current_working_directory ();
231 static char *job_working_directory ();
232 static pid_t last_pid ();
233 static int set_new_line_discipline (), map_over_jobs (), last_running_job ();
234 static int most_recent_job_in_state (), last_stopped_job (), find_job ();
235 static void notify_of_job_status (), cleanup_dead_jobs (), discard_pipeline ();
236 static void add_process (), set_current_job (), reset_current ();
237 static void print_pipeline ();
238 static void pretty_print_job ();
239 static void mark_dead_jobs_as_notified ();
240 #if defined (PGRP_PIPE)
241 static void pipe_read (), pipe_close ();
244 /* Used to synchronize between wait_for and the SIGCHLD signal handler. */
246 static int waiting_for_job;
248 /* A place to temporarily save the current pipeline. */
249 static PROCESS *saved_pipeline;
250 static int saved_already_making_children;
252 /* Set this to non-zero whenever you don't want the jobs list to change at
253 all: no jobs deleted and no status change notifications. This is used,
254 for example, when executing SIGCHLD traps, which may run arbitrary
256 static int jobs_list_frozen;
258 #if !defined (_POSIX_VERSION)
260 /* These are definitions to map POSIX 1003.1 functions onto existing BSD
261 library functions and system calls. */
262 #define setpgid(pid, pgrp) setpgrp (pid, pgrp)
263 #define tcsetpgrp(fd, pgrp) ioctl ((fd), TIOCSPGRP, &(pgrp))
271 /* ioctl will handle setting errno correctly. */
272 if (ioctl (fd, TIOCGPGRP, &pgrp) < 0)
277 #endif /* !_POSIX_VERSION */
279 /* Return the working directory for the current process. Unlike
280 job_working_directory, this does not call malloc (), nor do any
281 of the functions it calls. This is so that it can safely be called
282 from a signal handler. */
284 current_working_directory ()
287 static char d[PATH_MAX];
289 dir = get_string_value ("PWD");
291 if (dir == 0 && the_current_working_directory && no_symbolic_links)
292 dir = the_current_working_directory;
296 dir = getcwd (d, sizeof(d));
301 return (dir == 0) ? "<unknown>" : dir;
304 /* Return the working directory for the current process. */
306 job_working_directory ()
310 dir = get_string_value ("PWD");
312 return (savestring (dir));
314 dir = get_working_directory ("job-working-directory");
318 return (savestring ("<unknown>"));
324 if (already_making_children)
327 already_making_children = 1;
332 stop_making_children ()
334 already_making_children = 0;
338 cleanup_the_pipeline ()
342 discard_pipeline (the_pipeline);
343 the_pipeline = (PROCESS *)NULL;
348 save_pipeline (clear)
351 saved_pipeline = the_pipeline;
352 saved_already_making_children = already_making_children;
354 the_pipeline = (PROCESS *)NULL;
358 restore_pipeline (discard)
361 PROCESS *old_pipeline;
363 old_pipeline = the_pipeline;
364 the_pipeline = saved_pipeline;
365 already_making_children = saved_already_making_children;
367 discard_pipeline (old_pipeline);
370 /* Start building a pipeline. */
376 cleanup_the_pipeline ();
378 #if defined (PGRP_PIPE)
379 pipe_close (pgrp_pipe);
383 #if defined (PGRP_PIPE)
386 if (pipe (pgrp_pipe) == -1)
387 sys_error ("start_pipeline: pgrp pipe");
392 /* Stop building a pipeline. Install the process list in the job array.
393 This returns the index of the newly installed job.
394 DEFERRED is a command structure to be executed upon satisfactory
395 execution exit of this pipeline. */
397 stop_pipeline (async, deferred)
405 BLOCK_CHILD (set, oset);
407 #if defined (PGRP_PIPE)
408 /* The parent closes the process group synchronization pipe. */
409 pipe_close (pgrp_pipe);
412 cleanup_dead_jobs ();
416 job_slots = JOB_SLOTS;
417 jobs = (JOB **)xmalloc (job_slots * sizeof (JOB *));
419 /* Now blank out these new entries. */
420 for (i = 0; i < job_slots; i++)
421 jobs[i] = (JOB *)NULL;
424 /* Scan from the last slot backward, looking for the next free one. */
427 for (i = job_slots; i; i--)
433 /* If we're not interactive, we don't need to monotonically increase
434 the job number (in fact, we don't care about the job number at all),
435 so we can simply scan for the first free slot. This helps to keep
436 us from continuously reallocating the jobs array when running
437 certain kinds of shell loops, and saves time spent searching. */
438 for (i = 0; i < job_slots; i++)
443 /* Do we need more room? */
446 job_slots += JOB_SLOTS;
447 jobs = (JOB **)xrealloc (jobs, ((1 + job_slots) * sizeof (JOB *)));
449 for (j = i; j < job_slots; j++)
450 jobs[j] = (JOB *)NULL;
453 /* Add the current pipeline to the job list. */
457 int any_alive, any_stopped;
459 newjob = (JOB *)xmalloc (sizeof (JOB));
461 for (p = the_pipeline; p->next != the_pipeline; p = p->next)
463 p->next = (PROCESS *)NULL;
464 newjob->pipe = REVERSE_LIST (the_pipeline, PROCESS *);
465 for (p = newjob->pipe; p->next; p = p->next)
467 p->next = newjob->pipe;
469 the_pipeline = (PROCESS *)NULL;
470 newjob->pgrp = pipeline_pgrp;
475 /* Flag to see if in another pgrp. */
477 newjob->flags |= J_JOBCONTROL;
479 /* Set the state of this pipeline. */
481 any_alive = any_stopped = 0;
484 any_alive |= p->running;
485 any_stopped |= WIFSTOPPED (p->status);
488 while (p != newjob->pipe);
490 newjob->state = any_alive ? JRUNNING : (any_stopped ? JSTOPPED : JDEAD);
491 newjob->wd = job_working_directory ();
492 newjob->deferred = deferred;
494 newjob->j_cleanup = (VFunction *)NULL;
495 newjob->cleanarg = (PTR_T) NULL;
498 if (newjob->state == JDEAD && (newjob->flags & J_FOREGROUND))
502 newjob = (JOB *)NULL;
507 newjob->flags &= ~J_FOREGROUND;
514 newjob->flags |= J_FOREGROUND;
516 * !!!!! NOTE !!!!! (chet@ins.cwru.edu)
518 * The currently-accepted job control wisdom says to set the
519 * terminal's process group n+1 times in an n-step pipeline:
520 * once in the parent and once in each child. This is where
521 * the parent gives it away.
524 if (job_control && newjob->pgrp)
525 give_terminal_to (newjob->pgrp);
529 stop_making_children ();
530 UNBLOCK_CHILD (oset);
531 return (current_job);
534 /* Delete all DEAD jobs that the user had received notification about. */
541 if (job_slots == 0 || jobs_list_frozen)
544 BLOCK_CHILD (set, oset);
546 for (i = 0; i < job_slots; i++)
547 if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i))
550 UNBLOCK_CHILD (oset);
553 /* Delete the job at INDEX from the job list. Must be called
554 with SIGCHLD blocked. */
556 delete_job (job_index)
561 if (jobs_list_frozen)
564 temp = jobs[job_index];
565 if (job_index == current_job || job_index == previous_job)
568 jobs[job_index] = (JOB *)NULL;
571 discard_pipeline (temp->pipe);
574 dispose_command (temp->deferred);
579 /* Must be called with SIGCHLD blocked. */
581 nohup_job (job_index)
586 if (temp = jobs[job_index])
587 temp->flags |= J_NOHUP;
590 /* Get rid of the data structure associated with a process chain. */
592 discard_pipeline (chain)
593 register PROCESS *chain;
595 register PROCESS *this, *next;
601 FREE (this->command);
605 while (this != chain);
608 /* Add this process to the chain being built in the_pipeline.
609 NAME is the command string that will be exec'ed later.
610 PID is the process id of the child. */
612 add_process (name, pid)
618 t = (PROCESS *)xmalloc (sizeof (PROCESS));
619 t->next = the_pipeline;
621 WSTATUS (t->status) = 0;
631 while (p->next != t->next)
638 /* Take the last job and make it the first job. Must be called with
641 rotate_the_pipeline ()
645 if (the_pipeline->next == the_pipeline)
647 for (p = the_pipeline; p->next != the_pipeline; p = p->next)
652 /* Reverse the order of the processes in the_pipeline. Must be called with
655 reverse_the_pipeline ()
659 if (the_pipeline->next == the_pipeline)
662 for (p = the_pipeline; p->next != the_pipeline; p = p->next)
664 p->next = (PROCESS *)NULL;
666 n = REVERSE_LIST (the_pipeline, PROCESS *);
669 for (p = the_pipeline; p->next; p = p->next)
671 p->next = the_pipeline;
675 /* Map FUNC over the list of jobs. If FUNC returns non-zero,
676 then it is time to stop mapping, and that is the return value
677 for map_over_jobs. FUNC is called with a JOB, arg1, arg2,
680 map_over_jobs (func, arg1, arg2)
688 BLOCK_CHILD (set, oset);
690 for (i = result = 0; i < job_slots; i++)
694 result = (*func)(jobs[i], arg1, arg2, i);
700 UNBLOCK_CHILD (oset);
705 /* Cause all the jobs in the current pipeline to exit. */
707 terminate_current_pipeline ()
709 if (pipeline_pgrp && pipeline_pgrp != shell_pgrp)
711 killpg (pipeline_pgrp, SIGTERM);
712 killpg (pipeline_pgrp, SIGCONT);
716 /* Cause all stopped jobs to exit. */
718 terminate_stopped_jobs ()
722 for (i = 0; i < job_slots; i++)
724 if (jobs[i] && STOPPED (i))
726 killpg (jobs[i]->pgrp, SIGTERM);
727 killpg (jobs[i]->pgrp, SIGCONT);
732 /* Cause all jobs, running or stopped, to receive a hangup signal. If
733 a job is marked J_NOHUP, don't send the SIGHUP. */
739 for (i = 0; i < job_slots; i++)
743 if ((jobs[i]->flags & J_NOHUP) == 0)
744 killpg (jobs[i]->pgrp, SIGHUP);
746 killpg (jobs[i]->pgrp, SIGCONT);
752 kill_current_pipeline ()
754 stop_making_children ();
758 /* Return the pipeline that PID belongs to. Note that the pipeline
759 doesn't have to belong to a job. Must be called with SIGCHLD blocked. */
767 /* See if this process is in the pipeline that we are building. */
773 /* Return it if we found it. */
779 while (p != the_pipeline);
782 job = find_job (pid);
784 return (job == NO_JOB) ? (PROCESS *)NULL : jobs[job]->pipe;
787 /* Return the job index that PID belongs to, or NO_JOB if it doesn't
788 belong to any job. Must be called with SIGCHLD blocked. */
796 for (i = 0; i < job_slots; i++)
809 while (p != jobs[i]->pipe);
816 /* Find a job given a PID. If BLOCK is non-zero, block SIGCHLD as
817 required by find_job. */
819 get_job_by_pid (pid, block)
827 BLOCK_CHILD (set, oset);
828 job = find_job (pid);
830 UNBLOCK_CHILD (oset);
835 /* Print descriptive information about the job with leader pid PID. */
843 BLOCK_CHILD (set, oset);
845 job = find_job (pid);
848 printf ("[%d] %d\n", job + 1, (int)pid);
850 programming_error ("describe_pid: %d: no such pid", (int)pid);
852 UNBLOCK_CHILD (oset);
855 /* This is the way to print out information on a job if you
856 know the index. FORMAT is:
858 JLIST_NORMAL) [1]+ Running emacs
859 JLIST_LONG ) [1]+ 2378 Running emacs
862 JLIST_NORMAL) [1]+ Stopped ls | more
863 JLIST_LONG ) [1]+ 2369 Stopped ls
866 Just list the pid of the process group leader (really
869 Use format JLIST_NORMAL, but list only jobs about which
870 the user has not been notified. */
872 /* Print status for pipeline P. If JOB_INDEX is >= 0, it is the index into
873 the JOBS array corresponding to this pipeline. FORMAT is as described
874 above. Must be called with SIGCHLD blocked.
876 If you're printing a pipeline that's not in the jobs array, like the
877 current pipeline as it's being created, pass -1 for JOB_INDEX */
879 print_pipeline (p, job_index, format, stream)
881 int job_index, format;
884 PROCESS *first, *last, *show;
885 int es, name_padding;
886 char retcode_name_buffer[20], *temp;
892 while (last->next != first)
898 fprintf (stream, format ? " " : " |");
900 if (format != JLIST_STANDARD)
901 fprintf (stream, "%5d", (int)p->pid);
903 fprintf (stream, " ");
905 if (format > -1 && job_index >= 0)
907 show = format ? p : last;
910 if (STOPPED (job_index) && format == 0)
913 else if (RUNNING (job_index))
917 if (WIFSTOPPED (show->status))
918 temp = strsignal (WSTOPSIG (show->status));
919 else if (WIFSIGNALED (show->status))
920 temp = strsignal (WTERMSIG (show->status));
921 else if (WIFEXITED (show->status))
923 temp = retcode_name_buffer;
924 es = WEXITSTATUS (show->status);
927 strcpy (temp, "Done");
928 else if (posixly_correct)
929 sprintf (temp, "Done(%d)", es);
931 sprintf (temp, "Exit %d", es);
934 temp = "Unknown status";
941 if (show->running == first->running &&
942 WSTATUS (show->status) == WSTATUS (first->status))
951 fprintf (stream, "%s", temp);
955 es = 2; /* strlen ("| ") */
956 name_padding = LONGEST_SIGNAL_DESC - es;
958 fprintf (stream, "%*s", name_padding, "");
960 if ((WIFSTOPPED (show->status) == 0) && WIFCORED (show->status))
961 fprintf (stream, "(core dumped) ");
965 if (p != first && format)
966 fprintf (stream, "| ");
969 fprintf (stream, "%s", p->command);
971 if (p == last && job_index >= 0)
973 temp = current_working_directory ();
975 if (RUNNING (job_index) && (IS_FOREGROUND (job_index) == 0))
976 fprintf (stream, " &");
978 if (strcmp (temp, jobs[job_index]->wd) != 0)
980 " (wd: %s)", polite_directory_format (jobs[job_index]->wd));
983 if (format || (p == last))
984 fprintf (stream, "\r\n");
994 pretty_print_job (job_index, format, stream)
995 int job_index, format;
1001 BLOCK_CHILD (set, oset);
1003 /* Format only pid information about the process group leader? */
1004 if (format == JLIST_PID_ONLY)
1006 fprintf (stream, "%d\n", (int)jobs[job_index]->pipe->pid);
1007 UNBLOCK_CHILD (oset);
1011 if (format == JLIST_CHANGED_ONLY)
1013 if (IS_NOTIFIED (job_index))
1015 UNBLOCK_CHILD (oset);
1018 format = JLIST_STANDARD;
1021 if (format != JLIST_NONINTERACTIVE)
1022 fprintf (stream, "[%d]%c ", job_index + 1,
1023 (job_index == current_job) ? '+':
1024 (job_index == previous_job) ? '-' : ' ');
1026 if (format == JLIST_NONINTERACTIVE)
1027 format = JLIST_LONG;
1029 p = jobs[job_index]->pipe;
1031 /* We have printed information about this job. When the job's
1032 status changes, waitchld () sets the notification flag to 0. */
1033 jobs[job_index]->flags |= J_NOTIFIED;
1035 print_pipeline (p, job_index, format, stream);
1037 UNBLOCK_CHILD (oset);
1041 print_job (job, format, state, job_index)
1043 int format, state, job_index;
1045 if (state == -1 || (JOB_STATE)state == job->state)
1046 pretty_print_job (job_index, format, stdout);
1051 list_one_job (job, format, ignore, job_index)
1053 int format, ignore, job_index;
1055 print_job (job, format, -1, job_index);
1059 list_stopped_jobs (format)
1062 cleanup_dead_jobs ();
1063 map_over_jobs (print_job, format, (int)JSTOPPED);
1067 list_running_jobs (format)
1070 cleanup_dead_jobs ();
1071 map_over_jobs (print_job, format, (int)JRUNNING);
1074 /* List jobs. If FORMAT is non-zero, then the long form of the information
1075 is printed, else just a short version. */
1077 list_all_jobs (format)
1080 cleanup_dead_jobs ();
1081 map_over_jobs (print_job, format, -1);
1084 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
1085 COMMAND is just for remembering the name of the command; we don't do
1086 anything else with it. ASYNC_P says what to do with the tty. If
1087 non-zero, then don't give it away. */
1089 make_child (command, async_p)
1097 sigaddset (&set, SIGCHLD);
1098 sigaddset (&set, SIGINT);
1099 sigemptyset (&oset);
1100 sigprocmask (SIG_BLOCK, &set, &oset);
1104 #if defined (BUFFERED_INPUT)
1105 /* If default_buffered_input is active, we are reading a script. If
1106 the command is asynchronous, we have already duplicated /dev/null
1107 as fd 0, but have not changed the buffered stream corresponding to
1108 the old fd 0. We don't want to sync the stream in this case. */
1109 if (default_buffered_input != -1 &&
1110 (!async_p || default_buffered_input > 0))
1111 sync_buffered_stream (default_buffered_input);
1112 #endif /* BUFFERED_INPUT */
1114 /* Create the child, handle severe errors. */
1115 if ((pid = fork ()) < 0)
1119 /* Kill all of the processes in the current pipeline. */
1120 terminate_current_pipeline ();
1122 /* Discard the current pipeline, if any. */
1124 kill_current_pipeline ();
1126 throw_to_top_level (); /* Reset signals, etc. */
1131 /* In the child. Give this child the right process group, set the
1132 signals to the default state for a new process. */
1136 #if defined (BUFFERED_INPUT)
1137 /* Close default_buffered_input if it's > 0. We don't close it if it's
1138 0 because that's the file descriptor used when redirecting input,
1139 and it's wrong to close the file in that case. */
1140 if (default_buffered_input > 0)
1142 close_buffered_fd (default_buffered_input);
1143 default_buffered_input = bash_input.location.buffered_fd = -1;
1145 #endif /* BUFFERED_INPUT */
1147 /* Restore top-level signal mask. */
1148 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
1152 /* All processes in this pipeline belong in the same
1155 if (pipeline_pgrp == 0) /* This is the first child. */
1156 pipeline_pgrp = mine;
1158 /* Check for running command in backquotes. */
1159 if (pipeline_pgrp == shell_pgrp)
1160 ignore_tty_job_signals ();
1162 default_tty_job_signals ();
1164 /* Set the process group before trying to mess with the terminal's
1165 process group. This is mandated by POSIX. */
1166 /* This is in accordance with the Posix 1003.1 standard,
1167 section B.7.2.4, which says that trying to set the terminal
1168 process group with tcsetpgrp() to an unused pgrp value (like
1169 this would have for the first child) is an error. Section
1170 B.4.3.3, p. 237 also covers this, in the context of job control
1172 if (setpgid (mine, pipeline_pgrp) < 0)
1173 sys_error ("child setpgid (%d to %d)", mine, pipeline_pgrp);
1174 #if defined (PGRP_PIPE)
1175 if (pipeline_pgrp == mine)
1179 give_terminal_to (pipeline_pgrp);
1181 #if defined (PGRP_PIPE)
1182 pipe_read (pgrp_pipe);
1186 else /* Without job control... */
1188 if (pipeline_pgrp == 0)
1189 pipeline_pgrp = shell_pgrp;
1191 /* If these signals are set to SIG_DFL, we encounter the curious
1192 situation of an interactive ^Z to a running process *working*
1193 and stopping the process, but being unable to do anything with
1194 that process to change its state. On the other hand, if they
1195 are set to SIG_IGN, jobs started from scripts do not stop when
1196 the shell running the script gets a SIGTSTP and stops. */
1198 default_tty_job_signals ();
1201 #if defined (PGRP_PIPE)
1202 /* Release the process group pipe, since our call to setpgid ()
1203 is done. The last call to pipe_close is done in stop_pipeline. */
1204 pipe_close (pgrp_pipe);
1205 #endif /* PGRP_PIPE */
1208 last_asynchronous_pid = getpid ();
1212 /* In the parent. Remember the pid of the child just created
1213 as the proper pgrp if this is the first child. */
1217 if (pipeline_pgrp == 0)
1219 pipeline_pgrp = pid;
1220 /* Don't twiddle terminal pgrps in the parent! This is the bug,
1221 not the good thing of twiddling them in the child! */
1222 /* give_terminal_to (pipeline_pgrp); */
1224 /* This is done on the recommendation of the Rationale section of
1225 the POSIX 1003.1 standard, where it discusses job control and
1226 shells. It is done to avoid possible race conditions. (Ref.
1227 1003.1 Rationale, section B.4.3.3, page 236). */
1228 setpgid (pid, pipeline_pgrp);
1232 if (pipeline_pgrp == 0)
1233 pipeline_pgrp = shell_pgrp;
1236 /* Place all processes into the jobs array regardless of the
1237 state of job_control. */
1238 add_process (command, pid);
1241 last_asynchronous_pid = pid;
1243 last_made_pid = pid;
1245 /* Unblock SIGINT and SIGCHLD. */
1246 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
1253 ignore_tty_job_signals ()
1255 set_signal_handler (SIGTSTP, SIG_IGN);
1256 set_signal_handler (SIGTTIN, SIG_IGN);
1257 set_signal_handler (SIGTTOU, SIG_IGN);
1261 default_tty_job_signals ()
1263 set_signal_handler (SIGTSTP, SIG_DFL);
1264 set_signal_handler (SIGTTIN, SIG_DFL);
1265 set_signal_handler (SIGTTOU, SIG_DFL);
1268 /* When we end a job abnormally, or if we stop a job, we set the tty to the
1269 state kept in here. When a job ends normally, we set the state in here
1270 to the state of the tty. */
1272 #if defined (NEW_TTY_DRIVER)
1273 static struct sgttyb shell_tty_info;
1274 static struct tchars shell_tchars;
1275 static struct ltchars shell_ltchars;
1276 #endif /* NEW_TTY_DRIVER */
1278 #if defined (TERMIO_TTY_DRIVER)
1279 static struct termio shell_tty_info;
1280 #endif /* TERMIO_TTY_DRIVER */
1282 #if defined (TERMIOS_TTY_DRIVER)
1283 static struct termios shell_tty_info;
1284 #endif /* TERMIOS_TTY_DRIVER */
1286 #if defined (NEW_TTY_DRIVER) && defined (DRAIN_OUTPUT)
1287 /* Since the BSD tty driver does not allow us to change the tty modes
1288 while simultaneously waiting for output to drain and preserving
1289 typeahead, we have to drain the output ourselves before calling
1290 ioctl. We cheat by finding the length of the output queue, and
1291 using select to wait for an appropriate length of time. This is
1292 a hack, and should be labeled as such (it's a hastily-adapted
1293 mutation of a `usleep' implementation). It's only reason for
1294 existing is the flaw in the BSD tty driver. */
1296 static int ttspeeds[] =
1298 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
1299 1800, 2400, 4800, 9600, 19200, 38400
1306 register int delay = ttspeeds[ospeed];
1312 while ((ioctl (fd, TIOCOUTQ, &n) == 0) && n)
1314 if (n > (delay / 100))
1318 n *= 10; /* 2 bits more for conservativeness. */
1319 tv.tv_sec = n / delay;
1320 tv.tv_usec = ((n % delay) * 1000000) / delay;
1321 select (fd, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
1327 #endif /* NEW_TTY_DRIVER && DRAIN_OUTPUT */
1329 /* Return the fd from which we are actually getting input. */
1330 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
1332 /* Fill the contents of shell_tty_info with the current tty info. */
1341 #if defined (NEW_TTY_DRIVER)
1342 ioctl (tty, TIOCGETP, &shell_tty_info);
1343 ioctl (tty, TIOCGETC, &shell_tchars);
1344 ioctl (tty, TIOCGLTC, &shell_ltchars);
1345 #endif /* NEW_TTY_DRIVER */
1347 #if defined (TERMIO_TTY_DRIVER)
1348 ioctl (tty, TCGETA, &shell_tty_info);
1349 #endif /* TERMIO_TTY_DRIVER */
1351 #if defined (TERMIOS_TTY_DRIVER)
1352 if (tcgetattr (tty, &shell_tty_info) < 0)
1355 /* Only print an error message if we're really interactive at
1358 sys_error ("[%d: %d] tcgetattr", getpid (), shell_level);
1362 #endif /* TERMIOS_TTY_DRIVER */
1363 if (check_window_size)
1364 get_new_window_size (0);
1369 /* Make the current tty use the state in shell_tty_info. */
1378 #if defined (NEW_TTY_DRIVER)
1379 # if defined (DRAIN_OUTPUT)
1380 draino (tty, shell_tty_info.sg_ospeed);
1381 # endif /* DRAIN_OUTPUT */
1382 ioctl (tty, TIOCSETN, &shell_tty_info);
1383 ioctl (tty, TIOCSETC, &shell_tchars);
1384 ioctl (tty, TIOCSLTC, &shell_ltchars);
1385 #endif /* NEW_TTY_DRIVER */
1387 #if defined (TERMIO_TTY_DRIVER)
1388 ioctl (tty, TCSETAW, &shell_tty_info);
1389 #endif /* TERMIO_TTY_DRIVER */
1391 #if defined (TERMIOS_TTY_DRIVER)
1392 if (tcsetattr (tty, TCSADRAIN, &shell_tty_info) < 0)
1394 /* Only print an error message if we're really interactive at
1397 sys_error ("[%d: %d] tcsetattr", getpid (), shell_level);
1400 #endif /* TERMIOS_TTY_DRIVER */
1405 /* Given an index into the jobs array JOB, return the pid of the last
1406 process in that job's pipeline. This is the one whose exit status
1412 register PROCESS *p;
1415 BLOCK_CHILD (set, oset);
1417 p = jobs[job]->pipe;
1418 while (p->next != jobs[job]->pipe)
1421 UNBLOCK_CHILD (oset);
1425 /* Wait for a particular child of the shell to finish executing.
1426 This low-level function prints an error message if PID is not
1427 a child of this shell. It returns -1 if it fails, or 0 if not. */
1429 wait_for_single_pid (pid)
1432 register PROCESS *child;
1435 BLOCK_CHILD (set, oset);
1436 child = find_pipeline (pid);
1437 UNBLOCK_CHILD (oset);
1441 internal_error ("wait: pid %d is not a child of this shell", pid);
1445 return (wait_for (pid));
1448 /* Wait for all of the backgrounds of this shell to finish. */
1450 wait_for_background_pids ()
1452 register int i, count;
1458 BLOCK_CHILD (set, oset);
1461 for (i = 0; i < job_slots; i++)
1462 if (jobs[i] && RUNNING (i) && IS_FOREGROUND (i) == 0)
1470 UNBLOCK_CHILD (oset);
1474 for (i = 0; i < job_slots; i++)
1475 if (jobs[i] && RUNNING (i) && IS_FOREGROUND (i) == 0)
1478 UNBLOCK_CHILD (oset);
1480 wait_for_single_pid (pid);
1486 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
1487 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
1488 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
1491 restore_sigint_handler ()
1493 if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
1495 set_signal_handler (SIGINT, old_sigint_handler);
1496 old_sigint_handler = INVALID_SIGNAL_HANDLER;
1500 static int wait_sigint_received;
1502 /* Handle SIGINT while we are waiting for children in a script to exit.
1503 The `wait' builtin should be interruptible, but all others should be
1504 effectively ignored (i.e. not cause the shell to exit). */
1506 wait_sigint_handler (sig)
1509 if (interrupt_immediately ||
1510 (this_shell_builtin && this_shell_builtin == wait_builtin))
1512 last_command_exit_value = EXECUTION_FAILURE;
1513 restore_sigint_handler ();
1518 /* XXX - should this be interrupt_state? If it is, the shell will act
1519 as if it got the SIGINT interrupt. */
1520 wait_sigint_received = 1;
1522 /* Otherwise effectively ignore the SIGINT and allow the running job to
1528 process_exit_status (status)
1531 if (WIFSIGNALED (status))
1532 return (128 + WTERMSIG (status));
1533 else if (WIFSTOPPED (status) == 0)
1534 return (WEXITSTATUS (status));
1536 return (EXECUTION_SUCCESS);
1540 job_exit_status (job)
1543 register PROCESS *p;
1544 for (p = jobs[job]->pipe; p->next != jobs[job]->pipe; p = p->next)
1546 return (process_exit_status (p->status));
1549 /* Wait for pid (one of our children) to terminate, then
1550 return the termination state. */
1551 #define FIND_CHILD(pid, child) \
1554 child = find_pipeline (pid); \
1557 give_terminal_to (shell_pgrp); \
1558 UNBLOCK_CHILD (oset); \
1559 internal_error ("wait_for: No record of process %d", pid); \
1560 restore_sigint_handler (); \
1561 return (termination_state = 127); \
1570 int job, termination_state;
1571 register PROCESS *child;
1574 register PROCESS *p;
1575 int job_state, any_stopped;
1578 /* In the case that this code is interrupted, and we longjmp () out of it,
1579 we are relying on the code in throw_to_top_level () to restore the
1580 top-level signal mask. */
1581 BLOCK_CHILD (set, oset);
1583 /* Ignore interrupts while waiting for a job run without job control
1584 to finish. We don't want the shell to exit if an interrupt is
1585 received, only if one of the jobs run is killed via SIGINT. If
1586 job control is not set, the job will be run in the same pgrp as
1587 the shell, and the shell will see any signals the job gets. */
1589 /* This is possibly a race condition -- should it go in stop_pipeline? */
1590 wait_sigint_received = 0;
1591 if (job_control == 0)
1592 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
1594 termination_state = last_command_exit_value;
1596 if (interactive && job_control == 0)
1599 /* If we say wait_for (), then we have a record of this child somewhere.
1600 If it and none of its peers are running, don't call waitchld(). */
1605 FIND_CHILD (pid, child);
1607 /* If this child is part of a job, then we are really waiting for the
1608 job to finish. Otherwise, we are waiting for the child to finish.
1609 We check for JDEAD in case the job state has been set by waitchld
1610 after receipt of a SIGCHLD. */
1612 job = find_job (pid);
1615 /* XXX - let waitchld take care of setting this. If the job has
1616 already exited before this is called, sigchld_handler will have
1617 called waitchld and this will be set to JDEAD. */
1618 if (job != NO_JOB && JOBSTATE (job) != JDEAD)
1620 job_state = any_stopped = 0;
1621 p = jobs[job]->pipe;
1624 job_state |= p->running;
1625 if (p->running == 0)
1626 any_stopped |= WIFSTOPPED (p->status);
1629 while (p != jobs[job]->pipe);
1632 jobs[job]->state = any_stopped ? JSTOPPED : JDEAD;
1636 if (child->running || (job != NO_JOB && RUNNING (job)))
1638 #if defined (WAITPID_BROKEN) /* SCOv4 */
1639 sigset_t suspend_set;
1640 sigemptyset (&suspend_set);
1641 sigsuspend (&suspend_set);
1642 #else /* !WAITPID_BROKEN */
1643 # if defined (MUST_UNBLOCK_CHLD)
1644 struct sigaction act, oact;
1645 sigset_t nullset, chldset;
1647 sigemptyset (&nullset);
1648 sigemptyset (&chldset);
1649 sigprocmask (SIG_SETMASK, &nullset, &chldset);
1650 act.sa_handler = SIG_DFL;
1651 sigemptyset (&act.sa_mask);
1652 sigemptyset (&oact.sa_mask);
1654 sigaction (SIGCHLD, &act, &oact);
1656 waiting_for_job = 1;
1658 # if defined (MUST_UNBLOCK_CHLD)
1659 sigaction (SIGCHLD, &oact, (struct sigaction *)NULL);
1660 sigprocmask (SIG_SETMASK, &chldset, (sigset_t *)NULL);
1662 waiting_for_job = 0;
1663 #endif /* WAITPID_BROKEN */
1666 /* If the shell is interactive, and job control is disabled, see
1667 if the foreground process has died due to SIGINT and jump out
1668 of the wait loop if it has. waitchld has already restored the
1669 old SIGINT signal handler. */
1670 if (interactive && job_control == 0)
1673 while (child->running || (job != NO_JOB && RUNNING (job)));
1675 /* The exit state of the command is either the termination state of the
1676 child, or the termination state of the job. If a job, the status
1677 of the last child in the pipeline is the significant one. */
1680 termination_state = job_exit_status (job);
1682 termination_state = process_exit_status (child->status);
1684 if (job == NO_JOB || IS_JOBCONTROL (job))
1685 give_terminal_to (shell_pgrp);
1687 /* If the command did not exit cleanly, or the job is just
1688 being stopped, then reset the tty state back to what it
1689 was before this command. Reset the tty state and notify
1690 the user of the job termination only if the shell is
1691 interactive. Clean up any dead jobs in either case. */
1694 if (interactive_shell && subshell_environment == 0)
1696 if (WIFSIGNALED (child->status) || WIFSTOPPED (child->status))
1701 /* If job control is enabled, the job was started with job
1702 control, the job was the foreground job, and it was killed
1703 by SIGINT, then print a newline to compensate for the kernel
1704 printing the ^C without a trailing newline. */
1705 if (job_control && IS_JOBCONTROL (job) && IS_FOREGROUND (job) &&
1706 WIFSIGNALED (child->status) &&
1707 WTERMSIG (child->status) == SIGINT)
1709 /* If SIGINT is not trapped and the shell is in a for, while,
1710 or until loop, act as if the shell received SIGINT as
1711 well, so the loop can be broken. This doesn't call the
1712 SIGINT signal handler; maybe it should. */
1713 if (signal_is_trapped (SIGINT) == 0 && loop_level)
1722 notify_and_cleanup ();
1726 /* If this job is dead, and the shell is not interactive, make
1727 sure we turn on the notify bit so we don't get an unwanted
1728 message about the job's termination, and so delete_job really
1729 clears the slot in the jobs table. */
1732 jobs[job]->flags |= J_NOTIFIED;
1733 cleanup_dead_jobs ();
1735 notify_and_cleanup ();
1740 UNBLOCK_CHILD (oset);
1742 /* Restore the original SIGINT signal handler before we return. */
1743 restore_sigint_handler ();
1745 return (termination_state);
1748 /* Wait for the last process in the pipeline for JOB. */
1755 pid = last_pid (job);
1756 return (wait_for (pid));
1759 /* Print info about dead jobs, and then delete them from the list
1760 of known jobs. This does not actually delete jobs when the
1761 shell is not interactive, because the dead jobs are not marked
1764 notify_and_cleanup ()
1766 if (jobs_list_frozen)
1769 if (interactive || interactive_shell == 0)
1770 notify_of_job_status ();
1772 cleanup_dead_jobs ();
1775 /* Make dead jobs disappear from the jobs array without notification.
1776 This is used when the shell is not interactive. */
1780 mark_dead_jobs_as_notified ();
1781 cleanup_dead_jobs ();
1784 /* Return the next closest (chronologically) job to JOB which is in
1785 STATE. STATE can be JSTOPPED, JRUNNING. NO_JOB is returned if
1786 there is no next recent job. */
1788 most_recent_job_in_state (job, state)
1792 register int i, result;
1795 BLOCK_CHILD (set, oset);
1796 for (result = NO_JOB, i = job - 1; i >= 0; i--)
1798 if (jobs[i] && (JOBSTATE (i) == state))
1804 UNBLOCK_CHILD (oset);
1809 /* Return the newest *stopped* job older than JOB, or NO_JOB if not
1812 last_stopped_job (job)
1815 return (most_recent_job_in_state (job, JSTOPPED));
1818 /* Return the newest *running* job older than JOB, or NO_JOB if not
1821 last_running_job (job)
1824 return (most_recent_job_in_state (job, JRUNNING));
1827 /* Make JOB be the current job, and make previous be useful. Must be
1828 called with SIGCHLD blocked. */
1830 set_current_job (job)
1835 if (current_job != job)
1837 previous_job = current_job;
1841 /* First choice for previous_job is the old current_job. */
1842 if (previous_job != current_job &&
1843 previous_job != NO_JOB &&
1844 jobs[previous_job] &&
1845 STOPPED (previous_job))
1848 /* Second choice: Newest stopped job that is older than
1851 if (STOPPED (current_job))
1853 candidate = last_stopped_job (current_job);
1855 if (candidate != NO_JOB)
1857 previous_job = candidate;
1862 /* If we get here, there is either only one stopped job, in which case it is
1863 the current job and the previous job should be set to the newest running
1864 job, or there are only running jobs and the previous job should be set to
1865 the newest running job older than the current job. We decide on which
1866 alternative to use based on whether or not JOBSTATE(current_job) is
1869 candidate = RUNNING (current_job) ? last_running_job (current_job)
1870 : last_running_job (job_slots);
1872 if (candidate != NO_JOB)
1874 previous_job = candidate;
1878 /* There is only a single job, and it is both `+' and `-'. */
1879 previous_job = current_job;
1882 /* Make current_job be something useful, if it isn't already. */
1884 /* Here's the deal: The newest non-running job should be `+', and the
1885 next-newest non-running job should be `-'. If there is only a single
1886 stopped job, the previous_job is the newest non-running job. If there
1887 are only running jobs, the newest running job is `+' and the
1888 next-newest running job is `-'. Must be called with SIGCHLD blocked. */
1895 if (job_slots && current_job != NO_JOB && jobs[current_job] && STOPPED (current_job))
1896 candidate = current_job;
1901 /* First choice: the previous job. */
1902 if (previous_job != NO_JOB && jobs[previous_job] && STOPPED (previous_job))
1903 candidate = previous_job;
1905 /* Second choice: the most recently stopped job. */
1906 if (candidate == NO_JOB)
1907 candidate = last_stopped_job (job_slots);
1909 /* Third choice: the newest running job. */
1910 if (candidate == NO_JOB)
1911 candidate = last_running_job (job_slots);
1914 /* If we found a job to use, then use it. Otherwise, there
1915 are no jobs period. */
1916 if (candidate != NO_JOB)
1917 set_current_job (candidate);
1919 current_job = previous_job = NO_JOB;
1922 /* Set up the job structures so we know the job and its processes are
1925 set_job_running (job)
1928 register PROCESS *p;
1930 /* Each member of the pipeline is now running. */
1931 p = jobs[job]->pipe;
1935 if (WIFSTOPPED (p->status))
1939 while (p != jobs[job]->pipe);
1941 /* This means that the job is running. */
1942 JOBSTATE (job) = JRUNNING;
1945 /* Start a job. FOREGROUND if non-zero says to do that. Otherwise,
1946 start the job in the background. JOB is a zero-based index into
1947 JOBS. Returns -1 if it is unable to start a job, and the return
1948 status of the job otherwise. */
1950 start_job (job, foreground)
1951 int job, foreground;
1953 register PROCESS *p;
1954 int already_running;
1957 #if defined (NEW_TTY_DRIVER)
1958 static struct sgttyb save_stty;
1960 #if defined (TERMIO_TTY_DRIVER)
1961 static struct termio save_stty;
1963 #if defined (TERMIOS_TTY_DRIVER)
1964 static struct termios save_stty;
1967 BLOCK_CHILD (set, oset);
1971 internal_error ("%s: job has terminated", this_command_name);
1972 UNBLOCK_CHILD (oset);
1976 already_running = RUNNING (job);
1978 if (foreground == 0 && already_running)
1980 internal_error ("%s: bg background job?", this_command_name);
1981 UNBLOCK_CHILD (oset);
1985 wd = current_working_directory ();
1987 /* You don't know about the state of this job. Do you? */
1988 jobs[job]->flags &= ~J_NOTIFIED;
1992 set_current_job (job);
1993 jobs[job]->flags |= J_FOREGROUND;
1996 /* Tell the outside world what we're doing. */
1997 p = jobs[job]->pipe;
1999 if (foreground == 0)
2000 fprintf (stderr, "[%d]%c ", job + 1,
2001 (job == current_job) ? '+': ((job == previous_job) ? '-' : ' '));
2005 fprintf (stderr, "%s%s",
2006 p->command ? p->command : "",
2007 p->next != jobs[job]->pipe? " | " : "");
2010 while (p != jobs[job]->pipe);
2012 if (foreground == 0)
2013 fprintf (stderr, " &");
2015 if (strcmp (wd, jobs[job]->wd) != 0)
2016 fprintf (stderr, " (wd: %s)", polite_directory_format (jobs[job]->wd));
2018 fprintf (stderr, "\n");
2021 if (already_running == 0)
2022 set_job_running (job);
2024 /* Save the tty settings before we start the job in the foreground. */
2028 save_stty = shell_tty_info;
2029 /* Give the terminal to this job. */
2030 if (IS_JOBCONTROL (job))
2031 give_terminal_to (jobs[job]->pgrp);
2034 jobs[job]->flags &= ~J_FOREGROUND;
2036 /* If the job is already running, then don't bother jump-starting it. */
2037 if (already_running == 0)
2039 jobs[job]->flags |= J_NOTIFIED;
2040 killpg (jobs[job]->pgrp, SIGCONT);
2043 UNBLOCK_CHILD (oset);
2050 pid = last_pid (job);
2052 shell_tty_info = save_stty;
2058 BLOCK_CHILD (set, oset);
2060 UNBLOCK_CHILD (oset);
2065 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
2066 If PID does belong to a job, and the job is stopped, then CONTinue the
2067 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
2068 then kill the process group associated with PID. */
2070 kill_pid (pid, sig, group)
2074 register PROCESS *p;
2078 BLOCK_CHILD (set, oset);
2079 p = find_pipeline (pid);
2080 job = find_job (pid);
2082 result = EXECUTION_SUCCESS;
2087 jobs[job]->flags &= ~J_NOTIFIED;
2089 /* Kill process in backquotes or one started without job control? */
2090 if (jobs[job]->pgrp == shell_pgrp)
2092 p = jobs[job]->pipe;
2097 if (p->running == 0 && (sig == SIGTERM || sig == SIGHUP))
2098 kill (p->pid, SIGCONT);
2101 while (p != jobs[job]->pipe);
2105 result = killpg (jobs[job]->pgrp, sig);
2106 if (p && STOPPED (job) && (sig == SIGTERM || sig == SIGHUP))
2107 killpg (jobs[job]->pgrp, SIGCONT);
2108 /* If we're continuing a stopped job via kill rather than bg or
2109 fg, emulate the `bg' behavior. */
2110 if (p && STOPPED (job) && (sig == SIGCONT))
2112 set_job_running (job);
2113 jobs[job]->flags &= ~J_FOREGROUND;
2114 jobs[job]->flags |= J_NOTIFIED;
2119 result = killpg (pid, sig);
2122 result = kill (pid, sig);
2124 UNBLOCK_CHILD (oset);
2128 /* sigchld_handler () flushes at least one of the children that we are
2129 waiting for. It gets run when we have gotten a SIGCHLD signal. */
2131 sigchld_handler (sig)
2136 REINSTALL_SIGCHLD_HANDLER;
2139 if (waiting_for_job == 0)
2140 n = waitchld (-1, 0);
2144 /* waitchld() reaps dead or stopped children. It's called by wait_for and
2145 flush_child, and runs until there aren't any children terminating any more.
2146 If BLOCK is 1, this is to be a blocking wait for a single child, although
2147 an arriving SIGCHLD could cause the wait to be non-blocking. */
2149 waitchld (wpid, block)
2156 int call_set_current, last_stopped_job, job, children_exited;
2157 int job_state, any_stopped, any_tstped, waitpid_flags, tstatus;
2159 call_set_current = children_exited = 0;
2160 last_stopped_job = NO_JOB;
2164 /* We don't want to be notified about jobs stopping if job control
2165 is not active. XXX - was interactive_shell instead of job_control */
2166 waitpid_flags = (job_control && subshell_environment == 0)
2169 if (sigchld || block == 0)
2170 waitpid_flags |= WNOHANG;
2171 pid = WAITPID (-1, &status, waitpid_flags);
2172 /* The check for WNOHANG is to make sure we decrement sigchld only
2173 if it was non-zero before we called waitpid. */
2174 if (sigchld > 0 && (waitpid_flags & WNOHANG))
2177 /* If waitpid returns 0, there are running children. */
2179 continue; /* jumps right to the test */
2183 /* Locate our PROCESS for this pid. */
2184 child = find_pipeline (pid);
2186 /* It is not an error to have a child terminate that we did
2187 not have a record of. This child could have been part of
2188 a pipeline in backquote substitution. Even so, I'm not
2189 sure child is ever non-zero. */
2193 while (child->pid != pid)
2194 child = child->next;
2196 /* Remember status, and fact that process is not running. */
2197 child->status = status;
2200 job = find_job (pid);
2205 /* Note that we're resetting `child' here because we now want to
2206 deal with the job. */
2207 child = jobs[job]->pipe;
2208 jobs[job]->flags &= ~J_NOTIFIED;
2210 /* If all children are not running, but any of them is
2211 stopped, then the job is stopped, not dead. */
2212 job_state = any_stopped = any_tstped = 0;
2215 job_state |= child->running;
2216 if (child->running == 0 && (WIFSTOPPED (child->status)))
2219 any_tstped |= interactive && job_control &&
2220 (WSTOPSIG (child->status) == SIGTSTP);
2222 child = child->next;
2224 while (child != jobs[job]->pipe);
2226 /* If job_state != 0, the job is still running, so don't bother with
2227 setting the process exit status and job state. */
2231 /* The job is either stopped or dead. Set the state of the job
2235 jobs[job]->state = JSTOPPED;
2236 jobs[job]->flags &= ~J_FOREGROUND;
2238 last_stopped_job = job;
2239 /* Suspending a job with SIGTSTP breaks all active loops. */
2240 if (any_tstped && loop_level)
2241 breaking = loop_level;
2245 /* ASSERT(child == jobs[job]->pipe); */
2246 jobs[job]->state = JDEAD;
2247 if (job == last_stopped_job)
2248 last_stopped_job = NO_JOB;
2250 if (IS_FOREGROUND (job))
2251 setjstatus (job); /* XXX */
2253 /* If this job has a cleanup function associated with it, call it
2254 with `cleanarg' as the single argument, then set the function
2255 pointer to NULL so it is not inadvertently called twice. The
2256 cleanup function is responsible for deallocating cleanarg. */
2257 if (jobs[job]->j_cleanup)
2259 (*jobs[job]->j_cleanup) (jobs[job]->cleanarg);
2260 jobs[job]->j_cleanup = (VFunction *)NULL;
2264 If we're running a shell script and we get a SIGINT with a
2265 SIGINT trap handler, but the foreground job handles it and
2266 does not exit due to SIGINT, run the trap handler but do not
2267 otherwise act as if we got the interrupt. */
2268 if (wait_sigint_received && interactive_shell == 0 &&
2269 WIFSIGNALED (child->status) == 0 && IS_FOREGROUND (job) &&
2270 signal_is_trapped (SIGINT))
2272 wait_sigint_received = 0;
2273 last_command_exit_value = process_exit_status (child->status);
2275 jobs_list_frozen = 1;
2276 tstatus = maybe_call_trap_handler (SIGINT);
2277 jobs_list_frozen = 0;
2280 /* If the foreground job is killed by SIGINT when
2281 job control is not active, we need to perform
2282 some special handling.
2284 The check of wait_sigint_received is a way to
2285 determine if the SIGINT came from the keyboard
2286 (in which case the shell has already seen it,
2287 and wait_sigint_received is non-zero, because
2288 keyboard signals are sent to process groups)
2289 or via kill(2) to the foreground process by
2290 another process (or itself). If the shell did
2291 receive the SIGINT, it needs to perform normal
2292 SIGINT processing. */
2293 else if (wait_sigint_received && (WTERMSIG (child->status) == SIGINT) &&
2294 IS_FOREGROUND (job) && IS_JOBCONTROL (job) == 0)
2296 wait_sigint_received = 0;
2298 /* If SIGINT is trapped, set the exit status so
2299 that the trap handler can see it. */
2300 if (signal_is_trapped (SIGINT))
2301 last_command_exit_value = process_exit_status (child->status);
2303 /* If the signal is trapped, let the trap handler
2304 get it no matter what and simply return if
2305 the trap handler returns.
2306 maybe_call_trap_handler() may cause dead jobs
2307 to be removed from the job table because of
2308 a call to execute_command. Watch out for this. */
2309 jobs_list_frozen = 1;
2310 tstatus = maybe_call_trap_handler (SIGINT);
2311 jobs_list_frozen = 0;
2312 if (tstatus == 0 && old_sigint_handler != INVALID_SIGNAL_HANDLER)
2314 /* wait_sigint_handler () has already seen SIGINT and
2315 allowed the wait builtin to jump out. We need to
2316 call the original SIGINT handler, if necessary. If
2317 the original handler is SIG_DFL, we need to resend
2318 the signal to ourselves. */
2319 SigHandler *temp_handler;
2320 temp_handler = old_sigint_handler;
2321 restore_sigint_handler ();
2322 if (temp_handler == SIG_DFL)
2323 termination_unwind_protect (SIGINT);
2324 else if (temp_handler != SIG_IGN)
2325 (*temp_handler) (SIGINT);
2330 while ((sigchld || block == 0) && pid > (pid_t)0);
2332 /* If a job was running and became stopped, then set the current
2333 job. Otherwise, don't change a thing. */
2334 if (call_set_current)
2335 if (last_stopped_job != NO_JOB)
2336 set_current_job (last_stopped_job);
2340 /* Call a SIGCHLD trap handler for each child that exits, if one is set. */
2341 if (job_control && signal_is_trapped (SIGCHLD) &&
2342 trap_list[SIGCHLD] != (char *)IGNORE_SIG)
2347 /* Turn off the trap list during the call to parse_and_execute ()
2348 to avoid potentially infinite recursive calls. Preserve the
2349 values of last_command_exit_value, last_made_pid, and the_pipeline
2350 around the execution of the trap commands. */
2351 trap_command = savestring (trap_list[SIGCHLD]);
2353 begin_unwind_frame ("SIGCHLD trap");
2354 unwind_protect_int (last_command_exit_value);
2355 if (sizeof (pid_t) == sizeof (short))
2356 unwind_protect_short (last_made_pid);
2358 unwind_protect_int (last_made_pid);
2359 unwind_protect_int (interrupt_immediately);
2360 unwind_protect_int (jobs_list_frozen);
2361 unwind_protect_pointer (the_pipeline);
2363 /* We have to add the commands this way because they will be run
2364 in reverse order of adding. We don't want maybe_set_sigchld_trap ()
2365 to reference freed memory. */
2366 add_unwind_protect ((Function *)xfree, trap_command);
2367 add_unwind_protect ((Function *)maybe_set_sigchld_trap, trap_command);
2369 the_pipeline = (PROCESS *)NULL;
2370 restore_default_signal (SIGCHLD);
2371 jobs_list_frozen = 1;
2372 for (i = 0; i < children_exited; i++)
2374 interrupt_immediately = 1;
2375 parse_and_execute (savestring (trap_command), "trap", SEVAL_NOHIST);
2378 run_unwind_frame ("SIGCHLD trap");
2381 /* We have successfully recorded the useful information about this process
2382 that has just changed state. If we notify asynchronously, and the job
2383 that this process belongs to is no longer running, then notify the user
2384 of that fact now. */
2385 if (asynchronous_notification && interactive)
2386 notify_of_job_status ();
2388 return (children_exited);
2391 /* Function to call when you want to notify people of changes
2392 in job status. This prints out all jobs which are pending
2393 notification to stderr, and marks those printed as already
2394 notified, thus making them candidates for cleanup. */
2396 notify_of_job_status ()
2398 register int job, termsig;
2404 sigaddset (&set, SIGCHLD);
2405 sigaddset (&set, SIGTTOU);
2406 sigemptyset (&oset);
2407 sigprocmask (SIG_BLOCK, &set, &oset);
2409 for (job = 0, dir = (char *)NULL; job < job_slots; job++)
2411 if (jobs[job] && IS_NOTIFIED (job) == 0)
2413 s = jobs[job]->pipe->status;
2414 termsig = WTERMSIG (s);
2416 /* If job control is disabled, don't print the status messages.
2417 Mark dead jobs as notified so that they get cleaned up. If
2418 startup_state == 2, we were started to run `-c command', so
2419 don't print anything. If the shell is not interactive, don't
2420 print anything unless the job was killed by a signal. */
2421 if ((job_control == 0 && interactive_shell) || startup_state == 2 ||
2422 (startup_state == 0 && WIFSIGNALED (s) == 0))
2425 jobs[job]->flags |= J_NOTIFIED;
2429 /* Print info on jobs that are running in the background,
2430 and on foreground jobs that were killed by anything
2431 except SIGINT (and possibly SIGPIPE). */
2432 switch (JOBSTATE (job))
2435 if (interactive_shell == 0 && termsig && WIFSIGNALED (s) &&
2436 signal_is_trapped (termsig) == 0)
2438 fprintf (stderr, "%s: line %d: ", get_name_for_error (), line_number);
2439 pretty_print_job (job, JLIST_NONINTERACTIVE, stderr);
2441 else if (IS_FOREGROUND (job))
2443 #if !defined (DONT_REPORT_SIGPIPE)
2444 if (termsig && WIFSIGNALED (s) && termsig != SIGINT)
2446 if (termsig && WIFSIGNALED (s) && termsig != SIGINT && termsig != SIGPIPE)
2449 fprintf (stderr, "%s", strsignal (termsig));
2452 fprintf (stderr, " (core dumped)");
2454 fprintf (stderr, "\n");
2460 dir = current_working_directory ();
2461 pretty_print_job (job, JLIST_STANDARD, stderr);
2462 if (dir && strcmp (dir, jobs[job]->wd) != 0)
2464 "(wd now: %s)\n", polite_directory_format (dir));
2467 jobs[job]->flags |= J_NOTIFIED;
2471 fprintf (stderr, "\n");
2473 dir = current_working_directory ();
2474 pretty_print_job (job, JLIST_STANDARD, stderr);
2475 if (dir && (strcmp (dir, jobs[job]->wd) != 0))
2477 "(wd now: %s)\n", polite_directory_format (dir));
2478 jobs[job]->flags |= J_NOTIFIED;
2486 programming_error ("notify_of_job_status");
2490 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2493 /* Initialize the job control mechanism, and set up the tty stuff. */
2495 initialize_job_control (force)
2498 shell_pgrp = getpgid (0);
2500 if (shell_pgrp == -1)
2502 sys_error ("initialize_job_control: getpgrp failed");
2506 /* We can only have job control if we are interactive. */
2507 if (interactive == 0)
2510 original_pgrp = NO_PID;
2511 shell_tty = fileno (stderr);
2515 /* Get our controlling terminal. If job_control is set, or
2516 interactive is set, then this is an interactive shell no
2517 matter where fd 2 is directed. */
2518 shell_tty = dup (fileno (stderr)); /* fd 2 */
2520 shell_tty = move_to_high_fd (shell_tty, 1, -1);
2522 /* Compensate for a bug in systems that compiled the BSD
2523 rlogind with DEBUG defined, like NeXT and Alliant. */
2524 if (shell_pgrp == 0)
2526 shell_pgrp = getpid ();
2527 setpgid (0, shell_pgrp);
2528 tcsetpgrp (shell_tty, shell_pgrp);
2531 while ((terminal_pgrp = tcgetpgrp (shell_tty)) != -1)
2533 if (shell_pgrp != terminal_pgrp)
2535 SigHandler *old_ttin;
2537 old_ttin = set_signal_handler(SIGTTIN, SIG_DFL);
2539 set_signal_handler (SIGTTIN, old_ttin);
2545 /* Make sure that we are using the new line discipline. */
2546 if (set_new_line_discipline (shell_tty) < 0)
2548 sys_error ("initialize_job_control: line discipline");
2553 original_pgrp = shell_pgrp;
2554 shell_pgrp = getpid ();
2556 if ((original_pgrp != shell_pgrp) && (setpgid (0, shell_pgrp) < 0))
2558 sys_error ("initialize_job_control: setpgid");
2559 shell_pgrp = original_pgrp;
2564 /* If (and only if) we just set our process group to our pid,
2565 thereby becoming a process group leader, and the terminal
2566 is not in the same process group as our (new) process group,
2567 then set the terminal's process group to our (new) process
2568 group. If that fails, set our process group back to what it
2569 was originally (so we can still read from the terminal) and
2570 turn off job control. */
2571 if (shell_pgrp != original_pgrp && shell_pgrp != terminal_pgrp)
2573 if (give_terminal_to (shell_pgrp) < 0) /* XXX */
2575 setpgid (0, original_pgrp); /* XXX */
2576 shell_pgrp = original_pgrp; /* XXX */
2577 job_control = 0; /* XXX */
2581 if (job_control == 0)
2582 internal_error ("no job control in this shell"); /* XXX */
2585 if (shell_tty != fileno (stderr))
2586 SET_CLOSE_ON_EXEC (shell_tty);
2588 set_signal_handler (SIGCHLD, sigchld_handler);
2590 change_flag ('m', job_control ? '-' : '+');
2598 /* Set the line discipline to the best this system has to offer.
2599 Return -1 if this is not possible. */
2601 set_new_line_discipline (tty)
2604 #if defined (NEW_TTY_DRIVER)
2607 if (ioctl (tty, TIOCGETD, &ldisc) < 0)
2610 if (ldisc != NTTYDISC)
2614 if (ioctl (tty, TIOCSETD, &ldisc) < 0)
2618 #endif /* NEW_TTY_DRIVER */
2620 #if defined (TERMIO_TTY_DRIVER)
2621 # if defined (TERMIO_LDISC) && (NTTYDISC)
2622 if (ioctl (tty, TCGETA, &shell_tty_info) < 0)
2625 if (shell_tty_info.c_line != NTTYDISC)
2627 shell_tty_info.c_line = NTTYDISC;
2628 if (ioctl (tty, TCSETAW, &shell_tty_info) < 0)
2631 # endif /* TERMIO_LDISC && NTTYDISC */
2633 #endif /* TERMIO_TTY_DRIVER */
2635 #if defined (TERMIOS_TTY_DRIVER)
2636 # if defined (TERMIOS_LDISC) && defined (NTTYDISC)
2637 if (tcgetattr (tty, &shell_tty_info) < 0)
2640 if (shell_tty_info.c_line != NTTYDISC)
2642 shell_tty_info.c_line = NTTYDISC;
2643 if (tcsetattr (tty, TCSADRAIN, &shell_tty_info) < 0)
2646 # endif /* TERMIOS_LDISC && NTTYDISC */
2648 #endif /* TERMIOS_TTY_DRIVER */
2650 #if !defined (NEW_TTY_DRIVER) && !defined (TERMIO_TTY_DRIVER) && !defined (TERMIOS_TTY_DRIVER)
2655 static SigHandler *old_tstp, *old_ttou, *old_ttin;
2656 static SigHandler *old_cont = (SigHandler *)SIG_DFL;
2657 static sighandler stop_signal_handler (), cont_signal_handler ();
2659 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
2660 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
2663 get_new_window_size (from_sig)
2668 if ((ioctl (shell_tty, TIOCGWINSZ, &win) == 0) &&
2669 win.ws_row > 0 && win.ws_col > 0)
2672 shell_tty_info.c_winsize = win; /* structure copying */
2674 set_lines_and_columns (win.ws_row, win.ws_col);
2675 #if defined (READLINE)
2676 _rl_set_screen_size (win.ws_row, win.ws_col);
2682 sigwinch_sighandler (sig)
2685 #if defined (MUST_REINSTALL_SIGHANDLERS)
2686 set_signal_handler (SIGWINCH, sigwinch_sighandler);
2687 #endif /* MUST_REINSTALL_SIGHANDLERS */
2688 get_new_window_size (1);
2693 get_new_window_size (from_sig)
2697 #endif /* TIOCGWINSZ && SIGWINCH */
2700 set_sigwinch_handler ()
2702 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
2703 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
2708 unset_sigwinch_handler ()
2710 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
2711 set_signal_handler (SIGWINCH, old_winch);
2715 /* Setup this shell to handle C-C, etc. */
2717 initialize_job_signals ()
2721 set_signal_handler (SIGINT, sigint_sighandler);
2722 set_signal_handler (SIGTSTP, SIG_IGN);
2723 set_signal_handler (SIGTTOU, SIG_IGN);
2724 set_signal_handler (SIGTTIN, SIG_IGN);
2725 set_sigwinch_handler ();
2727 else if (job_control)
2729 old_tstp = set_signal_handler (SIGTSTP, stop_signal_handler);
2730 old_ttou = set_signal_handler (SIGTTOU, stop_signal_handler);
2731 old_ttin = set_signal_handler (SIGTTIN, stop_signal_handler);
2733 /* Leave these things alone for non-interactive shells without job
2737 /* Here we handle CONT signals. */
2739 cont_signal_handler (sig)
2742 initialize_job_signals ();
2743 set_signal_handler (SIGCONT, old_cont);
2744 kill (getpid (), SIGCONT);
2749 /* Here we handle stop signals while we are running not as a login shell. */
2751 stop_signal_handler (sig)
2754 set_signal_handler (SIGTSTP, old_tstp);
2755 set_signal_handler (SIGTTOU, old_ttou);
2756 set_signal_handler (SIGTTIN, old_ttin);
2758 old_cont = set_signal_handler (SIGCONT, cont_signal_handler);
2760 give_terminal_to (shell_pgrp);
2762 kill (getpid (), sig);
2767 /* Give the terminal to PGRP. */
2769 give_terminal_to (pgrp)
2779 sigaddset (&set, SIGTTOU);
2780 sigaddset (&set, SIGTTIN);
2781 sigaddset (&set, SIGTSTP);
2782 sigaddset (&set, SIGCHLD);
2783 sigemptyset (&oset);
2784 sigprocmask (SIG_BLOCK, &set, &oset);
2786 if (tcsetpgrp (shell_tty, pgrp) < 0)
2788 /* Maybe we should print an error message? */
2790 sys_error ("tcsetpgrp(%d) failed: pid %d to pgrp %d",
2791 shell_tty, getpid(), pgrp);
2796 terminal_pgrp = pgrp;
2797 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2803 /* Clear out any jobs in the job array. This is intended to be used by
2804 children of the shell, who should not have any job structures as baggage
2805 when they start executing (forking subshells for parenthesized execution
2806 and functions with pipes are the two that spring to mind). */
2813 BLOCK_CHILD (set, oset);
2817 current_job = previous_job = NO_JOB;
2819 for (i = 0; i < job_slots; i++)
2823 free ((char *)jobs);
2827 UNBLOCK_CHILD (oset);
2830 /* Mark all jobs in the job array so that they don't get a SIGHUP when the
2838 BLOCK_CHILD (set, oset);
2842 for (i = 0; i < job_slots; i++)
2847 UNBLOCK_CHILD (oset);
2850 /* Mark all dead jobs as notified, so delete_job () cleans them out
2851 of the job table properly. */
2853 mark_dead_jobs_as_notified ()
2860 BLOCK_CHILD (set, oset);
2862 for (i = 0; i < job_slots; i++)
2863 if (jobs[i] && DEADJOB (i))
2864 jobs[i]->flags |= J_NOTIFIED;
2866 UNBLOCK_CHILD (oset);
2870 /* Here to allow other parts of the shell (like the trap stuff) to
2871 unfreeze the jobs list. */
2873 unfreeze_jobs_list ()
2875 jobs_list_frozen = 0;
2878 /* Allow or disallow job control to take place. Returns the old value
2881 set_job_control (arg)
2891 /* Turn off all traces of job control. This is run by children of the shell
2892 which are going to do shellsy things, like wait (), etc. */
2894 without_job_control ()
2896 stop_making_children ();
2899 set_job_control (0);
2902 /* If this shell is interactive, terminate all stopped jobs and
2903 restore the original terminal process group. This is done
2904 before the `exec' builtin calls shell_execve. */
2908 if (interactive_shell) /* XXX - should it be interactive? */
2910 terminate_stopped_jobs ();
2912 if (original_pgrp >= 0)
2913 give_terminal_to (original_pgrp);
2916 if (original_pgrp >= 0)
2917 setpgid (0, original_pgrp);
2920 /* Restart job control by closing shell tty and reinitializing. This is
2921 called after an exec fails in an interactive shell and we do not exit. */
2923 restart_job_control ()
2925 if (shell_tty != -1)
2927 initialize_job_control (0);
2930 /* Set the handler to run when the shell receives a SIGCHLD signal. */
2932 set_sigchld_handler ()
2934 set_signal_handler (SIGCHLD, sigchld_handler);
2937 #if defined (PGRP_PIPE)
2938 /* Read from the read end of a pipe. This is how the process group leader
2939 blocks until all of the processes in a pipeline have been made. */
2954 while (read (pp[0], &ch, 1) == -1 && errno == EINTR)
2959 /* Close the read and write ends of PP, an array of file descriptors. */
2973 /* Functional interface closes our local-to-job-control pipes. */
2977 pipe_close (pgrp_pipe);
2980 #endif /* PGRP_PIPE */
2986 #if defined (ARRAY_VARS)
2988 register PROCESS *p;
2990 for (i = 1, p = jobs[j]->pipe; p->next != jobs[j]->pipe; p = p->next, i++)
2995 pstatuses = (int *)xrealloc (pstatuses, i * sizeof (int));
3002 pstatuses[i++] = process_exit_status (p->status);
3005 while (p != jobs[j]->pipe);
3007 pstatuses[i] = -1; /* sentinel */
3008 set_pipestatus_array (pstatuses);
3012 #if defined (ARRAY_VARS)
3014 set_pipestatus_array (ps)
3022 v = find_variable ("PIPESTATUS");
3024 v = make_new_array_variable ("PIPESTATUS");
3025 if (array_p (v) == 0)
3026 return; /* Do nothing if not an array variable. */
3030 for (i = 0; ps[i] != -1; i++)
3033 array_add_element (a, i, t);