Adnroid Binder分析

作者: Young_Allen | 来源:发表于2018-11-11 08:47 被阅读44次

    每天一篇系列:
    强化知识体系,查漏补缺。
    欢迎指正,共同学习!

    Binder是Android最核心的IPC通信机制,至于为什么不采用管道、信号、共享内存,套接字等方式来实现IPC,总结起来就是Binder只有一次数据拷贝,并且更加安全。

    也参考其他大神对Binder的分析,他们分析的很好,在这里为了便于自己更好的理解也引用他们的图例:

    具体实现类如下:

    然后以MediaplayerService为例:

    涉及到的类图:

    MediaplayerService的入口函数:

    /**Main_mediaserver.cpp*/
    int main(int argc __unused, char** argv)
    {
            sp<ProcessState> proc(ProcessState::self());
            sp<IServiceManager> sm = defaultServiceManager();
            MediaPlayerService::instantiate();
            ProcessState::self()->startThreadPool();
            IPCThreadState::self()->joinThreadPool();
    }
    

    第一句代码分析:

    sp,wp是android的智能指针实现,继承RefBase抽象类。

    sp<ProcessState> ProcessState::self()
    {
        Mutex::Autolock _l(gProcessMutex);
        if (gProcess != NULL) {
            return gProcess;
        }
        gProcess = new ProcessState;
        return gProcess;
    }
    
    class ProcessState : public virtual RefBase
    {
        ...
    }
    
    ProcessState::ProcessState()
        : mDriverFD(open_driver())  //网上说的很重要,联系着binder
        , mVMStart(MAP_FAILED)
        , mManagesContexts(false)
        , mBinderContextCheckFunc(NULL)
        , mBinderContextUserData(NULL)
        , mThreadPoolStarted(false)
        , mThreadPoolSeq(1)
    {
        if (mDriverFD >= 0) {
            // XXX Ideally, there should be a specific define for whether we
            // have mmap (or whether we could possibly have the kernel module
            // availabla).
    #if !defined(HAVE_WIN32_IPC)
            // mmap the binder, providing a chunk of virtual address space to receive transactions.
            mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
            if (mVMStart == MAP_FAILED) {
                // *sigh*
                ALOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
                close(mDriverFD);
                mDriverFD = -1;
            }
    #else
            mDriverFD = -1;
    #endif
        }
    
        LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened.  Terminating.");
    }
    
    static int open_driver()
    {
        int fd = open("/dev/binder", O_RDWR);  //在这里去打开binder驱动,之后就是ioctl操作
        if (fd >= 0) {
            fcntl(fd, F_SETFD, FD_CLOEXEC);
            int vers = 0;
            status_t result = ioctl(fd, BINDER_VERSION, &vers);
            if (result == -1) {
                ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
                close(fd);
                fd = -1;
            }
            if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
                ALOGE("Binder driver protocol does not match user space protocol!");
                close(fd);
                fd = -1;
            }
            size_t maxThreads = 15;
            result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
            if (result == -1) {
                ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
            }
        } else {
            ALOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
        }
        return fd;
    }
    

    Mutex::Autolock是android C++的智能锁机制,在构造函数加锁,在析构函数释放锁,即入方法开始加锁,出方法后会释放锁。
    分析到open_driver后,大致知道以下代码:

    sp<ProcessState> proc(ProcessState::self());
    

    1.打开binder设备
    2.映射了一块大小为BINDER_VM_SIZE的fd到内存
    3.赋值给proc

    第二句代码分析:

    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;
    }
    

    可以看到只有一句关键的需要分析:

    gDefaultServiceManager = interface_cast<IServiceManager>(
                    ProcessState::self()->getContextObject(NULL));
    

    首先分析ProcessState::self()->getContextObject(NULL):

    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) {
            IBinder* b = e->binder;
            //句柄为0,初次创建时e-binder就是NULL,后面那个不分析了
            if (b == NULL || !e->refs->attemptIncWeak(this)) {
                //句柄为0即service_manager,其他的服务都是大于0的随机分配的值
                if (handle == 0) {
                    Parcel data;
                    //这里可以先不分析,后面有实际调用
                    status_t status = IPCThreadState::self()->transact(
                            0, IBinder::PING_TRANSACTION, data, NULL, 0);
                    if (status == DEAD_OBJECT)
                       return NULL;
                }
                //出现了和Binder相关的对象
                b = new BpBinder(handle); 
                e->binder = b;
                //为了智能指针的处理?
                if (b) e->refs = b->getWeakRefs();
                result = b;
            } else {
                //已存在则可以直接使用,但肯定是BpBinder
                result.force_set(b);
                e->refs->decWeak(this);
            }
        }
        return result;
    }
    
    //这个函数就是根据句柄的值查看是否需要创建并返回
    ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
    {
        const size_t N=mHandleToObject.size();
        if (N <= (size_t)handle) {
            handle_entry e;
            e.binder = NULL;
            e.refs = NULL;
            status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
            if (err < NO_ERROR) return NULL;
        }
        return &mHandleToObject.editItemAt(handle);
    }
    

    这里开始出现了IBinder、BpBinder。

    #ifndef ANDROID_IBINDER_H
    #define ANDROID_IBINDER_H
    
    #include <utils/Errors.h>
    #include <utils/RefBase.h>
    #include <utils/String16.h>
    #include <utils/Vector.h>
    
    
    #define B_PACK_CHARS(c1, c2, c3, c4) \
        ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
    
    // ---------------------------------------------------------------------------
    namespace android {
    
    class BBinder;
    class BpBinder;
    class IInterface;
    class Parcel;
    
    class IBinder : public virtual RefBase
    {
    public:
        enum {
            FIRST_CALL_TRANSACTION  = 0x00000001,
            LAST_CALL_TRANSACTION   = 0x00ffffff,
    
            PING_TRANSACTION        = B_PACK_CHARS('_','P','N','G'),
            DUMP_TRANSACTION        = B_PACK_CHARS('_','D','M','P'),
            INTERFACE_TRANSACTION   = B_PACK_CHARS('_', 'N', 'T', 'F'),
            SYSPROPS_TRANSACTION    = B_PACK_CHARS('_', 'S', 'P', 'R'),
    
            // Corresponds to TF_ONE_WAY -- an asynchronous call.
            FLAG_ONEWAY             = 0x00000001
        };
    
                              IBinder();
    
        /**
         * Check if this IBinder implements the interface named by
         * @a descriptor.  If it does, the base pointer to it is returned,
         * which you can safely static_cast<> to the concrete C++ interface.
         */
        virtual sp<IInterface>  queryLocalInterface(const String16& descriptor);
    
        /**
         * Return the canonical name of the interface provided by this IBinder
         * object.
         */
        virtual const String16& getInterfaceDescriptor() const = 0;
    
        virtual bool            isBinderAlive() const = 0;
        virtual status_t        pingBinder() = 0;
        virtual status_t        dump(int fd, const Vector<String16>& args) = 0;
    
        virtual status_t        transact(   uint32_t code,
                                            const Parcel& data,
                                            Parcel* reply,
                                            uint32_t flags = 0) = 0;
    
        class DeathRecipient : public virtual RefBase
        {
        public:
            virtual void binderDied(const wp<IBinder>& who) = 0;
        };
    
        virtual status_t        linkToDeath(const sp<DeathRecipient>& recipient,
                                            void* cookie = NULL,
                                            uint32_t flags = 0) = 0;
    
        virtual status_t        unlinkToDeath(  const wp<DeathRecipient>& recipient,
                                                void* cookie = NULL,
                                                uint32_t flags = 0,
                                                wp<DeathRecipient>* outRecipient = NULL) = 0;
    
        virtual bool            checkSubclass(const void* subclassID) const;
    
        typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
    
        virtual void            attachObject(   const void* objectID,
                                                void* object,
                                                void* cleanupCookie,
                                                object_cleanup_func func) = 0;
        virtual void*           findObject(const void* objectID) const = 0;
        virtual void            detachObject(const void* objectID) = 0;
    
        virtual BBinder*        localBinder();
        virtual BpBinder*       remoteBinder();
    
    protected:
        virtual          ~IBinder();
    
    private:
    };
    
    }; // namespace android
    
    // ---------------------------------------------------------------------------
    
    #endif // ANDROID_IBINDER_H
    
    
    class BpBinder : public IBinder
    {
      ...
    }
    
    BpBinder::BpBinder(int32_t handle)
        : mHandle(handle)
        , mAlive(1)
        , mObitsSent(0)
        , mObituaries(NULL)
    {
        ALOGV("Creating BpBinder %p handle %d\n", this, mHandle);
    
        extendObjectLifetime(OBJECT_LIFETIME_WEAK);
        IPCThreadState::self()->incWeakHandle(handle);
    }
    

    好,BpBinder的构造函数又去调用了IPCThreadState::self()->incWeakHandle,注意是IPCThreadState,不是ProcessState,我第一次就看错了:

    IPCThreadState* IPCThreadState::self()
    {
        if (gHaveTLS) {
    restart:
            const pthread_key_t k = gTLS;
            IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
            if (st) return st;
            return new IPCThreadState;
        }
        
        if (gShutdown) return NULL;
        
        pthread_mutex_lock(&gTLSMutex);
        if (!gHaveTLS) {
            if (pthread_key_create(&gTLS, threadDestructor) != 0) {
                pthread_mutex_unlock(&gTLSMutex);
                return NULL;
            }
            gHaveTLS = true;
        }
        pthread_mutex_unlock(&gTLSMutex);
        goto restart;
    }
    
    IPCThreadState::IPCThreadState()
        : mProcess(ProcessState::self()),
          mMyThreadId(androidGetTid()),
          mStrictModePolicy(0),
          mLastTransactionBinderFlags(0)
    {
        pthread_setspecific(gTLS, this);
        clearCaller();
        mIn.setDataCapacity(256);
        mOut.setDataCapacity(256);
    }
    
    void IPCThreadState::incWeakHandle(int32_t handle)
    {
        LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
        //写入了句柄
        mOut.writeInt32(BC_INCREFS);
        mOut.writeInt32(handle);
    }
    

    看到在IPCThreadState构造函数里,分别创建了一个大小为256的mIn,mOut的Parcel对象(Parcel用来实现对象序列化)。
    总之:

    gDefaultServiceManager = interface_cast<IServiceManager>(
                    ProcessState::self()->getContextObject(NULL));
    

    经过分析后等价于:

    gDefaultServiceManager = interface_cast<IServiceManager>(new BpBinder(0));
    

    接下来应该去找interface_cast是什么?

    class IServiceManager : public IInterface
    {
    ...
    }
    
    /**IInterface.h*/
    template<typename INTERFACE>
    inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
    {
        return INTERFACE::asInterface(obj);
    }
    

    那么IServiceManager::asInterface在哪啊?没找到啊...
    查了很久的资料,发现原来不按套路出牌,在IInterface.h里面有如下定义:

    #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() { }                                   \
    

    IServiceManager又是继承于IInterface:

    class IServiceManager : public IInterface
    {
    public:
        DECLARE_META_INTERFACE(ServiceManager);
        ...
    }
    

    就等价于:

    class IServiceManager : public IInterface
    {
    public:
        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();
        ...
    }
    

    对应的在IServiceManager.cpp中:

    IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
    

    等价于:

        const android::String16 IServiceManager::descriptor("android.os.IServiceManager");             
        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就是new BpBinder(0)         
                    obj->queryLocalInterface(                
                            IServiceManager::descriptor).get());     
                //在第一次创建时为NULL,从这里开始分析            
                if (intr == NULL) {                             
                    intr = new BpServiceManager(obj);  
                }                                              
            }                                            
            return intr;                               
        }                                                 
        IServiceManager::IServiceManager() { }                     
        IServiceManager::~IServiceManager() { }
    

    难怪了,我说有时候看到DECLARE_META_INTERFACE(ServiceManager);里面的ServiceManager是怎么命名的呢。

    template<typename INTERFACE>
    class BpInterface : public INTERFACE, public BpRefBase
    {
    public:
                                    BpInterface(const sp<IBinder>& remote);
    
    protected:
        virtual IBinder*            onAsBinder();
    };
    
    template<typename INTERFACE>
    inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
        : BpRefBase(remote)
    {
    }
    
    class BpServiceManager : public BpInterface<IServiceManager>
    {
    public:
        BpServiceManager(const sp<IBinder>& impl)
            : BpInterface<IServiceManager>(impl)
        {
        }
        ...
    }
    

    也就是说BpServiceManager,既继承了BpInterface,又继承了IServiceManager,还出现了BpRefBase。这样分析后

    gDefaultServiceManager = interface_cast<IServiceManager>(new BpBinder(0));
    

    等价于:

    gDefaultServiceManager = new BpServiceManager(new BpBinder(0));
    

    那么就是说第二句代码就是创建了一个BpServiceManager对象,并且赋值给gDefaultServiceManager 。
    记住,到现在IBinder、BpBinder、IInterface、BpInterface、BpServiceManager已经接触到这几种了,很容易让人混乱。

    第三句代码分析:

    void MediaPlayerService::instantiate() {
        defaultServiceManager()->addService(
                String16("media.player"), new MediaPlayerService());
    }
    

    在之前已经分析过defaultServiceManager是去创建一个BpServiceManager对象,在MediaPlayerService::instantiate()内部也会调用defaultServiceManager()方法,也就是说第二句源码不是必须的(由于是单例模式,先实例化也是可以的),因为第三句源码内部会调用defaultServiceManager()。

    class BpServiceManager : public BpInterface<IServiceManager>
    {
    public:
        BpServiceManager(const sp<IBinder>& impl)
            : BpInterface<IServiceManager>(impl)
        {
        }
    
        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;
                ALOGI("Waiting for service %s...\n", String8(name).string());
                sleep(1);
            }
            return NULL;
        }
    
        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();
        }
    
        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);
            return err == NO_ERROR ? reply.readExceptionCode() : err;
        }
    
        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;
        }
    };
    

    data.writeStrongBinder(service)放在文章后面分析。
    之前提到过BpRefBase:

    class BpRefBase : public virtual RefBase
    {
    protected:
                                BpRefBase(const sp<IBinder>& o);
        virtual                 ~BpRefBase();
        virtual void            onFirstRef();
        virtual void            onLastStrongRef(const void* id);
        virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);
    
        inline  IBinder*        remote()                { return mRemote; }
        inline  IBinder*        remote() const          { return mRemote; }
    
    private:
                                BpRefBase(const BpRefBase& o);
        BpRefBase&              operator=(const BpRefBase& o);
    
        IBinder* const          mRemote;
        RefBase::weakref_type*  mRefs;
        volatile int32_t        mState;
    };
    
    BpRefBase::BpRefBase(const sp<IBinder>& o)
        : mRemote(o.get()), mRefs(NULL), mState(0)
    {
        extendObjectLifetime(OBJECT_LIFETIME_WEAK);
    
        if (mRemote) {
            mRemote->incStrong(this);           // Removed on first IncStrong().
            mRefs = mRemote->createWeak(this);  // Held for our entire lifetime.
        }
    }
    

    从之前BpInterface的构造函数看到,把new BpBinder(0)传递给了BpRefBase构造参数。因此remote() 其实就是之前创建出来的BpBinder。那么下面源码:

      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);
            return err == NO_ERROR ? reply.readExceptionCode() : err;
        }
    

    等价于:

      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);
            //注意我这里并不是真正的去创建一个BpBinder,只是代表之前创建的那个对象方便描述而已
            status_t err = (new BpBinder(0))->transact(ADD_SERVICE_TRANSACTION, data, &reply);
            return err == NO_ERROR ? reply.readExceptionCode() : err;
        }
    

    继续分析:

    status_t BpBinder::transact(
        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    {
        // Once a binder has died, it will never come back to life.
        if (mAlive) {
            status_t status = IPCThreadState::self()->transact(
                mHandle, code, data, reply, flags);
            if (status == DEAD_OBJECT) mAlive = 0;
            return status;
        }
    
        return DEAD_OBJECT;
    }
    
    status_t IPCThreadState::transact(int32_t handle,
                                      uint32_t code, const Parcel& data,
                                      Parcel* reply, uint32_t flags)
    {
        status_t err = data.errorCheck();
    
        flags |= TF_ACCEPT_FDS;
    
        IF_LOG_TRANSACTIONS() {
            TextOutput::Bundle _b(alog);
            alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
                << handle << " / code " << TypeCode(code) << ": "
                << indent << data << dedent << endl;
        }
        
        if (err == NO_ERROR) {
            LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
                (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
            err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
        }
        
        if (err != NO_ERROR) {
            if (reply) reply->setError(err);
            return (mLastError = err);
        }
        
        if ((flags & TF_ONE_WAY) == 0) {
            #if 0
            if (code == 4) { // relayout
                ALOGI(">>>>>> CALLING transaction 4");
            } else {
                ALOGI(">>>>>> CALLING transaction %d", code);
            }
            #endif
            if (reply) {
                err = waitForResponse(reply);
            } else {
                Parcel fakeReply;
                err = waitForResponse(&fakeReply);
            }
            #if 0
            if (code == 4) { // relayout
                ALOGI("<<<<<< RETURNING transaction 4");
            } else {
                ALOGI("<<<<<< RETURNING transaction %d", code);
            }
            #endif
            
            IF_LOG_TRANSACTIONS() {
                TextOutput::Bundle _b(alog);
                alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
                    << handle << ": ";
                if (reply) alog << indent << *reply << dedent << endl;
                else alog << "(none requested)" << endl;
            }
        } else {
            err = waitForResponse(NULL, NULL);
        }
        
        return err;
    }
    

    在IPCThreadState的transact中writeTransactionData负责把数据写入到mOut(之前提到过是一个大小为256的Parcel对象)

    status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
        int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
    {
        binder_transaction_data tr;
    
        tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
        tr.target.handle = handle;
        tr.code = code;
        tr.flags = binderFlags;
        tr.cookie = 0;
        tr.sender_pid = 0;
        tr.sender_euid = 0;
        
        const status_t err = data.errorCheck();
        if (err == NO_ERROR) {
            tr.data_size = data.ipcDataSize();
            tr.data.ptr.buffer = data.ipcData();
            tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
            tr.data.ptr.offsets = data.ipcObjects();
        } else if (statusBuffer) {
            tr.flags |= TF_STATUS_CODE;
            *statusBuffer = err;
            tr.data_size = sizeof(status_t);
            tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
            tr.offsets_size = 0;
            tr.data.ptr.offsets = 0;
        } else {
            return (mLastError = err);
        }
        
        mOut.writeInt32(cmd);
        mOut.write(&tr, sizeof(tr));
        
        return NO_ERROR;
    }
    

    waitForResponse负责写入并等待写入结果:

    status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
    {
        int32_t cmd;
        int32_t err;
    
        while (1) {
            if ((err=talkWithDriver()) < NO_ERROR) break;
            err = mIn.errorCheck();
            if (err < NO_ERROR) break;
            if (mIn.dataAvail() == 0) continue;
            
            cmd = mIn.readInt32();
            
            IF_LOG_COMMANDS() {
                alog << "Processing waitForResponse Command: "
                    << getReturnString(cmd) << endl;
            }
    
            switch (cmd) {
            case BR_TRANSACTION_COMPLETE:
                if (!reply && !acquireResult) goto finish;
                break;
            
            case BR_DEAD_REPLY:
                err = DEAD_OBJECT;
                goto finish;
    
            case BR_FAILED_REPLY:
                err = FAILED_TRANSACTION;
                goto finish;
            
            case BR_ACQUIRE_RESULT:
                {
                    ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
                    const int32_t result = mIn.readInt32();
                    if (!acquireResult) continue;
                    *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
                }
                goto finish;
            
            case BR_REPLY:
                {
                    binder_transaction_data tr;
                    err = mIn.read(&tr, sizeof(tr));
                    ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
                    if (err != NO_ERROR) goto finish;
    
                    if (reply) {
                        if ((tr.flags & TF_STATUS_CODE) == 0) {
                            reply->ipcSetDataReference(
                                reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
                                tr.data_size,
                                reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
                                tr.offsets_size/sizeof(binder_size_t),
                                freeBuffer, this);
                        } else {
                            err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
                            freeBuffer(NULL,
                                reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
                                tr.data_size,
                                reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
                                tr.offsets_size/sizeof(binder_size_t), this);
                        }
                    } else {
                        freeBuffer(NULL,
                            reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
                            tr.data_size,
                            reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
                            tr.offsets_size/sizeof(binder_size_t), this);
                        continue;
                    }
                }
                goto finish;
    
            default:
                err = executeCommand(cmd);
                if (err != NO_ERROR) goto finish;
                break;
            }
        }
    
    finish:
        if (err != NO_ERROR) {
            if (acquireResult) *acquireResult = err;
            if (reply) reply->setError(err);
            mLastError = err;
        }
        
        return err;
    }
    

    其中talkWithDriver才是使用ioctl接口把mOut数据写入binder驱动,并且通过mIn返回处理结果。

    status_t IPCThreadState::talkWithDriver(bool doReceive)
    {
        if (mProcess->mDriverFD <= 0) {
            return -EBADF;
        }
        
        binder_write_read bwr;
        
        // Is the read buffer empty?
        const bool needRead = mIn.dataPosition() >= mIn.dataSize();
        
        // We don't want to write anything if we are still reading
        // from data left in the input buffer and the caller
        // has requested to read the next data.
        const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
        
        bwr.write_size = outAvail;
        bwr.write_buffer = (uintptr_t)mOut.data();
    
        // This is what we'll read.
        if (doReceive && needRead) {
            bwr.read_size = mIn.dataCapacity();
            bwr.read_buffer = (uintptr_t)mIn.data();
        } else {
            bwr.read_size = 0;
            bwr.read_buffer = 0;
        }
    
        IF_LOG_COMMANDS() {
            TextOutput::Bundle _b(alog);
            if (outAvail != 0) {
                alog << "Sending commands to driver: " << indent;
                const void* cmds = (const void*)bwr.write_buffer;
                const void* end = ((const uint8_t*)cmds)+bwr.write_size;
                alog << HexDump(cmds, bwr.write_size) << endl;
                while (cmds < end) cmds = printCommand(alog, cmds);
                alog << dedent;
            }
            alog << "Size of receive buffer: " << bwr.read_size
                << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
        }
        
        // Return immediately if there is nothing to do.
        if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
    
        bwr.write_consumed = 0;
        bwr.read_consumed = 0;
        status_t err;
        do {
            IF_LOG_COMMANDS() {
                alog << "About to read/write, write size = " << mOut.dataSize() << endl;
            }
    #if defined(HAVE_ANDROID_OS)
            if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
                err = NO_ERROR;
            else
                err = -errno;
    #else
            err = INVALID_OPERATION;
    #endif
            if (mProcess->mDriverFD <= 0) {
                err = -EBADF;
            }
            IF_LOG_COMMANDS() {
                alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
            }
        } while (err == -EINTR);
    
        IF_LOG_COMMANDS() {
            alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
                << bwr.write_consumed << " (of " << mOut.dataSize()
                            << "), read consumed: " << bwr.read_consumed << endl;
        }
    
        if (err >= NO_ERROR) {
            if (bwr.write_consumed > 0) {
                if (bwr.write_consumed < mOut.dataSize())
                    mOut.remove(0, bwr.write_consumed);
                else
                    mOut.setDataSize(0);
            }
            if (bwr.read_consumed > 0) {
                mIn.setDataSize(bwr.read_consumed);
                mIn.setDataPosition(0);
            }
            IF_LOG_COMMANDS() {
                TextOutput::Bundle _b(alog);
                alog << "Remaining data size: " << mOut.dataSize() << endl;
                alog << "Received commands from driver: " << indent;
                const void* cmds = mIn.data();
                const void* end = mIn.data() + mIn.dataSize();
                alog << HexDump(cmds, mIn.dataSize()) << endl;
                while (cmds < end) cmds = printReturnCommand(alog, cmds);
                alog << dedent;
            }
            return NO_ERROR;
        }
        return err;
    }
    

    那么Binder的Client端就是通过BpServiceManger调用BpBinder及IPCThreadState开始写入binder驱动的,那么对应的应该有一个Server端接收binder的数据并作相应的处理。
    这里需要分析android的Service_manager.c:

    int main(int argc, char **argv)
    {
        struct binder_state *bs;
    
        bs = binder_open(128*1024);
        if (!bs) {
            ALOGE("failed to open binder driver\n");
            return -1;
        }
    
        if (binder_become_context_manager(bs)) {
            ALOGE("cannot become context manager (%s)\n", strerror(errno));
            return -1;
        }
    
        selinux_enabled = is_selinux_enabled();
        sehandle = selinux_android_service_context_handle();
    
        if (selinux_enabled > 0) {
            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();
            }
        }
    
        union selinux_callback cb;
        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);
    
        svcmgr_handle = BINDER_SERVICE_MANAGER;
        binder_loop(bs, svcmgr_handler);
    
        return 0;
    }
    

    selinux模块不分析,其实只有几个函数需要看看,使用Binder.c的接口:

    struct binder_state *binder_open(size_t mapsize)
    {
        struct binder_state *bs;
        struct binder_version vers;
    
        bs = malloc(sizeof(*bs));
        if (!bs) {
            errno = ENOMEM;
            return NULL;
        }
    
        bs->fd = open("/dev/binder", O_RDWR);  //也是去打开binder设备
        if (bs->fd < 0) {
            fprintf(stderr,"binder: cannot open device (%s)\n",
                    strerror(errno));
            goto fail_open;
        }
    
        if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
            (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
            fprintf(stderr, "binder: driver version differs from user space\n");
            goto fail_open;
        }
    
        bs->mapsize = mapsize;
        bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
        if (bs->mapped == MAP_FAILED) {
            fprintf(stderr,"binder: cannot map device (%s)\n",
                    strerror(errno));
            goto fail_map;
        }
    
        return bs;
    
    fail_map:
        close(bs->fd);
    fail_open:
        free(bs);
        return NULL;
    }
    

    去打开binder设备并且映射内存。然后一直线程轮询:

    void binder_loop(struct binder_state *bs, binder_handler func)
    {
        int res;
        struct binder_write_read bwr;
        uint32_t readbuf[32];
    
        bwr.write_size = 0;
        bwr.write_consumed = 0;
        bwr.write_buffer = 0;
    
        readbuf[0] = BC_ENTER_LOOPER;
        binder_write(bs, readbuf, sizeof(uint32_t));
    
        for (;;) {
            bwr.read_size = sizeof(readbuf);
            bwr.read_consumed = 0;
            bwr.read_buffer = (uintptr_t) readbuf;
    
            res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
    
            if (res < 0) {
                ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
                break;
            }
    
            res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
            if (res == 0) {
                ALOGE("binder_loop: unexpected reply?!\n");
                break;
            }
            if (res < 0) {
                ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
                break;
            }
        }
    }
    

    线程一直在通过ioctl读binder设备,并且把读到的内容解析后作相应处理:

    int binder_parse(struct binder_state *bs, struct binder_io *bio,
                     uintptr_t ptr, size_t size, binder_handler func)
    {
        ...
        if (func) {
                    unsigned rdata[256/4];
                    struct binder_io msg;
                    struct binder_io reply;
                    int res;
    
                    bio_init(&reply, rdata, sizeof(rdata), 4);
                    bio_init_from_txn(&msg, txn);
                    res = func(bs, txn, &msg, &reply);
                    binder_send_reply(bs, &reply, txn->data.ptr.buffer, res);
                }
        ...
    }
    

    其中func是Service_manager.c中的svcmgr_handler回调:

    int svcmgr_handler(struct binder_state *bs,
                       struct binder_transaction_data *txn,
                       struct binder_io *msg,
                       struct binder_io *reply)
    {
        ...
        case SVC_MGR_ADD_SERVICE:
            s = bio_get_string16(msg, &len);
            if (s == NULL) {
                return -1;
            }
            handle = bio_get_ref(msg);
            allow_isolated = bio_get_uint32(msg) ? 1 : 0;
            if (do_add_service(bs, s, len, handle, txn->sender_euid,
                allow_isolated, txn->sender_pid))
                return -1;
            break;
        ...  
    }
    
    int do_add_service(struct binder_state *bs,
                       const uint16_t *s, size_t len,
                       uint32_t handle, uid_t uid, int allow_isolated,
                       pid_t spid)
    {
        struct svcinfo *si;
    
        //ALOGI("add_service('%s',%x,%s) uid=%d\n", str8(s, len), handle,
        //        allow_isolated ? "allow_isolated" : "!allow_isolated", uid);
    
        if (!handle || (len == 0) || (len > 127))
            return -1;
    
        if (!svc_can_register(s, len, spid)) {
            ALOGE("add_service('%s',%x) uid=%d - PERMISSION DENIED\n",
                 str8(s, len), handle, uid);
            return -1;
        }
    
        si = find_svc(s, len);
        if (si) {
            if (si->handle) {
                ALOGE("add_service('%s',%x) uid=%d - ALREADY REGISTERED, OVERRIDE\n",
                     str8(s, len), handle, uid);
                svcinfo_death(bs, si);
            }
            si->handle = handle;
        } else {
            si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
            if (!si) {
                ALOGE("add_service('%s',%x) uid=%d - OUT OF MEMORY\n",
                     str8(s, len), handle, uid);
                return -1;
            }
            si->handle = handle;
            si->len = len;
            memcpy(si->name, s, (len + 1) * sizeof(uint16_t));
            si->name[len] = '\0';
            si->death.func = (void*) svcinfo_death;
            si->death.ptr = si;
            si->allow_isolated = allow_isolated;
            si->next = svclist;//这个svclist是一个列表,保存了当前注册到ServiceManager
            svclist = si;
        }
    
        binder_acquire(bs, handle);
        binder_link_to_death(bs, handle, &si->death);
        return 0;
    }
    

    到这一步之后已经清楚:

    MediaPlayerService::instantiate();
    

    是向Service_manager添加一个服务,而这个服务就是MediaPlayerService:

    defaultServiceManager()->addService(
                String16("media.player"), new MediaPlayerService());
    
    class MediaPlayerService : public BnMediaPlayerService
    {
        ...
    }
    
    class BnMediaPlayerService: public BnInterface<IMediaPlayerService>
    {
    public:
        virtual status_t    onTransact( uint32_t code,
                                        const Parcel& data,
                                        Parcel* reply,
                                        uint32_t flags = 0);
    };
    
    class IMediaPlayerService: public IInterface
    {
    public:
        DECLARE_META_INTERFACE(MediaPlayerService);
        ...
    }
    

    可以看到BnInterface:

    template<typename INTERFACE>
    class BnInterface : public INTERFACE, public BBinder
    {
    public:
        virtual sp<IInterface>      queryLocalInterface(const String16& _descriptor);
        virtual const String16&     getInterfaceDescriptor() const;
    
    protected:
        virtual IBinder*            onAsBinder();
    };
    

    这么看来,在BpServiceManager中:

    data.writeStrongBinder(service);
    

    这个service就是BnMediaPlayerService:

    status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
    {
        return flatten_binder(ProcessState::self(), val, this);
    }
    
    status_t flatten_binder(const sp<ProcessState>& /*proc*/,
        const sp<IBinder>& binder, Parcel* out)
    {
        flat_binder_object obj;
    
        obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
        if (binder != NULL) {
            //这里判断是bn 还是 bp
            IBinder *local = binder->localBinder();
            //不是bn 则认为是bp
            if (!local) {
                BpBinder *proxy = binder->remoteBinder();
                if (proxy == NULL) {
                    ALOGE("null proxy");
                }
                const int32_t handle = proxy ? proxy->handle() : 0;
                obj.type = BINDER_TYPE_HANDLE;
                obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
                obj.handle = handle;
                obj.cookie = 0;
            } else {
                
                obj.type = BINDER_TYPE_BINDER;
                obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
                obj.cookie = reinterpret_cast<uintptr_t>(local);
            }
        } else {
            obj.type = BINDER_TYPE_BINDER;
            obj.binder = 0;
            obj.cookie = 0;
        }
    
        return finish_flatten_binder(binder, obj, out);
    }
    
    inline static status_t finish_flatten_binder(
        const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
    {
        return out->writeObject(flat, false);
    }
    

    obj.binder就是要在Service_manager.c添加到svslist的Binder对象。
    接下来,client是如何使用这个对象的:

    // establish binder interface to MediaPlayerService
    /*static*/const sp<IMediaPlayerService>&
    IMediaDeathNotifier::getMediaPlayerService()
    {
        ALOGV("getMediaPlayerService");
        Mutex::Autolock _l(sServiceLock);
        if (sMediaPlayerService == 0) {
            sp<IServiceManager> sm = defaultServiceManager();
            sp<IBinder> binder;
            do {
                binder = sm->getService(String16("media.player"));
                if (binder != 0) {
                    break;
                }
                ALOGW("Media player service not published, waiting...");
                usleep(500000); // 0.5 s
            } while (true);
    
            if (sDeathNotifier == NULL) {
                sDeathNotifier = new DeathNotifier();
            }
            binder->linkToDeath(sDeathNotifier);
            sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);
        }
        ALOGE_IF(sMediaPlayerService == 0, "no media player service!?");
        return sMediaPlayerService;
    }
    

    defaultServiceManager返回的是一个Bp指针,因此通过BpServiceManager::getService去获取一个binder:

    class BpServiceManager : public BpInterface<IServiceManager>
    {
    public:
        BpServiceManager(const sp<IBinder>& impl)
            : BpInterface<IServiceManager>(impl)
        {
        }
    
        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;
                ALOGI("Waiting for service %s...\n", String8(name).string());
                sleep(1);
            }
            return NULL;
        }
    
        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();
        }
        ...
    }
    

    将一个CHECK_SERVICE_TRANSACTION的指令传递给Bn端(之前说过Service_manager.c的实现可以看成是ServiceManager的Bn端):

     case SVC_MGR_CHECK_SERVICE:
            s = bio_get_string16(msg, &len);
            if (s == NULL) {
                return -1;
            }
            handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
            if (!handle)
                break;
            bio_put_ref(reply, handle);
            return 0;
    

    可以看到通过查询svrlist里去找之前添加的media.player的BnBinder,返回其句柄,然后在BpServiceManager端readStrongBinder:

    sp<IBinder> Parcel::readStrongBinder() const
    {
        sp<IBinder> val;
        unflatten_binder(ProcessState::self(), *this, &val);
        return val;
    }
    
    status_t unflatten_binder(const sp<ProcessState>& proc,
        const Parcel& in, sp<IBinder>* out)
    {
        const flat_binder_object* flat = in.readObject(false);
    
        if (flat) {
            switch (flat->type) {
                case BINDER_TYPE_BINDER:
                    *out = reinterpret_cast<IBinder*>(flat->cookie);
                    return finish_unflatten_binder(NULL, *flat, in);
                case BINDER_TYPE_HANDLE:
                    *out = proc->getStrongProxyForHandle(flat->handle);
                    return finish_unflatten_binder(
                        static_cast<BpBinder*>(out->get()), *flat, in);
            }
        }
        return BAD_TYPE;
    }
    
    sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
    {
        sp<IBinder> result;
    
        AutoMutex _l(mLock);
    
        handle_entry* e = lookupHandleLocked(handle);
    
        if (e != NULL) {
            IBinder* b = e->binder;
            if (b == NULL || !e->refs->attemptIncWeak(this)) {
                if (handle == 0) { 
                    Parcel data;
                    status_t status = IPCThreadState::self()->transact(
                            0, IBinder::PING_TRANSACTION, data, NULL, 0);
                    if (status == DEAD_OBJECT)
                       return NULL;
                }
    
                b = new BpBinder(handle); 
                e->binder = b;
                if (b) e->refs = b->getWeakRefs();
                result = b;
            } else {
                result.force_set(b);
                e->refs->decWeak(this);
            }
        }
        return result;
    }
    

    看到了吧,read的是一个BpBinder。然后就是把BpBinder转换:

    sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);
    
    template<typename INTERFACE>
    inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
    {
        return INTERFACE::asInterface(obj);
    }
    

    之前也分析过IMPLEMENT_META_INTERFACE:

    IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");
    

    等价于:

        const android::String16 IMediaPlayerService:descriptor("android.media.IMediaPlayerService");             
        const android::String16& IMediaPlayerService::getInterfaceDescriptor() const {            
            return IMediaPlayerService::descriptor;                          
        }                                                            
        android::sp<IMediaPlayerService> IMediaPlayerService::asInterface(                
                const android::sp<android::IBinder>& obj) 
        {                                              
            android::sp<IMediaPlayerService> intr;
            if (obj != NULL) {                     
                intr = static_cast<IMediaPlayerService*>(
                    //这里传下来的obj就是new BpBinder(0)         
                    obj->queryLocalInterface(                
                            IMediaPlayerService::descriptor).get());     
                //在第一次创建时为NULL,从这里开始分析            
                if (intr == NULL) {                             
                    intr = new BpMediaPlayerService(obj);  
                }                                              
            }                                            
            return intr;                               
        }                                                 
        IMediaPlayerService::IMediaPlayerService() { }                     
        IMediaPlayerService::~IMediaPlayerService() { }
    

    看到了吧,MediaPlayer获得了BpMediaPlayerService指针,通过BpMediaPlayerService的调用,实现和MediaPlayerService的通信。

    第四句源码分析

    ProcessState::self()->startThreadPool();
    
    void ProcessState::startThreadPool()
    {
        AutoMutex _l(mLock);
        if (!mThreadPoolStarted) {
            mThreadPoolStarted = true;
            spawnPooledThread(true);
        }
    }
    
    void ProcessState::spawnPooledThread(bool isMain)
    {
        if (mThreadPoolStarted) {
            String8 name = makeBinderThreadName();
            ALOGV("Spawning new pooled thread, name=%s\n", name.string());
            sp<Thread> t = new PoolThread(isMain);
            t->run(name.string());
        }
    }
    
    class PoolThread : public Thread
    {
    public:
        PoolThread(bool isMain)
            : mIsMain(isMain)
        {
        }
        
    protected:
        virtual bool threadLoop()
        {
            IPCThreadState::self()->joinThreadPool(mIsMain);
            return false;
        }
        
        const bool mIsMain;
    };
    
    void IPCThreadState::joinThreadPool(bool isMain)
    {
        mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
        status_t result;
        do {
            processPendingDerefs();
            result = getAndExecuteCommand();
    
            if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
                abort();
            }
            if(result == TIMED_OUT && !isMain) {
                break;
            }
        } while (result != -ECONNREFUSED && result != -EBADF);
        mOut.writeInt32(BC_EXIT_LOOPER);
        talkWithDriver(false);
    }
    

    从上面的源码可以知道,创建了一个主线程,而且和binder驱动一直通信。中间函数跳转分析省略,可以看到调用了Binder的transact:

        case BR_TRANSACTION:
            {
                binder_transaction_data tr;
                result = mIn.read(&tr, sizeof(tr));
                ALOG_ASSERT(result == NO_ERROR,
                    "Not enough command data for brTRANSACTION");
                if (result != NO_ERROR) break;
                
                Parcel buffer;
                buffer.ipcSetDataReference(
                    reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
                    tr.data_size,
                    reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
                    tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
                
                const pid_t origPid = mCallingPid;
                const uid_t origUid = mCallingUid;
                const int32_t origStrictModePolicy = mStrictModePolicy;
                const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
    
                mCallingPid = tr.sender_pid;
                mCallingUid = tr.sender_euid;
                mLastTransactionBinderFlags = tr.flags;
    
                int curPrio = getpriority(PRIO_PROCESS, mMyThreadId);
                if (gDisableBackgroundScheduling) {
                    if (curPrio > ANDROID_PRIORITY_NORMAL) {
                        // We have inherited a reduced priority from the caller, but do not
                        // want to run in that state in this process.  The driver set our
                        // priority already (though not our scheduling class), so bounce
                        // it back to the default before invoking the transaction.
                        setpriority(PRIO_PROCESS, mMyThreadId, ANDROID_PRIORITY_NORMAL);
                    }
                } else {
                    if (curPrio >= ANDROID_PRIORITY_BACKGROUND) {
                        // We want to use the inherited priority from the caller.
                        // Ensure this thread is in the background scheduling class,
                        // since the driver won't modify scheduling classes for us.
                        // The scheduling group is reset to default by the caller
                        // once this method returns after the transaction is complete.
                        set_sched_policy(mMyThreadId, SP_BACKGROUND);
                    }
                }
    
                //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
    
                Parcel reply;
                status_t error;
                IF_LOG_TRANSACTIONS() {
                    TextOutput::Bundle _b(alog);
                    alog << "BR_TRANSACTION thr " << (void*)pthread_self()
                        << " / obj " << tr.target.ptr << " / code "
                        << TypeCode(tr.code) << ": " << indent << buffer
                        << dedent << endl
                        << "Data addr = "
                        << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
                        << ", offsets addr="
                        << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
                }
                if (tr.target.ptr) {
                    sp<BBinder> b((BBinder*)tr.cookie);
                    error = b->transact(tr.code, buffer, &reply, tr.flags);
    
                } else {
                    error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
                }
    
                //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
                //     mCallingPid, origPid, origUid);
                
                if ((tr.flags & TF_ONE_WAY) == 0) {
                    LOG_ONEWAY("Sending reply to %d!", mCallingPid);
                    if (error < NO_ERROR) reply.setError(error);
                    sendReply(reply, 0);
                } else {
                    LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
                }
                
                mCallingPid = origPid;
                mCallingUid = origUid;
                mStrictModePolicy = origStrictModePolicy;
                mLastTransactionBinderFlags = origTransactionBinderFlags;
    
                IF_LOG_TRANSACTIONS() {
                    TextOutput::Bundle _b(alog);
                    alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
                        << tr.target.ptr << ": " << indent << reply << dedent << endl;
                }
            }
    
    status_t BBinder::transact(
        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    {
        data.setDataPosition(0);
    
        status_t err = NO_ERROR;
        switch (code) {
            case PING_TRANSACTION:
                reply->writeInt32(pingBinder());
                break;
            default:
                err = onTransact(code, data, reply, flags);
                break;
        }
    
        if (reply != NULL) {
            reply->setDataPosition(0);
        }
    
        return err;
    }
    

    可以看到在最后一句源码:

    IPCThreadState::self()->joinThreadPool();
    

    等价于:

    IPCThreadState::self()->joinThreadPool(true);
    

    这句代码其实在ProcessState::self()->startThreadPool();的过程中是调用过的,最后还调用一次不知道是什么意义。

    下面是一个通过Binder机制实现安装应用和启动应用的例子,例子简单便于分析:
    首先定义出接口:

    isAppInstalled
    installApp
    startAppByIntent
    startAppByName
    

    既然是需要是用Binder框架,接口自然需要继承IInterface:

    class IAppManagerService : public IInterface
    {
    public:
        enum {
            APP_START_BYNAME = IBinder::FIRST_CALL_TRANSACTION,
            APP_START_BYINTENT,
            APP_STATUS_INSTALL,
            APP_STATUS_ISINSTALLED
        };
    
        //为什么能这么定义,之前在分析的时候提到过,I##INTERFACE的INTERFACE被替换为AppManagerService
        DECLARE_META_INTERFACE(AppManagerService);
    
        virtual int isAppInstalled_I(std::string appName){return 0;}
    
        virtual void installApp_I(std::string path){}
    
        virtual void startAppByIntent_I(std::string intent){}
    
        virtual void startByName_I(std::string appName){}
    };
    

    Bn端继承BnInterface:

    class BnAppManagerService: public BnInterface<IAppManagerService>
    {
    public:
        virtual status_t onTransact(uint32_t code,
                                        const Parcel& data,
                                        Parcel* reply,
                                        uint32_t flags = 0);
    };
    
    class AppManagerService : public BnAppManagerService
    {   
    public:
        AppManagerService();
        ~AppManagerService();
    
        static void instantiate();
    
        virtual status_t onTransact(
            uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0);
        
        virtual int isAppInstalled_N(std::string appName); 
        virtual void installApp_N(std::string path);
        virtual void startAppByName_N(std::string appName);
        virtual void startAppByIntent_N(std::string intent);
    };
    

    Bp端继承BpInterface:

    class BpAppManagerService: public BpInterface<IAppManagerService>
    {
    public:
        BpAppManagerService(const sp<IBinder>& impl)
            : BpInterface<IAppManagerService>(impl) {
        }
    
        int isAppInstalled_I(std::string appName) {
            Parcel data, reply;
            status_t err = NO_ERROR;
            data.writeInterfaceToken(IAppManagerService::getInterfaceDescriptor());
            data.writeCString(appName.data());
            err = remote()->transact(APP_STATUS_ISINSTALLED, data, &reply);
            int ret = reply.readInt32();
            if (err != NO_ERROR) {
                return 0;
            }
            return ret;
        }
    
        void installApp_I(std::string url) {
            Parcel data, reply;
            status_t err = NO_ERROR;
            data.writeInterfaceToken(IAppManagerService::getInterfaceDescriptor());
            data.writeCString(url.data());
            err = remote()->transact(APP_STATUS_INSTALL, data, &reply);
        }
    
        void startAppByName_I(std::string appName) {
            Parcel data, reply;
            status_t err = NO_ERROR;
            data.writeInterfaceToken(IAppManagerService::getInterfaceDescriptor());
            data.writeCString(appName.data());
            err = remote()->transact(APP_START_BYNAME, data, &reply);
        }
        
        void startAppByIntent_I(std::string intent) {
            Parcel data, reply;
            status_t err = NO_ERROR;
            data.writeInterfaceToken(IAppManagerService::getInterfaceDescriptor());
            data.writeCString(intent.data());
            err = remote()->transact(APP_START_BYINTENT, data, &reply);
        }
        
    };
    //对应Bn端的DECLARE_META_INTERFACE
    IMPLEMENT_META_INTERFACE(AppManagerService, "android.example.IAppManagerService");
    };
    

    根据之前的分析,首先需要启动Binder,然后把服务添加到svrlist中,最后开始Binder的轮询。

    int main(int arg, char** argv)
    {
        sp<ProcessState> proc(ProcessState::self());
        //这一句不是必须的,其他模块已经启动过ServiceManager或者在instantiate阶段也会通过defaultServiceManager()实例化
        //sp<IServiceManager> sm = defaultServiceManager();
        AppManagerService::instantiate();
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
        return 0;
    }
    

    客户端获取服务:

    static status_t getAppManagerService(sp<IAppManagerService>* outService) {
        status_t ret = getService(String16("example.appmanager"), outService);
        if (ret != NO_ERROR) {
            ALOGE("get service \"example.appmanager\" failed!");
        }
        return ret;
    }
    

    之前分析过,getService获取的是一个Bp指针:

    template<typename INTERFACE>
    status_t getService(const String16& name, sp<INTERFACE>* outService)
    {
        const sp<IServiceManager> sm = defaultServiceManager();
        if (sm != NULL) {
            *outService = interface_cast<INTERFACE>(sm->getService(name));
            if ((*outService) != NULL) return NO_ERROR;
        }
        return NAME_NOT_FOUND;
    }
    

    这里获取的是BpAppManagerService:

    void AppManagerClient::startAppByName(string name)
    {
        sp<IAppManagerService> mAppManagerSrv;
        status_t err = getAppManagerService(&mAppManagerSrv);
        if (err != NO_ERROR) {
            return ;
        }
        mAppManagerSrv->startAppByName_I(name);
    }
    
    void AppManagerClient::startAppByIntent(string intent)
    {
        sp<IAppManagerService> mAppManagerSrv;
        status_t err = getAppManagerService(&mAppManagerSrv);
        if (err != NO_ERROR) {
            return ;
        }
        mAppManagerSrv->startAppByIntent_I(intent);
    }
    
    void AppManagerClient::installApp(string url)
    {
        sp<IAppManagerService> mAppManagerSrv;
        status_t err = getAppManagerService(&mAppManagerSrv);
        if (err != NO_ERROR) {
            return ;
        }
        mAppManagerSrv->installApp_I(url);
    }
    
    int AppManagerClient::isAppInstalled(string appName)
    {
        sp<IAppManagerService> mAppManagerSrv;
        status_t err = getAppManagerService(&mAppManagerSrv);
        if (err != NO_ERROR) {
            return 0;
        }
        int ret = mAppManagerSrv->isAppInstalled_I(appName);
        ALOGE("isAppInstalled %d",ret);
        return ret;
    }
    

    实际上是BpAppManagerService端的调用。例如isAppInstalled的调用:

        int isAppInstalled_I(std::string appName) {
            Parcel data, reply;
            status_t err = NO_ERROR;
            data.writeInterfaceToken(IAppManagerService::getInterfaceDescriptor());
            data.writeCString(appName.data());
            err = remote()->transact(APP_STATUS_ISINSTALLED, data, &reply);
            int ret = reply.readInt32();
            if (err != NO_ERROR) {
                return 0;
            }
            return ret;
        }
    

    根据之前的分析,remote()是new BpBinder(handle)。分析Bn端的Ontransact:

    status_t AppManagerService::onTransact(
        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
        switch (code) {
        
        case APP_START_BYNAME: {
            CHECK_INTERFACE(IAppManagerService, data, reply);
            const char *if_name = data.readCString();
            ALOGI("startAppByName(%s)", if_name);
            startAppByName_N(if_name);
            reply->writeInt32(0);
            return NO_ERROR;
            }
        case APP_START_BYINTENT: {
            CHECK_INTERFACE(IAppManagerService, data, reply);
            const char *if_name = data.readCString();
            ALOGI("startAppByIntent(%s)", if_name);
            startAppByIntent_N(if_name);
            reply->writeInt32(0);
            return NO_ERROR;
            }
        case APP_STATUS_INSTALL: {
            CHECK_INTERFACE(IAppManagerService, data, reply);
            const char *if_name = data.readCString();
            ALOGI("installApp(%s)", if_name);
            installApp_N(if_name);
            reply->writeInt32(0);
            return NO_ERROR;
            }
        case APP_STATUS_ISINSTALLED: {
            CHECK_INTERFACE(IAppManagerService, data, reply);
            const char *if_name = data.readCString();
            ALOGI("isAppInstalled(%s)", if_name);
            reply->writeInt32(isAppInstalled_N(if_name));
            return NO_ERROR;
            }
        default:
            return BnAppManagerService::onTransact(code, data, reply, flags);
        }
        return UNKNOWN_TRANSACTION;
    }
    
    int AppManagerService::isAppInstalled_N(std::string appName) {
        return m_appManager->isAppInstalled((char*)appName.c_str());
    }
    

    这样一个Binder的例子就介绍完了。实际上BpAppManagerService只是为了便于分析才加上的,其实是可以更简洁的。Demo会放在GitHub上。

    整个文章还没有很好的串联起来,参考意义还不太好。需要多花时间去分析,去思考,在有了更好的理解的基础上再更新这篇文章。

    相关文章

      网友评论

        本文标题:Adnroid Binder分析

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