IServiceManager.cpp revision edbf3b6af777b721cd2a1ef461947e51e88241e1
1fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera/* 2fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Copyright (C) 2005 The Android Open Source Project 3fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 4fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Licensed under the Apache License, Version 2.0 (the "License"); 5fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * you may not use this file except in compliance with the License. 6fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * You may obtain a copy of the License at 7fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 8fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * http://www.apache.org/licenses/LICENSE-2.0 9fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * 10fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * Unless required by applicable law or agreed to in writing, software 11fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * distributed under the License is distributed on an "AS IS" BASIS, 12fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * See the License for the specific language governing permissions and 14fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera * limitations under the License. 15fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera */ 16fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 17fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#define LOG_TAG "ServiceManager" 18fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 19fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#include <utils/IServiceManager.h> 2090855078eb989944bca1824058d7231cd68e5021Henrik B Andersson 2190855078eb989944bca1824058d7231cd68e5021Henrik B Andersson#include <utils/Debug.h> 2290855078eb989944bca1824058d7231cd68e5021Henrik B Andersson#include <utils/IPCThreadState.h> 23559bf2836f5da25b75bfb229fec0d20d540ee426James Dong#include <utils/Log.h> 24fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#include <utils/Parcel.h> 25fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#include <utils/String8.h> 26fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#include <utils/SystemClock.h> 27fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 28fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#include <private/utils/Static.h> 29fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 30fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera#include <unistd.h> 31fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 32fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeheranamespace android { 33fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 34fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeherasp<IServiceManager> defaultServiceManager() 35fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera{ 36fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (gDefaultServiceManager != NULL) return gDefaultServiceManager; 37fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 38fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera { 39fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera AutoMutex _l(gDefaultServiceManagerLock); 40fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (gDefaultServiceManager == NULL) { 41fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera gDefaultServiceManager = interface_cast<IServiceManager>( 42fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera ProcessState::self()->getContextObject(NULL)); 43fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 44fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera } 45fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 46fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera return gDefaultServiceManager; 47fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera} 48fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 49fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeherabool checkCallingPermission(const String16& permission) 50fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera{ 519691a73139bb36c5bf294ed13eedcb04d7f05320Iliyan Malchev return checkCallingPermission(permission, NULL, NULL); 52fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera} 53fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 54fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeherastatic String16 _permission("permission"); 55fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera 56fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbeherabool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) 57fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera{ 58fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera IPCThreadState* ipcState = IPCThreadState::self(); 59fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera int32_t pid = ipcState->getCallingPid(); 60fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera int32_t uid = ipcState->getCallingUid(); 61fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (outPid) *outPid = pid; 62fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera if (outUid) *outUid= uid; 630a32d7981b98fdfca48a1e35ca746f1cc11849d3Gloria Wang 64fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera sp<IPermissionController> pc; 65fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera gDefaultServiceManagerLock.lock(); 66fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera pc = gPermissionController; 67fdd65a0fc7df2c878cc601e4c0f4021cb264f051Pravat Dalbehera gDefaultServiceManagerLock.unlock(); 68 69 int64_t startTime = 0; 70 71 while (true) { 72 if (pc != NULL) { 73 bool res = pc->checkPermission(permission, pid, uid); 74 if (res) { 75 if (startTime != 0) { 76 LOGI("Check passed after %d seconds for %s from uid=%d pid=%d", 77 (int)((uptimeMillis()-startTime)/1000), 78 String8(permission).string(), uid, pid); 79 } 80 return res; 81 } 82 83 // Is this a permission failure, or did the controller go away? 84 if (pc->asBinder()->isBinderAlive()) { 85 LOGW("Permission failure: %s from uid=%d pid=%d", 86 String8(permission).string(), uid, pid); 87 return false; 88 } 89 90 // Object is dead! 91 gDefaultServiceManagerLock.lock(); 92 if (gPermissionController == pc) { 93 gPermissionController = NULL; 94 } 95 gDefaultServiceManagerLock.unlock(); 96 } 97 98 // Need to retrieve the permission controller. 99 sp<IBinder> binder = defaultServiceManager()->checkService(_permission); 100 if (binder == NULL) { 101 // Wait for the permission controller to come back... 102 if (startTime == 0) { 103 startTime = uptimeMillis(); 104 LOGI("Waiting to check permission %s from uid=%d pid=%d", 105 String8(permission).string(), uid, pid); 106 } 107 sleep(1); 108 } else { 109 pc = interface_cast<IPermissionController>(binder); 110 // Install the new permission controller, and try again. 111 gDefaultServiceManagerLock.lock(); 112 gPermissionController = pc; 113 gDefaultServiceManagerLock.unlock(); 114 } 115 } 116} 117 118// ---------------------------------------------------------------------- 119 120class BpServiceManager : public BpInterface<IServiceManager> 121{ 122public: 123 BpServiceManager(const sp<IBinder>& impl) 124 : BpInterface<IServiceManager>(impl) 125 { 126 } 127 128 virtual sp<IBinder> getService(const String16& name) const 129 { 130 unsigned n; 131 for (n = 0; n < 5; n++){ 132 sp<IBinder> svc = checkService(name); 133 if (svc != NULL) return svc; 134 LOGI("Waiting for sevice %s...\n", String8(name).string()); 135 sleep(1); 136 } 137 return NULL; 138 } 139 140 virtual sp<IBinder> checkService( const String16& name) const 141 { 142 Parcel data, reply; 143 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); 144 data.writeString16(name); 145 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply); 146 return reply.readStrongBinder(); 147 } 148 149 virtual status_t addService(const String16& name, const sp<IBinder>& service) 150 { 151 Parcel data, reply; 152 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); 153 data.writeString16(name); 154 data.writeStrongBinder(service); 155 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply); 156 return err == NO_ERROR ? reply.readInt32() : err; 157 } 158 159 virtual Vector<String16> listServices() 160 { 161 Vector<String16> res; 162 int n = 0; 163 164 for (;;) { 165 Parcel data, reply; 166 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); 167 data.writeInt32(n++); 168 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply); 169 if (err != NO_ERROR) 170 break; 171 res.add(reply.readString16()); 172 } 173 return res; 174 } 175}; 176 177IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager"); 178 179// ---------------------------------------------------------------------- 180 181#define CHECK_INTERFACE(interface, data, reply) \ 182 do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ 183 LOGW("Call incorrectly routed to " #interface); \ 184 return PERMISSION_DENIED; \ 185 } } while (0) 186 187status_t BnServiceManager::onTransact( 188 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 189{ 190 //printf("ServiceManager received: "); data.print(); 191 switch(code) { 192 case GET_SERVICE_TRANSACTION: { 193 CHECK_INTERFACE(IServiceManager, data, reply); 194 String16 which = data.readString16(); 195 sp<IBinder> b = const_cast<BnServiceManager*>(this)->getService(which); 196 reply->writeStrongBinder(b); 197 return NO_ERROR; 198 } break; 199 case CHECK_SERVICE_TRANSACTION: { 200 CHECK_INTERFACE(IServiceManager, data, reply); 201 String16 which = data.readString16(); 202 sp<IBinder> b = const_cast<BnServiceManager*>(this)->checkService(which); 203 reply->writeStrongBinder(b); 204 return NO_ERROR; 205 } break; 206 case ADD_SERVICE_TRANSACTION: { 207 CHECK_INTERFACE(IServiceManager, data, reply); 208 String16 which = data.readString16(); 209 sp<IBinder> b = data.readStrongBinder(); 210 status_t err = addService(which, b); 211 reply->writeInt32(err); 212 return NO_ERROR; 213 } break; 214 case LIST_SERVICES_TRANSACTION: { 215 CHECK_INTERFACE(IServiceManager, data, reply); 216 Vector<String16> list = listServices(); 217 const size_t N = list.size(); 218 reply->writeInt32(N); 219 for (size_t i=0; i<N; i++) { 220 reply->writeString16(list[i]); 221 } 222 return NO_ERROR; 223 } break; 224 default: 225 return BBinder::onTransact(code, data, reply, flags); 226 } 227} 228 229}; // namespace android 230 231