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 #include <host/commands/kernel_log_monitor/utils.h>
30 
31 #include "common/libs/fs/shared_fd.h"
32 #include "host/frontend/adb_connector/adb_connection_maintainer.h"
33 #include "host/libs/config/cuttlefish_config.h"
34 #include "host/libs/config/logging.h"
35 
36 DEFINE_string(addresses, "", "Comma-separated list of addresses to "
37                              "'adb connect' to");
38 
39 namespace cuttlefish {
40 namespace {
LaunchConnectionMaintainerThread(const std::string & address)41 void LaunchConnectionMaintainerThread(const std::string& address) {
42   std::thread(EstablishAndMaintainConnection, address).detach();
43 }
44 
ParseAddressList(std::string ports)45 std::vector<std::string> ParseAddressList(std::string ports) {
46   std::replace(ports.begin(), ports.end(), ',', ' ');
47   std::istringstream port_stream{ports};
48   return {std::istream_iterator<std::string>{port_stream},
49           std::istream_iterator<std::string>{}};
50 }
51 
SleepForever()52 [[noreturn]] void SleepForever() {
53   while (true) {
54     sleep(std::numeric_limits<unsigned int>::max());
55   }
56 }
57 
58 }  // namespace
59 
AdbConnectorMain(int argc,char * argv[])60 int AdbConnectorMain(int argc, char* argv[]) {
61   DefaultSubprocessLogging(argv);
62   gflags::ParseCommandLineFlags(&argc, &argv, true);
63   CHECK(!FLAGS_addresses.empty()) << "Must specify --addresses flag";
64 
65   for (const auto& address : ParseAddressList(FLAGS_addresses)) {
66     LaunchConnectionMaintainerThread(address);
67   }
68 
69   SleepForever();
70 }
71 
72 }  // namespace cuttlefish
73 
main(int argc,char * argv[])74 int main(int argc, char* argv[]) {
75   return cuttlefish::AdbConnectorMain(argc, argv);
76 }
77