Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [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 | |
| 5 | #include "update_engine/subprocess.h" |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 6 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 11 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 12 | #include <memory> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 13 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 14 | #include <vector> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 15 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 16 | #include <base/bind.h> |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 17 | #include <base/logging.h> |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 18 | #include <base/posix/eintr_wrapper.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 19 | #include <base/strings/string_util.h> |
| 20 | #include <base/strings/stringprintf.h> |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 21 | #include <chromeos/process.h> |
| 22 | #include <chromeos/secure_blob.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 23 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 24 | using chromeos::MessageLoop; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 26 | using std::unique_ptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | using std::vector; |
| 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 31 | namespace { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 32 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 33 | bool SetupChild(const std::map<string, string>& env, uint32_t flags) { |
| 34 | // Setup the environment variables. |
| 35 | clearenv(); |
| 36 | for (const auto& key_value : env) { |
| 37 | setenv(key_value.first.c_str(), key_value.second.c_str(), 0); |
| 38 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 39 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 40 | if ((flags & Subprocess::kRedirectStderrToStdout) != 0) { |
| 41 | if (HANDLE_EINTR(dup2(STDOUT_FILENO, STDERR_FILENO)) != STDERR_FILENO) |
| 42 | return false; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 43 | } |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 44 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 45 | int fd = HANDLE_EINTR(open("/dev/null", O_RDONLY)); |
| 46 | if (fd < 0) |
| 47 | return false; |
| 48 | if (HANDLE_EINTR(dup2(fd, STDIN_FILENO)) != STDIN_FILENO) |
| 49 | return false; |
| 50 | IGNORE_EINTR(close(fd)); |
| 51 | |
| 52 | return true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 55 | // Helper function to launch a process with the given Subprocess::Flags. |
| 56 | // This function only sets up and starts the process according to the |flags|. |
| 57 | // The caller is responsible for watching the termination of the subprocess. |
| 58 | // Return whether the process was successfully launched and fills in the |proc| |
| 59 | // Process. |
| 60 | bool LaunchProcess(const vector<string>& cmd, |
| 61 | uint32_t flags, |
| 62 | chromeos::Process* proc) { |
| 63 | for (const string& arg : cmd) |
| 64 | proc->AddArg(arg); |
| 65 | proc->SetSearchPath((flags & Subprocess::kSearchPath) != 0); |
| 66 | |
| 67 | // Create an environment for the child process with just the required PATHs. |
| 68 | std::map<string, string> env; |
| 69 | for (const char* key : {"LD_LIBRARY_PATH", "PATH"}) { |
| 70 | const char* value = getenv(key); |
| 71 | if (value) |
| 72 | env.emplace(key, value); |
| 73 | } |
| 74 | |
| 75 | proc->RedirectUsingPipe(STDOUT_FILENO, false); |
| 76 | proc->SetPreExecCallback(base::Bind(&SetupChild, env, flags)); |
| 77 | |
| 78 | return proc->Start(); |
| 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | void Subprocess::Init() { |
| 84 | if (subprocess_singleton_ == this) |
| 85 | return; |
| 86 | CHECK(subprocess_singleton_ == nullptr); |
| 87 | subprocess_singleton_ = this; |
| 88 | |
| 89 | async_signal_handler_.Init(); |
| 90 | process_reaper_.Register(&async_signal_handler_); |
| 91 | } |
| 92 | |
| 93 | Subprocess::~Subprocess() { |
| 94 | if (subprocess_singleton_ == this) |
| 95 | subprocess_singleton_ = nullptr; |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 98 | void Subprocess::OnStdoutReady(SubprocessRecord* record) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 99 | char buf[1024]; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 100 | ssize_t rc = 0; |
| 101 | do { |
| 102 | rc = HANDLE_EINTR(read(record->stdout_fd, buf, arraysize(buf))); |
| 103 | if (rc < 0) { |
| 104 | // EAGAIN and EWOULDBLOCK are normal return values when there's no more |
| 105 | // input as we are in non-blocking mode. |
| 106 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
| 107 | PLOG(ERROR) << "Error reading fd " << record->stdout_fd; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 108 | MessageLoop::current()->CancelTask(record->stdout_task_id); |
| 109 | record->stdout_task_id = MessageLoop::kTaskIdNull; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 110 | } |
Alex Deymo | 957cf4d | 2015-07-14 23:18:33 -0700 | [diff] [blame] | 111 | } else if (rc == 0) { |
| 112 | // A value of 0 means that the child closed its end of the pipe and there |
| 113 | // is nothing else to read from stdout. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 114 | MessageLoop::current()->CancelTask(record->stdout_task_id); |
| 115 | record->stdout_task_id = MessageLoop::kTaskIdNull; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 116 | } else { |
| 117 | record->stdout.append(buf, rc); |
| 118 | } |
| 119 | } while (rc > 0); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 122 | void Subprocess::ChildExitedCallback(const siginfo_t& info) { |
| 123 | auto pid_record = subprocess_records_.find(info.si_pid); |
| 124 | if (pid_record == subprocess_records_.end()) |
| 125 | return; |
| 126 | SubprocessRecord* record = pid_record->second.get(); |
| 127 | |
| 128 | // Make sure we read any remaining process output and then close the pipe. |
| 129 | OnStdoutReady(record); |
| 130 | |
| 131 | MessageLoop::current()->CancelTask(record->stdout_task_id); |
| 132 | record->stdout_task_id = MessageLoop::kTaskIdNull; |
| 133 | |
| 134 | // Release and close all the pipes now. |
| 135 | record->proc.Release(); |
| 136 | record->proc.Reset(0); |
| 137 | |
| 138 | // Don't print any log if the subprocess exited with exit code 0. |
| 139 | if (info.si_code != CLD_EXITED) { |
| 140 | LOG(INFO) << "Subprocess terminated with si_code " << info.si_code; |
| 141 | } else if (info.si_status != 0) { |
| 142 | LOG(INFO) << "Subprocess exited with si_status: " << info.si_status; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 143 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 144 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 145 | if (!record->stdout.empty()) { |
| 146 | LOG(INFO) << "Subprocess output:\n" << record->stdout; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 147 | } |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 148 | if (!record->callback.is_null()) { |
| 149 | record->callback.Run(info.si_status, record->stdout); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 150 | } |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 151 | subprocess_records_.erase(pid_record); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 154 | pid_t Subprocess::Exec(const vector<string>& cmd, |
| 155 | const ExecCallback& callback) { |
| 156 | return ExecFlags(cmd, kRedirectStderrToStdout, callback); |
| 157 | } |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 158 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 159 | pid_t Subprocess::ExecFlags(const vector<string>& cmd, |
| 160 | uint32_t flags, |
| 161 | const ExecCallback& callback) { |
| 162 | unique_ptr<SubprocessRecord> record(new SubprocessRecord(callback)); |
| 163 | |
| 164 | if (!LaunchProcess(cmd, flags, &record->proc)) { |
| 165 | LOG(ERROR) << "Failed to launch subprocess"; |
Chris Masone | c6c57a5 | 2010-09-23 13:06:14 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 168 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 169 | pid_t pid = record->proc.pid(); |
| 170 | CHECK(process_reaper_.WatchForChild(FROM_HERE, pid, base::Bind( |
| 171 | &Subprocess::ChildExitedCallback, |
| 172 | base::Unretained(this)))); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 173 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 174 | record->stdout_fd = record->proc.GetPipe(STDOUT_FILENO); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 175 | // Capture the subprocess output. Make our end of the pipe non-blocking. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 176 | int fd_flags = fcntl(record->stdout_fd, F_GETFL, 0) | O_NONBLOCK; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 177 | if (HANDLE_EINTR(fcntl(record->stdout_fd, F_SETFL, fd_flags)) < 0) { |
| 178 | LOG(ERROR) << "Unable to set non-blocking I/O mode on fd " |
| 179 | << record->stdout_fd << "."; |
| 180 | } |
| 181 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 182 | record->stdout_task_id = MessageLoop::current()->WatchFileDescriptor( |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 183 | FROM_HERE, |
| 184 | record->stdout_fd, |
| 185 | MessageLoop::WatchMode::kWatchRead, |
| 186 | true, |
| 187 | base::Bind(&Subprocess::OnStdoutReady, record.get())); |
| 188 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 189 | subprocess_records_[pid].reset(record.release()); |
| 190 | return pid; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 193 | void Subprocess::KillExec(pid_t pid) { |
| 194 | auto pid_record = subprocess_records_.find(pid); |
| 195 | if (pid_record == subprocess_records_.end()) |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 196 | return; |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 197 | pid_record->second->callback.Reset(); |
| 198 | kill(pid, SIGTERM); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 201 | bool Subprocess::SynchronousExec(const vector<string>& cmd, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 202 | int* return_code, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 203 | string* stdout) { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 204 | // The default for SynchronousExec is to use kSearchPath since the code relies |
| 205 | // on that. |
| 206 | return SynchronousExecFlags( |
| 207 | cmd, |
| 208 | kRedirectStderrToStdout | kSearchPath, |
| 209 | return_code, |
| 210 | stdout); |
| 211 | } |
| 212 | |
| 213 | bool Subprocess::SynchronousExecFlags(const vector<string>& cmd, |
| 214 | uint32_t flags, |
| 215 | int* return_code, |
| 216 | string* stdout) { |
| 217 | chromeos::ProcessImpl proc; |
| 218 | if (!LaunchProcess(cmd, flags, &proc)) { |
| 219 | LOG(ERROR) << "Failed to launch subprocess"; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | if (stdout) { |
| 224 | stdout->clear(); |
| 225 | } |
| 226 | |
| 227 | int fd = proc.GetPipe(STDOUT_FILENO); |
| 228 | vector<char> buffer(32 * 1024); |
| 229 | while (true) { |
| 230 | int rc = HANDLE_EINTR(read(fd, buffer.data(), buffer.size())); |
| 231 | if (rc < 0) { |
| 232 | PLOG(ERROR) << "Reading from child's output"; |
| 233 | break; |
| 234 | } else if (rc == 0) { |
| 235 | break; |
| 236 | } else { |
| 237 | if (stdout) |
| 238 | stdout->append(buffer.data(), rc); |
| 239 | } |
| 240 | } |
| 241 | // At this point, the subprocess already closed the output, so we only need to |
| 242 | // wait for it to finish. |
| 243 | int proc_return_code = proc.Wait(); |
| 244 | if (return_code) |
| 245 | *return_code = proc_return_code; |
| 246 | return proc_return_code != chromeos::Process::kErrorExitStatus; |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 249 | bool Subprocess::SubprocessInFlight() { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame^] | 250 | for (const auto& pid_record : subprocess_records_) { |
| 251 | if (!pid_record.second->callback.is_null()) |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 257 | Subprocess* Subprocess::subprocess_singleton_ = nullptr; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 258 | |
| 259 | } // namespace chromeos_update_engine |