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