Spencer Low | 7dc759f | 2015-05-06 16:13:42 -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 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include "sysdeps.h" |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/strings.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 24 | #include <cutils/sockets.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | #include "adb_client.h" |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 28 | #include "adb_io.h" |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 29 | #include "adb_utils.h" |
| 30 | |
| 31 | // Return the console authentication command for the emulator, if needed |
| 32 | static std::string adb_construct_auth_command() { |
| 33 | static const char auth_token_filename[] = ".emulator_console_auth_token"; |
| 34 | |
Josh Gao | 62ff9d4 | 2016-08-30 15:23:35 -0700 | [diff] [blame] | 35 | std::string auth_token_path = adb_get_homedir_path(); |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 36 | auth_token_path += OS_PATH_SEPARATOR; |
| 37 | auth_token_path += auth_token_filename; |
| 38 | |
| 39 | // read the token |
| 40 | std::string token; |
| 41 | if (!android::base::ReadFileToString(auth_token_path, &token) |
| 42 | || token.empty()) { |
| 43 | // we either can't read the file, or it doesn't exist, or it's empty - |
| 44 | // either way we won't add any authentication command. |
| 45 | return {}; |
| 46 | } |
| 47 | |
| 48 | // now construct and return the actual command: "auth <token>\n" |
| 49 | std::string command = "auth "; |
| 50 | command += token; |
| 51 | command += '\n'; |
| 52 | return command; |
| 53 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 54 | |
| 55 | // Return the console port of the currently connected emulator (if any) or -1 if |
| 56 | // there is no emulator, and -2 if there is more than one. |
| 57 | static int adb_get_emulator_console_port(const char* serial) { |
| 58 | if (serial) { |
| 59 | // The user specified a serial number; is it an emulator? |
| 60 | int port; |
| 61 | return (sscanf(serial, "emulator-%d", &port) == 1) ? port : -1; |
| 62 | } |
| 63 | |
| 64 | // No specific device was given, so get the list of connected devices and |
| 65 | // search for emulators. If there's one, we'll take it. If there are more |
| 66 | // than one, that's an error. |
| 67 | std::string devices; |
| 68 | std::string error; |
| 69 | if (!adb_query("host:devices", &devices, &error)) { |
| 70 | fprintf(stderr, "error: no emulator connected: %s\n", error.c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | return -1; |
| 72 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 73 | |
Stephen Hines | 6bc205e | 2019-05-09 12:55:41 -0700 | [diff] [blame^] | 74 | int port = -1; |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 75 | size_t emulator_count = 0; |
| 76 | for (const auto& device : android::base::Split(devices, "\n")) { |
| 77 | if (sscanf(device.c_str(), "emulator-%d", &port) == 1) { |
| 78 | if (++emulator_count > 1) { |
| 79 | fprintf( |
| 80 | stderr, "error: more than one emulator detected; use -s\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (emulator_count == 0) { |
| 87 | fprintf(stderr, "error: no emulator detected\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | return port; |
| 92 | } |
| 93 | |
| 94 | static int connect_to_console(const char* serial) { |
| 95 | int port = adb_get_emulator_console_port(serial); |
| 96 | if (port == -1) { |
| 97 | return -1; |
| 98 | } |
| 99 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 100 | std::string error; |
| 101 | int fd = network_loopback_client(port, SOCK_STREAM, &error); |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 102 | if (fd == -1) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 103 | fprintf(stderr, "error: could not connect to TCP port %d: %s\n", port, |
| 104 | error.c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | return -1; |
| 106 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 107 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 110 | int adb_send_emulator_command(int argc, const char** argv, const char* serial) { |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 111 | unique_fd fd(connect_to_console(serial)); |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 112 | if (fd == -1) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | return 1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 115 | |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 116 | std::string commands = adb_construct_auth_command(); |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 117 | |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 118 | for (int i = 1; i < argc; i++) { |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 119 | commands.append(argv[i]); |
Yurii Zubrytskyi | c0889ab | 2016-05-25 15:17:10 -0700 | [diff] [blame] | 120 | commands.push_back(i == argc - 1 ? '\n' : ' '); |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 123 | commands.append("quit\n"); |
| 124 | |
| 125 | if (!WriteFdExactly(fd, commands)) { |
| 126 | fprintf(stderr, "error: cannot write to emulator: %s\n", |
| 127 | strerror(errno)); |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 128 | return 1; |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Drain output that the emulator console has sent us to prevent a problem |
| 132 | // on Windows where if adb closes the socket without reading all the data, |
| 133 | // the emulator's next call to recv() will have an ECONNABORTED error, |
| 134 | // preventing the emulator from reading the command that adb has sent. |
| 135 | // https://code.google.com/p/android/issues/detail?id=21021 |
| 136 | int result; |
bohu | ddcfe21 | 2018-10-26 07:11:55 -0700 | [diff] [blame] | 137 | std::string emulator_output; |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 138 | do { |
| 139 | char buf[BUFSIZ]; |
| 140 | result = adb_read(fd, buf, sizeof(buf)); |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 141 | // Keep reading until zero bytes (orderly/graceful shutdown) or an |
| 142 | // error. If 'adb emu kill' is executed, the emulator calls exit() with |
| 143 | // the socket open (and shutdown(SD_SEND) was not called), which causes |
| 144 | // Windows to send a TCP RST segment which causes adb to get ECONNRESET. |
| 145 | // Any other emu command is followed by the quit command that we |
| 146 | // appended above, and that causes the emulator to close the socket |
| 147 | // which should cause zero bytes (orderly/graceful shutdown) to be |
| 148 | // returned. |
bohu | ddcfe21 | 2018-10-26 07:11:55 -0700 | [diff] [blame] | 149 | if (result > 0) emulator_output.append(buf, result); |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 150 | } while (result > 0); |
| 151 | |
bohu | ddcfe21 | 2018-10-26 07:11:55 -0700 | [diff] [blame] | 152 | // Note: the following messages are expected to be quite stable from emulator. |
| 153 | // |
| 154 | // Emulator console will send the following message upon connection: |
| 155 | // |
| 156 | // Android Console: Authentication required |
| 157 | // Android Console: type 'auth <auth_token>' to authenticate |
| 158 | // Android Console: you can find your <auth_token> in |
| 159 | // '/<path-to-home>/.emulator_console_auth_token' |
| 160 | // OK\r\n |
| 161 | // |
| 162 | // and the following after authentication: |
| 163 | // Android Console: type 'help' for a list of commands |
| 164 | // OK\r\n |
| 165 | // |
| 166 | // So try search and skip first two "OK\r\n", print the rest. |
| 167 | // |
| 168 | const std::string delims = "OK\r\n"; |
| 169 | size_t found = 0; |
| 170 | for (int i = 0; i < 2; ++i) { |
| 171 | const size_t result = emulator_output.find(delims, found); |
| 172 | if (result == std::string::npos) { |
| 173 | break; |
| 174 | } else { |
| 175 | found = result + delims.size(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | printf("%s", emulator_output.c_str() + found); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |