美文网首页Android技术进阶架构设计
架构设计分析(三)Android 9.0 Binder机制

架构设计分析(三)Android 9.0 Binder机制

作者: 宾格66 | 来源:发表于2019-11-11 00:02 被阅读0次

    来点前奏说明

    当你打开这个文档的时候,你已经做好准备了,话不多说开搞。
    本文以Android 9.0 版本进行分析,当然你也可以在线看源码
    在线源码查看
    kernel_3.18 在线源码
    Android源码下载编译
    9.0源码下载链接 提取码:d0ks
    在此特别说明,这篇文档参考了很多文章和视频,主要自己记录一下,有些我也不明白。

    产生Binder原因:
    • 为了多进程间的通信
    进程间通讯为什么选择使用Binder:
    • 性能上:Binder相对出传统的Socket方式,更加高效。Binder数据拷贝只需要一次,而管道、消息队列、Socket都需要2次
    • 安全方面:Binder机制从协议本身就支持对通信双方做身份校检,因而大大提升了安全性。
    Binder是什么:
    • 从IPC角度说:它是Android中一种跨进程通信方式,是Android独有的
    • App层:它是客户端和服务端进行通信的媒介,当bindService时候,服务端返回一个包含了服务器端业务调用的Binder对象,通过这个对象,客户端可以获取服务端提供的服务或者数据,这里的服务包括普通服务和基于AIDL的服务。
    • Framework层:它是各种Manager(ActivityManager/WindowManager等)和XxxManagerService(ActivityManagerService/WindowManagerService等)的桥梁。
    • Native层:它是创建Service Manager以及BpBinder/BBinder模型,搭建和Kernel层的桥梁。
    • Kernel层:它提供了最底层的数据传递、对象标识、线程管理、调用过程控制等功能。驱动层是整个Binder机制的核心。它还可以理解为一种虚拟的数据设备,它的设备驱动是别dev/binder
    核心类图
    核心类图
    Binder架构
    Binder架构图
    源码分析

    Framework层

     frameworks/base/core/jni/AndroidRuntime.cpp的start方法开始
    /*
     * Start the Android runtime.  This involves starting the virtual machine
     * and calling the "static void main(String[] args)" method in the class
     * named by "className".
     *
     * Passes the main function two arguments, the class name and the specified
     * options string.
     */
    void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
    {
        ALOGD(">>>>>> START %s uid %d <<<<<<\n",
                className != NULL ? className : "(unknown)", getuid());
    
        static const String8 startSystemServer("start-system-server");
    
        /*
         * 'startSystemServer == true' means runtime is obsolete and not run from
         * init.rc anymore, so we print out the boot start event here.
         */
        for (size_t i = 0; i < options.size(); ++i) {
            if (options[i] == startSystemServer) {
               /* track our progress through the boot sequence */
               const int LOG_BOOT_PROGRESS_START = 3000;
               LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,  ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
            }
        }
    
        const char* rootDir = getenv("ANDROID_ROOT");
        if (rootDir == NULL) {
            rootDir = "/system";
            if (!hasDir("/system")) {
                LOG_FATAL("No root directory specified, and /android does not exist.");
                return;
            }
            setenv("ANDROID_ROOT", rootDir, 1);
        }
    
        //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
        //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
    
        /* start the virtual machine */
        JniInvocation jni_invocation;
        jni_invocation.Init(NULL);
        JNIEnv* env;
        if (startVm(&mJavaVM, &env, zygote) != 0) {
            return;
        }
        onVmCreated(env);
    
        /*
         * Register android functions.
         */
        if (startReg(env) < 0) {
            ALOGE("Unable to register all android natives\n");
            return;
        }
    
        /*
         * We want to call main() with a String array with arguments in it.
         * At present we have two arguments, the class name and an option string.
         * Create an array to hold them.
         */
        jclass stringClass;
        jobjectArray strArray;
        jstring classNameStr;
    
        stringClass = env->FindClass("java/lang/String");
        assert(stringClass != NULL);
        strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
        assert(strArray != NULL);
        classNameStr = env->NewStringUTF(className);
        assert(classNameStr != NULL);
        env->SetObjectArrayElement(strArray, 0, classNameStr);
    
        for (size_t i = 0; i < options.size(); ++i) {
            jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
            assert(optionsStr != NULL);
            env->SetObjectArrayElement(strArray, i + 1, optionsStr);
        }
    
        /*
         * Start VM.  This thread becomes the main thread of the VM, and will
         * not return until the VM exits.
         */
        char* slashClassName = toSlashClassName(className != NULL ? className : "");
        jclass startClass = env->FindClass(slashClassName);
        if (startClass == NULL) {
            ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
            /* keep going */
        } else {
            jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
                "([Ljava/lang/String;)V");
            if (startMeth == NULL) {
                ALOGE("JavaVM unable to find main() in '%s'\n", className);
                /* keep going */
            } else {
                env->CallStaticVoidMethod(startClass, startMeth, strArray);
    
    #if 0
                if (env->ExceptionCheck())
                    threadExitUncaughtException(env);
    #endif
            }
        }
        free(slashClassName);
    
        ALOGD("Shutting down VM\n");
        if (mJavaVM->DetachCurrentThread() != JNI_OK)
            ALOGW("Warning: unable to detach main thread\n");
        if (mJavaVM->DestroyJavaVM() != 0)
            ALOGW("Warning: VM did not shut down cleanly\n");
    }
    

    startReg 在Android系统开机过程中,Zygote启动会有一个虚拟机注册过程,该过程调用了该方法完成了jni的注册

    /*
     * Register android native functions with the VM.
     */
    /*static*/ int AndroidRuntime::startReg(JNIEnv* env)
    {
        ATRACE_NAME("RegisterAndroidNatives");
        /*
         * This hook causes all future threads created in this process to be
         * attached to the JavaVM.  (This needs to go away in favor of JNI
         * Attach calls.)
         */
        androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
    
        ALOGV("--- registering native functions ---\n");
    
        /*
         * Every "register" function calls one or more things that return
         * a local reference (e.g. FindClass).  Because we haven't really
         * started the VM yet, they're all getting stored in the base frame
         * and never released.  Use Push/Pop to manage the storage.
         */
        env->PushLocalFrame(200);
    
        if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
            env->PopLocalFrame(NULL);
            return -1;
        }
        env->PopLocalFrame(NULL);
    
        //createJavaThread("fubar", quickTest, (void*) "hello");
    
        return 0;
    }
    

    Native层(System Manager)

    1 、启动Service Manager时序图

    启动Service Manager时序图
    • 启动大致从 frameworks/native/cmds/service_manager.c这个文件开始
    • main方法开始
    • binder_open 打开binder驱动
    • binder_become_context_manager 注册成为binder服务
    • binder_loop 进入无限循环,处理客户client端发来的请求
    int main(int argc, char** argv)
    {
        struct binder_state *bs;
        union selinux_callback cb;
        char *driver;
    
        if (argc > 1) {
            driver = argv[1];
        } else {
            driver = "/dev/binder";
        }
    
        bs = binder_open(driver, 128*1024);
        if (!bs) {
    #ifdef VENDORSERVICEMANAGER
            ALOGW("failed to open binder driver %s\n", driver);
            while (true) {
                sleep(UINT_MAX);
            }
    #else
            ALOGE("failed to open binder driver %s\n", driver);
    #endif
            return -1;
        }
    
        if (binder_become_context_manager(bs)) {
            ALOGE("cannot become context manager (%s)\n", strerror(errno));
            return -1;
        }
    
        cb.func_audit = audit_callback;
        selinux_set_callback(SELINUX_CB_AUDIT, cb);
        cb.func_log = selinux_log_callback;
        selinux_set_callback(SELINUX_CB_LOG, cb);
    
    #ifdef VENDORSERVICEMANAGER
        sehandle = selinux_android_vendor_service_context_handle();
    #else
        sehandle = selinux_android_service_context_handle();
    #endif
        selinux_status_open(true);
    
        if (sehandle == NULL) {
            ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
            abort();
        }
    
        if (getcon(&service_manager_context) != 0) {
            ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
            abort();
        }
    
    
        binder_loop(bs, svcmgr_handler);
    
        return 0;
    }
    

    2、获取Service Manager时序图


    获取Service Manager时序图
    • 获取Service Manager大致从 frameworks/native/libs/binder/IServiceManager.cpp 这个文件开始
    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;
    }
    
    • frameworks/native/libs/binder/ProcessState.cpp
      ProcessState::self()获取ProcessState单例对象,每个进程有且只有一个ProcessState对象,存在则返回,否则就创建。
    sp<ProcessState> ProcessState::self()
    {
        Mutex::Autolock _l(gProcessMutex);
        if (gProcess != NULL) {
            return gProcess;
        }
        gProcess = new ProcessState("/dev/binder");
        return gProcess;
    }
    

    getContextObject() 用于获取BpBinder对象,对于handle=0的BpBinder对象,存在就返回否则创建

    sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
    {
        return getStrongProxyForHandle(0);
    }
    
    sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
    {
        sp<IBinder> result;
    
        AutoMutex _l(mLock);
    
        handle_entry* e = lookupHandleLocked(handle);
    
        if (e != NULL) {
            // We need to create a new BpBinder if there isn't currently one, OR we
            // are unable to acquire a weak reference on this current one.  See comment
            // in getWeakProxyForHandle() for more info about this.
            IBinder* b = e->binder;
            if (b == NULL || !e->refs->attemptIncWeak(this)) {
                if (handle == 0) {
                    // Special case for context manager...
                    // The context manager is the only object for which we create
                    // a BpBinder proxy without already holding a reference.
                    // Perform a dummy transaction to ensure the context manager
                    // is registered before we create the first local reference
                    // to it (which will occur when creating the BpBinder).
                    // If a local reference is created for the BpBinder when the
                    // context manager is not present, the driver will fail to
                    // provide a reference to the context manager, but the
                    // driver API does not return status.
                    //
                    // Note that this is not race-free if the context manager
                    // dies while this code runs.
                    //
                    // TODO: add a driver API to wait for context manager, or
                    // stop special casing handle 0 for context manager and add
                    // a driver API to get a handle to the context manager with
                    // proper reference counting.
    
                    Parcel data;
                    status_t status = IPCThreadState::self()->transact(
                            0, IBinder::PING_TRANSACTION, data, NULL, 0);
                    if (status == DEAD_OBJECT)
                       return NULL;
                }
    
                b = BpBinder::create(handle);
                e->binder = b;
                if (b) e->refs = b->getWeakRefs();
                result = b;
            } else {
                // This little bit of nastyness is to allow us to add a primary
                // reference to the remote proxy when this team doesn't have one
                // but another team is sending the handle to us.
                result.force_set(b);
                e->refs->decWeak(this);
            }
        }
    
        return result;
    }
    

    3、注册服务(对照架构图1.注册服务)

    注册服务.jpg
    • 注册服务大致从 frameworks/native/libs/binder/IServiceManager.cpp 这个文件开始分析
        virtual status_t addService(const String16& name, const sp<IBinder>& service,
                                    bool allowIsolated, int dumpsysPriority) {
            Parcel data, reply;
            data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
            data.writeString16(name);
            data.writeStrongBinder(service);
            data.writeInt32(allowIsolated ? 1 : 0);
            data.writeInt32(dumpsysPriority);
            status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
            return err == NO_ERROR ? reply.readExceptionCode() : err;
        }
    

    Kernel层:

    • 大致从 kernel3.18/driver/staging/android/binder.c 这个文件分析
    • binder_init 驱动设备初始化方法
    • binder_open 打开binder驱动设备
    • binder_mmap 实现用户空间Buffer和内存空间的Buffer 同步操作。在内存虚拟地址空间,申请一块与用户虚拟内存相同大小的内存;再申请一个page大小的物理内存,将同一块物理内存分别映射到内存虚拟地址空间和用户虚拟空间去实现的。
    • binder_ioctl 数据操作

    相关文章

      网友评论

        本文标题:架构设计分析(三)Android 9.0 Binder机制

        本文链接:https://www.haomeiwen.com/subject/bihbbctx.html