Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析
在上一篇文章中,我们分析了android系统进程间通信机制binder中的server在启动过程使用service manager的addservice接口把自己添加到service manager守护过程中接受管理。在这一篇文章中,我们将深入到binder驱动程序源代码去分析client是如何通过service manager的getservice接口中来获得server远程接口的。client只有获得了server的远程接口之后,才能进一步调用server提供的服务。
这里,我们仍然是通过android系统中自带的多媒体播放器为例子来说明client是如何通过iservicemanager::getservice接口来获得mediaplayerservice这个server的远程接口的。假设计读者已经阅读过前面三篇文章浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路、浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路和android系统进程间通信(ipc)机制binder中的server启动过程源代码分析,即假设service manager和mediaplayerservice已经启动完毕,service manager现在等待client的请求。
这里,我们要举例子说明的client便是mediaplayer了,它声明和实现在frameworks/base/include/media/mediaplayer.h和frameworks/base/media/libmedia/mediaplayer.cpp文件中。mediaplayer继承于imediadeathnotifier类,这个类声明和实现在frameworks/base/include/media/imediadeathnotifier.h和frameworks/base/media/libmedia//imediadeathnotifier.cpp文件中,里面有一个静态成员函数getmeidaplayerservice,它通过iservicemanager::getservice接口来获得mediaplayerservice的远程接口。
在介绍imediadeathnotifier::getmeidaplayerservice函数之前,我们先了解一下这个函数的目标。看来前面浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路这篇文章的读者知道,我们在获取service manager远程接口时,最终是获得了一个bpservicemanager对象的iservicemanager接口。类似地,我们要获得mediaplayerservice的远程接口,实际上就是要获得一个称为bpmediaplayerservice对象的imediaplayerservice接口。现在,我们就先来看一下bpmediaplayerservice的类图:
从这个类图可以看到,bpmediaplayerservice继承于bpinterface<imediaplayerservice>类,即bpmediaplayerservice继承了imediaplayerservice类和bprefbase类,这两个类又分别继续了refbase类。bprefbase类有一个成员变量mremote,它的类型为ibinder,实际是一个bpbinder对象。bpbinder类使用了ipcthreadstate类来与binder驱动程序进行交互,而ipcthreadstate类有一个成员变量mprocess,它的类型为processstate,ipcthreadstate类借助processstate类来打开binder设备文件/dev/binder,因此,它可以和binder驱动程序进行交互。
bpmediaplayerservice的构造函数有一个参数impl,它的类型为const sp<ibinder>&,从上面的描述中,这个实际上就是一个bpbinder对象。这样,要创建一个bpmediaplayerservice对象,首先就要有一个bpbinder对象。再来看bpbinder类的构造函数,它有一个参数handle,类型为int32_t,这个参数的意义就是请求mediaplayerservice这个远程接口的进程对mediaplayerservice这个binder实体的引用了。因此,获取mediaplayerservice这个远程接口的本质问题就变为从service manager中获得mediaplayerservice的一个句柄了。
现在,我们就来看一下imediadeathnotifier::getmeidaplayerservice的实现:
// establish binder interface to mediaplayerservice /*static*/const sp<imediaplayerservice>& imediadeathnotifier::getmediaplayerservice() { logv("getmediaplayerservice"); mutex::autolock _l(sservicelock); if (smediaplayerservice.get() == 0) { sp<iservicemanager> sm = defaultservicemanager(); sp<ibinder> binder; do { binder = sm->getservice(string16("media.player")); if (binder != 0) { break; } logw("media player service not published, waiting..."); usleep(500000); // 0.5 s } while(true); if (sdeathnotifier == null) { sdeathnotifier = new deathnotifier(); } binder->linktodeath(sdeathnotifier); smediaplayerservice = interface_cast<imediaplayerservice>(binder); } loge_if(smediaplayerservice == 0, "no media player service!?"); return smediaplayerservice; }
函数首先通过defaultservicemanager函数来获得service manager的远程接口,实际上就是获得bpservicemanager的iservicemanager接口,具体可以参考浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路一文。总的来说,这里的语句:
sp<iservicemanager> sm = defaultservicemanager();
相当于是:
sp<iservicemanager> sm = new bpservicemanager(new bpbinder(0));
这里的0表示service manager的远程接口的句柄值是0。
接下去的while循环是通过sm->getservice接口来不断尝试获得名称为“media.player”的service,即mediaplayerservice。为什么要通过这无穷循环来得mediaplayerservice呢?因为这时候mediaplayerservice可能还没有启动起来,所以这里如果发现取回来的binder接口为null,就睡眠0.5秒,然后再尝试获取,这是获取service接口的标准做法。
我们来看一下bpservicemanager::getservice的实现:
class bpservicemanager : public bpinterface<iservicemanager> { ...... virtual sp<ibinder> getservice(const string16& name) const { unsigned n; for (n = 0; n < 5; n++){ sp<ibinder> svc = checkservice(name); if (svc != null) return svc; logi("waiting for service %s...\n", string8(name).string()); sleep(1); } return null; } virtual sp<ibinder> checkservice( const string16& name) const { parcel data, reply; data.writeinterfacetoken(iservicemanager::getinterfacedescriptor()); data.writestring16(name); remote()->transact(check_service_transaction, data, &reply); return reply.readstrongbinder(); } ...... };
bpservicemanager::getservice通过bpservicemanager::checkservice执行操作。
在bpservicemanager::checkservice中,首先是通过parcel::writeinterfacetoken往data写入一个rpc头,这个我们在android系统进程间通信(ipc)机制binder中的server启动过程源代码分析一文已经介绍过了,就是写往data里面写入了一个整数和一个字符串“android.os.iservicemanager”, service manager来处理check_service_transaction请求之前,会先验证一下这个rpc头,看看是否正确。接着再往data写入一个字符串name,这里就是“media.player”了。回忆一下android系统进程间通信(ipc)机制binder中的server启动过程源代码分析这篇文章,那里已经往service manager中注册了一个名字为“media.player”的mediaplayerservice。
这里的remote()返回的是一个bpbinder,具体可以参考浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager接口之路一文,于是,就进行到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; }
这里的mhandle = 0,code = check_service_transaction,flags = 0。
这里再进入到ipcthread::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; }
首先是调用函数writetransactiondata写入将要传输的数据到ipcthreadstate的成员变量mout中去:
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; }
结构体binder_transaction_data在上一篇文章android系统进程间通信(ipc)机制binder中的server启动过程源代码分析已经介绍过,这里不再累述,这个结构体是用来描述要传输的参数的内容的。这里着重描述一下将要传输的参数tr里面的内容,handle = 0,code = check_service_transaction,cmd = bc_transaction,data里面的数据分别为:
writeint32(ipcthreadstate::self()->getstrictmodepolicy() | strict_mode_penalty_gather); writestring16("android.os.iservicemanager"); writestring16("media.player");
这是在bpservicemanager::checkservice函数里面写进去的,其中前两个是rpc头,service manager在收到这个请求时会验证这两个参数是否正确,这点前面也提到了。ipcthread->getstrictmodepolicy默认返回0,strict_mode_penalty_gather定义为:
// note: must be kept in sync with android/os/strictmode.java's penalty_gather
#define strict_mode_penalty_gather 0x100
我们不关心这个参数的含义,这不会影响我们分析下面的源代码,有兴趣的读者可以研究一下。这里要注意的是,要传输的参数不包含有binder对象,因此tr.offsets_size = 0。要传输的参数最后写入到ipcthreadstate的成员变量mout中,包括cmd和tr两个数据。
回到ipcthread::transact函数中,由于(flags & tf_one_way) == 0为true,即这是一个同步请求,并且reply != null,
最终调用:
err = 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; }
这个函数通过ipcthreadstate::talkwithdriver与驱动程序进行交互:
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; } ...... // 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 defined(have_android_os) if (ioctl(mprocess->mdriverfd, binder_write_read, &bwr) >= 0) err = no_error; else err = -errno; #else err = invalid_operation; #endif ...... } while (err == -eintr); ...... 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; } return err; }
这里的needread为true,因此,bwr.read_size大于0;outavail也大于0,因此,bwr.write_size也大于0。函数最后通过:
ioctl(mprocess->mdriverfd, binder_write_read, &bwr)
进入到binder驱动程序的binder_ioctl函数中。注意,这里的mprocess->mdriverfd是在我们前面调用defaultservicemanager函数获得service manager远程接口时,打开的设备文件/dev/binder的文件描述符,mprocess是ipcsthreadstate的成员变量。
binder驱动程序的binder_ioctl函数中,我们只关注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; } ...... default: ret = -einval; goto err; } ret = 0; err: ...... return ret; }
这里的filp->private_data的值是在defaultservicemanager函数创建processstate对象时,在processstate构造函数通过open文件操作函数打开设备文件/dev/binder时设置好的,它表示的是调用open函数打开设备文件/dev/binder的进程上下文信息,这里将它取出来保存在proc本地变量中。
这里的thread本地变量表示当前线程上下文信息,通过binder_get_thread函数获得。在前面执行processstate构造函数时,也会通过ioctl文件操作函数进入到这个函数,那是第一次进入到binder_ioctl这里,因此,调用binder_get_thread时,表示当前进程上下文信息的proc变量还没有关于当前线程的上下文信息,因此,会为proc创建一个表示当前线程上下文信息的thread,会保存在proc->threads表示的红黑树结构中。这里调用binder_get_thread就可以直接从proc找到并返回了。
进入到binder_write_read相关的逻辑。先看看binder_write_read的定义:
#define binder_write_read _iowr('b', 1, struct binder_write_read)
这里可以看出,binder_write_read命令的参数类型为struct binder_write_read:
struct binder_write_read { signed long write_size; /* bytes to write */ signed long write_consumed; /* bytes consumed by driver */ unsigned long write_buffer; signed long read_size; /* bytes to read */ signed long read_consumed; /* bytes consumed by driver */ unsigned long read_buffer; };
这个结构体的含义可以参考浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路一文。这里首先是通过copy_from_user函数把用户传进来的参数的内容拷贝到本地变量bwr中。
从上面的调用过程,我们知道,这里bwr.write_size是大于0的,因此进入到binder_thread_write函数中,我们只关注bc_transaction相关的逻辑:
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; } ...... default: printk(kern_err "binder: %d:%d unknown command %d\n", proc->pid, thread->pid, cmd); return -einval; } *consumed = ptr - buffer; } return 0; }
这里再次把用户传出来的参数拷贝到本地变量tr中,tr的类型为struct binder_transaction_data,这个就是前面我们在ipcthreadstate::writetransactiondata写入的内容了。
接着进入到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 (!(tr->flags & tf_one_way) && thread->transaction_stack) { ...... } } 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; } binder_stats.obj_created[binder_stat_transaction]++; tcomplete = kzalloc(sizeof(*tcomplete), gfp_kernel); if (tcomplete == null) { return_error = br_failed_reply; goto err_alloc_tcomplete_failed; } binder_stats.obj_created[binder_stat_transaction_complete]++; t->debug_id = ++binder_last_id; ...... 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 (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,表示这是一个bc_transaction命令。
前面我们提到,传给驱动程序的handle值为0,即这里的tr->target.handle = 0,表示请求的目标binder对象是service manager,因此有:
target_node = binder_context_mgr_node; target_proc = target_node->proc; target_list = &target_proc->todo; target_wait = &target_proc->wait;
其中binder_context_mgr_node是在service manager通知binder驱动程序它是守护过程时创建的。
接着创建一个待完成事项tcomplete,它的类型为struct binder_work,这是等一会要保存在当前线程的todo队列去的,表示当前线程有一个待完成的事务。紧跟着创建一个待处理事务t,它的类型为struct binder_transaction,这是等一会要存在到service manager的todo队列去的,表示service manager当前有一个事务需要处理。同时,这个待处理事务t也要存放在当前线程的待完成事务transaction_stack列表中去:
t->from_parent = thread->transaction_stack;
thread->transaction_stack = t;
这样表明当前线程还有事务要处理。
继续往下看,就是分别把tcomplete和t放在当前线程thread和service manager进程的todo队列去了:
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);
最后,service manager有事情可做了,就要唤醒它了:
wake_up_interruptible(target_wait);
前面我们提到,此时service manager正在等待client的请求,也就是service manager此时正在进入到binder驱动程序的binder_thread_read函数中,并且休眠在target->wait上,具体参考浅谈service manager成为android进程间通信(ipc)机制binder守护进程之路一文。
这里,我们暂时忽略service manager被唤醒之后的情景,继续看当前线程的执行。
函数binder_transaction执行完成之后,就一路返回到binder_ioctl函数里去了。函数binder_ioctl从binder_thread_write函数调用处返回后,发现bwr.read_size大于0,于是就进入到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; }
函数首先是写入一个操作码br_noop到用户传进来的缓冲区中去。
回忆一下上面的binder_transaction函数,这里的thread->transaction_stack != null,并且thread->todo也不为空,所以线程不会进入休眠状态。
进入while循环中,首先是从thread->todo队列中取回待处理事项w,w的类型为binder_work_transaction_complete,这也是在binder_transaction函数里面设置的。对binder_work_transaction_complete的处理也很简单,只是把一个操作码br_transaction_complete写回到用户传进来的缓冲区中去。这时候,用户传进来的缓冲区就包含两个操作码了,分别是br_noop和binder_work_transaction_complete。
binder_thread_read执行完之后,返回到binder_ioctl函数中,将操作结果写回到用户空间中去:
if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { ret = -efault; goto err; }
最后就返回到ipcthreadstate::talkwithdriver函数中了。
ipcthreadstate::talkwithdriver函数从下面语句:
ioctl(mprocess->mdriverfd, binder_write_read, &bwr)
返回后,首先是清空之前写入binder驱动程序的内容:
if (bwr.write_consumed > 0) { if (bwr.write_consumed < (ssize_t)mout.datasize()) mout.remove(0, bwr.write_consumed); else mout.setdatasize(0); }
接着是设置从binder驱动程序读取的内容:
if (bwr.read_consumed > 0) { min.setdatasize(bwr.read_consumed); min.setdataposition(0); }
然后就返回到ipcthreadstate::waitforresponse去了。ipcthreadstate::waitforresponse函数的处理也很简单,就是处理刚才从binder驱动程序读入内容了。从前面的分析中,我们知道,从binder驱动程序读入的内容就是两个整数了,分别是br_noop和br_transaction_complete。对br_noop的处理很简单,正如它的名字所示,什么也不做;而对br_transaction_complete的处理,就分情况了,如果这个请求是异步的,那个整个bc_transaction操作就完成了,如果这个请求是同步的,即要等待回复的,也就是reply不为空,那么还要继续通过ipcthreadstate::talkwithdriver进入到binder驱动程序中去等待bc_transaction操作的处理结果。
这里属于后一种情况,于是再次通过ipcthreadstate::talkwithdriver进入到binder驱动程序的binder_ioctl函数中。不过这一次在binder_ioctl函数中,bwr.write_size等于0,而bwr.read_size大于0,于是再次进入到binder_thread_read函数中。这时候thread->transaction_stack仍然不为null,不过thread->todo队列已经为空了,因为前面我们已经处理过thread->todo队列的内容了,于是就通过下面语句:
ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
进入休眠状态了,等待service manager的唤醒。
现在,我们终于可以回到service manager被唤醒之后的过程了。前面我们说过,service manager此时正在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; t->saved_priority = task_nice(current); if (t->priority < target_node->min_priority && !(t->flags & tf_one_way)) binder_set_nice(t->priority); else if (!(t->flags & tf_one_way) || t->saved_priority > target_node->min_priority) binder_set_nice(target_node->min_priority); 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.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 { ...... } break; } done: *consumed = ptr - buffer; ...... return 0; }
这里就是从语句中唤醒了:
ret = wait_event_interruptible_exclusive(proc->wait, binder_has_proc_work(proc, thread));
service manager唤醒过来看,继续往下执行,进入到while循环中。首先是从proc->todo中取回待处理事项w。这个事项w的类型是binder_work_transaction,这是上面调用binder_transaction的时候设置的,于是通过w得到待处理事务t:
t = container_of(w, struct binder_transaction, work);
接下来的内容,就把cmd和t->buffer的内容拷贝到用户传进来的缓冲区去了,这里就是service manager从用户空间传进来的缓冲区了:
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);
注意,这里先是把t->buffer的内容拷贝到本地变量tr中,再拷贝到用户空间缓冲区去。关于t->buffer内容的拷贝,请参考android系统进程间通信(ipc)机制binder中的server启动过程源代码分析一文,它的一个关键地方是binder驱动程序和service manager守护进程共享了同一个物理内存的内容,拷贝的只是这个物理内存在用户空间的虚拟地址回去:
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驱动程序这次操作来说,这个事项就算是处理完了,就要从todo队列中删除了:
list_del(&t->work.entry);
紧接着,还不放删除这个事务,因为它还要等待service manager处理完成后,再进一步处理,因此,放在thread->transaction_stack队列中:
t->to_parent = thread->transaction_stack;
t->to_thread = thread;
thread->transaction_stack = t;
还要注意的一个地方是,上面写入的cmd = br_transaction,告诉service manager守护进程,它要做什么事情,后面我们会看到相应的分析。
这样,binder_thread_read函数就处理完了,回到binder_ioctl函数中,同样是操作结果写回到用户空间的缓冲区中去:
if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { ret = -efault; goto err; }
最后,就返回到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; } } }
这里就是从下面的语句:
res = ioctl(bs->fd, binder_write_read, &bwr);
返回来了。接着就进入binder_parse函数处理从binder驱动程序里面读取出来的数据:
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++; switch(cmd) { ...... case br_transaction: { struct binder_txn *txn = (void *) ptr; ...... 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驱动程序写入到用户空间的缓冲区中的cmd为br_transaction,因此,这里我们只关注br_transaction相关的逻辑。
这里用到的两个数据结构struct binder_txn和struct binder_io可以参考前面一篇文章android系统进程间通信(ipc)机制binder中的server启动过程源代码分析,这里就不复述了。
接着往下看,函数调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; // logi("target=%p code=%d pid=%d uid=%d\n", // txn->target, txn->code, txn->sender_pid, txn->sender_euid); 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_get_service: case svc_mgr_check_service: s = bio_get_string16(msg, &len); ptr = do_find_service(bs, s, len); if (!ptr) break; bio_put_ref(reply, ptr); return 0; ...... } default: loge("unknown code %d\n", txn->code); return -1; } bio_put_uint32(reply, 0); return 0; }
这里, service manager要处理的code是svc_mgr_check_service,这是在前面的bpservicemanager::checkservice函数里面设置的。
回忆一下,在bpservicemanager::checkservice时,传给binder驱动程序的参数为:
writeint32(ipcthreadstate::self()->getstrictmodepolicy() | strict_mode_penalty_gather);
writestring16("android.os.iservicemanager");
writestring16("media.player");
这里的语句:
strict_policy = bio_get_uint32(msg); s = bio_get_string16(msg, &len); s = bio_get_string16(msg, &len);
其中,会验证一下传进来的第二个参数,即"android.os.iservicemanager"是否正确,这个是验证rpc头,注释已经说得很清楚了。
最后,就是调用do_find_service函数查找是存在名称为"media.player"的服务了。回忆一下前面一篇文章android系统进程间通信(ipc)机制binder中的server启动过程源代码分析,mediaplayerservice已经把一个名称为"media.player"的服务注册到service manager中,所以这里一定能找到。我们看看do_find_service这个函数:
void *do_find_service(struct binder_state *bs, uint16_t *s, unsigned len) { struct svcinfo *si; si = find_svc(s, len); // logi("check_service('%s') ptr = %p\n", str8(s), si ? si->ptr : 0); if (si && si->ptr) { return si->ptr; } else { return 0; } }
这里又调用了find_svc函数:
struct svcinfo *find_svc(uint16_t *s16, unsigned len) { struct svcinfo *si; for (si = svclist; si; si = si->next) { if ((len == si->len) && !memcmp(s16, si->name, len * sizeof(uint16_t))) { return si; } } return 0; }
就是在svclist列表中查找对应名称的svcinfo了。
然后返回到do_find_service函数中。回忆一下前面一篇文章android系统进程间通信(ipc)机制binder中的server启动过程源代码分析,这里的si->ptr就是指mediaplayerservice这个binder实体在service manager进程中的句柄值了。
回到svcmgr_handler函数中,调用bio_put_ref函数将这个binder引用写回到reply参数。我们看看bio_put_ref的实现:
void bio_put_ref(struct binder_io *bio, void *ptr) { struct binder_object *obj; if (ptr) obj = bio_alloc_obj(bio); else obj = bio_alloc(bio, sizeof(*obj)); if (!obj) return; obj->flags = 0x7f | flat_binder_flag_accepts_fds; obj->type = binder_type_handle; obj->pointer = ptr; obj->cookie = 0; }
这里很简单,就是把一个类型为binder_type_handle的binder_object写入到reply缓冲区中去。这里的binder_object就是相当于是flat_binder_obj了,具体可以参考android系统进程间通信(ipc)机制binder中的server启动过程源代码分析一文。
再回到svcmgr_handler函数中,最后,还写入一个0值到reply缓冲区中,表示操作结果码:
bio_put_uint32(reply, 0);
最后返回到binder_parse函数中,调用binder_send_reply函数将操作结果反馈给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)); }
注意,这里的status参数为0。从这里可以看出,binder_send_reply告诉binder驱动程序执行bc_free_buffer和bc_reply命令,前者释放之前在binder_transaction分配的空间,地址为buffer_to_free,buffer_to_free这个地址是binder驱动程序把自己在内核空间用的地址转换成用户空间地址再传给service manager的,所以binder驱动程序拿到这个地址后,知道怎么样释放这个空间;后者告诉binder驱动程序,它的svc_mgr_check_service操作已经完成了,要查询的服务的句柄值也是保存在data.txn.data,操作结果码是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命令,这个命令的执行在前面一篇文章android系统进程间通信(ipc)机制binder中的server启动过程源代码分析已经介绍过了,这里就不再累述了。
我们重点关注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; } ...... thread->transaction_stack = in_reply_to->to_parent; target_thread = in_reply_to->from; ...... 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; } binder_stats.obj_created[binder_stat_transaction]++; 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; } ...... 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_handle: case binder_type_weak_handle: { struct binder_ref *ref = binder_get_ref(proc, fp->handle); if (ref == null) { ...... return_error = br_failed_reply; goto err_binder_get_ref_failed; } if (ref->node->proc == target_proc) { ...... } else { struct binder_ref *new_ref; new_ref = binder_get_ref_for_node(target_proc, ref->node); if (new_ref == null) { return_error = br_failed_reply; goto err_binder_get_ref_for_node_failed; } fp->handle = new_ref->desc; binder_inc_ref(new_ref, fp->type == binder_type_handle, null); ...... } } break; ...... } } 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; ...... }
这次进入binder_transaction函数的情形和上面介绍的binder_transaction函数的情形基本一致,只是这里的proc、thread和target_proc、target_thread调换了角色,这里的proc和thread指的是service manager进程,而target_proc和target_thread指的是刚才请求svc_mgr_check_service的进程。
那么,这次是如何找到target_proc和target_thread呢。首先,我们注意到,这里的reply等于1,其次,上面我们提到,binder驱动程序在唤醒service manager,告诉它有一个事务t要处理时,事务t虽然从service manager的todo队列中删除了,但是仍然保留在transaction_stack中。因此,这里可以从thread->transaction_stack找回这个等待回复的事务t,然后通过它找回target_proc和target_thread:
in_reply_to = thread->transaction_stack; target_thread = in_reply_to->from; target_list = &target_thread->todo; target_wait = &target_thread->wait;
再接着往下看,由于service manager返回来了一个binder引用,所以这里要处理一下,就是中间的for循环了。这是一个binder_type_handle类型的binder引用,这是前面设置的。先把t->buffer->data的内容转换为一个struct flat_binder_object对象fp,这里的fp->handle值就是这个service在service manager进程里面的引用值了。接通过调用binder_get_ref函数得到binder引用对象struct binder_ref类型的对象ref:
struct binder_ref *ref = binder_get_ref(proc, fp->handle);
这里一定能找到,因为前面mediaplayerservice执行iservicemanager::addservice的时候把自己添加到service manager的时候,会在service manager进程中创建这个binder引用,然后把这个binder引用的句柄值返回给service manager用户空间。
这里面的ref->node->proc不等于target_proc,因为这个binder实体是属于创建mediaplayerservice的进程的,而不是请求这个服务的远程接口的进程的,因此,这里调用binder_get_ref_for_node函数为这个binder实体在target_proc创建一个引用:
struct binder_ref *new_ref; new_ref = binder_get_ref_for_node(target_proc, ref->node);
然后增加引用计数:
binder_inc_ref(new_ref, fp->type == binder_type_handle, null);
这样,返回数据中的binder对象就处理完成了。注意,这里会把fp->handle的值改为在target_proc中的引用值:
fp->handle = new_ref->desc;
这里就相当于是把t->buffer->data里面的binder对象的句柄值改写了。因为这是在另外一个不同的进程里面的binder引用,所以句柄值当然要用新的了。这个值最终是要拷贝回target_proc进程的用户空间去的。
再往下看:
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 { ...... }
这里reply等于1,执行binder_pop_transaction函数把当前事务in_reply_to从target_thread->transaction_stack队列中删掉,这是上次调用binder_transaction函数的时候设置的,现在不需要了,所以把它删掉。
再往后的逻辑就跟前面执行binder_transaction函数时候一样了,这里不再介绍。最后的结果就是唤醒请求svc_mgr_check_service操作的线程:
if (target_wait)
wake_up_interruptible(target_wait);
这样,service manger回复调用svc_mgr_check_service请求就算完成了,重新回到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的请求。
上面讲到调用请求svc_mgr_check_service操作的线程被唤醒了,于是,重新执行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; }
就是从下面这个调用:
ret = wait_event_interruptible(thread->wait, binder_has_thread_work(thread));
被唤醒过来了。在while循环中,从thread->todo得到w,w->type为binder_work_transaction,于是,得到t。从上面可以知道,service manager返回来了一个binder引用和一个结果码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);
我们简单看一下parcel::ipcsetdatareference函数的实现:
void parcel::ipcsetdatareference(const uint8_t* data, size_t datasize, const size_t* objects, size_t objectscount, release_func relfunc, void* relcookie) { freedatanoinit(); merror = no_error; mdata = const_cast<uint8_t*>(data); mdatasize = mdatacapacity = datasize; //logi("setdatareference setting data size of %p to %lu (pid=%d)\n", this, mdatasize, getpid()); mdatapos = 0; logv("setdatareference setting data pos of %p to %d\n", this, mdatapos); mobjects = const_cast<size_t*>(objects); mobjectssize = mobjectscapacity = objectscount; mnextobjecthint = 0; mowner = relfunc; mownercookie = relcookie; scanforfds(); }
上面提到,返回来的数据中有一个binder引用,因此,这里的mobjectsize等于1,这个binder引用对应的位置记录在mobjects成员变量中。
从这里层层返回,最后回到bpservicemanager::checkservice函数中:
virtual sp<ibinder> bpservicemanager::checkservice( const string16& name) const { parcel data, reply; data.writeinterfacetoken(iservicemanager::getinterfacedescriptor()); data.writestring16(name); remote()->transact(check_service_transaction, data, &reply); return reply.readstrongbinder(); }
这里就是从:
remote()->transact(check_service_transaction, data, &reply);
返回来了。我们接着看一下reply.readstrongbinder函数的实现:
sp<ibinder> parcel::readstrongbinder() const { sp<ibinder> val; unflatten_binder(processstate::self(), *this, &val); return val; }
这里调用了unflatten_binder函数来构造一个binder对象:
status_t unflatten_binder(const sp<processstate>& proc, const parcel& in, sp<ibinder>* out) { const flat_binder_object* flat = in.readobject(false); if (flat) { switch (flat->type) { case binder_type_binder: *out = static_cast<ibinder*>(flat->cookie); return finish_unflatten_binder(null, *flat, in); case binder_type_handle: *out = proc->getstrongproxyforhandle(flat->handle); return finish_unflatten_binder( static_cast<bpbinder*>(out->get()), *flat, in); } } return bad_type; }
这里的flat->type是binder_type_handle,因此调用processstate::getstrongproxyforhandle函数:
sp<ibinder> processstate::getstrongproxyforhandle(int32_t handle) { sp<ibinder> result; automutex _l(mlock); handle_entry* e = lookuphandlelocked(handle); if (e != null) { // we need to create a new bpbinder if there isn't currently one, or we // are unable to acquire a weak reference on this current one. see comment // in getweakproxyforhandle() for more info about this. ibinder* b = e->binder; if (b == null || !e->refs->attemptincweak(this)) { b = new bpbinder(handle); e->binder = b; if (b) e->refs = b->getweakrefs(); result = b; } else { // this little bit of nastyness is to allow us to add a primary // reference to the remote proxy when this team doesn't have one // but another team is sending the handle to us. result.force_set(b); e->refs->decweak(this); } } return result; }
这里我们可以看到,processstate会把使用过的binder远程接口(bpbinder)缓存起来,这样下次从service manager那里请求得到相同的句柄(handle)时就可以直接返回这个binder远程接口了,不用再创建一个出来。这里是第一次使用,因此,e->binder为空,于是创建了一个bpbinder对象:
b = new bpbinder(handle); e->binder = b; if (b) e->refs = b->getweakrefs(); result = b;
最后,函数返回到imediadeathnotifier::getmediaplayerservice这里,从这个语句返回:
binder = sm->getservice(string16("media.player"));
这里,就相当于是:
binder = new bpbinder(handle);
最后,函数调用:
smediaplayerservice = interface_cast<imediaplayerservice>(binder);
到了这里,我们可以参考一下前面一篇文章浅谈android系统进程间通信(ipc)机制binder中的server和client获得service manager,就会知道,这里的interface_cast实际上最终调用了imediaplayerservice::asinterface函数:
android::sp<imediaplayerservice> imediaplayerservice::asinterface(const android::sp<android::ibinder>& obj) { android::sp<iservicemanager> intr; if (obj != null) { intr = static_cast<imediaplayerservice*>( obj->querylocalinterface(imediaplayerservice::descriptor).get()); if (intr == null) { intr = new bpmediaplayerservice(obj); } } return intr; }
这里的obj就是bpbinder,而bpbinder::querylocalinterface返回null,因此就创建了一个bpmediaplayerservice对象:
intr = new bpmediaplayerservice(new bpbinder(handle));
因此,我们最终就得到了一个bpmediaplayerservice对象,达到我们最初的目标。
有了这个bpmediaplayerservice这个远程接口之后,mediaplayer就可以调用mediaplayerservice的服务了。
至此,android系统进程间通信(ipc)机制binder中的client如何通过service manager的getservice函数获得server远程接口的过程就分析完了,binder机制的学习就暂告一段落了。
不过,细心的读者可能会发现,我们这里介绍的binder机制都是基于c/c++语言实现的,但是我们在编写应用程序都是基于java语言的,那么,我们如何使用java语言来使用系统的binder机制来进行进程间通信呢?这就是下一篇文章要介绍的内容了,敬请关注。
以上就是对android ipc binder client获得server 远程接口过程的源码分析,后续继续补充相关文章,谢谢大家对本站的支持!