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 | |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 25 | #include <map> |
| 26 | #include <set> |
| 27 | |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 28 | #include <glog/logging.h> |
Cody Schuffelen | 5133a31 | 2019-08-02 17:58:00 -0700 | [diff] [blame^] | 29 | |
| 30 | #include "common/libs/fs/shared_buf.h" |
| 31 | |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 32 | namespace { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 33 | |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 34 | // If a redirected-to file descriptor was already closed, it's possible that |
| 35 | // some inherited file descriptor duped to this file descriptor and the redirect |
| 36 | // would override that. This function makes sure that doesn't happen. |
| 37 | bool validate_redirects( |
| 38 | const std::map<cvd::Subprocess::StdIOChannel, int>& redirects, |
| 39 | const std::map<cvd::SharedFD, int>& inherited_fds) { |
| 40 | // Add the redirected IO channels to a set as integers. This allows converting |
| 41 | // the enum values into integers instead of the other way around. |
| 42 | std::set<int> int_redirects; |
| 43 | for (const auto& entry: redirects) { |
| 44 | int_redirects.insert(static_cast<int>(entry.first)); |
| 45 | } |
| 46 | for (const auto& entry: inherited_fds) { |
| 47 | auto dupped_fd = entry.second; |
| 48 | if (int_redirects.count(dupped_fd)) { |
| 49 | LOG(ERROR) << "Requested redirect of fd(" << dupped_fd |
| 50 | << ") conflicts with inherited FD."; |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | void do_redirects(const std::map<cvd::Subprocess::StdIOChannel, int>& redirects) { |
| 58 | for (const auto& entry: redirects) { |
| 59 | auto std_channel = static_cast<int>(entry.first); |
| 60 | auto fd = entry.second; |
| 61 | TEMP_FAILURE_RETRY(dup2(fd, std_channel)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | cvd::Subprocess subprocess_impl( |
| 66 | const char* const* command, |
| 67 | const char* const* envp, |
| 68 | const std::map<cvd::Subprocess::StdIOChannel, int>& redirects, |
| 69 | const std::map<cvd::SharedFD, int>& inherited_fds, |
| 70 | bool with_control_socket) { |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 71 | // The parent socket will get closed on the child on the call to exec, the |
| 72 | // child socket will be closed on the parent when this function returns and no |
| 73 | // references to the fd are left |
| 74 | cvd::SharedFD parent_socket, child_socket; |
| 75 | if (with_control_socket) { |
| 76 | if (!cvd::SharedFD::SocketPair(AF_LOCAL, SOCK_STREAM, 0, &parent_socket, |
| 77 | &child_socket)) { |
| 78 | LOG(ERROR) << "Unable to create control socket pair: " << strerror(errno); |
| 79 | return cvd::Subprocess(-1, {}); |
| 80 | } |
| 81 | // Remove FD_CLOEXEC from the child socket, ensure the parent has it |
| 82 | child_socket->Fcntl(F_SETFD, 0); |
| 83 | parent_socket->Fcntl(F_SETFD, FD_CLOEXEC); |
| 84 | } |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 85 | |
| 86 | if (!validate_redirects(redirects, inherited_fds)) { |
| 87 | return cvd::Subprocess(-1, {}); |
| 88 | } |
| 89 | |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 90 | pid_t pid = fork(); |
| 91 | if (!pid) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 92 | do_redirects(redirects); |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 93 | int rval; |
| 94 | // If envp is NULL, the current process's environment is used as the |
| 95 | // environment of the child process. To force an empty emvironment for |
| 96 | // the child process pass the address of a pointer to NULL |
| 97 | if (envp == NULL) { |
| 98 | rval = execv(command[0], const_cast<char* const*>(command)); |
| 99 | } else { |
| 100 | rval = execve(command[0], |
| 101 | const_cast<char* const*>(command), |
| 102 | const_cast<char* const*>(envp)); |
| 103 | } |
| 104 | // No need for an if: if exec worked it wouldn't have returned |
| 105 | LOG(ERROR) << "exec of " << command[0] << " failed (" << strerror(errno) |
| 106 | << ")"; |
| 107 | exit(rval); |
| 108 | } |
| 109 | if (pid == -1) { |
| 110 | LOG(ERROR) << "fork failed (" << strerror(errno) << ")"; |
| 111 | } |
Jorge E. Moreira | 415eea2 | 2018-07-02 18:06:26 -0700 | [diff] [blame] | 112 | LOG(INFO) << "Started (pid: " << pid << "): " << command[0]; |
| 113 | int i = 1; |
| 114 | while (command[i]) { |
| 115 | LOG(INFO) << command[i++]; |
| 116 | } |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 117 | return cvd::Subprocess(pid, parent_socket); |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 120 | std::vector<const char*> ToCharPointers( |
| 121 | const std::vector<std::string>& vect) { |
| 122 | std::vector<const char*> ret = {}; |
| 123 | for (const auto& str : vect) { |
| 124 | ret.push_back(str.c_str()); |
| 125 | } |
| 126 | ret.push_back(NULL); |
| 127 | return ret; |
| 128 | } |
| 129 | } // namespace |
| 130 | namespace cvd { |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 131 | |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 132 | Subprocess::Subprocess(Subprocess&& subprocess) |
| 133 | : pid_(subprocess.pid_), |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 134 | started_(subprocess.started_), |
| 135 | control_socket_(subprocess.control_socket_) { |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 136 | // Make sure the moved object no longer controls this subprocess |
| 137 | subprocess.pid_ = -1; |
| 138 | subprocess.started_ = false; |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 139 | subprocess.control_socket_ = SharedFD(); |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Subprocess& Subprocess::operator=(Subprocess&& other) { |
| 143 | pid_ = other.pid_; |
| 144 | started_ = other.started_; |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 145 | control_socket_ = other.control_socket_; |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 146 | |
| 147 | other.pid_ = -1; |
| 148 | other.started_ = false; |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 149 | other.control_socket_ = SharedFD(); |
Jorge E. Moreira | 8e9793e | 2018-11-05 21:57:26 -0800 | [diff] [blame] | 150 | return *this; |
| 151 | } |
| 152 | |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 153 | int Subprocess::Wait() { |
| 154 | if (pid_ < 0) { |
| 155 | LOG(ERROR) |
| 156 | << "Attempt to wait on invalid pid(has it been waited on already?): " |
| 157 | << pid_; |
| 158 | return -1; |
| 159 | } |
| 160 | int wstatus = 0; |
| 161 | auto pid = pid_; // Wait will set pid_ to -1 after waiting |
| 162 | auto wait_ret = Wait(&wstatus, 0); |
| 163 | if (wait_ret < 0) { |
| 164 | LOG(ERROR) << "Error on call to waitpid: " << strerror(errno); |
| 165 | return wait_ret; |
| 166 | } |
| 167 | int retval = 0; |
| 168 | if (WIFEXITED(wstatus)) { |
| 169 | retval = WEXITSTATUS(wstatus); |
| 170 | if (retval) { |
| 171 | LOG(ERROR) << "Subprocess " << pid |
| 172 | << " exited with error code: " << retval; |
| 173 | } |
| 174 | } else if (WIFSIGNALED(wstatus)) { |
| 175 | LOG(ERROR) << "Subprocess " << pid |
| 176 | << " was interrupted by a signal: " << WTERMSIG(wstatus); |
| 177 | retval = -1; |
| 178 | } |
| 179 | return retval; |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 180 | } |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 181 | pid_t Subprocess::Wait(int* wstatus, int options) { |
| 182 | if (pid_ < 0) { |
| 183 | LOG(ERROR) |
| 184 | << "Attempt to wait on invalid pid(has it been waited on already?): " |
| 185 | << pid_; |
| 186 | return -1; |
| 187 | } |
| 188 | auto retval = waitpid(pid_, wstatus, options); |
| 189 | // We don't want to wait twice for the same process |
| 190 | pid_ = -1; |
| 191 | return retval; |
| 192 | } |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 193 | |
Jorge E. Moreira | b945eb1 | 2019-05-06 17:20:38 -0700 | [diff] [blame] | 194 | Command::ParameterBuilder::~ParameterBuilder() { |
| 195 | Build(); |
| 196 | } |
| 197 | void Command::ParameterBuilder::Build() { |
| 198 | auto param = stream_.str(); |
| 199 | stream_ = std::stringstream(); |
| 200 | if (param.size()) { |
| 201 | cmd_->AddParameter(param); |
| 202 | } |
| 203 | } |
| 204 | |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 205 | Command::~Command() { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 206 | // Close all inherited file descriptors |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 207 | for(const auto& entry: inherited_fds_) { |
| 208 | close(entry.second); |
| 209 | } |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 210 | // Close all redirected file descriptors |
| 211 | for (const auto& entry: redirects_) { |
| 212 | close(entry.second); |
| 213 | } |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 214 | } |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 215 | |
| 216 | bool Command::BuildParameter(std::stringstream* stream, SharedFD shared_fd) { |
| 217 | int fd; |
| 218 | if (inherited_fds_.count(shared_fd)) { |
| 219 | fd = inherited_fds_[shared_fd]; |
| 220 | } else { |
| 221 | fd = shared_fd->UNMANAGED_Dup(); |
| 222 | if (fd < 0) { |
| 223 | return false; |
| 224 | } |
| 225 | inherited_fds_[shared_fd] = fd; |
| 226 | } |
| 227 | *stream << fd; |
| 228 | return true; |
| 229 | } |
| 230 | |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 231 | bool Command::RedirectStdIO(cvd::Subprocess::StdIOChannel channel, |
| 232 | cvd::SharedFD shared_fd){ |
| 233 | if (!shared_fd->IsOpen()) { |
| 234 | return false; |
| 235 | } |
| 236 | if (redirects_.count(channel)) { |
| 237 | LOG(ERROR) << "Attempted multiple redirections of fd: " |
| 238 | << static_cast<int>(channel); |
| 239 | return false; |
| 240 | } |
| 241 | auto dup_fd = shared_fd->UNMANAGED_Dup(); |
| 242 | if (dup_fd < 0) { |
| 243 | return false; |
| 244 | } |
| 245 | redirects_[channel] = dup_fd; |
| 246 | return true; |
| 247 | } |
| 248 | |
Jorge E. Moreira | 1a62e76 | 2018-11-05 22:05:57 -0800 | [diff] [blame] | 249 | Subprocess Command::Start(bool with_control_socket) const { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 250 | auto cmd = ToCharPointers(command_); |
| 251 | if (use_parent_env_) { |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 252 | return subprocess_impl(cmd.data(), nullptr, redirects_, inherited_fds_, |
| 253 | with_control_socket); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 254 | } else { |
| 255 | auto envp = ToCharPointers(env_); |
Jorge E. Moreira | a4dac8b | 2019-01-25 18:14:51 -0800 | [diff] [blame] | 256 | return subprocess_impl(cmd.data(), envp.data(), redirects_, inherited_fds_, |
| 257 | with_control_socket); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 261 | int execute(const std::vector<std::string>& command, |
| 262 | const std::vector<std::string>& env) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 263 | Command cmd(command[0]); |
| 264 | for (size_t i = 1; i < command.size(); ++i) { |
| 265 | cmd.AddParameter(command[i]); |
| 266 | } |
| 267 | cmd.SetEnvironment(env); |
| 268 | auto subprocess = cmd.Start(); |
| 269 | if (!subprocess.Started()) { |
| 270 | return -1; |
| 271 | } |
| 272 | return subprocess.Wait(); |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 273 | } |
| 274 | int execute(const std::vector<std::string>& command) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 275 | Command cmd(command[0]); |
| 276 | for (size_t i = 1; i < command.size(); ++i) { |
| 277 | cmd.AddParameter(command[i]); |
| 278 | } |
| 279 | auto subprocess = cmd.Start(); |
| 280 | if (!subprocess.Started()) { |
| 281 | return -1; |
| 282 | } |
| 283 | return subprocess.Wait(); |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 284 | } |
Cody Schuffelen | 5133a31 | 2019-08-02 17:58:00 -0700 | [diff] [blame^] | 285 | |
| 286 | int execute_capture_output(const std::vector<std::string>& command, |
| 287 | std::string* output) { |
| 288 | Command cmd(command[0]); |
| 289 | for (size_t i = 1; i < command.size(); ++i) { |
| 290 | cmd.AddParameter(command[i]); |
| 291 | } |
| 292 | cvd::SharedFD pipe_read, pipe_write; |
| 293 | cvd::SharedFD::Pipe(&pipe_read, &pipe_write); |
| 294 | cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, pipe_write); |
| 295 | cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdErr, pipe_write); |
| 296 | |
| 297 | auto subprocess = cmd.Start(); |
| 298 | if (!subprocess.Started()) { |
| 299 | return -1; |
| 300 | } |
| 301 | { |
| 302 | pipe_write->Close(); |
| 303 | // Force the destructor to run by moving it into a smaller scope. |
| 304 | // This is necessary to close the write end of the pipe. |
| 305 | cvd::Command forceDelete = std::move(cmd); |
| 306 | } |
| 307 | |
| 308 | int read = cvd::ReadAll(pipe_read, output); |
| 309 | if (read < 0) { |
| 310 | LOG(FATAL) << "Could not read from pipe in execute_capture_output"; |
| 311 | } |
| 312 | return subprocess.Wait(); |
| 313 | } |
| 314 | |
Jorge E. Moreira | 6a9d629 | 2018-06-11 11:52:57 -0700 | [diff] [blame] | 315 | } // namespace cvd |