IServiceManager.cpp revision 83c0446f27b9542d6c2e724817b2b2d8d1f55085
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 19#include <binder/IServiceManager.h> 20 21#include <utils/Debug.h> 22#include <binder/IPCThreadState.h> 23#include <utils/Log.h> 24#include <binder/Parcel.h> 25#include <utils/String8.h> 26#include <utils/SystemClock.h> 27 28#include <private/binder/Static.h> 29 30#include <unistd.h> 31 32namespace android { 33 34sp<IServiceManager> defaultServiceManager() 35{ 36 if (gDefaultServiceManager != NULL) return gDefaultServiceManager; 37 38 { 39 AutoMutex _l(gDefaultServiceManagerLock); 40 if (gDefaultServiceManager == NULL) { 41 gDefaultServiceManager = interface_cast<IServiceManager>( 42 ProcessState::self()->getContextObject(NULL)); 43 } 44 } 45 46 return gDefaultServiceManager; 47} 48 49bool checkCallingPermission(const String16& permission) 50{ 51 return checkCallingPermission(permission, NULL, NULL); 52} 53 54static String16 _permission("permission"); 55 56bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid) 57{ 58 IPCThreadState* ipcState = IPCThreadState::self(); 59 int32_t pid = ipcState->getCallingPid(); 60 int32_t uid = ipcState->getCallingUid(); 61 if (outPid) *outPid = pid; 62 if (outUid) *outUid= uid; 63 64 sp<IPermissionController> pc; 65 gDefaultServiceManagerLock.lock(); 66 pc = gPermissionController; 67 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 181status_t BnServiceManager::onTransact( 182 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 183{ 184 //printf("ServiceManager received: "); data.print(); 185 switch(code) { 186 case GET_SERVICE_TRANSACTION: { 187 CHECK_INTERFACE(IServiceManager, data, reply); 188 String16 which = data.readString16(); 189 sp<IBinder> b = const_cast<BnServiceManager*>(this)->getService(which); 190 reply->writeStrongBinder(b); 191 return NO_ERROR; 192 } break; 193 case CHECK_SERVICE_TRANSACTION: { 194 CHECK_INTERFACE(IServiceManager, data, reply); 195 String16 which = data.readString16(); 196 sp<IBinder> b = const_cast<BnServiceManager*>(this)->checkService(which); 197 reply->writeStrongBinder(b); 198 return NO_ERROR; 199 } break; 200 case ADD_SERVICE_TRANSACTION: { 201 CHECK_INTERFACE(IServiceManager, data, reply); 202 String16 which = data.readString16(); 203 sp<IBinder> b = data.readStrongBinder(); 204 status_t err = addService(which, b); 205 reply->writeInt32(err); 206 return NO_ERROR; 207 } break; 208 case LIST_SERVICES_TRANSACTION: { 209 CHECK_INTERFACE(IServiceManager, data, reply); 210 Vector<String16> list = listServices(); 211 const size_t N = list.size(); 212 reply->writeInt32(N); 213 for (size_t i=0; i<N; i++) { 214 reply->writeString16(list[i]); 215 } 216 return NO_ERROR; 217 } break; 218 default: 219 return BBinder::onTransact(code, data, reply, flags); 220 } 221} 222 223}; // namespace android 224 225