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 <android-base/logging.h>
25 #include <gflags/gflags.h>
26 
27 #include <unistd.h>
28 #include <host/commands/kernel_log_monitor/kernel_log_server.h>
29 
30 #include "common/libs/fs/shared_fd.h"
31 #include "host/frontend/adb_connector/adb_connection_maintainer.h"
32 #include "host/libs/config/cuttlefish_config.h"
33 #include "host/libs/config/logging.h"
34 
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");
40 
41 namespace {
LaunchConnectionMaintainerThread(const std::string & address)42 void LaunchConnectionMaintainerThread(const std::string& address) {
43   std::thread(cuttlefish::EstablishAndMaintainConnection, address).detach();
44 }
45 
ParseAddressList(std::string ports)46 std::vector<std::string> ParseAddressList(std::string ports) {
47   std::replace(ports.begin(), ports.end(), ',', ' ');
48   std::istringstream port_stream{ports};
49   return {std::istream_iterator<std::string>{port_stream},
50           std::istream_iterator<std::string>{}};
51 }
52 
SleepForever()53 [[noreturn]] void SleepForever() {
54   while (true) {
55     sleep(std::numeric_limits<unsigned int>::max());
56   }
57 }
58 
WaitForAdbdToBeStarted(int events_fd)59 void WaitForAdbdToBeStarted(int events_fd) {
60   auto evt_shared_fd = cuttlefish::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(DEBUG) << "Adbd has started in the guest, connecting adb";
74       return;
75     }
76   }
77 }
78 }  // namespace
79 
main(int argc,char * argv[])80 int main(int argc, char* argv[]) {
81   cuttlefish::DefaultSubprocessLogging(argv);
82   gflags::ParseCommandLineFlags(&argc, &argv, true);
83   CHECK(!FLAGS_addresses.empty()) << "Must specify --addresses flag";
84 
85   if (FLAGS_adbd_events_fd >= 0) {
86     WaitForAdbdToBeStarted(FLAGS_adbd_events_fd);
87   }
88 
89   for (auto address : ParseAddressList(FLAGS_addresses)) {
90     LaunchConnectionMaintainerThread(address);
91   }
92 
93   SleepForever();
94 }
95