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