blob: 934436847e5cf304649fa3df8e698761f8b76909 [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
37static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) {
38 if (!Stability::requiresVintfDeclaration(binder)) {
39 return true;
40 }
41
42 size_t firstSlash = name.find('/');
43 size_t lastDot = name.rfind('.', firstSlash);
44 if (firstSlash == std::string::npos || lastDot == std::string::npos) {
45 LOG(ERROR) << "VINTF HALs require names in the format type/instance (e.g. "
46 << "some.package.foo.IFoo/default) but got: " << name;
47 return false;
48 }
49 const std::string package = name.substr(0, lastDot);
50 const std::string iface = name.substr(lastDot+1, firstSlash-lastDot-1);
51 const std::string instance = name.substr(firstSlash+1);
52
53 for (const auto& manifest : {
54 vintf::VintfObject::GetDeviceHalManifest(),
55 vintf::VintfObject::GetFrameworkHalManifest()
56 }) {
57 if (manifest->hasAidlInstance(package, iface, instance)) {
58 return true;
59 }
60 }
61 LOG(ERROR) << "Could not find " << package << "." << iface << "/" << instance
62 << " in the VINTF manifest.";
63 return false;
64}
65#endif // !VENDORSERVICEMANAGER
66
Steven Moreland80e1e6d2019-06-21 12:35:59 -070067ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {}
Steven Moreland130242d2019-08-26 17:41:32 -070068ServiceManager::~ServiceManager() {
69 // this should only happen in tests
70
Steven Moreland27cfab02019-08-12 14:34:16 -070071 for (const auto& [name, callbacks] : mNameToCallback) {
72 CHECK(!callbacks.empty()) << name;
73 for (const auto& callback : callbacks) {
74 CHECK(callback != nullptr) << name;
75 }
76 }
77
Steven Moreland130242d2019-08-26 17:41:32 -070078 for (const auto& [name, service] : mNameToService) {
79 CHECK(service.binder != nullptr) << name;
80 }
81}
Steven Moreland80e1e6d2019-06-21 12:35:59 -070082
83Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -070084 *outBinder = tryGetService(name, true);
85 // returns ok regardless of result for legacy reasons
86 return Status::ok();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070087}
88
89Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Jon Spivack0d844302019-07-22 18:40:34 -070090 *outBinder = tryGetService(name, false);
91 // returns ok regardless of result for legacy reasons
92 return Status::ok();
93}
94
95sp<IBinder> ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
Steven Morelanda9fe4742019-07-18 14:45:20 -070096 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070097
Jon Spivack0d844302019-07-22 18:40:34 -070098 sp<IBinder> out;
99 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
100 const Service& service = it->second;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700101
Jon Spivack0d844302019-07-22 18:40:34 -0700102 if (!service.allowIsolated) {
103 uid_t appid = multiuser_get_app_id(ctx.uid);
104 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700105
Jon Spivack0d844302019-07-22 18:40:34 -0700106 if (isIsolated) {
107 return nullptr;
108 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700109 }
Jon Spivack0d844302019-07-22 18:40:34 -0700110 out = service.binder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700111 }
112
Steven Morelanda9fe4742019-07-18 14:45:20 -0700113 if (!mAccess->canFind(ctx, name)) {
Jon Spivack0d844302019-07-22 18:40:34 -0700114 return nullptr;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700115 }
116
Jon Spivack0d844302019-07-22 18:40:34 -0700117 if (!out && startIfNotFound) {
118 tryStartService(name);
119 }
120
121 return out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700122}
123
Steven Moreland905e2e82019-07-17 11:05:45 -0700124bool isValidServiceName(const std::string& name) {
125 if (name.size() == 0) return false;
126 if (name.size() > 127) return false;
127
128 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -0700129 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -0700130 if (c >= 'a' && c <= 'z') continue;
131 if (c >= 'A' && c <= 'Z') continue;
132 if (c >= '0' && c <= '9') continue;
133 return false;
134 }
135
136 return true;
137}
138
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700139Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700140 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700141
142 // apps cannot add services
143 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
144 return Status::fromExceptionCode(Status::EX_SECURITY);
145 }
146
Steven Morelanda9fe4742019-07-18 14:45:20 -0700147 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700148 return Status::fromExceptionCode(Status::EX_SECURITY);
149 }
150
151 if (binder == nullptr) {
152 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
153 }
154
Steven Moreland905e2e82019-07-17 11:05:45 -0700155 if (!isValidServiceName(name)) {
156 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700157 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
158 }
159
Steven Moreland86a17f82019-09-10 10:18:00 -0700160#ifndef VENDORSERVICEMANAGER
161 if (!meetsDeclarationRequirements(binder, name)) {
162 // already logged
163 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
164 }
165#endif // !VENDORSERVICEMANAGER
166
Steven Moreland88860b02019-08-12 14:24:14 -0700167 // implicitly unlinked when the binder is removed
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700168 if (OK != binder->linkToDeath(this)) {
169 LOG(ERROR) << "Could not linkToDeath when adding " << name;
170 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
171 }
172
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700173 mNameToService[name] = Service {
174 .binder = binder,
175 .allowIsolated = allowIsolated,
176 .dumpPriority = dumpPriority,
177 };
178
Steven Moreland27cfab02019-08-12 14:34:16 -0700179 auto it = mNameToCallback.find(name);
180 if (it != mNameToCallback.end()) {
181 for (const sp<IServiceCallback>& cb : it->second) {
182 // permission checked in registerForNotifications
183 cb->onRegistration(name, binder);
184 }
185 }
186
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700187 return Status::ok();
188}
189
190Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700191 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700192 return Status::fromExceptionCode(Status::EX_SECURITY);
193 }
194
195 size_t toReserve = 0;
196 for (auto const& [name, service] : mNameToService) {
197 (void) name;
198
199 if (service.dumpPriority & dumpPriority) ++toReserve;
200 }
201
202 CHECK(outList->empty());
203
204 outList->reserve(toReserve);
205 for (auto const& [name, service] : mNameToService) {
206 (void) service;
207
208 if (service.dumpPriority & dumpPriority) {
209 outList->push_back(name);
210 }
211 }
212
213 return Status::ok();
214}
215
Steven Moreland27cfab02019-08-12 14:34:16 -0700216Status ServiceManager::registerForNotifications(
217 const std::string& name, const sp<IServiceCallback>& callback) {
218 auto ctx = mAccess->getCallingContext();
219
220 if (!mAccess->canFind(ctx, name)) {
221 return Status::fromExceptionCode(Status::EX_SECURITY);
222 }
223
224 if (!isValidServiceName(name)) {
225 LOG(ERROR) << "Invalid service name: " << name;
226 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
227 }
228
229 if (callback == nullptr) {
230 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
231 }
232
233 if (OK != IInterface::asBinder(callback)->linkToDeath(this)) {
234 LOG(ERROR) << "Could not linkToDeath when adding " << name;
235 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
236 }
237
238 mNameToCallback[name].push_back(callback);
239
240 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
241 const sp<IBinder>& binder = it->second.binder;
242
243 // never null if an entry exists
244 CHECK(binder != nullptr) << name;
245 callback->onRegistration(name, binder);
246 }
247
248 return Status::ok();
249}
250Status ServiceManager::unregisterForNotifications(
251 const std::string& name, const sp<IServiceCallback>& callback) {
252 auto ctx = mAccess->getCallingContext();
253
254 if (!mAccess->canFind(ctx, name)) {
255 return Status::fromExceptionCode(Status::EX_SECURITY);
256 }
257
258 bool found = false;
259
260 auto it = mNameToCallback.find(name);
261 if (it != mNameToCallback.end()) {
262 removeCallback(IInterface::asBinder(callback), &it, &found);
263 }
264
265 if (!found) {
266 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
267 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
268 }
269
270 return Status::ok();
271}
272
273void ServiceManager::removeCallback(const wp<IBinder>& who,
274 CallbackMap::iterator* it,
275 bool* found) {
276 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
277
278 for (auto lit = listeners.begin(); lit != listeners.end();) {
279 if (IInterface::asBinder(*lit) == who) {
280 if(found) *found = true;
281 lit = listeners.erase(lit);
282 } else {
283 ++lit;
284 }
285 }
286
287 if (listeners.empty()) {
288 *it = mNameToCallback.erase(*it);
289 } else {
290 it++;
291 }
292}
293
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700294void ServiceManager::binderDied(const wp<IBinder>& who) {
295 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
296 if (who == it->second.binder) {
297 it = mNameToService.erase(it);
298 } else {
299 ++it;
300 }
301 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700302
303 for (auto it = mNameToCallback.begin(); it != mNameToCallback.end();) {
304 removeCallback(who, &it, nullptr /*found*/);
305 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700306}
307
Jon Spivack0d844302019-07-22 18:40:34 -0700308void ServiceManager::tryStartService(const std::string& name) {
309 ALOGI("Since '%s' could not be found, trying to start it as a lazy AIDL service",
310 name.c_str());
311
312 std::thread([=] {
313 (void)base::SetProperty("ctl.interface_start", "aidl/" + name);
314 }).detach();
315}
316
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700317} // namespace android