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