blob: 3d3d824fd75f63fe521723cc3f3ebfd2e852a56e [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 "host/libs/config/cuttlefish_config.h"
32#include "host/libs/adb_connection_maintainer/adb_connection_maintainer.h"
33
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070034DEFINE_string(addresses, "", "Comma-separated list of addresses to "
35 "'adb connect' to");
36DEFINE_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 Haininge0ac6c62018-06-27 17:25:09 -070039
40namespace {
Cody Schuffelenda018ca2019-02-05 13:49:56 -080041void LaunchConnectionMaintainerThread(const std::string& address) {
42 std::thread(cvd::EstablishAndMaintainConnection, address).detach();
Ryan Haininge0ac6c62018-06-27 17:25:09 -070043}
44
Cody Schuffelenda018ca2019-02-05 13:49:56 -080045std::vector<std::string> ParseAddressList(std::string ports) {
Ryan Haininge0ac6c62018-06-27 17:25:09 -070046 std::replace(ports.begin(), ports.end(), ',', ' ');
47 std::istringstream port_stream{ports};
Cody Schuffelenda018ca2019-02-05 13:49:56 -080048 return {std::istream_iterator<std::string>{port_stream},
49 std::istream_iterator<std::string>{}};
Ryan Haininge0ac6c62018-06-27 17:25:09 -070050}
51
52[[noreturn]] void SleepForever() {
53 while (true) {
54 sleep(std::numeric_limits<unsigned int>::max());
55 }
56}
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070057
58void 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 Haininge0ac6c62018-06-27 17:25:09 -070077} // namespace
78
79int main(int argc, char* argv[]) {
80 gflags::ParseCommandLineFlags(&argc, &argv, true);
Cody Schuffelenda018ca2019-02-05 13:49:56 -080081 CHECK(!FLAGS_addresses.empty()) << "Must specify --addresses flag";
Ryan Haininge0ac6c62018-06-27 17:25:09 -070082
Jorge E. Moreira84f539b2019-05-02 17:52:58 -070083 if (FLAGS_adbd_events_fd >= 0) {
84 WaitForAdbdToBeStarted(FLAGS_adbd_events_fd);
85 }
86
Cody Schuffelenda018ca2019-02-05 13:49:56 -080087 for (auto address : ParseAddressList(FLAGS_addresses)) {
88 LaunchConnectionMaintainerThread(address);
Ryan Haininge0ac6c62018-06-27 17:25:09 -070089 }
90
91 SleepForever();
92}