Skip to content

Commit 9bf3e87

Browse files
Merge changes from topic "no_sm_proc" into main am: 03cb1fd am: 6c0cd27
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/3365276 Change-Id: I9bc4e6ea692ea63ea257017232cce70d6d78d86d Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2 parents 8d57570 + 6c0cd27 commit 9bf3e87

7 files changed

Lines changed: 313 additions & 46 deletions

File tree

libs/binder/BackendUnifiedServiceManager.cpp

Lines changed: 121 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include "BackendUnifiedServiceManager.h"
1717

18+
#include <android-base/strings.h>
1819
#include <android/os/IAccessor.h>
1920
#include <android/os/IServiceManager.h>
2021
#include <binder/RpcSession.h>
@@ -47,6 +48,9 @@ using AidlServiceManager = android::os::IServiceManager;
4748
using android::os::IAccessor;
4849
using binder::Status;
4950

51+
static const char* kUnsupportedOpNoServiceManager =
52+
"Unsupported operation without a kernel binder servicemanager process";
53+
5054
static const char* kStaticCachableList[] = {
5155
// go/keep-sorted start
5256
"accessibility",
@@ -220,7 +224,10 @@ Status BackendUnifiedServiceManager::getService2(const ::std::string& name, os::
220224
return Status::ok();
221225
}
222226
os::Service service;
223-
Status status = mTheRealServiceManager->getService2(name, &service);
227+
Status status = Status::ok();
228+
if (mTheRealServiceManager) {
229+
status = mTheRealServiceManager->getService2(name, &service);
230+
}
224231

225232
if (status.isOk()) {
226233
status = toBinderService(name, service, _out);
@@ -237,7 +244,10 @@ Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os:
237244
return Status::ok();
238245
}
239246

240-
Status status = mTheRealServiceManager->checkService(name, &service);
247+
Status status = Status::ok();
248+
if (mTheRealServiceManager) {
249+
status = mTheRealServiceManager->checkService(name, &service);
250+
}
241251
if (status.isOk()) {
242252
status = toBinderService(name, service, _out);
243253
if (status.isOk()) {
@@ -315,66 +325,156 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name,
315325
Status BackendUnifiedServiceManager::addService(const ::std::string& name,
316326
const sp<IBinder>& service, bool allowIsolated,
317327
int32_t dumpPriority) {
318-
Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
319-
// mEnableAddServiceCache is true by default.
320-
if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
321-
return updateCache(name, service,
322-
dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
328+
if (mTheRealServiceManager) {
329+
Status status =
330+
mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority);
331+
// mEnableAddServiceCache is true by default.
332+
if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) {
333+
return updateCache(name, service,
334+
dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE);
335+
}
336+
return status;
323337
}
324-
return status;
338+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
339+
kUnsupportedOpNoServiceManager);
325340
}
326341
Status BackendUnifiedServiceManager::listServices(int32_t dumpPriority,
327342
::std::vector<::std::string>* _aidl_return) {
328-
return mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
343+
Status status = Status::ok();
344+
if (mTheRealServiceManager) {
345+
status = mTheRealServiceManager->listServices(dumpPriority, _aidl_return);
346+
}
347+
if (!status.isOk()) return status;
348+
349+
appendInjectedAccessorServices(_aidl_return);
350+
351+
return status;
329352
}
330353
Status BackendUnifiedServiceManager::registerForNotifications(
331354
const ::std::string& name, const sp<os::IServiceCallback>& callback) {
332-
return mTheRealServiceManager->registerForNotifications(name, callback);
355+
if (mTheRealServiceManager) {
356+
return mTheRealServiceManager->registerForNotifications(name, callback);
357+
}
358+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
359+
kUnsupportedOpNoServiceManager);
333360
}
334361
Status BackendUnifiedServiceManager::unregisterForNotifications(
335362
const ::std::string& name, const sp<os::IServiceCallback>& callback) {
336-
return mTheRealServiceManager->unregisterForNotifications(name, callback);
363+
if (mTheRealServiceManager) {
364+
return mTheRealServiceManager->unregisterForNotifications(name, callback);
365+
}
366+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
367+
kUnsupportedOpNoServiceManager);
337368
}
338369
Status BackendUnifiedServiceManager::isDeclared(const ::std::string& name, bool* _aidl_return) {
339-
return mTheRealServiceManager->isDeclared(name, _aidl_return);
370+
Status status = Status::ok();
371+
if (mTheRealServiceManager) {
372+
status = mTheRealServiceManager->isDeclared(name, _aidl_return);
373+
}
374+
if (!status.isOk()) return status;
375+
376+
if (!*_aidl_return) {
377+
forEachInjectedAccessorService([&](const std::string& instance) {
378+
if (name == instance) {
379+
*_aidl_return = true;
380+
}
381+
});
382+
}
383+
384+
return status;
340385
}
341386
Status BackendUnifiedServiceManager::getDeclaredInstances(
342387
const ::std::string& iface, ::std::vector<::std::string>* _aidl_return) {
343-
return mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
388+
Status status = Status::ok();
389+
if (mTheRealServiceManager) {
390+
status = mTheRealServiceManager->getDeclaredInstances(iface, _aidl_return);
391+
}
392+
if (!status.isOk()) return status;
393+
394+
forEachInjectedAccessorService([&](const std::string& instance) {
395+
// Declared instances have the format
396+
// <interface>/instance like foo.bar.ISomething/instance
397+
// If it does not have that format, consider the instance to be ""
398+
std::string_view name(instance);
399+
if (base::ConsumePrefix(&name, iface + "/")) {
400+
_aidl_return->emplace_back(name);
401+
} else if (iface == instance) {
402+
_aidl_return->push_back("");
403+
}
404+
});
405+
406+
return status;
344407
}
345408
Status BackendUnifiedServiceManager::updatableViaApex(
346409
const ::std::string& name, ::std::optional<::std::string>* _aidl_return) {
347-
return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
410+
if (mTheRealServiceManager) {
411+
return mTheRealServiceManager->updatableViaApex(name, _aidl_return);
412+
}
413+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
414+
kUnsupportedOpNoServiceManager);
348415
}
349416
Status BackendUnifiedServiceManager::getUpdatableNames(const ::std::string& apexName,
350417
::std::vector<::std::string>* _aidl_return) {
351-
return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
418+
if (mTheRealServiceManager) {
419+
return mTheRealServiceManager->getUpdatableNames(apexName, _aidl_return);
420+
}
421+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
422+
kUnsupportedOpNoServiceManager);
352423
}
353424
Status BackendUnifiedServiceManager::getConnectionInfo(
354425
const ::std::string& name, ::std::optional<os::ConnectionInfo>* _aidl_return) {
355-
return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
426+
if (mTheRealServiceManager) {
427+
return mTheRealServiceManager->getConnectionInfo(name, _aidl_return);
428+
}
429+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
430+
kUnsupportedOpNoServiceManager);
356431
}
357432
Status BackendUnifiedServiceManager::registerClientCallback(
358433
const ::std::string& name, const sp<IBinder>& service,
359434
const sp<os::IClientCallback>& callback) {
360-
return mTheRealServiceManager->registerClientCallback(name, service, callback);
435+
if (mTheRealServiceManager) {
436+
return mTheRealServiceManager->registerClientCallback(name, service, callback);
437+
}
438+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
439+
kUnsupportedOpNoServiceManager);
361440
}
362441
Status BackendUnifiedServiceManager::tryUnregisterService(const ::std::string& name,
363442
const sp<IBinder>& service) {
364-
return mTheRealServiceManager->tryUnregisterService(name, service);
443+
if (mTheRealServiceManager) {
444+
return mTheRealServiceManager->tryUnregisterService(name, service);
445+
}
446+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
447+
kUnsupportedOpNoServiceManager);
365448
}
366449
Status BackendUnifiedServiceManager::getServiceDebugInfo(
367450
::std::vector<os::ServiceDebugInfo>* _aidl_return) {
368-
return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
451+
if (mTheRealServiceManager) {
452+
return mTheRealServiceManager->getServiceDebugInfo(_aidl_return);
453+
}
454+
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
455+
kUnsupportedOpNoServiceManager);
369456
}
370457

371458
[[clang::no_destroy]] static std::once_flag gUSmOnce;
372459
[[clang::no_destroy]] static sp<BackendUnifiedServiceManager> gUnifiedServiceManager;
373460

461+
static bool hasOutOfProcessServiceManager() {
462+
#ifndef BINDER_WITH_KERNEL_IPC
463+
return false;
464+
#else
465+
#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
466+
return android::base::GetBoolProperty("servicemanager.installed", true);
467+
#else
468+
return true;
469+
#endif
470+
#endif // BINDER_WITH_KERNEL_IPC
471+
}
472+
374473
sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
375474
std::call_once(gUSmOnce, []() {
376475
#if defined(__BIONIC__) && !defined(__ANDROID_VNDK__)
377-
/* wait for service manager */ {
476+
/* wait for service manager */
477+
if (hasOutOfProcessServiceManager()) {
378478
using std::literals::chrono_literals::operator""s;
379479
using android::base::WaitForProperty;
380480
while (!WaitForProperty("servicemanager.ready", "true", 1s)) {
@@ -384,7 +484,7 @@ sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager() {
384484
#endif
385485

386486
sp<AidlServiceManager> sm = nullptr;
387-
while (sm == nullptr) {
487+
while (hasOutOfProcessServiceManager() && sm == nullptr) {
388488
sm = interface_cast<AidlServiceManager>(
389489
ProcessState::self()->getContextObject(nullptr));
390490
if (sm == nullptr) {

libs/binder/BackendUnifiedServiceManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,9 @@ class BackendUnifiedServiceManager : public android::os::BnServiceManager {
167167
sp<BackendUnifiedServiceManager> getBackendUnifiedServiceManager();
168168

169169
android::binder::Status getInjectedAccessor(const std::string& name, android::os::Service* service);
170+
void appendInjectedAccessorServices(std::vector<std::string>* list);
171+
// Do not call any other service manager APIs that might take the accessor
172+
// mutex because this will be holding it!
173+
void forEachInjectedAccessorService(const std::function<void(const std::string&)>& f);
170174

171175
} // namespace android

libs/binder/IServiceManager.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,25 @@ android::binder::Status getInjectedAccessor(const std::string& name,
304304
return android::binder::Status::ok();
305305
}
306306

307+
void appendInjectedAccessorServices(std::vector<std::string>* list) {
308+
LOG_ALWAYS_FATAL_IF(list == nullptr,
309+
"Attempted to get list of services from Accessors with nullptr");
310+
std::lock_guard<std::mutex> lock(gAccessorProvidersMutex);
311+
for (const auto& entry : gAccessorProviders) {
312+
list->insert(list->end(), entry.mProvider->instances().begin(),
313+
entry.mProvider->instances().end());
314+
}
315+
}
316+
317+
void forEachInjectedAccessorService(const std::function<void(const std::string&)>& f) {
318+
std::lock_guard<std::mutex> lock(gAccessorProvidersMutex);
319+
for (const auto& entry : gAccessorProviders) {
320+
for (const auto& instance : entry.mProvider->instances()) {
321+
f(instance);
322+
}
323+
}
324+
}
325+
307326
sp<IServiceManager> defaultServiceManager()
308327
{
309328
std::call_once(gSmOnce, []() {

libs/binder/include/binder/IServiceManager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ class LIBBINDER_EXPORTED IServiceManager : public IInterface {
8080

8181
/**
8282
* Register a service.
83+
*
84+
* Note:
85+
* This status_t return value may be an exception code from an underlying
86+
* Status type that doesn't have a representive error code in
87+
* utils/Errors.h.
88+
* One example of this is a return value of -7
89+
* (Status::Exception::EX_UNSUPPORTED_OPERATION) when the service manager
90+
* process is not installed on the device when addService is called.
8391
*/
8492
// NOLINTNEXTLINE(google-default-arguments)
8593
virtual status_t addService(const String16& name, const sp<IBinder>& service,

libs/binder/rust/src/system_only.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::ffi::{c_void, CStr, CString};
2323
use std::os::raw::c_char;
2424

2525
use libc::{sockaddr, sockaddr_un, sockaddr_vm, socklen_t};
26-
use std::sync::Arc;
26+
use std::boxed::Box;
2727
use std::{mem, ptr};
2828

2929
/// Rust wrapper around ABinderRpc_Accessor objects for RPC binder service management.
@@ -65,7 +65,7 @@ impl Accessor {
6565
where
6666
F: Fn(&str) -> Option<ConnectionInfo> + Send + Sync + 'static,
6767
{
68-
let callback: *mut c_void = Arc::into_raw(Arc::new(callback)) as *mut c_void;
68+
let callback: *mut c_void = Box::into_raw(Box::new(callback)) as *mut c_void;
6969
let inst = CString::new(instance).unwrap();
7070

7171
// Safety: The function pointer is a valid connection_info callback.
@@ -149,7 +149,7 @@ impl Accessor {
149149
/// the string within isize::MAX from the pointer. The memory must not be mutated for
150150
/// the duration of this function call and must be valid for reads from the pointer
151151
/// to the null terminator.
152-
/// - The `cookie` parameter must be the cookie for an `Arc<F>` and
152+
/// - The `cookie` parameter must be the cookie for a `Box<F>` and
153153
/// the caller must hold a ref-count to it.
154154
unsafe extern "C" fn connection_info<F>(
155155
instance: *const c_char,
@@ -162,7 +162,7 @@ impl Accessor {
162162
log::error!("Cookie({cookie:p}) or instance({instance:p}) is null!");
163163
return ptr::null_mut();
164164
}
165-
// Safety: The caller promises that `cookie` is for an Arc<F>.
165+
// Safety: The caller promises that `cookie` is for a Box<F>.
166166
let callback = unsafe { (cookie as *const F).as_ref().unwrap() };
167167

168168
// Safety: The caller in libbinder_ndk will have already verified this is a valid
@@ -207,19 +207,19 @@ impl Accessor {
207207
}
208208
}
209209

210-
/// Callback that decrements the ref-count.
210+
/// Callback that drops the `Box<F>`.
211211
/// This is invoked from C++ when a binder is unlinked.
212212
///
213213
/// # Safety
214214
///
215-
/// - The `cookie` parameter must be the cookie for an `Arc<F>` and
215+
/// - The `cookie` parameter must be the cookie for a `Box<F>` and
216216
/// the owner must give up a ref-count to it.
217217
unsafe extern "C" fn cookie_decr_refcount<F>(cookie: *mut c_void)
218218
where
219219
F: Fn(&str) -> Option<ConnectionInfo> + Send + Sync + 'static,
220220
{
221-
// Safety: The caller promises that `cookie` is for an Arc<F>.
222-
unsafe { Arc::decrement_strong_count(cookie as *const F) };
221+
// Safety: The caller promises that `cookie` is for a Box<F>.
222+
unsafe { std::mem::drop(Box::from_raw(cookie as *mut F)) };
223223
}
224224
}
225225

@@ -296,7 +296,7 @@ impl AccessorProvider {
296296
where
297297
F: Fn(&str) -> Option<Accessor> + Send + Sync + 'static,
298298
{
299-
let callback: *mut c_void = Arc::into_raw(Arc::new(provider)) as *mut c_void;
299+
let callback: *mut c_void = Box::into_raw(Box::new(provider)) as *mut c_void;
300300
let c_str_instances: Vec<CString> =
301301
instances.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect();
302302
let mut c_instances: Vec<*const c_char> =
@@ -346,7 +346,7 @@ impl AccessorProvider {
346346
log::error!("Cookie({cookie:p}) or instance({instance:p}) is null!");
347347
return ptr::null_mut();
348348
}
349-
// Safety: The caller promises that `cookie` is for an Arc<F>.
349+
// Safety: The caller promises that `cookie` is for a Box<F>.
350350
let callback = unsafe { (cookie as *const F).as_ref().unwrap() };
351351

352352
let inst = {
@@ -377,14 +377,14 @@ impl AccessorProvider {
377377
///
378378
/// # Safety
379379
///
380-
/// - The `cookie` parameter must be the cookie for an `Arc<F>` and
380+
/// - The `cookie` parameter must be the cookie for a `Box<F>` and
381381
/// the owner must give up a ref-count to it.
382382
unsafe extern "C" fn accessor_cookie_decr_refcount<F>(cookie: *mut c_void)
383383
where
384384
F: Fn(&str) -> Option<Accessor> + Send + Sync + 'static,
385385
{
386-
// Safety: The caller promises that `cookie` is for an Arc<F>.
387-
unsafe { Arc::decrement_strong_count(cookie as *const F) };
386+
// Safety: The caller promises that `cookie` is for a Box<F>.
387+
unsafe { std::mem::drop(Box::from_raw(cookie as *mut F)) };
388388
}
389389
}
390390

0 commit comments

Comments
 (0)