Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame^] | 5 | #include "debugd/src/process_with_output.h" |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 6 | |
| 7 | #include <base/basictypes.h> |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 8 | #include <base/file_util.h> |
Ben Chan | 5facf4a | 2014-07-23 16:36:54 -0700 | [diff] [blame] | 9 | #include <base/files/file_path.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 10 | #include <base/strings/string_split.h> |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 11 | |
| 12 | namespace debugd { |
| 13 | |
| 14 | ProcessWithOutput::ProcessWithOutput() : outfile_(NULL) { } |
| 15 | ProcessWithOutput::~ProcessWithOutput() { |
| 16 | if (outfile_) |
| 17 | fclose(outfile_); |
| 18 | if (!outfile_path_.empty()) |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 19 | base::DeleteFile(outfile_path_, false); // not recursive |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | bool ProcessWithOutput::Init() { |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 23 | if (!SandboxedProcess::Init()) |
| 24 | return false; |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 25 | outfile_ = base::CreateAndOpenTemporaryFile(&outfile_path_); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 26 | if (!outfile_) |
| 27 | return false; |
| 28 | // We can't just RedirectOutput to the file we just created, since |
| 29 | // RedirectOutput uses O_CREAT | O_EXCL to open the target file (i.e., it'll |
| 30 | // fail if the file already exists). We can't CreateTemporaryFile() and then |
| 31 | // use that filename, since we'd have to remove it before using |
| 32 | // RedirectOutput, which exposes us to a /tmp race. Instead, bind outfile_'s |
| 33 | // fd to the subprocess's stdout and stderr. |
| 34 | BindFd(fileno(outfile_), STDOUT_FILENO); |
| 35 | BindFd(fileno(outfile_), STDERR_FILENO); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool ProcessWithOutput::GetOutputLines(std::vector<std::string>* output) { |
| 40 | std::string contents; |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 41 | if (!base::ReadFileToString(outfile_path_, &contents)) |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 42 | return false; |
| 43 | base::SplitString(contents, '\n', output); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool ProcessWithOutput::GetOutput(std::string* output) { |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 48 | return base::ReadFileToString(outfile_path_, output); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 51 | } // namespace debugd |