diff options
Diffstat (limited to 'shell/hush.c')
-rw-r--r-- | shell/hush.c | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/shell/hush.c b/shell/hush.c index df1b046ab..b76b8fda4 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -1478,12 +1478,15 @@ static void free_strings(char **strings) | |||
1478 | free(strings); | 1478 | free(strings); |
1479 | } | 1479 | } |
1480 | 1480 | ||
1481 | static int fcntl_F_DUPFD(int fd, int avoid_fd) | 1481 | static int dup_CLOEXEC(int fd, int avoid_fd) |
1482 | { | 1482 | { |
1483 | int newfd; | 1483 | int newfd; |
1484 | repeat: | 1484 | repeat: |
1485 | newfd = fcntl(fd, F_DUPFD, avoid_fd + 1); | 1485 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
1486 | if (newfd < 0) { | 1486 | if (newfd >= 0) { |
1487 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ | ||
1488 | fcntl(newfd, F_SETFD, FD_CLOEXEC); | ||
1489 | } else { /* newfd < 0 */ | ||
1487 | if (errno == EBUSY) | 1490 | if (errno == EBUSY) |
1488 | goto repeat; | 1491 | goto repeat; |
1489 | if (errno == EINTR) | 1492 | if (errno == EINTR) |
@@ -6766,7 +6769,7 @@ static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) | |||
6766 | if (sq) for (; sq[i].orig_fd >= 0; i++) { | 6769 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
6767 | /* If we collide with an already moved fd... */ | 6770 | /* If we collide with an already moved fd... */ |
6768 | if (fd == sq[i].moved_to) { | 6771 | if (fd == sq[i].moved_to) { |
6769 | sq[i].moved_to = fcntl_F_DUPFD(sq[i].moved_to, avoid_fd); | 6772 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
6770 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); | 6773 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
6771 | if (sq[i].moved_to < 0) /* what? */ | 6774 | if (sq[i].moved_to < 0) /* what? */ |
6772 | xfunc_die(); | 6775 | xfunc_die(); |
@@ -6780,7 +6783,7 @@ static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) | |||
6780 | } | 6783 | } |
6781 | 6784 | ||
6782 | /* If this fd is open, we move and remember it; if it's closed, moved_to = -1 */ | 6785 | /* If this fd is open, we move and remember it; if it's closed, moved_to = -1 */ |
6783 | moved_to = fcntl_F_DUPFD(fd, avoid_fd); | 6786 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
6784 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); | 6787 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
6785 | if (moved_to < 0 && errno != EBADF) | 6788 | if (moved_to < 0 && errno != EBADF) |
6786 | xfunc_die(); | 6789 | xfunc_die(); |
@@ -7429,6 +7432,10 @@ static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, | |||
7429 | */ | 7432 | */ |
7430 | close_saved_fds_and_FILE_fds(); | 7433 | close_saved_fds_and_FILE_fds(); |
7431 | //FIXME: should also close saved redir fds | 7434 | //FIXME: should also close saved redir fds |
7435 | //This casuses test failures in | ||
7436 | //redir_children_should_not_see_saved_fd_2.tests | ||
7437 | //redir_children_should_not_see_saved_fd_3.tests | ||
7438 | //if you replace "busybox find" with just "find" in them | ||
7432 | /* Without this, "rm -i FILE" can't be ^C'ed: */ | 7439 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
7433 | switch_off_special_sigs(G.special_sig_mask); | 7440 | switch_off_special_sigs(G.special_sig_mask); |
7434 | debug_printf_exec("running applet '%s'\n", argv[0]); | 7441 | debug_printf_exec("running applet '%s'\n", argv[0]); |
@@ -9133,7 +9140,7 @@ int hush_main(int argc, char **argv) | |||
9133 | G_saved_tty_pgrp = 0; | 9140 | G_saved_tty_pgrp = 0; |
9134 | 9141 | ||
9135 | /* try to dup stdin to high fd#, >= 255 */ | 9142 | /* try to dup stdin to high fd#, >= 255 */ |
9136 | G_interactive_fd = fcntl_F_DUPFD(STDIN_FILENO, 254); | 9143 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
9137 | if (G_interactive_fd < 0) { | 9144 | if (G_interactive_fd < 0) { |
9138 | /* try to dup to any fd */ | 9145 | /* try to dup to any fd */ |
9139 | G_interactive_fd = dup(STDIN_FILENO); | 9146 | G_interactive_fd = dup(STDIN_FILENO); |
@@ -9206,10 +9213,10 @@ int hush_main(int argc, char **argv) | |||
9206 | #elif ENABLE_HUSH_INTERACTIVE | 9213 | #elif ENABLE_HUSH_INTERACTIVE |
9207 | /* No job control compiled in, only prompt/line editing */ | 9214 | /* No job control compiled in, only prompt/line editing */ |
9208 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { | 9215 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
9209 | G_interactive_fd = fcntl_F_DUPFD(STDIN_FILENO, 254); | 9216 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
9210 | if (G_interactive_fd < 0) { | 9217 | if (G_interactive_fd < 0) { |
9211 | /* try to dup to any fd */ | 9218 | /* try to dup to any fd */ |
9212 | G_interactive_fd = dup(STDIN_FILENO); | 9219 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO); |
9213 | if (G_interactive_fd < 0) | 9220 | if (G_interactive_fd < 0) |
9214 | /* give up */ | 9221 | /* give up */ |
9215 | G_interactive_fd = 0; | 9222 | G_interactive_fd = 0; |