2015-03-14 07:06:01 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include "android-base/test_utils.h"
|
2015-03-14 07:06:01 +01:00
|
|
|
|
2015-04-29 20:32:23 +02:00
|
|
|
#include <fcntl.h>
|
2015-03-14 07:06:01 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-04-29 20:32:23 +02:00
|
|
|
#include <sys/stat.h>
|
2015-03-14 07:06:01 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-05-06 22:26:00 +02:00
|
|
|
#include <string>
|
|
|
|
|
2018-05-23 18:16:46 +02:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
|
|
|
|
CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
|
2018-08-30 22:31:45 +02:00
|
|
|
Start();
|
2016-10-21 18:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:16:46 +02:00
|
|
|
CapturedStdFd::~CapturedStdFd() {
|
2018-08-30 22:31:45 +02:00
|
|
|
if (old_fd_ != -1) {
|
|
|
|
Stop();
|
|
|
|
}
|
2016-10-21 18:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:16:46 +02:00
|
|
|
int CapturedStdFd::fd() const {
|
2016-10-21 18:23:39 +02:00
|
|
|
return temp_file_.fd;
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:16:46 +02:00
|
|
|
std::string CapturedStdFd::str() {
|
|
|
|
std::string result;
|
|
|
|
CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
|
|
|
|
android::base::ReadFdToString(fd(), &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:31:45 +02:00
|
|
|
void CapturedStdFd::Reset() {
|
|
|
|
// Do not reset while capturing.
|
|
|
|
CHECK_EQ(-1, old_fd_);
|
|
|
|
CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
|
|
|
|
CHECK_EQ(0, ftruncate(fd(), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CapturedStdFd::Start() {
|
2016-10-21 18:23:39 +02:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// On Windows, stderr is often buffered, so make sure it is unbuffered so
|
|
|
|
// that we can immediately read back what was written to stderr.
|
2018-08-30 22:31:45 +02:00
|
|
|
if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, nullptr, _IONBF, 0));
|
2016-10-21 18:23:39 +02:00
|
|
|
#endif
|
2018-05-23 18:16:46 +02:00
|
|
|
old_fd_ = dup(std_fd_);
|
|
|
|
CHECK_NE(-1, old_fd_);
|
|
|
|
CHECK_NE(-1, dup2(fd(), std_fd_));
|
2016-10-21 18:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 22:31:45 +02:00
|
|
|
void CapturedStdFd::Stop() {
|
|
|
|
CHECK_NE(-1, old_fd_);
|
2018-05-23 18:16:46 +02:00
|
|
|
CHECK_NE(-1, dup2(old_fd_, std_fd_));
|
2018-08-30 22:31:45 +02:00
|
|
|
close(old_fd_);
|
|
|
|
old_fd_ = -1;
|
2016-10-21 18:23:39 +02:00
|
|
|
// Note: cannot restore prior setvbuf() setting.
|
|
|
|
}
|