Android SystemProperties系统属性分析
Android 的系统属性包括两部分:文件保存的持久属性和每次开机导入的cache属性。前者主要保存在下面几个文件中:
bionic/libc/include/sys/_system_properties.h
1 #define PROP_SERVICE_NAME "property_service"
2 #define PROP_PATH_RAMDISK_DEFAULT "/default.prop"
3 #define PROP_PATH_SYSTEM_BUILD "/system/build.prop"
4 #define PROP_PATH_SYSTEM_DEFAULT "/system/default.prop"
5 #define PROP_PATH_LOCAL_OVERRIDE "/data/local.prop"
后者则通过frameworks/base/core/java/android/os/SystemProperties.java的接口定义,
1 private static native String native_get(String key);
2 private static native String native_get(String key, String def);
3 private static native void native_set(String key, String def);
4 public static void set(String key, String val) {
5 if (key.length() > PROP_NAME_MAX) {
6 throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
7 }
8 if (val != null && val.length() > PROP_VALUE_MAX) {
9 throw new IllegalArgumentException("val.length > " +
10 PROP_VALUE_MAX);
11 }
12 native_set(key, val);
13 }
该接口类在初始化运行环境中注册对应的cpp接口android_os_SystemProperties.cpp,实际操作通过JNI调用的是cpp文件对应的接口:
frameworks/base/core/jni/AndroidRuntime.cpp
1 namespace android {
2 extern int register_android_os_SystemProperties(JNIEnv *env);
3 }
frameworks/base/core/jni/android_os_SystemProperties.cpp
1 static void SystemProperties_set(JNIEnv *env, jobject clazz, jstring keyJ, jstring valJ)
2 {
3 int err;
4 const char* key;
5 const char* val;
6 key = env->GetStringUTFChars(keyJ, NULL);
7 if (valJ == NULL) {
8 val = ""; /* NULL pointer not allowed here */
9 } else {
10 val = env->GetStringUTFChars(valJ, NULL);
11 }
12 err = property_set(key, val);
13 env->ReleaseStringUTFChars(keyJ, key);
14 if (valJ != NULL) {
15 env->ReleaseStringUTFChars(valJ, val);
16 }
17 }
设置key的value时,需要作鉴权,根据设置程序所在进程的fd获知uid值,比如system server进程可以设置net打头的key,不可以设置gsm打头的key,相关的定义如下:
system/core/include/private/android_filesystem_config.h
1 #define AID_ROOT 0 /* traditional unix root user */
2 #define AID_SYSTEM 1000 /* system server */
3 #define AID_RADIO 1001 /* telephony subsystem, RIL */
4 #define AID_DHCP 1014 /* dhcp client */
5 #define AID_SHELL 2000 /* adb and debug shell user */
6 #define AID_CACHE 2001 /* cache access */
7 #define AID_APP 10000 /* first app user */
system/core/init/property_service.c
1 #define PERSISTENT_PROPERTY_DIR "/data/property"
2 struct {
3 const char *prefix;
4 unsigned int uid;
5 } property_perms[] = {
6 { "net.rmnet0.", AID_RADIO },
7 { "net.gprs.", AID_RADIO },
8 { "ril.", AID_RADIO },
9 { "gsm.", AID_RADIO },
10 { "net.dns", AID_RADIO },
11 { "net.usb0", AID_RADIO },
12 { "net.", AID_SYSTEM },
13 { "dev.", AID_SYSTEM },
14 { "runtime.", AID_SYSTEM },
15 { "hw.", AID_SYSTEM },
16 { "sys.", AID_SYSTEM },
17 { "service.", AID_SYSTEM },
18 { "wlan.", AID_SYSTEM },
19 { "dhcp.", AID_SYSTEM },
20 { "dhcp.", AID_DHCP },
21 { "debug.", AID_SHELL },
22 { "log.", AID_SHELL },
23 { "ser
补充:移动开发 , Android ,