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