blob: 07273835f9bacb0b8a95eacdb4cdbc8de2bfe976 [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) {
52 LOG(ERROR) << "NULL VintfObjectRecovery!";
53 return {};
54 }
55 return {ManifestWithDescription{vintfObject->getRecoveryHalManifest(), "recovery"}};
56#else
57 auto vintfObject = vintf::VintfObject::GetInstance();
58 if (vintfObject == nullptr) {
59 LOG(ERROR) << "NULL VintfObject!";
60 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) {
71 LOG(ERROR) << "NULL VINTF MANIFEST!: " << mwd.description;
72 // note, we explicitly do not retry here, so that we can detect VINTF
73 // or other bugs (b/151696835)
74 continue;
75 }
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) {
90 LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. "
91 << "some.package.foo.IFoo/default) but got: " << name;
92 return false;
93 }
94 aname->package = name.substr(0, lastDot);
95 aname->iface = name.substr(lastDot + 1, firstSlash - lastDot - 1);
96 aname->instance = name.substr(firstSlash + 1);
97 return true;
98 }
99};
100
101static bool isVintfDeclared(const std::string& name) {
102 AidlName aname;
103 if (!AidlName::fill(name, &aname)) return false;
104
105 bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
106 if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) {
Steven Moreland2edde8e2020-04-30 17:04:54 -0700107 LOG(INFO) << "Found " << name << " in " << mwd.description << " VINTF manifest.";
Steven Morelandedd4e072021-04-21 00:27:29 +0000108 return true; // break
Steven Moreland86a17f82019-09-10 10:18:00 -0700109 }
Steven Moreland2e293aa2020-09-23 00:25:16 +0000110 return false; // continue
111 });
112
113 if (!found) {
114 // Although it is tested, explicitly rebuilding qualified name, in case it
115 // becomes something unexpected.
Steven Moreland3def9c42022-01-18 22:43:38 +0000116 LOG(INFO) << "Could not find " << aname.package << "." << aname.iface << "/"
117 << aname.instance << " in the VINTF manifest.";
Steven Moreland86a17f82019-09-10 10:18:00 -0700118 }
Steven Moreland2edde8e2020-04-30 17:04:54 -0700119
Steven Moreland2e293aa2020-09-23 00:25:16 +0000120 return found;
121}
122
Steven Morelandedd4e072021-04-21 00:27:29 +0000123static std::optional<std::string> getVintfUpdatableApex(const std::string& name) {
124 AidlName aname;
125 if (!AidlName::fill(name, &aname)) return std::nullopt;
126
127 std::optional<std::string> updatableViaApex;
128
129 forEachManifest([&](const ManifestWithDescription& mwd) {
130 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
131 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
132 if (manifestInstance.package() != aname.package) return true;
133 if (manifestInstance.interface() != aname.iface) return true;
134 if (manifestInstance.instance() != aname.instance) return true;
135 updatableViaApex = manifestInstance.updatableViaApex();
136 return false; // break (libvintf uses opposite convention)
137 });
138 return false; // continue
139 });
140
141 return updatableViaApex;
142}
143
Devin Moore5e4c2f12021-09-09 22:36:33 +0000144static std::optional<ConnectionInfo> getVintfConnectionInfo(const std::string& name) {
145 AidlName aname;
146 if (!AidlName::fill(name, &aname)) return std::nullopt;
147
148 std::optional<std::string> ip;
149 std::optional<uint64_t> port;
150 forEachManifest([&](const ManifestWithDescription& mwd) {
151 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
152 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
153 if (manifestInstance.package() != aname.package) return true;
154 if (manifestInstance.interface() != aname.iface) return true;
155 if (manifestInstance.instance() != aname.instance) return true;
156 ip = manifestInstance.ip();
157 port = manifestInstance.port();
158 return false; // break (libvintf uses opposite convention)
159 });
160 return false; // continue
161 });
162
163 if (ip.has_value() && port.has_value()) {
164 ConnectionInfo info;
165 info.ipAddress = *ip;
166 info.port = *port;
167 return std::make_optional<ConnectionInfo>(info);
168 } else {
169 return std::nullopt;
170 }
171}
172
Steven Moreland2e293aa2020-09-23 00:25:16 +0000173static std::vector<std::string> getVintfInstances(const std::string& interface) {
174 size_t lastDot = interface.rfind('.');
175 if (lastDot == std::string::npos) {
176 LOG(ERROR) << "VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) but got: " << interface;
177 return {};
178 }
179 const std::string package = interface.substr(0, lastDot);
180 const std::string iface = interface.substr(lastDot+1);
181
182 std::vector<std::string> ret;
183 (void)forEachManifest([&](const ManifestWithDescription& mwd) {
184 auto instances = mwd.manifest->getAidlInstances(package, iface);
185 ret.insert(ret.end(), instances.begin(), instances.end());
186 return false; // continue
187 });
188
189 return ret;
Steven Moreland86a17f82019-09-10 10:18:00 -0700190}
Steven Morelandb82b8f82019-10-28 10:52:34 -0700191
192static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) {
193 if (!Stability::requiresVintfDeclaration(binder)) {
194 return true;
195 }
196
197 return isVintfDeclared(name);
198}
Steven Moreland86a17f82019-09-10 10:18:00 -0700199#endif // !VENDORSERVICEMANAGER
200
Steven Morelandd13f08b2019-11-18 14:23:09 -0800201ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700202// TODO(b/151696835): reenable performance hack when we solve bug, since with
203// this hack and other fixes, it is unlikely we will see even an ephemeral
204// failure when the manifest parse fails. The goal is that the manifest will
205// be read incorrectly and cause the process trying to register a HAL to
206// fail. If this is in fact an early boot kernel contention issue, then we
207// will get no failure, and by its absence, be signalled to invest more
208// effort in re-adding this performance hack.
209// #ifndef VENDORSERVICEMANAGER
210// // can process these at any times, don't want to delay first VINTF client
211// std::thread([] {
212// vintf::VintfObject::GetDeviceHalManifest();
213// vintf::VintfObject::GetFrameworkHalManifest();
214// }).detach();
215// #endif // !VENDORSERVICEMANAGER
Steven Morelandd13f08b2019-11-18 14:23:09 -0800216}
Steven Moreland130242d2019-08-26 17:41:32 -0700217ServiceManager::~ServiceManager() {
218 // this should only happen in tests
219
Jon Spivackf288b1d2019-12-19 17:15:51 -0800220 for (const auto& [name, callbacks] : mNameToRegistrationCallback) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700221 CHECK(!callbacks.empty()) << name;
222 for (const auto& callback : callbacks) {
223 CHECK(callback != nullptr) << name;
224 }
225 }
226
Steven Moreland130242d2019-08-26 17:41:32 -0700227 for (const auto& [name, service] : mNameToService) {
228 CHECK(service.binder != nullptr) << name;
229 }
230}
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700231
232Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700233 *outBinder = tryGetService(name, true);
234 // returns ok regardless of result for legacy reasons
235 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700236}
237
238Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700239 *outBinder = tryGetService(name, false);
240 // returns ok regardless of result for legacy reasons
241 return Status::ok();
242}
243
244sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700245 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700246
Jon Spivack0d844302019-07-22 18:40:34 -0700247 sp<IBinder> out;
Jon Spivack9f503a42019-10-22 16:49:19 -0700248 Service* service = nullptr;
Jon Spivack0d844302019-07-22 18:40:34 -0700249 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700250 service = &(it->second);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700251
Jon Spivack9f503a42019-10-22 16:49:19 -0700252 if (!service->allowIsolated) {
Jon Spivack0d844302019-07-22 18:40:34 -0700253 uid_t appid = multiuser_get_app_id(ctx.uid);
254 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700255
Jon Spivack0d844302019-07-22 18:40:34 -0700256 if (isIsolated) {
257 return nullptr;
258 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700259 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700260 out = service->binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700261 }
262
Steven Morelanda9fe4742019-07-18 14:45:20 -0700263 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700264 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700265 }
266
Jon Spivack0d844302019-07-22 18:40:34 -0700267 if (!out && startIfNotFound) {
268 tryStartService(name);
269 }
270
Jon Spivack9f503a42019-10-22 16:49:19 -0700271 if (out) {
272 // Setting this guarantee each time we hand out a binder ensures that the client-checking
273 // loop knows about the event even if the client immediately drops the service
274 service->guaranteeClient = true;
275 }
276
Jon Spivack0d844302019-07-22 18:40:34 -0700277 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700278}
279
Steven Moreland905e2e82019-07-17 11:05:45 -0700280bool isValidServiceName(const std::string& name) {
281 if (name.size() == 0) return false;
282 if (name.size() > 127) return false;
283
284 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700285 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700286 if (c >= 'a' && c <= 'z') continue;
287 if (c >= 'A' && c <= 'Z') continue;
288 if (c >= '0' && c <= '9') continue;
289 return false;
290 }
291
292 return true;
293}
294
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700295Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700296 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700297
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700298 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
Steven Morelandac2d2852022-03-18 18:15:20 +0000299 return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700300 }
301
Steven Morelanda9fe4742019-07-18 14:45:20 -0700302 if (!mAccess->canAdd(ctx, name)) {
Steven Morelandac2d2852022-03-18 18:15:20 +0000303 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denial");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700304 }
305
306 if (binder == nullptr) {
Steven Morelandac2d2852022-03-18 18:15:20 +0000307 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700308 }
309
Steven Moreland905e2e82019-07-17 11:05:45 -0700310 if (!isValidServiceName(name)) {
311 LOG(ERROR) << "Invalid service name: " << name;
Steven Morelandac2d2852022-03-18 18:15:20 +0000312 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700313 }
314
Steven Moreland86a17f82019-09-10 10:18:00 -0700315#ifndef VENDORSERVICEMANAGER
316 if (!meetsDeclarationRequirements(binder, name)) {
317 // already logged
Steven Morelandac2d2852022-03-18 18:15:20 +0000318 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error");
Steven Moreland86a17f82019-09-10 10:18:00 -0700319 }
320#endif // !VENDORSERVICEMANAGER
321
Steven Moreland88860b02019-08-12 14:24:14 -0700322 // implicitly unlinked when the binder is removed
Steven Morelandb0983182021-04-02 03:14:04 +0000323 if (binder->remoteBinder() != nullptr &&
324 binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700325 LOG(ERROR) << "Could not linkToDeath when adding " << name;
Steven Morelandac2d2852022-03-18 18:15:20 +0000326 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "linkToDeath failure");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700327 }
328
Devin Moore05ffe522020-08-06 13:58:29 -0700329 // Overwrite the old service if it exists
330 mNameToService[name] = Service {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700331 .binder = binder,
332 .allowIsolated = allowIsolated,
333 .dumpPriority = dumpPriority,
Jon Spivack9f503a42019-10-22 16:49:19 -0700334 .debugPid = ctx.debugPid,
Devin Moore05ffe522020-08-06 13:58:29 -0700335 };
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700336
Jon Spivackf288b1d2019-12-19 17:15:51 -0800337 auto it = mNameToRegistrationCallback.find(name);
338 if (it != mNameToRegistrationCallback.end()) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700339 for (const sp<IServiceCallback>& cb : it->second) {
Devin Moore05ffe522020-08-06 13:58:29 -0700340 mNameToService[name].guaranteeClient = true;
Steven Moreland27cfab02019-08-12 14:34:16 -0700341 // permission checked in registerForNotifications
342 cb->onRegistration(name, binder);
343 }
344 }
345
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700346 return Status::ok();
347}
348
349Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700350 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700351 return Status::fromExceptionCode(Status::EX_SECURITY);
352 }
353
354 size_t toReserve = 0;
355 for (auto const& [name, service] : mNameToService) {
356 (void) name;
357
358 if (service.dumpPriority & dumpPriority) ++toReserve;
359 }
360
361 CHECK(outList->empty());
362
363 outList->reserve(toReserve);
364 for (auto const& [name, service] : mNameToService) {
365 (void) service;
366
367 if (service.dumpPriority & dumpPriority) {
368 outList->push_back(name);
369 }
370 }
371
372 return Status::ok();
373}
374
Steven Moreland27cfab02019-08-12 14:34:16 -0700375Status ServiceManager::registerForNotifications(
376 const std::string& name, const sp<IServiceCallback>& callback) {
377 auto ctx = mAccess->getCallingContext();
378
379 if (!mAccess->canFind(ctx, name)) {
380 return Status::fromExceptionCode(Status::EX_SECURITY);
381 }
382
383 if (!isValidServiceName(name)) {
384 LOG(ERROR) << "Invalid service name: " << name;
385 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
386 }
387
388 if (callback == nullptr) {
389 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
390 }
391
Steven Morelandb0983182021-04-02 03:14:04 +0000392 if (OK !=
393 IInterface::asBinder(callback)->linkToDeath(
394 sp<ServiceManager>::fromExisting(this))) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700395 LOG(ERROR) << "Could not linkToDeath when adding " << name;
396 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
397 }
398
Jon Spivackf288b1d2019-12-19 17:15:51 -0800399 mNameToRegistrationCallback[name].push_back(callback);
Steven Moreland27cfab02019-08-12 14:34:16 -0700400
401 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
402 const sp<IBinder>& binder = it->second.binder;
403
404 // never null if an entry exists
405 CHECK(binder != nullptr) << name;
406 callback->onRegistration(name, binder);
407 }
408
409 return Status::ok();
410}
411Status ServiceManager::unregisterForNotifications(
412 const std::string& name, const sp<IServiceCallback>& callback) {
413 auto ctx = mAccess->getCallingContext();
414
415 if (!mAccess->canFind(ctx, name)) {
416 return Status::fromExceptionCode(Status::EX_SECURITY);
417 }
418
419 bool found = false;
420
Jon Spivackf288b1d2019-12-19 17:15:51 -0800421 auto it = mNameToRegistrationCallback.find(name);
422 if (it != mNameToRegistrationCallback.end()) {
423 removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
Steven Moreland27cfab02019-08-12 14:34:16 -0700424 }
425
426 if (!found) {
427 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
428 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
429 }
430
431 return Status::ok();
432}
433
Steven Morelandb82b8f82019-10-28 10:52:34 -0700434Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
435 auto ctx = mAccess->getCallingContext();
436
437 if (!mAccess->canFind(ctx, name)) {
438 return Status::fromExceptionCode(Status::EX_SECURITY);
439 }
440
441 *outReturn = false;
442
443#ifndef VENDORSERVICEMANAGER
444 *outReturn = isVintfDeclared(name);
445#endif
446 return Status::ok();
447}
448
Steven Moreland2e293aa2020-09-23 00:25:16 +0000449binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) {
450 auto ctx = mAccess->getCallingContext();
451
452 std::vector<std::string> allInstances;
453#ifndef VENDORSERVICEMANAGER
454 allInstances = getVintfInstances(interface);
455#endif
456
457 outReturn->clear();
458
459 for (const std::string& instance : allInstances) {
Steven Moreland2e293aa2020-09-23 00:25:16 +0000460 if (mAccess->canFind(ctx, interface + "/" + instance)) {
461 outReturn->push_back(instance);
462 }
463 }
464
465 if (outReturn->size() == 0 && allInstances.size() != 0) {
466 return Status::fromExceptionCode(Status::EX_SECURITY);
467 }
468
469 return Status::ok();
470}
471
Steven Morelandedd4e072021-04-21 00:27:29 +0000472Status ServiceManager::updatableViaApex(const std::string& name,
473 std::optional<std::string>* outReturn) {
474 auto ctx = mAccess->getCallingContext();
475
476 if (!mAccess->canFind(ctx, name)) {
477 return Status::fromExceptionCode(Status::EX_SECURITY);
478 }
479
480 *outReturn = std::nullopt;
481
482#ifndef VENDORSERVICEMANAGER
483 *outReturn = getVintfUpdatableApex(name);
484#endif
485 return Status::ok();
486}
487
Devin Moore5e4c2f12021-09-09 22:36:33 +0000488Status ServiceManager::getConnectionInfo(const std::string& name,
489 std::optional<ConnectionInfo>* outReturn) {
490 auto ctx = mAccess->getCallingContext();
491
492 if (!mAccess->canFind(ctx, name)) {
493 return Status::fromExceptionCode(Status::EX_SECURITY);
494 }
495
496 *outReturn = std::nullopt;
497
498#ifndef VENDORSERVICEMANAGER
499 *outReturn = getVintfConnectionInfo(name);
500#endif
501 return Status::ok();
502}
503
Jon Spivackf288b1d2019-12-19 17:15:51 -0800504void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
505 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700506 bool* found) {
507 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
508
509 for (auto lit = listeners.begin(); lit != listeners.end();) {
510 if (IInterface::asBinder(*lit) == who) {
511 if(found) *found = true;
512 lit = listeners.erase(lit);
513 } else {
514 ++lit;
515 }
516 }
517
518 if (listeners.empty()) {
Jon Spivackf288b1d2019-12-19 17:15:51 -0800519 *it = mNameToRegistrationCallback.erase(*it);
Steven Moreland27cfab02019-08-12 14:34:16 -0700520 } else {
Jon Spivacke223f082019-11-19 16:21:20 -0800521 (*it)++;
Steven Moreland27cfab02019-08-12 14:34:16 -0700522 }
523}
524
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700525void ServiceManager::binderDied(const wp<IBinder>& who) {
526 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
527 if (who == it->second.binder) {
528 it = mNameToService.erase(it);
529 } else {
530 ++it;
531 }
532 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700533
Jon Spivackf288b1d2019-12-19 17:15:51 -0800534 for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
535 removeRegistrationCallback(who, &it, nullptr /*found*/);
Steven Moreland27cfab02019-08-12 14:34:16 -0700536 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700537
538 for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
539 removeClientCallback(who, &it);
540 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700541}
542
Jon Spivack0d844302019-07-22 18:40:34 -0700543void ServiceManager::tryStartService(const std::string& name) {
544 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service",
545 name.c_str());
546
547 std::thread([=] {
Steven Morelandbfe9fba2021-04-27 18:39:57 +0000548 if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
549 LOG(INFO) << "Tried to start aidl service " << name
550 << " as a lazy service, but was unable to. Usually this happens when a "
551 "service is not installed, but if the service is intended to be used as a "
552 "lazy service, then it may be configured incorrectly.";
553 }
Jon Spivack0d844302019-07-22 18:40:34 -0700554 }).detach();
555}
556
Jon Spivack9f503a42019-10-22 16:49:19 -0700557Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
558 const sp<IClientCallback>& cb) {
559 if (cb == nullptr) {
560 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
561 }
562
563 auto ctx = mAccess->getCallingContext();
564 if (!mAccess->canAdd(ctx, name)) {
565 return Status::fromExceptionCode(Status::EX_SECURITY);
566 }
567
568 auto serviceIt = mNameToService.find(name);
569 if (serviceIt == mNameToService.end()) {
570 LOG(ERROR) << "Could not add callback for nonexistent service: " << name;
571 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
572 }
573
574 if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) {
575 LOG(WARNING) << "Only a server can register for client callbacks (for " << name << ")";
576 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
577 }
578
579 if (serviceIt->second.binder != service) {
580 LOG(WARNING) << "Tried to register client callback for " << name
581 << " but a different service is registered under this name.";
582 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
583 }
584
Steven Morelandb0983182021-04-02 03:14:04 +0000585 if (OK !=
586 IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700587 LOG(ERROR) << "Could not linkToDeath when adding client callback for " << name;
588 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
589 }
590
591 mNameToClientCallback[name].push_back(cb);
592
593 return Status::ok();
594}
595
596void ServiceManager::removeClientCallback(const wp<IBinder>& who,
597 ClientCallbackMap::iterator* it) {
598 std::vector<sp<IClientCallback>>& listeners = (*it)->second;
599
600 for (auto lit = listeners.begin(); lit != listeners.end();) {
601 if (IInterface::asBinder(*lit) == who) {
602 lit = listeners.erase(lit);
603 } else {
604 ++lit;
605 }
606 }
607
608 if (listeners.empty()) {
609 *it = mNameToClientCallback.erase(*it);
610 } else {
611 (*it)++;
612 }
613}
614
615ssize_t ServiceManager::Service::getNodeStrongRefCount() {
Steven Morelandb0983182021-04-02 03:14:04 +0000616 sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder());
Jon Spivack9f503a42019-10-22 16:49:19 -0700617 if (bpBinder == nullptr) return -1;
618
Steven Morelande8393882020-12-18 02:27:20 +0000619 return ProcessState::self()->getStrongRefCountForNode(bpBinder);
Jon Spivack9f503a42019-10-22 16:49:19 -0700620}
621
622void ServiceManager::handleClientCallbacks() {
623 for (const auto& [name, service] : mNameToService) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000624 handleServiceClientCallback(name, true);
Jon Spivack9f503a42019-10-22 16:49:19 -0700625 }
626}
627
Jon Spivackd9533c22020-01-27 22:19:22 +0000628ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName,
629 bool isCalledOnInterval) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700630 auto serviceIt = mNameToService.find(serviceName);
631 if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
632 return -1;
633 }
634
635 Service& service = serviceIt->second;
636 ssize_t count = service.getNodeStrongRefCount();
637
638 // binder driver doesn't support this feature
639 if (count == -1) return count;
640
641 bool hasClients = count > 1; // this process holds a strong count
642
643 if (service.guaranteeClient) {
644 // we have no record of this client
645 if (!service.hasClients && !hasClients) {
646 sendClientCallbackNotifications(serviceName, true);
647 }
648
649 // guarantee is temporary
650 service.guaranteeClient = false;
651 }
652
Jon Spivackd9533c22020-01-27 22:19:22 +0000653 // only send notifications if this was called via the interval checking workflow
654 if (isCalledOnInterval) {
655 if (hasClients && !service.hasClients) {
656 // client was retrieved in some other way
657 sendClientCallbackNotifications(serviceName, true);
658 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700659
Jon Spivackd9533c22020-01-27 22:19:22 +0000660 // there are no more clients, but the callback has not been called yet
661 if (!hasClients && service.hasClients) {
662 sendClientCallbackNotifications(serviceName, false);
663 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700664 }
665
666 return count;
667}
668
669void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, bool hasClients) {
670 auto serviceIt = mNameToService.find(serviceName);
671 if (serviceIt == mNameToService.end()) {
672 LOG(WARNING) << "sendClientCallbackNotifications could not find service " << serviceName;
673 return;
674 }
675 Service& service = serviceIt->second;
676
677 CHECK(hasClients != service.hasClients) << "Record shows: " << service.hasClients
678 << " so we can't tell clients again that we have client: " << hasClients;
679
680 LOG(INFO) << "Notifying " << serviceName << " they have clients: " << hasClients;
681
682 auto ccIt = mNameToClientCallback.find(serviceName);
683 CHECK(ccIt != mNameToClientCallback.end())
684 << "sendClientCallbackNotifications could not find callbacks for service ";
685
686 for (const auto& callback : ccIt->second) {
687 callback->onClients(service.binder, hasClients);
688 }
689
690 service.hasClients = hasClients;
691}
692
693Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
694 if (binder == nullptr) {
695 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
696 }
697
698 auto ctx = mAccess->getCallingContext();
699 if (!mAccess->canAdd(ctx, name)) {
700 return Status::fromExceptionCode(Status::EX_SECURITY);
701 }
702
703 auto serviceIt = mNameToService.find(name);
704 if (serviceIt == mNameToService.end()) {
705 LOG(WARNING) << "Tried to unregister " << name
706 << ", but that service wasn't registered to begin with.";
707 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
708 }
709
710 if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) {
711 LOG(WARNING) << "Only a server can unregister itself (for " << name << ")";
712 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
713 }
714
715 sp<IBinder> storedBinder = serviceIt->second.binder;
716
717 if (binder != storedBinder) {
718 LOG(WARNING) << "Tried to unregister " << name
719 << ", but a different service is registered under this name.";
720 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
721 }
722
Jon Spivack0f18f2c2020-03-13 20:45:18 -0700723 if (serviceIt->second.guaranteeClient) {
724 LOG(INFO) << "Tried to unregister " << name << ", but there is about to be a client.";
725 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
726 }
727
Jon Spivackd9533c22020-01-27 22:19:22 +0000728 int clients = handleServiceClientCallback(name, false);
Jon Spivack9f503a42019-10-22 16:49:19 -0700729
730 // clients < 0: feature not implemented or other error. Assume clients.
731 // Otherwise:
732 // - kernel driver will hold onto one refcount (during this transaction)
733 // - servicemanager has a refcount (guaranteed by this transaction)
734 // So, if clients > 2, then at least one other service on the system must hold a refcount.
735 if (clients < 0 || clients > 2) {
736 // client callbacks are either disabled or there are other clients
Jon Spivackd9533c22020-01-27 22:19:22 +0000737 LOG(INFO) << "Tried to unregister " << name << ", but there are clients: " << clients;
Jon Spivack620d2dc2020-03-06 13:58:01 -0800738 // Set this flag to ensure the clients are acknowledged in the next callback
739 serviceIt->second.guaranteeClient = true;
Jon Spivack9f503a42019-10-22 16:49:19 -0700740 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
741 }
742
743 mNameToService.erase(name);
744
745 return Status::ok();
746}
747
Steven Moreland3ea43272021-01-28 22:49:28 +0000748Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) {
749 if (!mAccess->canList(mAccess->getCallingContext())) {
750 return Status::fromExceptionCode(Status::EX_SECURITY);
751 }
752
753 outReturn->reserve(mNameToService.size());
754 for (auto const& [name, service] : mNameToService) {
755 ServiceDebugInfo info;
756 info.name = name;
757 info.debugPid = service.debugPid;
758
759 outReturn->push_back(std::move(info));
760 }
761
762 return Status::ok();
763}
764
Pawan Wagh243888e2022-09-20 19:37:35 +0000765void ServiceManager::clear() {
766 mNameToService.clear();
767 mNameToRegistrationCallback.clear();
768 mNameToClientCallback.clear();
769}
770
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700771} // namespace android