blob: cbbea128a6114682afcd11ac02e6bd0d1310a189 [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 Morelandb82b8f82019-10-28 10:52:34 -070040static bool isVintfDeclared(const std::string& name) {
Steven Moreland86a17f82019-09-10 10:18:00 -070041 size_t firstSlash = name.find('/');
42 size_t lastDot = name.rfind('.', firstSlash);
43 if (firstSlash == std::string::npos || lastDot == std::string::npos) {
44 LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. "
45 << "some.package.foo.IFoo/default) but got: " << name;
46 return false;
47 }
48 const std::string package = name.substr(0, lastDot);
49 const std::string iface = name.substr(lastDot+1, firstSlash-lastDot-1);
50 const std::string instance = name.substr(firstSlash+1);
51
52 for (const auto& manifest : {
53 vintf::VintfObject::GetDeviceHalManifest(),
54 vintf::VintfObject::GetFrameworkHalManifest()
55 }) {
Steven Moreland97d1d452019-11-01 10:08:34 -070056 if (manifest != nullptr && manifest->hasAidlInstance(package, iface, instance)) {
Steven Moreland86a17f82019-09-10 10:18:00 -070057 return true;
58 }
59 }
60 LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance
61 << " in the VINTF manifest.";
62 return false;
63}
Steven Morelandb82b8f82019-10-28 10:52:34 -070064
65static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) {
66 if (!Stability::requiresVintfDeclaration(binder)) {
67 return true;
68 }
69
70 return isVintfDeclared(name);
71}
Steven Moreland86a17f82019-09-10 10:18:00 -070072#endif // !VENDORSERVICEMANAGER
73
Steven Morelandd13f08b2019-11-18 14:23:09 -080074ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {
75#ifndef VENDORSERVICEMANAGER
76 // can process these at any times, don't want to delay first VINTF client
77 std::thread([] {
78 vintf::VintfObject::GetDeviceHalManifest();
79 vintf::VintfObject::GetFrameworkHalManifest();
80 }).detach();
81#endif // !VENDORSERVICEMANAGER
82}
Steven Moreland130242d2019-08-26 17:41:32 -070083ServiceManager::~ServiceManager() {
84 // this should only happen in tests
85
Jon Spivackf288b1d2019-12-19 17:15:51 -080086 for (const auto& [name, callbacks] : mNameToRegistrationCallback) {
Steven Moreland27cfab02019-08-12 14:34:16 -070087 CHECK(!callbacks.empty()) << name;
88 for (const auto& callback : callbacks) {
89 CHECK(callback != nullptr) << name;
90 }
91 }
92
Steven Moreland130242d2019-08-26 17:41:32 -070093 for (const auto& [name, service] : mNameToService) {
94 CHECK(service.binder != nullptr) << name;
95 }
96}
Steven Moreland80e1e6d2019-06-21 12:35:59 -070097
98Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -070099 *outBinder = tryGetService(name, true);
100 // returns ok regardless of result for legacy reasons
101 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700102}
103
104Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700105 *outBinder = tryGetService(name, false);
106 // returns ok regardless of result for legacy reasons
107 return Status::ok();
108}
109
110sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700111 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700112
Jon Spivack0d844302019-07-22 18:40:34 -0700113 sp<IBinder> out;
Jon Spivack9f503a42019-10-22 16:49:19 -0700114 Service* service = nullptr;
Jon Spivack0d844302019-07-22 18:40:34 -0700115 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700116 service = &(it->second);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700117
Jon Spivack9f503a42019-10-22 16:49:19 -0700118 if (!service->allowIsolated) {
Jon Spivack0d844302019-07-22 18:40:34 -0700119 uid_t appid = multiuser_get_app_id(ctx.uid);
120 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700121
Jon Spivack0d844302019-07-22 18:40:34 -0700122 if (isIsolated) {
123 return nullptr;
124 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700125 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700126 out = service->binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700127 }
128
Steven Morelanda9fe4742019-07-18 14:45:20 -0700129 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700130 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700131 }
132
Jon Spivack0d844302019-07-22 18:40:34 -0700133 if (!out && startIfNotFound) {
134 tryStartService(name);
135 }
136
Jon Spivack9f503a42019-10-22 16:49:19 -0700137 if (out) {
138 // Setting this guarantee each time we hand out a binder ensures that the client-checking
139 // loop knows about the event even if the client immediately drops the service
140 service->guaranteeClient = true;
141 }
142
Jon Spivack0d844302019-07-22 18:40:34 -0700143 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700144}
145
Steven Moreland905e2e82019-07-17 11:05:45 -0700146bool isValidServiceName(const std::string& name) {
147 if (name.size() == 0) return false;
148 if (name.size() > 127) return false;
149
150 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700151 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700152 if (c >= 'a' && c <= 'z') continue;
153 if (c >= 'A' && c <= 'Z') continue;
154 if (c >= '0' && c <= '9') continue;
155 return false;
156 }
157
158 return true;
159}
160
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700161Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700162 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700163
164 // apps cannot add services
165 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
166 return Status::fromExceptionCode(Status::EX_SECURITY);
167 }
168
Steven Morelanda9fe4742019-07-18 14:45:20 -0700169 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700170 return Status::fromExceptionCode(Status::EX_SECURITY);
171 }
172
173 if (binder == nullptr) {
174 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
175 }
176
Steven Moreland905e2e82019-07-17 11:05:45 -0700177 if (!isValidServiceName(name)) {
178 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700179 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
180 }
181
Steven Moreland86a17f82019-09-10 10:18:00 -0700182#ifndef VENDORSERVICEMANAGER
183 if (!meetsDeclarationRequirements(binder, name)) {
184 // already logged
185 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
186 }
187#endif // !VENDORSERVICEMANAGER
188
Steven Moreland88860b02019-08-12 14:24:14 -0700189 // implicitly unlinked when the binder is removed
Steven Moreland46f380b2019-10-16 16:28:21 -0700190 if (binder->remoteBinder() != nullptr && binder->linkToDeath(this) != OK) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700191 LOG(ERROR) << "Could not linkToDeath when adding " << name;
192 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
193 }
194
Jon Spivack9f503a42019-10-22 16:49:19 -0700195 auto entry = mNameToService.emplace(name, Service {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700196 .binder = binder,
197 .allowIsolated = allowIsolated,
198 .dumpPriority = dumpPriority,
Jon Spivack9f503a42019-10-22 16:49:19 -0700199 .debugPid = ctx.debugPid,
200 });
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700201
Jon Spivackf288b1d2019-12-19 17:15:51 -0800202 auto it = mNameToRegistrationCallback.find(name);
203 if (it != mNameToRegistrationCallback.end()) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700204 for (const sp<IServiceCallback>& cb : it->second) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700205 entry.first->second.guaranteeClient = true;
Steven Moreland27cfab02019-08-12 14:34:16 -0700206 // permission checked in registerForNotifications
207 cb->onRegistration(name, binder);
208 }
209 }
210
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700211 return Status::ok();
212}
213
214Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700215 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700216 return Status::fromExceptionCode(Status::EX_SECURITY);
217 }
218
219 size_t toReserve = 0;
220 for (auto const& [name, service] : mNameToService) {
221 (void) name;
222
223 if (service.dumpPriority & dumpPriority) ++toReserve;
224 }
225
226 CHECK(outList->empty());
227
228 outList->reserve(toReserve);
229 for (auto const& [name, service] : mNameToService) {
230 (void) service;
231
232 if (service.dumpPriority & dumpPriority) {
233 outList->push_back(name);
234 }
235 }
236
237 return Status::ok();
238}
239
Steven Moreland27cfab02019-08-12 14:34:16 -0700240Status ServiceManager::registerForNotifications(
241 const std::string& name, const sp<IServiceCallback>& callback) {
242 auto ctx = mAccess->getCallingContext();
243
244 if (!mAccess->canFind(ctx, name)) {
245 return Status::fromExceptionCode(Status::EX_SECURITY);
246 }
247
248 if (!isValidServiceName(name)) {
249 LOG(ERROR) << "Invalid service name: " << name;
250 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
251 }
252
253 if (callback == nullptr) {
254 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
255 }
256
257 if (OK != IInterface::asBinder(callback)->linkToDeath(this)) {
258 LOG(ERROR) << "Could not linkToDeath when adding " << name;
259 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
260 }
261
Jon Spivackf288b1d2019-12-19 17:15:51 -0800262 mNameToRegistrationCallback[name].push_back(callback);
Steven Moreland27cfab02019-08-12 14:34:16 -0700263
264 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
265 const sp<IBinder>& binder = it->second.binder;
266
267 // never null if an entry exists
268 CHECK(binder != nullptr) << name;
269 callback->onRegistration(name, binder);
270 }
271
272 return Status::ok();
273}
274Status ServiceManager::unregisterForNotifications(
275 const std::string& name, const sp<IServiceCallback>& callback) {
276 auto ctx = mAccess->getCallingContext();
277
278 if (!mAccess->canFind(ctx, name)) {
279 return Status::fromExceptionCode(Status::EX_SECURITY);
280 }
281
282 bool found = false;
283
Jon Spivackf288b1d2019-12-19 17:15:51 -0800284 auto it = mNameToRegistrationCallback.find(name);
285 if (it != mNameToRegistrationCallback.end()) {
286 removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
Steven Moreland27cfab02019-08-12 14:34:16 -0700287 }
288
289 if (!found) {
290 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
291 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
292 }
293
294 return Status::ok();
295}
296
Steven Morelandb82b8f82019-10-28 10:52:34 -0700297Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
298 auto ctx = mAccess->getCallingContext();
299
300 if (!mAccess->canFind(ctx, name)) {
301 return Status::fromExceptionCode(Status::EX_SECURITY);
302 }
303
304 *outReturn = false;
305
306#ifndef VENDORSERVICEMANAGER
307 *outReturn = isVintfDeclared(name);
308#endif
309 return Status::ok();
310}
311
Jon Spivackf288b1d2019-12-19 17:15:51 -0800312void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
313 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700314 bool* found) {
315 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
316
317 for (auto lit = listeners.begin(); lit != listeners.end();) {
318 if (IInterface::asBinder(*lit) == who) {
319 if(found) *found = true;
320 lit = listeners.erase(lit);
321 } else {
322 ++lit;
323 }
324 }
325
326 if (listeners.empty()) {
Jon Spivackf288b1d2019-12-19 17:15:51 -0800327 *it = mNameToRegistrationCallback.erase(*it);
Steven Moreland27cfab02019-08-12 14:34:16 -0700328 } else {
Jon Spivacke223f082019-11-19 16:21:20 -0800329 (*it)++;
Steven Moreland27cfab02019-08-12 14:34:16 -0700330 }
331}
332
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700333void ServiceManager::binderDied(const wp<IBinder>& who) {
334 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
335 if (who == it->second.binder) {
336 it = mNameToService.erase(it);
337 } else {
338 ++it;
339 }
340 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700341
Jon Spivackf288b1d2019-12-19 17:15:51 -0800342 for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
343 removeRegistrationCallback(who, &it, nullptr /*found*/);
Steven Moreland27cfab02019-08-12 14:34:16 -0700344 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700345
346 for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
347 removeClientCallback(who, &it);
348 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700349}
350
Jon Spivack0d844302019-07-22 18:40:34 -0700351void ServiceManager::tryStartService(const std::string& name) {
352 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service",
353 name.c_str());
354
355 std::thread([=] {
356 (void)base::SetProperty("ctl.interface_start", "aidl/" + name);
357 }).detach();
358}
359
Jon Spivack9f503a42019-10-22 16:49:19 -0700360Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
361 const sp<IClientCallback>& cb) {
362 if (cb == nullptr) {
363 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
364 }
365
366 auto ctx = mAccess->getCallingContext();
367 if (!mAccess->canAdd(ctx, name)) {
368 return Status::fromExceptionCode(Status::EX_SECURITY);
369 }
370
371 auto serviceIt = mNameToService.find(name);
372 if (serviceIt == mNameToService.end()) {
373 LOG(ERROR) << "Could not add callback for nonexistent service: " << name;
374 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
375 }
376
377 if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) {
378 LOG(WARNING) << "Only a server can register for client callbacks (for " << name << ")";
379 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
380 }
381
382 if (serviceIt->second.binder != service) {
383 LOG(WARNING) << "Tried to register client callback for " << name
384 << " but a different service is registered under this name.";
385 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
386 }
387
388 if (OK != IInterface::asBinder(cb)->linkToDeath(this)) {
389 LOG(ERROR) << "Could not linkToDeath when adding client callback for " << name;
390 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
391 }
392
393 mNameToClientCallback[name].push_back(cb);
394
395 return Status::ok();
396}
397
398void ServiceManager::removeClientCallback(const wp<IBinder>& who,
399 ClientCallbackMap::iterator* it) {
400 std::vector<sp<IClientCallback>>& listeners = (*it)->second;
401
402 for (auto lit = listeners.begin(); lit != listeners.end();) {
403 if (IInterface::asBinder(*lit) == who) {
404 lit = listeners.erase(lit);
405 } else {
406 ++lit;
407 }
408 }
409
410 if (listeners.empty()) {
411 *it = mNameToClientCallback.erase(*it);
412 } else {
413 (*it)++;
414 }
415}
416
417ssize_t ServiceManager::Service::getNodeStrongRefCount() {
418 sp<BpBinder> bpBinder = binder->remoteBinder();
419 if (bpBinder == nullptr) return -1;
420
421 return ProcessState::self()->getStrongRefCountForNodeByHandle(bpBinder->handle());
422}
423
424void ServiceManager::handleClientCallbacks() {
425 for (const auto& [name, service] : mNameToService) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000426 handleServiceClientCallback(name, true);
Jon Spivack9f503a42019-10-22 16:49:19 -0700427 }
428}
429
Jon Spivackd9533c22020-01-27 22:19:22 +0000430ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName,
431 bool isCalledOnInterval) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700432 auto serviceIt = mNameToService.find(serviceName);
433 if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
434 return -1;
435 }
436
437 Service& service = serviceIt->second;
438 ssize_t count = service.getNodeStrongRefCount();
439
440 // binder driver doesn't support this feature
441 if (count == -1) return count;
442
443 bool hasClients = count > 1; // this process holds a strong count
444
445 if (service.guaranteeClient) {
446 // we have no record of this client
447 if (!service.hasClients && !hasClients) {
448 sendClientCallbackNotifications(serviceName, true);
449 }
450
451 // guarantee is temporary
452 service.guaranteeClient = false;
453 }
454
Jon Spivackd9533c22020-01-27 22:19:22 +0000455 // only send notifications if this was called via the interval checking workflow
456 if (isCalledOnInterval) {
457 if (hasClients && !service.hasClients) {
458 // client was retrieved in some other way
459 sendClientCallbackNotifications(serviceName, true);
460 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700461
Jon Spivackd9533c22020-01-27 22:19:22 +0000462 // there are no more clients, but the callback has not been called yet
463 if (!hasClients && service.hasClients) {
464 sendClientCallbackNotifications(serviceName, false);
465 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700466 }
467
468 return count;
469}
470
471void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, bool hasClients) {
472 auto serviceIt = mNameToService.find(serviceName);
473 if (serviceIt == mNameToService.end()) {
474 LOG(WARNING) << "sendClientCallbackNotifications could not find service " << serviceName;
475 return;
476 }
477 Service& service = serviceIt->second;
478
479 CHECK(hasClients != service.hasClients) << "Record shows: " << service.hasClients
480 << " so we can't tell clients again that we have client: " << hasClients;
481
482 LOG(INFO) << "Notifying " << serviceName << " they have clients: " << hasClients;
483
484 auto ccIt = mNameToClientCallback.find(serviceName);
485 CHECK(ccIt != mNameToClientCallback.end())
486 << "sendClientCallbackNotifications could not find callbacks for service ";
487
488 for (const auto& callback : ccIt->second) {
489 callback->onClients(service.binder, hasClients);
490 }
491
492 service.hasClients = hasClients;
493}
494
495Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
496 if (binder == nullptr) {
497 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
498 }
499
500 auto ctx = mAccess->getCallingContext();
501 if (!mAccess->canAdd(ctx, name)) {
502 return Status::fromExceptionCode(Status::EX_SECURITY);
503 }
504
505 auto serviceIt = mNameToService.find(name);
506 if (serviceIt == mNameToService.end()) {
507 LOG(WARNING) << "Tried to unregister " << name
508 << ", but that service wasn't registered to begin with.";
509 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
510 }
511
512 if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) {
513 LOG(WARNING) << "Only a server can unregister itself (for " << name << ")";
514 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
515 }
516
517 sp<IBinder> storedBinder = serviceIt->second.binder;
518
519 if (binder != storedBinder) {
520 LOG(WARNING) << "Tried to unregister " << name
521 << ", but a different service is registered under this name.";
522 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
523 }
524
Jon Spivackbb108a12020-03-13 20:45:18 -0700525 if (serviceIt->second.guaranteeClient) {
526 LOG(INFO) << "Tried to unregister " << name << ", but there is about to be a client.";
527 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
528 }
529
Jon Spivackd9533c22020-01-27 22:19:22 +0000530 int clients = handleServiceClientCallback(name, false);
Jon Spivack9f503a42019-10-22 16:49:19 -0700531
532 // clients < 0: feature not implemented or other error. Assume clients.
533 // Otherwise:
534 // - kernel driver will hold onto one refcount (during this transaction)
535 // - servicemanager has a refcount (guaranteed by this transaction)
536 // So, if clients > 2, then at least one other service on the system must hold a refcount.
537 if (clients < 0 || clients > 2) {
538 // client callbacks are either disabled or there are other clients
Jon Spivackd9533c22020-01-27 22:19:22 +0000539 LOG(INFO) << "Tried to unregister " << name << ", but there are clients: " << clients;
Jon Spivacke17055a2020-03-06 13:58:01 -0800540 // Set this flag to ensure the clients are acknowledged in the next callback
541 serviceIt->second.guaranteeClient = true;
Jon Spivack9f503a42019-10-22 16:49:19 -0700542 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
543 }
544
545 mNameToService.erase(name);
546
547 return Status::ok();
548}
549
550} // namespace android