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 | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 230 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) { |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 231 | // TODO(b/151696835): reenable performance hack when we solve bug, since with |
| 232 | // this hack and other fixes, it is unlikely we will see even an ephemeral |
| 233 | // failure when the manifest parse fails. The goal is that the manifest will |
| 234 | // be read incorrectly and cause the process trying to register a HAL to |
| 235 | // fail. If this is in fact an early boot kernel contention issue, then we |
| 236 | // will get no failure, and by its absence, be signalled to invest more |
| 237 | // effort in re-adding this performance hack. |
| 238 | // #ifndef VENDORSERVICEMANAGER |
| 239 | // // can process these at any times, don't want to delay first VINTF client |
| 240 | // std::thread([] { |
| 241 | // vintf::VintfObject::GetDeviceHalManifest(); |
| 242 | // vintf::VintfObject::GetFrameworkHalManifest(); |
| 243 | // }).detach(); |
| 244 | // #endif // !VENDORSERVICEMANAGER |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 245 | } |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 246 | ServiceManager::~ServiceManager() { |
| 247 | // this should only happen in tests |
| 248 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 249 | for (const auto& [name, callbacks] : mNameToRegistrationCallback) { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 250 | CHECK(!callbacks.empty()) << name; |
| 251 | for (const auto& callback : callbacks) { |
| 252 | CHECK(callback != nullptr) << name; |
| 253 | } |
| 254 | } |
| 255 | |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 256 | for (const auto& [name, service] : mNameToService) { |
| 257 | CHECK(service.binder != nullptr) << name; |
| 258 | } |
| 259 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 260 | |
| 261 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 262 | *outBinder = tryGetService(name, true); |
| 263 | // returns ok regardless of result for legacy reasons |
| 264 | return Status::ok(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 268 | *outBinder = tryGetService(name, false); |
| 269 | // returns ok regardless of result for legacy reasons |
| 270 | return Status::ok(); |
| 271 | } |
| 272 | |
| 273 | sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 274 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 275 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 276 | sp<IBinder> out; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 277 | Service* service = nullptr; |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 278 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 279 | service = &(it->second); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 280 | |
Steven Moreland | b9e1cbe | 2023-02-01 22:44:45 +0000 | [diff] [blame^] | 281 | if (!service->allowIsolated && is_multiuser_uid_isolated(ctx.uid)) { |
| 282 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 283 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 284 | out = service->binder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 287 | if (!mAccess->canFind(ctx, name)) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 288 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 291 | if (!out && startIfNotFound) { |
| 292 | tryStartService(name); |
| 293 | } |
| 294 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 295 | if (out) { |
| 296 | // Setting this guarantee each time we hand out a binder ensures that the client-checking |
| 297 | // loop knows about the event even if the client immediately drops the service |
| 298 | service->guaranteeClient = true; |
| 299 | } |
| 300 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 301 | return out; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 304 | bool isValidServiceName(const std::string& name) { |
| 305 | if (name.size() == 0) return false; |
| 306 | if (name.size() > 127) return false; |
| 307 | |
| 308 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 309 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 310 | if (c >= 'a' && c <= 'z') continue; |
| 311 | if (c >= 'A' && c <= 'Z') continue; |
| 312 | if (c >= '0' && c <= '9') continue; |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 319 | Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 320 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 321 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 322 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 323 | return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 326 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 327 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denial"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (binder == nullptr) { |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 331 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 334 | if (!isValidServiceName(name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 335 | ALOGE("Invalid service name: %s", name.c_str()); |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 336 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 339 | #ifndef VENDORSERVICEMANAGER |
| 340 | if (!meetsDeclarationRequirements(binder, name)) { |
| 341 | // already logged |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 342 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error"); |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 343 | } |
| 344 | #endif // !VENDORSERVICEMANAGER |
| 345 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 346 | // implicitly unlinked when the binder is removed |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 347 | if (binder->remoteBinder() != nullptr && |
| 348 | binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 349 | ALOGE("Could not linkToDeath when adding %s", name.c_str()); |
Steven Moreland | ac2d285 | 2022-03-18 18:15:20 +0000 | [diff] [blame] | 350 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "linkToDeath failure"); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 353 | auto it = mNameToService.find(name); |
| 354 | if (it != mNameToService.end()) { |
| 355 | const Service& existing = it->second; |
| 356 | |
| 357 | // We could do better than this because if the other service dies, it |
| 358 | // may not have an entry here. However, this case is unlikely. We are |
| 359 | // only trying to detect when two different services are accidentally installed. |
| 360 | |
| 361 | if (existing.ctx.uid != ctx.uid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 362 | ALOGW("Service '%s' originally registered from UID %u but it is now being registered " |
| 363 | "from UID %u. Multiple instances installed?", |
| 364 | name.c_str(), existing.ctx.uid, ctx.uid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (existing.ctx.sid != ctx.sid) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 368 | ALOGW("Service '%s' originally registered from SID %s but it is now being registered " |
| 369 | "from SID %s. Multiple instances installed?", |
| 370 | name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str()); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 373 | ALOGI("Service '%s' originally registered from PID %d but it is being registered again " |
| 374 | "from PID %d. Bad state? Late death notification? Multiple instances installed?", |
| 375 | name.c_str(), existing.ctx.debugPid, ctx.debugPid); |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 378 | // Overwrite the old service if it exists |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 379 | mNameToService[name] = Service{ |
| 380 | .binder = binder, |
| 381 | .allowIsolated = allowIsolated, |
| 382 | .dumpPriority = dumpPriority, |
| 383 | .ctx = ctx, |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 384 | }; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 385 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 386 | if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 387 | for (const sp<IServiceCallback>& cb : it->second) { |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 388 | mNameToService[name].guaranteeClient = true; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 389 | // permission checked in registerForNotifications |
| 390 | cb->onRegistration(name, binder); |
| 391 | } |
| 392 | } |
| 393 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 394 | return Status::ok(); |
| 395 | } |
| 396 | |
| 397 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 398 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 399 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 400 | } |
| 401 | |
| 402 | size_t toReserve = 0; |
| 403 | for (auto const& [name, service] : mNameToService) { |
| 404 | (void) name; |
| 405 | |
| 406 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 407 | } |
| 408 | |
| 409 | CHECK(outList->empty()); |
| 410 | |
| 411 | outList->reserve(toReserve); |
| 412 | for (auto const& [name, service] : mNameToService) { |
| 413 | (void) service; |
| 414 | |
| 415 | if (service.dumpPriority & dumpPriority) { |
| 416 | outList->push_back(name); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return Status::ok(); |
| 421 | } |
| 422 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 423 | Status ServiceManager::registerForNotifications( |
| 424 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 425 | auto ctx = mAccess->getCallingContext(); |
| 426 | |
| 427 | if (!mAccess->canFind(ctx, name)) { |
Steven Moreland | b9e1cbe | 2023-02-01 22:44:45 +0000 | [diff] [blame^] | 428 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux"); |
| 429 | } |
| 430 | |
| 431 | // note - we could allow isolated apps to get notifications if we |
| 432 | // keep track of isolated callbacks and non-isolated callbacks, but |
| 433 | // this is done since isolated apps shouldn't access lazy services |
| 434 | // so we should be able to use different APIs to keep things simple. |
| 435 | // Here, we disallow everything, because the service might not be |
| 436 | // registered yet. |
| 437 | if (is_multiuser_uid_isolated(ctx.uid)) { |
| 438 | return Status::fromExceptionCode(Status::EX_SECURITY, "isolated app"); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (!isValidServiceName(name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 442 | ALOGE("Invalid service name: %s", name.c_str()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 443 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 444 | } |
| 445 | |
| 446 | if (callback == nullptr) { |
| 447 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 448 | } |
| 449 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 450 | if (OK != |
| 451 | IInterface::asBinder(callback)->linkToDeath( |
| 452 | sp<ServiceManager>::fromExisting(this))) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 453 | ALOGE("Could not linkToDeath when adding %s", name.c_str()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 454 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 455 | } |
| 456 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 457 | mNameToRegistrationCallback[name].push_back(callback); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 458 | |
| 459 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 460 | const sp<IBinder>& binder = it->second.binder; |
| 461 | |
| 462 | // never null if an entry exists |
| 463 | CHECK(binder != nullptr) << name; |
| 464 | callback->onRegistration(name, binder); |
| 465 | } |
| 466 | |
| 467 | return Status::ok(); |
| 468 | } |
| 469 | Status ServiceManager::unregisterForNotifications( |
| 470 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 471 | auto ctx = mAccess->getCallingContext(); |
| 472 | |
| 473 | if (!mAccess->canFind(ctx, name)) { |
| 474 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 475 | } |
| 476 | |
| 477 | bool found = false; |
| 478 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 479 | auto it = mNameToRegistrationCallback.find(name); |
| 480 | if (it != mNameToRegistrationCallback.end()) { |
| 481 | removeRegistrationCallback(IInterface::asBinder(callback), &it, &found); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (!found) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 485 | ALOGE("Trying to unregister callback, but none exists %s", name.c_str()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 486 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 487 | } |
| 488 | |
| 489 | return Status::ok(); |
| 490 | } |
| 491 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 492 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { |
| 493 | auto ctx = mAccess->getCallingContext(); |
| 494 | |
| 495 | if (!mAccess->canFind(ctx, name)) { |
| 496 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 497 | } |
| 498 | |
| 499 | *outReturn = false; |
| 500 | |
| 501 | #ifndef VENDORSERVICEMANAGER |
| 502 | *outReturn = isVintfDeclared(name); |
| 503 | #endif |
| 504 | return Status::ok(); |
| 505 | } |
| 506 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 507 | binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) { |
| 508 | auto ctx = mAccess->getCallingContext(); |
| 509 | |
| 510 | std::vector<std::string> allInstances; |
| 511 | #ifndef VENDORSERVICEMANAGER |
| 512 | allInstances = getVintfInstances(interface); |
| 513 | #endif |
| 514 | |
| 515 | outReturn->clear(); |
| 516 | |
| 517 | for (const std::string& instance : allInstances) { |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 518 | if (mAccess->canFind(ctx, interface + "/" + instance)) { |
| 519 | outReturn->push_back(instance); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (outReturn->size() == 0 && allInstances.size() != 0) { |
| 524 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 525 | } |
| 526 | |
| 527 | return Status::ok(); |
| 528 | } |
| 529 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 530 | Status ServiceManager::updatableViaApex(const std::string& name, |
| 531 | std::optional<std::string>* outReturn) { |
| 532 | auto ctx = mAccess->getCallingContext(); |
| 533 | |
| 534 | if (!mAccess->canFind(ctx, name)) { |
| 535 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 536 | } |
| 537 | |
| 538 | *outReturn = std::nullopt; |
| 539 | |
| 540 | #ifndef VENDORSERVICEMANAGER |
| 541 | *outReturn = getVintfUpdatableApex(name); |
| 542 | #endif |
| 543 | return Status::ok(); |
| 544 | } |
| 545 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 546 | Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName, |
| 547 | std::vector<std::string>* outReturn) { |
| 548 | auto ctx = mAccess->getCallingContext(); |
| 549 | |
| 550 | std::vector<std::string> apexUpdatableInstances; |
| 551 | #ifndef VENDORSERVICEMANAGER |
| 552 | apexUpdatableInstances = getVintfUpdatableInstances(apexName); |
| 553 | #endif |
| 554 | |
| 555 | outReturn->clear(); |
| 556 | |
| 557 | for (const std::string& instance : apexUpdatableInstances) { |
| 558 | if (mAccess->canFind(ctx, instance)) { |
| 559 | outReturn->push_back(instance); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (outReturn->size() == 0 && apexUpdatableInstances.size() != 0) { |
| 564 | return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denial"); |
| 565 | } |
| 566 | |
| 567 | return Status::ok(); |
| 568 | } |
| 569 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 570 | Status ServiceManager::getConnectionInfo(const std::string& name, |
| 571 | std::optional<ConnectionInfo>* outReturn) { |
| 572 | auto ctx = mAccess->getCallingContext(); |
| 573 | |
| 574 | if (!mAccess->canFind(ctx, name)) { |
| 575 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 576 | } |
| 577 | |
| 578 | *outReturn = std::nullopt; |
| 579 | |
| 580 | #ifndef VENDORSERVICEMANAGER |
| 581 | *outReturn = getVintfConnectionInfo(name); |
| 582 | #endif |
| 583 | return Status::ok(); |
| 584 | } |
| 585 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 586 | void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who, |
| 587 | ServiceCallbackMap::iterator* it, |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 588 | bool* found) { |
| 589 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; |
| 590 | |
| 591 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 592 | if (IInterface::asBinder(*lit) == who) { |
| 593 | if(found) *found = true; |
| 594 | lit = listeners.erase(lit); |
| 595 | } else { |
| 596 | ++lit; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (listeners.empty()) { |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 601 | *it = mNameToRegistrationCallback.erase(*it); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 602 | } else { |
Jon Spivack | e223f08 | 2019-11-19 16:21:20 -0800 | [diff] [blame] | 603 | (*it)++; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 607 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
| 608 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 609 | if (who == it->second.binder) { |
| 610 | it = mNameToService.erase(it); |
| 611 | } else { |
| 612 | ++it; |
| 613 | } |
| 614 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 615 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 616 | for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) { |
| 617 | removeRegistrationCallback(who, &it, nullptr /*found*/); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 618 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 619 | |
| 620 | for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) { |
| 621 | removeClientCallback(who, &it); |
| 622 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 625 | void ServiceManager::tryStartService(const std::string& name) { |
Steven Moreland | ba0f33c | 2022-11-04 22:24:31 +0000 | [diff] [blame] | 626 | ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service. (if it's not " |
| 627 | "configured to be a lazy service, it may be stuck starting or still starting).", |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 628 | name.c_str()); |
| 629 | |
| 630 | std::thread([=] { |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 631 | if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 632 | ALOGI("Tried to start aidl service %s as a lazy service, but was unable to. Usually " |
| 633 | "this happens when a " |
| 634 | "service is not installed, but if the service is intended to be used as a " |
| 635 | "lazy service, then it may be configured incorrectly.", |
| 636 | name.c_str()); |
Steven Moreland | bfe9fba | 2021-04-27 18:39:57 +0000 | [diff] [blame] | 637 | } |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 638 | }).detach(); |
| 639 | } |
| 640 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 641 | Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 642 | const sp<IClientCallback>& cb) { |
| 643 | if (cb == nullptr) { |
| 644 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 645 | } |
| 646 | |
| 647 | auto ctx = mAccess->getCallingContext(); |
| 648 | if (!mAccess->canAdd(ctx, name)) { |
| 649 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 650 | } |
| 651 | |
| 652 | auto serviceIt = mNameToService.find(name); |
| 653 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 654 | ALOGE("Could not add callback for nonexistent service: %s", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 655 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 656 | } |
| 657 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 658 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 659 | ALOGW("Only a server can register for client callbacks (for %s)", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 660 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION); |
| 661 | } |
| 662 | |
| 663 | if (serviceIt->second.binder != service) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 664 | ALOGW("Tried to register client callback for %s but a different service is registered " |
| 665 | "under this name.", |
| 666 | name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 667 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 668 | } |
| 669 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 670 | if (OK != |
| 671 | IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 672 | ALOGE("Could not linkToDeath when adding client callback for %s", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 673 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 674 | } |
| 675 | |
| 676 | mNameToClientCallback[name].push_back(cb); |
| 677 | |
| 678 | return Status::ok(); |
| 679 | } |
| 680 | |
| 681 | void ServiceManager::removeClientCallback(const wp<IBinder>& who, |
| 682 | ClientCallbackMap::iterator* it) { |
| 683 | std::vector<sp<IClientCallback>>& listeners = (*it)->second; |
| 684 | |
| 685 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 686 | if (IInterface::asBinder(*lit) == who) { |
| 687 | lit = listeners.erase(lit); |
| 688 | } else { |
| 689 | ++lit; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (listeners.empty()) { |
| 694 | *it = mNameToClientCallback.erase(*it); |
| 695 | } else { |
| 696 | (*it)++; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | ssize_t ServiceManager::Service::getNodeStrongRefCount() { |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 701 | sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 702 | if (bpBinder == nullptr) return -1; |
| 703 | |
Steven Moreland | e839388 | 2020-12-18 02:27:20 +0000 | [diff] [blame] | 704 | return ProcessState::self()->getStrongRefCountForNode(bpBinder); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void ServiceManager::handleClientCallbacks() { |
| 708 | for (const auto& [name, service] : mNameToService) { |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 709 | handleServiceClientCallback(name, true); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 713 | ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName, |
| 714 | bool isCalledOnInterval) { |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 715 | auto serviceIt = mNameToService.find(serviceName); |
| 716 | if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) { |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | Service& service = serviceIt->second; |
| 721 | ssize_t count = service.getNodeStrongRefCount(); |
| 722 | |
| 723 | // binder driver doesn't support this feature |
| 724 | if (count == -1) return count; |
| 725 | |
| 726 | bool hasClients = count > 1; // this process holds a strong count |
| 727 | |
| 728 | if (service.guaranteeClient) { |
| 729 | // we have no record of this client |
| 730 | if (!service.hasClients && !hasClients) { |
| 731 | sendClientCallbackNotifications(serviceName, true); |
| 732 | } |
| 733 | |
| 734 | // guarantee is temporary |
| 735 | service.guaranteeClient = false; |
| 736 | } |
| 737 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 738 | // only send notifications if this was called via the interval checking workflow |
| 739 | if (isCalledOnInterval) { |
| 740 | if (hasClients && !service.hasClients) { |
| 741 | // client was retrieved in some other way |
| 742 | sendClientCallbackNotifications(serviceName, true); |
| 743 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 744 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 745 | // there are no more clients, but the callback has not been called yet |
| 746 | if (!hasClients && service.hasClients) { |
| 747 | sendClientCallbackNotifications(serviceName, false); |
| 748 | } |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | return count; |
| 752 | } |
| 753 | |
| 754 | void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, bool hasClients) { |
| 755 | auto serviceIt = mNameToService.find(serviceName); |
| 756 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 757 | ALOGW("sendClientCallbackNotifications could not find service %s", serviceName.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | Service& service = serviceIt->second; |
| 761 | |
| 762 | CHECK(hasClients != service.hasClients) << "Record shows: " << service.hasClients |
| 763 | << " so we can't tell clients again that we have client: " << hasClients; |
| 764 | |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 765 | ALOGI("Notifying %s they have clients: %d", serviceName.c_str(), hasClients); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 766 | |
| 767 | auto ccIt = mNameToClientCallback.find(serviceName); |
| 768 | CHECK(ccIt != mNameToClientCallback.end()) |
| 769 | << "sendClientCallbackNotifications could not find callbacks for service "; |
| 770 | |
| 771 | for (const auto& callback : ccIt->second) { |
| 772 | callback->onClients(service.binder, hasClients); |
| 773 | } |
| 774 | |
| 775 | service.hasClients = hasClients; |
| 776 | } |
| 777 | |
| 778 | Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) { |
| 779 | if (binder == nullptr) { |
| 780 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 781 | } |
| 782 | |
| 783 | auto ctx = mAccess->getCallingContext(); |
| 784 | if (!mAccess->canAdd(ctx, name)) { |
| 785 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 786 | } |
| 787 | |
| 788 | auto serviceIt = mNameToService.find(name); |
| 789 | if (serviceIt == mNameToService.end()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 790 | ALOGW("Tried to unregister %s, but that service wasn't registered to begin with.", |
| 791 | name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 792 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 793 | } |
| 794 | |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 795 | if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 796 | ALOGW("Only a server can unregister itself (for %s)", name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 797 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION); |
| 798 | } |
| 799 | |
| 800 | sp<IBinder> storedBinder = serviceIt->second.binder; |
| 801 | |
| 802 | if (binder != storedBinder) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 803 | ALOGW("Tried to unregister %s, but a different service is registered under this name.", |
| 804 | name.c_str()); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 805 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 806 | } |
| 807 | |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 808 | if (serviceIt->second.guaranteeClient) { |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 809 | ALOGI("Tried to unregister %s, but there is about to be a client.", name.c_str()); |
Jon Spivack | 0f18f2c | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 810 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 811 | } |
| 812 | |
Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 813 | int clients = handleServiceClientCallback(name, false); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 814 | |
| 815 | // clients < 0: feature not implemented or other error. Assume clients. |
| 816 | // Otherwise: |
| 817 | // - kernel driver will hold onto one refcount (during this transaction) |
| 818 | // - servicemanager has a refcount (guaranteed by this transaction) |
| 819 | // So, if clients > 2, then at least one other service on the system must hold a refcount. |
| 820 | if (clients < 0 || clients > 2) { |
| 821 | // client callbacks are either disabled or there are other clients |
Pawan Wagh | 3752616 | 2022-09-29 21:55:26 +0000 | [diff] [blame] | 822 | ALOGI("Tried to unregister %s, but there are clients: %d", name.c_str(), clients); |
Jon Spivack | 620d2dc | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 823 | // Set this flag to ensure the clients are acknowledged in the next callback |
| 824 | serviceIt->second.guaranteeClient = true; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 825 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 826 | } |
| 827 | |
| 828 | mNameToService.erase(name); |
| 829 | |
| 830 | return Status::ok(); |
| 831 | } |
| 832 | |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 833 | Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) { |
| 834 | if (!mAccess->canList(mAccess->getCallingContext())) { |
| 835 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 836 | } |
| 837 | |
| 838 | outReturn->reserve(mNameToService.size()); |
| 839 | for (auto const& [name, service] : mNameToService) { |
| 840 | ServiceDebugInfo info; |
| 841 | info.name = name; |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 842 | info.debugPid = service.ctx.debugPid; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 843 | |
| 844 | outReturn->push_back(std::move(info)); |
| 845 | } |
| 846 | |
| 847 | return Status::ok(); |
| 848 | } |
| 849 | |
Pawan Wagh | 243888e | 2022-09-20 19:37:35 +0000 | [diff] [blame] | 850 | void ServiceManager::clear() { |
| 851 | mNameToService.clear(); |
| 852 | mNameToRegistrationCallback.clear(); |
| 853 | mNameToClientCallback.clear(); |
| 854 | } |
| 855 | |
Steven Moreland | 8d0c9a7 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 856 | } // namespace android |