David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 17 | // Functionality for launching and managing shell subprocesses. |
| 18 | // |
| 19 | // There are two types of subprocesses, PTY or raw. PTY is typically used for |
| 20 | // an interactive session, raw for non-interactive. There are also two methods |
| 21 | // of communication with the subprocess, passing raw data or using a simple |
| 22 | // protocol to wrap packets. The protocol allows separating stdout/stderr and |
| 23 | // passing the exit code back, but is not backwards compatible. |
| 24 | // ----------------+-------------------------------------- |
| 25 | // Type Protocol | Exit code? Separate stdout/stderr? |
| 26 | // ----------------+-------------------------------------- |
| 27 | // PTY No | No No |
| 28 | // Raw No | No No |
| 29 | // PTY Yes | Yes No |
| 30 | // Raw Yes | Yes Yes |
| 31 | // ----------------+-------------------------------------- |
| 32 | // |
| 33 | // Non-protocol subprocesses work by passing subprocess stdin/out/err through |
| 34 | // a single pipe which is registered with a local socket in adbd. The local |
| 35 | // socket uses the fdevent loop to pass raw data between this pipe and the |
| 36 | // transport, which then passes data back to the adb client. Cleanup is done by |
| 37 | // waiting in a separate thread for the subprocesses to exit and then signaling |
| 38 | // a separate fdevent to close out the local socket from the main loop. |
| 39 | // |
| 40 | // ------------------+-------------------------+------------------------------ |
| 41 | // Subprocess | adbd subprocess thread | adbd main fdevent loop |
| 42 | // ------------------+-------------------------+------------------------------ |
| 43 | // | | |
| 44 | // stdin/out/err <-----------------------------> LocalSocket |
| 45 | // | | | |
| 46 | // | | Block on exit | |
| 47 | // | | * | |
| 48 | // v | * | |
| 49 | // Exit ---> Unblock | |
| 50 | // | | | |
| 51 | // | v | |
| 52 | // | Notify shell exit FD ---> Close LocalSocket |
| 53 | // ------------------+-------------------------+------------------------------ |
| 54 | // |
| 55 | // The protocol requires the thread to intercept stdin/out/err in order to |
| 56 | // wrap/unwrap data with shell protocol packets. |
| 57 | // |
| 58 | // ------------------+-------------------------+------------------------------ |
| 59 | // Subprocess | adbd subprocess thread | adbd main fdevent loop |
| 60 | // ------------------+-------------------------+------------------------------ |
| 61 | // | | |
| 62 | // stdin/out <---> Protocol <---> LocalSocket |
| 63 | // stderr ---> Protocol ---> LocalSocket |
| 64 | // | | | |
| 65 | // v | | |
| 66 | // Exit ---> Exit code protocol ---> LocalSocket |
| 67 | // | | | |
| 68 | // | v | |
| 69 | // | Notify shell exit FD ---> Close LocalSocket |
| 70 | // ------------------+-------------------------+------------------------------ |
| 71 | // |
| 72 | // An alternate approach is to put the protocol wrapping/unwrapping in the main |
| 73 | // fdevent loop, which has the advantage of being able to re-use the existing |
| 74 | // select() code for handling data streams. However, implementation turned out |
| 75 | // to be more complex due to partial reads and non-blocking I/O so this model |
| 76 | // was chosen instead. |
| 77 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame^] | 78 | #define TRACE_TAG SHELL |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 79 | |
| 80 | #include "shell_service.h" |
| 81 | |
| 82 | #if !ADB_HOST |
| 83 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 84 | #include <errno.h> |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 85 | #include <pty.h> |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 86 | #include <sys/select.h> |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 87 | #include <termios.h> |
| 88 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 89 | #include <memory> |
| 90 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 91 | #include <base/logging.h> |
| 92 | #include <base/stringprintf.h> |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 93 | #include <paths.h> |
| 94 | |
| 95 | #include "adb.h" |
| 96 | #include "adb_io.h" |
| 97 | #include "adb_trace.h" |
| 98 | #include "sysdeps.h" |
| 99 | |
| 100 | namespace { |
| 101 | |
| 102 | void init_subproc_child() |
| 103 | { |
| 104 | setsid(); |
| 105 | |
| 106 | // Set OOM score adjustment to prevent killing |
| 107 | int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC); |
| 108 | if (fd >= 0) { |
| 109 | adb_write(fd, "0", 1); |
| 110 | adb_close(fd); |
| 111 | } else { |
| 112 | D("adb: unable to update oom_score_adj"); |
| 113 | } |
| 114 | } |
| 115 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 116 | // Reads from |fd| until close or failure. |
| 117 | std::string ReadAll(int fd) { |
| 118 | char buffer[512]; |
| 119 | std::string received; |
| 120 | |
| 121 | while (1) { |
| 122 | int bytes = adb_read(fd, buffer, sizeof(buffer)); |
| 123 | if (bytes <= 0) { |
| 124 | break; |
| 125 | } |
| 126 | received.append(buffer, bytes); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 127 | } |
| 128 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 129 | return received; |
| 130 | } |
| 131 | |
| 132 | // Helper to automatically close an FD when it goes out of scope. |
| 133 | class ScopedFd { |
| 134 | public: |
| 135 | ScopedFd() {} |
| 136 | ~ScopedFd() { Reset(); } |
| 137 | |
| 138 | void Reset(int fd=-1) { |
| 139 | if (fd != fd_) { |
| 140 | if (valid()) { |
| 141 | adb_close(fd_); |
| 142 | } |
| 143 | fd_ = fd; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | int Release() { |
| 148 | int temp = fd_; |
| 149 | fd_ = -1; |
| 150 | return temp; |
| 151 | } |
| 152 | |
| 153 | bool valid() const { return fd_ >= 0; } |
| 154 | |
| 155 | int fd() const { return fd_; } |
| 156 | |
| 157 | private: |
| 158 | int fd_ = -1; |
| 159 | |
| 160 | DISALLOW_COPY_AND_ASSIGN(ScopedFd); |
| 161 | }; |
| 162 | |
| 163 | // Creates a socketpair and saves the endpoints to |fd1| and |fd2|. |
| 164 | bool CreateSocketpair(ScopedFd* fd1, ScopedFd* fd2) { |
| 165 | int sockets[2]; |
| 166 | if (adb_socketpair(sockets) < 0) { |
| 167 | PLOG(ERROR) << "cannot create socket pair"; |
| 168 | return false; |
| 169 | } |
| 170 | fd1->Reset(sockets[0]); |
| 171 | fd2->Reset(sockets[1]); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | class Subprocess { |
| 176 | public: |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 177 | Subprocess(const std::string& command, SubprocessType type, |
| 178 | SubprocessProtocol protocol); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 179 | ~Subprocess(); |
| 180 | |
| 181 | const std::string& command() const { return command_; } |
| 182 | bool is_interactive() const { return command_.empty(); } |
| 183 | |
| 184 | int local_socket_fd() const { return local_socket_sfd_.fd(); } |
| 185 | |
| 186 | pid_t pid() const { return pid_; } |
| 187 | |
| 188 | // Sets up FDs, forks a subprocess, starts the subprocess manager thread, |
| 189 | // and exec's the child. Returns false on failure. |
| 190 | bool ForkAndExec(); |
| 191 | |
| 192 | private: |
| 193 | // Opens the file at |pts_name|. |
| 194 | int OpenPtyChildFd(const char* pts_name, ScopedFd* error_sfd); |
| 195 | |
| 196 | static void* ThreadHandler(void* userdata); |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 197 | void PassDataStreams(); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 198 | void WaitForExit(); |
| 199 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 200 | ScopedFd* SelectLoop(fd_set* master_read_set_ptr, |
| 201 | fd_set* master_write_set_ptr); |
| 202 | |
| 203 | // Input/output stream handlers. Success returns nullptr, failure returns |
| 204 | // a pointer to the failed FD. |
| 205 | ScopedFd* PassInput(); |
| 206 | ScopedFd* PassOutput(ScopedFd* sfd, ShellProtocol::Id id); |
| 207 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 208 | const std::string command_; |
| 209 | SubprocessType type_; |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 210 | SubprocessProtocol protocol_; |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 211 | pid_t pid_ = -1; |
| 212 | ScopedFd local_socket_sfd_; |
| 213 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 214 | // Shell protocol variables. |
| 215 | ScopedFd stdinout_sfd_, stderr_sfd_, protocol_sfd_; |
| 216 | std::unique_ptr<ShellProtocol> input_, output_; |
| 217 | size_t input_bytes_left_ = 0; |
| 218 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 219 | DISALLOW_COPY_AND_ASSIGN(Subprocess); |
| 220 | }; |
| 221 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 222 | Subprocess::Subprocess(const std::string& command, SubprocessType type, |
| 223 | SubprocessProtocol protocol) |
| 224 | : command_(command), type_(type), protocol_(protocol) { |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | Subprocess::~Subprocess() { |
| 228 | } |
| 229 | |
| 230 | bool Subprocess::ForkAndExec() { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 231 | ScopedFd child_stdinout_sfd, child_stderr_sfd; |
| 232 | ScopedFd parent_error_sfd, child_error_sfd; |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 233 | char pts_name[PATH_MAX]; |
| 234 | |
| 235 | // Create a socketpair for the fork() child to report any errors back to |
| 236 | // the parent. Since we use threads, logging directly from the child could |
| 237 | // create a race condition. |
| 238 | if (!CreateSocketpair(&parent_error_sfd, &child_error_sfd)) { |
| 239 | LOG(ERROR) << "failed to create pipe for subprocess error reporting"; |
| 240 | } |
| 241 | |
| 242 | if (type_ == SubprocessType::kPty) { |
| 243 | int fd; |
| 244 | pid_ = forkpty(&fd, pts_name, nullptr, nullptr); |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 245 | stdinout_sfd_.Reset(fd); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 246 | } else { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 247 | if (!CreateSocketpair(&stdinout_sfd_, &child_stdinout_sfd)) { |
| 248 | return false; |
| 249 | } |
| 250 | // Raw subprocess + shell protocol allows for splitting stderr. |
| 251 | if (protocol_ == SubprocessProtocol::kShell && |
| 252 | !CreateSocketpair(&stderr_sfd_, &child_stderr_sfd)) { |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | pid_ = fork(); |
| 256 | } |
| 257 | |
| 258 | if (pid_ == -1) { |
| 259 | PLOG(ERROR) << "fork failed"; |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | if (pid_ == 0) { |
| 264 | // Subprocess child. |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 265 | init_subproc_child(); |
| 266 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 267 | if (type_ == SubprocessType::kPty) { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 268 | child_stdinout_sfd.Reset(OpenPtyChildFd(pts_name, &child_error_sfd)); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 269 | } |
| 270 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 271 | dup2(child_stdinout_sfd.fd(), STDIN_FILENO); |
| 272 | dup2(child_stdinout_sfd.fd(), STDOUT_FILENO); |
| 273 | dup2(child_stderr_sfd.valid() ? child_stderr_sfd.fd() : child_stdinout_sfd.fd(), |
| 274 | STDERR_FILENO); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 275 | |
| 276 | // exec doesn't trigger destructors, close the FDs manually. |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 277 | stdinout_sfd_.Reset(); |
| 278 | stderr_sfd_.Reset(); |
| 279 | child_stdinout_sfd.Reset(); |
| 280 | child_stderr_sfd.Reset(); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 281 | parent_error_sfd.Reset(); |
| 282 | close_on_exec(child_error_sfd.fd()); |
| 283 | |
| 284 | if (is_interactive()) { |
| 285 | execl(_PATH_BSHELL, _PATH_BSHELL, "-", nullptr); |
| 286 | } else { |
| 287 | execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command_.c_str(), nullptr); |
| 288 | } |
| 289 | WriteFdExactly(child_error_sfd.fd(), "exec '" _PATH_BSHELL "' failed"); |
| 290 | child_error_sfd.Reset(); |
| 291 | exit(-1); |
| 292 | } |
| 293 | |
| 294 | // Subprocess parent. |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 295 | D("subprocess parent: stdin/stdout FD = %d, stderr FD = %d", |
| 296 | stdinout_sfd_.fd(), stderr_sfd_.fd()); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 297 | |
| 298 | // Wait to make sure the subprocess exec'd without error. |
| 299 | child_error_sfd.Reset(); |
| 300 | std::string error_message = ReadAll(parent_error_sfd.fd()); |
| 301 | if (!error_message.empty()) { |
| 302 | LOG(ERROR) << error_message; |
| 303 | return false; |
| 304 | } |
| 305 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 306 | if (protocol_ == SubprocessProtocol::kNone) { |
| 307 | // No protocol: all streams pass through the stdinout FD and hook |
| 308 | // directly into the local socket for raw data transfer. |
| 309 | local_socket_sfd_.Reset(stdinout_sfd_.Release()); |
| 310 | } else { |
| 311 | // Shell protocol: create another socketpair to intercept data. |
| 312 | if (!CreateSocketpair(&protocol_sfd_, &local_socket_sfd_)) { |
| 313 | return false; |
| 314 | } |
| 315 | D("protocol FD = %d", protocol_sfd_.fd()); |
| 316 | |
| 317 | input_.reset(new ShellProtocol(protocol_sfd_.fd())); |
| 318 | output_.reset(new ShellProtocol(protocol_sfd_.fd())); |
| 319 | if (!input_ || !output_) { |
| 320 | LOG(ERROR) << "failed to allocate shell protocol objects"; |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | // Don't let reads/writes to the subprocess block our thread. This isn't |
| 325 | // likely but could happen under unusual circumstances, such as if we |
| 326 | // write a ton of data to stdin but the subprocess never reads it and |
| 327 | // the pipe fills up. |
| 328 | for (int fd : {stdinout_sfd_.fd(), stderr_sfd_.fd()}) { |
| 329 | if (fd >= 0) { |
| 330 | int flags = fcntl(fd, F_GETFL, 0); |
| 331 | if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { |
| 332 | PLOG(ERROR) << "error making FD " << fd << " non-blocking"; |
| 333 | return false; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 338 | |
| 339 | if (!adb_thread_create(ThreadHandler, this)) { |
| 340 | PLOG(ERROR) << "failed to create subprocess thread"; |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | int Subprocess::OpenPtyChildFd(const char* pts_name, ScopedFd* error_sfd) { |
| 348 | int child_fd = adb_open(pts_name, O_RDWR | O_CLOEXEC); |
| 349 | if (child_fd == -1) { |
| 350 | // Don't use WriteFdFmt; since we're in the fork() child we don't want |
| 351 | // to allocate any heap memory to avoid race conditions. |
| 352 | const char* messages[] = {"child failed to open pseudo-term slave ", |
| 353 | pts_name, ": ", strerror(errno)}; |
| 354 | for (const char* message : messages) { |
| 355 | WriteFdExactly(error_sfd->fd(), message); |
| 356 | } |
| 357 | exit(-1); |
| 358 | } |
| 359 | |
| 360 | if (!is_interactive()) { |
| 361 | termios tattr; |
| 362 | if (tcgetattr(child_fd, &tattr) == -1) { |
| 363 | WriteFdExactly(error_sfd->fd(), "tcgetattr failed"); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 364 | exit(-1); |
| 365 | } |
| 366 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 367 | cfmakeraw(&tattr); |
| 368 | if (tcsetattr(child_fd, TCSADRAIN, &tattr) == -1) { |
| 369 | WriteFdExactly(error_sfd->fd(), "tcsetattr failed"); |
| 370 | exit(-1); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 371 | } |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 372 | } |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 373 | |
| 374 | return child_fd; |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 375 | } |
| 376 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 377 | void* Subprocess::ThreadHandler(void* userdata) { |
| 378 | Subprocess* subprocess = reinterpret_cast<Subprocess*>(userdata); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 379 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 380 | adb_thread_setname(android::base::StringPrintf( |
| 381 | "shell srvc %d", subprocess->local_socket_fd())); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 382 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 383 | subprocess->PassDataStreams(); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 384 | subprocess->WaitForExit(); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 385 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 386 | D("deleting Subprocess"); |
| 387 | delete subprocess; |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 388 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 389 | return nullptr; |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 390 | } |
| 391 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 392 | void Subprocess::PassDataStreams() { |
| 393 | if (!protocol_sfd_.valid()) { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | // Start by trying to read from the protocol FD, stdout, and stderr. |
| 398 | fd_set master_read_set, master_write_set; |
| 399 | FD_ZERO(&master_read_set); |
| 400 | FD_ZERO(&master_write_set); |
| 401 | for (ScopedFd* sfd : {&protocol_sfd_, &stdinout_sfd_, &stderr_sfd_}) { |
| 402 | if (sfd->valid()) { |
| 403 | FD_SET(sfd->fd(), &master_read_set); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Pass data until the protocol FD or both the subprocess pipes die, at |
| 408 | // which point we can't pass any more data. |
| 409 | while (protocol_sfd_.valid() && |
| 410 | (stdinout_sfd_.valid() || stderr_sfd_.valid())) { |
| 411 | ScopedFd* dead_sfd = SelectLoop(&master_read_set, &master_write_set); |
| 412 | if (dead_sfd) { |
| 413 | D("closing FD %d", dead_sfd->fd()); |
| 414 | FD_CLR(dead_sfd->fd(), &master_read_set); |
| 415 | FD_CLR(dead_sfd->fd(), &master_write_set); |
David Pursell | 544e795 | 2015-09-14 15:36:26 -0700 | [diff] [blame] | 416 | if (dead_sfd == &protocol_sfd_) { |
| 417 | // Using SIGHUP is a decent general way to indicate that the |
| 418 | // controlling process is going away. If specific signals are |
| 419 | // needed (e.g. SIGINT), pass those through the shell protocol |
| 420 | // and only fall back on this for unexpected closures. |
| 421 | D("protocol FD died, sending SIGHUP to pid %d", pid_); |
| 422 | kill(pid_, SIGHUP); |
| 423 | } |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 424 | dead_sfd->Reset(); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | namespace { |
| 430 | |
| 431 | inline bool ValidAndInSet(const ScopedFd& sfd, fd_set* set) { |
| 432 | return sfd.valid() && FD_ISSET(sfd.fd(), set); |
| 433 | } |
| 434 | |
| 435 | } // namespace |
| 436 | |
| 437 | ScopedFd* Subprocess::SelectLoop(fd_set* master_read_set_ptr, |
| 438 | fd_set* master_write_set_ptr) { |
| 439 | fd_set read_set, write_set; |
| 440 | int select_n = std::max(std::max(protocol_sfd_.fd(), stdinout_sfd_.fd()), |
| 441 | stderr_sfd_.fd()) + 1; |
| 442 | ScopedFd* dead_sfd = nullptr; |
| 443 | |
| 444 | // Keep calling select() and passing data until an FD closes/errors. |
| 445 | while (!dead_sfd) { |
| 446 | memcpy(&read_set, master_read_set_ptr, sizeof(read_set)); |
| 447 | memcpy(&write_set, master_write_set_ptr, sizeof(write_set)); |
| 448 | if (select(select_n, &read_set, &write_set, nullptr, nullptr) < 0) { |
| 449 | if (errno == EINTR) { |
| 450 | continue; |
| 451 | } else { |
| 452 | PLOG(ERROR) << "select failed, closing subprocess pipes"; |
| 453 | stdinout_sfd_.Reset(); |
| 454 | stderr_sfd_.Reset(); |
| 455 | return nullptr; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Read stdout, write to protocol FD. |
| 460 | if (ValidAndInSet(stdinout_sfd_, &read_set)) { |
| 461 | dead_sfd = PassOutput(&stdinout_sfd_, ShellProtocol::kIdStdout); |
| 462 | } |
| 463 | |
| 464 | // Read stderr, write to protocol FD. |
| 465 | if (!dead_sfd && ValidAndInSet(stderr_sfd_, &read_set)) { |
| 466 | dead_sfd = PassOutput(&stderr_sfd_, ShellProtocol::kIdStderr); |
| 467 | } |
| 468 | |
| 469 | // Read protocol FD, write to stdin. |
| 470 | if (!dead_sfd && ValidAndInSet(protocol_sfd_, &read_set)) { |
| 471 | dead_sfd = PassInput(); |
| 472 | // If we didn't finish writing, block on stdin write. |
| 473 | if (input_bytes_left_) { |
| 474 | FD_CLR(protocol_sfd_.fd(), master_read_set_ptr); |
| 475 | FD_SET(stdinout_sfd_.fd(), master_write_set_ptr); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Continue writing to stdin; only happens if a previous write blocked. |
| 480 | if (!dead_sfd && ValidAndInSet(stdinout_sfd_, &write_set)) { |
| 481 | dead_sfd = PassInput(); |
| 482 | // If we finished writing, go back to blocking on protocol read. |
| 483 | if (!input_bytes_left_) { |
| 484 | FD_SET(protocol_sfd_.fd(), master_read_set_ptr); |
| 485 | FD_CLR(stdinout_sfd_.fd(), master_write_set_ptr); |
| 486 | } |
| 487 | } |
| 488 | } // while (!dead_sfd) |
| 489 | |
| 490 | return dead_sfd; |
| 491 | } |
| 492 | |
| 493 | ScopedFd* Subprocess::PassInput() { |
| 494 | // Only read a new packet if we've finished writing the last one. |
| 495 | if (!input_bytes_left_) { |
| 496 | if (!input_->Read()) { |
| 497 | // Read() uses ReadFdExactly() which sets errno to 0 on EOF. |
| 498 | if (errno != 0) { |
| 499 | PLOG(ERROR) << "error reading protocol FD " |
| 500 | << protocol_sfd_.fd(); |
| 501 | } |
| 502 | return &protocol_sfd_; |
| 503 | } |
| 504 | |
| 505 | // We only care about stdin packets. |
| 506 | if (stdinout_sfd_.valid() && input_->id() == ShellProtocol::kIdStdin) { |
| 507 | input_bytes_left_ = input_->data_length(); |
| 508 | } else { |
| 509 | input_bytes_left_ = 0; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (input_bytes_left_ > 0) { |
| 514 | int index = input_->data_length() - input_bytes_left_; |
| 515 | int bytes = adb_write(stdinout_sfd_.fd(), input_->data() + index, |
| 516 | input_bytes_left_); |
| 517 | if (bytes == 0 || (bytes < 0 && errno != EAGAIN)) { |
| 518 | if (bytes < 0) { |
| 519 | PLOG(ERROR) << "error reading stdin FD " << stdinout_sfd_.fd(); |
| 520 | } |
| 521 | // stdin is done, mark this packet as finished and we'll just start |
| 522 | // dumping any further data received from the protocol FD. |
| 523 | input_bytes_left_ = 0; |
| 524 | return &stdinout_sfd_; |
| 525 | } else if (bytes > 0) { |
| 526 | input_bytes_left_ -= bytes; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return nullptr; |
| 531 | } |
| 532 | |
| 533 | ScopedFd* Subprocess::PassOutput(ScopedFd* sfd, ShellProtocol::Id id) { |
| 534 | int bytes = adb_read(sfd->fd(), output_->data(), output_->data_capacity()); |
| 535 | if (bytes == 0 || (bytes < 0 && errno != EAGAIN)) { |
| 536 | if (bytes < 0) { |
| 537 | PLOG(ERROR) << "error reading output FD " << sfd->fd(); |
| 538 | } |
| 539 | return sfd; |
| 540 | } |
| 541 | |
| 542 | if (bytes > 0 && !output_->Write(id, bytes)) { |
| 543 | if (errno != 0) { |
| 544 | PLOG(ERROR) << "error reading protocol FD " << protocol_sfd_.fd(); |
| 545 | } |
| 546 | return &protocol_sfd_; |
| 547 | } |
| 548 | |
| 549 | return nullptr; |
| 550 | } |
| 551 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 552 | void Subprocess::WaitForExit() { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 553 | int exit_code = 1; |
| 554 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 555 | D("waiting for pid %d", pid_); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 556 | while (true) { |
| 557 | int status; |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 558 | if (pid_ == waitpid(pid_, &status, 0)) { |
| 559 | D("post waitpid (pid=%d) status=%04x", pid_, status); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 560 | if (WIFSIGNALED(status)) { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 561 | exit_code = 0x80 | WTERMSIG(status); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 562 | D("subprocess killed by signal %d", WTERMSIG(status)); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 563 | break; |
| 564 | } else if (!WIFEXITED(status)) { |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 565 | D("subprocess didn't exit"); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 566 | break; |
| 567 | } else if (WEXITSTATUS(status) >= 0) { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 568 | exit_code = WEXITSTATUS(status); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 569 | D("subprocess exit code = %d", WEXITSTATUS(status)); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 570 | break; |
| 571 | } |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 572 | } |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 573 | } |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 574 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 575 | // If we have an open protocol FD send an exit packet. |
| 576 | if (protocol_sfd_.valid()) { |
| 577 | output_->data()[0] = exit_code; |
| 578 | if (output_->Write(ShellProtocol::kIdExit, 1)) { |
| 579 | D("wrote the exit code packet: %d", exit_code); |
| 580 | } else { |
| 581 | PLOG(ERROR) << "failed to write the exit code packet"; |
| 582 | } |
| 583 | protocol_sfd_.Reset(); |
| 584 | } |
| 585 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 586 | // Pass the local socket FD to the shell cleanup fdevent. |
| 587 | if (SHELL_EXIT_NOTIFY_FD >= 0) { |
| 588 | int fd = local_socket_sfd_.fd(); |
| 589 | if (WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd))) { |
| 590 | D("passed fd %d to SHELL_EXIT_NOTIFY_FD (%d) for pid %d", |
| 591 | fd, SHELL_EXIT_NOTIFY_FD, pid_); |
| 592 | // The shell exit fdevent now owns the FD and will close it once |
| 593 | // the last bit of data flushes through. |
| 594 | local_socket_sfd_.Release(); |
| 595 | } else { |
| 596 | PLOG(ERROR) << "failed to write fd " << fd |
| 597 | << " to SHELL_EXIT_NOTIFY_FD (" << SHELL_EXIT_NOTIFY_FD |
| 598 | << ") for pid " << pid_; |
| 599 | } |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | } // namespace |
| 604 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 605 | int StartSubprocess(const char *name, SubprocessType type, |
| 606 | SubprocessProtocol protocol) { |
| 607 | D("starting %s subprocess (protocol=%s): '%s'", |
| 608 | type == SubprocessType::kRaw ? "raw" : "PTY", |
| 609 | protocol == SubprocessProtocol::kNone ? "none" : "shell", name); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 610 | |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 611 | Subprocess* subprocess = new Subprocess(name, type, protocol); |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 612 | if (!subprocess) { |
| 613 | LOG(ERROR) << "failed to allocate new subprocess"; |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 614 | return -1; |
| 615 | } |
| 616 | |
David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 617 | if (!subprocess->ForkAndExec()) { |
| 618 | LOG(ERROR) << "failed to start subprocess"; |
| 619 | delete subprocess; |
| 620 | return -1; |
| 621 | } |
| 622 | |
| 623 | D("subprocess creation successful: local_socket_fd=%d, pid=%d", |
| 624 | subprocess->local_socket_fd(), subprocess->pid()); |
| 625 | return subprocess->local_socket_fd(); |
David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | #endif // !ADB_HOST |