Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 21 | #include <binder/BpBinder.h> |
| 22 | #include <binder/IPCThreadState.h> |
| 23 | #include <binder/ProcessState.h> |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 24 | #include <binder/Stability.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 25 | #include <cutils/android_filesystem_config.h> |
| 26 | #include <cutils/multiuser.h> |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 27 | #include <thread> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 28 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 29 | #ifndef VENDORSERVICEMANAGER |
| 30 | #include <vintf/VintfObject.h> |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 31 | #ifdef __ANDROID_RECOVERY__ |
| 32 | #include <vintf/VintfObjectRecovery.h> |
| 33 | #endif // __ANDROID_RECOVERY__ |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 34 | #include <vintf/constants.h> |
| 35 | #endif // !VENDORSERVICEMANAGER |
| 36 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 37 | using ::android::binder::Status; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 38 | using ::android::internal::Stability; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 42 | #ifndef VENDORSERVICEMANAGER |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 43 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 44 | struct ManifestWithDescription { |
| 45 | std::shared_ptr<const vintf::HalManifest> manifest; |
| 46 | const char* description; |
| 47 | }; |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 48 | static std::vector<ManifestWithDescription> GetManifestsWithDescription() { |
| 49 | #ifdef __ANDROID_RECOVERY__ |
| 50 | auto vintfObject = vintf::VintfObjectRecovery::GetInstance(); |
| 51 | if (vintfObject == nullptr) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 52 | ALOGE("NULL VintfObjectRecovery!"); |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 53 | return {}; |
| 54 | } |
| 55 | return {ManifestWithDescription{vintfObject->getRecoveryHalManifest(), "recovery"}}; |
| 56 | #else |
| 57 | auto vintfObject = vintf::VintfObject::GetInstance(); |
| 58 | if (vintfObject == nullptr) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 59 | ALOGE("NULL VintfObject!"); |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 60 | return {}; |
| 61 | } |
| 62 | return {ManifestWithDescription{vintfObject->getDeviceHalManifest(), "device"}, |
| 63 | ManifestWithDescription{vintfObject->getFrameworkHalManifest(), "framework"}}; |
| 64 | #endif |
| 65 | } |
| 66 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 67 | // func true -> stop search and forEachManifest will return true |
| 68 | static bool forEachManifest(const std::function<bool(const ManifestWithDescription&)>& func) { |
Yifan Hong | 0a9b56e | 2021-11-30 16:45:40 -0800 | [diff] [blame] | 69 | for (const ManifestWithDescription& mwd : GetManifestsWithDescription()) { |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 70 | if (mwd.manifest == nullptr) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 71 | ALOGE("NULL VINTF MANIFEST!: %s", mwd.description); |
| 72 | // note, we explicitly do not retry here, so that we can detect VINTF |
| 73 | // or other bugs (b/151696835) |
| 74 | continue; |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 75 | } |
| 76 | if (func(mwd)) return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 81 | struct AidlName { |
| 82 | std::string package; |
| 83 | std::string iface; |
| 84 | std::string instance; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 85 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 86 | 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) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 90 | ALOGE("VINTF HALs require names in the format type/instance (e.g. " |
| 91 | "some.package.foo.IFoo/default) but got: %s", |
| 92 | name.c_str()); |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | aname->package = name.substr(0, lastDot); |
| 96 | aname->iface = name.substr(lastDot + 1, firstSlash - lastDot - 1); |
| 97 | aname->instance = name.substr(firstSlash + 1); |
| 98 | return true; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | static bool isVintfDeclared(const std::string& name) { |
| 103 | AidlName aname; |
| 104 | if (!AidlName::fill(name, &aname)) return false; |
| 105 | |
| 106 | bool found = forEachManifest([&](const ManifestWithDescription& mwd) { |
| 107 | if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 108 | ALOGI("Found %s in %s VINTF manifest.", name.c_str(), mwd.description); |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 109 | return true; // break |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 110 | } |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 111 | return false; // continue |
| 112 | }); |
| 113 | |
| 114 | if (!found) { |
| 115 | // Although it is tested, explicitly rebuilding qualified name, in case it |
| 116 | // becomes something unexpected. |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 117 | ALOGI("Could not find %s.%s/%s in the VINTF manifest.", aname.package.c_str(), |
| 118 | aname.iface.c_str(), aname.instance.c_str()); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 119 | } |
Steven Moreland | 2edde8e | 2020-04-30 17:04:54 -0700 | [diff] [blame] | 120 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 121 | return found; |
| 122 | } |
| 123 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 124 | static std::optional<std::string> getVintfUpdatableApex(const std::string& name) { |
| 125 | AidlName aname; |
| 126 | if (!AidlName::fill(name, &aname)) return std::nullopt; |
| 127 | |
| 128 | std::optional<std::string> updatableViaApex; |
| 129 | |
| 130 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 131 | mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
| 132 | if (manifestInstance.format() != vintf::HalFormat::AIDL) return true; |
| 133 | if (manifestInstance.package() != aname.package) return true; |
| 134 | if (manifestInstance.interface() != aname.iface) return true; |
| 135 | if (manifestInstance.instance() != aname.instance) return true; |
| 136 | updatableViaApex = manifestInstance.updatableViaApex(); |
| 137 | return false; // break (libvintf uses opposite convention) |
| 138 | }); |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame] | 139 | if (updatableViaApex.has_value()) return true; // break (found match) |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 140 | return false; // continue |
| 141 | }); |
| 142 | |
| 143 | return updatableViaApex; |
| 144 | } |
| 145 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 146 | static std::vector<std::string> getVintfUpdatableInstances(const std::string& apexName) { |
| 147 | std::vector<std::string> instances; |
| 148 | |
| 149 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 150 | mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
| 151 | if (manifestInstance.format() == vintf::HalFormat::AIDL && |
| 152 | manifestInstance.updatableViaApex().has_value() && |
| 153 | manifestInstance.updatableViaApex().value() == apexName) { |
| 154 | std::string aname = manifestInstance.package() + "." + |
| 155 | manifestInstance.interface() + "/" + manifestInstance.instance(); |
| 156 | instances.push_back(aname); |
| 157 | } |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame] | 158 | return true; // continue (libvintf uses opposite convention) |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 159 | }); |
| 160 | return false; // continue |
| 161 | }); |
| 162 | |
| 163 | return instances; |
| 164 | } |
| 165 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 166 | static std::optional<ConnectionInfo> getVintfConnectionInfo(const std::string& name) { |
| 167 | AidlName aname; |
| 168 | if (!AidlName::fill(name, &aname)) return std::nullopt; |
| 169 | |
| 170 | std::optional<std::string> ip; |
| 171 | std::optional<uint64_t> port; |
| 172 | forEachManifest([&](const ManifestWithDescription& mwd) { |
| 173 | mwd.manifest->forEachInstance([&](const auto& manifestInstance) { |
| 174 | if (manifestInstance.format() != vintf::HalFormat::AIDL) return true; |
| 175 | if (manifestInstance.package() != aname.package) return true; |
| 176 | if (manifestInstance.interface() != aname.iface) return true; |
| 177 | if (manifestInstance.instance() != aname.instance) return true; |
| 178 | ip = manifestInstance.ip(); |
| 179 | port = manifestInstance.port(); |
| 180 | return false; // break (libvintf uses opposite convention) |
| 181 | }); |
| 182 | return false; // continue |
| 183 | }); |
| 184 | |
| 185 | if (ip.has_value() && port.has_value()) { |
| 186 | ConnectionInfo info; |
| 187 | info.ipAddress = *ip; |
| 188 | info.port = *port; |
| 189 | return std::make_optional<ConnectionInfo>(info); |
| 190 | } else { |
| 191 | return std::nullopt; |
| 192 | } |
| 193 | } |
| 194 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 195 | static std::vector<std::string> getVintfInstances(const std::string& interface) { |
| 196 | size_t lastDot = interface.rfind('.'); |
| 197 | if (lastDot == std::string::npos) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 198 | ALOGE("VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) " |
| 199 | "but got: %s", |
| 200 | interface.c_str()); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 201 | return {}; |
| 202 | } |
| 203 | const std::string package = interface.substr(0, lastDot); |
| 204 | const std::string iface = interface.substr(lastDot+1); |
| 205 | |
| 206 | std::vector<std::string> ret; |
| 207 | (void)forEachManifest([&](const ManifestWithDescription& mwd) { |
| 208 | auto instances = mwd.manifest->getAidlInstances(package, iface); |
| 209 | ret.insert(ret.end(), instances.begin(), instances.end()); |
| 210 | return false; // continue |
| 211 | }); |
| 212 | |
| 213 | return ret; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 214 | } |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 215 | |
| 216 | static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) { |
| 217 | if (!Stability::requiresVintfDeclaration(binder)) { |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | return isVintfDeclared(name); |
| 222 | } |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 223 | #endif // !VENDORSERVICEMANAGER |
| 224 | |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 225 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) { |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 226 | // TODO(b/151696835): reenable performance hack when we solve bug, since with |
| 227 | // this hack and other fixes, it is unlikely we will see even an ephemeral |
| 228 | // failure when the manifest parse fails. The goal is that the manifest will |
| 229 | // be read incorrectly and cause the process trying to register a HAL to |
| 230 | // fail. If this is in fact an early boot kernel contention issue, then we |
| 231 | // will get no failure, and by its absence, be signalled to invest more |
| 232 | // effort in re-adding this performance hack. |
| 233 | // #ifndef VENDORSERVICEMANAGER |
| 234 | // // can process these at any times, don't want to delay first VINTF client |
| 235 | // std::thread([] { |
| 236 | // vintf::VintfObject::GetDeviceHalManifest(); |
| 237 | // vintf::VintfObject::GetFrameworkHalManifest(); |
| 238 | // }).detach(); |
| 239 | // #endif // !VENDORSERVICEMANAGER |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 240 | } |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 241 | ServiceManager::~ServiceManager() { |
| 242 | // this should only happen in tests |
| 243 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 244 | for (const auto& [name, callbacks] : mNameToRegistrationCallback) { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 245 | CHECK(!callbacks.empty()) << name; |
| 246 | for (const auto& callback : callbacks) { |
| 247 | CHECK(callback != nullptr) << name; |
| 248 | } |
| 249 | } |
| 250 | |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 251 | for (const auto& [name, service] : mNameToService) { |
| 252 | CHECK(service.binder != nullptr) << name; |
| 253 | } |
| 254 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 255 | |
| 256 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 257 | *outBinder = tryGetService(name, true); |
| 258 | // returns ok regardless of result for legacy reasons |
| 259 | return Status::ok(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 263 | *outBinder = tryGetService(name, false); |
| 264 | // returns ok regardless of result for legacy reasons |
| 265 | return Status::ok(); |
| 266 | } |
| 267 | |
| 268 | sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 269 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 270 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 271 | sp<IBinder> out; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 272 | Service* service = nullptr; |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 273 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 274 | service = &(it->second); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 275 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 276 | if (!service->allowIsolated) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 277 | uid_t appid = multiuser_get_app_id(ctx.uid); |
| 278 | bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 279 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 280 | if (isIsolated) { |
| 281 | return nullptr; |
| 282 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 283 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 284 | out = service->binder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 287 | if (!mAccess->canFind(ctx, name)) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 288 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 291 | if (!out && startIfNotFound) { |
| 292 | tryStartService(name); |
| 293 | } |
| 294 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 295 | if (out) { |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 296 | // Setting this guarantee each time we hand out a binder ensures that the client-checking |
| 297 | // loop knows about the event even if the client immediately drops the service |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 298 | service->guaranteeClient = true; |
| 299 | } |
| 300 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 301 | return out; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 304 | bool isValidServiceName(const std::string& name) { |
| 305 | if (name.size() == 0) return false; |
| 306 | if (name.size() > 127) return false; |
| 307 | |
| 308 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 309 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 310 | if (c >= 'a' && c <= 'z') continue; |
| 311 | if (c >= 'A' && c <= 'Z') continue; |
| 312 | if (c >= '0' && c <= '9') continue; |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 319 | Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 320 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 321 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 322 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 323 | return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 326 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 327 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denial"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (binder == nullptr) { |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 331 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 334 | if (!isValidServiceName(name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 335 | ALOGE("Invalid service name: %s", name.c_str()); |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 336 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 339 | #ifndef VENDORSERVICEMANAGER |
| 340 | if (!meetsDeclarationRequirements(binder, name)) { |
| 341 | // already logged |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 342 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error"); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 343 | } |
| 344 | #endif // !VENDORSERVICEMANAGER |
| 345 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 346 | // implicitly unlinked when the binder is removed |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 347 | if (binder->remoteBinder() != nullptr && |
| 348 | binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 349 | ALOGE("Could not linkToDeath when adding %s", name.c_str()); |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 350 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "linkToDeath failure"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 353 | auto it = mNameToService.find(name); |
| 354 | if (it != mNameToService.end()) { |
| 355 | const Service& existing = it->second; |
| 356 | |
| 357 | // We could do better than this because if the other service dies, it |
| 358 | // may not have an entry here. However, this case is unlikely. We are |
| 359 | // only trying to detect when two different services are accidentally installed. |
| 360 | |
| 361 | if (existing.ctx.uid != ctx.uid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 362 | ALOGW("Service '%s' originally registered from UID %u but it is now being registered " |
| 363 | "from UID %u. Multiple instances installed?", |
| 364 | name.c_str(), existing.ctx.uid, ctx.uid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (existing.ctx.sid != ctx.sid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 368 | ALOGW("Service '%s' originally registered from SID %s but it is now being registered " |
| 369 | "from SID %s. Multiple instances installed?", |
| 370 | name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str()); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 373 | ALOGI("Service '%s' originally registered from PID %d but it is being registered again " |
| 374 | "from PID %d. Bad state? Late death notification? Multiple instances installed?", |
| 375 | name.c_str(), existing.ctx.debugPid, ctx.debugPid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 378 | // Overwrite the old service if it exists |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 379 | mNameToService[name] = Service{ |
| 380 | .binder = binder, |
| 381 | .allowIsolated = allowIsolated, |
| 382 | .dumpPriority = dumpPriority, |
| 383 | .ctx = ctx, |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 384 | }; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 385 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 386 | if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 387 | for (const sp<IServiceCallback>& cb : it->second) { |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 388 | mNameToService[name].guaranteeClient = true; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 389 | // permission checked in registerForNotifications |
| 390 | cb->onRegistration(name, binder); |
| 391 | } |
| 392 | } |
| 393 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 394 | return Status::ok(); |
| 395 | } |
| 396 | |
| 397 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 398 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 399 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 400 | } |
| 401 | |
| 402 | size_t toReserve = 0; |
| 403 | for (auto const& [name, service] : mNameToService) { |
| 404 | (void) name; |
| 405 | |
| 406 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 407 | } |
| 408 | |
| 409 | CHECK(outList->empty()); |
| 410 | |
| 411 | outList->reserve(toReserve); |
| 412 | for (auto const& [name, service] : mNameToService) { |
| 413 | (void) service; |
| 414 | |
| 415 | if (service.dumpPriority & dumpPriority) { |
| 416 | outList->push_back(name); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return Status::ok(); |
| 421 | } |
| 422 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 423 | Status ServiceManager::registerForNotifications( |
| 424 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 425 | auto ctx = mAccess->getCallingContext(); |
| 426 | |
| 427 | if (!mAccess->canFind(ctx, name)) { |
| 428 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 429 | } |
| 430 | |
| 431 | if (!isValidServiceName(name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 432 | ALOGE("Invalid service name: %s", name.c_str()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 433 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 434 | } |
| 435 | |
| 436 | if (callback == nullptr) { |
| 437 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 438 | } |
| 439 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 440 | if (OK != |
| 441 | IInterface::asBinder(callback)->linkToDeath( |
| 442 | sp<ServiceManager>::fromExisting(this))) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 443 | ALOGE("Could not linkToDeath when adding %s", name.c_str()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 444 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 445 | } |
| 446 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 447 | mNameToRegistrationCallback[name].push_back(callback); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 448 | |
| 449 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 450 | const sp<IBinder>& binder = it->second.binder; |
| 451 | |
| 452 | // never null if an entry exists |
| 453 | CHECK(binder != nullptr) << name; |
| 454 | callback->onRegistration(name, binder); |
| 455 | } |
| 456 | |
| 457 | return Status::ok(); |
| 458 | } |
| 459 | Status ServiceManager::unregisterForNotifications( |
| 460 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 461 | auto ctx = mAccess->getCallingContext(); |
| 462 | |
| 463 | if (!mAccess->canFind(ctx, name)) { |
| 464 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 465 | } |
| 466 | |
| 467 | bool found = false; |
| 468 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 469 | auto it = mNameToRegistrationCallback.find(name); |
| 470 | if (it != mNameToRegistrationCallback.end()) { |
| 471 | removeRegistrationCallback(IInterface::asBinder(callback), &it, &found); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (!found) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 475 | ALOGE("Trying to unregister callback, but none exists %s", name.c_str()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 476 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 477 | } |
| 478 | |
| 479 | return Status::ok(); |
| 480 | } |
| 481 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 482 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { |
| 483 | auto ctx = mAccess->getCallingContext(); |
| 484 | |
| 485 | if (!mAccess->canFind(ctx, name)) { |
| 486 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 487 | } |
| 488 | |
| 489 | *outReturn = false; |
| 490 | |
| 491 | #ifndef VENDORSERVICEMANAGER |
| 492 | *outReturn = isVintfDeclared(name); |
| 493 | #endif |
| 494 | return Status::ok(); |
| 495 | } |
| 496 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 497 | binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) { |
| 498 | auto ctx = mAccess->getCallingContext(); |
| 499 | |
| 500 | std::vector<std::string> allInstances; |
| 501 | #ifndef VENDORSERVICEMANAGER |
| 502 | allInstances = getVintfInstances(interface); |
| 503 | #endif |
| 504 | |
| 505 | outReturn->clear(); |
| 506 | |
| 507 | for (const std::string& instance : allInstances) { |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 508 | if (mAccess->canFind(ctx, interface + "/" + instance)) { |
| 509 | outReturn->push_back(instance); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (outReturn->size() == 0 && allInstances.size() != 0) { |
| 514 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 515 | } |
| 516 | |
| 517 | return Status::ok(); |
| 518 | } |
| 519 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 520 | Status ServiceManager::updatableViaApex(const std::string& name, |
| 521 | std::optional<std::string>* outReturn) { |
| 522 | auto ctx = mAccess->getCallingContext(); |
| 523 | |
| 524 | if (!mAccess->canFind(ctx, name)) { |
| 525 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 526 | } |
| 527 | |
| 528 | *outReturn = std::nullopt; |
| 529 | |
| 530 | #ifndef VENDORSERVICEMANAGER |
| 531 | *outReturn = getVintfUpdatableApex(name); |
| 532 | #endif |
| 533 | return Status::ok(); |
| 534 | } |
| 535 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 536 | Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName, |
| 537 | std::vector<std::string>* outReturn) { |
| 538 | auto ctx = mAccess->getCallingContext(); |
| 539 | |
| 540 | std::vector<std::string> apexUpdatableInstances; |
| 541 | #ifndef VENDORSERVICEMANAGER |
| 542 | apexUpdatableInstances = getVintfUpdatableInstances(apexName); |
| 543 | #endif |
| 544 | |
| 545 | outReturn->clear(); |
| 546 | |
| 547 | for (const std::string& instance : apexUpdatableInstances) { |
| 548 | if (mAccess->canFind(ctx, instance)) { |
| 549 | outReturn->push_back(instance); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (outReturn->size() == 0 && apexUpdatableInstances.size() != 0) { |
| 554 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denial"); |
| 555 | } |
| 556 | |
| 557 | return Status::ok(); |
| 558 | } |
| 559 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 560 | Status ServiceManager::getConnectionInfo(const std::string& name, |
| 561 | std::optional<ConnectionInfo>* outReturn) { |
| 562 | auto ctx = mAccess->getCallingContext(); |
| 563 | |
| 564 | if (!mAccess->canFind(ctx, name)) { |
| 565 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 566 | } |
| 567 | |
| 568 | *outReturn = std::nullopt; |
| 569 | |
| 570 | #ifndef VENDORSERVICEMANAGER |
| 571 | *outReturn = getVintfConnectionInfo(name); |
| 572 | #endif |
| 573 | return Status::ok(); |
| 574 | } |
| 575 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 576 | void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who, |
| 577 | ServiceCallbackMap::iterator* it, |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 578 | bool* found) { |
| 579 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; |
| 580 | |
| 581 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 582 | if (IInterface::asBinder(*lit) == who) { |
| 583 | if(found) *found = true; |
| 584 | lit = listeners.erase(lit); |
| 585 | } else { |
| 586 | ++lit; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (listeners.empty()) { |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 591 | *it = mNameToRegistrationCallback.erase(*it); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 592 | } else { |
Jon Spivack | e223f08 | 2019-11-19 16:21:20 -0800 | [diff] [blame] | 593 | (*it)++; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 597 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
| 598 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 599 | if (who == it->second.binder) { |
| 600 | it = mNameToService.erase(it); |
| 601 | } else { |
| 602 | ++it; |
| 603 | } |
| 604 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 605 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 606 | for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) { |
| 607 | removeRegistrationCallback(who, &it, nullptr /*found*/); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 608 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 609 | |
| 610 | for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) { |
| 611 | removeClientCallback(who, &it); |
| 612 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 615 | void ServiceManager::tryStartService(const std::string& name) { |
Steven Moreland | ba0f33c | 2022-11-04 22:24:31 +0000 | [diff] [blame] | 616 | ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service. (if it's not " |
| 617 | "configured to be a lazy service, it may be stuck starting or still starting).", |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 618 | name.c_str()); |
| 619 | |
| 620 | std::thread([=] { |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 621 | if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 622 | ALOGI("Tried to start aidl service %s as a lazy service, but was unable to. Usually " |
| 623 | "this happens when a " |
| 624 | "service is not installed, but if the service is intended to be used as a " |
| 625 | "lazy service, then it may be configured incorrectly.", |
| 626 | name.c_str()); |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 627 | } |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 628 | }).detach(); |
| 629 | } |
| 630 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 631 | Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 632 | const sp<IClientCallback>& cb) { |
| 633 | if (cb == nullptr) { |
| 634 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 635 | } |
| 636 | |
| 637 | auto ctx = mAccess->getCallingContext(); |
| 638 | if (!mAccess->canAdd(ctx, name)) { |
| 639 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 640 | } |
| 641 | |
| 642 | auto serviceIt = mNameToService.find(name); |
| 643 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 644 | ALOGE("Could not add callback for nonexistent service: %s", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 645 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 646 | } |
| 647 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 648 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 649 | ALOGW("Only a server can register for client callbacks (for %s)", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 650 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION); |
| 651 | } |
| 652 | |
| 653 | if (serviceIt->second.binder != service) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 654 | ALOGW("Tried to register client callback for %s but a different service is registered " |
| 655 | "under this name.", |
| 656 | name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 657 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 658 | } |
| 659 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 660 | if (OK != |
| 661 | IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 662 | ALOGE("Could not linkToDeath when adding client callback for %s", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 663 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 664 | } |
| 665 | |
| 666 | mNameToClientCallback[name].push_back(cb); |
| 667 | |
| 668 | return Status::ok(); |
| 669 | } |
| 670 | |
| 671 | void ServiceManager::removeClientCallback(const wp<IBinder>& who, |
| 672 | ClientCallbackMap::iterator* it) { |
| 673 | std::vector<sp<IClientCallback>>& listeners = (*it)->second; |
| 674 | |
| 675 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 676 | if (IInterface::asBinder(*lit) == who) { |
| 677 | lit = listeners.erase(lit); |
| 678 | } else { |
| 679 | ++lit; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if (listeners.empty()) { |
| 684 | *it = mNameToClientCallback.erase(*it); |
| 685 | } else { |
| 686 | (*it)++; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | ssize_t ServiceManager::Service::getNodeStrongRefCount() { |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 691 | sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 692 | if (bpBinder == nullptr) return -1; |
| 693 | |
Steven Moreland | e839388 | 2020-12-18 02:27:20 +0000 | [diff] [blame] | 694 | return ProcessState::self()->getStrongRefCountForNode(bpBinder); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void ServiceManager::handleClientCallbacks() { |
| 698 | for (const auto& [name, service] : mNameToService) { |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 699 | handleServiceClientCallback(name, true); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 703 | ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName, |
| 704 | bool isCalledOnInterval) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 705 | auto serviceIt = mNameToService.find(serviceName); |
| 706 | if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) { |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 707 | return -1; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | Service& service = serviceIt->second; |
| 711 | ssize_t count = service.getNodeStrongRefCount(); |
| 712 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 713 | // binder driver doesn't support this feature |
| 714 | if (count == -1) return count; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 715 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 716 | bool hasClients = count > 1; // this process holds a strong count |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 717 | |
| 718 | if (service.guaranteeClient) { |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 719 | // we have no record of this client |
| 720 | if (!service.hasClients && !hasClients) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 721 | sendClientCallbackNotifications(serviceName, true, |
| 722 | "service is guaranteed to be in use"); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // guarantee is temporary |
| 726 | service.guaranteeClient = false; |
| 727 | } |
| 728 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 729 | // only send notifications if this was called via the interval checking workflow |
Steven Moreland | 0db2add | 2023-01-28 02:50:00 +0000 | [diff] [blame] | 730 | if (isCalledOnInterval) { |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 731 | if (hasClients && !service.hasClients) { |
| 732 | // client was retrieved in some other way |
| 733 | sendClientCallbackNotifications(serviceName, true, "we now have a record of a client"); |
| 734 | } |
| 735 | |
| 736 | // there are no more clients, but the callback has not been called yet |
| 737 | if (!hasClients && service.hasClients) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 738 | sendClientCallbackNotifications(serviceName, false, |
| 739 | "we now have no record of a client"); |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 740 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 743 | return count; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 746 | void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, |
| 747 | bool hasClients, const char* context) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 748 | auto serviceIt = mNameToService.find(serviceName); |
| 749 | if (serviceIt == mNameToService.end()) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 750 | ALOGW("sendClientCallbackNotifications could not find service %s when %s", |
| 751 | serviceName.c_str(), context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | Service& service = serviceIt->second; |
| 755 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 756 | CHECK(hasClients != service.hasClients) |
| 757 | << "Record shows: " << service.hasClients |
| 758 | << " so we can't tell clients again that we have client: " << hasClients |
| 759 | << " when: " << context; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 760 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 761 | ALOGI("Notifying %s they %s have clients when %s", serviceName.c_str(), |
| 762 | hasClients ? "do" : "don't", context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 763 | |
| 764 | auto ccIt = mNameToClientCallback.find(serviceName); |
| 765 | CHECK(ccIt != mNameToClientCallback.end()) |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 766 | << "sendClientCallbackNotifications could not find callbacks for service when " |
| 767 | << context; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 768 | |
| 769 | for (const auto& callback : ccIt->second) { |
| 770 | callback->onClients(service.binder, hasClients); |
| 771 | } |
| 772 | |
| 773 | service.hasClients = hasClients; |
| 774 | } |
| 775 | |
| 776 | Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) { |
| 777 | if (binder == nullptr) { |
| 778 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 779 | } |
| 780 | |
| 781 | auto ctx = mAccess->getCallingContext(); |
| 782 | if (!mAccess->canAdd(ctx, name)) { |
| 783 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 784 | } |
| 785 | |
| 786 | auto serviceIt = mNameToService.find(name); |
| 787 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 788 | ALOGW("Tried to unregister %s, but that service wasn't registered to begin with.", |
| 789 | name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 790 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 791 | } |
| 792 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 793 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 794 | ALOGW("Only a server can unregister itself (for %s)", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 795 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION); |
| 796 | } |
| 797 | |
| 798 | sp<IBinder> storedBinder = serviceIt->second.binder; |
| 799 | |
| 800 | if (binder != storedBinder) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 801 | ALOGW("Tried to unregister %s, but a different service is registered under this name.", |
| 802 | name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 803 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 804 | } |
| 805 | |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 806 | if (serviceIt->second.guaranteeClient) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 807 | ALOGI("Tried to unregister %s, but there is about to be a client.", name.c_str()); |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 808 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 809 | } |
| 810 | |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 811 | int clients = handleServiceClientCallback(name, false); |
| 812 | |
| 813 | // clients < 0: feature not implemented or other error. Assume clients. |
| 814 | // Otherwise: |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 815 | // - kernel driver will hold onto one refcount (during this transaction) |
| 816 | // - servicemanager has a refcount (guaranteed by this transaction) |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame^] | 817 | // So, if clients > 2, then at least one other service on the system must hold a refcount. |
| 818 | if (clients < 0 || clients > 2) { |
| 819 | // client callbacks are either disabled or there are other clients |
| 820 | ALOGI("Tried to unregister %s, but there are clients: %d", name.c_str(), clients); |
| 821 | // Set this flag to ensure the clients are acknowledged in the next callback |
Jon Spivack | 620d2dc | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 822 | serviceIt->second.guaranteeClient = true; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 823 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 824 | } |
| 825 | |
| 826 | mNameToService.erase(name); |
| 827 | |
| 828 | return Status::ok(); |
| 829 | } |
| 830 | |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 831 | Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) { |
| 832 | if (!mAccess->canList(mAccess->getCallingContext())) { |
| 833 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 834 | } |
| 835 | |
| 836 | outReturn->reserve(mNameToService.size()); |
| 837 | for (auto const& [name, service] : mNameToService) { |
| 838 | ServiceDebugInfo info; |
| 839 | info.name = name; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 840 | info.debugPid = service.ctx.debugPid; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 841 | |
| 842 | outReturn->push_back(std::move(info)); |
| 843 | } |
| 844 | |
| 845 | return Status::ok(); |
| 846 | } |
| 847 | |
Pawan Wagh | 243888e | 2022-09-20 19:37:35 +0000 | [diff] [blame] | 848 | void ServiceManager::clear() { |
| 849 | mNameToService.clear(); |
| 850 | mNameToRegistrationCallback.clear(); |
| 851 | mNameToClientCallback.clear(); |
| 852 | } |
| 853 | |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 854 | } // namespace android |