Skip to content

Commit d11a352

Browse files
committed
Fix prop override on Android V
Change-Id: Iead5765395ffe4d8a5a8f93db4841c68e3c0b9b3
1 parent 37887e5 commit d11a352

File tree

7 files changed

+88
-1
lines changed

7 files changed

+88
-1
lines changed

aosp/bionic/libc/bionic/system_property_api.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ int __system_property_add(const char* name, unsigned int namelen, const char* va
9898
return system_properties.Add(name, namelen, value, valuelen);
9999
}
100100

101+
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
102+
int __system_property_del(const char* name) {
103+
return system_properties.Delete(name);
104+
}
105+
101106
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
102107
uint32_t __system_property_serial(const prop_info* pi) {
103108
return system_properties.Serial(pi);

aosp/bionic/libc/include/sys/_system_properties.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ uint32_t __system_property_area_serial(void);
104104
*/
105105
int __system_property_add(const char* __name, unsigned int __name_length, const char* __value, unsigned int __value_length);
106106

107+
/* Delete a system property.
108+
**
109+
** Returns 0 on success, -1 if the property area is full.
110+
*/
111+
int __system_property_del(const char *__name);
112+
107113
/* Update the value of a system property returned by
108114
** __system_property_find. Can only be done by a single process
109115
** that has write access to the property area, and that process

aosp/bionic/libc/system_properties/include/system_properties/prop_area.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class prop_area {
110110

111111
const prop_info* find(const char* name);
112112
bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
113+
bool del(const char* name);
113114

114115
bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
115116

aosp/bionic/libc/system_properties/include/system_properties/system_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class SystemProperties {
6666
int Get(const char* name, char* value);
6767
int Update(prop_info* pi, const char* value, unsigned int len);
6868
int Add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
69+
int Delete(const char* name);
6970
uint32_t Serial(const prop_info* pi);
7071
uint32_t WaitAny(uint32_t old_serial);
7172
bool Wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr,

aosp/bionic/libc/system_properties/prop_area.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,49 @@ bool prop_area::add(const char* name, unsigned int namelen, const char* value,
368368
return find_property(root_node(), name, namelen, value, valuelen, true);
369369
}
370370

371+
bool prop_area::del(const char* name) {
372+
prop_bt* const trie = root_node();
373+
if (!trie) return false;
374+
375+
const char* remaining_name = name;
376+
prop_bt* current = trie;
377+
while (true) {
378+
const char* sep = strchr(remaining_name, '.');
379+
const bool want_subtree = (sep != nullptr);
380+
const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
381+
382+
if (!substr_size) {
383+
return false;
384+
}
385+
386+
prop_bt* root = nullptr;
387+
uint_least32_t children_offset = atomic_load_explicit(&current->children, memory_order_relaxed);
388+
if (children_offset != 0) {
389+
root = to_prop_bt(&current->children);
390+
}
391+
392+
if (!root) {
393+
return false;
394+
}
395+
396+
current = find_prop_bt(root, remaining_name, substr_size, false);
397+
if (!current) {
398+
return false;
399+
}
400+
401+
if (!want_subtree) break;
402+
403+
remaining_name = sep + 1;
404+
}
405+
406+
uint_least32_t prop_offset = atomic_load_explicit(&current->prop, memory_order_relaxed);
407+
if (prop_offset != 0) {
408+
uint_least32_t new_offset = 0;
409+
atomic_store_explicit(&current->prop, new_offset, memory_order_release);
410+
}
411+
return true;
412+
}
413+
371414
bool prop_area::foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
372415
return foreach_property(root_node(), propfn, cookie);
373416
}

aosp/bionic/libc/system_properties/system_properties.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,36 @@ int SystemProperties::Add(const char* name, unsigned int namelen, const char* va
294294
return 0;
295295
}
296296

297+
int SystemProperties::Delete(const char* name) {
298+
if (!initialized_) {
299+
return -1;
300+
}
301+
302+
prop_area* serial_pa = contexts_->GetSerialPropArea();
303+
if (serial_pa == nullptr) {
304+
return -1;
305+
}
306+
307+
prop_area* pa = contexts_->GetPropAreaForName(name);
308+
if (!pa) {
309+
async_safe_format_log(ANDROID_LOG_ERROR, "libc", "Access denied deleting property \"%s\"", name);
310+
return -1;
311+
}
312+
313+
bool ret = pa->del(name);
314+
if (!ret) {
315+
return -1;
316+
}
317+
318+
// There is only a single mutator, but we want to make sure that
319+
// updates are visible to a reader waiting for the update.
320+
atomic_store_explicit(serial_pa->serial(),
321+
atomic_load_explicit(serial_pa->serial(), memory_order_relaxed) + 1,
322+
memory_order_release);
323+
__futex_wake(serial_pa->serial(), INT32_MAX);
324+
return 0;
325+
}
326+
297327
// Wait for non-locked serial, and retrieve it with acquire semantics.
298328
uint32_t SystemProperties::Serial(const prop_info* pi) {
299329
uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire);

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ void property_override(char const prop[], char const value[], bool add = false)
99
auto pi = (prop_info *) __system_property_find(prop);
1010

1111
if (pi != nullptr) {
12-
__system_property_update(pi, value, strlen(value));
12+
__system_property_del(prop);
13+
__system_property_add(prop, strlen(prop), value, strlen(value));
1314
} else if (add) {
1415
__system_property_add(prop, strlen(prop), value, strlen(value));
1516
}

0 commit comments

Comments
 (0)