| 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 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | sp<IServiceManager> defaultServiceManager() | 
|  | 47 | { | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 48 | static Mutex gDefaultServiceManagerLock; | 
|  | 49 | static sp<IServiceManager> gDefaultServiceManager; | 
|  | 50 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 51 | if (gDefaultServiceManager != nullptr) return gDefaultServiceManager; | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 52 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | { | 
|  | 54 | AutoMutex _l(gDefaultServiceManagerLock); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 55 | while (gDefaultServiceManager == nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | gDefaultServiceManager = interface_cast<IServiceManager>( | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 57 | ProcessState::self()->getContextObject(nullptr)); | 
|  | 58 | if (gDefaultServiceManager == nullptr) | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 59 | sleep(1); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } | 
|  | 61 | } | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 62 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | return gDefaultServiceManager; | 
|  | 64 | } | 
|  | 65 |  | 
| Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 66 | #if !defined(__ANDROID_VNDK__) && defined(__ANDROID__) | 
| Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 67 | // IPermissionController is not accessible to vendors | 
|  | 68 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | bool checkCallingPermission(const String16& permission) | 
|  | 70 | { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 71 | return checkCallingPermission(permission, nullptr, nullptr); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | static String16 _permission("permission"); | 
|  | 75 |  | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 76 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) | 
|  | 78 | { | 
|  | 79 | IPCThreadState* ipcState = IPCThreadState::self(); | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 80 | pid_t pid = ipcState->getCallingPid(); | 
|  | 81 | uid_t uid = ipcState->getCallingUid(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | if (outPid) *outPid = pid; | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 83 | if (outUid) *outUid = uid; | 
|  | 84 | return checkPermission(permission, pid, uid); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid) | 
|  | 88 | { | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 89 | static Mutex gPermissionControllerLock; | 
|  | 90 | static sp<IPermissionController> gPermissionController; | 
|  | 91 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | sp<IPermissionController> pc; | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 93 | gPermissionControllerLock.lock(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | pc = gPermissionController; | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 95 | gPermissionControllerLock.unlock(); | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 96 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | int64_t startTime = 0; | 
|  | 98 |  | 
|  | 99 | while (true) { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 100 | if (pc != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | bool res = pc->checkPermission(permission, pid, uid); | 
|  | 102 | if (res) { | 
|  | 103 | if (startTime != 0) { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 104 | 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] | 105 | (int)((uptimeMillis()-startTime)/1000), | 
|  | 106 | String8(permission).string(), uid, pid); | 
|  | 107 | } | 
|  | 108 | return res; | 
|  | 109 | } | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 110 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | // Is this a permission failure, or did the controller go away? | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 112 | if (IInterface::asBinder(pc)->isBinderAlive()) { | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 113 | ALOGW("Permission failure: %s from uid=%d pid=%d", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | String8(permission).string(), uid, pid); | 
|  | 115 | return false; | 
|  | 116 | } | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 117 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | // Object is dead! | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 119 | gPermissionControllerLock.lock(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | if (gPermissionController == pc) { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 121 | gPermissionController = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | } | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 123 | gPermissionControllerLock.unlock(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | } | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 125 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | // Need to retrieve the permission controller. | 
|  | 127 | sp<IBinder> binder = defaultServiceManager()->checkService(_permission); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 128 | if (binder == nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | // Wait for the permission controller to come back... | 
|  | 130 | if (startTime == 0) { | 
|  | 131 | startTime = uptimeMillis(); | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 132 | 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] | 133 | String8(permission).string(), uid, pid); | 
|  | 134 | } | 
|  | 135 | sleep(1); | 
|  | 136 | } else { | 
|  | 137 | pc = interface_cast<IPermissionController>(binder); | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 138 | // Install the new permission controller, and try again. | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 139 | gPermissionControllerLock.lock(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | gPermissionController = pc; | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 141 | gPermissionControllerLock.unlock(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | } | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
| Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 146 | #endif //__ANDROID_VNDK__ | 
|  | 147 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | // ---------------------------------------------------------------------- | 
|  | 149 |  | 
|  | 150 | class BpServiceManager : public BpInterface<IServiceManager> | 
|  | 151 | { | 
|  | 152 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 153 | explicit BpServiceManager(const sp<IBinder>& impl) | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 154 | : BpInterface<IServiceManager>(impl), | 
|  | 155 | mTheRealServiceManager(interface_cast<AidlServiceManager>(impl)) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | { | 
|  | 157 | } | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 158 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 159 | sp<IBinder> getService(const String16& name) const override | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | { | 
| Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 161 | static bool gSystemBootCompleted = false; | 
|  | 162 |  | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 163 | sp<IBinder> svc = checkService(name); | 
| Xin Li | f11e2bd | 2018-06-08 15:11:57 -0700 | [diff] [blame] | 164 | if (svc != nullptr) return svc; | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 165 |  | 
|  | 166 | const bool isVendorService = | 
|  | 167 | strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0; | 
|  | 168 | const long timeout = uptimeMillis() + 5000; | 
| Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 169 | // Vendor code can't access system properties | 
| Martijn Coenen | 2d0d167 | 2018-06-25 09:40:14 +0200 | [diff] [blame] | 170 | if (!gSystemBootCompleted && !isVendorService) { | 
| Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 171 | #ifdef __ANDROID__ | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 172 | char bootCompleted[PROPERTY_VALUE_MAX]; | 
|  | 173 | property_get("sys.boot_completed", bootCompleted, "0"); | 
|  | 174 | gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false; | 
| Steven Moreland | ed3b563 | 2019-09-20 11:24:28 -0700 | [diff] [blame] | 175 | #else | 
|  | 176 | gSystemBootCompleted = true; | 
| Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 177 | #endif | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 178 | } | 
| Martijn Coenen | 2d0d167 | 2018-06-25 09:40:14 +0200 | [diff] [blame] | 179 | // retry interval in millisecond; note that vendor services stay at 100ms | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 180 | const long sleepTime = gSystemBootCompleted ? 1000 : 100; | 
|  | 181 |  | 
|  | 182 | int n = 0; | 
|  | 183 | while (uptimeMillis() < timeout) { | 
|  | 184 | n++; | 
| Steven Moreland | ef086dd | 2018-07-06 12:43:30 -0700 | [diff] [blame] | 185 | ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(), | 
|  | 186 | ProcessState::self()->getDriverName().c_str()); | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 187 | usleep(1000*sleepTime); | 
|  | 188 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | sp<IBinder> svc = checkService(name); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 190 | if (svc != nullptr) return svc; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } | 
| Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 192 | ALOGW("Service %s didn't start. Returning NULL", String8(name).string()); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 193 | return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 195 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 196 | sp<IBinder> checkService(const String16& name) const override { | 
|  | 197 | sp<IBinder> ret; | 
|  | 198 | if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) { | 
|  | 199 | return nullptr; | 
|  | 200 | } | 
|  | 201 | return ret; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 204 | status_t addService(const String16& name, const sp<IBinder>& service, | 
|  | 205 | bool allowIsolated, int dumpsysPriority) override { | 
|  | 206 | Status status = mTheRealServiceManager->addService(String8(name).c_str(), service, allowIsolated, dumpsysPriority); | 
|  | 207 | return status.exceptionCode(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 210 | virtual Vector<String16> listServices(int dumpsysPriority) { | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 211 | std::vector<std::string> ret; | 
|  | 212 | if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) { | 
|  | 213 | return {}; | 
|  | 214 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 216 | Vector<String16> res; | 
|  | 217 | res.setCapacity(ret.size()); | 
|  | 218 | for (const std::string& name : ret) { | 
|  | 219 | res.push(String16(name.c_str())); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } | 
|  | 221 | return res; | 
|  | 222 | } | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 223 |  | 
| Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 224 | sp<IBinder> waitForService(const String16& name16) override { | 
|  | 225 | class Waiter : public android::os::BnServiceCallback { | 
|  | 226 | Status onRegistration(const std::string& /*name*/, | 
|  | 227 | const sp<IBinder>& binder) override { | 
|  | 228 | std::unique_lock<std::mutex> lock(mMutex); | 
|  | 229 | mBinder = binder; | 
|  | 230 | lock.unlock(); | 
|  | 231 | mCv.notify_one(); | 
|  | 232 | return Status::ok(); | 
|  | 233 | } | 
|  | 234 | public: | 
|  | 235 | sp<IBinder> mBinder; | 
|  | 236 | std::mutex mMutex; | 
|  | 237 | std::condition_variable mCv; | 
|  | 238 | }; | 
|  | 239 |  | 
|  | 240 | const std::string name = String8(name16).c_str(); | 
|  | 241 |  | 
|  | 242 | sp<IBinder> out; | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 243 | if (!mTheRealServiceManager->getService(name, &out).isOk()) { | 
| Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 244 | return nullptr; | 
|  | 245 | } | 
|  | 246 | if(out != nullptr) return out; | 
|  | 247 |  | 
|  | 248 | sp<Waiter> waiter = new Waiter; | 
|  | 249 | if (!mTheRealServiceManager->registerForNotifications( | 
|  | 250 | name, waiter).isOk()) { | 
|  | 251 | return nullptr; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | while(true) { | 
|  | 255 | { | 
|  | 256 | std::unique_lock<std::mutex> lock(waiter->mMutex); | 
|  | 257 | using std::literals::chrono_literals::operator""s; | 
|  | 258 | waiter->mCv.wait_for(lock, 1s, [&] { | 
|  | 259 | return waiter->mBinder != nullptr; | 
|  | 260 | }); | 
|  | 261 | if (waiter->mBinder != nullptr) return waiter->mBinder; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | // Handle race condition for lazy services. Here is what can happen: | 
|  | 265 | // - the service dies (not processed by init yet). | 
|  | 266 | // - sm processes death notification. | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 267 | // - sm gets getService and calls init to start service. | 
| Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 268 | // - init gets the start signal, but the service already appears | 
|  | 269 | //   started, so it does nothing. | 
|  | 270 | // - init gets death signal, but doesn't know it needs to restart | 
|  | 271 | //   the service | 
|  | 272 | // - we need to request service again to get it to start | 
| Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 273 | if (!mTheRealServiceManager->getService(name, &out).isOk()) { | 
| Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 274 | return nullptr; | 
|  | 275 | } | 
|  | 276 | if(out != nullptr) return out; | 
|  | 277 |  | 
|  | 278 | ALOGW("Waited one second for %s", name.c_str()); | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 |  | 
| Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 282 | private: | 
|  | 283 | sp<AidlServiceManager> mTheRealServiceManager; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | }; | 
|  | 285 |  | 
|  | 286 | IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager"); | 
|  | 287 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | }; // namespace android |