Java Binder要想和Native Binder进行通信,需要通过JNI
JNI注册 Java Binder要想和Native Binder进行通信,需要通过JNI,JNI各种函数是在Zygote进程启动过程中注册的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 void AndroidRuntime::start(){ ...... if (startVm(&mJavaVM, &env, zygote) != 0 ) { return ; } onVmCreated(env); if (startReg(env) < 0 ) { ALOGE("Unable to register all android natives\n" ); return ; } ...... } int AndroidRuntime::startReg(JNIEnv* env){ ...... if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0 ) { env->PopLocalFrame(NULL); return -1 ; } ...... return 0 ; } void AndroidRuntime::onVmCreated(){}static const RegJNIRec gRegJNI[] = { REG_JNI(register_com_android_internal_os_RuntimeInit), REG_JNI(register_com_android_internal_os_ZygoteInit_nativeZygoteInit), ...... REG_JNI(register_android_os_Binder), ...... } int register_android_os_Binder (JNIEnv* env) { if (int_register_android_os_Binder(env) < 0 ) return -1 ; if (int_register_android_os_BinderInternal(env) < 0 ) return -1 ; if (int_register_android_os_BinderProxy(env) < 0 ) return -1 ; ...... return 0 ; }
注册Binder类
注册BinderInternal类
注册BinderProxy类
Binder类的注册(BinderInternal类的注册 类似) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 static const JNINativeMethod gBinderMethods[] = { { "getCallingPid" , "()I" , (void *)android_os_Binder_getCallingPid }, { "getCallingUid" , "()I" , (void *)android_os_Binder_getCallingUid }, ...... } const char * const kBinderPathName = "android/os/Binder" ; static int int_register_android_os_Binder (JNIEnv* env) { jclass clazz = FindClassOrDie(env, kBinderPathName); gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz); gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact" , "(IJJI)Z" ); gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject" , "J" ); return RegisterMethodsOrDie( env, kBinderPathName, gBinderMethods, NELEM(gBinderMethods)); } static struct bindernative_offsets_t{ jclass mClass; jmethodID mExecTransact; jfieldID mObject; } gBinderOffsets;
AMS注册 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public static void main (String[] args) {new SystemServer ().run();}private void run () { …… startBootstrapServices(); …… } private void startBootstrapServices () { …… mActivityManagerService.setSystemProcess(); …… } public class ActivityManagerService extends IActivityManager .Stubpublic void setSystemProcess () { …… ServiceManager.addService(Context.ACTIVITY_SERVICE, this , ……); …… } public static void addService (String name, IBinder service) { addService(name, service, false , IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT); } public static void addService (String name, IBinder service, boolean allowIsolated) { addService(name, service, allowIsolated, IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT); } public static void addService (String name, IBinder service, boolean allowIsolated, int dumpPriority) { try { getIServiceManager().addService(name, service, allowIsolated, dumpPriority); } catch (RemoteException e) { Log.e(TAG, "error in addService" , e); } } private static IServiceManager getIServiceManager () { if (sServiceManager != null ) {return sServiceManager;} sServiceManager = ServiceManagerNative .asInterface(Binder.allowBlocking(BinderInternal.getContextObject())); return sServiceManager; }
先处理getIServiceManager中的逻辑:
BinderInternal.getContextObject()
Binder.allowBlocking()
ServiceManagerNative.asInterface()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public static final native IBinder getContextObject () ;static jobject android_os_BinderInternal_getContextObject (JNIEnv* env, jobject clazz) { sp<IBinder> b = ProcessState::self()->getContextObject(NULL); return javaObjectForIBinder(env, b); } sp<IBinder> ProcessState::getContextObject(const sp<IBinder>&){ return getStrongProxyForHandle(0 ); } sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle){ …… if (b == nullptr || !e->refs->attemptIncWeak(this )) { …… b = BpBinder::create(handle); …… } …… }
得出ProcessState.getContextObject()返回BpBinder对象
javaObjectForIBinder(env, b)返回一个BinderProxy对象,其中mObject(BinderProxy成员变量)记录了BpBinder对象
得出BinderInternal.getContextObject()最终为BinderProxy对象(Java Binder的客户端的代表)
Binder.allowBlocking的作用是将BinderProxy的sWarnOnBlocking值置为false
接着分析 ServiceManagerNative.asInterface() 1 2 3 4 5 6 7 8 9 10 11 sServiceManager = ServiceManagerNative .asInterface(Binder.allowBlocking(BinderInternal.getContextObject())); static public IServiceManager asInterface (IBinder obj) { if (obj == null ) {return null ;} IServiceManager in = (IServiceManager)obj.queryLocalInterface(descriptor); if (in != null ) {return in;} return new ServiceManagerProxy (obj); }
可以得出: sServiceManager(getIServiceManager())是ServiceManagerProxy对象,其中有addService方法
小结: 1 2 3 4 Jave Binder 客户端的代表:BinderProxy Native Binder 客户端的代表:BpBinder Java:ServiceManagerProxy通过BinderProxy来实现通信 Native:BpServiceManager通过BpBinder来实现通信
接着分析addService() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class ServiceManagerProxy implements IServiceManager { public ServiceManagerProxy (IBinder remote) { mRemote = remote; } public void addService (String name, IBinder service, boolean allowIsolated, int dumpPriority) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IServiceManager.descriptor); data.writeString(name); data.writeStrongBinder(service); data.writeInt(allowIsolated ? 1 : 0 ); data.writeInt(dumpPriority); mRemote.transact(ADD_SERVICE_TRANSACTION, data, reply, 0 ); reply.recycle(); data.recycle(); } } static jboolean android_os_BinderProxy_transact () { ...... Parcel* data = parcelForJavaObject(env, dataObj); ...... Parcel* reply = parcelForJavaObject(env, replyObj); ...... IBinder* target = getBPNativeData(env, obj)->mObject.get(); ...... status_t err = target->transact(code, *data, reply, flags); }
可以看出进行了 Framework -> JNI -> Native 这个过程
接下来可参考【Binder之系统服务注册】一文,执行ioctl,像Binder驱动发送数据
分析 data.writeStrongBinder(service); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public final void writeStrongBinder (IBinder val) { nativeWriteStrongBinder(mNativePtr, val); } static void android_os_Parcel_writeStrongBinder (...) { ...... const status_t err = parcel->writeStrongBinder(ibinderForJavaObject(env,object)); ...... } sp<IBinder> ibinderForJavaObject (JNIEnv* env, jobject obj) { if (obj == NULL) return NULL; if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) { JavaBBinderHolder* jbh = (JavaBBinderHolder*) env->GetLongField(obj, gBinderOffsets.mObject); return jbh->get(env, obj); } if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) { return getBPNativeData(env, obj)->mObject; } ALOGW("ibinderForJavaObject: %p is not a Binder object" , obj); return NULL; }
得出:data.writeStrongBinder(service) 即 data.writeStrongBinder(JavaBBinder(Binder)) 传入的不是AMS本身,而是JavaBBinder
JavaBBinder 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class JavaBBinder : public BBinder{ virtual status_t onTransact () { ...... jboolean res = env->CallBooleanMethod(mObject,gBinderOffsets.mExecTransact,code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply),flags); ...... } } private boolean execTransact (int code, long dataObj, long replyObj, int flags) { ...... execTransactInternal(code, dataObj, replyObj, flags, callingUid); ...... } private boolean execTransactInternal (int code, long dataObj, long replyObj, int flags, int callingUid) { ...... res = onTransact(code, data, reply, flags); ...... }