Index: runtime/bin/file_linux.cc |
diff --git a/runtime/bin/file_linux.cc b/runtime/bin/file_linux.cc |
index bc6f6109a5d708cb4dd0938b2fd70b7bf16bd3e2..2e79248cbd1a2381812119e35348fc95cd2fc710 100644 |
--- a/runtime/bin/file_linux.cc |
+++ b/runtime/bin/file_linux.cc |
@@ -38,21 +38,26 @@ class FileHandle { |
File::~File() { |
- // Close the file (unless it's a standard stream). |
- if (handle_->fd() > STDERR_FILENO) { |
- Close(); |
- } |
+ Close(); |
delete handle_; |
} |
void File::Close() { |
ASSERT(handle_->fd() >= 0); |
- int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(handle_->fd())); |
- if (err != 0) { |
- const int kBufferSize = 1024; |
- char error_buf[kBufferSize]; |
- Log::PrintErr("%s\n", strerror_r(errno, error_buf, kBufferSize)); |
+ if (handle_->fd() == STDOUT_FILENO) { |
+ // If stdout, redirect fd to /dev/null. |
+ int null_fd = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY)); |
+ ASSERT(null_fd >= 0); |
+ VOID_TEMP_FAILURE_RETRY(dup2(null_fd, handle_->fd())); |
+ VOID_TEMP_FAILURE_RETRY(close(null_fd)); |
+ } else { |
+ int err = TEMP_FAILURE_RETRY_BLOCK_SIGNALS(close(handle_->fd())); |
+ if (err != 0) { |
+ const int kBufferSize = 1024; |
+ char error_buf[kBufferSize]; |
+ Log::PrintErr("%s\n", strerror_r(errno, error_buf, kBufferSize)); |
+ } |
} |
handle_->set_fd(kClosedFd); |
} |