blob: 36b4cdfa44e1825a8c0f61fd947a426d6b55ca36 [file] [log] [blame]
Dan Alberte3ea0582015-03-13 23:06:01 -07001/*
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 Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/test_utils.h"
Dan Alberte3ea0582015-03-13 23:06:01 -070018
Dan Albert26cb8ff2015-04-29 11:32:23 -070019#include <fcntl.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070020#include <stdio.h>
21#include <stdlib.h>
Dan Albert26cb8ff2015-04-29 11:32:23 -070022#include <sys/stat.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070023#include <unistd.h>
24
Alex Valléed0ac74c2015-05-06 16:26:00 -040025#include <string>
26
Elliott Hughes1c1409f2018-05-23 09:16:46 -070027#include <android-base/file.h>
28#include <android-base/logging.h>
29
Elliott Hughes1c1409f2018-05-23 09:16:46 -070030CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070031 Start();
Wei Wangcaee0eb2016-10-21 09:23:39 -070032}
33
Elliott Hughes1c1409f2018-05-23 09:16:46 -070034CapturedStdFd::~CapturedStdFd() {
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070035 if (old_fd_ != -1) {
36 Stop();
37 }
Wei Wangcaee0eb2016-10-21 09:23:39 -070038}
39
Elliott Hughes1c1409f2018-05-23 09:16:46 -070040int CapturedStdFd::fd() const {
Wei Wangcaee0eb2016-10-21 09:23:39 -070041 return temp_file_.fd;
42}
43
Elliott Hughes1c1409f2018-05-23 09:16:46 -070044std::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 Ferrisa6f0b672018-08-30 13:31:45 -070051void 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
58void CapturedStdFd::Start() {
Wei Wangcaee0eb2016-10-21 09:23:39 -070059#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 Ferrisa6f0b672018-08-30 13:31:45 -070062 if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, nullptr, _IONBF, 0));
Wei Wangcaee0eb2016-10-21 09:23:39 -070063#endif
Elliott Hughes1c1409f2018-05-23 09:16:46 -070064 old_fd_ = dup(std_fd_);
65 CHECK_NE(-1, old_fd_);
66 CHECK_NE(-1, dup2(fd(), std_fd_));
Wei Wangcaee0eb2016-10-21 09:23:39 -070067}
68
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070069void CapturedStdFd::Stop() {
70 CHECK_NE(-1, old_fd_);
Elliott Hughes1c1409f2018-05-23 09:16:46 -070071 CHECK_NE(-1, dup2(old_fd_, std_fd_));
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070072 close(old_fd_);
73 old_fd_ = -1;
Wei Wangcaee0eb2016-10-21 09:23:39 -070074 // Note: cannot restore prior setvbuf() setting.
75}