1 /*
2  * Copyright (C) 2021 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 <sys/eventfd.h>
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/tests/lazy_cb/1.0/ILazyCb.h>
21 #include <hidl/HidlLazyUtils.h>
22 #include <hidl/HidlTransportSupport.h>
23 
24 using android::OK;
25 using android::sp;
26 using android::hardware::configureRpcThreadpool;
27 using android::hardware::hidl_handle;
28 using android::hardware::joinRpcThreadpool;
29 using android::hardware::LazyServiceRegistrar;
30 using android::hardware::Return;
31 using android::hardware::tests::lazy_cb::V1_0::ILazyCb;
32 
33 class LazyCb : public ILazyCb {
34   public:
LazyCb()35     LazyCb() : mFd(-1) {}
36     void setCustomActiveServicesCallback();
37     ::android::hardware::Return<bool> setEventFd(const hidl_handle& fds);
38 
39   private:
40     int mFd;
41 };
42 
setCustomActiveServicesCallback()43 void LazyCb::setCustomActiveServicesCallback() {
44     auto lazyRegistrar = android::hardware::LazyServiceRegistrar::getInstance();
45     lazyRegistrar.setActiveServicesCallback([lazyRegistrar, this](bool hasClients) mutable -> bool {
46         if (hasClients) {
47             return false;
48         }
49 
50         if (mFd < 0) {
51             // Prevent shutdown (test will fail)
52             return true;
53         }
54 
55         // Unregister all services
56         if (!lazyRegistrar.tryUnregister()) {
57             // Prevent shutdown (test will fail)
58             return true;
59         }
60 
61         // Re-register all services
62         lazyRegistrar.reRegister();
63 
64         // Unregister again before shutdown
65         if (!lazyRegistrar.tryUnregister()) {
66             // Prevent shutdown (test will fail)
67             return true;
68         }
69 
70         // Tell the test we're shutting down
71         if (TEMP_FAILURE_RETRY(eventfd_write(mFd, /* value */ 1)) < 0) {
72             // Prevent shutdown (test will fail)
73             return true;
74         }
75 
76         exit(EXIT_SUCCESS);
77         // Unreachable
78     });
79 }
80 
setEventFd(const hidl_handle & fds)81 ::android::hardware::Return<bool> LazyCb::setEventFd(const hidl_handle& fds) {
82     mFd = dup(fds->data[0]);
83     return Return<bool>(mFd >= 0);
84 }
85 
main()86 int main() {
87     configureRpcThreadpool(1, true /*willJoin*/);
88     sp<LazyCb> service = new LazyCb();
89     service->setCustomActiveServicesCallback();
90     CHECK(OK == LazyServiceRegistrar::getInstance().registerService(service, "default"));
91     joinRpcThreadpool();
92     return EXIT_FAILURE;  // should not reach
93 }
94