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