mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-21 13:34:40 +03:00
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.
This commit is contained in:
parent
5a3c597fd5
commit
c68c157573
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -1230,7 +1230,7 @@ static void _close_descriptor(int fd, unsigned suppress_warnings,
|
||||
const char *filename;
|
||||
|
||||
/* Ignore bad file descriptors */
|
||||
if (fcntl(fd, F_GETFD) == -1 && errno == EBADF)
|
||||
if (!is_valid_fd(fd))
|
||||
return;
|
||||
|
||||
if (!suppress_warnings)
|
||||
|
Loading…
Reference in New Issue
Block a user