blob: 91e89ae7e80cf0afd515f1d2267227dccd711c40 [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001/*
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 Spivack0d844302019-07-22 18:40:34 -070020#include <android-base/properties.h>
Steven Moreland86a17f82019-09-10 10:18:00 -070021#include <binder/Stability.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070022#include <cutils/android_filesystem_config.h>
23#include <cutils/multiuser.h>
Jon Spivack0d844302019-07-22 18:40:34 -070024#include <thread>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070025
Steven Moreland86a17f82019-09-10 10:18:00 -070026#ifndef VENDORSERVICEMANAGER
27#include <vintf/VintfObject.h>
28#include <vintf/constants.h>
29#endif // !VENDORSERVICEMANAGER
30
Steven Moreland80e1e6d2019-06-21 12:35:59 -070031using ::android::binder::Status;
Steven Moreland86a17f82019-09-10 10:18:00 -070032using ::android::internal::Stability;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070033
34namespace android {
35
Steven Moreland86a17f82019-09-10 10:18:00 -070036#ifndef VENDORSERVICEMANAGER
Steven Morelandb82b8f82019-10-28 10:52:34 -070037static bool isVintfDeclared(const std::string& name) {
Steven Moreland86a17f82019-09-10 10:18:00 -070038 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 }) {
53 if (manifest->hasAidlInstance(package, iface, instance)) {
54 return true;
55 }
56 }
57 LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance
58 << " in the VINTF manifest.";
59 return false;
60}
Steven Morelandb82b8f82019-10-28 10:52:34 -070061
62static 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 Moreland86a17f82019-09-10 10:18:00 -070069#endif // !VENDORSERVICEMANAGER
70
Steven Moreland80e1e6d2019-06-21 12:35:59 -070071ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {}
Steven Moreland130242d2019-08-26 17:41:32 -070072ServiceManager::~ServiceManager() {
73 // this should only happen in tests
74
Steven Moreland27cfab02019-08-12 14:34:16 -070075 for (const auto& [name, callbacks] : mNameToCallback) {
76 CHECK(!callbacks.empty()) << name;
77 for (const auto& callback : callbacks) {
78 CHECK(callback != nullptr) << name;
79 }
80 }
81
Steven Moreland130242d2019-08-26 17:41:32 -070082 for (const auto& [name, service] : mNameToService) {
83 CHECK(service.binder != nullptr) << name;
84 }
85}
Steven Moreland80e1e6d2019-06-21 12:35:59 -070086
87Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -070088 *outBinder = tryGetService(name, true);
89 // returns ok regardless of result for legacy reasons
90 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070091}
92
93Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -070094 *outBinder = tryGetService(name, false);
95 // returns ok regardless of result for legacy reasons
96 return Status::ok();
97}
98
99sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700100 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700101
Jon Spivack0d844302019-07-22 18:40:34 -0700102 sp<IBinder> out;
103 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
104 const Service& service = it->second;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700105
Jon Spivack0d844302019-07-22 18:40:34 -0700106 if (!service.allowIsolated) {
107 uid_t appid = multiuser_get_app_id(ctx.uid);
108 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700109
Jon Spivack0d844302019-07-22 18:40:34 -0700110 if (isIsolated) {
111 return nullptr;
112 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700113 }
Jon Spivack0d844302019-07-22 18:40:34 -0700114 out = service.binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700115 }
116
Steven Morelanda9fe4742019-07-18 14:45:20 -0700117 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700118 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700119 }
120
Jon Spivack0d844302019-07-22 18:40:34 -0700121 if (!out && startIfNotFound) {
122 tryStartService(name);
123 }
124
125 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700126}
127
Steven Moreland905e2e82019-07-17 11:05:45 -0700128bool isValidServiceName(const std::string& name) {
129 if (name.size() == 0) return false;
130 if (name.size() > 127) return false;
131
132 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700133 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700134 if (c >= 'a' && c <= 'z') continue;
135 if (c >= 'A' && c <= 'Z') continue;
136 if (c >= '0' && c <= '9') continue;
137 return false;
138 }
139
140 return true;
141}
142
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700143Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700144 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700145
146 // apps cannot add services
147 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
148 return Status::fromExceptionCode(Status::EX_SECURITY);
149 }
150
Steven Morelanda9fe4742019-07-18 14:45:20 -0700151 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700152 return Status::fromExceptionCode(Status::EX_SECURITY);
153 }
154
155 if (binder == nullptr) {
156 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
157 }
158
Steven Moreland905e2e82019-07-17 11:05:45 -0700159 if (!isValidServiceName(name)) {
160 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700161 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
162 }
163
Steven Moreland86a17f82019-09-10 10:18:00 -0700164#ifndef VENDORSERVICEMANAGER
165 if (!meetsDeclarationRequirements(binder, name)) {
166 // already logged
167 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
168 }
169#endif // !VENDORSERVICEMANAGER
170
Steven Moreland88860b02019-08-12 14:24:14 -0700171 // implicitly unlinked when the binder is removed
Steven Moreland46f380b2019-10-16 16:28:21 -0700172 if (binder->remoteBinder() != nullptr && binder->linkToDeath(this) != OK) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700173 LOG(ERROR) << "Could not linkToDeath when adding " << name;
174 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
175 }
176
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700177 mNameToService[name] = Service {
178 .binder = binder,
179 .allowIsolated = allowIsolated,
180 .dumpPriority = dumpPriority,
181 };
182
Steven Moreland27cfab02019-08-12 14:34:16 -0700183 auto it = mNameToCallback.find(name);
184 if (it != mNameToCallback.end()) {
185 for (const sp<IServiceCallback>& cb : it->second) {
186 // permission checked in registerForNotifications
187 cb->onRegistration(name, binder);
188 }
189 }
190
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700191 return Status::ok();
192}
193
194Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700195 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700196 return Status::fromExceptionCode(Status::EX_SECURITY);
197 }
198
199 size_t toReserve = 0;
200 for (auto const& [name, service] : mNameToService) {
201 (void) name;
202
203 if (service.dumpPriority & dumpPriority) ++toReserve;
204 }
205
206 CHECK(outList->empty());
207
208 outList->reserve(toReserve);
209 for (auto const& [name, service] : mNameToService) {
210 (void) service;
211
212 if (service.dumpPriority & dumpPriority) {
213 outList->push_back(name);
214 }
215 }
216
217 return Status::ok();
218}
219
Steven Moreland27cfab02019-08-12 14:34:16 -0700220Status ServiceManager::registerForNotifications(
221 const std::string& name, const sp<IServiceCallback>& callback) {
222 auto ctx = mAccess->getCallingContext();
223
224 if (!mAccess->canFind(ctx, name)) {
225 return Status::fromExceptionCode(Status::EX_SECURITY);
226 }
227
228 if (!isValidServiceName(name)) {
229 LOG(ERROR) << "Invalid service name: " << name;
230 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
231 }
232
233 if (callback == nullptr) {
234 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
235 }
236
237 if (OK != IInterface::asBinder(callback)->linkToDeath(this)) {
238 LOG(ERROR) << "Could not linkToDeath when adding " << name;
239 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
240 }
241
242 mNameToCallback[name].push_back(callback);
243
244 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
245 const sp<IBinder>& binder = it->second.binder;
246
247 // never null if an entry exists
248 CHECK(binder != nullptr) << name;
249 callback->onRegistration(name, binder);
250 }
251
252 return Status::ok();
253}
254Status ServiceManager::unregisterForNotifications(
255 const std::string& name, const sp<IServiceCallback>& callback) {
256 auto ctx = mAccess->getCallingContext();
257
258 if (!mAccess->canFind(ctx, name)) {
259 return Status::fromExceptionCode(Status::EX_SECURITY);
260 }
261
262 bool found = false;
263
264 auto it = mNameToCallback.find(name);
265 if (it != mNameToCallback.end()) {
266 removeCallback(IInterface::asBinder(callback), &it, &found);
267 }
268
269 if (!found) {
270 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
271 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
272 }
273
274 return Status::ok();
275}
276
Steven Morelandb82b8f82019-10-28 10:52:34 -0700277Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
278 auto ctx = mAccess->getCallingContext();
279
280 if (!mAccess->canFind(ctx, name)) {
281 return Status::fromExceptionCode(Status::EX_SECURITY);
282 }
283
284 *outReturn = false;
285
286#ifndef VENDORSERVICEMANAGER
287 *outReturn = isVintfDeclared(name);
288#endif
289 return Status::ok();
290}
291
Steven Moreland27cfab02019-08-12 14:34:16 -0700292void ServiceManager::removeCallback(const wp<IBinder>& who,
293 CallbackMap::iterator* it,
294 bool* found) {
295 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
296
297 for (auto lit = listeners.begin(); lit != listeners.end();) {
298 if (IInterface::asBinder(*lit) == who) {
299 if(found) *found = true;
300 lit = listeners.erase(lit);
301 } else {
302 ++lit;
303 }
304 }
305
306 if (listeners.empty()) {
307 *it = mNameToCallback.erase(*it);
308 } else {
309 it++;
310 }
311}
312
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700313void ServiceManager::binderDied(const wp<IBinder>& who) {
314 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
315 if (who == it->second.binder) {
316 it = mNameToService.erase(it);
317 } else {
318 ++it;
319 }
320 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700321
322 for (auto it = mNameToCallback.begin(); it != mNameToCallback.end();) {
323 removeCallback(who, &it, nullptr /*found*/);
324 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700325}
326
Jon Spivack0d844302019-07-22 18:40:34 -0700327void ServiceManager::tryStartService(const std::string& name) {
328 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service",
329 name.c_str());
330
331 std::thread([=] {
332 (void)base::SetProperty("ctl.interface_start", "aidl/" + name);
333 }).detach();
334}
335
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700336} // namespace android