1 /* redir.c -- Functions to perform input and output redirection. */
3 /* Copyright (C) 1997-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush 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 Bush 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 Bush. 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 "bushtypes.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 "runner/execute_cmd.h"
57 #if defined (BUFFERED_INPUT)
58 # include "input/input.h"
61 #include "builtins/pipesize.h"
63 /* FreeBSD 13 can reliably handle atomic writes at this capacity without
65 #if __FreeBSD__ && !defined (HEREDOC_PIPESIZE)
66 # define HEREDOC_PIPESIZE 4096
69 /* Normally set by a build process command that computes pipe capacity */
72 # define PIPESIZE PIPE_BUF
74 # define PIPESIZE 4096
78 #ifndef HEREDOC_PIPESIZE
79 # define HEREDOC_PIPESIZE PIPESIZE
82 #if defined (HEREDOC_PIPEMAX)
83 # if HEREDOC_PIPESIZE > HEREDOC_PIPEMAX
84 # define HEREDOC_PIPESIZE HEREDOC_PIPEMAX
88 #define SHELL_FD_BASE 10
92 extern REDIRECT
*redirection_undo_list
;
93 extern REDIRECT
*exec_redirection_undo_list
;
95 /* Static functions defined and used in this file. */
96 static void add_exec_redirect
PARAMS((REDIRECT
*));
97 static int add_undo_redirect
PARAMS((int, enum r_instruction
, int));
98 static int add_undo_close_redirect
PARAMS((int));
99 static int expandable_redirection_filename
PARAMS((REDIRECT
*));
100 static int stdin_redirection
PARAMS((enum r_instruction
, int));
101 static int undoablefd
PARAMS((int));
102 static int do_redirection_internal
PARAMS((REDIRECT
*, int, char **));
104 static char *heredoc_expand
PARAMS((WORD_DESC
*, enum r_instruction
, size_t *));
105 static int heredoc_write
PARAMS((int, char *, size_t));
106 static int here_document_to_fd
PARAMS((WORD_DESC
*, enum r_instruction
));
108 static int redir_special_open
PARAMS((int, char *, int, int, enum r_instruction
));
109 static int noclobber_open
PARAMS((char *, int, int, enum r_instruction
));
110 static int redir_open
PARAMS((char *, int, int, enum r_instruction
));
112 static int redir_varassign
PARAMS((REDIRECT
*, int));
113 static int redir_varvalue
PARAMS((REDIRECT
*));
115 /* Spare redirector used when translating [N]>&WORD[-] or [N]<&WORD[-] to
116 a new redirection and when creating the redirection undo list. */
117 static REDIRECTEE rd
;
119 /* Set to errno when a here document cannot be created for some reason.
120 Used to print a reasonable error message. */
121 static int heredoc_errno
;
123 #define REDIRECTION_ERROR(r, e, fd) \
129 set_exit_status (EXECUTION_FAILURE);\
130 return ((e) == 0 ? EINVAL : (e));\
135 redirection_error (temp
, error
, fn
)
138 char *fn
; /* already-expanded filename */
140 char *filename
, *allocname
;
144 if ((temp
->rflags
& REDIR_VARASSIGN
) && error
< 0)
145 filename
= allocname
= savestring (temp
->redirector
.filename
->word
);
146 else if ((temp
->rflags
& REDIR_VARASSIGN
) == 0 && temp
->redirector
.dest
< 0)
147 /* This can happen when read_token_word encounters overflow, like in
149 filename
= _("file descriptor out of range");
151 /* This error can never involve NOCLOBBER */
152 else if (error
!= NOCLOBBER_REDIRECT
&& temp
->redirector
.dest
>= 0 && error
== EBADF
)
154 /* If we're dealing with two file descriptors, we have to guess about
155 which one is invalid; in the cases of r_{duplicating,move}_input and
156 r_{duplicating,move}_output we're here because dup2() failed. */
157 switch (temp
->instruction
)
159 case r_duplicating_input
:
160 case r_duplicating_output
:
163 filename
= allocname
= itos (temp
->redirectee
.dest
);
165 case r_duplicating_input_word
:
166 if (temp
->redirector
.dest
== 0) /* Guess */
167 filename
= temp
->redirectee
.filename
->word
; /* XXX */
169 filename
= allocname
= itos (temp
->redirector
.dest
);
171 case r_duplicating_output_word
:
172 if (temp
->redirector
.dest
== 1) /* Guess */
173 filename
= temp
->redirectee
.filename
->word
; /* XXX */
175 filename
= allocname
= itos (temp
->redirector
.dest
);
178 filename
= allocname
= itos (temp
->redirector
.dest
);
185 else if (expandable_redirection_filename (temp
))
187 oflags
= temp
->redirectee
.filename
->flags
;
188 if (posixly_correct
&& interactive_shell
== 0)
189 temp
->redirectee
.filename
->flags
|= W_NOGLOB
;
190 temp
->redirectee
.filename
->flags
|= W_NOCOMSUB
;
191 filename
= allocname
= redirection_expand (temp
->redirectee
.filename
);
192 temp
->redirectee
.filename
->flags
= oflags
;
194 filename
= temp
->redirectee
.filename
->word
;
196 else if (temp
->redirectee
.dest
< 0)
197 filename
= _("file descriptor out of range");
199 filename
= allocname
= itos (temp
->redirectee
.dest
);
203 case AMBIGUOUS_REDIRECT
:
204 internal_error (_("%s: ambiguous redirect"), filename
);
207 case NOCLOBBER_REDIRECT
:
208 internal_error (_("%s: cannot overwrite existing file"), filename
);
211 #if defined (RESTRICTED_SHELL)
212 case RESTRICTED_REDIRECT
:
213 internal_error (_("%s: restricted: cannot redirect output"), filename
);
215 #endif /* RESTRICTED_SHELL */
217 case HEREDOC_REDIRECT
:
218 internal_error (_("cannot create temp file for here-document: %s"), strerror (heredoc_errno
));
221 case BADVAR_REDIRECT
:
222 internal_error (_("%s: cannot assign fd to variable"), filename
);
226 internal_error ("%s: %s", filename
, strerror (error
));
233 /* Perform the redirections on LIST. If flags & RX_ACTIVE, then actually
234 make input and output file descriptors, otherwise just do whatever is
235 necessary for side effecting. flags & RX_UNDOABLE says to remember
236 how to undo the redirections later, if non-zero. If flags & RX_CLEXEC
237 is non-zero, file descriptors opened in do_redirection () have their
238 close-on-exec flag set. */
240 do_redirections (list
, flags
)
248 if (flags
& RX_UNDOABLE
)
250 if (redirection_undo_list
)
252 dispose_redirects (redirection_undo_list
);
253 redirection_undo_list
= (REDIRECT
*)NULL
;
255 if (exec_redirection_undo_list
)
256 dispose_exec_redirects ();
259 for (temp
= list
; temp
; temp
= temp
->next
)
262 error
= do_redirection_internal (temp
, flags
, &fn
);
265 redirection_error (temp
, error
, fn
);
274 /* Return non-zero if the redirection pointed to by REDIRECT has a
275 redirectee.filename that can be expanded. */
277 expandable_redirection_filename (redirect
)
280 switch (redirect
->instruction
)
282 case r_output_direction
:
284 case r_input_direction
:
285 case r_inputa_direction
:
287 case r_append_err_and_out
:
290 case r_duplicating_input_word
:
291 case r_duplicating_output_word
:
292 case r_move_input_word
:
293 case r_move_output_word
:
301 /* Expand the word in WORD returning a string. If WORD expands to
302 multiple words (or no words), then return NULL. */
304 redirection_expand (word
)
308 WORD_LIST
*tlist1
, *tlist2
;
312 w
= copy_word (word
);
314 w
->flags
|= W_NOSPLIT
;
316 tlist1
= make_word_list (w
, (WORD_LIST
*)NULL
);
318 /* Now that we've changed the variable search order to ignore the temp
319 environment, see if we need to change the cached IFS values. */
321 tlist2
= expand_words_no_vars (tlist1
);
323 /* Now we need to change the variable search order back to include the temp
324 environment. We force the temp environment search by forcing
325 executing_builtin to 1. This is what makes `read' get the right values
326 for the IFS-related cached variables, for example. */
327 old
= executing_builtin
;
328 executing_builtin
= 1;
330 executing_builtin
= old
;
331 dispose_words (tlist1
);
333 if (tlist2
== 0 || tlist2
->next
)
335 /* We expanded to no words, or to more than a single word.
336 Dispose of the word list and return NULL. */
338 dispose_words (tlist2
);
339 return ((char *)NULL
);
341 result
= string_list (tlist2
); /* XXX savestring (tlist2->word->word)? */
342 dispose_words (tlist2
);
346 /* Expand a here-document or here-string (determined by RI) contained in
347 REDIRECTEE and return the expanded document. If LENP is non-zero, put
348 the length of the returned string into *LENP.
350 This captures everything about expanding here-documents and here-strings:
351 the returned document should be written directly to whatever file
352 descriptor is specified. In particular, it adds a newline to the end of
353 a here-string to preserve previous semantics. */
355 heredoc_expand (redirectee
, ri
, lenp
)
356 WORD_DESC
*redirectee
;
357 enum r_instruction ri
;
364 if (redirectee
->word
== 0 || redirectee
->word
[0] == '\0')
368 return (redirectee
->word
);
371 /* Quoted here documents are not expanded */
372 if (ri
!= r_reading_string
&& (redirectee
->flags
& W_QUOTED
))
375 *lenp
= STRLEN (redirectee
->word
);
376 return (redirectee
->word
);
380 /* Now that we've changed the variable search order to ignore the temp
381 environment, see if we need to change the cached IFS values. */
383 document
= (ri
== r_reading_string
) ? expand_assignment_string_to_string (redirectee
->word
, 0)
384 : expand_string_to_string (redirectee
->word
, Q_HERE_DOCUMENT
);
386 /* Now we need to change the variable search order back to include the temp
387 environment. We force the temp environment search by forcing
388 executing_builtin to 1. This is what makes `read' get the right values
389 for the IFS-related cached variables, for example. */
390 old
= executing_builtin
;
391 executing_builtin
= 1;
393 executing_builtin
= old
;
395 dlen
= STRLEN (document
);
396 /* XXX - Add trailing newline to here-string */
397 if (ri
== r_reading_string
)
399 document
= xrealloc (document
, dlen
+ 2);
400 document
[dlen
++] = '\n';
401 document
[dlen
] = '\0';
409 /* Write HEREDOC (of length HDLEN) to FD, returning 0 on success and ERRNO on
410 error. Don't handle interrupts. */
412 heredoc_write (fd
, heredoc
, herelen
)
421 nw
= write (fd
, heredoc
, herelen
);
432 /* Create a temporary file or pipe holding the text of the here document
433 pointed to by REDIRECTEE, and return a file descriptor open for reading
434 to it. Return -1 on any error, and make sure errno is set appropriately. */
436 here_document_to_fd (redirectee
, ri
)
437 WORD_DESC
*redirectee
;
438 enum r_instruction ri
;
441 int r
, fd
, fd2
, herepipe
[2];
445 struct stat st1
, st2
;
448 /* Expand the here-document/here-string first and then decide what to do. */
449 document
= heredoc_expand (redirectee
, ri
, &document_len
);
451 /* If we have a zero-length document, don't mess with a temp file */
452 if (document_len
== 0)
454 fd
= open ("/dev/null", O_RDONLY
);
456 if (document
!= redirectee
->word
)
462 #if defined (HEREDOC_PIPESIZE)
463 /* Try to use a pipe internal to this process if the document is shorter
464 than the system's pipe capacity (computed at build time). We want to
465 write the entire document without write blocking. */
466 if (document_len
<= HEREDOC_PIPESIZE
)
468 if (pipe (herepipe
) < 0)
471 if (document
!= redirectee
->word
)
477 #if defined (F_GETPIPE_SZ)
478 if (fcntl (herepipe
[1], F_GETPIPE_SZ
, 0) < document_len
)
482 r
= heredoc_write (herepipe
[1], document
, document_len
);
483 if (document
!= redirectee
->word
)
486 if (r
) /* write error */
492 return (herepipe
[0]);
498 fd
= sh_mktmpfd ("sh-thd", MT_USERANDOM
|MT_USETMPDIR
, &filename
);
500 /* If we failed for some reason other than the file existing, abort */
505 if (document
!= redirectee
->word
)
511 fchmod (fd
, S_IRUSR
| S_IWUSR
);
512 SET_CLOSE_ON_EXEC (fd
);
514 errno
= r
= 0; /* XXX */
515 r
= heredoc_write (fd
, document
, document_len
);
516 if (document
!= redirectee
->word
)
528 /* In an attempt to avoid races, we close the first fd only after opening
530 /* Make the document really temporary. Also make it the input. */
531 fd2
= open (filename
, O_RDONLY
|O_BINARY
, 0600);
544 /* We can use same_file here to check whether or not fd and fd2 refer to
545 the same file, but we don't do that unless HEREDOC_PARANOID is defined. */
546 if (fstat (fd
, &st1
) < 0 || S_ISREG (st1
.st_mode
) == 0 ||
547 fstat (fd2
, &st2
) < 0 || S_ISREG (st2
.st_mode
) == 0 ||
548 same_file (filename
, filename
, &st1
, &st2
) == 0)
560 if (unlink (filename
) < 0)
571 fchmod (fd2
, S_IRUSR
);
576 #define RF_DEVSTDERR 2
577 #define RF_DEVSTDIN 3
578 #define RF_DEVSTDOUT 4
582 /* A list of pattern/value pairs for filenames that the redirection
583 code handles specially. */
584 static STRING_INT_ALIST _redir_special_filenames
[] = {
585 #if !defined (HAVE_DEV_FD)
586 { "/dev/fd/[0-9]*", RF_DEVFD
},
588 #if !defined (HAVE_DEV_STDIN)
589 { "/dev/stderr", RF_DEVSTDERR
},
590 { "/dev/stdin", RF_DEVSTDIN
},
591 { "/dev/stdout", RF_DEVSTDOUT
},
593 #if defined (NETWORK_REDIRECTIONS)
594 { "/dev/tcp/*/*", RF_DEVTCP
},
595 { "/dev/udp/*/*", RF_DEVUDP
},
601 redir_special_open (spec
, filename
, flags
, mode
, ri
)
605 enum r_instruction ri
;
608 #if !defined (HAVE_DEV_FD)
615 #if !defined (HAVE_DEV_FD)
617 if (all_digits (filename
+8) && legal_number (filename
+8, &lfd
) && lfd
== (int)lfd
)
620 fd
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
);
623 fd
= AMBIGUOUS_REDIRECT
;
627 #if !defined (HAVE_DEV_STDIN)
629 fd
= fcntl (0, F_DUPFD
, SHELL_FD_BASE
);
632 fd
= fcntl (1, F_DUPFD
, SHELL_FD_BASE
);
635 fd
= fcntl (2, F_DUPFD
, SHELL_FD_BASE
);
639 #if defined (NETWORK_REDIRECTIONS)
642 #if defined (RESTRICTED_SHELL)
644 return (RESTRICTED_REDIRECT
);
646 #if defined (HAVE_NETWORK)
647 fd
= netopen (filename
);
649 internal_warning (_("/dev/(tcp|udp)/host/port not supported without networking"));
650 fd
= open (filename
, flags
, mode
);
653 #endif /* NETWORK_REDIRECTIONS */
659 /* Open FILENAME with FLAGS in noclobber mode, hopefully avoiding most
660 race conditions and avoiding the problem where the file is replaced
661 between the stat(2) and open(2). */
663 noclobber_open (filename
, flags
, mode
, ri
)
666 enum r_instruction ri
;
669 struct stat finfo
, finfo2
;
671 /* If the file exists and is a regular file, return an error
673 r
= stat (filename
, &finfo
);
674 if (r
== 0 && (S_ISREG (finfo
.st_mode
)))
675 return (NOCLOBBER_REDIRECT
);
677 /* If the file was not present (r != 0), make sure we open it
678 exclusively so that if it is created before we open it, our open
679 will fail. Make sure that we do not truncate an existing file.
680 Note that we don't turn on O_EXCL unless the stat failed -- if
681 the file was not a regular file, we leave O_EXCL off. */
685 fd
= open (filename
, flags
|O_EXCL
, mode
);
686 return ((fd
< 0 && errno
== EEXIST
) ? NOCLOBBER_REDIRECT
: fd
);
688 fd
= open (filename
, flags
, mode
);
690 /* If the open failed, return the file descriptor right away. */
692 return (errno
== EEXIST
? NOCLOBBER_REDIRECT
: fd
);
694 /* OK, the open succeeded, but the file may have been changed from a
695 non-regular file to a regular file between the stat and the open.
696 We are assuming that the O_EXCL open handles the case where FILENAME
697 did not exist and is symlinked to an existing file between the stat
700 /* If we can open it and fstat the file descriptor, and neither check
701 revealed that it was a regular file, and the file has not been replaced,
702 return the file descriptor. */
703 if ((fstat (fd
, &finfo2
) == 0) && (S_ISREG (finfo2
.st_mode
) == 0) &&
704 r
== 0 && (S_ISREG (finfo
.st_mode
) == 0) &&
705 same_file (filename
, filename
, &finfo
, &finfo2
))
708 /* The file has been replaced. badness. */
711 return (NOCLOBBER_REDIRECT
);
715 redir_open (filename
, flags
, mode
, ri
)
718 enum r_instruction ri
;
722 r
= find_string_in_alist (filename
, _redir_special_filenames
, 1);
724 return (redir_special_open (r
, filename
, flags
, mode
, ri
));
726 /* If we are in noclobber mode, you are not allowed to overwrite
727 existing files. Check before opening. */
728 if (noclobber
&& CLOBBERING_REDIRECT (ri
))
730 fd
= noclobber_open (filename
, flags
, mode
, ri
);
731 if (fd
== NOCLOBBER_REDIRECT
)
732 return (NOCLOBBER_REDIRECT
);
738 fd
= open (filename
, flags
, mode
);
740 if (fd
< 0 && e
== EINTR
)
743 run_pending_traps ();
747 while (fd
< 0 && errno
== EINTR
);
750 if ((fd
< 0) && (errno
== EACCES
))
752 fd
= open (filename
, flags
& ~O_CREAT
, mode
);
753 errno
= EACCES
; /* restore errno */
767 clexec
= fcntl (fd
, F_GETFD
, 0);
768 if (clexec
== -1 || (fd
>= SHELL_FD_BASE
&& clexec
== 1))
773 /* Do the specific redirection requested. Returns errno or one of the
774 special redirection errors (*_REDIRECT) in case of error, 0 on success.
775 If flags & RX_ACTIVE is zero, then just do whatever is necessary to
776 produce the appropriate side effects. flags & RX_UNDOABLE, if non-zero,
777 says to remember how to undo each redirection. If flags & RX_CLEXEC is
778 non-zero, then we set all file descriptors > 2 that we open to be
779 close-on-exec. FNP, if non-null is a pointer to a location where the
780 expanded filename is stored. The caller will free it. */
782 do_redirection_internal (redirect
, flags
, fnp
)
787 WORD_DESC
*redirectee
;
788 int redir_fd
, fd
, redirector
, r
, oflags
;
790 char *redirectee_word
;
791 enum r_instruction ri
;
792 REDIRECT
*new_redirect
;
795 redirectee
= redirect
->redirectee
.filename
;
796 redir_fd
= redirect
->redirectee
.dest
;
797 redirector
= redirect
->redirector
.dest
;
798 ri
= redirect
->instruction
;
800 if (redirect
->flags
& RX_INTERNAL
)
801 flags
|= RX_INTERNAL
;
803 if (TRANSLATE_REDIRECT (ri
))
805 /* We have [N]>&WORD[-] or [N]<&WORD[-] (or {V}>&WORD[-] or {V}<&WORD-).
806 and WORD, then translate the redirection into a new one and
808 redirectee_word
= redirection_expand (redirectee
);
810 /* XXX - what to do with [N]<&$w- where w is unset or null? ksh93
812 if (redirectee_word
== 0)
813 return (AMBIGUOUS_REDIRECT
);
814 else if (redirectee_word
[0] == '-' && redirectee_word
[1] == '\0')
816 sd
= redirect
->redirector
;
818 new_redirect
= make_redirection (sd
, r_close_this
, rd
, 0);
820 else if (all_digits (redirectee_word
))
822 sd
= redirect
->redirector
;
823 if (legal_number (redirectee_word
, &lfd
) && (int)lfd
== lfd
)
826 rd
.dest
= -1; /* XXX */
829 case r_duplicating_input_word
:
830 new_redirect
= make_redirection (sd
, r_duplicating_input
, rd
, 0);
832 case r_duplicating_output_word
:
833 new_redirect
= make_redirection (sd
, r_duplicating_output
, rd
, 0);
835 case r_move_input_word
:
836 new_redirect
= make_redirection (sd
, r_move_input
, rd
, 0);
838 case r_move_output_word
:
839 new_redirect
= make_redirection (sd
, r_move_output
, rd
, 0);
842 break; /* shut up gcc */
845 else if (ri
== r_duplicating_output_word
&& (redirect
->rflags
& REDIR_VARASSIGN
) == 0 && redirector
== 1)
847 sd
= redirect
->redirector
;
848 rd
.filename
= make_bare_word (redirectee_word
);
849 new_redirect
= make_redirection (sd
, r_err_and_out
, rd
, 0);
853 free (redirectee_word
);
854 return (AMBIGUOUS_REDIRECT
);
857 free (redirectee_word
);
859 /* Set up the variables needed by the rest of the function from the
861 if (new_redirect
->instruction
== r_err_and_out
)
865 /* Copy the word without allocating any memory that must be
867 redirectee
= (WORD_DESC
*)alloca (sizeof (WORD_DESC
));
868 xbcopy ((char *)new_redirect
->redirectee
.filename
,
869 (char *)redirectee
, sizeof (WORD_DESC
));
871 alloca_hack
= (char *)
872 alloca (1 + strlen (new_redirect
->redirectee
.filename
->word
));
873 redirectee
->word
= alloca_hack
;
874 strcpy (redirectee
->word
, new_redirect
->redirectee
.filename
->word
);
877 /* It's guaranteed to be an integer, and shouldn't be freed. */
878 redirectee
= new_redirect
->redirectee
.filename
;
880 redir_fd
= new_redirect
->redirectee
.dest
;
881 redirector
= new_redirect
->redirector
.dest
;
882 ri
= new_redirect
->instruction
;
884 /* Overwrite the flags element of the old redirect with the new value. */
885 redirect
->flags
= new_redirect
->flags
;
886 dispose_redirects (new_redirect
);
891 case r_output_direction
:
893 case r_input_direction
:
894 case r_inputa_direction
:
895 case r_err_and_out
: /* command &>filename */
896 case r_append_err_and_out
: /* command &>> filename */
899 if (posixly_correct
&& interactive_shell
== 0)
901 oflags
= redirectee
->flags
;
902 redirectee
->flags
|= W_NOGLOB
;
904 redirectee_word
= redirection_expand (redirectee
);
905 if (posixly_correct
&& interactive_shell
== 0)
906 redirectee
->flags
= oflags
;
908 if (redirectee_word
== 0)
909 return (AMBIGUOUS_REDIRECT
);
911 #if defined (RESTRICTED_SHELL)
912 if (restricted
&& (WRITE_REDIRECT (ri
)))
914 free (redirectee_word
);
915 return (RESTRICTED_REDIRECT
);
917 #endif /* RESTRICTED_SHELL */
919 fd
= redir_open (redirectee_word
, redirect
->flags
, 0666, ri
);
921 *fnp
= redirectee_word
;
923 free (redirectee_word
);
925 if (fd
== NOCLOBBER_REDIRECT
|| fd
== RESTRICTED_REDIRECT
)
931 if (flags
& RX_ACTIVE
)
933 if (redirect
->rflags
& REDIR_VARASSIGN
)
935 redirector
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
); /* XXX try this for now */
938 sys_error (_("redirection error: cannot duplicate fd"));
939 REDIRECTION_ERROR (redirector
, r
, fd
);
942 if ((flags
& RX_UNDOABLE
) && (redirect
->rflags
& REDIR_VARASSIGN
) == 0)
944 /* Only setup to undo it if the thing to undo is active. */
945 if ((fd
!= redirector
) && (fcntl (redirector
, F_GETFD
, 0) != -1))
946 r
= add_undo_redirect (redirector
, ri
, -1);
948 r
= add_undo_close_redirect (redirector
);
949 REDIRECTION_ERROR (r
, errno
, fd
);
952 #if defined (BUFFERED_INPUT)
953 /* inhibit call to sync_buffered_stream() for async processes */
954 if (redirector
!= 0 || (subshell_environment
& SUBSHELL_ASYNC
) == 0)
955 check_bush_input (redirector
);
958 /* Make sure there is no pending output before we change the state
959 of the underlying file descriptor, since the builtins use stdio
961 if (redirector
== 1 && fileno (stdout
) == redirector
)
966 else if (redirector
== 2 && fileno (stderr
) == redirector
)
972 if (redirect
->rflags
& REDIR_VARASSIGN
)
974 if ((r
= redir_varassign (redirect
, redirector
)) < 0)
978 return (r
); /* XXX */
981 else if ((fd
!= redirector
) && (dup2 (fd
, redirector
) < 0))
983 close (fd
); /* dup2 failed? must be fd limit issue */
987 #if defined (BUFFERED_INPUT)
988 /* Do not change the buffered stream for an implicit redirection
989 of /dev/null to fd 0 for asynchronous commands without job
990 control (r_inputa_direction). */
991 if (ri
== r_input_direction
|| ri
== r_input_output
)
992 duplicate_buffered_stream (fd
, redirector
);
993 #endif /* BUFFERED_INPUT */
996 * If we're remembering, then this is the result of a while, for
997 * or until loop with a loop redirection, or a function/builtin
998 * executing in the parent shell with a redirection. In the
999 * function/builtin case, we want to set all file descriptors > 2
1000 * to be close-on-exec to duplicate the effect of the old
1001 * for i = 3 to NOFILE close(i) loop. In the case of the loops,
1002 * both sh and ksh leave the file descriptors open across execs.
1003 * The Posix standard mentions only the exec builtin.
1005 if ((flags
& RX_CLEXEC
) && (redirector
> 2))
1006 SET_CLOSE_ON_EXEC (redirector
);
1009 if (fd
!= redirector
)
1011 #if defined (BUFFERED_INPUT)
1012 if (INPUT_REDIRECT (ri
))
1013 close_buffered_fd (fd
);
1015 #endif /* !BUFFERED_INPUT */
1016 close (fd
); /* Don't close what we just opened! */
1019 /* If we are hacking both stdout and stderr, do the stderr
1020 redirection here. XXX - handle {var} here? */
1021 if (ri
== r_err_and_out
|| ri
== r_append_err_and_out
)
1023 if (flags
& RX_ACTIVE
)
1025 if (flags
& RX_UNDOABLE
)
1026 add_undo_redirect (2, ri
, -1);
1027 if (dup2 (1, 2) < 0)
1033 case r_reading_until
:
1034 case r_deblank_reading_until
:
1035 case r_reading_string
:
1036 /* REDIRECTEE is a pointer to a WORD_DESC containing the text of
1037 the new input. Place it in a temporary file. */
1040 fd
= here_document_to_fd (redirectee
, ri
);
1044 heredoc_errno
= errno
;
1045 return (HEREDOC_REDIRECT
);
1048 if (redirect
->rflags
& REDIR_VARASSIGN
)
1050 redirector
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
); /* XXX try this for now */
1053 sys_error (_("redirection error: cannot duplicate fd"));
1054 REDIRECTION_ERROR (redirector
, r
, fd
);
1057 if (flags
& RX_ACTIVE
)
1059 if ((flags
& RX_UNDOABLE
) && (redirect
->rflags
& REDIR_VARASSIGN
) == 0)
1061 /* Only setup to undo it if the thing to undo is active. */
1062 if ((fd
!= redirector
) && (fcntl (redirector
, F_GETFD
, 0) != -1))
1063 r
= add_undo_redirect (redirector
, ri
, -1);
1065 r
= add_undo_close_redirect (redirector
);
1066 REDIRECTION_ERROR (r
, errno
, fd
);
1069 #if defined (BUFFERED_INPUT)
1070 check_bush_input (redirector
);
1072 if (redirect
->rflags
& REDIR_VARASSIGN
)
1074 if ((r
= redir_varassign (redirect
, redirector
)) < 0)
1078 return (r
); /* XXX */
1081 else if (fd
!= redirector
&& dup2 (fd
, redirector
) < 0)
1088 #if defined (BUFFERED_INPUT)
1089 duplicate_buffered_stream (fd
, redirector
);
1092 if ((flags
& RX_CLEXEC
) && (redirector
> 2))
1093 SET_CLOSE_ON_EXEC (redirector
);
1096 if (fd
!= redirector
)
1097 #if defined (BUFFERED_INPUT)
1098 close_buffered_fd (fd
);
1105 case r_duplicating_input
:
1106 case r_duplicating_output
:
1109 if ((flags
& RX_ACTIVE
) && (redirect
->rflags
& REDIR_VARASSIGN
))
1111 redirector
= fcntl (redir_fd
, F_DUPFD
, SHELL_FD_BASE
); /* XXX try this for now */
1114 sys_error (_("redirection error: cannot duplicate fd"));
1115 REDIRECTION_ERROR (redirector
, r
, -1);
1118 if ((flags
& RX_ACTIVE
) && (redir_fd
!= redirector
))
1120 if ((flags
& RX_UNDOABLE
) && (redirect
->rflags
& REDIR_VARASSIGN
) == 0)
1122 /* Only setup to undo it if the thing to undo is active. */
1123 if (fcntl (redirector
, F_GETFD
, 0) != -1)
1124 r
= add_undo_redirect (redirector
, ri
, redir_fd
);
1126 r
= add_undo_close_redirect (redirector
);
1127 REDIRECTION_ERROR (r
, errno
, -1);
1129 if ((flags
& RX_UNDOABLE
) && (ri
== r_move_input
|| ri
== r_move_output
))
1131 /* r_move_input and r_move_output add an additional close()
1132 that needs to be undone */
1133 if (fcntl (redirector
, F_GETFD
, 0) != -1)
1135 r
= add_undo_redirect (redir_fd
, r_close_this
, -1);
1136 REDIRECTION_ERROR (r
, errno
, -1);
1139 #if defined (BUFFERED_INPUT)
1140 /* inhibit call to sync_buffered_stream() for async processes */
1141 if (redirector
!= 0 || (subshell_environment
& SUBSHELL_ASYNC
) == 0)
1142 check_bush_input (redirector
);
1144 if (redirect
->rflags
& REDIR_VARASSIGN
)
1146 if ((r
= redir_varassign (redirect
, redirector
)) < 0)
1149 return (r
); /* XXX */
1152 /* This is correct. 2>&1 means dup2 (1, 2); */
1153 else if (dup2 (redir_fd
, redirector
) < 0)
1156 #if defined (BUFFERED_INPUT)
1157 if (ri
== r_duplicating_input
|| ri
== r_move_input
)
1158 duplicate_buffered_stream (redir_fd
, redirector
);
1159 #endif /* BUFFERED_INPUT */
1161 /* First duplicate the close-on-exec state of redirectee. dup2
1162 leaves the flag unset on the new descriptor, which means it
1163 stays open. Only set the close-on-exec bit for file descriptors
1164 greater than 2 in any case, since 0-2 should always be open
1165 unless closed by something like `exec 2<&-'. It should always
1166 be safe to set fds > 2 to close-on-exec if they're being used to
1167 save file descriptors < 2, since we don't need to preserve the
1168 state of the close-on-exec flag for those fds -- they should
1170 /* if ((already_set || set_unconditionally) && (ok_to_set))
1173 if (((fcntl (redir_fd
, F_GETFD
, 0) == 1) || redir_fd
< 2 || (flags
& RX_CLEXEC
)) &&
1176 if (((fcntl (redir_fd
, F_GETFD
, 0) == 1) || (redir_fd
< 2 && (flags
& RX_INTERNAL
)) || (flags
& RX_CLEXEC
)) &&
1179 SET_CLOSE_ON_EXEC (redirector
);
1181 /* When undoing saving of non-standard file descriptors (>=3) using
1182 file descriptors >= SHELL_FD_BASE, we set the saving fd to be
1183 close-on-exec and use a flag to decide how to set close-on-exec
1184 when the fd is restored. */
1185 if ((redirect
->flags
& RX_INTERNAL
) && (redirect
->flags
& RX_SAVCLEXEC
) && redirector
>= 3 && (redir_fd
>= SHELL_FD_BASE
|| (redirect
->flags
& RX_SAVEFD
)))
1186 SET_OPEN_ON_EXEC (redirector
);
1188 /* dup-and-close redirection */
1189 if (ri
== r_move_input
|| ri
== r_move_output
)
1191 xtrace_fdchk (redir_fd
);
1194 #if defined (COPROCESS_SUPPORT)
1195 coproc_fdchk (redir_fd
); /* XXX - loses coproc fds */
1202 if (flags
& RX_ACTIVE
)
1204 if (redirect
->rflags
& REDIR_VARASSIGN
)
1206 redirector
= redir_varvalue (redirect
);
1208 return AMBIGUOUS_REDIRECT
;
1212 /* XXX - only if REDIR_VARASSIGN not set? */
1213 if (flags
& RX_UNDOABLE
)
1215 if (fcntl (redirector
, F_GETFD
, 0) != -1)
1216 r
= add_undo_redirect (redirector
, ri
, -1);
1218 r
= add_undo_close_redirect (redirector
);
1219 REDIRECTION_ERROR (r
, errno
, redirector
);
1222 #if defined (COPROCESS_SUPPORT)
1223 coproc_fdchk (redirector
);
1225 xtrace_fdchk (redirector
);
1227 #if defined (BUFFERED_INPUT)
1228 /* inhibit call to sync_buffered_stream() for async processes */
1229 if (redirector
!= 0 || (subshell_environment
& SUBSHELL_ASYNC
) == 0)
1230 check_bush_input (redirector
);
1231 r
= close_buffered_fd (redirector
);
1232 #else /* !BUFFERED_INPUT */
1233 r
= close (redirector
);
1234 #endif /* !BUFFERED_INPUT */
1236 if (r
< 0 && (flags
& RX_INTERNAL
) && (errno
== EIO
|| errno
== ENOSPC
))
1237 REDIRECTION_ERROR (r
, errno
, -1);
1241 case r_duplicating_input_word
:
1242 case r_duplicating_output_word
:
1243 case r_move_input_word
:
1244 case r_move_output_word
:
1250 /* Remember the file descriptor associated with the slot FD,
1251 on REDIRECTION_UNDO_LIST. Note that the list will be reversed
1252 before it is executed. Any redirections that need to be undone
1253 even if REDIRECTION_UNDO_LIST is discarded by the exec builtin
1254 are also saved on EXEC_REDIRECTION_UNDO_LIST. FDBASE says where to
1255 start the duplicating. If it's less than SHELL_FD_BASE, we're ok,
1256 and can use SHELL_FD_BASE (-1 == don't care). If it's >= SHELL_FD_BASE,
1257 we have to make sure we don't use fdbase to save a file descriptor,
1258 since we're going to use it later (e.g., make sure we don't save fd 0
1259 to fd 10 if we have a redirection like 0<&10). If the value of fdbase
1260 puts the process over its fd limit, causing fcntl to fail, we try
1261 again with SHELL_FD_BASE. Return 0 on success, -1 on error. */
1263 add_undo_redirect (fd
, ri
, fdbase
)
1265 enum r_instruction ri
;
1268 int new_fd
, clexec_flag
, savefd_flag
;
1269 REDIRECT
*new_redirect
, *closer
, *dummy_redirect
;
1273 new_fd
= fcntl (fd
, F_DUPFD
, (fdbase
< SHELL_FD_BASE
) ? SHELL_FD_BASE
: fdbase
+1);
1275 new_fd
= fcntl (fd
, F_DUPFD
, SHELL_FD_BASE
);
1278 new_fd
= fcntl (fd
, F_DUPFD
, 0);
1284 sys_error (_("redirection error: cannot duplicate fd"));
1288 clexec_flag
= fcntl (fd
, F_GETFD
, 0);
1292 closer
= make_redirection (sd
, r_close_this
, rd
, 0);
1293 closer
->flags
|= RX_INTERNAL
;
1294 dummy_redirect
= copy_redirects (closer
);
1299 new_redirect
= make_redirection (sd
, r_duplicating_input
, rd
, 0);
1301 new_redirect
= make_redirection (sd
, r_duplicating_output
, rd
, 0);
1302 new_redirect
->flags
|= RX_INTERNAL
;
1304 new_redirect
->flags
|= RX_SAVEFD
;
1305 if (clexec_flag
== 0 && fd
>= 3 && (new_fd
>= SHELL_FD_BASE
|| savefd_flag
))
1306 new_redirect
->flags
|= RX_SAVCLEXEC
;
1307 new_redirect
->next
= closer
;
1309 closer
->next
= redirection_undo_list
;
1310 redirection_undo_list
= new_redirect
;
1312 /* Save redirections that need to be undone even if the undo list
1313 is thrown away by the `exec' builtin. */
1314 add_exec_redirect (dummy_redirect
);
1316 /* experimental: if we're saving a redirection to undo for a file descriptor
1317 above SHELL_FD_BASE, add a redirection to be undone if the exec builtin
1318 causes redirections to be discarded. There needs to be a difference
1319 between fds that are used to save other fds and then are the target of
1320 user redirections and fds that are just the target of user redirections.
1321 We use the close-on-exec flag to tell the difference; fds > SHELL_FD_BASE
1322 that have the close-on-exec flag set are assumed to be fds used internally
1324 if (fd
>= SHELL_FD_BASE
&& ri
!= r_close_this
&& clexec_flag
)
1328 new_redirect
= make_redirection (sd
, r_duplicating_output
, rd
, 0);
1329 new_redirect
->flags
|= RX_INTERNAL
;
1331 add_exec_redirect (new_redirect
);
1334 /* File descriptors used only for saving others should always be
1335 marked close-on-exec. Unfortunately, we have to preserve the
1336 close-on-exec state of the file descriptor we are saving, since
1337 fcntl (F_DUPFD) sets the new file descriptor to remain open
1338 across execs. If, however, the file descriptor whose state we
1339 are saving is <= 2, we can just set the close-on-exec flag,
1340 because file descriptors 0-2 should always be open-on-exec,
1341 and the restore above in do_redirection() will take care of it. */
1342 if (clexec_flag
|| fd
< 3)
1343 SET_CLOSE_ON_EXEC (new_fd
);
1344 else if (redirection_undo_list
->flags
& RX_SAVCLEXEC
)
1345 SET_CLOSE_ON_EXEC (new_fd
);
1350 /* Set up to close FD when we are finished with the current command
1351 and its redirections. Return 0 on success, -1 on error. */
1353 add_undo_close_redirect (fd
)
1361 closer
= make_redirection (sd
, r_close_this
, rd
, 0);
1362 closer
->flags
|= RX_INTERNAL
;
1363 closer
->next
= redirection_undo_list
;
1364 redirection_undo_list
= closer
;
1370 add_exec_redirect (dummy_redirect
)
1371 REDIRECT
*dummy_redirect
;
1373 dummy_redirect
->next
= exec_redirection_undo_list
;
1374 exec_redirection_undo_list
= dummy_redirect
;
1377 /* Return 1 if the redirection specified by RI and REDIRECTOR alters the
1380 stdin_redirection (ri
, redirector
)
1381 enum r_instruction ri
;
1386 case r_input_direction
:
1387 case r_inputa_direction
:
1388 case r_input_output
:
1389 case r_reading_until
:
1390 case r_deblank_reading_until
:
1391 case r_reading_string
:
1393 case r_duplicating_input
:
1394 case r_duplicating_input_word
:
1396 return (redirector
== 0);
1397 case r_output_direction
:
1398 case r_appending_to
:
1399 case r_duplicating_output
:
1401 case r_append_err_and_out
:
1402 case r_output_force
:
1403 case r_duplicating_output_word
:
1406 case r_move_input_word
:
1407 case r_move_output_word
:
1413 /* Return non-zero if any of the redirections in REDIRS alter the standard
1416 stdin_redirects (redirs
)
1422 for (n
= 0, rp
= redirs
; rp
; rp
= rp
->next
)
1423 if ((rp
->rflags
& REDIR_VARASSIGN
) == 0)
1424 n
+= stdin_redirection (rp
->instruction
, rp
->redirector
.dest
);
1427 /* bind_var_to_int handles array references */
1429 redir_varassign (redir
, fd
)
1436 w
= redir
->redirector
.filename
;
1437 v
= bind_var_to_int (w
->word
, fd
);
1438 if (v
== 0 || readonly_p (v
) || noassign_p (v
))
1439 return BADVAR_REDIRECT
;
1441 stupidly_hack_special_variables (w
->word
);
1445 /* Handles {array[ind]} for redirection words */
1447 redir_varvalue (redir
)
1454 #if defined (ARRAY_VARS)
1459 w
= redir
->redirector
.filename
->word
; /* shorthand */
1460 /* XXX - handle set -u here? */
1461 #if defined (ARRAY_VARS)
1462 if (vr
= valid_array_reference (w
, 0))
1464 v
= array_variable_part (w
, 0, &sub
, &len
);
1469 v
= find_variable (w
);
1470 #if defined (ARRAY_VARS)
1473 v
= find_variable_last_nameref (w
, 0);
1474 if (v
&& nameref_p (v
))
1476 w
= nameref_cell (v
);
1477 if (vr
= valid_array_reference (w
, 0))
1478 v
= array_variable_part (w
, 0, &sub
, &len
);
1480 v
= find_variable (w
);
1486 if (v
== 0 || invisible_p (v
))
1489 #if defined (ARRAY_VARS)
1490 /* get_variable_value handles references to array variables without
1492 if (vr
&& (array_p (v
) || assoc_p (v
)))
1493 val
= get_array_value (w
, 0, (int *)NULL
, (arrayind_t
*)0);
1496 val
= get_variable_value (v
);
1497 if (val
== 0 || *val
== 0)
1500 if (legal_number (val
, &vmax
) < 0)
1503 i
= vmax
; /* integer truncation */