forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redir.cc
1469 lines (1299 loc) · 44.6 KB
/
redir.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* redir.cc -- Functions to perform input and output redirection. */
/* Copyright (C) 1997-2020 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "bashtypes.hh"
#if defined(HAVE_SYS_FILE_H)
#include <sys/file.h>
#endif
#include "posixstat.hh"
#include <unistd.h>
#include "bashintl.hh"
#include "flags.hh"
#include "shell.hh"
#include "trap.hh"
#if defined(BUFFERED_INPUT)
#include "input.hh"
#endif
#include "pipesize.hh"
namespace bash
{
/* FreeBSD 13 can reliably handle atomic writes at this capacity without
hanging. */
#if defined(__FreeBSD__) && !defined(HEREDOC_PIPESIZE)
#define HEREDOC_PIPESIZE 4096
#endif
/* Normally set by a build process command that computes pipe capacity */
#ifndef PIPESIZE
#ifdef PIPE_BUF
#define PIPESIZE PIPE_BUF
#else
#define PIPESIZE 4096
#endif
#endif
#ifndef HEREDOC_PIPESIZE
#define HEREDOC_PIPESIZE PIPESIZE
#endif
#if defined(HEREDOC_PIPEMAX)
#if HEREDOC_PIPESIZE > HEREDOC_PIPEMAX
#define HEREDOC_PIPESIZE HEREDOC_PIPEMAX
#endif
#endif
#define SHELL_FD_BASE 10
static bool expandable_redirection_filename (REDIRECT *);
#if 0
extern REDIRECT *redirection_undo_list;
extern REDIRECT *exec_redirection_undo_list;
/* Static functions defined and used in this file. */
static void add_exec_redirect (REDIRECT *);
static int add_undo_redirect (int, r_instruction, int);
static int add_undo_close_redirect (int);
static int stdin_redirection (r_instruction, int);
static int undoablefd (int);
static int do_redirection_internal (REDIRECT *, int, char **);
static char *heredoc_expand (WordDesc *, r_instruction, size_t *);
static int heredoc_write (int, char *, size_t);
static int here_document_to_fd (WordDesc *, r_instruction);
static int redir_special_open (int, char *, int, int, r_instruction);
static int noclobber_open (char *, int, int, r_instruction);
static int redir_open (char *, int, int, r_instruction);
static int redir_varassign (REDIRECT *, int);
static int redir_varvalue (REDIRECT *);
/* Spare redirector used when translating [N]>&WORD[-] or [N]<&WORD[-] to
a new redirection and when creating the redirection undo list. */
static REDIRECTEE rd;
#endif
#define REDIRECTION_ERROR(r, e, fd) \
do \
{ \
if ((r) < 0) \
{ \
if (fd >= 0) \
close (fd); \
set_exit_status (EXECUTION_FAILURE); \
return (e) == 0 ? EINVAL : (e); \
} \
} \
while (0)
void
Shell::redirection_error (REDIRECT *temp, int error,
const char *fn) /* already-expanded filename */
{
const char *filename;
char *allocname = nullptr;
word_desc_flags oflags;
if ((temp->rflags & REDIR_VARASSIGN) && error < 0)
filename = allocname = savestring (temp->redirector.filename->word);
else if ((temp->rflags & REDIR_VARASSIGN) == 0 && temp->redirector.dest < 0)
/* This can happen when read_token_word encounters overflow, like in
exec 4294967297>x */
filename = _ ("file descriptor out of range");
#ifdef EBADF
/* This error can never involve NOCLOBBER */
else if (error != NOCLOBBER_REDIRECT && temp->redirector.dest >= 0
&& error == EBADF)
{
/* If we're dealing with two file descriptors, we have to guess about
which one is invalid; in the cases of r_{duplicating,move}_input and
r_{duplicating,move}_output we're here because dup2() failed. */
switch (temp->instruction)
{
case r_duplicating_input:
case r_duplicating_output:
case r_move_input:
case r_move_output:
filename = allocname = itos (temp->redirectee.dest);
break;
case r_duplicating_input_word:
if (temp->redirector.dest == 0) /* Guess */
filename = temp->redirectee.filename->word.c_str (); /* XXX */
else
filename = allocname = itos (temp->redirector.dest);
break;
case r_duplicating_output_word:
if (temp->redirector.dest == 1) /* Guess */
filename = temp->redirectee.filename->word.c_str (); /* XXX */
else
filename = allocname = itos (temp->redirector.dest);
break;
default:
filename = allocname = itos (temp->redirector.dest);
break;
}
}
#endif
else if (fn)
filename = fn;
else if (expandable_redirection_filename (temp))
{
oflags = temp->redirectee.filename->flags;
if (posixly_correct && interactive_shell == 0)
temp->redirectee.filename->flags |= W_NOGLOB;
temp->redirectee.filename->flags |= W_NOCOMSUB;
filename = allocname = redirection_expand (temp->redirectee.filename);
temp->redirectee.filename->flags = oflags;
if (filename == nullptr)
filename = temp->redirectee.filename->word.c_str ();
}
else if (temp->redirectee.dest < 0)
filename = _ ("file descriptor out of range");
else
filename = allocname = itos (temp->redirectee.dest);
switch (error)
{
case AMBIGUOUS_REDIRECT:
internal_error (_ ("%s: ambiguous redirect"), filename);
break;
case NOCLOBBER_REDIRECT:
internal_error (_ ("%s: cannot overwrite existing file"), filename);
break;
#if defined(RESTRICTED_SHELL)
case RESTRICTED_REDIRECT:
internal_error (_ ("%s: restricted: cannot redirect output"), filename);
break;
#endif /* RESTRICTED_SHELL */
case HEREDOC_REDIRECT:
internal_error (_ ("cannot create temp file for here-document: %s"),
strerror (heredoc_errno));
break;
case BADVAR_REDIRECT:
internal_error (_ ("%s: cannot assign fd to variable"), filename);
break;
default:
internal_error ("%s: %s", filename, strerror (error));
break;
}
delete[] allocname;
}
/* Perform the redirections on LIST. If flags & RX_ACTIVE, then actually
make input and output file descriptors, otherwise just do whatever is
necessary for side effecting. flags & RX_UNDOABLE says to remember
how to undo the redirections later, if non-zero. If flags & RX_CLEXEC
is non-zero, file descriptors opened in do_redirection () have their
close-on-exec flag set. */
int
Shell::do_redirections (REDIRECT *list, redir_flags flags)
{
int error;
REDIRECT *temp;
char *fn;
if (flags & RX_UNDOABLE)
{
if (redirection_undo_list)
{
dispose_redirects (redirection_undo_list);
redirection_undo_list = (REDIRECT *)NULL;
}
if (exec_redirection_undo_list)
dispose_exec_redirects ();
}
for (temp = list; temp; temp = temp->next)
{
fn = 0;
error = do_redirection_internal (temp, flags, &fn);
if (error)
{
redirection_error (temp, error, fn);
FREE (fn);
return error;
}
FREE (fn);
}
return 0;
}
/* Return non-zero if the redirection pointed to by REDIRECT has a
redirectee.filename that can be expanded. */
static bool
expandable_redirection_filename (REDIRECT *redirect)
{
switch (redirect->instruction)
{
case r_output_direction:
case r_appending_to:
case r_input_direction:
case r_inputa_direction:
case r_err_and_out:
case r_append_err_and_out:
case r_input_output:
case r_output_force:
case r_duplicating_input_word:
case r_duplicating_output_word:
case r_move_input_word:
case r_move_output_word:
return true;
default:
return false;
}
}
/* Expand the word in WORD returning a string. If WORD expands to
multiple words (or no words), then return NULL. */
char *
Shell::redirection_expand (WORD_DESC *word)
{
char *result;
WORD_LIST *tlist1, *tlist2;
WORD_DESC *w;
int old;
w = new WORD_DESC (*word);
if (posixly_correct)
w->flags |= W_NOSPLIT;
tlist1 = make_word_list (w, (WORD_LIST *)NULL);
expanding_redir = 1;
/* Now that we've changed the variable search order to ignore the temp
environment, see if we need to change the cached IFS values. */
sv_ifs ("IFS");
tlist2 = expand_words_no_vars (tlist1);
expanding_redir = 0;
/* Now we need to change the variable search order back to include the temp
environment. We force the temp environment search by forcing
executing_builtin to 1. This is what makes `read' get the right values
for the IFS-related cached variables, for example. */
old = executing_builtin;
executing_builtin = 1;
sv_ifs ("IFS");
executing_builtin = old;
dispose_words (tlist1);
if (tlist2 == 0 || tlist2->next)
{
/* We expanded to no words, or to more than a single word.
Dispose of the word list and return NULL. */
if (tlist2)
dispose_words (tlist2);
return nullptr;
}
result = string_list (tlist2); /* XXX savestring (tlist2->word->word)? */
dispose_words (tlist2);
return result;
}
/* Expand a here-document or here-string (determined by RI) contained in
REDIRECTEE and return the expanded document. If LENP is non-zero, put
the length of the returned string into *LENP.
This captures everything about expanding here-documents and here-strings:
the returned document should be written directly to whatever file
descriptor is specified. In particular, it adds a newline to the end of
a here-string to preserve previous semantics. */
char *
Shell::heredoc_expand (WORD_DESC *redirectee, enum r_instruction ri,
size_t *lenp)
{
char *document;
size_t dlen;
int old;
if (redirectee->word == 0 || redirectee->word[0] == '\0')
{
if (lenp)
*lenp = 0;
return redirectee->word;
}
/* Quoted here documents are not expanded */
if (ri != r_reading_string && (redirectee->flags & W_QUOTED))
{
if (lenp)
*lenp = STRLEN (redirectee->word);
return redirectee->word;
}
expanding_redir = 1;
/* Now that we've changed the variable search order to ignore the temp
environment, see if we need to change the cached IFS values. */
sv_ifs ("IFS");
document = (ri == r_reading_string)
? expand_assignment_string_to_string (redirectee->word, 0)
: expand_string_to_string (redirectee->word, Q_HERE_DOCUMENT);
expanding_redir = 0;
/* Now we need to change the variable search order back to include the temp
environment. We force the temp environment search by forcing
executing_builtin to 1. This is what makes `read' get the right values
for the IFS-related cached variables, for example. */
old = executing_builtin;
executing_builtin = 1;
sv_ifs ("IFS");
executing_builtin = old;
dlen = STRLEN (document);
/* XXX - Add trailing newline to here-string */
if (ri == r_reading_string)
{
document = (char *)xrealloc (document, dlen + 2);
document[dlen++] = '\n';
document[dlen] = '\0';
}
if (lenp)
*lenp = dlen;
return document;
}
/* Write HEREDOC (of length HDLEN) to FD, returning 0 on success and ERRNO on
error. Don't handle interrupts. */
static inline int
heredoc_write (int fd, char *heredoc, size_t herelen)
{
ssize_t nw;
int e;
errno = 0;
nw = write (fd, heredoc, herelen);
e = errno;
if (nw != herelen)
{
if (e == 0)
e = ENOSPC;
return e;
}
return 0;
}
/* Create a temporary file or pipe holding the text of the here document
pointed to by REDIRECTEE, and return a file descriptor open for reading
to it. Return -1 on any error, and make sure errno is set appropriately. */
static int
here_document_to_fd (WORD_DESC *redirectee, enum r_instruction ri)
{
char *filename;
int r, fd, fd2, herepipe[2];
char *document;
size_t document_len;
#if HEREDOC_PARANOID
struct stat st1, st2;
#endif
/* Expand the here-document/here-string first and then decide what to do. */
document = heredoc_expand (redirectee, ri, &document_len);
/* If we have a zero-length document, don't mess with a temp file */
if (document_len == 0)
{
fd = open ("/dev/null", O_RDONLY);
r = errno;
if (document != redirectee->word)
FREE (document);
errno = r;
return fd;
}
#if defined(HEREDOC_PIPESIZE)
/* Try to use a pipe internal to this process if the document is shorter
than the system's pipe capacity (computed at build time). We want to
write the entire document without write blocking. */
if (document_len <= HEREDOC_PIPESIZE)
{
if (pipe (herepipe) < 0)
{
r = errno;
if (document != redirectee->word)
free (document);
errno = r;
return -1;
}
#if defined(F_GETPIPE_SZ)
if (fcntl (herepipe[1], F_GETPIPE_SZ, 0) < document_len)
goto use_tempfile;
#endif
r = heredoc_write (herepipe[1], document, document_len);
if (document != redirectee->word)
free (document);
close (herepipe[1]);
if (r) /* write error */
{
close (herepipe[0]);
errno = r;
return -1;
}
return herepipe[0];
}
#endif
use_tempfile:
fd = sh_mktmpfd ("sh-thd", MT_USERANDOM | MT_USETMPDIR, &filename);
/* If we failed for some reason other than the file existing, abort */
if (fd < 0)
{
r = errno;
FREE (filename);
if (document != redirectee->word)
FREE (document);
errno = r;
return fd;
}
fchmod (fd, S_IRUSR | S_IWUSR);
SET_CLOSE_ON_EXEC (fd);
errno = r = 0; /* XXX */
r = heredoc_write (fd, document, document_len);
if (document != redirectee->word)
FREE (document);
if (r)
{
close (fd);
unlink (filename);
free (filename);
errno = r;
return -1;
}
/* In an attempt to avoid races, we close the first fd only after opening
the second. */
/* Make the document really temporary. Also make it the input. */
fd2 = open (filename, O_RDONLY | O_BINARY, 0600);
if (fd2 < 0)
{
r = errno;
unlink (filename);
free (filename);
close (fd);
errno = r;
return -1;
}
#if HEREDOC_PARANOID
/* We can use same_file here to check whether or not fd and fd2 refer to
the same file, but we don't do that unless HEREDOC_PARANOID is defined. */
if (fstat (fd, &st1) < 0 || S_ISREG (st1.st_mode) == 0
|| fstat (fd2, &st2) < 0 || S_ISREG (st2.st_mode) == 0
|| same_file (filename, filename, &st1, &st2) == 0)
{
unlink (filename);
free (filename);
close (fd);
close (fd2);
errno = EEXIST;
return -1;
}
#endif
close (fd);
if (unlink (filename) < 0)
{
r = errno;
close (fd2);
free (filename);
errno = r;
return -1;
}
free (filename);
fchmod (fd2, S_IRUSR);
return fd2;
}
#define RF_DEVFD 1
#define RF_DEVSTDERR 2
#define RF_DEVSTDIN 3
#define RF_DEVSTDOUT 4
#define RF_DEVTCP 5
#define RF_DEVUDP 6
/* A list of pattern/value pairs for filenames that the redirection
code handles specially. */
static STRING_INT_ALIST _redir_special_filenames[] = {
#if !defined(HAVE_DEV_FD)
{ "/dev/fd/[0-9]*", RF_DEVFD },
#endif
#if !defined(HAVE_DEV_STDIN)
{ "/dev/stderr", RF_DEVSTDERR },
{ "/dev/stdin", RF_DEVSTDIN },
{ "/dev/stdout", RF_DEVSTDOUT },
#endif
#if defined(NETWORK_REDIRECTIONS)
{ "/dev/tcp/*/*", RF_DEVTCP },
{ "/dev/udp/*/*", RF_DEVUDP },
#endif
{ nullptr, -1 }
};
int
Shell::redir_special_open (int spec, char *filename, int flags, int mode,
enum r_instruction ri)
{
int fd;
#if !defined(HAVE_DEV_FD)
int64_t lfd;
#endif
fd = -1;
switch (spec)
{
#if !defined(HAVE_DEV_FD)
case RF_DEVFD:
if (all_digits (filename + 8) && legal_number (filename + 8, &lfd)
&& lfd == (int)lfd)
{
fd = lfd;
fd = fcntl (fd, F_DUPFD, SHELL_FD_BASE);
}
else
fd = AMBIGUOUS_REDIRECT;
break;
#endif
#if !defined(HAVE_DEV_STDIN)
case RF_DEVSTDIN:
fd = fcntl (0, F_DUPFD, SHELL_FD_BASE);
break;
case RF_DEVSTDOUT:
fd = fcntl (1, F_DUPFD, SHELL_FD_BASE);
break;
case RF_DEVSTDERR:
fd = fcntl (2, F_DUPFD, SHELL_FD_BASE);
break;
#endif
#if defined(NETWORK_REDIRECTIONS)
case RF_DEVTCP:
case RF_DEVUDP:
#if defined(RESTRICTED_SHELL)
if (restricted)
return RESTRICTED_REDIRECT;
#endif
#if defined(HAVE_NETWORK)
fd = netopen (filename);
#else
internal_warning (
_ ("/dev/(tcp|udp)/host/port not supported without networking"));
fd = open (filename, flags, mode);
#endif
break;
#endif /* NETWORK_REDIRECTIONS */
}
return fd;
}
/* Open FILENAME with FLAGS in noclobber mode, hopefully avoiding most
race conditions and avoiding the problem where the file is replaced
between the stat(2) and open(2). */
static int
noclobber_open (char *filename, int flags, int mode, enum r_instruction ri)
{
int r, fd;
struct stat finfo, finfo2;
/* If the file exists and is a regular file, return an error
immediately. */
r = stat (filename, &finfo);
if (r == 0 && (S_ISREG (finfo.st_mode)))
return NOCLOBBER_REDIRECT;
/* If the file was not present (r != 0), make sure we open it
exclusively so that if it is created before we open it, our open
will fail. Make sure that we do not truncate an existing file.
Note that we don't turn on O_EXCL unless the stat failed -- if
the file was not a regular file, we leave O_EXCL off. */
flags &= ~O_TRUNC;
if (r != 0)
{
fd = open (filename, flags | O_EXCL, mode);
return (fd < 0 && errno == EEXIST) ? NOCLOBBER_REDIRECT : fd;
}
fd = open (filename, flags, mode);
/* If the open failed, return the file descriptor right away. */
if (fd < 0)
return errno == EEXIST ? NOCLOBBER_REDIRECT : fd;
/* OK, the open succeeded, but the file may have been changed from a
non-regular file to a regular file between the stat and the open.
We are assuming that the O_EXCL open handles the case where FILENAME
did not exist and is symlinked to an existing file between the stat
and open. */
/* If we can open it and fstat the file descriptor, and neither check
revealed that it was a regular file, and the file has not been replaced,
return the file descriptor. */
if ((fstat (fd, &finfo2) == 0) && (S_ISREG (finfo2.st_mode) == 0) && r == 0
&& (S_ISREG (finfo.st_mode) == 0)
&& same_file (filename, filename, &finfo, &finfo2))
return fd;
/* The file has been replaced. badness. */
close (fd);
errno = EEXIST;
return NOCLOBBER_REDIRECT;
}
static int
redir_open (char *filename, int flags, int mode, enum r_instruction ri)
{
int fd, r, e;
r = find_string_in_alist (filename, _redir_special_filenames, 1);
if (r >= 0)
return redir_special_open (r, filename, flags, mode, ri);
/* If we are in noclobber mode, you are not allowed to overwrite
existing files. Check before opening. */
if (noclobber && CLOBBERING_REDIRECT (ri))
{
fd = noclobber_open (filename, flags, mode, ri);
if (fd == NOCLOBBER_REDIRECT)
return NOCLOBBER_REDIRECT;
}
else
{
do
{
fd = open (filename, flags, mode);
e = errno;
if (fd < 0 && e == EINTR)
{
QUIT;
run_pending_traps ();
}
errno = e;
}
while (fd < 0 && errno == EINTR);
}
return fd;
}
static int
undoablefd (int fd)
{
int clexec;
clexec = fcntl (fd, F_GETFD, 0);
if (clexec == -1 || (fd >= SHELL_FD_BASE && clexec == 1))
return 0;
return 1;
}
/* Do the specific redirection requested. Returns errno or one of the
special redirection errors (*_REDIRECT) in case of error, 0 on success.
If flags & RX_ACTIVE is zero, then just do whatever is necessary to
produce the appropriate side effects. flags & RX_UNDOABLE, if non-zero,
says to remember how to undo each redirection. If flags & RX_CLEXEC is
non-zero, then we set all file descriptors > 2 that we open to be
close-on-exec. FNP, if non-null is a pointer to a location where the
expanded filename is stored. The caller will free it. */
static int
do_redirection_internal (REDIRECT *redirect, int flags, char **fnp)
{
WORD_DESC *redirectee;
int redir_fd, fd, redirector, r, oflags;
int64_t lfd;
char *redirectee_word;
enum r_instruction ri;
REDIRECT *new_redirect;
REDIRECTEE sd;
redirectee = redirect->redirectee.filename;
redir_fd = redirect->redirectee.dest;
redirector = redirect->redirector.dest;
ri = redirect->instruction;
if (redirect->flags & RX_INTERNAL)
flags |= RX_INTERNAL;
if (TRANSLATE_REDIRECT (ri))
{
/* We have [N]>&WORD[-] or [N]<&WORD[-] (or {V}>&WORD[-] or {V}<&WORD-).
and WORD, then translate the redirection into a new one and
continue. */
redirectee_word = redirection_expand (redirectee);
/* XXX - what to do with [N]<&$w- where w is unset or null? ksh93
closes N. */
if (redirectee_word == 0)
return AMBIGUOUS_REDIRECT;
else if (redirectee_word[0] == '-' && redirectee_word[1] == '\0')
{
sd = redirect->redirector;
rd.dest = 0;
new_redirect = make_redirection (sd, r_close_this, rd, 0);
}
else if (all_digits (redirectee_word))
{
sd = redirect->redirector;
if (legal_number (redirectee_word, &lfd) && (int)lfd == lfd)
rd.dest = lfd;
else
rd.dest = -1; /* XXX */
switch (ri)
{
case r_duplicating_input_word:
new_redirect = make_redirection (sd, r_duplicating_input, rd, 0);
break;
case r_duplicating_output_word:
new_redirect
= make_redirection (sd, r_duplicating_output, rd, 0);
break;
case r_move_input_word:
new_redirect = make_redirection (sd, r_move_input, rd, 0);
break;
case r_move_output_word:
new_redirect = make_redirection (sd, r_move_output, rd, 0);
break;
default:
break; /* shut up gcc */
}
}
else if (ri == r_duplicating_output_word
&& (redirect->rflags & REDIR_VARASSIGN) == 0 && redirector == 1)
{
sd = redirect->redirector;
rd.filename = make_bare_word (redirectee_word);
new_redirect = make_redirection (sd, r_err_and_out, rd, 0);
}
else
{
free (redirectee_word);
return AMBIGUOUS_REDIRECT;
}
free (redirectee_word);
/* Set up the variables needed by the rest of the function from the
new redirection. */
if (new_redirect->instruction == r_err_and_out)
{
char *alloca_hack;
/* Copy the word without allocating any memory that must be
explicitly freed. */
redirectee = (WORD_DESC *)alloca (sizeof (WORD_DESC));
xbcopy ((char *)new_redirect->redirectee.filename,
(char *)redirectee, sizeof (WORD_DESC));
alloca_hack = (char *)alloca (
1 + strlen (new_redirect->redirectee.filename->word));
redirectee->word = alloca_hack;
strcpy (redirectee->word, new_redirect->redirectee.filename->word);
}
else
/* It's guaranteed to be an integer, and shouldn't be freed. */
redirectee = new_redirect->redirectee.filename;
redir_fd = new_redirect->redirectee.dest;
redirector = new_redirect->redirector.dest;
ri = new_redirect->instruction;
/* Overwrite the flags element of the old redirect with the new value. */
redirect->flags = new_redirect->flags;
dispose_redirects (new_redirect);
}
switch (ri)
{
case r_output_direction:
case r_appending_to:
case r_input_direction:
case r_inputa_direction:
case r_err_and_out: /* command &>filename */
case r_append_err_and_out: /* command &>> filename */
case r_input_output:
case r_output_force:
if (posixly_correct && interactive_shell == 0)
{
oflags = redirectee->flags;
redirectee->flags |= W_NOGLOB;
}
redirectee_word = redirection_expand (redirectee);
if (posixly_correct && interactive_shell == 0)
redirectee->flags = oflags;
if (redirectee_word == 0)
return AMBIGUOUS_REDIRECT;
#if defined(RESTRICTED_SHELL)
if (restricted && (WRITE_REDIRECT (ri)))
{
free (redirectee_word);
return RESTRICTED_REDIRECT;
}
#endif /* RESTRICTED_SHELL */
fd = redir_open (redirectee_word, redirect->flags, 0666, ri);
if (fnp)
*fnp = redirectee_word;
else
free (redirectee_word);
if (fd == NOCLOBBER_REDIRECT || fd == RESTRICTED_REDIRECT)
return fd;
if (fd < 0)
return errno;
if (flags & RX_ACTIVE)
{
if (redirect->rflags & REDIR_VARASSIGN)
{
redirector = fcntl (fd, F_DUPFD,
SHELL_FD_BASE); /* XXX try this for now */
r = errno;
if (redirector < 0)
sys_error (_ ("redirection error: cannot duplicate fd"));
REDIRECTION_ERROR (redirector, r, fd);
}
if ((flags & RX_UNDOABLE)
&& (redirect->rflags & REDIR_VARASSIGN) == 0)
{
/* Only setup to undo it if the thing to undo is active. */
if ((fd != redirector) && (fcntl (redirector, F_GETFD, 0) != -1))
r = add_undo_redirect (redirector, ri, -1);
else
r = add_undo_close_redirect (redirector);
REDIRECTION_ERROR (r, errno, fd);
}
#if defined(BUFFERED_INPUT)
/* inhibit call to sync_buffered_stream() for async processes */
if (redirector != 0 || (subshell_environment & SUBSHELL_ASYNC) == 0)
check_bash_input (redirector);
#endif
/* Make sure there is no pending output before we change the state
of the underlying file descriptor, since the builtins use stdio
for output. */
if (redirector == 1 && fileno (stdout) == redirector)
{
fflush (stdout);
fpurge (stdout);
}
else if (redirector == 2 && fileno (stderr) == redirector)
{
fflush (stderr);
fpurge (stderr);
}
if (redirect->rflags & REDIR_VARASSIGN)
{
if ((r = redir_varassign (redirect, redirector)) < 0)
{
close (redirector);
close (fd);
return r; /* XXX */
}
}
else if ((fd != redirector) && (dup2 (fd, redirector) < 0))
{
close (fd); /* dup2 failed? must be fd limit issue */
return errno;
}
#if defined(BUFFERED_INPUT)
/* Do not change the buffered stream for an implicit redirection
of /dev/null to fd 0 for asynchronous commands without job
control (r_inputa_direction). */
if (ri == r_input_direction || ri == r_input_output)
duplicate_buffered_stream (fd, redirector);
#endif /* BUFFERED_INPUT */
/*
* If we're remembering, then this is the result of a while, for
* or until loop with a loop redirection, or a function/builtin
* executing in the parent shell with a redirection. In the
* function/builtin case, we want to set all file descriptors > 2
* to be close-on-exec to duplicate the effect of the old
* for i = 3 to NOFILE close(i) loop. In the case of the loops,
* both sh and ksh leave the file descriptors open across execs.
* The Posix standard mentions only the exec builtin.
*/
if ((flags & RX_CLEXEC) && (redirector > 2))
SET_CLOSE_ON_EXEC (redirector);
}
if (fd != redirector)
{
#if defined(BUFFERED_INPUT)
if (INPUT_REDIRECT (ri))
close_buffered_fd (fd);
else
#endif /* !BUFFERED_INPUT */
close (fd); /* Don't close what we just opened! */
}
/* If we are hacking both stdout and stderr, do the stderr
redirection here. XXX - handle {var} here? */
if (ri == r_err_and_out || ri == r_append_err_and_out)
{
if (flags & RX_ACTIVE)
{
if (flags & RX_UNDOABLE)
add_undo_redirect (2, ri, -1);
if (dup2 (1, 2) < 0)
return errno;
}
}
break;
case r_reading_until:
case r_deblank_reading_until:
case r_reading_string:
/* REDIRECTEE is a pointer to a WORD_DESC containing the text of
the new input. Place it in a temporary file. */
if (redirectee)
{
fd = here_document_to_fd (redirectee, ri);