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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 23 | #ifndef __ANDROID_VNDK__ |
| 24 | #include <binder/IPermissionController.h> |
| 25 | #endif |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 26 | #include <binder/Parcel.h> |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 27 | #include <cutils/properties.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <utils/String8.h> |
| 29 | #include <utils/SystemClock.h> |
| 30 | |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame^] | 31 | #include "Static.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | sp<IServiceManager> defaultServiceManager() |
| 38 | { |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 39 | static Mutex gDefaultServiceManagerLock; |
| 40 | static sp<IServiceManager> gDefaultServiceManager; |
| 41 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 42 | if (gDefaultServiceManager != nullptr) return gDefaultServiceManager; |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 43 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | { |
| 45 | AutoMutex _l(gDefaultServiceManagerLock); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 46 | while (gDefaultServiceManager == nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | gDefaultServiceManager = interface_cast<IServiceManager>( |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 48 | ProcessState::self()->getContextObject(nullptr)); |
| 49 | if (gDefaultServiceManager == nullptr) |
Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 50 | sleep(1); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 53 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | return gDefaultServiceManager; |
| 55 | } |
| 56 | |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 57 | #ifndef __ANDROID_VNDK__ |
| 58 | // IPermissionController is not accessible to vendors |
| 59 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | bool checkCallingPermission(const String16& permission) |
| 61 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 62 | return checkCallingPermission(permission, nullptr, nullptr); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static String16 _permission("permission"); |
| 66 | |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 67 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) |
| 69 | { |
| 70 | IPCThreadState* ipcState = IPCThreadState::self(); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 71 | pid_t pid = ipcState->getCallingPid(); |
| 72 | uid_t uid = ipcState->getCallingUid(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | if (outPid) *outPid = pid; |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 74 | if (outUid) *outUid = uid; |
| 75 | return checkPermission(permission, pid, uid); |
| 76 | } |
| 77 | |
| 78 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid) |
| 79 | { |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 80 | static Mutex gPermissionControllerLock; |
| 81 | static sp<IPermissionController> gPermissionController; |
| 82 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | sp<IPermissionController> pc; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 84 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | pc = gPermissionController; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 86 | gPermissionControllerLock.unlock(); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 87 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | int64_t startTime = 0; |
| 89 | |
| 90 | while (true) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 91 | if (pc != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | bool res = pc->checkPermission(permission, pid, uid); |
| 93 | if (res) { |
| 94 | if (startTime != 0) { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 95 | 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] | 96 | (int)((uptimeMillis()-startTime)/1000), |
| 97 | String8(permission).string(), uid, pid); |
| 98 | } |
| 99 | return res; |
| 100 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 101 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | // Is this a permission failure, or did the controller go away? |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 103 | if (IInterface::asBinder(pc)->isBinderAlive()) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 104 | ALOGW("Permission failure: %s from uid=%d pid=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | String8(permission).string(), uid, pid); |
| 106 | return false; |
| 107 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 108 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | // Object is dead! |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 110 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | if (gPermissionController == pc) { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 112 | gPermissionController = nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 114 | gPermissionControllerLock.unlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | } |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 116 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | // Need to retrieve the permission controller. |
| 118 | sp<IBinder> binder = defaultServiceManager()->checkService(_permission); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 119 | if (binder == nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | // Wait for the permission controller to come back... |
| 121 | if (startTime == 0) { |
| 122 | startTime = uptimeMillis(); |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 123 | 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] | 124 | String8(permission).string(), uid, pid); |
| 125 | } |
| 126 | sleep(1); |
| 127 | } else { |
| 128 | pc = interface_cast<IPermissionController>(binder); |
Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 129 | // Install the new permission controller, and try again. |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 130 | gPermissionControllerLock.lock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | gPermissionController = pc; |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 132 | gPermissionControllerLock.unlock(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Jiyong Park | 47f876b | 2018-04-17 13:56:46 +0900 | [diff] [blame] | 137 | #endif //__ANDROID_VNDK__ |
| 138 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | // ---------------------------------------------------------------------- |
| 140 | |
| 141 | class BpServiceManager : public BpInterface<IServiceManager> |
| 142 | { |
| 143 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 144 | explicit BpServiceManager(const sp<IBinder>& impl) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | : BpInterface<IServiceManager>(impl) |
| 146 | { |
| 147 | } |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | virtual sp<IBinder> getService(const String16& name) const |
| 150 | { |
Steven Moreland | 92b17c6 | 2019-04-02 15:46:24 -0700 | [diff] [blame] | 151 | static bool gSystemBootCompleted = false; |
| 152 | |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 153 | sp<IBinder> svc = checkService(name); |
Xin Li | f11e2bd | 2018-06-08 15:11:57 -0700 | [diff] [blame] | 154 | if (svc != nullptr) return svc; |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 155 | |
| 156 | const bool isVendorService = |
| 157 | strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0; |
| 158 | const long timeout = uptimeMillis() + 5000; |
Martijn Coenen | 2d0d167 | 2018-06-25 09:40:14 +0200 | [diff] [blame] | 159 | if (!gSystemBootCompleted && !isVendorService) { |
| 160 | // Vendor code can't access system properties |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 161 | char bootCompleted[PROPERTY_VALUE_MAX]; |
| 162 | property_get("sys.boot_completed", bootCompleted, "0"); |
| 163 | gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false; |
| 164 | } |
Martijn Coenen | 2d0d167 | 2018-06-25 09:40:14 +0200 | [diff] [blame] | 165 | // retry interval in millisecond; note that vendor services stay at 100ms |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 166 | const long sleepTime = gSystemBootCompleted ? 1000 : 100; |
| 167 | |
| 168 | int n = 0; |
| 169 | while (uptimeMillis() < timeout) { |
| 170 | n++; |
Steven Moreland | ef086dd | 2018-07-06 12:43:30 -0700 | [diff] [blame] | 171 | ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(), |
| 172 | ProcessState::self()->getDriverName().c_str()); |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 173 | usleep(1000*sleepTime); |
| 174 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | sp<IBinder> svc = checkService(name); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 176 | if (svc != nullptr) return svc; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
Yunfan Chen | 788e1c4 | 2018-03-29 16:52:09 +0900 | [diff] [blame] | 178 | ALOGW("Service %s didn't start. Returning NULL", String8(name).string()); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 179 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | virtual sp<IBinder> checkService( const String16& name) const |
| 183 | { |
| 184 | Parcel data, reply; |
| 185 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); |
| 186 | data.writeString16(name); |
| 187 | remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply); |
| 188 | return reply.readStrongBinder(); |
| 189 | } |
| 190 | |
Dianne Hackborn | a94f129 | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 191 | virtual status_t addService(const String16& name, const sp<IBinder>& service, |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 192 | bool allowIsolated, int dumpsysPriority) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | Parcel data, reply; |
| 194 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); |
| 195 | data.writeString16(name); |
| 196 | data.writeStrongBinder(service); |
Dianne Hackborn | a94f129 | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 197 | data.writeInt32(allowIsolated ? 1 : 0); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 198 | data.writeInt32(dumpsysPriority); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply); |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 200 | return err == NO_ERROR ? reply.readExceptionCode() : err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 203 | virtual Vector<String16> listServices(int dumpsysPriority) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | Vector<String16> res; |
| 205 | int n = 0; |
| 206 | |
| 207 | for (;;) { |
| 208 | Parcel data, reply; |
| 209 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); |
| 210 | data.writeInt32(n++); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 211 | data.writeInt32(dumpsysPriority); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply); |
| 213 | if (err != NO_ERROR) |
| 214 | break; |
| 215 | res.add(reply.readString16()); |
| 216 | } |
| 217 | return res; |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager"); |
| 222 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | }; // namespace android |