Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/subprocess.h" |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 18 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 23 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 24 | #include <memory> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 25 | #include <string> |
Amin Hassani | a885954 | 2018-03-07 16:24:43 -0800 | [diff] [blame] | 26 | #include <utility> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | #include <vector> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 28 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 29 | #include <base/bind.h> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 30 | #include <base/logging.h> |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 31 | #include <base/posix/eintr_wrapper.h> |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame^] | 32 | #include <base/stl_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 33 | #include <base/strings/string_util.h> |
| 34 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 35 | #include <brillo/process.h> |
| 36 | #include <brillo/secure_blob.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 38 | #include "update_engine/common/utils.h" |
| 39 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 41 | using std::unique_ptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 42 | using std::vector; |
| 43 | |
| 44 | namespace chromeos_update_engine { |
| 45 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 46 | namespace { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 47 | |
Alex Deymo | 109c28d | 2016-04-05 23:00:52 +0000 | [diff] [blame] | 48 | bool SetupChild(const std::map<string, string>& env, uint32_t flags) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 49 | // Setup the environment variables. |
| 50 | clearenv(); |
| 51 | for (const auto& key_value : env) { |
| 52 | setenv(key_value.first.c_str(), key_value.second.c_str(), 0); |
| 53 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 54 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 55 | if ((flags & Subprocess::kRedirectStderrToStdout) != 0) { |
| 56 | if (HANDLE_EINTR(dup2(STDOUT_FILENO, STDERR_FILENO)) != STDERR_FILENO) |
| 57 | return false; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 58 | } |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 59 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 60 | int fd = HANDLE_EINTR(open("/dev/null", O_RDONLY)); |
| 61 | if (fd < 0) |
| 62 | return false; |
| 63 | if (HANDLE_EINTR(dup2(fd, STDIN_FILENO)) != STDIN_FILENO) |
| 64 | return false; |
| 65 | IGNORE_EINTR(close(fd)); |
| 66 | |
| 67 | return true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 70 | // Helper function to launch a process with the given Subprocess::Flags. |
| 71 | // This function only sets up and starts the process according to the |flags|. |
| 72 | // The caller is responsible for watching the termination of the subprocess. |
| 73 | // Return whether the process was successfully launched and fills in the |proc| |
| 74 | // Process. |
| 75 | bool LaunchProcess(const vector<string>& cmd, |
| 76 | uint32_t flags, |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 77 | const vector<int>& output_pipes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 78 | brillo::Process* proc) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 79 | for (const string& arg : cmd) |
| 80 | proc->AddArg(arg); |
| 81 | proc->SetSearchPath((flags & Subprocess::kSearchPath) != 0); |
| 82 | |
| 83 | // Create an environment for the child process with just the required PATHs. |
| 84 | std::map<string, string> env; |
| 85 | for (const char* key : {"LD_LIBRARY_PATH", "PATH"}) { |
| 86 | const char* value = getenv(key); |
| 87 | if (value) |
| 88 | env.emplace(key, value); |
| 89 | } |
| 90 | |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 91 | for (const int fd : output_pipes) { |
| 92 | proc->RedirectUsingPipe(fd, false); |
| 93 | } |
| 94 | proc->SetCloseUnusedFileDescriptors(true); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 95 | proc->RedirectUsingPipe(STDOUT_FILENO, false); |
Alex Deymo | 109c28d | 2016-04-05 23:00:52 +0000 | [diff] [blame] | 96 | proc->SetPreExecCallback(base::Bind(&SetupChild, env, flags)); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 97 | |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 98 | LOG(INFO) << "Running \"" << base::JoinString(cmd, " ") << "\""; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 99 | return proc->Start(); |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 104 | void Subprocess::Init( |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 105 | brillo::AsynchronousSignalHandlerInterface* async_signal_handler) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 106 | if (subprocess_singleton_ == this) |
| 107 | return; |
| 108 | CHECK(subprocess_singleton_ == nullptr); |
| 109 | subprocess_singleton_ = this; |
| 110 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 111 | process_reaper_.Register(async_signal_handler); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Subprocess::~Subprocess() { |
| 115 | if (subprocess_singleton_ == this) |
| 116 | subprocess_singleton_ = nullptr; |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 119 | void Subprocess::OnStdoutReady(SubprocessRecord* record) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 120 | char buf[1024]; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 121 | size_t bytes_read; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 122 | do { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 123 | bytes_read = 0; |
| 124 | bool eof; |
| 125 | bool ok = utils::ReadAll( |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame^] | 126 | record->stdout_fd, buf, base::size(buf), &bytes_read, &eof); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 127 | record->stdout.append(buf, bytes_read); |
| 128 | if (!ok || eof) { |
| 129 | // There was either an error or an EOF condition, so we are done watching |
| 130 | // the file descriptor. |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 131 | record->stdout_controller.reset(); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 132 | return; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 133 | } |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 134 | } while (bytes_read); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 137 | void Subprocess::ChildExitedCallback(const siginfo_t& info) { |
| 138 | auto pid_record = subprocess_records_.find(info.si_pid); |
| 139 | if (pid_record == subprocess_records_.end()) |
| 140 | return; |
| 141 | SubprocessRecord* record = pid_record->second.get(); |
| 142 | |
| 143 | // Make sure we read any remaining process output and then close the pipe. |
| 144 | OnStdoutReady(record); |
| 145 | |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 146 | record->stdout_controller.reset(); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 147 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 148 | // Don't print any log if the subprocess exited with exit code 0. |
| 149 | if (info.si_code != CLD_EXITED) { |
| 150 | LOG(INFO) << "Subprocess terminated with si_code " << info.si_code; |
| 151 | } else if (info.si_status != 0) { |
| 152 | LOG(INFO) << "Subprocess exited with si_status: " << info.si_status; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 153 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 154 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 155 | if (!record->stdout.empty()) { |
| 156 | LOG(INFO) << "Subprocess output:\n" << record->stdout; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 157 | } |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 158 | if (!record->callback.is_null()) { |
| 159 | record->callback.Run(info.si_status, record->stdout); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 160 | } |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 161 | // Release and close all the pipes after calling the callback so our |
| 162 | // redirected pipes are still alive. Releasing the process first makes |
| 163 | // Reset(0) not attempt to kill the process, which is already a zombie at this |
| 164 | // point. |
| 165 | record->proc.Release(); |
| 166 | record->proc.Reset(0); |
| 167 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 168 | subprocess_records_.erase(pid_record); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 171 | pid_t Subprocess::Exec(const vector<string>& cmd, |
| 172 | const ExecCallback& callback) { |
Alex Deymo | 109c28d | 2016-04-05 23:00:52 +0000 | [diff] [blame] | 173 | return ExecFlags(cmd, kRedirectStderrToStdout, {}, callback); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 174 | } |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 175 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 176 | pid_t Subprocess::ExecFlags(const vector<string>& cmd, |
| 177 | uint32_t flags, |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 178 | const vector<int>& output_pipes, |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 179 | const ExecCallback& callback) { |
| 180 | unique_ptr<SubprocessRecord> record(new SubprocessRecord(callback)); |
| 181 | |
Alex Deymo | 109c28d | 2016-04-05 23:00:52 +0000 | [diff] [blame] | 182 | if (!LaunchProcess(cmd, flags, output_pipes, &record->proc)) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 183 | LOG(ERROR) << "Failed to launch subprocess"; |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 186 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 187 | pid_t pid = record->proc.pid(); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 188 | CHECK(process_reaper_.WatchForChild( |
| 189 | FROM_HERE, |
| 190 | pid, |
| 191 | base::Bind(&Subprocess::ChildExitedCallback, base::Unretained(this)))); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 192 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 193 | record->stdout_fd = record->proc.GetPipe(STDOUT_FILENO); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 194 | // Capture the subprocess output. Make our end of the pipe non-blocking. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 195 | int fd_flags = fcntl(record->stdout_fd, F_GETFL, 0) | O_NONBLOCK; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 196 | if (HANDLE_EINTR(fcntl(record->stdout_fd, F_SETFL, fd_flags)) < 0) { |
| 197 | LOG(ERROR) << "Unable to set non-blocking I/O mode on fd " |
| 198 | << record->stdout_fd << "."; |
| 199 | } |
| 200 | |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 201 | record->stdout_controller = base::FileDescriptorWatcher::WatchReadable( |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 202 | record->stdout_fd, |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 203 | base::BindRepeating(&Subprocess::OnStdoutReady, record.get())); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 204 | |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 205 | subprocess_records_[pid] = std::move(record); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 206 | return pid; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 209 | void Subprocess::KillExec(pid_t pid) { |
| 210 | auto pid_record = subprocess_records_.find(pid); |
| 211 | if (pid_record == subprocess_records_.end()) |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 212 | return; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 213 | pid_record->second->callback.Reset(); |
Sen Jiang | 1d3b863 | 2016-05-13 12:32:10 -0700 | [diff] [blame] | 214 | // We don't care about output/return code, so we use SIGKILL here to ensure it |
| 215 | // will be killed, SIGTERM might lead to leaked subprocess. |
| 216 | if (kill(pid, SIGKILL) != 0) { |
| 217 | PLOG(WARNING) << "Error sending SIGKILL to " << pid; |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 218 | } |
| 219 | // Release the pid now so we don't try to kill it if Subprocess is destroyed |
| 220 | // before the corresponding ChildExitedCallback() is called. |
| 221 | pid_record->second->proc.Release(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 224 | int Subprocess::GetPipeFd(pid_t pid, int fd) const { |
| 225 | auto pid_record = subprocess_records_.find(pid); |
| 226 | if (pid_record == subprocess_records_.end()) |
| 227 | return -1; |
| 228 | return pid_record->second->proc.GetPipe(fd); |
| 229 | } |
| 230 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 231 | bool Subprocess::SynchronousExec(const vector<string>& cmd, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 232 | int* return_code, |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 233 | string* stdout, |
| 234 | string* stderr) { |
| 235 | // The default for |SynchronousExec| is to use |kSearchPath| since the code |
| 236 | // relies on that. |
| 237 | return SynchronousExecFlags(cmd, kSearchPath, return_code, stdout, stderr); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool Subprocess::SynchronousExecFlags(const vector<string>& cmd, |
| 241 | uint32_t flags, |
| 242 | int* return_code, |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 243 | string* stdout, |
| 244 | string* stderr) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 245 | brillo::ProcessImpl proc; |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 246 | if (!LaunchProcess(cmd, flags, {STDERR_FILENO}, &proc)) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 247 | LOG(ERROR) << "Failed to launch subprocess"; |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if (stdout) { |
| 252 | stdout->clear(); |
| 253 | } |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 254 | if (stderr) { |
| 255 | stderr->clear(); |
| 256 | } |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 257 | |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 258 | // Read from both stdout and stderr individually. |
| 259 | int stdout_fd = proc.GetPipe(STDOUT_FILENO); |
| 260 | int stderr_fd = proc.GetPipe(STDERR_FILENO); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 261 | vector<char> buffer(32 * 1024); |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 262 | bool stdout_closed = false, stderr_closed = false; |
| 263 | while (!stdout_closed || !stderr_closed) { |
| 264 | if (!stdout_closed) { |
| 265 | int rc = HANDLE_EINTR(read(stdout_fd, buffer.data(), buffer.size())); |
| 266 | if (rc <= 0) { |
| 267 | stdout_closed = true; |
| 268 | if (rc < 0) |
| 269 | PLOG(ERROR) << "Reading from child's stdout"; |
| 270 | } else if (stdout != nullptr) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 271 | stdout->append(buffer.data(), rc); |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | if (!stderr_closed) { |
| 276 | int rc = HANDLE_EINTR(read(stderr_fd, buffer.data(), buffer.size())); |
| 277 | if (rc <= 0) { |
| 278 | stderr_closed = true; |
| 279 | if (rc < 0) |
| 280 | PLOG(ERROR) << "Reading from child's stderr"; |
| 281 | } else if (stderr != nullptr) { |
| 282 | stderr->append(buffer.data(), rc); |
| 283 | } |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 286 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 287 | // At this point, the subprocess already closed the output, so we only need to |
| 288 | // wait for it to finish. |
| 289 | int proc_return_code = proc.Wait(); |
| 290 | if (return_code) |
| 291 | *return_code = proc_return_code; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 292 | return proc_return_code != brillo::Process::kErrorExitStatus; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Amin Hassani | a885954 | 2018-03-07 16:24:43 -0800 | [diff] [blame] | 295 | void Subprocess::FlushBufferedLogsAtExit() { |
| 296 | if (!subprocess_records_.empty()) { |
| 297 | LOG(INFO) << "We are exiting, but there are still in flight subprocesses!"; |
| 298 | for (auto& pid_record : subprocess_records_) { |
| 299 | SubprocessRecord* record = pid_record.second.get(); |
| 300 | // Make sure we read any remaining process output. |
| 301 | OnStdoutReady(record); |
| 302 | if (!record->stdout.empty()) { |
| 303 | LOG(INFO) << "Subprocess(" << pid_record.first << ") output:\n" |
| 304 | << record->stdout; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 310 | Subprocess* Subprocess::subprocess_singleton_ = nullptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 311 | |
| 312 | } // namespace chromeos_update_engine |