blob: 2f5524940efd41f816e3ebddda10b582cfa16469 [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>
31#include <vintf/constants.h>
32#endif // !VENDORSERVICEMANAGER
33
Steven Moreland80e1e6d2019-06-21 12:35:59 -070034using ::android::binder::Status;
Steven Moreland86a17f82019-09-10 10:18:00 -070035using ::android::internal::Stability;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070036
37namespace android {
38
Steven Moreland86a17f82019-09-10 10:18:00 -070039#ifndef VENDORSERVICEMANAGER
Steven Moreland2e293aa2020-09-23 00:25:16 +000040struct ManifestWithDescription {
41 std::shared_ptr<const vintf::HalManifest> manifest;
42 const char* description;
43};
44// func true -> stop search and forEachManifest will return true
45static bool forEachManifest(const std::function<bool(const ManifestWithDescription&)>& func) {
46 for (const ManifestWithDescription& mwd : {
47 ManifestWithDescription{ vintf::VintfObject::GetDeviceHalManifest(), "device" },
48 ManifestWithDescription{ vintf::VintfObject::GetFrameworkHalManifest(), "framework" },
49 }) {
50 if (mwd.manifest == nullptr) {
51 LOG(ERROR) << "NULL VINTF MANIFEST!: " << mwd.description;
52 // note, we explicitly do not retry here, so that we can detect VINTF
53 // or other bugs (b/151696835)
54 continue;
55 }
56 if (func(mwd)) return true;
57 }
58 return false;
59}
60
Steven Morelandb82b8f82019-10-28 10:52:34 -070061static bool isVintfDeclared(const std::string& name) {
Steven Moreland86a17f82019-09-10 10:18:00 -070062 size_t firstSlash = name.find('/');
63 size_t lastDot = name.rfind('.', firstSlash);
64 if (firstSlash == std::string::npos || lastDot == std::string::npos) {
65 LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. "
66 << "some.package.foo.IFoo/default) but got: " << name;
67 return false;
68 }
69 const std::string package = name.substr(0, lastDot);
70 const std::string iface = name.substr(lastDot+1, firstSlash-lastDot-1);
71 const std::string instance = name.substr(firstSlash+1);
72
Steven Moreland2e293aa2020-09-23 00:25:16 +000073 bool found = forEachManifest([&] (const ManifestWithDescription& mwd) {
Steven Moreland2edde8e2020-04-30 17:04:54 -070074 if (mwd.manifest->hasAidlInstance(package, iface, instance)) {
75 LOG(INFO) << "Found " << name << " in " << mwd.description << " VINTF manifest.";
Steven Moreland86a17f82019-09-10 10:18:00 -070076 return true;
77 }
Steven Moreland2e293aa2020-09-23 00:25:16 +000078 return false; // continue
79 });
80
81 if (!found) {
82 // Although it is tested, explicitly rebuilding qualified name, in case it
83 // becomes something unexpected.
84 LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance
85 << " in the VINTF manifest.";
Steven Moreland86a17f82019-09-10 10:18:00 -070086 }
Steven Moreland2edde8e2020-04-30 17:04:54 -070087
Steven Moreland2e293aa2020-09-23 00:25:16 +000088 return found;
89}
90
91static std::vector<std::string> getVintfInstances(const std::string& interface) {
92 size_t lastDot = interface.rfind('.');
93 if (lastDot == std::string::npos) {
94 LOG(ERROR) << "VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) but got: " << interface;
95 return {};
96 }
97 const std::string package = interface.substr(0, lastDot);
98 const std::string iface = interface.substr(lastDot+1);
99
100 std::vector<std::string> ret;
101 (void)forEachManifest([&](const ManifestWithDescription& mwd) {
102 auto instances = mwd.manifest->getAidlInstances(package, iface);
103 ret.insert(ret.end(), instances.begin(), instances.end());
104 return false; // continue
105 });
106
107 return ret;
Steven Moreland86a17f82019-09-10 10:18:00 -0700108}
Steven Morelandb82b8f82019-10-28 10:52:34 -0700109
110static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) {
111 if (!Stability::requiresVintfDeclaration(binder)) {
112 return true;
113 }
114
115 return isVintfDeclared(name);
116}
Steven Moreland86a17f82019-09-10 10:18:00 -0700117#endif // !VENDORSERVICEMANAGER
118
Steven Morelandd13f08b2019-11-18 14:23:09 -0800119ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700120// TODO(b/151696835): reenable performance hack when we solve bug, since with
121// this hack and other fixes, it is unlikely we will see even an ephemeral
122// failure when the manifest parse fails. The goal is that the manifest will
123// be read incorrectly and cause the process trying to register a HAL to
124// fail. If this is in fact an early boot kernel contention issue, then we
125// will get no failure, and by its absence, be signalled to invest more
126// effort in re-adding this performance hack.
127// #ifndef VENDORSERVICEMANAGER
128// // can process these at any times, don't want to delay first VINTF client
129// std::thread([] {
130// vintf::VintfObject::GetDeviceHalManifest();
131// vintf::VintfObject::GetFrameworkHalManifest();
132// }).detach();
133// #endif // !VENDORSERVICEMANAGER
Steven Morelandd13f08b2019-11-18 14:23:09 -0800134}
Steven Moreland130242d2019-08-26 17:41:32 -0700135ServiceManager::~ServiceManager() {
136 // this should only happen in tests
137
Jon Spivackf288b1d2019-12-19 17:15:51 -0800138 for (const auto& [name, callbacks] : mNameToRegistrationCallback) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700139 CHECK(!callbacks.empty()) << name;
140 for (const auto& callback : callbacks) {
141 CHECK(callback != nullptr) << name;
142 }
143 }
144
Steven Moreland130242d2019-08-26 17:41:32 -0700145 for (const auto& [name, service] : mNameToService) {
146 CHECK(service.binder != nullptr) << name;
147 }
148}
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700149
150Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700151 *outBinder = tryGetService(name, true);
152 // returns ok regardless of result for legacy reasons
153 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700154}
155
156Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700157 *outBinder = tryGetService(name, false);
158 // returns ok regardless of result for legacy reasons
159 return Status::ok();
160}
161
162sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700163 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700164
Jon Spivack0d844302019-07-22 18:40:34 -0700165 sp<IBinder> out;
Jon Spivack9f503a42019-10-22 16:49:19 -0700166 Service* service = nullptr;
Jon Spivack0d844302019-07-22 18:40:34 -0700167 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700168 service = &(it->second);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700169
Jon Spivack9f503a42019-10-22 16:49:19 -0700170 if (!service->allowIsolated) {
Jon Spivack0d844302019-07-22 18:40:34 -0700171 uid_t appid = multiuser_get_app_id(ctx.uid);
172 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700173
Jon Spivack0d844302019-07-22 18:40:34 -0700174 if (isIsolated) {
175 return nullptr;
176 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700177 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700178 out = service->binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700179 }
180
Steven Morelanda9fe4742019-07-18 14:45:20 -0700181 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700182 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700183 }
184
Jon Spivack0d844302019-07-22 18:40:34 -0700185 if (!out && startIfNotFound) {
186 tryStartService(name);
187 }
188
Jon Spivack9f503a42019-10-22 16:49:19 -0700189 if (out) {
190 // Setting this guarantee each time we hand out a binder ensures that the client-checking
191 // loop knows about the event even if the client immediately drops the service
192 service->guaranteeClient = true;
193 }
194
Jon Spivack0d844302019-07-22 18:40:34 -0700195 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700196}
197
Steven Moreland905e2e82019-07-17 11:05:45 -0700198bool isValidServiceName(const std::string& name) {
199 if (name.size() == 0) return false;
200 if (name.size() > 127) return false;
201
202 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700203 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700204 if (c >= 'a' && c <= 'z') continue;
205 if (c >= 'A' && c <= 'Z') continue;
206 if (c >= '0' && c <= '9') continue;
207 return false;
208 }
209
210 return true;
211}
212
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700213Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700214 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700215
216 // apps cannot add services
217 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
218 return Status::fromExceptionCode(Status::EX_SECURITY);
219 }
220
Steven Morelanda9fe4742019-07-18 14:45:20 -0700221 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700222 return Status::fromExceptionCode(Status::EX_SECURITY);
223 }
224
225 if (binder == nullptr) {
226 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
227 }
228
Steven Moreland905e2e82019-07-17 11:05:45 -0700229 if (!isValidServiceName(name)) {
230 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700231 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
232 }
233
Steven Moreland86a17f82019-09-10 10:18:00 -0700234#ifndef VENDORSERVICEMANAGER
235 if (!meetsDeclarationRequirements(binder, name)) {
236 // already logged
237 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
238 }
239#endif // !VENDORSERVICEMANAGER
240
Steven Moreland88860b02019-08-12 14:24:14 -0700241 // implicitly unlinked when the binder is removed
Steven Morelandb0983182021-04-02 03:14:04 +0000242 if (binder->remoteBinder() != nullptr &&
243 binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700244 LOG(ERROR) << "Could not linkToDeath when adding " << name;
245 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
246 }
247
Devin Moore05ffe522020-08-06 13:58:29 -0700248 // Overwrite the old service if it exists
249 mNameToService[name] = Service {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700250 .binder = binder,
251 .allowIsolated = allowIsolated,
252 .dumpPriority = dumpPriority,
Jon Spivack9f503a42019-10-22 16:49:19 -0700253 .debugPid = ctx.debugPid,
Devin Moore05ffe522020-08-06 13:58:29 -0700254 };
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700255
Jon Spivackf288b1d2019-12-19 17:15:51 -0800256 auto it = mNameToRegistrationCallback.find(name);
257 if (it != mNameToRegistrationCallback.end()) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700258 for (const sp<IServiceCallback>& cb : it->second) {
Devin Moore05ffe522020-08-06 13:58:29 -0700259 mNameToService[name].guaranteeClient = true;
Steven Moreland27cfab02019-08-12 14:34:16 -0700260 // permission checked in registerForNotifications
261 cb->onRegistration(name, binder);
262 }
263 }
264
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700265 return Status::ok();
266}
267
268Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700269 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700270 return Status::fromExceptionCode(Status::EX_SECURITY);
271 }
272
273 size_t toReserve = 0;
274 for (auto const& [name, service] : mNameToService) {
275 (void) name;
276
277 if (service.dumpPriority & dumpPriority) ++toReserve;
278 }
279
280 CHECK(outList->empty());
281
282 outList->reserve(toReserve);
283 for (auto const& [name, service] : mNameToService) {
284 (void) service;
285
286 if (service.dumpPriority & dumpPriority) {
287 outList->push_back(name);
288 }
289 }
290
291 return Status::ok();
292}
293
Steven Moreland27cfab02019-08-12 14:34:16 -0700294Status ServiceManager::registerForNotifications(
295 const std::string& name, const sp<IServiceCallback>& callback) {
296 auto ctx = mAccess->getCallingContext();
297
298 if (!mAccess->canFind(ctx, name)) {
299 return Status::fromExceptionCode(Status::EX_SECURITY);
300 }
301
302 if (!isValidServiceName(name)) {
303 LOG(ERROR) << "Invalid service name: " << name;
304 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
305 }
306
307 if (callback == nullptr) {
308 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
309 }
310
Steven Morelandb0983182021-04-02 03:14:04 +0000311 if (OK !=
312 IInterface::asBinder(callback)->linkToDeath(
313 sp<ServiceManager>::fromExisting(this))) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700314 LOG(ERROR) << "Could not linkToDeath when adding " << name;
315 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
316 }
317
Jon Spivackf288b1d2019-12-19 17:15:51 -0800318 mNameToRegistrationCallback[name].push_back(callback);
Steven Moreland27cfab02019-08-12 14:34:16 -0700319
320 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
321 const sp<IBinder>& binder = it->second.binder;
322
323 // never null if an entry exists
324 CHECK(binder != nullptr) << name;
325 callback->onRegistration(name, binder);
326 }
327
328 return Status::ok();
329}
330Status ServiceManager::unregisterForNotifications(
331 const std::string& name, const sp<IServiceCallback>& callback) {
332 auto ctx = mAccess->getCallingContext();
333
334 if (!mAccess->canFind(ctx, name)) {
335 return Status::fromExceptionCode(Status::EX_SECURITY);
336 }
337
338 bool found = false;
339
Jon Spivackf288b1d2019-12-19 17:15:51 -0800340 auto it = mNameToRegistrationCallback.find(name);
341 if (it != mNameToRegistrationCallback.end()) {
342 removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
Steven Moreland27cfab02019-08-12 14:34:16 -0700343 }
344
345 if (!found) {
346 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
347 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
348 }
349
350 return Status::ok();
351}
352
Steven Morelandb82b8f82019-10-28 10:52:34 -0700353Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
354 auto ctx = mAccess->getCallingContext();
355
356 if (!mAccess->canFind(ctx, name)) {
357 return Status::fromExceptionCode(Status::EX_SECURITY);
358 }
359
360 *outReturn = false;
361
362#ifndef VENDORSERVICEMANAGER
363 *outReturn = isVintfDeclared(name);
364#endif
365 return Status::ok();
366}
367
Steven Moreland2e293aa2020-09-23 00:25:16 +0000368binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) {
369 auto ctx = mAccess->getCallingContext();
370
371 std::vector<std::string> allInstances;
372#ifndef VENDORSERVICEMANAGER
373 allInstances = getVintfInstances(interface);
374#endif
375
376 outReturn->clear();
377
378 for (const std::string& instance : allInstances) {
Steven Moreland2e293aa2020-09-23 00:25:16 +0000379 if (mAccess->canFind(ctx, interface + "/" + instance)) {
380 outReturn->push_back(instance);
381 }
382 }
383
384 if (outReturn->size() == 0 && allInstances.size() != 0) {
385 return Status::fromExceptionCode(Status::EX_SECURITY);
386 }
387
388 return Status::ok();
389}
390
Jon Spivackf288b1d2019-12-19 17:15:51 -0800391void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
392 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700393 bool* found) {
394 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
395
396 for (auto lit = listeners.begin(); lit != listeners.end();) {
397 if (IInterface::asBinder(*lit) == who) {
398 if(found) *found = true;
399 lit = listeners.erase(lit);
400 } else {
401 ++lit;
402 }
403 }
404
405 if (listeners.empty()) {
Jon Spivackf288b1d2019-12-19 17:15:51 -0800406 *it = mNameToRegistrationCallback.erase(*it);
Steven Moreland27cfab02019-08-12 14:34:16 -0700407 } else {
Jon Spivacke223f082019-11-19 16:21:20 -0800408 (*it)++;
Steven Moreland27cfab02019-08-12 14:34:16 -0700409 }
410}
411
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700412void ServiceManager::binderDied(const wp<IBinder>& who) {
413 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
414 if (who == it->second.binder) {
415 it = mNameToService.erase(it);
416 } else {
417 ++it;
418 }
419 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700420
Jon Spivackf288b1d2019-12-19 17:15:51 -0800421 for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
422 removeRegistrationCallback(who, &it, nullptr /*found*/);
Steven Moreland27cfab02019-08-12 14:34:16 -0700423 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700424
425 for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
426 removeClientCallback(who, &it);
427 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700428}
429
Jon Spivack0d844302019-07-22 18:40:34 -0700430void ServiceManager::tryStartService(const std::string& name) {
431 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service",
432 name.c_str());
433
434 std::thread([=] {
435 (void)base::SetProperty("ctl.interface_start", "aidl/" + name);
436 }).detach();
437}
438
Jon Spivack9f503a42019-10-22 16:49:19 -0700439Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
440 const sp<IClientCallback>& cb) {
441 if (cb == nullptr) {
442 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
443 }
444
445 auto ctx = mAccess->getCallingContext();
446 if (!mAccess->canAdd(ctx, name)) {
447 return Status::fromExceptionCode(Status::EX_SECURITY);
448 }
449
450 auto serviceIt = mNameToService.find(name);
451 if (serviceIt == mNameToService.end()) {
452 LOG(ERROR) << "Could not add callback for nonexistent service: " << name;
453 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
454 }
455
456 if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) {
457 LOG(WARNING) << "Only a server can register for client callbacks (for " << name << ")";
458 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
459 }
460
461 if (serviceIt->second.binder != service) {
462 LOG(WARNING) << "Tried to register client callback for " << name
463 << " but a different service is registered under this name.";
464 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
465 }
466
Steven Morelandb0983182021-04-02 03:14:04 +0000467 if (OK !=
468 IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700469 LOG(ERROR) << "Could not linkToDeath when adding client callback for " << name;
470 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
471 }
472
473 mNameToClientCallback[name].push_back(cb);
474
475 return Status::ok();
476}
477
478void ServiceManager::removeClientCallback(const wp<IBinder>& who,
479 ClientCallbackMap::iterator* it) {
480 std::vector<sp<IClientCallback>>& listeners = (*it)->second;
481
482 for (auto lit = listeners.begin(); lit != listeners.end();) {
483 if (IInterface::asBinder(*lit) == who) {
484 lit = listeners.erase(lit);
485 } else {
486 ++lit;
487 }
488 }
489
490 if (listeners.empty()) {
491 *it = mNameToClientCallback.erase(*it);
492 } else {
493 (*it)++;
494 }
495}
496
497ssize_t ServiceManager::Service::getNodeStrongRefCount() {
Steven Morelandb0983182021-04-02 03:14:04 +0000498 sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder());
Jon Spivack9f503a42019-10-22 16:49:19 -0700499 if (bpBinder == nullptr) return -1;
500
Steven Morelande8393882020-12-18 02:27:20 +0000501 return ProcessState::self()->getStrongRefCountForNode(bpBinder);
Jon Spivack9f503a42019-10-22 16:49:19 -0700502}
503
504void ServiceManager::handleClientCallbacks() {
505 for (const auto& [name, service] : mNameToService) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000506 handleServiceClientCallback(name, true);
Jon Spivack9f503a42019-10-22 16:49:19 -0700507 }
508}
509
Jon Spivackd9533c22020-01-27 22:19:22 +0000510ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName,
511 bool isCalledOnInterval) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700512 auto serviceIt = mNameToService.find(serviceName);
513 if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
514 return -1;
515 }
516
517 Service& service = serviceIt->second;
518 ssize_t count = service.getNodeStrongRefCount();
519
520 // binder driver doesn't support this feature
521 if (count == -1) return count;
522
523 bool hasClients = count > 1; // this process holds a strong count
524
525 if (service.guaranteeClient) {
526 // we have no record of this client
527 if (!service.hasClients && !hasClients) {
528 sendClientCallbackNotifications(serviceName, true);
529 }
530
531 // guarantee is temporary
532 service.guaranteeClient = false;
533 }
534
Jon Spivackd9533c22020-01-27 22:19:22 +0000535 // only send notifications if this was called via the interval checking workflow
536 if (isCalledOnInterval) {
537 if (hasClients && !service.hasClients) {
538 // client was retrieved in some other way
539 sendClientCallbackNotifications(serviceName, true);
540 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700541
Jon Spivackd9533c22020-01-27 22:19:22 +0000542 // there are no more clients, but the callback has not been called yet
543 if (!hasClients && service.hasClients) {
544 sendClientCallbackNotifications(serviceName, false);
545 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700546 }
547
548 return count;
549}
550
551void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, bool hasClients) {
552 auto serviceIt = mNameToService.find(serviceName);
553 if (serviceIt == mNameToService.end()) {
554 LOG(WARNING) << "sendClientCallbackNotifications could not find service " << serviceName;
555 return;
556 }
557 Service& service = serviceIt->second;
558
559 CHECK(hasClients != service.hasClients) << "Record shows: " << service.hasClients
560 << " so we can't tell clients again that we have client: " << hasClients;
561
562 LOG(INFO) << "Notifying " << serviceName << " they have clients: " << hasClients;
563
564 auto ccIt = mNameToClientCallback.find(serviceName);
565 CHECK(ccIt != mNameToClientCallback.end())
566 << "sendClientCallbackNotifications could not find callbacks for service ";
567
568 for (const auto& callback : ccIt->second) {
569 callback->onClients(service.binder, hasClients);
570 }
571
572 service.hasClients = hasClients;
573}
574
575Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
576 if (binder == nullptr) {
577 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
578 }
579
580 auto ctx = mAccess->getCallingContext();
581 if (!mAccess->canAdd(ctx, name)) {
582 return Status::fromExceptionCode(Status::EX_SECURITY);
583 }
584
585 auto serviceIt = mNameToService.find(name);
586 if (serviceIt == mNameToService.end()) {
587 LOG(WARNING) << "Tried to unregister " << name
588 << ", but that service wasn't registered to begin with.";
589 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
590 }
591
592 if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) {
593 LOG(WARNING) << "Only a server can unregister itself (for " << name << ")";
594 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
595 }
596
597 sp<IBinder> storedBinder = serviceIt->second.binder;
598
599 if (binder != storedBinder) {
600 LOG(WARNING) << "Tried to unregister " << name
601 << ", but a different service is registered under this name.";
602 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
603 }
604
Jon Spivack0f18f2c2020-03-13 20:45:18 -0700605 if (serviceIt->second.guaranteeClient) {
606 LOG(INFO) << "Tried to unregister " << name << ", but there is about to be a client.";
607 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
608 }
609
Jon Spivackd9533c22020-01-27 22:19:22 +0000610 int clients = handleServiceClientCallback(name, false);
Jon Spivack9f503a42019-10-22 16:49:19 -0700611
612 // clients < 0: feature not implemented or other error. Assume clients.
613 // Otherwise:
614 // - kernel driver will hold onto one refcount (during this transaction)
615 // - servicemanager has a refcount (guaranteed by this transaction)
616 // So, if clients > 2, then at least one other service on the system must hold a refcount.
617 if (clients < 0 || clients > 2) {
618 // client callbacks are either disabled or there are other clients
Jon Spivackd9533c22020-01-27 22:19:22 +0000619 LOG(INFO) << "Tried to unregister " << name << ", but there are clients: " << clients;
Jon Spivack620d2dc2020-03-06 13:58:01 -0800620 // Set this flag to ensure the clients are acknowledged in the next callback
621 serviceIt->second.guaranteeClient = true;
Jon Spivack9f503a42019-10-22 16:49:19 -0700622 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
623 }
624
625 mNameToService.erase(name);
626
627 return Status::ok();
628}
629
Steven Moreland3ea43272021-01-28 22:49:28 +0000630Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) {
631 if (!mAccess->canList(mAccess->getCallingContext())) {
632 return Status::fromExceptionCode(Status::EX_SECURITY);
633 }
634
635 outReturn->reserve(mNameToService.size());
636 for (auto const& [name, service] : mNameToService) {
637 ServiceDebugInfo info;
638 info.name = name;
639 info.debugPid = service.debugPid;
640
641 outReturn->push_back(std::move(info));
642 }
643
644 return Status::ok();
645}
646
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700647} // namespace android