1.什么是RPC(远程过程调用)
Binder系统的目的是实现远程过程调用(RPC),即进程A去调用进程B的某个函数,它是在进程间通信(IPC)的基础上实现的。RPC的一个应用场景如下:
A进程想去打开LED,它会去调用led_open,然后调用led_ctl,但是如果A进程并没有权限去打开驱动程序呢?
假设此时有一个进程B由权限去操作LED驱动程序,那么进程A可以通过如下方式来操作LED驱动:
①封装数据,即A进程首先把想要调用的B进程的某个函数的(事先约定好的)代号等信息封装成数据包
②A进程把封装好了的数据包通过IPC(进程间通信)发送给B进程
③B取出数据之后,通过从数据包里解析出来的函数的代号来调用它自己相应的led_open或led_ctl函数
整个过程的结果好像A程序直接来操纵LED一样,这就是所谓的RPC。整个过程涉及到了IPC(进程间通信)的三大要素,源、目的和数据。在这个例子里面,源就是进程A,目的是进程B,数据实际上就是一个双方约定好了数据格式的buffer。
2.Binder系统实现的RPC
Binder系统采用的是CS架构,提供服务的进程称为server进程,访问服务的进程称为client进程,server进程和client进程的通信需要依靠内核中的Binder驱动来进行。同时Binder系统提供了一个上下文的管理者servicemanager, server进程可以向servicemanager注册服务,然后client进程可以通过向servicemanager查询服务来获取server进程注册的服务。
回到上面的例子,A进程想操作LED,它可以通过将B进程的某个函数的(事先约定好的)代号通过IPC发给B进程,通过B进程来间接的操作LED,但是如果A进程不知道可以通过哪个进程来间接的操作LED呢,它应该将封装好了的数据包发送给哪个进程呢?这就引入了Binder系统的大管家servicemanager。首先B进程向servicemanager注册LED服务,然后我们的A进程就可以通过向servicemanager查询LED服务,就会得到一个handle,这个handle就是指向进程B的,这样进程A就知道把数据包(约定好数据格式的buffer)发送给哪个进程就可以间接的操作LED了。在这个例子中进程B就是server进程,进程A是client进程。
小小的总结一下,在 Binder系统中主要涉及到4个东西,一个是我们的A进程也就是client进程,一个是B进程也就是我们的server进程。client进程怎么知道要向哪一个server进程发送数据呢,中间就引入了Binder系统的大管家servicemanager。client进程、server进程和servicemanager之间的通信是建立在内核binder驱动的基础上的,它们四个的关系如下图所示
3.Binder系统的简单应用(基于Android内核,抛开Android系统框架)
在Android源码里面有一些C语言写的binder应用程序
frameworks/native/cmds/servicemanager/bctest.c frameworks/native/cmds/servicemanager/binder.c frameworks/native/cmds/servicemanager/binder.h frameworks/native/cmds/servicemanager/service_manager.c
我们可以参照这些程序,基于Android内核,在Linux上实现一个Binder RPC的程序来理解使用Binder实现进程间通信的整个函数调用过程。
我们首先把android源码frameworks/native/cmds/servicemanager目录下的内容拷贝到我们自己的工程中,然后基于bctest.c来实现我们的server和client程序,因为我们是脱离Android系统来实现的,所以还需要将依赖的头文件拷贝到工程中,然后对service_manager.c和binder.c做一些修改,去掉一些不必要的内容。最后我们还需要写一个Makefile文件来构建整个工程,工程结构如下图所示。
3.1.Server进程
首先实现Server程序,它实现两个函数,sayhello和sayhello_to,并通过binder系统将向ServiceManager注册服务,然后循环的从binder驱动读取client进程发过来请求数据,并且通过这些请求数据调用自己相应的sayhello和sayhello_to函数。整个过程如下图所示。
接着我们就来分析以下具体的代码
/*test_server.h*/#ifndef _TEST_SERVER_H #define _TEST_SERVER_H /*事先约定好的Server进程的相应函数的代号*/ #define HELLO_SVR_CMD_SAYHELLO 0 #define HELLO_SVR_CMD_SAYHELLO_TO 1 #endif // _TEST_SERVER_H
/*test_server.c*//* Copyright 2008 The Android Open Source Project*/ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <linux/types.h> #include<stdbool.h> #include <string.h> #include <private/android_filesystem_config.h> #include "binder.h" #include "test_server.h" int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr) {int status;unsigned iodata[512/4];struct binder_io msg, reply;bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode headerbio_put_string16_x(&msg, SVC_MGR_NAME);bio_put_string16_x(&msg, name);bio_put_obj(&msg, ptr);/*远程调用ServiceManager的do_add_service函数*/if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE))return -1;status = bio_get_uint32(&reply);binder_done(bs, &msg, &reply);return status; } void sayhello(void) {static int cnt = 0;fprintf(stderr, "say hello : %d\n", cnt++); } int sayhello_to(char *name) {static int cnt = 0;fprintf(stderr, "say hello to %s : %d\n", name, cnt++);return cnt; } int hello_service_handler(struct binder_state *bs,struct binder_transaction_data *txn,struct binder_io *msg,struct binder_io *reply) {/* 根据txn->code知道要调用哪一个函数* 如果需要参数, 可以从msg取出* 如果要返回结果, 可以把结果放入reply*//* sayhello* sayhello_to*/uint16_t *s;char name[512];size_t len;uint32_t handle;uint32_t strict_policy;int i;// 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);switch(txn->code) {case HELLO_SVR_CMD_SAYHELLO:sayhello();return 0;case HELLO_SVR_CMD_SAYHELLO_TO:/* 从msg里取出字符串 */s = bio_get_string16(msg, &len);if (s == NULL) {return -1;}for (i = 0; i < len; i++)name[i] = s[i];name[i] = '\0';/* 处理 */i = sayhello_to(name);/* 把结果放入reply */bio_put_uint32(reply, i);break;default:fprintf(stderr, "unknown code %d\n", txn->code);return -1;}return 0; } int main(int argc, char **argv) {int fd;struct binder_state *bs;uint32_t svcmgr = BINDER_SERVICE_MANAGER;uint32_t handle;int ret;/*打开并映射binder驱动*/bs = binder_open(128*1024);if (!bs) {fprintf(stderr, "failed to open binder driver\n");return -1;}/* 向ServiceManager注册服务 */ret = svcmgr_publish(bs, svcmgr, "hello", (void *)123);if (ret) {fprintf(stderr, "failed to publish hello service\n");return -1;}ret = svcmgr_publish(bs, svcmgr, "goodbye", (void *)124);if (ret) {fprintf(stderr, "failed to publish goodbye service\n");} #if 0while (1){/* read data *//* parse data, and process *//* reply */} #endif/*通过我们传入的hello_service_handler循环处理从binder驱动读出的数据*/binder_loop(bs, hello_service_handler);return 0; }
接着我们来分析一下这个binder_loop函数,它主要实现了3个功能
1.读数据
2.解析并处理数据
3.回复
void binder_loop(struct binder_state *bs, binder_handler func) {int res;struct binder_write_read bwr;uint32_t readbuf[32];//bwr.write_size = 0 表明下面的ioctl不会发起写操作,只不过发起读操作bwr.write_size = 0;bwr.write_consumed = 0;bwr.write_buffer = 0;readbuf[0] = BC_ENTER_LOOPER;binder_write(bs, readbuf, sizeof(uint32_t));for (;;) {bwr.read_size = sizeof(readbuf);bwr.read_consumed = 0;bwr.read_buffer = (uintptr_t) readbuf;/*通过ioctl从binder驱动中读数据*/res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);if (res < 0) {ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));break;}//读到数据之后调用binder_parse解析数据,如果传入func参数还会处理数据res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);if (res == 0) {ALOGE("binder_loop: unexpected reply?!\n");break;}if (res < 0) {ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));break;}} }
看一下我们是怎么处理数据的,注意我们传入的binder_handler这个参数,它是一个函数指针
int binder_parse(struct binder_state *bs, struct binder_io *bio,uintptr_t ptr, size_t size, binder_handler func) {int r = 1;uintptr_t end = ptr + (uintptr_t) size;while (ptr < end) {uint32_t cmd = *(uint32_t *) ptr;ptr += sizeof(uint32_t); #if TRACEfprintf(stderr,"%s:\n", cmd_name(cmd)); #endifswitch(cmd) {case BR_NOOP:break;case BR_TRANSACTION_COMPLETE:break;case BR_INCREFS:case BR_ACQUIRE:case BR_RELEASE:case BR_DECREFS: #if TRACEfprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *))); #endifptr += sizeof(struct binder_ptr_cookie);break;//我们收到的命令是BR_TRANSACTIONcase BR_TRANSACTION: {struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;if ((end - ptr) < sizeof(*txn)) {ALOGE("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;//接收到数据之后,构造一个binder_iobio_init(&reply, rdata, sizeof(rdata), 4);bio_init_from_txn(&msg, txn);//调用我们的处理函数res = func(bs, txn, &msg, &reply);//处理完之后发送一个replybinder_send_reply(bs, &reply, txn->data.ptr.buffer, res);}ptr += sizeof(*txn);break;}case BR_REPLY: {struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;if ((end - ptr) < sizeof(*txn)) {ALOGE("parse: reply too small!\n");return -1;}binder_dump_txn(txn);if (bio) {bio_init_from_txn(bio, txn);bio = 0;} else {/* todo FREE BUFFER */}ptr += sizeof(*txn);r = 0;break;}case BR_DEAD_BINDER: {struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;ptr += sizeof(binder_uintptr_t);death->func(bs, death->ptr);break;}case BR_FAILED_REPLY:r = -1;break;case BR_DEAD_REPLY:r = -1;break;default:ALOGE("parse: OOPS %d\n", cmd);return -1;}}return r; }
3.2.Client进程
Client进程和Server进程的大致流程差不多,它首先打开和映射binder驱动,然后向ServiceManager查询服务,最后通过查询服务时ServiceManager返回的handle远程调用Server进程的函数,主要流程如下所示。
下面我们就分析一下具体的源码
/*test_client.c*//* Copyright 2008 The Android Open Source Project*/ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <linux/types.h> #include<stdbool.h> #include <string.h> #include <private/android_filesystem_config.h> #include "binder.h" #include "test_server.h" uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name) {uint32_t handle;unsigned iodata[512/4];struct binder_io msg, reply;bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode headerbio_put_string16_x(&msg, SVC_MGR_NAME);bio_put_string16_x(&msg, name);/*远程调用ServiceManager的do_find_service函数*/if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE))return 0;handle = bio_get_ref(&reply);if (handle)binder_acquire(bs, handle);binder_done(bs, &msg, &reply);return handle; } struct binder_state *g_bs; uint32_t g_handle; void sayhello(void) {unsigned iodata[512/4];struct binder_io msg, reply;/* 构造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode header/* 放入参数 *//* 调用binder_call远程调用Server的sayhello函数*/if (binder_call(g_bs, &msg, &reply, g_handle, HELLO_SVR_CMD_SAYHELLO))return ;/* 从reply中解析出返回值 */binder_done(g_bs, &msg, &reply);} int sayhello_to(char *name) {unsigned iodata[512/4];struct binder_io msg, reply;int ret;/* 构造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode header/* 放入参数 */bio_put_string16_x(&msg, name);/* 调用binder_call远程调用Server的sayhello_to函数 */if (binder_call(g_bs, &msg, &reply, g_handle, HELLO_SVR_CMD_SAYHELLO_TO))return 0;/* 从reply中解析出返回值 */ret = bio_get_uint32(&reply);binder_done(g_bs, &msg, &reply);return ret;} /* ./test_client hello* ./test_client hello <name>*/ int main(int argc, char **argv) {int fd;struct binder_state *bs;uint32_t svcmgr = BINDER_SERVICE_MANAGER;uint32_t handle;int ret;if (argc < 2){fprintf(stderr, "Usage:\n");fprintf(stderr, "%s hello\n", argv[0]);fprintf(stderr, "%s hello <name>\n", argv[0]);return -1;}/*打开binder驱动*/bs = binder_open(128*1024);if (!bs) {fprintf(stderr, "failed to open binder driver\n");return -1;}g_bs = bs;/* 向ServiceManager查询hello服务 */handle = svcmgr_lookup(bs, svcmgr, "hello");if (!handle) {fprintf(stderr, "failed to get hello service\n");return -1;}g_handle = handle;/* send data to server */if (argc == 2) {sayhello();} else if (argc == 3) {ret = sayhello_to(argv[2]);fprintf(stderr, "get ret of sayhello_to = %d\n", ret); }binder_release(bs, handle);return 0; }
这里需要注意的一点是,不管我们的Server进程还是Client进程,他们在远程调用其他进程的函数的时候,都是通过binder_call这个函数来实现的,下面我们就来分析一下这个函数。
int binder_call(struct binder_state *bs,struct binder_io *msg, struct binder_io *reply,uint32_t target, uint32_t code) {int res;/*构造参数*/struct binder_write_read bwr;struct {uint32_t cmd;struct binder_transaction_data txn;} __attribute__((packed)) writebuf;unsigned readbuf[32];if (msg->flags & BIO_F_OVERFLOW) {fprintf(stderr,"binder: txn buffer overflow\n");goto fail;}writebuf.cmd = BC_TRANSACTION;writebuf.txn.target.handle = target;writebuf.txn.code = code;writebuf.txn.flags = 0;writebuf.txn.data_size = msg->data - msg->data0;writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;bwr.write_size = sizeof(writebuf);bwr.write_consumed = 0;bwr.write_buffer = (uintptr_t) &writebuf;hexdump(msg->data0, msg->data - msg->data0);for (;;) {bwr.read_size = sizeof(readbuf);bwr.read_consumed = 0;bwr.read_buffer = (uintptr_t) readbuf;/*调用ioctl发送数据*/res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);if (res < 0) {fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));goto fail;}/*解析返回的数据*/res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);if (res == 0) return 0;if (res < 0) goto fail;} fail:memset(reply, 0, sizeof(*reply));reply->flags |= BIO_F_IOERROR;return -1; }
其中第一个参数用来描述当前binder的状态,是调用binder_open时返回的,第二个参数是要发送的数据,第三个参数用来保存返回的数据,第四非参数是数据发送的目的地,即向谁发送数据,第五个参数是要调用的远程的函数的约定好的代号。
3.3.ServiceManager进程
紧接着我们就来分析一下它的main函数,和其他一些主要的函数
int main(int argc, char **argv) {struct binder_state *bs;/*打开binder驱动*/bs = binder_open(128*1024);if (!bs) {ALOGE("failed to open binder driver\n");return -1;}/*告诉驱动,我是大管家*/if (binder_become_context_manager(bs)) {ALOGE("cannot become context manager (%s)\n", strerror(errno));return -1;}svcmgr_handle = BINDER_SERVICE_MANAGER;/*进入无限循环,处理client端发来的请求*/binder_loop(bs, svcmgr_handler);return 0; }
分析一下binder_become_context_manager这个函数,看一下是怎样向驱动注册为大管家的
int binder_become_context_manager(struct binder_state *bs) {/*通过ioctl,传递BINDER_SET_CONTEXT_MGR指令*/return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0); }
整个流程的时序如下图所示
总结一下,整个binder远程过程调用,就是首先大管家ServiceManager告诉binder驱动,我现在是大管家了,然后Server进程和Client进程通过这个大管家互相了解了之后,Client进程就可以远程调用Server进程的函数了。