xref: /aosp_15_r20/system/libhidl/transport/allocator/1.0/default/service.cpp (revision 8222fbe171c3d6fadfe95119c180cf3010c392a8)
1 #define LOG_TAG "[email protected]"
2 
3 #include "AshmemAllocator.h"
4 
5 #include <android-base/logging.h>
6 #include <android/hidl/allocator/1.0/IAllocator.h>
7 #include <android/hidl/manager/1.2/IServiceManager.h>
8 #include <cutils/properties.h>
9 #include <hidl/HidlTransportSupport.h>
10 
11 using android::sp;
12 using android::status_t;
13 using android::hardware::configureRpcThreadpool;
14 using android::hardware::joinRpcThreadpool;
15 using android::hidl::allocator::V1_0::IAllocator;
16 using android::hidl::allocator::V1_0::implementation::AshmemAllocator;
17 using android::hidl::manager::V1_2::IServiceManager;
18 
19 static constexpr char kInstanceName[] = "ashmem";
20 
main()21 int main() {
22     configureRpcThreadpool(1, true /* callerWillJoin */);
23 
24     sp<IAllocator> allocator = new AshmemAllocator();
25 
26     IServiceManager::Transport transport =
27             android::hardware::defaultServiceManager1_2()->getTransport(IAllocator::descriptor,
28                                                                         kInstanceName);
29     if (transport == IServiceManager::Transport::HWBINDER) {
30         status_t status = allocator->registerAsService(kInstanceName);
31         if (android::OK != status) {
32             LOG(FATAL) << "Unable to register allocator service: " << status;
33             return -1;
34         }
35     } else {
36         LOG(INFO) << IAllocator::descriptor << "/" << kInstanceName
37                   << " is not registered in the VINTF manifest as it is deprecated.";
38         // The transport won't change at run time, so make sure we don't restart
39         int rc = property_set("hidl_memory.disabled", "true");
40         if (rc) {
41             LOG_ALWAYS_FATAL("Failed to set \"hidl_memory.disabled\" (error %d).\"", rc);
42         }
43         return 0;
44     }
45 
46     joinRpcThreadpool();
47 
48     return -1;
49 }
50