Spencer Low | 142ec75 | 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 | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include "sysdeps.h" |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | |
Elliott Hughes | 4f71319 | 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 | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 24 | #include <cutils/sockets.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | #include "adb_client.h" |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 28 | #include "adb_io.h" |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 29 | |
| 30 | // Return the console port of the currently connected emulator (if any) or -1 if |
| 31 | // there is no emulator, and -2 if there is more than one. |
| 32 | static int adb_get_emulator_console_port(const char* serial) { |
| 33 | if (serial) { |
| 34 | // The user specified a serial number; is it an emulator? |
| 35 | int port; |
| 36 | return (sscanf(serial, "emulator-%d", &port) == 1) ? port : -1; |
| 37 | } |
| 38 | |
| 39 | // No specific device was given, so get the list of connected devices and |
| 40 | // search for emulators. If there's one, we'll take it. If there are more |
| 41 | // than one, that's an error. |
| 42 | std::string devices; |
| 43 | std::string error; |
| 44 | if (!adb_query("host:devices", &devices, &error)) { |
| 45 | fprintf(stderr, "error: no emulator connected: %s\n", error.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | return -1; |
| 47 | } |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 48 | |
| 49 | int port; |
| 50 | size_t emulator_count = 0; |
| 51 | for (const auto& device : android::base::Split(devices, "\n")) { |
| 52 | if (sscanf(device.c_str(), "emulator-%d", &port) == 1) { |
| 53 | if (++emulator_count > 1) { |
| 54 | fprintf( |
| 55 | stderr, "error: more than one emulator detected; use -s\n"); |
| 56 | return -1; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (emulator_count == 0) { |
| 62 | fprintf(stderr, "error: no emulator detected\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | return port; |
| 67 | } |
| 68 | |
| 69 | static int connect_to_console(const char* serial) { |
| 70 | int port = adb_get_emulator_console_port(serial); |
| 71 | if (port == -1) { |
| 72 | return -1; |
| 73 | } |
| 74 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 75 | std::string error; |
| 76 | int fd = network_loopback_client(port, SOCK_STREAM, &error); |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 77 | if (fd == -1) { |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 78 | fprintf(stderr, "error: could not connect to TCP port %d: %s\n", port, |
| 79 | error.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | return -1; |
| 81 | } |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 82 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 85 | int adb_send_emulator_command(int argc, const char** argv, const char* serial) { |
| 86 | int fd = connect_to_console(serial); |
| 87 | if (fd == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 90 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 91 | std::string commands; |
| 92 | |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 93 | for (int i = 1; i < argc; i++) { |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 94 | commands.append(argv[i]); |
| 95 | commands.append(i == argc - 1 ? "\n" : " "); |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 98 | commands.append("quit\n"); |
| 99 | |
| 100 | if (!WriteFdExactly(fd, commands)) { |
| 101 | fprintf(stderr, "error: cannot write to emulator: %s\n", |
| 102 | strerror(errno)); |
| 103 | adb_close(fd); |
| 104 | return 1; |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Drain output that the emulator console has sent us to prevent a problem |
| 108 | // on Windows where if adb closes the socket without reading all the data, |
| 109 | // the emulator's next call to recv() will have an ECONNABORTED error, |
| 110 | // preventing the emulator from reading the command that adb has sent. |
| 111 | // https://code.google.com/p/android/issues/detail?id=21021 |
| 112 | int result; |
| 113 | do { |
| 114 | char buf[BUFSIZ]; |
| 115 | result = adb_read(fd, buf, sizeof(buf)); |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 116 | // Keep reading until zero bytes (orderly/graceful shutdown) or an |
| 117 | // error. If 'adb emu kill' is executed, the emulator calls exit() with |
| 118 | // the socket open (and shutdown(SD_SEND) was not called), which causes |
| 119 | // Windows to send a TCP RST segment which causes adb to get ECONNRESET. |
| 120 | // Any other emu command is followed by the quit command that we |
| 121 | // appended above, and that causes the emulator to close the socket |
| 122 | // which should cause zero bytes (orderly/graceful shutdown) to be |
| 123 | // returned. |
Spencer Low | 142ec75 | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 124 | } while (result > 0); |
| 125 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | adb_close(fd); |
| 127 | |
| 128 | return 0; |
| 129 | } |