blob: 1750640fc1d94e870b1b7a2ad5fb0e35f1214780 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
21#include <utils/Debug.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Log.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070023#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/String8.h>
26#include <utils/SystemClock.h>
27
Mathias Agopian208059f2009-05-18 15:08:03 -070028#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
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
Mathias Agopian375f5632009-06-15 18:24:59 -070056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
58{
59 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070060 pid_t pid = ipcState->getCallingPid();
61 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070063 if (outUid) *outUid = uid;
64 return checkPermission(permission, pid, uid);
65}
66
67bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
68{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 sp<IPermissionController> pc;
70 gDefaultServiceManagerLock.lock();
71 pc = gPermissionController;
72 gDefaultServiceManagerLock.unlock();
73
74 int64_t startTime = 0;
75
76 while (true) {
77 if (pc != NULL) {
78 bool res = pc->checkPermission(permission, pid, uid);
79 if (res) {
80 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +000081 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 (int)((uptimeMillis()-startTime)/1000),
83 String8(permission).string(), uid, pid);
84 }
85 return res;
86 }
87
88 // Is this a permission failure, or did the controller go away?
89 if (pc->asBinder()->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +000090 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 String8(permission).string(), uid, pid);
92 return false;
93 }
94
95 // Object is dead!
96 gDefaultServiceManagerLock.lock();
97 if (gPermissionController == pc) {
98 gPermissionController = NULL;
99 }
100 gDefaultServiceManagerLock.unlock();
101 }
102
103 // Need to retrieve the permission controller.
104 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
105 if (binder == NULL) {
106 // Wait for the permission controller to come back...
107 if (startTime == 0) {
108 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000109 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 String8(permission).string(), uid, pid);
111 }
112 sleep(1);
113 } else {
114 pc = interface_cast<IPermissionController>(binder);
115 // Install the new permission controller, and try again.
116 gDefaultServiceManagerLock.lock();
117 gPermissionController = pc;
118 gDefaultServiceManagerLock.unlock();
119 }
120 }
121}
122
123// ----------------------------------------------------------------------
124
125class BpServiceManager : public BpInterface<IServiceManager>
126{
127public:
128 BpServiceManager(const sp<IBinder>& impl)
129 : BpInterface<IServiceManager>(impl)
130 {
131 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 virtual sp<IBinder> getService(const String16& name) const
134 {
135 unsigned n;
136 for (n = 0; n < 5; n++){
137 sp<IBinder> svc = checkService(name);
138 if (svc != NULL) return svc;
Steve Blocka19954a2012-01-04 20:05:49 +0000139 ALOGI("Waiting for service %s...\n", String8(name).string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 sleep(1);
141 }
142 return NULL;
143 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 virtual sp<IBinder> checkService( const String16& name) const
146 {
147 Parcel data, reply;
148 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
149 data.writeString16(name);
150 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
151 return reply.readStrongBinder();
152 }
153
Dianne Hackborna94f1292012-02-09 16:12:18 -0800154 virtual status_t addService(const String16& name, const sp<IBinder>& service,
155 bool allowIsolated)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 {
157 Parcel data, reply;
158 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
159 data.writeString16(name);
160 data.writeStrongBinder(service);
Dianne Hackborna94f1292012-02-09 16:12:18 -0800161 data.writeInt32(allowIsolated ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700163 return err == NO_ERROR ? reply.readExceptionCode() : err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 }
165
166 virtual Vector<String16> listServices()
167 {
168 Vector<String16> res;
169 int n = 0;
170
171 for (;;) {
172 Parcel data, reply;
173 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
174 data.writeInt32(n++);
175 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
176 if (err != NO_ERROR)
177 break;
178 res.add(reply.readString16());
179 }
180 return res;
181 }
182};
183
184IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
185
186// ----------------------------------------------------------------------
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188status_t BnServiceManager::onTransact(
189 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
190{
191 //printf("ServiceManager received: "); data.print();
192 switch(code) {
193 case GET_SERVICE_TRANSACTION: {
194 CHECK_INTERFACE(IServiceManager, data, reply);
195 String16 which = data.readString16();
196 sp<IBinder> b = const_cast<BnServiceManager*>(this)->getService(which);
197 reply->writeStrongBinder(b);
198 return NO_ERROR;
199 } break;
200 case CHECK_SERVICE_TRANSACTION: {
201 CHECK_INTERFACE(IServiceManager, data, reply);
202 String16 which = data.readString16();
203 sp<IBinder> b = const_cast<BnServiceManager*>(this)->checkService(which);
204 reply->writeStrongBinder(b);
205 return NO_ERROR;
206 } break;
207 case ADD_SERVICE_TRANSACTION: {
208 CHECK_INTERFACE(IServiceManager, data, reply);
209 String16 which = data.readString16();
210 sp<IBinder> b = data.readStrongBinder();
211 status_t err = addService(which, b);
212 reply->writeInt32(err);
213 return NO_ERROR;
214 } break;
215 case LIST_SERVICES_TRANSACTION: {
216 CHECK_INTERFACE(IServiceManager, data, reply);
217 Vector<String16> list = listServices();
218 const size_t N = list.size();
219 reply->writeInt32(N);
220 for (size_t i=0; i<N; i++) {
221 reply->writeString16(list[i]);
222 }
223 return NO_ERROR;
224 } break;
225 default:
226 return BBinder::onTransact(code, data, reply, flags);
227 }
228}
229
230}; // namespace android