Android 属性系统 Property service 设定分析
Android 属性系统 Property service 设定分析
在Window中有个注册表的东东,可以存储一些类似key:value的键值对,而在android平台上也有类似的机制叫做属易做图(Property service)进行初始化,设置及修改和查询的功能,adb shell命令使用 setprop 及 getprop 可以看到。
问题:
Su易做图ceFlinger启动后线程调用readyToRun函数时设定有一个属性值:
status_t Su易做图ceFlinger::readyToRun()
{
LOGI( "Su易做图ceFlinger's main thread ready to run. "
"Initializing graphics H/W...");
...
/*
* We're now ready to accept clients...
*/
// start boot animation
property_set("ctl.start", "bootanim");
return NO_ERROR;
}
是如何启动bootanim这个服务的呢?bootanim就是开机动画的一个单独进程,在init.rc中以 service bootanim /system/bin/bootanimation 作为一个服务启动,为了使用图形系统功能必须等待Su易做图ceFlinger启动后才能执行,这里就利用属易做图作为进程同步之用法。
下面我们就这个流程进行一个简单梳理:
一、属性客户端流程
property_set("ctl.start", "bootanim"); 这就是启动触发点!!!
-->
property_set @ /system/core/libcutils/properties.c
int property_set(const char *key, const char *value)
{
msg.cmd = PROP_MSG_SETPROP;
strcpy((char*) msg.name, key);
strcpy((char*) msg.value, value);
return send_prop_msg(&msg);
}
-->
这里就是通过一个普通的TCP(SOCK_STREAM)套接字进行通讯
static int send_prop_msg(prop_msg *msg)
{
s = socket_local_client(PROP_SERVICE_NAME,
ANDROID_SOCKET_NAMESPACE_RESERVED,
SOCK_STREAM);
if(s < 0) return -1;
while((r = send(s, msg, sizeof(prop_msg), 0)) < 0) {
if((errno == EINTR) || (errno == EAGAIN)) continue;
break;
}
close(s);
return r;
}
二、服务端是如何监听并实现注程
main @ /system/core/init/init.c
int main(int argc, char **argv)
{
int property_set_fd = -1;
/* read any property files on system or data and
* fire up the property service. This must happen
* after the ro.foo properties are set above so
* that /data/local.prop cannot interfere with them.
*/
property_set_fd = start_property_service();
// 将 property_set_fd 设定到poll监听队列
ufds[0].fd = device_fd;
ufds[0].events = POLLIN;
ufds[1].fd = property_set_fd;
ufds[1].events = POLLIN;
for(;;) {
...
nr = poll(ufds, fd_count, timeout);
// 监听到有属易做图请求需要处理
if (ufds[1].revents == POLLIN)
handle_property_set_fd(property_set_fd);
...
}
return 0;
}
先看一下 start_property_service 如何实现的?
int start_property_service(void)
{
int fd;
load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
/* Read persistent properties after all default values have been loaded. */
load_persistent_properties();
fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
if(fd < 0) return -1;
fcntl(fd, F_SETFD, FD_CLOEXEC);
fcntl(fd, F_SETFL, O_NONBLOCK);
listen(fd, 8);
return fd;
}
ok,明白了吧,创建了一个SOCK_STREAM套接字并进入监听listen状态
handle_property_set_fd @ /system/core/init/property_service.c
void handle_property_set_fd(int fd)
{
//1、接收socket请求连接www.zzzyk.com
if ((s = accept(fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
return;
}
//2、收取属性请求数
r = recv(s, &msg, sizeof(msg), 0);
close(s);
//3、处理属情请求数据
switch(msg.cmd) {
case PROP_MSG_SETPROP:
...
if(memcmp(msg.name,"ctl.",4) == 0) {
if (check_control_perms(msg.value, cr.uid, cr.gid)) {
handle_control_message((char*) msg.name + 4, (char*) msg.value);
}
}else {
if (check_perms(msg.name, cr.uid, cr.gid)) {
property_set((char*) msg.name, (char*) msg.value);
}
}
}
}
由于请求消息是:ctl.start 则执行 handle_control_message 这个函数:
handle_control_message @ /system/core/init/init.c
void handle_control_message(const char *msg, const char *arg)
{
if (!strcmp(msg,"start")) {
msg_start(arg);
} else if (!strcmp(msg,"stop")) {
msg_stop(arg);
} else {
ERROR("unknown control msg '%s'\n", msg);
}
}
static void msg_start(const char *name)
{
svc = service_find_by_name(name);
...
service_start(svc, args);
}
static void msg_stop(const char *name)
{
struct service *svc = service_find_by_name(name);
service_stop(svc);
}
看下上面的代码大家应该明白了吧,就是请求ServiceManager服务进行启动或停止某个服务,这里就是那是 bootanim 服务了。
还有一点为何 bootanim 在init.rc 脚本中没有开机就启动呢?请见 init.rc 脚本:
service bootanim /system/bin/bootanimation
user graphics
group graphics
disabled
oneshot
看到 disabled 没有,这个关键字是在添加到 service_list 双键表时使用:
#define SVC_DISABLED 0x01 /* do not autostart with class */
#define SVC_ONESHOT 0x02&nb
补充:移动开发 , Android ,