blob: 59c4d53bc04725e9667c5df6ef674e13b3057be3 [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>
Parth Sane5ade9f12024-05-19 13:02:07 +000021#include <android-base/scopeguard.h>
Devin Moore42407bc2023-09-26 21:30:39 +000022#include <android-base/strings.h>
Jon Spivack9f503a42019-10-22 16:49:19 -070023#include <binder/BpBinder.h>
24#include <binder/IPCThreadState.h>
25#include <binder/ProcessState.h>
Steven Moreland86a17f82019-09-10 10:18:00 -070026#include <binder/Stability.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070027#include <cutils/android_filesystem_config.h>
28#include <cutils/multiuser.h>
Jon Spivack0d844302019-07-22 18:40:34 -070029#include <thread>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070030
Parth Sane5ade9f12024-05-19 13:02:07 +000031#if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +000032#include "perfetto/public/protos/trace/android/android_track_event.pzc.h"
Parth Sane5ade9f12024-05-19 13:02:07 +000033#include "perfetto/public/te_category_macros.h"
34#include "perfetto/public/te_macros.h"
35#endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
36
Steven Moreland86a17f82019-09-10 10:18:00 -070037#ifndef VENDORSERVICEMANAGER
38#include <vintf/VintfObject.h>
Yifan Hong0a9b56e2021-11-30 16:45:40 -080039#ifdef __ANDROID_RECOVERY__
40#include <vintf/VintfObjectRecovery.h>
41#endif // __ANDROID_RECOVERY__
Steven Moreland86a17f82019-09-10 10:18:00 -070042#include <vintf/constants.h>
43#endif // !VENDORSERVICEMANAGER
44
Jooyung Han205e2822023-12-19 16:59:39 +090045#include "NameUtil.h"
46
Steven Moreland80e1e6d2019-06-21 12:35:59 -070047using ::android::binder::Status;
Steven Moreland86a17f82019-09-10 10:18:00 -070048using ::android::internal::Stability;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070049
50namespace android {
51
Parth Sane5ade9f12024-05-19 13:02:07 +000052#if defined(VENDORSERVICEMANAGER) || defined(__ANDROID_RECOVERY__)
53#define SM_PERFETTO_TRACE_FUNC(...)
54#else
55
56PERFETTO_TE_CATEGORIES_DEFINE(PERFETTO_SM_CATEGORIES);
57
58#define SM_PERFETTO_TRACE_FUNC(...) \
59 PERFETTO_TE_SCOPED(servicemanager, PERFETTO_TE_SLICE_BEGIN(__func__) __VA_OPT__(, ) __VA_ARGS__)
60
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +000061constexpr uint32_t kProtoServiceName =
62 perfetto_protos_AndroidTrackEvent_binder_service_name_field_number;
63constexpr uint32_t kProtoInterfaceName =
64 perfetto_protos_AndroidTrackEvent_binder_interface_name_field_number;
65constexpr uint32_t kProtoApexName = perfetto_protos_AndroidTrackEvent_apex_name_field_number;
66
Parth Sane5ade9f12024-05-19 13:02:07 +000067#endif // !(defined(VENDORSERVICEMANAGER) || defined(__ANDROID_RECOVERY__))
68
Steven Morelandb9e1cbe2023-02-01 22:44:45 +000069bool is_multiuser_uid_isolated(uid_t uid) {
70 uid_t appid = multiuser_get_app_id(uid);
71 return appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
72}
73
Steven Moreland86a17f82019-09-10 10:18:00 -070074#ifndef VENDORSERVICEMANAGER
Yifan Hong0a9b56e2021-11-30 16:45:40 -080075
Steven Moreland2e293aa2020-09-23 00:25:16 +000076struct ManifestWithDescription {
77 std::shared_ptr<const vintf::HalManifest> manifest;
78 const char* description;
79};
Yifan Hong0a9b56e2021-11-30 16:45:40 -080080static std::vector<ManifestWithDescription> GetManifestsWithDescription() {
81#ifdef __ANDROID_RECOVERY__
82 auto vintfObject = vintf::VintfObjectRecovery::GetInstance();
83 if (vintfObject == nullptr) {
Pawan Wagh37526162022-09-29 21:55:26 +000084 ALOGE("NULL VintfObjectRecovery!");
Yifan Hong0a9b56e2021-11-30 16:45:40 -080085 return {};
86 }
87 return {ManifestWithDescription{vintfObject->getRecoveryHalManifest(), "recovery"}};
88#else
89 auto vintfObject = vintf::VintfObject::GetInstance();
90 if (vintfObject == nullptr) {
Pawan Wagh37526162022-09-29 21:55:26 +000091 ALOGE("NULL VintfObject!");
Yifan Hong0a9b56e2021-11-30 16:45:40 -080092 return {};
93 }
94 return {ManifestWithDescription{vintfObject->getDeviceHalManifest(), "device"},
95 ManifestWithDescription{vintfObject->getFrameworkHalManifest(), "framework"}};
96#endif
97}
98
Steven Moreland2e293aa2020-09-23 00:25:16 +000099// func true -> stop search and forEachManifest will return true
100static bool forEachManifest(const std::function<bool(const ManifestWithDescription&)>& func) {
Yifan Hong0a9b56e2021-11-30 16:45:40 -0800101 for (const ManifestWithDescription& mwd : GetManifestsWithDescription()) {
Steven Moreland2e293aa2020-09-23 00:25:16 +0000102 if (mwd.manifest == nullptr) {
Pawan Wagh37526162022-09-29 21:55:26 +0000103 ALOGE("NULL VINTF MANIFEST!: %s", mwd.description);
104 // note, we explicitly do not retry here, so that we can detect VINTF
105 // or other bugs (b/151696835)
106 continue;
Steven Moreland2e293aa2020-09-23 00:25:16 +0000107 }
108 if (func(mwd)) return true;
109 }
110 return false;
111}
112
Jooyung Han205e2822023-12-19 16:59:39 +0900113static std::string getNativeInstanceName(const vintf::ManifestInstance& instance) {
114 return instance.package() + "/" + instance.instance();
115}
116
Steven Morelandedd4e072021-04-21 00:27:29 +0000117struct AidlName {
118 std::string package;
119 std::string iface;
120 std::string instance;
Steven Moreland86a17f82019-09-10 10:18:00 -0700121
Devin Moore9d1dfa02024-07-23 21:40:05 +0000122 static bool fill(const std::string& name, AidlName* aname, bool logError) {
Steven Morelandedd4e072021-04-21 00:27:29 +0000123 size_t firstSlash = name.find('/');
124 size_t lastDot = name.rfind('.', firstSlash);
125 if (firstSlash == std::string::npos || lastDot == std::string::npos) {
Devin Moore9d1dfa02024-07-23 21:40:05 +0000126 if (logError) {
127 ALOGE("VINTF HALs require names in the format type/instance (e.g. "
128 "some.package.foo.IFoo/default) but got: %s",
129 name.c_str());
130 }
Steven Morelandedd4e072021-04-21 00:27:29 +0000131 return false;
132 }
133 aname->package = name.substr(0, lastDot);
134 aname->iface = name.substr(lastDot + 1, firstSlash - lastDot - 1);
135 aname->instance = name.substr(firstSlash + 1);
136 return true;
137 }
138};
139
Jooyung Han205e2822023-12-19 16:59:39 +0900140static std::string getAidlInstanceName(const vintf::ManifestInstance& instance) {
141 return instance.package() + "." + instance.interface() + "/" + instance.instance();
142}
143
Steven Moreland5759db02024-03-27 00:03:05 +0000144static bool isVintfDeclared(const Access::CallingContext& ctx, const std::string& name) {
Jooyung Han205e2822023-12-19 16:59:39 +0900145 NativeName nname;
146 if (NativeName::fill(name, &nname)) {
147 bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
148 if (mwd.manifest->hasNativeInstance(nname.package, nname.instance)) {
Steven Moreland5759db02024-03-27 00:03:05 +0000149 ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(),
150 name.c_str(), mwd.description);
Jooyung Han205e2822023-12-19 16:59:39 +0900151 return true; // break
152 }
153 return false; // continue
154 });
155 if (!found) {
Steven Moreland5759db02024-03-27 00:03:05 +0000156 ALOGI("%s Could not find %s in the VINTF manifest.", ctx.toDebugString().c_str(),
157 name.c_str());
Jooyung Han205e2822023-12-19 16:59:39 +0900158 }
159 return found;
160 }
161
Steven Morelandedd4e072021-04-21 00:27:29 +0000162 AidlName aname;
Devin Moore9d1dfa02024-07-23 21:40:05 +0000163 if (!AidlName::fill(name, &aname, true)) return false;
Steven Morelandedd4e072021-04-21 00:27:29 +0000164
165 bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
166 if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) {
Steven Moreland5759db02024-03-27 00:03:05 +0000167 ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(), name.c_str(),
168 mwd.description);
Steven Morelandedd4e072021-04-21 00:27:29 +0000169 return true; // break
Steven Moreland86a17f82019-09-10 10:18:00 -0700170 }
Steven Moreland2e293aa2020-09-23 00:25:16 +0000171 return false; // continue
172 });
173
174 if (!found) {
Devin Moore42407bc2023-09-26 21:30:39 +0000175 std::set<std::string> instances;
176 forEachManifest([&](const ManifestWithDescription& mwd) {
177 std::set<std::string> res = mwd.manifest->getAidlInstances(aname.package, aname.iface);
178 instances.insert(res.begin(), res.end());
179 return true;
180 });
181
182 std::string available;
183 if (instances.empty()) {
184 available = "No alternative instances declared in VINTF";
185 } else {
186 // for logging only. We can't return this information to the client
187 // because they may not have permissions to find or list those
188 // instances
189 available = "VINTF declared instances: " + base::Join(instances, ", ");
190 }
Steven Moreland2e293aa2020-09-23 00:25:16 +0000191 // Although it is tested, explicitly rebuilding qualified name, in case it
192 // becomes something unexpected.
Steven Moreland5759db02024-03-27 00:03:05 +0000193 ALOGI("%s Could not find %s.%s/%s in the VINTF manifest. %s.", ctx.toDebugString().c_str(),
194 aname.package.c_str(), aname.iface.c_str(), aname.instance.c_str(),
195 available.c_str());
Steven Moreland86a17f82019-09-10 10:18:00 -0700196 }
Steven Moreland2edde8e2020-04-30 17:04:54 -0700197
Steven Moreland2e293aa2020-09-23 00:25:16 +0000198 return found;
199}
200
Steven Morelandedd4e072021-04-21 00:27:29 +0000201static std::optional<std::string> getVintfUpdatableApex(const std::string& name) {
Jooyung Han205e2822023-12-19 16:59:39 +0900202 NativeName nname;
203 if (NativeName::fill(name, &nname)) {
204 std::optional<std::string> updatableViaApex;
205
206 forEachManifest([&](const ManifestWithDescription& mwd) {
207 bool cont = mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
208 if (manifestInstance.format() != vintf::HalFormat::NATIVE) return true;
209 if (manifestInstance.package() != nname.package) return true;
210 if (manifestInstance.instance() != nname.instance) return true;
211 updatableViaApex = manifestInstance.updatableViaApex();
212 return false; // break (libvintf uses opposite convention)
213 });
214 return !cont;
215 });
216
217 return updatableViaApex;
218 }
219
Steven Morelandedd4e072021-04-21 00:27:29 +0000220 AidlName aname;
Devin Moore9d1dfa02024-07-23 21:40:05 +0000221 if (!AidlName::fill(name, &aname, true)) return std::nullopt;
Steven Morelandedd4e072021-04-21 00:27:29 +0000222
223 std::optional<std::string> updatableViaApex;
224
225 forEachManifest([&](const ManifestWithDescription& mwd) {
Jooyung Han9f1c6872024-01-23 06:42:02 +0900226 bool cont = mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
Steven Morelandedd4e072021-04-21 00:27:29 +0000227 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
228 if (manifestInstance.package() != aname.package) return true;
229 if (manifestInstance.interface() != aname.iface) return true;
230 if (manifestInstance.instance() != aname.instance) return true;
231 updatableViaApex = manifestInstance.updatableViaApex();
232 return false; // break (libvintf uses opposite convention)
233 });
Jooyung Han9f1c6872024-01-23 06:42:02 +0900234 return !cont;
Steven Morelandedd4e072021-04-21 00:27:29 +0000235 });
236
237 return updatableViaApex;
238}
239
Jooyung Han205e2822023-12-19 16:59:39 +0900240static std::vector<std::string> getVintfUpdatableNames(const std::string& apexName) {
241 std::vector<std::string> names;
Jooyung Han76944fe2022-10-25 17:02:45 +0900242
243 forEachManifest([&](const ManifestWithDescription& mwd) {
244 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
Jooyung Han205e2822023-12-19 16:59:39 +0900245 if (manifestInstance.updatableViaApex().has_value() &&
Jooyung Han76944fe2022-10-25 17:02:45 +0900246 manifestInstance.updatableViaApex().value() == apexName) {
Jooyung Han205e2822023-12-19 16:59:39 +0900247 if (manifestInstance.format() == vintf::HalFormat::NATIVE) {
248 names.push_back(getNativeInstanceName(manifestInstance));
249 } else if (manifestInstance.format() == vintf::HalFormat::AIDL) {
250 names.push_back(getAidlInstanceName(manifestInstance));
251 }
Jooyung Han76944fe2022-10-25 17:02:45 +0900252 }
Jooyung Hance94b752022-11-14 18:55:06 +0900253 return true; // continue (libvintf uses opposite convention)
Jooyung Han76944fe2022-10-25 17:02:45 +0900254 });
255 return false; // continue
256 });
257
Jooyung Han205e2822023-12-19 16:59:39 +0900258 return names;
Jooyung Han76944fe2022-10-25 17:02:45 +0900259}
260
Alice Wang8578f132024-05-03 09:01:56 +0000261static std::optional<std::string> getVintfAccessorName(const std::string& name) {
262 AidlName aname;
Devin Moore9d1dfa02024-07-23 21:40:05 +0000263 if (!AidlName::fill(name, &aname, false)) return std::nullopt;
Alice Wang8578f132024-05-03 09:01:56 +0000264
265 std::optional<std::string> accessor;
266 forEachManifest([&](const ManifestWithDescription& mwd) {
267 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
268 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
269 if (manifestInstance.package() != aname.package) return true;
270 if (manifestInstance.interface() != aname.iface) return true;
271 if (manifestInstance.instance() != aname.instance) return true;
272 accessor = manifestInstance.accessor();
273 return false; // break (libvintf uses opposite convention)
274 });
275 return false; // continue
276 });
277 return accessor;
278}
279
Devin Moore5e4c2f12021-09-09 22:36:33 +0000280static std::optional<ConnectionInfo> getVintfConnectionInfo(const std::string& name) {
281 AidlName aname;
Devin Moore9d1dfa02024-07-23 21:40:05 +0000282 if (!AidlName::fill(name, &aname, true)) return std::nullopt;
Devin Moore5e4c2f12021-09-09 22:36:33 +0000283
284 std::optional<std::string> ip;
285 std::optional<uint64_t> port;
286 forEachManifest([&](const ManifestWithDescription& mwd) {
287 mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
288 if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
289 if (manifestInstance.package() != aname.package) return true;
290 if (manifestInstance.interface() != aname.iface) return true;
291 if (manifestInstance.instance() != aname.instance) return true;
292 ip = manifestInstance.ip();
293 port = manifestInstance.port();
294 return false; // break (libvintf uses opposite convention)
295 });
296 return false; // continue
297 });
298
299 if (ip.has_value() && port.has_value()) {
300 ConnectionInfo info;
301 info.ipAddress = *ip;
302 info.port = *port;
303 return std::make_optional<ConnectionInfo>(info);
304 } else {
305 return std::nullopt;
306 }
307}
308
Steven Moreland2e293aa2020-09-23 00:25:16 +0000309static std::vector<std::string> getVintfInstances(const std::string& interface) {
310 size_t lastDot = interface.rfind('.');
311 if (lastDot == std::string::npos) {
Jooyung Han205e2822023-12-19 16:59:39 +0900312 // This might be a package for native instance.
313 std::vector<std::string> ret;
314 (void)forEachManifest([&](const ManifestWithDescription& mwd) {
315 auto instances = mwd.manifest->getNativeInstances(interface);
316 ret.insert(ret.end(), instances.begin(), instances.end());
317 return false; // continue
318 });
319 // If found, return it without error log.
320 if (!ret.empty()) {
321 return ret;
322 }
323
Pawan Wagh37526162022-09-29 21:55:26 +0000324 ALOGE("VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) "
325 "but got: %s",
326 interface.c_str());
Steven Moreland2e293aa2020-09-23 00:25:16 +0000327 return {};
328 }
329 const std::string package = interface.substr(0, lastDot);
330 const std::string iface = interface.substr(lastDot+1);
331
332 std::vector<std::string> ret;
333 (void)forEachManifest([&](const ManifestWithDescription& mwd) {
334 auto instances = mwd.manifest->getAidlInstances(package, iface);
335 ret.insert(ret.end(), instances.begin(), instances.end());
336 return false; // continue
337 });
338
339 return ret;
Steven Moreland86a17f82019-09-10 10:18:00 -0700340}
Steven Morelandb82b8f82019-10-28 10:52:34 -0700341
Steven Moreland5759db02024-03-27 00:03:05 +0000342static bool meetsDeclarationRequirements(const Access::CallingContext& ctx,
343 const sp<IBinder>& binder, const std::string& name) {
Steven Morelandb82b8f82019-10-28 10:52:34 -0700344 if (!Stability::requiresVintfDeclaration(binder)) {
345 return true;
346 }
347
Steven Moreland5759db02024-03-27 00:03:05 +0000348 return isVintfDeclared(ctx, name);
Steven Morelandb82b8f82019-10-28 10:52:34 -0700349}
Steven Moreland86a17f82019-09-10 10:18:00 -0700350#endif // !VENDORSERVICEMANAGER
351
Steven Morelandb8361902023-02-01 23:18:04 +0000352ServiceManager::Service::~Service() {
Steven Morelandcb591562023-03-06 15:53:44 +0000353 if (hasClients) {
354 // only expected to happen on process death, we don't store the service
355 // name this late (it's in the map that holds this service), but if it
356 // is happening, we might want to change 'unlinkToDeath' to explicitly
357 // clear this bit so that we can abort in other cases, where it would
358 // mean inconsistent logic in servicemanager (unexpected and tested, but
359 // the original lazy service impl here had that bug).
Steven Moreland5759db02024-03-27 00:03:05 +0000360 ALOGW("A service was removed when there are clients");
Steven Morelandb8361902023-02-01 23:18:04 +0000361 }
362}
363
Steven Morelandd13f08b2019-11-18 14:23:09 -0800364ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {
Steven Moreland8d0c9a72020-04-30 16:51:56 -0700365// TODO(b/151696835): reenable performance hack when we solve bug, since with
366// this hack and other fixes, it is unlikely we will see even an ephemeral
367// failure when the manifest parse fails. The goal is that the manifest will
368// be read incorrectly and cause the process trying to register a HAL to
369// fail. If this is in fact an early boot kernel contention issue, then we
370// will get no failure, and by its absence, be signalled to invest more
371// effort in re-adding this performance hack.
372// #ifndef VENDORSERVICEMANAGER
373// // can process these at any times, don't want to delay first VINTF client
374// std::thread([] {
375// vintf::VintfObject::GetDeviceHalManifest();
376// vintf::VintfObject::GetFrameworkHalManifest();
377// }).detach();
378// #endif // !VENDORSERVICEMANAGER
Steven Morelandd13f08b2019-11-18 14:23:09 -0800379}
Steven Moreland130242d2019-08-26 17:41:32 -0700380ServiceManager::~ServiceManager() {
381 // this should only happen in tests
382
Jon Spivackf288b1d2019-12-19 17:15:51 -0800383 for (const auto& [name, callbacks] : mNameToRegistrationCallback) {
Steven Moreland27cfab02019-08-12 14:34:16 -0700384 CHECK(!callbacks.empty()) << name;
385 for (const auto& callback : callbacks) {
386 CHECK(callback != nullptr) << name;
387 }
388 }
389
Steven Moreland130242d2019-08-26 17:41:32 -0700390 for (const auto& [name, service] : mNameToService) {
391 CHECK(service.binder != nullptr) << name;
392 }
393}
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700394
Alice Wang11da1502024-07-25 12:03:22 +0000395Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
396 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
397 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
398
Parth Sane5e1b7e12024-11-29 10:40:41 +0000399 *outBinder = tryGetBinder(name, true).service;
Alice Wang11da1502024-07-25 12:03:22 +0000400 // returns ok regardless of result for legacy reasons
401 return Status::ok();
402}
403
404Status ServiceManager::getService2(const std::string& name, os::Service* outService) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000405 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
406 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000407
Alice Wang8578f132024-05-03 09:01:56 +0000408 *outService = tryGetService(name, true);
Jon Spivack0d844302019-07-22 18:40:34 -0700409 // returns ok regardless of result for legacy reasons
410 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700411}
412
Alice Wang2b613442025-01-02 11:53:05 +0000413Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
414 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
415 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
416
417 *outBinder = tryGetBinder(name, false).service;
418 // returns ok regardless of result for legacy reasons
419 return Status::ok();
420}
421
422Status ServiceManager::checkService2(const std::string& name, os::Service* outService) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000423 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
424 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000425
Alice Wang8578f132024-05-03 09:01:56 +0000426 *outService = tryGetService(name, false);
Jon Spivack0d844302019-07-22 18:40:34 -0700427 // returns ok regardless of result for legacy reasons
428 return Status::ok();
429}
430
Alice Wang8578f132024-05-03 09:01:56 +0000431os::Service ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
432 std::optional<std::string> accessorName;
433#ifndef VENDORSERVICEMANAGER
434 accessorName = getVintfAccessorName(name);
435#endif
436 if (accessorName.has_value()) {
437 auto ctx = mAccess->getCallingContext();
438 if (!mAccess->canFind(ctx, name)) {
439 return os::Service::make<os::Service::Tag::accessor>(nullptr);
440 }
441 return os::Service::make<os::Service::Tag::accessor>(
Parth Sane5e1b7e12024-11-29 10:40:41 +0000442 tryGetBinder(*accessorName, startIfNotFound).service);
Alice Wang8578f132024-05-03 09:01:56 +0000443 } else {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000444 return os::Service::make<os::Service::Tag::serviceWithMetadata>(
445 tryGetBinder(name, startIfNotFound));
Alice Wang8578f132024-05-03 09:01:56 +0000446 }
447}
448
Parth Sane5e1b7e12024-11-29 10:40:41 +0000449os::ServiceWithMetadata ServiceManager::tryGetBinder(const std::string& name,
450 bool startIfNotFound) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000451 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
452 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000453
Steven Morelanda9fe4742019-07-18 14:45:20 -0700454 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700455
Jon Spivack0d844302019-07-22 18:40:34 -0700456 sp<IBinder> out;
Jon Spivack9f503a42019-10-22 16:49:19 -0700457 Service* service = nullptr;
Jon Spivack0d844302019-07-22 18:40:34 -0700458 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700459 service = &(it->second);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700460
Steven Morelandb9e1cbe2023-02-01 22:44:45 +0000461 if (!service->allowIsolated && is_multiuser_uid_isolated(ctx.uid)) {
Steven Morelandbad75882023-06-16 20:59:06 +0000462 LOG(WARNING) << "Isolated app with UID " << ctx.uid << " requested '" << name
463 << "', but the service is not allowed for isolated apps.";
Parth Sane5e1b7e12024-11-29 10:40:41 +0000464 return os::ServiceWithMetadata();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700465 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700466 out = service->binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700467 }
468
Steven Morelanda9fe4742019-07-18 14:45:20 -0700469 if (!mAccess->canFind(ctx, name)) {
Parth Sane5e1b7e12024-11-29 10:40:41 +0000470 return os::ServiceWithMetadata();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700471 }
472
Jon Spivack0d844302019-07-22 18:40:34 -0700473 if (!out && startIfNotFound) {
Steven Morelandaa33e852023-05-10 16:42:15 +0000474 tryStartService(ctx, name);
Jon Spivack0d844302019-07-22 18:40:34 -0700475 }
476
Jon Spivack9f503a42019-10-22 16:49:19 -0700477 if (out) {
Steven Morelandb8361902023-02-01 23:18:04 +0000478 // Force onClients to get sent, and then make sure the timerfd won't clear it
479 // by setting guaranteeClient again. This logic could be simplified by using
480 // a time-based guarantee. However, forcing onClients(true) to get sent
481 // right here is always going to be important for processes serving multiple
482 // lazy interfaces.
483 service->guaranteeClient = true;
484 CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
Jon Spivack9f503a42019-10-22 16:49:19 -0700485 service->guaranteeClient = true;
486 }
Parth Sane5e1b7e12024-11-29 10:40:41 +0000487 os::ServiceWithMetadata serviceWithMetadata = os::ServiceWithMetadata();
488 serviceWithMetadata.service = out;
489 serviceWithMetadata.isLazyService =
490 service ? service->dumpPriority & FLAG_IS_LAZY_SERVICE : false;
491 return serviceWithMetadata;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700492}
493
Steven Moreland905e2e82019-07-17 11:05:45 -0700494bool isValidServiceName(const std::string& name) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000495 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
496 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000497
Steven Moreland905e2e82019-07-17 11:05:45 -0700498 if (name.size() == 0) return false;
499 if (name.size() > 127) return false;
500
501 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700502 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700503 if (c >= 'a' && c <= 'z') continue;
504 if (c >= 'A' && c <= 'Z') continue;
505 if (c >= '0' && c <= '9') continue;
506 return false;
507 }
508
509 return true;
510}
511
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700512Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000513 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
514 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000515
Steven Morelanda9fe4742019-07-18 14:45:20 -0700516 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700517
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700518 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000519 return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700520 }
521
Alice Wangd404e0f2024-07-26 16:18:09 +0000522 std::optional<std::string> accessorName;
523 if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) {
524 return status;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700525 }
526
527 if (binder == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000528 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700529 }
530
Steven Moreland905e2e82019-07-17 11:05:45 -0700531 if (!isValidServiceName(name)) {
Steven Moreland5759db02024-03-27 00:03:05 +0000532 ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000533 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700534 }
535
Steven Moreland86a17f82019-09-10 10:18:00 -0700536#ifndef VENDORSERVICEMANAGER
Steven Moreland5759db02024-03-27 00:03:05 +0000537 if (!meetsDeclarationRequirements(ctx, binder, name)) {
Steven Moreland86a17f82019-09-10 10:18:00 -0700538 // already logged
Steven Morelandffb905b2023-03-28 18:24:37 +0000539 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error.");
Steven Moreland86a17f82019-09-10 10:18:00 -0700540 }
541#endif // !VENDORSERVICEMANAGER
542
Devin Moore4e21def2023-02-24 21:54:14 +0000543 if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) {
Steven Moreland5759db02024-03-27 00:03:05 +0000544 ALOGW("%s Dump flag priority is not set when adding %s", ctx.toDebugString().c_str(),
545 name.c_str());
Devin Moore4e21def2023-02-24 21:54:14 +0000546 }
547
Steven Moreland88860b02019-08-12 14:24:14 -0700548 // implicitly unlinked when the binder is removed
Steven Morelandb0983182021-04-02 03:14:04 +0000549 if (binder->remoteBinder() != nullptr &&
550 binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) {
Steven Moreland5759db02024-03-27 00:03:05 +0000551 ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000552 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700553 }
554
Steven Moreland7ee423b2022-09-24 03:52:08 +0000555 auto it = mNameToService.find(name);
Steven Moreland79578672023-04-27 19:38:00 +0000556 bool prevClients = false;
Steven Moreland7ee423b2022-09-24 03:52:08 +0000557 if (it != mNameToService.end()) {
558 const Service& existing = it->second;
Steven Moreland79578672023-04-27 19:38:00 +0000559 prevClients = existing.hasClients;
Steven Moreland7ee423b2022-09-24 03:52:08 +0000560
561 // We could do better than this because if the other service dies, it
562 // may not have an entry here. However, this case is unlikely. We are
563 // only trying to detect when two different services are accidentally installed.
564
565 if (existing.ctx.uid != ctx.uid) {
Pawan Wagh37526162022-09-29 21:55:26 +0000566 ALOGW("Service '%s' originally registered from UID %u but it is now being registered "
567 "from UID %u. Multiple instances installed?",
568 name.c_str(), existing.ctx.uid, ctx.uid);
Steven Moreland7ee423b2022-09-24 03:52:08 +0000569 }
570
571 if (existing.ctx.sid != ctx.sid) {
Pawan Wagh37526162022-09-29 21:55:26 +0000572 ALOGW("Service '%s' originally registered from SID %s but it is now being registered "
573 "from SID %s. Multiple instances installed?",
574 name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str());
Steven Moreland7ee423b2022-09-24 03:52:08 +0000575 }
576
Pawan Wagh37526162022-09-29 21:55:26 +0000577 ALOGI("Service '%s' originally registered from PID %d but it is being registered again "
578 "from PID %d. Bad state? Late death notification? Multiple instances installed?",
579 name.c_str(), existing.ctx.debugPid, ctx.debugPid);
Steven Moreland7ee423b2022-09-24 03:52:08 +0000580 }
581
Devin Moore05ffe522020-08-06 13:58:29 -0700582 // Overwrite the old service if it exists
Steven Moreland7ee423b2022-09-24 03:52:08 +0000583 mNameToService[name] = Service{
584 .binder = binder,
585 .allowIsolated = allowIsolated,
586 .dumpPriority = dumpPriority,
Steven Moreland79578672023-04-27 19:38:00 +0000587 .hasClients = prevClients, // see b/279898063, matters if existing callbacks
Steven Morelandefea66b2023-06-17 01:59:34 +0000588 .guaranteeClient = false,
Steven Moreland7ee423b2022-09-24 03:52:08 +0000589 .ctx = ctx,
Devin Moore05ffe522020-08-06 13:58:29 -0700590 };
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700591
Steven Moreland7ee423b2022-09-24 03:52:08 +0000592 if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) {
Steven Morelandefea66b2023-06-17 01:59:34 +0000593 // If someone is currently waiting on the service, notify the service that
594 // we're waiting and flush it to the service.
Steven Morelandb8361902023-02-01 23:18:04 +0000595 mNameToService[name].guaranteeClient = true;
596 CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
597 mNameToService[name].guaranteeClient = true;
598
Steven Moreland27cfab02019-08-12 14:34:16 -0700599 for (const sp<IServiceCallback>& cb : it->second) {
600 // permission checked in registerForNotifications
601 cb->onRegistration(name, binder);
602 }
603 }
604
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700605 return Status::ok();
606}
607
608Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Parth Sane5ade9f12024-05-19 13:02:07 +0000609 SM_PERFETTO_TRACE_FUNC();
610
Steven Morelanda9fe4742019-07-18 14:45:20 -0700611 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000612 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700613 }
614
615 size_t toReserve = 0;
616 for (auto const& [name, service] : mNameToService) {
617 (void) name;
618
619 if (service.dumpPriority & dumpPriority) ++toReserve;
620 }
621
622 CHECK(outList->empty());
623
624 outList->reserve(toReserve);
625 for (auto const& [name, service] : mNameToService) {
626 (void) service;
627
628 if (service.dumpPriority & dumpPriority) {
629 outList->push_back(name);
630 }
631 }
632
633 return Status::ok();
634}
635
Steven Moreland27cfab02019-08-12 14:34:16 -0700636Status ServiceManager::registerForNotifications(
637 const std::string& name, const sp<IServiceCallback>& callback) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000638 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
639 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000640
Steven Moreland27cfab02019-08-12 14:34:16 -0700641 auto ctx = mAccess->getCallingContext();
642
Alice Wang8578f132024-05-03 09:01:56 +0000643 // TODO(b/338541373): Implement the notification mechanism for services accessed via
644 // IAccessor.
645 std::optional<std::string> accessorName;
646 if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) {
647 return status;
Steven Morelandb9e1cbe2023-02-01 22:44:45 +0000648 }
649
650 // note - we could allow isolated apps to get notifications if we
651 // keep track of isolated callbacks and non-isolated callbacks, but
652 // this is done since isolated apps shouldn't access lazy services
653 // so we should be able to use different APIs to keep things simple.
654 // Here, we disallow everything, because the service might not be
655 // registered yet.
656 if (is_multiuser_uid_isolated(ctx.uid)) {
657 return Status::fromExceptionCode(Status::EX_SECURITY, "isolated app");
Steven Moreland27cfab02019-08-12 14:34:16 -0700658 }
659
660 if (!isValidServiceName(name)) {
Steven Moreland5759db02024-03-27 00:03:05 +0000661 ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000662 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700663 }
664
665 if (callback == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000666 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null callback.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700667 }
668
Steven Morelandb0983182021-04-02 03:14:04 +0000669 if (OK !=
670 IInterface::asBinder(callback)->linkToDeath(
671 sp<ServiceManager>::fromExisting(this))) {
Steven Moreland5759db02024-03-27 00:03:05 +0000672 ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000673 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700674 }
675
Jon Spivackf288b1d2019-12-19 17:15:51 -0800676 mNameToRegistrationCallback[name].push_back(callback);
Steven Moreland27cfab02019-08-12 14:34:16 -0700677
678 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
679 const sp<IBinder>& binder = it->second.binder;
680
681 // never null if an entry exists
682 CHECK(binder != nullptr) << name;
683 callback->onRegistration(name, binder);
684 }
685
686 return Status::ok();
687}
688Status ServiceManager::unregisterForNotifications(
689 const std::string& name, const sp<IServiceCallback>& callback) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000690 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
691 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000692
Steven Moreland27cfab02019-08-12 14:34:16 -0700693 auto ctx = mAccess->getCallingContext();
694
Alice Wang8578f132024-05-03 09:01:56 +0000695 std::optional<std::string> accessorName;
696 if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) {
697 return status;
Steven Moreland27cfab02019-08-12 14:34:16 -0700698 }
699
700 bool found = false;
701
Jon Spivackf288b1d2019-12-19 17:15:51 -0800702 auto it = mNameToRegistrationCallback.find(name);
703 if (it != mNameToRegistrationCallback.end()) {
704 removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
Steven Moreland27cfab02019-08-12 14:34:16 -0700705 }
706
707 if (!found) {
Steven Moreland5759db02024-03-27 00:03:05 +0000708 ALOGE("%s Trying to unregister callback, but none exists %s", ctx.toDebugString().c_str(),
709 name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000710 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister.");
Steven Moreland27cfab02019-08-12 14:34:16 -0700711 }
712
713 return Status::ok();
714}
715
Steven Morelandb82b8f82019-10-28 10:52:34 -0700716Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000717 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
718 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000719
Steven Morelandb82b8f82019-10-28 10:52:34 -0700720 auto ctx = mAccess->getCallingContext();
721
Alice Wang8578f132024-05-03 09:01:56 +0000722 std::optional<std::string> accessorName;
723 if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) {
724 return status;
Steven Morelandb82b8f82019-10-28 10:52:34 -0700725 }
726
727 *outReturn = false;
728
729#ifndef VENDORSERVICEMANAGER
Steven Moreland5759db02024-03-27 00:03:05 +0000730 *outReturn = isVintfDeclared(ctx, name);
Steven Morelandb82b8f82019-10-28 10:52:34 -0700731#endif
732 return Status::ok();
733}
734
Steven Moreland2e293aa2020-09-23 00:25:16 +0000735binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000736 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
737 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoInterfaceName, interface.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000738
Steven Moreland2e293aa2020-09-23 00:25:16 +0000739 auto ctx = mAccess->getCallingContext();
740
741 std::vector<std::string> allInstances;
742#ifndef VENDORSERVICEMANAGER
743 allInstances = getVintfInstances(interface);
744#endif
745
746 outReturn->clear();
747
Alice Wang8578f132024-05-03 09:01:56 +0000748 std::optional<std::string> _accessorName;
Steven Moreland2e293aa2020-09-23 00:25:16 +0000749 for (const std::string& instance : allInstances) {
Alice Wang8578f132024-05-03 09:01:56 +0000750 if (auto status = canFindService(ctx, interface + "/" + instance, &_accessorName);
751 status.isOk()) {
Steven Moreland2e293aa2020-09-23 00:25:16 +0000752 outReturn->push_back(instance);
753 }
754 }
755
756 if (outReturn->size() == 0 && allInstances.size() != 0) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000757 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland2e293aa2020-09-23 00:25:16 +0000758 }
759
760 return Status::ok();
761}
762
Steven Morelandedd4e072021-04-21 00:27:29 +0000763Status ServiceManager::updatableViaApex(const std::string& name,
764 std::optional<std::string>* outReturn) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000765 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
766 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000767
Steven Morelandedd4e072021-04-21 00:27:29 +0000768 auto ctx = mAccess->getCallingContext();
769
Alice Wang8578f132024-05-03 09:01:56 +0000770 std::optional<std::string> _accessorName;
771 if (auto status = canFindService(ctx, name, &_accessorName); !status.isOk()) {
772 return status;
Steven Morelandedd4e072021-04-21 00:27:29 +0000773 }
774
775 *outReturn = std::nullopt;
776
777#ifndef VENDORSERVICEMANAGER
778 *outReturn = getVintfUpdatableApex(name);
779#endif
780 return Status::ok();
781}
782
Jooyung Han76944fe2022-10-25 17:02:45 +0900783Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName,
784 std::vector<std::string>* outReturn) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000785 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
786 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoApexName, apexName.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000787
Jooyung Han76944fe2022-10-25 17:02:45 +0900788 auto ctx = mAccess->getCallingContext();
789
Jooyung Han205e2822023-12-19 16:59:39 +0900790 std::vector<std::string> apexUpdatableNames;
Jooyung Han76944fe2022-10-25 17:02:45 +0900791#ifndef VENDORSERVICEMANAGER
Jooyung Han205e2822023-12-19 16:59:39 +0900792 apexUpdatableNames = getVintfUpdatableNames(apexName);
Jooyung Han76944fe2022-10-25 17:02:45 +0900793#endif
794
795 outReturn->clear();
796
Alice Wang8578f132024-05-03 09:01:56 +0000797 std::optional<std::string> _accessorName;
Jooyung Han205e2822023-12-19 16:59:39 +0900798 for (const std::string& name : apexUpdatableNames) {
Alice Wang8578f132024-05-03 09:01:56 +0000799 if (auto status = canFindService(ctx, name, &_accessorName); status.isOk()) {
Jooyung Han205e2822023-12-19 16:59:39 +0900800 outReturn->push_back(name);
Jooyung Han76944fe2022-10-25 17:02:45 +0900801 }
802 }
803
Jooyung Han205e2822023-12-19 16:59:39 +0900804 if (outReturn->size() == 0 && apexUpdatableNames.size() != 0) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000805 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Jooyung Han76944fe2022-10-25 17:02:45 +0900806 }
Jooyung Han76944fe2022-10-25 17:02:45 +0900807 return Status::ok();
808}
809
Devin Moore5e4c2f12021-09-09 22:36:33 +0000810Status ServiceManager::getConnectionInfo(const std::string& name,
811 std::optional<ConnectionInfo>* outReturn) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000812 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
813 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000814
Devin Moore5e4c2f12021-09-09 22:36:33 +0000815 auto ctx = mAccess->getCallingContext();
816
Alice Wang8578f132024-05-03 09:01:56 +0000817 std::optional<std::string> _accessorName;
818 if (auto status = canFindService(ctx, name, &_accessorName); !status.isOk()) {
819 return status;
Devin Moore5e4c2f12021-09-09 22:36:33 +0000820 }
821
822 *outReturn = std::nullopt;
823
824#ifndef VENDORSERVICEMANAGER
825 *outReturn = getVintfConnectionInfo(name);
826#endif
827 return Status::ok();
828}
829
Jon Spivackf288b1d2019-12-19 17:15:51 -0800830void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
831 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700832 bool* found) {
Parth Sane5ade9f12024-05-19 13:02:07 +0000833 SM_PERFETTO_TRACE_FUNC();
834
Steven Moreland27cfab02019-08-12 14:34:16 -0700835 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
836
837 for (auto lit = listeners.begin(); lit != listeners.end();) {
838 if (IInterface::asBinder(*lit) == who) {
839 if(found) *found = true;
840 lit = listeners.erase(lit);
841 } else {
842 ++lit;
843 }
844 }
845
846 if (listeners.empty()) {
Jon Spivackf288b1d2019-12-19 17:15:51 -0800847 *it = mNameToRegistrationCallback.erase(*it);
Steven Moreland27cfab02019-08-12 14:34:16 -0700848 } else {
Jon Spivacke223f082019-11-19 16:21:20 -0800849 (*it)++;
Steven Moreland27cfab02019-08-12 14:34:16 -0700850 }
851}
852
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700853void ServiceManager::binderDied(const wp<IBinder>& who) {
Parth Sane5ade9f12024-05-19 13:02:07 +0000854 SM_PERFETTO_TRACE_FUNC();
855
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700856 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
857 if (who == it->second.binder) {
Steven Moreland79578672023-04-27 19:38:00 +0000858 // TODO: currently, this entry contains the state also
859 // associated with mNameToClientCallback. If we allowed
860 // other processes to register client callbacks, we
861 // would have to preserve hasClients (perhaps moving
862 // that state into mNameToClientCallback, which is complicated
863 // because those callbacks are associated w/ particular binder
864 // objects, though they are indexed by name now, they may
865 // need to be indexed by binder at that point).
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700866 it = mNameToService.erase(it);
867 } else {
868 ++it;
869 }
870 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700871
Jon Spivackf288b1d2019-12-19 17:15:51 -0800872 for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
873 removeRegistrationCallback(who, &it, nullptr /*found*/);
Steven Moreland27cfab02019-08-12 14:34:16 -0700874 }
Jon Spivack9f503a42019-10-22 16:49:19 -0700875
876 for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
877 removeClientCallback(who, &it);
878 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700879}
880
Steven Morelandaa33e852023-05-10 16:42:15 +0000881void ServiceManager::tryStartService(const Access::CallingContext& ctx, const std::string& name) {
Steven Moreland5759db02024-03-27 00:03:05 +0000882 ALOGI("%s Since '%s' could not be found trying to start it as a lazy AIDL service. (if it's "
883 "not configured to be a lazy service, it may be stuck starting or still starting).",
884 ctx.toDebugString().c_str(), name.c_str());
Jon Spivack0d844302019-07-22 18:40:34 -0700885
886 std::thread([=] {
Steven Morelandbfe9fba2021-04-27 18:39:57 +0000887 if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
Steven Moreland5759db02024-03-27 00:03:05 +0000888 ALOGI("%s Tried to start aidl service %s as a lazy service, but was unable to. Usually "
889 "this happens when a service is not installed, but if the service is intended to "
890 "be used as a lazy service, then it may be configured incorrectly.",
891 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandbfe9fba2021-04-27 18:39:57 +0000892 }
Jon Spivack0d844302019-07-22 18:40:34 -0700893 }).detach();
894}
895
Jon Spivack9f503a42019-10-22 16:49:19 -0700896Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
897 const sp<IClientCallback>& cb) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +0000898 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
899 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +0000900
Jon Spivack9f503a42019-10-22 16:49:19 -0700901 if (cb == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +0000902 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Callback null.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700903 }
904
905 auto ctx = mAccess->getCallingContext();
Alice Wangd404e0f2024-07-26 16:18:09 +0000906 std::optional<std::string> accessorName;
907 if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) {
908 return status;
Jon Spivack9f503a42019-10-22 16:49:19 -0700909 }
910
911 auto serviceIt = mNameToService.find(name);
912 if (serviceIt == mNameToService.end()) {
Steven Moreland5759db02024-03-27 00:03:05 +0000913 ALOGE("%s Could not add callback for nonexistent service: %s", ctx.toDebugString().c_str(),
914 name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000915 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700916 }
917
Steven Moreland7ee423b2022-09-24 03:52:08 +0000918 if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
Steven Moreland5759db02024-03-27 00:03:05 +0000919 ALOGW("%s Only a server can register for client callbacks (for %s)",
920 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000921 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
922 "Only service can register client callback for itself.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700923 }
924
925 if (serviceIt->second.binder != service) {
Steven Moreland5759db02024-03-27 00:03:05 +0000926 ALOGW("%s Tried to register client callback for %s but a different service is registered "
Pawan Wagh37526162022-09-29 21:55:26 +0000927 "under this name.",
Steven Moreland5759db02024-03-27 00:03:05 +0000928 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000929 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service mismatch.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700930 }
931
Steven Morelandb0983182021-04-02 03:14:04 +0000932 if (OK !=
933 IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
David Duarte67d65282024-04-10 23:54:36 +0000934 ALOGE("%s Could not linkToDeath when adding client callback for %s",
935 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +0000936 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
Jon Spivack9f503a42019-10-22 16:49:19 -0700937 }
938
Steven Moreland79578672023-04-27 19:38:00 +0000939 // WARNING: binderDied makes an assumption about this. If we open up client
940 // callbacks to other services, certain race conditions may lead to services
941 // getting extra client callback notifications.
942 // Make sure all callbacks have been told about a consistent state - b/278038751
Steven Moreland7bb4ab82023-04-13 20:29:33 +0000943 if (serviceIt->second.hasClients) {
944 cb->onClients(service, true);
945 }
946
Jon Spivack9f503a42019-10-22 16:49:19 -0700947 mNameToClientCallback[name].push_back(cb);
948
Steven Morelandefea66b2023-06-17 01:59:34 +0000949 // Flush updated info to client callbacks (especially if guaranteeClient
950 // and !hasClient, see b/285202885). We may or may not have clients at
951 // this point, so ignore the return value.
952 (void)handleServiceClientCallback(2 /* sm + transaction */, name, false);
953
Jon Spivack9f503a42019-10-22 16:49:19 -0700954 return Status::ok();
955}
956
957void ServiceManager::removeClientCallback(const wp<IBinder>& who,
958 ClientCallbackMap::iterator* it) {
959 std::vector<sp<IClientCallback>>& listeners = (*it)->second;
960
961 for (auto lit = listeners.begin(); lit != listeners.end();) {
962 if (IInterface::asBinder(*lit) == who) {
963 lit = listeners.erase(lit);
964 } else {
965 ++lit;
966 }
967 }
968
969 if (listeners.empty()) {
970 *it = mNameToClientCallback.erase(*it);
971 } else {
972 (*it)++;
973 }
974}
975
976ssize_t ServiceManager::Service::getNodeStrongRefCount() {
Steven Morelandb0983182021-04-02 03:14:04 +0000977 sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder());
Jon Spivack9f503a42019-10-22 16:49:19 -0700978 if (bpBinder == nullptr) return -1;
979
Steven Morelande8393882020-12-18 02:27:20 +0000980 return ProcessState::self()->getStrongRefCountForNode(bpBinder);
Jon Spivack9f503a42019-10-22 16:49:19 -0700981}
982
983void ServiceManager::handleClientCallbacks() {
984 for (const auto& [name, service] : mNameToService) {
Steven Morelandb8361902023-02-01 23:18:04 +0000985 handleServiceClientCallback(1 /* sm has one refcount */, name, true);
Jon Spivack9f503a42019-10-22 16:49:19 -0700986 }
987}
988
Steven Morelandb8361902023-02-01 23:18:04 +0000989bool ServiceManager::handleServiceClientCallback(size_t knownClients,
990 const std::string& serviceName,
991 bool isCalledOnInterval) {
Jon Spivack9f503a42019-10-22 16:49:19 -0700992 auto serviceIt = mNameToService.find(serviceName);
993 if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
Steven Morelandb8361902023-02-01 23:18:04 +0000994 return true; // return we do have clients a.k.a. DON'T DO ANYTHING
Jon Spivack9f503a42019-10-22 16:49:19 -0700995 }
996
997 Service& service = serviceIt->second;
998 ssize_t count = service.getNodeStrongRefCount();
999
Steven Morelandb8361902023-02-01 23:18:04 +00001000 // binder driver doesn't support this feature, consider we have clients
1001 if (count == -1) return true;
Jon Spivack9f503a42019-10-22 16:49:19 -07001002
Steven Morelandb8361902023-02-01 23:18:04 +00001003 bool hasKernelReportedClients = static_cast<size_t>(count) > knownClients;
Jon Spivack9f503a42019-10-22 16:49:19 -07001004
1005 if (service.guaranteeClient) {
Steven Morelandb8361902023-02-01 23:18:04 +00001006 if (!service.hasClients && !hasKernelReportedClients) {
Steven Moreland3e083b22023-01-26 00:46:30 +00001007 sendClientCallbackNotifications(serviceName, true,
1008 "service is guaranteed to be in use");
Jon Spivack9f503a42019-10-22 16:49:19 -07001009 }
1010
1011 // guarantee is temporary
1012 service.guaranteeClient = false;
1013 }
1014
Steven Morelandb8361902023-02-01 23:18:04 +00001015 // Regardless of this situation, we want to give this notification as soon as possible.
1016 // This way, we have a chance of preventing further thrashing.
1017 if (hasKernelReportedClients && !service.hasClients) {
1018 sendClientCallbackNotifications(serviceName, true, "we now have a record of a client");
1019 }
Steven Moreland66417652023-02-01 22:19:41 +00001020
Steven Morelandb8361902023-02-01 23:18:04 +00001021 // But limit rate of shutting down service.
1022 if (isCalledOnInterval) {
1023 if (!hasKernelReportedClients && service.hasClients) {
Steven Moreland3e083b22023-01-26 00:46:30 +00001024 sendClientCallbackNotifications(serviceName, false,
1025 "we now have no record of a client");
Jon Spivackd9533c22020-01-27 22:19:22 +00001026 }
Jon Spivack9f503a42019-10-22 16:49:19 -07001027 }
1028
Steven Morelandb8361902023-02-01 23:18:04 +00001029 // May be different than 'hasKernelReportedClients'. We intentionally delay
1030 // information about clients going away to reduce thrashing.
1031 return service.hasClients;
Jon Spivack9f503a42019-10-22 16:49:19 -07001032}
1033
Steven Moreland3e083b22023-01-26 00:46:30 +00001034void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName,
1035 bool hasClients, const char* context) {
Jon Spivack9f503a42019-10-22 16:49:19 -07001036 auto serviceIt = mNameToService.find(serviceName);
1037 if (serviceIt == mNameToService.end()) {
Steven Moreland3e083b22023-01-26 00:46:30 +00001038 ALOGW("sendClientCallbackNotifications could not find service %s when %s",
1039 serviceName.c_str(), context);
Jon Spivack9f503a42019-10-22 16:49:19 -07001040 return;
1041 }
1042 Service& service = serviceIt->second;
1043
Steven Morelandb8361902023-02-01 23:18:04 +00001044 CHECK_NE(hasClients, service.hasClients) << context;
Jon Spivack9f503a42019-10-22 16:49:19 -07001045
Steven Morelandb8361902023-02-01 23:18:04 +00001046 ALOGI("Notifying %s they %s (previously: %s) have clients when %s", serviceName.c_str(),
1047 hasClients ? "do" : "don't", service.hasClients ? "do" : "don't", context);
Jon Spivack9f503a42019-10-22 16:49:19 -07001048
1049 auto ccIt = mNameToClientCallback.find(serviceName);
1050 CHECK(ccIt != mNameToClientCallback.end())
Steven Moreland3e083b22023-01-26 00:46:30 +00001051 << "sendClientCallbackNotifications could not find callbacks for service when "
1052 << context;
Jon Spivack9f503a42019-10-22 16:49:19 -07001053
1054 for (const auto& callback : ccIt->second) {
1055 callback->onClients(service.binder, hasClients);
1056 }
1057
1058 service.hasClients = hasClients;
1059}
1060
1061Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
Daniele Di Proiettof7bdee42024-07-04 14:27:56 +00001062 SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
1063 PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
Parth Sane5ade9f12024-05-19 13:02:07 +00001064
Jon Spivack9f503a42019-10-22 16:49:19 -07001065 if (binder == nullptr) {
Steven Morelandffb905b2023-03-28 18:24:37 +00001066 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null service.");
Jon Spivack9f503a42019-10-22 16:49:19 -07001067 }
1068
1069 auto ctx = mAccess->getCallingContext();
Alice Wangd404e0f2024-07-26 16:18:09 +00001070 std::optional<std::string> accessorName;
1071 if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) {
1072 return status;
Jon Spivack9f503a42019-10-22 16:49:19 -07001073 }
1074
1075 auto serviceIt = mNameToService.find(name);
1076 if (serviceIt == mNameToService.end()) {
Steven Moreland5759db02024-03-27 00:03:05 +00001077 ALOGW("%s Tried to unregister %s, but that service wasn't registered to begin with.",
1078 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +00001079 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered.");
Jon Spivack9f503a42019-10-22 16:49:19 -07001080 }
1081
Steven Moreland7ee423b2022-09-24 03:52:08 +00001082 if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
Steven Moreland5759db02024-03-27 00:03:05 +00001083 ALOGW("%s Only a server can unregister itself (for %s)", ctx.toDebugString().c_str(),
1084 name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +00001085 return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
1086 "Service can only unregister itself.");
Jon Spivack9f503a42019-10-22 16:49:19 -07001087 }
1088
1089 sp<IBinder> storedBinder = serviceIt->second.binder;
1090
1091 if (binder != storedBinder) {
Steven Moreland5759db02024-03-27 00:03:05 +00001092 ALOGW("%s Tried to unregister %s, but a different service is registered under this name.",
1093 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +00001094 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
1095 "Different service registered under this name.");
Jon Spivack9f503a42019-10-22 16:49:19 -07001096 }
1097
Steven Morelandb8361902023-02-01 23:18:04 +00001098 // important because we don't have timer-based guarantees, we don't want to clear
1099 // this
Jon Spivack0f18f2c2020-03-13 20:45:18 -07001100 if (serviceIt->second.guaranteeClient) {
Steven Moreland5759db02024-03-27 00:03:05 +00001101 ALOGI("%s Tried to unregister %s, but there is about to be a client.",
1102 ctx.toDebugString().c_str(), name.c_str());
Steven Morelandffb905b2023-03-28 18:24:37 +00001103 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
1104 "Can't unregister, pending client.");
Jon Spivack0f18f2c2020-03-13 20:45:18 -07001105 }
1106
Jon Spivack9f503a42019-10-22 16:49:19 -07001107 // - kernel driver will hold onto one refcount (during this transaction)
1108 // - servicemanager has a refcount (guaranteed by this transaction)
Steven Morelandb8361902023-02-01 23:18:04 +00001109 constexpr size_t kKnownClients = 2;
1110
1111 if (handleServiceClientCallback(kKnownClients, name, false)) {
Steven Moreland5759db02024-03-27 00:03:05 +00001112 ALOGI("%s Tried to unregister %s, but there are clients.", ctx.toDebugString().c_str(),
1113 name.c_str());
Steven Morelandb8361902023-02-01 23:18:04 +00001114
1115 // Since we had a failed registration attempt, and the HIDL implementation of
1116 // delaying service shutdown for multiple periods wasn't ported here... this may
1117 // help reduce thrashing, but we should be able to remove it.
Jon Spivack620d2dc2020-03-06 13:58:01 -08001118 serviceIt->second.guaranteeClient = true;
Steven Morelandb8361902023-02-01 23:18:04 +00001119
Steven Morelandffb905b2023-03-28 18:24:37 +00001120 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
1121 "Can't unregister, known client.");
Jon Spivack9f503a42019-10-22 16:49:19 -07001122 }
1123
Steven Moreland5759db02024-03-27 00:03:05 +00001124 ALOGI("%s Unregistering %s", ctx.toDebugString().c_str(), name.c_str());
Jon Spivack9f503a42019-10-22 16:49:19 -07001125 mNameToService.erase(name);
1126
1127 return Status::ok();
1128}
1129
Alice Wangd404e0f2024-07-26 16:18:09 +00001130Status ServiceManager::canAddService(const Access::CallingContext& ctx, const std::string& name,
1131 std::optional<std::string>* accessor) {
1132 if (!mAccess->canAdd(ctx, name)) {
1133 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied for service.");
1134 }
1135#ifndef VENDORSERVICEMANAGER
1136 *accessor = getVintfAccessorName(name);
1137#endif
1138 if (accessor->has_value()) {
1139 if (!mAccess->canAdd(ctx, accessor->value())) {
1140 return Status::fromExceptionCode(Status::EX_SECURITY,
1141 "SELinux denied for the accessor of the service.");
1142 }
1143 }
1144 return Status::ok();
1145}
1146
Alice Wang8578f132024-05-03 09:01:56 +00001147Status ServiceManager::canFindService(const Access::CallingContext& ctx, const std::string& name,
1148 std::optional<std::string>* accessor) {
1149 if (!mAccess->canFind(ctx, name)) {
1150 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied for service.");
1151 }
1152#ifndef VENDORSERVICEMANAGER
1153 *accessor = getVintfAccessorName(name);
1154#endif
1155 if (accessor->has_value()) {
1156 if (!mAccess->canFind(ctx, accessor->value())) {
1157 return Status::fromExceptionCode(Status::EX_SECURITY,
1158 "SELinux denied for the accessor of the service.");
1159 }
1160 }
1161 return Status::ok();
1162}
1163
Steven Moreland3ea43272021-01-28 22:49:28 +00001164Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) {
Parth Sane5ade9f12024-05-19 13:02:07 +00001165 SM_PERFETTO_TRACE_FUNC();
Steven Moreland3ea43272021-01-28 22:49:28 +00001166 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Morelandffb905b2023-03-28 18:24:37 +00001167 return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
Steven Moreland3ea43272021-01-28 22:49:28 +00001168 }
1169
1170 outReturn->reserve(mNameToService.size());
1171 for (auto const& [name, service] : mNameToService) {
1172 ServiceDebugInfo info;
1173 info.name = name;
Steven Moreland7ee423b2022-09-24 03:52:08 +00001174 info.debugPid = service.ctx.debugPid;
Steven Moreland3ea43272021-01-28 22:49:28 +00001175
1176 outReturn->push_back(std::move(info));
1177 }
1178
1179 return Status::ok();
1180}
1181
Pawan Wagh243888e2022-09-20 19:37:35 +00001182void ServiceManager::clear() {
1183 mNameToService.clear();
1184 mNameToRegistrationCallback.clear();
1185 mNameToClientCallback.clear();
1186}
1187
Steven Moreland8d0c9a72020-04-30 16:51:56 -07001188} // namespace android