From 8fb32059088ae123f2c2f08567279d6346245160 Mon Sep 17 00:00:00 2001 From: Andrew Gunnerson Date: Mon, 2 Jul 2018 21:55:43 -0400 Subject: [PATCH] libmbutil: Place system properties fork in mb namespace There were symbol conflicts previously masked due to -Wl,--allow-multiple-definition, which is required due to conflicts between libc.a and libandroid_support.a. Signed-off-by: Andrew Gunnerson --- libmbutil/CMakeLists.txt | 6 +- libmbutil/external.yaml | 2 +- .../mbutil/external/_system_properties.h | 53 +++---- .../mbutil/external/system_properties.h | 40 +++--- libmbutil/src/external/system_properties.cpp | 133 +++++++++--------- ..._compat.c => system_properties_compat.cpp} | 96 +++++++------ libmbutil/src/properties.cpp | 27 ++-- mbtool/src/boot/property_service.cpp | 16 +-- 8 files changed, 195 insertions(+), 178 deletions(-) rename libmbutil/src/external/{system_properties_compat.c => system_properties_compat.cpp} (59%) diff --git a/libmbutil/CMakeLists.txt b/libmbutil/CMakeLists.txt index 49ad7daf5..6af8eed28 100644 --- a/libmbutil/CMakeLists.txt +++ b/libmbutil/CMakeLists.txt @@ -6,10 +6,10 @@ set_source_files_properties( ) set_source_files_properties( - src/external/system_properties_compat.c + src/external/system_properties_compat.cpp PROPERTIES COMPILE_FLAGS - "-Wno-cast-align -Wno-cast-qual -Wno-shorten-64-to-32 -Wno-sign-conversion -Wno-strict-prototypes" + "-Wno-shorten-64-to-32 -Wno-sign-conversion" ) set(variants) @@ -53,7 +53,7 @@ foreach(variant ${variants}) src/time.cpp src/vibrate.cpp src/external/system_properties.cpp - src/external/system_properties_compat.c + src/external/system_properties_compat.cpp src/result/file_op_result.cpp ) diff --git a/libmbutil/external.yaml b/libmbutil/external.yaml index 749c14381..574d6aa4f 100644 --- a/libmbutil/external.yaml +++ b/libmbutil/external.yaml @@ -11,7 +11,7 @@ commit: 3c75c11a5cb537bbdf499701e4298cee2e07a604 - files: - - src/external/system_properties_compat.c + - src/external/system_properties_compat.cpp repo: https://android.googlesource.com/platform/bionic tag: android-7.1.2_r36 commit: 8011119dffb81326eeecdc2effb3b878d536ff50 diff --git a/libmbutil/include/mbutil/external/_system_properties.h b/libmbutil/include/mbutil/external/_system_properties.h index caa0205b7..5cb5e6b0f 100644 --- a/libmbutil/include/mbutil/external/_system_properties.h +++ b/libmbutil/include/mbutil/external/_system_properties.h @@ -38,7 +38,8 @@ #include "mbutil/external/system_properties.h" -__BEGIN_DECLS +namespace mb +{ #define MB_ENABLE_COMPAT_PROPERTIES 1 @@ -63,38 +64,38 @@ __BEGIN_DECLS ** Map the property area from the specified filename. This ** method is for testing only. */ -int mb__system_property_set_filename(const char *filename); +int __system_property_set_filename(const char *filename); /* ** Initialize the area to be used to store properties. Can ** only be done by a single process that has write access to ** the property area. */ -int mb__system_property_area_init(); +int __system_property_area_init(); /* Read the global serial number of the system properties ** -** Called to predict if a series of cached mb__system_property_find -** objects will have seen mb__system_property_serial values change. +** Called to predict if a series of cached __system_property_find +** objects will have seen __system_property_serial values change. ** But also aids the converse, as changes in the global serial can -** also be used to predict if a failed mb__system_property_find +** also be used to predict if a failed __system_property_find ** could in-turn now find a new object; thus preventing the -** cycles of effort to poll mb__system_property_find. +** cycles of effort to poll __system_property_find. ** ** Typically called at beginning of a cache cycle to signal if _any_ possible ** changes have occurred since last. If there is, one may check each individual -** mb__system_property_serial to confirm dirty, or mb__system_property_find -** to check if the property now exists. If a call to mb__system_property_add -** or mb__system_property_update has completed between two calls to -** mb__system_property_area_serial then the second call will return a larger +** __system_property_serial to confirm dirty, or __system_property_find +** to check if the property now exists. If a call to __system_property_add +** or __system_property_update has completed between two calls to +** __system_property_area_serial then the second call will return a larger ** value than the first call. Beware of race conditions as changes to the ** properties are not atomic, the main value of this call is to determine -** whether the expensive mb__system_property_find is worth retrying to see if +** whether the expensive __system_property_find is worth retrying to see if ** a property now exists. ** ** Returns the serial number on success, -1 on error. */ -uint32_t mb__system_property_area_serial(); +uint32_t __system_property_area_serial(); /* Add a new system property. Can only be done by a single ** process that has write access to the property area, and @@ -104,24 +105,24 @@ uint32_t mb__system_property_area_serial(); ** ** Returns 0 on success, -1 if the property area is full. */ -int mb__system_property_add(const char *name, unsigned int namelen, const char *value, unsigned int valuelen); +int __system_property_add(const char *name, unsigned int namelen, const char *value, unsigned int valuelen); /* Update the value of a system property returned by -** mb__system_property_find. Can only be done by a single process +** __system_property_find. Can only be done by a single process ** that has write access to the property area, and that process ** must handle sequencing to ensure that only one property is ** updated at a time. ** ** Returns 0 on success, -1 if the parameters are incorrect. */ -int mb__system_property_update(prop_info *pi, const char *value, unsigned int len); +int __system_property_update(prop_info *pi, const char *value, unsigned int len); /* Read the serial number of a system property returned by -** mb__system_property_find. +** __system_property_find. ** ** Returns the serial number on success, -1 on error. */ -uint32_t mb__system_property_serial(const prop_info* pi); +uint32_t __system_property_serial(const prop_info* pi); /* Initialize the system properties area in read only mode. * Should be done by all processes that need to read system @@ -129,23 +130,23 @@ uint32_t mb__system_property_serial(const prop_info* pi); * * Returns 0 on success, -1 otherwise. */ -int mb__system_properties_init(); +int __system_properties_init(); -/* Deprecated: use mb__system_property_wait instead. */ -uint32_t mb__system_property_wait_any(uint32_t old_serial); +/* Deprecated: use __system_property_wait instead. */ +uint32_t __system_property_wait_any(uint32_t old_serial); #if MB_ENABLE_COMPAT_PROPERTIES /* Compatibility functions to support using an old init with a new libc, ** mostly for the OTA updater binary. These can be deleted once OTAs from ** a pre-K release no longer needed to be supported. */ -uint32_t mb__system_property_serial_compat(const prop_info *pi); -const prop_info *mb__system_property_find_compat(const char *name); -int mb__system_property_read_compat(const prop_info *pi, char *name, char *value); -int mb__system_property_foreach_compat( +uint32_t __system_property_serial_compat(const prop_info *pi); +const prop_info *__system_property_find_compat(const char *name); +int __system_property_read_compat(const prop_info *pi, char *name, char *value); +int __system_property_foreach_compat( void (*propfn)(const prop_info *pi, void *cookie), void *cookie); #endif -__END_DECLS +} #endif diff --git a/libmbutil/include/mbutil/external/system_properties.h b/libmbutil/include/mbutil/external/system_properties.h index f3fcb7467..e9c330a68 100644 --- a/libmbutil/include/mbutil/external/system_properties.h +++ b/libmbutil/include/mbutil/external/system_properties.h @@ -34,7 +34,10 @@ #include #include -__BEGIN_DECLS +struct timespec; + +namespace mb +{ typedef struct prop_info prop_info; @@ -43,30 +46,30 @@ typedef struct prop_info prop_info; /* * Sets system property `key` to `value`, creating the system property if it doesn't already exist. */ -int mb__system_property_set(const char* key, const char* value) /* __INTRODUCED_IN(12) */; +int __system_property_set(const char* key, const char* value) /* __INTRODUCED_IN(12) */; /* * Returns a `prop_info` corresponding system property `name`, or nullptr if it doesn't exist. - * Use mb__system_property_read_callback to query the current value. + * Use __system_property_read_callback to query the current value. * * Property lookup is expensive, so it can be useful to cache the result of this function. */ -const prop_info* mb__system_property_find(const char* name); +const prop_info* __system_property_find(const char* name); /* * Calls `callback` with a consistent trio of name, value, and serial number for property `pi`. */ -void mb__system_property_read_callback(const prop_info *pi, +void __system_property_read_callback(const prop_info *pi, void (*callback)(void* cookie, const char *name, const char *value, uint32_t serial), void* cookie) /* __INTRODUCED_IN(26) */; /* * Passes a `prop_info` for each system property to the provided - * callback. Use mb__system_property_read_callback() to read the value. + * callback. Use __system_property_read_callback() to read the value. * * This method is for inspecting and debugging the property system, and not generally useful. */ -int mb__system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) +int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) /* __INTRODUCED_IN(19) */; /* @@ -81,22 +84,21 @@ int mb__system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie * Returns true and updates `*new_serial_ptr` on success, or false if the call * timed out. */ -struct timespec; -bool mb__system_property_wait(const prop_info* pi, - uint32_t old_serial, - uint32_t* new_serial_ptr, - const struct timespec* relative_timeout) +bool __system_property_wait(const prop_info* pi, + uint32_t old_serial, + uint32_t* new_serial_ptr, + const struct timespec* relative_timeout) /* __INTRODUCED_IN(26) */; /* Deprecated. In Android O and above, there's no limit on property name length. */ #define PROP_NAME_MAX 32 -/* Deprecated. Use mb__system_property_read_callback instead. */ -int mb__system_property_read(const prop_info* pi, char* name, char* value); -/* Deprecated. Use mb__system_property_read_callback instead. */ -int mb__system_property_get(const char* name, char* value); -/* Deprecated. Use mb__system_property_foreach instead. */ -const prop_info* mb__system_property_find_nth(unsigned n); +/* Deprecated. Use __system_property_read_callback instead. */ +int __system_property_read(const prop_info* pi, char* name, char* value); +/* Deprecated. Use __system_property_read_callback instead. */ +int __system_property_get(const char* name, char* value); +/* Deprecated. Use __system_property_foreach instead. */ +const prop_info* __system_property_find_nth(unsigned n); -__END_DECLS +} #endif diff --git a/libmbutil/src/external/system_properties.cpp b/libmbutil/src/external/system_properties.cpp index 0f33e7b6a..232766eaf 100644 --- a/libmbutil/src/external/system_properties.cpp +++ b/libmbutil/src/external/system_properties.cpp @@ -84,6 +84,9 @@ } \ } while(0) +namespace mb +{ + static constexpr int PROP_FILENAME_MAX = 1024; static constexpr uint32_t PROP_AREA_MAGIC = 0x504f5250; @@ -227,7 +230,7 @@ struct prop_info { }; // This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol. -prop_area* mb__system_property_area__ = nullptr; +prop_area* __system_property_area__ = nullptr; static char property_filename[PROP_FILENAME_MAX] = PROP_FILENAME; #if MB_ENABLE_COMPAT_PROPERTIES @@ -965,8 +968,8 @@ void context_node::unmap() { } munmap(pa_, pa_size); - if (pa_ == mb__system_property_area__) { - mb__system_property_area__ = nullptr; + if (pa_ == __system_property_area__) { + __system_property_area__ = nullptr; } pa_ = nullptr; } @@ -976,21 +979,21 @@ static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) { int len = snprintf(filename, sizeof(filename), "%s/properties_serial", property_filename); if (len < 0 || len > PROP_FILENAME_MAX) { - mb__system_property_area__ = nullptr; + __system_property_area__ = nullptr; return false; } if (access_rw) { - mb__system_property_area__ = + __system_property_area__ = map_prop_area_rw(filename, "u:object_r:properties_serial:s0", fsetxattr_failed); } else { - mb__system_property_area__ = map_prop_area(filename + __system_property_area__ = map_prop_area(filename #if MB_ENABLE_COMPAT_PROPERTIES , false #endif ); } - return mb__system_property_area__; + return __system_property_area__; } static prop_area* get_prop_area_for_name(const char* name) { @@ -1186,15 +1189,15 @@ static bool is_dir(const char* pathname) { static void free_and_unmap_contexts() { list_free(&prefixes); list_free(&contexts); - if (mb__system_property_area__) { - munmap(mb__system_property_area__, pa_size); - mb__system_property_area__ = nullptr; + if (__system_property_area__) { + munmap(__system_property_area__, pa_size); + __system_property_area__ = nullptr; } } -int mb__system_properties_init() { +int __system_properties_init() { // This is called from __libc_init_common, and should leave errno at 0 (http://b/37248982). - mb::ErrorRestorer error_restorer; + ErrorRestorer error_restorer; if (initialized) { list_foreach(contexts, [](context_node* l) { l->reset_access(); }); @@ -1209,22 +1212,22 @@ int mb__system_properties_init() { return -1; } } else { - mb__system_property_area__ = map_prop_area(property_filename + __system_property_area__ = map_prop_area(property_filename #if MB_ENABLE_COMPAT_PROPERTIES , true #endif ); - if (!mb__system_property_area__) { + if (!__system_property_area__) { return -1; } - list_add(&contexts, "legacy_system_prop_area", mb__system_property_area__); + list_add(&contexts, "legacy_system_prop_area", __system_property_area__); list_add_after_len(&prefixes, "*", contexts); } initialized = true; return 0; } -int mb__system_property_set_filename(const char* filename) { +int __system_property_set_filename(const char* filename) { size_t len = strlen(filename); if (len >= sizeof(property_filename)) return -1; @@ -1232,7 +1235,7 @@ int mb__system_property_set_filename(const char* filename) { return 0; } -int mb__system_property_area_init() { +int __system_property_area_init() { free_and_unmap_contexts(); mkdir(property_filename, S_IRWXU | S_IXGRP | S_IXOTH); if (!initialize_properties()) { @@ -1253,23 +1256,23 @@ int mb__system_property_area_init() { return fsetxattr_failed ? -2 : 0; } -uint32_t mb__system_property_area_serial() { - prop_area* pa = mb__system_property_area__; +uint32_t __system_property_area_serial() { + prop_area* pa = __system_property_area__; if (!pa) { return -1; } - // Make sure this read fulfilled before mb__system_property_serial + // Make sure this read fulfilled before __system_property_serial return atomic_load_explicit(pa->serial(), memory_order_acquire); } -const prop_info* mb__system_property_find(const char* name) { - if (!mb__system_property_area__) { +const prop_info* __system_property_find(const char* name) { + if (!__system_property_area__) { return nullptr; } #if MB_ENABLE_COMPAT_PROPERTIES if (__predict_false(compat_mode)) { - return mb__system_property_find_compat(name); + return __system_property_find_compat(name); } #endif @@ -1289,15 +1292,15 @@ static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, m return atomic_load_explicit(non_const_s, mo); } -int mb__system_property_read(const prop_info* pi, char* name, char* value) { +int __system_property_read(const prop_info* pi, char* name, char* value) { #if MB_ENABLE_COMPAT_PROPERTIES if (__predict_false(compat_mode)) { - return mb__system_property_read_compat(pi, name, value); + return __system_property_read_compat(pi, name, value); } #endif while (true) { - uint32_t serial = mb__system_property_serial(pi); // acquire semantics + uint32_t serial = __system_property_serial(pi); // acquire semantics size_t len = SERIAL_VALUE_LEN(serial); memcpy(value, pi->value, len + 1); // TODO: Fix the synchronization scheme here. @@ -1314,7 +1317,7 @@ int mb__system_property_read(const prop_info* pi, char* name, char* value) { size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX); if (namelen >= PROP_NAME_MAX) { LOGE("The property name length for \"%s\" is >= %d;" - " please use mb__system_property_read_callback" + " please use __system_property_read_callback" " to read this property. (the name is truncated to \"%s\")", pi->name, PROP_NAME_MAX - 1, name); } @@ -1324,33 +1327,33 @@ int mb__system_property_read(const prop_info* pi, char* name, char* value) { } } -void mb__system_property_read_callback(const prop_info* pi, - void (*callback)(void* cookie, - const char* name, - const char* value, - uint32_t serial), - void* cookie) { +void __system_property_read_callback(const prop_info* pi, + void (*callback)(void* cookie, + const char* name, + const char* value, + uint32_t serial), + void* cookie) { #if MB_ENABLE_COMPAT_PROPERTIES // TODO (dimitry): do we need compat mode for this function? if (__predict_false(compat_mode)) { - uint32_t serial = mb__system_property_serial_compat(pi); + uint32_t serial = __system_property_serial_compat(pi); char name_buf[PROP_NAME_MAX]; char value_buf[PROP_VALUE_MAX]; - mb__system_property_read_compat(pi, name_buf, value_buf); + __system_property_read_compat(pi, name_buf, value_buf); callback(cookie, name_buf, value_buf, serial); return; } #endif while (true) { - uint32_t serial = mb__system_property_serial(pi); // acquire semantics + uint32_t serial = __system_property_serial(pi); // acquire semantics size_t len = SERIAL_VALUE_LEN(serial); char value_buf[len + 1]; memcpy(value_buf, pi->value, len); value_buf[len] = '\0'; - // TODO: see todo in mb__system_property_read function + // TODO: see todo in __system_property_read function atomic_thread_fence(memory_order_acquire); if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) { callback(cookie, pi->name, value_buf, serial); @@ -1359,11 +1362,11 @@ void mb__system_property_read_callback(const prop_info* pi, } } -int mb__system_property_get(const char* name, char* value) { - const prop_info* pi = mb__system_property_find(name); +int __system_property_get(const char* name, char* value) { + const prop_info* pi = __system_property_find(name); if (pi != 0) { - return mb__system_property_read(pi, nullptr, value); + return __system_property_read(pi, nullptr, value); } else { value[0] = 0; return 0; @@ -1377,7 +1380,7 @@ static atomic_uint_least32_t g_propservice_protocol_version = 0; static void detect_protocol_version() { char value[PROP_VALUE_MAX]; - if (mb__system_property_get(kServiceVersionPropertyName, value) == 0) { + if (__system_property_get(kServiceVersionPropertyName, value) == 0) { g_propservice_protocol_version = kProtocolVersion1; LOGW("Using old property service protocol (\"%s\" is not set)", kServiceVersionPropertyName); @@ -1393,7 +1396,7 @@ static void detect_protocol_version() { } } -int mb__system_property_set(const char* key, const char* value) { +int __system_property_set(const char* key, const char* value) { if (key == nullptr) return -1; if (value == nullptr) value = ""; if (strlen(value) >= PROP_VALUE_MAX) return -1; @@ -1460,12 +1463,12 @@ int mb__system_property_set(const char* key, const char* value) { } } -int mb__system_property_update(prop_info* pi, const char* value, unsigned int len) { +int __system_property_update(prop_info* pi, const char* value, unsigned int len) { if (len >= PROP_VALUE_MAX) { return -1; } - prop_area* pa = mb__system_property_area__; + prop_area* pa = __system_property_area__; if (!pa) { return -1; @@ -1490,8 +1493,8 @@ int mb__system_property_update(prop_info* pi, const char* value, unsigned int le return 0; } -int mb__system_property_add(const char* name, unsigned int namelen, const char* value, - unsigned int valuelen) { +int __system_property_add(const char* name, unsigned int namelen, const char* value, + unsigned int valuelen) { if (valuelen >= PROP_VALUE_MAX) { return -1; } @@ -1500,7 +1503,7 @@ int mb__system_property_add(const char* name, unsigned int namelen, const char* return -1; } - if (!mb__system_property_area__) { + if (!__system_property_area__) { return -1; } @@ -1519,15 +1522,15 @@ int mb__system_property_add(const char* name, unsigned int namelen, const char* // There is only a single mutator, but we want to make sure that // updates are visible to a reader waiting for the update. atomic_store_explicit( - mb__system_property_area__->serial(), - atomic_load_explicit(mb__system_property_area__->serial(), memory_order_relaxed) + 1, + __system_property_area__->serial(), + atomic_load_explicit(__system_property_area__->serial(), memory_order_relaxed) + 1, memory_order_release); - __futex_wake(mb__system_property_area__->serial(), INT32_MAX); + __futex_wake(__system_property_area__->serial(), INT32_MAX); return 0; } // Wait for non-locked serial, and retrieve it with acquire semantics. -uint32_t mb__system_property_serial(const prop_info* pi) { +uint32_t __system_property_serial(const prop_info* pi) { uint32_t serial = load_const_atomic(&pi->serial, memory_order_acquire); while (SERIAL_DIRTY(serial)) { __futex_wait(const_cast<_Atomic(uint_least32_t)*>(&pi->serial), serial, nullptr); @@ -1536,21 +1539,21 @@ uint32_t mb__system_property_serial(const prop_info* pi) { return serial; } -uint32_t mb__system_property_wait_any(uint32_t old_serial) { +uint32_t __system_property_wait_any(uint32_t old_serial) { uint32_t new_serial; - mb__system_property_wait(nullptr, old_serial, &new_serial, nullptr); + __system_property_wait(nullptr, old_serial, &new_serial, nullptr); return new_serial; } -bool mb__system_property_wait(const prop_info* pi, - uint32_t old_serial, - uint32_t* new_serial_ptr, - const timespec* relative_timeout) { +bool __system_property_wait(const prop_info* pi, + uint32_t old_serial, + uint32_t* new_serial_ptr, + const timespec* relative_timeout) { // Are we waiting on the global serial or a specific serial? atomic_uint_least32_t* serial_ptr; if (pi == nullptr) { - if (mb__system_property_area__ == nullptr) return -1; - serial_ptr = mb__system_property_area__->serial(); + if (__system_property_area__ == nullptr) return -1; + serial_ptr = __system_property_area__->serial(); } else { serial_ptr = const_cast(&pi->serial); } @@ -1568,7 +1571,7 @@ bool mb__system_property_wait(const prop_info* pi, return true; } -const prop_info* mb__system_property_find_nth(unsigned n) { +const prop_info* __system_property_find_nth(unsigned n) { struct find_nth { const uint32_t sought; uint32_t current; @@ -1580,18 +1583,18 @@ const prop_info* mb__system_property_find_nth(unsigned n) { if (self->current++ == self->sought) self->result = pi; } } state(n); - mb__system_property_foreach(find_nth::fn, &state); + __system_property_foreach(find_nth::fn, &state); return state.result; } -int mb__system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) { - if (!mb__system_property_area__) { +int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) { + if (!__system_property_area__) { return -1; } #if MB_ENABLE_COMPAT_PROPERTIES if (__predict_false(compat_mode)) { - return mb__system_property_foreach_compat(propfn, cookie); + return __system_property_foreach_compat(propfn, cookie); } #endif @@ -1602,3 +1605,5 @@ int mb__system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie }); return 0; } + +} diff --git a/libmbutil/src/external/system_properties_compat.c b/libmbutil/src/external/system_properties_compat.cpp similarity index 59% rename from libmbutil/src/external/system_properties_compat.c rename to libmbutil/src/external/system_properties_compat.cpp index def5bcf35..39c6a9a0d 100644 --- a/libmbutil/src/external/system_properties_compat.c +++ b/libmbutil/src/external/system_properties_compat.cpp @@ -34,19 +34,26 @@ * OTAs from pre-K versions are no longer supported. */ -#include +#include + +#include #include "mbutil/external/bionic_futex.h" #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ #include "mbutil/external/_system_properties.h" -#define TOC_NAME_LEN(toc) ((toc) >> 24) -#define TOC_TO_INFO(area, toc) ((prop_info_compat*) (((char*) area) + ((toc) & 0xFFFFFF))) -#define SERIAL_DIRTY(serial) ((serial)&1) +#define TOC_NAME_LEN(toc) ((toc) >> 24) +#define TOC_TO_INFO(area, toc) (reinterpret_cast( \ + reinterpret_cast(area) + ((toc) & 0xFFFFFF))) +#define SERIAL_DIRTY(serial) ((serial) & 1) #define SERIAL_VALUE_LEN(serial) ((serial) >> 24) -struct prop_area_compat { +namespace mb +{ + +struct PropAreaCompat +{ unsigned volatile count; unsigned volatile serial; unsigned magic; @@ -55,68 +62,70 @@ struct prop_area_compat { unsigned toc[1]; }; -typedef struct prop_area_compat prop_area_compat; - struct prop_area; -typedef struct prop_area prop_area; -struct prop_info_compat { +struct PropInfoCompat +{ char name[PROP_NAME_MAX]; unsigned volatile serial; char value[PROP_VALUE_MAX]; }; -typedef struct prop_info_compat prop_info_compat; - -extern prop_area *mb__system_property_area__; +extern prop_area *__system_property_area__; -__LIBC_HIDDEN__ uint32_t mb__system_property_serial_compat(const prop_info *_pi) +__LIBC_HIDDEN__ uint32_t __system_property_serial_compat(const prop_info *_pi) { - const prop_info_compat *pi = (const prop_info_compat *) _pi; + auto *pi = reinterpret_cast(_pi); return pi->serial; } -__LIBC_HIDDEN__ const prop_info *mb__system_property_find_compat(const char *name) +__LIBC_HIDDEN__ const prop_info *__system_property_find_compat(const char *name) { - prop_area_compat *pa = (prop_area_compat *)mb__system_property_area__; + auto *pa = reinterpret_cast(__system_property_area__); unsigned count = pa->count; unsigned *toc = pa->toc; unsigned len = strlen(name); - prop_info_compat *pi; - if (len >= PROP_NAME_MAX) - return 0; - if (len < 1) - return 0; + if (len >= PROP_NAME_MAX) { + return nullptr; + } + if (len < 1) { + return nullptr; + } - while(count--) { + while (count--) { unsigned entry = *toc++; - if(TOC_NAME_LEN(entry) != len) continue; + if (TOC_NAME_LEN(entry) != len) { + continue; + } - pi = TOC_TO_INFO(pa, entry); - if(memcmp(name, pi->name, len)) continue; + PropInfoCompat *pi = TOC_TO_INFO(pa, entry); + if (memcmp(name, pi->name, len) != 0) { + continue; + } - return (const prop_info *)pi; + return reinterpret_cast(pi); } - return 0; + return nullptr; } -__LIBC_HIDDEN__ int mb__system_property_read_compat(const prop_info *_pi, char *name, char *value) +__LIBC_HIDDEN__ int __system_property_read_compat(const prop_info *_pi, + char *name, char *value) { - unsigned serial, len; - const prop_info_compat *pi = (const prop_info_compat *)_pi; + auto *pi = reinterpret_cast(_pi); - for(;;) { - serial = pi->serial; - while(SERIAL_DIRTY(serial)) { - __futex_wait((volatile void *)&pi->serial, serial, NULL); + for (;;) { + unsigned serial = pi->serial; + while (SERIAL_DIRTY(serial)) { + __futex_wait(const_cast(&pi->serial), serial, + nullptr); serial = pi->serial; } - len = SERIAL_VALUE_LEN(serial); + unsigned len = SERIAL_VALUE_LEN(serial); memcpy(value, pi->value, len + 1); - if(serial == pi->serial) { - if(name != 0) { + if (serial == pi->serial) { + if (name) { strcpy(name, pi->name); } return len; @@ -124,18 +133,19 @@ __LIBC_HIDDEN__ int mb__system_property_read_compat(const prop_info *_pi, char * } } -__LIBC_HIDDEN__ int mb__system_property_foreach_compat( +__LIBC_HIDDEN__ int __system_property_foreach_compat( void (*propfn)(const prop_info *pi, void *cookie), void *cookie) { - prop_area_compat *pa = (prop_area_compat *)mb__system_property_area__; - unsigned i; + auto *pa = reinterpret_cast(__system_property_area__); - for (i = 0; i < pa->count; i++) { + for (unsigned i = 0; i < pa->count; i++) { unsigned entry = pa->toc[i]; - prop_info_compat *pi = TOC_TO_INFO(pa, entry); - propfn((const prop_info *)pi, cookie); + auto *pi = TOC_TO_INFO(pa, entry); + propfn(reinterpret_cast(pi), cookie); } return 0; } + +} diff --git a/libmbutil/src/properties.cpp b/libmbutil/src/properties.cpp index ac54f5603..1a203ae4d 100644 --- a/libmbutil/src/properties.cpp +++ b/libmbutil/src/properties.cpp @@ -53,7 +53,7 @@ static void initialize_properties() std::lock_guard lock(initialized_lock); if (!initialized) { - mb__system_properties_init(); + __system_properties_init(); initialized = true; } } @@ -91,7 +91,7 @@ static PropertyIterAction read_property_cb(const prop_info *pi, Ctx ctx{&fn, PropertyIterAction::Stop}; // Assume properties are already initialized if there's a prop_info object - mb__system_property_read_callback( + __system_property_read_callback( pi, [](void *cookie, const char *name, const char *value, uint32_t serial) { (void) serial; @@ -107,7 +107,7 @@ std::optional property_get(const std::string &key) { initialize_properties(); - const prop_info *pi = mb__system_property_find(key.c_str()); + const prop_info *pi = __system_property_find(key.c_str()); if (!pi) { return std::nullopt; } @@ -148,7 +148,7 @@ bool property_set(const std::string &key, const std::string &value) { initialize_properties(); - return mb__system_property_set(key.c_str(), value.c_str()) == 0; + return __system_property_set(key.c_str(), value.c_str()) == 0; } bool property_set_direct(const std::string &key, const std::string &value) @@ -156,17 +156,17 @@ bool property_set_direct(const std::string &key, const std::string &value) initialize_properties(); prop_info *pi = const_cast( - mb__system_property_find(key.c_str())); + __system_property_find(key.c_str())); int ret; if (pi) { - ret = mb__system_property_update(pi, value.c_str(), - static_cast(value.size())); + ret = __system_property_update(pi, value.c_str(), + static_cast(value.size())); } else { - ret = mb__system_property_add(key.c_str(), - static_cast(key.size()), - value.c_str(), - static_cast(value.size())); + ret = __system_property_add(key.c_str(), + static_cast(key.size()), + value.c_str(), + static_cast(value.size())); } return ret == 0; @@ -184,7 +184,7 @@ bool property_iter(const std::function &fn) initialize_properties(); - return mb__system_property_foreach( + return __system_property_foreach( [](const prop_info *pi, void *cookie) { auto *ctx_ = static_cast(cookie); @@ -203,8 +203,7 @@ std::optional property_get_all() initialize_properties(); - if (mb__system_property_foreach( - [](const prop_info *pi, void *cookie) { + if (__system_property_foreach([](const prop_info *pi, void *cookie) { auto *map = static_cast(cookie); read_property_cb(pi, [&](std::string_view key, std::string_view value) { diff --git a/mbtool/src/boot/property_service.cpp b/mbtool/src/boot/property_service.cpp index 360f66e93..00a9e0840 100644 --- a/mbtool/src/boot/property_service.cpp +++ b/mbtool/src/boot/property_service.cpp @@ -192,7 +192,7 @@ bool PropertyService::initialize() return false; } - if (mb__system_property_area_init() != 0) { + if (mb::__system_property_area_init() != 0) { return false; } @@ -201,9 +201,9 @@ bool PropertyService::initialize() std::optional PropertyService::get(const std::string &name) { - if (auto const pi = mb__system_property_find(name.c_str())) { + if (auto const pi = mb::__system_property_find(name.c_str())) { std::string value; - mb__system_property_read_callback( + mb::__system_property_read_callback( pi, [](void *cookie, const char *, const char *v, uint32_t) { *static_cast(cookie) = v; }, &value); @@ -256,8 +256,8 @@ uint32_t PropertyService::set_internal(const std::string &name, return PROP_ERROR_INVALID_VALUE; } - if (auto pi = const_cast( - mb__system_property_find(name.c_str()))) { + if (auto pi = const_cast( + mb::__system_property_find(name.c_str()))) { // ro.* properties are actually "write-once". if (mb::starts_with(name, "ro.")) { LOGE("['%s'='%s'] Failed to set property: Property already set", @@ -265,10 +265,10 @@ uint32_t PropertyService::set_internal(const std::string &name, return PROP_ERROR_READ_ONLY_PROPERTY; } - mb__system_property_update(pi, value.data(), value.size()); + mb::__system_property_update(pi, value.data(), value.size()); } else { - int rc = mb__system_property_add(name.data(), name.size(), value.data(), - value.size()); + int rc = mb::__system_property_add(name.data(), name.size(), + value.data(), value.size()); if (rc < 0) { LOGE("['%s'='%s'] Failed to add property", name.c_str(), std::string(value).c_str());