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 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 7 | #include <signal.h> |
| 8 | |
Ben Chan | cd8fda4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 9 | #include <base/files/file_util.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 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | const char kDBusErrorString[] = "org.chromium.debugd.error.RunProcess"; |
| 17 | const char kInitErrorString[] = "Process initialization failure."; |
| 18 | const char kStartErrorString[] = "Process start failure."; |
| 19 | const char kInputErrorString[] = "Process input write failure."; |
| 20 | const char kPathLengthErrorString[] = "Path length is too long."; |
| 21 | |
| 22 | // Sets the D-Bus error if it's non-NULL. |
| 23 | void SetError(const char* message, DBus::Error* error) { |
| 24 | if (error) { |
| 25 | error->set(kDBusErrorString, message); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | ProcessWithOutput::ProcessWithOutput() |
| 32 | : separate_stderr_(false), use_minijail_(true) { |
| 33 | } |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 34 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 35 | ProcessWithOutput::~ProcessWithOutput() { |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 36 | outfile_.reset(); |
| 37 | errfile_.reset(); |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 38 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 39 | if (!outfile_path_.empty()) |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 40 | base::DeleteFile(outfile_path_, false); // not recursive |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 41 | if (!errfile_path_.empty()) |
| 42 | base::DeleteFile(errfile_path_, false); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool ProcessWithOutput::Init() { |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 46 | if (use_minijail_) { |
| 47 | if (!SandboxedProcess::Init()) |
| 48 | return false; |
| 49 | } |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 50 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 51 | outfile_.reset(base::CreateAndOpenTemporaryFile(&outfile_path_)); |
| 52 | if (!outfile_.get()) { |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 53 | return false; |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 54 | } |
| 55 | if (separate_stderr_) { |
| 56 | errfile_.reset(base::CreateAndOpenTemporaryFile(&errfile_path_)); |
| 57 | if (!errfile_.get()) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 61 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 62 | // We can't just RedirectOutput to the file we just created, since |
| 63 | // RedirectOutput uses O_CREAT | O_EXCL to open the target file (i.e., it'll |
| 64 | // fail if the file already exists). We can't CreateTemporaryFile() and then |
| 65 | // use that filename, since we'd have to remove it before using |
| 66 | // RedirectOutput, which exposes us to a /tmp race. Instead, bind outfile_'s |
| 67 | // fd to the subprocess's stdout and stderr. |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 68 | BindFd(fileno(outfile_.get()), STDOUT_FILENO); |
| 69 | BindFd(fileno(separate_stderr_ ? errfile_.get() : outfile_.get()), |
| 70 | STDERR_FILENO); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool ProcessWithOutput::GetOutputLines(std::vector<std::string>* output) { |
| 75 | std::string contents; |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 76 | if (!base::ReadFileToString(outfile_path_, &contents)) |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 77 | return false; |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 78 | |
Alex Vakulenko | e50371c | 2016-01-20 16:06:19 -0800 | [diff] [blame^] | 79 | *output = base::SplitString(contents, "\n", base::KEEP_WHITESPACE, |
| 80 | base::SPLIT_WANT_ALL); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool ProcessWithOutput::GetOutput(std::string* output) { |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 85 | return base::ReadFileToString(outfile_path_, output); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 86 | } |
| 87 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 88 | bool ProcessWithOutput::GetError(std::string* error) { |
| 89 | return base::ReadFileToString(errfile_path_, error); |
| 90 | } |
| 91 | |
| 92 | int ProcessWithOutput::RunProcess(const std::string& command, |
| 93 | const ArgList& arguments, |
| 94 | bool requires_root, |
| 95 | const std::string* stdin, |
| 96 | std::string* stdout, |
| 97 | std::string* stderr, |
| 98 | DBus::Error* error) { |
| 99 | ProcessWithOutput process; |
| 100 | if (requires_root) { |
| 101 | process.SandboxAs("root", "root"); |
| 102 | } |
| 103 | return DoRunProcess( |
| 104 | command, arguments, stdin, stdout, stderr, error, &process); |
| 105 | } |
| 106 | |
| 107 | int ProcessWithOutput::RunHelper(const std::string& helper, |
| 108 | const ArgList& arguments, |
| 109 | bool requires_root, |
| 110 | const std::string* stdin, |
| 111 | std::string* stdout, |
| 112 | std::string* stderr, |
| 113 | DBus::Error* error) { |
| 114 | std::string helper_path; |
| 115 | if (!SandboxedProcess::GetHelperPath(helper, &helper_path)) { |
| 116 | SetError(kPathLengthErrorString, error); |
| 117 | return kRunError; |
| 118 | } |
| 119 | return RunProcess( |
| 120 | helper_path, arguments, requires_root, stdin, stdout, stderr, error); |
| 121 | } |
| 122 | |
| 123 | int ProcessWithOutput::RunProcessFromHelper(const std::string& command, |
| 124 | const ArgList& arguments, |
| 125 | const std::string* stdin, |
| 126 | std::string* stdout, |
| 127 | std::string* stderr) { |
| 128 | ProcessWithOutput process; |
| 129 | process.set_use_minijail(false); |
| 130 | process.SetSearchPath(true); |
| 131 | return DoRunProcess( |
| 132 | command, arguments, stdin, stdout, stderr, nullptr, &process); |
| 133 | } |
| 134 | |
| 135 | int ProcessWithOutput::DoRunProcess(const std::string& command, |
| 136 | const ArgList& arguments, |
| 137 | const std::string* stdin, |
| 138 | std::string* stdout, |
| 139 | std::string* stderr, |
| 140 | DBus::Error* error, |
| 141 | ProcessWithOutput* process) { |
| 142 | process->set_separate_stderr(true); |
| 143 | if (!process->Init()) { |
| 144 | SetError(kInitErrorString, error); |
| 145 | return kRunError; |
| 146 | } |
| 147 | |
| 148 | process->AddArg(command); |
| 149 | for (const auto& argument : arguments) { |
| 150 | process->AddArg(argument); |
| 151 | } |
| 152 | |
| 153 | int result = kRunError; |
| 154 | if (stdin) { |
| 155 | process->RedirectUsingPipe(STDIN_FILENO, true); |
| 156 | if (process->Start()) { |
| 157 | int stdin_fd = process->GetPipe(STDIN_FILENO); |
| 158 | // Kill the process if writing to or closing the pipe fails. |
Alex Vakulenko | 26d2623 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 159 | if (!base::WriteFileDescriptor(stdin_fd, stdin->c_str(), |
| 160 | stdin->length()) || |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 161 | IGNORE_EINTR(close(stdin_fd)) < 0) { |
| 162 | process->Kill(SIGKILL, 0); |
| 163 | SetError(kInputErrorString, error); |
| 164 | } |
| 165 | result = process->Wait(); |
| 166 | } else { |
| 167 | SetError(kStartErrorString, error); |
| 168 | } |
| 169 | } else { |
| 170 | result = process->Run(); |
| 171 | } |
| 172 | |
| 173 | if (stdout) { |
| 174 | process->GetOutput(stdout); |
| 175 | } |
| 176 | if (stderr) { |
| 177 | process->GetError(stderr); |
| 178 | } |
| 179 | return result; |
| 180 | } |
| 181 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 182 | } // namespace debugd |