当前位置: 移动技术网 > 移动技术>移动开发>Android > Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

        在前面一篇文章android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路中,介绍了在android系统中binder进程间通信机制中的server角色是如何获得service manager远程接口的,即defaultservicemanager函数的实现。server获得了service manager远程接口之后,就要把自己的service添加到service manager中去,然后把自己启动起来,等待client的请求。本文将通过分析源代码了解server的启动过程是怎么样的。

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

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

        我们将要介绍的主角mediaplayerservice继承于bnmediaplayerservice类,熟悉binder机制的同学应该知道bnmediaplayerservice是一个binder native类,用来处理client请求的。bnmediaplayerservice继承于bninterface<imediaplayerservice>类,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实际是继承了imediaplayerservice和bbinder类。imediaplayerservice和bbinder类又分别继承了iinterface和ibinder类,iinterface和ibinder类又同时继承了refbase类。

       实际上,bnmediaplayerservice并不是直接接收到client处发送过来的请求,而是使用了ipcthreadstate接收client处发送过来的请求,而ipcthreadstate又借助了processstate类来与binder驱动程序交互。有关ipcthreadstate和processstate的关系,可以参考上一篇文章android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路,接下来也会有相应的描述。ipcthreadstate接收到了client处的请求后,就会调用bbinder类的transact函数,并传入相关参数,bbinder类的transact函数最终调用bnmediaplayerservice类的ontransact函数,于是,就开始真正地处理client的请求了。

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

      首先,看看mediaplayerservice是如何启动的。启动mediaplayerservice的代码位于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()调用创建一个processstate实例。processstate::self()是processstate类的一个静态成员变量,定义在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实例gprocess。全局唯一实例变量gprocess定义在frameworks/base/libs/binder/static.cpp文件中:

                        mutex gprocessmutex; 
                        sp<processstate> gprocess;  

       再来看processstate的构造函数:

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

        这个函数有两个关键地方,一是通过open_driver函数打开binder设备文件/dev/binder,并将打开设备文件描述符保存在成员变量mdriverfd中;二是通过mmap来把设备文件/dev/binder映射到内存中。

        先看open_driver函数的实现,这个函数同样位于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; 
} 

        这个函数的作用主要是通过open文件操作函数来打开/dev/binder设备文件,然后再调用ioctl文件控制函数来分别执行binder_version和binder_set_max_threads两个命令来和binder驱动程序进行交互,前者用于获得当前binder驱动程序的版本号,后者用于通知binder驱动程序,mediaplayerservice最多可同时启动15个线程来处理client端的请求。

        open在binder驱动程序中的具体实现,请参考前面一篇文章浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路,这里不再重复描述。打开/dev/binder设备文件后,binder驱动程序就为mediaplayerservice进程创建了一个struct binder_proc结构体实例来维护mediaplayerservice进程上下文相关信息。

        我们来看一下ioctl文件操作函数执行binder_version命令的过程:

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

        这个函数调用最终进入到binder驱动程序的binder_ioctl函数中,我们只关注binder_version相关的部分逻辑:

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写入到传入的参数arg指向的用户缓冲区中去就返回了。binder_current_protocol_version是一个宏,定义在kernel/common/drivers/staging/android/binder.h文件中:

                     /* this is the current protocol version. */ 
             #define binder_current_protocol_version 7  

       这里为什么要把ubuf转换成struct binder_version之后,再通过其protocol_version成员变量再来写入呢,转了一圈,最终内容还是写入到ubuf中。我们看一下struct binder_version的定义就会明白,同样是在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函数,因此,这里调用binder_get_thread的时候,就会为当前线程创建一个struct binder_thread结构体变量来维护线程上下文信息,具体可以参考浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路一文。

        接着我们再来看一下ioctl文件操作函数执行binder_set_max_threads命令的过程:

                   result = ioctl(fd, binder_set_max_threads, &maxthreads);  

        这个函数调用最终进入到binder驱动程序的binder_ioctl函数中,我们只关注binder_set_max_threads相关的部分逻辑:

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_get_thread函数的时候,就可以在proc->threads中找到当前线程对应的struct binder_thread结构了,因为前面已经创建好并保存在proc->threads红黑树中。

        回到processstate的构造函数中,这里还通过mmap函数来把设备文件/dev/binder映射到内存中,这个函数在浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路一文也已经有详细介绍,这里不再重复描述。宏binder_vm_size就定义在processstate.cpp文件中:

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

        mmap函数调用完成之后,binder驱动程序就为当前进程预留了binder_vm_size大小的内存空间了。

        这样,processstate全局唯一变量gprocess就创建完毕了,回到frameworks/base/media/mediaserver/main_mediaserver.cpp文件中的main函数,下一步是调用defaultservicemanager函数来获得service manager的远程接口,这个已经在上一篇文章浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路有详细描述,读者可以回过头去参考一下。

        再接下来,就进入到mediaplayerservice::instantiate函数把mediaplayerservice添加到service manger中去了。这个函数定义在frameworks/base/media/libmediaplayerservice/mediaplayerservice.cpp文件中:

void mediaplayerservice::instantiate() { 
 defaultservicemanager()->addservice( 
   string16("media.player"), new mediaplayerservice()); 
} 

        我们重点看一下iservicemanger::addservice的过程,这有助于我们加深对binder机制的理解。

        在上一篇文章浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路中说到,defaultservicemanager返回的实际是一个bpservicemanger类实例,因此,我们看一下bpservicemanger::addservice的实现,这个函数实现在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类是用来于序列化进程间通信数据用的。
         先来看这一句的调用:

           data.writeinterfacetoken(iservicemanager::getinterfacedescriptor());  

         iservicemanager::getinterfacedescriptor()返回来的是一个字符串,即"android.os.iservicemanager",具体可以参考iservicemanger的实现。我们看一下parcel::writeinterfacetoken的实现,位于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中去。

         再来看下面的调用:

                    data.writestring16(name);  

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

               data.writestrongbinder(service);  

        这里定入一个binder对象到parcel去。我们重点看一下这个函数的实现,因为它涉及到进程间传输binder实体的问题,比较复杂,需要重点关注,同时,也是理解binder机制的一个重点所在。注意,这里的service参数是一个mediaplayerservice对象。

status_t parcel::writestrongbinder(const sp<ibinder>& val) 
{ 
 return flatten_binder(processstate::self(), val, this); 
} 

        看到flatten_binder函数,是不是似曾相识的感觉?我们在前面一篇文章浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路中,曾经提到在binder驱动程序中,使用struct flat_binder_object来表示传输中的一个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; 
}; 

        各个成员变量的含义请参考资料android binder设计与实现。
        我们进入到flatten_binder函数看看:

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

        首先是初始化flat_binder_object的flags域: 

               obj.flags = 0x7f | flat_binder_flag_accepts_fds;  

        0x7f表示处理本binder实体请求数据包的线程的最低优先级,flat_binder_flag_accepts_fds表示这个binder实体可以接受文件描述符,binder实体在收到文件描述符时,就会在本进程中打开这个文件。

       传进来的binder即为mediaplayerservice::instantiate函数中new出来的mediaplayerservice实例,因此,不为空。又由于mediaplayerservice继承自bbinder类,它是一个本地binder实体,因此binder->localbinder返回一个bbinder指针,而且肯定不为空,于是执行下面语句:

obj.type = binder_type_binder; 
obj.binder = local->getweakrefs(); 
obj.cookie = local; 

        设置了flat_binder_obj的其他成员变量,注意,指向这个binder实体地址的指针local保存在flat_binder_obj的成员变量cookie中。

        函数调用finish_flatten_binder来将这个flat_binder_obj写入到parcel中去:

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的实现如下:

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

        这里除了把flat_binder_obj写到parcel里面之内,还要记录这个flat_binder_obj在parcel里面的偏移位置:

                    mobjects[mobjectssize] = mdatapos;  

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

       再回到bpservicemanager::addservice函数中,调用下面语句:

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

       回到浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路一文中的类图中去看一下,这里的remote成员函数来自于bprefbase类,它返回一个bpbinder指针。因此,我们继续进入到bpbinder::transact函数中去看看:

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进执行实际的操作。注意,这里的mhandle为0,code为add_service_transaction。add_service_transaction是上面以参数形式传进来的,那mhandle为什么是0呢?因为这里表示的是service manager远程接口,它的句柄值一定是0,具体请参考浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路一文。

       再进入到ipcthreadstate::transact函数,看看做了些什么事情:

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函数的参数flags是一个默认值为0的参数,上面没有传相应的实参进来,因此,这里就为0。

        函数首先调用writetransactiondata函数准备好一个struct binder_transaction_data结构体变量,这个是等一下要传输给binder驱动程序的。struct binder_transaction_data的定义我们在浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路一文中有详细描述,读者不妨回过去读一下。这里为了方便描述,将struct binder_transaction_data的定义再次列出来:

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函数的实现如下:

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:

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,因此需要设置tr.offsets_size就为1,tr.data.ptr.offsets就指向了这个mediaplayerservice的地址在tr.data.ptr.buffer中的偏移量。最后,将tr的内容保存在ipcthreadstate的成员变量mout中。

       回到ipcthreadstate::transact函数中,接下去看,(flags & tf_one_way) == 0为true,并且reply不为空,所以最终进入到waitforresponse(reply)这条路径来。我们看一下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: 
   { 
    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函数来与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驱动程序,先执行write操作,再执行read操作,下面我们将会看到。

        最后,通过ioctl(mprocess->mdriverfd, binder_write_read, &bwr)进行到binder驱动程序的binder_ioctl函数,我们只关注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; 
} 

         函数首先是将用户传进来的参数拷贝到本地变量struct binder_write_read bwr中去。这里bwr.write_size > 0为true,因此,进入到binder_thread_write函数中,我们只关注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 tr中去,接着调用binder_transaction函数进一步处理,这里我们忽略掉无关代码:

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和一个待完成工作项tcomplete,并执行初始化工作:

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

         注意,这里的事务t是要交给target_proc处理的,在这个场景之下,就是service manager了。因此,下面的语句:

t->buffer = binder_alloc_buf(target_proc, tr->data_size, 
  tr->offsets_size, !reply && (t->flags & tf_one_way)); 

         就是在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:

 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,调用binder_get_node函数查询这个binder实体时,会返回空,于是binder_new_node在proc中新建一个,下次就可以直接使用了。

        现在,由于要把这个binder实体mediaplayerservice交给target_proc,也就是service manager来管理,也就是说service manager要引用这个mediaplayerservice了,于是通过binder_get_ref_for_node为mediaplayerservice创建一个引用,并且通过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列表中去:

                 list_add_tail(&t->work.entry, target_list);  

        并且把待完成工作项加入到本线程的todo等待执行列表中去:

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

        现在目标进程有事情可做了,于是唤醒它:

             if (target_wait)  
                          wake_up_interruptible(target_wait);   

       这里就是要唤醒service manager进程了。回忆一下前面这篇文章,此时, service manager正在binder_t浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路hread_read函数中调用wait_event_interruptible进入休眠状态。

       这里我们先忽略一下service manager被唤醒之后的场景,继续medaplayerservice的启动过程,然后再回来。

       回到binder_ioctl函数,bwr.read_size > 0为true,于是进入binder_thread_read函数:

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和thread->todo均不为空,于是wait_for_proc_work为false,由于binder_has_thread_work的时候,返回true,这里因为thread->todo不为空,因此,线程虽然调用了wait_event_interruptible,但是不会睡眠,于是继续往下执行。

        由于thread->todo不为空,执行下列语句:

if (!list_empty(&thread->todo)) 
  w = list_first_entry(&thread->todo, struct binder_work, entry); 

        w->type为binder_work_transaction_complete,这是在上面的binder_transaction函数设置的,于是执行:

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

        这里就将w从thread->todo删除了。由于这里t为空,重新执行while循环,这时由于已经没有事情可做了,最后就返回到binder_ioctl函数中。注间,这里一共往用户传进来的缓冲区buffer写入了两个整数,分别是br_noop和br_transaction_complete。

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

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

        最后返回到ipcthreadstate::talkwithdriver函数中,执行下面语句: 

 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) { 
<pre code_snippet_id="134056" snippet_file_name="blog_20131230_54_6706870" name="code" class="cpp">   min.setdatasize(bwr.read_consumed); 
   min.setdataposition(0);</pre>  }  ......  return no_error; } 

        首先是把mout的数据清空:
                          mout.setdatasize(0);  

       然后设置已经读取的内容的大小:

                          min.setdatasize(bwr.read_consumed);  
                          min.setdataposition(0);
  

        然后返回到ipcthreadstate::waitforresponse函数中。在ipcthreadstate::waitforresponse函数,先是从min读出一个整数,这个便是br_noop了,这是一个空操作,什么也不做。然后继续进入ipcthreadstate::talkwithdriver函数中。

        这时候,下面语句执行后:

                       const bool needread = min.dataposition() >= min.datasize();  

        needread为false,因为在min中,尚有一个整数br_transaction_complete未读出。

       这时候,下面语句执行后:

                       const size_t outavail = (!doreceive || needread) ? mout.datasize() : 0;  

        outavail等于0。因此,最后bwr.write_size和bwr.read_size均为0,ipcthreadstate::talkwithdriver函数什么也不做,直接返回到ipcthreadstate::waitforresponse函数中。在ipcthreadstate::waitforresponse函数,又继续从min读出一个整数,这个便是br_transaction_complete:

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

        reply不为null,因此,ipcthreadstate::waitforresponse的循环没有结束,继续执行,又进入到ipcthreadstate::talkwithdrive中。

        这次,needread就为true了,而outavail仍为0,所以bwr.read_size不为0,bwr.write_size为0。于是通过:

                       ioctl(mprocess->mdriverfd, binder_write_read, &bwr)  

        进入到binder驱动程序中的binder_ioctl函数中。由于bwr.write_size为0,bwr.read_size不为0,这次直接就进入到binder_thread_read函数中。这时候,thread->transaction_stack不等于0,但是thread->todo为空,于是线程就通过:
[cpp] view plain copy 在code上查看代码片派生到我的代码片
wait_event_interruptible(thread->wait, binder_has_thread_work(thread));  

        进入睡眠状态,等待service manager来唤醒了。

        现在,我们可以回到service manager被唤醒的过程了。我们接着前面浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路这篇文章的最后,继续描述。此时, service manager正在binder_thread_read函数中调用wait_event_interruptible_exclusive进入休眠状态。上面被mediaplayerservice启动后进程唤醒后,继续执行binder_thread_read函数:

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列表中得到第一个工作项:

                   w = list_first_entry(&proc->todo, struct binder_work, entry);  

        从上面的描述中,我们知道,这个工作项的类型为binder_work_transaction,于是通过下面语句得到事务项:

                    t = container_of(w, struct binder_transaction, work);  

       接着就是把事务项t中的数据拷贝到本地局部变量struct binder_transaction_data tr中去了: 

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 *)); 

        这里有一个非常重要的地方,是binder进程间通信机制的精髓所在:

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指向这个用户缓冲区的地址:

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列表中删除:

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队列的头部:

t->to_parent = thread->transaction_stack; 
t->to_thread = thread; 
thread->transaction_stack = t; 

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

         这个while最后通过一个break跳了出来,最后返回到binder_ioctl函数中:

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返回来后,再看看proc->todo是否还有事务等待处理,如果是,就把睡眠在proc->wait队列的线程唤醒来处理。最后,把本地变量struct binder_write_read bwr的内容拷贝回到用户传进来的缓冲区中,就返回了。

        这里就是返回到frameworks/base/cmds/servicemanager/binder.c文件中的binder_loop函数了:

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中,接着调用binder_parse进行解析:

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结构体,保存在txn本地变量中,struct binder_txn定义在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,也是定义在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来初始化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来初始化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,这里就是定义在frameworks/base/cmds/servicemanager/service_manager.c文件中的svcmgr_handler函数:

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

         回忆一下,在bpservicemanager::addservice时,传给binder驱动程序的参数为:

writeint32(ipcthreadstate::self()->getstrictmodepolicy() | strict_mode_penalty_gather); 
writestring16("android.os.iservicemanager"); 
writestring16("media.player"); 
writestrongbinder(new mediaplayerservice()); 

         这里的语句:

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的实现。先看一个数据结构struct binder_obj的定义:

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

        这个结构体其实就是对应struct flat_binder_obj的。
        接着看bio_get_ref实现:

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的flat_binder_obj了,这个原始的flat_binder_obj的type为binder_type_binder,binder为指向mediaplayerservice的弱引用的地址。在前面我们说过,在binder驱动驱动程序里面,会把这个flat_binder_obj的type改为binder_type_handle,handle改为一个句柄值。这里的handle值就等于obj->pointer的值。

        回到svcmgr_handler函数,调用do_add_service进一步处理:


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这个binder实体的引用写到一个struct svcinfo结构体中,主要是它的名称和句柄值,然后插入到链接svclist的头部去。这样,client来向service manager查询服务接口时,只要给定服务名称,service manger就可以返回相应的句柄值了。

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

             bio_put_uint32(reply, 0);  

       svcmgr_handler函数执行完成后,返回到binder_parse函数,执行下面语句:

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

       我们看一下binder_send_reply的实现,从函数名就可以猜到它要做什么了,告诉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告诉binder驱动程序执行bc_free_buffer和bc_reply命令,前者释放之前在binder_transaction分配的空间,地址为buffer_to_free,buffer_to_free这个地址是binder驱动程序把自己在内核空间用的地址转换成用户空间地址再传给service manager的,所以binder驱动程序拿到这个地址后,知道怎么样释放这个空间;后者告诉mediaplayerservice,它的addservice操作已经完成了,错误码是0,保存在data.txn.data中。

       再来看binder_write函数:

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。

       这里又是一个ioctl的binder_write_read操作。直入到驱动程序的binder_ioctl函数后,执行binder_write_read命令,这里就不累述了。

       最后,从binder_ioctl执行到binder_thread_write函数,我们首先看第一个命令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; 
} 

       首先通过看这个语句:

get_user(data_ptr, (void * __user *)ptr) 

       这个是获得要删除的buffer的用户空间地址,接着通过下面这个语句来找到这个地址对应的struct binder_buffer信息:

            buffer = binder_buffer_lookup(proc, data_ptr);  

       因为这个空间是前面在binder_transaction里面分配的,所以这里一定能找到。

       最后,就可以释放这块内存了:

            binder_transaction_buffer_release(proc, buffer, null);  
            binder_free_buf(proc, buffer);  

       再来看另外一个命令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函数:

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函数中被mediaplayerservice启动后进程唤醒后,在最后会把当前处理完的事务放在thread->transaction_stack中:

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取回来,并且放在本地变量in_reply_to中:

                      in_reply_to = thread->transaction_stack;  

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

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

        然后得到target_list和target_wait:

                    target_list = &target_thread->todo;  
                    target_wait = &target_thread->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)) { 
 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; 
} 

          我们在前面已经分析过了,这里不再重复。但是有一点要注意的是,这里target_node为null,因此,t->buffer->target_node也为null。

          函数本来有一个for循环,用来处理数据中的binder对象,这里由于没有binder对象,所以就略过了。到了下面这句代码:

                   binder_pop_transaction(target_thread, in_reply_to);  

          我们看看做了什么事情: 

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函数:   

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和thread->todo队列中,这里的target_list指的就是最初调用iservicemanager::addservice的mediaplayerservice的server主线程的的thread->todo队列了,而thread->todo指的是service manager中用来回复iservicemanager::addservice请求的线程。

        最后,唤醒等待在target_wait队列上的线程了,就是最初调用iservicemanager::addservice的mediaplayerservice的server主线程了,它最后在binder_thread_read函数中睡眠在thread->wait上,就是这里的target_wait了:

                  if (target_wait)  
                              wake_up_interruptible(target_wait);  

        这样,service manger回复调用iservicemanager::addservice请求就算完成了,重新回到frameworks/base/cmds/servicemanager/binder.c文件中的binder_loop函数等待下一个client请求的到来。事实上,service manger回到binder_loop函数再次执行ioctl函数时候,又会再次进入到binder_thread_read函数。这时个会发现thread->todo不为空,这是因为刚才我们调用了:

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

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

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函数执行完这个ioctl调用后,才会在下一次调用ioctl进入到binder驱动程序进入休眠状态,等待下一次client的请求。

        上面讲到调用iservicemanager::addservice的mediaplayerservice的server主线程被唤醒了,于是,重新执行binder_thread_read函数:

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得到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函数,执行下面语句,把数据返回给用户空间:

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

         接着返回到用户空间ipcthreadstate::talkwithdriver函数,最后返回到ipcthreadstate::waitforresponse函数,最终执行到下面语句:

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函数里设置的。最终把结果保存在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函数中。

       至此,iservicemanager::addservice终于执行完毕了。这个过程非常复杂,但是如果我们能够深刻地理解这一过程,将能很好地理解binder机制的设计思想和实现过程。这里,对iservicemanager::addservice过程中mediaplayerservice、servicemanager和binderdriver之间的交互作一个小结:

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

                 processstate::self()->startthreadpool(); 
                 ipcthreadstate::self()->jointhreadpool();  

        首先看processstate::startthreadpool函数的实现:

void processstate::startthreadpool() 
{ 
 automutex _l(mlock); 
 if (!mthreadpoolstarted) { 
  mthreadpoolstarted = true; 
  spawnpooledthread(true); 
 } 
} 

       这里调用spwanpooledthread:

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函数,这里即为poolthread::threadloop函数:

virtual bool threadloop() 
{ 
 ipcthreadstate::self()->jointhreadpool(mismain); 
 return false; 
} 

       这里和frameworks/base/media/mediaserver/main_mediaserver.cpp文件中的main函数一样,最终都是调用了ipcthreadstate::jointhreadpool函数,它们的区别是,一个参数是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函数来和binder驱动程序进行交互,实际上就是调用talkwithdriver来等待client的请求,然后再调用executecommand来处理请求,而在executecommand函数中,最终会调用bbinder::transact来真正处理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的实现:

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函数来处理。在这个场景中,bnmediaplayerservice继承了bbinder类,并且重载了ontransact函数,因此,这里实际上是调用了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的请求了。在下一篇文章中,我们将介绍client如何通过service manager远程接口来获得server远程接口,进而调用server远程接口来使用server提供的服务,敬请关注。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网