blob: 81e61daae17b3d87a8a4b484b2672bf5f8c70c9b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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#define LOG_TAG "ServiceManager"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Tom Cherry8fda97f2020-07-22 17:15:44 -070021#include <inttypes.h>
22#include <unistd.h>
23
Steven Moreland1c47b582019-08-27 18:05:27 -070024#include <android/os/BnServiceCallback.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070025#include <android/os/IServiceManager.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070026#include <binder/IPCThreadState.h>
Steven Moreland28723ae2019-04-01 18:52:30 -070027#include <binder/Parcel.h>
28#include <utils/Log.h>
29#include <utils/String8.h>
30#include <utils/SystemClock.h>
31
Jiyong Park47f876b2018-04-17 13:56:46 +090032#ifndef __ANDROID_VNDK__
33#include <binder/IPermissionController.h>
34#endif
Steven Moreland28723ae2019-04-01 18:52:30 -070035
Steven Morelanded3b5632019-09-20 11:24:28 -070036#ifdef __ANDROID__
Yunfan Chen788e1c42018-03-29 16:52:09 +090037#include <cutils/properties.h>
Yifan Hongf7760012021-06-04 16:04:42 -070038#else
39#include "ServiceManagerHost.h"
Steven Moreland28723ae2019-04-01 18:52:30 -070040#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Steven Morelanda4853cd2019-07-12 15:44:37 -070042#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044namespace android {
45
Steven Moreland80e1e6d2019-06-21 12:35:59 -070046using AidlServiceManager = android::os::IServiceManager;
47using android::binder::Status;
48
Steven Moreland635a2912019-10-02 15:50:10 -070049// libbinder's IServiceManager.h can't rely on the values generated by AIDL
50// because many places use its headers via include_dirs (meaning, without
51// declaring the dependency in the build system). So, for now, we can just check
52// the values here.
53static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_CRITICAL == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
54static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_HIGH == IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
55static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_NORMAL == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
56static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_DEFAULT == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
57static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_ALL == IServiceManager::DUMP_FLAG_PRIORITY_ALL);
58static_assert(AidlServiceManager::DUMP_FLAG_PROTO == IServiceManager::DUMP_FLAG_PROTO);
59
Steven Moreland583685e2019-10-02 16:45:11 -070060const String16& IServiceManager::getInterfaceDescriptor() const {
61 return AidlServiceManager::descriptor;
62}
63IServiceManager::IServiceManager() {}
64IServiceManager::~IServiceManager() {}
65
66// From the old libbinder IServiceManager interface to IServiceManager.
67class ServiceManagerShim : public IServiceManager
68{
69public:
70 explicit ServiceManagerShim (const sp<AidlServiceManager>& impl);
71
72 sp<IBinder> getService(const String16& name) const override;
73 sp<IBinder> checkService(const String16& name) const override;
74 status_t addService(const String16& name, const sp<IBinder>& service,
75 bool allowIsolated, int dumpsysPriority) override;
76 Vector<String16> listServices(int dumpsysPriority) override;
77 sp<IBinder> waitForService(const String16& name16) override;
Steven Morelandb82b8f82019-10-28 10:52:34 -070078 bool isDeclared(const String16& name) override;
Steven Moreland2e293aa2020-09-23 00:25:16 +000079 Vector<String16> getDeclaredInstances(const String16& interface) override;
Steven Morelandedd4e072021-04-21 00:27:29 +000080 std::optional<String16> updatableViaApex(const String16& name) override;
Devin Moore5e4c2f12021-09-09 22:36:33 +000081 std::optional<IServiceManager::ConnectionInfo> getConnectionInfo(const String16& name) override;
Steven Moreland583685e2019-10-02 16:45:11 -070082
83 // for legacy ABI
84 const String16& getInterfaceDescriptor() const override {
85 return mTheRealServiceManager->getInterfaceDescriptor();
86 }
87 IBinder* onAsBinder() override {
88 return IInterface::asBinder(mTheRealServiceManager).get();
89 }
Yifan Hongf7760012021-06-04 16:04:42 -070090
91protected:
Steven Moreland583685e2019-10-02 16:45:11 -070092 sp<AidlServiceManager> mTheRealServiceManager;
Yifan Hongf7760012021-06-04 16:04:42 -070093
94 // Directly get the service in a way that, for lazy services, requests the service to be started
95 // if it is not currently started. This way, calls directly to ServiceManagerShim::getService
96 // will still have the 5s delay that is expected by a large amount of Android code.
97 //
98 // When implementing ServiceManagerShim, use realGetService instead of
99 // mTheRealServiceManager->getService so that it can be overridden in ServiceManagerHostShim.
100 virtual Status realGetService(const std::string& name, sp<IBinder>* _aidl_return) {
101 return mTheRealServiceManager->getService(name, _aidl_return);
102 }
Steven Moreland583685e2019-10-02 16:45:11 -0700103};
104
Steven Moreland1698ffd2020-05-15 23:43:52 +0000105[[clang::no_destroy]] static std::once_flag gSmOnce;
106[[clang::no_destroy]] static sp<IServiceManager> gDefaultServiceManager;
Brett Chabot38dbade2020-01-24 12:59:43 -0800107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108sp<IServiceManager> defaultServiceManager()
109{
Martijn Coenen402c8672020-02-03 10:01:36 +0100110 std::call_once(gSmOnce, []() {
111 sp<AidlServiceManager> sm = nullptr;
112 while (sm == nullptr) {
113 sm = interface_cast<AidlServiceManager>(ProcessState::self()->getContextObject(nullptr));
114 if (sm == nullptr) {
Steven Moreland39a57212020-05-19 18:10:07 +0000115 ALOGE("Waiting 1s on context object on %s.", ProcessState::self()->getDriverName().c_str());
Todd Poynora7b0f042013-06-18 17:25:37 -0700116 sleep(1);
Martijn Coenen402c8672020-02-03 10:01:36 +0100117 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 }
Martijn Coenen402c8672020-02-03 10:01:36 +0100119
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000120 gDefaultServiceManager = sp<ServiceManagerShim>::make(sm);
Martijn Coenen402c8672020-02-03 10:01:36 +0100121 });
Daniel Eratc2832702015-10-13 15:29:32 -0600122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 return gDefaultServiceManager;
124}
125
Brett Chabot38dbade2020-01-24 12:59:43 -0800126void setDefaultServiceManager(const sp<IServiceManager>& sm) {
Martijn Coenen402c8672020-02-03 10:01:36 +0100127 bool called = false;
128 std::call_once(gSmOnce, [&]() {
129 gDefaultServiceManager = sm;
130 called = true;
131 });
132
133 if (!called) {
134 LOG_ALWAYS_FATAL("setDefaultServiceManager() called after defaultServiceManager().");
135 }
Brett Chabot38dbade2020-01-24 12:59:43 -0800136}
137
Steven Morelanded3b5632019-09-20 11:24:28 -0700138#if !defined(__ANDROID_VNDK__) && defined(__ANDROID__)
Jiyong Park47f876b2018-04-17 13:56:46 +0900139// IPermissionController is not accessible to vendors
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141bool checkCallingPermission(const String16& permission)
142{
Yi Kongfdd8da92018-06-07 17:52:27 -0700143 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}
145
Steven Moreland1b264072021-06-03 23:04:02 +0000146static StaticString16 _permission(u"permission");
Mathias Agopian375f5632009-06-15 18:24:59 -0700147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
149{
150 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -0700151 pid_t pid = ipcState->getCallingPid();
152 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -0700154 if (outUid) *outUid = uid;
155 return checkPermission(permission, pid, uid);
156}
157
Jayant Chowdhary49bc34b2021-07-28 20:27:21 +0000158bool checkPermission(const String16& permission, pid_t pid, uid_t uid, bool logPermissionFailure) {
Steven Moreland92b17c62019-04-02 15:46:24 -0700159 static Mutex gPermissionControllerLock;
160 static sp<IPermissionController> gPermissionController;
161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 sp<IPermissionController> pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700163 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 pc = gPermissionController;
Steven Moreland92b17c62019-04-02 15:46:24 -0700165 gPermissionControllerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -0600166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 int64_t startTime = 0;
168
169 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700170 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 bool res = pc->checkPermission(permission, pid, uid);
172 if (res) {
173 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +0000174 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 (int)((uptimeMillis()-startTime)/1000),
176 String8(permission).string(), uid, pid);
177 }
178 return res;
179 }
Daniel Eratc2832702015-10-13 15:29:32 -0600180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -0800182 if (IInterface::asBinder(pc)->isBinderAlive()) {
Jayant Chowdhary49bc34b2021-07-28 20:27:21 +0000183 if (logPermissionFailure) {
184 ALOGW("Permission failure: %s from uid=%d pid=%d", String8(permission).string(),
185 uid, pid);
186 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 return false;
188 }
Daniel Eratc2832702015-10-13 15:29:32 -0600189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 // Object is dead!
Steven Moreland92b17c62019-04-02 15:46:24 -0700191 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700193 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 }
Steven Moreland92b17c62019-04-02 15:46:24 -0700195 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 }
Daniel Eratc2832702015-10-13 15:29:32 -0600197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 // Need to retrieve the permission controller.
199 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700200 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 // Wait for the permission controller to come back...
202 if (startTime == 0) {
203 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000204 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 String8(permission).string(), uid, pid);
206 }
207 sleep(1);
208 } else {
209 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600210 // Install the new permission controller, and try again.
Steven Moreland92b17c62019-04-02 15:46:24 -0700211 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 gPermissionController = pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700213 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 }
215 }
216}
217
Jiyong Park47f876b2018-04-17 13:56:46 +0900218#endif //__ANDROID_VNDK__
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220// ----------------------------------------------------------------------
221
Steven Moreland583685e2019-10-02 16:45:11 -0700222ServiceManagerShim::ServiceManagerShim(const sp<AidlServiceManager>& impl)
223 : mTheRealServiceManager(impl)
224{}
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700225
Steven Moreland5af7d852020-04-29 15:40:29 -0700226// This implementation could be simplified and made more efficient by delegating
227// to waitForService. However, this changes the threading structure in some
228// cases and could potentially break prebuilts. Once we have higher logistical
229// complexity, this could be attempted.
Steven Moreland583685e2019-10-02 16:45:11 -0700230sp<IBinder> ServiceManagerShim::getService(const String16& name) const
231{
232 static bool gSystemBootCompleted = false;
233
234 sp<IBinder> svc = checkService(name);
235 if (svc != nullptr) return svc;
236
237 const bool isVendorService =
238 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
Jiyong Park16c6e702020-11-13 20:53:12 +0900239 constexpr int64_t timeout = 5000;
Tom Cherry8fda97f2020-07-22 17:15:44 -0700240 int64_t startTime = uptimeMillis();
Steven Moreland583685e2019-10-02 16:45:11 -0700241 // Vendor code can't access system properties
242 if (!gSystemBootCompleted && !isVendorService) {
243#ifdef __ANDROID__
244 char bootCompleted[PROPERTY_VALUE_MAX];
245 property_get("sys.boot_completed", bootCompleted, "0");
246 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
247#else
248 gSystemBootCompleted = true;
249#endif
250 }
251 // retry interval in millisecond; note that vendor services stay at 100ms
Jiyong Park16c6e702020-11-13 20:53:12 +0900252 const useconds_t sleepTime = gSystemBootCompleted ? 1000 : 100;
Steven Moreland583685e2019-10-02 16:45:11 -0700253
Tom Cherry8fda97f2020-07-22 17:15:44 -0700254 ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(),
255 ProcessState::self()->getDriverName().c_str());
256
Steven Moreland583685e2019-10-02 16:45:11 -0700257 int n = 0;
Tom Cherry8fda97f2020-07-22 17:15:44 -0700258 while (uptimeMillis() - startTime < timeout) {
Steven Moreland583685e2019-10-02 16:45:11 -0700259 n++;
Steven Moreland583685e2019-10-02 16:45:11 -0700260 usleep(1000*sleepTime);
Steven Moreland92b17c62019-04-02 15:46:24 -0700261
Yunfan Chen788e1c42018-03-29 16:52:09 +0900262 sp<IBinder> svc = checkService(name);
Tom Cherry8fda97f2020-07-22 17:15:44 -0700263 if (svc != nullptr) {
264 ALOGI("Waiting for service '%s' on '%s' successful after waiting %" PRIi64 "ms",
265 String8(name).string(), ProcessState::self()->getDriverName().c_str(),
266 uptimeMillis() - startTime);
267 return svc;
268 }
Steven Moreland583685e2019-10-02 16:45:11 -0700269 }
270 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
271 return nullptr;
272}
Yunfan Chen788e1c42018-03-29 16:52:09 +0900273
Steven Moreland583685e2019-10-02 16:45:11 -0700274sp<IBinder> ServiceManagerShim::checkService(const String16& name) const
275{
276 sp<IBinder> ret;
277 if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) {
278 return nullptr;
279 }
280 return ret;
281}
282
283status_t ServiceManagerShim::addService(const String16& name, const sp<IBinder>& service,
284 bool allowIsolated, int dumpsysPriority)
285{
286 Status status = mTheRealServiceManager->addService(
287 String8(name).c_str(), service, allowIsolated, dumpsysPriority);
288 return status.exceptionCode();
289}
290
291Vector<String16> ServiceManagerShim::listServices(int dumpsysPriority)
292{
293 std::vector<std::string> ret;
294 if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) {
295 return {};
296 }
297
298 Vector<String16> res;
299 res.setCapacity(ret.size());
300 for (const std::string& name : ret) {
301 res.push(String16(name.c_str()));
302 }
303 return res;
304}
305
306sp<IBinder> ServiceManagerShim::waitForService(const String16& name16)
307{
308 class Waiter : public android::os::BnServiceCallback {
309 Status onRegistration(const std::string& /*name*/,
310 const sp<IBinder>& binder) override {
311 std::unique_lock<std::mutex> lock(mMutex);
312 mBinder = binder;
313 lock.unlock();
Jon Spivack9f503a42019-10-22 16:49:19 -0700314 // Flushing here helps ensure the service's ref count remains accurate
315 IPCThreadState::self()->flushCommands();
Steven Moreland583685e2019-10-02 16:45:11 -0700316 mCv.notify_one();
317 return Status::ok();
Yunfan Chen788e1c42018-03-29 16:52:09 +0900318 }
Steven Moreland583685e2019-10-02 16:45:11 -0700319 public:
320 sp<IBinder> mBinder;
321 std::mutex mMutex;
322 std::condition_variable mCv;
323 };
Yunfan Chen788e1c42018-03-29 16:52:09 +0900324
Jon Spivackfed6db42019-11-18 16:43:59 -0800325 // Simple RAII object to ensure a function call immediately before going out of scope
326 class Defer {
327 public:
Jiyong Park70072fd2020-11-13 17:19:14 +0900328 explicit Defer(std::function<void()>&& f) : mF(std::move(f)) {}
Jon Spivackfed6db42019-11-18 16:43:59 -0800329 ~Defer() { mF(); }
330 private:
331 std::function<void()> mF;
332 };
333
Steven Moreland583685e2019-10-02 16:45:11 -0700334 const std::string name = String8(name16).c_str();
Yunfan Chen788e1c42018-03-29 16:52:09 +0900335
Steven Moreland583685e2019-10-02 16:45:11 -0700336 sp<IBinder> out;
Yifan Hongf7760012021-06-04 16:04:42 -0700337 if (Status status = realGetService(name, &out); !status.isOk()) {
Steven Morelanda1d70172021-05-24 22:03:42 +0000338 ALOGW("Failed to getService in waitForService for %s: %s", name.c_str(),
339 status.toString8().c_str());
Steven Moreland583685e2019-10-02 16:45:11 -0700340 return nullptr;
341 }
Jon Spivackfed6db42019-11-18 16:43:59 -0800342 if (out != nullptr) return out;
Steven Moreland583685e2019-10-02 16:45:11 -0700343
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000344 sp<Waiter> waiter = sp<Waiter>::make();
Steven Morelanda1d70172021-05-24 22:03:42 +0000345 if (Status status = mTheRealServiceManager->registerForNotifications(name, waiter);
346 !status.isOk()) {
347 ALOGW("Failed to registerForNotifications in waitForService for %s: %s", name.c_str(),
348 status.toString8().c_str());
Yi Kongfdd8da92018-06-07 17:52:27 -0700349 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 }
Jon Spivackfed6db42019-11-18 16:43:59 -0800351 Defer unregister ([&] {
352 mTheRealServiceManager->unregisterForNotifications(name, waiter);
353 });
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700354
Steven Moreland583685e2019-10-02 16:45:11 -0700355 while(true) {
356 {
Steven Morelandf2677e22020-07-14 19:58:08 +0000357 // It would be really nice if we could read binder commands on this
358 // thread instead of needing a threadpool to be started, but for
359 // instance, if we call getAndExecuteCommand, it might be the case
360 // that another thread serves the callback, and we never get a
361 // command, so we hang indefinitely.
Steven Moreland583685e2019-10-02 16:45:11 -0700362 std::unique_lock<std::mutex> lock(waiter->mMutex);
363 using std::literals::chrono_literals::operator""s;
364 waiter->mCv.wait_for(lock, 1s, [&] {
365 return waiter->mBinder != nullptr;
366 });
367 if (waiter->mBinder != nullptr) return waiter->mBinder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700368 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369
Steven Morelandf2677e22020-07-14 19:58:08 +0000370 ALOGW("Waited one second for %s (is service started? are binder threads started and available?)", name.c_str());
371
Steven Moreland583685e2019-10-02 16:45:11 -0700372 // Handle race condition for lazy services. Here is what can happen:
373 // - the service dies (not processed by init yet).
374 // - sm processes death notification.
375 // - sm gets getService and calls init to start service.
376 // - init gets the start signal, but the service already appears
377 // started, so it does nothing.
378 // - init gets death signal, but doesn't know it needs to restart
379 // the service
380 // - we need to request service again to get it to start
Yifan Hongf7760012021-06-04 16:04:42 -0700381 if (Status status = realGetService(name, &out); !status.isOk()) {
Steven Morelanda1d70172021-05-24 22:03:42 +0000382 ALOGW("Failed to getService in waitForService on later try for %s: %s", name.c_str(),
383 status.toString8().c_str());
Steven Moreland1c47b582019-08-27 18:05:27 -0700384 return nullptr;
385 }
Jon Spivackfed6db42019-11-18 16:43:59 -0800386 if (out != nullptr) return out;
Steven Moreland1c47b582019-08-27 18:05:27 -0700387 }
Steven Moreland583685e2019-10-02 16:45:11 -0700388}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389
Steven Morelandb82b8f82019-10-28 10:52:34 -0700390bool ServiceManagerShim::isDeclared(const String16& name) {
391 bool declared;
Steven Morelanda1d70172021-05-24 22:03:42 +0000392 if (Status status = mTheRealServiceManager->isDeclared(String8(name).c_str(), &declared);
393 !status.isOk()) {
394 ALOGW("Failed to get isDeclard for %s: %s", String8(name).c_str(),
395 status.toString8().c_str());
Steven Morelandb82b8f82019-10-28 10:52:34 -0700396 return false;
397 }
398 return declared;
399}
400
Steven Moreland2e293aa2020-09-23 00:25:16 +0000401Vector<String16> ServiceManagerShim::getDeclaredInstances(const String16& interface) {
402 std::vector<std::string> out;
Steven Morelanda1d70172021-05-24 22:03:42 +0000403 if (Status status =
404 mTheRealServiceManager->getDeclaredInstances(String8(interface).c_str(), &out);
405 !status.isOk()) {
406 ALOGW("Failed to getDeclaredInstances for %s: %s", String8(interface).c_str(),
407 status.toString8().c_str());
Steven Moreland2e293aa2020-09-23 00:25:16 +0000408 return {};
409 }
410
411 Vector<String16> res;
412 res.setCapacity(out.size());
413 for (const std::string& instance : out) {
414 res.push(String16(instance.c_str()));
415 }
416 return res;
417}
418
Steven Morelandedd4e072021-04-21 00:27:29 +0000419std::optional<String16> ServiceManagerShim::updatableViaApex(const String16& name) {
420 std::optional<std::string> declared;
Steven Morelanda1d70172021-05-24 22:03:42 +0000421 if (Status status = mTheRealServiceManager->updatableViaApex(String8(name).c_str(), &declared);
422 !status.isOk()) {
423 ALOGW("Failed to get updatableViaApex for %s: %s", String8(name).c_str(),
424 status.toString8().c_str());
Steven Morelandedd4e072021-04-21 00:27:29 +0000425 return std::nullopt;
426 }
427 return declared ? std::optional<String16>(String16(declared.value().c_str())) : std::nullopt;
428}
429
Devin Moore5e4c2f12021-09-09 22:36:33 +0000430std::optional<IServiceManager::ConnectionInfo> ServiceManagerShim::getConnectionInfo(
431 const String16& name) {
432 std::optional<os::ConnectionInfo> connectionInfo;
433 if (Status status =
434 mTheRealServiceManager->getConnectionInfo(String8(name).c_str(), &connectionInfo);
435 !status.isOk()) {
436 ALOGW("Failed to get ConnectionInfo for %s: %s", String8(name).c_str(),
437 status.toString8().c_str());
438 }
439 return connectionInfo.has_value()
440 ? std::make_optional<IServiceManager::ConnectionInfo>(
441 {connectionInfo->ipAddress, static_cast<unsigned int>(connectionInfo->port)})
442 : std::nullopt;
443}
444
Yifan Hongf7760012021-06-04 16:04:42 -0700445#ifndef __ANDROID__
446// ServiceManagerShim for host. Implements the old libbinder android::IServiceManager API.
447// The internal implementation of the AIDL interface android::os::IServiceManager calls into
448// on-device service manager.
449class ServiceManagerHostShim : public ServiceManagerShim {
450public:
Yifan Hong5a05ef72021-10-08 17:33:47 -0700451 ServiceManagerHostShim(const sp<AidlServiceManager>& impl,
452 const RpcDelegateServiceManagerOptions& options)
453 : ServiceManagerShim(impl), mOptions(options) {}
Yifan Hongf7760012021-06-04 16:04:42 -0700454 // ServiceManagerShim::getService is based on checkService, so no need to override it.
455 sp<IBinder> checkService(const String16& name) const override {
Yifan Hong5a05ef72021-10-08 17:33:47 -0700456 return getDeviceService({String8(name).c_str()}, mOptions);
Yifan Hongf7760012021-06-04 16:04:42 -0700457 }
458
459protected:
460 // Override realGetService for ServiceManagerShim::waitForService.
461 Status realGetService(const std::string& name, sp<IBinder>* _aidl_return) {
Yifan Hong5a05ef72021-10-08 17:33:47 -0700462 *_aidl_return = getDeviceService({"-g", name}, mOptions);
Yifan Hongf7760012021-06-04 16:04:42 -0700463 return Status::ok();
464 }
Yifan Hong5a05ef72021-10-08 17:33:47 -0700465
466private:
467 RpcDelegateServiceManagerOptions mOptions;
Yifan Hongf7760012021-06-04 16:04:42 -0700468};
Yifan Hong5a05ef72021-10-08 17:33:47 -0700469sp<IServiceManager> createRpcDelegateServiceManager(
470 const RpcDelegateServiceManagerOptions& options) {
471 auto binder = getDeviceService({"manager"}, options);
Yifan Hongf7760012021-06-04 16:04:42 -0700472 if (binder == nullptr) {
473 ALOGE("getDeviceService(\"manager\") returns null");
474 return nullptr;
475 }
476 auto interface = AidlServiceManager::asInterface(binder);
477 if (interface == nullptr) {
478 ALOGE("getDeviceService(\"manager\") returns non service manager");
479 return nullptr;
480 }
Yifan Hong5a05ef72021-10-08 17:33:47 -0700481 return sp<ServiceManagerHostShim>::make(interface, options);
Yifan Hongf7760012021-06-04 16:04:42 -0700482}
483#endif
484
Steven Moreland61ff8492019-09-26 16:05:45 -0700485} // namespace android