Prefer platform implementation over internal

This commit is contained in:
topjohnwu 2020-11-04 04:42:02 -08:00
parent bf650332d8
commit 1e0f96d0fd
3 changed files with 101 additions and 59 deletions

View File

@ -3,7 +3,7 @@
#include <string>
#include <functional>
int setprop(const char *name, const char *value, bool trigger = true);
int setprop(const char *name, const char *value, bool prop_svc = true);
std::string getprop(const char *name, bool persist = false);
void getprops(void (*callback)(const char *, const char *, void *),
void *cookie = nullptr, bool persist = false);

View File

@ -95,7 +95,7 @@ static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **ar
prop.name.arg = &name;
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
return false;
auto cb = reinterpret_cast<prop_cb*>(*arg);
auto cb = static_cast<prop_cb*>(*arg);
cb->exec(std::move(name), prop.value);
return true;
}
@ -186,13 +186,17 @@ string persist_getprop(const char *name) {
if (use_pb) {
auto prop = match_prop_name(name);
pb_getprop(&prop);
if (prop.value[0])
if (prop.value[0]) {
LOGD("resetprop: getprop (persist) [%s]: [%s]\n", name, prop.value);
return prop.value;
}
} else {
// Try to read from file
char value[PROP_VALUE_MAX];
if (file_getprop(name, value))
if (file_getprop(name, value)) {
LOGD("resetprop: getprop (persist) [%s]: [%s]\n", name, value);
return value;
}
}
return string();
}

View File

@ -15,8 +15,32 @@ using namespace std;
static bool verbose = false;
static int (*system_property_get)(const char*, char*);
#ifdef APPLET_STUB_MAIN
#define system_property_set __system_property_set
#define system_property_find __system_property_find
#define system_property_read_callback __system_property_read_callback
#define system_property_foreach __system_property_foreach
#define system_property_read(...)
#else
static int (*system_property_set)(const char*, const char*);
static int (*system_property_read)(const prop_info*, char*, char*);
static const prop_info *(*system_property_find)(const char*);
static void (*system_property_read_callback)(
const prop_info*, void (*)(void*, const char*, const char*, uint32_t), void*);
static int (*system_property_foreach)(void (*)(const prop_info*, void*), void*);
#define DLOAD(name) \
*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name)
static void load_functions() {
DLOAD(system_property_set);
DLOAD(system_property_read);
DLOAD(system_property_find);
DLOAD(system_property_read_callback);
DLOAD(system_property_foreach);
}
#undef DLOAD
#endif
[[noreturn]] static void usage(char* arg0) {
fprintf(stderr,
@ -73,44 +97,76 @@ illegal:
}
static void read_prop(const prop_info *pi, void *cb) {
auto callback = [](void *cb, const char *name, const char *value, uint32_t) {
if (system_property_read_callback) {
auto callback = [](void *cb, const char *name, const char *value, uint32_t) {
static_cast<prop_cb*>(cb)->exec(name, value);
};
system_property_read_callback(pi, callback, cb);
} else {
char name[PROP_NAME_MAX];
char value[PROP_VALUE_MAX];
name[0] = '\0';
value[0] = '\0';
system_property_read(pi, name, value);
static_cast<prop_cb*>(cb)->exec(name, value);
};
__system_property_read_callback(pi, callback, cb);
}
static void print_props(bool persist) {
getprops([](const char *name, const char *value, auto) {
printf("[%s]: [%s]\n", name, value);
}, nullptr, persist);
}
}
struct sysprop_stub {
virtual int setprop(const char *name, const char *value, bool trigger) { return 1; }
virtual string getprop(const char *name, bool persist) { return ""; }
virtual string getprop(const char *name, bool persist) { return string(); }
virtual void getprops(void (*callback)(const char *, const char *, void *),
void *cookie, bool persist) {}
virtual int delprop(const char *name, bool persist) { return 1; }
};
struct sysprop : public sysprop_stub {
int setprop(const char *name, const char *value, bool) override {
if (!check_legal_property_name(name))
return 1;
return system_property_set(name, value);
}
struct prop_to_string : prop_cb {
explicit prop_to_string(string &s) : val(s) {}
void exec(const char *, const char *value) override {
val = value;
}
private:
string &val;
};
string getprop(const char *name, bool) override {
char buf[PROP_VALUE_MAX];
buf[0] = '\0';
system_property_get(name, buf);
return buf;
string val;
if (!check_legal_property_name(name))
return val;
auto pi = system_property_find(name);
if (pi == nullptr)
return val;
auto prop = prop_to_string(val);
read_prop(pi, &prop);
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
return val;
}
void getprops(void (*callback)(const char*, const char*, void*), void *cookie, bool) override {
prop_list list;
prop_collector collector(list);
system_property_foreach(read_prop, &collector);
for (auto &[key, val] : list)
callback(key.data(), val.data(), cookie);
}
};
struct resetprop : public sysprop_stub {
struct resetprop : public sysprop {
int setprop(const char *name, const char *value, bool prop_svc) override {
if (!check_legal_property_name(name))
return 1;
const char *msg = prop_svc ? "property_service" : "modifying prop data structure";
int ret;
auto pi = const_cast<prop_info *>(__system_property_find(name));
if (pi != nullptr) {
@ -132,45 +188,25 @@ struct resetprop : public sysprop_stub {
}
if (ret)
LOGE("resetprop: setprop error\n");
LOGW("resetprop: setprop error\n");
return ret;
}
struct prop_to_string : prop_cb {
explicit prop_to_string(string &s) : val(s) {}
void exec(const char *name, const char *value) override {
val = value;
}
private:
string &val;
};
string getprop(const char *name, bool persist) override {
if (!check_legal_property_name(name))
return string();
auto pi = __system_property_find(name);
if (pi == nullptr) {
if (persist && strncmp(name, "persist.", 8) == 0) {
if (auto value = persist_getprop(name); !value.empty())
return value;
}
string val = sysprop::getprop(name, persist);
if (val.empty() && persist && strncmp(name, "persist.", 8) == 0)
val = persist_getprop(name);
if (val.empty())
LOGD("resetprop: prop [%s] does not exist\n", name);
return string();
} else {
string val;
auto prop = prop_to_string(val);
read_prop(pi, &prop);
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
return val;
}
return val;
}
void getprops(void (*callback)(const char *, const char *, void *),
void *cookie, bool persist) override {
prop_list list;
prop_collector collector(list);
__system_property_foreach(read_prop, &collector);
system_property_foreach(read_prop, &collector);
if (persist)
persist_getprops(&collector);
for (auto &[key, val] : list)
@ -187,9 +223,6 @@ struct resetprop : public sysprop_stub {
}
};
#define DLOAD(name) \
*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name)
static sysprop_stub *get_impl() {
static sysprop_stub *impl = nullptr;
if (impl == nullptr) {
@ -200,13 +233,12 @@ static sysprop_stub *get_impl() {
exit(1);
}
impl = new resetprop();
system_property_set = &__system_property_set;
#else
DLOAD(system_property_set); /* Always prefer to use setprop from platform */
// Load platform implementations
load_functions();
if (__system_properties_init()) {
LOGW("resetprop: __system_properties_init error\n");
DLOAD(system_property_get);
impl = system_property_get ? new sysprop() : new sysprop_stub();
impl = new sysprop();
} else {
impl = new resetprop();
}
@ -215,9 +247,15 @@ static sysprop_stub *get_impl() {
return impl;
}
/*********************************
* Implementation of public APIs
*********************************/
/***********************************
* Implementation of top-level APIs
***********************************/
static void print_props(bool persist) {
getprops([](const char *name, const char *value, auto) {
printf("[%s]: [%s]\n", name, value);
}, nullptr, persist);
}
string getprop(const char *name, bool persist) {
return get_impl()->getprop(name, persist);
@ -227,8 +265,8 @@ void getprops(void (*callback)(const char *, const char *, void *), void *cookie
get_impl()->getprops(callback, cookie, persist);
}
int setprop(const char *name, const char *value, bool trigger) {
return get_impl()->setprop(name, value, trigger);
int setprop(const char *name, const char *value, bool prop_svc) {
return get_impl()->setprop(name, value, prop_svc);
}
int delprop(const char *name, bool persist) {