# Android10 Framework—Init进程-9.服务端属性值初始化 站点:答源(https://www.dandroid.cn/) 原文:https://www.dandroid.cn/archives/22444 Canonical:https://www.dandroid.cn/archives/22444 作者:技术信源组 发布日期:2024-10-07 最后更新:2026-11-06 18:51 分类:技术方案 内容类型:post 说明:本页是原文的纯文本镜像,供大模型与检索器完整读取;不含导航、广告、付费框。引用请以上方「原文」链接为准。 ## 正文 本章主要讲“属性值初始化”,先给出完整数据流程图 属性值初始化核心内容: 属性值哪儿来 以怎样的方式写入属性文件 int SecondStageMain(int argc, char** argv) { process_kernel_dt(); process_kernel_cmdline(); export_kernel_boot_props(); property_load_boot_defaults(load_debug_prop); } 属性值初始化阶段由如下几个函数实现,其中property_load_boot_defaults最为重要,因此以它为切入点进行讲解。 void property_load_boot_defaults(bool load_debug_prop) { std::map properties; if (!load_properties_from_file("/system/etc/prop.default", nullptr, &properties)) { if (!load_properties_from_file("/prop.default", nullptr, &properties)) { load_properties_from_file("/default.prop", nullptr, &properties); } } load_properties_from_file("/system/build.prop", nullptr, &properties); load_properties_from_file("/vendor/default.prop", nullptr, &properties); load_properties_from_file("/vendor/build.prop", nullptr, &properties); if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_Q__) { load_properties_from_file("/odm/etc/build.prop", nullptr, &properties); } else { load_properties_from_file("/odm/default.prop", nullptr, &properties); load_properties_from_file("/odm/build.prop", nullptr, &properties); } load_properties_from_file("/product/build.prop", nullptr, &properties); load_properties_from_file("/product_services/build.prop", nullptr, &properties); load_properties_from_file("/factory/factory.prop", "ro.*", &properties); if (load_debug_prop) { LOG(INFO) pa_就是prop_area,它指向了属性文件的映射地址 void PropertyInfoArea::GetPropertyInfoIndexes(const char* name, uint32_t* context_index, uint32_t* type_index) const { uint32_t return_context_index = ~0u; uint32_t return_type_index = ~0u; const char* remaining_name = name; auto trie_node = root_node(); while (true) { const char* sep = strchr(remaining_name, '.'); if (trie_node.context_index() != ~0u) { return_context_index = trie_node.context_index(); } if (trie_node.type_index() != ~0u) { return_type_index = trie_node.type_index(); } CheckPrefixMatch(remaining_name, trie_node, &return_context_index, &return_type_index); if (sep == nullptr) { break; } const uint32_t substr_size = sep - remaining_name; TrieNode child_node; if (!trie_node.FindChildForString(remaining_name, substr_size, &child_node)) { break; } trie_node = child_node; remaining_name = sep + 1; } for (uint32_t i = 0; i < trie_node.num_exact_matches(); ++i) { if (!strcmp(c_string(trie_node.exact_match(i)->name_offset), remaining_name)) { if (context_index != nullptr) { if (trie_node.exact_match(i)->context_index != ~0u) { *context_index = trie_node.exact_match(i)->context_index; } else { *context_index = return_context_index; } } if (type_index != nullptr) { if (trie_node.exact_match(i)->type_index != ~0u) { *type_index = trie_node.exact_match(i)->type_index; } else { *type_index = return_type_index; } } return; } } CheckPrefixMatch(remaining_name, trie_node, &return_context_index, &return_type_index); if (context_index != nullptr) *context_index = return_context_index; if (type_index != nullptr) *type_index = return_type_index; return; } 通过root_offset可以找到字典树节点数组 使用点.分割属性名后变为数组,循环遍历查找到数组中倒数第二项 遍历倒数第二个节点的exact_match数组,找到与remaining_name同名的数据,type也要保持一致(remaining_name实际就是属性名分割后的最后一项) 然后就是调用pa->find查找属性文件内容 const prop_info* prop_area::find(const char* name) { return find_property(root_node(), name, strlen(name), nullptr, 0, false); } 因为这个查找过程比较复杂,本人也看得不是很透彻,所以就没法详细展开了;这一小部分看不懂起始并不影响我们的学习,大家也可先跳过。 属性添加 int SystemProperties::Add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen) { if (valuelen >= PROP_VALUE_MAX && !is_read_only(name)) { return -1; } if (namelen < 1) { return -1; } if (!initialized_) { return -1; } prop_area* serial_pa = contexts_->GetSerialPropArea(); if (serial_pa == nullptr) { return -1; } prop_area* pa = contexts_->GetPropAreaForName(name); if (!pa) { async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied adding property "%s"", name); return -1; } bool ret = pa->add(name, namelen, value, valuelen); if (!ret) { return -1; } 前面一部分和属性查找一样,调用GetPropAreaForName通过属性名获取到prop_area,这样才能读写属性文件 然后调用prop_area->add添加属性 bool prop_area::add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen) { return find_property(root_node(), name, namelen, value, valuelen, true); } 添加和查找都是调用find_property,它们的唯一区别是最后一个参数,add方法传入的true标识可以创建节点。 属性更新 int SystemProperties::Update(prop_info* pi, const char* value, unsigned int len) { if (len >= PROP_VALUE_MAX) { return -1; } if (!initialized_) { return -1; } prop_area* pa = contexts_->GetSerialPropArea(); if (!pa) { return -1; } uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed); serial |= 1; atomic_store_explicit(&pi->serial, serial, memory_order_relaxed); atomic_thread_fence(memory_order_release); strlcpy(pi->value, value, len + 1); atomic_store_explicit(&pi->serial, (len serial, INT32_MAX); atomic_store_explicit(pa->serial(), atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1, memory_order_release); __futex_wake(pa->serial(), INT32_MAX); return 0; } 当属性存在时,前面的如下代码已经查找到prop_info,然后传入到Update方法中,这样更新pi->value即可。 prop_info* pi = (prop_info*) __system_property_find(name.c_str()); 属性通知 当添加或更新属性时,需要通知系统,因为系统有许多Action时依赖属性值的变化而变化的,就是架构图中的“属性触发器” static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) { size_t valuelen = value.size(); prop_info* pi = (prop_info*) __system_property_find(name.c_str()); if (pi != nullptr) { if (StartsWith(name, "ro.")) { *error = "Read-only property was already set"; return PROP_ERROR_READ_ONLY_PROPERTY; } __system_property_update(pi, value.c_str(), valuelen); } else { int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen); if (rc < 0) { *error = "__system_property_add failed"; return PROP_ERROR_SET_FAILED; } } if (persistent_properties_loaded && StartsWith(name, "persist.")) { WritePersistentProperty(name, value); } property_changed(name, value); return PROP_SUCCESS; } void property_changed(const std::string& name, const std::string& value) { if (name == "sys.powerctl") { shutdown_command = value; do_shutdown = true; } if (property_triggers_enabled) ActionManager::GetInstance().QueuePropertyChange(name, value); if (waiting_for_prop) { if (wait_prop_name == name && wait_prop_value == value) { LOG(INFO) --- 由 答源 DAN 底座 1.4.1 生成 · 2026-11-23T20:32:55+00:00