Merge "servicemanager: Overwrite old service when a new one is registered"
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index ec97f1b..78c09f2 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -76,6 +76,7 @@
#include <hardware_legacy/power.h>
#include <hidl/ServiceManagement.h>
#include <log/log.h>
+#include <log/log_read.h>
#include <openssl/sha.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>
@@ -634,14 +635,24 @@
static const long MINIMUM_LOGCAT_TIMEOUT_MS = 50000;
-/* timeout in ms to read a list of buffers */
+// Returns the actual readable size of the given buffer or -1 on error.
+static long logcat_buffer_readable_size(const std::string& buffer) {
+ std::unique_ptr<logger_list, decltype(&android_logger_list_free)> logger_list{
+ android_logger_list_alloc(0, 0, 0), &android_logger_list_free};
+ auto logger = android_logger_open(logger_list.get(), android_name_to_log_id(buffer.c_str()));
+
+ return android_logger_get_log_readable_size(logger);
+}
+
+// Returns timeout in ms to read a list of buffers.
static unsigned long logcat_timeout(const std::vector<std::string>& buffers) {
unsigned long timeout_ms = 0;
for (const auto& buffer : buffers) {
- log_id_t id = android_name_to_log_id(buffer.c_str());
- unsigned long property_size = __android_logger_get_buffer_size(id);
- /* Engineering margin is ten-fold our guess */
- timeout_ms += 10 * (property_size + worst_write_perf) / worst_write_perf;
+ long readable_size = logcat_buffer_readable_size(buffer);
+ if (readable_size > 0) {
+ // Engineering margin is ten-fold our guess.
+ timeout_ms += 10 * (readable_size + worst_write_perf) / worst_write_perf;
+ }
}
return timeout_ms > MINIMUM_LOGCAT_TIMEOUT_MS ? timeout_ms : MINIMUM_LOGCAT_TIMEOUT_MS;
}
diff --git a/libs/binder/include/binder/LazyServiceRegistrar.h b/libs/binder/include/binder/LazyServiceRegistrar.h
index 6d711bc..d18c88e 100644
--- a/libs/binder/include/binder/LazyServiceRegistrar.h
+++ b/libs/binder/include/binder/LazyServiceRegistrar.h
@@ -26,7 +26,19 @@
class ClientCounterCallback;
} // namespace internal
-/** Exits when all services registered through this object have 0 clients */
+/**
+ * Exits when all services registered through this object have 0 clients
+ *
+ * In order to use this class, it's expected that your service:
+ * - registers all services in the process with this API
+ * - configures services as oneshot in init .rc files
+ * - configures services as disabled in init.rc files, unless a client is
+ * guaranteed early in boot, in which case, forcePersist should also be used
+ * to avoid races.
+ * - uses 'interface' declarations in init .rc files
+ *
+ * For more information on init .rc configuration, see system/core/init/README.md
+ **/
class LazyServiceRegistrar {
public:
static LazyServiceRegistrar& getInstance();
@@ -47,4 +59,4 @@
};
} // namespace binder
-} // namespace android
\ No newline at end of file
+} // namespace android
diff --git a/libs/binder/ndk/include_platform/android/binder_manager.h b/libs/binder/ndk/include_platform/android/binder_manager.h
index 055c79b..52bcd20 100644
--- a/libs/binder/ndk/include_platform/android/binder_manager.h
+++ b/libs/binder/ndk/include_platform/android/binder_manager.h
@@ -18,6 +18,7 @@
#include <android/binder_ibinder.h>
#include <android/binder_status.h>
+#include <sys/cdefs.h>
__BEGIN_DECLS
@@ -50,4 +51,47 @@
*/
__attribute__((warn_unused_result)) AIBinder* AServiceManager_getService(const char* instance);
+/**
+ * Registers a lazy service with the default service manager under the 'instance' name.
+ * Does not take ownership of binder.
+ * The service must be configured statically with init so it can be restarted with
+ * ctl.interface.* messages from servicemanager.
+ * AServiceManager_registerLazyService cannot safely be used with AServiceManager_addService
+ * in the same process. If one service is registered with AServiceManager_registerLazyService,
+ * the entire process will have its lifetime controlled by servicemanager.
+ * Instead, all services in the process should be registered using
+ * AServiceManager_registerLazyService.
+ *
+ * \param binder object to register globally with the service manager.
+ * \param instance identifier of the service. This will be used to lookup the service.
+ *
+ * \return STATUS_OK on success.
+ */
+binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance)
+ __INTRODUCED_IN(31);
+
+/**
+ * Gets a binder object with this specific instance name. Efficiently waits for the service.
+ * If the service is not declared, it will wait indefinitely. Requires the threadpool
+ * to be started in the service.
+ * This also implicitly calls AIBinder_incStrong (so the caller of this function is responsible
+ * for calling AIBinder_decStrong).
+ *
+ * \param instance identifier of the service used to lookup the service.
+ *
+ * \return service if registered, null if not.
+ */
+__attribute__((warn_unused_result)) AIBinder* AServiceManager_waitForService(const char* instance)
+ __INTRODUCED_IN(31);
+
+/**
+ * Check if a service is declared (e.g. VINTF manifest).
+ *
+ * \param instance identifier of the service.
+ *
+ * \return true on success, meaning AServiceManager_waitForService should always
+ * be able to return the service.
+ */
+bool AServiceManager_isDeclared(const char* instance) __INTRODUCED_IN(31);
+
__END_DECLS
diff --git a/libs/binder/ndk/libbinder_ndk.map.txt b/libs/binder/ndk/libbinder_ndk.map.txt
index d435382..1701fb5 100644
--- a/libs/binder/ndk/libbinder_ndk.map.txt
+++ b/libs/binder/ndk/libbinder_ndk.map.txt
@@ -117,6 +117,9 @@
ABinderProcess_setupPolling; # apex
AIBinder_getCallingSid; # apex
AIBinder_setRequestingSid; # apex
+ AServiceManager_isDeclared; # apex llndk
+ AServiceManager_registerLazyService; # llndk
+ AServiceManager_waitForService; # apex llndk
};
LIBBINDER_NDK_PLATFORM {
diff --git a/libs/binder/ndk/service_manager.cpp b/libs/binder/ndk/service_manager.cpp
index d0b166d..6b2184e 100644
--- a/libs/binder/ndk/service_manager.cpp
+++ b/libs/binder/ndk/service_manager.cpp
@@ -20,6 +20,7 @@
#include "status_internal.h"
#include <binder/IServiceManager.h>
+#include <binder/LazyServiceRegistrar.h>
using ::android::defaultServiceManager;
using ::android::IBinder;
@@ -61,3 +62,33 @@
AIBinder_incStrong(ret.get());
return ret.get();
}
+binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) {
+ if (binder == nullptr || instance == nullptr) {
+ return STATUS_UNEXPECTED_NULL;
+ }
+
+ auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance();
+ status_t status = serviceRegistrar.registerService(binder->getBinder(), instance);
+
+ return PruneStatusT(status);
+}
+AIBinder* AServiceManager_waitForService(const char* instance) {
+ if (instance == nullptr) {
+ return nullptr;
+ }
+
+ sp<IServiceManager> sm = defaultServiceManager();
+ sp<IBinder> binder = sm->waitForService(String16(instance));
+
+ sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
+ AIBinder_incStrong(ret.get());
+ return ret.get();
+}
+bool AServiceManager_isDeclared(const char* instance) {
+ if (instance == nullptr) {
+ return false;
+ }
+
+ sp<IServiceManager> sm = defaultServiceManager();
+ return sm->isDeclared(String16(instance));
+}
diff --git a/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp b/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp
index 9b2fcf0..332b4ca 100644
--- a/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp
+++ b/libs/binder/ndk/tests/libbinder_ndk_unit_test.cpp
@@ -43,6 +43,7 @@
constexpr char kExistingNonNdkService[] = "SurfaceFlinger";
constexpr char kBinderNdkUnitTestService[] = "BinderNdkUnitTest";
+constexpr char kLazyBinderNdkUnitTestService[] = "LazyBinderNdkUnitTest";
class MyBinderNdkUnitTest : public aidl::BnBinderNdkUnitTest {
ndk::ScopedAStatus repeatInt(int32_t in, int32_t* out) {
@@ -143,6 +144,27 @@
return 1;
}
+int lazyService(const char* instance) {
+ ABinderProcess_setThreadPoolMaxThreadCount(0);
+ // Wait to register this service to make sure the main test process will
+ // actually wait for the service to be available. Tested with sleep(60),
+ // and reduced for sake of time.
+ sleep(1);
+ // Strong reference to MyBinderNdkUnitTest kept by service manager.
+ // This is just for testing, it has no corresponding init behavior.
+ auto service = ndk::SharedRefBase::make<MyBinderNdkUnitTest>();
+ auto binder = service->asBinder();
+
+ binder_status_t status = AServiceManager_registerLazyService(binder.get(), instance);
+ if (status != STATUS_OK) {
+ LOG(FATAL) << "Could not register: " << status << " " << instance;
+ }
+
+ ABinderProcess_joinThreadPool();
+
+ return 1; // should not return
+}
+
// This is too slow
// TEST(NdkBinder, GetServiceThatDoesntExist) {
// sp<IFoo> foo = IFoo::getService("asdfghkl;");
@@ -171,6 +193,33 @@
EXPECT_EQ(2, out);
}
+TEST(NdkBinder, GetLazyService) {
+ // Not declared in the vintf manifest
+ ASSERT_FALSE(AServiceManager_isDeclared(kLazyBinderNdkUnitTestService));
+ ndk::SpAIBinder binder(AServiceManager_waitForService(kLazyBinderNdkUnitTestService));
+ std::shared_ptr<aidl::IBinderNdkUnitTest> service =
+ aidl::IBinderNdkUnitTest::fromBinder(binder);
+ ASSERT_NE(service, nullptr);
+
+ EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
+}
+
+// This is too slow
+TEST(NdkBinder, CheckLazyServiceShutDown) {
+ ndk::SpAIBinder binder(AServiceManager_waitForService(kLazyBinderNdkUnitTestService));
+ std::shared_ptr<aidl::IBinderNdkUnitTest> service =
+ aidl::IBinderNdkUnitTest::fromBinder(binder);
+ ASSERT_NE(service, nullptr);
+
+ EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
+ binder = nullptr;
+ service = nullptr;
+ IPCThreadState::self()->flushCommands();
+ // Make sure the service is dead after some time of no use
+ sleep(10);
+ ASSERT_EQ(nullptr, AServiceManager_checkService(kLazyBinderNdkUnitTestService));
+}
+
void LambdaOnDeath(void* cookie) {
auto onDeath = static_cast<std::function<void(void)>*>(cookie);
(*onDeath)();
@@ -477,6 +526,10 @@
}
if (fork() == 0) {
prctl(PR_SET_PDEATHSIG, SIGHUP);
+ return lazyService(kLazyBinderNdkUnitTestService);
+ }
+ if (fork() == 0) {
+ prctl(PR_SET_PDEATHSIG, SIGHUP);
return generatedService();
}
diff --git a/libs/vr/libdvr/tests/dvr_display_manager-test.cpp b/libs/vr/libdvr/tests/dvr_display_manager-test.cpp
index 7b3717e..07e2121 100644
--- a/libs/vr/libdvr/tests/dvr_display_manager-test.cpp
+++ b/libs/vr/libdvr/tests/dvr_display_manager-test.cpp
@@ -308,14 +308,6 @@
class DvrDisplayManagerTest : public Test {
protected:
void SetUp() override {
- // dvr display manager test doesn't apply to standalone vr devices because
- // tests cannot create display manager client on these devices.
- if (property_get_bool("ro.boot.vr", false)) {
- GTEST_SKIP()
- << "All tests in DvrDisplayManagerTest test case are skipped "
- "because the device boot to VR.";
- }
-
int ret;
DvrDisplayManager* display_manager;
DvrSurfaceState* surface_state;
diff --git a/libs/vr/libvrflinger/hardware_composer.cpp b/libs/vr/libvrflinger/hardware_composer.cpp
index e1240d6..57a77cf 100644
--- a/libs/vr/libvrflinger/hardware_composer.cpp
+++ b/libs/vr/libvrflinger/hardware_composer.cpp
@@ -47,7 +47,6 @@
namespace {
const char kDvrPerformanceProperty[] = "sys.dvr.performance";
-const char kDvrStandaloneProperty[] = "ro.boot.vr";
const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns";
@@ -147,8 +146,6 @@
return false;
}
- is_standalone_device_ = property_get_bool(kDvrStandaloneProperty, false);
-
request_display_callback_ = request_display_callback;
primary_display_ = GetDisplayParams(composer, primary_display_id, true);
@@ -192,8 +189,6 @@
return;
boot_finished_ = true;
post_thread_wait_.notify_one();
- if (is_standalone_device_)
- request_display_callback_(true);
}
// Update the post thread quiescent state based on idle and suspended inputs.
@@ -253,17 +248,11 @@
layers_.clear();
// Phones create a new composer client on resume and destroy it on pause.
- // Standalones only create the composer client once and then use SetPowerMode
- // to control the screen on pause/resume.
- if (!is_standalone_device_) {
- if (composer_callback_ != nullptr) {
- composer_callback_->SetVsyncService(nullptr);
- composer_callback_ = nullptr;
- }
- composer_.reset(nullptr);
- } else {
- EnableDisplay(*target_display_, false);
+ if (composer_callback_ != nullptr) {
+ composer_callback_->SetVsyncService(nullptr);
+ composer_callback_ = nullptr;
}
+ composer_.reset(nullptr);
// Trigger target-specific performance mode change.
property_set(kDvrPerformanceProperty, "idle");
@@ -574,7 +563,7 @@
surfaces_changed_ = true;
}
- if (request_display_callback_ && !is_standalone_device_)
+ if (request_display_callback_)
request_display_callback_(!display_idle);
// Set idle state based on whether there are any surfaces to handle.
@@ -759,28 +748,6 @@
VsyncEyeOffsets vsync_eye_offsets = get_vsync_eye_offsets();
- if (is_standalone_device_) {
- // First, wait until boot finishes.
- std::unique_lock<std::mutex> lock(post_thread_mutex_);
- if (PostThreadCondWait(lock, -1, [this] { return boot_finished_; })) {
- return;
- }
-
- // Then, wait until we're either leaving the quiescent state, or the boot
- // finished display off timeout expires.
- if (PostThreadCondWait(lock, kBootFinishedDisplayOffTimeoutSec,
- [this] { return !post_thread_quiescent_; })) {
- return;
- }
-
- LOG_ALWAYS_FATAL_IF(post_thread_state_ & PostThreadState::Suspended,
- "Vr flinger should own the display by now.");
- post_thread_resumed_ = true;
- post_thread_ready_.notify_all();
- if (!composer_)
- CreateComposer();
- }
-
while (1) {
ATRACE_NAME("HardwareComposer::PostThread");
diff --git a/libs/vr/libvrflinger/tests/vrflinger_test.cpp b/libs/vr/libvrflinger/tests/vrflinger_test.cpp
index 7075e88..3b449f2 100644
--- a/libs/vr/libvrflinger/tests/vrflinger_test.cpp
+++ b/libs/vr/libvrflinger/tests/vrflinger_test.cpp
@@ -42,9 +42,6 @@
// completed.
constexpr auto kVrFlingerSwitchPollInterval = std::chrono::milliseconds(50);
-// How long to wait for a device that boots to VR to have vr flinger ready.
-constexpr auto kBootVrFlingerWaitTimeout = std::chrono::seconds(30);
-
// A Binder connection to surface flinger.
class SurfaceFlingerConnection {
public:
@@ -154,11 +151,6 @@
return;
}
- // This test doesn't apply to standalone vr devices.
- if (property_get_bool("ro.boot.vr", false)) {
- return;
- }
-
auto surface_flinger_connection = SurfaceFlingerConnection::Create();
ASSERT_NE(surface_flinger_connection, nullptr);
@@ -231,31 +223,5 @@
SurfaceFlingerConnection::VrFlingerSwitchResult::kSuccess);
}
-// This test runs only on devices that boot to vr. Such a device should boot to
-// a state where vr flinger is running, and the test verifies this after a
-// delay.
-TEST(BootVrFlingerTest, BootsToVrFlinger) {
- // Exit if we are not running on a device that boots to vr.
- if (!property_get_bool("ro.boot.vr", false)) {
- return;
- }
-
- auto surface_flinger_connection = SurfaceFlingerConnection::Create();
- ASSERT_NE(surface_flinger_connection, nullptr);
-
- // Verify that vr flinger is enabled.
- ASSERT_TRUE(surface_flinger_connection->IsAlive());
- auto vr_flinger_active = surface_flinger_connection->IsVrFlingerActive();
- ASSERT_TRUE(vr_flinger_active.has_value());
-
- bool active_value = vr_flinger_active.value();
- if (!active_value) {
- // Try again, but delay up to 30 seconds.
- ASSERT_EQ(surface_flinger_connection->WaitForVrFlingerTimed(true,
- kVrFlingerSwitchPollInterval, kBootVrFlingerWaitTimeout),
- SurfaceFlingerConnection::VrFlingerSwitchResult::kSuccess);
- }
-}
-
} // namespace dvr
} // namespace android