Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame^] | 1 | From 089c9c701a1b68b721f479dfc0c58c35b9dd4175 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Wed, 20 Jan 2016 04:39:53 +0000 |
| 4 | Subject: [PATCH 1/4] implement libc specific _reopen_stream |
| 5 | |
| 6 | musl defines stdin/stdio/stderr as constant types which means |
| 7 | we can not assign to them as we are doing here but works ok with glibc |
| 8 | therefore abstract out the _reopen_stream definition depending upon if |
| 9 | we are using glibc or otherwise |
| 10 | |
| 11 | Origin: |
| 12 | http://git.alpinelinux.org/cgit/aports/tree/main/lvm2/fix-stdio-usage.patch |
| 13 | |
| 14 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 15 | --- |
| 16 | Upstream-Status: Pending |
| 17 | |
| 18 | lib/commands/toolcontext.c | 22 +++++++++++----------- |
| 19 | tools/lvmcmdline.c | 6 +++--- |
| 20 | 2 files changed, 14 insertions(+), 14 deletions(-) |
| 21 | |
| 22 | diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c |
| 23 | index a2f21b8..7f796e4 100644 |
| 24 | --- a/lib/commands/toolcontext.c |
| 25 | +++ b/lib/commands/toolcontext.c |
| 26 | @@ -1637,7 +1637,10 @@ static void _init_globals(struct cmd_context *cmd) |
| 27 | /* |
| 28 | * Close and reopen stream on file descriptor fd. |
| 29 | */ |
| 30 | -static int _reopen_stream(FILE *stream, int fd, const char *mode, const char *name, FILE **new_stream) |
| 31 | +#ifdef __GLIBC__ |
| 32 | +#define _reopen_stream(stream, fd, mode, name) __reopen_stream(stream, fd, mode, name, &stream) |
| 33 | + |
| 34 | +static int __reopen_stream(FILE *stream, int fd, const char *mode, const char *name, FILE **new_stream) |
| 35 | { |
| 36 | int fd_copy, new_fd; |
| 37 | |
| 38 | @@ -1664,6 +1667,9 @@ static int _reopen_stream(FILE *stream, int fd, const char *mode, const char *na |
| 39 | |
| 40 | return 1; |
| 41 | } |
| 42 | +#else |
| 43 | +#define _reopen_stream(stream, fd, mode, name) (freopen(NULL, mode, stream) != NULL) |
| 44 | +#endif |
| 45 | |
| 46 | static int _init_lvmetad(struct cmd_context *cmd) |
| 47 | { |
| 48 | @@ -1741,7 +1747,6 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, |
| 49 | unsigned set_filters) |
| 50 | { |
| 51 | struct cmd_context *cmd; |
| 52 | - FILE *new_stream; |
| 53 | int flags; |
| 54 | |
| 55 | #ifdef M_MMAP_MAX |
| 56 | @@ -1791,9 +1796,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, |
| 57 | if (is_valid_fd(STDIN_FILENO) && |
| 58 | ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) && |
| 59 | (flags & O_ACCMODE) != O_WRONLY) { |
| 60 | - if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) |
| 61 | + if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin")) |
| 62 | goto_out; |
| 63 | - stdin = new_stream; |
| 64 | if (setvbuf(stdin, cmd->linebuffer, _IOLBF, linebuffer_size)) { |
| 65 | log_sys_error("setvbuf", ""); |
| 66 | goto out; |
| 67 | @@ -1803,9 +1807,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, |
| 68 | if (is_valid_fd(STDOUT_FILENO) && |
| 69 | ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) && |
| 70 | (flags & O_ACCMODE) != O_RDONLY) { |
| 71 | - if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) |
| 72 | + if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout")) |
| 73 | goto_out; |
| 74 | - stdout = new_stream; |
| 75 | if (setvbuf(stdout, cmd->linebuffer + linebuffer_size, |
| 76 | _IOLBF, linebuffer_size)) { |
| 77 | log_sys_error("setvbuf", ""); |
| 78 | @@ -2131,7 +2134,6 @@ int refresh_toolcontext(struct cmd_context *cmd) |
| 79 | void destroy_toolcontext(struct cmd_context *cmd) |
| 80 | { |
| 81 | struct dm_config_tree *cft_cmdline; |
| 82 | - FILE *new_stream; |
| 83 | int flags; |
| 84 | |
| 85 | if (cmd->dump_filter && cmd->filter && cmd->filter->dump && |
| 86 | @@ -2167,8 +2169,7 @@ void destroy_toolcontext(struct cmd_context *cmd) |
| 87 | if (is_valid_fd(STDIN_FILENO) && |
| 88 | ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) && |
| 89 | (flags & O_ACCMODE) != O_WRONLY) { |
| 90 | - if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) { |
| 91 | - stdin = new_stream; |
| 92 | + if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin")) { |
| 93 | setlinebuf(stdin); |
| 94 | } else |
| 95 | cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ |
| 96 | @@ -2177,8 +2178,7 @@ void destroy_toolcontext(struct cmd_context *cmd) |
| 97 | if (is_valid_fd(STDOUT_FILENO) && |
| 98 | ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) && |
| 99 | (flags & O_ACCMODE) != O_RDONLY) { |
| 100 | - if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) { |
| 101 | - stdout = new_stream; |
| 102 | + if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout")) { |
| 103 | setlinebuf(stdout); |
| 104 | } else |
| 105 | cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ |
| 106 | diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c |
| 107 | index 6577977..a33258a 100644 |
| 108 | --- a/tools/lvmcmdline.c |
| 109 | +++ b/tools/lvmcmdline.c |
| 110 | @@ -1744,7 +1744,7 @@ static int _check_standard_fds(void) |
| 111 | int err = is_valid_fd(STDERR_FILENO); |
| 112 | |
| 113 | if (!is_valid_fd(STDIN_FILENO) && |
| 114 | - !(stdin = fopen(_PATH_DEVNULL, "r"))) { |
| 115 | + !freopen(_PATH_DEVNULL, "r", stdin)) { |
| 116 | if (err) |
| 117 | perror("stdin stream open"); |
| 118 | else |
| 119 | @@ -1754,7 +1754,7 @@ static int _check_standard_fds(void) |
| 120 | } |
| 121 | |
| 122 | if (!is_valid_fd(STDOUT_FILENO) && |
| 123 | - !(stdout = fopen(_PATH_DEVNULL, "w"))) { |
| 124 | + !freopen(_PATH_DEVNULL, "w", stdout)) { |
| 125 | if (err) |
| 126 | perror("stdout stream open"); |
| 127 | /* else no stdout */ |
| 128 | @@ -1762,7 +1762,7 @@ static int _check_standard_fds(void) |
| 129 | } |
| 130 | |
| 131 | if (!is_valid_fd(STDERR_FILENO) && |
| 132 | - !(stderr = fopen(_PATH_DEVNULL, "w"))) { |
| 133 | + !freopen(_PATH_DEVNULL, "w", stderr)) { |
| 134 | printf("stderr stream open: %s\n", |
| 135 | strerror(errno)); |
| 136 | return 0; |
| 137 | -- |
| 138 | 2.7.0 |
| 139 | |