diff options
Diffstat (limited to 'liblog/properties.cpp')
-rw-r--r-- | liblog/properties.cpp | 243 |
1 files changed, 6 insertions, 237 deletions
diff --git a/liblog/properties.cpp b/liblog/properties.cpp index 239211273..88f0bf1ee 100644 --- a/liblog/properties.cpp +++ b/liblog/properties.cpp @@ -294,33 +294,12 @@ int __android_log_is_loggable(int prio, const char* tag, int default_prio) { } int __android_log_is_debuggable() { - static uint32_t serial; - static struct cache_char tag_cache; - static const char key[] = "ro.debuggable"; - int ret; - - if (tag_cache.c) { /* ro property does not change after set */ - ret = tag_cache.c == '1'; - } else if (lock()) { - struct cache_char temp_cache = {{NULL, 0xFFFFFFFF}, '\0'}; - refresh_cache(&temp_cache, key); - ret = temp_cache.c == '1'; - } else { - int change_detected = check_cache(&tag_cache.cache); - uint32_t current_serial = __system_property_area_serial(); - if (current_serial != serial) { - change_detected = 1; - } - if (change_detected) { - refresh_cache(&tag_cache, key); - serial = current_serial; - } - ret = tag_cache.c == '1'; - - unlock(); - } + static int is_debuggable = [] { + char value[PROP_VALUE_MAX] = {}; + return __system_property_get("ro.debuggable", value) > 0 && !strcmp(value, "1"); + }(); - return ret; + return is_debuggable; } /* @@ -385,216 +364,6 @@ int __android_log_security() { return do_cache2_char(&security); } -/* - * Interface that represents the logd buffer size determination so that others - * need not guess our intentions. - */ - -/* Property helper */ -static bool check_flag(const char* prop, const char* flag) { - const char* cp = strcasestr(prop, flag); - if (!cp) { - return false; - } - /* We only will document comma (,) */ - static const char sep[] = ",:;|+ \t\f"; - if ((cp != prop) && !strchr(sep, cp[-1])) { - return false; - } - cp += strlen(flag); - return !*cp || !!strchr(sep, *cp); -} - -/* cache structure */ -struct cache_property { - struct cache cache; - char property[PROP_VALUE_MAX]; -}; - -static void refresh_cache_property(struct cache_property* cache, const char* key) { - if (!cache->cache.pinfo) { - cache->cache.pinfo = __system_property_find(key); - if (!cache->cache.pinfo) { - return; - } - } - cache->cache.serial = __system_property_serial(cache->cache.pinfo); - __system_property_read(cache->cache.pinfo, 0, cache->property); -} - -/* get boolean with the logger twist that supports eng adjustments */ -bool __android_logger_property_get_bool(const char* key, int flag) { - struct cache_property property = {{NULL, 0xFFFFFFFF}, {0}}; - if (flag & BOOL_DEFAULT_FLAG_PERSIST) { - char newkey[strlen("persist.") + strlen(key) + 1]; - snprintf(newkey, sizeof(newkey), "ro.%s", key); - refresh_cache_property(&property, newkey); - property.cache.pinfo = NULL; - property.cache.serial = 0xFFFFFFFF; - snprintf(newkey, sizeof(newkey), "persist.%s", key); - refresh_cache_property(&property, newkey); - property.cache.pinfo = NULL; - property.cache.serial = 0xFFFFFFFF; - } - - refresh_cache_property(&property, key); - - if (check_flag(property.property, "true")) { - return true; - } - if (check_flag(property.property, "false")) { - return false; - } - if (property.property[0]) { - flag &= ~(BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE); - } - if (check_flag(property.property, "eng")) { - flag |= BOOL_DEFAULT_FLAG_ENG; - } - /* this is really a "not" flag */ - if (check_flag(property.property, "svelte")) { - flag |= BOOL_DEFAULT_FLAG_SVELTE; - } - - /* Sanity Check */ - if (flag & (BOOL_DEFAULT_FLAG_SVELTE | BOOL_DEFAULT_FLAG_ENG)) { - flag &= ~BOOL_DEFAULT_FLAG_TRUE_FALSE; - flag |= BOOL_DEFAULT_TRUE; - } - - if ((flag & BOOL_DEFAULT_FLAG_SVELTE) && - __android_logger_property_get_bool("ro.config.low_ram", BOOL_DEFAULT_FALSE)) { - return false; - } - if ((flag & BOOL_DEFAULT_FLAG_ENG) && !__android_log_is_debuggable()) { - return false; - } - - return (flag & BOOL_DEFAULT_FLAG_TRUE_FALSE) != BOOL_DEFAULT_FALSE; -} - -bool __android_logger_valid_buffer_size(unsigned long value) { - return LOG_BUFFER_MIN_SIZE <= value && value <= LOG_BUFFER_MAX_SIZE; -} - -struct cache2_property_size { - pthread_mutex_t lock; - uint32_t serial; - const char* key_persist; - struct cache_property cache_persist; - const char* key_ro; - struct cache_property cache_ro; - unsigned long (*const evaluate)(const struct cache2_property_size* self); -}; - -static inline unsigned long do_cache2_property_size(struct cache2_property_size* self) { - uint32_t current_serial; - int change_detected; - unsigned long v; - - if (pthread_mutex_trylock(&self->lock)) { - /* We are willing to accept some race in this context */ - return self->evaluate(self); - } - - change_detected = check_cache(&self->cache_persist.cache) || check_cache(&self->cache_ro.cache); - current_serial = __system_property_area_serial(); - if (current_serial != self->serial) { - change_detected = 1; - } - if (change_detected) { - refresh_cache_property(&self->cache_persist, self->key_persist); - refresh_cache_property(&self->cache_ro, self->key_ro); - self->serial = current_serial; - } - v = self->evaluate(self); - - pthread_mutex_unlock(&self->lock); - - return v; -} - -static unsigned long property_get_size_from_cache(const struct cache_property* cache) { - char* cp; - unsigned long value = strtoul(cache->property, &cp, 10); - - switch (*cp) { - case 'm': - case 'M': - value *= 1024; - [[fallthrough]]; - case 'k': - case 'K': - value *= 1024; - [[fallthrough]]; - case '\0': - break; - - default: - value = 0; - } - - if (!__android_logger_valid_buffer_size(value)) { - value = 0; - } - - return value; -} - -static unsigned long evaluate_property_get_size(const struct cache2_property_size* self) { - unsigned long size = property_get_size_from_cache(&self->cache_persist); - if (size) { - return size; - } - return property_get_size_from_cache(&self->cache_ro); -} - -unsigned long __android_logger_get_buffer_size(log_id_t logId) { - static const char global_tunable[] = "persist.logd.size"; /* Settings App */ - static const char global_default[] = "ro.logd.size"; /* BoardConfig.mk */ - static struct cache2_property_size global = { - /* clang-format off */ - PTHREAD_MUTEX_INITIALIZER, 0, - global_tunable, { { NULL, 0xFFFFFFFF }, {} }, - global_default, { { NULL, 0xFFFFFFFF }, {} }, - evaluate_property_get_size - /* clang-format on */ - }; - char key_persist[strlen(global_tunable) + strlen(".security") + 1]; - char key_ro[strlen(global_default) + strlen(".security") + 1]; - struct cache2_property_size local = { - /* clang-format off */ - PTHREAD_MUTEX_INITIALIZER, 0, - key_persist, { { NULL, 0xFFFFFFFF }, {} }, - key_ro, { { NULL, 0xFFFFFFFF }, {} }, - evaluate_property_get_size - /* clang-format on */ - }; - unsigned long property_size, default_size; - - default_size = do_cache2_property_size(&global); - if (!default_size) { - default_size = __android_logger_property_get_bool("ro.config.low_ram", BOOL_DEFAULT_FALSE) - ? LOG_BUFFER_MIN_SIZE /* 64K */ - : LOG_BUFFER_SIZE; /* 256K */ - } - - snprintf(key_persist, sizeof(key_persist), "%s.%s", global_tunable, - android_log_id_to_name(logId)); - snprintf(key_ro, sizeof(key_ro), "%s.%s", global_default, android_log_id_to_name(logId)); - property_size = do_cache2_property_size(&local); - - if (!property_size) { - property_size = default_size; - } - - if (!property_size) { - property_size = LOG_BUFFER_SIZE; - } - - return property_size; -} - #else int __android_log_is_loggable(int prio, const char*, int) { @@ -613,4 +382,4 @@ int __android_log_is_debuggable() { return 1; } -#endif
\ No newline at end of file +#endif |