首页 > 代码库 > Android 属性系统 Property service 设定分析 (转载)
Android 属性系统 Property service 设定分析 (转载)
转自:http://blog.csdn.net/andyhuabing/article/details/7381879
Android 属性系统 Property service 设定分析
在Window中有个注册表的东东,可以存储一些类似key:value的键值对,而在android平台上也有类似的机制叫做属性服务(Property service)进行初始化,设置及修改和查询的功能,adb shell命令使用 setprop 及 getprop 可以看到。
问题:
SurfaceFlinger启动后线程调用readyToRun函数时设定有一个属性值:
status_t SurfaceFlinger::readyToRun()
{
LOGI( "SurfaceFlinger‘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 作为一个服务启动,为了使用图形系统功能必须等待SurfaceFlinger启动后才能执行,这里就利用属性服务作为进程同步之用法。
下面我们就这个流程进行一个简单梳理:
一、属性客户端流程
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状态
//1、接收socket请求连接 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 /* do not restart on exit */
#define SVC_RUNNING 0x04 /* currently active */
#define SVC_RESTARTING 0x08 /* waiting to restart */
#define SVC_CONSOLE 0x10 /* requires console */
#define SVC_CRITICAL 0x20 /* will reboot into recovery if keeps crashing */
static void parse_line_service(struct parse_state *state, int nargs, char **args)
{
kw = lookup_keyword(args[0]);
switch (kw) {
case K_disabled:
svc->flags |= SVC_DISABLED;
break;
...
}
而在执行 service_start 及 service_stop 时都会判定这个 flags 值:
void service_stop(struct service *svc)
{
/* if the service has not yet started, prevent
* it from auto-starting with its class
*/
svc->flags |= SVC_DISABLED;
...
}
初始启动Service流程:
int do_class_start(int nargs, char **args)
{
/* Starting a class does not start services
* which are explicitly disabled. They must
* be started individually.
*/
service_for_each_class(args[1], service_start_if_not_disabled);
return 0;
}
static void service_start_if_not_disabled(struct service *svc)
{
if (!(svc->flags & SVC_DISABLED)) {
service_start(svc, NULL);
}
}
这里会决定这个 Service 是否初始开机启机,通过这个 SVC_DISABLED flag即可判定。
还有一个补充说明一下:
ctr.start和ctr.stop系统属性?
每一项服务必须在/init.rc中定义.Android系统启动时,init守护进程将解析init.rc和启动属性服务,属性“ ctl.start ”和“ ctl.stop ”是用来启动和停止服务的。一旦收到设置“ ctrl.start ”属性的请求,属性服务将使用该属性值作为服务名找到该服务,启动该服务。这项服务的启动结果将会放入“ init.svc.<服务名>“属性中 。客户端应用程序可以轮询那个属性值,以确定结果。
基本常用代码写法:
static const char DAEMON_NAME[] = "dhcpcd";
static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
int dhcp_stop(const char *interface)
{
char result_prop_name[PROPERTY_KEY_MAX];
const char *ctrl_prop = "ctl.stop";
const char *desired_status = "stopped";
...
/* Stop the daemon and wait until it‘s reported to be stopped */
property_set(ctrl_prop, DAEMON_NAME);
if (wait_for_property(DAEMON_PROP_NAME, desired_status, 5) < 0) {
return -1;
}
}
这里对于 wait_for_property 的状态信息说明一下:
请注意执行 service_start 函数最后会执行如下语句:
void service_start(struct service *svc, const char *dynamic_args)
{
...
notify_service_state(svc->name, "running");
}
static void notify_service_state(const char *name, const char *state)
{
char pname[PROP_NAME_MAX];
int len = strlen(name);
if ((len + 10) > PROP_NAME_MAX)
return;
// 通过这里组成 init.svc.xxx 属性名称,并调用 property_set 设定其状态完成
snprintf(pname, sizeof(pname), "init.svc.%s", name);
property_set(pname, state);
}
Android 属性系统 Property service 设定分析 (转载)