Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #include <algorithm> |
| 18 | #include <iterator> |
| 19 | #include <limits> |
| 20 | #include <sstream> |
| 21 | #include <thread> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <glog/logging.h> |
| 25 | #include <gflags/gflags.h> |
| 26 | |
| 27 | #include <unistd.h> |
Jorge E. Moreira | 84f539b | 2019-05-02 17:52:58 -0700 | [diff] [blame] | 28 | #include <host/commands/kernel_log_monitor/kernel_log_server.h> |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 29 | |
Jorge E. Moreira | 84f539b | 2019-05-02 17:52:58 -0700 | [diff] [blame] | 30 | #include "common/libs/fs/shared_fd.h" |
A. Cody Schuffelen | 957fb0f | 2020-01-29 18:48:06 -0800 | [diff] [blame] | 31 | #include "host/frontend/adb_connector/adb_connection_maintainer.h" |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 32 | #include "host/libs/config/cuttlefish_config.h" |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 33 | |
Jorge E. Moreira | 84f539b | 2019-05-02 17:52:58 -0700 | [diff] [blame] | 34 | DEFINE_string(addresses, "", "Comma-separated list of addresses to " |
| 35 | "'adb connect' to"); |
| 36 | DEFINE_int32(adbd_events_fd, -1, "A file descriptor. If set it will wait for " |
| 37 | "AdbdStarted boot event from the kernel log " |
| 38 | "monitor before trying to connect adb"); |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 39 | |
| 40 | namespace { |
Cody Schuffelen | da018ca | 2019-02-05 13:49:56 -0800 | [diff] [blame] | 41 | void LaunchConnectionMaintainerThread(const std::string& address) { |
| 42 | std::thread(cvd::EstablishAndMaintainConnection, address).detach(); |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Cody Schuffelen | da018ca | 2019-02-05 13:49:56 -0800 | [diff] [blame] | 45 | std::vector<std::string> ParseAddressList(std::string ports) { |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 46 | std::replace(ports.begin(), ports.end(), ',', ' '); |
| 47 | std::istringstream port_stream{ports}; |
Cody Schuffelen | da018ca | 2019-02-05 13:49:56 -0800 | [diff] [blame] | 48 | return {std::istream_iterator<std::string>{port_stream}, |
| 49 | std::istream_iterator<std::string>{}}; |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | [[noreturn]] void SleepForever() { |
| 53 | while (true) { |
| 54 | sleep(std::numeric_limits<unsigned int>::max()); |
| 55 | } |
| 56 | } |
Jorge E. Moreira | 84f539b | 2019-05-02 17:52:58 -0700 | [diff] [blame] | 57 | |
| 58 | void WaitForAdbdToBeStarted(int events_fd) { |
| 59 | auto evt_shared_fd = cvd::SharedFD::Dup(events_fd); |
| 60 | close(events_fd); |
| 61 | while (evt_shared_fd->IsOpen()) { |
| 62 | monitor::BootEvent event; |
| 63 | auto bytes_read = evt_shared_fd->Read(&event, sizeof(event)); |
| 64 | if (bytes_read != sizeof(event)) { |
| 65 | LOG(ERROR) << "Fail to read a complete event, read " << bytes_read |
| 66 | << " bytes only instead of the expected " << sizeof(event); |
| 67 | // The file descriptor can't be trusted anymore, stop waiting and try to |
| 68 | // connect |
| 69 | return; |
| 70 | } |
| 71 | if (event == monitor::BootEvent::AdbdStarted) { |
| 72 | LOG(INFO) << "Adbd has started in the guest, connecting adb"; |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | } |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 77 | } // namespace |
| 78 | |
| 79 | int main(int argc, char* argv[]) { |
| 80 | gflags::ParseCommandLineFlags(&argc, &argv, true); |
Cody Schuffelen | da018ca | 2019-02-05 13:49:56 -0800 | [diff] [blame] | 81 | CHECK(!FLAGS_addresses.empty()) << "Must specify --addresses flag"; |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 82 | |
Jorge E. Moreira | 84f539b | 2019-05-02 17:52:58 -0700 | [diff] [blame] | 83 | if (FLAGS_adbd_events_fd >= 0) { |
| 84 | WaitForAdbdToBeStarted(FLAGS_adbd_events_fd); |
| 85 | } |
| 86 | |
Cody Schuffelen | da018ca | 2019-02-05 13:49:56 -0800 | [diff] [blame] | 87 | for (auto address : ParseAddressList(FLAGS_addresses)) { |
| 88 | LaunchConnectionMaintainerThread(address); |
Ryan Haining | e0ac6c6 | 2018-06-27 17:25:09 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | SleepForever(); |
| 92 | } |