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