blob: a99ccae2f1a65fa96e0ac08f776e2157cc610109 [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
Steven Moreland7ee423b2022-09-24 03:52:08 +0000329 auto it = mNameToService.find(name);
330 if (it != mNameToService.end()) {
331 const Service& existing = it->second;
332
333 // We could do better than this because if the other service dies, it
334 // may not have an entry here. However, this case is unlikely. We are
335 // only trying to detect when two different services are accidentally installed.
336
337 if (existing.ctx.uid != ctx.uid) {
338 LOG(WARNING) << "Service '" << name << "' originally registered from UID "
339 << existing.ctx.uid << " but it is now being registered from UID "
340 << ctx.uid << ". Multiple instances installed?";
341 }
342
343 if (existing.ctx.sid != ctx.sid) {
344 LOG(WARNING) << "Service '" << name << "' originally registered from SID "
345 << existing.ctx.sid << " but it is now being registered from SID "
346 << ctx.sid << ". Multiple instances installed?";
347 }
348
349 LOG(INFO) << "Service '" << name << "' originally registered from PID "
350 << existing.ctx.debugPid << " but it is being registered again from PID "
351 << ctx.debugPid
352 << ". Bad state? Late death notification? Multiple instances installed?";
353 }
354
Devin Moore05ffe522020-08-06 13:58:29 -0700355 // Overwrite the old service if it exists
Steven Moreland7ee423b2022-09-24 03:52:08 +0000356 mNameToService[name] = Service{
357 .binder = binder,
358 .allowIsolated = allowIsolated,
359 .dumpPriority = dumpPriority,
360 .ctx = ctx,
Devin Moore05ffe522020-08-06 13:58:29 -0700361 };
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700362
Steven Moreland7ee423b2022-09-24 03:52:08 +0000363 if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700364 for (const sp<IServiceCallback>& cb : it->second) {
Devin Moore05ffe522020-08-06 13:58:29 -0700365 mNameToService[name].guaranteeClient = true;
Steven Moreland27cfab02019-08-12 14:34:16 -0700366 // permission checked in registerForNotifications
367 cb->onRegistration(name, binder);
368 }
369 }
370
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700371 return Status::ok();
372}
373
374Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700375 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700376 return Status::fromExceptionCode(Status::EX_SECURITY);
377 }
378
379 size_t toReserve = 0;
380 for (auto const& [name, service] : mNameToService) {
381 (void) name;
382
383 if (service.dumpPriority & dumpPriority) ++toReserve;
384 }
385
386 CHECK(outList->empty());
387
388 outList->reserve(toReserve);
389 for (auto const& [name, service] : mNameToService) {
390 (void) service;
391
392 if (service.dumpPriority & dumpPriority) {
393 outList->push_back(name);
394 }
395 }
396
397 return Status::ok();
398}
399
Steven Moreland27cfab02019-08-12 14:34:16 -0700400Status ServiceManager::registerForNotifications(
401 const std::string& name, const sp<IServiceCallback>& callback) {
402 auto ctx = mAccess->getCallingContext();
403
404 if (!mAccess->canFind(ctx, name)) {
405 return Status::fromExceptionCode(Status::EX_SECURITY);
406 }
407
408 if (!isValidServiceName(name)) {
409 LOG(ERROR) << "Invalid service name: " << name;
410 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
411 }
412
413 if (callback == nullptr) {
414 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
415 }
416
Steven Morelandb0983182021-04-02 03:14:04 +0000417 if (OK !=
418 IInterface::asBinder(callback)->linkToDeath(
419 sp<ServiceManager>::fromExisting(this))) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700420 LOG(ERROR) << "Could not linkToDeath when adding " << name;
421 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
422 }
423
Jon Spivackf288b1d2019-12-19 17:15:51 -0800424 mNameToRegistrationCallback[name].push_back(callback);
Steven Moreland27cfab02019-08-12 14:34:16 -0700425
426 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
427 const sp<IBinder>& binder = it->second.binder;
428
429 // never null if an entry exists
430 CHECK(binder != nullptr) << name;
431 callback->onRegistration(name, binder);
432 }
433
434 return Status::ok();
435}
436Status ServiceManager::unregisterForNotifications(
437 const std::string& name, const sp<IServiceCallback>& callback) {
438 auto ctx = mAccess->getCallingContext();
439
440 if (!mAccess->canFind(ctx, name)) {
441 return Status::fromExceptionCode(Status::EX_SECURITY);
442 }
443
444 bool found = false;
445
Jon Spivackf288b1d2019-12-19 17:15:51 -0800446 auto it = mNameToRegistrationCallback.find(name);
447 if (it != mNameToRegistrationCallback.end()) {
448 removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
Steven Moreland27cfab02019-08-12 14:34:16 -0700449 }
450
451 if (!found) {
452 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
453 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
454 }
455
456 return Status::ok();
457}
458
Steven Morelandb82b8f82019-10-28 10:52:34 -0700459Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
460 auto ctx = mAccess->getCallingContext();
461
462 if (!mAccess->canFind(ctx, name)) {
463 return Status::fromExceptionCode(Status::EX_SECURITY);
464 }
465
466 *outReturn = false;
467
468#ifndef VENDORSERVICEMANAGER
469 *outReturn = isVintfDeclared(name);
470#endif
471 return Status::ok();
472}
473
Steven Moreland2e293aa2020-09-23 00:25:16 +0000474binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) {
475 auto ctx = mAccess->getCallingContext();
476
477 std::vector<std::string> allInstances;
478#ifndef VENDORSERVICEMANAGER
479 allInstances = getVintfInstances(interface);
480#endif
481
482 outReturn->clear();
483
484 for (const std::string& instance : allInstances) {
Steven Moreland2e293aa2020-09-23 00:25:16 +0000485 if (mAccess->canFind(ctx, interface + "/" + instance)) {
486 outReturn->push_back(instance);
487 }
488 }
489
490 if (outReturn->size() == 0 && allInstances.size() != 0) {
491 return Status::fromExceptionCode(Status::EX_SECURITY);
492 }
493
494 return Status::ok();
495}
496
Steven Morelandedd4e072021-04-21 00:27:29 +0000497Status ServiceManager::updatableViaApex(const std::string& name,
498 std::optional<std::string>* outReturn) {
499 auto ctx = mAccess->getCallingContext();
500
501 if (!mAccess->canFind(ctx, name)) {
502 return Status::fromExceptionCode(Status::EX_SECURITY);
503 }
504
505 *outReturn = std::nullopt;
506
507#ifndef VENDORSERVICEMANAGER
508 *outReturn = getVintfUpdatableApex(name);
509#endif
510 return Status::ok();
511}
512
Devin Moore5e4c2f12021-09-09 22:36:33 +0000513Status ServiceManager::getConnectionInfo(const std::string& name,
514 std::optional<ConnectionInfo>* outReturn) {
515 auto ctx = mAccess->getCallingContext();
516
517 if (!mAccess->canFind(ctx, name)) {
518 return Status::fromExceptionCode(Status::EX_SECURITY);
519 }
520
521 *outReturn = std::nullopt;
522
523#ifndef VENDORSERVICEMANAGER
524 *outReturn = getVintfConnectionInfo(name);
525#endif
526 return Status::ok();
527}
528
Jon Spivackf288b1d2019-12-19 17:15:51 -0800529void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
530 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700531 bool* found) {
532 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
533
534 for (auto lit = listeners.begin(); lit != listeners.end();) {
535 if (IInterface::asBinder(*lit) == who) {
536 if(found) *found = true;
537 lit = listeners.erase(lit);
538 } else {
539 ++lit;
540 }
541 }
542
543 if (listeners.empty()) {
Jon Spivackf288b1d2019-12-19 17:15:51 -0800544 *it = mNameToRegistrationCallback.erase(*it);
Steven Moreland27cfab02019-08-12 14:34:16 -0700545 } else {
Jon Spivacke223f082019-11-19 16:21:20 -0800546 (*it)++;
Steven Moreland27cfab02019-08-12 14:34:16 -0700547 }
548}
549
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700550void ServiceManager::binderDied(const wp<IBinder>& who) {
551 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
552 if (who == it->second.binder) {
553 it = mNameToService.erase(it);
554 } else {
555 ++it;
556 }
557 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700558
Jon Spivackf288b1d2019-12-19 17:15:51 -0800559 for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
560 removeRegistrationCallback(who, &it, nullptr /*found*/);
Steven Moreland27cfab02019-08-12 14:34:16 -0700561 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700562
563 for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
564 removeClientCallback(who, &it);
565 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700566}
567
Jon Spivack0d844302019-07-22 18:40:34 -0700568void ServiceManager::tryStartService(const std::string& name) {
569 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service",
570 name.c_str());
571
572 std::thread([=] {
Steven Morelandbfe9fba2021-04-27 18:39:57 +0000573 if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
574 LOG(INFO) << "Tried to start aidl service " << name
575 << " as a lazy service, but was unable to. Usually this happens when a "
576 "service is not installed, but if the service is intended to be used as a "
577 "lazy service, then it may be configured incorrectly.";
578 }
Jon Spivack0d844302019-07-22 18:40:34 -0700579 }).detach();
580}
581
Jon Spivack9f503a42019-10-22 16:49:19 -0700582Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
583 const sp<IClientCallback>& cb) {
584 if (cb == nullptr) {
585 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
586 }
587
588 auto ctx = mAccess->getCallingContext();
589 if (!mAccess->canAdd(ctx, name)) {
590 return Status::fromExceptionCode(Status::EX_SECURITY);
591 }
592
593 auto serviceIt = mNameToService.find(name);
594 if (serviceIt == mNameToService.end()) {
595 LOG(ERROR) << "Could not add callback for nonexistent service: " << name;
596 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
597 }
598
Steven Moreland7ee423b2022-09-24 03:52:08 +0000599 if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700600 LOG(WARNING) << "Only a server can register for client callbacks (for " << name << ")";
601 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
602 }
603
604 if (serviceIt->second.binder != service) {
605 LOG(WARNING) << "Tried to register client callback for " << name
606 << " but a different service is registered under this name.";
607 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
608 }
609
Steven Morelandb0983182021-04-02 03:14:04 +0000610 if (OK !=
611 IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700612 LOG(ERROR) << "Could not linkToDeath when adding client callback for " << name;
613 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
614 }
615
616 mNameToClientCallback[name].push_back(cb);
617
618 return Status::ok();
619}
620
621void ServiceManager::removeClientCallback(const wp<IBinder>& who,
622 ClientCallbackMap::iterator* it) {
623 std::vector<sp<IClientCallback>>& listeners = (*it)->second;
624
625 for (auto lit = listeners.begin(); lit != listeners.end();) {
626 if (IInterface::asBinder(*lit) == who) {
627 lit = listeners.erase(lit);
628 } else {
629 ++lit;
630 }
631 }
632
633 if (listeners.empty()) {
634 *it = mNameToClientCallback.erase(*it);
635 } else {
636 (*it)++;
637 }
638}
639
640ssize_t ServiceManager::Service::getNodeStrongRefCount() {
Steven Morelandb0983182021-04-02 03:14:04 +0000641 sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder());
Jon Spivack9f503a42019-10-22 16:49:19 -0700642 if (bpBinder == nullptr) return -1;
643
Steven Morelande8393882020-12-18 02:27:20 +0000644 return ProcessState::self()->getStrongRefCountForNode(bpBinder);
Jon Spivack9f503a42019-10-22 16:49:19 -0700645}
646
647void ServiceManager::handleClientCallbacks() {
648 for (const auto& [name, service] : mNameToService) {
Jon Spivackd9533c22020-01-27 22:19:22 +0000649 handleServiceClientCallback(name, true);
Jon Spivack9f503a42019-10-22 16:49:19 -0700650 }
651}
652
Jon Spivackd9533c22020-01-27 22:19:22 +0000653ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName,
654 bool isCalledOnInterval) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700655 auto serviceIt = mNameToService.find(serviceName);
656 if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
657 return -1;
658 }
659
660 Service& service = serviceIt->second;
661 ssize_t count = service.getNodeStrongRefCount();
662
663 // binder driver doesn't support this feature
664 if (count == -1) return count;
665
666 bool hasClients = count > 1; // this process holds a strong count
667
668 if (service.guaranteeClient) {
669 // we have no record of this client
670 if (!service.hasClients && !hasClients) {
671 sendClientCallbackNotifications(serviceName, true);
672 }
673
674 // guarantee is temporary
675 service.guaranteeClient = false;
676 }
677
Jon Spivackd9533c22020-01-27 22:19:22 +0000678 // only send notifications if this was called via the interval checking workflow
679 if (isCalledOnInterval) {
680 if (hasClients && !service.hasClients) {
681 // client was retrieved in some other way
682 sendClientCallbackNotifications(serviceName, true);
683 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700684
Jon Spivackd9533c22020-01-27 22:19:22 +0000685 // there are no more clients, but the callback has not been called yet
686 if (!hasClients && service.hasClients) {
687 sendClientCallbackNotifications(serviceName, false);
688 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700689 }
690
691 return count;
692}
693
694void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, bool hasClients) {
695 auto serviceIt = mNameToService.find(serviceName);
696 if (serviceIt == mNameToService.end()) {
697 LOG(WARNING) << "sendClientCallbackNotifications could not find service " << serviceName;
698 return;
699 }
700 Service& service = serviceIt->second;
701
702 CHECK(hasClients != service.hasClients) << "Record shows: " << service.hasClients
703 << " so we can't tell clients again that we have client: " << hasClients;
704
705 LOG(INFO) << "Notifying " << serviceName << " they have clients: " << hasClients;
706
707 auto ccIt = mNameToClientCallback.find(serviceName);
708 CHECK(ccIt != mNameToClientCallback.end())
709 << "sendClientCallbackNotifications could not find callbacks for service ";
710
711 for (const auto& callback : ccIt->second) {
712 callback->onClients(service.binder, hasClients);
713 }
714
715 service.hasClients = hasClients;
716}
717
718Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
719 if (binder == nullptr) {
720 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
721 }
722
723 auto ctx = mAccess->getCallingContext();
724 if (!mAccess->canAdd(ctx, name)) {
725 return Status::fromExceptionCode(Status::EX_SECURITY);
726 }
727
728 auto serviceIt = mNameToService.find(name);
729 if (serviceIt == mNameToService.end()) {
730 LOG(WARNING) << "Tried to unregister " << name
731 << ", but that service wasn't registered to begin with.";
732 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
733 }
734
Steven Moreland7ee423b2022-09-24 03:52:08 +0000735 if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700736 LOG(WARNING) << "Only a server can unregister itself (for " << name << ")";
737 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
738 }
739
740 sp<IBinder> storedBinder = serviceIt->second.binder;
741
742 if (binder != storedBinder) {
743 LOG(WARNING) << "Tried to unregister " << name
744 << ", but a different service is registered under this name.";
745 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
746 }
747
Jon Spivack0f18f2c2020-03-13 20:45:18 -0700748 if (serviceIt->second.guaranteeClient) {
749 LOG(INFO) << "Tried to unregister " << name << ", but there is about to be a client.";
750 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
751 }
752
Jon Spivackd9533c22020-01-27 22:19:22 +0000753 int clients = handleServiceClientCallback(name, false);
Jon Spivack9f503a42019-10-22 16:49:19 -0700754
755 // clients < 0: feature not implemented or other error. Assume clients.
756 // Otherwise:
757 // - kernel driver will hold onto one refcount (during this transaction)
758 // - servicemanager has a refcount (guaranteed by this transaction)
759 // So, if clients > 2, then at least one other service on the system must hold a refcount.
760 if (clients < 0 || clients > 2) {
761 // client callbacks are either disabled or there are other clients
Jon Spivackd9533c22020-01-27 22:19:22 +0000762 LOG(INFO) << "Tried to unregister " << name << ", but there are clients: " << clients;
Jon Spivack620d2dc2020-03-06 13:58:01 -0800763 // Set this flag to ensure the clients are acknowledged in the next callback
764 serviceIt->second.guaranteeClient = true;
Jon Spivack9f503a42019-10-22 16:49:19 -0700765 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
766 }
767
768 mNameToService.erase(name);
769
770 return Status::ok();
771}
772
Steven Moreland3ea43272021-01-28 22:49:28 +0000773Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) {
774 if (!mAccess->canList(mAccess->getCallingContext())) {
775 return Status::fromExceptionCode(Status::EX_SECURITY);
776 }
777
778 outReturn->reserve(mNameToService.size());
779 for (auto const& [name, service] : mNameToService) {
780 ServiceDebugInfo info;
781 info.name = name;
Steven Moreland7ee423b2022-09-24 03:52:08 +0000782 info.debugPid = service.ctx.debugPid;
Steven Moreland3ea43272021-01-28 22:49:28 +0000783
784 outReturn->push_back(std::move(info));
785 }
786
787 return Status::ok();
788}
789
Pawan Wagh243888e2022-09-20 19:37:35 +0000790void ServiceManager::clear() {
791 mNameToService.clear();
792 mNameToRegistrationCallback.clear();
793 mNameToClientCallback.clear();
794}
795
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700796} // namespace android