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