1 /*
2 *
3 * Copyright 2023 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include <gflags/gflags.h>
24 #include <grpcpp/ext/proto_server_reflection_plugin.h>
25 #include <grpcpp/grpcpp.h>
26 #include <grpcpp/health_check_service_interface.h>
27 #include <json/json.h>
28
29 #include "common/libs/utils/files.h"
30 #include "common/libs/utils/result.h"
31 #include "control_env_proxy.grpc.pb.h"
32 #include "host/libs/control_env/grpc_service_handler.h"
33
34 using controlenvproxyserver::CallUnaryMethodReply;
35 using controlenvproxyserver::CallUnaryMethodRequest;
36 using controlenvproxyserver::ControlEnvProxyService;
37 using controlenvproxyserver::ListMethodsReply;
38 using controlenvproxyserver::ListMethodsRequest;
39 using controlenvproxyserver::ListReqResTypeReply;
40 using controlenvproxyserver::ListReqResTypeRequest;
41 using controlenvproxyserver::ListServicesReply;
42 using controlenvproxyserver::TypeInformationReply;
43 using controlenvproxyserver::TypeInformationRequest;
44 using google::protobuf::Empty;
45 using google::protobuf::RepeatedPtrField;
46 using grpc::Server;
47 using grpc::ServerBuilder;
48 using grpc::ServerContext;
49 using grpc::Status;
50 using grpc::StatusCode;
51
52 DEFINE_string(grpc_uds_path, "", "grpc_uds_path");
53 DEFINE_string(grpc_socket_path, "", "The path of gRPC sockets");
54
55 class ControlEnvProxyServiceImpl final
56 : public ControlEnvProxyService::Service {
57 public:
CallUnaryMethod(ServerContext * context,const CallUnaryMethodRequest * request,CallUnaryMethodReply * reply)58 Status CallUnaryMethod(ServerContext* context,
59 const CallUnaryMethodRequest* request,
60 CallUnaryMethodReply* reply) override {
61 std::vector<std::string> args{request->service_name(),
62 request->method_name(),
63 request->json_formatted_proto()};
64 auto result = cuttlefish::HandleCmds(FLAGS_grpc_socket_path, "call", args);
65 if (!TypeIsSuccess(result)) {
66 return Status(StatusCode::FAILED_PRECONDITION,
67 "Calling gRPC method failed");
68 }
69 reply->set_json_formatted_proto(*result);
70
71 return Status::OK;
72 }
73
ListServices(ServerContext * context,const Empty * request,ListServicesReply * reply)74 Status ListServices(ServerContext* context, const Empty* request,
75 ListServicesReply* reply) override {
76 std::vector<std::string> args;
77 auto result = cuttlefish::HandleCmds(FLAGS_grpc_socket_path, "ls", args);
78 if (!TypeIsSuccess(result)) {
79 return Status(StatusCode::FAILED_PRECONDITION,
80 "Listing gRPC services failed");
81 }
82
83 Json::Value value;
84 if (!reader.parse(*result, value)) {
85 return parsing_json_failure_status;
86 }
87 if (!value["services"].isArray()) {
88 return parsing_json_failure_status;
89 }
90 for (auto& service : value["services"]) {
91 if (!service.isString()) {
92 return parsing_json_failure_status;
93 }
94 reply->add_services(service.asString());
95 }
96
97 return Status::OK;
98 }
99
ListMethods(ServerContext * context,const ListMethodsRequest * request,ListMethodsReply * reply)100 Status ListMethods(ServerContext* context, const ListMethodsRequest* request,
101 ListMethodsReply* reply) override {
102 std::vector<std::string> args{request->service_name()};
103 auto result = cuttlefish::HandleCmds(FLAGS_grpc_socket_path, "ls", args);
104 if (!TypeIsSuccess(result)) {
105 return Status(StatusCode::FAILED_PRECONDITION,
106 "Listing gRPC methods failed");
107 }
108
109 Json::Value value;
110 if (!reader.parse(*result, value)) {
111 return parsing_json_failure_status;
112 }
113 if (!value["methods"].isArray()) {
114 return parsing_json_failure_status;
115 }
116 for (auto& method : value["methods"]) {
117 if (!method.isString()) {
118 return parsing_json_failure_status;
119 }
120 reply->add_methods(method.asString());
121 }
122
123 return Status::OK;
124 }
125
ListReqResType(ServerContext * context,const ListReqResTypeRequest * request,ListReqResTypeReply * reply)126 Status ListReqResType(ServerContext* context,
127 const ListReqResTypeRequest* request,
128 ListReqResTypeReply* reply) override {
129 std::vector<std::string> args{request->service_name(),
130 request->method_name()};
131 auto result = cuttlefish::HandleCmds(FLAGS_grpc_socket_path, "ls", args);
132 if (!TypeIsSuccess(result)) {
133 return Status(StatusCode::FAILED_PRECONDITION,
134 "Listing gRPC request and response message type failed");
135 }
136
137 Json::Value value;
138 if (!reader.parse(*result, value)) {
139 return parsing_json_failure_status;
140 }
141 if (!value["request_type"].isString() ||
142 !value["response_type"].isString()) {
143 return parsing_json_failure_status;
144 }
145 reply->set_request_type_name(value["request_type"].asString());
146 reply->set_response_type_name(value["response_type"].asString());
147
148 return Status::OK;
149 }
150
TypeInformation(ServerContext * context,const TypeInformationRequest * request,TypeInformationReply * reply)151 Status TypeInformation(ServerContext* context,
152 const TypeInformationRequest* request,
153 TypeInformationReply* reply) override {
154 std::vector<std::string> args{request->service_name(),
155 request->type_name()};
156 auto result = cuttlefish::HandleCmds(FLAGS_grpc_socket_path, "type", args);
157 if (!TypeIsSuccess(result)) {
158 return Status(StatusCode::FAILED_PRECONDITION,
159 "Calling gRPC method failed");
160 }
161 reply->set_text_formatted_type_info(*result);
162
163 return Status::OK;
164 }
165
166 private:
167 template <typename T>
ToVector(const RepeatedPtrField<T> & repeated_field)168 std::vector<T> ToVector(const RepeatedPtrField<T>& repeated_field) {
169 std::vector<T> vec;
170 for (const auto& value : repeated_field) {
171 vec.push_back(value);
172 }
173 return vec;
174 }
175
176 Json::Reader reader;
177 Status parsing_json_failure_status = Status(
178 StatusCode::FAILED_PRECONDITION, "Parsing result into json failed");
179 };
180
RunServer()181 void RunServer() {
182 std::string server_address("unix:" + FLAGS_grpc_uds_path);
183 ControlEnvProxyServiceImpl service;
184
185 grpc::EnableDefaultHealthCheckService(true);
186 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
187 ServerBuilder builder;
188 // Listen on the given address without any authentication mechanism.
189 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
190 // Register "service" as the instance through which we'll communicate with
191 // clients. In this case it corresponds to an *synchronous* service.
192 builder.RegisterService(&service);
193 // Finally assemble the server.
194 std::unique_ptr<Server> server(builder.BuildAndStart());
195 std::cout << "Server listening on " << server_address << std::endl;
196
197 // Let the socket for this server as writable
198 auto change_group_result =
199 cuttlefish::ChangeGroup(FLAGS_grpc_uds_path, "cvdnetwork");
200 if (!TypeIsSuccess(change_group_result)) {
201 std::cout << "Failed ChangeGroup " << FLAGS_grpc_uds_path << std::endl;
202 }
203 int chmod_result = chmod(FLAGS_grpc_uds_path.c_str(), 0775);
204 if (chmod_result) {
205 std::cout << "Failed chmod 775 " << FLAGS_grpc_uds_path << std::endl;
206 }
207
208 // Wait for the server to shutdown. Note that some other thread must be
209 // responsible for shutting down the server for this call to ever return.
210 server->Wait();
211 }
212
main(int argc,char ** argv)213 int main(int argc, char** argv) {
214 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
215 RunServer();
216
217 return 0;
218 }