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