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