Update nanopb
This commit is contained in:
parent
f32e0af830
commit
bf650332d8
2
native/jni/external/nanopb
vendored
2
native/jni/external/nanopb
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 3c69a905b16df149e1bda12f25e0522073a24678
|
Subproject commit 1466e6f953835b191a7f5acf0c06c941d4cd33d9
|
@ -8,4 +8,4 @@ std::string getprop(const char *name, bool persist = false);
|
|||||||
void getprops(void (*callback)(const char *, const char *, void *),
|
void getprops(void (*callback)(const char *, const char *, void *),
|
||||||
void *cookie = nullptr, bool persist = false);
|
void *cookie = nullptr, bool persist = false);
|
||||||
int delprop(const char *name, bool persist = false);
|
int delprop(const char *name, bool persist = false);
|
||||||
void load_prop_file(const char *filename, bool trigger = true);
|
void load_prop_file(const char *filename, bool prop_svc = true);
|
||||||
|
@ -8,41 +8,30 @@
|
|||||||
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
||||||
|
|
||||||
struct prop_cb {
|
struct prop_cb {
|
||||||
virtual void exec(const char *name, const char *value) = 0;
|
virtual void exec(const char *name, const char *value) {
|
||||||
};
|
exec(std::string(name), value);
|
||||||
|
}
|
||||||
template<class T>
|
virtual void exec(std::string &&name, const char *value) {
|
||||||
struct prop_cb_impl : public prop_cb {
|
exec(name.data(), value);
|
||||||
typedef void (*callback_type)(const char *, const char *, T&);
|
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wdangling-field" // Dangling field is expected
|
|
||||||
prop_cb_impl(T &arg, callback_type fn) : arg(arg), fn(fn) {}
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
|
|
||||||
void exec(const char *name, const char *value) override {
|
|
||||||
fn(name, value, arg);
|
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
T &arg;
|
|
||||||
callback_type fn;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern bool use_pb;
|
extern bool use_pb;
|
||||||
|
|
||||||
using prop_list = std::map<std::string_view, std::string>;
|
using prop_list = std::map<std::string, std::string>;
|
||||||
|
|
||||||
struct prop_collector : prop_cb_impl<prop_list> {
|
struct prop_collector : prop_cb {
|
||||||
prop_collector(prop_list &list) :
|
explicit prop_collector(prop_list &list) : list(list) {}
|
||||||
prop_cb_impl<prop_list>(list, [](auto name, auto val, auto list){ list.emplace(name, val); }) {}
|
void exec(const char *name, const char *value) override {
|
||||||
|
list.insert_or_assign(name, value);
|
||||||
|
}
|
||||||
|
void exec(std::string &&name, const char *value) override {
|
||||||
|
list.insert_or_assign(std::move(name), value);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
prop_list &list;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T, class Func>
|
|
||||||
prop_cb_impl<T> make_prop_cb(T &arg, Func f) {
|
|
||||||
return prop_cb_impl<T>(arg, f);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string persist_getprop(const char *name);
|
std::string persist_getprop(const char *name);
|
||||||
void persist_getprops(prop_cb *prop_cb);
|
void persist_getprops(prop_cb *prop_cb);
|
||||||
bool persist_deleteprop(const char *name);
|
bool persist_deleteprop(const char *name);
|
||||||
void persist_cleanup();
|
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <pb.h>
|
#include <pb.h>
|
||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
|
|
||||||
#include <utils.hpp>
|
#include <utils.hpp>
|
||||||
|
|
||||||
#include "_resetprop.hpp"
|
#include "_resetprop.hpp"
|
||||||
@ -16,34 +9,28 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/* ***********************************************************************
|
/* ***********************************************************************
|
||||||
* Auto generated header and constant definitions compiled from
|
* Auto generated header and field definitions compiled from
|
||||||
* android/platform/system/core/master/init/persistent_properties.proto
|
* https://android.googlesource.com/platform/system/core/+/master/init/persistent_properties.proto
|
||||||
* using Nanopb's protoc
|
* Generated with Nanopb: https://github.com/nanopb/nanopb
|
||||||
* Nanopb: https://github.com/nanopb/nanopb
|
|
||||||
* ***********************************************************************/
|
* ***********************************************************************/
|
||||||
|
|
||||||
/* Automatically generated nanopb header */
|
/* Automatically generated nanopb header */
|
||||||
/* Generated by nanopb-0.3.9.1 at Sun Apr 22 14:36:22 2018. */
|
/* Generated by nanopb-0.4.3 */
|
||||||
|
|
||||||
/* @@protoc_insertion_point(includes) */
|
#if PB_PROTO_HEADER_VERSION != 40
|
||||||
#if PB_PROTO_HEADER_VERSION != 30
|
|
||||||
#error Regenerate this file with the current version of nanopb generator.
|
#error Regenerate this file with the current version of nanopb generator.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Struct definitions */
|
/* Struct definitions */
|
||||||
typedef struct _PersistentProperties {
|
struct PersistentProperties {
|
||||||
pb_callback_t properties;
|
pb_callback_t properties;
|
||||||
/* @@protoc_insertion_point(struct:PersistentProperties) */
|
};
|
||||||
} PersistentProperties;
|
|
||||||
|
|
||||||
typedef struct _PersistentProperties_PersistentPropertyRecord {
|
struct PersistentProperties_PersistentPropertyRecord {
|
||||||
pb_callback_t name;
|
pb_callback_t name;
|
||||||
bool has_value;
|
bool has_value;
|
||||||
char value[92];
|
char value[92];
|
||||||
/* @@protoc_insertion_point(struct:PersistentProperties_PersistentPropertyRecord) */
|
};
|
||||||
} PersistentProperties_PersistentPropertyRecord;
|
|
||||||
|
|
||||||
/* Default values for struct fields */
|
|
||||||
|
|
||||||
/* Initializer values for message structs */
|
/* Initializer values for message structs */
|
||||||
#define PersistentProperties_init_default {{{NULL}, NULL}}
|
#define PersistentProperties_init_default {{{NULL}, NULL}}
|
||||||
@ -56,33 +43,33 @@ typedef struct _PersistentProperties_PersistentPropertyRecord {
|
|||||||
#define PersistentProperties_PersistentPropertyRecord_name_tag 1
|
#define PersistentProperties_PersistentPropertyRecord_name_tag 1
|
||||||
#define PersistentProperties_PersistentPropertyRecord_value_tag 2
|
#define PersistentProperties_PersistentPropertyRecord_value_tag 2
|
||||||
|
|
||||||
/* Automatically generated nanopb constant definitions */
|
|
||||||
/* Generated by nanopb-0.3.9.1 at Sun Apr 22 14:36:22 2018. */
|
|
||||||
|
|
||||||
/* Struct field encoding specification for nanopb */
|
/* Struct field encoding specification for nanopb */
|
||||||
const pb_field_t PersistentProperties_PersistentPropertyRecord_fields[3] = {
|
#define PersistentProperties_FIELDLIST(X, a) \
|
||||||
PB_FIELD( 1, STRING , OPTIONAL, CALLBACK, FIRST, PersistentProperties_PersistentPropertyRecord, name, name, 0),
|
X(a, CALLBACK, REPEATED, MESSAGE, properties, 1)
|
||||||
PB_FIELD( 2, STRING , OPTIONAL, STATIC , OTHER, PersistentProperties_PersistentPropertyRecord, value, name, 0),
|
#define PersistentProperties_CALLBACK pb_default_field_callback
|
||||||
PB_LAST_FIELD
|
#define PersistentProperties_DEFAULT NULL
|
||||||
};
|
#define PersistentProperties_properties_MSGTYPE PersistentProperties_PersistentPropertyRecord
|
||||||
|
|
||||||
const pb_field_t PersistentProperties_fields[2] = {
|
#define PersistentProperties_PersistentPropertyRecord_FIELDLIST(X, a) \
|
||||||
PB_FIELD( 1, MESSAGE , REPEATED, CALLBACK, FIRST, PersistentProperties, properties, properties, &PersistentProperties_PersistentPropertyRecord_fields),
|
X(a, CALLBACK, OPTIONAL, STRING, name, 1) \
|
||||||
PB_LAST_FIELD
|
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) */
|
/* Maximum encoded size of messages (where known) */
|
||||||
/* PersistentProperties_size depends on runtime parameters */
|
/* PersistentProperties_size depends on runtime parameters */
|
||||||
/* PersistentProperties_PersistentPropertyRecord_size depends on runtime parameters */
|
/* PersistentProperties_PersistentPropertyRecord_size depends on runtime parameters */
|
||||||
|
|
||||||
/* Message IDs (where set with "msgid" option) */
|
PB_BIND(PersistentProperties, PersistentProperties, AUTO)
|
||||||
#ifdef PB_MSGID
|
|
||||||
|
|
||||||
#define PROPS_MESSAGES \
|
|
||||||
|
|
||||||
#endif
|
|
||||||
/* @@protoc_insertion_point(eof) */
|
|
||||||
|
|
||||||
|
PB_BIND(PersistentProperties_PersistentPropertyRecord, PersistentProperties_PersistentPropertyRecord, AUTO)
|
||||||
|
|
||||||
/* ***************************
|
/* ***************************
|
||||||
* End of auto generated code
|
* End of auto generated code
|
||||||
@ -90,27 +77,10 @@ const pb_field_t PersistentProperties_fields[2] = {
|
|||||||
|
|
||||||
bool use_pb = false;
|
bool use_pb = false;
|
||||||
|
|
||||||
#ifdef APPLET_STUB_MAIN
|
|
||||||
struct {
|
|
||||||
void push_back(...){};
|
|
||||||
} allocs;
|
|
||||||
void persist_cleanup(){}
|
|
||||||
#else
|
|
||||||
static thread_local vector<void *> allocs;
|
|
||||||
void persist_cleanup() {
|
|
||||||
std::for_each(allocs.begin(), allocs.end(), [](auto p){ free(p); });
|
|
||||||
allocs.clear();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static bool name_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
static bool name_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
||||||
auto name = (pb_byte_t *) malloc(stream->bytes_left + 1);
|
string &name = *static_cast<string *>(*arg);
|
||||||
allocs.push_back(name); /* Record all mallocs to cleanup */
|
name.resize(stream->bytes_left);
|
||||||
name[stream->bytes_left] = '\0';
|
return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left);
|
||||||
if (!pb_read(stream, name, stream->bytes_left))
|
|
||||||
return false;
|
|
||||||
*arg = name;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
||||||
@ -119,25 +89,28 @@ static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * co
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
||||||
PersistentProperties_PersistentPropertyRecord prop = {};
|
PersistentProperties_PersistentPropertyRecord prop{};
|
||||||
|
string name;
|
||||||
prop.name.funcs.decode = name_decode;
|
prop.name.funcs.decode = name_decode;
|
||||||
if (!pb_decode(stream, PersistentProperties_PersistentPropertyRecord_fields, &prop))
|
prop.name.arg = &name;
|
||||||
|
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||||
return false;
|
return false;
|
||||||
reinterpret_cast<prop_cb*>(*arg)->exec((const char *) prop.name.arg, prop.value);
|
auto cb = reinterpret_cast<prop_cb*>(*arg);
|
||||||
|
cb->exec(std::move(name), prop.value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
||||||
PersistentProperties_PersistentPropertyRecord prop = {};
|
PersistentProperties_PersistentPropertyRecord prop{};
|
||||||
prop.name.funcs.encode = name_encode;
|
prop.name.funcs.encode = name_encode;
|
||||||
prop.has_value = true;
|
prop.has_value = true;
|
||||||
auto &list = *(prop_list *) *arg;
|
auto &list = *static_cast<prop_list *>(*arg);
|
||||||
for (auto &p : list) {
|
for (auto &p : list) {
|
||||||
if (!pb_encode_tag_for_field(stream, field))
|
if (!pb_encode_tag_for_field(stream, field))
|
||||||
return false;
|
return false;
|
||||||
prop.name.arg = (void *) p.first.data();
|
prop.name.arg = (void *) p.first.data();
|
||||||
strcpy(prop.value, p.second.data());
|
strcpy(prop.value, p.second.data());
|
||||||
if (!pb_encode_submessage(stream, PersistentProperties_PersistentPropertyRecord_fields, &prop))
|
if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -168,18 +141,18 @@ static void pb_getprop(prop_cb *prop_cb) {
|
|||||||
size_t size;
|
size_t size;
|
||||||
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", buf, size);
|
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", buf, size);
|
||||||
pb_istream_t stream = pb_istream_from_buffer(buf, size);
|
pb_istream_t stream = pb_istream_from_buffer(buf, size);
|
||||||
pb_decode(&stream, PersistentProperties_fields, &props);
|
pb_decode(&stream, &PersistentProperties_msg, &props);
|
||||||
munmap(buf, size);
|
munmap(buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool file_getprop(const char *name, char *value) {
|
static bool file_getprop(const char *name, char *value) {
|
||||||
char path[PATH_MAX];
|
char path[4096];
|
||||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return false;
|
return false;
|
||||||
LOGD("resetprop: read prop from [%s]\n", path);
|
LOGD("resetprop: read prop from [%s]\n", path);
|
||||||
value[read(fd, value, PROP_VALUE_MAX)] = '\0'; // Null terminate the read value
|
value[read(fd, value, PROP_VALUE_MAX - 1)] = '\0'; // Null terminate the read value
|
||||||
close(fd);
|
close(fd);
|
||||||
return value[0] != '\0';
|
return value[0] != '\0';
|
||||||
}
|
}
|
||||||
@ -188,11 +161,9 @@ void persist_getprops(prop_cb *prop_cb) {
|
|||||||
if (use_pb) {
|
if (use_pb) {
|
||||||
pb_getprop(prop_cb);
|
pb_getprop(prop_cb);
|
||||||
} else {
|
} else {
|
||||||
DIR *dir = opendir(PERSISTENT_PROPERTY_DIR);
|
auto dir = open_dir(PERSISTENT_PROPERTY_DIR);
|
||||||
struct dirent *entry;
|
if (!dir) return;
|
||||||
while ((entry = readdir(dir))) {
|
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 )
|
|
||||||
continue;
|
|
||||||
char value[PROP_VALUE_MAX];
|
char value[PROP_VALUE_MAX];
|
||||||
if (file_getprop(entry->d_name, value))
|
if (file_getprop(entry->d_name, value))
|
||||||
prop_cb->exec(entry->d_name, value);
|
prop_cb->exec(entry->d_name, value);
|
||||||
@ -200,20 +171,21 @@ void persist_getprops(prop_cb *prop_cb) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct match_prop_name : prop_cb {
|
||||||
|
explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; }
|
||||||
|
void exec(string &&name, const char *val) override {
|
||||||
|
if (name == _name)
|
||||||
|
strcpy(value, val);
|
||||||
|
}
|
||||||
|
char value[PROP_VALUE_MAX];
|
||||||
|
private:
|
||||||
|
const char *_name;
|
||||||
|
};
|
||||||
|
|
||||||
string persist_getprop(const char *name) {
|
string persist_getprop(const char *name) {
|
||||||
if (use_pb) {
|
if (use_pb) {
|
||||||
run_finally f([]{ persist_cleanup(); });
|
auto prop = match_prop_name(name);
|
||||||
struct {
|
pb_getprop(&prop);
|
||||||
const char *name;
|
|
||||||
char value[PROP_VALUE_MAX];
|
|
||||||
} prop;
|
|
||||||
prop.name = name;
|
|
||||||
prop.value[0] = '\0';
|
|
||||||
auto reader = make_prop_cb(prop, [](auto name, auto value, auto prop) {
|
|
||||||
if (strcmp(name, prop.name) == 0)
|
|
||||||
strcpy(prop.value, value);
|
|
||||||
});
|
|
||||||
pb_getprop(&reader);
|
|
||||||
if (prop.value[0])
|
if (prop.value[0])
|
||||||
return prop.value;
|
return prop.value;
|
||||||
} else {
|
} else {
|
||||||
@ -227,7 +199,6 @@ string persist_getprop(const char *name) {
|
|||||||
|
|
||||||
bool persist_deleteprop(const char *name) {
|
bool persist_deleteprop(const char *name) {
|
||||||
if (use_pb) {
|
if (use_pb) {
|
||||||
run_finally f([]{ persist_cleanup(); });
|
|
||||||
prop_list list;
|
prop_list list;
|
||||||
prop_collector collector(list);
|
prop_collector collector(list);
|
||||||
persist_getprops(&collector);
|
persist_getprops(&collector);
|
||||||
@ -236,14 +207,14 @@ bool persist_deleteprop(const char *name) {
|
|||||||
if (it->first == name) {
|
if (it->first == name) {
|
||||||
list.erase(it);
|
list.erase(it);
|
||||||
// Dump the props back
|
// Dump the props back
|
||||||
PersistentProperties props = PersistentProperties_init_zero;
|
PersistentProperties props{};
|
||||||
pb_ostream_t ostream = create_ostream(PERSISTENT_PROPERTY_DIR
|
pb_ostream_t ostream = create_ostream(PERSISTENT_PROPERTY_DIR
|
||||||
"/persistent_properties.tmp");
|
"/persistent_properties.tmp");
|
||||||
props.properties.funcs.encode = prop_encode;
|
props.properties.funcs.encode = prop_encode;
|
||||||
props.properties.arg = &list;
|
props.properties.arg = &list;
|
||||||
LOGD("resetprop: encode with protobuf [" PERSISTENT_PROPERTY_DIR
|
LOGD("resetprop: encode with protobuf [" PERSISTENT_PROPERTY_DIR
|
||||||
"/persistent_properties.tmp]\n");
|
"/persistent_properties.tmp]\n");
|
||||||
if (!pb_encode(&ostream, PersistentProperties_fields, &props))
|
if (!pb_encode(&ostream, &PersistentProperties_msg, &props))
|
||||||
return false;
|
return false;
|
||||||
clone_attr(PERSISTENT_PROPERTY_DIR "/persistent_properties",
|
clone_attr(PERSISTENT_PROPERTY_DIR "/persistent_properties",
|
||||||
PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp");
|
PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp");
|
||||||
@ -254,7 +225,7 @@ bool persist_deleteprop(const char *name) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
char path[PATH_MAX];
|
char path[4096];
|
||||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||||
if (unlink(path) == 0) {
|
if (unlink(path) == 0) {
|
||||||
LOGD("resetprop: unlink [%s]\n", path);
|
LOGD("resetprop: unlink [%s]\n", path);
|
||||||
|
@ -67,25 +67,25 @@ static bool check_legal_property_name(const char *name) {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
illegal:
|
illegal:
|
||||||
LOGE("Illegal property name: [%s]\n", name);
|
LOGE("Illegal property name: [%s]\n", name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_prop(const prop_info *pi, void *cb) {
|
static void read_prop(const prop_info *pi, void *cb) {
|
||||||
__system_property_read_callback(
|
auto callback = [](void *cb, const char *name, const char *value, uint32_t) {
|
||||||
pi, [](auto cb, auto name, auto value, auto) {
|
static_cast<prop_cb*>(cb)->exec(name, value);
|
||||||
reinterpret_cast<prop_cb*>(cb)->exec(name, value);
|
};
|
||||||
}, cb);
|
__system_property_read_callback(pi, callback, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_props(bool persist) {
|
static void print_props(bool persist) {
|
||||||
getprops([](auto name, auto value, auto) {
|
getprops([](const char *name, const char *value, auto) {
|
||||||
printf("[%s]: [%s]\n", name, value);
|
printf("[%s]: [%s]\n", name, value);
|
||||||
}, nullptr, persist);
|
}, nullptr, persist);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sysprop {
|
struct sysprop_stub {
|
||||||
virtual int setprop(const char *name, const char *value, bool trigger) { return 1; }
|
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 ""; }
|
||||||
virtual void getprops(void (*callback)(const char *, const char *, void *),
|
virtual void getprops(void (*callback)(const char *, const char *, void *),
|
||||||
@ -93,7 +93,7 @@ struct sysprop {
|
|||||||
virtual int delprop(const char *name, bool persist) { return 1; }
|
virtual int delprop(const char *name, bool persist) { return 1; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct stock_sysprop : public sysprop {
|
struct sysprop : public sysprop_stub {
|
||||||
int setprop(const char *name, const char *value, bool) override {
|
int setprop(const char *name, const char *value, bool) override {
|
||||||
return system_property_set(name, value);
|
return system_property_set(name, value);
|
||||||
}
|
}
|
||||||
@ -105,31 +105,30 @@ struct stock_sysprop : public sysprop {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct resetprop : public sysprop {
|
struct resetprop : public sysprop_stub {
|
||||||
int setprop(const char *name, const char *value, bool trigger) override {
|
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;
|
||||||
|
|
||||||
|
const char *msg = prop_svc ? "property_service" : "modifying prop data structure";
|
||||||
int ret;
|
int ret;
|
||||||
auto pi = const_cast<prop_info *>(__system_property_find(name));
|
auto pi = const_cast<prop_info *>(__system_property_find(name));
|
||||||
if (pi != nullptr) {
|
if (pi != nullptr) {
|
||||||
if (trigger) {
|
if (prop_svc) {
|
||||||
if (strncmp(name, "ro.", 3) == 0)
|
if (strncmp(name, "ro.", 3) == 0)
|
||||||
delprop(name, false);
|
delprop(name, false);
|
||||||
ret = system_property_set(name, value);
|
ret = system_property_set(name, value);
|
||||||
} else {
|
} else {
|
||||||
ret = __system_property_update(pi, value, strlen(value));
|
ret = __system_property_update(pi, value, strlen(value));
|
||||||
}
|
}
|
||||||
LOGD("resetprop: update prop [%s]: [%s] by %s\n", name, value,
|
LOGD("resetprop: update prop [%s]: [%s] by %s\n", name, value, msg);
|
||||||
trigger ? "property_service" : "modifying prop data structure");
|
|
||||||
} else {
|
} else {
|
||||||
if (trigger) {
|
if (prop_svc) {
|
||||||
ret = system_property_set(name, value);
|
ret = system_property_set(name, value);
|
||||||
} else {
|
} else {
|
||||||
ret = __system_property_add(name, strlen(name), value, strlen(value));
|
ret = __system_property_add(name, strlen(name), value, strlen(value));
|
||||||
}
|
}
|
||||||
LOGD("resetprop: create prop [%s]: [%s] by %s\n", name, value,
|
LOGD("resetprop: create prop [%s]: [%s] by %s\n", name, value, msg);
|
||||||
trigger ? "property_service" : "modifying prop data structure");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret)
|
if (ret)
|
||||||
@ -138,24 +137,30 @@ struct resetprop : public sysprop {
|
|||||||
return ret;
|
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 {
|
string getprop(const char *name, bool persist) override {
|
||||||
if (!check_legal_property_name(name))
|
if (!check_legal_property_name(name))
|
||||||
return string();
|
return string();
|
||||||
auto pi = __system_property_find(name);
|
auto pi = __system_property_find(name);
|
||||||
if (pi == nullptr) {
|
if (pi == nullptr) {
|
||||||
if (persist && strncmp(name, "persist.", 8) == 0) {
|
if (persist && strncmp(name, "persist.", 8) == 0) {
|
||||||
auto value = persist_getprop(name);
|
if (auto value = persist_getprop(name); !value.empty())
|
||||||
if (value.empty())
|
return value;
|
||||||
goto not_found;
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
not_found:
|
|
||||||
LOGD("resetprop: prop [%s] does not exist\n", name);
|
LOGD("resetprop: prop [%s] does not exist\n", name);
|
||||||
return string();
|
return string();
|
||||||
} else {
|
} else {
|
||||||
string val;
|
string val;
|
||||||
auto reader = make_prop_cb(val, [](auto, auto value, auto str){ str = value; });
|
auto prop = prop_to_string(val);
|
||||||
read_prop(pi, &reader);
|
read_prop(pi, &prop);
|
||||||
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
|
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
@ -170,8 +175,6 @@ struct resetprop : public sysprop {
|
|||||||
persist_getprops(&collector);
|
persist_getprops(&collector);
|
||||||
for (auto &[key, val] : list)
|
for (auto &[key, val] : list)
|
||||||
callback(key.data(), val.data(), cookie);
|
callback(key.data(), val.data(), cookie);
|
||||||
if (persist)
|
|
||||||
persist_cleanup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int delprop(const char *name, bool persist) override {
|
int delprop(const char *name, bool persist) override {
|
||||||
@ -187,8 +190,8 @@ struct resetprop : public sysprop {
|
|||||||
#define DLOAD(name) \
|
#define DLOAD(name) \
|
||||||
*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name)
|
*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name)
|
||||||
|
|
||||||
static sysprop *get_impl() {
|
static sysprop_stub *get_impl() {
|
||||||
static sysprop *impl = nullptr;
|
static sysprop_stub *impl = nullptr;
|
||||||
if (impl == nullptr) {
|
if (impl == nullptr) {
|
||||||
use_pb = access(PERSISTENT_PROPERTY_DIR "/persistent_properties", R_OK) == 0;
|
use_pb = access(PERSISTENT_PROPERTY_DIR "/persistent_properties", R_OK) == 0;
|
||||||
#ifdef APPLET_STUB_MAIN
|
#ifdef APPLET_STUB_MAIN
|
||||||
@ -203,7 +206,7 @@ static sysprop *get_impl() {
|
|||||||
if (__system_properties_init()) {
|
if (__system_properties_init()) {
|
||||||
LOGW("resetprop: __system_properties_init error\n");
|
LOGW("resetprop: __system_properties_init error\n");
|
||||||
DLOAD(system_property_get);
|
DLOAD(system_property_get);
|
||||||
impl = system_property_get ? new stock_sysprop() : new sysprop();
|
impl = system_property_get ? new sysprop() : new sysprop_stub();
|
||||||
} else {
|
} else {
|
||||||
impl = new resetprop();
|
impl = new resetprop();
|
||||||
}
|
}
|
||||||
@ -232,11 +235,11 @@ int delprop(const char *name, bool persist) {
|
|||||||
return get_impl()->delprop(name, persist);
|
return get_impl()->delprop(name, persist);
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_prop_file(const char *filename, bool trigger) {
|
void load_prop_file(const char *filename, bool prop_svc) {
|
||||||
auto impl = get_impl();
|
auto impl = get_impl();
|
||||||
LOGD("resetprop: Parse prop file [%s]\n", filename);
|
LOGD("resetprop: Parse prop file [%s]\n", filename);
|
||||||
parse_prop_file(filename, [=](auto key, auto val) -> bool {
|
parse_prop_file(filename, [=](auto key, auto val) -> bool {
|
||||||
impl->setprop(key.data(), val.data(), trigger);
|
impl->setprop(key.data(), val.data(), prop_svc);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -244,9 +247,9 @@ void load_prop_file(const char *filename, bool trigger) {
|
|||||||
int resetprop_main(int argc, char *argv[]) {
|
int resetprop_main(int argc, char *argv[]) {
|
||||||
log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };
|
log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };
|
||||||
|
|
||||||
bool trigger = true, persist = false;
|
bool prop_svc = true;
|
||||||
|
bool persist = false;
|
||||||
char *argv0 = argv[0];
|
char *argv0 = argv[0];
|
||||||
string prop;
|
|
||||||
|
|
||||||
--argc;
|
--argc;
|
||||||
++argv;
|
++argv;
|
||||||
@ -257,7 +260,7 @@ int resetprop_main(int argc, char *argv[]) {
|
|||||||
switch (argv[0][idx]) {
|
switch (argv[0][idx]) {
|
||||||
case '-':
|
case '-':
|
||||||
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
|
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
|
||||||
load_prop_file(argv[1], trigger);
|
load_prop_file(argv[1], prop_svc);
|
||||||
return 0;
|
return 0;
|
||||||
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
|
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
|
||||||
return delprop(argv[1], persist);
|
return delprop(argv[1], persist);
|
||||||
@ -271,7 +274,7 @@ int resetprop_main(int argc, char *argv[]) {
|
|||||||
persist = true;
|
persist = true;
|
||||||
continue;
|
continue;
|
||||||
case 'n':
|
case 'n':
|
||||||
trigger = false;
|
prop_svc = false;
|
||||||
continue;
|
continue;
|
||||||
case '\0':
|
case '\0':
|
||||||
break;
|
break;
|
||||||
@ -289,13 +292,15 @@ int resetprop_main(int argc, char *argv[]) {
|
|||||||
case 0:
|
case 0:
|
||||||
print_props(persist);
|
print_props(persist);
|
||||||
return 0;
|
return 0;
|
||||||
case 1:
|
case 1: {
|
||||||
prop = getprop(argv[0], persist);
|
string prop = getprop(argv[0], persist);
|
||||||
if (prop.empty()) return 1;
|
if (prop.empty())
|
||||||
|
return 1;
|
||||||
printf("%s\n", prop.data());
|
printf("%s\n", prop.data());
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
case 2:
|
case 2:
|
||||||
return setprop(argv[0], argv[1], trigger);
|
return setprop(argv[0], argv[1], prop_svc);
|
||||||
default:
|
default:
|
||||||
usage(argv0);
|
usage(argv0);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user