framework/native/cmds/servicemanager/
SM本身也是一个Server,是Binder IPC通信过程中的守护进程,负责所有Server进程的注册,并处理Client进程的查询
前面说过,如果网络要能够使用,那DNS就必须在用户开始使用网络前就准备好,所以它是在网络接入的时候设置好,这样就不会存在时间上的误差;同理Android系统启动后会有很多进程需要相互通信,Server进程需要到Service Manager 进行注册,保留自己的信息,Cilent进程需要通过Service Manager查找Server进程的信息;既然这样那Service Manager就需要在所有进程通信前就启动。
这样的话,我们很容易就能想到它是在init进程解析init.rc脚本文件时被启动的
实际上Android系统是运行在Linux内核上的一系列“服务进程”,并不算一个完整意义上的操作系统,这些“服务进程”是维持设备正常工作的关键,而他们怎么来的呢?没错,就是他们的老父亲 ---- init 进程。
当Linux Kernel启动后,作为Android中第一个进程就会被启动,init进程的pid是0;接着执行main函数,在这里就会解析init.rc文件,然后Service Manager就会被启动。
其它Android系统服务进程大多是在这个脚本文件中被启动的,比如Zygote。
init.rc文件语法简单,采用纯文本的编写方式,是各大开发商控制Android系统启动的一大利器。
init源码路径:/system/core/init/init.cpp,其main函数如下:
int main(int argc, char** argv) {
......
init_parse_config_file("/init.rc"); //解析init.rc文件
......
return 0;
}
init.rc文件路径/system/core/init/init.rc,其对ServiceManager描述如下
service servicemanager /system/bin/servicemanager
class core
user system
group system
critical
onrestart restart healthd
onrestart restart zygote
onrestart restart media
onrestart restart surfaceflinger
onrestart restart drm
文件中的描述的可执行程序servicemanager对应的源文件是service_manager.c,至此要真正的启动ServiceManager了,看这个类里的main函数
int main(int argc, char **argv) {
struct binder_state *bs;
//打开binder驱动,申请128k字节大小的内存空间
bs = binder_open(128*1024);
......
//将自己设置为Binder大管家,整个Android系统只允许一个ServiceManager存在
//所以如果后续还有人调用这个函数就会失败,直接返回
if (binder_become_context_manager(bs)) {
return -1;
}
......
//进入无限循环,处理client端发来的请求
binder_loop(bs, svcmgr_handler);
return 0;
}
该方法执行逻辑如下:
接下来看看具体逻辑:
####binder_state
这是个结构体,记录了ServiceManager中关于Binder的信息
struct binder_state
{
int fd; // dev/binder的文件描述符
void *mapped; //指向mmap的内存地址
size_t mapsize; //分配的内存大小,默认为128KB
};
####binder_open()
struct binder_state *binder_open(size_t mapsize)
{
struct binder_state *bs;
struct binder_version vers;
......
//通过系统调用进入内核,打开Binder设备驱动
bs->fd = open("/dev/binder", O_RDWR);
if (bs->fd < 0) {
goto fail_open; // 无法打开binder设备
}
//通过系统调用,ioctl获取binder版本信息
if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
(vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
//内核空间与用户空间的binder不是同一版本
goto fail_open;
}
//设置待分配内存大小
bs->mapsize = mapsize;
//通过系统调用mmap,进行内存映射
bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
if (bs->mapped == MAP_FAILED) {
goto fail_map; // binder设备内存无法映射
}
return bs;
fail_map:
close(bs->fd);//关闭文件
fail_open:
free(bs);//释放内存
return NULL;
}
方法逻辑如下:
####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_ioctl函数,具体实现在上一篇博客已经详细描述过,这里简述
static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) {
binder_lock(__func__);
switch (cmd) {
case BINDER_SET_CONTEXT_MGR:
//成为binder的上下文管理者,也就是ServiceManager成为守护进程
ret = binder_ioctl_set_ctx_mgr(filp);
break;
}
case :...
}
binder_unlock(__func__);
}
这里ServiceManager通过BINDER_SET_CONTEXT_MGR命令,调用binder_ioctl_set_ctx_mgr函数将ServiceManager注册成Binder大管家,也就是上下文管理者;因为ServiceManager启动的很早,能保证它是系统中第一个向Binder驱动注册成管家的程序
####binder_loop
前面方法都执行完,基本上所有工作已经准备就绪了,SM开始等待Client进程的请求,这是SM的重点
void binder_loop(struct binder_state *bs, binder_handler func) {
int res;
//这是执行BINDER_WRITE_READ命令所需数据格式
struct binder_write_read bwr;
//一次读取容量
uint32_t readbuf[32];
bwr.write_size = 0;//write_buffer的总字节数
bwr.write_consumed = 0;//write_buffer已消费的字节数
bwr.write_buffer = 0;//写缓冲数据的指针
//在SM进入循环前,先通知Binder驱动这一变化
readbuf[0] = BC_ENTER_LOOPER;
//将BC_ENTER_LOOPER命令发送给binder驱动,让Service Manager进入循环
binder_write(bs, readbuf, sizeof(uint32_t));
for (;;) {
bwr.read_size = sizeof(readbuf);//read_buffer的总字节数
bwr.read_consumed = 0;//read_buffer已消费的字节数
bwr.read_buffer = (uintptr_t) readbuf;//读缓存数据的指针
//进入循环,不断地从binder读消息
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
if (res < 0) {
break;
}
// 处理一条binder信息
res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
if (res == 0) {
break;
}
if (res < 0) {
break;
}
}
}
#####第一步:
这里先通过binder_write,然后通过ioctl()等函数将BC_ENTER_LOOPER命令发送给binder驱动,此时bwr只有write_buffer有数据(命令),进入binder_thread_write()方法
int binder_write(struct binder_state *bs, void *data, size_t len) {
struct binder_write_read bwr;
int res;
bwr.write_size = len;
bwr.write_consumed = 0;
bwr.write_buffer = (uintptr_t) data; //此处data为BC_ENTER_LOOPER
bwr.read_size = 0;
bwr.read_consumed = 0;
bwr.read_buffer = 0;
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
return res;
}
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;
ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
...
binder_lock(__func__);
thread = binder_get_thread(proc); //获取binder_thread
switch (cmd) {
case BINDER_WRITE_READ: //进行binder的读写操作
ret = binder_ioctl_write_read(filp, cmd, arg, thread);
if (ret)
goto err;
break;
case ...
}
ret = 0;
err:
if (thread)
thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
binder_unlock(__func__);
...
return ret;
}
static int binder_ioctl_write_read(struct file *filp,
unsigned int cmd, unsigned long arg,
struct binder_thread *thread)
{
int ret = 0;
struct binder_proc *proc = filp->private_data;
void __user *ubuf = (void __user *)arg;
struct binder_write_read bwr;
if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { //把用户空间数据ubuf拷贝到bwr
ret = -EFAULT;
goto out;
}
if (bwr.write_size > 0) { //此时写缓存有数据
ret = binder_thread_write(proc, thread,
bwr.write_buffer, bwr.write_size, &bwr.write_consumed);
...
}
if (bwr.read_size > 0) { //此时读缓存无数据
...
}
if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { //将内核数据bwr拷贝到用户空间ubuf
ret = -EFAULT;
goto out;
}
out:
return ret;
}
static int binder_thread_write(struct binder_proc *proc, struct binder_thread *thread, binder_uintptr_t binder_buffer, size_t size, binder_size_t *consumed) {
uint32_t cmd;
void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
void __user *ptr = buffer + *consumed;
void __user *end = buffer + size;
while (ptr < end && thread->return_error == BR_OK) {
get_user(cmd, (uint32_t __user *)ptr); //获取命令
switch (cmd) {
case BC_ENTER_LOOPER:
//设置该线程的looper状态
thread->looper |= BINDER_LOOPER_STATE_ENTERED;
break;
case ...;
}
}
}
#####第二步:
接下来进入for循环,执行ioctl(),此时bwr只有read_buffer有数据,那么最终会进入binder_thread_read()函数。ioctl()后面调用过程跟上面一样,只不过在binder_ioctl_write_read函数中会进入到binder_thread_read()
static int binder_thread_read(struct binder_proc *proc,
struct binder_thread *thread,
binder_uintptr_t binder_buffer, size_t size,
binder_size_t *consumed, int non_block){
wait_for_proc_work = thread->transaction_stack == NULL &&
list_empty(&thread->todo);
//根据wait_for_proc_work来决定wait在当前线程还是进程的等待队列
if (wait_for_proc_work) {
ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
...
} else {
ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
...
}
while (1) {
//当&thread->todo和&proc->todo都为空时,goto到retry标志处,否则往下执行:
struct binder_transaction_data tr;
struct binder_transaction *t = NULL;
switch (w->type) {
case BINDER_WORK_TRANSACTION: ...
case BINDER_WORK_TRANSACTION_COMPLETE: ...
case BINDER_WORK_NODE: ...
case BINDER_WORK_DEAD_BINDER: ...
case BINDER_WORK_DEAD_BINDER_AND_CLEAR: ...
case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: ...
}
...
}
done:
*consumed = ptr - buffer;
//当满足请求线程加已准备线程数等于0,已启动线程数小于最大线程数(15),
//且looper状态为已注册或已进入时创建新的线程。
if (proc->requested_threads + proc->ready_threads == 0 &&
proc->requested_threads_started < proc->max_threads &&
(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
BINDER_LOOPER_STATE_ENTERED))) {
proc->requested_threads++;
// 生成BR_SPAWN_LOOPER命令,用于创建新的线程
put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer);
}
return 0;
}
这里做的主要是从Binder驱动读取消息
#####第三步:
调用binder_parse函数解析Binder信息
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);
switch(cmd) {
case BR_NOOP: //无操作,退出循环
break;
case BR_TRANSACTION_COMPLETE:
break;
case BR_INCREFS:
case BR_ACQUIRE:
case BR_RELEASE:
case BR_DECREFS:
ptr += sizeof(struct binder_ptr_cookie);
break;
case BR_TRANSACTION: {
struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
...
//dump追踪
binder_dump_txn(txn);
//这个函数是svcmgr_handler
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); //从txn解析出binder_io信息
res = func(bs, txn, &msg, &reply);//具体处理消息
/回应消息处理结果
binder_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;
...
binder_dump_txn(txn);
if (bio) {
bio_init_from_txn(bio, txn);
bio = 0;
}
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:
return -1;
}
}
return r;
}
这里我们主要看BR_TRANSACTION这个命令,这个命令主要是处理Binder驱动发送来的请求数据,主要通过func函数处理,然后将结果返回给Binder驱动
我们知道ServiceManager主要是为了完成Binder Server Name和Server Handle 之间的对应关系的查询而存在的,类似于域名和IP的关系查询,所以主要有两个功能:
func对应的是svcmgr_handler函数
int svcmgr_handler(struct binder_state *bs,
struct binder_transaction_data *txn,
struct binder_io *msg,
struct binder_io *reply)
{
struct svcinfo *si;
uint16_t *s;
size_t len;
uint32_t handle;
uint32_t strict_policy;
int allow_isolated;
...
//SM的handle为0
if(txn -> target != svcmgr_handle)
return -1;
//bio_get_xxx系列函数为取出各种类型的数据提供了便利
strict_policy = bio_get_uint32(msg);
s = bio_get_string16(msg, &len);
...
switch(txn->code) {
case SVC_MGR_GET_SERVICE:
case SVC_MGR_CHECK_SERVICE:
s = bio_get_string16(msg, &len); //服务名
//根据名称查找相应服务
handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
//保存查找结果以返回给Client
bio_put_ref(reply, handle);
return 0;
case SVC_MGR_ADD_SERVICE:
s = bio_get_string16(msg, &len); //服务名
handle = bio_get_ref(msg); //handle
allow_isolated = bio_get_uint32(msg) ? 1 : 0;
//注册指定服务
if (do_add_service(bs, s, len, handle, txn->sender_euid,
allow_isolated, txn->sender_pid))
return -1;
break;
case SVC_MGR_LIST_SERVICES: {
uint32_t n = bio_get_uint32(msg);
if (!svc_can_list(txn->sender_pid)) {
return -1;
}
si = svclist;
while ((n-- > 0) && si)
si = si->next;
if (si) {
bio_put_string16(reply, si->name);
return 0;
}
return -1;
}
default:
return -1;
}
bio_put_uint32(reply, 0);
return 0;
}
这个函数主要根据命令做相应处理
其中涉及到一个结构体svcinfo:每一个服务用svcinfo结构体来表示,其handle值是在注册服务的过程中,由服务所在进程那一端所确定的。
struct svcinfo
{
struct svcinfo *next;
uint32_t handle; //服务的handle值
struct binder_death death;
int allow_isolated;
size_t len; //名字长度
uint16_t name[0]; //服务名
};
接下来看看函数里做的几个核心工作
####do_find_service
uint32_t do_find_service(struct binder_state *bs, const uint16_t *s, size_t len, uid_t uid, pid_t spid)
{
//查询相应的服务
struct svcinfo *si = find_svc(s, len);
if (!si || !si->handle) {
return 0;
}
if (!si->allow_isolated) {
uid_t appid = uid % AID_USER;
//检查该服务是否允许孤立于进程而单独存在
if (appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END) {
return 0;
}
}
//服务是否满足查询条件
if (!svc_can_find(s, len, spid)) {
return 0;
}
return si->handle;
}
struct svcinfo *find_svc(const uint16_t *s16, size_t 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 NULL;
}
这个函数主要是响应Client进程查询Server进程的请求,ServiceManager中维护了一个全局svclist,用户保存所有的Server注册信息,查找过程也就是遍历这个svclist,根据服务名遍历查找是否已经注册。当服务已存在svclist,则返回相应的服务名,否则返回NULL;最后将对应的Server的handle返回
最后调用bio_put_ref函数,将handle封装到reply.
void bio_put_ref(struct binder_io *bio, uint32_t handle) {
struct flat_binder_object *obj;
if (handle)
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; //返回的是HANDLE类型
obj->handle = handle;
obj->cookie = 0;
}
static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
{
struct flat_binder_object *obj;
obj = bio_alloc(bio, sizeof(*obj));
if (obj && bio->offs_avail) {
bio->offs_avail--;
*bio->offs++ = ((char*) obj) - ((char*) bio->data0);
return obj;
}
bio->flags |= BIO_F_OVERFLOW;
return NULL;
}
static void *bio_alloc(struct binder_io *bio, size_t size)
{
size = (size + 3) & (~3);
if (size > bio->data_avail) {
bio->flags |= BIO_F_OVERFLOW;
return NULL;
} else {
void *ptr = bio->data;
bio->data += size;
bio->data_avail -= size;
return ptr;
}
}
####do_add_service
uint32_t bio_get_ref(struct binder_io *bio) {
struct flat_binder_object *obj;
obj = _bio_get_obj(bio);
if (!obj)
return 0;
if (obj->type == BINDER_TYPE_HANDLE)
return obj->handle;
return 0;
}
int do_add_service(struct binder_state *bs,
const uint16_t *s, size_t len,
uint32_t handle, uid_t uid, int allow_isolated,
pid_t spid)
{
struct svcinfo *si;
if (!handle || (len == 0) || (len > 127))
return -1;
//权限检查
if (!svc_can_register(s, len, spid)) {
return -1;
}
//服务检索,判断当前列表是否存在这个服务
si = find_svc(s, len);
if (si) {
if (si->handle) {
svcinfo_death(bs, si); //服务已注册时,释放相应的服务
}
si->handle = handle;
} else {
si = malloc(sizeof(*si) + (len + 1) * sizeof(uint16_t));
if (!si) { //内存不足,无法分配足够内存
return -1;
}
si->handle = handle;
si->len = len;
memcpy(si->name, s, (len + 1) * sizeof(uint16_t)); //内存拷贝服务信息
si->name[len] = '\0';
si->death.func = (void*) svcinfo_death;
si->death.ptr = si;
si->allow_isolated = allow_isolated;
si->next = svclist; // svclist保存所有已注册的服务
svclist = si;
}
//以BC_ACQUIRE命令,handle为目标的信息,通过ioctl发送给binder驱动
binder_acquire(bs, handle);
//以BC_REQUEST_DEATH_NOTIFICATION命令的信息,通过ioctl发送给binder驱动,主要用于清理内存等收尾工作。
binder_link_to_death(bs, handle, &si->death);
return 0;
}
static int svc_can_register(const uint16_t *name, size_t name_len, pid_t spid) {
const char *perm = "add";
//检查linux权限是否满足
return check_mac_perms_from_lookup(spid, perm, str8(name, name_len)) ? 1 : 0;
}
void svcinfo_death(struct binder_state *bs, void *ptr) {
struct svcinfo *si = (struct svcinfo* ) ptr;
if (si->handle) {
binder_release(bs, si->handle);
si->handle = 0;
}
}
注册Binder Server的过程不复杂,主要分三步:
svcmgr_handler函数处理完Binder发送过来的Client请求(这时候ServiceManager相当于Server,发出注册请求的其它Server相当于Client),binder_parse函数中会通过binder_send_reply将执行结果返回给底层驱动,然后传递给Client。
处理完一条消息,binder_loop函数中就会继续向Binder驱动发送BINDER_WRITE_READ命令查询有没有新的消息,以此循环
在binder_parse函数中还有一个命令BR_REPLY,这个命令表示Binder驱动向Client端发送回复数据,各命令可参见第二篇博客;往上面看其并没有具体实质性的动作,因为ServiceManager是为别人服务的,并不会向其它Server主动发请求。
ServiceManager启动流程:
#####ServiceManager最核心的两个功能为查询和注册服务:
注册服务:记录服务名和handle信息,保存到svclist列表;
查询服务:根据服务名查询相应的的handle信息。