| 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 |  | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 80 | #include "sysdeps.h" | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 81 |  | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 82 | #include "shell_service.h" | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 83 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 84 | #include <errno.h> | 
| Mark Salyzyn | 6debf98 | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 85 | #include <paths.h> | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 86 | #include <pty.h> | 
| Elliott Hughes | fbe4332 | 2015-11-02 13:29:19 -0800 | [diff] [blame] | 87 | #include <pwd.h> | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 88 | #include <sys/select.h> | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 89 | #include <termios.h> | 
|  | 90 |  | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 91 | #include <memory> | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 92 | #include <string> | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 93 | #include <thread> | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 94 | #include <unordered_map> | 
|  | 95 | #include <vector> | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 96 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 97 | #include <android-base/logging.h> | 
| Elliott Hughes | ba4d9fd | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 98 | #include <android-base/properties.h> | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 99 | #include <android-base/stringprintf.h> | 
| Mark Salyzyn | 6debf98 | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 100 | #include <private/android_logger.h> | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | #include "adb.h" | 
|  | 103 | #include "adb_io.h" | 
|  | 104 | #include "adb_trace.h" | 
| Josh Gao | 924d35a | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 105 | #include "adb_unique_fd.h" | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 106 | #include "adb_utils.h" | 
| Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 107 | #include "security_log_tags.h" | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | namespace { | 
|  | 110 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 111 | // Reads from |fd| until close or failure. | 
|  | 112 | std::string ReadAll(int fd) { | 
|  | 113 | char buffer[512]; | 
|  | 114 | std::string received; | 
|  | 115 |  | 
|  | 116 | while (1) { | 
|  | 117 | int bytes = adb_read(fd, buffer, sizeof(buffer)); | 
|  | 118 | if (bytes <= 0) { | 
|  | 119 | break; | 
|  | 120 | } | 
|  | 121 | received.append(buffer, bytes); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 124 | return received; | 
|  | 125 | } | 
|  | 126 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 127 | // Creates a socketpair and saves the endpoints to |fd1| and |fd2|. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 128 | bool CreateSocketpair(unique_fd* fd1, unique_fd* fd2) { | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 129 | int sockets[2]; | 
|  | 130 | if (adb_socketpair(sockets) < 0) { | 
|  | 131 | PLOG(ERROR) << "cannot create socket pair"; | 
|  | 132 | return false; | 
|  | 133 | } | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 134 | fd1->reset(sockets[0]); | 
|  | 135 | fd2->reset(sockets[1]); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 136 | return true; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | class Subprocess { | 
|  | 140 | public: | 
| Elliott Hughes | 18ddf5c | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 141 | Subprocess(const std::string& command, const char* terminal_type, | 
|  | 142 | SubprocessType type, SubprocessProtocol protocol); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 143 | ~Subprocess(); | 
|  | 144 |  | 
|  | 145 | const std::string& command() const { return command_; } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 146 |  | 
| Josh Gao | cd5d737 | 2016-06-22 15:57:12 -0700 | [diff] [blame] | 147 | int ReleaseLocalSocket() { return local_socket_sfd_.release(); } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 148 |  | 
|  | 149 | pid_t pid() const { return pid_; } | 
|  | 150 |  | 
|  | 151 | // Sets up FDs, forks a subprocess, starts the subprocess manager thread, | 
| Josh Gao | 344778d | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 152 | // and exec's the child. Returns false and sets error on failure. | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 153 | bool ForkAndExec(std::string* _Nonnull error); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 154 |  | 
| Josh Gao | 344778d | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 155 | // Start the subprocess manager thread. Consumes the subprocess, regardless of success. | 
|  | 156 | // Returns false and sets error on failure. | 
|  | 157 | static bool StartThread(std::unique_ptr<Subprocess> subprocess, | 
|  | 158 | std::string* _Nonnull error); | 
|  | 159 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 160 | private: | 
|  | 161 | // Opens the file at |pts_name|. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 162 | int OpenPtyChildFd(const char* pts_name, unique_fd* error_sfd); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 163 |  | 
| Josh Gao | b5fea14 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 164 | static void ThreadHandler(void* userdata); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 165 | void PassDataStreams(); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 166 | void WaitForExit(); | 
|  | 167 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 168 | unique_fd* SelectLoop(fd_set* master_read_set_ptr, | 
|  | 169 | fd_set* master_write_set_ptr); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 170 |  | 
|  | 171 | // Input/output stream handlers. Success returns nullptr, failure returns | 
|  | 172 | // a pointer to the failed FD. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 173 | unique_fd* PassInput(); | 
|  | 174 | unique_fd* PassOutput(unique_fd* sfd, ShellProtocol::Id id); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 175 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 176 | const std::string command_; | 
| Elliott Hughes | 18ddf5c | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 177 | const std::string terminal_type_; | 
| David Pursell | 57dd5ae | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 178 | bool make_pty_raw_ = false; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 179 | SubprocessType type_; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 180 | SubprocessProtocol protocol_; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 181 | pid_t pid_ = -1; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 182 | unique_fd local_socket_sfd_; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 183 |  | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 184 | // Shell protocol variables. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 185 | unique_fd stdinout_sfd_, stderr_sfd_, protocol_sfd_; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 186 | std::unique_ptr<ShellProtocol> input_, output_; | 
|  | 187 | size_t input_bytes_left_ = 0; | 
|  | 188 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 189 | DISALLOW_COPY_AND_ASSIGN(Subprocess); | 
|  | 190 | }; | 
|  | 191 |  | 
| Elliott Hughes | 18ddf5c | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 192 | Subprocess::Subprocess(const std::string& command, const char* terminal_type, | 
|  | 193 | SubprocessType type, SubprocessProtocol protocol) | 
|  | 194 | : command_(command), | 
|  | 195 | terminal_type_(terminal_type ? terminal_type : ""), | 
|  | 196 | type_(type), | 
|  | 197 | protocol_(protocol) { | 
| David Pursell | 57dd5ae | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 198 | // If we aren't using the shell protocol we must allocate a PTY to properly close the | 
|  | 199 | // subprocess. PTYs automatically send SIGHUP to the slave-side process when the master side | 
|  | 200 | // of the PTY closes, which we rely on. If we use a raw pipe, processes that don't read/write, | 
|  | 201 | // e.g. screenrecord, will never notice the broken pipe and terminate. | 
|  | 202 | // The shell protocol doesn't require a PTY because it's always monitoring the local socket FD | 
|  | 203 | // with select() and will send SIGHUP manually to the child process. | 
|  | 204 | if (protocol_ == SubprocessProtocol::kNone && type_ == SubprocessType::kRaw) { | 
|  | 205 | // Disable PTY input/output processing since the client is expecting raw data. | 
|  | 206 | D("Can't create raw subprocess without shell protocol, using PTY in raw mode instead"); | 
|  | 207 | type_ = SubprocessType::kPty; | 
|  | 208 | make_pty_raw_ = true; | 
|  | 209 | } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
|  | 212 | Subprocess::~Subprocess() { | 
| Josh Gao | c65fae9 | 2016-01-19 16:21:17 -0800 | [diff] [blame] | 213 | WaitForExit(); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 214 | } | 
|  | 215 |  | 
| Elliott Hughes | ba4d9fd | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 216 | static std::string GetHostName() { | 
|  | 217 | char buf[HOST_NAME_MAX]; | 
|  | 218 | if (gethostname(buf, sizeof(buf)) != -1 && strcmp(buf, "localhost") != 0) return buf; | 
|  | 219 |  | 
|  | 220 | return android::base::GetProperty("ro.product.device", "android"); | 
|  | 221 | } | 
|  | 222 |  | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 223 | bool Subprocess::ForkAndExec(std::string* error) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 224 | unique_fd child_stdinout_sfd, child_stderr_sfd; | 
|  | 225 | unique_fd parent_error_sfd, child_error_sfd; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 226 | char pts_name[PATH_MAX]; | 
|  | 227 |  | 
| Rubin Xu | d61a25c | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 228 | if (command_.empty()) { | 
|  | 229 | __android_log_security_bswrite(SEC_TAG_ADB_SHELL_INTERACTIVE, ""); | 
|  | 230 | } else { | 
|  | 231 | __android_log_security_bswrite(SEC_TAG_ADB_SHELL_CMD, command_.c_str()); | 
|  | 232 | } | 
|  | 233 |  | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 234 | // Create a socketpair for the fork() child to report any errors back to the parent. Since we | 
|  | 235 | // use threads, logging directly from the child might deadlock due to locks held in another | 
|  | 236 | // thread during the fork. | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 237 | if (!CreateSocketpair(&parent_error_sfd, &child_error_sfd)) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 238 | *error = android::base::StringPrintf( | 
|  | 239 | "failed to create pipe for subprocess error reporting: %s", strerror(errno)); | 
|  | 240 | return false; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 243 | // Construct the environment for the child before we fork. | 
|  | 244 | passwd* pw = getpwuid(getuid()); | 
|  | 245 | std::unordered_map<std::string, std::string> env; | 
| Josh Gao | e03c988 | 2015-12-11 15:49:12 -0800 | [diff] [blame] | 246 | if (environ) { | 
|  | 247 | char** current = environ; | 
|  | 248 | while (char* env_cstr = *current++) { | 
|  | 249 | std::string env_string = env_cstr; | 
| Dan Austin | 80fd454 | 2016-03-28 14:37:01 -0700 | [diff] [blame] | 250 | char* delimiter = strchr(&env_string[0], '='); | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 251 |  | 
| Josh Gao | e03c988 | 2015-12-11 15:49:12 -0800 | [diff] [blame] | 252 | // Drop any values that don't contain '='. | 
|  | 253 | if (delimiter) { | 
|  | 254 | *delimiter++ = '\0'; | 
|  | 255 | env[env_string.c_str()] = delimiter; | 
|  | 256 | } | 
|  | 257 | } | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | if (pw != nullptr) { | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 261 | env["HOME"] = pw->pw_dir; | 
| Elliott Hughes | ba4d9fd | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 262 | env["HOSTNAME"] = GetHostName(); | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 263 | env["LOGNAME"] = pw->pw_name; | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 264 | env["SHELL"] = pw->pw_shell; | 
| Elliott Hughes | d42df42 | 2017-12-11 10:40:57 -0800 | [diff] [blame] | 265 | env["TMPDIR"] = "/data/local/tmp"; | 
| Elliott Hughes | ba4d9fd | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 266 | env["USER"] = pw->pw_name; | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
|  | 269 | if (!terminal_type_.empty()) { | 
|  | 270 | env["TERM"] = terminal_type_; | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | std::vector<std::string> joined_env; | 
|  | 274 | for (auto it : env) { | 
|  | 275 | const char* key = it.first.c_str(); | 
|  | 276 | const char* value = it.second.c_str(); | 
|  | 277 | joined_env.push_back(android::base::StringPrintf("%s=%s", key, value)); | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | std::vector<const char*> cenv; | 
|  | 281 | for (const std::string& str : joined_env) { | 
|  | 282 | cenv.push_back(str.c_str()); | 
|  | 283 | } | 
|  | 284 | cenv.push_back(nullptr); | 
|  | 285 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 286 | if (type_ == SubprocessType::kPty) { | 
|  | 287 | int fd; | 
|  | 288 | pid_ = forkpty(&fd, pts_name, nullptr, nullptr); | 
| Josh Gao | fcb063c | 2016-03-04 17:50:10 -0800 | [diff] [blame] | 289 | if (pid_ > 0) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 290 | stdinout_sfd_.reset(fd); | 
| Josh Gao | fcb063c | 2016-03-04 17:50:10 -0800 | [diff] [blame] | 291 | } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 292 | } else { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 293 | if (!CreateSocketpair(&stdinout_sfd_, &child_stdinout_sfd)) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 294 | *error = android::base::StringPrintf("failed to create socketpair for stdin/out: %s", | 
|  | 295 | strerror(errno)); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 296 | return false; | 
|  | 297 | } | 
|  | 298 | // Raw subprocess + shell protocol allows for splitting stderr. | 
|  | 299 | if (protocol_ == SubprocessProtocol::kShell && | 
|  | 300 | !CreateSocketpair(&stderr_sfd_, &child_stderr_sfd)) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 301 | *error = android::base::StringPrintf("failed to create socketpair for stderr: %s", | 
|  | 302 | strerror(errno)); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 303 | return false; | 
|  | 304 | } | 
|  | 305 | pid_ = fork(); | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | if (pid_ == -1) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 309 | *error = android::base::StringPrintf("fork failed: %s", strerror(errno)); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 310 | return false; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | if (pid_ == 0) { | 
|  | 314 | // Subprocess child. | 
| Elliott Hughes | 284f07b | 2016-11-17 10:32:16 -0800 | [diff] [blame] | 315 | setsid(); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 316 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 317 | if (type_ == SubprocessType::kPty) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 318 | child_stdinout_sfd.reset(OpenPtyChildFd(pts_name, &child_error_sfd)); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 319 | } | 
|  | 320 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 321 | dup2(child_stdinout_sfd, STDIN_FILENO); | 
|  | 322 | dup2(child_stdinout_sfd, STDOUT_FILENO); | 
|  | 323 | dup2(child_stderr_sfd != -1 ? child_stderr_sfd : child_stdinout_sfd, STDERR_FILENO); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 324 |  | 
|  | 325 | // exec doesn't trigger destructors, close the FDs manually. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 326 | stdinout_sfd_.reset(-1); | 
|  | 327 | stderr_sfd_.reset(-1); | 
|  | 328 | child_stdinout_sfd.reset(-1); | 
|  | 329 | child_stderr_sfd.reset(-1); | 
|  | 330 | parent_error_sfd.reset(-1); | 
|  | 331 | close_on_exec(child_error_sfd); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 332 |  | 
| Elliott Hughes | 9eb5518 | 2017-02-17 11:14:33 -0800 | [diff] [blame] | 333 | // adbd sets SIGPIPE to SIG_IGN to get EPIPE instead, and Linux propagates that to child | 
|  | 334 | // processes, so we need to manually reset back to SIG_DFL here (http://b/35209888). | 
|  | 335 | signal(SIGPIPE, SIG_DFL); | 
|  | 336 |  | 
| Josh Gao | b5028e4 | 2016-01-19 17:31:09 -0800 | [diff] [blame] | 337 | if (command_.empty()) { | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 338 | execle(_PATH_BSHELL, _PATH_BSHELL, "-", nullptr, cenv.data()); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 339 | } else { | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 340 | execle(_PATH_BSHELL, _PATH_BSHELL, "-c", command_.c_str(), nullptr, cenv.data()); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 341 | } | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 342 | WriteFdExactly(child_error_sfd, "exec '" _PATH_BSHELL "' failed: "); | 
|  | 343 | WriteFdExactly(child_error_sfd, strerror(errno)); | 
|  | 344 | child_error_sfd.reset(-1); | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 345 | _Exit(1); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
|  | 348 | // Subprocess parent. | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 349 | D("subprocess parent: stdin/stdout FD = %d, stderr FD = %d", | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 350 | stdinout_sfd_.get(), stderr_sfd_.get()); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 351 |  | 
|  | 352 | // Wait to make sure the subprocess exec'd without error. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 353 | child_error_sfd.reset(-1); | 
|  | 354 | std::string error_message = ReadAll(parent_error_sfd); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 355 | if (!error_message.empty()) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 356 | *error = error_message; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 357 | return false; | 
|  | 358 | } | 
|  | 359 |  | 
| Josh Gao | 9b3fd67 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 360 | D("subprocess parent: exec completed"); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 361 | if (protocol_ == SubprocessProtocol::kNone) { | 
|  | 362 | // No protocol: all streams pass through the stdinout FD and hook | 
|  | 363 | // directly into the local socket for raw data transfer. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 364 | local_socket_sfd_.reset(stdinout_sfd_.release()); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 365 | } else { | 
|  | 366 | // Shell protocol: create another socketpair to intercept data. | 
|  | 367 | if (!CreateSocketpair(&protocol_sfd_, &local_socket_sfd_)) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 368 | *error = android::base::StringPrintf( | 
|  | 369 | "failed to create socketpair to intercept data: %s", strerror(errno)); | 
|  | 370 | kill(pid_, SIGKILL); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 371 | return false; | 
|  | 372 | } | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 373 | D("protocol FD = %d", protocol_sfd_.get()); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 374 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 375 | input_.reset(new ShellProtocol(protocol_sfd_)); | 
|  | 376 | output_.reset(new ShellProtocol(protocol_sfd_)); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 377 | if (!input_ || !output_) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 378 | *error = "failed to allocate shell protocol objects"; | 
|  | 379 | kill(pid_, SIGKILL); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 380 | return false; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | // Don't let reads/writes to the subprocess block our thread. This isn't | 
|  | 384 | // likely but could happen under unusual circumstances, such as if we | 
|  | 385 | // write a ton of data to stdin but the subprocess never reads it and | 
|  | 386 | // the pipe fills up. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 387 | for (int fd : {stdinout_sfd_.get(), stderr_sfd_.get()}) { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 388 | if (fd >= 0) { | 
| Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 389 | if (!set_file_block_mode(fd, false)) { | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 390 | *error = android::base::StringPrintf( | 
|  | 391 | "failed to set non-blocking mode for fd %d", fd); | 
|  | 392 | kill(pid_, SIGKILL); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 393 | return false; | 
|  | 394 | } | 
|  | 395 | } | 
|  | 396 | } | 
|  | 397 | } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 398 |  | 
| Josh Gao | 344778d | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 399 | D("subprocess parent: completed"); | 
|  | 400 | return true; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | bool Subprocess::StartThread(std::unique_ptr<Subprocess> subprocess, std::string* error) { | 
|  | 404 | Subprocess* raw = subprocess.release(); | 
| Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 405 | std::thread(ThreadHandler, raw).detach(); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 406 |  | 
|  | 407 | return true; | 
|  | 408 | } | 
|  | 409 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 410 | int Subprocess::OpenPtyChildFd(const char* pts_name, unique_fd* error_sfd) { | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 411 | int child_fd = adb_open(pts_name, O_RDWR | O_CLOEXEC); | 
|  | 412 | if (child_fd == -1) { | 
|  | 413 | // Don't use WriteFdFmt; since we're in the fork() child we don't want | 
|  | 414 | // to allocate any heap memory to avoid race conditions. | 
|  | 415 | const char* messages[] = {"child failed to open pseudo-term slave ", | 
|  | 416 | pts_name, ": ", strerror(errno)}; | 
|  | 417 | for (const char* message : messages) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 418 | WriteFdExactly(*error_sfd, message); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 419 | } | 
| Josh Gao | 4abdeee | 2016-05-13 18:16:43 -0700 | [diff] [blame] | 420 | abort(); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 421 | } | 
|  | 422 |  | 
| David Pursell | 57dd5ae | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 423 | if (make_pty_raw_) { | 
|  | 424 | termios tattr; | 
|  | 425 | if (tcgetattr(child_fd, &tattr) == -1) { | 
|  | 426 | int saved_errno = errno; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 427 | WriteFdExactly(*error_sfd, "tcgetattr failed: "); | 
|  | 428 | WriteFdExactly(*error_sfd, strerror(saved_errno)); | 
| Josh Gao | 4abdeee | 2016-05-13 18:16:43 -0700 | [diff] [blame] | 429 | abort(); | 
| David Pursell | 57dd5ae | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
|  | 432 | cfmakeraw(&tattr); | 
|  | 433 | if (tcsetattr(child_fd, TCSADRAIN, &tattr) == -1) { | 
|  | 434 | int saved_errno = errno; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 435 | WriteFdExactly(*error_sfd, "tcsetattr failed: "); | 
|  | 436 | WriteFdExactly(*error_sfd, strerror(saved_errno)); | 
| Josh Gao | 4abdeee | 2016-05-13 18:16:43 -0700 | [diff] [blame] | 437 | abort(); | 
| David Pursell | 57dd5ae | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 438 | } | 
|  | 439 | } | 
|  | 440 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 441 | return child_fd; | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Josh Gao | b5fea14 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 444 | void Subprocess::ThreadHandler(void* userdata) { | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 445 | Subprocess* subprocess = reinterpret_cast<Subprocess*>(userdata); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 446 |  | 
| Josh Gao | bcadc77 | 2017-09-28 16:29:53 -0700 | [diff] [blame] | 447 | adb_thread_setname(android::base::StringPrintf("shell svc %d", subprocess->pid())); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 448 |  | 
| Josh Gao | 344778d | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 449 | D("passing data streams for PID %d", subprocess->pid()); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 450 | subprocess->PassDataStreams(); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 451 |  | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 452 | D("deleting Subprocess for PID %d", subprocess->pid()); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 453 | delete subprocess; | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 456 | void Subprocess::PassDataStreams() { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 457 | if (protocol_sfd_ == -1) { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 458 | return; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | // Start by trying to read from the protocol FD, stdout, and stderr. | 
|  | 462 | fd_set master_read_set, master_write_set; | 
|  | 463 | FD_ZERO(&master_read_set); | 
|  | 464 | FD_ZERO(&master_write_set); | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 465 | for (unique_fd* sfd : {&protocol_sfd_, &stdinout_sfd_, &stderr_sfd_}) { | 
|  | 466 | if (*sfd != -1) { | 
|  | 467 | FD_SET(*sfd, &master_read_set); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 468 | } | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | // Pass data until the protocol FD or both the subprocess pipes die, at | 
|  | 472 | // which point we can't pass any more data. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 473 | while (protocol_sfd_ != -1 && (stdinout_sfd_ != -1 || stderr_sfd_ != -1)) { | 
|  | 474 | unique_fd* dead_sfd = SelectLoop(&master_read_set, &master_write_set); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 475 | if (dead_sfd) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 476 | D("closing FD %d", dead_sfd->get()); | 
|  | 477 | FD_CLR(*dead_sfd, &master_read_set); | 
|  | 478 | FD_CLR(*dead_sfd, &master_write_set); | 
| David Pursell | 544e795 | 2015-09-14 15:36:26 -0700 | [diff] [blame] | 479 | if (dead_sfd == &protocol_sfd_) { | 
|  | 480 | // Using SIGHUP is a decent general way to indicate that the | 
|  | 481 | // controlling process is going away. If specific signals are | 
|  | 482 | // needed (e.g. SIGINT), pass those through the shell protocol | 
|  | 483 | // and only fall back on this for unexpected closures. | 
|  | 484 | D("protocol FD died, sending SIGHUP to pid %d", pid_); | 
|  | 485 | kill(pid_, SIGHUP); | 
| David Pursell | f2aa186 | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 486 |  | 
|  | 487 | // We also need to close the pipes connected to the child process | 
|  | 488 | // so that if it ignores SIGHUP and continues to write data it | 
|  | 489 | // won't fill up the pipe and block. | 
| Josh Gao | 5d1b1a8 | 2016-09-14 12:47:02 -0700 | [diff] [blame] | 490 | stdinout_sfd_.reset(); | 
|  | 491 | stderr_sfd_.reset(); | 
| David Pursell | 544e795 | 2015-09-14 15:36:26 -0700 | [diff] [blame] | 492 | } | 
| Josh Gao | 5d1b1a8 | 2016-09-14 12:47:02 -0700 | [diff] [blame] | 493 | dead_sfd->reset(); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 494 | } | 
|  | 495 | } | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | namespace { | 
|  | 499 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 500 | inline bool ValidAndInSet(const unique_fd& sfd, fd_set* set) { | 
|  | 501 | return sfd != -1 && FD_ISSET(sfd, set); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 502 | } | 
|  | 503 |  | 
|  | 504 | }   // namespace | 
|  | 505 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 506 | unique_fd* Subprocess::SelectLoop(fd_set* master_read_set_ptr, | 
|  | 507 | fd_set* master_write_set_ptr) { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 508 | fd_set read_set, write_set; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 509 | int select_n = std::max(std::max(protocol_sfd_, stdinout_sfd_), stderr_sfd_) + 1; | 
|  | 510 | unique_fd* dead_sfd = nullptr; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 511 |  | 
|  | 512 | // Keep calling select() and passing data until an FD closes/errors. | 
|  | 513 | while (!dead_sfd) { | 
|  | 514 | memcpy(&read_set, master_read_set_ptr, sizeof(read_set)); | 
|  | 515 | memcpy(&write_set, master_write_set_ptr, sizeof(write_set)); | 
|  | 516 | if (select(select_n, &read_set, &write_set, nullptr, nullptr) < 0) { | 
|  | 517 | if (errno == EINTR) { | 
|  | 518 | continue; | 
|  | 519 | } else { | 
|  | 520 | PLOG(ERROR) << "select failed, closing subprocess pipes"; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 521 | stdinout_sfd_.reset(-1); | 
|  | 522 | stderr_sfd_.reset(-1); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 523 | return nullptr; | 
|  | 524 | } | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | // Read stdout, write to protocol FD. | 
|  | 528 | if (ValidAndInSet(stdinout_sfd_, &read_set)) { | 
|  | 529 | dead_sfd = PassOutput(&stdinout_sfd_, ShellProtocol::kIdStdout); | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | // Read stderr, write to protocol FD. | 
|  | 533 | if (!dead_sfd && ValidAndInSet(stderr_sfd_, &read_set)) { | 
|  | 534 | dead_sfd = PassOutput(&stderr_sfd_, ShellProtocol::kIdStderr); | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | // Read protocol FD, write to stdin. | 
|  | 538 | if (!dead_sfd && ValidAndInSet(protocol_sfd_, &read_set)) { | 
|  | 539 | dead_sfd = PassInput(); | 
|  | 540 | // If we didn't finish writing, block on stdin write. | 
|  | 541 | if (input_bytes_left_) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 542 | FD_CLR(protocol_sfd_, master_read_set_ptr); | 
|  | 543 | FD_SET(stdinout_sfd_, master_write_set_ptr); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 544 | } | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | // Continue writing to stdin; only happens if a previous write blocked. | 
|  | 548 | if (!dead_sfd && ValidAndInSet(stdinout_sfd_, &write_set)) { | 
|  | 549 | dead_sfd = PassInput(); | 
|  | 550 | // If we finished writing, go back to blocking on protocol read. | 
|  | 551 | if (!input_bytes_left_) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 552 | FD_SET(protocol_sfd_, master_read_set_ptr); | 
|  | 553 | FD_CLR(stdinout_sfd_, master_write_set_ptr); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 554 | } | 
|  | 555 | } | 
|  | 556 | }  // while (!dead_sfd) | 
|  | 557 |  | 
|  | 558 | return dead_sfd; | 
|  | 559 | } | 
|  | 560 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 561 | unique_fd* Subprocess::PassInput() { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 562 | // Only read a new packet if we've finished writing the last one. | 
|  | 563 | if (!input_bytes_left_) { | 
|  | 564 | if (!input_->Read()) { | 
|  | 565 | // Read() uses ReadFdExactly() which sets errno to 0 on EOF. | 
|  | 566 | if (errno != 0) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 567 | PLOG(ERROR) << "error reading protocol FD " << protocol_sfd_; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 568 | } | 
|  | 569 | return &protocol_sfd_; | 
|  | 570 | } | 
|  | 571 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 572 | if (stdinout_sfd_ != -1) { | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 573 | switch (input_->id()) { | 
| Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 574 | case ShellProtocol::kIdWindowSizeChange: | 
|  | 575 | int rows, cols, x_pixels, y_pixels; | 
|  | 576 | if (sscanf(input_->data(), "%dx%d,%dx%d", | 
|  | 577 | &rows, &cols, &x_pixels, &y_pixels) == 4) { | 
|  | 578 | winsize ws; | 
|  | 579 | ws.ws_row = rows; | 
|  | 580 | ws.ws_col = cols; | 
|  | 581 | ws.ws_xpixel = x_pixels; | 
|  | 582 | ws.ws_ypixel = y_pixels; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 583 | ioctl(stdinout_sfd_, TIOCSWINSZ, &ws); | 
| Elliott Hughes | c15b17f | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 584 | } | 
|  | 585 | break; | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 586 | case ShellProtocol::kIdStdin: | 
|  | 587 | input_bytes_left_ = input_->data_length(); | 
|  | 588 | break; | 
|  | 589 | case ShellProtocol::kIdCloseStdin: | 
|  | 590 | if (type_ == SubprocessType::kRaw) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 591 | if (adb_shutdown(stdinout_sfd_, SHUT_WR) == 0) { | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 592 | return nullptr; | 
|  | 593 | } | 
|  | 594 | PLOG(ERROR) << "failed to shutdown writes to FD " | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 595 | << stdinout_sfd_; | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 596 | return &stdinout_sfd_; | 
|  | 597 | } else { | 
|  | 598 | // PTYs can't close just input, so rather than close the | 
|  | 599 | // FD and risk losing subprocess output, leave it open. | 
|  | 600 | // This only happens if the client starts a PTY shell | 
|  | 601 | // non-interactively which is rare and unsupported. | 
|  | 602 | // If necessary, the client can manually close the shell | 
|  | 603 | // with `exit` or by killing the adb client process. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 604 | D("can't close input for PTY FD %d", stdinout_sfd_.get()); | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 605 | } | 
|  | 606 | break; | 
|  | 607 | } | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 608 | } | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | if (input_bytes_left_ > 0) { | 
|  | 612 | int index = input_->data_length() - input_bytes_left_; | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 613 | int bytes = adb_write(stdinout_sfd_, input_->data() + index, input_bytes_left_); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 614 | if (bytes == 0 || (bytes < 0 && errno != EAGAIN)) { | 
|  | 615 | if (bytes < 0) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 616 | PLOG(ERROR) << "error reading stdin FD " << stdinout_sfd_; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 617 | } | 
|  | 618 | // stdin is done, mark this packet as finished and we'll just start | 
|  | 619 | // dumping any further data received from the protocol FD. | 
|  | 620 | input_bytes_left_ = 0; | 
|  | 621 | return &stdinout_sfd_; | 
|  | 622 | } else if (bytes > 0) { | 
|  | 623 | input_bytes_left_ -= bytes; | 
|  | 624 | } | 
|  | 625 | } | 
|  | 626 |  | 
|  | 627 | return nullptr; | 
|  | 628 | } | 
|  | 629 |  | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 630 | unique_fd* Subprocess::PassOutput(unique_fd* sfd, ShellProtocol::Id id) { | 
|  | 631 | int bytes = adb_read(*sfd, output_->data(), output_->data_capacity()); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 632 | if (bytes == 0 || (bytes < 0 && errno != EAGAIN)) { | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 633 | // read() returns EIO if a PTY closes; don't report this as an error, | 
|  | 634 | // it just means the subprocess completed. | 
|  | 635 | if (bytes < 0 && !(type_ == SubprocessType::kPty && errno == EIO)) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 636 | PLOG(ERROR) << "error reading output FD " << *sfd; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 637 | } | 
|  | 638 | return sfd; | 
|  | 639 | } | 
|  | 640 |  | 
|  | 641 | if (bytes > 0 && !output_->Write(id, bytes)) { | 
|  | 642 | if (errno != 0) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 643 | PLOG(ERROR) << "error reading protocol FD " << protocol_sfd_; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 644 | } | 
|  | 645 | return &protocol_sfd_; | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | return nullptr; | 
|  | 649 | } | 
|  | 650 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 651 | void Subprocess::WaitForExit() { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 652 | int exit_code = 1; | 
|  | 653 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 654 | D("waiting for pid %d", pid_); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 655 | while (true) { | 
|  | 656 | int status; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 657 | if (pid_ == waitpid(pid_, &status, 0)) { | 
|  | 658 | D("post waitpid (pid=%d) status=%04x", pid_, status); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 659 | if (WIFSIGNALED(status)) { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 660 | exit_code = 0x80 | WTERMSIG(status); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 661 | D("subprocess killed by signal %d", WTERMSIG(status)); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 662 | break; | 
|  | 663 | } else if (!WIFEXITED(status)) { | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 664 | D("subprocess didn't exit"); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 665 | break; | 
|  | 666 | } else if (WEXITSTATUS(status) >= 0) { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 667 | exit_code = WEXITSTATUS(status); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 668 | D("subprocess exit code = %d", WEXITSTATUS(status)); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 669 | break; | 
|  | 670 | } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 671 | } | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 672 | } | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 673 |  | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 674 | // If we have an open protocol FD send an exit packet. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 675 | if (protocol_sfd_ != -1) { | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 676 | output_->data()[0] = exit_code; | 
|  | 677 | if (output_->Write(ShellProtocol::kIdExit, 1)) { | 
|  | 678 | D("wrote the exit code packet: %d", exit_code); | 
|  | 679 | } else { | 
|  | 680 | PLOG(ERROR) << "failed to write the exit code packet"; | 
|  | 681 | } | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 682 | protocol_sfd_.reset(-1); | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 683 | } | 
|  | 684 |  | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 685 | // Pass the local socket FD to the shell cleanup fdevent. | 
|  | 686 | if (SHELL_EXIT_NOTIFY_FD >= 0) { | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 687 | int fd = local_socket_sfd_; | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 688 | if (WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd))) { | 
|  | 689 | D("passed fd %d to SHELL_EXIT_NOTIFY_FD (%d) for pid %d", | 
|  | 690 | fd, SHELL_EXIT_NOTIFY_FD, pid_); | 
|  | 691 | // The shell exit fdevent now owns the FD and will close it once | 
|  | 692 | // the last bit of data flushes through. | 
| Elliott Hughes | 2ce86e5 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 693 | static_cast<void>(local_socket_sfd_.release()); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 694 | } else { | 
|  | 695 | PLOG(ERROR) << "failed to write fd " << fd | 
|  | 696 | << " to SHELL_EXIT_NOTIFY_FD (" << SHELL_EXIT_NOTIFY_FD | 
|  | 697 | << ") for pid " << pid_; | 
|  | 698 | } | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 699 | } | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | }  // namespace | 
|  | 703 |  | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 704 | // Create a pipe containing the error. | 
|  | 705 | static int ReportError(SubprocessProtocol protocol, const std::string& message) { | 
|  | 706 | int pipefd[2]; | 
|  | 707 | if (pipe(pipefd) != 0) { | 
|  | 708 | LOG(ERROR) << "failed to create pipe to report error"; | 
|  | 709 | return -1; | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | std::string buf = android::base::StringPrintf("error: %s\n", message.c_str()); | 
|  | 713 | if (protocol == SubprocessProtocol::kShell) { | 
|  | 714 | ShellProtocol::Id id = ShellProtocol::kIdStderr; | 
|  | 715 | uint32_t length = buf.length(); | 
|  | 716 | WriteFdExactly(pipefd[1], &id, sizeof(id)); | 
|  | 717 | WriteFdExactly(pipefd[1], &length, sizeof(length)); | 
|  | 718 | } | 
|  | 719 |  | 
|  | 720 | WriteFdExactly(pipefd[1], buf.data(), buf.length()); | 
|  | 721 |  | 
|  | 722 | if (protocol == SubprocessProtocol::kShell) { | 
|  | 723 | ShellProtocol::Id id = ShellProtocol::kIdExit; | 
|  | 724 | uint32_t length = 1; | 
|  | 725 | char exit_code = 126; | 
|  | 726 | WriteFdExactly(pipefd[1], &id, sizeof(id)); | 
|  | 727 | WriteFdExactly(pipefd[1], &length, sizeof(length)); | 
|  | 728 | WriteFdExactly(pipefd[1], &exit_code, sizeof(exit_code)); | 
|  | 729 | } | 
|  | 730 |  | 
|  | 731 | adb_close(pipefd[1]); | 
|  | 732 | return pipefd[0]; | 
|  | 733 | } | 
|  | 734 |  | 
| Elliott Hughes | 18ddf5c | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 735 | int StartSubprocess(const char* name, const char* terminal_type, | 
|  | 736 | SubprocessType type, SubprocessProtocol protocol) { | 
|  | 737 | D("starting %s subprocess (protocol=%s, TERM=%s): '%s'", | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 738 | type == SubprocessType::kRaw ? "raw" : "PTY", | 
| Elliott Hughes | 18ddf5c | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 739 | protocol == SubprocessProtocol::kNone ? "none" : "shell", | 
|  | 740 | terminal_type, name); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 741 |  | 
| Josh Gao | 344778d | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 742 | auto subprocess = std::make_unique<Subprocess>(name, terminal_type, type, protocol); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 743 | if (!subprocess) { | 
|  | 744 | LOG(ERROR) << "failed to allocate new subprocess"; | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 745 | return ReportError(protocol, "failed to allocate new subprocess"); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 746 | } | 
|  | 747 |  | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 748 | std::string error; | 
|  | 749 | if (!subprocess->ForkAndExec(&error)) { | 
|  | 750 | LOG(ERROR) << "failed to start subprocess: " << error; | 
| Josh Gao | 4323507 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 751 | return ReportError(protocol, error); | 
| David Pursell | a932058 | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 752 | } | 
|  | 753 |  | 
| Josh Gao | e31a7a4 | 2016-06-23 11:21:11 -0700 | [diff] [blame] | 754 | unique_fd local_socket(subprocess->ReleaseLocalSocket()); | 
|  | 755 | D("subprocess creation successful: local_socket_fd=%d, pid=%d", local_socket.get(), | 
|  | 756 | subprocess->pid()); | 
| Josh Gao | 344778d | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 757 |  | 
|  | 758 | if (!Subprocess::StartThread(std::move(subprocess), &error)) { | 
|  | 759 | LOG(ERROR) << "failed to start subprocess management thread: " << error; | 
|  | 760 | return ReportError(protocol, error); | 
|  | 761 | } | 
|  | 762 |  | 
| Josh Gao | e31a7a4 | 2016-06-23 11:21:11 -0700 | [diff] [blame] | 763 | return local_socket.release(); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 764 | } |