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> |
Mark Salyzyn | 81a870e | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 85 | #include <paths.h> |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 86 | #include <pty.h> |
Elliott Hughes | 90676d9 | 2015-11-02 13:29:19 -0800 | [diff] [blame] | 87 | #include <pwd.h> |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 88 | #include <sys/select.h> |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 89 | #include <termios.h> |
| 90 | |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 91 | #include <memory> |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 92 | #include <string> |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 93 | #include <thread> |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 94 | #include <unordered_map> |
| 95 | #include <vector> |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 96 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 97 | #include <android-base/logging.h> |
Elliott Hughes | 299da1c | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 98 | #include <android-base/properties.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 99 | #include <android-base/stringprintf.h> |
Mark Salyzyn | 81a870e | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 100 | #include <private/android_logger.h> |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 101 | |
| 102 | #if defined(__ANDROID__) |
Jiyong Park | 19c39fa | 2018-05-29 16:41:30 +0900 | [diff] [blame] | 103 | #include <selinux/android.h> |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 104 | #endif |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 105 | |
| 106 | #include "adb.h" |
| 107 | #include "adb_io.h" |
| 108 | #include "adb_trace.h" |
Josh Gao | ea7457b | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 109 | #include "adb_unique_fd.h" |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 110 | #include "adb_utils.h" |
Rubin Xu | 29a64f9 | 2016-01-11 10:23:47 +0000 | [diff] [blame] | 111 | #include "security_log_tags.h" |
Josh Gao | 076b5ba | 2018-07-25 16:07:26 -0700 | [diff] [blame] | 112 | #include "shell_protocol.h" |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 113 | |
| 114 | namespace { |
| 115 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 116 | // Reads from |fd| until close or failure. |
| 117 | std::string ReadAll(int fd) { |
| 118 | char buffer[512]; |
| 119 | std::string received; |
| 120 | |
| 121 | while (1) { |
| 122 | int bytes = adb_read(fd, buffer, sizeof(buffer)); |
| 123 | if (bytes <= 0) { |
| 124 | break; |
| 125 | } |
| 126 | received.append(buffer, bytes); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 127 | } |
| 128 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 129 | return received; |
| 130 | } |
| 131 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 132 | // Creates a socketpair and saves the endpoints to |fd1| and |fd2|. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 133 | bool CreateSocketpair(unique_fd* fd1, unique_fd* fd2) { |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 134 | int sockets[2]; |
| 135 | if (adb_socketpair(sockets) < 0) { |
| 136 | PLOG(ERROR) << "cannot create socket pair"; |
| 137 | return false; |
| 138 | } |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 139 | fd1->reset(sockets[0]); |
| 140 | fd2->reset(sockets[1]); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
| 144 | class Subprocess { |
| 145 | public: |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 146 | Subprocess(std::string command, const char* terminal_type, SubprocessType type, |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 147 | SubprocessProtocol protocol, bool make_pty_raw); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 148 | ~Subprocess(); |
| 149 | |
| 150 | const std::string& command() const { return command_; } |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 151 | |
Josh Gao | a654514 | 2016-06-22 15:57:12 -0700 | [diff] [blame] | 152 | int ReleaseLocalSocket() { return local_socket_sfd_.release(); } |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 153 | |
| 154 | pid_t pid() const { return pid_; } |
| 155 | |
| 156 | // Sets up FDs, forks a subprocess, starts the subprocess manager thread, |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 157 | // and exec's the child. Returns false and sets error on failure. |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 158 | bool ForkAndExec(std::string* _Nonnull error); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 159 | |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 160 | // Sets up FDs, starts a thread executing command and the manager thread, |
| 161 | // Returns false and sets error on failure. |
| 162 | bool ExecInProcess(Command command, std::string* _Nonnull error); |
| 163 | |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 164 | // Start the subprocess manager thread. Consumes the subprocess, regardless of success. |
| 165 | // Returns false and sets error on failure. |
| 166 | static bool StartThread(std::unique_ptr<Subprocess> subprocess, |
| 167 | std::string* _Nonnull error); |
| 168 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 169 | private: |
| 170 | // Opens the file at |pts_name|. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 171 | int OpenPtyChildFd(const char* pts_name, unique_fd* error_sfd); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 172 | |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame^] | 173 | bool ConnectProtocolEndpoints(std::string* _Nonnull error); |
| 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 | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 189 | SubprocessType type_; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 190 | SubprocessProtocol protocol_; |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 191 | bool make_pty_raw_; |
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 | |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 203 | Subprocess::Subprocess(std::string command, const char* terminal_type, SubprocessType type, |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 204 | SubprocessProtocol protocol, bool make_pty_raw) |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 205 | : command_(std::move(command)), |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 206 | terminal_type_(terminal_type ? terminal_type : ""), |
| 207 | type_(type), |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 208 | protocol_(protocol), |
| 209 | make_pty_raw_(make_pty_raw) {} |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 210 | |
| 211 | Subprocess::~Subprocess() { |
Josh Gao | 6a98c6e | 2016-01-19 16:21:17 -0800 | [diff] [blame] | 212 | WaitForExit(); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Elliott Hughes | 299da1c | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 215 | static std::string GetHostName() { |
| 216 | char buf[HOST_NAME_MAX]; |
| 217 | if (gethostname(buf, sizeof(buf)) != -1 && strcmp(buf, "localhost") != 0) return buf; |
| 218 | |
| 219 | return android::base::GetProperty("ro.product.device", "android"); |
| 220 | } |
| 221 | |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 222 | bool Subprocess::ForkAndExec(std::string* error) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 223 | unique_fd child_stdinout_sfd, child_stderr_sfd; |
| 224 | unique_fd parent_error_sfd, child_error_sfd; |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 225 | char pts_name[PATH_MAX]; |
| 226 | |
Rubin Xu | 29a64f9 | 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 | 8d76c45 | 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 | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 236 | if (!CreateSocketpair(&parent_error_sfd, &child_error_sfd)) { |
Josh Gao | 9dc2e93 | 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 | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Josh Gao | 8d76c45 | 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 | 81ea002 | 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; |
Dan Austin | 4bdf38b | 2016-03-28 14:37:01 -0700 | [diff] [blame] | 249 | char* delimiter = strchr(&env_string[0], '='); |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 250 | |
Josh Gao | 81ea002 | 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 | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (pw != nullptr) { |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 260 | env["HOME"] = pw->pw_dir; |
Elliott Hughes | 299da1c | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 261 | env["HOSTNAME"] = GetHostName(); |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 262 | env["LOGNAME"] = pw->pw_name; |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 263 | env["SHELL"] = pw->pw_shell; |
Elliott Hughes | 8719f41 | 2017-12-11 10:40:57 -0800 | [diff] [blame] | 264 | env["TMPDIR"] = "/data/local/tmp"; |
Elliott Hughes | 299da1c | 2017-10-03 08:44:27 -0700 | [diff] [blame] | 265 | env["USER"] = pw->pw_name; |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (!terminal_type_.empty()) { |
| 269 | env["TERM"] = terminal_type_; |
| 270 | } |
| 271 | |
| 272 | std::vector<std::string> joined_env; |
Chih-Hung Hsieh | 93eb389 | 2018-12-11 10:34:33 -0800 | [diff] [blame] | 273 | for (const auto& it : env) { |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 274 | const char* key = it.first.c_str(); |
| 275 | const char* value = it.second.c_str(); |
| 276 | joined_env.push_back(android::base::StringPrintf("%s=%s", key, value)); |
| 277 | } |
| 278 | |
| 279 | std::vector<const char*> cenv; |
| 280 | for (const std::string& str : joined_env) { |
| 281 | cenv.push_back(str.c_str()); |
| 282 | } |
| 283 | cenv.push_back(nullptr); |
| 284 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 285 | if (type_ == SubprocessType::kPty) { |
| 286 | int fd; |
| 287 | pid_ = forkpty(&fd, pts_name, nullptr, nullptr); |
Josh Gao | 9a4b5e9 | 2016-03-04 17:50:10 -0800 | [diff] [blame] | 288 | if (pid_ > 0) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 289 | stdinout_sfd_.reset(fd); |
Josh Gao | 9a4b5e9 | 2016-03-04 17:50:10 -0800 | [diff] [blame] | 290 | } |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 291 | } else { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 292 | if (!CreateSocketpair(&stdinout_sfd_, &child_stdinout_sfd)) { |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 293 | *error = android::base::StringPrintf("failed to create socketpair for stdin/out: %s", |
| 294 | strerror(errno)); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | // Raw subprocess + shell protocol allows for splitting stderr. |
| 298 | if (protocol_ == SubprocessProtocol::kShell && |
| 299 | !CreateSocketpair(&stderr_sfd_, &child_stderr_sfd)) { |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 300 | *error = android::base::StringPrintf("failed to create socketpair for stderr: %s", |
| 301 | strerror(errno)); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | pid_ = fork(); |
| 305 | } |
| 306 | |
| 307 | if (pid_ == -1) { |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 308 | *error = android::base::StringPrintf("fork failed: %s", strerror(errno)); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
| 312 | if (pid_ == 0) { |
| 313 | // Subprocess child. |
Elliott Hughes | fd20a0f | 2016-11-17 10:32:16 -0800 | [diff] [blame] | 314 | setsid(); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 315 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 316 | if (type_ == SubprocessType::kPty) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 317 | child_stdinout_sfd.reset(OpenPtyChildFd(pts_name, &child_error_sfd)); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 320 | dup2(child_stdinout_sfd, STDIN_FILENO); |
| 321 | dup2(child_stdinout_sfd, STDOUT_FILENO); |
| 322 | 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] | 323 | |
| 324 | // exec doesn't trigger destructors, close the FDs manually. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 325 | stdinout_sfd_.reset(-1); |
| 326 | stderr_sfd_.reset(-1); |
| 327 | child_stdinout_sfd.reset(-1); |
| 328 | child_stderr_sfd.reset(-1); |
| 329 | parent_error_sfd.reset(-1); |
| 330 | close_on_exec(child_error_sfd); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 331 | |
Elliott Hughes | 961083e | 2017-02-17 11:14:33 -0800 | [diff] [blame] | 332 | // adbd sets SIGPIPE to SIG_IGN to get EPIPE instead, and Linux propagates that to child |
| 333 | // processes, so we need to manually reset back to SIG_DFL here (http://b/35209888). |
| 334 | signal(SIGPIPE, SIG_DFL); |
| 335 | |
Josh Gao | 212294f | 2018-03-28 13:16:01 -0700 | [diff] [blame] | 336 | // Increase oom_score_adj from -1000, so that the child is visible to the OOM-killer. |
| 337 | // Don't treat failure as an error, because old Android kernels explicitly disabled this. |
| 338 | int oom_score_adj_fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC); |
| 339 | if (oom_score_adj_fd != -1) { |
| 340 | const char* oom_score_adj_value = "-950"; |
| 341 | TEMP_FAILURE_RETRY( |
| 342 | adb_write(oom_score_adj_fd, oom_score_adj_value, strlen(oom_score_adj_value))); |
| 343 | } |
| 344 | |
Jiyong Park | 19c39fa | 2018-05-29 16:41:30 +0900 | [diff] [blame] | 345 | #ifdef __ANDROID_RECOVERY__ |
| 346 | // Special routine for recovery. Switch to shell domain when adbd is |
| 347 | // is running with dropped privileged (i.e. not running as root) and |
| 348 | // is built for the recovery mode. This is required because recovery |
| 349 | // rootfs is not labeled and everything is labeled just as rootfs. |
| 350 | char* con = nullptr; |
| 351 | if (getcon(&con) == 0) { |
| 352 | if (!strcmp(con, "u:r:adbd:s0")) { |
| 353 | if (selinux_android_setcon("u:r:shell:s0") < 0) { |
| 354 | LOG(FATAL) << "Could not set SELinux context for subprocess"; |
| 355 | } |
| 356 | } |
| 357 | freecon(con); |
| 358 | } else { |
| 359 | LOG(FATAL) << "Failed to get SELinux context"; |
| 360 | } |
| 361 | #endif |
| 362 | |
Josh Gao | 8a63116 | 2016-01-19 17:31:09 -0800 | [diff] [blame] | 363 | if (command_.empty()) { |
Josh Gao | 28db729 | 2018-03-21 18:06:20 -0700 | [diff] [blame] | 364 | // Spawn a login shell if we don't have a command. |
| 365 | execle(_PATH_BSHELL, "-" _PATH_BSHELL, nullptr, cenv.data()); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 366 | } else { |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 367 | execle(_PATH_BSHELL, _PATH_BSHELL, "-c", command_.c_str(), nullptr, cenv.data()); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 368 | } |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 369 | WriteFdExactly(child_error_sfd, "exec '" _PATH_BSHELL "' failed: "); |
| 370 | WriteFdExactly(child_error_sfd, strerror(errno)); |
| 371 | child_error_sfd.reset(-1); |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 372 | _Exit(1); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // Subprocess parent. |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 376 | D("subprocess parent: stdin/stdout FD = %d, stderr FD = %d", |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 377 | stdinout_sfd_.get(), stderr_sfd_.get()); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 378 | |
| 379 | // Wait to make sure the subprocess exec'd without error. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 380 | child_error_sfd.reset(-1); |
| 381 | std::string error_message = ReadAll(parent_error_sfd); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 382 | if (!error_message.empty()) { |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 383 | *error = error_message; |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
Josh Gao | 8d76c45 | 2015-12-11 10:52:55 -0800 | [diff] [blame] | 387 | D("subprocess parent: exec completed"); |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame^] | 388 | if (!ConnectProtocolEndpoints(error)) { |
| 389 | kill(pid_, SIGKILL); |
| 390 | return false; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 391 | } |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 392 | |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 393 | D("subprocess parent: completed"); |
| 394 | return true; |
| 395 | } |
| 396 | |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 397 | bool Subprocess::ExecInProcess(Command command, std::string* _Nonnull error) { |
| 398 | unique_fd child_stdinout_sfd, child_stderr_sfd; |
| 399 | |
| 400 | CHECK(type_ == SubprocessType::kRaw); |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 401 | |
| 402 | __android_log_security_bswrite(SEC_TAG_ADB_SHELL_CMD, command_.c_str()); |
| 403 | |
| 404 | if (!CreateSocketpair(&stdinout_sfd_, &child_stdinout_sfd)) { |
| 405 | *error = android::base::StringPrintf("failed to create socketpair for stdin/out: %s", |
| 406 | strerror(errno)); |
| 407 | return false; |
| 408 | } |
| 409 | // Raw subprocess + shell protocol allows for splitting stderr. |
| 410 | if (!CreateSocketpair(&stderr_sfd_, &child_stderr_sfd)) { |
| 411 | *error = android::base::StringPrintf("failed to create socketpair for stderr: %s", |
| 412 | strerror(errno)); |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | D("execinprocess: stdin/stdout FD = %d, stderr FD = %d", stdinout_sfd_.get(), |
| 417 | stderr_sfd_.get()); |
| 418 | |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame^] | 419 | if (!ConnectProtocolEndpoints(error)) { |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 420 | return false; |
| 421 | } |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 422 | |
| 423 | std::thread([inout_sfd = std::move(child_stdinout_sfd), err_sfd = std::move(child_stderr_sfd), |
| 424 | command = std::move(command), |
| 425 | args = command_]() { command(args, inout_sfd, inout_sfd, err_sfd); }) |
| 426 | .detach(); |
| 427 | |
| 428 | D("execinprocess: completed"); |
| 429 | return true; |
| 430 | } |
| 431 | |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame^] | 432 | bool Subprocess::ConnectProtocolEndpoints(std::string* _Nonnull error) { |
| 433 | if (protocol_ == SubprocessProtocol::kNone) { |
| 434 | // No protocol: all streams pass through the stdinout FD and hook |
| 435 | // directly into the local socket for raw data transfer. |
| 436 | local_socket_sfd_.reset(stdinout_sfd_.release()); |
| 437 | } else { |
| 438 | // Required for shell protocol: create another socketpair to intercept data. |
| 439 | if (!CreateSocketpair(&protocol_sfd_, &local_socket_sfd_)) { |
| 440 | *error = android::base::StringPrintf( |
| 441 | "failed to create socketpair to intercept data: %s", strerror(errno)); |
| 442 | return false; |
| 443 | } |
| 444 | D("protocol FD = %d", protocol_sfd_.get()); |
| 445 | |
| 446 | input_ = std::make_unique<ShellProtocol>(protocol_sfd_); |
| 447 | output_ = std::make_unique<ShellProtocol>(protocol_sfd_); |
| 448 | if (!input_ || !output_) { |
| 449 | *error = "failed to allocate shell protocol objects"; |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | // Don't let reads/writes to the subprocess block our thread. This isn't |
| 454 | // likely but could happen under unusual circumstances, such as if we |
| 455 | // write a ton of data to stdin but the subprocess never reads it and |
| 456 | // the pipe fills up. |
| 457 | for (int fd : {stdinout_sfd_.get(), stderr_sfd_.get()}) { |
| 458 | if (fd >= 0) { |
| 459 | if (!set_file_block_mode(fd, false)) { |
| 460 | *error = android::base::StringPrintf( |
| 461 | "failed to set non-blocking mode for fd %d", fd); |
| 462 | return false; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 471 | bool Subprocess::StartThread(std::unique_ptr<Subprocess> subprocess, std::string* error) { |
| 472 | Subprocess* raw = subprocess.release(); |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 473 | std::thread(ThreadHandler, raw).detach(); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 474 | |
| 475 | return true; |
| 476 | } |
| 477 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 478 | int Subprocess::OpenPtyChildFd(const char* pts_name, unique_fd* error_sfd) { |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 479 | int child_fd = adb_open(pts_name, O_RDWR | O_CLOEXEC); |
| 480 | if (child_fd == -1) { |
| 481 | // Don't use WriteFdFmt; since we're in the fork() child we don't want |
| 482 | // to allocate any heap memory to avoid race conditions. |
| 483 | const char* messages[] = {"child failed to open pseudo-term slave ", |
| 484 | pts_name, ": ", strerror(errno)}; |
| 485 | for (const char* message : messages) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 486 | WriteFdExactly(*error_sfd, message); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 487 | } |
Josh Gao | 57cb217 | 2016-05-13 18:16:43 -0700 | [diff] [blame] | 488 | abort(); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 489 | } |
| 490 | |
David Pursell | 182dc32 | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 491 | if (make_pty_raw_) { |
| 492 | termios tattr; |
| 493 | if (tcgetattr(child_fd, &tattr) == -1) { |
| 494 | int saved_errno = errno; |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 495 | WriteFdExactly(*error_sfd, "tcgetattr failed: "); |
| 496 | WriteFdExactly(*error_sfd, strerror(saved_errno)); |
Josh Gao | 57cb217 | 2016-05-13 18:16:43 -0700 | [diff] [blame] | 497 | abort(); |
David Pursell | 182dc32 | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | cfmakeraw(&tattr); |
| 501 | if (tcsetattr(child_fd, TCSADRAIN, &tattr) == -1) { |
| 502 | int saved_errno = errno; |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 503 | WriteFdExactly(*error_sfd, "tcsetattr failed: "); |
| 504 | WriteFdExactly(*error_sfd, strerror(saved_errno)); |
Josh Gao | 57cb217 | 2016-05-13 18:16:43 -0700 | [diff] [blame] | 505 | abort(); |
David Pursell | 182dc32 | 2016-01-27 16:07:52 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 509 | return child_fd; |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 512 | void Subprocess::ThreadHandler(void* userdata) { |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 513 | Subprocess* subprocess = reinterpret_cast<Subprocess*>(userdata); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 514 | |
Josh Gao | d51515b | 2017-09-28 16:29:53 -0700 | [diff] [blame] | 515 | adb_thread_setname(android::base::StringPrintf("shell svc %d", subprocess->pid())); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 516 | |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 517 | D("passing data streams for PID %d", subprocess->pid()); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 518 | subprocess->PassDataStreams(); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 519 | |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 520 | D("deleting Subprocess for PID %d", subprocess->pid()); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 521 | delete subprocess; |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 522 | } |
| 523 | |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 524 | void Subprocess::PassDataStreams() { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 525 | if (protocol_sfd_ == -1) { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
| 529 | // Start by trying to read from the protocol FD, stdout, and stderr. |
| 530 | fd_set master_read_set, master_write_set; |
| 531 | FD_ZERO(&master_read_set); |
| 532 | FD_ZERO(&master_write_set); |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 533 | for (unique_fd* sfd : {&protocol_sfd_, &stdinout_sfd_, &stderr_sfd_}) { |
| 534 | if (*sfd != -1) { |
| 535 | FD_SET(*sfd, &master_read_set); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | // Pass data until the protocol FD or both the subprocess pipes die, at |
| 540 | // which point we can't pass any more data. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 541 | while (protocol_sfd_ != -1 && (stdinout_sfd_ != -1 || stderr_sfd_ != -1)) { |
| 542 | unique_fd* dead_sfd = SelectLoop(&master_read_set, &master_write_set); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 543 | if (dead_sfd) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 544 | D("closing FD %d", dead_sfd->get()); |
| 545 | FD_CLR(*dead_sfd, &master_read_set); |
| 546 | FD_CLR(*dead_sfd, &master_write_set); |
David Pursell | 2b8d4a4 | 2015-09-14 15:36:26 -0700 | [diff] [blame] | 547 | if (dead_sfd == &protocol_sfd_) { |
| 548 | // Using SIGHUP is a decent general way to indicate that the |
| 549 | // controlling process is going away. If specific signals are |
| 550 | // needed (e.g. SIGINT), pass those through the shell protocol |
| 551 | // and only fall back on this for unexpected closures. |
| 552 | D("protocol FD died, sending SIGHUP to pid %d", pid_); |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 553 | if (pid_ != -1) { |
| 554 | kill(pid_, SIGHUP); |
| 555 | } |
David Pursell | aeee003 | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 556 | |
| 557 | // We also need to close the pipes connected to the child process |
| 558 | // so that if it ignores SIGHUP and continues to write data it |
| 559 | // won't fill up the pipe and block. |
Josh Gao | c2d2cb6 | 2016-09-14 12:47:02 -0700 | [diff] [blame] | 560 | stdinout_sfd_.reset(); |
| 561 | stderr_sfd_.reset(); |
David Pursell | 2b8d4a4 | 2015-09-14 15:36:26 -0700 | [diff] [blame] | 562 | } |
Josh Gao | c2d2cb6 | 2016-09-14 12:47:02 -0700 | [diff] [blame] | 563 | dead_sfd->reset(); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | namespace { |
| 569 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 570 | inline bool ValidAndInSet(const unique_fd& sfd, fd_set* set) { |
| 571 | return sfd != -1 && FD_ISSET(sfd, set); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | } // namespace |
| 575 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 576 | unique_fd* Subprocess::SelectLoop(fd_set* master_read_set_ptr, |
| 577 | fd_set* master_write_set_ptr) { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 578 | fd_set read_set, write_set; |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 579 | int select_n = std::max(std::max(protocol_sfd_, stdinout_sfd_), stderr_sfd_) + 1; |
| 580 | unique_fd* dead_sfd = nullptr; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 581 | |
| 582 | // Keep calling select() and passing data until an FD closes/errors. |
| 583 | while (!dead_sfd) { |
| 584 | memcpy(&read_set, master_read_set_ptr, sizeof(read_set)); |
| 585 | memcpy(&write_set, master_write_set_ptr, sizeof(write_set)); |
| 586 | if (select(select_n, &read_set, &write_set, nullptr, nullptr) < 0) { |
| 587 | if (errno == EINTR) { |
| 588 | continue; |
| 589 | } else { |
| 590 | PLOG(ERROR) << "select failed, closing subprocess pipes"; |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 591 | stdinout_sfd_.reset(-1); |
| 592 | stderr_sfd_.reset(-1); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 593 | return nullptr; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Read stdout, write to protocol FD. |
| 598 | if (ValidAndInSet(stdinout_sfd_, &read_set)) { |
| 599 | dead_sfd = PassOutput(&stdinout_sfd_, ShellProtocol::kIdStdout); |
| 600 | } |
| 601 | |
| 602 | // Read stderr, write to protocol FD. |
| 603 | if (!dead_sfd && ValidAndInSet(stderr_sfd_, &read_set)) { |
| 604 | dead_sfd = PassOutput(&stderr_sfd_, ShellProtocol::kIdStderr); |
| 605 | } |
| 606 | |
| 607 | // Read protocol FD, write to stdin. |
| 608 | if (!dead_sfd && ValidAndInSet(protocol_sfd_, &read_set)) { |
| 609 | dead_sfd = PassInput(); |
| 610 | // If we didn't finish writing, block on stdin write. |
| 611 | if (input_bytes_left_) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 612 | FD_CLR(protocol_sfd_, master_read_set_ptr); |
| 613 | FD_SET(stdinout_sfd_, master_write_set_ptr); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | // Continue writing to stdin; only happens if a previous write blocked. |
| 618 | if (!dead_sfd && ValidAndInSet(stdinout_sfd_, &write_set)) { |
| 619 | dead_sfd = PassInput(); |
| 620 | // If we finished writing, go back to blocking on protocol read. |
| 621 | if (!input_bytes_left_) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 622 | FD_SET(protocol_sfd_, master_read_set_ptr); |
| 623 | FD_CLR(stdinout_sfd_, master_write_set_ptr); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | } // while (!dead_sfd) |
| 627 | |
| 628 | return dead_sfd; |
| 629 | } |
| 630 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 631 | unique_fd* Subprocess::PassInput() { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 632 | // Only read a new packet if we've finished writing the last one. |
| 633 | if (!input_bytes_left_) { |
| 634 | if (!input_->Read()) { |
| 635 | // Read() uses ReadFdExactly() which sets errno to 0 on EOF. |
| 636 | if (errno != 0) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 637 | PLOG(ERROR) << "error reading protocol FD " << protocol_sfd_; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 638 | } |
| 639 | return &protocol_sfd_; |
| 640 | } |
| 641 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 642 | if (stdinout_sfd_ != -1) { |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 643 | switch (input_->id()) { |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 644 | case ShellProtocol::kIdWindowSizeChange: |
| 645 | int rows, cols, x_pixels, y_pixels; |
| 646 | if (sscanf(input_->data(), "%dx%d,%dx%d", |
| 647 | &rows, &cols, &x_pixels, &y_pixels) == 4) { |
| 648 | winsize ws; |
| 649 | ws.ws_row = rows; |
| 650 | ws.ws_col = cols; |
| 651 | ws.ws_xpixel = x_pixels; |
| 652 | ws.ws_ypixel = y_pixels; |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 653 | ioctl(stdinout_sfd_, TIOCSWINSZ, &ws); |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 654 | } |
| 655 | break; |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 656 | case ShellProtocol::kIdStdin: |
| 657 | input_bytes_left_ = input_->data_length(); |
| 658 | break; |
| 659 | case ShellProtocol::kIdCloseStdin: |
| 660 | if (type_ == SubprocessType::kRaw) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 661 | if (adb_shutdown(stdinout_sfd_, SHUT_WR) == 0) { |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 662 | return nullptr; |
| 663 | } |
| 664 | PLOG(ERROR) << "failed to shutdown writes to FD " |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 665 | << stdinout_sfd_; |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 666 | return &stdinout_sfd_; |
| 667 | } else { |
| 668 | // PTYs can't close just input, so rather than close the |
| 669 | // FD and risk losing subprocess output, leave it open. |
| 670 | // This only happens if the client starts a PTY shell |
| 671 | // non-interactively which is rare and unsupported. |
| 672 | // If necessary, the client can manually close the shell |
| 673 | // with `exit` or by killing the adb client process. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 674 | D("can't close input for PTY FD %d", stdinout_sfd_.get()); |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 675 | } |
| 676 | break; |
| 677 | } |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
| 681 | if (input_bytes_left_ > 0) { |
| 682 | int index = input_->data_length() - input_bytes_left_; |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 683 | int bytes = adb_write(stdinout_sfd_, input_->data() + index, input_bytes_left_); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 684 | if (bytes == 0 || (bytes < 0 && errno != EAGAIN)) { |
| 685 | if (bytes < 0) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 686 | PLOG(ERROR) << "error reading stdin FD " << stdinout_sfd_; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 687 | } |
| 688 | // stdin is done, mark this packet as finished and we'll just start |
| 689 | // dumping any further data received from the protocol FD. |
| 690 | input_bytes_left_ = 0; |
| 691 | return &stdinout_sfd_; |
| 692 | } else if (bytes > 0) { |
| 693 | input_bytes_left_ -= bytes; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return nullptr; |
| 698 | } |
| 699 | |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 700 | unique_fd* Subprocess::PassOutput(unique_fd* sfd, ShellProtocol::Id id) { |
| 701 | int bytes = adb_read(*sfd, output_->data(), output_->data_capacity()); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 702 | if (bytes == 0 || (bytes < 0 && errno != EAGAIN)) { |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 703 | // read() returns EIO if a PTY closes; don't report this as an error, |
| 704 | // it just means the subprocess completed. |
| 705 | if (bytes < 0 && !(type_ == SubprocessType::kPty && errno == EIO)) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 706 | PLOG(ERROR) << "error reading output FD " << *sfd; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 707 | } |
| 708 | return sfd; |
| 709 | } |
| 710 | |
| 711 | if (bytes > 0 && !output_->Write(id, bytes)) { |
| 712 | if (errno != 0) { |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 713 | PLOG(ERROR) << "error reading protocol FD " << protocol_sfd_; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 714 | } |
| 715 | return &protocol_sfd_; |
| 716 | } |
| 717 | |
| 718 | return nullptr; |
| 719 | } |
| 720 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 721 | void Subprocess::WaitForExit() { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 722 | int exit_code = 1; |
| 723 | |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 724 | D("waiting for pid %d", pid_); |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 725 | while (pid_ != -1) { |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 726 | int status; |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 727 | if (pid_ == waitpid(pid_, &status, 0)) { |
| 728 | D("post waitpid (pid=%d) status=%04x", pid_, status); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 729 | if (WIFSIGNALED(status)) { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 730 | exit_code = 0x80 | WTERMSIG(status); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 731 | D("subprocess killed by signal %d", WTERMSIG(status)); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 732 | break; |
| 733 | } else if (!WIFEXITED(status)) { |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 734 | D("subprocess didn't exit"); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 735 | break; |
| 736 | } else if (WEXITSTATUS(status) >= 0) { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 737 | exit_code = WEXITSTATUS(status); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 738 | D("subprocess exit code = %d", WEXITSTATUS(status)); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 739 | break; |
| 740 | } |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 741 | } |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 742 | } |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 743 | |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 744 | // If we have an open protocol FD send an exit packet. |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 745 | if (protocol_sfd_ != -1) { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 746 | output_->data()[0] = exit_code; |
| 747 | if (output_->Write(ShellProtocol::kIdExit, 1)) { |
| 748 | D("wrote the exit code packet: %d", exit_code); |
| 749 | } else { |
| 750 | PLOG(ERROR) << "failed to write the exit code packet"; |
| 751 | } |
Elliott Hughes | 857e659 | 2016-05-27 17:51:24 -0700 | [diff] [blame] | 752 | protocol_sfd_.reset(-1); |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 753 | } |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | } // namespace |
| 757 | |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 758 | // Create a pipe containing the error. |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 759 | unique_fd ReportError(SubprocessProtocol protocol, const std::string& message) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 760 | unique_fd read, write; |
| 761 | if (!Pipe(&read, &write)) { |
| 762 | PLOG(ERROR) << "failed to create pipe to report error"; |
| 763 | return unique_fd{}; |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | std::string buf = android::base::StringPrintf("error: %s\n", message.c_str()); |
| 767 | if (protocol == SubprocessProtocol::kShell) { |
| 768 | ShellProtocol::Id id = ShellProtocol::kIdStderr; |
| 769 | uint32_t length = buf.length(); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 770 | WriteFdExactly(write.get(), &id, sizeof(id)); |
| 771 | WriteFdExactly(write.get(), &length, sizeof(length)); |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 774 | WriteFdExactly(write.get(), buf.data(), buf.length()); |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 775 | |
| 776 | if (protocol == SubprocessProtocol::kShell) { |
| 777 | ShellProtocol::Id id = ShellProtocol::kIdExit; |
| 778 | uint32_t length = 1; |
| 779 | char exit_code = 126; |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 780 | WriteFdExactly(write.get(), &id, sizeof(id)); |
| 781 | WriteFdExactly(write.get(), &length, sizeof(length)); |
| 782 | WriteFdExactly(write.get(), &exit_code, sizeof(exit_code)); |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 785 | return read; |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 788 | unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type, |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 789 | SubprocessProtocol protocol) { |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 790 | // If we aren't using the shell protocol we must allocate a PTY to properly close the |
| 791 | // subprocess. PTYs automatically send SIGHUP to the slave-side process when the master side |
| 792 | // of the PTY closes, which we rely on. If we use a raw pipe, processes that don't read/write, |
| 793 | // e.g. screenrecord, will never notice the broken pipe and terminate. |
| 794 | // The shell protocol doesn't require a PTY because it's always monitoring the local socket FD |
| 795 | // with select() and will send SIGHUP manually to the child process. |
| 796 | bool make_pty_raw = false; |
| 797 | if (protocol == SubprocessProtocol::kNone && type == SubprocessType::kRaw) { |
| 798 | // Disable PTY input/output processing since the client is expecting raw data. |
| 799 | D("Can't create raw subprocess without shell protocol, using PTY in raw mode instead"); |
| 800 | type = SubprocessType::kPty; |
| 801 | make_pty_raw = true; |
| 802 | } |
| 803 | |
| 804 | unique_fd error_fd; |
| 805 | unique_fd fd = StartSubprocess(std::move(name), terminal_type, type, protocol, make_pty_raw, |
| 806 | protocol, &error_fd); |
| 807 | if (fd == -1) { |
| 808 | return error_fd; |
| 809 | } |
| 810 | return fd; |
| 811 | } |
| 812 | |
| 813 | unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type, |
| 814 | SubprocessProtocol protocol, bool make_pty_raw, |
| 815 | SubprocessProtocol error_protocol, unique_fd* error_fd) { |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 816 | D("starting %s subprocess (protocol=%s, TERM=%s): '%s'", |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 817 | type == SubprocessType::kRaw ? "raw" : "PTY", |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 818 | protocol == SubprocessProtocol::kNone ? "none" : "shell", terminal_type, name.c_str()); |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 819 | |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 820 | auto subprocess = std::make_unique<Subprocess>(std::move(name), terminal_type, type, protocol, |
| 821 | make_pty_raw); |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 822 | if (!subprocess) { |
| 823 | LOG(ERROR) << "failed to allocate new subprocess"; |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 824 | *error_fd = ReportError(error_protocol, "failed to allocate new subprocess"); |
| 825 | return {}; |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Josh Gao | 9dc2e93 | 2016-01-25 17:11:43 -0800 | [diff] [blame] | 828 | std::string error; |
| 829 | if (!subprocess->ForkAndExec(&error)) { |
| 830 | LOG(ERROR) << "failed to start subprocess: " << error; |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 831 | *error_fd = ReportError(error_protocol, error); |
| 832 | return {}; |
David Pursell | 917dcfa | 2015-08-28 18:31:29 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Josh Gao | 8d84a31 | 2016-06-23 11:21:11 -0700 | [diff] [blame] | 835 | unique_fd local_socket(subprocess->ReleaseLocalSocket()); |
| 836 | D("subprocess creation successful: local_socket_fd=%d, pid=%d", local_socket.get(), |
| 837 | subprocess->pid()); |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 838 | |
| 839 | if (!Subprocess::StartThread(std::move(subprocess), &error)) { |
| 840 | LOG(ERROR) << "failed to start subprocess management thread: " << error; |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 841 | *error_fd = ReportError(error_protocol, error); |
| 842 | return {}; |
| 843 | } |
| 844 | |
| 845 | return local_socket; |
| 846 | } |
| 847 | |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame^] | 848 | unique_fd StartCommandInProcess(std::string name, Command command, SubprocessProtocol protocol) { |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 849 | LOG(INFO) << "StartCommandInProcess(" << dump_hex(name.data(), name.size()) << ")"; |
| 850 | |
| 851 | constexpr auto terminal_type = ""; |
| 852 | constexpr auto type = SubprocessType::kRaw; |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 853 | constexpr auto make_pty_raw = false; |
| 854 | |
| 855 | auto subprocess = std::make_unique<Subprocess>(std::move(name), terminal_type, type, protocol, |
| 856 | make_pty_raw); |
| 857 | if (!subprocess) { |
| 858 | LOG(ERROR) << "failed to allocate new subprocess"; |
| 859 | return ReportError(protocol, "failed to allocate new subprocess"); |
| 860 | } |
| 861 | |
| 862 | std::string error; |
| 863 | if (!subprocess->ExecInProcess(std::move(command), &error)) { |
| 864 | LOG(ERROR) << "failed to start subprocess: " << error; |
| 865 | return ReportError(protocol, error); |
| 866 | } |
| 867 | |
| 868 | unique_fd local_socket(subprocess->ReleaseLocalSocket()); |
| 869 | D("inprocess creation successful: local_socket_fd=%d, pid=%d", local_socket.get(), |
| 870 | subprocess->pid()); |
| 871 | |
| 872 | if (!Subprocess::StartThread(std::move(subprocess), &error)) { |
| 873 | LOG(ERROR) << "failed to start inprocess management thread: " << error; |
Josh Gao | 6d3a75a | 2016-06-17 14:53:57 -0700 | [diff] [blame] | 874 | return ReportError(protocol, error); |
| 875 | } |
| 876 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 877 | return local_socket; |
David Pursell | 4f344bb | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 878 | } |