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 | |
Ben Chan | 5facf4a | 2014-07-23 16:36:54 -0700 | [diff] [blame] | 7 | #include <base/files/file_path.h> |
Ben Chan | cd8fda4 | 2014-09-05 08:21:06 -0700 | [diff] [blame^] | 8 | #include <base/files/file_util.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 9 | #include <base/strings/string_split.h> |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 10 | |
| 11 | namespace debugd { |
| 12 | |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 13 | ProcessWithOutput::ProcessWithOutput() : outfile_(NULL) {} |
| 14 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 15 | ProcessWithOutput::~ProcessWithOutput() { |
| 16 | if (outfile_) |
| 17 | fclose(outfile_); |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 18 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 19 | if (!outfile_path_.empty()) |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 20 | base::DeleteFile(outfile_path_, false); // not recursive |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | bool ProcessWithOutput::Init() { |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 24 | if (!SandboxedProcess::Init()) |
| 25 | return false; |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 26 | |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 27 | outfile_ = base::CreateAndOpenTemporaryFile(&outfile_path_); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 28 | if (!outfile_) |
| 29 | return false; |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 30 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 31 | // We can't just RedirectOutput to the file we just created, since |
| 32 | // RedirectOutput uses O_CREAT | O_EXCL to open the target file (i.e., it'll |
| 33 | // fail if the file already exists). We can't CreateTemporaryFile() and then |
| 34 | // use that filename, since we'd have to remove it before using |
| 35 | // RedirectOutput, which exposes us to a /tmp race. Instead, bind outfile_'s |
| 36 | // fd to the subprocess's stdout and stderr. |
| 37 | BindFd(fileno(outfile_), STDOUT_FILENO); |
| 38 | BindFd(fileno(outfile_), STDERR_FILENO); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | bool ProcessWithOutput::GetOutputLines(std::vector<std::string>* output) { |
| 43 | std::string contents; |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 44 | if (!base::ReadFileToString(outfile_path_, &contents)) |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 45 | return false; |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 46 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 47 | base::SplitString(contents, '\n', output); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool ProcessWithOutput::GetOutput(std::string* output) { |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 52 | return base::ReadFileToString(outfile_path_, output); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 55 | } // namespace debugd |