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