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 | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 225 | ServiceManager::Service::~Service() { |
Steven Moreland | cb59156 | 2023-03-06 15:53:44 +0000 | [diff] [blame] | 226 | if (hasClients) { |
| 227 | // only expected to happen on process death, we don't store the service |
| 228 | // name this late (it's in the map that holds this service), but if it |
| 229 | // is happening, we might want to change 'unlinkToDeath' to explicitly |
| 230 | // clear this bit so that we can abort in other cases, where it would |
| 231 | // mean inconsistent logic in servicemanager (unexpected and tested, but |
| 232 | // the original lazy service impl here had that bug). |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 233 | LOG(WARNING) << "a service was removed when there are clients"; |
| 234 | } |
| 235 | } |
| 236 | |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 237 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) { |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 238 | // TODO(b/151696835): reenable performance hack when we solve bug, since with |
| 239 | // this hack and other fixes, it is unlikely we will see even an ephemeral |
| 240 | // failure when the manifest parse fails. The goal is that the manifest will |
| 241 | // be read incorrectly and cause the process trying to register a HAL to |
| 242 | // fail. If this is in fact an early boot kernel contention issue, then we |
| 243 | // will get no failure, and by its absence, be signalled to invest more |
| 244 | // effort in re-adding this performance hack. |
| 245 | // #ifndef VENDORSERVICEMANAGER |
| 246 | // // can process these at any times, don't want to delay first VINTF client |
| 247 | // std::thread([] { |
| 248 | // vintf::VintfObject::GetDeviceHalManifest(); |
| 249 | // vintf::VintfObject::GetFrameworkHalManifest(); |
| 250 | // }).detach(); |
| 251 | // #endif // !VENDORSERVICEMANAGER |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 252 | } |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 253 | ServiceManager::~ServiceManager() { |
| 254 | // this should only happen in tests |
| 255 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 256 | for (const auto& [name, callbacks] : mNameToRegistrationCallback) { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 257 | CHECK(!callbacks.empty()) << name; |
| 258 | for (const auto& callback : callbacks) { |
| 259 | CHECK(callback != nullptr) << name; |
| 260 | } |
| 261 | } |
| 262 | |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 263 | for (const auto& [name, service] : mNameToService) { |
| 264 | CHECK(service.binder != nullptr) << name; |
| 265 | } |
| 266 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 267 | |
| 268 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 269 | *outBinder = tryGetService(name, true); |
| 270 | // returns ok regardless of result for legacy reasons |
| 271 | return Status::ok(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 275 | *outBinder = tryGetService(name, false); |
| 276 | // returns ok regardless of result for legacy reasons |
| 277 | return Status::ok(); |
| 278 | } |
| 279 | |
| 280 | sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 281 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 282 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 283 | sp<IBinder> out; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 284 | Service* service = nullptr; |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 285 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 286 | service = &(it->second); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 287 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 288 | if (!service->allowIsolated) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 289 | uid_t appid = multiuser_get_app_id(ctx.uid); |
| 290 | bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 291 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 292 | if (isIsolated) { |
| 293 | return nullptr; |
| 294 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 295 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 296 | out = service->binder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 299 | if (!mAccess->canFind(ctx, name)) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 300 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 303 | if (!out && startIfNotFound) { |
Steven Moreland | aa33e85 | 2023-05-10 16:42:15 +0000 | [diff] [blame] | 304 | tryStartService(ctx, name); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 307 | if (out) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 308 | // Force onClients to get sent, and then make sure the timerfd won't clear it |
| 309 | // by setting guaranteeClient again. This logic could be simplified by using |
| 310 | // a time-based guarantee. However, forcing onClients(true) to get sent |
| 311 | // right here is always going to be important for processes serving multiple |
| 312 | // lazy interfaces. |
| 313 | service->guaranteeClient = true; |
| 314 | CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false)); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 315 | service->guaranteeClient = true; |
| 316 | } |
| 317 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 318 | return out; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 321 | bool isValidServiceName(const std::string& name) { |
| 322 | if (name.size() == 0) return false; |
| 323 | if (name.size() > 127) return false; |
| 324 | |
| 325 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 326 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 327 | if (c >= 'a' && c <= 'z') continue; |
| 328 | if (c >= 'A' && c <= 'Z') continue; |
| 329 | if (c >= '0' && c <= '9') continue; |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 336 | 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] | 337 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 338 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 339 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 340 | return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 343 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 344 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (binder == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 348 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 351 | if (!isValidServiceName(name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 352 | ALOGE("Invalid service name: %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 353 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 356 | #ifndef VENDORSERVICEMANAGER |
| 357 | if (!meetsDeclarationRequirements(binder, name)) { |
| 358 | // already logged |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 359 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error."); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 360 | } |
| 361 | #endif // !VENDORSERVICEMANAGER |
| 362 | |
Devin Moore | 4e21def | 2023-02-24 21:54:14 +0000 | [diff] [blame] | 363 | if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) { |
| 364 | ALOGW("Dump flag priority is not set when adding %s", name.c_str()); |
| 365 | } |
| 366 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 367 | // implicitly unlinked when the binder is removed |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 368 | if (binder->remoteBinder() != nullptr && |
| 369 | binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 370 | ALOGE("Could not linkToDeath when adding %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 371 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 374 | auto it = mNameToService.find(name); |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 375 | bool prevClients = false; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 376 | if (it != mNameToService.end()) { |
| 377 | const Service& existing = it->second; |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 378 | prevClients = existing.hasClients; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 379 | |
| 380 | // We could do better than this because if the other service dies, it |
| 381 | // may not have an entry here. However, this case is unlikely. We are |
| 382 | // only trying to detect when two different services are accidentally installed. |
| 383 | |
| 384 | if (existing.ctx.uid != ctx.uid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 385 | ALOGW("Service '%s' originally registered from UID %u but it is now being registered " |
| 386 | "from UID %u. Multiple instances installed?", |
| 387 | name.c_str(), existing.ctx.uid, ctx.uid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | if (existing.ctx.sid != ctx.sid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 391 | ALOGW("Service '%s' originally registered from SID %s but it is now being registered " |
| 392 | "from SID %s. Multiple instances installed?", |
| 393 | name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str()); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 396 | ALOGI("Service '%s' originally registered from PID %d but it is being registered again " |
| 397 | "from PID %d. Bad state? Late death notification? Multiple instances installed?", |
| 398 | name.c_str(), existing.ctx.debugPid, ctx.debugPid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 401 | // Overwrite the old service if it exists |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 402 | mNameToService[name] = Service{ |
| 403 | .binder = binder, |
| 404 | .allowIsolated = allowIsolated, |
| 405 | .dumpPriority = dumpPriority, |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 406 | .hasClients = prevClients, // see b/279898063, matters if existing callbacks |
Steven Moreland | efea66b | 2023-06-17 01:59:34 +0000 | [diff] [blame^] | 407 | .guaranteeClient = false, |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 408 | .ctx = ctx, |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 409 | }; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 410 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 411 | if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) { |
Steven Moreland | efea66b | 2023-06-17 01:59:34 +0000 | [diff] [blame^] | 412 | // If someone is currently waiting on the service, notify the service that |
| 413 | // we're waiting and flush it to the service. |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 414 | mNameToService[name].guaranteeClient = true; |
| 415 | CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false)); |
| 416 | mNameToService[name].guaranteeClient = true; |
| 417 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 418 | for (const sp<IServiceCallback>& cb : it->second) { |
| 419 | // permission checked in registerForNotifications |
| 420 | cb->onRegistration(name, binder); |
| 421 | } |
| 422 | } |
| 423 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 424 | return Status::ok(); |
| 425 | } |
| 426 | |
| 427 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 428 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 429 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | size_t toReserve = 0; |
| 433 | for (auto const& [name, service] : mNameToService) { |
| 434 | (void) name; |
| 435 | |
| 436 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 437 | } |
| 438 | |
| 439 | CHECK(outList->empty()); |
| 440 | |
| 441 | outList->reserve(toReserve); |
| 442 | for (auto const& [name, service] : mNameToService) { |
| 443 | (void) service; |
| 444 | |
| 445 | if (service.dumpPriority & dumpPriority) { |
| 446 | outList->push_back(name); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | return Status::ok(); |
| 451 | } |
| 452 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 453 | Status ServiceManager::registerForNotifications( |
| 454 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 455 | auto ctx = mAccess->getCallingContext(); |
| 456 | |
| 457 | if (!mAccess->canFind(ctx, name)) { |
| 458 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 459 | } |
| 460 | |
| 461 | if (!isValidServiceName(name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 462 | ALOGE("Invalid service name: %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 463 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | if (callback == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 467 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null callback."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 470 | if (OK != |
| 471 | IInterface::asBinder(callback)->linkToDeath( |
| 472 | sp<ServiceManager>::fromExisting(this))) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 473 | ALOGE("Could not linkToDeath when adding %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 474 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 477 | mNameToRegistrationCallback[name].push_back(callback); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 478 | |
| 479 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 480 | const sp<IBinder>& binder = it->second.binder; |
| 481 | |
| 482 | // never null if an entry exists |
| 483 | CHECK(binder != nullptr) << name; |
| 484 | callback->onRegistration(name, binder); |
| 485 | } |
| 486 | |
| 487 | return Status::ok(); |
| 488 | } |
| 489 | Status ServiceManager::unregisterForNotifications( |
| 490 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 491 | auto ctx = mAccess->getCallingContext(); |
| 492 | |
| 493 | if (!mAccess->canFind(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 494 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | bool found = false; |
| 498 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 499 | auto it = mNameToRegistrationCallback.find(name); |
| 500 | if (it != mNameToRegistrationCallback.end()) { |
| 501 | removeRegistrationCallback(IInterface::asBinder(callback), &it, &found); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (!found) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 505 | ALOGE("Trying to unregister callback, but none exists %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 506 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister."); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return Status::ok(); |
| 510 | } |
| 511 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 512 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { |
| 513 | auto ctx = mAccess->getCallingContext(); |
| 514 | |
| 515 | if (!mAccess->canFind(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 516 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | *outReturn = false; |
| 520 | |
| 521 | #ifndef VENDORSERVICEMANAGER |
| 522 | *outReturn = isVintfDeclared(name); |
| 523 | #endif |
| 524 | return Status::ok(); |
| 525 | } |
| 526 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 527 | binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) { |
| 528 | auto ctx = mAccess->getCallingContext(); |
| 529 | |
| 530 | std::vector<std::string> allInstances; |
| 531 | #ifndef VENDORSERVICEMANAGER |
| 532 | allInstances = getVintfInstances(interface); |
| 533 | #endif |
| 534 | |
| 535 | outReturn->clear(); |
| 536 | |
| 537 | for (const std::string& instance : allInstances) { |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 538 | if (mAccess->canFind(ctx, interface + "/" + instance)) { |
| 539 | outReturn->push_back(instance); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (outReturn->size() == 0 && allInstances.size() != 0) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 544 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return Status::ok(); |
| 548 | } |
| 549 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 550 | Status ServiceManager::updatableViaApex(const std::string& name, |
| 551 | std::optional<std::string>* outReturn) { |
| 552 | auto ctx = mAccess->getCallingContext(); |
| 553 | |
| 554 | if (!mAccess->canFind(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 555 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | *outReturn = std::nullopt; |
| 559 | |
| 560 | #ifndef VENDORSERVICEMANAGER |
| 561 | *outReturn = getVintfUpdatableApex(name); |
| 562 | #endif |
| 563 | return Status::ok(); |
| 564 | } |
| 565 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 566 | Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName, |
| 567 | std::vector<std::string>* outReturn) { |
| 568 | auto ctx = mAccess->getCallingContext(); |
| 569 | |
| 570 | std::vector<std::string> apexUpdatableInstances; |
| 571 | #ifndef VENDORSERVICEMANAGER |
| 572 | apexUpdatableInstances = getVintfUpdatableInstances(apexName); |
| 573 | #endif |
| 574 | |
| 575 | outReturn->clear(); |
| 576 | |
| 577 | for (const std::string& instance : apexUpdatableInstances) { |
| 578 | if (mAccess->canFind(ctx, instance)) { |
| 579 | outReturn->push_back(instance); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if (outReturn->size() == 0 && apexUpdatableInstances.size() != 0) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 584 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return Status::ok(); |
| 588 | } |
| 589 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 590 | Status ServiceManager::getConnectionInfo(const std::string& name, |
| 591 | std::optional<ConnectionInfo>* outReturn) { |
| 592 | auto ctx = mAccess->getCallingContext(); |
| 593 | |
| 594 | if (!mAccess->canFind(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 595 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | *outReturn = std::nullopt; |
| 599 | |
| 600 | #ifndef VENDORSERVICEMANAGER |
| 601 | *outReturn = getVintfConnectionInfo(name); |
| 602 | #endif |
| 603 | return Status::ok(); |
| 604 | } |
| 605 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 606 | void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who, |
| 607 | ServiceCallbackMap::iterator* it, |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 608 | bool* found) { |
| 609 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; |
| 610 | |
| 611 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 612 | if (IInterface::asBinder(*lit) == who) { |
| 613 | if(found) *found = true; |
| 614 | lit = listeners.erase(lit); |
| 615 | } else { |
| 616 | ++lit; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (listeners.empty()) { |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 621 | *it = mNameToRegistrationCallback.erase(*it); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 622 | } else { |
Jon Spivack | e223f08 | 2019-11-19 16:21:20 -0800 | [diff] [blame] | 623 | (*it)++; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 627 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
| 628 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 629 | if (who == it->second.binder) { |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 630 | // TODO: currently, this entry contains the state also |
| 631 | // associated with mNameToClientCallback. If we allowed |
| 632 | // other processes to register client callbacks, we |
| 633 | // would have to preserve hasClients (perhaps moving |
| 634 | // that state into mNameToClientCallback, which is complicated |
| 635 | // because those callbacks are associated w/ particular binder |
| 636 | // objects, though they are indexed by name now, they may |
| 637 | // need to be indexed by binder at that point). |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 638 | it = mNameToService.erase(it); |
| 639 | } else { |
| 640 | ++it; |
| 641 | } |
| 642 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 643 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 644 | for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) { |
| 645 | removeRegistrationCallback(who, &it, nullptr /*found*/); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 646 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 647 | |
| 648 | for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) { |
| 649 | removeClientCallback(who, &it); |
| 650 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Steven Moreland | aa33e85 | 2023-05-10 16:42:15 +0000 | [diff] [blame] | 653 | void ServiceManager::tryStartService(const Access::CallingContext& ctx, const std::string& name) { |
| 654 | ALOGI("Since '%s' could not be found (requested by debug pid %d), trying to start it as a lazy " |
| 655 | "AIDL service. (if it's not configured to be a lazy service, it may be stuck starting or " |
| 656 | "still starting).", |
| 657 | name.c_str(), ctx.debugPid); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 658 | |
| 659 | std::thread([=] { |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 660 | if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 661 | ALOGI("Tried to start aidl service %s as a lazy service, but was unable to. Usually " |
| 662 | "this happens when a " |
| 663 | "service is not installed, but if the service is intended to be used as a " |
| 664 | "lazy service, then it may be configured incorrectly.", |
| 665 | name.c_str()); |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 666 | } |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 667 | }).detach(); |
| 668 | } |
| 669 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 670 | Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 671 | const sp<IClientCallback>& cb) { |
| 672 | if (cb == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 673 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Callback null."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | auto ctx = mAccess->getCallingContext(); |
| 677 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 678 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | auto serviceIt = mNameToService.find(name); |
| 682 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 683 | ALOGE("Could not add callback for nonexistent service: %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 684 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 687 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 688 | ALOGW("Only a server can register for client callbacks (for %s)", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 689 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 690 | "Only service can register client callback for itself."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (serviceIt->second.binder != service) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 694 | ALOGW("Tried to register client callback for %s but a different service is registered " |
| 695 | "under this name.", |
| 696 | name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 697 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service mismatch."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 700 | if (OK != |
| 701 | IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 702 | ALOGE("Could not linkToDeath when adding client callback for %s", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 703 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Steven Moreland | 7957867 | 2023-04-27 19:38:00 +0000 | [diff] [blame] | 706 | // WARNING: binderDied makes an assumption about this. If we open up client |
| 707 | // callbacks to other services, certain race conditions may lead to services |
| 708 | // getting extra client callback notifications. |
| 709 | // Make sure all callbacks have been told about a consistent state - b/278038751 |
Steven Moreland | 7bb4ab8 | 2023-04-13 20:29:33 +0000 | [diff] [blame] | 710 | if (serviceIt->second.hasClients) { |
| 711 | cb->onClients(service, true); |
| 712 | } |
| 713 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 714 | mNameToClientCallback[name].push_back(cb); |
| 715 | |
Steven Moreland | efea66b | 2023-06-17 01:59:34 +0000 | [diff] [blame^] | 716 | // Flush updated info to client callbacks (especially if guaranteeClient |
| 717 | // and !hasClient, see b/285202885). We may or may not have clients at |
| 718 | // this point, so ignore the return value. |
| 719 | (void)handleServiceClientCallback(2 /* sm + transaction */, name, false); |
| 720 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 721 | return Status::ok(); |
| 722 | } |
| 723 | |
| 724 | void ServiceManager::removeClientCallback(const wp<IBinder>& who, |
| 725 | ClientCallbackMap::iterator* it) { |
| 726 | std::vector<sp<IClientCallback>>& listeners = (*it)->second; |
| 727 | |
| 728 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 729 | if (IInterface::asBinder(*lit) == who) { |
| 730 | lit = listeners.erase(lit); |
| 731 | } else { |
| 732 | ++lit; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (listeners.empty()) { |
| 737 | *it = mNameToClientCallback.erase(*it); |
| 738 | } else { |
| 739 | (*it)++; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | ssize_t ServiceManager::Service::getNodeStrongRefCount() { |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 744 | sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 745 | if (bpBinder == nullptr) return -1; |
| 746 | |
Steven Moreland | e839388 | 2020-12-18 02:27:20 +0000 | [diff] [blame] | 747 | return ProcessState::self()->getStrongRefCountForNode(bpBinder); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void ServiceManager::handleClientCallbacks() { |
| 751 | for (const auto& [name, service] : mNameToService) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 752 | handleServiceClientCallback(1 /* sm has one refcount */, name, true); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 756 | bool ServiceManager::handleServiceClientCallback(size_t knownClients, |
| 757 | const std::string& serviceName, |
| 758 | bool isCalledOnInterval) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 759 | auto serviceIt = mNameToService.find(serviceName); |
| 760 | if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 761 | return true; // return we do have clients a.k.a. DON'T DO ANYTHING |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | Service& service = serviceIt->second; |
| 765 | ssize_t count = service.getNodeStrongRefCount(); |
| 766 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 767 | // binder driver doesn't support this feature, consider we have clients |
| 768 | if (count == -1) return true; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 769 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 770 | bool hasKernelReportedClients = static_cast<size_t>(count) > knownClients; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 771 | |
| 772 | if (service.guaranteeClient) { |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 773 | if (!service.hasClients && !hasKernelReportedClients) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 774 | sendClientCallbackNotifications(serviceName, true, |
| 775 | "service is guaranteed to be in use"); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | // guarantee is temporary |
| 779 | service.guaranteeClient = false; |
| 780 | } |
| 781 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 782 | // Regardless of this situation, we want to give this notification as soon as possible. |
| 783 | // This way, we have a chance of preventing further thrashing. |
| 784 | if (hasKernelReportedClients && !service.hasClients) { |
| 785 | sendClientCallbackNotifications(serviceName, true, "we now have a record of a client"); |
| 786 | } |
Steven Moreland | 6641765 | 2023-02-01 22:19:41 +0000 | [diff] [blame] | 787 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 788 | // But limit rate of shutting down service. |
| 789 | if (isCalledOnInterval) { |
| 790 | if (!hasKernelReportedClients && service.hasClients) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 791 | sendClientCallbackNotifications(serviceName, false, |
| 792 | "we now have no record of a client"); |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 793 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 796 | // May be different than 'hasKernelReportedClients'. We intentionally delay |
| 797 | // information about clients going away to reduce thrashing. |
| 798 | return service.hasClients; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 801 | void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, |
| 802 | bool hasClients, const char* context) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 803 | auto serviceIt = mNameToService.find(serviceName); |
| 804 | if (serviceIt == mNameToService.end()) { |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 805 | ALOGW("sendClientCallbackNotifications could not find service %s when %s", |
| 806 | serviceName.c_str(), context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 807 | return; |
| 808 | } |
| 809 | Service& service = serviceIt->second; |
| 810 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 811 | CHECK_NE(hasClients, service.hasClients) << context; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 812 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 813 | ALOGI("Notifying %s they %s (previously: %s) have clients when %s", serviceName.c_str(), |
| 814 | hasClients ? "do" : "don't", service.hasClients ? "do" : "don't", context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 815 | |
| 816 | auto ccIt = mNameToClientCallback.find(serviceName); |
| 817 | CHECK(ccIt != mNameToClientCallback.end()) |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 818 | << "sendClientCallbackNotifications could not find callbacks for service when " |
| 819 | << context; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 820 | |
| 821 | for (const auto& callback : ccIt->second) { |
| 822 | callback->onClients(service.binder, hasClients); |
| 823 | } |
| 824 | |
| 825 | service.hasClients = hasClients; |
| 826 | } |
| 827 | |
| 828 | Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) { |
| 829 | if (binder == nullptr) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 830 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null service."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | auto ctx = mAccess->getCallingContext(); |
| 834 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 835 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | auto serviceIt = mNameToService.find(name); |
| 839 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 840 | ALOGW("Tried to unregister %s, but that service wasn't registered to begin with.", |
| 841 | name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 842 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 845 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 846 | ALOGW("Only a server can unregister itself (for %s)", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 847 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION, |
| 848 | "Service can only unregister itself."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | sp<IBinder> storedBinder = serviceIt->second.binder; |
| 852 | |
| 853 | if (binder != storedBinder) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 854 | ALOGW("Tried to unregister %s, but a different service is registered under this name.", |
| 855 | name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 856 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, |
| 857 | "Different service registered under this name."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 860 | // important because we don't have timer-based guarantees, we don't want to clear |
| 861 | // this |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 862 | if (serviceIt->second.guaranteeClient) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 863 | ALOGI("Tried to unregister %s, but there is about to be a client.", name.c_str()); |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 864 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, |
| 865 | "Can't unregister, pending client."); |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 868 | // - kernel driver will hold onto one refcount (during this transaction) |
| 869 | // - servicemanager has a refcount (guaranteed by this transaction) |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 870 | constexpr size_t kKnownClients = 2; |
| 871 | |
| 872 | if (handleServiceClientCallback(kKnownClients, name, false)) { |
| 873 | ALOGI("Tried to unregister %s, but there are clients.", name.c_str()); |
| 874 | |
| 875 | // Since we had a failed registration attempt, and the HIDL implementation of |
| 876 | // delaying service shutdown for multiple periods wasn't ported here... this may |
| 877 | // help reduce thrashing, but we should be able to remove it. |
Jon Spivack | 620d2dc | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 878 | serviceIt->second.guaranteeClient = true; |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 879 | |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 880 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, |
| 881 | "Can't unregister, known client."); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 884 | ALOGI("Unregistering %s", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 885 | mNameToService.erase(name); |
| 886 | |
| 887 | return Status::ok(); |
| 888 | } |
| 889 | |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 890 | Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) { |
| 891 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | ffb905b | 2023-03-28 18:24:37 +0000 | [diff] [blame] | 892 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied."); |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | outReturn->reserve(mNameToService.size()); |
| 896 | for (auto const& [name, service] : mNameToService) { |
| 897 | ServiceDebugInfo info; |
| 898 | info.name = name; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 899 | info.debugPid = service.ctx.debugPid; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 900 | |
| 901 | outReturn->push_back(std::move(info)); |
| 902 | } |
| 903 | |
| 904 | return Status::ok(); |
| 905 | } |
| 906 | |
Pawan Wagh | 243888e | 2022-09-20 19:37:35 +0000 | [diff] [blame] | 907 | void ServiceManager::clear() { |
| 908 | mNameToService.clear(); |
| 909 | mNameToRegistrationCallback.clear(); |
| 910 | mNameToClientCallback.clear(); |
| 911 | } |
| 912 | |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 913 | } // namespace android |