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