Resetprop small refactor

This commit is contained in:
topjohnwu 2018-03-26 21:21:48 +08:00
parent cc7e74ca11
commit f0bac6b154
2 changed files with 14 additions and 60 deletions

View File

@ -2,52 +2,6 @@
* *
* Copyright 2016 nkk71 <nkk71x@gmail.com> * Copyright 2016 nkk71 <nkk71x@gmail.com>
* Copyright 2016 topjohnwu <topjohnwu@gmail.com> * Copyright 2016 topjohnwu <topjohnwu@gmail.com>
*
* Info:
*
* all changes are in
*
* bionic/libc/bionic/system_properties.cpp
*
* Functions that need to be patched/added in system_properties.cpp
*
* int __system_properties_init2()
* on android 7, first tear down the everything then let it initialize again:
* if (initialized) {
* //list_foreach(contexts, [](context_node* l) { l->reset_access(); });
* //return 0;
* free_and_unmap_contexts();
* initialized = false;
* }
*
*
* static prop_area* map_prop_area(const char* filename, bool is_legacy)
* we dont want this read only so change: 'O_RDONLY' to 'O_RDWR'
*
* static prop_area* map_fd_ro(const int fd)
* we dont want this read only so change: 'PROT_READ' to 'PROT_READ | PROT_WRITE'
*
*
* Copy the code of prop_info *prop_area::find_property, and modify to delete props
* const prop_info *prop_area::find_property_and_del(prop_bt *const trie, const char *name)
* {
* ...
* ... Do not alloc a new prop_bt here, remove all code involve alloc_if_needed
* ...
*
* if (prop_offset != 0) {
* atomic_store_explicit(&current->prop, 0, memory_order_release); // Add this line to nullify the prop entry
* return to_prop_info(&current->prop);
* } else {
*
* ....
* }
*
*
* by patching just those functions directly, all other functions should be ok
* as is.
*
*
*/ */
#include <stdio.h> #include <stdio.h>

View File

@ -153,7 +153,7 @@ class prop_area {
} }
const prop_info* find(const char* name); const prop_info* find(const char* name);
bool del(const char *name); // resetprop add bool del(const char *name); /* resetprop add */
bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen); bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie); bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
@ -184,7 +184,7 @@ class prop_area {
const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen, const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen,
const char* value, uint32_t valuelen, bool alloc_if_needed); const char* value, uint32_t valuelen, bool alloc_if_needed);
bool find_property_and_del(prop_bt *const trie, const char *name); // resetprop add bool find_property_and_del(prop_bt *const trie, const char *name); /* resetprop add */
bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie), bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie),
void* cookie); void* cookie);
@ -283,7 +283,8 @@ static prop_area* map_prop_area_rw(const char* filename, const char* context,
return pa; return pa;
} }
static prop_area* map_fd_ro(const int fd) { // resetprop: map the memory as rw
static prop_area* map_fd_rw(const int fd) {
struct stat fd_stat; struct stat fd_stat;
if (fstat(fd, &fd_stat) < 0) { if (fstat(fd, &fd_stat) < 0) {
return nullptr; return nullptr;
@ -298,7 +299,7 @@ static prop_area* map_fd_ro(const int fd) {
pa_size = fd_stat.st_size; pa_size = fd_stat.st_size;
pa_data_size = pa_size - sizeof(prop_area); pa_data_size = pa_size - sizeof(prop_area);
void* const map_result = mmap(nullptr, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // resetprop: add PROT_WRITE void* const map_result = mmap(nullptr, pa_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* resetprop: add PROT_WRITE */
if (map_result == MAP_FAILED) { if (map_result == MAP_FAILED) {
return nullptr; return nullptr;
} }
@ -313,10 +314,10 @@ static prop_area* map_fd_ro(const int fd) {
} }
static prop_area* map_prop_area(const char* filename) { static prop_area* map_prop_area(const char* filename) {
int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDWR); // resetprop: O_RDONLY -> O_RDWR int fd = open(filename, O_CLOEXEC | O_NOFOLLOW | O_RDWR); /* resetprop: O_RDONLY -> O_RDWR */
if (fd == -1) return nullptr; if (fd == -1) return nullptr;
prop_area* map_result = map_fd_ro(fd); prop_area* map_result = map_fd_rw(fd);
close(fd); close(fd);
return map_result; return map_result;
@ -530,7 +531,7 @@ bool prop_area::find_property_and_del(prop_bt* const trie, const char* name) {
uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed); uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
if (prop_offset != 0) { if (prop_offset != 0) {
atomic_store_explicit(&current->prop, 0, memory_order_release); // resetprop: nullify the offset to delete the prop atomic_store_explicit(&current->prop, 0, memory_order_release);
return true; return true;
} else { } else {
return false; return false;
@ -1116,17 +1117,16 @@ static bool initialize_properties() {
if (!initialize_properties_from_file("/system/etc/selinux/plat_property_contexts")) { if (!initialize_properties_from_file("/system/etc/selinux/plat_property_contexts")) {
return false; return false;
} }
if (!initialize_properties_from_file("/vendor/etc/selinux/nonplat_property_contexts") // Don't check for failure here, so we always have a sane list of properties.
&& !initialize_properties_from_file("/vendor/etc/selinux/vendor_property_contexts")) { // E.g. In case of recovery, the vendor partition will not have mounted and we
return false; // still need the system / platform properties to function.
} initialize_properties_from_file("/vendor/etc/selinux/nonplat_property_contexts") ||
initialize_properties_from_file("/vendor/etc/selinux/vendor_property_contexts");
} else { } else {
if (!initialize_properties_from_file("/plat_property_contexts")) { if (!initialize_properties_from_file("/plat_property_contexts")) {
return false; return false;
} }
if (!initialize_properties_from_file("/nonplat_property_contexts")) { initialize_properties_from_file("/nonplat_property_contexts");
return false;
}
} }
return true; return true;