1. property应用例子
Java:
import android.os.SystemProperties;
SystemProperties.get("persist.sys.usb.config", "mtp,adb");
SystemProperties.getBoolean("ro.adb.secure", false);
SystemProperties.set("sys.usb.config", config);
SystemProperties.set需要有system权限,可以这么做
a. 在Android.mk中,加上LOCAL_CERTIFICATE :=platform
b. 在AndroidManifest.xml中,加上android:sharedUserId="android.uid.system"
Native C:
#include <cutils/properties.h>
LOCAL_SHARED_LIBRARIES := \
libcutils
int property_set(const char *key, const char *value)
int property_get(const char *key, char *value, const char *default_value)
property_set("service.adb.root", "1");
property_get("ro.debuggable", value, "")
2. 设置属性的调用过程
SystemProperties.set in Java
set frameworks/base/core/java/android/os/SystemProperties.java
native_set(key, val)
SystemProperties_set frameworks/base/core/jni/android_os_SystemProperties.cpp
property_set /system/core/libcutils/properties.c
setprop in .rc/.sh file
setprop_main system/core/toolbox/setprop.c
property_set /system/core/libcutils/properties.c
property_set /system/core/libcutils/properties.c
HAVE_LIBC_SYSTEM_PROPERTIES #define HAVE_LIBC_SYSTEM_PROPERTIES 1 in build/core/combo/include/arch/linux-arm/AndroidConfig.h
__system_property_set bionic/libc/bionic/system_properties.c
send_prop_msg(PROP_MSG_SETPROP)
handle_property_set_onfd(case PROP_MSG_SETPROP) system/core/init/property_service.c
检查权限
if "ctl." 开头 handle_control_message() system/core/init/init.c
ctrl.start msg_start() 启动service
ctrl.stop msg_stop() 停止service
ctrl.restart msg_restart() 重启service
else property_set()
如果能找到且以ro开头,就返回-1;能找到且不以ro开头调用 __system_property_update()
如果找不到则调用__system_property_add()
如果是"net."开头,调用property_set("net.change", name)
如果以"persist."开头,调用write_persistent_property();
property_changed(name, value);
除了"persist."开头的property,关机后值会消失;以"persist."开头的会保存在/data/property目录下一文件里,可以看write_persistent_property()
3. 属性的初始值
从下面一些文件可以得到属性的初始值
void start_property_service(void)
{
...
load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
load_override_properties();
/* Read persistent properties after all default values have been loaded. */
load_persistent_properties();
...
#define PROP_PATH_RAMDISK_DEFAULT "/default.prop"
#define PROP_PATH_SYSTEM_BUILD "/system/build.prop"
#define PROP_PATH_SYSTEM_DEFAULT "/system/default.prop"
#define PROP_PATH_LOCAL_OVERRIDE "/data/local.prop"
4. 属性的权限
如果是serice, 调用check_control_perms()检查权限,可以看看control_perms[]
如果是其他属性,调用check_perms()检查权限,可以看看property_perms[]
5. property_changed() system/core/init/init.c
void property_changed(const char *name, const char *value)
{
if (property_triggers_enabled)
queue_property_triggers(name, value);
}
queue_property_triggers()
action_add_queue_tail(act)
设置完属性后,会调用on property相应的action, 可以看看init.rc