blob: dc9482c536949aa1ea9c54a2a104b4b773f374bf [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 Spivack37f70cc2020-01-25 02:55:04 +000056 std::string name;
Jon Spivack9f503a42019-10-22 16:49:19 -070057 bool allowIsolated;
58 int dumpFlags;
59 };
60 /**
Jon Spivack37f70cc2020-01-25 02:55:04 +000061 * Number of services that have been registered.
Jon Spivack9f503a42019-10-22 16:49:19 -070062 */
Jon Spivack37f70cc2020-01-25 02:55:04 +000063 std::vector<Service> mRegisteredServices;
Jon Spivack9f503a42019-10-22 16:49:19 -070064};
65
66bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
67 bool allowIsolated, int dumpFlags) {
68 auto manager = interface_cast<AidlServiceManager>(
69 ProcessState::self()->getContextObject(nullptr));
70
Jon Spivack37f70cc2020-01-25 02:55:04 +000071 ALOGI("Registering service %s", 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 Spivack37f70cc2020-01-25 02:55:04 +000078 if (!manager->registerClientCallback(name, service, this).isOk())
79 {
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 Spivack37f70cc2020-01-25 02:55:04 +000084 mRegisteredServices.push_back({service, name, allowIsolated, dumpFlags});
Jon Spivack9f503a42019-10-22 16:49:19 -070085
86 return true;
87}
88
89/**
90 * onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
91 * invocations could occur on different threads however.
92 */
93Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) {
94 if (clients) {
95 mNumConnectedServices++;
96 } else {
97 mNumConnectedServices--;
98 }
99
100 ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
101 mNumConnectedServices, mRegisteredServices.size(),
102 String8(service->getInterfaceDescriptor()).string(), clients);
103
104 if (mNumConnectedServices == 0) {
105 tryShutdown();
106 }
107
108 return Status::ok();
109}
110
111void ClientCounterCallback::tryShutdown() {
112 ALOGI("Trying to shut down the service. No clients in use for any service in process.");
113
114 // This makes the same assumption as IServiceManager.cpp. Could dedupe if used in more places.
115 auto manager = interface_cast<AidlServiceManager>(
116 ProcessState::self()->getContextObject(nullptr));
117
118 auto unRegisterIt = mRegisteredServices.begin();
119 for (; unRegisterIt != mRegisteredServices.end(); ++unRegisterIt) {
120 auto& entry = (*unRegisterIt);
121
Jon Spivack37f70cc2020-01-25 02:55:04 +0000122 bool success = manager->tryUnregisterService(entry.name, entry.service).isOk();
Jon Spivack9f503a42019-10-22 16:49:19 -0700123
124 if (!success) {
Jon Spivack37f70cc2020-01-25 02:55:04 +0000125 ALOGI("Failed to unregister service %s", entry.name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700126 break;
127 }
128 }
129
130 if (unRegisterIt == mRegisteredServices.end()) {
131 ALOGI("Unregistered all clients and exiting");
132 exit(EXIT_SUCCESS);
133 }
134
135 for (auto reRegisterIt = mRegisteredServices.begin(); reRegisterIt != unRegisterIt;
136 reRegisterIt++) {
137 auto& entry = (*reRegisterIt);
138
139 // re-register entry
Jon Spivack37f70cc2020-01-25 02:55:04 +0000140 if (!registerService(entry.service, entry.name, entry.allowIsolated, entry.dumpFlags)) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700141 // Must restart. Otherwise, clients will never be able to get a hold of this service.
142 ALOGE("Bad state: could not re-register services");
143 }
144 }
145}
146
147} // namespace internal
148
149LazyServiceRegistrar::LazyServiceRegistrar() {
150 mClientCC = std::make_shared<internal::ClientCounterCallback>();
151}
152
153LazyServiceRegistrar& LazyServiceRegistrar::getInstance() {
154 static auto registrarInstance = new LazyServiceRegistrar();
155 return *registrarInstance;
156}
157
158status_t LazyServiceRegistrar::registerService(const sp<IBinder>& service, const std::string& name,
159 bool allowIsolated, int dumpFlags) {
160 if (!mClientCC->registerService(service, name, allowIsolated, dumpFlags)) {
161 return UNKNOWN_ERROR;
162 }
163 return OK;
164}
165
166} // namespace hardware
167} // namespace android