Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1 | /* redir.c -- Functions to perform input and output redirection. */ |
| 2 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1997-2012 Free Software Foundation, Inc. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 16 | |
| 17 | You should have received a copy of the GNU General Public License |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
| 23 | #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX) |
| 24 | #pragma alloca |
| 25 | #endif /* _AIX && RISC6000 && !__GNUC__ */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include "bashtypes.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 29 | #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 30 | # include <sys/file.h> |
| 31 | #endif |
| 32 | #include "filecntl.h" |
| 33 | #include "posixstat.h" |
| 34 | |
| 35 | #if defined (HAVE_UNISTD_H) |
| 36 | # include <unistd.h> |
| 37 | #endif |
| 38 | |
| 39 | #include <errno.h> |
| 40 | |
| 41 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 42 | #include <errno.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #include "bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 46 | #include "bashintl.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 47 | #include "memalloc.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 48 | |
| 49 | #define NEED_FPURGE_DECL |
| 50 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 51 | #include "shell.h" |
| 52 | #include "flags.h" |
| 53 | #include "execute_cmd.h" |
| 54 | #include "redir.h" |
| 55 | |
| 56 | #if defined (BUFFERED_INPUT) |
| 57 | # include "input.h" |
| 58 | #endif |
| 59 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 60 | #define SHELL_FD_BASE 10 |
| 61 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 62 | int expanding_redir; |
| 63 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 64 | extern int posixly_correct; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 65 | extern int last_command_exit_value; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 66 | extern int executing_builtin; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 67 | extern REDIRECT *redirection_undo_list; |
| 68 | extern REDIRECT *exec_redirection_undo_list; |
| 69 | |
| 70 | /* Static functions defined and used in this file. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 71 | static void add_exec_redirect __P((REDIRECT *)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 72 | static int add_undo_redirect __P((int, enum r_instruction, int)); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 73 | static int add_undo_close_redirect __P((int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 74 | static int expandable_redirection_filename __P((REDIRECT *)); |
| 75 | static int stdin_redirection __P((enum r_instruction, int)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 76 | static int undoablefd __P((int)); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 77 | static int do_redirection_internal __P((REDIRECT *, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 78 | |
| 79 | static int write_here_document __P((int, WORD_DESC *)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 80 | static int write_here_string __P((int, WORD_DESC *)); |
| 81 | static int here_document_to_fd __P((WORD_DESC *, enum r_instruction)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 82 | |
| 83 | static int redir_special_open __P((int, char *, int, int, enum r_instruction)); |
| 84 | static int noclobber_open __P((char *, int, int, enum r_instruction)); |
| 85 | static int redir_open __P((char *, int, int, enum r_instruction)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 86 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 87 | static int redir_varassign __P((REDIRECT *, int)); |
| 88 | static int redir_varvalue __P((REDIRECT *)); |
| 89 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 90 | /* Spare redirector used when translating [N]>&WORD[-] or [N]<&WORD[-] to |
| 91 | a new redirection and when creating the redirection undo list. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 92 | static REDIRECTEE rd; |
| 93 | |
| 94 | /* Set to errno when a here document cannot be created for some reason. |
| 95 | Used to print a reasonable error message. */ |
| 96 | static int heredoc_errno; |
| 97 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 98 | #define REDIRECTION_ERROR(r, e, fd) \ |
| 99 | do { \ |
| 100 | if ((r) < 0) \ |
| 101 | { \ |
| 102 | if (fd >= 0) \ |
| 103 | close (fd); \ |
| 104 | last_command_exit_value = EXECUTION_FAILURE;\ |
| 105 | return ((e) == 0 ? EINVAL : (e));\ |
| 106 | } \ |
| 107 | } while (0) |
| 108 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 109 | void |
| 110 | redirection_error (temp, error) |
| 111 | REDIRECT *temp; |
| 112 | int error; |
| 113 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 114 | char *filename, *allocname; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 115 | int oflags; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 116 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 117 | allocname = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 118 | if (temp->rflags & REDIR_VARASSIGN) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 119 | filename = allocname = savestring (temp->redirector.filename->word); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 120 | else if (temp->redirector.dest < 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 121 | /* This can happen when read_token_word encounters overflow, like in |
| 122 | exec 4294967297>x */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 123 | filename = _("file descriptor out of range"); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 124 | #ifdef EBADF |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 125 | /* This error can never involve NOCLOBBER */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 126 | else if (error != NOCLOBBER_REDIRECT && temp->redirector.dest >= 0 && error == EBADF) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 127 | { |
| 128 | /* If we're dealing with two file descriptors, we have to guess about |
| 129 | which one is invalid; in the cases of r_{duplicating,move}_input and |
| 130 | r_{duplicating,move}_output we're here because dup2() failed. */ |
| 131 | switch (temp->instruction) |
| 132 | { |
| 133 | case r_duplicating_input: |
| 134 | case r_duplicating_output: |
| 135 | case r_move_input: |
| 136 | case r_move_output: |
| 137 | filename = allocname = itos (temp->redirectee.dest); |
| 138 | break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 139 | case r_duplicating_input_word: |
| 140 | if (temp->redirector.dest == 0) /* Guess */ |
| 141 | filename = temp->redirectee.filename->word; /* XXX */ |
| 142 | else |
| 143 | filename = allocname = itos (temp->redirector.dest); |
| 144 | break; |
| 145 | case r_duplicating_output_word: |
| 146 | if (temp->redirector.dest == 1) /* Guess */ |
| 147 | filename = temp->redirectee.filename->word; /* XXX */ |
| 148 | else |
| 149 | filename = allocname = itos (temp->redirector.dest); |
| 150 | break; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 151 | default: |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 152 | filename = allocname = itos (temp->redirector.dest); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 156 | #endif |
| 157 | else if (expandable_redirection_filename (temp)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 158 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 159 | expandable_filename: |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 160 | oflags = temp->redirectee.filename->flags; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 161 | if (posixly_correct && interactive_shell == 0) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 162 | temp->redirectee.filename->flags |= W_NOGLOB; |
| 163 | temp->redirectee.filename->flags |= W_NOCOMSUB; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 164 | filename = allocname = redirection_expand (temp->redirectee.filename); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 165 | temp->redirectee.filename->flags = oflags; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 166 | if (filename == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 167 | filename = temp->redirectee.filename->word; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 168 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 169 | else if (temp->redirectee.dest < 0) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 170 | filename = _("file descriptor out of range"); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 171 | else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 172 | filename = allocname = itos (temp->redirectee.dest); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 173 | |
| 174 | switch (error) |
| 175 | { |
| 176 | case AMBIGUOUS_REDIRECT: |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 177 | internal_error (_("%s: ambiguous redirect"), filename); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 178 | break; |
| 179 | |
| 180 | case NOCLOBBER_REDIRECT: |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 181 | internal_error (_("%s: cannot overwrite existing file"), filename); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 182 | break; |
| 183 | |
| 184 | #if defined (RESTRICTED_SHELL) |
| 185 | case RESTRICTED_REDIRECT: |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 186 | internal_error (_("%s: restricted: cannot redirect output"), filename); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 187 | break; |
| 188 | #endif /* RESTRICTED_SHELL */ |
| 189 | |
| 190 | case HEREDOC_REDIRECT: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 191 | internal_error (_("cannot create temp file for here-document: %s"), strerror (heredoc_errno)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 192 | break; |
| 193 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 194 | case BADVAR_REDIRECT: |
| 195 | internal_error (_("%s: cannot assign fd to variable"), filename); |
| 196 | break; |
| 197 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 198 | default: |
| 199 | internal_error ("%s: %s", filename, strerror (error)); |
| 200 | break; |
| 201 | } |
| 202 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 203 | FREE (allocname); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 206 | /* Perform the redirections on LIST. If flags & RX_ACTIVE, then actually |
| 207 | make input and output file descriptors, otherwise just do whatever is |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 208 | necessary for side effecting. flags & RX_UNDOABLE says to remember |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 209 | how to undo the redirections later, if non-zero. If flags & RX_CLEXEC |
| 210 | is non-zero, file descriptors opened in do_redirection () have their |
| 211 | close-on-exec flag set. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 212 | int |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 213 | do_redirections (list, flags) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 214 | REDIRECT *list; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 215 | int flags; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 216 | { |
| 217 | int error; |
| 218 | REDIRECT *temp; |
| 219 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 220 | if (flags & RX_UNDOABLE) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 221 | { |
| 222 | if (redirection_undo_list) |
| 223 | { |
| 224 | dispose_redirects (redirection_undo_list); |
| 225 | redirection_undo_list = (REDIRECT *)NULL; |
| 226 | } |
| 227 | if (exec_redirection_undo_list) |
| 228 | dispose_exec_redirects (); |
| 229 | } |
| 230 | |
| 231 | for (temp = list; temp; temp = temp->next) |
| 232 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 233 | error = do_redirection_internal (temp, flags); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 234 | if (error) |
| 235 | { |
| 236 | redirection_error (temp, error); |
| 237 | return (error); |
| 238 | } |
| 239 | } |
| 240 | return (0); |
| 241 | } |
| 242 | |
| 243 | /* Return non-zero if the redirection pointed to by REDIRECT has a |
| 244 | redirectee.filename that can be expanded. */ |
| 245 | static int |
| 246 | expandable_redirection_filename (redirect) |
| 247 | REDIRECT *redirect; |
| 248 | { |
| 249 | switch (redirect->instruction) |
| 250 | { |
| 251 | case r_output_direction: |
| 252 | case r_appending_to: |
| 253 | case r_input_direction: |
| 254 | case r_inputa_direction: |
| 255 | case r_err_and_out: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 256 | case r_append_err_and_out: |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 257 | case r_input_output: |
| 258 | case r_output_force: |
| 259 | case r_duplicating_input_word: |
| 260 | case r_duplicating_output_word: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 261 | case r_move_input_word: |
| 262 | case r_move_output_word: |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 263 | return 1; |
| 264 | |
| 265 | default: |
| 266 | return 0; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* Expand the word in WORD returning a string. If WORD expands to |
| 271 | multiple words (or no words), then return NULL. */ |
| 272 | char * |
| 273 | redirection_expand (word) |
| 274 | WORD_DESC *word; |
| 275 | { |
| 276 | char *result; |
| 277 | WORD_LIST *tlist1, *tlist2; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 278 | WORD_DESC *w; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 279 | int old; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 280 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 281 | w = copy_word (word); |
| 282 | if (posixly_correct) |
| 283 | w->flags |= W_NOSPLIT; |
| 284 | |
| 285 | tlist1 = make_word_list (w, (WORD_LIST *)NULL); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 286 | expanding_redir = 1; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 287 | /* Now that we've changed the variable search order to ignore the temp |
| 288 | environment, see if we need to change the cached IFS values. */ |
| 289 | sv_ifs ("IFS"); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 290 | tlist2 = expand_words_no_vars (tlist1); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 291 | expanding_redir = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 292 | /* Now we need to change the variable search order back to include the temp |
| 293 | environment. We force the temp environment search by forcing |
| 294 | executing_builtin to 1. This is what makes `read' get the right values |
| 295 | for the IFS-related cached variables, for example. */ |
| 296 | old = executing_builtin; |
| 297 | executing_builtin = 1; |
| 298 | sv_ifs ("IFS"); |
| 299 | executing_builtin = old; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 300 | dispose_words (tlist1); |
| 301 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 302 | if (tlist2 == 0 || tlist2->next) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 303 | { |
| 304 | /* We expanded to no words, or to more than a single word. |
| 305 | Dispose of the word list and return NULL. */ |
| 306 | if (tlist2) |
| 307 | dispose_words (tlist2); |
| 308 | return ((char *)NULL); |
| 309 | } |
| 310 | result = string_list (tlist2); /* XXX savestring (tlist2->word->word)? */ |
| 311 | dispose_words (tlist2); |
| 312 | return (result); |
| 313 | } |
| 314 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 315 | static int |
| 316 | write_here_string (fd, redirectee) |
| 317 | int fd; |
| 318 | WORD_DESC *redirectee; |
| 319 | { |
| 320 | char *herestr; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 321 | int herelen, n, e, old; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 322 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 323 | expanding_redir = 1; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 324 | /* Now that we've changed the variable search order to ignore the temp |
| 325 | environment, see if we need to change the cached IFS values. */ |
| 326 | sv_ifs ("IFS"); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 327 | herestr = expand_string_to_string (redirectee->word, 0); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 328 | expanding_redir = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 329 | /* Now we need to change the variable search order back to include the temp |
| 330 | environment. We force the temp environment search by forcing |
| 331 | executing_builtin to 1. This is what makes `read' get the right values |
| 332 | for the IFS-related cached variables, for example. */ |
| 333 | old = executing_builtin; |
| 334 | executing_builtin = 1; |
| 335 | sv_ifs ("IFS"); |
| 336 | executing_builtin = old; |
| 337 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 338 | herelen = STRLEN (herestr); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 339 | |
| 340 | n = write (fd, herestr, herelen); |
| 341 | if (n == herelen) |
| 342 | { |
| 343 | n = write (fd, "\n", 1); |
| 344 | herelen = 1; |
| 345 | } |
| 346 | e = errno; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 347 | FREE (herestr); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 348 | if (n != herelen) |
| 349 | { |
| 350 | if (e == 0) |
| 351 | e = ENOSPC; |
| 352 | return e; |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 357 | /* Write the text of the here document pointed to by REDIRECTEE to the file |
| 358 | descriptor FD, which is already open to a temp file. Return 0 if the |
| 359 | write is successful, otherwise return errno. */ |
| 360 | static int |
| 361 | write_here_document (fd, redirectee) |
| 362 | int fd; |
| 363 | WORD_DESC *redirectee; |
| 364 | { |
| 365 | char *document; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 366 | int document_len, fd2, old; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 367 | FILE *fp; |
| 368 | register WORD_LIST *t, *tlist; |
| 369 | |
| 370 | /* Expand the text if the word that was specified had |
| 371 | no quoting. The text that we expand is treated |
| 372 | exactly as if it were surrounded by double quotes. */ |
| 373 | |
| 374 | if (redirectee->flags & W_QUOTED) |
| 375 | { |
| 376 | document = redirectee->word; |
| 377 | document_len = strlen (document); |
| 378 | /* Set errno to something reasonable if the write fails. */ |
| 379 | if (write (fd, document, document_len) < document_len) |
| 380 | { |
| 381 | if (errno == 0) |
| 382 | errno = ENOSPC; |
| 383 | return (errno); |
| 384 | } |
| 385 | else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 386 | return 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 389 | expanding_redir = 1; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 390 | /* Now that we've changed the variable search order to ignore the temp |
| 391 | environment, see if we need to change the cached IFS values. */ |
| 392 | sv_ifs ("IFS"); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 393 | tlist = expand_string (redirectee->word, Q_HERE_DOCUMENT); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 394 | expanding_redir = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 395 | /* Now we need to change the variable search order back to include the temp |
| 396 | environment. We force the temp environment search by forcing |
| 397 | executing_builtin to 1. This is what makes `read' get the right values |
| 398 | for the IFS-related cached variables, for example. */ |
| 399 | old = executing_builtin; |
| 400 | executing_builtin = 1; |
| 401 | sv_ifs ("IFS"); |
| 402 | executing_builtin = old; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 403 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 404 | if (tlist) |
| 405 | { |
| 406 | /* Try using buffered I/O (stdio) and writing a word |
| 407 | at a time, letting stdio do the work of buffering |
| 408 | for us rather than managing our own strings. Most |
| 409 | stdios are not particularly fast, however -- this |
| 410 | may need to be reconsidered later. */ |
| 411 | if ((fd2 = dup (fd)) < 0 || (fp = fdopen (fd2, "w")) == NULL) |
| 412 | { |
| 413 | if (fd2 >= 0) |
| 414 | close (fd2); |
| 415 | return (errno); |
| 416 | } |
| 417 | errno = 0; |
| 418 | for (t = tlist; t; t = t->next) |
| 419 | { |
| 420 | /* This is essentially the body of |
| 421 | string_list_internal expanded inline. */ |
| 422 | document = t->word->word; |
| 423 | document_len = strlen (document); |
| 424 | if (t != tlist) |
| 425 | putc (' ', fp); /* separator */ |
| 426 | fwrite (document, document_len, 1, fp); |
| 427 | if (ferror (fp)) |
| 428 | { |
| 429 | if (errno == 0) |
| 430 | errno = ENOSPC; |
| 431 | fd2 = errno; |
| 432 | fclose(fp); |
| 433 | dispose_words (tlist); |
| 434 | return (fd2); |
| 435 | } |
| 436 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 437 | dispose_words (tlist); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 438 | if (fclose (fp) != 0) |
| 439 | { |
| 440 | if (errno == 0) |
| 441 | errno = ENOSPC; |
| 442 | return (errno); |
| 443 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /* Create a temporary file holding the text of the here document pointed to |
| 449 | by REDIRECTEE, and return a file descriptor open for reading to the temp |
| 450 | file. Return -1 on any error, and make sure errno is set appropriately. */ |
| 451 | static int |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 452 | here_document_to_fd (redirectee, ri) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 453 | WORD_DESC *redirectee; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 454 | enum r_instruction ri; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 455 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 456 | char *filename; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 457 | int r, fd, fd2; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 458 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 459 | fd = sh_mktmpfd ("sh-thd", MT_USERANDOM|MT_USETMPDIR, &filename); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 460 | |
| 461 | /* If we failed for some reason other than the file existing, abort */ |
| 462 | if (fd < 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 463 | { |
| 464 | FREE (filename); |
| 465 | return (fd); |
| 466 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 467 | |
| 468 | errno = r = 0; /* XXX */ |
| 469 | /* write_here_document returns 0 on success, errno on failure. */ |
| 470 | if (redirectee->word) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 471 | r = (ri != r_reading_string) ? write_here_document (fd, redirectee) |
| 472 | : write_here_string (fd, redirectee); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 473 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 474 | if (r) |
| 475 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 476 | close (fd); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 477 | unlink (filename); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 478 | free (filename); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 479 | errno = r; |
| 480 | return (-1); |
| 481 | } |
| 482 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 483 | /* In an attempt to avoid races, we close the first fd only after opening |
| 484 | the second. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 485 | /* Make the document really temporary. Also make it the input. */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 486 | fd2 = open (filename, O_RDONLY|O_BINARY, 0600); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 487 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 488 | if (fd2 < 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 489 | { |
| 490 | r = errno; |
| 491 | unlink (filename); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 492 | free (filename); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 493 | close (fd); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 494 | errno = r; |
| 495 | return -1; |
| 496 | } |
| 497 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 498 | close (fd); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 499 | if (unlink (filename) < 0) |
| 500 | { |
| 501 | r = errno; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 502 | close (fd2); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 503 | free (filename); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 504 | errno = r; |
| 505 | return (-1); |
| 506 | } |
| 507 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 508 | free (filename); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 509 | return (fd2); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 512 | #define RF_DEVFD 1 |
| 513 | #define RF_DEVSTDERR 2 |
| 514 | #define RF_DEVSTDIN 3 |
| 515 | #define RF_DEVSTDOUT 4 |
| 516 | #define RF_DEVTCP 5 |
| 517 | #define RF_DEVUDP 6 |
| 518 | |
| 519 | /* A list of pattern/value pairs for filenames that the redirection |
| 520 | code handles specially. */ |
| 521 | static STRING_INT_ALIST _redir_special_filenames[] = { |
| 522 | #if !defined (HAVE_DEV_FD) |
| 523 | { "/dev/fd/[0-9]*", RF_DEVFD }, |
| 524 | #endif |
| 525 | #if !defined (HAVE_DEV_STDIN) |
| 526 | { "/dev/stderr", RF_DEVSTDERR }, |
| 527 | { "/dev/stdin", RF_DEVSTDIN }, |
| 528 | { "/dev/stdout", RF_DEVSTDOUT }, |
| 529 | #endif |
| 530 | #if defined (NETWORK_REDIRECTIONS) |
| 531 | { "/dev/tcp/*/*", RF_DEVTCP }, |
| 532 | { "/dev/udp/*/*", RF_DEVUDP }, |
| 533 | #endif |
| 534 | { (char *)NULL, -1 } |
| 535 | }; |
| 536 | |
| 537 | static int |
| 538 | redir_special_open (spec, filename, flags, mode, ri) |
| 539 | int spec; |
| 540 | char *filename; |
| 541 | int flags, mode; |
| 542 | enum r_instruction ri; |
| 543 | { |
| 544 | int fd; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 545 | #if !defined (HAVE_DEV_FD) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 546 | intmax_t lfd; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 547 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 548 | |
| 549 | fd = -1; |
| 550 | switch (spec) |
| 551 | { |
| 552 | #if !defined (HAVE_DEV_FD) |
| 553 | case RF_DEVFD: |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 554 | if (all_digits (filename+8) && legal_number (filename+8, &lfd) && lfd == (int)lfd) |
| 555 | { |
| 556 | fd = lfd; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 557 | fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 558 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 559 | else |
| 560 | fd = AMBIGUOUS_REDIRECT; |
| 561 | break; |
| 562 | #endif |
| 563 | |
| 564 | #if !defined (HAVE_DEV_STDIN) |
| 565 | case RF_DEVSTDIN: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 566 | fd = fcntl (0, F_DUPFD, SHELL_FD_BASE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 567 | break; |
| 568 | case RF_DEVSTDOUT: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 569 | fd = fcntl (1, F_DUPFD, SHELL_FD_BASE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 570 | break; |
| 571 | case RF_DEVSTDERR: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 572 | fd = fcntl (2, F_DUPFD, SHELL_FD_BASE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 573 | break; |
| 574 | #endif |
| 575 | |
| 576 | #if defined (NETWORK_REDIRECTIONS) |
| 577 | case RF_DEVTCP: |
| 578 | case RF_DEVUDP: |
| 579 | #if defined (HAVE_NETWORK) |
| 580 | fd = netopen (filename); |
| 581 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 582 | internal_warning (_("/dev/(tcp|udp)/host/port not supported without networking")); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 583 | fd = open (filename, flags, mode); |
| 584 | #endif |
| 585 | break; |
| 586 | #endif /* NETWORK_REDIRECTIONS */ |
| 587 | } |
| 588 | |
| 589 | return fd; |
| 590 | } |
| 591 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 592 | /* Open FILENAME with FLAGS in noclobber mode, hopefully avoiding most |
| 593 | race conditions and avoiding the problem where the file is replaced |
| 594 | between the stat(2) and open(2). */ |
| 595 | static int |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 596 | noclobber_open (filename, flags, mode, ri) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 597 | char *filename; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 598 | int flags, mode; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 599 | enum r_instruction ri; |
| 600 | { |
| 601 | int r, fd; |
| 602 | struct stat finfo, finfo2; |
| 603 | |
| 604 | /* If the file exists and is a regular file, return an error |
| 605 | immediately. */ |
| 606 | r = stat (filename, &finfo); |
| 607 | if (r == 0 && (S_ISREG (finfo.st_mode))) |
| 608 | return (NOCLOBBER_REDIRECT); |
| 609 | |
| 610 | /* If the file was not present (r != 0), make sure we open it |
| 611 | exclusively so that if it is created before we open it, our open |
| 612 | will fail. Make sure that we do not truncate an existing file. |
| 613 | Note that we don't turn on O_EXCL unless the stat failed -- if |
| 614 | the file was not a regular file, we leave O_EXCL off. */ |
| 615 | flags &= ~O_TRUNC; |
| 616 | if (r != 0) |
| 617 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 618 | fd = open (filename, flags|O_EXCL, mode); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 619 | return ((fd < 0 && errno == EEXIST) ? NOCLOBBER_REDIRECT : fd); |
| 620 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 621 | fd = open (filename, flags, mode); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 622 | |
| 623 | /* If the open failed, return the file descriptor right away. */ |
| 624 | if (fd < 0) |
| 625 | return (errno == EEXIST ? NOCLOBBER_REDIRECT : fd); |
| 626 | |
| 627 | /* OK, the open succeeded, but the file may have been changed from a |
| 628 | non-regular file to a regular file between the stat and the open. |
| 629 | We are assuming that the O_EXCL open handles the case where FILENAME |
| 630 | did not exist and is symlinked to an existing file between the stat |
| 631 | and open. */ |
| 632 | |
| 633 | /* If we can open it and fstat the file descriptor, and neither check |
| 634 | revealed that it was a regular file, and the file has not been replaced, |
| 635 | return the file descriptor. */ |
| 636 | if ((fstat (fd, &finfo2) == 0) && (S_ISREG (finfo2.st_mode) == 0) && |
| 637 | r == 0 && (S_ISREG (finfo.st_mode) == 0) && |
| 638 | same_file (filename, filename, &finfo, &finfo2)) |
| 639 | return fd; |
| 640 | |
| 641 | /* The file has been replaced. badness. */ |
| 642 | close (fd); |
| 643 | errno = EEXIST; |
| 644 | return (NOCLOBBER_REDIRECT); |
| 645 | } |
| 646 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 647 | static int |
| 648 | redir_open (filename, flags, mode, ri) |
| 649 | char *filename; |
| 650 | int flags, mode; |
| 651 | enum r_instruction ri; |
| 652 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 653 | int fd, r, e; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 654 | |
| 655 | r = find_string_in_alist (filename, _redir_special_filenames, 1); |
| 656 | if (r >= 0) |
| 657 | return (redir_special_open (r, filename, flags, mode, ri)); |
| 658 | |
| 659 | /* If we are in noclobber mode, you are not allowed to overwrite |
| 660 | existing files. Check before opening. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 661 | if (noclobber && CLOBBERING_REDIRECT (ri)) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 662 | { |
| 663 | fd = noclobber_open (filename, flags, mode, ri); |
| 664 | if (fd == NOCLOBBER_REDIRECT) |
| 665 | return (NOCLOBBER_REDIRECT); |
| 666 | } |
| 667 | else |
| 668 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 669 | do |
| 670 | { |
| 671 | fd = open (filename, flags, mode); |
| 672 | e = errno; |
| 673 | if (fd < 0 && e == EINTR) |
| 674 | QUIT; |
| 675 | errno = e; |
| 676 | } |
| 677 | while (fd < 0 && errno == EINTR); |
| 678 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 679 | #if defined (AFS) |
| 680 | if ((fd < 0) && (errno == EACCES)) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 681 | { |
| 682 | fd = open (filename, flags & ~O_CREAT, mode); |
| 683 | errno = EACCES; /* restore errno */ |
| 684 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 685 | #endif /* AFS */ |
| 686 | } |
| 687 | |
| 688 | return fd; |
| 689 | } |
| 690 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 691 | static int |
| 692 | undoablefd (fd) |
| 693 | int fd; |
| 694 | { |
| 695 | int clexec; |
| 696 | |
| 697 | clexec = fcntl (fd, F_GETFD, 0); |
| 698 | if (clexec == -1 || (fd >= SHELL_FD_BASE && clexec == 1)) |
| 699 | return 0; |
| 700 | return 1; |
| 701 | } |
| 702 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 703 | /* Do the specific redirection requested. Returns errno or one of the |
| 704 | special redirection errors (*_REDIRECT) in case of error, 0 on success. |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 705 | If flags & RX_ACTIVE is zero, then just do whatever is necessary to |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 706 | produce the appropriate side effects. flags & RX_UNDOABLE, if non-zero, |
| 707 | says to remember how to undo each redirection. If flags & RX_CLEXEC is |
| 708 | non-zero, then we set all file descriptors > 2 that we open to be |
| 709 | close-on-exec. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 710 | static int |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 711 | do_redirection_internal (redirect, flags) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 712 | REDIRECT *redirect; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 713 | int flags; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 714 | { |
| 715 | WORD_DESC *redirectee; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 716 | int redir_fd, fd, redirector, r, oflags; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 717 | intmax_t lfd; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 718 | char *redirectee_word; |
| 719 | enum r_instruction ri; |
| 720 | REDIRECT *new_redirect; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 721 | REDIRECTEE sd; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 722 | |
| 723 | redirectee = redirect->redirectee.filename; |
| 724 | redir_fd = redirect->redirectee.dest; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 725 | redirector = redirect->redirector.dest; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 726 | ri = redirect->instruction; |
| 727 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 728 | if (redirect->flags & RX_INTERNAL) |
| 729 | flags |= RX_INTERNAL; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 730 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 731 | if (TRANSLATE_REDIRECT (ri)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 732 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 733 | /* We have [N]>&WORD[-] or [N]<&WORD[-] (or {V}>&WORD[-] or {V}<&WORD-). |
| 734 | and WORD, then translate the redirection into a new one and |
| 735 | continue. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 736 | redirectee_word = redirection_expand (redirectee); |
| 737 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 738 | /* XXX - what to do with [N]<&$w- where w is unset or null? ksh93 |
| 739 | closes N. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 740 | if (redirectee_word == 0) |
| 741 | return (AMBIGUOUS_REDIRECT); |
| 742 | else if (redirectee_word[0] == '-' && redirectee_word[1] == '\0') |
| 743 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 744 | sd = redirect->redirector; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 745 | rd.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 746 | new_redirect = make_redirection (sd, r_close_this, rd, 0); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 747 | } |
| 748 | else if (all_digits (redirectee_word)) |
| 749 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 750 | sd = redirect->redirector; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 751 | if (legal_number (redirectee_word, &lfd) && (int)lfd == lfd) |
| 752 | rd.dest = lfd; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 753 | else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 754 | rd.dest = -1; /* XXX */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 755 | switch (ri) |
| 756 | { |
| 757 | case r_duplicating_input_word: |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 758 | new_redirect = make_redirection (sd, r_duplicating_input, rd, 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 759 | break; |
| 760 | case r_duplicating_output_word: |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 761 | new_redirect = make_redirection (sd, r_duplicating_output, rd, 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 762 | break; |
| 763 | case r_move_input_word: |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 764 | new_redirect = make_redirection (sd, r_move_input, rd, 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 765 | break; |
| 766 | case r_move_output_word: |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 767 | new_redirect = make_redirection (sd, r_move_output, rd, 0); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 768 | break; |
| 769 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 770 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 771 | else if (ri == r_duplicating_output_word && (redirect->rflags & REDIR_VARASSIGN) == 0 && redirector == 1) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 772 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 773 | sd = redirect->redirector; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 774 | rd.filename = make_bare_word (redirectee_word); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 775 | new_redirect = make_redirection (sd, r_err_and_out, rd, 0); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 776 | } |
| 777 | else |
| 778 | { |
| 779 | free (redirectee_word); |
| 780 | return (AMBIGUOUS_REDIRECT); |
| 781 | } |
| 782 | |
| 783 | free (redirectee_word); |
| 784 | |
| 785 | /* Set up the variables needed by the rest of the function from the |
| 786 | new redirection. */ |
| 787 | if (new_redirect->instruction == r_err_and_out) |
| 788 | { |
| 789 | char *alloca_hack; |
| 790 | |
| 791 | /* Copy the word without allocating any memory that must be |
| 792 | explicitly freed. */ |
| 793 | redirectee = (WORD_DESC *)alloca (sizeof (WORD_DESC)); |
| 794 | xbcopy ((char *)new_redirect->redirectee.filename, |
| 795 | (char *)redirectee, sizeof (WORD_DESC)); |
| 796 | |
| 797 | alloca_hack = (char *) |
| 798 | alloca (1 + strlen (new_redirect->redirectee.filename->word)); |
| 799 | redirectee->word = alloca_hack; |
| 800 | strcpy (redirectee->word, new_redirect->redirectee.filename->word); |
| 801 | } |
| 802 | else |
| 803 | /* It's guaranteed to be an integer, and shouldn't be freed. */ |
| 804 | redirectee = new_redirect->redirectee.filename; |
| 805 | |
| 806 | redir_fd = new_redirect->redirectee.dest; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 807 | redirector = new_redirect->redirector.dest; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 808 | ri = new_redirect->instruction; |
| 809 | |
| 810 | /* Overwrite the flags element of the old redirect with the new value. */ |
| 811 | redirect->flags = new_redirect->flags; |
| 812 | dispose_redirects (new_redirect); |
| 813 | } |
| 814 | |
| 815 | switch (ri) |
| 816 | { |
| 817 | case r_output_direction: |
| 818 | case r_appending_to: |
| 819 | case r_input_direction: |
| 820 | case r_inputa_direction: |
| 821 | case r_err_and_out: /* command &>filename */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 822 | case r_append_err_and_out: /* command &>> filename */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 823 | case r_input_output: |
| 824 | case r_output_force: |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 825 | if (posixly_correct && interactive_shell == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 826 | { |
| 827 | oflags = redirectee->flags; |
| 828 | redirectee->flags |= W_NOGLOB; |
| 829 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 830 | redirectee_word = redirection_expand (redirectee); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 831 | if (posixly_correct && interactive_shell == 0) |
| 832 | redirectee->flags = oflags; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 833 | |
| 834 | if (redirectee_word == 0) |
| 835 | return (AMBIGUOUS_REDIRECT); |
| 836 | |
| 837 | #if defined (RESTRICTED_SHELL) |
| 838 | if (restricted && (WRITE_REDIRECT (ri))) |
| 839 | { |
| 840 | free (redirectee_word); |
| 841 | return (RESTRICTED_REDIRECT); |
| 842 | } |
| 843 | #endif /* RESTRICTED_SHELL */ |
| 844 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 845 | fd = redir_open (redirectee_word, redirect->flags, 0666, ri); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 846 | free (redirectee_word); |
| 847 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 848 | if (fd == NOCLOBBER_REDIRECT) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 849 | return (fd); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 850 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 851 | if (fd < 0) |
| 852 | return (errno); |
| 853 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 854 | if (flags & RX_ACTIVE) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 855 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 856 | if (redirect->rflags & REDIR_VARASSIGN) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 857 | { |
| 858 | redirector = fcntl (fd, F_DUPFD, SHELL_FD_BASE); /* XXX try this for now */ |
| 859 | r = errno; |
| 860 | if (redirector < 0) |
| 861 | sys_error (_("redirection error: cannot duplicate fd")); |
| 862 | REDIRECTION_ERROR (redirector, r, fd); |
| 863 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 864 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 865 | if ((flags & RX_UNDOABLE) && (redirect->rflags & REDIR_VARASSIGN) == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 866 | { |
| 867 | /* Only setup to undo it if the thing to undo is active. */ |
| 868 | if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1)) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 869 | r = add_undo_redirect (redirector, ri, -1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 870 | else |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 871 | r = add_undo_close_redirect (redirector); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 872 | REDIRECTION_ERROR (r, errno, fd); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 873 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 874 | |
| 875 | #if defined (BUFFERED_INPUT) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 876 | /* inhibit call to sync_buffered_stream() for async processes */ |
| 877 | if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0) |
| 878 | check_bash_input (redirector); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 879 | #endif |
| 880 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 881 | /* Make sure there is no pending output before we change the state |
| 882 | of the underlying file descriptor, since the builtins use stdio |
| 883 | for output. */ |
| 884 | if (redirector == 1 && fileno (stdout) == redirector) |
| 885 | { |
| 886 | fflush (stdout); |
| 887 | fpurge (stdout); |
| 888 | } |
| 889 | else if (redirector == 2 && fileno (stderr) == redirector) |
| 890 | { |
| 891 | fflush (stderr); |
| 892 | fpurge (stderr); |
| 893 | } |
| 894 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 895 | if (redirect->rflags & REDIR_VARASSIGN) |
| 896 | { |
| 897 | if ((r = redir_varassign (redirect, redirector)) < 0) |
| 898 | { |
| 899 | close (redirector); |
| 900 | close (fd); |
| 901 | return (r); /* XXX */ |
| 902 | } |
| 903 | } |
| 904 | else if ((fd != redirector) && (dup2 (fd, redirector) < 0)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 905 | return (errno); |
| 906 | |
| 907 | #if defined (BUFFERED_INPUT) |
| 908 | /* Do not change the buffered stream for an implicit redirection |
| 909 | of /dev/null to fd 0 for asynchronous commands without job |
| 910 | control (r_inputa_direction). */ |
| 911 | if (ri == r_input_direction || ri == r_input_output) |
| 912 | duplicate_buffered_stream (fd, redirector); |
| 913 | #endif /* BUFFERED_INPUT */ |
| 914 | |
| 915 | /* |
| 916 | * If we're remembering, then this is the result of a while, for |
| 917 | * or until loop with a loop redirection, or a function/builtin |
| 918 | * executing in the parent shell with a redirection. In the |
| 919 | * function/builtin case, we want to set all file descriptors > 2 |
| 920 | * to be close-on-exec to duplicate the effect of the old |
| 921 | * for i = 3 to NOFILE close(i) loop. In the case of the loops, |
| 922 | * both sh and ksh leave the file descriptors open across execs. |
| 923 | * The Posix standard mentions only the exec builtin. |
| 924 | */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 925 | if ((flags & RX_CLEXEC) && (redirector > 2)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 926 | SET_CLOSE_ON_EXEC (redirector); |
| 927 | } |
| 928 | |
| 929 | if (fd != redirector) |
| 930 | { |
| 931 | #if defined (BUFFERED_INPUT) |
| 932 | if (INPUT_REDIRECT (ri)) |
| 933 | close_buffered_fd (fd); |
| 934 | else |
| 935 | #endif /* !BUFFERED_INPUT */ |
| 936 | close (fd); /* Don't close what we just opened! */ |
| 937 | } |
| 938 | |
| 939 | /* If we are hacking both stdout and stderr, do the stderr |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 940 | redirection here. XXX - handle {var} here? */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 941 | if (ri == r_err_and_out || ri == r_append_err_and_out) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 942 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 943 | if (flags & RX_ACTIVE) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 944 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 945 | if (flags & RX_UNDOABLE) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 946 | add_undo_redirect (2, ri, -1); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 947 | if (dup2 (1, 2) < 0) |
| 948 | return (errno); |
| 949 | } |
| 950 | } |
| 951 | break; |
| 952 | |
| 953 | case r_reading_until: |
| 954 | case r_deblank_reading_until: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 955 | case r_reading_string: |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 956 | /* REDIRECTEE is a pointer to a WORD_DESC containing the text of |
| 957 | the new input. Place it in a temporary file. */ |
| 958 | if (redirectee) |
| 959 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 960 | fd = here_document_to_fd (redirectee, ri); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 961 | |
| 962 | if (fd < 0) |
| 963 | { |
| 964 | heredoc_errno = errno; |
| 965 | return (HEREDOC_REDIRECT); |
| 966 | } |
| 967 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 968 | if (redirect->rflags & REDIR_VARASSIGN) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 969 | { |
| 970 | redirector = fcntl (fd, F_DUPFD, SHELL_FD_BASE); /* XXX try this for now */ |
| 971 | r = errno; |
| 972 | if (redirector < 0) |
| 973 | sys_error (_("redirection error: cannot duplicate fd")); |
| 974 | REDIRECTION_ERROR (redirector, r, fd); |
| 975 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 976 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 977 | if (flags & RX_ACTIVE) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 978 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 979 | if ((flags & RX_UNDOABLE) && (redirect->rflags & REDIR_VARASSIGN) == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 980 | { |
| 981 | /* Only setup to undo it if the thing to undo is active. */ |
| 982 | if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1)) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 983 | r = add_undo_redirect (redirector, ri, -1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 984 | else |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 985 | r = add_undo_close_redirect (redirector); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 986 | REDIRECTION_ERROR (r, errno, fd); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 987 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 988 | |
| 989 | #if defined (BUFFERED_INPUT) |
| 990 | check_bash_input (redirector); |
| 991 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 992 | if (redirect->rflags & REDIR_VARASSIGN) |
| 993 | { |
| 994 | if ((r = redir_varassign (redirect, redirector)) < 0) |
| 995 | { |
| 996 | close (redirector); |
| 997 | close (fd); |
| 998 | return (r); /* XXX */ |
| 999 | } |
| 1000 | } |
| 1001 | else if (fd != redirector && dup2 (fd, redirector) < 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1002 | { |
| 1003 | r = errno; |
| 1004 | close (fd); |
| 1005 | return (r); |
| 1006 | } |
| 1007 | |
| 1008 | #if defined (BUFFERED_INPUT) |
| 1009 | duplicate_buffered_stream (fd, redirector); |
| 1010 | #endif |
| 1011 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1012 | if ((flags & RX_CLEXEC) && (redirector > 2)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1013 | SET_CLOSE_ON_EXEC (redirector); |
| 1014 | } |
| 1015 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1016 | if (fd != redirector) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1017 | #if defined (BUFFERED_INPUT) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1018 | close_buffered_fd (fd); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1019 | #else |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1020 | close (fd); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1021 | #endif |
| 1022 | } |
| 1023 | break; |
| 1024 | |
| 1025 | case r_duplicating_input: |
| 1026 | case r_duplicating_output: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1027 | case r_move_input: |
| 1028 | case r_move_output: |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1029 | if ((flags & RX_ACTIVE) && (redirect->rflags & REDIR_VARASSIGN)) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1030 | { |
| 1031 | redirector = fcntl (redir_fd, F_DUPFD, SHELL_FD_BASE); /* XXX try this for now */ |
| 1032 | r = errno; |
| 1033 | if (redirector < 0) |
| 1034 | sys_error (_("redirection error: cannot duplicate fd")); |
| 1035 | REDIRECTION_ERROR (redirector, r, -1); |
| 1036 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1037 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1038 | if ((flags & RX_ACTIVE) && (redir_fd != redirector)) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1039 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1040 | if ((flags & RX_UNDOABLE) && (redirect->rflags & REDIR_VARASSIGN) == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1041 | { |
| 1042 | /* Only setup to undo it if the thing to undo is active. */ |
| 1043 | if (fcntl (redirector, F_GETFD, 0) != -1) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1044 | r = add_undo_redirect (redirector, ri, redir_fd); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1045 | else |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1046 | r = add_undo_close_redirect (redirector); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1047 | REDIRECTION_ERROR (r, errno, -1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1048 | } |
Chet Ramey | f281b8f | 2013-03-07 15:22:14 -0500 | [diff] [blame] | 1049 | if ((flags & RX_UNDOABLE) && (ri == r_move_input || ri == r_move_output)) |
| 1050 | { |
| 1051 | /* r_move_input and r_move_output add an additional close() |
| 1052 | that needs to be undone */ |
| 1053 | if (fcntl (redirector, F_GETFD, 0) != -1) |
| 1054 | { |
| 1055 | r = add_undo_redirect (redir_fd, r_close_this, -1); |
| 1056 | REDIRECTION_ERROR (r, errno, -1); |
| 1057 | } |
| 1058 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1059 | #if defined (BUFFERED_INPUT) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1060 | /* inhibit call to sync_buffered_stream() for async processes */ |
| 1061 | if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0) |
| 1062 | check_bash_input (redirector); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1063 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1064 | if (redirect->rflags & REDIR_VARASSIGN) |
| 1065 | { |
| 1066 | if ((r = redir_varassign (redirect, redirector)) < 0) |
| 1067 | { |
| 1068 | close (redirector); |
| 1069 | return (r); /* XXX */ |
| 1070 | } |
| 1071 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1072 | /* This is correct. 2>&1 means dup2 (1, 2); */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1073 | else if (dup2 (redir_fd, redirector) < 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1074 | return (errno); |
| 1075 | |
| 1076 | #if defined (BUFFERED_INPUT) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1077 | if (ri == r_duplicating_input || ri == r_move_input) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1078 | duplicate_buffered_stream (redir_fd, redirector); |
| 1079 | #endif /* BUFFERED_INPUT */ |
| 1080 | |
| 1081 | /* First duplicate the close-on-exec state of redirectee. dup2 |
| 1082 | leaves the flag unset on the new descriptor, which means it |
| 1083 | stays open. Only set the close-on-exec bit for file descriptors |
| 1084 | greater than 2 in any case, since 0-2 should always be open |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1085 | unless closed by something like `exec 2<&-'. It should always |
| 1086 | be safe to set fds > 2 to close-on-exec if they're being used to |
| 1087 | save file descriptors < 2, since we don't need to preserve the |
| 1088 | state of the close-on-exec flag for those fds -- they should |
| 1089 | always be open. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1090 | /* if ((already_set || set_unconditionally) && (ok_to_set)) |
| 1091 | set_it () */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1092 | #if 0 |
| 1093 | if (((fcntl (redir_fd, F_GETFD, 0) == 1) || redir_fd < 2 || (flags & RX_CLEXEC)) && |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1094 | (redirector > 2)) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1095 | #else |
| 1096 | if (((fcntl (redir_fd, F_GETFD, 0) == 1) || (redir_fd < 2 && (flags & RX_INTERNAL)) || (flags & RX_CLEXEC)) && |
| 1097 | (redirector > 2)) |
| 1098 | #endif |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1099 | SET_CLOSE_ON_EXEC (redirector); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1100 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1101 | /* When undoing saving of non-standard file descriptors (>=3) using |
| 1102 | file descriptors >= SHELL_FD_BASE, we set the saving fd to be |
| 1103 | close-on-exec and use a flag to decide how to set close-on-exec |
| 1104 | when the fd is restored. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1105 | if ((redirect->flags & RX_INTERNAL) && (redirect->flags & RX_SAVCLEXEC) && redirector >= 3 && (redir_fd >= SHELL_FD_BASE || (redirect->flags & RX_SAVEFD))) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1106 | SET_OPEN_ON_EXEC (redirector); |
| 1107 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1108 | /* dup-and-close redirection */ |
| 1109 | if (ri == r_move_input || ri == r_move_output) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1110 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1111 | xtrace_fdchk (redir_fd); |
| 1112 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1113 | close (redir_fd); |
| 1114 | #if defined (COPROCESS_SUPPORT) |
| 1115 | coproc_fdchk (redir_fd); /* XXX - loses coproc fds */ |
| 1116 | #endif |
| 1117 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1118 | } |
| 1119 | break; |
| 1120 | |
| 1121 | case r_close_this: |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1122 | if (flags & RX_ACTIVE) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1123 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1124 | if (redirect->rflags & REDIR_VARASSIGN) |
| 1125 | { |
| 1126 | redirector = redir_varvalue (redirect); |
| 1127 | if (redirector < 0) |
| 1128 | return AMBIGUOUS_REDIRECT; |
| 1129 | } |
| 1130 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1131 | r = 0; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1132 | /* XXX - only if REDIR_VARASSIGN not set? */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1133 | if ((flags & RX_UNDOABLE) && (fcntl (redirector, F_GETFD, 0) != -1)) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1134 | { |
| 1135 | r = add_undo_redirect (redirector, ri, -1); |
| 1136 | REDIRECTION_ERROR (r, errno, redirector); |
| 1137 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1138 | |
| 1139 | #if defined (COPROCESS_SUPPORT) |
| 1140 | coproc_fdchk (redirector); |
| 1141 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1142 | xtrace_fdchk (redirector); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1143 | |
| 1144 | #if defined (BUFFERED_INPUT) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1145 | /* inhibit call to sync_buffered_stream() for async processes */ |
| 1146 | if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0) |
| 1147 | check_bash_input (redirector); |
Chet Ramey | 774d3bf | 2013-01-10 19:47:54 -0500 | [diff] [blame] | 1148 | r = close_buffered_fd (redirector); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1149 | #else /* !BUFFERED_INPUT */ |
Chet Ramey | 774d3bf | 2013-01-10 19:47:54 -0500 | [diff] [blame] | 1150 | r = close (redirector); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1151 | #endif /* !BUFFERED_INPUT */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1152 | |
Chet Ramey | 774d3bf | 2013-01-10 19:47:54 -0500 | [diff] [blame] | 1153 | if (r < 0 && (flags & RX_INTERNAL) && (errno == EIO || errno == ENOSPC)) |
| 1154 | REDIRECTION_ERROR (r, errno, -1); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1155 | } |
| 1156 | break; |
| 1157 | |
| 1158 | case r_duplicating_input_word: |
| 1159 | case r_duplicating_output_word: |
| 1160 | break; |
| 1161 | } |
| 1162 | return (0); |
| 1163 | } |
| 1164 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1165 | /* Remember the file descriptor associated with the slot FD, |
| 1166 | on REDIRECTION_UNDO_LIST. Note that the list will be reversed |
| 1167 | before it is executed. Any redirections that need to be undone |
| 1168 | even if REDIRECTION_UNDO_LIST is discarded by the exec builtin |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1169 | are also saved on EXEC_REDIRECTION_UNDO_LIST. FDBASE says where to |
| 1170 | start the duplicating. If it's less than SHELL_FD_BASE, we're ok, |
| 1171 | and can use SHELL_FD_BASE (-1 == don't care). If it's >= SHELL_FD_BASE, |
| 1172 | we have to make sure we don't use fdbase to save a file descriptor, |
| 1173 | since we're going to use it later (e.g., make sure we don't save fd 0 |
| 1174 | to fd 10 if we have a redirection like 0<&10). If the value of fdbase |
| 1175 | puts the process over its fd limit, causing fcntl to fail, we try |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1176 | again with SHELL_FD_BASE. Return 0 on success, -1 on error. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1177 | static int |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1178 | add_undo_redirect (fd, ri, fdbase) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1179 | int fd; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1180 | enum r_instruction ri; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1181 | int fdbase; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1182 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1183 | int new_fd, clexec_flag, savefd_flag; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1184 | REDIRECT *new_redirect, *closer, *dummy_redirect; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1185 | REDIRECTEE sd; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1186 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1187 | savefd_flag = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1188 | new_fd = fcntl (fd, F_DUPFD, (fdbase < SHELL_FD_BASE) ? SHELL_FD_BASE : fdbase+1); |
| 1189 | if (new_fd < 0) |
| 1190 | new_fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1191 | if (new_fd < 0) |
| 1192 | { |
| 1193 | new_fd = fcntl (fd, F_DUPFD, 0); |
| 1194 | savefd_flag = 1; |
| 1195 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (new_fd < 0) |
| 1198 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1199 | sys_error (_("redirection error: cannot duplicate fd")); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1200 | return (-1); |
| 1201 | } |
| 1202 | |
| 1203 | clexec_flag = fcntl (fd, F_GETFD, 0); |
| 1204 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1205 | sd.dest = new_fd; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1206 | rd.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1207 | closer = make_redirection (sd, r_close_this, rd, 0); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1208 | closer->flags |= RX_INTERNAL; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1209 | dummy_redirect = copy_redirects (closer); |
| 1210 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1211 | sd.dest = fd; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1212 | rd.dest = new_fd; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1213 | if (fd == 0) |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1214 | new_redirect = make_redirection (sd, r_duplicating_input, rd, 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 1215 | else |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1216 | new_redirect = make_redirection (sd, r_duplicating_output, rd, 0); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1217 | new_redirect->flags |= RX_INTERNAL; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1218 | if (savefd_flag) |
| 1219 | new_redirect->flags |= RX_SAVEFD; |
| 1220 | if (clexec_flag == 0 && fd >= 3 && (new_fd >= SHELL_FD_BASE || savefd_flag)) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1221 | new_redirect->flags |= RX_SAVCLEXEC; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1222 | new_redirect->next = closer; |
| 1223 | |
| 1224 | closer->next = redirection_undo_list; |
| 1225 | redirection_undo_list = new_redirect; |
| 1226 | |
| 1227 | /* Save redirections that need to be undone even if the undo list |
| 1228 | is thrown away by the `exec' builtin. */ |
| 1229 | add_exec_redirect (dummy_redirect); |
| 1230 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1231 | /* experimental: if we're saving a redirection to undo for a file descriptor |
| 1232 | above SHELL_FD_BASE, add a redirection to be undone if the exec builtin |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1233 | causes redirections to be discarded. There needs to be a difference |
| 1234 | between fds that are used to save other fds and then are the target of |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1235 | user redirections and fds that are just the target of user redirections. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1236 | We use the close-on-exec flag to tell the difference; fds > SHELL_FD_BASE |
| 1237 | that have the close-on-exec flag set are assumed to be fds used internally |
| 1238 | to save others. */ |
| 1239 | if (fd >= SHELL_FD_BASE && ri != r_close_this && clexec_flag) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1240 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1241 | sd.dest = fd; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1242 | rd.dest = new_fd; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1243 | new_redirect = make_redirection (sd, r_duplicating_output, rd, 0); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1244 | new_redirect->flags |= RX_INTERNAL; |
| 1245 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1246 | add_exec_redirect (new_redirect); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1249 | /* File descriptors used only for saving others should always be |
| 1250 | marked close-on-exec. Unfortunately, we have to preserve the |
| 1251 | close-on-exec state of the file descriptor we are saving, since |
| 1252 | fcntl (F_DUPFD) sets the new file descriptor to remain open |
| 1253 | across execs. If, however, the file descriptor whose state we |
| 1254 | are saving is <= 2, we can just set the close-on-exec flag, |
| 1255 | because file descriptors 0-2 should always be open-on-exec, |
| 1256 | and the restore above in do_redirection() will take care of it. */ |
| 1257 | if (clexec_flag || fd < 3) |
| 1258 | SET_CLOSE_ON_EXEC (new_fd); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1259 | else if (redirection_undo_list->flags & RX_SAVCLEXEC) |
| 1260 | SET_CLOSE_ON_EXEC (new_fd); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1261 | |
| 1262 | return (0); |
| 1263 | } |
| 1264 | |
| 1265 | /* Set up to close FD when we are finished with the current command |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1266 | and its redirections. Return 0 on success, -1 on error. */ |
| 1267 | static int |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1268 | add_undo_close_redirect (fd) |
| 1269 | int fd; |
| 1270 | { |
| 1271 | REDIRECT *closer; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1272 | REDIRECTEE sd; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1273 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1274 | sd.dest = fd; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1275 | rd.dest = 0; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1276 | closer = make_redirection (sd, r_close_this, rd, 0); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1277 | closer->flags |= RX_INTERNAL; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1278 | closer->next = redirection_undo_list; |
| 1279 | redirection_undo_list = closer; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1280 | |
| 1281 | return 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static void |
| 1285 | add_exec_redirect (dummy_redirect) |
| 1286 | REDIRECT *dummy_redirect; |
| 1287 | { |
| 1288 | dummy_redirect->next = exec_redirection_undo_list; |
| 1289 | exec_redirection_undo_list = dummy_redirect; |
| 1290 | } |
| 1291 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1292 | /* Return 1 if the redirection specified by RI and REDIRECTOR alters the |
| 1293 | standard input. */ |
| 1294 | static int |
| 1295 | stdin_redirection (ri, redirector) |
| 1296 | enum r_instruction ri; |
| 1297 | int redirector; |
| 1298 | { |
| 1299 | switch (ri) |
| 1300 | { |
| 1301 | case r_input_direction: |
| 1302 | case r_inputa_direction: |
| 1303 | case r_input_output: |
| 1304 | case r_reading_until: |
| 1305 | case r_deblank_reading_until: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1306 | case r_reading_string: |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1307 | return (1); |
| 1308 | case r_duplicating_input: |
| 1309 | case r_duplicating_input_word: |
| 1310 | case r_close_this: |
| 1311 | return (redirector == 0); |
| 1312 | case r_output_direction: |
| 1313 | case r_appending_to: |
| 1314 | case r_duplicating_output: |
| 1315 | case r_err_and_out: |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1316 | case r_append_err_and_out: |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1317 | case r_output_force: |
| 1318 | case r_duplicating_output_word: |
| 1319 | return (0); |
| 1320 | } |
| 1321 | return (0); |
| 1322 | } |
| 1323 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1324 | /* Return non-zero if any of the redirections in REDIRS alter the standard |
| 1325 | input. */ |
| 1326 | int |
| 1327 | stdin_redirects (redirs) |
| 1328 | REDIRECT *redirs; |
| 1329 | { |
| 1330 | REDIRECT *rp; |
| 1331 | int n; |
| 1332 | |
| 1333 | for (n = 0, rp = redirs; rp; rp = rp->next) |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1334 | if ((rp->rflags & REDIR_VARASSIGN) == 0) |
| 1335 | n += stdin_redirection (rp->instruction, rp->redirector.dest); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1336 | return n; |
| 1337 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1338 | /* bind_var_to_int handles array references */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1339 | static int |
| 1340 | redir_varassign (redir, fd) |
| 1341 | REDIRECT *redir; |
| 1342 | int fd; |
| 1343 | { |
| 1344 | WORD_DESC *w; |
| 1345 | SHELL_VAR *v; |
| 1346 | |
| 1347 | w = redir->redirector.filename; |
| 1348 | v = bind_var_to_int (w->word, fd); |
| 1349 | if (v == 0 || readonly_p (v) || noassign_p (v)) |
| 1350 | return BADVAR_REDIRECT; |
| 1351 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1352 | stupidly_hack_special_variables (w->word); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1353 | return 0; |
| 1354 | } |
| 1355 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1356 | /* Handles {array[ind]} for redirection words */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1357 | static int |
| 1358 | redir_varvalue (redir) |
| 1359 | REDIRECT *redir; |
| 1360 | { |
| 1361 | SHELL_VAR *v; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1362 | char *val, *w; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1363 | intmax_t vmax; |
| 1364 | int i; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1365 | #if defined (ARRAY_VARS) |
| 1366 | char *sub; |
| 1367 | int len, vr; |
| 1368 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1369 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1370 | w = redir->redirector.filename->word; /* shorthand */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1371 | /* XXX - handle set -u here? */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1372 | #if defined (ARRAY_VARS) |
| 1373 | if (vr = valid_array_reference (w)) |
| 1374 | v = array_variable_part (w, &sub, &len); |
| 1375 | else |
| 1376 | #endif |
| 1377 | v = find_variable (w); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1378 | if (v == 0 || invisible_p (v)) |
| 1379 | return -1; |
| 1380 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1381 | #if defined (ARRAY_VARS) |
| 1382 | /* get_variable_value handles references to array variables without |
| 1383 | subscripts */ |
| 1384 | if (vr && (array_p (v) || assoc_p (v))) |
| 1385 | val = get_array_value (w, 0, (int *)NULL, (arrayind_t *)0); |
| 1386 | else |
| 1387 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1388 | val = get_variable_value (v); |
| 1389 | if (val == 0 || *val == 0) |
| 1390 | return -1; |
| 1391 | |
| 1392 | if (legal_number (val, &vmax) < 0) |
| 1393 | return -1; |
| 1394 | |
| 1395 | i = vmax; /* integer truncation */ |
| 1396 | return i; |
| 1397 | } |