| 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> | 
|  | 31 | #include <vintf/constants.h> | 
|  | 32 | #endif  // !VENDORSERVICEMANAGER | 
|  | 33 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 34 | using ::android::binder::Status; | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 35 | using ::android::internal::Stability; | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | namespace android { | 
|  | 38 |  | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 39 | #ifndef VENDORSERVICEMANAGER | 
| Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 40 | static bool isVintfDeclared(const std::string& name) { | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 41 | size_t firstSlash = name.find('/'); | 
|  | 42 | size_t lastDot = name.rfind('.', firstSlash); | 
|  | 43 | if (firstSlash == std::string::npos || lastDot == std::string::npos) { | 
|  | 44 | LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. " | 
|  | 45 | << "some.package.foo.IFoo/default) but got: " << name; | 
|  | 46 | return false; | 
|  | 47 | } | 
|  | 48 | const std::string package = name.substr(0, lastDot); | 
|  | 49 | const std::string iface = name.substr(lastDot+1, firstSlash-lastDot-1); | 
|  | 50 | const std::string instance = name.substr(firstSlash+1); | 
|  | 51 |  | 
| Steven Moreland | e192a67 | 2020-04-30 17:04:54 -0700 | [diff] [blame] | 52 | struct ManifestWithDescription { | 
|  | 53 | std::shared_ptr<const vintf::HalManifest> manifest; | 
|  | 54 | const char* description; | 
|  | 55 | }; | 
|  | 56 | for (const ManifestWithDescription& mwd : { | 
|  | 57 | ManifestWithDescription{ vintf::VintfObject::GetDeviceHalManifest(), "device" }, | 
|  | 58 | ManifestWithDescription{ vintf::VintfObject::GetFrameworkHalManifest(), "framework" }, | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 59 | }) { | 
| Steven Moreland | e192a67 | 2020-04-30 17:04:54 -0700 | [diff] [blame] | 60 | if (mwd.manifest == nullptr) { | 
|  | 61 | LOG(ERROR) << "NULL VINTF MANIFEST!: " << mwd.description; | 
|  | 62 | // note, we explicitly do not retry here, so that we can detect VINTF | 
|  | 63 | // or other bugs (b/151696835) | 
|  | 64 | continue; | 
|  | 65 | } | 
|  | 66 | if (mwd.manifest->hasAidlInstance(package, iface, instance)) { | 
|  | 67 | LOG(INFO) << "Found " << name << " in " << mwd.description << " VINTF manifest."; | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 68 | return true; | 
|  | 69 | } | 
|  | 70 | } | 
| Steven Moreland | e192a67 | 2020-04-30 17:04:54 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | // Although it is tested, explicitly rebuilding qualified name, in case it | 
|  | 73 | // becomes something unexpected. | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 74 | LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance | 
|  | 75 | << " in the VINTF manifest."; | 
|  | 76 | return false; | 
|  | 77 | } | 
| Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) { | 
|  | 80 | if (!Stability::requiresVintfDeclaration(binder)) { | 
|  | 81 | return true; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | return isVintfDeclared(name); | 
|  | 85 | } | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 86 | #endif  // !VENDORSERVICEMANAGER | 
|  | 87 |  | 
| Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 88 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) { | 
| Steven Moreland | d221c25 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 89 | // TODO(b/151696835): reenable performance hack when we solve bug, since with | 
|  | 90 | //     this hack and other fixes, it is unlikely we will see even an ephemeral | 
|  | 91 | //     failure when the manifest parse fails. The goal is that the manifest will | 
|  | 92 | //     be read incorrectly and cause the process trying to register a HAL to | 
|  | 93 | //     fail. If this is in fact an early boot kernel contention issue, then we | 
|  | 94 | //     will get no failure, and by its absence, be signalled to invest more | 
|  | 95 | //     effort in re-adding this performance hack. | 
|  | 96 | // #ifndef VENDORSERVICEMANAGER | 
|  | 97 | //     // can process these at any times, don't want to delay first VINTF client | 
|  | 98 | //     std::thread([] { | 
|  | 99 | //         vintf::VintfObject::GetDeviceHalManifest(); | 
|  | 100 | //         vintf::VintfObject::GetFrameworkHalManifest(); | 
|  | 101 | //     }).detach(); | 
|  | 102 | // #endif  // !VENDORSERVICEMANAGER | 
| Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame] | 103 | } | 
| Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 104 | ServiceManager::~ServiceManager() { | 
|  | 105 | // this should only happen in tests | 
|  | 106 |  | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 107 | for (const auto& [name, callbacks] : mNameToRegistrationCallback) { | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 108 | CHECK(!callbacks.empty()) << name; | 
|  | 109 | for (const auto& callback : callbacks) { | 
|  | 110 | CHECK(callback != nullptr) << name; | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
| Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 114 | for (const auto& [name, service] : mNameToService) { | 
|  | 115 | CHECK(service.binder != nullptr) << name; | 
|  | 116 | } | 
|  | 117 | } | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 120 | *outBinder = tryGetService(name, true); | 
|  | 121 | // returns ok regardless of result for legacy reasons | 
|  | 122 | return Status::ok(); | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
|  | 125 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 126 | *outBinder = tryGetService(name, false); | 
|  | 127 | // returns ok regardless of result for legacy reasons | 
|  | 128 | return Status::ok(); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { | 
| Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 132 | auto ctx = mAccess->getCallingContext(); | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 133 |  | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 134 | sp<IBinder> out; | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 135 | Service* service = nullptr; | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 136 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 137 | service = &(it->second); | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 138 |  | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 139 | if (!service->allowIsolated) { | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 140 | uid_t appid = multiuser_get_app_id(ctx.uid); | 
|  | 141 | bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 142 |  | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 143 | if (isIsolated) { | 
|  | 144 | return nullptr; | 
|  | 145 | } | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 146 | } | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 147 | out = service->binder; | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 150 | if (!mAccess->canFind(ctx, name)) { | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 151 | return nullptr; | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 154 | if (!out && startIfNotFound) { | 
|  | 155 | tryStartService(name); | 
|  | 156 | } | 
|  | 157 |  | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 158 | if (out) { | 
|  | 159 | // Setting this guarantee each time we hand out a binder ensures that the client-checking | 
|  | 160 | // loop knows about the event even if the client immediately drops the service | 
|  | 161 | service->guaranteeClient = true; | 
|  | 162 | } | 
|  | 163 |  | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 164 | return out; | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 167 | bool isValidServiceName(const std::string& name) { | 
|  | 168 | if (name.size() == 0) return false; | 
|  | 169 | if (name.size() > 127) return false; | 
|  | 170 |  | 
|  | 171 | for (char c : name) { | 
| Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 172 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; | 
| Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 173 | if (c >= 'a' && c <= 'z') continue; | 
|  | 174 | if (c >= 'A' && c <= 'Z') continue; | 
|  | 175 | if (c >= '0' && c <= '9') continue; | 
|  | 176 | return false; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | return true; | 
|  | 180 | } | 
|  | 181 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 182 | 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] | 183 | auto ctx = mAccess->getCallingContext(); | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 184 |  | 
|  | 185 | // apps cannot add services | 
|  | 186 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { | 
|  | 187 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 188 | } | 
|  | 189 |  | 
| Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 190 | if (!mAccess->canAdd(ctx, name)) { | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 191 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | if (binder == nullptr) { | 
|  | 195 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); | 
|  | 196 | } | 
|  | 197 |  | 
| Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 198 | if (!isValidServiceName(name)) { | 
|  | 199 | LOG(ERROR) << "Invalid service name: " << name; | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 200 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); | 
|  | 201 | } | 
|  | 202 |  | 
| Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 203 | #ifndef VENDORSERVICEMANAGER | 
|  | 204 | if (!meetsDeclarationRequirements(binder, name)) { | 
|  | 205 | // already logged | 
|  | 206 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); | 
|  | 207 | } | 
|  | 208 | #endif  // !VENDORSERVICEMANAGER | 
|  | 209 |  | 
| Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 210 | // implicitly unlinked when the binder is removed | 
| Steven Moreland | 46f380b | 2019-10-16 16:28:21 -0700 | [diff] [blame] | 211 | if (binder->remoteBinder() != nullptr && binder->linkToDeath(this) != OK) { | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 212 | LOG(ERROR) << "Could not linkToDeath when adding " << name; | 
|  | 213 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 214 | } | 
|  | 215 |  | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 216 | auto entry = mNameToService.emplace(name, Service { | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 217 | .binder = binder, | 
|  | 218 | .allowIsolated = allowIsolated, | 
|  | 219 | .dumpPriority = dumpPriority, | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 220 | .debugPid = ctx.debugPid, | 
|  | 221 | }); | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 222 |  | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 223 | auto it = mNameToRegistrationCallback.find(name); | 
|  | 224 | if (it != mNameToRegistrationCallback.end()) { | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 225 | for (const sp<IServiceCallback>& cb : it->second) { | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 226 | entry.first->second.guaranteeClient = true; | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 227 | // permission checked in registerForNotifications | 
|  | 228 | cb->onRegistration(name, binder); | 
|  | 229 | } | 
|  | 230 | } | 
|  | 231 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 232 | return Status::ok(); | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { | 
| Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 236 | if (!mAccess->canList(mAccess->getCallingContext())) { | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 237 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | size_t toReserve = 0; | 
|  | 241 | for (auto const& [name, service] : mNameToService) { | 
|  | 242 | (void) name; | 
|  | 243 |  | 
|  | 244 | if (service.dumpPriority & dumpPriority) ++toReserve; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | CHECK(outList->empty()); | 
|  | 248 |  | 
|  | 249 | outList->reserve(toReserve); | 
|  | 250 | for (auto const& [name, service] : mNameToService) { | 
|  | 251 | (void) service; | 
|  | 252 |  | 
|  | 253 | if (service.dumpPriority & dumpPriority) { | 
|  | 254 | outList->push_back(name); | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | return Status::ok(); | 
|  | 259 | } | 
|  | 260 |  | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 261 | Status ServiceManager::registerForNotifications( | 
|  | 262 | const std::string& name, const sp<IServiceCallback>& callback) { | 
|  | 263 | auto ctx = mAccess->getCallingContext(); | 
|  | 264 |  | 
|  | 265 | if (!mAccess->canFind(ctx, name)) { | 
|  | 266 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | if (!isValidServiceName(name)) { | 
|  | 270 | LOG(ERROR) << "Invalid service name: " << name; | 
|  | 271 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | if (callback == nullptr) { | 
|  | 275 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | if (OK != IInterface::asBinder(callback)->linkToDeath(this)) { | 
|  | 279 | LOG(ERROR) << "Could not linkToDeath when adding " << name; | 
|  | 280 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 281 | } | 
|  | 282 |  | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 283 | mNameToRegistrationCallback[name].push_back(callback); | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 284 |  | 
|  | 285 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { | 
|  | 286 | const sp<IBinder>& binder = it->second.binder; | 
|  | 287 |  | 
|  | 288 | // never null if an entry exists | 
|  | 289 | CHECK(binder != nullptr) << name; | 
|  | 290 | callback->onRegistration(name, binder); | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | return Status::ok(); | 
|  | 294 | } | 
|  | 295 | Status ServiceManager::unregisterForNotifications( | 
|  | 296 | const std::string& name, const sp<IServiceCallback>& callback) { | 
|  | 297 | auto ctx = mAccess->getCallingContext(); | 
|  | 298 |  | 
|  | 299 | if (!mAccess->canFind(ctx, name)) { | 
|  | 300 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | bool found = false; | 
|  | 304 |  | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 305 | auto it = mNameToRegistrationCallback.find(name); | 
|  | 306 | if (it != mNameToRegistrationCallback.end()) { | 
|  | 307 | removeRegistrationCallback(IInterface::asBinder(callback), &it, &found); | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
|  | 310 | if (!found) { | 
|  | 311 | LOG(ERROR) << "Trying to unregister callback, but none exists " << name; | 
|  | 312 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | return Status::ok(); | 
|  | 316 | } | 
|  | 317 |  | 
| Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 318 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { | 
|  | 319 | auto ctx = mAccess->getCallingContext(); | 
|  | 320 |  | 
|  | 321 | if (!mAccess->canFind(ctx, name)) { | 
|  | 322 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | *outReturn = false; | 
|  | 326 |  | 
|  | 327 | #ifndef VENDORSERVICEMANAGER | 
|  | 328 | *outReturn = isVintfDeclared(name); | 
|  | 329 | #endif | 
|  | 330 | return Status::ok(); | 
|  | 331 | } | 
|  | 332 |  | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 333 | void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who, | 
|  | 334 | ServiceCallbackMap::iterator* it, | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 335 | bool* found) { | 
|  | 336 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; | 
|  | 337 |  | 
|  | 338 | for (auto lit = listeners.begin(); lit != listeners.end();) { | 
|  | 339 | if (IInterface::asBinder(*lit) == who) { | 
|  | 340 | if(found) *found = true; | 
|  | 341 | lit = listeners.erase(lit); | 
|  | 342 | } else { | 
|  | 343 | ++lit; | 
|  | 344 | } | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | if (listeners.empty()) { | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 348 | *it = mNameToRegistrationCallback.erase(*it); | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 349 | } else { | 
| Jon Spivack | e223f08 | 2019-11-19 16:21:20 -0800 | [diff] [blame] | 350 | (*it)++; | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 351 | } | 
|  | 352 | } | 
|  | 353 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 354 | void ServiceManager::binderDied(const wp<IBinder>& who) { | 
|  | 355 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { | 
|  | 356 | if (who == it->second.binder) { | 
|  | 357 | it = mNameToService.erase(it); | 
|  | 358 | } else { | 
|  | 359 | ++it; | 
|  | 360 | } | 
|  | 361 | } | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 362 |  | 
| Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 363 | for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) { | 
|  | 364 | removeRegistrationCallback(who, &it, nullptr /*found*/); | 
| Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 365 | } | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 366 |  | 
|  | 367 | for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) { | 
|  | 368 | removeClientCallback(who, &it); | 
|  | 369 | } | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 372 | void ServiceManager::tryStartService(const std::string& name) { | 
|  | 373 | ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service", | 
|  | 374 | name.c_str()); | 
|  | 375 |  | 
|  | 376 | std::thread([=] { | 
|  | 377 | (void)base::SetProperty("ctl.interface_start", "aidl/" + name); | 
|  | 378 | }).detach(); | 
|  | 379 | } | 
|  | 380 |  | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 381 | Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service, | 
|  | 382 | const sp<IClientCallback>& cb) { | 
|  | 383 | if (cb == nullptr) { | 
|  | 384 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | auto ctx = mAccess->getCallingContext(); | 
|  | 388 | if (!mAccess->canAdd(ctx, name)) { | 
|  | 389 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | auto serviceIt = mNameToService.find(name); | 
|  | 393 | if (serviceIt == mNameToService.end()) { | 
|  | 394 | LOG(ERROR) << "Could not add callback for nonexistent service: " << name; | 
|  | 395 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) { | 
|  | 399 | LOG(WARNING) << "Only a server can register for client callbacks (for " << name << ")"; | 
|  | 400 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION); | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | if (serviceIt->second.binder != service) { | 
|  | 404 | LOG(WARNING) << "Tried to register client callback for " << name | 
|  | 405 | << " but a different service is registered under this name."; | 
|  | 406 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | if (OK != IInterface::asBinder(cb)->linkToDeath(this)) { | 
|  | 410 | LOG(ERROR) << "Could not linkToDeath when adding client callback for " << name; | 
|  | 411 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | mNameToClientCallback[name].push_back(cb); | 
|  | 415 |  | 
|  | 416 | return Status::ok(); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | void ServiceManager::removeClientCallback(const wp<IBinder>& who, | 
|  | 420 | ClientCallbackMap::iterator* it) { | 
|  | 421 | std::vector<sp<IClientCallback>>& listeners = (*it)->second; | 
|  | 422 |  | 
|  | 423 | for (auto lit = listeners.begin(); lit != listeners.end();) { | 
|  | 424 | if (IInterface::asBinder(*lit) == who) { | 
|  | 425 | lit = listeners.erase(lit); | 
|  | 426 | } else { | 
|  | 427 | ++lit; | 
|  | 428 | } | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | if (listeners.empty()) { | 
|  | 432 | *it = mNameToClientCallback.erase(*it); | 
|  | 433 | } else { | 
|  | 434 | (*it)++; | 
|  | 435 | } | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | ssize_t ServiceManager::Service::getNodeStrongRefCount() { | 
|  | 439 | sp<BpBinder> bpBinder = binder->remoteBinder(); | 
|  | 440 | if (bpBinder == nullptr) return -1; | 
|  | 441 |  | 
|  | 442 | return ProcessState::self()->getStrongRefCountForNodeByHandle(bpBinder->handle()); | 
|  | 443 | } | 
|  | 444 |  | 
|  | 445 | void ServiceManager::handleClientCallbacks() { | 
|  | 446 | for (const auto& [name, service] : mNameToService) { | 
| Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 447 | handleServiceClientCallback(name, true); | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 448 | } | 
|  | 449 | } | 
|  | 450 |  | 
| Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 451 | ssize_t ServiceManager::handleServiceClientCallback(const std::string& serviceName, | 
|  | 452 | bool isCalledOnInterval) { | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 453 | auto serviceIt = mNameToService.find(serviceName); | 
|  | 454 | if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) { | 
|  | 455 | return -1; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | Service& service = serviceIt->second; | 
|  | 459 | ssize_t count = service.getNodeStrongRefCount(); | 
|  | 460 |  | 
|  | 461 | // binder driver doesn't support this feature | 
|  | 462 | if (count == -1) return count; | 
|  | 463 |  | 
|  | 464 | bool hasClients = count > 1; // this process holds a strong count | 
|  | 465 |  | 
|  | 466 | if (service.guaranteeClient) { | 
|  | 467 | // we have no record of this client | 
|  | 468 | if (!service.hasClients && !hasClients) { | 
|  | 469 | sendClientCallbackNotifications(serviceName, true); | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | // guarantee is temporary | 
|  | 473 | service.guaranteeClient = false; | 
|  | 474 | } | 
|  | 475 |  | 
| Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 476 | // only send notifications if this was called via the interval checking workflow | 
|  | 477 | if (isCalledOnInterval) { | 
|  | 478 | if (hasClients && !service.hasClients) { | 
|  | 479 | // client was retrieved in some other way | 
|  | 480 | sendClientCallbackNotifications(serviceName, true); | 
|  | 481 | } | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 482 |  | 
| Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 483 | // there are no more clients, but the callback has not been called yet | 
|  | 484 | if (!hasClients && service.hasClients) { | 
|  | 485 | sendClientCallbackNotifications(serviceName, false); | 
|  | 486 | } | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 487 | } | 
|  | 488 |  | 
|  | 489 | return count; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName, bool hasClients) { | 
|  | 493 | auto serviceIt = mNameToService.find(serviceName); | 
|  | 494 | if (serviceIt == mNameToService.end()) { | 
|  | 495 | LOG(WARNING) << "sendClientCallbackNotifications could not find service " << serviceName; | 
|  | 496 | return; | 
|  | 497 | } | 
|  | 498 | Service& service = serviceIt->second; | 
|  | 499 |  | 
|  | 500 | CHECK(hasClients != service.hasClients) << "Record shows: " << service.hasClients | 
|  | 501 | << " so we can't tell clients again that we have client: " << hasClients; | 
|  | 502 |  | 
|  | 503 | LOG(INFO) << "Notifying " << serviceName << " they have clients: " << hasClients; | 
|  | 504 |  | 
|  | 505 | auto ccIt = mNameToClientCallback.find(serviceName); | 
|  | 506 | CHECK(ccIt != mNameToClientCallback.end()) | 
|  | 507 | << "sendClientCallbackNotifications could not find callbacks for service "; | 
|  | 508 |  | 
|  | 509 | for (const auto& callback : ccIt->second) { | 
|  | 510 | callback->onClients(service.binder, hasClients); | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | service.hasClients = hasClients; | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) { | 
|  | 517 | if (binder == nullptr) { | 
|  | 518 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); | 
|  | 519 | } | 
|  | 520 |  | 
|  | 521 | auto ctx = mAccess->getCallingContext(); | 
|  | 522 | if (!mAccess->canAdd(ctx, name)) { | 
|  | 523 | return Status::fromExceptionCode(Status::EX_SECURITY); | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | auto serviceIt = mNameToService.find(name); | 
|  | 527 | if (serviceIt == mNameToService.end()) { | 
|  | 528 | LOG(WARNING) << "Tried to unregister " << name | 
|  | 529 | << ", but that service wasn't registered to begin with."; | 
|  | 530 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | if (serviceIt->second.debugPid != IPCThreadState::self()->getCallingPid()) { | 
|  | 534 | LOG(WARNING) << "Only a server can unregister itself (for " << name << ")"; | 
|  | 535 | return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | sp<IBinder> storedBinder = serviceIt->second.binder; | 
|  | 539 |  | 
|  | 540 | if (binder != storedBinder) { | 
|  | 541 | LOG(WARNING) << "Tried to unregister " << name | 
|  | 542 | << ", but a different service is registered under this name."; | 
|  | 543 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 544 | } | 
|  | 545 |  | 
| Jon Spivack | bb108a1 | 2020-03-13 20:45:18 -0700 | [diff] [blame] | 546 | if (serviceIt->second.guaranteeClient) { | 
|  | 547 | LOG(INFO) << "Tried to unregister " << name << ", but there is about to be a client."; | 
|  | 548 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 549 | } | 
|  | 550 |  | 
| Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 551 | int clients = handleServiceClientCallback(name, false); | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 552 |  | 
|  | 553 | // clients < 0: feature not implemented or other error. Assume clients. | 
|  | 554 | // Otherwise: | 
|  | 555 | // - kernel driver will hold onto one refcount (during this transaction) | 
|  | 556 | // - servicemanager has a refcount (guaranteed by this transaction) | 
|  | 557 | // So, if clients > 2, then at least one other service on the system must hold a refcount. | 
|  | 558 | if (clients < 0 || clients > 2) { | 
|  | 559 | // client callbacks are either disabled or there are other clients | 
| Jon Spivack | d9533c2 | 2020-01-27 22:19:22 +0000 | [diff] [blame] | 560 | LOG(INFO) << "Tried to unregister " << name << ", but there are clients: " << clients; | 
| Jon Spivack | e17055a | 2020-03-06 13:58:01 -0800 | [diff] [blame] | 561 | // Set this flag to ensure the clients are acknowledged in the next callback | 
|  | 562 | serviceIt->second.guaranteeClient = true; | 
| Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 563 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | mNameToService.erase(name); | 
|  | 567 |  | 
|  | 568 | return Status::ok(); | 
|  | 569 | } | 
|  | 570 |  | 
| Steven Moreland | d221c25 | 2020-04-30 16:51:56 -0700 | [diff] [blame] | 571 | }  // namespace android |