blob: 93a929db27b966331851f5d6546f6a1b36006f16 [file] [log] [blame]
Ryan Haininge0ac6c62018-06-27 17:25:09 -07001/*
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. Moreira84f539b2019-05-02 17:52:58 -070028#include <host/commands/kernel_log_monitor/kernel_log_server.h>
Ryan Haininge0ac6c62018-06-27 17:25:09 -070029
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070030#include "common/libs/fs/shared_fd.h"
Ryan Haininge0ac6c62018-06-27 17:25:09 -070031#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. Moreira84f539b2019-05-02 17:52:58 -070035DEFINE_string(addresses, "", "Comma-separated list of addresses to "
36 "'adb connect' to");
37DEFINE_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 Haininge0ac6c62018-06-27 17:25:09 -070040
41namespace {
Cody Schuffelenda018ca2019-02-05 13:49:56 -080042void LaunchConnectionMaintainerThread(const std::string& address) {
43 std::thread(cvd::EstablishAndMaintainConnection, address).detach();
Ryan Haininge0ac6c62018-06-27 17:25:09 -070044}
45
Cody Schuffelenda018ca2019-02-05 13:49:56 -080046std::vector<std::string> ParseAddressList(std::string ports) {
Ryan Haininge0ac6c62018-06-27 17:25:09 -070047 std::replace(ports.begin(), ports.end(), ',', ' ');
48 std::istringstream port_stream{ports};
Cody Schuffelenda018ca2019-02-05 13:49:56 -080049 return {std::istream_iterator<std::string>{port_stream},
50 std::istream_iterator<std::string>{}};
Ryan Haininge0ac6c62018-06-27 17:25:09 -070051}
52
53[[noreturn]] void SleepForever() {
54 while (true) {
55 sleep(std::numeric_limits<unsigned int>::max());
56 }
57}
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070058
59void 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 Haininge0ac6c62018-06-27 17:25:09 -070078} // namespace
79
80int main(int argc, char* argv[]) {
81 gflags::ParseCommandLineFlags(&argc, &argv, true);
Cody Schuffelenda018ca2019-02-05 13:49:56 -080082 CHECK(!FLAGS_addresses.empty()) << "Must specify --addresses flag";
Ryan Haininge0ac6c62018-06-27 17:25:09 -070083
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070084 if (FLAGS_adbd_events_fd >= 0) {
85 WaitForAdbdToBeStarted(FLAGS_adbd_events_fd);
86 }
87
Cody Schuffelenda018ca2019-02-05 13:49:56 -080088 for (auto address : ParseAddressList(FLAGS_addresses)) {
89 LaunchConnectionMaintainerThread(address);
Ryan Haininge0ac6c62018-06-27 17:25:09 -070090 }
91
92 SleepForever();
93}