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 <cstdint>
18 #include <cstdlib>
19 #include <iostream>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/logging.h>
24 #include <gflags/gflags.h>
25 #include <json/value.h>
26 
27 #include "common/libs/fs/shared_fd.h"
28 #include "common/libs/utils/files.h"
29 #include "common/libs/utils/flag_parser.h"
30 #include "common/libs/utils/result.h"
31 #include "common/libs/utils/tee_logging.h"
32 #include "host/libs/command_util/runner/defs.h"
33 #include "host/libs/command_util/util.h"
34 #include "host/libs/config/cuttlefish_config.h"
35 
36 namespace cuttlefish {
37 namespace {
38 
39 struct StatusFlags {
40   std::int32_t wait_for_launcher = 5;
41   std::string instance_name;
42   bool print = false;
43   bool all_instances = false;
44   bool help_xml = false;
45 };
46 
GetFlagValues(int argc,char ** argv)47 Result<StatusFlags> GetFlagValues(int argc, char** argv) {
48   StatusFlags flag_values;
49   std::vector<Flag> flags;
50   flags.emplace_back(
51       GflagsCompatFlag("wait_for_launcher", flag_values.wait_for_launcher)
52           .Help("How many seconds to wait for the launcher to respond to the "
53                 "status command. A value of zero means wait indefinitely"));
54   flags.emplace_back(
55       GflagsCompatFlag("instance_name", flag_values.instance_name)
56           .Help("Name of the instance to check. If not "
57                 "provided, DefaultInstance is used."));
58   flags.emplace_back(GflagsCompatFlag("print", flag_values.print)
59                          .Help("If provided, prints status and instance config "
60                                "information to stdout instead of CHECK"));
61   flags.emplace_back(
62       GflagsCompatFlag("all_instances", flag_values.all_instances)
63           .Help("List all instances status and instance config information."));
64   flags.emplace_back(HelpFlag(flags));
65   flags.emplace_back(HelpXmlFlag(flags, std::cout, flag_values.help_xml));
66   flags.emplace_back(UnexpectedArgumentGuard());
67 
68   std::vector<std::string> args =
69       ArgsToVec(argc - 1, argv + 1);  // Skip argv[0]
70   CF_EXPECT(ConsumeFlags(flags, args), "Could not process command line flags.");
71   return flag_values;
72 }
73 
74 struct WebAccessUrlParam {
75   std::string sig_server_addr;
76   std::string webrtc_device_id;
77 };
CalcWebAccessUrl(const WebAccessUrlParam & web_access_url_param)78 std::string CalcWebAccessUrl(const WebAccessUrlParam& web_access_url_param) {
79   if (!FileIsSocket(web_access_url_param.sig_server_addr)) {
80     return "";
81   }
82   return std::string("https://") + "localhost" + ":" + "1443" + "/devices/" +
83          web_access_url_param.webrtc_device_id + "/files" + "/client.html";
84 }
85 
PopulateDevicesInfoFromInstance(const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance_config)86 Json::Value PopulateDevicesInfoFromInstance(
87     const CuttlefishConfig& config,
88     const CuttlefishConfig::InstanceSpecific& instance_config) {
89   Json::Value device_info;
90   std::string webrtc_device_id = instance_config.webrtc_device_id();
91   if (webrtc_device_id.empty()) {
92     webrtc_device_id = instance_config.instance_name();
93   }
94   device_info["assembly_dir"] = config.assembly_dir();
95   device_info["webrtc_device_id"] = webrtc_device_id;
96   device_info["instance_dir"] = instance_config.instance_dir();
97   // 1443 is the port of the global webrtc "operator" service
98   device_info["web_access"] =
99       CalcWebAccessUrl({.sig_server_addr = config.sig_server_address(),
100                         .webrtc_device_id = webrtc_device_id});
101   device_info["adb_serial"] = instance_config.adb_ip_and_port();
102   device_info["webrtc_port"] = std::to_string(config.sig_server_port());
103   for (int i = 0; i < instance_config.display_configs().size(); i++) {
104     device_info["displays"][i] =
105         std::to_string(instance_config.display_configs()[i].width) + " x " +
106         std::to_string(instance_config.display_configs()[i].height) + " ( " +
107         std::to_string(instance_config.display_configs()[i].dpi) + " )";
108   }
109   device_info["status"] = "Running";
110   return device_info;
111 }
112 
CvdStatusMain(const StatusFlags & flag_values)113 Result<void> CvdStatusMain(const StatusFlags& flag_values) {
114   const CuttlefishConfig* config =
115       CF_EXPECT(CuttlefishConfig::Get(), "Failed to obtain config object");
116 
117   Json::Value devices_info;
118   auto instance_names =
119       flag_values.all_instances
120           ? config->instance_names()
121           : std::vector<std::string>{flag_values.instance_name};
122   for (int index = 0; index < instance_names.size(); index++) {
123     std::string instance_name = instance_names[index];
124     auto instance_config = instance_name.empty()
125                                ? config->ForDefaultInstance()
126                                : config->ForInstanceName(instance_name);
127     SharedFD monitor_socket = CF_EXPECT(GetLauncherMonitorFromInstance(
128         instance_config, flag_values.wait_for_launcher));
129 
130     LOG(INFO) << "Requesting status for instance "
131               << instance_config.instance_name();
132     CF_EXPECT(RunLauncherAction(monitor_socket, LauncherAction::kStatus,
133                                 flag_values.wait_for_launcher));
134 
135     devices_info[index] =
136         PopulateDevicesInfoFromInstance(*config, instance_config);
137     LOG(INFO) << "run_cvd is active for instance "
138               << instance_config.instance_name();
139   }
140   if (flag_values.print) {
141     std::cout << devices_info.toStyledString();
142   }
143   return {};
144 }
145 
146 }  // namespace
147 }  // namespace cuttlefish
148 
main(int argc,char ** argv)149 int main(int argc, char** argv) {
150   ::android::base::InitLogging(argv, android::base::StderrLogger);
151   cuttlefish::Result<cuttlefish::StatusFlags> flag_result =
152       cuttlefish::GetFlagValues(argc, argv);
153   if (!flag_result.ok()) {
154     LOG(ERROR) << flag_result.error().FormatForEnv();
155     return EXIT_FAILURE;
156   }
157 
158   auto result = cuttlefish::CvdStatusMain(flag_result.value());
159   if (!result.ok()) {
160     LOG(ERROR) << result.error().FormatForEnv();
161     return EXIT_FAILURE;
162   }
163   return EXIT_SUCCESS;
164 }
165