Android的属性系统

http://blog.csdn.net/jerryutscn/article/details/5519423

Android的属性系统

每条属性包含了名字和其对应的值,两者都用字符串来描述。Android系统中大量的使用了属性系统用于记录系统的设置(注:和windows系统里的注册表类似),以及进程间的信息交互。属性系统对于整个系统来说是全局的,也就是说每一个进程都可以获取和设置每条属性。

当系统初始化的时候,Android系统会分配一块共享内存用于存储属性信息。这些操作是在"init"这个守护进程里完成的,其对应的源代码目录在:device/system/init。"init"守护进程同时也启动了属性服务。属性服务运行在init进程中(注:是线程?)。客户端需要设置属性时,需要连接到属性服务上,同时发送消息给属性服务。这样属性服务就可以更新保存在共享内存中的属性信息。当客户端需要读取属性信息时,可以直接访问共享内存,这样做可以提高性能。

客户端程序可以通过调用libcutils库里的API函数实现对属性的设置和读取的操作。

libcutils库对应的源代码存放在: device/libs/cutils。

这些API函数包括:

int property_get(const char *key, char *value, const char *default_value);

int property_set(const char *key, const char *value);

    libcutils库是通过调用libc库中的__system_property_xxx这一系列函数实现从共享内存中获取属性的。对应的libc的源代码保存在: device/system/bionic。

属性服务也会调用libc库中的函数__system_property_init来实现对共享内存的初始化。当启动属性服务时会通过下面文件来加载默认的属性信息。

/default.prop

/system/build.prop

/system/default.prop

/data/local.prop

属性信息按照上面的顺序被加载。后加载的属性会覆盖前面的属性值(注:当属性名称相同的时候)。当上面加载完成后,最后加载的是驻留属性,保存在/data/property文件中。

特别的属性

如果属性是有”ro.”字符串开头,那么这条属性被看成一个只读属性,一旦设置就不能被修改了。(注:初始化设置的时候是可写的)

如果属性是有“persist.”字符串开头,那么就认为是驻留属性,当修改的时候同时也会被保存在/data/property文件中。

如果属性是有“net.”字符串开头,当设置这种属性的时候,“net.change”这条属性也会被自动设置,其内容设为最后更新过的属性名。

属性“ctrl.start” 和 “ctrl.stop” 用于启动和停止服务。这些服务必须在文件/init.rc中被定义。在系统启动的时候,init守护进程会解析init.rc文件,然后启动属性服务。一旦收到了“ctrl.start”这个属性的设置请求,属性服务会根据该属性的值作为服务名,在列表中找到服务对象并启动。服务启动的结果会反映在“init.svc.<service name>”这个属性之中。客户端程序可以轮询该属性值来判断服务的启动结果。

Android 工具箱

Android 工具箱提供了2个应用程序:setprop 和 getprop用于读取和设置属性项目。使用方法是:

getprop <property name>

setprop <property name> <property value>

Java

Java程序可以通过使用函数System.getProperty() 和 System.setProperty()获取和设置属性条目。

动作

默认情况下设置属性只会触发init进程对共享内存进行操作,并不会执行任何脚本和程序。但是你可以修改文件init.rc,这样在属性值发生变化的时候来触发你定义的动作。例如在默认的init.rc文件中你可以找到下面这部分:

# adbd on at boot in emulator

on property:ro.kernel.qemu=1

    start adbd

on property:persist.service.adb.enable=1

    start adbd

on property:persist.service.adb.enable=0

    stop adbd

所以当属性条目persist.service.adb.enable被设置成1的时候,init守护进程知道他需要启动adbd服务。

原文如下:

Android Property System

Every property has a name and value. Both name and value are text strings. Property is heavily used in Android to record system setting or exchange information between processes. The property is globally visible in the whole system. Every process can get/set a property.

On system initialization, Android will allocates a block of shared memory for storing the properties. This is done in “init” daemon whose source code is at: device/system/init. The “init” daemon will start a Property Service. The Property Service is running in the process of “init” daemon. Every client that wants to SET property needs to connect to the Property Service and send message to Property Service. Property Service will update/create the property in shared memory. Any client that wants to GET property can read the property from the shared memory directly. This promotes the read performance.

The client application can invoke the API function exposed from libcutils to GET/SET a property.

The source code of libcutils locates at: device/libs/cutils.

The API function is:

int property_get(const char *key, char *value, const char *default_value);

int property_set(const char *key, const char *value);

The libcutils is in turn calling the __system_property_xxx function in libc to get a property from the shared memory. The source code of libc is at: device/system/bionic.

The Property Service is also in turn calling the __system_property_init function in libc to initiate the shared memory for properties. When starting the Property Service will load the default properties from below files:

/default.prop

/system/build.prop

/system/default.prop

/data/local.prop

The properties are loaded in the above order. Later loaded properties will override the previous values. After those properties are loaded, the last loaded is the persistent properties which is persisted in /data/property.

Special Properties

If a property’s name begins with “ro.”, then this property is treated as a read-only property. Once set, the value of the property can’t be changed.

If a property’s name begins with “persist.”, then when setting this property, the value will be written to /data/property, too.

If a property’s name begins with “net.”, when when setting this property, the “net.change” property will be set automatically to contain the name of the last updated property. (It’s tricky. The netresolve module uses this property to track if there is any change on the net.* properties.)

The property “ctrl.start” and “ctrl.stop” is used to start and stop a service. Every service must be defined in /init.rc. On system startup, the init daemon will parse the init.rc and start the Property Service. Once received a request to set the property of “ctrl.start”, the Property Service will use the property value as the service name to find the service and then start the service. The service starting result is then put to the property “init.svc.<service name>”. The client application can poll the value of that property to determine the result.

Android toolbox

The Android toolbox provides two applets: setprop and getprop to get and set properties. The usage is:

getprop <property name>

setprop <property name> <property value>

Java

The java application can use the System.getProperty() and System.setProperty() function to Get and Set the property.

Action

By default the set property will only cause "init" daemon to write to shared memory, it won‘t execute any script or binary. But you can add your actions to correspond to property change in init.rc. For example, in the default init.rc, you can find.

# adbd on at boot in emulator

on property:ro.kernel.qemu=1

    start adbd

on property:persist.service.adb.enable=1

    start adbd

on property:persist.service.adb.enable=0

    stop adbd

So if you set persist.service.adb.enable to 1, the "init" daemon knows it has actions to do, then it will start adbd service.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。