blob: 4472db10c06c709f6a84ae8697bc72cb8232cc07 [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -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#include "ServiceManager.h"
18
19#include <android-base/logging.h>
Jon Spivack0d844302019-07-22 18:40:34 -070020#include <android-base/properties.h>
Jon Spivack9f503a42019-10-22 16:49:19 -070021#include <binder/BpBinder.h>
22#include <binder/IPCThreadState.h>
23#include <binder/ProcessState.h>
Steven Moreland86a17f82019-09-10 10:18:00 -070024#include <binder/Stability.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070025#include <cutils/android_filesystem_config.h>
26#include <cutils/multiuser.h>
Jon Spivack0d844302019-07-22 18:40:34 -070027#include <thread>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070028
Steven Moreland86a17f82019-09-10 10:18:00 -070029#ifndef VENDORSERVICEMANAGER
30#include <vintf/VintfObject.h>
Yifan Hong0a9b56e2021-11-30 16:45:40 -080031#ifdef __ANDROID_RECOVERY__
32#include <vintf/VintfObjectRecovery.h>
33#endif // __ANDROID_RECOVERY__
Steven Moreland86a17f82019-09-10 10:18:00 -070034#include <vintf/constants.h>
35#endif // !VENDORSERVICEMANAGER
36
Steven Moreland80e1e6d2019-06-21 12:35:59 -070037using ::android::binder::Status;
Steven Moreland86a17f82019-09-10 10:18:00 -070038using ::android::internal::Stability;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070039
40namespace android {
41
Steven Moreland86a17f82019-09-10 10:18:00 -070042#ifndef VENDORSERVICEMANAGER
Yifan Hong0a9b56e2021-11-30 16:45:40 -080043
Steven Moreland2e293aa2020-09-23 00:25:16 +000044struct ManifestWithDescription {
45 std::shared_ptr<const vintf::HalManifest> manifest;
46 const char* description;
47};
Yifan Hong0a9b56e2021-11-30 16:45:40 -080048static std::vector<ManifestWithDescription> GetManifestsWithDescription() {
49#ifdef __ANDROID_RECOVERY__
50 auto vintfObject = vintf::VintfObjectRecovery::GetInstance();
51 if (vintfObject == nullptr) {
Pawan Wagh37526162022-09-29 21:55:26 +000052 ALOGE("NULL VintfObjectRecovery!");
Yifan Hong0a9b56e2021-11-30 16:45:40 -080053 return {};
54 }
55 return {ManifestWithDescription{vintfObject->getRecoveryHalManifest(), "recovery"}};
56#else
57 auto vintfObject = vintf::VintfObject::GetInstance();
58 if (vintfObject == nullptr) {
Pawan Wagh37526162022-09-29 21:55:26 +000059 ALOGE("NULL VintfObject!");
Yifan Hong0a9b56e2021-11-30 16:45:40 -080060 return {};
61 }
62 return {ManifestWithDescription{vintfObject->getDeviceHalManifest(), "device"},
63 ManifestWithDescription{vintfObject->getFrameworkHalManifest(), "framework"}};
64#endif
65}
66
Steven Moreland2e293aa2020-09-23 00:25:16 +000067// func true -> stop search and forEachManifest will return true
68static bool forEachManifest(const std::function<bool(const ManifestWithDescription&)>& func) {
Yifan Hong0a9b56e2021-11-30 16:45:40 -080069 for (const ManifestWithDescription& mwd : GetManifestsWithDescription()) {
Steven Moreland2e293aa2020-09-23 00:25:16 +000070 if (mwd.manifest == nullptr) {
Pawan Wagh37526162022-09-29 21:55:26 +000071 ALOGE("NULL VINTF MANIFEST!: %s", mwd.description);
72 // note, we explicitly do not retry here, so that we can detect VINTF
73 // or other bugs (b/151696835)
74 continue;
Steven Moreland2e293aa2020-09-23 00:25:16 +000075 }
76 if (func(mwd)) return true;
77 }
78 return false;
79}
80
Steven Morelandedd4e072021-04-21 00:27:29 +000081struct AidlName {
82 std::string package;
83 std::string iface;
84 std::string instance;
Steven Moreland86a17f82019-09-10 10:18:00 -070085
Steven Morelandedd4e072021-04-21 00:27:29 +000086 static bool fill(const std::string& name, AidlName* aname) {
87 size_t firstSlash = name.find('/');
88 size_t lastDot = name.rfind('.', firstSlash);
89 if (firstSlash == std::string::npos || lastDot == std::string::npos) {
Pawan Wagh37526162022-09-29 21:55:26 +000090 ALOGE("VINTF HALs require names in the format type/instance (e.g. "
91 "some.package.foo.IFoo/default) but got: %s",
92 name.c_str());
Steven Morelandedd4e072021-04-21 00:27:29 +000093 return false;
94 }
95 aname->package = name.substr(0, lastDot);
96 aname->iface = name.substr(lastDot + 1, firstSlash - lastDot - 1);
97 aname->instance = name.substr(firstSlash + 1);
98 return true;
99 }
100};
101
102static bool isVintfDeclared(const std::string& name) {
103 AidlName aname;
104 if (!AidlName::fill(name, &aname)) return false;
105
106 bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
107 if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) {
Pawan Wagh37526162022-09-29 21:55:26 +0000108 ALOGI("Found %s in %s VINTF manifest.", name.c_str(), mwd.description);
Steven Morelandedd4e072021-04-21 00:27:29 +0000109 return true; // break
Steven Moreland86a17f82019-09-10 10:18:00 -0700110 }
Steven Moreland2e293aa2020-09-23 00:25:16 +0000111 return false; // continue
112 });
113
114 if (!found) {
115 // Although it is tested, explicitly rebuilding qualified name, in case it
116 // becomes something unexpected.
Pawan Wagh37526162022-09-29 21:55:26 +0000117 ALOGI("Could not find %s.%s/%s in the VINTF manifest.", aname.package.c_str(),
118 aname.iface.c_str(), aname.instance.c_str());
Steven Moreland86a17f82019-09-10 10:18:00 -0700119 }
Steven Moreland2edde8e2020-04-30 17:04:54 -0700120
Steven Moreland2e293aa2020-09-23 00:25:16 +0000121 return found;
122}
123
Steven Morelandedd4e072021-04-21 00:27:29 +0000124static std::optional<std::string> getVintfUpdatableApex(const std::string& name) {
125 AidlName aname;
126 if (!AidlName::fill(name, &aname)) return std::nullopt;
127
128 std::optional<std::string> updatableViaApex;
129
130 forEachManifest([&](const ManifestWithDescription& mwd) {
131 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
132 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
133 if (manifestInstance.package() != aname.package) return true;
134 if (manifestInstance.interface() != aname.iface) return true;
135 if (manifestInstance.instance() != aname.instance) return true;
136 updatableViaApex = manifestInstance.updatableViaApex();
137 return false; // break (libvintf uses opposite convention)
138 });
Jooyung Hance94b752022-11-14 18:55:06 +0900139 if (updatableViaApex.has_value()) return true; // break (found match)
Steven Morelandedd4e072021-04-21 00:27:29 +0000140 return false; // continue
141 });
142
143 return updatableViaApex;
144}
145
Jooyung Han76944fe2022-10-25 17:02:45 +0900146static std::vector<std::string> getVintfUpdatableInstances(const std::string& apexName) {
147 std::vector<std::string> instances;
148
149 forEachManifest([&](const ManifestWithDescription& mwd) {
150 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
151 if (manifestInstance.format() == vintf::HalFormat::AIDL &&
152 manifestInstance.updatableViaApex().has_value() &&
153 manifestInstance.updatableViaApex().value() == apexName) {
154 std::string aname = manifestInstance.package() + "." +
155 manifestInstance.interface() + "/" + manifestInstance.instance();
156 instances.push_back(aname);
157 }
Jooyung Hance94b752022-11-14 18:55:06 +0900158 return true; // continue (libvintf uses opposite convention)
Jooyung Han76944fe2022-10-25 17:02:45 +0900159 });
160 return false; // continue
161 });
162
163 return instances;
164}
165
Devin Moore5e4c2f12021-09-09 22:36:33 +0000166static std::optional<ConnectionInfo> getVintfConnectionInfo(const std::string& name) {
167 AidlName aname;
168 if (!AidlName::fill(name, &aname)) return std::nullopt;
169
170 std::optional<std::string> ip;
171 std::optional<uint64_t> port;
172 forEachManifest([&](const ManifestWithDescription& mwd) {
173 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
174 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
175 if (manifestInstance.package() != aname.package) return true;
176 if (manifestInstance.interface() != aname.iface) return true;
177 if (manifestInstance.instance() != aname.instance) return true;
178 ip = manifestInstance.ip();
179 port = manifestInstance.port();
180 return false; // break (libvintf uses opposite convention)
181 });
182 return false; // continue
183 });
184
185 if (ip.has_value() && port.has_value()) {
186 ConnectionInfo info;
187 info.ipAddress = *ip;
188 info.port = *port;
189 return std::make_optional<ConnectionInfo>(info);
190 } else {
191 return std::nullopt;
192 }
193}
194
Steven Moreland2e293aa2020-09-23 00:25:16 +0000195static std::vector<std::string> getVintfInstances(const std::string& interface) {
196 size_t lastDot = interface.rfind('.');
197 if (lastDot == std::string::npos) {
Pawan Wagh37526162022-09-29 21:55:26 +0000198 ALOGE("VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) "
199 "but got: %s",
200 interface.c_str());
Steven Moreland2e293aa2020-09-23 00:25:16 +0000201 return {};
202 }
203 const std::string package = interface.substr(0, lastDot);
204 const std::string iface = interface.substr(lastDot+1);
205
206 std::vector<std::string> ret;
207 (void)forEachManifest([&](const ManifestWithDescription& mwd) {
208 auto instances = mwd.manifest->getAidlInstances(package, iface);
209 ret.insert(ret.end(), instances.begin(), instances.end());
210 return false; // continue
211 });
212
213 return ret;
Steven Moreland86a17f82019-09-10 10:18:00 -0700214}
Steven Morelandb82b8f82019-10-28 10:52:34 -0700215
216static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) {
217 if (!Stability::requiresVintfDeclaration(binder)) {
218 return true;
219 }
220
221 return isVintfDeclared(name);
222}
Steven Moreland86a17f82019-09-10 10:18:00 -0700223#endif // !VENDORSERVICEMANAGER
224
Steven Morelandb8361902023-02-01 23:18:04 +0000225ServiceManager::Service::~Service() {
Steven Morelandcb591562023-03-06 15:53:44 +0000226 if (hasClients) {
227 // only expected to happen on process death, we don't store the service
228 // name this late (it's in the map that holds this service), but if it
229 // is happening, we might want to change 'unlinkToDeath' to explicitly
230 // clear this bit so that we can abort in other cases, where it would
231 // mean inconsistent logic in servicemanager (unexpected and tested, but
232 // the original lazy service impl here had that bug).
Steven Morelandb8361902023-02-01 23:18:04 +0000233 LOG(WARNING) << "a service was removed when there are clients";
234 }
235}
236
Steven Morelandd13f08b2019-11-18 14:23:09 -0800237ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700238// TODO(b/151696835): reenable performance hack when we solve bug, since with
239// this hack and other fixes, it is unlikely we will see even an ephemeral
240// failure when the manifest parse fails. The goal is that the manifest will
241// be read incorrectly and cause the process trying to register a HAL to
242// fail. If this is in fact an early boot kernel contention issue, then we
243// will get no failure, and by its absence, be signalled to invest more
244// effort in re-adding this performance hack.
245// #ifndef VENDORSERVICEMANAGER
246// // can process these at any times, don't want to delay first VINTF client
247// std::thread([] {
248// vintf::VintfObject::GetDeviceHalManifest();
249// vintf::VintfObject::GetFrameworkHalManifest();
250// }).detach();
251// #endif // !VENDORSERVICEMANAGER
Steven Morelandd13f08b2019-11-18 14:23:09 -0800252}
Steven Moreland130242d2019-08-26 17:41:32 -0700253ServiceManager::~ServiceManager() {
254 // this should only happen in tests
255
Jon Spivackf288b1d2019-12-19 17:15:51 -0800256 for (const auto& [name, callbacks] : mNameToRegistrationCallback) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700257 CHECK(!callbacks.empty()) << name;
258 for (const auto& callback : callbacks) {
259 CHECK(callback != nullptr) << name;
260 }
261 }
262
Steven Moreland130242d2019-08-26 17:41:32 -0700263 for (const auto& [name, service] : mNameToService) {
264 CHECK(service.binder != nullptr) << name;
265 }
266}
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700267
268Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700269 *outBinder = tryGetService(name, true);
270 // returns ok regardless of result for legacy reasons
271 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700272}
273
274Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700275 *outBinder = tryGetService(name, false);
276 // returns ok regardless of result for legacy reasons
277 return Status::ok();
278}
279
280sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700281 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700282
Jon Spivack0d844302019-07-22 18:40:34 -0700283 sp<IBinder> out;
Jon Spivack9f503a42019-10-22 16:49:19 -0700284 Service* service = nullptr;
Jon Spivack0d844302019-07-22 18:40:34 -0700285 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700286 service = &(it->second);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700287
Jon Spivack9f503a42019-10-22 16:49:19 -0700288 if (!service->allowIsolated) {
Jon Spivack0d844302019-07-22 18:40:34 -0700289 uid_t appid = multiuser_get_app_id(ctx.uid);
290 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700291
Jon Spivack0d844302019-07-22 18:40:34 -0700292 if (isIsolated) {
293 return nullptr;
294 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700295 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700296 out = service->binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700297 }
298
Steven Morelanda9fe4742019-07-18 14:45:20 -0700299 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700300 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700301 }
302
Jon Spivack0d844302019-07-22 18:40:34 -0700303 if (!out && startIfNotFound) {
304 tryStartService(name);
305 }
306
Jon Spivack9f503a42019-10-22 16:49:19 -0700307 if (out) {
Steven Morelandb8361902023-02-01 23:18:04 +0000308 // Force onClients to get sent, and then make sure the timerfd won't clear it
309 // by setting guaranteeClient again. This logic could be simplified by using
310 // a time-based guarantee. However, forcing onClients(true) to get sent
311 // right here is always going to be important for processes serving multiple
312 // lazy interfaces.
313 service->guaranteeClient = true;
314 CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
Jon Spivack9f503a42019-10-22 16:49:19 -0700315 service->guaranteeClient = true;
316 }
317
Jon Spivack0d844302019-07-22 18:40:34 -0700318 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700319}
320
Steven Moreland905e2e82019-07-17 11:05:45 -0700321bool isValidServiceName(const std::string& name) {
322 if (name.size() == 0) return false;
323 if (name.size() > 127) return false;
324
325 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700326 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700327 if (c >= 'a' && c <= 'z') continue;
328 if (c >= 'A' && c <= 'Z') continue;
329 if (c >= '0' && c <= '9') continue;
330 return false;
331 }
332
333 return true;
334}
335
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700336Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700337 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700338
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700339 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000340 return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700341 }
342
Steven Morelanda9fe4742019-07-18 14:45:20 -0700343 if (!mAccess->canAdd(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000344 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700345 }
346
347 if (binder == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000348 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700349 }
350
Steven Moreland905e2e82019-07-17 11:05:45 -0700351 if (!isValidServiceName(name)) {
Pawan Wagh37526162022-09-29 21:55:26 +0000352 ALOGE("Invalid service name: %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000353 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700354 }
355
Steven Moreland86a17f82019-09-10 10:18:00 -0700356#ifndef VENDORSERVICEMANAGER
357 if (!meetsDeclarationRequirements(binder, name)) {
358 // already logged
Steven Morelandffb905b2023-03-28 18:24:37 +0000359 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error.");
Steven Moreland86a17f82019-09-10 10:18:00 -0700360 }
361#endif // !VENDORSERVICEMANAGER
362
Devin Moore4e21def2023-02-24 21:54:14 +0000363 if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) {
364 ALOGW("Dump flag priority is not set when adding %s", name.c_str());
365 }
366
Steven Moreland88860b02019-08-12 14:24:14 -0700367 // implicitly unlinked when the binder is removed
Steven Morelandb0983182021-04-02 03:14:04 +0000368 if (binder->remoteBinder() != nullptr &&
369 binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) {
Pawan Wagh37526162022-09-29 21:55:26 +0000370 ALOGE("Could not linkToDeath when adding %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000371 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700372 }
373
Steven Moreland7ee423b2022-09-24 03:52:08 +0000374 auto it = mNameToService.find(name);
Steven Moreland79578672023-04-27 19:38:00 +0000375 bool prevClients = false;
Steven Moreland7ee423b2022-09-24 03:52:08 +0000376 if (it != mNameToService.end()) {
377 const Service& existing = it->second;
Steven Moreland79578672023-04-27 19:38:00 +0000378 prevClients = existing.hasClients;
Steven Moreland7ee423b2022-09-24 03:52:08 +0000379
380 // We could do better than this because if the other service dies, it
381 // may not have an entry here. However, this case is unlikely. We are
382 // only trying to detect when two different services are accidentally installed.
383
384 if (existing.ctx.uid != ctx.uid) {
Pawan Wagh37526162022-09-29 21:55:26 +0000385 ALOGW("Service '%s' originally registered from UID %u but it is now being registered "
386 "from UID %u. Multiple instances installed?",
387 name.c_str(), existing.ctx.uid, ctx.uid);
Steven Moreland7ee423b2022-09-24 03:52:08 +0000388 }
389
390 if (existing.ctx.sid != ctx.sid) {
Pawan Wagh37526162022-09-29 21:55:26 +0000391 ALOGW("Service '%s' originally registered from SID %s but it is now being registered "
392 "from SID %s. Multiple instances installed?",
393 name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str());
Steven Moreland7ee423b2022-09-24 03:52:08 +0000394 }
395
Pawan Wagh37526162022-09-29 21:55:26 +0000396 ALOGI("Service '%s' originally registered from PID %d but it is being registered again "
397 "from PID %d. Bad state? Late death notification? Multiple instances installed?",
398 name.c_str(), existing.ctx.debugPid, ctx.debugPid);
Steven Moreland7ee423b2022-09-24 03:52:08 +0000399 }
400
Devin Moore05ffe522020-08-06 13:58:29 -0700401 // Overwrite the old service if it exists
Steven Moreland7ee423b2022-09-24 03:52:08 +0000402 mNameToService[name] = Service{
403 .binder = binder,
404 .allowIsolated = allowIsolated,
405 .dumpPriority = dumpPriority,
Steven Moreland79578672023-04-27 19:38:00 +0000406 .hasClients = prevClients, // see b/279898063, matters if existing callbacks
407 .guaranteeClient = false, // handled below
Steven Moreland7ee423b2022-09-24 03:52:08 +0000408 .ctx = ctx,
Devin Moore05ffe522020-08-06 13:58:29 -0700409 };
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700410
Steven Moreland7ee423b2022-09-24 03:52:08 +0000411 if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) {
Steven Moreland79578672023-04-27 19:38:00 +0000412 // TODO: this is only needed once
Steven Morelandb8361902023-02-01 23:18:04 +0000413 // See also getService - handles case where client never gets the service,
414 // we want the service to quit.
415 mNameToService[name].guaranteeClient = true;
416 CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
417 mNameToService[name].guaranteeClient = true;
418
Steven Moreland27cfab02019-08-12 14:34:16 -0700419 for (const sp<IServiceCallback>& cb : it->second) {
420 // permission checked in registerForNotifications
421 cb->onRegistration(name, binder);
422 }
423 }
424
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700425 return Status::ok();
426}
427
428Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700429 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000430 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700431 }
432
433 size_t toReserve = 0;
434 for (auto const& [name, service] : mNameToService) {
435 (void) name;
436
437 if (service.dumpPriority & dumpPriority) ++toReserve;
438 }
439
440 CHECK(outList->empty());
441
442 outList->reserve(toReserve);
443 for (auto const& [name, service] : mNameToService) {
444 (void) service;
445
446 if (service.dumpPriority & dumpPriority) {
447 outList->push_back(name);
448 }
449 }
450
451 return Status::ok();
452}
453
Steven Moreland27cfab02019-08-12 14:34:16 -0700454Status ServiceManager::registerForNotifications(
455 const std::string& name, const sp<IServiceCallback>& callback) {
456 auto ctx = mAccess->getCallingContext();
457
458 if (!mAccess->canFind(ctx, name)) {
459 return Status::fromExceptionCode(Status::EX_SECURITY);
460 }
461
462 if (!isValidServiceName(name)) {
Pawan Wagh37526162022-09-29 21:55:26 +0000463 ALOGE("Invalid service name: %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000464 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700465 }
466
467 if (callback == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000468 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null callback.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700469 }
470
Steven Morelandb0983182021-04-02 03:14:04 +0000471 if (OK !=
472 IInterface::asBinder(callback)->linkToDeath(
473 sp<ServiceManager>::fromExisting(this))) {
Pawan Wagh37526162022-09-29 21:55:26 +0000474 ALOGE("Could not linkToDeath when adding %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000475 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700476 }
477
Jon Spivackf288b1d2019-12-19 17:15:51 -0800478 mNameToRegistrationCallback[name].push_back(callback);
Steven Moreland27cfab02019-08-12 14:34:16 -0700479
480 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
481 const sp<IBinder>& binder = it->second.binder;
482
483 // never null if an entry exists
484 CHECK(binder != nullptr) << name;
485 callback->onRegistration(name, binder);
486 }
487
488 return Status::ok();
489}
490Status ServiceManager::unregisterForNotifications(
491 const std::string& name, const sp<IServiceCallback>& callback) {
492 auto ctx = mAccess->getCallingContext();
493
494 if (!mAccess->canFind(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000495 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700496 }
497
498 bool found = false;
499
Jon Spivackf288b1d2019-12-19 17:15:51 -0800500 auto it = mNameToRegistrationCallback.find(name);
501 if (it != mNameToRegistrationCallback.end()) {
502 removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
Steven Moreland27cfab02019-08-12 14:34:16 -0700503 }
504
505 if (!found) {
Pawan Wagh37526162022-09-29 21:55:26 +0000506 ALOGE("Trying to unregister callback, but none exists %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000507 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700508 }
509
510 return Status::ok();
511}
512
Steven Morelandb82b8f82019-10-28 10:52:34 -0700513Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
514 auto ctx = mAccess->getCallingContext();
515
516 if (!mAccess->canFind(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000517 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Morelandb82b8f82019-10-28 10:52:34 -0700518 }
519
520 *outReturn = false;
521
522#ifndef VENDORSERVICEMANAGER
523 *outReturn = isVintfDeclared(name);
524#endif
525 return Status::ok();
526}
527
Steven Moreland2e293aa2020-09-23 00:25:16 +0000528binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) {
529 auto ctx = mAccess->getCallingContext();
530
531 std::vector<std::string> allInstances;
532#ifndef VENDORSERVICEMANAGER
533 allInstances = getVintfInstances(interface);
534#endif
535
536 outReturn->clear();
537
538 for (const std::string& instance : allInstances) {
Steven Moreland2e293aa2020-09-23 00:25:16 +0000539 if (mAccess->canFind(ctx, interface + "/" + instance)) {
540 outReturn->push_back(instance);
541 }
542 }
543
544 if (outReturn->size() == 0 && allInstances.size() != 0) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000545 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland2e293aa2020-09-23 00:25:16 +0000546 }
547
548 return Status::ok();
549}
550
Steven Morelandedd4e072021-04-21 00:27:29 +0000551Status ServiceManager::updatableViaApex(const std::string& name,
552 std::optional<std::string>* outReturn) {
553 auto ctx = mAccess->getCallingContext();
554
555 if (!mAccess->canFind(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000556 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Morelandedd4e072021-04-21 00:27:29 +0000557 }
558
559 *outReturn = std::nullopt;
560
561#ifndef VENDORSERVICEMANAGER
562 *outReturn = getVintfUpdatableApex(name);
563#endif
564 return Status::ok();
565}
566
Jooyung Han76944fe2022-10-25 17:02:45 +0900567Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName,
568 std::vector<std::string>* outReturn) {
569 auto ctx = mAccess->getCallingContext();
570
571 std::vector<std::string> apexUpdatableInstances;
572#ifndef VENDORSERVICEMANAGER
573 apexUpdatableInstances = getVintfUpdatableInstances(apexName);
574#endif
575
576 outReturn->clear();
577
578 for (const std::string& instance : apexUpdatableInstances) {
579 if (mAccess->canFind(ctx, instance)) {
580 outReturn->push_back(instance);
581 }
582 }
583
584 if (outReturn->size() == 0 && apexUpdatableInstances.size() != 0) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000585 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Jooyung Han76944fe2022-10-25 17:02:45 +0900586 }
587
588 return Status::ok();
589}
590
Devin Moore5e4c2f12021-09-09 22:36:33 +0000591Status ServiceManager::getConnectionInfo(const std::string& name,
592 std::optional<ConnectionInfo>* outReturn) {
593 auto ctx = mAccess->getCallingContext();
594
595 if (!mAccess->canFind(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000596 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Devin Moore5e4c2f12021-09-09 22:36:33 +0000597 }
598
599 *outReturn = std::nullopt;
600
601#ifndef VENDORSERVICEMANAGER
602 *outReturn = getVintfConnectionInfo(name);
603#endif
604 return Status::ok();
605}
606
Jon Spivackf288b1d2019-12-19 17:15:51 -0800607void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
608 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700609 bool* found) {
610 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
611
612 for (auto lit = listeners.begin(); lit != listeners.end();) {
613 if (IInterface::asBinder(*lit) == who) {
614 if(found) *found = true;
615 lit = listeners.erase(lit);
616 } else {
617 ++lit;
618 }
619 }
620
621 if (listeners.empty()) {
Jon Spivackf288b1d2019-12-19 17:15:51 -0800622 *it = mNameToRegistrationCallback.erase(*it);
Steven Moreland27cfab02019-08-12 14:34:16 -0700623 } else {
Jon Spivacke223f082019-11-19 16:21:20 -0800624 (*it)++;
Steven Moreland27cfab02019-08-12 14:34:16 -0700625 }
626}
627
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700628void ServiceManager::binderDied(const wp<IBinder>& who) {
629 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
630 if (who == it->second.binder) {
Steven Moreland79578672023-04-27 19:38:00 +0000631 // TODO: currently, this entry contains the state also
632 // associated with mNameToClientCallback. If we allowed
633 // other processes to register client callbacks, we
634 // would have to preserve hasClients (perhaps moving
635 // that state into mNameToClientCallback, which is complicated
636 // because those callbacks are associated w/ particular binder
637 // objects, though they are indexed by name now, they may
638 // need to be indexed by binder at that point).
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700639 it = mNameToService.erase(it);
640 } else {
641 ++it;
642 }
643 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700644
Jon Spivackf288b1d2019-12-19 17:15:51 -0800645 for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
646 removeRegistrationCallback(who, &it, nullptr /*found*/);
Steven Moreland27cfab02019-08-12 14:34:16 -0700647 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700648
649 for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
650 removeClientCallback(who, &it);
651 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700652}
653
Jon Spivack0d844302019-07-22 18:40:34 -0700654void ServiceManager::tryStartService(const std::string& name) {
Steven Morelandba0f33c2022-11-04 22:24:31 +0000655 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service. (if it's not "
656 "configured to be a lazy service, it may be stuck starting or still starting).",
Jon Spivack0d844302019-07-22 18:40:34 -0700657 name.c_str());
658
659 std::thread([=] {
Steven Morelandbfe9fba2021-04-27 18:39:57 +0000660 if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
Pawan Wagh37526162022-09-29 21:55:26 +0000661 ALOGI("Tried to start aidl service %s as a lazy service, but was unable to. Usually "
662 "this happens when a "
663 "service is not installed, but if the service is intended to be used as a "
664 "lazy service, then it may be configured incorrectly.",
665 name.c_str());
Steven Morelandbfe9fba2021-04-27 18:39:57 +0000666 }
Jon Spivack0d844302019-07-22 18:40:34 -0700667 }).detach();
668}
669
Jon Spivack9f503a42019-10-22 16:49:19 -0700670Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
671 const sp<IClientCallback>& cb) {
672 if (cb == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000673 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Callback null.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700674 }
675
676 auto ctx = mAccess->getCallingContext();
677 if (!mAccess->canAdd(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000678 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700679 }
680
681 auto serviceIt = mNameToService.find(name);
682 if (serviceIt == mNameToService.end()) {
Pawan Wagh37526162022-09-29 21:55:26 +0000683 ALOGE("Could not add callback for nonexistent service: %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000684 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700685 }
686
Steven Moreland7ee423b2022-09-24 03:52:08 +0000687 if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
Pawan Wagh37526162022-09-29 21:55:26 +0000688 ALOGW("Only a server can register for client callbacks (for %s)", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000689 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
690 "Only service can register client callback for itself.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700691 }
692
693 if (serviceIt->second.binder != service) {
Pawan Wagh37526162022-09-29 21:55:26 +0000694 ALOGW("Tried to register client callback for %s but a different service is registered "
695 "under this name.",
696 name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000697 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service mismatch.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700698 }
699
Steven Morelandb0983182021-04-02 03:14:04 +0000700 if (OK !=
701 IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
Pawan Wagh37526162022-09-29 21:55:26 +0000702 ALOGE("Could not linkToDeath when adding client callback for %s", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000703 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700704 }
705
Steven Moreland79578672023-04-27 19:38:00 +0000706 // WARNING: binderDied makes an assumption about this. If we open up client
707 // callbacks to other services, certain race conditions may lead to services
708 // getting extra client callback notifications.
709 // Make sure all callbacks have been told about a consistent state - b/278038751
Steven Moreland7bb4ab82023-04-13 20:29:33 +0000710 if (serviceIt->second.hasClients) {
711 cb->onClients(service, true);
712 }
713
Jon Spivack9f503a42019-10-22 16:49:19 -0700714 mNameToClientCallback[name].push_back(cb);
715
716 return Status::ok();
717}
718
719void ServiceManager::removeClientCallback(const wp<IBinder>& who,
720 ClientCallbackMap::iterator* it) {
721 std::vector<sp<IClientCallback>>& listeners = (*it)->second;
722
723 for (auto lit = listeners.begin(); lit != listeners.end();) {
724 if (IInterface::asBinder(*lit) == who) {
725 lit = listeners.erase(lit);
726 } else {
727 ++lit;
728 }
729 }
730
731 if (listeners.empty()) {
732 *it = mNameToClientCallback.erase(*it);
733 } else {
734 (*it)++;
735 }
736}
737
738ssize_t ServiceManager::Service::getNodeStrongRefCount() {
Steven Morelandb0983182021-04-02 03:14:04 +0000739 sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder());
Jon Spivack9f503a42019-10-22 16:49:19 -0700740 if (bpBinder == nullptr) return -1;
741
Steven Morelande8393882020-12-18 02:27:20 +0000742 return ProcessState::self()->getStrongRefCountForNode(bpBinder);
Jon Spivack9f503a42019-10-22 16:49:19 -0700743}
744
745void ServiceManager::handleClientCallbacks() {
746 for (const auto& [name, service] : mNameToService) {
Steven Morelandb8361902023-02-01 23:18:04 +0000747 handleServiceClientCallback(1 /* sm has one refcount */, name, true);
Jon Spivack9f503a42019-10-22 16:49:19 -0700748 }
749}
750
Steven Morelandb8361902023-02-01 23:18:04 +0000751bool ServiceManager::handleServiceClientCallback(size_t knownClients,
752 const std::string& serviceName,
753 bool isCalledOnInterval) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700754 auto serviceIt = mNameToService.find(serviceName);
755 if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
Steven Morelandb8361902023-02-01 23:18:04 +0000756 return true; // return we do have clients a.k.a. DON'T DO ANYTHING
Jon Spivack9f503a42019-10-22 16:49:19 -0700757 }
758
759 Service& service = serviceIt->second;
760 ssize_t count = service.getNodeStrongRefCount();
761
Steven Morelandb8361902023-02-01 23:18:04 +0000762 // binder driver doesn't support this feature, consider we have clients
763 if (count == -1) return true;
Jon Spivack9f503a42019-10-22 16:49:19 -0700764
Steven Morelandb8361902023-02-01 23:18:04 +0000765 bool hasKernelReportedClients = static_cast<size_t>(count) > knownClients;
Jon Spivack9f503a42019-10-22 16:49:19 -0700766
767 if (service.guaranteeClient) {
Steven Morelandb8361902023-02-01 23:18:04 +0000768 if (!service.hasClients && !hasKernelReportedClients) {
Steven Moreland3e083b22023-01-26 00:46:30 +0000769 sendClientCallbackNotifications(serviceName, true,
770 "service is guaranteed to be in use");
Jon Spivack9f503a42019-10-22 16:49:19 -0700771 }
772
773 // guarantee is temporary
774 service.guaranteeClient = false;
775 }
776
Steven Morelandb8361902023-02-01 23:18:04 +0000777 // Regardless of this situation, we want to give this notification as soon as possible.
778 // This way, we have a chance of preventing further thrashing.
779 if (hasKernelReportedClients && !service.hasClients) {
780 sendClientCallbackNotifications(serviceName, true, "we now have a record of a client");
781 }
Steven Moreland66417652023-02-01 22:19:41 +0000782
Steven Morelandb8361902023-02-01 23:18:04 +0000783 // But limit rate of shutting down service.
784 if (isCalledOnInterval) {
785 if (!hasKernelReportedClients && service.hasClients) {
Steven Moreland3e083b22023-01-26 00:46:30 +0000786 sendClientCallbackNotifications(serviceName, false,
787 "we now have no record of a client");
Jon Spivackd9533c22020-01-27 22:19:22 +0000788 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700789 }
790
Steven Morelandb8361902023-02-01 23:18:04 +0000791 // May be different than 'hasKernelReportedClients'. We intentionally delay
792 // information about clients going away to reduce thrashing.
793 return service.hasClients;
Jon Spivack9f503a42019-10-22 16:49:19 -0700794}
795
Steven Moreland3e083b22023-01-26 00:46:30 +0000796void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName,
797 bool hasClients, const char* context) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700798 auto serviceIt = mNameToService.find(serviceName);
799 if (serviceIt == mNameToService.end()) {
Steven Moreland3e083b22023-01-26 00:46:30 +0000800 ALOGW("sendClientCallbackNotifications could not find service %s when %s",
801 serviceName.c_str(), context);
Jon Spivack9f503a42019-10-22 16:49:19 -0700802 return;
803 }
804 Service& service = serviceIt->second;
805
Steven Morelandb8361902023-02-01 23:18:04 +0000806 CHECK_NE(hasClients, service.hasClients) << context;
Jon Spivack9f503a42019-10-22 16:49:19 -0700807
Steven Morelandb8361902023-02-01 23:18:04 +0000808 ALOGI("Notifying %s they %s (previously: %s) have clients when %s", serviceName.c_str(),
809 hasClients ? "do" : "don't", service.hasClients ? "do" : "don't", context);
Jon Spivack9f503a42019-10-22 16:49:19 -0700810
811 auto ccIt = mNameToClientCallback.find(serviceName);
812 CHECK(ccIt != mNameToClientCallback.end())
Steven Moreland3e083b22023-01-26 00:46:30 +0000813 << "sendClientCallbackNotifications could not find callbacks for service when "
814 << context;
Jon Spivack9f503a42019-10-22 16:49:19 -0700815
816 for (const auto& callback : ccIt->second) {
817 callback->onClients(service.binder, hasClients);
818 }
819
820 service.hasClients = hasClients;
821}
822
823Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
824 if (binder == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000825 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null service.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700826 }
827
828 auto ctx = mAccess->getCallingContext();
829 if (!mAccess->canAdd(ctx, name)) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000830 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700831 }
832
833 auto serviceIt = mNameToService.find(name);
834 if (serviceIt == mNameToService.end()) {
Pawan Wagh37526162022-09-29 21:55:26 +0000835 ALOGW("Tried to unregister %s, but that service wasn't registered to begin with.",
836 name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000837 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700838 }
839
Steven Moreland7ee423b2022-09-24 03:52:08 +0000840 if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
Pawan Wagh37526162022-09-29 21:55:26 +0000841 ALOGW("Only a server can unregister itself (for %s)", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000842 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
843 "Service can only unregister itself.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700844 }
845
846 sp<IBinder> storedBinder = serviceIt->second.binder;
847
848 if (binder != storedBinder) {
Pawan Wagh37526162022-09-29 21:55:26 +0000849 ALOGW("Tried to unregister %s, but a different service is registered under this name.",
850 name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000851 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
852 "Different service registered under this name.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700853 }
854
Steven Morelandb8361902023-02-01 23:18:04 +0000855 // important because we don't have timer-based guarantees, we don't want to clear
856 // this
Jon Spivack0f18f2c2020-03-13 20:45:18 -0700857 if (serviceIt->second.guaranteeClient) {
Pawan Wagh37526162022-09-29 21:55:26 +0000858 ALOGI("Tried to unregister %s, but there is about to be a client.", name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000859 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
860 "Can't unregister, pending client.");
Jon Spivack0f18f2c2020-03-13 20:45:18 -0700861 }
862
Jon Spivack9f503a42019-10-22 16:49:19 -0700863 // - kernel driver will hold onto one refcount (during this transaction)
864 // - servicemanager has a refcount (guaranteed by this transaction)
Steven Morelandb8361902023-02-01 23:18:04 +0000865 constexpr size_t kKnownClients = 2;
866
867 if (handleServiceClientCallback(kKnownClients, name, false)) {
868 ALOGI("Tried to unregister %s, but there are clients.", name.c_str());
869
870 // Since we had a failed registration attempt, and the HIDL implementation of
871 // delaying service shutdown for multiple periods wasn't ported here... this may
872 // help reduce thrashing, but we should be able to remove it.
Jon Spivack620d2dc2020-03-06 13:58:01 -0800873 serviceIt->second.guaranteeClient = true;
Steven Morelandb8361902023-02-01 23:18:04 +0000874
Steven Morelandffb905b2023-03-28 18:24:37 +0000875 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
876 "Can't unregister, known client.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700877 }
878
Steven Morelandb8361902023-02-01 23:18:04 +0000879 ALOGI("Unregistering %s", name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -0700880 mNameToService.erase(name);
881
882 return Status::ok();
883}
884
Steven Moreland3ea43272021-01-28 22:49:28 +0000885Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) {
886 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000887 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland3ea43272021-01-28 22:49:28 +0000888 }
889
890 outReturn->reserve(mNameToService.size());
891 for (auto const& [name, service] : mNameToService) {
892 ServiceDebugInfo info;
893 info.name = name;
Steven Moreland7ee423b2022-09-24 03:52:08 +0000894 info.debugPid = service.ctx.debugPid;
Steven Moreland3ea43272021-01-28 22:49:28 +0000895
896 outReturn->push_back(std::move(info));
897 }
898
899 return Status::ok();
900}
901
Pawan Wagh243888e2022-09-20 19:37:35 +0000902void ServiceManager::clear() {
903 mNameToService.clear();
904 mNameToRegistrationCallback.clear();
905 mNameToClientCallback.clear();
906}
907
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700908} // namespace android