2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "ServiceManager"
|
|
|
|
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <utils/Log.h>
|
2009-06-16 01:24:59 +00:00
|
|
|
#include <binder/IPCThreadState.h>
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/Parcel.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <utils/String8.h>
|
|
|
|
#include <utils/SystemClock.h>
|
|
|
|
|
2009-05-18 22:08:03 +00:00
|
|
|
#include <private/binder/Static.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
sp<IServiceManager> defaultServiceManager()
|
|
|
|
{
|
|
|
|
if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
|
|
|
|
|
|
|
|
{
|
|
|
|
AutoMutex _l(gDefaultServiceManagerLock);
|
2013-06-19 00:25:37 +00:00
|
|
|
while (gDefaultServiceManager == NULL) {
|
2009-03-04 03:31:44 +00:00
|
|
|
gDefaultServiceManager = interface_cast<IServiceManager>(
|
|
|
|
ProcessState::self()->getContextObject(NULL));
|
2013-06-19 00:25:37 +00:00
|
|
|
if (gDefaultServiceManager == NULL)
|
|
|
|
sleep(1);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gDefaultServiceManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkCallingPermission(const String16& permission)
|
|
|
|
{
|
|
|
|
return checkCallingPermission(permission, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String16 _permission("permission");
|
|
|
|
|
2009-06-16 01:24:59 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
|
|
|
|
{
|
|
|
|
IPCThreadState* ipcState = IPCThreadState::self();
|
2009-06-16 01:24:59 +00:00
|
|
|
pid_t pid = ipcState->getCallingPid();
|
|
|
|
uid_t uid = ipcState->getCallingUid();
|
2009-03-04 03:31:44 +00:00
|
|
|
if (outPid) *outPid = pid;
|
2009-06-16 01:24:59 +00:00
|
|
|
if (outUid) *outUid = uid;
|
|
|
|
return checkPermission(permission, pid, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
|
|
|
|
{
|
2009-03-04 03:31:44 +00:00
|
|
|
sp<IPermissionController> pc;
|
|
|
|
gDefaultServiceManagerLock.lock();
|
|
|
|
pc = gPermissionController;
|
|
|
|
gDefaultServiceManagerLock.unlock();
|
|
|
|
|
|
|
|
int64_t startTime = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (pc != NULL) {
|
|
|
|
bool res = pc->checkPermission(permission, pid, uid);
|
|
|
|
if (res) {
|
|
|
|
if (startTime != 0) {
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
|
2009-03-04 03:31:44 +00:00
|
|
|
(int)((uptimeMillis()-startTime)/1000),
|
|
|
|
String8(permission).string(), uid, pid);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a permission failure, or did the controller go away?
|
2014-11-14 16:01:01 +00:00
|
|
|
if (IInterface::asBinder(pc)->isBinderAlive()) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Permission failure: %s from uid=%d pid=%d",
|
2009-03-04 03:31:44 +00:00
|
|
|
String8(permission).string(), uid, pid);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object is dead!
|
|
|
|
gDefaultServiceManagerLock.lock();
|
|
|
|
if (gPermissionController == pc) {
|
|
|
|
gPermissionController = NULL;
|
|
|
|
}
|
|
|
|
gDefaultServiceManagerLock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to retrieve the permission controller.
|
|
|
|
sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
|
|
|
|
if (binder == NULL) {
|
|
|
|
// Wait for the permission controller to come back...
|
|
|
|
if (startTime == 0) {
|
|
|
|
startTime = uptimeMillis();
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("Waiting to check permission %s from uid=%d pid=%d",
|
2009-03-04 03:31:44 +00:00
|
|
|
String8(permission).string(), uid, pid);
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
} else {
|
|
|
|
pc = interface_cast<IPermissionController>(binder);
|
|
|
|
// Install the new permission controller, and try again.
|
|
|
|
gDefaultServiceManagerLock.lock();
|
|
|
|
gPermissionController = pc;
|
|
|
|
gDefaultServiceManagerLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BpServiceManager : public BpInterface<IServiceManager>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BpServiceManager(const sp<IBinder>& impl)
|
|
|
|
: BpInterface<IServiceManager>(impl)
|
|
|
|
{
|
|
|
|
}
|
2010-06-18 20:07:53 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
virtual sp<IBinder> getService(const String16& name) const
|
|
|
|
{
|
|
|
|
unsigned n;
|
|
|
|
for (n = 0; n < 5; n++){
|
|
|
|
sp<IBinder> svc = checkService(name);
|
|
|
|
if (svc != NULL) return svc;
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("Waiting for service %s...\n", String8(name).string());
|
2009-03-04 03:31:44 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-18 20:07:53 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
virtual sp<IBinder> checkService( const String16& name) const
|
|
|
|
{
|
|
|
|
Parcel data, reply;
|
|
|
|
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
|
|
|
|
data.writeString16(name);
|
|
|
|
remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
|
|
|
|
return reply.readStrongBinder();
|
|
|
|
}
|
|
|
|
|
2012-02-10 00:12:18 +00:00
|
|
|
virtual status_t addService(const String16& name, const sp<IBinder>& service,
|
|
|
|
bool allowIsolated)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
Parcel data, reply;
|
|
|
|
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
|
|
|
|
data.writeString16(name);
|
|
|
|
data.writeStrongBinder(service);
|
2012-02-10 00:12:18 +00:00
|
|
|
data.writeInt32(allowIsolated ? 1 : 0);
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
|
Add Parcel::readExceptionCode() and Parcel::writeNoException()
Add native Parcel methods analogous to the Java versions.
Currently, these don't do much, but upcoming StrictMode work changes
the RPC calling conventions in some cases, so it's important that
everybody uses these consistently, rather than having a lot of code
trying to parse RPC responses out of Parcels themselves.
As a summary, the current convention that Java Binder services use is
to prepend the reply Parcel with an int32 signaling the exception
status:
0: no exception
-1: Security exception
-2: Bad Parcelable
-3: ...
-4: ...
-5: ...
... followed by Parceled String if the exception code is non-zero.
With an upcoming change, it'll be the case that a response Parcel can,
non-exceptionally return rich data in the header, and also return data
to the caller. The important thing to note in this new case is that
the first int32 in the reply parcel *will not be zero*, so anybody
manually checking for it with reply.readInt32() will get false
negative failures.
Short summary: If you're calling into a Java service and manually
checking the exception status with reply.readInt32(), change it to
reply.readExceptionCode().
Change-Id: I23f9a0e53a8cfbbd9759242cfde16723641afe04
2010-07-13 22:33:35 +00:00
|
|
|
return err == NO_ERROR ? reply.readExceptionCode() : err;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Vector<String16> listServices()
|
|
|
|
{
|
|
|
|
Vector<String16> res;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
Parcel data, reply;
|
|
|
|
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
|
|
|
|
data.writeInt32(n++);
|
|
|
|
status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
|
|
|
|
if (err != NO_ERROR)
|
|
|
|
break;
|
|
|
|
res.add(reply.readString16());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
status_t BnServiceManager::onTransact(
|
|
|
|
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
|
|
|
|
{
|
|
|
|
//printf("ServiceManager received: "); data.print();
|
|
|
|
switch(code) {
|
|
|
|
case GET_SERVICE_TRANSACTION: {
|
|
|
|
CHECK_INTERFACE(IServiceManager, data, reply);
|
|
|
|
String16 which = data.readString16();
|
|
|
|
sp<IBinder> b = const_cast<BnServiceManager*>(this)->getService(which);
|
|
|
|
reply->writeStrongBinder(b);
|
|
|
|
return NO_ERROR;
|
|
|
|
} break;
|
|
|
|
case CHECK_SERVICE_TRANSACTION: {
|
|
|
|
CHECK_INTERFACE(IServiceManager, data, reply);
|
|
|
|
String16 which = data.readString16();
|
|
|
|
sp<IBinder> b = const_cast<BnServiceManager*>(this)->checkService(which);
|
|
|
|
reply->writeStrongBinder(b);
|
|
|
|
return NO_ERROR;
|
|
|
|
} break;
|
|
|
|
case ADD_SERVICE_TRANSACTION: {
|
|
|
|
CHECK_INTERFACE(IServiceManager, data, reply);
|
|
|
|
String16 which = data.readString16();
|
|
|
|
sp<IBinder> b = data.readStrongBinder();
|
|
|
|
status_t err = addService(which, b);
|
|
|
|
reply->writeInt32(err);
|
|
|
|
return NO_ERROR;
|
|
|
|
} break;
|
|
|
|
case LIST_SERVICES_TRANSACTION: {
|
|
|
|
CHECK_INTERFACE(IServiceManager, data, reply);
|
|
|
|
Vector<String16> list = listServices();
|
|
|
|
const size_t N = list.size();
|
|
|
|
reply->writeInt32(N);
|
|
|
|
for (size_t i=0; i<N; i++) {
|
|
|
|
reply->writeString16(list[i]);
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
return BBinder::onTransact(code, data, reply, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|