1 /* redir.c -- Functions to perform input and output redirection. */
3 /* Copyright (C) 1997-2015 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
25 #endif /* _AIX && RISC6000 && !__GNUC__ */
28 #include "bashtypes.h"
29 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
30 # include <sys/file.h>
33 #include "posixstat.h"
35 #if defined (HAVE_UNISTD_H)
49 #define NEED_FPURGE_DECL
53 #include "execute_cmd.h"
57 #if defined (BUFFERED_INPUT)
61 #define SHELL_FD_BASE 10
65 extern int posixly_correct
;
66 extern int last_command_exit_value
;
67 extern int executing_builtin
;
68 extern REDIRECT
*redirection_undo_list
;
69 extern REDIRECT
*exec_redirection_undo_list
;
71 /* Static functions defined and used in this file. */
72 static void add_exec_redirect
__P((REDIRECT
*));
73 static int add_undo_redirect
__P((int, enum r_instruction
, int));
74 static int add_undo_close_redirect
__P((int));
75 static int expandable_redirection_filename
__P((REDIRECT
*));
76 static int stdin_redirection
__P((enum r_instruction
, int));
77 static int undoablefd
__P((int));
78 static int do_redirection_internal
__P((REDIRECT
*, int));
80 static int write_here_document
__P((int, WORD_DESC
*));
81 static int write_here_string
__P((int, WORD_DESC
*));
82 static int here_document_to_fd
__P((WORD_DESC
*, enum r_instruction
));
84 static int redir_special_open
__P((int, char *, int, int, enum r_instruction
));
85 static int noclobber_open
__P((char *, int, int, enum r_instruction
));
86 static int redir_open
__P((char *, int, int, enum r_instruction
));
88 static int redir_varassign
__P((REDIRECT
*, int));
89 static int redir_varvalue
__P((REDIRECT
*));
91 /* Spare redirector used when translating [N]>&WORD[-] or [N]<&WORD[-] to
92 a new redirection and when creating the redirection undo list. */
95 /* Set to errno when a here document cannot be created for some reason.
96 Used to print a reasonable error message. */
97 static int heredoc_errno
;
99 #define REDIRECTION_ERROR(r, e, fd) \
105 last_command_exit_value = EXECUTION_FAILURE;\
106 return ((e) == 0 ? EINVAL : (e));\
111 redirection_error (temp
, error
)
115 char *filename
, *allocname
;
119 if ((temp
->rflags
& REDIR_VARASSIGN
) && error
< 0)
120 filename
= allocname
= savestring (temp
->redirector
.filename
->word
);
121 else if ((temp
->rflags
& REDIR_VARASSIGN
) == 0 && temp
->redirector
.dest
< 0)
122 /* This can happen when read_token_word encounters overflow, like in
124 filename
= _("file descriptor out of range");
126 /* This error can never involve NOCLOBBER */
127 else if (error
!= NOCLOBBER_REDIRECT
&& temp
->redirector
.dest
>= 0 && error
== EBADF
)
129 /* If we're dealing with two file descriptors, we have to guess about
130 which one is invalid; in the cases of r_{duplicating,move}_input and
131 r_{duplicating,move}_output we're here because dup2() failed. */
132 switch (temp
->instruction
)
134 case r_duplicating_input
:
135 case r_duplicating_output
:
138 filename
= allocname
= itos (temp
->redirectee
.dest
);
140 case r_duplicating_input_word
:
141 if (temp
->redirector
.dest
== 0) /* Guess */
142 filename
= temp
->redirectee
.filename
->word
; /* XXX */
144 filename
= allocname
= itos (temp
->redirector
.dest
);
146 case r_duplicating_output_word
:
147 if (temp
->redirector
.dest
== 1) /* Guess */
148 filename
= temp
->redirectee
.filename
->word
; /* XXX */
150 filename
= allocname
= itos (temp
->redirector
.dest
);
153 filename
= allocname
= itos (temp
->redirector
.dest
);
158 else if (expandable_redirection_filename (temp
))
161 oflags
= temp
->redirectee
.filename
->flags
;
162 if (posixly_correct
&& interactive_shell
== 0)
163 temp
->redirectee
.filename
->flags
|= W_NOGLOB
;
164 temp
->redirectee
.filename
->flags
|= W_NOCOMSUB
;
165 filename
= allocname
= redirection_expand (temp
->redirectee
.filename
);
166 temp
->redirectee
.filename
->flags
= oflags
;
168 filename
= temp
->redirectee
.filename
->word
;
170 else if (temp
->redirectee
.dest
< 0)
171 filename
= _("file descriptor out of range");
173 filename
= allocname
= itos (temp
->redirectee
.dest
);
177 case AMBIGUOUS_REDIRECT
:
178 internal_error (_("%s: ambiguous redirect"), filename
);
181 case NOCLOBBER_REDIRECT
:
182 internal_error (_("%s: cannot overwrite existing file"), filename
);
185 #if defined (RESTRICTED_SHELL)
186 case RESTRICTED_REDIRECT
:
187 internal_error (_("%s: restricted: cannot redirect output"), filename
);
189 #endif /* RESTRICTED_SHELL */
191 case HEREDOC_REDIRECT
:
192 internal_error (_("cannot create temp file for here-document: %s"), strerror (heredoc_errno
));
195 case BADVAR_REDIRECT
:
196 internal_error (_("%s: cannot assign fd to variable"), filename
);
200 internal_error ("%s: %s", filename
, strerror (error
));
207 /* Perform the redirections on LIST. If flags & RX_ACTIVE, then actually
208 make input and output file descriptors, otherwise just do whatever is
209 necessary for side effecting. flags & RX_UNDOABLE says to remember
210 how to undo the redirections later, if non-zero. If flags & RX_CLEXEC
211 is non-zero, file descriptors opened in do_redirection () have their
212 close-on-exec flag set. */
214 do_redirections (list
, flags
)
221 if (flags
& RX_UNDOABLE
)
223 if (redirection_undo_list
)
225 dispose_redirects (redirection_undo_list
);
226 redirection_undo_list
= (REDIRECT
*)NULL
;
228 if (exec_redirection_undo_list
)
229 dispose_exec_redirects ();
232 for (temp
= list
; temp
; temp
= temp
->next
)
234 error
= do_redirection_internal (temp
, flags
);
237 redirection_error (temp
, error
);
244 /* Return non-zero if the redirection pointed to by REDIRECT has a
245 redirectee.filename that can be expanded. */
247 expandable_redirection_filename (redirect
)
250 switch (redirect
->instruction
)
252 case r_output_direction
:
254 case r_input_direction
:
255 case r_inputa_direction
:
257 case r_append_err_and_out
:
260 case r_duplicating_input_word
:
261 case r_duplicating_output_word
:
262 case r_move_input_word
:
263 case r_move_output_word
:
271 /* Expand the word in WORD returning a string. If WORD expands to
272 multiple words (or no words), then return NULL. */
274 redirection_expand (word
)
278 WORD_LIST
*tlist1
, *tlist2
;
282 w
= copy_word (word
);
284 w
->flags
|= W_NOSPLIT
;
286 tlist1
= make_word_list (w
, (WORD_LIST
*)NULL
);
288 /* Now that we've changed the variable search order to ignore the temp
289 environment, see if we need to change the cached IFS values. */
291 tlist2
= expand_words_no_vars (tlist1
);
293 /* Now we need to change the variable search order back to include the temp
294 environment. We force the temp environment search by forcing
295 executing_builtin to 1. This is what makes `read' get the right values
296 for the IFS-related cached variables, for example. */
297 old
= executing_builtin
;
298 executing_builtin
= 1;
300 executing_builtin
= old
;
301 dispose_words (tlist1
);
303 if (tlist2
== 0 || tlist2
->next
)
305 /* We expanded to no words, or to more than a single word.
306 Dispose of the word list and return NULL. */
308 dispose_words (tlist2
);
309 return ((char *)NULL
);
311 result
= string_list (tlist2
); /* XXX savestring (tlist2->word->word)? */
312 dispose_words (tlist2
);
317 write_here_string (fd
, redirectee
)
319 WORD_DESC
*redirectee
;
322 int herelen
, n
, e
, old
;
325 /* Now that we've changed the variable search order to ignore the temp
326 environment, see if we need to change the cached IFS values. */
328 herestr
= expand_string_unsplit_to_string (redirectee
->word
, 0);
330 /* Now we need to change the variable search order back to include the temp
331 environment. We force the temp environment search by forcing
332 executing_builtin to 1. This is what makes `read' get the right values
333 for the IFS-related cached variables, for example. */
334 old
= executing_builtin
;
335 executing_builtin
= 1;
337 executing_builtin
= old
;
339 herelen
= STRLEN (herestr
);
341 n
= write (fd
, herestr
, herelen
);
344 n
= write (fd
, "\n", 1);
358 /* Write the text of the here document pointed to by REDIRECTEE to the file
359 descriptor FD, which is already open to a temp file. Return 0 if the
360 write is successful, otherwise return errno. */
362 write_here_document (fd
, redirectee
)
364 WORD_DESC
*redirectee
;
367 int document_len
, fd2
, old
;
369 register WORD_LIST
*t
, *tlist
;
371 /* Expand the text if the word that was specified had
372 no quoting. The text that we expand is treated
373 exactly as if it were surrounded by double quotes. */
375 if (redirectee
->flags
& W_QUOTED
)
377 document
= redirectee
->word
;
378 document_len
= strlen (document
);
379 /* Set errno to something reasonable if the write fails. */
380 if (write (fd
, document
, document_len
) < document_len
)
391 /* Now that we've changed the variable search order to ignore the temp
392 environment, see if we need to change the cached IFS values. */
394 tlist
= expand_string (redirectee
->word
, Q_HERE_DOCUMENT
);
396 /* Now we need to change the variable search order back to include the temp
397 environment. We force the temp environment search by forcing
398 executing_builtin to 1. This is what makes `read' get the right values
399 for the IFS-related cached variables, for example. */
400 old
= executing_builtin
;
401 executing_builtin
= 1;
403 executing_builtin
= old
;
407 /* Try using buffered I/O (stdio) and writing a word
408 at a time, letting stdio do the work of buffering
409 for us rather than managing our own strings. Most
410 stdios are not particularly fast, however -- this
411 may need to be reconsidered later. */
412 if ((fd2
= dup (fd
)) < 0 || (fp
= fdopen (fd2
, "w")) == NULL
)
417 dispose_words (tlist
);
422 for (t
= tlist
; t
; t
= t
->next
)
424 /* This is essentially the body of
425 string_list_internal expanded inline. */
426 document
= t
->word
->word
;
427 document_len
= strlen (document
);
429 putc (' ', fp
); /* separator */
430 fwrite (document
, document_len
, 1, fp
);
437 dispose_words (tlist
);
441 dispose_words (tlist
);
442 if (fclose (fp
) != 0)
452 /* Create a temporary file holding the text of the here document pointed to
453 by REDIRECTEE, and return a file descriptor open for reading to the temp
454 file. Return -1 on any error, and make sure errno is set appropriately. */
456 here_document_to_fd (redirectee
, ri
)
457 WORD_DESC
*redirectee
;
458 enum r_instruction ri
;
463 fd
= sh_mktmpfd ("sh-thd", MT_USERANDOM
|MT_USETMPDIR
, &filename
);
465 /* If we failed for some reason other than the file existing, abort */
472 SET_CLOSE_ON_EXEC (fd
);
474 errno
= r
= 0; /* XXX */
475 /* write_here_document returns 0 on success, errno on failure. */
476 if (redirectee
->word
)
477 r
= (ri
!= r_reading_string
) ? write_here_document (fd
, redirectee
)
478 : write_here_string (fd
, redirectee
);
489 /* In an attempt to avoid races, we close the first fd only after opening
491 /* Make the document really temporary. Also make it the input. */
492 fd2
= open (filename
, O_RDONLY
|O_BINARY
, 0600);
505 if (unlink (filename
) < 0)
519 #define RF_DEVSTDERR 2
520 #define RF_DEVSTDIN 3
521 #define RF_DEVSTDOUT 4
525 /* A list of pattern/value pairs for filenames that the redirection
526 code handles specially. */
527 static STRING_INT_ALIST _redir_special_filenames
[] = {
528 #if !defined (HAVE_DEV_FD)
529 { "/dev/fd/[0-9]*", RF_DEVFD
},
531 #if !defined (HAVE_DEV_STDIN)
532 { "/dev/stderr", RF_DEVSTDERR
},
533 { "/dev/stdin", RF_DEVSTDIN
},
534 { "/dev/stdout", RF_DEVSTDOUT
},
536 #if defined (NETWORK_REDIRECTIONS)
537 { "/dev/tcp/*/*", RF_DEVTCP
},
538 { "/dev/udp/*/*", RF_DEVUDP
},
544 redir_special_open (spec
, filename
, flags
, mode
, ri
)
548 enum r_instruction ri
;
551 #if !defined (HAVE_DEV_FD)
558 #if !defined (HAVE_DEV_FD)
560 if (all_digits (filename
+8) && legal_number (filename
+8, &lfd
) && lfd
== (int)lfd
)
563 fd
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
);
566 fd
= AMBIGUOUS_REDIRECT
;
570 #if !defined (HAVE_DEV_STDIN)
572 fd
= fcntl (0, F_DUPFD
, SHELL_FD_BASE
);
575 fd
= fcntl (1, F_DUPFD
, SHELL_FD_BASE
);
578 fd
= fcntl (2, F_DUPFD
, SHELL_FD_BASE
);
582 #if defined (NETWORK_REDIRECTIONS)
585 #if defined (HAVE_NETWORK)
586 fd
= netopen (filename
);
588 internal_warning (_("/dev/(tcp|udp)/host/port not supported without networking"));
589 fd
= open (filename
, flags
, mode
);
592 #endif /* NETWORK_REDIRECTIONS */
598 /* Open FILENAME with FLAGS in noclobber mode, hopefully avoiding most
599 race conditions and avoiding the problem where the file is replaced
600 between the stat(2) and open(2). */
602 noclobber_open (filename
, flags
, mode
, ri
)
605 enum r_instruction ri
;
608 struct stat finfo
, finfo2
;
610 /* If the file exists and is a regular file, return an error
612 r
= stat (filename
, &finfo
);
613 if (r
== 0 && (S_ISREG (finfo
.st_mode
)))
614 return (NOCLOBBER_REDIRECT
);
616 /* If the file was not present (r != 0), make sure we open it
617 exclusively so that if it is created before we open it, our open
618 will fail. Make sure that we do not truncate an existing file.
619 Note that we don't turn on O_EXCL unless the stat failed -- if
620 the file was not a regular file, we leave O_EXCL off. */
624 fd
= open (filename
, flags
|O_EXCL
, mode
);
625 return ((fd
< 0 && errno
== EEXIST
) ? NOCLOBBER_REDIRECT
: fd
);
627 fd
= open (filename
, flags
, mode
);
629 /* If the open failed, return the file descriptor right away. */
631 return (errno
== EEXIST
? NOCLOBBER_REDIRECT
: fd
);
633 /* OK, the open succeeded, but the file may have been changed from a
634 non-regular file to a regular file between the stat and the open.
635 We are assuming that the O_EXCL open handles the case where FILENAME
636 did not exist and is symlinked to an existing file between the stat
639 /* If we can open it and fstat the file descriptor, and neither check
640 revealed that it was a regular file, and the file has not been replaced,
641 return the file descriptor. */
642 if ((fstat (fd
, &finfo2
) == 0) && (S_ISREG (finfo2
.st_mode
) == 0) &&
643 r
== 0 && (S_ISREG (finfo
.st_mode
) == 0) &&
644 same_file (filename
, filename
, &finfo
, &finfo2
))
647 /* The file has been replaced. badness. */
650 return (NOCLOBBER_REDIRECT
);
654 redir_open (filename
, flags
, mode
, ri
)
657 enum r_instruction ri
;
661 r
= find_string_in_alist (filename
, _redir_special_filenames
, 1);
663 return (redir_special_open (r
, filename
, flags
, mode
, ri
));
665 /* If we are in noclobber mode, you are not allowed to overwrite
666 existing files. Check before opening. */
667 if (noclobber
&& CLOBBERING_REDIRECT (ri
))
669 fd
= noclobber_open (filename
, flags
, mode
, ri
);
670 if (fd
== NOCLOBBER_REDIRECT
)
671 return (NOCLOBBER_REDIRECT
);
677 fd
= open (filename
, flags
, mode
);
679 if (fd
< 0 && e
== EINTR
)
682 run_pending_traps ();
686 while (fd
< 0 && errno
== EINTR
);
689 if ((fd
< 0) && (errno
== EACCES
))
691 fd
= open (filename
, flags
& ~O_CREAT
, mode
);
692 errno
= EACCES
; /* restore errno */
706 clexec
= fcntl (fd
, F_GETFD
, 0);
707 if (clexec
== -1 || (fd
>= SHELL_FD_BASE
&& clexec
== 1))
712 /* Do the specific redirection requested. Returns errno or one of the
713 special redirection errors (*_REDIRECT) in case of error, 0 on success.
714 If flags & RX_ACTIVE is zero, then just do whatever is necessary to
715 produce the appropriate side effects. flags & RX_UNDOABLE, if non-zero,
716 says to remember how to undo each redirection. If flags & RX_CLEXEC is
717 non-zero, then we set all file descriptors > 2 that we open to be
720 do_redirection_internal (redirect
, flags
)
724 WORD_DESC
*redirectee
;
725 int redir_fd
, fd
, redirector
, r
, oflags
;
727 char *redirectee_word
;
728 enum r_instruction ri
;
729 REDIRECT
*new_redirect
;
732 redirectee
= redirect
->redirectee
.filename
;
733 redir_fd
= redirect
->redirectee
.dest
;
734 redirector
= redirect
->redirector
.dest
;
735 ri
= redirect
->instruction
;
737 if (redirect
->flags
& RX_INTERNAL
)
738 flags
|= RX_INTERNAL
;
740 if (TRANSLATE_REDIRECT (ri
))
742 /* We have [N]>&WORD[-] or [N]<&WORD[-] (or {V}>&WORD[-] or {V}<&WORD-).
743 and WORD, then translate the redirection into a new one and
745 redirectee_word
= redirection_expand (redirectee
);
747 /* XXX - what to do with [N]<&$w- where w is unset or null? ksh93
749 if (redirectee_word
== 0)
750 return (AMBIGUOUS_REDIRECT
);
751 else if (redirectee_word
[0] == '-' && redirectee_word
[1] == '\0')
753 sd
= redirect
->redirector
;
755 new_redirect
= make_redirection (sd
, r_close_this
, rd
, 0);
757 else if (all_digits (redirectee_word
))
759 sd
= redirect
->redirector
;
760 if (legal_number (redirectee_word
, &lfd
) && (int)lfd
== lfd
)
763 rd
.dest
= -1; /* XXX */
766 case r_duplicating_input_word
:
767 new_redirect
= make_redirection (sd
, r_duplicating_input
, rd
, 0);
769 case r_duplicating_output_word
:
770 new_redirect
= make_redirection (sd
, r_duplicating_output
, rd
, 0);
772 case r_move_input_word
:
773 new_redirect
= make_redirection (sd
, r_move_input
, rd
, 0);
775 case r_move_output_word
:
776 new_redirect
= make_redirection (sd
, r_move_output
, rd
, 0);
780 else if (ri
== r_duplicating_output_word
&& (redirect
->rflags
& REDIR_VARASSIGN
) == 0 && redirector
== 1)
782 sd
= redirect
->redirector
;
783 rd
.filename
= make_bare_word (redirectee_word
);
784 new_redirect
= make_redirection (sd
, r_err_and_out
, rd
, 0);
788 free (redirectee_word
);
789 return (AMBIGUOUS_REDIRECT
);
792 free (redirectee_word
);
794 /* Set up the variables needed by the rest of the function from the
796 if (new_redirect
->instruction
== r_err_and_out
)
800 /* Copy the word without allocating any memory that must be
802 redirectee
= (WORD_DESC
*)alloca (sizeof (WORD_DESC
));
803 xbcopy ((char *)new_redirect
->redirectee
.filename
,
804 (char *)redirectee
, sizeof (WORD_DESC
));
806 alloca_hack
= (char *)
807 alloca (1 + strlen (new_redirect
->redirectee
.filename
->word
));
808 redirectee
->word
= alloca_hack
;
809 strcpy (redirectee
->word
, new_redirect
->redirectee
.filename
->word
);
812 /* It's guaranteed to be an integer, and shouldn't be freed. */
813 redirectee
= new_redirect
->redirectee
.filename
;
815 redir_fd
= new_redirect
->redirectee
.dest
;
816 redirector
= new_redirect
->redirector
.dest
;
817 ri
= new_redirect
->instruction
;
819 /* Overwrite the flags element of the old redirect with the new value. */
820 redirect
->flags
= new_redirect
->flags
;
821 dispose_redirects (new_redirect
);
826 case r_output_direction
:
828 case r_input_direction
:
829 case r_inputa_direction
:
830 case r_err_and_out
: /* command &>filename */
831 case r_append_err_and_out
: /* command &>> filename */
834 if (posixly_correct
&& interactive_shell
== 0)
836 oflags
= redirectee
->flags
;
837 redirectee
->flags
|= W_NOGLOB
;
839 redirectee_word
= redirection_expand (redirectee
);
840 if (posixly_correct
&& interactive_shell
== 0)
841 redirectee
->flags
= oflags
;
843 if (redirectee_word
== 0)
844 return (AMBIGUOUS_REDIRECT
);
846 #if defined (RESTRICTED_SHELL)
847 if (restricted
&& (WRITE_REDIRECT (ri
)))
849 free (redirectee_word
);
850 return (RESTRICTED_REDIRECT
);
852 #endif /* RESTRICTED_SHELL */
854 fd
= redir_open (redirectee_word
, redirect
->flags
, 0666, ri
);
855 free (redirectee_word
);
857 if (fd
== NOCLOBBER_REDIRECT
)
863 if (flags
& RX_ACTIVE
)
865 if (redirect
->rflags
& REDIR_VARASSIGN
)
867 redirector
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
); /* XXX try this for now */
870 sys_error (_("redirection error: cannot duplicate fd"));
871 REDIRECTION_ERROR (redirector
, r
, fd
);
874 if ((flags
& RX_UNDOABLE
) && (redirect
->rflags
& REDIR_VARASSIGN
) == 0)
876 /* Only setup to undo it if the thing to undo is active. */
877 if ((fd
!= redirector
) && (fcntl (redirector
, F_GETFD
, 0) != -1))
878 r
= add_undo_redirect (redirector
, ri
, -1);
880 r
= add_undo_close_redirect (redirector
);
881 REDIRECTION_ERROR (r
, errno
, fd
);
884 #if defined (BUFFERED_INPUT)
885 /* inhibit call to sync_buffered_stream() for async processes */
886 if (redirector
!= 0 || (subshell_environment
& SUBSHELL_ASYNC
) == 0)
887 check_bash_input (redirector
);
890 /* Make sure there is no pending output before we change the state
891 of the underlying file descriptor, since the builtins use stdio
893 if (redirector
== 1 && fileno (stdout
) == redirector
)
898 else if (redirector
== 2 && fileno (stderr
) == redirector
)
904 if (redirect
->rflags
& REDIR_VARASSIGN
)
906 if ((r
= redir_varassign (redirect
, redirector
)) < 0)
910 return (r
); /* XXX */
913 else if ((fd
!= redirector
) && (dup2 (fd
, redirector
) < 0))
915 close (fd
); /* dup2 failed? must be fd limit issue */
919 #if defined (BUFFERED_INPUT)
920 /* Do not change the buffered stream for an implicit redirection
921 of /dev/null to fd 0 for asynchronous commands without job
922 control (r_inputa_direction). */
923 if (ri
== r_input_direction
|| ri
== r_input_output
)
924 duplicate_buffered_stream (fd
, redirector
);
925 #endif /* BUFFERED_INPUT */
928 * If we're remembering, then this is the result of a while, for
929 * or until loop with a loop redirection, or a function/builtin
930 * executing in the parent shell with a redirection. In the
931 * function/builtin case, we want to set all file descriptors > 2
932 * to be close-on-exec to duplicate the effect of the old
933 * for i = 3 to NOFILE close(i) loop. In the case of the loops,
934 * both sh and ksh leave the file descriptors open across execs.
935 * The Posix standard mentions only the exec builtin.
937 if ((flags
& RX_CLEXEC
) && (redirector
> 2))
938 SET_CLOSE_ON_EXEC (redirector
);
941 if (fd
!= redirector
)
943 #if defined (BUFFERED_INPUT)
944 if (INPUT_REDIRECT (ri
))
945 close_buffered_fd (fd
);
947 #endif /* !BUFFERED_INPUT */
948 close (fd
); /* Don't close what we just opened! */
951 /* If we are hacking both stdout and stderr, do the stderr
952 redirection here. XXX - handle {var} here? */
953 if (ri
== r_err_and_out
|| ri
== r_append_err_and_out
)
955 if (flags
& RX_ACTIVE
)
957 if (flags
& RX_UNDOABLE
)
958 add_undo_redirect (2, ri
, -1);
965 case r_reading_until
:
966 case r_deblank_reading_until
:
967 case r_reading_string
:
968 /* REDIRECTEE is a pointer to a WORD_DESC containing the text of
969 the new input. Place it in a temporary file. */
972 fd
= here_document_to_fd (redirectee
, ri
);
976 heredoc_errno
= errno
;
977 return (HEREDOC_REDIRECT
);
980 if (redirect
->rflags
& REDIR_VARASSIGN
)
982 redirector
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
); /* XXX try this for now */
985 sys_error (_("redirection error: cannot duplicate fd"));
986 REDIRECTION_ERROR (redirector
, r
, fd
);
989 if (flags
& RX_ACTIVE
)
991 if ((flags
& RX_UNDOABLE
) && (redirect
->rflags
& REDIR_VARASSIGN
) == 0)
993 /* Only setup to undo it if the thing to undo is active. */
994 if ((fd
!= redirector
) && (fcntl (redirector
, F_GETFD
, 0) != -1))
995 r
= add_undo_redirect (redirector
, ri
, -1);
997 r
= add_undo_close_redirect (redirector
);
998 REDIRECTION_ERROR (r
, errno
, fd
);
1001 #if defined (BUFFERED_INPUT)
1002 check_bash_input (redirector
);
1004 if (redirect
->rflags
& REDIR_VARASSIGN
)
1006 if ((r
= redir_varassign (redirect
, redirector
)) < 0)
1010 return (r
); /* XXX */
1013 else if (fd
!= redirector
&& dup2 (fd
, redirector
) < 0)
1020 #if defined (BUFFERED_INPUT)
1021 duplicate_buffered_stream (fd
, redirector
);
1024 if ((flags
& RX_CLEXEC
) && (redirector
> 2))
1025 SET_CLOSE_ON_EXEC (redirector
);
1028 if (fd
!= redirector
)
1029 #if defined (BUFFERED_INPUT)
1030 close_buffered_fd (fd
);
1037 case r_duplicating_input
:
1038 case r_duplicating_output
:
1041 if ((flags
& RX_ACTIVE
) && (redirect
->rflags
& REDIR_VARASSIGN
))
1043 redirector
= fcntl (redir_fd
, F_DUPFD
, SHELL_FD_BASE
); /* XXX try this for now */
1046 sys_error (_("redirection error: cannot duplicate fd"));
1047 REDIRECTION_ERROR (redirector
, r
, -1);
1050 if ((flags
& RX_ACTIVE
) && (redir_fd
!= redirector
))
1052 if ((flags
& RX_UNDOABLE
) && (redirect
->rflags
& REDIR_VARASSIGN
) == 0)
1054 /* Only setup to undo it if the thing to undo is active. */
1055 if (fcntl (redirector
, F_GETFD
, 0) != -1)
1056 r
= add_undo_redirect (redirector
, ri
, redir_fd
);
1058 r
= add_undo_close_redirect (redirector
);
1059 REDIRECTION_ERROR (r
, errno
, -1);
1061 if ((flags
& RX_UNDOABLE
) && (ri
== r_move_input
|| ri
== r_move_output
))
1063 /* r_move_input and r_move_output add an additional close()
1064 that needs to be undone */
1065 if (fcntl (redirector
, F_GETFD
, 0) != -1)
1067 r
= add_undo_redirect (redir_fd
, r_close_this
, -1);
1068 REDIRECTION_ERROR (r
, errno
, -1);
1071 #if defined (BUFFERED_INPUT)
1072 /* inhibit call to sync_buffered_stream() for async processes */
1073 if (redirector
!= 0 || (subshell_environment
& SUBSHELL_ASYNC
) == 0)
1074 check_bash_input (redirector
);
1076 if (redirect
->rflags
& REDIR_VARASSIGN
)
1078 if ((r
= redir_varassign (redirect
, redirector
)) < 0)
1081 return (r
); /* XXX */
1084 /* This is correct. 2>&1 means dup2 (1, 2); */
1085 else if (dup2 (redir_fd
, redirector
) < 0)
1088 #if defined (BUFFERED_INPUT)
1089 if (ri
== r_duplicating_input
|| ri
== r_move_input
)
1090 duplicate_buffered_stream (redir_fd
, redirector
);
1091 #endif /* BUFFERED_INPUT */
1093 /* First duplicate the close-on-exec state of redirectee. dup2
1094 leaves the flag unset on the new descriptor, which means it
1095 stays open. Only set the close-on-exec bit for file descriptors
1096 greater than 2 in any case, since 0-2 should always be open
1097 unless closed by something like `exec 2<&-'. It should always
1098 be safe to set fds > 2 to close-on-exec if they're being used to
1099 save file descriptors < 2, since we don't need to preserve the
1100 state of the close-on-exec flag for those fds -- they should
1102 /* if ((already_set || set_unconditionally) && (ok_to_set))
1105 if (((fcntl (redir_fd
, F_GETFD
, 0) == 1) || redir_fd
< 2 || (flags
& RX_CLEXEC
)) &&
1108 if (((fcntl (redir_fd
, F_GETFD
, 0) == 1) || (redir_fd
< 2 && (flags
& RX_INTERNAL
)) || (flags
& RX_CLEXEC
)) &&
1111 SET_CLOSE_ON_EXEC (redirector
);
1113 /* When undoing saving of non-standard file descriptors (>=3) using
1114 file descriptors >= SHELL_FD_BASE, we set the saving fd to be
1115 close-on-exec and use a flag to decide how to set close-on-exec
1116 when the fd is restored. */
1117 if ((redirect
->flags
& RX_INTERNAL
) && (redirect
->flags
& RX_SAVCLEXEC
) && redirector
>= 3 && (redir_fd
>= SHELL_FD_BASE
|| (redirect
->flags
& RX_SAVEFD
)))
1118 SET_OPEN_ON_EXEC (redirector
);
1120 /* dup-and-close redirection */
1121 if (ri
== r_move_input
|| ri
== r_move_output
)
1123 xtrace_fdchk (redir_fd
);
1126 #if defined (COPROCESS_SUPPORT)
1127 coproc_fdchk (redir_fd
); /* XXX - loses coproc fds */
1134 if (flags
& RX_ACTIVE
)
1136 if (redirect
->rflags
& REDIR_VARASSIGN
)
1138 redirector
= redir_varvalue (redirect
);
1140 return AMBIGUOUS_REDIRECT
;
1144 /* XXX - only if REDIR_VARASSIGN not set? */
1145 if ((flags
& RX_UNDOABLE
) && (fcntl (redirector
, F_GETFD
, 0) != -1))
1147 r
= add_undo_redirect (redirector
, ri
, -1);
1148 REDIRECTION_ERROR (r
, errno
, redirector
);
1151 #if defined (COPROCESS_SUPPORT)
1152 coproc_fdchk (redirector
);
1154 xtrace_fdchk (redirector
);
1156 #if defined (BUFFERED_INPUT)
1157 /* inhibit call to sync_buffered_stream() for async processes */
1158 if (redirector
!= 0 || (subshell_environment
& SUBSHELL_ASYNC
) == 0)
1159 check_bash_input (redirector
);
1160 r
= close_buffered_fd (redirector
);
1161 #else /* !BUFFERED_INPUT */
1162 r
= close (redirector
);
1163 #endif /* !BUFFERED_INPUT */
1165 if (r
< 0 && (flags
& RX_INTERNAL
) && (errno
== EIO
|| errno
== ENOSPC
))
1166 REDIRECTION_ERROR (r
, errno
, -1);
1170 case r_duplicating_input_word
:
1171 case r_duplicating_output_word
:
1177 /* Remember the file descriptor associated with the slot FD,
1178 on REDIRECTION_UNDO_LIST. Note that the list will be reversed
1179 before it is executed. Any redirections that need to be undone
1180 even if REDIRECTION_UNDO_LIST is discarded by the exec builtin
1181 are also saved on EXEC_REDIRECTION_UNDO_LIST. FDBASE says where to
1182 start the duplicating. If it's less than SHELL_FD_BASE, we're ok,
1183 and can use SHELL_FD_BASE (-1 == don't care). If it's >= SHELL_FD_BASE,
1184 we have to make sure we don't use fdbase to save a file descriptor,
1185 since we're going to use it later (e.g., make sure we don't save fd 0
1186 to fd 10 if we have a redirection like 0<&10). If the value of fdbase
1187 puts the process over its fd limit, causing fcntl to fail, we try
1188 again with SHELL_FD_BASE. Return 0 on success, -1 on error. */
1190 add_undo_redirect (fd
, ri
, fdbase
)
1192 enum r_instruction ri
;
1195 int new_fd
, clexec_flag
, savefd_flag
;
1196 REDIRECT
*new_redirect
, *closer
, *dummy_redirect
;
1200 new_fd
= fcntl (fd
, F_DUPFD
, (fdbase
< SHELL_FD_BASE
) ? SHELL_FD_BASE
: fdbase
+1);
1202 new_fd
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
);
1205 new_fd
= fcntl (fd
, F_DUPFD
, 0);
1211 sys_error (_("redirection error: cannot duplicate fd"));
1215 clexec_flag
= fcntl (fd
, F_GETFD
, 0);
1219 closer
= make_redirection (sd
, r_close_this
, rd
, 0);
1220 closer
->flags
|= RX_INTERNAL
;
1221 dummy_redirect
= copy_redirects (closer
);
1226 new_redirect
= make_redirection (sd
, r_duplicating_input
, rd
, 0);
1228 new_redirect
= make_redirection (sd
, r_duplicating_output
, rd
, 0);
1229 new_redirect
->flags
|= RX_INTERNAL
;
1231 new_redirect
->flags
|= RX_SAVEFD
;
1232 if (clexec_flag
== 0 && fd
>= 3 && (new_fd
>= SHELL_FD_BASE
|| savefd_flag
))
1233 new_redirect
->flags
|= RX_SAVCLEXEC
;
1234 new_redirect
->next
= closer
;
1236 closer
->next
= redirection_undo_list
;
1237 redirection_undo_list
= new_redirect
;
1239 /* Save redirections that need to be undone even if the undo list
1240 is thrown away by the `exec' builtin. */
1241 add_exec_redirect (dummy_redirect
);
1243 /* experimental: if we're saving a redirection to undo for a file descriptor
1244 above SHELL_FD_BASE, add a redirection to be undone if the exec builtin
1245 causes redirections to be discarded. There needs to be a difference
1246 between fds that are used to save other fds and then are the target of
1247 user redirections and fds that are just the target of user redirections.
1248 We use the close-on-exec flag to tell the difference; fds > SHELL_FD_BASE
1249 that have the close-on-exec flag set are assumed to be fds used internally
1251 if (fd
>= SHELL_FD_BASE
&& ri
!= r_close_this
&& clexec_flag
)
1255 new_redirect
= make_redirection (sd
, r_duplicating_output
, rd
, 0);
1256 new_redirect
->flags
|= RX_INTERNAL
;
1258 add_exec_redirect (new_redirect
);
1261 /* File descriptors used only for saving others should always be
1262 marked close-on-exec. Unfortunately, we have to preserve the
1263 close-on-exec state of the file descriptor we are saving, since
1264 fcntl (F_DUPFD) sets the new file descriptor to remain open
1265 across execs. If, however, the file descriptor whose state we
1266 are saving is <= 2, we can just set the close-on-exec flag,
1267 because file descriptors 0-2 should always be open-on-exec,
1268 and the restore above in do_redirection() will take care of it. */
1269 if (clexec_flag
|| fd
< 3)
1270 SET_CLOSE_ON_EXEC (new_fd
);
1271 else if (redirection_undo_list
->flags
& RX_SAVCLEXEC
)
1272 SET_CLOSE_ON_EXEC (new_fd
);
1277 /* Set up to close FD when we are finished with the current command
1278 and its redirections. Return 0 on success, -1 on error. */
1280 add_undo_close_redirect (fd
)
1288 closer
= make_redirection (sd
, r_close_this
, rd
, 0);
1289 closer
->flags
|= RX_INTERNAL
;
1290 closer
->next
= redirection_undo_list
;
1291 redirection_undo_list
= closer
;
1297 add_exec_redirect (dummy_redirect
)
1298 REDIRECT
*dummy_redirect
;
1300 dummy_redirect
->next
= exec_redirection_undo_list
;
1301 exec_redirection_undo_list
= dummy_redirect
;
1304 /* Return 1 if the redirection specified by RI and REDIRECTOR alters the
1307 stdin_redirection (ri
, redirector
)
1308 enum r_instruction ri
;
1313 case r_input_direction
:
1314 case r_inputa_direction
:
1315 case r_input_output
:
1316 case r_reading_until
:
1317 case r_deblank_reading_until
:
1318 case r_reading_string
:
1320 case r_duplicating_input
:
1321 case r_duplicating_input_word
:
1323 return (redirector
== 0);
1324 case r_output_direction
:
1325 case r_appending_to
:
1326 case r_duplicating_output
:
1328 case r_append_err_and_out
:
1329 case r_output_force
:
1330 case r_duplicating_output_word
:
1336 /* Return non-zero if any of the redirections in REDIRS alter the standard
1339 stdin_redirects (redirs
)
1345 for (n
= 0, rp
= redirs
; rp
; rp
= rp
->next
)
1346 if ((rp
->rflags
& REDIR_VARASSIGN
) == 0)
1347 n
+= stdin_redirection (rp
->instruction
, rp
->redirector
.dest
);
1350 /* bind_var_to_int handles array references */
1352 redir_varassign (redir
, fd
)
1359 w
= redir
->redirector
.filename
;
1360 v
= bind_var_to_int (w
->word
, fd
);
1361 if (v
== 0 || readonly_p (v
) || noassign_p (v
))
1362 return BADVAR_REDIRECT
;
1364 stupidly_hack_special_variables (w
->word
);
1368 /* Handles {array[ind]} for redirection words */
1370 redir_varvalue (redir
)
1377 #if defined (ARRAY_VARS)
1382 w
= redir
->redirector
.filename
->word
; /* shorthand */
1383 /* XXX - handle set -u here? */
1384 #if defined (ARRAY_VARS)
1385 if (vr
= valid_array_reference (w
, 0))
1387 v
= array_variable_part (w
, &sub
, &len
);
1392 v
= find_variable (w
);
1393 #if defined (ARRAY_VARS)
1396 v
= find_variable_last_nameref (w
, 0);
1397 if (v
&& nameref_p (v
))
1399 w
= nameref_cell (v
);
1400 if (vr
= valid_array_reference (w
, 0))
1401 v
= array_variable_part (w
, &sub
, &len
);
1403 v
= find_variable (w
);
1409 if (v
== 0 || invisible_p (v
))
1412 #if defined (ARRAY_VARS)
1413 /* get_variable_value handles references to array variables without
1415 if (vr
&& (array_p (v
) || assoc_p (v
)))
1416 val
= get_array_value (w
, 0, (int *)NULL
, (arrayind_t
*)0);
1419 val
= get_variable_value (v
);
1420 if (val
== 0 || *val
== 0)
1423 if (legal_number (val
, &vmax
) < 0)
1426 i
= vmax
; /* integer truncation */