From 309b99eac0677a6e323b6990e8f8f545a766769f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 11 Jun 2017 20:22:10 +0800 Subject: [PATCH] Always init for resetprop --- jni/resetprop/resetprop.cpp | 23 ++++++++++++++--------- jni/resetprop/resetprop.h | 2 +- jni/resetprop/system_properties.cpp | 4 +--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/jni/resetprop/resetprop.cpp b/jni/resetprop/resetprop.cpp index 6c2b94aa3..966730890 100644 --- a/jni/resetprop/resetprop.cpp +++ b/jni/resetprop/resetprop.cpp @@ -121,8 +121,7 @@ static int usage(char* arg0) { return 1; } -int init_resetprop() { - PRINT_D("resetprop: Initializing...\n"); +static int init_resetprop() { if (__system_properties_init2()) { PRINT_E("resetprop: Initialize error\n"); return -1; @@ -130,15 +129,21 @@ int init_resetprop() { return 0; } +int prop_exist(const char *name) { + if (init_resetprop()) return 0; + return __system_property_find2(name) != NULL; +} + static void read_prop_info(void* cookie, const char *name, const char *value, uint32_t serial) { strcpy((char *) cookie, value); } // Get prop by name, return string (should free manually!) char *getprop(const char *name) { + if (init_resetprop()) return NULL; const prop_info *pi = __system_property_find2(name); if (pi == NULL) { - PRINT_D("resetprop: failed to get [%s]\n", name); + PRINT_D("resetprop: prop [%s] does not exist\n", name); return NULL; } char value[PROP_VALUE_MAX]; @@ -152,16 +157,16 @@ int setprop(const char *name, const char *value) { } int setprop2(const char *name, const char *value, const int trigger) { + if (init_resetprop()) return -1; int ret; - char *check = getprop(name); - if (check) { - free(check); + prop_info *pi = (prop_info*) __system_property_find2(name); + if (pi != NULL) { if (trigger) { if (!strncmp(name, "ro.", 3)) deleteprop(name); ret = __system_property_set2(name, value); } else { - ret = __system_property_update2((prop_info*) __system_property_find2(name), value, strlen(value)); + ret = __system_property_update2(pi, value, strlen(value)); } } else { PRINT_D("resetprop: New prop [%s]\n", name); @@ -182,6 +187,7 @@ int setprop2(const char *name, const char *value, const int trigger) { } int deleteprop(const char *name) { + if (init_resetprop()) return -1; PRINT_D("resetprop: deleteprop [%s]\n", name); if (__system_property_del(name)) { PRINT_E("resetprop: delete prop: [%s] error\n", name); @@ -191,6 +197,7 @@ int deleteprop(const char *name) { } int read_prop_file(const char* filename, const int trigger) { + if (init_resetprop()) return -1; PRINT_D("resetprop: Load prop file [%s]\n", filename); FILE *fp = fopen(filename, "r"); if (fp == NULL) { @@ -277,8 +284,6 @@ int resetprop_main(int argc, char *argv[]) { } } - init_resetprop(); - if (file) { return read_prop_file(filename, trigger); } else if (del) { diff --git a/jni/resetprop/resetprop.h b/jni/resetprop/resetprop.h index e6fe11c9c..86da88f6a 100644 --- a/jni/resetprop/resetprop.h +++ b/jni/resetprop/resetprop.h @@ -8,7 +8,7 @@ extern "C" { #endif -int init_resetprop(); +int prop_exist(const char *name); int setprop(const char *name, const char *value); int setprop2(const char *name, const char *value, const int trigger); char *getprop(const char *name); diff --git a/jni/resetprop/system_properties.cpp b/jni/resetprop/system_properties.cpp index 20c0adf25..7ed9eec8b 100644 --- a/jni/resetprop/system_properties.cpp +++ b/jni/resetprop/system_properties.cpp @@ -1149,9 +1149,7 @@ int __system_properties_init2() { if (initialized) { // list_foreach(contexts, [](context_node* l) { l->reset_access(); }); // resetprop remove - // return 0; // resetprop remove - free_and_unmap_contexts(); // resetprop add - initialized = false; // resetprop add + return 0; } if (is_dir(property_filename)) { if (!initialize_properties()) {