Merge "Remove extra comma" am: d44e152f78
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1547614
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: If05d6b8388b7ea414e66e8bee99493de4539cd84
diff --git a/libs/binder/LazyServiceRegistrar.cpp b/libs/binder/LazyServiceRegistrar.cpp
index 325e204..bfd6942 100644
--- a/libs/binder/LazyServiceRegistrar.cpp
+++ b/libs/binder/LazyServiceRegistrar.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include "log/log_main.h"
#define LOG_TAG "AidlLazyServiceRegistrar"
#include <binder/LazyServiceRegistrar.h>
@@ -29,53 +30,86 @@
using AidlServiceManager = android::os::IServiceManager;
-class ClientCounterCallback : public ::android::os::BnClientCallback {
+class ClientCounterCallbackImpl : public ::android::os::BnClientCallback {
public:
- ClientCounterCallback() : mNumConnectedServices(0), mForcePersist(false) {}
+ ClientCounterCallbackImpl() : mNumConnectedServices(0), mForcePersist(false) {}
bool registerService(const sp<IBinder>& service, const std::string& name,
bool allowIsolated, int dumpFlags);
-
- /**
- * Set a flag to prevent services from automatically shutting down
- */
void forcePersist(bool persist);
+ void setActiveServicesCountCallback(const std::function<bool(int)>&
+ activeServicesCountCallback);
+
+ bool tryUnregister();
+
+ void reRegister();
+
protected:
Status onClients(const sp<IBinder>& service, bool clients) override;
private:
- struct Service {
- sp<IBinder> service;
- bool allowIsolated;
- int dumpFlags;
-
- // whether, based on onClients calls, we know we have a client for this
- // service or not
- bool clients = false;
- };
-
- /**
- * Looks up a service guaranteed to be registered (service from onClients).
- */
- std::map<std::string, Service>::iterator assertRegisteredService(const sp<IBinder>& service);
-
/**
* Unregisters all services that we can. If we can't unregister all, re-register other
* services.
*/
void tryShutdown();
- // count of services with clients
+ /**
+ * Try to shutdown the process, unless:
+ * - 'forcePersist' is 'true', or
+ * - The active services count callback returns 'true', or
+ * - Some services have clients.
+ */
+ void maybeTryShutdown();
+
+ /*
+ * Counter of the number of services that currently have at least one client.
+ */
size_t mNumConnectedServices;
- // map of registered names and services
+ struct Service {
+ sp<IBinder> service;
+ bool allowIsolated;
+ int dumpFlags;
+
+ bool registered = true;
+ };
+ /**
+ * Map of registered names and services
+ */
std::map<std::string, Service> mRegisteredServices;
bool mForcePersist;
+
+ // Callback used to report the number of services with clients
+ std::function<bool(int)> mActiveServicesCountCallback;
};
-bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
+class ClientCounterCallback {
+public:
+ ClientCounterCallback();
+
+ bool registerService(const sp<IBinder>& service, const std::string& name,
+ bool allowIsolated, int dumpFlags);
+
+ /**
+ * Set a flag to prevent services from automatically shutting down
+ */
+ void forcePersist(bool persist);
+
+ void setActiveServicesCountCallback(const std::function<bool(int)>&
+ activeServicesCountCallback);
+
+ bool tryUnregister();
+
+ void reRegister();
+
+private:
+ sp<ClientCounterCallbackImpl> mImpl;
+};
+
+bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name,
bool allowIsolated, int dumpFlags) {
auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
@@ -95,32 +129,68 @@
}
// Only add this when a service is added for the first time, as it is not removed
- mRegisteredServices[name] = {
- .service = service,
- .allowIsolated = allowIsolated,
- .dumpFlags = dumpFlags
- };
+ mRegisteredServices[name] = {service, allowIsolated, dumpFlags};
}
return true;
}
-std::map<std::string, ClientCounterCallback::Service>::iterator ClientCounterCallback::assertRegisteredService(const sp<IBinder>& service) {
- LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service");
- for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) {
- auto const& [name, registered] = *it;
- (void) name;
- if (registered.service != service) continue;
- return it;
+void ClientCounterCallbackImpl::forcePersist(bool persist) {
+ mForcePersist = persist;
+ if (!mForcePersist && mNumConnectedServices == 0) {
+ // Attempt a shutdown in case the number of clients hit 0 while the flag was on
+ maybeTryShutdown();
}
- LOG_ALWAYS_FATAL("Got callback on service which we did not register: %s", String8(service->getInterfaceDescriptor()).c_str());
- __builtin_unreachable();
}
-void ClientCounterCallback::forcePersist(bool persist) {
- mForcePersist = persist;
- if(!mForcePersist) {
- // Attempt a shutdown in case the number of clients hit 0 while the flag was on
+bool ClientCounterCallbackImpl::tryUnregister() {
+ auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
+
+ for (auto& [name, entry] : mRegisteredServices) {
+ bool success = manager->tryUnregisterService(name, entry.service).isOk();
+
+ if (!success) {
+ ALOGI("Failed to unregister service %s", name.c_str());
+ return false;
+ }
+ entry.registered = false;
+ }
+
+ return true;
+}
+
+void ClientCounterCallbackImpl::reRegister() {
+ for (auto& [name, entry] : mRegisteredServices) {
+ // re-register entry if not already registered
+ if (entry.registered) {
+ continue;
+ }
+
+ if (!registerService(entry.service, name, entry.allowIsolated,
+ entry.dumpFlags)) {
+ // Must restart. Otherwise, clients will never be able to get a hold of this service.
+ LOG_ALWAYS_FATAL("Bad state: could not re-register services");
+ }
+
+ entry.registered = true;
+ }
+}
+
+void ClientCounterCallbackImpl::maybeTryShutdown() {
+ if (mForcePersist) {
+ ALOGI("Shutdown prevented by forcePersist override flag.");
+ return;
+ }
+
+ bool handledInCallback = false;
+ if (mActiveServicesCountCallback != nullptr) {
+ handledInCallback = mActiveServicesCountCallback(mNumConnectedServices);
+ }
+
+ // If there is no callback defined or the callback did not handle this
+ // client count change event, try to shutdown the process if its services
+ // have no clients.
+ if (!handledInCallback && mNumConnectedServices == 0) {
tryShutdown();
}
}
@@ -129,75 +199,61 @@
* onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
* invocations could occur on different threads however.
*/
-Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) {
- auto & [name, registered] = *assertRegisteredService(service);
- if (registered.clients == clients) {
- LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has "
- "notified has clients: %d", name.c_str(), registered.clients, clients);
- }
- registered.clients = clients;
-
- // update cache count of clients
- {
- size_t numWithClients = 0;
- for (const auto& [name, registered] : mRegisteredServices) {
- (void) name;
- if (registered.clients) numWithClients++;
- }
- mNumConnectedServices = numWithClients;
+Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) {
+ if (clients) {
+ mNumConnectedServices++;
+ } else {
+ mNumConnectedServices--;
}
ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
- mNumConnectedServices, mRegisteredServices.size(), name.c_str(), clients);
+ mNumConnectedServices, mRegisteredServices.size(),
+ String8(service->getInterfaceDescriptor()).string(), clients);
- tryShutdown();
+ maybeTryShutdown();
return Status::ok();
}
-void ClientCounterCallback::tryShutdown() {
- if(mNumConnectedServices > 0) {
- // Should only shut down if there are no clients
- return;
- }
+ void ClientCounterCallbackImpl::tryShutdown() {
+ ALOGI("Trying to shut down the service. No clients in use for any service in process.");
- if(mForcePersist) {
- ALOGI("Shutdown prevented by forcePersist override flag.");
- return;
- }
+ if (tryUnregister()) {
+ ALOGI("Unregistered all clients and exiting");
+ exit(EXIT_SUCCESS);
+ }
- ALOGI("Trying to shut down the service. No clients in use for any service in process.");
+ reRegister();
+}
- auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
+void ClientCounterCallbackImpl::setActiveServicesCountCallback(const std::function<bool(int)>&
+ activeServicesCountCallback) {
+ mActiveServicesCountCallback = activeServicesCountCallback;
+}
- auto unRegisterIt = mRegisteredServices.begin();
- for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
- auto& entry = (*unRegisterIt);
+ClientCounterCallback::ClientCounterCallback() {
+ mImpl = new ClientCounterCallbackImpl();
+}
- bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk();
+bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
+ bool allowIsolated, int dumpFlags) {
+ return mImpl->registerService(service, name, allowIsolated, dumpFlags);
+}
+void ClientCounterCallback::forcePersist(bool persist) {
+ mImpl->forcePersist(persist);
+}
- if (!success) {
- ALOGI("Failed to unregister service %s", entry.first.c_str());
- break;
- }
- }
+void ClientCounterCallback::setActiveServicesCountCallback(const std::function<bool(int)>&
+ activeServicesCountCallback) {
+ mImpl->setActiveServicesCountCallback(activeServicesCountCallback);
+}
- if (unRegisterIt == mRegisteredServices.end()) {
- ALOGI("Unregistered all clients and exiting");
- exit(EXIT_SUCCESS);
- }
+bool ClientCounterCallback::tryUnregister() {
+ return mImpl->tryUnregister();
+}
- for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
- reRegisterIt++) {
- auto& entry = (*reRegisterIt);
-
- // re-register entry
- if (!registerService(entry.second.service, entry.first, entry.second.allowIsolated,
- entry.second.dumpFlags)) {
- // Must restart. Otherwise, clients will never be able to get a hold of this service.
- ALOGE("Bad state: could not re-register services");
- }
- }
+void ClientCounterCallback::reRegister() {
+ mImpl->reRegister();
}
} // namespace internal
@@ -223,5 +279,18 @@
mClientCC->forcePersist(persist);
}
+void LazyServiceRegistrar::setActiveServicesCountCallback(const std::function<bool(int)>&
+ activeServicesCountCallback) {
+ mClientCC->setActiveServicesCountCallback(activeServicesCountCallback);
+}
+
+bool LazyServiceRegistrar::tryUnregister() {
+ return mClientCC->tryUnregister();
+}
+
+void LazyServiceRegistrar::reRegister() {
+ mClientCC->reRegister();
+}
+
} // namespace hardware
} // namespace android
diff --git a/libs/binder/include/binder/LazyServiceRegistrar.h b/libs/binder/include/binder/LazyServiceRegistrar.h
index d18c88e..73893c7 100644
--- a/libs/binder/include/binder/LazyServiceRegistrar.h
+++ b/libs/binder/include/binder/LazyServiceRegistrar.h
@@ -16,6 +16,8 @@
#pragma once
+#include <functional>
+
#include <binder/IServiceManager.h>
#include <binder/Status.h>
#include <utils/StrongPointer.h>
@@ -53,6 +55,40 @@
*/
void forcePersist(bool persist);
+ /**
+ * Set a callback that is executed when the total number of services with
+ * clients changes.
+ * The callback takes an argument, which is the number of registered
+ * lazy services for this process which have clients.
+ *
+ * Callback return value:
+ * - false: Default behavior for lazy services (shut down the process if there
+ * are no clients).
+ * - true: Don't shut down the process even if there are no clients.
+ *
+ * This callback gives a chance to:
+ * 1 - Perform some additional operations before exiting;
+ * 2 - Prevent the process from exiting by returning "true" from the
+ * callback.
+ *
+ * This method should be called before 'registerService' to avoid races.
+ */
+ void setActiveServicesCountCallback(const std::function<bool(int)>&
+ activeServicesCountCallback);
+
+ /**
+ * Try to unregister all services previously registered with 'registerService'.
+ * Returns 'true' if successful.
+ */
+ bool tryUnregister();
+
+ /**
+ * Re-register services that were unregistered by 'tryUnregister'.
+ * This method should be called in the case 'tryUnregister' fails
+ * (and should be called on the same thread).
+ */
+ void reRegister();
+
private:
std::shared_ptr<internal::ClientCounterCallback> mClientCC;
LazyServiceRegistrar();