blob: 2d2af3c716c8f6c67727dd18b066f1c10509397e [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 }) {
Steven Moreland97d1d452019-11-01 10:08:34 -070053 if (manifest != nullptr && manifest->hasAidlInstance(package, iface, instance)) {
Steven Moreland86a17f82019-09-10 10:18:00 -070054 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 Morelandd13f08b2019-11-18 14:23:09 -080071ServiceManager::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 Moreland130242d2019-08-26 17:41:32 -070080ServiceManager::~ServiceManager() {
81 // this should only happen in tests
82
Steven Moreland27cfab02019-08-12 14:34:16 -070083 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 Moreland130242d2019-08-26 17:41:32 -070090 for (const auto& [name, service] : mNameToService) {
91 CHECK(service.binder != nullptr) << name;
92 }
93}
Steven Moreland80e1e6d2019-06-21 12:35:59 -070094
95Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -070096 *outBinder = tryGetService(name, true);
97 // returns ok regardless of result for legacy reasons
98 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070099}
100
101Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -0700102 *outBinder = tryGetService(name, false);
103 // returns ok regardless of result for legacy reasons
104 return Status::ok();
105}
106
107sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700108 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700109
Jon Spivack0d844302019-07-22 18:40:34 -0700110 sp<IBinder> out;
111 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
112 const Service& service = it->second;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700113
Jon Spivack0d844302019-07-22 18:40:34 -0700114 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 Moreland80e1e6d2019-06-21 12:35:59 -0700117
Jon Spivack0d844302019-07-22 18:40:34 -0700118 if (isIsolated) {
119 return nullptr;
120 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700121 }
Jon Spivack0d844302019-07-22 18:40:34 -0700122 out = service.binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700123 }
124
Steven Morelanda9fe4742019-07-18 14:45:20 -0700125 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700126 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700127 }
128
Jon Spivack0d844302019-07-22 18:40:34 -0700129 if (!out && startIfNotFound) {
130 tryStartService(name);
131 }
132
133 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700134}
135
Steven Moreland905e2e82019-07-17 11:05:45 -0700136bool 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 Morelandbb7951d2019-08-20 16:58:25 -0700141 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700142 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 Moreland80e1e6d2019-06-21 12:35:59 -0700151Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700152 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700153
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 Morelanda9fe4742019-07-18 14:45:20 -0700159 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700160 return Status::fromExceptionCode(Status::EX_SECURITY);
161 }
162
163 if (binder == nullptr) {
164 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
165 }
166
Steven Moreland905e2e82019-07-17 11:05:45 -0700167 if (!isValidServiceName(name)) {
168 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700169 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
170 }
171
Steven Moreland86a17f82019-09-10 10:18:00 -0700172#ifndef VENDORSERVICEMANAGER
173 if (!meetsDeclarationRequirements(binder, name)) {
174 // already logged
175 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
176 }
177#endif // !VENDORSERVICEMANAGER
178
Steven Moreland88860b02019-08-12 14:24:14 -0700179 // implicitly unlinked when the binder is removed
Steven Moreland46f380b2019-10-16 16:28:21 -0700180 if (binder->remoteBinder() != nullptr && binder->linkToDeath(this) != OK) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700181 LOG(ERROR) << "Could not linkToDeath when adding " << name;
182 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
183 }
184
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700185 mNameToService[name] = Service {
186 .binder = binder,
187 .allowIsolated = allowIsolated,
188 .dumpPriority = dumpPriority,
189 };
190
Steven Moreland27cfab02019-08-12 14:34:16 -0700191 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 Moreland80e1e6d2019-06-21 12:35:59 -0700199 return Status::ok();
200}
201
202Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700203 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700204 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 Moreland27cfab02019-08-12 14:34:16 -0700228Status 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}
262Status 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 Morelandb82b8f82019-10-28 10:52:34 -0700285Status 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 Moreland27cfab02019-08-12 14:34:16 -0700300void 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 Moreland80e1e6d2019-06-21 12:35:59 -0700321void 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 Moreland27cfab02019-08-12 14:34:16 -0700329
330 for (auto it = mNameToCallback.begin(); it != mNameToCallback.end();) {
331 removeCallback(who, &it, nullptr /*found*/);
332 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700333}
334
Jon Spivack0d844302019-07-22 18:40:34 -0700335void 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 Moreland80e1e6d2019-06-21 12:35:59 -0700344} // namespace android