1 /*
2 * Copyright (C) 2022 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 #define TLOG_TAG "binderRpcTestService"
18
19 #include <binder/RpcServerTrusty.h>
20 #include <inttypes.h>
21 #include <lib/tipc/tipc.h>
22 #include <lk/err_ptr.h>
23 #include <stdio.h>
24 #include <trusty_log.h>
25 #include <vector>
26
27 #include "binderRpcTestCommon.h"
28
29 using namespace android;
30 using binder::Status;
31
32 static int gConnectionCounter = 0;
33
34 class MyBinderRpcTestTrusty : public MyBinderRpcTestDefault {
35 public:
36 wp<RpcServerTrusty> server;
37
countBinders(std::vector<int32_t> * out)38 Status countBinders(std::vector<int32_t>* out) override {
39 return countBindersImpl(server, out);
40 }
41
scheduleShutdown()42 Status scheduleShutdown() override {
43 // TODO: Trusty does not support shutting down the tipc event loop,
44 // so we just terminate the service app since it is marked
45 // restart_on_exit
46 exit(EXIT_SUCCESS);
47 }
48
49 // TODO(b/242940548): implement echoAsFile and concatFiles
50 };
51
52 struct ServerInfo {
53 std::unique_ptr<std::string> port;
54 sp<RpcServerTrusty> server;
55 };
56
main(void)57 int main(void) {
58 TLOGI("Starting service\n");
59
60 tipc_hset* hset = tipc_hset_create();
61 if (IS_ERR(hset)) {
62 TLOGE("Failed to create handle set (%d)\n", PTR_ERR(hset));
63 return EXIT_FAILURE;
64 }
65
66 const auto port_acl = RpcServerTrusty::PortAcl{
67 .flags = IPC_PORT_ALLOW_NS_CONNECT | IPC_PORT_ALLOW_TA_CONNECT,
68 };
69
70 std::vector<ServerInfo> servers;
71 for (auto serverVersion : testVersions()) {
72 ServerInfo serverInfo{
73 .port = std::make_unique<std::string>(trustyIpcPort(serverVersion)),
74 };
75 TLOGI("Adding service port '%s'\n", serverInfo.port->c_str());
76
77 // Message size needs to be large enough to cover all messages sent by the
78 // tests: SendAndGetResultBackBig sends two large strings.
79 constexpr size_t max_msg_size = 4096;
80 auto server =
81 RpcServerTrusty::make(hset, serverInfo.port->c_str(),
82 std::shared_ptr<const RpcServerTrusty::PortAcl>(&port_acl),
83 max_msg_size);
84 if (server == nullptr) {
85 return EXIT_FAILURE;
86 }
87
88 serverInfo.server = server;
89 if (!serverInfo.server->setProtocolVersion(serverVersion)) {
90 return EXIT_FAILURE;
91 }
92 serverInfo.server->setPerSessionRootObject(
93 [=](wp<RpcSession> /*session*/, const void* /*addrPtr*/, size_t /*len*/) {
94 auto service = sp<MyBinderRpcTestTrusty>::make();
95 // Assign a unique connection identifier to service->port so
96 // getClientPort returns a unique value per connection
97 service->port = ++gConnectionCounter;
98 service->server = server;
99 return service;
100 });
101
102 servers.push_back(std::move(serverInfo));
103 }
104
105 return tipc_run_event_loop(hset);
106 }
107