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