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