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