Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
| 16 | |
Jorge E. Moreira | 2a777f6 | 2018-06-13 17:28:10 -0700 | [diff] [blame] | 17 | #include "common/libs/utils/subprocess.h" |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 18 | |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 19 | #ifdef __linux__ |
| 20 | #include <sys/prctl.h> |
| 21 | #endif |
| 22 | |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 23 | #include <errno.h> |
A. Cody Schuffelen | ffd01ef | 2022-03-24 15:17:05 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
Jorge E. Moreira | c4339ad | 2019-09-05 13:36:06 -0700 | [diff] [blame] | 25 | #include <signal.h> |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
A. Cody Schuffelen | b080c5a | 2023-08-22 21:42:45 -0700 | [diff] [blame] | 27 | #include <string.h> |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
| 31 | |
A. Cody Schuffelen | ffd01ef | 2022-03-24 15:17:05 -0700 | [diff] [blame] | 32 | #include <cerrno> |
| 33 | #include <cstring> |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 34 | #include <map> |
A. Cody Schuffelen | 04d182d | 2022-07-29 17:49:42 -0700 | [diff] [blame] | 35 | #include <optional> |
A. Cody Schuffelen | ffd01ef | 2022-03-24 15:17:05 -0700 | [diff] [blame] | 36 | #include <ostream> |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 37 | #include <set> |
A. Cody Schuffelen | ffd01ef | 2022-03-24 15:17:05 -0700 | [diff] [blame] | 38 | #include <sstream> |
| 39 | #include <string> |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 40 | #include <thread> |
A. Cody Schuffelen | 04d182d | 2022-07-29 17:49:42 -0700 | [diff] [blame] | 41 | #include <type_traits> |
A. Cody Schuffelen | ffd01ef | 2022-03-24 15:17:05 -0700 | [diff] [blame] | 42 | #include <utility> |
| 43 | #include <vector> |
| 44 | |
| 45 | #include <android-base/logging.h> |
| 46 | #include <android-base/strings.h> |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 47 | |
Cody Schuffelen | 5133a31 | 2019-08-02 17:58:00 -0700 | [diff] [blame] | 48 | #include "common/libs/fs/shared_buf.h" |
Jason Macnak | d8ff5cc | 2021-09-21 11:13:14 -0700 | [diff] [blame] | 49 | #include "common/libs/utils/files.h" |
Cody Schuffelen | 5133a31 | 2019-08-02 17:58:00 -0700 | [diff] [blame] | 50 | |
Daniel Norman | 1c1fa8c | 2021-08-11 09:42:45 -0700 | [diff] [blame] | 51 | extern char** environ; |
| 52 | |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 53 | namespace cuttlefish { |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 54 | namespace { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 55 | |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 56 | // If a redirected-to file descriptor was already closed, it's possible that |
| 57 | // some inherited file descriptor duped to this file descriptor and the redirect |
| 58 | // would override that. This function makes sure that doesn't happen. |
| 59 | bool validate_redirects( |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 60 | const std::map<Subprocess::StdIOChannel, int>& redirects, |
| 61 | const std::map<SharedFD, int>& inherited_fds) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 62 | // Add the redirected IO channels to a set as integers. This allows converting |
| 63 | // the enum values into integers instead of the other way around. |
| 64 | std::set<int> int_redirects; |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 65 | for (const auto& entry : redirects) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 66 | int_redirects.insert(static_cast<int>(entry.first)); |
| 67 | } |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 68 | for (const auto& entry : inherited_fds) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 69 | auto dupped_fd = entry.second; |
| 70 | if (int_redirects.count(dupped_fd)) { |
| 71 | LOG(ERROR) << "Requested redirect of fd(" << dupped_fd |
| 72 | << ") conflicts with inherited FD."; |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 79 | void do_redirects(const std::map<Subprocess::StdIOChannel, int>& redirects) { |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 80 | for (const auto& entry : redirects) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 81 | auto std_channel = static_cast<int>(entry.first); |
| 82 | auto fd = entry.second; |
| 83 | TEMP_FAILURE_RETRY(dup2(fd, std_channel)); |
| 84 | } |
| 85 | } |
| 86 | |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 87 | std::vector<const char*> ToCharPointers(const std::vector<std::string>& vect) { |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 88 | std::vector<const char*> ret = {}; |
| 89 | for (const auto& str : vect) { |
| 90 | ret.push_back(str.c_str()); |
| 91 | } |
| 92 | ret.push_back(NULL); |
| 93 | return ret; |
| 94 | } |
| 95 | } // namespace |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 96 | |
Steve Kim | 1f3fd7a | 2023-04-27 13:15:35 -0700 | [diff] [blame] | 97 | std::vector<std::string> ArgsToVec(char** argv) { |
| 98 | std::vector<std::string> args; |
| 99 | for (int i = 0; argv && argv[i]; i++) { |
| 100 | args.push_back(argv[i]); |
| 101 | } |
| 102 | return args; |
| 103 | } |
| 104 | |
| 105 | std::unordered_map<std::string, std::string> EnvpToMap(char** envp) { |
| 106 | std::unordered_map<std::string, std::string> env_map; |
| 107 | if (!envp) { |
| 108 | return env_map; |
| 109 | } |
| 110 | for (char** e = envp; *e != nullptr; e++) { |
| 111 | std::string env_var_val(*e); |
| 112 | auto tokens = android::base::Split(env_var_val, "="); |
| 113 | if (tokens.size() <= 1) { |
| 114 | LOG(WARNING) << "Environment var in unknown format: " << env_var_val; |
| 115 | continue; |
| 116 | } |
| 117 | const auto var = tokens.at(0); |
| 118 | tokens.erase(tokens.begin()); |
| 119 | env_map[var] = android::base::Join(tokens, "="); |
| 120 | } |
| 121 | return env_map; |
| 122 | } |
| 123 | |
A. Cody Schuffelen | a3879a5 | 2021-11-03 17:48:16 -0700 | [diff] [blame] | 124 | SubprocessOptions& SubprocessOptions::Verbose(bool verbose) & { |
| 125 | verbose_ = verbose; |
| 126 | return *this; |
| 127 | } |
| 128 | SubprocessOptions SubprocessOptions::Verbose(bool verbose) && { |
| 129 | verbose_ = verbose; |
| 130 | return *this; |
| 131 | } |
| 132 | |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 133 | #ifdef __linux__ |
A. Cody Schuffelen | a3879a5 | 2021-11-03 17:48:16 -0700 | [diff] [blame] | 134 | SubprocessOptions& SubprocessOptions::ExitWithParent(bool v) & { |
| 135 | exit_with_parent_ = v; |
| 136 | return *this; |
| 137 | } |
| 138 | SubprocessOptions SubprocessOptions::ExitWithParent(bool v) && { |
| 139 | exit_with_parent_ = v; |
| 140 | return *this; |
| 141 | } |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 142 | #endif |
A. Cody Schuffelen | a3879a5 | 2021-11-03 17:48:16 -0700 | [diff] [blame] | 143 | |
| 144 | SubprocessOptions& SubprocessOptions::InGroup(bool in_group) & { |
| 145 | in_group_ = in_group; |
| 146 | return *this; |
| 147 | } |
| 148 | SubprocessOptions SubprocessOptions::InGroup(bool in_group) && { |
| 149 | in_group_ = in_group; |
| 150 | return *this; |
| 151 | } |
| 152 | |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 153 | Subprocess::Subprocess(Subprocess&& subprocess) |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 154 | : pid_(subprocess.pid_.load()), |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 155 | started_(subprocess.started_), |
Jorge E. Moreira | c4339ad | 2019-09-05 13:36:06 -0700 | [diff] [blame] | 156 | stopper_(subprocess.stopper_) { |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 157 | // Make sure the moved object no longer controls this subprocess |
| 158 | subprocess.pid_ = -1; |
| 159 | subprocess.started_ = false; |
| 160 | } |
| 161 | |
| 162 | Subprocess& Subprocess::operator=(Subprocess&& other) { |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 163 | pid_ = other.pid_.load(); |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 164 | started_ = other.started_; |
Jorge E. Moreira | c4339ad | 2019-09-05 13:36:06 -0700 | [diff] [blame] | 165 | stopper_ = other.stopper_; |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 166 | |
| 167 | other.pid_ = -1; |
| 168 | other.started_ = false; |
| 169 | return *this; |
| 170 | } |
| 171 | |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 172 | int Subprocess::Wait() { |
| 173 | if (pid_ < 0) { |
| 174 | LOG(ERROR) |
| 175 | << "Attempt to wait on invalid pid(has it been waited on already?): " |
| 176 | << pid_; |
| 177 | return -1; |
| 178 | } |
| 179 | int wstatus = 0; |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 180 | auto pid = pid_.load(); // Wait will set pid_ to -1 after waiting |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 181 | auto wait_ret = waitpid(pid, &wstatus, 0); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 182 | if (wait_ret < 0) { |
Jorge E. Moreira | c4339ad | 2019-09-05 13:36:06 -0700 | [diff] [blame] | 183 | auto error = errno; |
| 184 | LOG(ERROR) << "Error on call to waitpid: " << strerror(error); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 185 | return wait_ret; |
| 186 | } |
| 187 | int retval = 0; |
| 188 | if (WIFEXITED(wstatus)) { |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 189 | pid_ = -1; |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 190 | retval = WEXITSTATUS(wstatus); |
| 191 | if (retval) { |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 192 | LOG(DEBUG) << "Subprocess " << pid |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 193 | << " exited with error code: " << retval; |
| 194 | } |
| 195 | } else if (WIFSIGNALED(wstatus)) { |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 196 | pid_ = -1; |
A. Cody Schuffelen | b080c5a | 2023-08-22 21:42:45 -0700 | [diff] [blame] | 197 | int sig_num = WTERMSIG(wstatus); |
| 198 | LOG(ERROR) << "Subprocess " << pid << " was interrupted by a signal '" |
| 199 | << strsignal(sig_num) << "' (" << sig_num << ")"; |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 200 | retval = -1; |
| 201 | } |
| 202 | return retval; |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 203 | } |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 204 | int Subprocess::Wait(siginfo_t* infop, int options) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 205 | if (pid_ < 0) { |
| 206 | LOG(ERROR) |
| 207 | << "Attempt to wait on invalid pid(has it been waited on already?): " |
| 208 | << pid_; |
| 209 | return -1; |
| 210 | } |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 211 | *infop = {}; |
| 212 | auto retval = waitid(P_PID, pid_, infop, options); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 213 | // We don't want to wait twice for the same process |
Steve Kim | 1f3fd7a | 2023-04-27 13:15:35 -0700 | [diff] [blame] | 214 | bool exited = infop->si_code == CLD_EXITED || infop->si_code == CLD_DUMPED; |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 215 | bool reaped = !(options & WNOWAIT); |
| 216 | if (exited && reaped) { |
| 217 | pid_ = -1; |
| 218 | } |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 219 | return retval; |
| 220 | } |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 221 | |
Steve Kim | 60b776d | 2023-06-16 11:01:18 -0700 | [diff] [blame] | 222 | static Result<void> SendSignalImpl(const int signal, const pid_t pid, |
| 223 | bool to_group, const bool started) { |
| 224 | if (pid == -1) { |
| 225 | return CF_ERR(strerror(ESRCH)); |
| 226 | } |
| 227 | CF_EXPECTF(started == true, |
| 228 | "The Subprocess object lost the ownership" |
| 229 | "of the process {}.", |
| 230 | pid); |
| 231 | int ret_code = 0; |
| 232 | if (to_group) { |
| 233 | ret_code = killpg(getpgid(pid), signal); |
| 234 | } else { |
| 235 | ret_code = kill(pid, signal); |
| 236 | } |
| 237 | CF_EXPECTF(ret_code == 0, "kill/killpg returns {} with errno: {}", ret_code, |
| 238 | strerror(errno)); |
| 239 | return {}; |
| 240 | } |
| 241 | |
| 242 | Result<void> Subprocess::SendSignal(const int signal) { |
| 243 | CF_EXPECT(SendSignalImpl(signal, pid_, /* to_group */ false, started_)); |
| 244 | return {}; |
| 245 | } |
| 246 | |
| 247 | Result<void> Subprocess::SendSignalToGroup(const int signal) { |
| 248 | CF_EXPECT(SendSignalImpl(signal, pid_, /* to_group */ true, started_)); |
| 249 | return {}; |
| 250 | } |
| 251 | |
A. Cody Schuffelen | c37a5ce | 2021-07-01 18:09:55 -0700 | [diff] [blame] | 252 | StopperResult KillSubprocess(Subprocess* subprocess) { |
Cody Schuffelen | 5aa643f | 2020-03-10 17:03:41 +0000 | [diff] [blame] | 253 | auto pid = subprocess->pid(); |
| 254 | if (pid > 0) { |
| 255 | auto pgid = getpgid(pid); |
| 256 | if (pgid < 0) { |
| 257 | auto error = errno; |
| 258 | LOG(WARNING) << "Error obtaining process group id of process with pid=" |
| 259 | << pid << ": " << strerror(error); |
| 260 | // Send the kill signal anyways, because pgid will be -1 it will be sent |
| 261 | // to the process and not a (non-existent) group |
| 262 | } |
| 263 | bool is_group_head = pid == pgid; |
A. Cody Schuffelen | c37a5ce | 2021-07-01 18:09:55 -0700 | [diff] [blame] | 264 | auto kill_ret = (is_group_head ? killpg : kill)(pid, SIGKILL); |
| 265 | if (kill_ret == 0) { |
| 266 | return StopperResult::kStopSuccess; |
Cody Schuffelen | 5aa643f | 2020-03-10 17:03:41 +0000 | [diff] [blame] | 267 | } |
A. Cody Schuffelen | c37a5ce | 2021-07-01 18:09:55 -0700 | [diff] [blame] | 268 | auto kill_cmd = is_group_head ? "killpg(" : "kill("; |
| 269 | PLOG(ERROR) << kill_cmd << pid << ", SIGKILL) failed: "; |
| 270 | return StopperResult::kStopFailure; |
Cody Schuffelen | 5aa643f | 2020-03-10 17:03:41 +0000 | [diff] [blame] | 271 | } |
A. Cody Schuffelen | c37a5ce | 2021-07-01 18:09:55 -0700 | [diff] [blame] | 272 | return StopperResult::kStopSuccess; |
Jorge E. Moreira | c4339ad | 2019-09-05 13:36:06 -0700 | [diff] [blame] | 273 | } |
Jorge E. Moreira | b945eb1 | 2019-05-06 17:20:38 -0700 | [diff] [blame] | 274 | |
A. Cody Schuffelen | 6a0331c | 2022-05-06 14:46:25 -0700 | [diff] [blame] | 275 | Command::Command(std::string executable, SubprocessStopper stopper) |
Daniel Norman | 1c1fa8c | 2021-08-11 09:42:45 -0700 | [diff] [blame] | 276 | : subprocess_stopper_(stopper) { |
| 277 | for (char** env = environ; *env; env++) { |
| 278 | env_.emplace_back(*env); |
| 279 | } |
A. Cody Schuffelen | 6a0331c | 2022-05-06 14:46:25 -0700 | [diff] [blame] | 280 | command_.emplace_back(std::move(executable)); |
Daniel Norman | 1c1fa8c | 2021-08-11 09:42:45 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 283 | Command::~Command() { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 284 | // Close all inherited file descriptors |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 285 | for (const auto& entry : inherited_fds_) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 286 | close(entry.second); |
| 287 | } |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 288 | // Close all redirected file descriptors |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 289 | for (const auto& entry : redirects_) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 290 | close(entry.second); |
| 291 | } |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 292 | } |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 293 | |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 294 | void Command::BuildParameter(std::stringstream* stream, SharedFD shared_fd) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 295 | int fd; |
| 296 | if (inherited_fds_.count(shared_fd)) { |
| 297 | fd = inherited_fds_[shared_fd]; |
| 298 | } else { |
Cody Schuffelen | c33a1dc | 2019-11-18 18:29:00 -0800 | [diff] [blame] | 299 | fd = shared_fd->Fcntl(F_DUPFD_CLOEXEC, 3); |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 300 | CHECK(fd >= 0) << "Could not acquire a new file descriptor: " |
| 301 | << shared_fd->StrError(); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 302 | inherited_fds_[shared_fd] = fd; |
| 303 | } |
| 304 | *stream << fd; |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 305 | } |
| 306 | |
A. Cody Schuffelen | e8b0b17 | 2022-01-25 15:09:30 -0800 | [diff] [blame] | 307 | Command& Command::RedirectStdIO(Subprocess::StdIOChannel channel, |
| 308 | SharedFD shared_fd) & { |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 309 | CHECK(shared_fd->IsOpen()); |
| 310 | CHECK(redirects_.count(channel) == 0) |
| 311 | << "Attempted multiple redirections of fd: " << static_cast<int>(channel); |
Cody Schuffelen | c33a1dc | 2019-11-18 18:29:00 -0800 | [diff] [blame] | 312 | auto dup_fd = shared_fd->Fcntl(F_DUPFD_CLOEXEC, 3); |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 313 | CHECK(dup_fd >= 0) << "Could not acquire a new file descriptor: " |
| 314 | << shared_fd->StrError(); |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 315 | redirects_[channel] = dup_fd; |
A. Cody Schuffelen | e8b0b17 | 2022-01-25 15:09:30 -0800 | [diff] [blame] | 316 | return *this; |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 317 | } |
A. Cody Schuffelen | e8b0b17 | 2022-01-25 15:09:30 -0800 | [diff] [blame] | 318 | Command Command::RedirectStdIO(Subprocess::StdIOChannel channel, |
| 319 | SharedFD shared_fd) && { |
| 320 | RedirectStdIO(channel, shared_fd); |
| 321 | return std::move(*this); |
| 322 | } |
| 323 | Command& Command::RedirectStdIO(Subprocess::StdIOChannel subprocess_channel, |
| 324 | Subprocess::StdIOChannel parent_channel) & { |
Cody Schuffelen | c374c8c | 2019-08-21 18:47:42 -0700 | [diff] [blame] | 325 | return RedirectStdIO(subprocess_channel, |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 326 | SharedFD::Dup(static_cast<int>(parent_channel))); |
Cody Schuffelen | c374c8c | 2019-08-21 18:47:42 -0700 | [diff] [blame] | 327 | } |
A. Cody Schuffelen | e8b0b17 | 2022-01-25 15:09:30 -0800 | [diff] [blame] | 328 | Command Command::RedirectStdIO(Subprocess::StdIOChannel subprocess_channel, |
| 329 | Subprocess::StdIOChannel parent_channel) && { |
| 330 | RedirectStdIO(subprocess_channel, parent_channel); |
| 331 | return std::move(*this); |
| 332 | } |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 333 | |
A. Cody Schuffelen | e01f736 | 2022-05-04 18:39:04 -0700 | [diff] [blame] | 334 | Command& Command::SetWorkingDirectory(const std::string& path) & { |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 335 | #ifdef __linux__ |
A. Cody Schuffelen | 2f7cdb4 | 2022-04-13 18:14:18 -0700 | [diff] [blame] | 336 | auto fd = SharedFD::Open(path, O_RDONLY | O_PATH | O_DIRECTORY); |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 337 | #elif defined(__APPLE__) |
| 338 | auto fd = SharedFD::Open(path, O_RDONLY | O_DIRECTORY); |
| 339 | #else |
| 340 | #error "Unsupported operating system" |
| 341 | #endif |
A. Cody Schuffelen | 2f7cdb4 | 2022-04-13 18:14:18 -0700 | [diff] [blame] | 342 | CHECK(fd->IsOpen()) << "Could not open \"" << path |
| 343 | << "\" dir fd: " << fd->StrError(); |
| 344 | return SetWorkingDirectory(fd); |
| 345 | } |
A. Cody Schuffelen | e01f736 | 2022-05-04 18:39:04 -0700 | [diff] [blame] | 346 | Command Command::SetWorkingDirectory(const std::string& path) && { |
| 347 | return std::move(SetWorkingDirectory(path)); |
A. Cody Schuffelen | 2f7cdb4 | 2022-04-13 18:14:18 -0700 | [diff] [blame] | 348 | } |
| 349 | Command& Command::SetWorkingDirectory(SharedFD dirfd) & { |
| 350 | CHECK(dirfd->IsOpen()) << "Dir fd invalid: " << dirfd->StrError(); |
A. Cody Schuffelen | e01f736 | 2022-05-04 18:39:04 -0700 | [diff] [blame] | 351 | working_directory_ = std::move(dirfd); |
A. Cody Schuffelen | 2f7cdb4 | 2022-04-13 18:14:18 -0700 | [diff] [blame] | 352 | return *this; |
| 353 | } |
| 354 | Command Command::SetWorkingDirectory(SharedFD dirfd) && { |
A. Cody Schuffelen | e01f736 | 2022-05-04 18:39:04 -0700 | [diff] [blame] | 355 | return std::move(SetWorkingDirectory(std::move(dirfd))); |
A. Cody Schuffelen | 2f7cdb4 | 2022-04-13 18:14:18 -0700 | [diff] [blame] | 356 | } |
| 357 | |
JaeMan Park | ebd19fc | 2023-06-16 14:07:44 +0900 | [diff] [blame] | 358 | Command& Command::AddPrerequisite( |
| 359 | const std::function<Result<void>()>& prerequisite) & { |
| 360 | prerequisites_.push_back(prerequisite); |
| 361 | return *this; |
| 362 | } |
| 363 | |
| 364 | Command Command::AddPrerequisite( |
| 365 | const std::function<Result<void>()>& prerequisite) && { |
| 366 | prerequisites_.push_back(prerequisite); |
| 367 | return std::move(*this); |
| 368 | } |
| 369 | |
Cody Schuffelen | f98a432 | 2019-12-10 17:36:44 -0800 | [diff] [blame] | 370 | Subprocess Command::Start(SubprocessOptions options) const { |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 371 | auto cmd = ToCharPointers(command_); |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 372 | |
| 373 | if (!validate_redirects(redirects_, inherited_fds_)) { |
| 374 | return Subprocess(-1, {}); |
| 375 | } |
| 376 | |
| 377 | pid_t pid = fork(); |
| 378 | if (!pid) { |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 379 | #ifdef __linux__ |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 380 | if (options.ExitWithParent()) { |
| 381 | prctl(PR_SET_PDEATHSIG, SIGHUP); // Die when parent dies |
| 382 | } |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 383 | #endif |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 384 | |
| 385 | do_redirects(redirects_); |
JaeMan Park | ebd19fc | 2023-06-16 14:07:44 +0900 | [diff] [blame] | 386 | |
| 387 | for (auto& prerequisite : prerequisites_) { |
| 388 | auto prerequisiteResult = prerequisite(); |
| 389 | |
| 390 | if (!prerequisiteResult.ok()) { |
| 391 | LOG(ERROR) << "Failed to check prerequisites: " |
A. Cody Schuffelen | b580d3c | 2023-08-28 22:28:29 -0700 | [diff] [blame] | 392 | << prerequisiteResult.error().FormatForEnv(); |
JaeMan Park | ebd19fc | 2023-06-16 14:07:44 +0900 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 396 | if (options.InGroup()) { |
| 397 | // This call should never fail (see SETPGID(2)) |
| 398 | if (setpgid(0, 0) != 0) { |
| 399 | auto error = errno; |
| 400 | LOG(ERROR) << "setpgid failed (" << strerror(error) << ")"; |
| 401 | } |
| 402 | } |
| 403 | for (const auto& entry : inherited_fds_) { |
| 404 | if (fcntl(entry.second, F_SETFD, 0)) { |
| 405 | int error_num = errno; |
| 406 | LOG(ERROR) << "fcntl failed: " << strerror(error_num); |
| 407 | } |
| 408 | } |
A. Cody Schuffelen | 2f7cdb4 | 2022-04-13 18:14:18 -0700 | [diff] [blame] | 409 | if (working_directory_->IsOpen()) { |
| 410 | if (SharedFD::Fchdir(working_directory_) != 0) { |
| 411 | LOG(ERROR) << "Fchdir failed: " << working_directory_->StrError(); |
| 412 | } |
| 413 | } |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 414 | int rval; |
Daniel Norman | 1c1fa8c | 2021-08-11 09:42:45 -0700 | [diff] [blame] | 415 | auto envp = ToCharPointers(env_); |
A. Cody Schuffelen | 6a0331c | 2022-05-06 14:46:25 -0700 | [diff] [blame] | 416 | const char* executable = executable_ ? executable_->c_str() : cmd[0]; |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 417 | #ifdef __linux__ |
A. Cody Schuffelen | 6a0331c | 2022-05-06 14:46:25 -0700 | [diff] [blame] | 418 | rval = execvpe(executable, const_cast<char* const*>(cmd.data()), |
Daniel Norman | 1c1fa8c | 2021-08-11 09:42:45 -0700 | [diff] [blame] | 419 | const_cast<char* const*>(envp.data())); |
A. Cody Schuffelen | 50ee053 | 2023-06-22 15:51:56 -0700 | [diff] [blame] | 420 | #elif defined(__APPLE__) |
| 421 | rval = execve(executable, const_cast<char* const*>(cmd.data()), |
| 422 | const_cast<char* const*>(envp.data())); |
| 423 | #else |
| 424 | #error "Unsupported architecture" |
| 425 | #endif |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 426 | // No need for an if: if exec worked it wouldn't have returned |
A. Cody Schuffelen | 94b6a63 | 2022-09-02 13:36:23 -0700 | [diff] [blame] | 427 | LOG(ERROR) << "exec of " << cmd[0] << " with path \"" << executable |
| 428 | << "\" failed (" << strerror(errno) << ")"; |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 429 | exit(rval); |
| 430 | } |
| 431 | if (pid == -1) { |
| 432 | LOG(ERROR) << "fork failed (" << strerror(errno) << ")"; |
| 433 | } |
A. Cody Schuffelen | f745774 | 2020-06-09 14:10:54 -0700 | [diff] [blame] | 434 | if (options.Verbose()) { // "more verbose", and LOG(DEBUG) > LOG(VERBOSE) |
| 435 | LOG(DEBUG) << "Started (pid: " << pid << "): " << cmd[0]; |
| 436 | for (int i = 1; cmd[i]; i++) { |
| 437 | LOG(DEBUG) << cmd[i]; |
| 438 | } |
| 439 | } else { |
| 440 | LOG(VERBOSE) << "Started (pid: " << pid << "): " << cmd[0]; |
| 441 | for (int i = 1; cmd[i]; i++) { |
| 442 | LOG(VERBOSE) << cmd[i]; |
Cody Schuffelen | 198402c | 2019-12-10 15:07:30 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
A. Cody Schuffelen | 19988a6 | 2021-01-13 18:58:52 -0800 | [diff] [blame] | 445 | return Subprocess(pid, subprocess_stopper_); |
Jorge E. Moreira | 189d823 | 2019-08-30 11:43:05 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Jason Macnak | d8ff5cc | 2021-09-21 11:13:14 -0700 | [diff] [blame] | 448 | std::string Command::AsBashScript( |
| 449 | const std::string& redirected_stdio_path) const { |
| 450 | CHECK(inherited_fds_.empty()) |
| 451 | << "Bash wrapper will not have inheritied file descriptors."; |
| 452 | CHECK(redirects_.empty()) << "Bash wrapper will not have redirected stdio."; |
| 453 | |
| 454 | std::string contents = |
| 455 | "#!/bin/bash\n\n" + android::base::Join(command_, " \\\n"); |
| 456 | if (!redirected_stdio_path.empty()) { |
| 457 | contents += " &> " + AbsolutePath(redirected_stdio_path); |
| 458 | } |
| 459 | return contents; |
| 460 | } |
| 461 | |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 462 | // A class that waits for threads to exit in its destructor. |
| 463 | class ThreadJoiner { |
| 464 | std::vector<std::thread*> threads_; |
| 465 | public: |
| 466 | ThreadJoiner(const std::vector<std::thread*> threads) : threads_(threads) {} |
| 467 | ~ThreadJoiner() { |
| 468 | for (auto& thread : threads_) { |
| 469 | if (thread->joinable()) { |
| 470 | thread->join(); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | }; |
| 475 | |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 476 | int RunWithManagedStdio(Command&& cmd_tmp, const std::string* stdin_str, |
| 477 | std::string* stdout_str, std::string* stderr_str, |
Cody Schuffelen | f98a432 | 2019-12-10 17:36:44 -0800 | [diff] [blame] | 478 | SubprocessOptions options) { |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 479 | /* |
| 480 | * The order of these declarations is necessary for safety. If the function |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 481 | * returns at any point, the Command will be destroyed first, closing all |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 482 | * of its references to SharedFDs. This will cause the thread internals to fail |
| 483 | * their reads or writes. The ThreadJoiner then waits for the threads to |
| 484 | * complete, as running the destructor of an active std::thread crashes the |
| 485 | * program. |
| 486 | * |
| 487 | * C++ scoping rules dictate that objects are descoped in reverse order to |
| 488 | * construction, so this behavior is predictable. |
| 489 | */ |
| 490 | std::thread stdin_thread, stdout_thread, stderr_thread; |
| 491 | ThreadJoiner thread_joiner({&stdin_thread, &stdout_thread, &stderr_thread}); |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 492 | Command cmd = std::move(cmd_tmp); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 493 | bool io_error = false; |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 494 | if (stdin_str != nullptr) { |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 495 | SharedFD pipe_read, pipe_write; |
| 496 | if (!SharedFD::Pipe(&pipe_read, &pipe_write)) { |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 497 | LOG(ERROR) << "Could not create a pipe to write the stdin of \"" |
| 498 | << cmd.GetShortName() << "\""; |
| 499 | return -1; |
| 500 | } |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 501 | cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdIn, pipe_read); |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 502 | stdin_thread = std::thread([pipe_write, stdin_str, &io_error]() { |
| 503 | int written = WriteAll(pipe_write, *stdin_str); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 504 | if (written < 0) { |
| 505 | io_error = true; |
| 506 | LOG(ERROR) << "Error in writing stdin to process"; |
| 507 | } |
| 508 | }); |
| 509 | } |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 510 | if (stdout_str != nullptr) { |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 511 | SharedFD pipe_read, pipe_write; |
| 512 | if (!SharedFD::Pipe(&pipe_read, &pipe_write)) { |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 513 | LOG(ERROR) << "Could not create a pipe to read the stdout of \"" |
| 514 | << cmd.GetShortName() << "\""; |
| 515 | return -1; |
| 516 | } |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 517 | cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut, pipe_write); |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 518 | stdout_thread = std::thread([pipe_read, stdout_str, &io_error]() { |
| 519 | int read = ReadAll(pipe_read, stdout_str); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 520 | if (read < 0) { |
| 521 | io_error = true; |
| 522 | LOG(ERROR) << "Error in reading stdout from process"; |
| 523 | } |
| 524 | }); |
| 525 | } |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 526 | if (stderr_str != nullptr) { |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 527 | SharedFD pipe_read, pipe_write; |
| 528 | if (!SharedFD::Pipe(&pipe_read, &pipe_write)) { |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 529 | LOG(ERROR) << "Could not create a pipe to read the stderr of \"" |
| 530 | << cmd.GetShortName() << "\""; |
| 531 | return -1; |
| 532 | } |
A. Cody Schuffelen | c99c6d4 | 2021-07-02 17:58:59 -0700 | [diff] [blame] | 533 | cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdErr, pipe_write); |
Colin Cross | b53586e | 2021-09-13 16:19:15 -0700 | [diff] [blame] | 534 | stderr_thread = std::thread([pipe_read, stderr_str, &io_error]() { |
| 535 | int read = ReadAll(pipe_read, stderr_str); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 536 | if (read < 0) { |
| 537 | io_error = true; |
| 538 | LOG(ERROR) << "Error in reading stderr from process"; |
| 539 | } |
| 540 | }); |
| 541 | } |
| 542 | |
Cody Schuffelen | f98a432 | 2019-12-10 17:36:44 -0800 | [diff] [blame] | 543 | auto subprocess = cmd.Start(options); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 544 | if (!subprocess.Started()) { |
| 545 | return -1; |
| 546 | } |
Chih-Hung Hsieh | b8027db | 2020-03-06 15:53:01 -0800 | [diff] [blame] | 547 | auto cmd_short_name = cmd.GetShortName(); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 548 | { |
| 549 | // Force the destructor to run by moving it into a smaller scope. |
| 550 | // This is necessary to close the write end of the pipe. |
A. Cody Schuffelen | d4b6b55 | 2020-06-23 22:26:02 +0000 | [diff] [blame] | 551 | Command forceDelete = std::move(cmd); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 552 | } |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 553 | |
| 554 | int code = subprocess.Wait(); |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 555 | { |
| 556 | auto join_threads = std::move(thread_joiner); |
| 557 | } |
| 558 | if (io_error) { |
Chih-Hung Hsieh | b8027db | 2020-03-06 15:53:01 -0800 | [diff] [blame] | 559 | LOG(ERROR) << "IO error communicating with " << cmd_short_name; |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 560 | return -1; |
| 561 | } |
A. Cody Schuffelen | 658b8b7 | 2022-02-25 16:07:31 -0800 | [diff] [blame] | 562 | return code; |
Cody Schuffelen | 7e35fb3 | 2019-10-03 16:28:35 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Steve Kim | 4951428 | 2023-05-01 17:38:57 -0700 | [diff] [blame] | 565 | namespace { |
| 566 | |
| 567 | struct ExtraParam { |
| 568 | // option for Subprocess::Start() |
| 569 | SubprocessOptions subprocess_options; |
| 570 | // options for Subprocess::Wait(...) |
| 571 | int wait_options; |
| 572 | siginfo_t* infop; |
| 573 | }; |
| 574 | Result<int> ExecuteImpl(const std::vector<std::string>& command, |
| 575 | const std::optional<std::vector<std::string>>& envs, |
| 576 | const std::optional<ExtraParam>& extra_param) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 577 | Command cmd(command[0]); |
| 578 | for (size_t i = 1; i < command.size(); ++i) { |
| 579 | cmd.AddParameter(command[i]); |
| 580 | } |
Steve Kim | 4951428 | 2023-05-01 17:38:57 -0700 | [diff] [blame] | 581 | if (envs) { |
| 582 | cmd.SetEnvironment(*envs); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 583 | } |
Steve Kim | 4951428 | 2023-05-01 17:38:57 -0700 | [diff] [blame] | 584 | auto subprocess = |
| 585 | (!extra_param ? cmd.Start() : cmd.Start(extra_param->subprocess_options)); |
| 586 | CF_EXPECT(subprocess.Started(), "Subprocess failed to start."); |
| 587 | |
| 588 | if (extra_param) { |
| 589 | CF_EXPECT(extra_param->infop != nullptr, |
| 590 | "When ExtraParam is given, the infop buffer address " |
| 591 | << "must not be nullptr."); |
| 592 | return subprocess.Wait(extra_param->infop, extra_param->wait_options); |
| 593 | } else { |
| 594 | return subprocess.Wait(); |
| 595 | } |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 596 | } |
Steve Kim | 4951428 | 2023-05-01 17:38:57 -0700 | [diff] [blame] | 597 | |
| 598 | } // namespace |
| 599 | |
| 600 | int Execute(const std::vector<std::string>& commands, |
| 601 | const std::vector<std::string>& envs) { |
| 602 | auto result = ExecuteImpl(commands, envs, /* extra_param */ std::nullopt); |
| 603 | return (!result.ok() ? -1 : *result); |
| 604 | } |
| 605 | |
| 606 | int Execute(const std::vector<std::string>& commands) { |
| 607 | std::vector<std::string> envs; |
| 608 | auto result = ExecuteImpl(commands, /* envs */ std::nullopt, |
| 609 | /* extra_param */ std::nullopt); |
| 610 | return (!result.ok() ? -1 : *result); |
| 611 | } |
| 612 | |
| 613 | Result<siginfo_t> Execute(const std::vector<std::string>& commands, |
| 614 | SubprocessOptions subprocess_options, |
| 615 | int wait_options) { |
| 616 | siginfo_t info; |
| 617 | auto ret_code = |
| 618 | CF_EXPECT(ExecuteImpl(commands, /* envs */ std::nullopt, |
| 619 | ExtraParam{.subprocess_options = subprocess_options, |
Steve Kim | 2bcb6bf | 2023-11-23 22:19:45 +0000 | [diff] [blame] | 620 | .wait_options = wait_options, |
| 621 | .infop = &info})); |
Steve Kim | 4951428 | 2023-05-01 17:38:57 -0700 | [diff] [blame] | 622 | CF_EXPECT(ret_code == 0, "Subprocess::Wait() returned " << ret_code); |
| 623 | return info; |
| 624 | } |
| 625 | |
| 626 | Result<siginfo_t> Execute(const std::vector<std::string>& commands, |
| 627 | const std::vector<std::string>& envs, |
| 628 | SubprocessOptions subprocess_options, |
| 629 | int wait_options) { |
| 630 | siginfo_t info; |
| 631 | auto ret_code = |
| 632 | CF_EXPECT(ExecuteImpl(commands, envs, |
| 633 | ExtraParam{.subprocess_options = subprocess_options, |
Steve Kim | 2bcb6bf | 2023-11-23 22:19:45 +0000 | [diff] [blame] | 634 | .wait_options = wait_options, |
| 635 | .infop = &info})); |
Steve Kim | 4951428 | 2023-05-01 17:38:57 -0700 | [diff] [blame] | 636 | CF_EXPECT(ret_code == 0, "Subprocess::Wait() returned " << ret_code); |
| 637 | return info; |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 638 | } |
Cody Schuffelen | 5133a31 | 2019-08-02 17:58:00 -0700 | [diff] [blame] | 639 | |
A. Cody Schuffelen | 51c5e5e | 2020-06-22 22:43:37 +0000 | [diff] [blame] | 640 | } // namespace cuttlefish |