Always init for resetprop

This commit is contained in:
topjohnwu 2017-06-11 20:22:10 +08:00
parent a5aa1b3917
commit 309b99eac0
3 changed files with 16 additions and 13 deletions

View File

@ -121,8 +121,7 @@ static int usage(char* arg0) {
return 1; return 1;
} }
int init_resetprop() { static int init_resetprop() {
PRINT_D("resetprop: Initializing...\n");
if (__system_properties_init2()) { if (__system_properties_init2()) {
PRINT_E("resetprop: Initialize error\n"); PRINT_E("resetprop: Initialize error\n");
return -1; return -1;
@ -130,15 +129,21 @@ int init_resetprop() {
return 0; 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) { static void read_prop_info(void* cookie, const char *name, const char *value, uint32_t serial) {
strcpy((char *) cookie, value); strcpy((char *) cookie, value);
} }
// Get prop by name, return string (should free manually!) // Get prop by name, return string (should free manually!)
char *getprop(const char *name) { char *getprop(const char *name) {
if (init_resetprop()) return NULL;
const prop_info *pi = __system_property_find2(name); const prop_info *pi = __system_property_find2(name);
if (pi == NULL) { if (pi == NULL) {
PRINT_D("resetprop: failed to get [%s]\n", name); PRINT_D("resetprop: prop [%s] does not exist\n", name);
return NULL; return NULL;
} }
char value[PROP_VALUE_MAX]; 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) { int setprop2(const char *name, const char *value, const int trigger) {
if (init_resetprop()) return -1;
int ret; int ret;
char *check = getprop(name); prop_info *pi = (prop_info*) __system_property_find2(name);
if (check) { if (pi != NULL) {
free(check);
if (trigger) { if (trigger) {
if (!strncmp(name, "ro.", 3)) deleteprop(name); if (!strncmp(name, "ro.", 3)) deleteprop(name);
ret = __system_property_set2(name, value); ret = __system_property_set2(name, value);
} else { } else {
ret = __system_property_update2((prop_info*) __system_property_find2(name), value, strlen(value)); ret = __system_property_update2(pi, value, strlen(value));
} }
} else { } else {
PRINT_D("resetprop: New prop [%s]\n", name); 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) { int deleteprop(const char *name) {
if (init_resetprop()) return -1;
PRINT_D("resetprop: deleteprop [%s]\n", name); PRINT_D("resetprop: deleteprop [%s]\n", name);
if (__system_property_del(name)) { if (__system_property_del(name)) {
PRINT_E("resetprop: delete prop: [%s] error\n", 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) { int read_prop_file(const char* filename, const int trigger) {
if (init_resetprop()) return -1;
PRINT_D("resetprop: Load prop file [%s]\n", filename); PRINT_D("resetprop: Load prop file [%s]\n", filename);
FILE *fp = fopen(filename, "r"); FILE *fp = fopen(filename, "r");
if (fp == NULL) { if (fp == NULL) {
@ -277,8 +284,6 @@ int resetprop_main(int argc, char *argv[]) {
} }
} }
init_resetprop();
if (file) { if (file) {
return read_prop_file(filename, trigger); return read_prop_file(filename, trigger);
} else if (del) { } else if (del) {

View File

@ -8,7 +8,7 @@
extern "C" { extern "C" {
#endif #endif
int init_resetprop(); int prop_exist(const char *name);
int setprop(const char *name, const char *value); int setprop(const char *name, const char *value);
int setprop2(const char *name, const char *value, const int trigger); int setprop2(const char *name, const char *value, const int trigger);
char *getprop(const char *name); char *getprop(const char *name);

View File

@ -1149,9 +1149,7 @@ int __system_properties_init2() {
if (initialized) { if (initialized) {
// list_foreach(contexts, [](context_node* l) { l->reset_access(); }); // resetprop remove // list_foreach(contexts, [](context_node* l) { l->reset_access(); }); // resetprop remove
// return 0; // resetprop remove return 0;
free_and_unmap_contexts(); // resetprop add
initialized = false; // resetprop add
} }
if (is_dir(property_filename)) { if (is_dir(property_filename)) {
if (!initialize_properties()) { if (!initialize_properties()) {