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 | |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 21 | #include <android/os/BnServiceCallback.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 22 | #include <android/os/IServiceManager.h> |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 24 | #include <binder/Parcel.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/String8.h> |
| 27 | #include <utils/SystemClock.h> |
| 28 | |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 29 | #ifndef __ANDROID_VNDK__ |
| 30 | #include <binder/IPermissionController.h> |
| 31 | #endif |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 32 | |
Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 33 | #ifdef __ANDROID__ |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 35 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 37 | #include "Static.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | namespace android { |
| 42 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 43 | using AidlServiceManager = android::os::IServiceManager; |
| 44 | using android::binder::Status; |
| 45 | |
Steven Moreland | 635a291 | 2019-10-02 15:50:10 -0700 | [diff] [blame] | 46 | // libbinder's IServiceManager.h can't rely on the values generated by AIDL |
| 47 | // because many places use its headers via include_dirs (meaning, without |
| 48 | // declaring the dependency in the build system). So, for now, we can just check |
| 49 | // the values here. |
| 50 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_CRITICAL == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL); |
| 51 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_HIGH == IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
| 52 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_NORMAL == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL); |
| 53 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_DEFAULT == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT); |
| 54 | static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_ALL == IServiceManager::DUMP_FLAG_PRIORITY_ALL); |
| 55 | static_assert(AidlServiceManager::DUMP_FLAG_PROTO == IServiceManager::DUMP_FLAG_PROTO); |
| 56 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 57 | const String16& IServiceManager::getInterfaceDescriptor() const { |
| 58 | return AidlServiceManager::descriptor; |
| 59 | } |
| 60 | IServiceManager::IServiceManager() {} |
| 61 | IServiceManager::~IServiceManager() {} |
| 62 | |
| 63 | // From the old libbinder IServiceManager interface to IServiceManager. |
| 64 | class ServiceManagerShim : public IServiceManager |
| 65 | { |
| 66 | public: |
| 67 | explicit ServiceManagerShim (const sp<AidlServiceManager>& impl); |
| 68 | |
| 69 | sp<IBinder> getService(const String16& name) const override; |
| 70 | sp<IBinder> checkService(const String16& name) const override; |
| 71 | status_t addService(const String16& name, const sp<IBinder>& service, |
| 72 | bool allowIsolated, int dumpsysPriority) override; |
| 73 | Vector<String16> listServices(int dumpsysPriority) override; |
| 74 | sp<IBinder> waitForService(const String16& name16) override; |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 75 | bool isDeclared(const String16& name) override; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 76 | |
| 77 | // for legacy ABI |
| 78 | const String16& getInterfaceDescriptor() const override { |
| 79 | return mTheRealServiceManager->getInterfaceDescriptor(); |
| 80 | } |
| 81 | IBinder* onAsBinder() override { |
| 82 | return IInterface::asBinder(mTheRealServiceManager).get(); |
| 83 | } |
| 84 | private: |
| 85 | sp<AidlServiceManager> mTheRealServiceManager; |
| 86 | }; |
| 87 | |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 88 | static std::once_flag gSmOnce; |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 89 | static sp<IServiceManager> gDefaultServiceManager; |
| 90 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | sp<IServiceManager> defaultServiceManager() |
| 92 | { |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 93 | std::call_once(gSmOnce, []() { |
| 94 | sp<AidlServiceManager> sm = nullptr; |
| 95 | while (sm == nullptr) { |
| 96 | sm = interface_cast<AidlServiceManager>(ProcessState::self()->getContextObject(nullptr)); |
| 97 | if (sm == nullptr) { |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 98 | sleep(1); |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 99 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 101 | |
| 102 | gDefaultServiceManager = new ServiceManagerShim(sm); |
| 103 | }); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 104 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | return gDefaultServiceManager; |
| 106 | } |
| 107 | |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 108 | void setDefaultServiceManager(const sp<IServiceManager>& sm) { |
Martijn Coenen | 402c867 | 2020-02-03 10:01:36 +0100 | [diff] [blame] | 109 | bool called = false; |
| 110 | std::call_once(gSmOnce, [&]() { |
| 111 | gDefaultServiceManager = sm; |
| 112 | called = true; |
| 113 | }); |
| 114 | |
| 115 | if (!called) { |
| 116 | LOG_ALWAYS_FATAL("setDefaultServiceManager() called after defaultServiceManager()."); |
| 117 | } |
Brett Chabot | 38dbade | 2020-01-24 12:59:43 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 120 | #if !defined(__ANDROID_VNDK__) && defined(__ANDROID__) |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 121 | // IPermissionController is not accessible to vendors |
| 122 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | bool checkCallingPermission(const String16& permission) |
| 124 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 125 | return checkCallingPermission(permission, nullptr, nullptr); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static String16 _permission("permission"); |
| 129 | |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 130 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) |
| 132 | { |
| 133 | IPCThreadState* ipcState = IPCThreadState::self(); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 134 | pid_t pid = ipcState->getCallingPid(); |
| 135 | uid_t uid = ipcState->getCallingUid(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | if (outPid) *outPid = pid; |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 137 | if (outUid) *outUid = uid; |
| 138 | return checkPermission(permission, pid, uid); |
| 139 | } |
| 140 | |
| 141 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid) |
| 142 | { |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 143 | static Mutex gPermissionControllerLock; |
| 144 | static sp<IPermissionController> gPermissionController; |
| 145 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | sp<IPermissionController> pc; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 147 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | pc = gPermissionController; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 149 | gPermissionControllerLock.unlock(); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 150 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | int64_t startTime = 0; |
| 152 | |
| 153 | while (true) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 154 | if (pc != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | bool res = pc->checkPermission(permission, pid, uid); |
| 156 | if (res) { |
| 157 | if (startTime != 0) { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 158 | 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] | 159 | (int)((uptimeMillis()-startTime)/1000), |
| 160 | String8(permission).string(), uid, pid); |
| 161 | } |
| 162 | return res; |
| 163 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 164 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | // Is this a permission failure, or did the controller go away? |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 166 | if (IInterface::asBinder(pc)->isBinderAlive()) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 167 | ALOGW("Permission failure: %s from uid=%d pid=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | String8(permission).string(), uid, pid); |
| 169 | return false; |
| 170 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 171 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | // Object is dead! |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 173 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | if (gPermissionController == pc) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 175 | gPermissionController = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | } |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 177 | gPermissionControllerLock.unlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | // Need to retrieve the permission controller. |
| 181 | sp<IBinder> binder = defaultServiceManager()->checkService(_permission); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 182 | if (binder == nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | // Wait for the permission controller to come back... |
| 184 | if (startTime == 0) { |
| 185 | startTime = uptimeMillis(); |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 186 | 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] | 187 | String8(permission).string(), uid, pid); |
| 188 | } |
| 189 | sleep(1); |
| 190 | } else { |
| 191 | pc = interface_cast<IPermissionController>(binder); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 192 | // Install the new permission controller, and try again. |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 193 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | gPermissionController = pc; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 195 | gPermissionControllerLock.unlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 200 | #endif //__ANDROID_VNDK__ |
| 201 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | // ---------------------------------------------------------------------- |
| 203 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 204 | ServiceManagerShim::ServiceManagerShim(const sp<AidlServiceManager>& impl) |
| 205 | : mTheRealServiceManager(impl) |
| 206 | {} |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 207 | |
Steven Moreland | 5af7d85 | 2020-04-29 15:40:29 -0700 | [diff] [blame] | 208 | // This implementation could be simplified and made more efficient by delegating |
| 209 | // to waitForService. However, this changes the threading structure in some |
| 210 | // cases and could potentially break prebuilts. Once we have higher logistical |
| 211 | // complexity, this could be attempted. |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 212 | sp<IBinder> ServiceManagerShim::getService(const String16& name) const |
| 213 | { |
| 214 | static bool gSystemBootCompleted = false; |
| 215 | |
| 216 | sp<IBinder> svc = checkService(name); |
| 217 | if (svc != nullptr) return svc; |
| 218 | |
| 219 | const bool isVendorService = |
| 220 | strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0; |
| 221 | const long timeout = uptimeMillis() + 5000; |
| 222 | // Vendor code can't access system properties |
| 223 | if (!gSystemBootCompleted && !isVendorService) { |
| 224 | #ifdef __ANDROID__ |
| 225 | char bootCompleted[PROPERTY_VALUE_MAX]; |
| 226 | property_get("sys.boot_completed", bootCompleted, "0"); |
| 227 | gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false; |
| 228 | #else |
| 229 | gSystemBootCompleted = true; |
| 230 | #endif |
| 231 | } |
| 232 | // retry interval in millisecond; note that vendor services stay at 100ms |
| 233 | const long sleepTime = gSystemBootCompleted ? 1000 : 100; |
| 234 | |
| 235 | int n = 0; |
| 236 | while (uptimeMillis() < timeout) { |
| 237 | n++; |
| 238 | ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(), |
| 239 | ProcessState::self()->getDriverName().c_str()); |
| 240 | usleep(1000*sleepTime); |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 241 | |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 242 | sp<IBinder> svc = checkService(name); |
Xin Li | f11e2bd | 2018-06-08 15:11:57 -0700 | [diff] [blame] | 243 | if (svc != nullptr) return svc; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 244 | } |
| 245 | ALOGW("Service %s didn't start. Returning NULL", String8(name).string()); |
| 246 | return nullptr; |
| 247 | } |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 248 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 249 | sp<IBinder> ServiceManagerShim::checkService(const String16& name) const |
| 250 | { |
| 251 | sp<IBinder> ret; |
| 252 | if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) { |
| 253 | return nullptr; |
| 254 | } |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | status_t ServiceManagerShim::addService(const String16& name, const sp<IBinder>& service, |
| 259 | bool allowIsolated, int dumpsysPriority) |
| 260 | { |
| 261 | Status status = mTheRealServiceManager->addService( |
| 262 | String8(name).c_str(), service, allowIsolated, dumpsysPriority); |
| 263 | return status.exceptionCode(); |
| 264 | } |
| 265 | |
| 266 | Vector<String16> ServiceManagerShim::listServices(int dumpsysPriority) |
| 267 | { |
| 268 | std::vector<std::string> ret; |
| 269 | if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) { |
| 270 | return {}; |
| 271 | } |
| 272 | |
| 273 | Vector<String16> res; |
| 274 | res.setCapacity(ret.size()); |
| 275 | for (const std::string& name : ret) { |
| 276 | res.push(String16(name.c_str())); |
| 277 | } |
| 278 | return res; |
| 279 | } |
| 280 | |
| 281 | sp<IBinder> ServiceManagerShim::waitForService(const String16& name16) |
| 282 | { |
| 283 | class Waiter : public android::os::BnServiceCallback { |
| 284 | Status onRegistration(const std::string& /*name*/, |
| 285 | const sp<IBinder>& binder) override { |
| 286 | std::unique_lock<std::mutex> lock(mMutex); |
| 287 | mBinder = binder; |
| 288 | lock.unlock(); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 289 | // Flushing here helps ensure the service's ref count remains accurate |
| 290 | IPCThreadState::self()->flushCommands(); |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 291 | mCv.notify_one(); |
| 292 | return Status::ok(); |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 293 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 294 | public: |
| 295 | sp<IBinder> mBinder; |
| 296 | std::mutex mMutex; |
| 297 | std::condition_variable mCv; |
| 298 | }; |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 299 | |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 300 | // Simple RAII object to ensure a function call immediately before going out of scope |
| 301 | class Defer { |
| 302 | public: |
| 303 | Defer(std::function<void()>&& f) : mF(std::move(f)) {} |
| 304 | ~Defer() { mF(); } |
| 305 | private: |
| 306 | std::function<void()> mF; |
| 307 | }; |
| 308 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 309 | const std::string name = String8(name16).c_str(); |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 310 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 311 | sp<IBinder> out; |
| 312 | if (!mTheRealServiceManager->getService(name, &out).isOk()) { |
| 313 | return nullptr; |
| 314 | } |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 315 | if (out != nullptr) return out; |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 316 | |
| 317 | sp<Waiter> waiter = new Waiter; |
| 318 | if (!mTheRealServiceManager->registerForNotifications( |
| 319 | name, waiter).isOk()) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 320 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 322 | Defer unregister ([&] { |
| 323 | mTheRealServiceManager->unregisterForNotifications(name, waiter); |
| 324 | }); |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 325 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 326 | while(true) { |
| 327 | { |
| 328 | std::unique_lock<std::mutex> lock(waiter->mMutex); |
| 329 | using std::literals::chrono_literals::operator""s; |
| 330 | waiter->mCv.wait_for(lock, 1s, [&] { |
| 331 | return waiter->mBinder != nullptr; |
| 332 | }); |
| 333 | if (waiter->mBinder != nullptr) return waiter->mBinder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 334 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 336 | // Handle race condition for lazy services. Here is what can happen: |
| 337 | // - the service dies (not processed by init yet). |
| 338 | // - sm processes death notification. |
| 339 | // - sm gets getService and calls init to start service. |
| 340 | // - init gets the start signal, but the service already appears |
| 341 | // started, so it does nothing. |
| 342 | // - init gets death signal, but doesn't know it needs to restart |
| 343 | // the service |
| 344 | // - we need to request service again to get it to start |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 345 | if (!mTheRealServiceManager->getService(name, &out).isOk()) { |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 346 | return nullptr; |
| 347 | } |
Jon Spivack | fed6db4 | 2019-11-18 16:43:59 -0800 | [diff] [blame] | 348 | if (out != nullptr) return out; |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 349 | |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 350 | ALOGW("Waited one second for %s", name.c_str()); |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 351 | } |
Steven Moreland | 583685e | 2019-10-02 16:45:11 -0700 | [diff] [blame] | 352 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 354 | bool ServiceManagerShim::isDeclared(const String16& name) { |
| 355 | bool declared; |
| 356 | if (!mTheRealServiceManager->isDeclared(String8(name).c_str(), &declared).isOk()) { |
| 357 | return false; |
| 358 | } |
| 359 | return declared; |
| 360 | } |
| 361 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 362 | } // namespace android |