The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Steven Moreland | 85d985c | 2022-09-12 20:57:51 +0000 | [diff] [blame] | 17 | #define LOG_TAG "ServiceManagerCppClient" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | #include <binder/IServiceManager.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Tom Cherry | 8fda97f | 2020-07-22 17:15:44 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | #include <unistd.h> |
| 23 | |
Steven Moreland | 454bfd9 | 2022-07-20 20:53:36 +0000 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 25 | #include <android/os/BnServiceCallback.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 26 | #include <android/os/IServiceManager.h> |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 28 | #include <binder/Parcel.h> |
| 29 | #include <utils/Log.h> |
| 30 | #include <utils/String8.h> |
| 31 | #include <utils/SystemClock.h> |
| 32 | |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 33 | #ifndef __ANDROID_VNDK__ |
| 34 | #include <binder/IPermissionController.h> |
| 35 | #endif |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 36 | |
Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 37 | #ifdef __ANDROID__ |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 39 | #else |
| 40 | #include "ServiceManagerHost.h" |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 41 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 43 | #include "Static.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 47 | using AidlRegistrationCallback = IServiceManager::LocalRegistrationCallback; |
| 48 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 49 | using AidlServiceManager = android::os::IServiceManager; |
| 50 | using android::binder::Status; |
| 51 | |
Steven Moreland | 635a291 | 2019-10-02 15:50:10 -0700 | [diff] [blame] | 52 | // libbinder's IServiceManager.h can't rely on the values generated by AIDL |
| 53 | // because many places use its headers via include_dirs (meaning, without |
| 54 | // declaring the dependency in the build system). So, for now, we can just check |
| 55 | // the values here. |
| 56 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_CRITICAL == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL); |
| 57 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_HIGH == IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
| 58 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_NORMAL == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL); |
| 59 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_DEFAULT == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT); |
| 60 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_ALL == IServiceManager::DUMP_FLAG_PRIORITY_ALL); |
| 61 | static_assert(AidlServiceManager::DUMP_FLAG_PROTO == IServiceManager::DUMP_FLAG_PROTO); |
| 62 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 63 | const String16& IServiceManager::getInterfaceDescriptor() const { |
| 64 | return AidlServiceManager::descriptor; |
| 65 | } |
| 66 | IServiceManager::IServiceManager() {} |
| 67 | IServiceManager::~IServiceManager() {} |
| 68 | |
| 69 | // From the old libbinder IServiceManager interface to IServiceManager. |
| 70 | class ServiceManagerShim : public IServiceManager |
| 71 | { |
| 72 | public: |
| 73 | explicit ServiceManagerShim (const sp<AidlServiceManager>& impl); |
| 74 | |
| 75 | sp<IBinder> getService(const String16& name) const override; |
| 76 | sp<IBinder> checkService(const String16& name) const override; |
| 77 | status_t addService(const String16& name, const sp<IBinder>& service, |
| 78 | bool allowIsolated, int dumpsysPriority) override; |
| 79 | Vector<String16> listServices(int dumpsysPriority) override; |
| 80 | sp<IBinder> waitForService(const String16& name16) override; |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 81 | bool isDeclared(const String16& name) override; |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 82 | Vector<String16> getDeclaredInstances(const String16& interface) override; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 83 | std::optional<String16> updatableViaApex(const String16& name) override; |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 84 | Vector<String16> getUpdatableNames(const String16& apexName) override; |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 85 | std::optional<IServiceManager::ConnectionInfo> getConnectionInfo(const String16& name) override; |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 86 | class RegistrationWaiter : public android::os::BnServiceCallback { |
| 87 | public: |
| 88 | explicit RegistrationWaiter(const sp<AidlRegistrationCallback>& callback) |
| 89 | : mImpl(callback) {} |
| 90 | Status onRegistration(const std::string& name, const sp<IBinder>& binder) override { |
| 91 | mImpl->onServiceRegistration(String16(name.c_str()), binder); |
| 92 | return Status::ok(); |
| 93 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 94 | |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 95 | private: |
| 96 | sp<AidlRegistrationCallback> mImpl; |
| 97 | }; |
| 98 | |
| 99 | status_t registerForNotifications(const String16& service, |
| 100 | const sp<AidlRegistrationCallback>& cb) override; |
| 101 | |
| 102 | status_t unregisterForNotifications(const String16& service, |
| 103 | const sp<AidlRegistrationCallback>& cb) override; |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 104 | |
| 105 | std::vector<IServiceManager::ServiceDebugInfo> getServiceDebugInfo() override; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 106 | // for legacy ABI |
| 107 | const String16& getInterfaceDescriptor() const override { |
| 108 | return mTheRealServiceManager->getInterfaceDescriptor(); |
| 109 | } |
| 110 | IBinder* onAsBinder() override { |
| 111 | return IInterface::asBinder(mTheRealServiceManager).get(); |
| 112 | } |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 113 | |
| 114 | protected: |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 115 | sp<AidlServiceManager> mTheRealServiceManager; |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 116 | // AidlRegistrationCallback -> services that its been registered for |
| 117 | // notifications. |
| 118 | using LocalRegistrationAndWaiter = |
| 119 | std::pair<sp<LocalRegistrationCallback>, sp<RegistrationWaiter>>; |
| 120 | using ServiceCallbackMap = std::map<std::string, std::vector<LocalRegistrationAndWaiter>>; |
| 121 | ServiceCallbackMap mNameToRegistrationCallback; |
| 122 | std::mutex mNameToRegistrationLock; |
| 123 | |
| 124 | void removeRegistrationCallbackLocked(const sp<AidlRegistrationCallback>& cb, |
| 125 | ServiceCallbackMap::iterator* it, |
| 126 | sp<RegistrationWaiter>* waiter); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 127 | |
| 128 | // Directly get the service in a way that, for lazy services, requests the service to be started |
| 129 | // if it is not currently started. This way, calls directly to ServiceManagerShim::getService |
| 130 | // will still have the 5s delay that is expected by a large amount of Android code. |
| 131 | // |
| 132 | // When implementing ServiceManagerShim, use realGetService instead of |
| 133 | // mTheRealServiceManager->getService so that it can be overridden in ServiceManagerHostShim. |
| 134 | virtual Status realGetService(const std::string& name, sp<IBinder>* _aidl_return) { |
| 135 | return mTheRealServiceManager->getService(name, _aidl_return); |
| 136 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Steven Moreland | 1698ffd | 2020-05-15 23:43:52 +0000 | [diff] [blame] | 139 | [[clang::no_destroy]] static std::once_flag gSmOnce; |
| 140 | [[clang::no_destroy]] static sp<IServiceManager> gDefaultServiceManager; |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 141 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | sp<IServiceManager> defaultServiceManager() |
| 143 | { |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 144 | std::call_once(gSmOnce, []() { |
Steven Moreland | 454bfd9 | 2022-07-20 20:53:36 +0000 | [diff] [blame] | 145 | #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) |
| 146 | /* wait for service manager */ { |
| 147 | using std::literals::chrono_literals::operator""s; |
| 148 | using android::base::WaitForProperty; |
| 149 | while (!WaitForProperty("servicemanager.ready", "true", 1s)) { |
| 150 | ALOGE("Waited for servicemanager.ready for a second, waiting another..."); |
| 151 | } |
| 152 | } |
| 153 | #endif |
| 154 | |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 155 | sp<AidlServiceManager> sm = nullptr; |
| 156 | while (sm == nullptr) { |
| 157 | sm = interface_cast<AidlServiceManager>(ProcessState::self()->getContextObject(nullptr)); |
| 158 | if (sm == nullptr) { |
Steven Moreland | 39a5721 | 2020-05-19 18:10:07 +0000 | [diff] [blame] | 159 | ALOGE("Waiting 1s on context object on %s.", ProcessState::self()->getDriverName().c_str()); |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 160 | sleep(1); |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 161 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 163 | |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 164 | gDefaultServiceManager = sp<ServiceManagerShim>::make(sm); |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 165 | }); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 166 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | return gDefaultServiceManager; |
| 168 | } |
| 169 | |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 170 | void setDefaultServiceManager(const sp<IServiceManager>& sm) { |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 171 | bool called = false; |
| 172 | std::call_once(gSmOnce, [&]() { |
| 173 | gDefaultServiceManager = sm; |
| 174 | called = true; |
| 175 | }); |
| 176 | |
| 177 | if (!called) { |
| 178 | LOG_ALWAYS_FATAL("setDefaultServiceManager() called after defaultServiceManager()."); |
| 179 | } |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Atneya Nair | 5b9cbbf | 2022-05-24 20:39:48 -0400 | [diff] [blame] | 182 | #if !defined(__ANDROID_VNDK__) |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 183 | // IPermissionController is not accessible to vendors |
| 184 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | bool checkCallingPermission(const String16& permission) |
| 186 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 187 | return checkCallingPermission(permission, nullptr, nullptr); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Steven Moreland | 1b26407 | 2021-06-03 23:04:02 +0000 | [diff] [blame] | 190 | static StaticString16 _permission(u"permission"); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) |
| 193 | { |
| 194 | IPCThreadState* ipcState = IPCThreadState::self(); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 195 | pid_t pid = ipcState->getCallingPid(); |
| 196 | uid_t uid = ipcState->getCallingUid(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | if (outPid) *outPid = pid; |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 198 | if (outUid) *outUid = uid; |
| 199 | return checkPermission(permission, pid, uid); |
| 200 | } |
| 201 | |
Jayant Chowdhary | 49bc34b | 2021-07-28 20:27:21 +0000 | [diff] [blame] | 202 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid, bool logPermissionFailure) { |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 203 | static Mutex gPermissionControllerLock; |
| 204 | static sp<IPermissionController> gPermissionController; |
| 205 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | sp<IPermissionController> pc; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 207 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | pc = gPermissionController; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 209 | gPermissionControllerLock.unlock(); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 210 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | int64_t startTime = 0; |
| 212 | |
| 213 | while (true) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 214 | if (pc != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | bool res = pc->checkPermission(permission, pid, uid); |
| 216 | if (res) { |
| 217 | if (startTime != 0) { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 218 | ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d", |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 219 | (int)((uptimeMillis() - startTime) / 1000), String8(permission).c_str(), |
| 220 | uid, pid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |
| 222 | return res; |
| 223 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 224 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // Is this a permission failure, or did the controller go away? |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 226 | if (IInterface::asBinder(pc)->isBinderAlive()) { |
Jayant Chowdhary | 49bc34b | 2021-07-28 20:27:21 +0000 | [diff] [blame] | 227 | if (logPermissionFailure) { |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 228 | ALOGW("Permission failure: %s from uid=%d pid=%d", String8(permission).c_str(), |
Jayant Chowdhary | 49bc34b | 2021-07-28 20:27:21 +0000 | [diff] [blame] | 229 | uid, pid); |
| 230 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | return false; |
| 232 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 233 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | // Object is dead! |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 235 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | if (gPermissionController == pc) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 237 | gPermissionController = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | } |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 239 | gPermissionControllerLock.unlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 241 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | // Need to retrieve the permission controller. |
| 243 | sp<IBinder> binder = defaultServiceManager()->checkService(_permission); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 244 | if (binder == nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | // Wait for the permission controller to come back... |
| 246 | if (startTime == 0) { |
| 247 | startTime = uptimeMillis(); |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 248 | ALOGI("Waiting to check permission %s from uid=%d pid=%d", |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 249 | String8(permission).c_str(), uid, pid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | } |
| 251 | sleep(1); |
| 252 | } else { |
| 253 | pc = interface_cast<IPermissionController>(binder); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 254 | // Install the new permission controller, and try again. |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 255 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | gPermissionController = pc; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 257 | gPermissionControllerLock.unlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 262 | #endif //__ANDROID_VNDK__ |
| 263 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // ---------------------------------------------------------------------- |
| 265 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 266 | ServiceManagerShim::ServiceManagerShim(const sp<AidlServiceManager>& impl) |
| 267 | : mTheRealServiceManager(impl) |
| 268 | {} |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 269 | |
Steven Moreland | 5af7d85 | 2020-04-29 15:40:29 -0700 | [diff] [blame] | 270 | // This implementation could be simplified and made more efficient by delegating |
| 271 | // to waitForService. However, this changes the threading structure in some |
| 272 | // cases and could potentially break prebuilts. Once we have higher logistical |
| 273 | // complexity, this could be attempted. |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 274 | sp<IBinder> ServiceManagerShim::getService(const String16& name) const |
| 275 | { |
| 276 | static bool gSystemBootCompleted = false; |
| 277 | |
| 278 | sp<IBinder> svc = checkService(name); |
| 279 | if (svc != nullptr) return svc; |
| 280 | |
| 281 | const bool isVendorService = |
| 282 | strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0; |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 283 | constexpr int64_t timeout = 5000; |
Tom Cherry | 8fda97f | 2020-07-22 17:15:44 -0700 | [diff] [blame] | 284 | int64_t startTime = uptimeMillis(); |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 285 | // Vendor code can't access system properties |
| 286 | if (!gSystemBootCompleted && !isVendorService) { |
| 287 | #ifdef __ANDROID__ |
| 288 | char bootCompleted[PROPERTY_VALUE_MAX]; |
| 289 | property_get("sys.boot_completed", bootCompleted, "0"); |
| 290 | gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false; |
| 291 | #else |
| 292 | gSystemBootCompleted = true; |
| 293 | #endif |
| 294 | } |
| 295 | // retry interval in millisecond; note that vendor services stay at 100ms |
Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 296 | const useconds_t sleepTime = gSystemBootCompleted ? 1000 : 100; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 297 | |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 298 | ALOGI("Waiting for service '%s' on '%s'...", String8(name).c_str(), |
Tom Cherry | 8fda97f | 2020-07-22 17:15:44 -0700 | [diff] [blame] | 299 | ProcessState::self()->getDriverName().c_str()); |
| 300 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 301 | int n = 0; |
Tom Cherry | 8fda97f | 2020-07-22 17:15:44 -0700 | [diff] [blame] | 302 | while (uptimeMillis() - startTime < timeout) { |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 303 | n++; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 304 | usleep(1000*sleepTime); |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 305 | |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 306 | sp<IBinder> svc = checkService(name); |
Tom Cherry | 8fda97f | 2020-07-22 17:15:44 -0700 | [diff] [blame] | 307 | if (svc != nullptr) { |
| 308 | ALOGI("Waiting for service '%s' on '%s' successful after waiting %" PRIi64 "ms", |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 309 | String8(name).c_str(), ProcessState::self()->getDriverName().c_str(), |
Tom Cherry | 8fda97f | 2020-07-22 17:15:44 -0700 | [diff] [blame] | 310 | uptimeMillis() - startTime); |
| 311 | return svc; |
| 312 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 313 | } |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame^] | 314 | ALOGW("Service %s didn't start. Returning NULL", String8(name).c_str()); |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 315 | return nullptr; |
| 316 | } |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 317 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 318 | sp<IBinder> ServiceManagerShim::checkService(const String16& name) const |
| 319 | { |
| 320 | sp<IBinder> ret; |
| 321 | if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) { |
| 322 | return nullptr; |
| 323 | } |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | status_t ServiceManagerShim::addService(const String16& name, const sp<IBinder>& service, |
| 328 | bool allowIsolated, int dumpsysPriority) |
| 329 | { |
| 330 | Status status = mTheRealServiceManager->addService( |
| 331 | String8(name).c_str(), service, allowIsolated, dumpsysPriority); |
| 332 | return status.exceptionCode(); |
| 333 | } |
| 334 | |
| 335 | Vector<String16> ServiceManagerShim::listServices(int dumpsysPriority) |
| 336 | { |
| 337 | std::vector<std::string> ret; |
| 338 | if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) { |
| 339 | return {}; |
| 340 | } |
| 341 | |
| 342 | Vector<String16> res; |
| 343 | res.setCapacity(ret.size()); |
| 344 | for (const std::string& name : ret) { |
| 345 | res.push(String16(name.c_str())); |
| 346 | } |
| 347 | return res; |
| 348 | } |
| 349 | |
| 350 | sp<IBinder> ServiceManagerShim::waitForService(const String16& name16) |
| 351 | { |
| 352 | class Waiter : public android::os::BnServiceCallback { |
| 353 | Status onRegistration(const std::string& /*name*/, |
| 354 | const sp<IBinder>& binder) override { |
| 355 | std::unique_lock<std::mutex> lock(mMutex); |
| 356 | mBinder = binder; |
| 357 | lock.unlock(); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 358 | // Flushing here helps ensure the service's ref count remains accurate |
| 359 | IPCThreadState::self()->flushCommands(); |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 360 | mCv.notify_one(); |
| 361 | return Status::ok(); |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 362 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 363 | public: |
| 364 | sp<IBinder> mBinder; |
| 365 | std::mutex mMutex; |
| 366 | std::condition_variable mCv; |
| 367 | }; |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 368 | |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 369 | // Simple RAII object to ensure a function call immediately before going out of scope |
| 370 | class Defer { |
| 371 | public: |
Jiyong Park | 70072fd | 2020-11-13 17:19:14 +0900 | [diff] [blame] | 372 | explicit Defer(std::function<void()>&& f) : mF(std::move(f)) {} |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 373 | ~Defer() { mF(); } |
| 374 | private: |
| 375 | std::function<void()> mF; |
| 376 | }; |
| 377 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 378 | const std::string name = String8(name16).c_str(); |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 379 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 380 | sp<IBinder> out; |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 381 | if (Status status = realGetService(name, &out); !status.isOk()) { |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 382 | ALOGW("Failed to getService in waitForService for %s: %s", name.c_str(), |
| 383 | status.toString8().c_str()); |
Elie Kheirallah | 27c2695 | 2022-09-07 21:45:26 +0000 | [diff] [blame] | 384 | if (0 == ProcessState::self()->getThreadPoolMaxTotalThreadCount()) { |
| 385 | ALOGW("Got service, but may be racey because we could not wait efficiently for it. " |
| 386 | "Threadpool has 0 guaranteed threads. " |
| 387 | "Is the threadpool configured properly? " |
| 388 | "See ProcessState::startThreadPool and " |
| 389 | "ProcessState::setThreadPoolMaxThreadCount."); |
| 390 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 391 | return nullptr; |
| 392 | } |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 393 | if (out != nullptr) return out; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 394 | |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 395 | sp<Waiter> waiter = sp<Waiter>::make(); |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 396 | if (Status status = mTheRealServiceManager->registerForNotifications(name, waiter); |
| 397 | !status.isOk()) { |
| 398 | ALOGW("Failed to registerForNotifications in waitForService for %s: %s", name.c_str(), |
| 399 | status.toString8().c_str()); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 400 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 402 | Defer unregister ([&] { |
| 403 | mTheRealServiceManager->unregisterForNotifications(name, waiter); |
| 404 | }); |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 405 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 406 | while(true) { |
| 407 | { |
Steven Moreland | f2677e2 | 2020-07-14 19:58:08 +0000 | [diff] [blame] | 408 | // It would be really nice if we could read binder commands on this |
| 409 | // thread instead of needing a threadpool to be started, but for |
| 410 | // instance, if we call getAndExecuteCommand, it might be the case |
| 411 | // that another thread serves the callback, and we never get a |
| 412 | // command, so we hang indefinitely. |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 413 | std::unique_lock<std::mutex> lock(waiter->mMutex); |
| 414 | using std::literals::chrono_literals::operator""s; |
Steven Moreland | 3d37ef2 | 2023-04-25 18:25:14 +0000 | [diff] [blame] | 415 | waiter->mCv.wait_for(lock, 1s, [&] { |
| 416 | return waiter->mBinder != nullptr; |
| 417 | }); |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 418 | if (waiter->mBinder != nullptr) return waiter->mBinder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 419 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | |
Steven Moreland | 3d37ef2 | 2023-04-25 18:25:14 +0000 | [diff] [blame] | 421 | ALOGW("Waited one second for %s (is service started? Number of threads started in the " |
Elie Kheirallah | 27c2695 | 2022-09-07 21:45:26 +0000 | [diff] [blame] | 422 | "threadpool: %zu. Are binder threads started and available?)", |
| 423 | name.c_str(), ProcessState::self()->getThreadPoolMaxTotalThreadCount()); |
Steven Moreland | f2677e2 | 2020-07-14 19:58:08 +0000 | [diff] [blame] | 424 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 425 | // Handle race condition for lazy services. Here is what can happen: |
| 426 | // - the service dies (not processed by init yet). |
| 427 | // - sm processes death notification. |
| 428 | // - sm gets getService and calls init to start service. |
| 429 | // - init gets the start signal, but the service already appears |
| 430 | // started, so it does nothing. |
| 431 | // - init gets death signal, but doesn't know it needs to restart |
| 432 | // the service |
| 433 | // - we need to request service again to get it to start |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 434 | if (Status status = realGetService(name, &out); !status.isOk()) { |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 435 | ALOGW("Failed to getService in waitForService on later try for %s: %s", name.c_str(), |
| 436 | status.toString8().c_str()); |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 437 | return nullptr; |
| 438 | } |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 439 | if (out != nullptr) return out; |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 440 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 441 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 443 | bool ServiceManagerShim::isDeclared(const String16& name) { |
| 444 | bool declared; |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 445 | if (Status status = mTheRealServiceManager->isDeclared(String8(name).c_str(), &declared); |
| 446 | !status.isOk()) { |
Vova Sharaienko | ca71b45 | 2022-12-08 01:38:29 +0000 | [diff] [blame] | 447 | ALOGW("Failed to get isDeclared for %s: %s", String8(name).c_str(), |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 448 | status.toString8().c_str()); |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | return declared; |
| 452 | } |
| 453 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 454 | Vector<String16> ServiceManagerShim::getDeclaredInstances(const String16& interface) { |
| 455 | std::vector<std::string> out; |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 456 | if (Status status = |
| 457 | mTheRealServiceManager->getDeclaredInstances(String8(interface).c_str(), &out); |
| 458 | !status.isOk()) { |
| 459 | ALOGW("Failed to getDeclaredInstances for %s: %s", String8(interface).c_str(), |
| 460 | status.toString8().c_str()); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 461 | return {}; |
| 462 | } |
| 463 | |
| 464 | Vector<String16> res; |
| 465 | res.setCapacity(out.size()); |
| 466 | for (const std::string& instance : out) { |
| 467 | res.push(String16(instance.c_str())); |
| 468 | } |
| 469 | return res; |
| 470 | } |
| 471 | |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 472 | std::optional<String16> ServiceManagerShim::updatableViaApex(const String16& name) { |
| 473 | std::optional<std::string> declared; |
Steven Moreland | a1d7017 | 2021-05-24 22:03:42 +0000 | [diff] [blame] | 474 | if (Status status = mTheRealServiceManager->updatableViaApex(String8(name).c_str(), &declared); |
| 475 | !status.isOk()) { |
| 476 | ALOGW("Failed to get updatableViaApex for %s: %s", String8(name).c_str(), |
| 477 | status.toString8().c_str()); |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 478 | return std::nullopt; |
| 479 | } |
| 480 | return declared ? std::optional<String16>(String16(declared.value().c_str())) : std::nullopt; |
| 481 | } |
| 482 | |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 483 | Vector<String16> ServiceManagerShim::getUpdatableNames(const String16& apexName) { |
| 484 | std::vector<std::string> out; |
| 485 | if (Status status = mTheRealServiceManager->getUpdatableNames(String8(apexName).c_str(), &out); |
| 486 | !status.isOk()) { |
| 487 | ALOGW("Failed to getUpdatableNames for %s: %s", String8(apexName).c_str(), |
| 488 | status.toString8().c_str()); |
| 489 | return {}; |
| 490 | } |
| 491 | |
| 492 | Vector<String16> res; |
| 493 | res.setCapacity(out.size()); |
| 494 | for (const std::string& instance : out) { |
| 495 | res.push(String16(instance.c_str())); |
| 496 | } |
| 497 | return res; |
| 498 | } |
| 499 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 500 | std::optional<IServiceManager::ConnectionInfo> ServiceManagerShim::getConnectionInfo( |
| 501 | const String16& name) { |
| 502 | std::optional<os::ConnectionInfo> connectionInfo; |
| 503 | if (Status status = |
| 504 | mTheRealServiceManager->getConnectionInfo(String8(name).c_str(), &connectionInfo); |
| 505 | !status.isOk()) { |
| 506 | ALOGW("Failed to get ConnectionInfo for %s: %s", String8(name).c_str(), |
| 507 | status.toString8().c_str()); |
| 508 | } |
| 509 | return connectionInfo.has_value() |
| 510 | ? std::make_optional<IServiceManager::ConnectionInfo>( |
| 511 | {connectionInfo->ipAddress, static_cast<unsigned int>(connectionInfo->port)}) |
| 512 | : std::nullopt; |
| 513 | } |
| 514 | |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 515 | status_t ServiceManagerShim::registerForNotifications(const String16& name, |
| 516 | const sp<AidlRegistrationCallback>& cb) { |
| 517 | if (cb == nullptr) { |
| 518 | ALOGE("%s: null cb passed", __FUNCTION__); |
| 519 | return BAD_VALUE; |
| 520 | } |
| 521 | std::string nameStr = String8(name).c_str(); |
| 522 | sp<RegistrationWaiter> registrationWaiter = sp<RegistrationWaiter>::make(cb); |
| 523 | std::lock_guard<std::mutex> lock(mNameToRegistrationLock); |
| 524 | if (Status status = |
| 525 | mTheRealServiceManager->registerForNotifications(nameStr, registrationWaiter); |
| 526 | !status.isOk()) { |
| 527 | ALOGW("Failed to registerForNotifications for %s: %s", nameStr.c_str(), |
| 528 | status.toString8().c_str()); |
| 529 | return UNKNOWN_ERROR; |
| 530 | } |
| 531 | mNameToRegistrationCallback[nameStr].push_back(std::make_pair(cb, registrationWaiter)); |
| 532 | return OK; |
| 533 | } |
| 534 | |
| 535 | void ServiceManagerShim::removeRegistrationCallbackLocked(const sp<AidlRegistrationCallback>& cb, |
| 536 | ServiceCallbackMap::iterator* it, |
| 537 | sp<RegistrationWaiter>* waiter) { |
| 538 | std::vector<LocalRegistrationAndWaiter>& localRegistrationAndWaiters = (*it)->second; |
| 539 | for (auto lit = localRegistrationAndWaiters.begin(); |
| 540 | lit != localRegistrationAndWaiters.end();) { |
| 541 | if (lit->first == cb) { |
| 542 | if (waiter) { |
| 543 | *waiter = lit->second; |
| 544 | } |
| 545 | lit = localRegistrationAndWaiters.erase(lit); |
| 546 | } else { |
| 547 | ++lit; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (localRegistrationAndWaiters.empty()) { |
| 552 | mNameToRegistrationCallback.erase(*it); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | status_t ServiceManagerShim::unregisterForNotifications(const String16& name, |
| 557 | const sp<AidlRegistrationCallback>& cb) { |
| 558 | if (cb == nullptr) { |
| 559 | ALOGE("%s: null cb passed", __FUNCTION__); |
| 560 | return BAD_VALUE; |
| 561 | } |
| 562 | std::string nameStr = String8(name).c_str(); |
| 563 | std::lock_guard<std::mutex> lock(mNameToRegistrationLock); |
| 564 | auto it = mNameToRegistrationCallback.find(nameStr); |
| 565 | sp<RegistrationWaiter> registrationWaiter; |
| 566 | if (it != mNameToRegistrationCallback.end()) { |
| 567 | removeRegistrationCallbackLocked(cb, &it, ®istrationWaiter); |
| 568 | } else { |
| 569 | ALOGE("%s no callback registered for notifications on %s", __FUNCTION__, nameStr.c_str()); |
| 570 | return BAD_VALUE; |
| 571 | } |
| 572 | if (registrationWaiter == nullptr) { |
| 573 | ALOGE("%s Callback passed wasn't used to register for notifications", __FUNCTION__); |
| 574 | return BAD_VALUE; |
| 575 | } |
| 576 | if (Status status = mTheRealServiceManager->unregisterForNotifications(String8(name).c_str(), |
| 577 | registrationWaiter); |
| 578 | !status.isOk()) { |
| 579 | ALOGW("Failed to get service manager to unregisterForNotifications for %s: %s", |
| 580 | String8(name).c_str(), status.toString8().c_str()); |
| 581 | return UNKNOWN_ERROR; |
| 582 | } |
| 583 | return OK; |
| 584 | } |
| 585 | |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 586 | std::vector<IServiceManager::ServiceDebugInfo> ServiceManagerShim::getServiceDebugInfo() { |
| 587 | std::vector<os::ServiceDebugInfo> serviceDebugInfos; |
| 588 | std::vector<IServiceManager::ServiceDebugInfo> ret; |
| 589 | if (Status status = mTheRealServiceManager->getServiceDebugInfo(&serviceDebugInfos); |
| 590 | !status.isOk()) { |
| 591 | ALOGW("%s Failed to get ServiceDebugInfo", __FUNCTION__); |
| 592 | return ret; |
| 593 | } |
| 594 | for (const auto& serviceDebugInfo : serviceDebugInfos) { |
| 595 | IServiceManager::ServiceDebugInfo retInfo; |
| 596 | retInfo.pid = serviceDebugInfo.debugPid; |
| 597 | retInfo.name = serviceDebugInfo.name; |
| 598 | ret.emplace_back(retInfo); |
| 599 | } |
| 600 | return ret; |
| 601 | } |
| 602 | |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 603 | #ifndef __ANDROID__ |
| 604 | // ServiceManagerShim for host. Implements the old libbinder android::IServiceManager API. |
| 605 | // The internal implementation of the AIDL interface android::os::IServiceManager calls into |
| 606 | // on-device service manager. |
| 607 | class ServiceManagerHostShim : public ServiceManagerShim { |
| 608 | public: |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 609 | ServiceManagerHostShim(const sp<AidlServiceManager>& impl, |
| 610 | const RpcDelegateServiceManagerOptions& options) |
| 611 | : ServiceManagerShim(impl), mOptions(options) {} |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 612 | // ServiceManagerShim::getService is based on checkService, so no need to override it. |
| 613 | sp<IBinder> checkService(const String16& name) const override { |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 614 | return getDeviceService({String8(name).c_str()}, mOptions); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | protected: |
| 618 | // Override realGetService for ServiceManagerShim::waitForService. |
| 619 | Status realGetService(const std::string& name, sp<IBinder>* _aidl_return) { |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 620 | *_aidl_return = getDeviceService({"-g", name}, mOptions); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 621 | return Status::ok(); |
| 622 | } |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 623 | |
| 624 | private: |
| 625 | RpcDelegateServiceManagerOptions mOptions; |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 626 | }; |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 627 | sp<IServiceManager> createRpcDelegateServiceManager( |
| 628 | const RpcDelegateServiceManagerOptions& options) { |
| 629 | auto binder = getDeviceService({"manager"}, options); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 630 | if (binder == nullptr) { |
| 631 | ALOGE("getDeviceService(\"manager\") returns null"); |
| 632 | return nullptr; |
| 633 | } |
| 634 | auto interface = AidlServiceManager::asInterface(binder); |
| 635 | if (interface == nullptr) { |
| 636 | ALOGE("getDeviceService(\"manager\") returns non service manager"); |
| 637 | return nullptr; |
| 638 | } |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 639 | return sp<ServiceManagerHostShim>::make(interface, options); |
Yifan Hong | f776001 | 2021-06-04 16:04:42 -0700 | [diff] [blame] | 640 | } |
| 641 | #endif |
| 642 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 643 | } // namespace android |