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