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