Skip to content

Commit 75fc06f

Browse files
author
Jooyung Han
committed
binder: add openDeclaredPassthroughHal()
It handles dlopen()ing a declared native hal. This can replace android_load_sphal_library to load declared native instance. The new API also supports when instances are from APEX. Bug: 316051788 Test: atest servicemanager_test Test: atest vts_treble_vintf_vendor_test Change-Id: I95b752253e0b550d38f0543ff56134d38d838855
1 parent 0e8add7 commit 75fc06f

7 files changed

Lines changed: 77 additions & 3 deletions

File tree

libs/binder/Android.bp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,24 @@ cc_defaults {
320320
"ServiceManagerHost.cpp",
321321
],
322322
},
323+
android: {
324+
shared_libs: [
325+
"libapexsupport",
326+
"libvndksupport",
327+
],
328+
},
329+
recovery: {
330+
exclude_shared_libs: [
331+
"libapexsupport",
332+
"libvndksupport",
333+
],
334+
},
335+
native_bridge: {
336+
exclude_shared_libs: [
337+
"libapexsupport",
338+
"libvndksupport",
339+
],
340+
},
323341
},
324342
cflags: [
325343
"-DBINDER_WITH_KERNEL_IPC",

libs/binder/IServiceManager.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
#include "ServiceManagerHost.h"
4141
#endif
4242

43+
#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) && !defined(__ANDROID_NATIVE_BRIDGE__)
44+
#include <android/apexsupport.h>
45+
#include <vndksupport/linker.h>
46+
#endif
47+
4348
#include "Static.h"
4449

4550
namespace android {
@@ -259,6 +264,27 @@ bool checkPermission(const String16& permission, pid_t pid, uid_t uid, bool logP
259264
}
260265
}
261266

267+
void* openDeclaredPassthroughHal(const String16& interface, const String16& instance, int flag) {
268+
#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) && !defined(__ANDROID_NATIVE_BRIDGE__)
269+
sp<IServiceManager> sm = defaultServiceManager();
270+
String16 name = interface + String16("/") + instance;
271+
if (!sm->isDeclared(name)) {
272+
return nullptr;
273+
}
274+
String16 libraryName = interface + String16(".") + instance + String16(".so");
275+
if (auto updatableViaApex = sm->updatableViaApex(name); updatableViaApex.has_value()) {
276+
return AApexSupport_loadLibrary(String8(libraryName).c_str(),
277+
String8(*updatableViaApex).c_str(), flag);
278+
}
279+
return android_load_sphal_library(String8(libraryName).c_str(), flag);
280+
#else
281+
(void)interface;
282+
(void)instance;
283+
(void)flag;
284+
return nullptr;
285+
#endif
286+
}
287+
262288
#endif //__ANDROID_VNDK__
263289

264290
// ----------------------------------------------------------------------

libs/binder/include/binder/IServiceManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ status_t getService(const String16& name, sp<INTERFACE>* outService)
207207
return NAME_NOT_FOUND;
208208
}
209209

210+
void* openDeclaredPassthroughHal(const String16& interface, const String16& instance, int flag);
211+
210212
bool checkCallingPermission(const String16& permission);
211213
bool checkCallingPermission(const String16& permission,
212214
int32_t* outPid, int32_t* outUid);

libs/binder/ndk/include_platform/android/binder_manager.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ void AServiceManager_getUpdatableApexName(const char* instance, void* context,
242242
void (*callback)(const char*, void*))
243243
__INTRODUCED_IN(__ANDROID_API_U__);
244244

245+
/**
246+
* Opens a declared passthrough HAL.
247+
*
248+
* \param instance identifier of the passthrough service (e.g. "mapper")
249+
* \param instance identifier of the implemenatation (e.g. "default")
250+
* \param flag passed to dlopen()
251+
*/
252+
void* AServiceManager_openDeclaredPassthroughHal(const char* interface, const char* instance,
253+
int flag)
254+
// TODO(b/302113279) use __INTRODUCED_LLNDK for vendor variants
255+
__INTRODUCED_IN(__ANDROID_API_V__);
256+
245257
/**
246258
* Prevent lazy services without client from shutting down their process
247259
*

libs/binder/ndk/libbinder_ndk.map.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ LIBBINDER_NDK35 { # introduced=VanillaIceCream
204204
APersistableBundle_getDoubleVectorKeys;
205205
APersistableBundle_getStringVectorKeys;
206206
APersistableBundle_getPersistableBundleKeys;
207+
AServiceManager_openDeclaredPassthroughHal; # systemapi llndk
207208
};
208209

209210
LIBBINDER_NDK_PLATFORM {

libs/binder/ndk/service_manager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ void AServiceManager_getUpdatableApexName(const char* instance, void* context,
200200
callback(String8(updatableViaApex.value()).c_str(), context);
201201
}
202202
}
203+
void* AServiceManager_openDeclaredPassthroughHal(const char* interface, const char* instance,
204+
int flag) {
205+
LOG_ALWAYS_FATAL_IF(interface == nullptr, "interface == nullptr");
206+
LOG_ALWAYS_FATAL_IF(instance == nullptr, "instance == nullptr");
207+
208+
return openDeclaredPassthroughHal(String16(interface), String16(instance), flag);
209+
}
203210
void AServiceManager_forceLazyServicesPersist(bool persist) {
204211
auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
205212
serviceRegistrar.forcePersist(persist);

libs/ui/Gralloc5.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,18 @@ static void *loadIMapperLibrary() {
8383
return nullptr;
8484
}
8585

86-
std::string lib_name = "mapper." + mapperSuffix + ".so";
87-
void *so = android_load_sphal_library(lib_name.c_str(), RTLD_LOCAL | RTLD_NOW);
86+
void* so = nullptr;
87+
// TODO(b/322384429) switch this to __ANDROID_API_V__ when V is finalized
88+
// TODO(b/302113279) use __ANDROID_VENDOR_API__ for vendor variant
89+
if (__builtin_available(android __ANDROID_API_FUTURE__, *)) {
90+
so = AServiceManager_openDeclaredPassthroughHal("mapper", mapperSuffix.c_str(),
91+
RTLD_LOCAL | RTLD_NOW);
92+
} else {
93+
std::string lib_name = "mapper." + mapperSuffix + ".so";
94+
so = android_load_sphal_library(lib_name.c_str(), RTLD_LOCAL | RTLD_NOW);
95+
}
8896
if (!so) {
89-
ALOGE("Failed to load %s", lib_name.c_str());
97+
ALOGE("Failed to load mapper.%s.so", mapperSuffix.c_str());
9098
}
9199
return so;
92100
}();

0 commit comments

Comments
 (0)