blob: d4f26a1021b206a2eb7381ca3385c97cf33531b4 [file] [log] [blame]
Jon Spivack9f503a42019-10-22 16:49:19 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AidlLazyServiceRegistrar"
18
19#include <binder/LazyServiceRegistrar.h>
20#include <binder/IPCThreadState.h>
21#include <binder/IServiceManager.h>
22#include <android/os/BnClientCallback.h>
23#include <android/os/IServiceManager.h>
24#include <utils/Log.h>
25
26namespace android {
27namespace binder {
28namespace internal {
29
30using AidlServiceManager = android::os::IServiceManager;
31
32class ClientCounterCallback : public ::android::os::BnClientCallback {
33public:
Jon Spivack620d2dc2020-03-06 13:58:01 -080034 ClientCounterCallback() : mNumConnectedServices(0), mForcePersist(false) {}
Jon Spivack9f503a42019-10-22 16:49:19 -070035
36 bool registerService(const sp<IBinder>& service, const std::string& name,
37 bool allowIsolated, int dumpFlags);
38
Jon Spivack620d2dc2020-03-06 13:58:01 -080039 /**
40 * Set a flag to prevent services from automatically shutting down
41 */
42 void forcePersist(bool persist);
43
Jon Spivack9f503a42019-10-22 16:49:19 -070044protected:
45 Status onClients(const sp<IBinder>& service, bool clients) override;
46
47private:
48 /**
49 * Unregisters all services that we can. If we can't unregister all, re-register other
50 * services.
51 */
52 void tryShutdown();
53
54 /**
55 * Counter of the number of services that currently have at least one client.
56 */
57 size_t mNumConnectedServices;
58
59 struct Service {
60 sp<IBinder> service;
Jon Spivack9f503a42019-10-22 16:49:19 -070061 bool allowIsolated;
62 int dumpFlags;
63 };
64 /**
Jon Spivackd9533c22020-01-27 22:19:22 +000065 * Map of registered names and services
Jon Spivack9f503a42019-10-22 16:49:19 -070066 */
Jon Spivackd9533c22020-01-27 22:19:22 +000067 std::map<std::string, Service> mRegisteredServices;
Jon Spivack620d2dc2020-03-06 13:58:01 -080068
69 bool mForcePersist;
Jon Spivack9f503a42019-10-22 16:49:19 -070070};
71
72bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
73 bool allowIsolated, int dumpFlags) {
Jon Spivack718470e2020-02-19 19:18:21 -080074 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -070075
Jon Spivackd9533c22020-01-27 22:19:22 +000076 bool reRegister = mRegisteredServices.count(name) > 0;
77 std::string regStr = (reRegister) ? "Re-registering" : "Registering";
78 ALOGI("%s service %s", regStr.c_str(), name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -070079
80 if (!manager->addService(name.c_str(), service, allowIsolated, dumpFlags).isOk()) {
81 ALOGE("Failed to register service %s", name.c_str());
82 return false;
83 }
84
Jon Spivackd9533c22020-01-27 22:19:22 +000085 if (!manager->registerClientCallback(name, service, this).isOk()) {
86 ALOGE("Failed to add client callback for service %s", name.c_str());
87 return false;
Jon Spivack9f503a42019-10-22 16:49:19 -070088 }
89
Jon Spivackd9533c22020-01-27 22:19:22 +000090 if (!reRegister) {
91 // Only add this when a service is added for the first time, as it is not removed
92 mRegisteredServices[name] = {service, allowIsolated, dumpFlags};
93 }
Jon Spivack9f503a42019-10-22 16:49:19 -070094
95 return true;
96}
97
Jon Spivack620d2dc2020-03-06 13:58:01 -080098void ClientCounterCallback::forcePersist(bool persist) {
99 mForcePersist = persist;
100 if(!mForcePersist) {
101 // Attempt a shutdown in case the number of clients hit 0 while the flag was on
102 tryShutdown();
103 }
104}
105
Jon Spivack9f503a42019-10-22 16:49:19 -0700106/**
107 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
108 * invocations could occur on different threads however.
109 */
110Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) {
111 if (clients) {
112 mNumConnectedServices++;
113 } else {
114 mNumConnectedServices--;
115 }
116
Steven Morelandc706c3e2020-04-07 00:44:52 +0000117 // if this fails, we should switch this to keep track of clients inside
118 // of mRegisteredServices so that we know which service is double-counted.
119 LOG_ALWAYS_FATAL_IF(mNumConnectedServices > mRegisteredServices.size(),
120 "Invalid state: %zu services have clients, but we only know about %zu",
121 mNumConnectedServices, mRegisteredServices.size());
122
Jon Spivack9f503a42019-10-22 16:49:19 -0700123 ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
124 mNumConnectedServices, mRegisteredServices.size(),
125 String8(service->getInterfaceDescriptor()).string(), clients);
126
Jon Spivack620d2dc2020-03-06 13:58:01 -0800127 tryShutdown();
Jon Spivack9f503a42019-10-22 16:49:19 -0700128 return Status::ok();
129}
130
131void ClientCounterCallback::tryShutdown() {
Jon Spivack620d2dc2020-03-06 13:58:01 -0800132 if(mNumConnectedServices > 0) {
133 // Should only shut down if there are no clients
134 return;
135 }
136
137 if(mForcePersist) {
138 ALOGI("Shutdown prevented by forcePersist override flag.");
139 return;
140 }
141
Jon Spivack9f503a42019-10-22 16:49:19 -0700142 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
143
Jon Spivack718470e2020-02-19 19:18:21 -0800144 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -0700145
146 auto unRegisterIt = mRegisteredServices.begin();
147 for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
148 auto& entry = (*unRegisterIt);
149
Jon Spivackd9533c22020-01-27 22:19:22 +0000150 bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk();
151
Jon Spivack9f503a42019-10-22 16:49:19 -0700152
153 if (!success) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000154 ALOGI("Failed to unregister service %s", entry.first.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700155 break;
156 }
157 }
158
159 if (unRegisterIt == mRegisteredServices.end()) {
160 ALOGI("Unregistered all clients and exiting");
161 exit(EXIT_SUCCESS);
162 }
163
164 for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
165 reRegisterIt++) {
166 auto& entry = (*reRegisterIt);
167
168 // re-register entry
Jon Spivackd9533c22020-01-27 22:19:22 +0000169 if (!registerService(entry.second.service, entry.first, entry.second.allowIsolated,
170 entry.second.dumpFlags)) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700171 // Must restart. Otherwise, clients will never be able to get a hold of this service.
172 ALOGE("Bad state: could not re-register services");
173 }
174 }
175}
176
177} // namespace internal
178
179LazyServiceRegistrar::LazyServiceRegistrar() {
180 mClientCC = std::make_shared<internal::ClientCounterCallback>();
181}
182
183LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
184 static auto registrarInstance = new LazyServiceRegistrar();
185 return *registrarInstance;
186}
187
188status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name,
189 bool allowIsolated, int dumpFlags) {
190 if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) {
191 return UNKNOWN_ERROR;
192 }
193 return OK;
194}
195
Jon Spivack620d2dc2020-03-06 13:58:01 -0800196void LazyServiceRegistrar::forcePersist(bool persist) {
197 mClientCC->forcePersist(persist);
198}
199
Jon Spivack9f503a42019-10-22 16:49:19 -0700200} // namespace hardware
201} // namespace android