blob: f064bd77cec8b2bc9801311814a56c37c6b5a7cf [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) {
67 auto manager = interface_cast<AidlServiceManager>(
68 ProcessState::self()->getContextObject(nullptr));
69
Jon Spivackd9533c22020-01-27 22:19:22 +000070 bool reRegister = mRegisteredServices.count(name) > 0;
71 std::string regStr = (reRegister) ? "Re-registering" : "Registering";
72 ALOGI("%s service %s", regStr.c_str(), name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -070073
74 if (!manager->addService(name.c_str(), service, allowIsolated, dumpFlags).isOk()) {
75 ALOGE("Failed to register service %s", name.c_str());
76 return false;
77 }
78
Jon Spivackd9533c22020-01-27 22:19:22 +000079 if (!manager->registerClientCallback(name, service, this).isOk()) {
80 ALOGE("Failed to add client callback for service %s", name.c_str());
81 return false;
Jon Spivack9f503a42019-10-22 16:49:19 -070082 }
83
Jon Spivackd9533c22020-01-27 22:19:22 +000084 if (!reRegister) {
85 // Only add this when a service is added for the first time, as it is not removed
86 mRegisteredServices[name] = {service, allowIsolated, dumpFlags};
87 }
Jon Spivack9f503a42019-10-22 16:49:19 -070088
89 return true;
90}
91
92/**
93 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
94 * invocations could occur on different threads however.
95 */
96Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) {
97 if (clients) {
98 mNumConnectedServices++;
99 } else {
100 mNumConnectedServices--;
101 }
102
103 ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
104 mNumConnectedServices, mRegisteredServices.size(),
105 String8(service->getInterfaceDescriptor()).string(), clients);
106
107 if (mNumConnectedServices == 0) {
108 tryShutdown();
109 }
110
111 return Status::ok();
112}
113
114void ClientCounterCallback::tryShutdown() {
115 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
116
117 // This makes the same assumption as IServiceManager.cpp. Could dedupe if used in more places.
118 auto manager = interface_cast<AidlServiceManager>(
119 ProcessState::self()->getContextObject(nullptr));
120
121 auto unRegisterIt = mRegisteredServices.begin();
122 for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
123 auto& entry = (*unRegisterIt);
124
Jon Spivackd9533c22020-01-27 22:19:22 +0000125 bool success = manager->tryUnregisterService(entry.first, entry.second.service).isOk();
126
Jon Spivack9f503a42019-10-22 16:49:19 -0700127
128 if (!success) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000129 ALOGI("Failed to unregister service %s", entry.first.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700130 break;
131 }
132 }
133
134 if (unRegisterIt == mRegisteredServices.end()) {
135 ALOGI("Unregistered all clients and exiting");
136 exit(EXIT_SUCCESS);
137 }
138
139 for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
140 reRegisterIt++) {
141 auto& entry = (*reRegisterIt);
142
143 // re-register entry
Jon Spivackd9533c22020-01-27 22:19:22 +0000144 if (!registerService(entry.second.service, entry.first, entry.second.allowIsolated,
145 entry.second.dumpFlags)) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700146 // Must restart. Otherwise, clients will never be able to get a hold of this service.
147 ALOGE("Bad state: could not re-register services");
148 }
149 }
150}
151
152} // namespace internal
153
154LazyServiceRegistrar::LazyServiceRegistrar() {
155 mClientCC = std::make_shared<internal::ClientCounterCallback>();
156}
157
158LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
159 static auto registrarInstance = new LazyServiceRegistrar();
160 return *registrarInstance;
161}
162
163status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name,
164 bool allowIsolated, int dumpFlags) {
165 if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) {
166 return UNKNOWN_ERROR;
167 }
168 return OK;
169}
170
171} // namespace hardware
172} // namespace android