Move resetprop under core

This commit is contained in:
topjohnwu
2023-05-18 22:15:49 -07:00
parent 1d2145b1b7
commit f8d62a4b6c
9 changed files with 16 additions and 22 deletions

View File

@@ -0,0 +1,240 @@
#include <pb.h>
#include <pb_decode.h>
#include <pb_encode.h>
#include <base.hpp>
#include "resetprop.hpp"
using namespace std;
/* ***********************************************************************
* Auto generated header and field definitions compiled from
* https://android.googlesource.com/platform/system/core/+/master/init/persistent_properties.proto
* Generated with Nanopb: https://github.com/nanopb/nanopb
* ***********************************************************************/
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.3 */
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
/* Struct definitions */
struct PersistentProperties {
pb_callback_t properties;
};
struct PersistentProperties_PersistentPropertyRecord {
pb_callback_t name;
bool has_value;
char value[92];
};
/* Initializer values for message structs */
#define PersistentProperties_init_default {{{NULL}, NULL}}
#define PersistentProperties_PersistentPropertyRecord_init_default {{{NULL}, NULL}, false, ""}
#define PersistentProperties_init_zero {{{NULL}, NULL}}
#define PersistentProperties_PersistentPropertyRecord_init_zero {{{NULL}, NULL}, false, ""}
/* Field tags (for use in manual encoding/decoding) */
#define PersistentProperties_properties_tag 1
#define PersistentProperties_PersistentPropertyRecord_name_tag 1
#define PersistentProperties_PersistentPropertyRecord_value_tag 2
/* Struct field encoding specification for nanopb */
#define PersistentProperties_FIELDLIST(X, a) \
X(a, CALLBACK, REPEATED, MESSAGE, properties, 1)
#define PersistentProperties_CALLBACK pb_default_field_callback
#define PersistentProperties_DEFAULT NULL
#define PersistentProperties_properties_MSGTYPE PersistentProperties_PersistentPropertyRecord
#define PersistentProperties_PersistentPropertyRecord_FIELDLIST(X, a) \
X(a, CALLBACK, OPTIONAL, STRING, name, 1) \
X(a, STATIC, OPTIONAL, STRING, value, 2)
#define PersistentProperties_PersistentPropertyRecord_CALLBACK pb_default_field_callback
#define PersistentProperties_PersistentPropertyRecord_DEFAULT NULL
extern const pb_msgdesc_t PersistentProperties_msg;
extern const pb_msgdesc_t PersistentProperties_PersistentPropertyRecord_msg;
/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
#define PersistentProperties_fields &PersistentProperties_msg
#define PersistentProperties_PersistentPropertyRecord_fields &PersistentProperties_PersistentPropertyRecord_msg
/* Maximum encoded size of messages (where known) */
/* PersistentProperties_size depends on runtime parameters */
/* PersistentProperties_PersistentPropertyRecord_size depends on runtime parameters */
PB_BIND(PersistentProperties, PersistentProperties, AUTO)
PB_BIND(PersistentProperties_PersistentPropertyRecord, PersistentProperties_PersistentPropertyRecord, AUTO)
/* ***************************
* End of auto generated code
* ***************************/
static bool name_decode(pb_istream_t *stream, const pb_field_t *, void **arg) {
string &name = *static_cast<string *>(*arg);
name.resize(stream->bytes_left);
return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left);
}
static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
return pb_encode_tag_for_field(stream, field) &&
pb_encode_string(stream, (const pb_byte_t *) *arg, strlen((const char *) *arg));
}
static bool prop_decode(pb_istream_t *stream, const pb_field_t *, void **arg) {
PersistentProperties_PersistentPropertyRecord prop{};
string name;
prop.name.funcs.decode = name_decode;
prop.name.arg = &name;
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
return false;
auto cb = static_cast<prop_cb*>(*arg);
cb->exec(name.data(), prop.value);
return true;
}
static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
PersistentProperties_PersistentPropertyRecord prop{};
prop.name.funcs.encode = name_encode;
prop.has_value = true;
auto &list = *static_cast<prop_list *>(*arg);
for (auto &p : list) {
if (!pb_encode_tag_for_field(stream, field))
return false;
prop.name.arg = (void *) p.first.data();
strscpy(prop.value, p.second.data(), sizeof(prop.value));
if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
return false;
}
return true;
}
static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count) {
int fd = (intptr_t)stream->state;
return xwrite(fd, buf, count) == count;
}
static pb_ostream_t create_ostream(const char *filename) {
int fd = creat(filename, 0644);
pb_ostream_t o = {
.callback = write_callback,
.state = (void*)(intptr_t)fd,
.max_size = SIZE_MAX,
.bytes_written = 0,
};
return o;
}
static void pb_getprop(prop_cb *prop_cb) {
LOGD("resetprop: decode with protobuf [" PERSISTENT_PROPERTY_DIR "/persistent_properties]\n");
PersistentProperties props = {};
props.properties.funcs.decode = prop_decode;
props.properties.arg = prop_cb;
auto m = mmap_data(PERSISTENT_PROPERTY_DIR "/persistent_properties");
pb_istream_t stream = pb_istream_from_buffer(m.buf, m.sz);
pb_decode(&stream, &PersistentProperties_msg, &props);
}
static bool file_getprop(const char *name, char *value) {
char path[4096];
ssprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return false;
LOGD("resetprop: read prop from [%s]\n", path);
value[read(fd, value, PROP_VALUE_MAX - 1)] = '\0'; // Null terminate the read value
close(fd);
return value[0] != '\0';
}
static bool check_pb() {
static bool use_pb = access(PERSISTENT_PROPERTY_DIR "/persistent_properties", R_OK) == 0;
return use_pb;
}
void persist_getprops(prop_cb *prop_cb) {
if (check_pb()) {
pb_getprop(prop_cb);
} else {
auto dir = open_dir(PERSISTENT_PROPERTY_DIR);
if (!dir) return;
for (dirent *entry; (entry = xreaddir(dir.get()));) {
char value[PROP_VALUE_MAX];
if (file_getprop(entry->d_name, value))
prop_cb->exec(entry->d_name, value);
}
}
}
struct match_prop_name : prop_cb {
explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; }
void exec(const char *name, const char *val) override {
if (std::strcmp(name, _name) == 0)
strscpy(value, val, sizeof(value));
}
char value[PROP_VALUE_MAX];
private:
const char *_name;
};
string persist_getprop(const char *name) {
if (check_pb()) {
auto prop = match_prop_name(name);
pb_getprop(&prop);
if (prop.value[0]) {
LOGD("resetprop: get prop (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)) {
LOGD("resetprop: get prop (persist) [%s]: [%s]\n", name, value);
return value;
}
}
return string();
}
bool persist_deleteprop(const char *name) {
if (check_pb()) {
prop_list list;
prop_collector collector(list);
persist_getprops(&collector);
for (auto it = list.begin(); it != list.end(); ++it) {
if (it->first == name) {
list.erase(it);
// Dump the props back
PersistentProperties props{};
pb_ostream_t ostream = create_ostream(PERSISTENT_PROPERTY_DIR
"/persistent_properties.tmp");
props.properties.funcs.encode = prop_encode;
props.properties.arg = &list;
LOGD("resetprop: encode with protobuf [" PERSISTENT_PROPERTY_DIR
"/persistent_properties.tmp]\n");
if (!pb_encode(&ostream, &PersistentProperties_msg, &props))
return false;
clone_attr(PERSISTENT_PROPERTY_DIR "/persistent_properties",
PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp");
rename(PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp",
PERSISTENT_PROPERTY_DIR "/persistent_properties");
return true;
}
}
return false;
} else {
char path[4096];
ssprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
if (unlink(path) == 0) {
LOGD("resetprop: unlink [%s]\n", path);
return true;
}
}
return false;
}

View File

@@ -0,0 +1,366 @@
#include <dlfcn.h>
#include <sys/types.h>
#include <vector>
#include <map>
#include <base.hpp>
#include "../core.hpp"
#include "resetprop.hpp"
using namespace std;
#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*);
#endif
struct PropFlags {
void setSkipSvc() { flags |= 1; }
void setPersist() { flags |= (1 << 1); }
void setContext() { flags |= (1 << 2); }
bool isSkipSvc() const { return flags & 1; }
bool isPersist() const { return flags & (1 << 1); }
bool isContext() const { return flags & (1 << 2); }
private:
uint32_t flags = 0;
};
[[noreturn]] static void usage(char* arg0) {
fprintf(stderr,
R"EOF(resetprop - System Property Manipulation Tool
Usage: %s [flags] [arguments...]
Read mode arguments:
(no arguments) print all properties
NAME get property
Write mode arguments:
NAME VALUE set property NAME as VALUE
-f,--file FILE load and set properties from FILE
-d,--delete NAME delete property
General flags:
-h,--help show this message
-v print verbose output to stderr
Read mode flags:
-Z get property context instead of value
-p also read persistent props from storage
Write mode flags:
-n set properties bypassing property_service
-p always write persistent props changes to storage
)EOF", arg0);
exit(1);
}
static bool check_legal_property_name(const char *name) {
int namelen = strlen(name);
if (namelen < 1) goto illegal;
if (name[0] == '.') goto illegal;
if (name[namelen - 1] == '.') goto illegal;
/* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
/* Don't allow ".." to appear in a property name */
for (size_t i = 0; i < namelen; i++) {
if (name[i] == '.') {
// i=0 is guaranteed to never have a dot. See above.
if (name[i-1] == '.') goto illegal;
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] >= '0' && name[i] <= '9') continue;
goto illegal;
}
return true;
illegal:
LOGE("Illegal property name: [%s]\n", name);
return false;
}
static void read_prop_with_cb(const prop_info *pi, void *cb) {
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);
}
}
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;
};
static int set_prop(const char *name, const char *value, PropFlags flags) {
if (!check_legal_property_name(name))
return 1;
const char *msg = flags.isSkipSvc() ? "direct modification" : "property_service";
auto pi = const_cast<prop_info *>(__system_property_find(name));
// Always delete existing read-only properties, because they could be
// long properties and cannot directly go through __system_property_update
if (pi != nullptr && str_starts(name, "ro.")) {
// Skip pruning nodes as we will add it back ASAP
__system_property_delete(name, false);
pi = nullptr;
}
int ret;
if (pi != nullptr) {
if (flags.isSkipSvc()) {
ret = __system_property_update(pi, value, strlen(value));
} else {
ret = system_property_set(name, value);
}
LOGD("resetprop: update prop [%s]: [%s] by %s\n", name, value, msg);
} else {
if (flags.isSkipSvc()) {
ret = __system_property_add(name, strlen(name), value, strlen(value));
} else {
ret = system_property_set(name, value);
}
LOGD("resetprop: create prop [%s]: [%s] by %s\n", name, value, msg);
}
if (ret) {
LOGW("resetprop: set prop error\n");
}
return ret;
}
static string get_prop(const char *name, PropFlags flags) {
if (!check_legal_property_name(name))
return "";
if (flags.isContext()) {
auto val = __system_property_get_context(name) ?: "";
LOGD("resetprop: prop context [%s]: [%s]\n", name, val);
return val;
}
string 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: get prop [%s]: [%s]\n", name, val.data());
if (val.empty() && flags.isPersist() && str_starts(name, "persist."))
val = persist_getprop(name);
if (val.empty())
LOGD("resetprop: prop [%s] does not exist\n", name);
return val;
}
static void print_props(PropFlags flags) {
prop_list list;
prop_collector collector(list);
system_property_foreach(read_prop_with_cb, &collector);
if (flags.isPersist())
persist_getprops(&collector);
for (auto &[key, val] : list) {
const char *v = flags.isContext() ?
(__system_property_get_context(key.data()) ?: "") :
val.data();
printf("[%s]: [%s]\n", key.data(), v);
}
}
static int delete_prop(const char *name, PropFlags flags) {
if (!check_legal_property_name(name))
return 1;
LOGD("resetprop: delete prop [%s]\n", name);
int ret = __system_property_delete(name, true);
if (flags.isPersist() && str_starts(name, "persist.")) {
if (persist_deleteprop(name))
ret = 0;
}
return ret;
}
static void load_file(const char *filename, PropFlags flags) {
LOGD("resetprop: Parse prop file [%s]\n", filename);
parse_prop_file(filename, [=](auto key, auto val) -> bool {
set_prop(key.data(), val.data(), flags);
return true;
});
}
struct Initialize {
Initialize() {
#ifndef APPLET_STUB_MAIN
#define DLOAD(name) (*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name))
// Load platform implementations
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");
}
}
};
static void InitOnce() {
struct Initialize init;
}
#define consume_next(val) \
if (argc != 2) usage(argv0); \
val = argv[1]; \
stop_parse = true; \
int resetprop_main(int argc, char *argv[]) {
PropFlags flags;
char *argv0 = argv[0];
const char *prop_file = nullptr;
const char *prop_to_rm = nullptr;
--argc;
++argv;
// Parse flags and -- options
while (argc && argv[0][0] == '-') {
bool stop_parse = false;
for (int idx = 1; true; ++idx) {
switch (argv[0][idx]) {
case '-':
if (argv[0] == "--file"sv) {
consume_next(prop_file);
} else if (argv[0] == "--delete"sv) {
consume_next(prop_to_rm);
} else {
usage(argv0);
}
break;
case 'd':
consume_next(prop_to_rm);
continue;
case 'f':
consume_next(prop_file);
continue;
case 'n':
flags.setSkipSvc();
continue;
case 'p':
flags.setPersist();
continue;
case 'v':
set_log_level_state(LogLevel::Debug, true);
continue;
case 'Z':
flags.setContext();
continue;
case '\0':
break;
default:
usage(argv0);
}
break;
}
--argc;
++argv;
if (stop_parse)
break;
}
InitOnce();
if (prop_to_rm) {
return delete_prop(prop_to_rm, flags);
}
if (prop_file) {
load_file(prop_file, flags);
return 0;
}
switch (argc) {
case 0:
print_props(flags);
return 0;
case 1: {
string val = get_prop(argv[0], flags);
if (val.empty())
return 1;
printf("%s\n", val.data());
return 0;
}
case 2:
return set_prop(argv[0], argv[1], flags);
default:
usage(argv0);
}
}
/***************
* Public APIs
****************/
string get_prop(const char *name, bool persist) {
InitOnce();
PropFlags flags;
if (persist) flags.setPersist();
return get_prop(name, flags);
}
int delete_prop(const char *name, bool persist) {
InitOnce();
PropFlags flags;
if (persist) flags.setPersist();
return delete_prop(name, flags);
}
int set_prop(const char *name, const char *value, bool skip_svc) {
InitOnce();
PropFlags flags;
if (skip_svc) flags.setSkipSvc();
return set_prop(name, value, flags);
}
void load_prop_file(const char *filename, bool skip_svc) {
InitOnce();
PropFlags flags;
if (skip_svc) flags.setSkipSvc();
load_file(filename, flags);
}

View File

@@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <map>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <api/_system_properties.h>
#define PERSISTENT_PROPERTY_DIR "/data/property"
struct prop_cb {
virtual void exec(const char *name, const char *value) = 0;
};
using prop_list = std::map<std::string, std::string>;
struct prop_collector : prop_cb {
explicit prop_collector(prop_list &list) : list(list) {}
void exec(const char *name, const char *value) override {
list.insert_or_assign(name, value);
}
private:
prop_list &list;
};
std::string persist_getprop(const char *name);
void persist_getprops(prop_cb *prop_cb);
bool persist_deleteprop(const char *name);