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