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
80 request_list.append(req);
81 config["config_request"]["request_list"] = request_list;
82
83 std::cout << config << "\n";
84 SendJsonMsg(monitor_socket, config);
85
86 auto resp_opt = RecvJsonMsg(monitor_socket);
87 if (!resp_opt.has_value()) {
88 std::cout << "Bad Response from server\n";
89 return -1;
90 }
91
92 auto resp = resp_opt.value();
93
94 std::cout << resp << "\n";
95 std::cout << "Create Interface operation: " << resp["request_status"]
96 << std::endl;
97 std::cout << resp["iface_name"] << std::endl;
98 }
99
100 if (!FLAGS_ifdestroy.empty() && (FLAGS_ifid != -1) && (FLAGS_session != -1)) {
101 Json::Value req;
102 req["request_type"] = "destroy_interface";
103 req["iface_name"] = FLAGS_ifdestroy;
104 req["resource_id"] = FLAGS_ifid;
105 req["session_id"] = FLAGS_session;
106 request_list.append(req);
107 config["config_request"]["request_list"] = request_list;
108 SendJsonMsg(monitor_socket, config);
109
110 LOG(INFO) << "Request Interface : '" << FLAGS_ifdestroy << "' be removed";
111
112 auto resp_opt = RecvJsonMsg(monitor_socket);
113 if (!resp_opt.has_value()) {
114 std::cout << "Bad Response from server\n";
115 return -1;
116 }
117
118 auto resp = resp_opt.value();
119
120 std::cout << resp << "\n";
121
122 std::cout << "Destroy Interface operation: " << resp["request_status"]
123 << std::endl;
124 std::cout << resp["iface_name"] << std::endl;
125 }
126
127 if (FLAGS_stop_session && (FLAGS_session != -1)) {
128 Json::Value req;
129 req["request_type"] = "stop_session";
130 req["session_id"] = FLAGS_session;
131 request_list.append(req);
132 config["config_request"]["request_list"] = request_list;
133 SendJsonMsg(monitor_socket, config);
134
135 LOG(INFO) << "Request Session : '" << FLAGS_session << "' be stopped";
136
137 auto resp_opt = RecvJsonMsg(monitor_socket);
138 if (!resp_opt.has_value()) {
139 std::cout << "Bad Response from server\n";
140 return -1;
141 }
142
143 auto resp = resp_opt.value();
144
145 std::cout << resp << "\n";
146 std::cout << "Stop Session operation: " << resp["config_status"];
147 }
148
149 if (FLAGS_shutdown) {
150 Json::Value req;
151 req["request_type"] = "shutdown";
152
153 request_list.append(req);
154 config["config_request"]["request_list"] = request_list;
155 cuttlefish::SendJsonMsg(monitor_socket, config);
156
157 auto resp_opt = cuttlefish::RecvJsonMsg(monitor_socket);
158 if (!resp_opt.has_value()) {
159 std::cout << "Bad Response from server\n";
160 return -1;
161 }
162
163 auto resp = resp_opt.value();
164
165 std::cout << resp << "\n";
166 std::cout << "Shutdown operation: " << resp["request_status"] << std::endl;
167 }
168
169 return 0;
170 }
171