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