1 /*
2  * Copyright (C) 2020 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 <android-base/logging.h>
18 #include <gflags/gflags.h>
19 #include <json/json.h>
20 #include <unistd.h>
21 
22 #include <iostream>
23 
24 #include "common/libs/fs/shared_fd.h"
25 #include "host/libs/allocd/request.h"
26 #include "host/libs/allocd/utils.h"
27 #include "host/libs/config/logging.h"
28 
29 using namespace cuttlefish;
30 
31 DEFINE_string(socket_path, kDefaultLocation, "Socket path");
32 DEFINE_bool(id, false, "Request new UUID");
33 DEFINE_bool(ifcreate, false, "Request a new Interface");
34 DEFINE_bool(shutdown, false, "Shutdown Resource Allocation Server");
35 DEFINE_bool(stop_session, false, "Remove all resources from session");
36 DEFINE_string(ifdestroy, "", "Request an interface be destroyed");
37 DEFINE_uint32(ifid, -1, "Global Resource ID");
38 DEFINE_uint32(session, -1, "Session ID");
39 
main(int argc,char * argv[])40 int main(int argc, char* argv[]) {
41   cuttlefish::DefaultSubprocessLogging(argv);
42   google::ParseCommandLineFlags(&argc, &argv, true);
43 
44   SharedFD monitor_socket = cuttlefish::SharedFD::SocketLocalClient(
45       FLAGS_socket_path, false, SOCK_STREAM);
46   if (!monitor_socket->IsOpen()) {
47     LOG(ERROR) << "Unable to connect to launcher monitor on "
48                << FLAGS_socket_path << ": " << monitor_socket->StrError();
49     return 1;
50   }
51 
52   if (FLAGS_id) {
53     Json::Value req;
54     req["request_type"] = "allocate_id";
55     SendJsonMsg(monitor_socket, req);
56 
57     auto resp_opt = RecvJsonMsg(monitor_socket);
58     if (!resp_opt.has_value()) {
59       std::cout << "Bad Response from server\n";
60       return -1;
61     }
62 
63     auto resp = resp_opt.value();
64     std::cout << resp << "\n";
65     std::cout << "New ID operation: " << resp["request_status"] << std::endl;
66     std::cout << "New ID: " << resp["id"] << std::endl;
67   }
68 
69   Json::Value config;
70   Json::Value request_list;
71 
72   if (FLAGS_ifcreate) {
73     Json::Value req;
74     req["request_type"] = "create_interface";
75     req["uid"] = geteuid();
76     req["iface_type"] = "mtap";
77     request_list.append(req);
78     req["iface_type"] = "wtap";
79     request_list.append(req);
80     req["iface_type"] = "wifiap";
81     request_list.append(req);
82     config["config_request"]["request_list"] = request_list;
83 
84     std::cout << config << "\n";
85     SendJsonMsg(monitor_socket, config);
86 
87     auto resp_opt = RecvJsonMsg(monitor_socket);
88     if (!resp_opt.has_value()) {
89       std::cout << "Bad Response from server\n";
90       return -1;
91     }
92 
93     auto resp = resp_opt.value();
94 
95     std::cout << resp << "\n";
96     std::cout << "Create Interface operation: " << resp["request_status"]
97               << std::endl;
98     std::cout << resp["iface_name"] << std::endl;
99   }
100 
101   if (!FLAGS_ifdestroy.empty() && (FLAGS_ifid != -1) && (FLAGS_session != -1)) {
102     Json::Value req;
103     req["request_type"] = "destroy_interface";
104     req["iface_name"] = FLAGS_ifdestroy;
105     req["resource_id"] = FLAGS_ifid;
106     req["session_id"] = FLAGS_session;
107     request_list.append(req);
108     config["config_request"]["request_list"] = request_list;
109     SendJsonMsg(monitor_socket, config);
110 
111     LOG(INFO) << "Request Interface : '" << FLAGS_ifdestroy << "' be removed";
112 
113     auto resp_opt = RecvJsonMsg(monitor_socket);
114     if (!resp_opt.has_value()) {
115       std::cout << "Bad Response from server\n";
116       return -1;
117     }
118 
119     auto resp = resp_opt.value();
120 
121     std::cout << resp << "\n";
122 
123     std::cout << "Destroy Interface operation: " << resp["request_status"]
124               << std::endl;
125     std::cout << resp["iface_name"] << std::endl;
126   }
127 
128   if (FLAGS_stop_session && (FLAGS_session != -1)) {
129     Json::Value req;
130     req["request_type"] = "stop_session";
131     req["session_id"] = FLAGS_session;
132     request_list.append(req);
133     config["config_request"]["request_list"] = request_list;
134     SendJsonMsg(monitor_socket, config);
135 
136     LOG(INFO) << "Request Session : '" << FLAGS_session << "' be stopped";
137 
138     auto resp_opt = RecvJsonMsg(monitor_socket);
139     if (!resp_opt.has_value()) {
140       std::cout << "Bad Response from server\n";
141       return -1;
142     }
143 
144     auto resp = resp_opt.value();
145 
146     std::cout << resp << "\n";
147     std::cout << "Stop Session operation: " << resp["config_status"];
148   }
149 
150   if (FLAGS_shutdown) {
151     Json::Value req;
152     req["request_type"] = "shutdown";
153 
154     request_list.append(req);
155     config["config_request"]["request_list"] = request_list;
156     cuttlefish::SendJsonMsg(monitor_socket, config);
157 
158     auto resp_opt = cuttlefish::RecvJsonMsg(monitor_socket);
159     if (!resp_opt.has_value()) {
160       std::cout << "Bad Response from server\n";
161       return -1;
162     }
163 
164     auto resp = resp_opt.value();
165 
166     std::cout << resp << "\n";
167     std::cout << "Shutdown operation: " << resp["request_status"] << std::endl;
168   }
169 
170   return 0;
171 }
172