blob: b3aa342a1973b6e0da1df1cbb7e21d6cb8b2d02e [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>
20#include <cutils/android_filesystem_config.h>
21#include <cutils/multiuser.h>
22
23using ::android::binder::Status;
24
25namespace android {
26
27ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {}
Steven Moreland130242d2019-08-26 17:41:32 -070028ServiceManager::~ServiceManager() {
29 // this should only happen in tests
30
Steven Moreland27cfab02019-08-12 14:34:16 -070031 for (const auto& [name, callbacks] : mNameToCallback) {
32 CHECK(!callbacks.empty()) << name;
33 for (const auto& callback : callbacks) {
34 CHECK(callback != nullptr) << name;
35 }
36 }
37
Steven Moreland130242d2019-08-26 17:41:32 -070038 for (const auto& [name, service] : mNameToService) {
39 CHECK(service.binder != nullptr) << name;
40 }
41}
Steven Moreland80e1e6d2019-06-21 12:35:59 -070042
43Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
44 // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
45 return checkService(name, outBinder);
46}
47
48Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
Steven Morelanda9fe4742019-07-18 14:45:20 -070049 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070050
51 auto it = mNameToService.find(name);
52 if (it == mNameToService.end()) {
53 *outBinder = nullptr;
54 return Status::ok();
55 }
56
57 const Service& service = it->second;
58
59 if (!service.allowIsolated) {
60 uid_t appid = multiuser_get_app_id(ctx.uid);
61 bool isIsolated = appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
62
63 if (isIsolated) {
64 *outBinder = nullptr;
65 return Status::ok();
66 }
67 }
68
Steven Morelanda9fe4742019-07-18 14:45:20 -070069 if (!mAccess->canFind(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -070070 // returns ok and null for legacy reasons
71 *outBinder = nullptr;
72 return Status::ok();
73 }
74
75 *outBinder = service.binder;
76 return Status::ok();
77}
78
Steven Moreland905e2e82019-07-17 11:05:45 -070079bool isValidServiceName(const std::string& name) {
80 if (name.size() == 0) return false;
81 if (name.size() > 127) return false;
82
83 for (char c : name) {
Steven Morelandbb7951d2019-08-20 16:58:25 -070084 if (c == '_' || c == '-' || c == '.' || c == '/') continue;
Steven Moreland905e2e82019-07-17 11:05:45 -070085 if (c >= 'a' && c <= 'z') continue;
86 if (c >= 'A' && c <= 'Z') continue;
87 if (c >= '0' && c <= '9') continue;
88 return false;
89 }
90
91 return true;
92}
93
Steven Moreland80e1e6d2019-06-21 12:35:59 -070094Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
Steven Morelanda9fe4742019-07-18 14:45:20 -070095 auto ctx = mAccess->getCallingContext();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070096
97 // apps cannot add services
98 if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
99 return Status::fromExceptionCode(Status::EX_SECURITY);
100 }
101
Steven Morelanda9fe4742019-07-18 14:45:20 -0700102 if (!mAccess->canAdd(ctx, name)) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700103 return Status::fromExceptionCode(Status::EX_SECURITY);
104 }
105
106 if (binder == nullptr) {
107 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
108 }
109
Steven Moreland905e2e82019-07-17 11:05:45 -0700110 if (!isValidServiceName(name)) {
111 LOG(ERROR) << "Invalid service name: " << name;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700112 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
113 }
114
Steven Moreland88860b02019-08-12 14:24:14 -0700115 // implicitly unlinked when the binder is removed
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700116 if (OK != binder->linkToDeath(this)) {
117 LOG(ERROR) << "Could not linkToDeath when adding " << name;
118 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
119 }
120
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700121 mNameToService[name] = Service {
122 .binder = binder,
123 .allowIsolated = allowIsolated,
124 .dumpPriority = dumpPriority,
125 };
126
Steven Moreland27cfab02019-08-12 14:34:16 -0700127 auto it = mNameToCallback.find(name);
128 if (it != mNameToCallback.end()) {
129 for (const sp<IServiceCallback>& cb : it->second) {
130 // permission checked in registerForNotifications
131 cb->onRegistration(name, binder);
132 }
133 }
134
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700135 return Status::ok();
136}
137
138Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
Steven Morelanda9fe4742019-07-18 14:45:20 -0700139 if (!mAccess->canList(mAccess->getCallingContext())) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700140 return Status::fromExceptionCode(Status::EX_SECURITY);
141 }
142
143 size_t toReserve = 0;
144 for (auto const& [name, service] : mNameToService) {
145 (void) name;
146
147 if (service.dumpPriority & dumpPriority) ++toReserve;
148 }
149
150 CHECK(outList->empty());
151
152 outList->reserve(toReserve);
153 for (auto const& [name, service] : mNameToService) {
154 (void) service;
155
156 if (service.dumpPriority & dumpPriority) {
157 outList->push_back(name);
158 }
159 }
160
161 return Status::ok();
162}
163
Steven Moreland27cfab02019-08-12 14:34:16 -0700164Status ServiceManager::registerForNotifications(
165 const std::string& name, const sp<IServiceCallback>& callback) {
166 auto ctx = mAccess->getCallingContext();
167
168 if (!mAccess->canFind(ctx, name)) {
169 return Status::fromExceptionCode(Status::EX_SECURITY);
170 }
171
172 if (!isValidServiceName(name)) {
173 LOG(ERROR) << "Invalid service name: " << name;
174 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
175 }
176
177 if (callback == nullptr) {
178 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
179 }
180
181 if (OK != IInterface::asBinder(callback)->linkToDeath(this)) {
182 LOG(ERROR) << "Could not linkToDeath when adding " << name;
183 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
184 }
185
186 mNameToCallback[name].push_back(callback);
187
188 if (auto it = mNameToService.find(name); it != mNameToService.end()) {
189 const sp<IBinder>& binder = it->second.binder;
190
191 // never null if an entry exists
192 CHECK(binder != nullptr) << name;
193 callback->onRegistration(name, binder);
194 }
195
196 return Status::ok();
197}
198Status ServiceManager::unregisterForNotifications(
199 const std::string& name, const sp<IServiceCallback>& callback) {
200 auto ctx = mAccess->getCallingContext();
201
202 if (!mAccess->canFind(ctx, name)) {
203 return Status::fromExceptionCode(Status::EX_SECURITY);
204 }
205
206 bool found = false;
207
208 auto it = mNameToCallback.find(name);
209 if (it != mNameToCallback.end()) {
210 removeCallback(IInterface::asBinder(callback), &it, &found);
211 }
212
213 if (!found) {
214 LOG(ERROR) << "Trying to unregister callback, but none exists " << name;
215 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
216 }
217
218 return Status::ok();
219}
220
221void ServiceManager::removeCallback(const wp<IBinder>& who,
222 CallbackMap::iterator* it,
223 bool* found) {
224 std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
225
226 for (auto lit = listeners.begin(); lit != listeners.end();) {
227 if (IInterface::asBinder(*lit) == who) {
228 if(found) *found = true;
229 lit = listeners.erase(lit);
230 } else {
231 ++lit;
232 }
233 }
234
235 if (listeners.empty()) {
236 *it = mNameToCallback.erase(*it);
237 } else {
238 it++;
239 }
240}
241
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700242void ServiceManager::binderDied(const wp<IBinder>& who) {
243 for (auto it = mNameToService.begin(); it != mNameToService.end();) {
244 if (who == it->second.binder) {
245 it = mNameToService.erase(it);
246 } else {
247 ++it;
248 }
249 }
Steven Moreland27cfab02019-08-12 14:34:16 -0700250
251 for (auto it = mNameToCallback.begin(); it != mNameToCallback.end();) {
252 removeCallback(who, &it, nullptr /*found*/);
253 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700254}
255
256} // namespace android