diff options
Diffstat (limited to 'brillo/process.cc')
-rw-r--r-- | brillo/process.cc | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/brillo/process.cc b/brillo/process.cc index 7af8fbb..8773244 100644 --- a/brillo/process.cc +++ b/brillo/process.cc @@ -15,6 +15,7 @@ #include <map> #include <memory> +#include <base/files/file_path.h> #include <base/files/file_util.h> #include <base/logging.h> #include <base/memory/ptr_util.h> @@ -64,6 +65,10 @@ void ProcessImpl::AddArg(const std::string& arg) { arguments_.push_back(arg); } +void ProcessImpl::RedirectInput(const std::string& input_file) { + input_file_ = input_file; +} + void ProcessImpl::RedirectOutput(const std::string& output_file) { output_file_ = output_file; } @@ -212,7 +217,7 @@ bool ProcessImpl::Start() { return false; } std::unique_ptr<char* []> argv = - base::MakeUnique<char* []>(arguments_.size() + 1); + std::make_unique<char* []>(arguments_.size() + 1); for (size_t i = 0; i < arguments_.size(); ++i) argv[i] = const_cast<char*>(arguments_[i].c_str()); @@ -257,6 +262,28 @@ bool ProcessImpl::Start() { continue; IGNORE_EINTR(close(i->second.child_fd_)); } + + if (!input_file_.empty()) { + int input_handle = + HANDLE_EINTR(open(input_file_.c_str(), + O_RDONLY | O_NOFOLLOW | O_NOCTTY)); + if (input_handle < 0) { + PLOG(ERROR) << "Could not open " << input_file_; + // Avoid exit() to avoid atexit handlers from parent. + _exit(kErrorExitStatus); + } + + // It's possible input_handle is already stdin. But if not, we need + // to dup into that file descriptor and close the original. + if (input_handle != STDIN_FILENO) { + if (HANDLE_EINTR(dup2(input_handle, STDIN_FILENO)) < 0) { + PLOG(ERROR) << "Could not dup fd to stdin for " << input_file_; + _exit(kErrorExitStatus); + } + IGNORE_EINTR(close(input_handle)); + } + } + if (!output_file_.empty()) { int output_handle = HANDLE_EINTR(open( output_file_.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, |