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> |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 21 | #include <binder/Stability.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 22 | #include <cutils/android_filesystem_config.h> |
| 23 | #include <cutils/multiuser.h> |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 24 | #include <thread> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 25 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 26 | #ifndef VENDORSERVICEMANAGER |
| 27 | #include <vintf/VintfObject.h> |
| 28 | #include <vintf/constants.h> |
| 29 | #endif // !VENDORSERVICEMANAGER |
| 30 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 31 | using ::android::binder::Status; |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 32 | using ::android::internal::Stability; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 36 | #ifndef VENDORSERVICEMANAGER |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 37 | static bool isVintfDeclared(const std::string& name) { |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 38 | size_t firstSlash = name.find('/'); |
| 39 | size_t lastDot = name.rfind('.', firstSlash); |
| 40 | if (firstSlash == std::string::npos || lastDot == std::string::npos) { |
| 41 | LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. " |
| 42 | << "some.package.foo.IFoo/default) but got: " << name; |
| 43 | return false; |
| 44 | } |
| 45 | const std::string package = name.substr(0, lastDot); |
| 46 | const std::string iface = name.substr(lastDot+1, firstSlash-lastDot-1); |
| 47 | const std::string instance = name.substr(firstSlash+1); |
| 48 | |
| 49 | for (const auto& manifest : { |
| 50 | vintf::VintfObject::GetDeviceHalManifest(), |
| 51 | vintf::VintfObject::GetFrameworkHalManifest() |
| 52 | }) { |
Steven Moreland | 97d1d45 | 2019-11-01 10:08:34 -0700 | [diff] [blame] | 53 | if (manifest != nullptr && manifest->hasAidlInstance(package, iface, instance)) { |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance |
| 58 | << " in the VINTF manifest."; |
| 59 | return false; |
| 60 | } |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 61 | |
| 62 | static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) { |
| 63 | if (!Stability::requiresVintfDeclaration(binder)) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | return isVintfDeclared(name); |
| 68 | } |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 69 | #endif // !VENDORSERVICEMANAGER |
| 70 | |
Steven Moreland | d13f08b | 2019-11-18 14:23:09 -0800 | [diff] [blame^] | 71 | ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) { |
| 72 | #ifndef VENDORSERVICEMANAGER |
| 73 | // can process these at any times, don't want to delay first VINTF client |
| 74 | std::thread([] { |
| 75 | vintf::VintfObject::GetDeviceHalManifest(); |
| 76 | vintf::VintfObject::GetFrameworkHalManifest(); |
| 77 | }).detach(); |
| 78 | #endif // !VENDORSERVICEMANAGER |
| 79 | } |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 80 | ServiceManager::~ServiceManager() { |
| 81 | // this should only happen in tests |
| 82 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 83 | for (const auto& [name, callbacks] : mNameToCallback) { |
| 84 | CHECK(!callbacks.empty()) << name; |
| 85 | for (const auto& callback : callbacks) { |
| 86 | CHECK(callback != nullptr) << name; |
| 87 | } |
| 88 | } |
| 89 | |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 90 | for (const auto& [name, service] : mNameToService) { |
| 91 | CHECK(service.binder != nullptr) << name; |
| 92 | } |
| 93 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 94 | |
| 95 | Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 96 | *outBinder = tryGetService(name, true); |
| 97 | // returns ok regardless of result for legacy reasons |
| 98 | return Status::ok(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 102 | *outBinder = tryGetService(name, false); |
| 103 | // returns ok regardless of result for legacy reasons |
| 104 | return Status::ok(); |
| 105 | } |
| 106 | |
| 107 | sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 108 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 109 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 110 | sp<IBinder> out; |
| 111 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 112 | const Service& service = it->second; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 113 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 114 | if (!service.allowIsolated) { |
| 115 | uid_t appid = multiuser_get_app_id(ctx.uid); |
| 116 | bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 117 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 118 | if (isIsolated) { |
| 119 | return nullptr; |
| 120 | } |
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 | out = service.binder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 125 | if (!mAccess->canFind(ctx, name)) { |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 126 | return nullptr; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 129 | if (!out && startIfNotFound) { |
| 130 | tryStartService(name); |
| 131 | } |
| 132 | |
| 133 | return out; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 136 | bool isValidServiceName(const std::string& name) { |
| 137 | if (name.size() == 0) return false; |
| 138 | if (name.size() > 127) return false; |
| 139 | |
| 140 | for (char c : name) { |
Steven Moreland | bb7951d | 2019-08-20 16:58:25 -0700 | [diff] [blame] | 141 | if (c == '_' || c == '-' || c == '.' || c == '/') continue; |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 142 | if (c >= 'a' && c <= 'z') continue; |
| 143 | if (c >= 'A' && c <= 'Z') continue; |
| 144 | if (c >= '0' && c <= '9') continue; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 151 | 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] | 152 | auto ctx = mAccess->getCallingContext(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 153 | |
| 154 | // apps cannot add services |
| 155 | if (multiuser_get_app_id(ctx.uid) >= AID_APP) { |
| 156 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 157 | } |
| 158 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 159 | if (!mAccess->canAdd(ctx, name)) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 160 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 161 | } |
| 162 | |
| 163 | if (binder == nullptr) { |
| 164 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 165 | } |
| 166 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 167 | if (!isValidServiceName(name)) { |
| 168 | LOG(ERROR) << "Invalid service name: " << name; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 169 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 170 | } |
| 171 | |
Steven Moreland | 86a17f8 | 2019-09-10 10:18:00 -0700 | [diff] [blame] | 172 | #ifndef VENDORSERVICEMANAGER |
| 173 | if (!meetsDeclarationRequirements(binder, name)) { |
| 174 | // already logged |
| 175 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 176 | } |
| 177 | #endif // !VENDORSERVICEMANAGER |
| 178 | |
Steven Moreland | 88860b0 | 2019-08-12 14:24:14 -0700 | [diff] [blame] | 179 | // implicitly unlinked when the binder is removed |
Steven Moreland | 46f380b | 2019-10-16 16:28:21 -0700 | [diff] [blame] | 180 | if (binder->remoteBinder() != nullptr && binder->linkToDeath(this) != OK) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 181 | LOG(ERROR) << "Could not linkToDeath when adding " << name; |
| 182 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 183 | } |
| 184 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 185 | mNameToService[name] = Service { |
| 186 | .binder = binder, |
| 187 | .allowIsolated = allowIsolated, |
| 188 | .dumpPriority = dumpPriority, |
| 189 | }; |
| 190 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 191 | auto it = mNameToCallback.find(name); |
| 192 | if (it != mNameToCallback.end()) { |
| 193 | for (const sp<IServiceCallback>& cb : it->second) { |
| 194 | // permission checked in registerForNotifications |
| 195 | cb->onRegistration(name, binder); |
| 196 | } |
| 197 | } |
| 198 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 199 | return Status::ok(); |
| 200 | } |
| 201 | |
| 202 | Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) { |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 203 | if (!mAccess->canList(mAccess->getCallingContext())) { |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 204 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 205 | } |
| 206 | |
| 207 | size_t toReserve = 0; |
| 208 | for (auto const& [name, service] : mNameToService) { |
| 209 | (void) name; |
| 210 | |
| 211 | if (service.dumpPriority & dumpPriority) ++toReserve; |
| 212 | } |
| 213 | |
| 214 | CHECK(outList->empty()); |
| 215 | |
| 216 | outList->reserve(toReserve); |
| 217 | for (auto const& [name, service] : mNameToService) { |
| 218 | (void) service; |
| 219 | |
| 220 | if (service.dumpPriority & dumpPriority) { |
| 221 | outList->push_back(name); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return Status::ok(); |
| 226 | } |
| 227 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 228 | Status ServiceManager::registerForNotifications( |
| 229 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 230 | auto ctx = mAccess->getCallingContext(); |
| 231 | |
| 232 | if (!mAccess->canFind(ctx, name)) { |
| 233 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 234 | } |
| 235 | |
| 236 | if (!isValidServiceName(name)) { |
| 237 | LOG(ERROR) << "Invalid service name: " << name; |
| 238 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 239 | } |
| 240 | |
| 241 | if (callback == nullptr) { |
| 242 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 243 | } |
| 244 | |
| 245 | if (OK != IInterface::asBinder(callback)->linkToDeath(this)) { |
| 246 | LOG(ERROR) << "Could not linkToDeath when adding " << name; |
| 247 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 248 | } |
| 249 | |
| 250 | mNameToCallback[name].push_back(callback); |
| 251 | |
| 252 | if (auto it = mNameToService.find(name); it != mNameToService.end()) { |
| 253 | const sp<IBinder>& binder = it->second.binder; |
| 254 | |
| 255 | // never null if an entry exists |
| 256 | CHECK(binder != nullptr) << name; |
| 257 | callback->onRegistration(name, binder); |
| 258 | } |
| 259 | |
| 260 | return Status::ok(); |
| 261 | } |
| 262 | Status ServiceManager::unregisterForNotifications( |
| 263 | const std::string& name, const sp<IServiceCallback>& callback) { |
| 264 | auto ctx = mAccess->getCallingContext(); |
| 265 | |
| 266 | if (!mAccess->canFind(ctx, name)) { |
| 267 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 268 | } |
| 269 | |
| 270 | bool found = false; |
| 271 | |
| 272 | auto it = mNameToCallback.find(name); |
| 273 | if (it != mNameToCallback.end()) { |
| 274 | removeCallback(IInterface::asBinder(callback), &it, &found); |
| 275 | } |
| 276 | |
| 277 | if (!found) { |
| 278 | LOG(ERROR) << "Trying to unregister callback, but none exists " << name; |
| 279 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 280 | } |
| 281 | |
| 282 | return Status::ok(); |
| 283 | } |
| 284 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 285 | Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) { |
| 286 | auto ctx = mAccess->getCallingContext(); |
| 287 | |
| 288 | if (!mAccess->canFind(ctx, name)) { |
| 289 | return Status::fromExceptionCode(Status::EX_SECURITY); |
| 290 | } |
| 291 | |
| 292 | *outReturn = false; |
| 293 | |
| 294 | #ifndef VENDORSERVICEMANAGER |
| 295 | *outReturn = isVintfDeclared(name); |
| 296 | #endif |
| 297 | return Status::ok(); |
| 298 | } |
| 299 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 300 | void ServiceManager::removeCallback(const wp<IBinder>& who, |
| 301 | CallbackMap::iterator* it, |
| 302 | bool* found) { |
| 303 | std::vector<sp<IServiceCallback>>& listeners = (*it)->second; |
| 304 | |
| 305 | for (auto lit = listeners.begin(); lit != listeners.end();) { |
| 306 | if (IInterface::asBinder(*lit) == who) { |
| 307 | if(found) *found = true; |
| 308 | lit = listeners.erase(lit); |
| 309 | } else { |
| 310 | ++lit; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (listeners.empty()) { |
| 315 | *it = mNameToCallback.erase(*it); |
| 316 | } else { |
| 317 | it++; |
| 318 | } |
| 319 | } |
| 320 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 321 | void ServiceManager::binderDied(const wp<IBinder>& who) { |
| 322 | for (auto it = mNameToService.begin(); it != mNameToService.end();) { |
| 323 | if (who == it->second.binder) { |
| 324 | it = mNameToService.erase(it); |
| 325 | } else { |
| 326 | ++it; |
| 327 | } |
| 328 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 329 | |
| 330 | for (auto it = mNameToCallback.begin(); it != mNameToCallback.end();) { |
| 331 | removeCallback(who, &it, nullptr /*found*/); |
| 332 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 335 | void ServiceManager::tryStartService(const std::string& name) { |
| 336 | ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service", |
| 337 | name.c_str()); |
| 338 | |
| 339 | std::thread([=] { |
| 340 | (void)base::SetProperty("ctl.interface_start", "aidl/" + name); |
| 341 | }).detach(); |
| 342 | } |
| 343 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 344 | } // namespace android |