Remove sysprop fallback

The library now supports mapping as ro
This commit is contained in:
topjohnwu 2023-05-18 15:47:50 -07:00
parent f36b21bae5
commit 17569005a4

View File

@ -11,14 +11,11 @@
using namespace std; using namespace std;
#ifdef APPLET_STUB_MAIN #ifdef APPLET_STUB_MAIN
#define system_property_set __system_property_set #define system_property_set __system_property_set
#define system_property_find __system_property_find #define system_property_find __system_property_find
#define system_property_read_callback __system_property_read_callback #define system_property_read_callback __system_property_read_callback
#define system_property_foreach __system_property_foreach #define system_property_foreach __system_property_foreach
#define system_property_read(...) #define system_property_read(...)
extern "C" int fsetxattr(int fd, const char* name, const void* value, size_t size, int flags) {
return syscall(__NR_fsetxattr, fd, name, value, size, flags);
}
#else #else
static int (*system_property_set)(const char*, const char*); static int (*system_property_set)(const char*, const char*);
static int (*system_property_read)(const prop_info*, char*, char*); static int (*system_property_read)(const prop_info*, char*, char*);
@ -26,18 +23,6 @@ static const prop_info *(*system_property_find)(const char*);
static void (*system_property_read_callback)( static void (*system_property_read_callback)(
const prop_info*, void (*)(void*, const char*, const char*, uint32_t), void*); const prop_info*, void (*)(void*, const char*, const char*, uint32_t), void*);
static int (*system_property_foreach)(void (*)(const prop_info*, void*), 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 #endif
[[noreturn]] static void usage(char* arg0) { [[noreturn]] static void usage(char* arg0) {
@ -96,7 +81,7 @@ illegal:
return false; return false;
} }
static void read_prop(const prop_info *pi, void *cb) { static void read_prop_with_cb(const prop_info *pi, void *cb) {
if (system_property_read_callback) { if (system_property_read_callback) {
auto callback = [](void *cb, const char *name, const char *value, uint32_t) { auto callback = [](void *cb, const char *name, const char *value, uint32_t) {
static_cast<prop_cb*>(cb)->exec(name, value); static_cast<prop_cb*>(cb)->exec(name, value);
@ -112,57 +97,37 @@ static void read_prop(const prop_info *pi, void *cb) {
} }
} }
struct sysprop_stub { struct prop_to_string : prop_cb {
virtual int setprop(const char *name, const char *value, bool trigger) { return 1; } explicit prop_to_string(string &s) : val(s) {}
virtual string getprop(const char *name, bool persist) { return string(); } void exec(const char *, const char *value) override {
virtual string getcontext(const char *name) { return string(); } val = value;
virtual void getprops(void (*callback)(const char *, const char *, void *), }
void *cookie, bool persist) {} private:
virtual int delprop(const char *name, bool persist) { return 1; } string &val;
}; };
struct sysprop : public sysprop_stub { struct resetprop {
int setprop(const char *name, const char *value, bool) override { resetprop() {
if (!check_legal_property_name(name))
return 1;
return system_property_set(name, value);
}
struct prop_to_string : prop_cb { #ifndef APPLET_STUB_MAIN
explicit prop_to_string(string &s) : val(s) {} #define DLOAD(name) (*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name))
void exec(const char *, const char *value) override { // Load platform implementations
val = value; DLOAD(system_property_set);
DLOAD(system_property_read);
DLOAD(system_property_find);
DLOAD(system_property_read_callback);
DLOAD(system_property_foreach);
#undef DLOAD
#endif
if (__system_properties_init()) {
LOGE("resetprop: __system_properties_init error\n");
} }
private:
string &val;
};
string getprop(const char *name, bool) override {
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 { int setprop(const char *name, const char *value, bool prop_svc) {
int setprop(const char *name, const char *value, bool prop_svc) override {
if (!check_legal_property_name(name)) if (!check_legal_property_name(name))
return 1; return 1;
@ -200,8 +165,17 @@ struct resetprop : public sysprop {
return ret; return ret;
} }
string getprop(const char *name, bool persist) override { string getprop(const char *name, bool persist) {
string val = sysprop::getprop(name, persist); string val;
if (!check_legal_property_name(name))
return val;
auto pi = system_property_find(name);
if (pi == nullptr)
return val;
auto cb = prop_to_string(val);
read_prop_with_cb(pi, &cb);
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
if (val.empty() && persist && strncmp(name, "persist.", 8) == 0) if (val.empty() && persist && strncmp(name, "persist.", 8) == 0)
val = persist_getprop(name); val = persist_getprop(name);
if (val.empty()) if (val.empty())
@ -209,23 +183,19 @@ struct resetprop : public sysprop {
return val; return val;
} }
string getcontext(const char *name) override { string getcontext(const char *name) {
if (!check_legal_property_name(name)) if (!check_legal_property_name(name))
return ""; return "";
auto val = __system_property_get_context(name); auto val = __system_property_get_context(name) ?: "";
if (val == nullptr) {
LOGD("resetprop: prop [%s] does not support context\n", name);
return "";
}
LOGD("resetprop: getcontext [%s]: [%s]\n", name, val); LOGD("resetprop: getcontext [%s]: [%s]\n", name, val);
return val; return val;
} }
void getprops(void (*callback)(const char *, const char *, void *), void getprops(void (*callback)(const char *, const char *, void *),
void *cookie, bool persist) override { void *cookie, bool persist) {
prop_list list; prop_list list;
prop_collector collector(list); prop_collector collector(list);
system_property_foreach(read_prop, &collector); system_property_foreach(read_prop_with_cb, &collector);
if (persist) if (persist)
persist_getprops(&collector); persist_getprops(&collector);
for (auto &[key, val] : list) for (auto &[key, val] : list)
@ -233,7 +203,7 @@ struct resetprop : public sysprop {
} }
// Not an error when something is deleted // Not an error when something is deleted
int delprop(const char *name, bool persist) override { int delprop(const char *name, bool persist) {
if (!check_legal_property_name(name)) if (!check_legal_property_name(name))
return 1; return 1;
LOGD("resetprop: delete prop [%s]\n", name); LOGD("resetprop: delete prop [%s]\n", name);
@ -247,26 +217,8 @@ struct resetprop : public sysprop {
} }
}; };
static sysprop_stub *get_impl() { static resetprop *get_impl() {
static sysprop_stub *impl = nullptr; static resetprop *impl = new resetprop();
if (impl == nullptr) {
#ifdef APPLET_STUB_MAIN
if (__system_properties_init()) {
LOGE("resetprop: __system_properties_init error\n");
exit(1);
}
impl = new resetprop();
#else
// Load platform implementations
load_functions();
if (__system_properties_init()) {
LOGW("resetprop: __system_properties_init error\n");
impl = new sysprop();
} else {
impl = new resetprop();
}
#endif
}
return impl; return impl;
} }