设置属性
// frameworks/base/core/java/android/os/SystemProperties.java
native_set(String key, String def);
// frameworks/base/core/jni/android_os_SystemProperties.cpp
void SystemProperties_set(JNIEnv *env, jobject clazz, jstring keyJ,jstring valJ)
android::base::SetProperty(key, val)
// system/core/base/properties.cpp
bool SetProperty(const std::string& key, const std::string& value) {
return (__system_property_set(key.c_str(), value.c_str()) == 0);
}
// bionic/libc/bionic/system_property_set.cpp
SocketWriter writer(&connection);
if (!writer.WriteUint32(PROP_MSG_SETPROP2).WriteString(key).WriteString(value).Send()) {
errno = connection.GetLastError();
async_safe_format_log(ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": write failed; errno=%d (%s)",
key, value, errno, strerror(errno));
return -1;
}
// system/core/init/property_service.cp
case PROP_MSG_SETPROP2: {
...
uint32_t result = HandlePropertySet(name, value, socket.source_context(), cr, &error);
...
break;
}
PropertySet(name, value, error);
获取属性
// bionic/libc/bionic/system_property_api.cpp
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
int __system_property_get(const char* name, char* value) {
return system_properties.Get(name, value);
}
// bionic/libc/system_properties/system_properties.cpp
int SystemProperties::Get(const char* name, char* value) {
const prop_info* pi = Find(name);
if (pi != nullptr) {
return Read(pi, nullptr, value);
} else {
value[0] = 0;
return 0;
}
}