| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/test_utils.h" | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 18 |  | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 20 | #include <stdio.h> | 
|  | 21 | #include <stdlib.h> | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 23 | #include <unistd.h> | 
|  | 24 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 25 | #include <string> | 
|  | 26 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> | 
|  | 28 | #include <android-base/logging.h> | 
|  | 29 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 30 | CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 31 | Start(); | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 32 | } | 
|  | 33 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 34 | CapturedStdFd::~CapturedStdFd() { | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 35 | if (old_fd_ != -1) { | 
|  | 36 | Stop(); | 
|  | 37 | } | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 40 | int CapturedStdFd::fd() const { | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 41 | return temp_file_.fd; | 
|  | 42 | } | 
|  | 43 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 44 | std::string CapturedStdFd::str() { | 
|  | 45 | std::string result; | 
|  | 46 | CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET))); | 
|  | 47 | android::base::ReadFdToString(fd(), &result); | 
|  | 48 | return result; | 
|  | 49 | } | 
|  | 50 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 51 | void CapturedStdFd::Reset() { | 
|  | 52 | // Do not reset while capturing. | 
|  | 53 | CHECK_EQ(-1, old_fd_); | 
|  | 54 | CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET))); | 
|  | 55 | CHECK_EQ(0, ftruncate(fd(), 0)); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | void CapturedStdFd::Start() { | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 59 | #if defined(_WIN32) | 
|  | 60 | // On Windows, stderr is often buffered, so make sure it is unbuffered so | 
|  | 61 | // that we can immediately read back what was written to stderr. | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 62 | if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, nullptr, _IONBF, 0)); | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 63 | #endif | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 64 | old_fd_ = dup(std_fd_); | 
|  | 65 | CHECK_NE(-1, old_fd_); | 
|  | 66 | CHECK_NE(-1, dup2(fd(), std_fd_)); | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 69 | void CapturedStdFd::Stop() { | 
|  | 70 | CHECK_NE(-1, old_fd_); | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 71 | CHECK_NE(-1, dup2(old_fd_, std_fd_)); | 
| Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 72 | close(old_fd_); | 
|  | 73 | old_fd_ = -1; | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 74 | // Note: cannot restore prior setvbuf() setting. | 
|  | 75 | } |