diff options
author | Peter Rajnoha <prajnoha@redhat.com> | 2012-08-27 13:07:07 +0200 |
---|---|---|
committer | Peter Rajnoha <prajnoha@redhat.com> | 2012-08-27 13:07:07 +0200 |
commit | c68c15757384e6cebd7667139661ab643eebbfbe (patch) | |
tree | 1bf8741a1a990eff6fbcb54cfde4c21645626ef1 /lib | |
parent | 5a3c597fd599c21a0bb0c551c92393518d3e70b8 (diff) | |
download | lvm2-c68c15757384e6cebd7667139661ab643eebbfbe.tar.gz lvm2-c68c15757384e6cebd7667139661ab643eebbfbe.tar.xz lvm2-c68c15757384e6cebd7667139661ab643eebbfbe.zip |
setvbuf: reopen only valid fd
We should check whether the fd is opened before trying to reopen it.
For example, the stdin is closed in test/lib/harness.c causing the
test suite to fail.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/commands/toolcontext.c | 54 | ||||
-rw-r--r-- | lib/misc/lvm-file.h | 2 |
2 files changed, 33 insertions, 23 deletions
diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index 0ede1040..9c2404f4 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -1326,21 +1326,25 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, goto out; } - if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) - goto_out; - stdin = new_stream; - if (setvbuf(stdin, cmd->linebuffer, _IOLBF, linebuffer_size)) { - log_sys_error("setvbuf", ""); - goto out; + if (is_valid_fd(STDIN_FILENO)) { + if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) + goto_out; + stdin = new_stream; + if (setvbuf(stdin, cmd->linebuffer, _IOLBF, linebuffer_size)) { + log_sys_error("setvbuf", ""); + goto out; + } } - if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) - goto_out; - stdout = new_stream; - if (setvbuf(stdout, cmd->linebuffer + linebuffer_size, - _IOLBF, linebuffer_size)) { - log_sys_error("setvbuf", ""); - goto out; + if (is_valid_fd(STDOUT_FILENO)) { + if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) + goto_out; + stdout = new_stream; + if (setvbuf(stdout, cmd->linebuffer + linebuffer_size, + _IOLBF, linebuffer_size)) { + log_sys_error("setvbuf", ""); + goto out; + } } /* Buffers are used for lines without '\n' */ } else @@ -1614,17 +1618,21 @@ void destroy_toolcontext(struct cmd_context *cmd) if (cmd->linebuffer) { /* Reset stream buffering to defaults */ - if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) { - stdin = new_stream; - setlinebuf(stdin); - } else - cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ + if (is_valid_fd(STDIN_FILENO)) { + if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) { + stdin = new_stream; + setlinebuf(stdin); + } else + cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ + } - if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) { - stdout = new_stream; - setlinebuf(stdout); - } else - cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ + if (is_valid_fd(STDOUT_FILENO)) { + if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) { + stdout = new_stream; + setlinebuf(stdout); + } else + cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ + } dm_free(cmd->linebuffer); } diff --git a/lib/misc/lvm-file.h b/lib/misc/lvm-file.h index 07327c35..c23d8ad6 100644 --- a/lib/misc/lvm-file.h +++ b/lib/misc/lvm-file.h @@ -51,6 +51,8 @@ void fcntl_unlock_file(int lockfd); ((buf1).st_ino == (buf2).st_ino && \ (buf1).st_dev == (buf2).st_dev) +#define is_valid_fd(fd) (!(fcntl(fd, F_GETFD) == -1 && errno == EBADF)) + /* * Close the specified stream, taking care to detect and diagnose any write * error. If there is an error, use the supplied file name in a diagnostic |