Merge "clang-format system_properties.cpp."

This commit is contained in:
Treehugger Robot 2017-01-31 15:20:19 +00:00 committed by Gerrit Code Review
commit 44bea80d31

View file

@ -129,9 +129,7 @@ private:
class prop_area {
public:
prop_area(const uint32_t magic, const uint32_t version) :
magic_(magic), version_(version) {
prop_area(const uint32_t magic, const uint32_t version) : magic_(magic), version_(version) {
atomic_init(&serial_, 0);
memset(reserved_, 0, sizeof(reserved_));
// Allocate enough space for the root node.
@ -139,20 +137,24 @@ public:
}
const prop_info* find(const char* name);
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);
atomic_uint_least32_t *serial() { return &serial_; }
uint32_t magic() const { return magic_; }
uint32_t version() const { return version_; }
atomic_uint_least32_t* serial() {
return &serial_;
}
uint32_t magic() const {
return magic_;
}
uint32_t version() const {
return version_;
}
private:
void* allocate_obj(const size_t size, uint_least32_t* const off);
prop_bt* new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off);
prop_info *new_prop_info(const char *name, uint32_t namelen,
const char *value, uint32_t valuelen,
prop_info* new_prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen,
uint_least32_t* const off);
void* to_prop_obj(uint_least32_t off);
prop_bt* to_prop_bt(atomic_uint_least32_t* off_p);
@ -160,15 +162,12 @@ private:
prop_bt* root_node();
prop_bt *find_prop_bt(prop_bt *const bt, const char *name,
uint32_t namelen, bool alloc_if_needed);
prop_bt* find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen, bool alloc_if_needed);
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 prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen,
const char* value, uint32_t valuelen, bool alloc_if_needed);
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);
uint32_t bytes_used_;
@ -195,6 +194,7 @@ struct prop_info {
memcpy(this->value, value, valuelen);
this->value[valuelen] = '\0';
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
};
@ -279,10 +279,9 @@ static prop_area* map_fd_ro(const int fd) {
return nullptr;
}
if ((fd_stat.st_uid != 0)
|| (fd_stat.st_gid != 0)
|| ((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0)
|| (fd_stat.st_size < static_cast<off_t>(sizeof(prop_area))) ) {
if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0) ||
((fd_stat.st_mode & (S_IWGRP | S_IWOTH)) != 0) ||
(fd_stat.st_size < static_cast<off_t>(sizeof(prop_area)))) {
return nullptr;
}
@ -313,8 +312,7 @@ static prop_area* map_prop_area(const char* filename) {
return map_result;
}
void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off)
{
void* prop_area::allocate_obj(const size_t size, uint_least32_t* const off) {
const size_t aligned = BIONIC_ALIGN(size, sizeof(uint_least32_t));
if (bytes_used_ + aligned > pa_data_size) {
return NULL;
@ -325,8 +323,7 @@ void *prop_area::allocate_obj(const size_t size, uint_least32_t *const off)
return data_ + *off;
}
prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32_t *const off)
{
prop_bt* prop_area::new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off) {
uint_least32_t new_offset;
void* const p = allocate_obj(sizeof(prop_bt) + namelen + 1, &new_offset);
if (p != NULL) {
@ -338,9 +335,8 @@ prop_bt *prop_area::new_prop_bt(const char *name, uint32_t namelen, uint_least32
return NULL;
}
prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen,
const char *value, uint32_t valuelen, uint_least32_t *const off)
{
prop_info* prop_area::new_prop_info(const char* name, uint32_t namelen, const char* value,
uint32_t valuelen, uint_least32_t* const off) {
uint_least32_t new_offset;
void* const p = allocate_obj(sizeof(prop_info) + namelen + 1, &new_offset);
if (p != NULL) {
@ -352,10 +348,8 @@ prop_info *prop_area::new_prop_info(const char *name, uint32_t namelen,
return NULL;
}
void *prop_area::to_prop_obj(uint_least32_t off)
{
if (off > pa_data_size)
return NULL;
void* prop_area::to_prop_obj(uint_least32_t off) {
if (off > pa_data_size) return NULL;
return (data_ + off);
}
@ -370,14 +364,11 @@ inline prop_info *prop_area::to_prop_info(atomic_uint_least32_t* off_p) {
return reinterpret_cast<prop_info*>(to_prop_obj(off));
}
inline prop_bt *prop_area::root_node()
{
inline prop_bt* prop_area::root_node() {
return reinterpret_cast<prop_bt*>(to_prop_obj(0));
}
static int cmp_prop_name(const char *one, uint32_t one_len, const char *two,
uint32_t two_len)
{
static int cmp_prop_name(const char* one, uint32_t one_len, const char* two, uint32_t two_len) {
if (one_len < two_len)
return -1;
else if (one_len > two_len)
@ -386,10 +377,8 @@ static int cmp_prop_name(const char *one, uint32_t one_len, const char *two,
return strncmp(one, two, one_len);
}
prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
uint32_t namelen, bool alloc_if_needed)
{
prop_bt* prop_area::find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen,
bool alloc_if_needed) {
prop_bt* current = bt;
while (true) {
if (!current) {
@ -437,10 +426,9 @@ prop_bt *prop_area::find_prop_bt(prop_bt *const bt, const char *name,
}
}
const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
uint32_t namelen, const char *value, uint32_t valuelen,
bool alloc_if_needed)
{
const prop_info* prop_area::find_property(prop_bt* const trie, const char* name, uint32_t namelen,
const char* value, uint32_t valuelen,
bool alloc_if_needed) {
if (!trie) return NULL;
const char* remaining_name = name;
@ -448,8 +436,7 @@ const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
while (true) {
const char* sep = strchr(remaining_name, '.');
const bool want_subtree = (sep != NULL);
const uint32_t substr_size = (want_subtree) ?
sep - remaining_name : strlen(remaining_name);
const uint32_t substr_size = (want_subtree) ? sep - remaining_name : strlen(remaining_name);
if (!substr_size) {
return NULL;
@ -476,8 +463,7 @@ const prop_info *prop_area::find_property(prop_bt *const trie, const char *name,
return NULL;
}
if (!want_subtree)
break;
if (!want_subtree) break;
remaining_name = sep + 1;
}
@ -564,6 +550,7 @@ class PropertyServiceConnection {
close(fd_);
}
}
private:
bool CheckSendRecvResult(int result, int expected_len) {
if (result == -1) {
@ -632,46 +619,38 @@ static int send_prop_msg(const prop_msg* msg) {
return result;
}
static void find_nth_fn(const prop_info *pi, void *ptr)
{
static void find_nth_fn(const prop_info* pi, void* ptr) {
find_nth_cookie* cookie = reinterpret_cast<find_nth_cookie*>(ptr);
if (cookie->n == cookie->count)
cookie->pi = pi;
if (cookie->n == cookie->count) cookie->pi = pi;
cookie->count++;
}
bool prop_area::foreach_property(prop_bt* const trie,
void (*propfn)(const prop_info *pi, void *cookie), void *cookie)
{
if (!trie)
return false;
void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
if (!trie) return false;
uint_least32_t left_offset = atomic_load_explicit(&trie->left, memory_order_relaxed);
if (left_offset != 0) {
const int err = foreach_property(to_prop_bt(&trie->left), propfn, cookie);
if (err < 0)
return false;
if (err < 0) return false;
}
uint_least32_t prop_offset = atomic_load_explicit(&trie->prop, memory_order_relaxed);
if (prop_offset != 0) {
prop_info* info = to_prop_info(&trie->prop);
if (!info)
return false;
if (!info) return false;
propfn(info, cookie);
}
uint_least32_t children_offset = atomic_load_explicit(&trie->children, memory_order_relaxed);
if (children_offset != 0) {
const int err = foreach_property(to_prop_bt(&trie->children), propfn, cookie);
if (err < 0)
return false;
if (err < 0) return false;
}
uint_least32_t right_offset = atomic_load_explicit(&trie->right, memory_order_relaxed);
if (right_offset != 0) {
const int err = foreach_property(to_prop_bt(&trie->right), propfn, cookie);
if (err < 0)
return false;
if (err < 0) return false;
}
return true;
@ -681,8 +660,8 @@ const prop_info *prop_area::find(const char *name) {
return find_property(root_node(), name, strlen(name), nullptr, 0, false);
}
bool prop_area::add(const char *name, unsigned int namelen,
const char *value, unsigned int valuelen) {
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);
}
@ -704,8 +683,12 @@ public:
bool check_access_and_open();
void reset_access();
const char* context() const { return context_; }
prop_area* pa() { return pa_; }
const char* context() const {
return context_;
}
prop_area* pa() {
return pa_;
}
context_node* next;
@ -800,8 +783,7 @@ bool context_node::open(bool access_rw, bool* fsetxattr_failed) {
}
char filename[PROP_FILENAME_MAX];
int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
property_filename, context_);
int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
if (len < 0 || len > PROP_FILENAME_MAX) {
lock_.unlock();
return false;
@ -836,8 +818,7 @@ void context_node::reset_access() {
bool context_node::check_access() {
char filename[PROP_FILENAME_MAX];
int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s",
property_filename, context_);
int len = __libc_format_buffer(filename, sizeof(filename), "%s/%s", property_filename, context_);
if (len < 0 || len > PROP_FILENAME_MAX) {
return false;
}
@ -859,8 +840,8 @@ void context_node::unmap() {
static bool map_system_property_area(bool access_rw, bool* fsetxattr_failed) {
char filename[PROP_FILENAME_MAX];
int len = __libc_format_buffer(filename, sizeof(filename),
"%s/properties_serial", property_filename);
int len =
__libc_format_buffer(filename, sizeof(filename), "%s/properties_serial", property_filename);
if (len < 0 || len > PROP_FILENAME_MAX) {
__system_property_area__ = nullptr;
return false;
@ -908,13 +889,11 @@ static prop_area* get_prop_area_for_name(const char* name) {
*/
/* Read an entry from a spec file (e.g. file_contexts) */
static inline int read_spec_entry(char **entry, char **ptr, int *len)
{
static inline int read_spec_entry(char** entry, char** ptr, int* len) {
*entry = NULL;
char* tmp_buf = NULL;
while (isspace(**ptr) && **ptr != '\0')
(*ptr)++;
while (isspace(**ptr) && **ptr != '\0') (*ptr)++;
tmp_buf = *ptr;
*len = 0;
@ -926,8 +905,7 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len)
if (*len) {
*entry = strndup(tmp_buf, *len);
if (!*entry)
return -1;
if (!*entry) return -1;
}
return 0;
@ -941,8 +919,7 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len)
*
* This function calls read_spec_entry() to do the actual string processing.
*/
static int read_spec_entries(char *line_buf, int num_args, ...)
{
static int read_spec_entries(char* line_buf, int num_args, ...) {
char **spec_entry, *buf_p;
int len, rc, items, entry_len = 0;
va_list ap;
@ -957,12 +934,10 @@ static int read_spec_entries(char *line_buf, int num_args, ...)
len++;
buf_p = line_buf;
while (isspace(*buf_p))
buf_p++;
while (isspace(*buf_p)) buf_p++;
/* Skip comment lines and empty lines. */
if (*buf_p == '#' || *buf_p == '\0')
return 0;
if (*buf_p == '#' || *buf_p == '\0') return 0;
/* Process the spec file entries */
va_start(ap, num_args);
@ -981,8 +956,7 @@ static int read_spec_entries(char *line_buf, int num_args, ...)
va_end(ap);
return rc;
}
if (entry_len)
items++;
if (entry_len) items++;
}
va_end(ap);
return items;
@ -1019,8 +993,8 @@ static bool initialize_properties_from_file(const char *filename) {
continue;
}
auto old_context = list_find(
contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
auto old_context =
list_find(contexts, [context](context_node* l) { return !strcmp(l->context(), context); });
if (old_context) {
list_add_after_len(&prefixes, prop_prefix, old_context);
} else {
@ -1074,8 +1048,7 @@ static void free_and_unmap_contexts() {
}
}
int __system_properties_init()
{
int __system_properties_init() {
if (initialized) {
list_foreach(contexts, [](context_node* l) { l->reset_access(); });
return 0;
@ -1100,18 +1073,15 @@ int __system_properties_init()
return 0;
}
int __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;
if (len >= sizeof(property_filename)) return -1;
strcpy(property_filename, filename);
return 0;
}
int __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()) {
@ -1132,8 +1102,7 @@ int __system_property_area_init()
return fsetxattr_failed ? -2 : 0;
}
unsigned int __system_property_area_serial()
{
unsigned int __system_property_area_serial() {
prop_area* pa = __system_property_area__;
if (!pa) {
return -1;
@ -1142,8 +1111,7 @@ unsigned int __system_property_area_serial()
return atomic_load_explicit(pa->serial(), memory_order_acquire);
}
const prop_info *__system_property_find(const char *name)
{
const prop_info* __system_property_find(const char* name) {
if (!__system_property_area__) {
return nullptr;
}
@ -1159,8 +1127,7 @@ const prop_info *__system_property_find(const char *name)
// The C11 standard doesn't allow atomic loads from const fields,
// though C++11 does. Fudge it until standards get straightened out.
static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s,
memory_order mo) {
static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
return atomic_load_explicit(non_const_s, mo);
}
@ -1179,8 +1146,7 @@ int __system_property_read(const prop_info *pi, char *name, char *value) {
// In practice it seems unlikely that the generated code would
// would be any different, so this should be OK.
atomic_thread_fence(memory_order_acquire);
if (serial ==
load_const_atomic(&(pi->serial), memory_order_relaxed)) {
if (serial == load_const_atomic(&(pi->serial), memory_order_relaxed)) {
if (name != nullptr) {
size_t namelen = strlcpy(name, pi->name, PROP_NAME_MAX);
if (namelen >= PROP_NAME_MAX) {
@ -1197,7 +1163,8 @@ int __system_property_read(const prop_info *pi, char *name, char *value) {
}
void __system_property_read_callback(const prop_info* pi,
void (*callback)(void* cookie, const char* name, const char* value),
void (*callback)(void* cookie, const char* name,
const char* value),
void* cookie) {
while (true) {
uint32_t serial = __system_property_serial(pi); // acquire semantics
@ -1216,8 +1183,7 @@ void __system_property_read_callback(const prop_info* pi,
}
}
int __system_property_get(const char *name, char *value)
{
int __system_property_get(const char* name, char* value) {
const prop_info* pi = __system_property_find(name);
if (pi != 0) {
@ -1277,29 +1243,26 @@ int __system_property_set(const char* key, const char* value) {
} else {
// Use proper protocol
PropertyServiceConnection connection;
if (connection.IsValid() &&
connection.SendUint32(PROP_MSG_SETPROP2) &&
connection.SendString(key) &&
connection.SendString(value) &&
if (connection.IsValid() && connection.SendUint32(PROP_MSG_SETPROP2) &&
connection.SendString(key) && connection.SendString(value) &&
connection.RecvInt32(&result)) {
if (result != PROP_SUCCESS) {
__libc_format_log(ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": error code: 0x%x",
key, value, result);
"Unable to set property \"%s\" to \"%s\": error code: 0x%x", key, value,
result);
}
} else {
result = connection.GetLastError();
__libc_format_log(ANDROID_LOG_WARN, "libc",
"Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)",
key, value, result, strerror(result));
"Unable to set property \"%s\" to \"%s\": error code: 0x%x (%s)", key,
value, result, strerror(result));
}
}
return result != 0 ? -1 : 0;
}
int __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;
}
@ -1319,24 +1282,18 @@ int __system_property_update(prop_info *pi, const char *value, unsigned int len)
atomic_thread_fence(memory_order_release);
strlcpy(pi->value, value, len + 1);
atomic_store_explicit(
&pi->serial,
(len << 24) | ((serial + 1) & 0xffffff),
memory_order_release);
atomic_store_explicit(&pi->serial, (len << 24) | ((serial + 1) & 0xffffff), memory_order_release);
__futex_wake(&pi->serial, INT32_MAX);
atomic_store_explicit(
pa->serial(),
atomic_load_explicit(pa->serial(), memory_order_relaxed) + 1,
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;
}
int __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;
}
@ -1372,20 +1329,17 @@ int __system_property_add(const char *name, unsigned int namelen,
}
// Wait for non-locked serial, and retrieve it with acquire semantics.
unsigned int __system_property_serial(const prop_info *pi)
{
unsigned int __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<volatile void *>(
reinterpret_cast<const void *>(&pi->serial)),
serial, NULL);
__futex_wait(const_cast<volatile void*>(reinterpret_cast<const void*>(&pi->serial)), serial,
NULL);
serial = load_const_atomic(&pi->serial, memory_order_acquire);
}
return serial;
}
unsigned int __system_property_wait_any(unsigned int serial)
{
unsigned int __system_property_wait_any(unsigned int serial) {
prop_area* pa = __system_property_area__;
uint32_t my_serial;
@ -1401,10 +1355,10 @@ unsigned int __system_property_wait_any(unsigned int serial)
return my_serial;
}
const prop_info *__system_property_find_nth(unsigned n)
{
const prop_info* __system_property_find_nth(unsigned n) {
if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
__libc_fatal("__system_property_find_nth is not supported since Android O,"
__libc_fatal(
"__system_property_find_nth is not supported since Android O,"
" please use __system_property_foreach instead.");
}
@ -1418,8 +1372,7 @@ const prop_info *__system_property_find_nth(unsigned n)
return cookie.pi;
}
int __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) {
if (!__system_property_area__) {
return -1;
}