| 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> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/Parcel.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/String8.h> | 
|  | 25 | #include <utils/SystemClock.h> | 
| Iliyan Malchev | 273e144 | 2017-04-10 14:09:04 -0700 | [diff] [blame] | 26 | #include <utils/CallStack.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 |  | 
| Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 28 | #include <private/binder/Static.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | #include <unistd.h> | 
|  | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 |  | 
|  | 34 | sp<IServiceManager> defaultServiceManager() | 
|  | 35 | { | 
|  | 36 | if (gDefaultServiceManager != NULL) return gDefaultServiceManager; | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 37 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | { | 
|  | 39 | AutoMutex _l(gDefaultServiceManagerLock); | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 40 | while (gDefaultServiceManager == NULL) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | gDefaultServiceManager = interface_cast<IServiceManager>( | 
|  | 42 | ProcessState::self()->getContextObject(NULL)); | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 43 | if (gDefaultServiceManager == NULL) | 
|  | 44 | sleep(1); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | } | 
|  | 46 | } | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 47 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | return gDefaultServiceManager; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | bool checkCallingPermission(const String16& permission) | 
|  | 52 | { | 
|  | 53 | return checkCallingPermission(permission, NULL, NULL); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | static String16 _permission("permission"); | 
|  | 57 |  | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 58 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) | 
|  | 60 | { | 
|  | 61 | IPCThreadState* ipcState = IPCThreadState::self(); | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 62 | pid_t pid = ipcState->getCallingPid(); | 
|  | 63 | uid_t uid = ipcState->getCallingUid(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | if (outPid) *outPid = pid; | 
| Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 65 | if (outUid) *outUid = uid; | 
|  | 66 | return checkPermission(permission, pid, uid); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | bool checkPermission(const String16& permission, pid_t pid, uid_t uid) | 
|  | 70 | { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | sp<IPermissionController> pc; | 
|  | 72 | gDefaultServiceManagerLock.lock(); | 
|  | 73 | pc = gPermissionController; | 
|  | 74 | gDefaultServiceManagerLock.unlock(); | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 75 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | int64_t startTime = 0; | 
|  | 77 |  | 
|  | 78 | while (true) { | 
|  | 79 | if (pc != NULL) { | 
|  | 80 | bool res = pc->checkPermission(permission, pid, uid); | 
|  | 81 | if (res) { | 
|  | 82 | if (startTime != 0) { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 83 | 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] | 84 | (int)((uptimeMillis()-startTime)/1000), | 
|  | 85 | String8(permission).string(), uid, pid); | 
|  | 86 | } | 
|  | 87 | return res; | 
|  | 88 | } | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 89 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | // Is this a permission failure, or did the controller go away? | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 91 | if (IInterface::asBinder(pc)->isBinderAlive()) { | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 92 | ALOGW("Permission failure: %s from uid=%d pid=%d", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | String8(permission).string(), uid, pid); | 
|  | 94 | return false; | 
|  | 95 | } | 
| 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 | // Object is dead! | 
|  | 98 | gDefaultServiceManagerLock.lock(); | 
|  | 99 | if (gPermissionController == pc) { | 
|  | 100 | gPermissionController = NULL; | 
|  | 101 | } | 
|  | 102 | gDefaultServiceManagerLock.unlock(); | 
|  | 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 | // Need to retrieve the permission controller. | 
|  | 106 | sp<IBinder> binder = defaultServiceManager()->checkService(_permission); | 
|  | 107 | if (binder == NULL) { | 
|  | 108 | // Wait for the permission controller to come back... | 
|  | 109 | if (startTime == 0) { | 
|  | 110 | startTime = uptimeMillis(); | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 111 | 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] | 112 | String8(permission).string(), uid, pid); | 
|  | 113 | } | 
|  | 114 | sleep(1); | 
|  | 115 | } else { | 
|  | 116 | pc = interface_cast<IPermissionController>(binder); | 
| Daniel Erat | c283270 | 2015-10-13 15:29:32 -0600 | [diff] [blame] | 117 | // Install the new permission controller, and try again. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | gDefaultServiceManagerLock.lock(); | 
|  | 119 | gPermissionController = pc; | 
|  | 120 | gDefaultServiceManagerLock.unlock(); | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // ---------------------------------------------------------------------- | 
|  | 126 |  | 
|  | 127 | class BpServiceManager : public BpInterface<IServiceManager> | 
|  | 128 | { | 
|  | 129 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 130 | explicit BpServiceManager(const sp<IBinder>& impl) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | : BpInterface<IServiceManager>(impl) | 
|  | 132 | { | 
|  | 133 | } | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 134 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | virtual sp<IBinder> getService(const String16& name) const | 
|  | 136 | { | 
|  | 137 | unsigned n; | 
|  | 138 | for (n = 0; n < 5; n++){ | 
| Andy Hung | 6456e44 | 2016-12-06 09:40:01 -0800 | [diff] [blame] | 139 | if (n > 0) { | 
| Iliyan Malchev | 273e144 | 2017-04-10 14:09:04 -0700 | [diff] [blame] | 140 | if (!strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder")) { | 
|  | 141 | ALOGI("Waiting for vendor service %s...", String8(name).string()); | 
|  | 142 | CallStack stack(LOG_TAG); | 
|  | 143 | } else { | 
|  | 144 | ALOGI("Waiting for service %s...", String8(name).string()); | 
|  | 145 | } | 
| Andy Hung | 6456e44 | 2016-12-06 09:40:01 -0800 | [diff] [blame] | 146 | sleep(1); | 
|  | 147 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | sp<IBinder> svc = checkService(name); | 
|  | 149 | if (svc != NULL) return svc; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } | 
|  | 151 | return NULL; | 
|  | 152 | } | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 153 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | virtual sp<IBinder> checkService( const String16& name) const | 
|  | 155 | { | 
|  | 156 | Parcel data, reply; | 
|  | 157 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); | 
|  | 158 | data.writeString16(name); | 
|  | 159 | remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply); | 
|  | 160 | return reply.readStrongBinder(); | 
|  | 161 | } | 
|  | 162 |  | 
| Dianne Hackborn | a94f129 | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 163 | virtual status_t addService(const String16& name, const sp<IBinder>& service, | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 164 | bool allowIsolated, int dumpsysPriority) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | Parcel data, reply; | 
|  | 166 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); | 
|  | 167 | data.writeString16(name); | 
|  | 168 | data.writeStrongBinder(service); | 
| Dianne Hackborn | a94f129 | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 169 | data.writeInt32(allowIsolated ? 1 : 0); | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 170 | data.writeInt32(dumpsysPriority); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply); | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 172 | return err == NO_ERROR ? reply.readExceptionCode() : err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 175 | virtual Vector<String16> listServices(int dumpsysPriority) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | Vector<String16> res; | 
|  | 177 | int n = 0; | 
|  | 178 |  | 
|  | 179 | for (;;) { | 
|  | 180 | Parcel data, reply; | 
|  | 181 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); | 
|  | 182 | data.writeInt32(n++); | 
| Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 183 | data.writeInt32(dumpsysPriority); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply); | 
|  | 185 | if (err != NO_ERROR) | 
|  | 186 | break; | 
|  | 187 | res.add(reply.readString16()); | 
|  | 188 | } | 
|  | 189 | return res; | 
|  | 190 | } | 
|  | 191 | }; | 
|  | 192 |  | 
|  | 193 | IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager"); | 
|  | 194 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | }; // namespace android |