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