William Hua的Blog

2010-03-05

[转]Android Property System

Filed under: Android — 标签:, , , — William Hua @ 15:38

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.

[First written by Steve Guo, please keep the mark if forwarding.]
抱歉没找到原帖地址,觉得会对大家有用就转一下。

6 条评论 »

  1. 貌似好几个月不见你博客有动静了。

    评论 by febird — 2010-03-07 @ 03:41

  2. 刘苏到访,留个脚印…

    评论 by 刘苏 — 2010-03-27 @ 15:33

  3. 抽空更新下我博客链接吧。。。

    评论 by 陶陶 — 2010-04-10 @ 22:44

  4. 精彩内容,怎容错过!
    o(∩_∩)o

    评论 by 济南seo — 2010-05-18 @ 17:20

  5. @febird
    确实哦,很久没更新了
    最近搞的东西都不能写啊,Confidential :D

    评论 by William Hua — 2010-06-22 @ 19:00

  6. [...]    现在问题是Android应用程序怎样启动让init进程知道我们想运行那个进程呢?答案是设置系统属性“ctl.start”,把“ctl.start”设置为你要运行的Service,假设为“xxx”,Android系统将会帮你运行“ctl.start”系统属性中指定的Service。那么运行结果init进程将会将会写入“init.svc.”+Service名称,也就是“init.svc.xxx”,应用程序可以参考查阅这个值来确定Service执行的情况。想更深入了解Android property系统可以参考博文《[转]Android Property System》。 [...]

    Pingback by 云中漫步 » Android应用程序获得root权限 — 2011-01-18 @ 23:36

这篇文章上的评论的 RSS feed TrackBack URL

留下评论

Powered by WordPress