Android进程间通信(四)——系统服务注册到servicemanager源码分析中包含了ServerManager服务获取servicemanager并注册系统服务的流程。这个流程中包含了Java层获取servicemanager服务的流程,常见的还有native层获取servicemanager,与Java层有所不同,本篇文章着重介绍native层获取servicemanager的流程。native层获取servicemanager的方法为IServiceManager.cpp的defaultServiceManager方法,源码github地址
sp<IServiceManager> defaultServiceManager()
{
if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
{
AutoMutex _l(gDefaultServiceManagerLock);
while (gDefaultServiceManager == NULL) {
gDefaultServiceManager = interface_cast<IServiceManager>(
ProcessState::self()->getContextObject(NULL));
if (gDefaultServiceManager == NULL)
sleep(1);
}
}
return gDefaultServiceManager;
}
ProcessState::self()->getContextObject(NULL)这个之前我们已经介绍过,返回的是一个BpBinder(0),注意这个方法是个单例,用while循环是防止servicemanager还没有注册完成。
1.ProcessState::self()->getContextObject(NULL)
- ProcessState::self()
a.打开binder驱动
b.设置最大线程数15
c.mmap设置共享内存
2.getContextObject
- 创建一个BpBinder对象
3.interface_cast<IServiceManager>
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
return INTERFACE::asInterface(obj);
}
IServiceManager替换INTERFACE,也就是调用IServiceManager.asInterface,但我们在IServiceManager中式找不到这个方法的,其实这个asInterface式通过define定义的,实际我们就是把IServiceManager替换宏定义中的INTERFACE
#define DECLARE_META_INTERFACE(INTERFACE) \
static const android::String16 descriptor; \
static android::sp<I##INTERFACE> asInterface( \
const android::sp<android::IBinder>& obj); \
virtual const android::String16& getInterfaceDescriptor() const; \
I##INTERFACE(); \
virtual ~I##INTERFACE(); \
#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
const android::String16 I##INTERFACE::descriptor(NAME); \
const android::String16& \
I##INTERFACE::getInterfaceDescriptor() const { \
return I##INTERFACE::descriptor; \
} \
android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<I##INTERFACE> intr; \
if (obj != NULL) { \
intr = static_cast<I##INTERFACE*>( \
obj->queryLocalInterface( \
I##INTERFACE::descriptor).get()); \
if (intr == NULL) { \
intr = new Bp##INTERFACE(obj); \
} \
} \
return intr; \
} \
I##INTERFACE::I##INTERFACE() { } \
I##INTERFACE::~I##INTERFACE() { } \
带入展开后,其实这个只是声明,类似Java中的接口
static const android::String16 descriptor; \
static android::sp<IServiceManager> asInterface( \
const android::sp<android::IBinder>& obj); \
virtual const android::String16& getInterfaceDescriptor() const; \
IServiceManager(); \
virtual ~IServiceManager(); \
实现代码为
const android::String16 IServiceManager::descriptor(NAME); \
const android::String16& \
IServiceManager::getInterfaceDescriptor() const { \
return IServiceManager::descriptor; \
} \
android::sp<IServiceManager> IServiceManager::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<IServiceManager> intr; \
if (obj != NULL) { \
intr = static_cast<IServiceManager*>( \
obj->queryLocalInterface( \
IServiceManager::descriptor).get()); \
if (intr == NULL) { \
intr = new BpServiceManager(obj); \
} \
} \
return intr; \
} \
IServiceManager::IServiceManager() { } \
IServiceManager::~IServiceManagerE() { } \
所以 asInterface 的返回为 new BpServiceManager(BpBinder(0))
class BpServiceManager : public BpInterface<IServiceManager>
{
public:
BpServiceManager(const sp<IBinder>& impl)
: BpInterface<IServiceManager>(impl)
{
}
virtual status_t addService(const String16& name, const sp<IBinder>& service,
bool allowIsolated)
{
Parcel data, reply;
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
data.writeString16(name);
data.writeStrongBinder(service);
data.writeInt32(allowIsolated ? 1 : 0);
status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);//实际还是会调用BpBinder
return err == NO_ERROR ? reply.readExceptionCode() : err;
}
......
总结:Nativce层和Java层作为客户端获取servicemanager流程有所不同,但最终都是对BpBinder的封装