Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析

转载:http://blog.csdn.net/luoshengyang/article/details/6629298

本文通过一个具体的例子来说明Binder机制中Server的启动过程。在Android系统中,提供了多媒体播放的功能,这个功能是以服务的形式来提供的。这里,我们就通过分析MediaPlayerService的实现来了解Media Server的启动过程。

首先,看一下MediaPlayerService[1]的类图,以便我们理解下面要描述的内容。

这里写图片描述

我们将要介绍的主角MediaPlayerService[1]继承于BnMediaPlayerService类[1.1]

  • BnMediaPlayerService类[1.1]是一个Binder Native类,用来处理Client请求的。BnMediaPlayerService类[1.1]继承于BnInterface< IMediaPlayerService>类[1.1.1],BnInterface是一个模板类,它定义在frameworks/base/include/binder/IInterface.h文件中:
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();
};

这里可以看出,BnMediaPlayerService类[1.1]实际是继承了IMediaPlayerService[1.1.2]BBinder[1.1.3]类IMediaPlayerService[1.1.2]BBinder[1.1.3]类类又分别继承了IInterface[1.1.3.1]IBinder[1.1.3.2]类IInterface[1.1.3.1]IBinder[1.1.3.2]类又同时继承了RefBase[1.1.3.2.1]类

实际上,BnMediaPlayerService类[1.1]并不是直接收到Client处发送过来的请求,

  1. 使用了IPCThreadState[1.1.4]接收Client处发送过来的请求,
  2. IPCThreadState[1.1.4]又借助了ProcessState类[1.1.4.1]来与Binder驱动程序交互。
  3. ProcessState类[1.1.4.1]接收到了Client处的请求后,就会调用BBinder类的transact()[1.1.4.1.1]函数,并传入相关参数
  4. BBinder[1.1.3]类transact()[1.1.4.1.1]函数最终调用BnMediaPlayerService类[1.1]onTransact()[1.1.4.1.1.1]函数
  5. 于是,就开始真正地处理Client的请求了。

了解了MediaPlayerService[1]类结构之后,就要开始进入到本文的主题了。

MediaPlayerService[1]是如何启动

首先,启动MediaPlayerService[1]的代码位于frameworks/base/media/mediaserver/main_mediaserver.cpp文件中:

int main(int argc, char** argv)
{
    sp<ProcessState> proc(ProcessState::self());
    sp<IServiceManager> sm = defaultServiceManager();
    LOGI("ServiceManager: %p", sm.get());
    AudioFlinger::instantiate();
    MediaPlayerService::instantiate();
    CameraService::instantiate();
    AudioPolicyService::instantiate();
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
}

这里我们不关注AudioFlinger和CameraService相关的代码。
先看下面这句代码:

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

这句代码的作用是通过ProcessState::self()[1.2]调用创建一个ProcessState[1.3]实例ProcessState::self()[1.2]ProcessState[1.3]类的一个静态成员变量,定义在frameworks/base/libs/binder/ProcessState.cpp文件中:

sp<ProcessState> ProcessState::self()
{
    if (gProcess != NULL) return gProcess;

    AutoMutex _l(gProcessMutex);
    if (gProcess == NULL) gProcess = new ProcessState;
    return gProcess;
}

这个函数返回一个全局唯一的ProcessState[1.3]实例gProcess。全局唯一实例变量gProcess定义在frameworks/base/libs/binder/Static.cpp文件中:

Mutex gProcessMutex;
sp<ProcessState> gProcess;

再来看ProcessState[1.3]的构造函数:

ProcessState::ProcessState()
    : mDriverFD(open_driver())
    , 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*
            LOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");
            close(mDriverFD);
            mDriverFD = -1;
        }
#else
        mDriverFD = -1;
#endif
    }
    if (mDriverFD < 0) {
        // Need to run without the driver, starting our own thread pool.
    }
}

这个函数有两个关键地方,

  1. 通过open_driver()[1.3.1]函数打开Binder设备文件/dev/binder,并将打开设备文件描述符保存在成员变量mDriverFD[1.3.2]中;

  2. 通过mmap()[1.3.3]来把设备文件/dev/binder映射到内存中。

    先看open_driver()[1.3.1]函数的实现,这个函数同样位于frameworks/base/libs/binder/ProcessState.cpp文件中:

static int open_driver()
{
    if (gSingleProcess) {
        return -1;
    }

    int fd = open("/dev/binder", O_RDWR);
    if (fd >= 0) {
        fcntl(fd, F_SETFD, FD_CLOEXEC);
        int vers;
#if defined(HAVE_ANDROID_OS)
        status_t result = ioctl(fd, BINDER_VERSION, &vers);
#else
        status_t result = -1;
        errno = EPERM;
#endif
        if (result == -1) {
            LOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
            close(fd);
            fd = -1;
        }
        if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
            LOGE("Binder driver protocol does not match user space protocol!");
            close(fd);
            fd = -1;
        }
#if defined(HAVE_ANDROID_OS)
        size_t maxThreads = 15;
        result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
        if (result == -1) {
            LOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
        }
#endif

    } else {
        LOGW("Opening '/dev/binder' failed: %s\n", strerror(errno));
    }
    return fd;
}
  1. 这个函数的作用主要是通过open()[1.3.1.1]文件操作函数来打开/dev/binder设备文件
  2. 再调用ioctl()[1.3.1.2]文件控制函数来分别执行BINDER_VERSIONBINDER_SET_MAX_THREADS两个命令来和Binder驱动程序进行交互,前者用于获得当前Binder驱动程序的版本号,后者用于通知Binder驱动程序,

MediaPlayerService[1]最多可同时启动15个线程来处理Client端的请求。

打开/dev/binder设备文件后,Binder驱动程序就为MediaPlayerService[1]进程创建了一个struct binder_proc[1.4]结构体实例来维护MediaPlayerService[1]进程上下文相关信息。

ioctl()[1.3.1.2]文件控制函数执行BINDER_VERSION命令的过程:

status_t result = ioctl(fd, BINDER_VERSION, &vers);

这个函数调用最终进入到Binder驱动程序的binder_ioctl()[1.3.1.2.1] 函数中,我们只关注BINDER_VERSION[1.3.1.2.2]相关的部分逻辑:

static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    int ret;
    struct binder_proc *proc = filp->private_data;
    struct binder_thread *thread;
    unsigned int size = _IOC_SIZE(cmd);
    void __user *ubuf = (void __user *)arg;

    /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/

    ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
    if (ret)
        return ret;

    mutex_lock(&binder_lock);
    thread = binder_get_thread(proc);
    if (thread == NULL) {
        ret = -ENOMEM;
        goto err;
    }

    switch (cmd) {
    ......
    case BINDER_VERSION:
        if (size != sizeof(struct binder_version)) {
            ret = -EINVAL;
            goto err;
        }
        if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, &((struct binder_version *)ubuf)->protocol_version)) {
            ret = -EINVAL;
            goto err;
        }
        break;
    ......
    }
    ret = 0;
err:
        ......
    return ret;
}

很简单,只是将BINDER_CURRENT_PROTOCOL_VERSION[1.3.1.2.1.1]写入到传入的参数arg指向的用户缓冲区中去就返回了。
BINDER_CURRENT_PROTOCOL_VERSION[1.3.1.2.1.1]是一个宏,定义在kernel/common/drivers/staging/android/binder.h文件中:

/* This is the current protocol version. */
#define BINDER_CURRENT_PROTOCOL_VERSION 7


这里为什么要把ubuf[1.3.1.2.1.2]转换成struct binder_version[1.3.1.2.1.3]之后,再通过protocol_version成员变量写入呢,转了一圈,最终内容还是写入到ubuf中(29行)。

我们看一下struct binder_version[1.3.1.2.1.3]的定义就会明白,同样是在kernel/common/drivers/staging/android/binder.h文件中:

/* Use with BINDER_VERSION, driver fills in fields. */
struct binder_version {
    /* driver protocol version -- increment with incompatible change */
    signed long protocol_version;
};

从注释中可以看出来,这里是考虑到兼容性,因为以后很有可能不是用signed long表示版本号。

注意,由于这里是打开设备文件/dev/binder之后,第一次进入到binder_ioctl()[1.3.1.2.1] 函数,因此,调用binder_get_thread()[1.3.1.2.1.4]时,就会为当前线程创建一个struct binder_thread[1.3.1.2.1.5]结构体变量来维护线程上下文信息(16行)

再看一下ioctl()[1.3.1.2]文件控制函数执行BINDER_SET_MAX_THREADS[1.3.1.2.3]命令的过程:

result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);

这个函数调用最终进入到Binder驱动程序的binder_ioctl()[1.3.1.2.1] 中,我们只关注BINDER_SET_MAX_THREADS[1.3.1.2.3]相关的部分逻辑:

static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    int ret;
    struct binder_proc *proc = filp->private_data;
    struct binder_thread *thread;
    unsigned int size = _IOC_SIZE(cmd);
    void __user *ubuf = (void __user *)arg;

    /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/

    ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
    if (ret)
        return ret;

    mutex_lock(&binder_lock);
    thread = binder_get_thread(proc);
    if (thread == NULL) {
        ret = -ENOMEM;
        goto err;
    }

    switch (cmd) {
    ......
    case BINDER_SET_MAX_THREADS:
        if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
            ret = -EINVAL;
            goto err;
        }
        break;
    ......
    }
    ret = 0;
err:
    ......
    return ret;
}

把用户传进来的参数保存在proc->max_threads中。

注意,这里再调用binder_ioctl()[1.3.1.2.1] 函数时,就可在proc->threads中找到当前线程对应的struct binder_thread[1.3.1.2.1.5]结构了,因为前面已经创建好并保存在proc->threads红黑树中。(proc的类型是struct binder_proc [1.3.1.2.1.6])

回到ProcessState[1.3]的构造函数中,这里还通过mmap()[1.3.3]来把设备文件/dev/binder映射到内存中。宏BINDER_VM_SIZE[1.3.4]就定义在ProcessState.cpp文件中:

#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))

mmap()[1.3.3]调用完成之后,Binder驱动程序就为当前进程预留了BINDER_VM_SIZE[1.3.4]大小的内存空间了。

  1. ProcessState[1.3]全局唯一变量gProcess就创建完毕了,
  2. 回到MediaPlayerService[1]中的frameworks/base/media/mediaserver/main_mediaserver.cpp文件中的main()函数,
  3. 调用defaultServiceManager()[1.5]函数来获得Service Manager的远程接口,这个已经在上一篇文章有详细描述
  4. 进入到MediaPlayerService::instantiate()[1.6]函数MediaPlayerService[1]添加到Service Manger中去了。

MediaPlayerService::instantiate()[1.6]函数定义在frameworks/base/media/libmediaplayerservice/MediaPlayerService.cpp文件中:

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

重点看一下IServiceManger::addService()[1.6.1]的过程,这有助于我们加深对Binder机制的理解

defaultServiceManager()[1.6.2]函数返回的实际是一个BpServiceManger类实例,因此,我们看一下BpServiceManger::addService[1.6.1]的实现,这个函数实现在frameworks/base/libs/binder/IServiceManager.cpp文件中:

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)  
    {  
        Parcel data, reply;  
        data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());  
        data.writeString16(name);  
        data.writeStrongBinder(service);  
        status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);  
        return err == NO_ERROR ? reply.readExceptionCode()   
    }  

    ......  

};

这里Parcel类[1.6.1.1]是用来于序列化进程间通信数据用的。
1、 先来看这一句的调用:

data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());  

IServiceManager::getInterfaceDescriptor()[1.6.1.1.1]返回来的是一个字符串,即”android.os.IServiceManager“,具体可以参考IServiceManger的实现。

我们看一下Parcel::writeInterfaceToken()[1.6.1.1.2]函数的实现,位于frameworks/base/libs/binder/Parcel.cpp文件中:

// Write RPC headers.  (previously just the interface token)  
status_t Parcel::writeInterfaceToken(const String16& interface)  
{  
    writeInt32(IPCThreadState::self()->getStrictModePolicy() |  
               STRICT_MODE_PENALTY_GATHER);  
    // currently the interface identification token is just its name as a string  
    return writeString16(interface);  
} 

它的作用是写入一个整数和一个字符串到Parcel中去。

2、再来看下面的调用:

data.writeString16(name);  

这里又是写入一个字符串到Parcel中去,这里的name即是上面传进来的“media.player”字符串。

3、

data.writeStrongBinder(service);  

这里传入一个Binder对象到Parcel去。

我们重点看一下writeStrongBinder()[1.6.1.1.3]函数的实现,因为它涉及到进程间传输Binder实体的问题,比较复杂,需要重点关注,同时,也是理解Binder机制的一个重点所在。注意,这里的service参数是一个MediaPlayerService[1]对象。

status_t Parcel::writeStrongBinder(const sp<IBinder>& val)  
{  
    return flatten_binder(ProcessState::self(), val, this);  
} 

在Binder驱动程序中,使用struct flat_binder_object[1.6.1.1.3.1]来表示传输中的一个binder对象,它的定义如下所示:

/* 
 * This is the flattened representation of a Binder object for transfer 
 * between processes.  The 'offsets' supplied as part of a binder transaction 
 * contains offsets into the data where these structures occur.  The Binder 
 * driver takes care of re-writing the structure type and data as it moves 
 * between processes. 
 */  
struct flat_binder_object {  
    /* 8 bytes for large_flat_header. */  
    unsigned long       type;  
    unsigned long       flags;  

    /* 8 bytes of data. */  
    union {  
        void        *binder;    /* local object */  
        signed long handle;     /* remote object */  
    };  

    /* extra data associated with local object */  
    void            *cookie;  
};  

进入到flatten_binder()[1.6.1.1.3.2]函数看看:

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) {  
        IBinder *local = binder->localBinder();  
        if (!local) {  
            BpBinder *proxy = binder->remoteBinder();  
            if (proxy == NULL) {  
                LOGE("null proxy");  
            }  
            const int32_t handle = proxy ? proxy->handle() : 0;  
            obj.type = BINDER_TYPE_HANDLE;  
            obj.handle = handle;  
            obj.cookie = NULL;  
        } else {  
            obj.type = BINDER_TYPE_BINDER;  
            obj.binder = local->getWeakRefs();  
            obj.cookie = local;  
        }  
    } else {  
        obj.type = BINDER_TYPE_BINDER;  
        obj.binder = NULL;  
        obj.cookie = NULL;  
    }  

    return finish_flatten_binder(binder, obj, out);  
}  

首先是初始化struct flat_binder_object[1.6.1.1.3.1]的flags域:

obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;  
  • 0x7f表示处理本Binder实体请求数据包的线程的最低优先级,
  • FLAT_BINDER_FLAG_ACCEPTS_FDS表示这个Binder实体可以接受文件描述符,

Binder实体在收到文件描述符时,就会在本进程中打开这个文件。

传进来的binder即为MediaPlayerService::instantiate()[1.6]函数函数中new出来的MediaPlayerService[1]实例,因此,不为空。

又由于MediaPlayerService[1]继承自BBinder类,它是一个本地Binder实体,因此binder->localBinder返回一个BBinder指针,而且肯定不为空,于是执行下面语句:

obj.type = BINDER_TYPE_BINDER;  
obj.binder = local->getWeakRefs();  
obj.cookie = local;  
  • 设置了struct flat_binder_object[1.6.1.1.3.1]的其他成员变量。

注意,指向这个Binder实体地址的指针local保存在struct flat_binder_object[1.6.1.1.3.1]的成员变量cookie中。

函数调用finish_flatten_binder()[1.6.1.1.3.3]来将这个struct flat_binder_object[1.6.1.1.3.1]写入到Parcel类[1.6.1.1]中去:

inline static status_t finish_flatten_binder(  
    const sp<IBinder>& binder, const flat_binder_object& flat, Parcel* out)  
{  
    return out->writeObject(flat, false);  
} 

Parcel::writeObject()[1.6.1.1.4]的实现如下:

status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)  
{  
    const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;  
    const bool enoughObjects = mObjectsSize < mObjectsCapacity;  
    if (enoughData && enoughObjects) {  
restart_write:  
        *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;  

        // Need to write meta-data?  
        if (nullMetaData || val.binder != NULL) {  
            mObjects[mObjectsSize] = mDataPos;  
            acquire_object(ProcessState::self(), val, this);  
            mObjectsSize++;  
        }  

        // remember if it's a file descriptor  
        if (val.type == BINDER_TYPE_FD) {  
            mHasFds = mFdsKnown = true;  
        }  

        return finishWrite(sizeof(flat_binder_object));  
    }  

    if (!enoughData) {  
        const status_t err = growData(sizeof(val));  
        if (err != NO_ERROR) return err;  
    }  
    if (!enoughObjects) {  
        size_t newSize = ((mObjectsSize+2)*3)/2;  
        size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));  
        if (objects == NULL) return NO_MEMORY;  
        mObjects = objects;  
        mObjectsCapacity = newSize;  
    }  

    goto restart_write;  
}  
  • struct flat_binder_object[1.6.1.1.3.1]写到Parcel类[1.6.1.1]里面之内
  • 还要记录这个struct flat_binder_object[1.6.1.1.3.1]Parcel类[1.6.1.1]里面的偏移位置(11行)

这里因为,如果进程间传输的数据间带有Binder对象的时候,Binder驱动程序需要作进一步的处理,以维护各个Binder实体的一致性,下面我们将会看到Binder驱动程序是怎么处理这些Binder对象的。

再回到BpServiceManger::addService[1.6.1]函数中,调用下面语句:

status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);  

这里的remote()[1.6.1.2]成员函数来自于BpRefBase类[1.6.1.2.1],它返回一个BpBinder[1.6.1.3]指针

因此,我们继续进入到BpBinder::transact()[1.6.1.4]函数中去看看:

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

这里又调用了IPCThreadState::transact()[1.6.1.4.1]进执行实际的操作。
注意,这里的

  • mHandle为0,
  • code为ADD_SERVICE_TRANSACTION。

ADD_SERVICE_TRANSACTION是上面以参数形式传进来的,那mHandle为什么是0呢?
因为这里表示的是Service Manager远程接口,它的句柄值一定是0

再进入到IPCThreadState::transact()[1.6.1.4.1],看看做了些什么事情:

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  
            LOGI(">>>>>> CALLING transaction 4");  
        } else {  
            LOGI(">>>>>> CALLING transaction %d", code);  
        }  
        #endif  
        if (reply) {  
            err = waitForResponse(reply);  
        } else {  
            Parcel fakeReply;  
            err = waitForResponse(&fakeReply);  
        }  
        #if 0  
        if (code == 4) { // relayout  
            LOGI("<<<<<< RETURNING transaction 4");  
        } else {  
            LOGI("<<<<<< 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()[1.6.1.4.1]函数的参数flags是一个默认值为0的参数,上面没有传相应的实参进来,因此,这里就为0。

函数首先调用writeTransactionData()[1.6.1.4.1.1]函数(19行)准备好一个struct binder_transaction_data结构体变量[1.6.1.4.1.2],这个是等一下要传输给Binder驱动程序的。

struct binder_transaction_data结构体变量[1.6.1.4.1.2]的定义:

struct binder_transaction_data {  
    /* The first two are only used for bcTRANSACTION and brTRANSACTION, 
     * identifying the target and contents of the transaction. 
     */  
    union {  
        size_t  handle; /* target descriptor of command transaction */  
        void    *ptr;   /* target descriptor of return transaction */  
    } target;  
    void        *cookie;    /* target object cookie */  
    unsigned int    code;       /* transaction command */  

    /* General information about the transaction. */  
    unsigned int    flags;  
    pid_t       sender_pid;  
    uid_t       sender_euid;  
    size_t      data_size;  /* number of bytes of data */  
    size_t      offsets_size;   /* number of bytes of offsets */  

    /* If this transaction is inline, the data immediately 
     * follows here; otherwise, it ends with a pointer to 
     * the data buffer. 
     */  
    union {  
        struct {  
            /* transaction data */  
            const void  *buffer;  
            /* offsets from buffer to flat_binder_object structs */  
            const void  *offsets;  
        } ptr;  
        uint8_t buf[8];  
    } data;  
};  

writeTransactionData()[1.6.1.4.1.1]函数的实现如下:

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.handle = handle;  
    tr.code = code;  
    tr.flags = binderFlags;  

    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(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 = statusBuffer;  
        tr.offsets_size = 0;  
        tr.data.ptr.offsets = NULL;  
    } else {  
        return (mLastError = err);  
    }  

    mOut.writeInt32(cmd);  
    mOut.write(&tr, sizeof(tr));  

    return NO_ERROR;  
}  

注意,这里的cmd为BC_TRANSACTION。

这个函数很简单,在这个场景下,就是执行下面语句来初始化本地变量tr(struct binder_transaction_data结构体变量[1.6.1.4.1.2]):

tr.data_size = data.ipcDataSize();  
tr.data.ptr.buffer = data.ipcData();  
tr.offsets_size = data.ipcObjectsCount()*sizeof(size_t);  
tr.data.ptr.offsets = data.ipcObjects();  

回忆一下上面的内容,写入到tr.data.ptr.buffer的内容相当于下面的内容:

writeInt32(IPCThreadState::self()->getStrictModePolicy() |  
               STRICT_MODE_PENALTY_GATHER);  
writeString16("android.os.IServiceManager");  
writeString16("media.player");  
writeStrongBinder(new MediaPlayerService());  

其中包含了一个Binder实体MediaPlayerService[1]

  • 因此需要设置tr.offsets_size就为1
  • tr.data.ptr.offsets就指向了这个MediaPlayerService[1]的地址在tr.data.ptr.buffer中的偏移量。
  • 最后,将tr的内容保存在IPCThreadState的成员变量mOut中。

    回到 IPCThreadState::transact()[1.6.1.4.1]函数中
    接下去看,

  • (flags & TF_ONE_WAY) == 0为true(27行),

  • 并且reply不为空,
  • 所以最终进入到waitForResponse(reply)(36行)

我们看一下 waitForResponse()[1.6.1.4.1.3]函数的实现:

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:  
            {  
                LOG_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));  
                LOG_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 size_t*>(tr.data.ptr.offsets),  
                            tr.offsets_size/sizeof(size_t),  
                            freeBuffer, this);  
                    } else {  
                        err = *static_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 size_t*>(tr.data.ptr.offsets),  
                            tr.offsets_size/sizeof(size_t), this);  
                    }  
                } else {  
                    freeBuffer(NULL,  
                        reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),  
                        tr.data_size,  
                        reinterpret_cast<const size_t*>(tr.data.ptr.offsets),  
                        tr.offsets_size/sizeof(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()[1.6.1.4.1.3.1]函数(7行)来与Binder驱动程序进行交互:

status_t IPCThreadState::talkWithDriver(bool doReceive)  
{  
    LOG_ASSERT(mProcess->mDriverFD >= 0, "Binder driver is not opened");  

    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 = (long unsigned int)mOut.data();  

    // This is what we'll read.  
    if (doReceive && needRead) {  
        bwr.read_size = mIn.dataCapacity();  
        bwr.read_buffer = (long unsigned int)mIn.data();  
    } else {  
        bwr.read_size = 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_LOG_COMMANDS() {  
            alog << "Finished read/write, write size = " << mOut.dataSize() << endl;  
        }  
    } while (err == -EINTR);  

    IF_LOG_COMMANDS() {  
        alog << "Our err: " << (void*)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 < (ssize_t)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;  
}  

这里doReceive和needRead均为1,有兴趣的读者可以自已分析一下。

这里告诉Binder驱动程序,

  1. 先执行write操作,
  2. 再执行read操作,

下面我们将会看到。

通过ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr)[1.6.1.4.1.3.1.1]函数(51行)进行到Binder驱动程序的binder_ioctl()[1.6.1.4.1.3.1.1]函数,我们只关注cmd为BINDER_WRITE_READ的逻辑:

static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)  
{  
    int ret;  
    struct binder_proc *proc = filp->private_data;  
    struct binder_thread *thread;  
    unsigned int size = _IOC_SIZE(cmd);  
    void __user *ubuf = (void __user *)arg;  

    /*printk(KERN_INFO "binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/  

    ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);  
    if (ret)  
        return ret;  

    mutex_lock(&binder_lock);  
    thread = binder_get_thread(proc);  
    if (thread == NULL) {  
        ret = -ENOMEM;  
        goto err;  
    }  

    switch (cmd) {  
    case BINDER_WRITE_READ: {  
        struct binder_write_read bwr;  
        if (size != sizeof(struct binder_write_read)) {  
            ret = -EINVAL;  
            goto err;  
        }  
        if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {  
            ret = -EFAULT;  
            goto err;  
        }  
        if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)  
            printk(KERN_INFO "binder: %d:%d write %ld at %08lx, read %ld at %08lx\n",  
            proc->pid, thread->pid, bwr.write_size, bwr.write_buffer, bwr.read_size, bwr.read_buffer);  
        if (bwr.write_size > 0) {  
            ret = binder_thread_write(proc, thread, (void __user *)bwr.write_buffer, bwr.write_size, &bwr.write_consumed);  
            if (ret < 0) {  
                bwr.read_consumed = 0;  
                if (copy_to_user(ubuf, &bwr, sizeof(bwr)))  
                    ret = -EFAULT;  
                goto err;  
            }  
        }  
        if (bwr.read_size > 0) {  
            ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);  
            if (!list_empty(&proc->todo))  
                wake_up_interruptible(&proc->wait);  
            if (ret < 0) {  
                if (copy_to_user(ubuf, &bwr, sizeof(bwr)))  
                    ret = -EFAULT;  
                goto err;  
            }  
        }  
        if (binder_debug_mask & BINDER_DEBUG_READ_WRITE)  
            printk(KERN_INFO "binder: %d:%d wrote %ld of %ld, read return %ld of %ld\n",  
            proc->pid, thread->pid, bwr.write_consumed, bwr.write_size, bwr.read_consumed, bwr.read_size);  
        if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {  
            ret = -EFAULT;  
            goto err;  
        }  
        break;  
    }  
    ......  
    }  
    ret = 0;  
err:  
    ......  
    return ret;  
}  

1、函数首先是将用户传进来的参数拷贝到本地变量struct binder_write_read bwr[1.6.1.4.1.3.1.1.1](24行)中去。
2、这里bwr.write_size > 0true,因此,进入到binder_thread_write()[1.6.1.4.1.3.1.1.2]函数中,我们只关注BC_TRANSACTION部分的逻辑:

binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,  
                    void __user *buffer, int size, signed long *consumed)  
{  
    uint32_t cmd;  
    void __user *ptr = buffer + *consumed;  
    void __user *end = buffer + size;  

    while (ptr < end && thread->return_error == BR_OK) {  
        if (get_user(cmd, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
        if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {  
            binder_stats.bc[_IOC_NR(cmd)]++;  
            proc->stats.bc[_IOC_NR(cmd)]++;  
            thread->stats.bc[_IOC_NR(cmd)]++;  
        }  
        switch (cmd) {  
            .....  
        case BC_TRANSACTION:  
        case BC_REPLY: {  
            struct binder_transaction_data tr;  

            if (copy_from_user(&tr, ptr, sizeof(tr)))  
                return -EFAULT;  
            ptr += sizeof(tr);  
            binder_transaction(proc, thread, &tr, cmd == BC_REPLY);  
            break;  
        }  
        ......  
        }  
        *consumed = ptr - buffer;  
    }  
    return 0;  
}  

首先将用户传进来的transact参数拷贝在本地变量struct binder_transaction_data[1.6.1.4.1.2] tr(21行)中去,
接着调用binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数(26行)进一步处理,这里我们忽略掉无关代码:

static void  
binder_transaction(struct binder_proc *proc, struct binder_thread *thread,  
struct binder_transaction_data *tr, int reply)  
{  
    struct binder_transaction *t;  
    struct binder_work *tcomplete;  
    size_t *offp, *off_end;  
    struct binder_proc *target_proc;  
    struct binder_thread *target_thread = NULL;  
    struct binder_node *target_node = NULL;  
    struct list_head *target_list;  
    wait_queue_head_t *target_wait;  
    struct binder_transaction *in_reply_to = NULL;  
    struct binder_transaction_log_entry *e;  
    uint32_t return_error;  

        ......  

    if (reply) {  
         ......  
    } else {  
        if (tr->target.handle) {  
            ......  
        } else {  
            target_node = binder_context_mgr_node;  
            if (target_node == NULL) {  
                return_error = BR_DEAD_REPLY;  
                goto err_no_context_mgr_node;  
            }  
        }  
        ......  
        target_proc = target_node->proc;  
        if (target_proc == NULL) {  
            return_error = BR_DEAD_REPLY;  
            goto err_dead_binder;  
        }  
        ......  
    }  
    if (target_thread) {  
        ......  
    } else {  
        target_list = &target_proc->todo;  
        target_wait = &target_proc->wait;  
    }  

    ......  

    /* TODO: reuse incoming transaction for reply */  
    t = kzalloc(sizeof(*t), GFP_KERNEL);  
    if (t == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_alloc_t_failed;  
    }  
    ......  

    tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);  
    if (tcomplete == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_alloc_tcomplete_failed;  
    }  

    ......  

    if (!reply && !(tr->flags & TF_ONE_WAY))  
        t->from = thread;  
    else  
        t->from = NULL;  
    t->sender_euid = proc->tsk->cred->euid;  
    t->to_proc = target_proc;  
    t->to_thread = target_thread;  
    t->code = tr->code;  
    t->flags = tr->flags;  
    t->priority = task_nice(current);  
    t->buffer = binder_alloc_buf(target_proc, tr->data_size,  
        tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));  
    if (t->buffer == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_binder_alloc_buf_failed;  
    }  
    t->buffer->allow_user_free = 0;  
    t->buffer->debug_id = t->debug_id;  
    t->buffer->transaction = t;  
    t->buffer->target_node = target_node;  
    if (target_node)  
        binder_inc_node(target_node, 1, 0, NULL);  

    offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));  

    if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {  
        ......  
        return_error = BR_FAILED_REPLY;  
        goto err_copy_data_failed;  
    }  
    if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {  
        ......  
        return_error = BR_FAILED_REPLY;  
        goto err_copy_data_failed;  
    }  
    ......  

    off_end = (void *)offp + tr->offsets_size;  
    for (; offp < off_end; offp++) {  
        struct flat_binder_object *fp;  
        ......  
        fp = (struct flat_binder_object *)(t->buffer->data + *offp);  
        switch (fp->type) {  
        case BINDER_TYPE_BINDER:  
        case BINDER_TYPE_WEAK_BINDER: {  
            struct binder_ref *ref;  
            struct binder_node *node = binder_get_node(proc, fp->binder);  
            if (node == NULL) {  
                node = binder_new_node(proc, fp->binder, fp->cookie);  
                if (node == NULL) {  
                    return_error = BR_FAILED_REPLY;  
                    goto err_binder_new_node_failed;  
                }  
                node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;  
                node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);  
            }  
            if (fp->cookie != node->cookie) {  
                ......  
                goto err_binder_get_ref_for_node_failed;  
            }  
            ref = binder_get_ref_for_node(target_proc, node);  
            if (ref == NULL) {  
                return_error = BR_FAILED_REPLY;  
                goto err_binder_get_ref_for_node_failed;  
            }  
            if (fp->type == BINDER_TYPE_BINDER)  
                fp->type = BINDER_TYPE_HANDLE;  
            else  
                fp->type = BINDER_TYPE_WEAK_HANDLE;  
            fp->handle = ref->desc;  
            binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, &thread->todo);  
            ......  

        } break;  
        ......  
        }  
    }  

    if (reply) {  
        ......  
    } else if (!(t->flags & TF_ONE_WAY)) {  
        BUG_ON(t->buffer->async_transaction != 0);  
        t->need_reply = 1;  
        t->from_parent = thread->transaction_stack;  
        thread->transaction_stack = t;  
    } else {  
        ......  
    }  
    t->work.type = BINDER_WORK_TRANSACTION;  
    list_add_tail(&t->work.entry, target_list);  
    tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;  
    list_add_tail(&tcomplete->entry, &thread->todo);  
    if (target_wait)  
        wake_up_interruptible(target_wait);  
    return;  
    ......  
}  

注意,这里传进来的参数reply为0,tr->target.handle也为0

因此,target_proc、target_thread、target_node、target_list和target_wait的值分别为:

target_node = binder_context_mgr_node;  
target_proc = target_node->proc;  
target_list = &target_proc->todo;  
target_wait = &target_proc->wait; 

接着,分配了

  • 一个待处理事务t[1.6.1.4.1.3.1.1.2.1.1](49行)
  • 一个待完成工作项tcomplete[1.6.1.4.1.3.1.1.2.1.2](56行)
  • 并执行初始化工作

    • 这里的待处理事务t[1.6.1.4.1.3.1.1.2.1.1](49行)是要交给target_proc(69行)[1.6.1.4.1.3.1.1.2.1.4]处理的,
  • 在这个场景之下,就是Service Manager了。因此,(74行)的语句
    就是在Service Manager的进程空间中分配一块内存来保存用户传进入的参数了:

if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {  
    ......  
    return_error = BR_FAILED_REPLY;  
    goto err_copy_data_failed;  
}  
if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {  
    ......  
    return_error = BR_FAILED_REPLY;  
    goto err_copy_data_failed;  
}  
  • 由于现在target_node要被使用了,增加它的引用计数:
if (target_node)  
        binder_inc_node(target_node, 1, 0, NULL);  
  • 接下去的for循环,就是用来处理传输数据中的Binder对象了。在我们的场景中,有一个类型为BINDER_TYPE_BINDER的Binder实体MediaPlayerService[1]
 switch (fp->type) {  
   case BINDER_TYPE_BINDER:  
   case BINDER_TYPE_WEAK_BINDER: {  
struct binder_ref *ref;  
struct binder_node *node = binder_get_node(proc, fp->binder);  
if (node == NULL) {  
    node = binder_new_node(proc, fp->binder, fp->cookie);  
    if (node == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_binder_new_node_failed;  
    }  
    node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;  
    node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);  
}  
if (fp->cookie != node->cookie) {  
    ......  
    goto err_binder_get_ref_for_node_failed;  
}  
ref = binder_get_ref_for_node(target_proc, node);  
if (ref == NULL) {  
    return_error = BR_FAILED_REPLY;  
    goto err_binder_get_ref_for_node_failed;  
}  
if (fp->type == BINDER_TYPE_BINDER)  
    fp->type = BINDER_TYPE_HANDLE;  
else  
    fp->type = BINDER_TYPE_WEAK_HANDLE;  
fp->handle = ref->desc;  
binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, &thread->todo);  
......  

} break;  
  • 由于是第一次在Binder驱动程序中传输这个MediaPlayerService[1],调用binder_get_node()[1.6.1.4.1.3.1.1.2.1.3]函数(5行)查询这个Binder实体时,
  • 会返回空,于是binder_new_node()[1.6.1.4.1.3.1.1.2.1.4]函数(7行)在proc中新建一个,下次就可以直接使用了。

  • 现在,由于要把这个Binder实体MediaPlayerService[1]交给target_proc(69行)[1.6.1.4.1.3.1.1.2.1.4],也就是Service Manager来管理,

    • 也就是说Service Manager要引用这个MediaPlayerService[1]了,
  • 于是通过binder_get_ref_for_node()函数[1.6.1.4.1.3.1.1.2.1.5](17行)MediaPlayerService[1]创建一个引用,

  • 并且通过binder_inc_ref来增加这个引用计数,防止这个引用还在使用过程当中就被销毁。

注意,到了这里的时候,
t->buffer中的

  • flat_binder_obj的type已经改为BINDER_TYPE_HANDLE,
  • handle已经改为ref->desc

跟原来不一样了,因为这个flat_binder_obj是最终是要传给Service Manager的,而Service Manager只能够通过句柄值来引用这个Binder实体。

  • 最后,把待处理事务加入到target_list[1.6.1.4.1.3.1.1.2.1.6]列表中去(153行)
  • 并且把待完成工作项加入到本线程的todo等待执行列表中去(155行)
  • 现在目标进程有事情可做了,于是唤醒它(156行)

    • 这里就是要唤醒Service Manager进程了。此时, Service Manager正在binder_thread_read()[1.3.1.2.1.7]函数 函数中调用wait_event_interruptible进入休眠状态。

    这里我们先忽略一下Service Manager被唤醒之后的场景,继续MediaPlayerService[1]的启动过程,然后再回来。

    回到binder_ioctl()[1.3.1.2.1] 函数,bwr.read_size > 0为true,于是进入binder_thread_read()[1.3.1.2.1.7]函数

static int  
binder_thread_read(struct binder_proc *proc, struct binder_thread *thread,  
                   void  __user *buffer, int size, signed long *consumed, int non_block)  
{  
    void __user *ptr = buffer + *consumed;  
    void __user *end = buffer + size;  

    int ret = 0;  
    int wait_for_proc_work;  

    if (*consumed == 0) {  
        if (put_user(BR_NOOP, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
    }  

retry:  
    wait_for_proc_work = thread->transaction_stack == NULL && list_empty(&thread->todo);  

    .......  

    if (wait_for_proc_work) {  
        .......  
    } else {  
        if (non_block) {  
            if (!binder_has_thread_work(thread))  
                ret = -EAGAIN;  
        } else  
            ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));  
    }  

    ......  

    while (1) {  
        uint32_t cmd;  
        struct binder_transaction_data tr;  
        struct binder_work *w;  
        struct binder_transaction *t = NULL;  

        if (!list_empty(&thread->todo))  
            w = list_first_entry(&thread->todo, struct binder_work, entry);  
        else if (!list_empty(&proc->todo) && wait_for_proc_work)  
            w = list_first_entry(&proc->todo, struct binder_work, entry);  
        else {  
            if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */  
                goto retry;  
            break;  
        }  

        if (end - ptr < sizeof(tr) + 4)  
            break;  

        switch (w->type) {  
        ......  
        case BINDER_WORK_TRANSACTION_COMPLETE: {  
            cmd = BR_TRANSACTION_COMPLETE;  
            if (put_user(cmd, (uint32_t __user *)ptr))  
                return -EFAULT;  
            ptr += sizeof(uint32_t);  

            binder_stat_br(proc, thread, cmd);  
            if (binder_debug_mask & BINDER_DEBUG_TRANSACTION_COMPLETE)  
                printk(KERN_INFO "binder: %d:%d BR_TRANSACTION_COMPLETE\n",  
                proc->pid, thread->pid);  

            list_del(&w->entry);  
            kfree(w);  
            binder_stats.obj_deleted[BINDER_STAT_TRANSACTION_COMPLETE]++;  
                                               } break;  
        ......  
        }  

        if (!t)  
            continue;  

        ......  
    }  

done:  
    ......  
    return 0;  
} 
  • thread->transaction_stack(18行)[1.3.1.2.1.7.1] thread->todo(40行)[1.3.1.2.1.7.2] 均不为空,于是wait_for_proc_work为0,
  • 由于执行binder_has_thread_work()(26行)[1.3.1.2.1.7.3]时候,返回true,这里因为thread->todo(40行)[1.3.1.2.1.7.2] 不为空,因此,线程虽然调用了wait_event_interruptible()函数[1.3.1.2.1.7.4](29行),但是不会睡眠,于是继续往下执行。
  • 由于thread->todo(40行)[1.3.1.2.1.7.2] 不为空,接着执行40行语句
  • w->type为BINDER_WORK_TRANSACTION_COMPLETE,这是在上面的binder_transaction函数设置的,接着执行53-71行。
  • 这里就将w从thread->todo(40行)[1.3.1.2.1.7.2] 删除了。由于t为空(73行),重新执行while循环,这时由于已经没有事情可做了,最后就返回到binder_ioctl()[1.3.1.2.1] 函数中。
    • 注意,这里一共往用户传进来的缓冲区buffer写入了两个整数,分别是BR_NOOP和BR_TRANSACTION_COMPLETE。

binder_ioctl()[1.3.1.2.1] 函数返回到用户空间之前,把数据消耗情况拷贝回用户空间中:

if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {  
    ret = -EFAULT;  
    goto err;  
}

最后返回到IPCThreadState::talkWithDriver()[1.6.1.4.1.3.1]函数函数中,执行下面语句:

] view plain copy 在CODE上查看代码片派生到我的代码片
    if (err >= NO_ERROR) {  
        if (bwr.write_consumed > 0) {  
            if (bwr.write_consumed < (ssize_t)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);        }        ......        return NO_ERROR;    }  
  • 首先是把mOut的数据清空:(6行)
  • 设置已经读取的内容的大小:(9行)

然后返回到IPCThreadState::waitForResponse()[1.6.1.4.1.3]函数中。在IPCThreadState::waitForResponse()[1.6.1.4.1.3]函数,

  • 先是从mIn读出一个整数,这个便是BR_NOOP了,这是一个空操作,什么也不做。
  • 然后继续进入IPCThreadState::talkWithDriver()[1.6.1.4.1.3.1]函数中,执行这个函数中的第8行。

就是这条语句:

const bool needRead = mIn.dataPosition() >= mIn.dataSize();  
  • needRead为false,因为在mIn中,尚有一个整数BR_TRANSACTION_COMPLETE未读出。
  • 接着执行第13行,就是下面这条语句
const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;  

outAvail等于0。

因此,

  • 最后bwr.write_sizebwr.read_size均为0,IPCThreadState::talkWithDriver()[1.6.1.4.1.3.1]函数什么也不做,直接返回到IPCThreadState::waitForResponse()[1.6.1.4.1.3]函数中。

  • IPCThreadState::waitForResponse()[1.6.1.4.1.3]函数,又继续从mIn读出一个整数,这个便是BR_TRANSACTION_COMPLETE:(这个函数中的第20行,就是下面这条语句)

switch (cmd) {  
case BR_TRANSACTION_COMPLETE:  
       if (!reply && !acquireResult) goto finish;  
       break;  
......  
} 

reply不为NULL,因此,IPCThreadState::waitForResponse()[1.6.1.4.1.3]函数的循环没有结束,继续执行,又进入到IPCThreadState::talkWithDriver()[1.6.1.4.1.3.1]函数中。

这次,

  • needRead就为true了,而outAvail仍为0,
  • 所以bwr.read_size不为0,bwr.write_size为0。

于是执行:(函数中的51行,就是下面这条语句)

ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr)

进入到Binder驱动程序中的binder_ioctl()[1.6.1.4.1.3.1.1]函数中。

  • 由于bwr.write_size为0,bwr.read_size不为0,
  • 这次直接就进入到binder_thread_read函数中。

这时候,thread->transaction_stack[1.3.1.2.1.7.1] 不等于0,但是thread->todo[1.3.1.2.1.7.2] 为空,于是线程就通过:

wait_event_interruptible(thread->wait, binder_has_thread_work(thread)); 

进入睡眠状态,等待Service Manager来唤醒了。

现在,我们可以回到Service Manager被唤醒的过程了。

  • 我们接着前面浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路这篇文章的最后,继续描述。
  • 此时, Service Manager正在binder_thread_read()[1.3.1.2.1.7]函数 中调用wait_event_interruptible_exclusive进入休眠状态。

  • 上面被MediaPlayerService[1]启动后进程唤醒后,继续执行binder_thread_read()[1.3.1.2.1.7]函数

static int  
binder_thread_read(struct binder_proc *proc, struct binder_thread *thread,  
                   void  __user *buffer, int size, signed long *consumed, int non_block)  
{  
    void __user *ptr = buffer + *consumed;  
    void __user *end = buffer + size;  

    int ret = 0;  
    int wait_for_proc_work;  

    if (*consumed == 0) {  
        if (put_user(BR_NOOP, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
    }  

retry:  
    wait_for_proc_work = thread->transaction_stack == NULL && list_empty(&thread->todo);  

    ......  

    if (wait_for_proc_work) {  
        ......  
        if (non_block) {  
            if (!binder_has_proc_work(proc, thread))  
                ret = -EAGAIN;  
        } else  
            ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));  
    } else {  
        ......  
    }  

    ......  

    while (1) {  
        uint32_t cmd;  
        struct binder_transaction_data tr;  
        struct binder_work *w;  
        struct binder_transaction *t = NULL;  

        if (!list_empty(&thread->todo))  
            w = list_first_entry(&thread->todo, struct binder_work, entry);  
        else if (!list_empty(&proc->todo) && wait_for_proc_work)  
            w = list_first_entry(&proc->todo, struct binder_work, entry);  
        else {  
            if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */  
                goto retry;  
            break;  
        }  

        if (end - ptr < sizeof(tr) + 4)  
            break;  

        switch (w->type) {  
        case BINDER_WORK_TRANSACTION: {  
            t = container_of(w, struct binder_transaction, work);  
                                      } break;  
        ......  
        }  

        if (!t)  
            continue;  

        BUG_ON(t->buffer == NULL);  
        if (t->buffer->target_node) {  
            struct binder_node *target_node = t->buffer->target_node;  
            tr.target.ptr = target_node->ptr;  
            tr.cookie =  target_node->cookie;  
            ......  
            cmd = BR_TRANSACTION;  
        } else {  
            ......  
        }  
        tr.code = t->code;  
        tr.flags = t->flags;  
        tr.sender_euid = t->sender_euid;  

        if (t->from) {  
            struct task_struct *sender = t->from->proc->tsk;  
            tr.sender_pid = task_tgid_nr_ns(sender, current->nsproxy->pid_ns);  
        } else {  
            tr.sender_pid = 0;  
        }  

        tr.data_size = t->buffer->data_size;  
        tr.offsets_size = t->buffer->offsets_size;  
        tr.data.ptr.buffer = (void *)t->buffer->data + proc->user_buffer_offset;  
        tr.data.ptr.offsets = tr.data.ptr.buffer + ALIGN(t->buffer->data_size, sizeof(void *));  

        if (put_user(cmd, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
        if (copy_to_user(ptr, &tr, sizeof(tr)))  
            return -EFAULT;  
        ptr += sizeof(tr);  

        ......  

        list_del(&t->work.entry);  
        t->buffer->allow_user_free = 1;  
        if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {  
            t->to_parent = thread->transaction_stack;  
            t->to_thread = thread;  
            thread->transaction_stack = t;  
        } else {  
            t->buffer->transaction = NULL;  
            kfree(t);  
            binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;  
        }  
        break;  
    }  

done:  

    ......  
    return 0;  
} 

Service Manager被唤醒之后,就进入while循环开始处理事务了。

  • 这里wait_for_proc_work等于1,并且proc->todo不为空,
  • 所以从proc->todo列表中得到第一个工作项(第44行)
  • 从上面的描述中,我们知道,这个工作项的类型为BINDER_WORK_TRANSACTION,于是通过56行语句得到事务项
  • 接着就是把事务项t中的数据拷贝到本地局部变量struct binder_transaction_data结构体变量[1.6.1.4.1.2] tr中去了(65-88行)

    这里有一个非常重要的地方,是Binder进程间通信机制的精髓所在:(第87-88行)

tr.data.ptr.buffer = (void *)t->buffer->data + proc->user_buffer_offset;  
tr.data.ptr.offsets = tr.data.ptr.buffer + ALIGN(t->buffer->data_size, sizeof(void *)); 
  • t->buffer->data所指向的地址是内核空间的,
  • 现在要把数据返回给Service Manager进程的用户空间,
  • 而Service Manager进程的用户空间是不能访问内核空间的数据的

    怎么处理呢?

我们在学面向对象语言的时候,对象的拷贝有深拷贝和浅拷贝之分,深拷贝是把另外分配一块新内存,然后把原始对象的内容搬过去,浅拷贝是并没有为新对象分配一块新空间,而只是分配一个引用,而个引用指向原始对象。

  • Binder机制用的是类似浅拷贝的方法,通过在用户空间分配一个虚拟地址,
  • 然后让这个用户空间虚拟地址与 t->buffer->data这个内核空间虚拟地址指向同一个物理地址,这样就可以实现浅拷贝了。


怎么样用户空间和内核空间的虚拟地址同时指向同一个物理地址呢?

请参考前面一篇文章浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路,那里有详细描述。

  • 这里只要将t->buffer->data加上一个偏移值proc->user_buffer_offset就可以得到t->buffer->data对应的用户空间虚拟地址了。
  • 调整了tr.data.ptr.buffer的值之后,不要忘记也要一起调整tr.data.ptr.offsets的值。
  • 接着就是把tr的内容拷贝到用户传进来的缓冲区去了,指针ptr指向这个用户缓冲区的地址:(第90-95行,就是下面这些语句)
if (put_user(cmd, (uint32_t __user *)ptr))  
    return -EFAULT;  
ptr += sizeof(uint32_t);  
if (copy_to_user(ptr, &tr, sizeof(tr)))  
    return -EFAULT;  
ptr += sizeof(tr);  

这里只是对作tr.data.ptr.bufferr和tr.data.ptr.offsets的内容作了浅拷贝。

  • 最后,由于已经处理了这个事务,要把它从todo列表中删除(99-109行,就是下面这些语句):
list_del(&t->work.entry);  
t->buffer->allow_user_free = 1;  
if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {  
    t->to_parent = thread->transaction_stack;  
    t->to_thread = thread;  
    thread->transaction_stack = t;  
} else {  
    t->buffer->transaction = NULL;  
    kfree(t);  
    binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;  
}  
  • 注意,这里的cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)为true,表明这个事务虽然在驱动程序中已经处理完了,但是它仍然要等待Service Manager完成之后,给驱动程序一个确认,也就是需要等待回复,于是把当前事务t放在thread->transaction_stack[1.3.1.2.1.7.1] 队列的头部(上面代码4-6行)

  • 如果cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)为false,那就不需要等待回复了,直接把事务t删掉。

binder_thread_read()[1.3.1.2.1.7]函数 中的while最后通过一个break跳了出来(110行),最后返回到binder_ioctl()[1.6.1.4.1.3.1.1]函数中:

static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)  
{  
    int ret;  
    struct binder_proc *proc = filp->private_data;  
    struct binder_thread *thread;  
    unsigned int size = _IOC_SIZE(cmd);  
    void __user *ubuf = (void __user *)arg;  

    ......  

    switch (cmd) {  
    case BINDER_WRITE_READ: {  
        struct binder_write_read bwr;  
        if (size != sizeof(struct binder_write_read)) {  
            ret = -EINVAL;  
            goto err;  
        }  
        if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {  
            ret = -EFAULT;  
            goto err;  
        }  
        ......  
        if (bwr.read_size > 0) {  
            ret = binder_thread_read(proc, thread, (void __user *)bwr.read_buffer, bwr.read_size, &bwr.read_consumed, filp->f_flags & O_NONBLOCK);  
            if (!list_empty(&proc->todo))  
                wake_up_interruptible(&proc->wait);  
            if (ret < 0) {  
                if (copy_to_user(ubuf, &bwr, sizeof(bwr)))  
                    ret = -EFAULT;  
                goto err;  
            }  
        }  
        ......  
        if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {  
            ret = -EFAULT;  
            goto err;  
        }  
        break;  
        }  
    ......  
    default:  
        ret = -EINVAL;  
        goto err;  
    }  
    ret = 0;  
err:  
    ......  
    return ret;  
}  

binder_thread_read()[1.3.1.2.1.7]函数 返回来后

  • 再看看proc->todo是否还有事务等待处理,如果是,就把睡眠在proc->wait队列的线程唤醒来处理。
  • 最后,把本地变量 struct binder_write_read bwr[1.6.1.4.1.3.1.1.1]的内容拷贝回到用户传进来的缓冲区中,就返回了。
  • 返回到frameworks/base/cmds/servicemanager/binder.c文件中的binder_loop()[1.3.1.2.1.7.1]函数(46行)了:
void binder_loop(struct binder_state *bs, binder_handler func)  
{  
    int res;  
    struct binder_write_read bwr;  
    unsigned readbuf[32];  

    bwr.write_size = 0;  
    bwr.write_consumed = 0;  
    bwr.write_buffer = 0;  

    readbuf[0] = BC_ENTER_LOOPER;  
    binder_write(bs, readbuf, sizeof(unsigned));  

    for (;;) {  
        bwr.read_size = sizeof(readbuf);  
        bwr.read_consumed = 0;  
        bwr.read_buffer = (unsigned) readbuf;  

        res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);  

        if (res < 0) {  
            LOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));  
            break;  
        }  

        res = binder_parse(bs, 0, readbuf, bwr.read_consumed, func);  
        if (res == 0) {  
            LOGE("binder_loop: unexpected reply?!\n");  
            break;  
        }  
        if (res < 0) {  
            LOGE("binder_loop: io error %d %s\n", res, strerror(errno));  
            break;  
        }  
    }  
}  
  • 返回来的数据都放在readbuf[1.3.1.2.1.7.1.1]中,
  • 接着调用binder_parse()[1.3.1.2.1.7.1.2]函数进行解析
int binder_parse(struct binder_state *bs, struct binder_io *bio,  
                 uint32_t *ptr, uint32_t size, binder_handler func)  
{  
    int r = 1;  
    uint32_t *end = ptr + (size / 4);  

    while (ptr < end) {  
        uint32_t cmd = *ptr++;  
        ......  
        case BR_TRANSACTION: {  
            struct binder_txn *txn = (void *) ptr;  
            if ((end - ptr) * sizeof(uint32_t) < sizeof(struct binder_txn)) {  
                LOGE("parse: txn too small!\n");  
                return -1;  
            }  
            binder_dump_txn(txn);  
            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, res);  
            }  
            ptr += sizeof(*txn) / sizeof(uint32_t);  
            break;  
                             }  
        ......  
        default:  
            LOGE("parse: OOPS %d\n", cmd);  
            return -1;  
        }  
    }  

    return r;  
}
  • 首先把从Binder驱动程序读出来的数据转换为一个struct binder_txn结构体[1.3.1.2.1.7.1.2.1],保存在txn本地变量中(11行)。
    struct binder_txn结构体[1.3.1.2.1.7.1.2.1]定义在frameworks/base/cmds/servicemanager/binder.h文件中:
struct binder_txn  
{  
    void *target;  
    void *cookie;  
    uint32_t code;  
    uint32_t flags;  

    uint32_t sender_pid;  
    uint32_t sender_euid;  

    uint32_t data_size;  
    uint32_t offs_size;  
    void *data;  
    void *offs;  
};  
  • 函数中还用到了另外一个数据结构struct binder_io[1.3.1.2.1.7.1.2.2](19行),也是定义在frameworks/base/cmds/servicemanager/binder.h文件中:
struct binder_io  
{  
    char *data;            /* pointer to read/write from */  
    uint32_t *offs;        /* array of offsets */  
    uint32_t data_avail;   /* bytes available in data buffer */  
    uint32_t offs_avail;   /* entries available in offsets array */  

    char *data0;           /* start of data buffer */  
    uint32_t *offs0;       /* start of offsets buffer */  
    uint32_t flags;  
    uint32_t unused;  
}; 
  • 接着往下看,调用bio_init()[1.3.1.2.1.7.1.2.3]函数(23行)来初始化reply变量:
void bio_init(struct binder_io *bio, void *data,  
              uint32_t maxdata, uint32_t maxoffs)  
{  
    uint32_t n = maxoffs * sizeof(uint32_t);  

    if (n > maxdata) {  
        bio->flags = BIO_F_OVERFLOW;  
        bio->data_avail = 0;  
        bio->offs_avail = 0;  
        return;  
    }  

    bio->data = bio->data0 = data + n;  
    bio->offs = bio->offs0 = data;  
    bio->data_avail = maxdata - n;  
    bio->offs_avail = maxoffs;  
    bio->flags = 0;  
}  
  • 接着又调用bio_init_from_txn()[1.3.1.2.1.7.1.2.4]函数(24行)来初始化msg变量
void bio_init_from_txn(struct binder_io *bio, struct binder_txn *txn)  
{  
    bio->data = bio->data0 = txn->data;  
    bio->offs = bio->offs0 = txn->offs;  
    bio->data_avail = txn->data_size;  
    bio->offs_avail = txn->offs_size / 4;  
    bio->flags = BIO_F_SHARED;  
} 

最后,真正进行处理的函数是从参数中传进来的函数指针func[1.3.1.2.1.7.1.2.5](25行),这里就是定义在frameworks/base/cmds/servicemanager/service_manager.c文件中的svcmgr_handler()[1.3.1.2.1.7.1.2.5]函数

int svcmgr_handler(struct binder_state *bs,  
                   struct binder_txn *txn,  
                   struct binder_io *msg,  
                   struct binder_io *reply)  
{  
    struct svcinfo *si;  
    uint16_t *s;  
    unsigned len;  
    void *ptr;  
    uint32_t strict_policy;  

    if (txn->target != svcmgr_handle)  
        return -1;  

    // Equivalent to Parcel::enforceInterface(), reading the RPC  
    // header with the strict mode policy mask and the interface name.  
    // Note that we ignore the strict_policy and don't propagate it  
    // further (since we do no outbound RPCs anyway).  
    strict_policy = bio_get_uint32(msg);  
    s = bio_get_string16(msg, &len);  
    if ((len != (sizeof(svcmgr_id) / 2)) ||  
        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {  
            fprintf(stderr,"invalid id %s\n", str8(s));  
            return -1;  
    }  

    switch(txn->code) {  
    ......  
    case SVC_MGR_ADD_SERVICE:  
        s = bio_get_string16(msg, &len);  
        ptr = bio_get_ref(msg);  
        if (do_add_service(bs, s, len, ptr, txn->sender_euid))  
            return -1;  
        break;  
    ......  
    }  

    bio_put_uint32(reply, 0);  
    return 0;  
}  

回忆一下,在 BpServiceManger::addService[1.6.1]时,传给Binder驱动程序的参数为:

writeInt32(IPCThreadState::self()->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);  
writeString16("android.os.IServiceManager");  
writeString16("media.player");  
writeStrongBinder(new MediaPlayerService());  

svcmgr_handler()[1.3.1.2.1.7.1.2.5]函数中的第19、20行,第30、31,就是这里的语句:

strict_policy = bio_get_uint32(msg);  
s = bio_get_string16(msg, &len);  
s = bio_get_string16(msg, &len);  
ptr = bio_get_ref(msg);  

就是依次把它们读取出来了,这里,我们只要看一下bio_get_ref()[1.3.1.2.1.7.1.2.5.1]函数(31行)的实现。先看一个数据结构struct binder_obj就是对应struct flat_binder_object[1.6.1.1.3.1]的定义:

struct binder_object  
{  
    uint32_t type;  
    uint32_t flags;  
    void *pointer;  
    void *cookie;  
};

接着看bio_get_ref()[1.3.1.2.1.7.1.2.5.1]函数实现:

void *bio_get_ref(struct binder_io *bio)  
{  
    struct binder_object *obj;  

    obj = _bio_get_obj(bio);  
    if (!obj)  
        return 0;  

    if (obj->type == BINDER_TYPE_HANDLE)  
        return obj->pointer;  

    return 0;  
}  
  • _bio_get_obj()这个函数就不跟进去看了,它的作用就是从binder_io中取得第一个还没取获取过的binder_object

  • 在这个场景下,就是我们最开始传过来代表MediaPlayerService[1]struct flat_binder_object[1.6.1.1.3.1]了,

  • 这个原始的struct flat_binder_object[1.6.1.1.3.1]的type为BINDER_TYPE_BINDER

  • binder为指向MediaPlayerService[1]的弱引用的地址。

在前面我们说过,在Binder驱动程序里面,会把这个struct flat_binder_object[1.6.1.1.3.1]的type改为BINDER_TYPE_HANDLE,handle改为一个句柄值。这里的handle值就等于obj->pointer的值

回到svcmgr_handler()[1.3.1.2.1.7.1.2.5]函数,调用do_add_service()[1.3.1.2.1.7.1.2.5.2]函数(32行)进一步处理:

int do_add_service(struct binder_state *bs,  
                   uint16_t *s, unsigned len,  
                   void *ptr, unsigned uid)  
{  
    struct svcinfo *si;  
//    LOGI("add_service('%s',%p) uid=%d\n", str8(s), ptr, uid);  

    if (!ptr || (len == 0) || (len > 127))  
        return -1;  

    if (!svc_can_register(uid, s)) {  
        LOGE("add_service('%s',%p) uid=%d - PERMISSION DENIED\n",  
             str8(s), ptr, uid);  
        return -1;  
    }  

    si = find_svc(s, len);  
    if (si) {  
        if (si->ptr) {  
            LOGE("add_service('%s',%p) uid=%d - ALREADY REGISTERED\n",  
                 str8(s), ptr, uid);  
            return -1;  
        }  
        si->ptr = ptr;  
    } else {  
        si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));  
        if (!si) {  
            LOGE("add_service('%s',%p) uid=%d - OUT OF MEMORY\n",  
                 str8(s), ptr, uid);  
            return -1;  
        }  
        si->ptr = ptr;  
        si->len = len;  
        memcpy(si->name, s, (len + 1) * sizeof(uint16_t));  
        si->name[len] = '\0';  
        si->death.func = svcinfo_death;  
        si->death.ptr = si;  
        si->next = svclist;  
        svclist = si;  
    }  

    binder_acquire(bs, ptr);  
    binder_link_to_death(bs, ptr, &si->death);  
    return 0;  
}  
  • 这个函数的实现很简单,就是把MediaPlayerService[1]这个Binder实体的引用写到一个struct svcinfo结构体[1.3.1.2.1.7.1.2.5.2.1]中,主要是它的名称和句柄值,

  • 然后插入到链接svclist的头部去。这样,Client来向Service Manager查询服务接口时,只要给定服务名称,Service Manger就可以返回相应的句柄值了。

这个函数执行完成后,返回到svcmgr_handler()[1.3.1.2.1.7.1.2.5]函数,函数的最后,将一个错误码0写到reply变量中去,表示一切正常(38行)

svcmgr_handler()[1.3.1.2.1.7.1.2.5]函数执行完成后,返回到binder_parse()[1.3.1.2.1.7.1.2]函数,执行(第26行的语句)就是下面语句:

binder_send_reply(bs, &reply, txn->data, res);  

我们看一下binder_send_reply()[1.3.1.2.1.7.1.2.6]函数(26行)的实现,从函数名就可以猜到它要做什么了,告诉Binder驱动程序,它完成了Binder驱动程序交给它的任务了。

void binder_send_reply(struct binder_state *bs,  
                       struct binder_io *reply,  
                       void *buffer_to_free,  
                       int status)  
{  
    struct {  
        uint32_t cmd_free;  
        void *buffer;  
        uint32_t cmd_reply;  
        struct binder_txn txn;  
    } __attribute__((packed)) data;  

    data.cmd_free = BC_FREE_BUFFER;  
    data.buffer = buffer_to_free;  
    data.cmd_reply = BC_REPLY;  
    data.txn.target = 0;  
    data.txn.cookie = 0;  
    data.txn.code = 0;  
    if (status) {  
        data.txn.flags = TF_STATUS_CODE;  
        data.txn.data_size = sizeof(int);  
        data.txn.offs_size = 0;  
        data.txn.data = &status;  
        data.txn.offs = 0;  
    } else {  
        data.txn.flags = 0;  
        data.txn.data_size = reply->data - reply->data0;  
        data.txn.offs_size = ((char*) reply->offs) - ((char*) reply->offs0);  
        data.txn.data = reply->data0;  
        data.txn.offs = reply->offs0;  
    }  
    binder_write(bs, &data, sizeof(data));  
}  
  • 从这里可以看出,binder_send_reply()[1.3.1.2.1.7.1.2.6]函数(26行)告诉Binder驱动程序执行BC_FREE_BUFFER和BC_REPLY命令,
    • 前者释放之前在binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数分配的空间,地址为buffer_to_free
      • buffer_to_free这个地址是Binder驱动程序把自己在内核空间用的地址转换成用户空间地址再传给Service Manager的,所以Binder驱动程序拿到这个地址后,知道怎么样释放这个空间;
    • 后者告诉MediaPlayerService[1],它的BpServiceManger::addService[1.6.1]操作已经完成了,错误码是0,保存在data.txn.data中。

再来看binder_write()[1.3.1.2.1.7.1.2.6.1]函数

int binder_write(struct binder_state *bs, void *data, unsigned len)  
{  
    struct binder_write_read bwr;  
    int res;  
    bwr.write_size = len;  
    bwr.write_consumed = 0;  
    bwr.write_buffer = (unsigned) data;  
    bwr.read_size = 0;  
    bwr.read_consumed = 0;  
    bwr.read_buffer = 0;  
    res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);  
    if (res < 0) {  
        fprintf(stderr,"binder_write: ioctl failed (%s)\n",  
                strerror(errno));  
    }  
    return res;  
}  

这里可以看出,只有写操作,没有读操作,即read_size为0。

这里又是一个ioctlBINDER_WRITE_READ操作。直入到驱动程序的binder_ioctl()[1.6.1.4.1.3.1.1]函数后,执行BINDER_WRITE_READ命令,这里就不累述了。

最后,从binder_ioctl()[1.6.1.4.1.3.1.1]函数执行到binder_thread_write()[1.6.1.4.1.3.1.1.2]函数,我们首先看第一个命令****BC_FREE_BUFFER

int  binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,  
                    void __user *buffer, int size, signed long *consumed)  
{  
    uint32_t cmd;  
    void __user *ptr = buffer + *consumed;  
    void __user *end = buffer + size;  

    while (ptr < end && thread->return_error == BR_OK) {  
        if (get_user(cmd, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
        if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {  
            binder_stats.bc[_IOC_NR(cmd)]++;  
            proc->stats.bc[_IOC_NR(cmd)]++;  
            thread->stats.bc[_IOC_NR(cmd)]++;  
        }  
        switch (cmd) {  
        ......  
        case BC_FREE_BUFFER: {  
            void __user *data_ptr;  
            struct binder_buffer *buffer;  

            if (get_user(data_ptr, (void * __user *)ptr))  
                return -EFAULT;  
            ptr += sizeof(void *);  

            buffer = binder_buffer_lookup(proc, data_ptr);  
            if (buffer == NULL) {  
                binder_user_error("binder: %d:%d "  
                    "BC_FREE_BUFFER u%p no match\n",  
                    proc->pid, thread->pid, data_ptr);  
                break;  
            }  
            if (!buffer->allow_user_free) {  
                binder_user_error("binder: %d:%d "  
                    "BC_FREE_BUFFER u%p matched "  
                    "unreturned buffer\n",  
                    proc->pid, thread->pid, data_ptr);  
                break;  
            }  
            if (binder_debug_mask & BINDER_DEBUG_FREE_BUFFER)  
                printk(KERN_INFO "binder: %d:%d BC_FREE_BUFFER u%p found buffer %d for %s transaction\n",  
                proc->pid, thread->pid, data_ptr, buffer->debug_id,  
                buffer->transaction ? "active" : "finished");  

            if (buffer->transaction) {  
                buffer->transaction->buffer = NULL;  
                buffer->transaction = NULL;  
            }  
            if (buffer->async_transaction && buffer->target_node) {  
                BUG_ON(!buffer->target_node->has_async_transaction);  
                if (list_empty(&buffer->target_node->async_todo))  
                    buffer->target_node->has_async_transaction = 0;  
                else  
                    list_move_tail(buffer->target_node->async_todo.next, &thread->todo);  
            }  
            binder_transaction_buffer_release(proc, buffer, NULL);  
            binder_free_buf(proc, buffer);  
            break;  
                             }  

        ......  
        *consumed = ptr - buffer;  
    }  
    return 0;  
}  
  • (24行)这个是获得要删除的Buffer的用户空间地址
  • 接着通过(28行)这个语句来找到这个地址对应的struct binder_buffer信息, 因为这个空间是前面在binder_transaction里面分配的,所以这里一定能找到。
  • 最后,就可以释放这块内存了,(58、59行)

再来看另外一个命令BC_REPLY:

int  
binder_thread_write(struct binder_proc *proc, struct binder_thread *thread,  
                    void __user *buffer, int size, signed long *consumed)  
{  
    uint32_t cmd;  
    void __user *ptr = buffer + *consumed;  
    void __user *end = buffer + size;  

    while (ptr < end && thread->return_error == BR_OK) {  
        if (get_user(cmd, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
        if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {  
            binder_stats.bc[_IOC_NR(cmd)]++;  
            proc->stats.bc[_IOC_NR(cmd)]++;  
            thread->stats.bc[_IOC_NR(cmd)]++;  
        }  
        switch (cmd) {  
        ......  
        case BC_TRANSACTION:  
        case BC_REPLY: {  
            struct binder_transaction_data tr;  

            if (copy_from_user(&tr, ptr, sizeof(tr)))  
                return -EFAULT;  
            ptr += sizeof(tr);  
            binder_transaction(proc, thread, &tr, cmd == BC_REPLY);  
            break;  
                       }  

        ......  
        *consumed = ptr - buffer;  
    }  
    return 0;  
}  

又再次进入到binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数函数:

static void  
binder_transaction(struct binder_proc *proc, struct binder_thread *thread,  
struct binder_transaction_data *tr, int reply)  
{  
    struct binder_transaction *t;  
    struct binder_work *tcomplete;  
    size_t *offp, *off_end;  
    struct binder_proc *target_proc;  
    struct binder_thread *target_thread = NULL;  
    struct binder_node *target_node = NULL;  
    struct list_head *target_list;  
    wait_queue_head_t *target_wait;  
    struct binder_transaction *in_reply_to = NULL;  
    struct binder_transaction_log_entry *e;  
    uint32_t return_error;  

    ......  

    if (reply) {  
        in_reply_to = thread->transaction_stack;  
        if (in_reply_to == NULL) {  
            ......  
            return_error = BR_FAILED_REPLY;  
            goto err_empty_call_stack;  
        }  
        binder_set_nice(in_reply_to->saved_priority);  
        if (in_reply_to->to_thread != thread) {  
            .......  
            goto err_bad_call_stack;  
        }  
        thread->transaction_stack = in_reply_to->to_parent;  
        target_thread = in_reply_to->from;  
        if (target_thread == NULL) {  
            return_error = BR_DEAD_REPLY;  
            goto err_dead_binder;  
        }  
        if (target_thread->transaction_stack != in_reply_to) {  
            ......  
            return_error = BR_FAILED_REPLY;  
            in_reply_to = NULL;  
            target_thread = NULL;  
            goto err_dead_binder;  
        }  
        target_proc = target_thread->proc;  
    } else {  
        ......  
    }  
    if (target_thread) {  
        e->to_thread = target_thread->pid;  
        target_list = &target_thread->todo;  
        target_wait = &target_thread->wait;  
    } else {  
        ......  
    }  


    /* TODO: reuse incoming transaction for reply */  
    t = kzalloc(sizeof(*t), GFP_KERNEL);  
    if (t == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_alloc_t_failed;  
    }  


    tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);  
    if (tcomplete == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_alloc_tcomplete_failed;  
    }  

    if (!reply && !(tr->flags & TF_ONE_WAY))  
        t->from = thread;  
    else  
        t->from = NULL;  
    t->sender_euid = proc->tsk->cred->euid;  
    t->to_proc = target_proc;  
    t->to_thread = target_thread;  
    t->code = tr->code;  
    t->flags = tr->flags;  
    t->priority = task_nice(current);  
    t->buffer = binder_alloc_buf(target_proc, tr->data_size,  
        tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));  
    if (t->buffer == NULL) {  
        return_error = BR_FAILED_REPLY;  
        goto err_binder_alloc_buf_failed;  
    }  
    t->buffer->allow_user_free = 0;  
    t->buffer->debug_id = t->debug_id;  
    t->buffer->transaction = t;  
    t->buffer->target_node = target_node;  
    if (target_node)  
        binder_inc_node(target_node, 1, 0, NULL);  

    offp = (size_t *)(t->buffer->data + ALIGN(tr->data_size, sizeof(void *)));  

    if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) {  
        binder_user_error("binder: %d:%d got transaction with invalid "  
            "data ptr\n", proc->pid, thread->pid);  
        return_error = BR_FAILED_REPLY;  
        goto err_copy_data_failed;  
    }  
    if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) {  
        binder_user_error("binder: %d:%d got transaction with invalid "  
            "offsets ptr\n", proc->pid, thread->pid);  
        return_error = BR_FAILED_REPLY;  
        goto err_copy_data_failed;  
    }  

    ......  

    if (reply) {  
        BUG_ON(t->buffer->async_transaction != 0);  
        binder_pop_transaction(target_thread, in_reply_to);  
    } else if (!(t->flags & TF_ONE_WAY)) {  
        ......  
    } else {  
        ......  
    }  
    t->work.type = BINDER_WORK_TRANSACTION;  
    list_add_tail(&t->work.entry, target_list);  
    tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;  
    list_add_tail(&tcomplete->entry, &thread->todo);  
    if (target_wait)  
        wake_up_interruptible(target_wait);  
    return;  
    ......  
}  

注意,这里的reply为1,我们忽略掉其它无关代码。

前面Service Manager正在binder_thread_read()[1.3.1.2.1.7]函数 中被MediaPlayerService[1]启动后进程唤醒后,在最后会把当前处理完的事务放在thread->transaction_stack[1.3.1.2.1.7.1] 中:

binder_thread_read()[1.3.1.2.1.7]函数 中的101-105代码,就是下面这个代码:

if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {  
    t->to_parent = thread->transaction_stack;  
    t->to_thread = thread;  
    thread->transaction_stack = t;  
} 

所以,首先是把它这个binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数取回来,并且放在本地变量in_reply_to中,代码第20行

in_reply_to = thread->transaction_stack;  

接着就可以通过in_reply_to得到最终发出这个事务请求的线程和进程(32、44行):

target_thread = in_reply_to->from;  
target_proc = target_thread->proc;

然后得到target_listtarget_wait(50、51行):

target_list = &target_thread->todo;  
target_wait = &target_thread->wait;  

下面这一段代码:58-109行
我们在前面已经分析过了,这里不再重复。
注意

  • 这里target_node为NULL,因此,t->buffer->target_node也为NULL。

函数本来有一个for循环,用来处理数据中的Binder对象,这里由于没有Binder对象,所以就略过了。

到了下面这句代码(113):

binder_pop_transaction(target_thread, in_reply_to);  

进入这个函数binder_pop_transaction()[1.6.1.4.1.3.1.1.2.1.1]函数

static void  
binder_pop_transaction(  
    struct binder_thread *target_thread, struct binder_transaction *t)  
{  
    if (target_thread) {  
        BUG_ON(target_thread->transaction_stack != t);  
        BUG_ON(target_thread->transaction_stack->from != target_thread);  
        target_thread->transaction_stack =  
            target_thread->transaction_stack->from_parent;  
        t->from = NULL;  
    }  
    t->need_reply = 0;  
    if (t->buffer)  
        t->buffer->transaction = NULL;  
    kfree(t);  
    binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;  
}  

由于到了这里,已经不需要in_reply_to这个transaction了,就把它删掉。
回到binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数(119-122):

t->work.type = BINDER_WORK_TRANSACTION;  
list_add_tail(&t->work.entry, target_list);  
tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;  
list_add_tail(&tcomplete->entry, &thread->todo);
  • 和前面一样,分别把t和tcomplete分别放在target_list[1.6.1.4.1.3.1.1.2.1.6]thread->todo(40行)[1.3.1.2.1.7.2] 队列中

  • 这里的target_list[1.6.1.4.1.3.1.1.2.1.6]指的就是最初调用IServiceManager::addService[1.6.1]MediaPlayerService[1]的Server主线程的thread->todo[1.3.1.2.1.7.2] 队列了

  • thread->todo[1.3.1.2.1.7.2] 指的是Service Manager中用来回复IServiceManager::addService[1.6.1]请求的线程。

  • 最后,唤醒等待在target_wait队列上的线程了,就是最初调用IServiceManager::addService[1.6.1]MediaPlayerService[1]的Server主线程了,它最后在binder_thread_read()[1.3.1.2.1.7]函数 中睡眠在thread->wait上,就是这里的target_wait

binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数的156行,就是下面这个语句

if (target_wait)  
    wake_up_interruptible(target_wait); 

这样,Service Manger回复调用IServiceManager::addService[1.6.1]请求就算完成了

重新回到frameworks/base/cmds/servicemanager/binder.c文件中的binder_loop()[1.3.1.2.1.7.1]函数等待下一个Client请求的到来。

事实上,Service Manger回到binder_loop()[1.3.1.2.1.7.1]函数(46行)再次执行ioctl()[1.3.1.2]文件控制函数时候,又会再次进入到binder_thread_read()[1.3.1.2.1.7]函数 。这时个会发现thread->todo[1.3.1.2.1.7.2] 不为空,这是因为刚才我们调用了:

binder_transaction()[1.6.1.4.1.3.1.1.2.1]函数的122行,就是下面这个语句

list_add_tail(&tcomplete->entry, &thread->todo);  

把一个工作项tcompelete放在了在thread->todo[1.3.1.2.1.7.2] 中,这个tcompelete的type为BINDER_WORK_TRANSACTION_COMPLETE,因此,Binder驱动程序会执行下面操作:

binder_thread_read()[1.3.1.2.1.7]函数 (54行)就是下面这个代码:

switch (w->type) {  
case BINDER_WORK_TRANSACTION_COMPLETE: {  
    cmd = BR_TRANSACTION_COMPLETE;  
    if (put_user(cmd, (uint32_t __user *)ptr))  
        return -EFAULT;  
    ptr += sizeof(uint32_t);  

    list_del(&w->entry);  
    kfree(w);  

    } break;  
    ......  
} 

binder_loop()[1.3.1.2.1.7.1]函数执行完这个ioctl()[1.3.1.2]文件控制函数调用后,才会在下一次调用ioctl()[1.3.1.2]文件控制函数进入到Binder驱动程序进入休眠状态,等待下一次Client的请求。

上面讲到调用IServiceManager::addService[1.6.1]MediaPlayerService[1]的Server主线程被唤醒了,于是,重新执行binder_thread_read()[1.3.1.2.1.7]函数 (54行)

static int  
binder_thread_read(struct binder_proc *proc, struct binder_thread *thread,  
                   void  __user *buffer, int size, signed long *consumed, int non_block)  
{  
    void __user *ptr = buffer + *consumed;  
    void __user *end = buffer + size;  

    int ret = 0;  
    int wait_for_proc_work;  

    if (*consumed == 0) {  
        if (put_user(BR_NOOP, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
    }  

retry:  
    wait_for_proc_work = thread->transaction_stack == NULL && list_empty(&thread->todo);  

    ......  

    if (wait_for_proc_work) {  
        ......  
    } else {  
        if (non_block) {  
            if (!binder_has_thread_work(thread))  
                ret = -EAGAIN;  
        } else  
            ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));  
    }  

    ......  

    while (1) {  
        uint32_t cmd;  
        struct binder_transaction_data tr;  
        struct binder_work *w;  
        struct binder_transaction *t = NULL;  

        if (!list_empty(&thread->todo))  
            w = list_first_entry(&thread->todo, struct binder_work, entry);  
        else if (!list_empty(&proc->todo) && wait_for_proc_work)  
            w = list_first_entry(&proc->todo, struct binder_work, entry);  
        else {  
            if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */  
                goto retry;  
            break;  
        }  

        ......  

        switch (w->type) {  
        case BINDER_WORK_TRANSACTION: {  
            t = container_of(w, struct binder_transaction, work);  
                                      } break;  
        ......  
        }  

        if (!t)  
            continue;  

        BUG_ON(t->buffer == NULL);  
        if (t->buffer->target_node) {  
            ......  
        } else {  
            tr.target.ptr = NULL;  
            tr.cookie = NULL;  
            cmd = BR_REPLY;  
        }  
        tr.code = t->code;  
        tr.flags = t->flags;  
        tr.sender_euid = t->sender_euid;  

        if (t->from) {  
            ......  
        } else {  
            tr.sender_pid = 0;  
        }  

        tr.data_size = t->buffer->data_size;  
        tr.offsets_size = t->buffer->offsets_size;  
        tr.data.ptr.buffer = (void *)t->buffer->data + proc->user_buffer_offset;  
        tr.data.ptr.offsets = tr.data.ptr.buffer + ALIGN(t->buffer->data_size, sizeof(void *));  

        if (put_user(cmd, (uint32_t __user *)ptr))  
            return -EFAULT;  
        ptr += sizeof(uint32_t);  
        if (copy_to_user(ptr, &tr, sizeof(tr)))  
            return -EFAULT;  
        ptr += sizeof(tr);  

        ......  

        list_del(&t->work.entry);  
        t->buffer->allow_user_free = 1;  
        if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {  
            ......  
        } else {  
            t->buffer->transaction = NULL;  
            kfree(t);  
            binder_stats.obj_deleted[BINDER_STAT_TRANSACTION]++;  
        }  
        break;  
    }  

done:  
    ......  
    return 0;  
}

在while循环中,从thread->todo[1.3.1.2.1.7.2] 得到w,w->type为BINDER_WORK_TRANSACTION,于是,得到t。

从上面可以知道,Service Manager反回了一个0回来,写在t->buffer->data里面,现在把t->buffer->data加上proc->user_buffer_offset,得到用户空间地址,保存在tr.data.ptr.buffer里面,这样用户空间就可以访问这个返回码了。

由于cmd不等于BR_TRANSACTION,这时就可以把t删除掉了,因为以后都不需要用了。

执行完这个函数后,就返回到binder_ioctl()[1.3.1.2.1] 函数(第40行),执行下面语句,把数据返回给用户空间:

if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {  
    ret = -EFAULT;  
    goto err;  
}  

接着返回到用户空间IPCThreadState::talkWithDriver()[1.6.1.4.1.3.1]函数,最后返回到IPCThreadState::waitForResponse()[1.6.1.4.1.3]函数,最终执行到下面语句:

status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)  
{  
    int32_t cmd;  
    int32_t err;  

    while (1) {  
        if ((err=talkWithDriver()) < NO_ERROR) break;  

        ......  

        cmd = mIn.readInt32();  

        ......  

        switch (cmd) {  
        ......  
        case BR_REPLY:  
            {  
                binder_transaction_data tr;  
                err = mIn.read(&tr, sizeof(tr));  
                LOG_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 size_t*>(tr.data.ptr.offsets),  
                            tr.offsets_size/sizeof(size_t),  
                            freeBuffer, this);  
                    } else {  
                        ......  
                    }  
                } else {  
                    ......  
                }  
            }  
            goto finish;  

        ......  
        }  
    }  

finish:  
    ......  
    return err;  
}  

注意,这里的tr.flags等于0,这个是在上面的binder_send_reply()[1.3.1.2.1.7.1.2.6]函数(26行)里设置的。最终把结果保存在reply了:

reply->ipcSetDataReference(  
       reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),  
       tr.data_size,  
       reinterpret_cast<const size_t*>(tr.data.ptr.offsets),  
       tr.offsets_size/sizeof(size_t),  
       freeBuffer, this);  

从这里层层返回,最后回到MediaPlayerService::instantiate()[1.6]函数函数中。

至此,IServiceManger::addService()[1.6.1]终于执行完毕了。这个过程非常复杂,但是如果我们能够深刻地理解这一过程,将能很好地理解Binder机制的设计思想和实现过程。

这里,对IServiceManger::addService()[1.6.1]过程中MediaPlayerService[1]、ServiceManager和BinderDriver之间的交互作一个小结:

这里写图片描述

回到frameworks/base/media/mediaserver/main_mediaserver.cpp文件中的main函数MediaPlayerService[1]的位置,接下去还要执行下面两个函数:

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

首先看ProcessState::startThreadPool()[1.7]函数的实现:

void ProcessState::startThreadPool()  
{  
    AutoMutex _l(mLock);  
    if (!mThreadPoolStarted) {  
        mThreadPoolStarted = true;  
        spawnPooledThread(true);  
    }  
}

调用spwanPooledThread()[1.7.1]

void ProcessState::spawnPooledThread(bool isMain)  
{  
    if (mThreadPoolStarted) {  
        int32_t s = android_atomic_add(1, &mThreadPoolSeq);  
        char buf[32];  
        sprintf(buf, "Binder Thread #%d", s);  
        LOGV("Spawning new pooled thread, name=%s\n", buf);  
        sp<Thread> t = new PoolThread(isMain);  
        t->run(buf);  
    }  
}  

这里主要是创建一个线程,PoolThread继续Thread类,Thread类定义在frameworks/base/libs/utils/Threads.cpp文件中,其run函数最终调用子类的threadLoop()[1.7.1.1]函数,这里即为PoolThread::threadLoop()[1.7.1.1]函数函数:

virtual bool threadLoop()  
{  
    IPCThreadState::self()->joinThreadPool(mIsMain);  
    return false;  
} 

这里和frameworks/base/media/mediaserver/main_mediaserver.cpp文件中的main函数一样,最终都是调用了IPCThreadState::joinThreadPool()[1.7.1.1.1]函数
它们的区别是,一个参数是true,一个是默认值false。
我们来看一下这个函数的实现:

void IPCThreadState::joinThreadPool(bool isMain)  
{  
    LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());  

    mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);  

    ......  

    status_t result;  
    do {  
        int32_t cmd;  

        .......  

        // now get the next command to be processed, waiting if necessary  
        result = talkWithDriver();  
        if (result >= NO_ERROR) {  
            size_t IN = mIn.dataAvail();  
            if (IN < sizeof(int32_t)) continue;  
            cmd = mIn.readInt32();  
            ......  
            }  

            result = executeCommand(cmd);  
        }  

        ......  
    } while (result != -ECONNREFUSED && result != -EBADF);  

    .......  

    mOut.writeInt32(BC_EXIT_LOOPER);  
    talkWithDriver(false);  
}  

这个函数最终是在一个无穷循环中,

  • 通过调用talkWithDriver()[1.6.1.4.1.3.1]函数函数来和Binder驱动程序进行交互,

    • 实际上就是调用talkWithDriver()[1.6.1.4.1.3.1]函数来等待Client的请求,
  • 然后再调用executeCommand来处理请求,而在executeCommand函数中,最终会调用BBinder::transact()[1.1.4.1.1]函数来真正处理Client的请求:

status_t IPCThreadState::executeCommand(int32_t cmd)  
{  
    BBinder* obj;  
    RefBase::weakref_type* refs;  
    status_t result = NO_ERROR;  

    switch (cmd) {  
    ......  

    case BR_TRANSACTION:  
        {  
            binder_transaction_data tr;  
            result = mIn.read(&tr, sizeof(tr));  

            ......  

            Parcel reply;  

            ......  

            if (tr.target.ptr) {  
                sp<BBinder> b((BBinder*)tr.cookie);  
                const status_t error = b->transact(tr.code, buffer, &reply, tr.flags);  
                if (error < NO_ERROR) reply.setError(error);  

            } else {  
                const status_t error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);  
                if (error < NO_ERROR) reply.setError(error);  
            }  

            ......  
        }  
        break;  

    .......  
    }  

    if (result != NO_ERROR) {  
        mLastError = result;  
    }  

    return result;  
}  

BBinder::transact()[1.1.4.1.1]函数的实现

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

最终会调用onTransact()[1.1.4.1.1.1]函数来处理。在这个场景中,BnMediaPlayerService继承了BBinder[1.1.3]类,并且重载了onTransact()[1.1.4.1.1.1]函数,因此,这里实际上是调用了BnMediaPlayerService::onTransact函数,这个函数定义在frameworks/base/libs/media/libmedia/IMediaPlayerService.cpp文件中:

status_t BnMediaPlayerService::onTransact(  
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)  
{  
    switch(code) {  
        case CREATE_URL: {  
            ......  
                         } break;  
        case CREATE_FD: {  
            ......  
                        } break;  
        case DECODE_URL: {  
            ......  
                         } break;  
        case DECODE_FD: {  
            ......  
                        } break;  
        case CREATE_MEDIA_RECORDER: {  
            ......  
                                    } break;  
        case CREATE_METADATA_RETRIEVER: {  
            ......  
                                        } break;  
        case GET_OMX: {  
            ......  
                      } break;  
        default:  
            return BBinder::onTransact(code, data, reply, flags);  
    }  
}  

至此,我们就以MediaPlayerService为例,完整地介绍了Android系统进程间通信Binder机制中的Server启动过程。Server启动起来之后,就会在一个无穷循环中等待Client的请求了。

发布了244 篇原创文章 · 获赞 799 · 访问量 234万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览