Update and add new resetprop features

This commit is contained in:
topjohnwu 2017-07-18 11:56:57 +08:00
parent a3fcc64aaa
commit 5b36b4472c
2 changed files with 34 additions and 9 deletions

View File

@ -66,23 +66,25 @@
#define PRINT_D(...) { LOGD(__VA_ARGS__); if (verbose) printf(__VA_ARGS__); } #define PRINT_D(...) { LOGD(__VA_ARGS__); if (verbose) printf(__VA_ARGS__); }
#define PRINT_E(...) { LOGE(__VA_ARGS__); fprintf(stderr, __VA_ARGS__); } #define PRINT_E(...) { LOGE(__VA_ARGS__); fprintf(stderr, __VA_ARGS__); }
#define PERSISTENT_PROPERTY_DIR "/data/property"
static int verbose = 0; static int verbose = 0;
static bool is_legal_property_name(const char* name, size_t namelen) { static bool is_legal_property_name(const char *name, size_t namelen) {
if (namelen < 1) return false; if (namelen < 1) return false;
if (name[0] == '.') return false; if (name[0] == '.') return false;
if (name[namelen - 1] == '.') return false; if (name[namelen - 1] == '.') return false;
/* Only allow alphanumeric, plus '.', '-', or '_' */ /* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
/* Don't allow ".." to appear in a property name */ /* Don't allow ".." to appear in a property name */
for (size_t i = 0; i < namelen; i++) { for (size_t i = 0; i < namelen; i++) {
if (name[i] == '.') { if (name[i] == '.') {
// i=0 is guaranteed to never have a dot. See above. // i=0 is guaranteed to never have a dot. See above.
if (name[i - 1] == '.') return false; if (name[i-1] == '.') return false;
continue; continue;
} }
if (name[i] == '_' || name[i] == '-') continue; if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
if (name[i] >= 'a' && name[i] <= 'z') continue; if (name[i] >= 'a' && name[i] <= 'z') continue;
if (name[i] >= 'A' && name[i] <= 'Z') continue; if (name[i] >= 'A' && name[i] <= 'Z') continue;
if (name[i] >= '0' && name[i] <= '9') continue; if (name[i] >= '0' && name[i] <= '9') continue;
@ -103,6 +105,7 @@ static int usage(char* arg0) {
"Options:\n" "Options:\n"
" -v verbose output\n" " -v verbose output\n"
" -n don't trigger events when changing props\n" " -n don't trigger events when changing props\n"
" if used with deleteprop determines whether remove persist prop file\n"
, arg0, arg0, arg0, arg0); , arg0, arg0, arg0, arg0);
return 1; return 1;
} }
@ -138,6 +141,22 @@ char *getprop(const char *name) {
return strdup(value); return strdup(value);
} }
static void (*cb)(const char *);
static void run_actual_cb(void* cookie, const char *name, const char *value, uint32_t serial) {
cb(name);
}
static void prop_foreach_cb(const prop_info* pi, void* cookie) {
__system_property_read_callback2(pi, run_actual_cb, NULL);
}
void getprop_all(void (*cbk)(const char *name)) {
if (init_resetprop()) return;
cb = cbk;
__system_property_foreach2(prop_foreach_cb, NULL);
}
int setprop(const char *name, const char *value) { int setprop(const char *name, const char *value) {
return setprop2(name, value, 1); return setprop2(name, value, 1);
} }
@ -149,7 +168,7 @@ int setprop2(const char *name, const char *value, const int trigger) {
prop_info *pi = (prop_info*) __system_property_find2(name); prop_info *pi = (prop_info*) __system_property_find2(name);
if (pi != NULL) { if (pi != NULL) {
if (trigger) { if (trigger) {
if (!strncmp(name, "ro.", 3)) deleteprop(name); if (!strncmp(name, "ro.", 3)) deleteprop(name, trigger);
ret = __system_property_set2(name, value); ret = __system_property_set2(name, value);
} else { } else {
ret = __system_property_update2(pi, value, strlen(value)); ret = __system_property_update2(pi, value, strlen(value));
@ -172,13 +191,18 @@ int setprop2(const char *name, const char *value, const int trigger) {
return ret; return ret;
} }
int deleteprop(const char *name) { int deleteprop(const char *name, const int trigger) {
if (init_resetprop()) return -1; 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_D("resetprop: delete prop: [%s] error\n", name);
return -1; return -1;
} }
if (trigger && strstr(name, "persist.")) {
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
unlink(buffer);
}
return 0; return 0;
} }
@ -273,7 +297,7 @@ int resetprop_main(int argc, char *argv[]) {
if (file) { if (file) {
return read_prop_file(filename, trigger); return read_prop_file(filename, trigger);
} else if (del) { } else if (del) {
return deleteprop(name); return deleteprop(name, trigger);
} else { } else {
return setprop2(name, value, trigger); return setprop2(name, value, trigger);
} }

View File

@ -12,8 +12,9 @@ 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);
int deleteprop(const char *name); int deleteprop(const char *name, const int trigger);
int read_prop_file(const char* filename, const int trigger); int read_prop_file(const char* filename, const int trigger);
void getprop_all(void (*cbk)(const char *name));
#ifdef __cplusplus #ifdef __cplusplus
} }