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