blob: 71d8130df9fe80e301fafd7b1955faee02362e32 [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:
34 ClientCounterCallback() : mNumConnectedServices(0) {}
35
36 bool registerService(const sp<IBinder>& service, const std::string& name,
37 bool allowIsolated, int dumpFlags);
38
39protected:
40 Status onClients(const sp<IBinder>& service, bool clients) override;
41
42private:
43 /**
44 * Unregisters all services that we can. If we can't unregister all, re-register other
45 * services.
46 */
47 void tryShutdown();
48
49 /**
50 * Counter of the number of services that currently have at least one client.
51 */
52 size_t mNumConnectedServices;
53
54 struct Service {
55 sp<IBinder> service;
Jon Spivack9f503a42019-10-22 16:49:19 -070056 bool allowIsolated;
57 int dumpFlags;
58 };
59 /**
Jon Spivackd9533c22020-01-27 22:19:22 +000060 * Map of registered names and services
Jon Spivack9f503a42019-10-22 16:49:19 -070061 */
Jon Spivackd9533c22020-01-27 22:19:22 +000062 std::map<std::string, Service> mRegisteredServices;
Jon Spivack9f503a42019-10-22 16:49:19 -070063};
64
65bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
66 bool allowIsolated, int dumpFlags) {
Jon Spivack718470e2020-02-19 19:18:21 -080067 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -070068
Jon Spivackd9533c22020-01-27 22:19:22 +000069 bool reRegister = mRegisteredServices.count(name) > 0;
70 std::string regStr = (reRegister) ? "Re-registering" : "Registering";
71 ALOGI("%s service %s", regStr.c_str(), name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -070072
73 if (!manager->addService(name.c_str(), service, allowIsolated, dumpFlags).isOk()) {
74 ALOGE("Failed to register service %s", name.c_str());
75 return false;
76 }
77
Jon Spivackd9533c22020-01-27 22:19:22 +000078 if (!manager->registerClientCallback(name, service, this).isOk()) {
79 ALOGE("Failed to add client callback for service %s", name.c_str());
80 return false;
Jon Spivack9f503a42019-10-22 16:49:19 -070081 }
82
Jon Spivackd9533c22020-01-27 22:19:22 +000083 if (!reRegister) {
84 // Only add this when a service is added for the first time, as it is not removed
85 mRegisteredServices[name] = {service, allowIsolated, dumpFlags};
86 }
Jon Spivack9f503a42019-10-22 16:49:19 -070087
88 return true;
89}
90
91/**
92 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
93 * invocations could occur on different threads however.
94 */
95Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) {
96 if (clients) {
97 mNumConnectedServices++;
98 } else {
99 mNumConnectedServices--;
100 }
101
102 ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
103 mNumConnectedServices, mRegisteredServices.size(),
104 String8(service->getInterfaceDescriptor()).string(), clients);
105
106 if (mNumConnectedServices == 0) {
107 tryShutdown();
108 }
109
110 return Status::ok();
111}
112
113void ClientCounterCallback::tryShutdown() {
114 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
115
Jon Spivack718470e2020-02-19 19:18:21 -0800116 auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
Jon Spivack9f503a42019-10-22 16:49:19 -0700117
118 auto unRegisterIt = mRegisteredServices.begin();
119 for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
120 auto& entry = (*unRegisterIt);
121
Jon Spivackd9533c22020-01-27 22:19:22 +0000122 bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk();
123
Jon Spivack9f503a42019-10-22 16:49:19 -0700124
125 if (!success) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000126 ALOGI("Failed to unregister service %s", entry.first.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700127 break;
128 }
129 }
130
131 if (unRegisterIt == mRegisteredServices.end()) {
132 ALOGI("Unregistered all clients and exiting");
133 exit(EXIT_SUCCESS);
134 }
135
136 for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
137 reRegisterIt++) {
138 auto& entry = (*reRegisterIt);
139
140 // re-register entry
Jon Spivackd9533c22020-01-27 22:19:22 +0000141 if (!registerService(entry.second.service, entry.first, entry.second.allowIsolated,
142 entry.second.dumpFlags)) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700143 // Must restart. Otherwise, clients will never be able to get a hold of this service.
144 ALOGE("Bad state: could not re-register services");
145 }
146 }
147}
148
149} // namespace internal
150
151LazyServiceRegistrar::LazyServiceRegistrar() {
152 mClientCC = std::make_shared<internal::ClientCounterCallback>();
153}
154
155LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
156 static auto registrarInstance = new LazyServiceRegistrar();
157 return *registrarInstance;
158}
159
160status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name,
161 bool allowIsolated, int dumpFlags) {
162 if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) {
163 return UNKNOWN_ERROR;
164 }
165 return OK;
166}
167
168} // namespace hardware
169} // namespace android