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