2017-04-13 22:06:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2017-10-09 18:04:50 +00:00
|
|
|
#include <dirent.h>
|
2017-04-13 22:06:41 +00:00
|
|
|
#include <sys/types.h>
|
2019-01-20 04:59:37 +00:00
|
|
|
#include <vector>
|
2020-05-07 13:08:30 +00:00
|
|
|
#include <map>
|
2017-04-13 22:06:41 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <logging.hpp>
|
|
|
|
#include <resetprop.hpp>
|
|
|
|
#include <utils.hpp>
|
2019-02-10 08:57:51 +00:00
|
|
|
#include <flags.h>
|
|
|
|
|
2017-04-13 22:06:41 +00:00
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <_system_properties.h>
|
|
|
|
|
|
|
|
#include "_resetprop.hpp"
|
2017-04-27 17:45:57 +00:00
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2018-11-03 03:56:15 +00:00
|
|
|
bool use_pb = false;
|
|
|
|
static bool verbose = false;
|
2017-04-13 22:06:41 +00:00
|
|
|
|
2018-11-03 03:56:15 +00:00
|
|
|
static bool check_legal_property_name(const char *name) {
|
2017-10-09 18:04:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-03 03:56:15 +00:00
|
|
|
return true;
|
2017-10-09 18:04:50 +00:00
|
|
|
|
|
|
|
illegal:
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGE("Illegal property name: [%s]\n", name);
|
2018-11-03 03:56:15 +00:00
|
|
|
return false;
|
2017-02-28 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 03:56:15 +00:00
|
|
|
[[noreturn]] static void usage(char* arg0) {
|
2017-10-09 18:04:50 +00:00
|
|
|
fprintf(stderr,
|
2020-05-07 13:08:30 +00:00
|
|
|
NAME_WITH_VER(resetprop) R"EOF(" - System Props Modification Tool
|
|
|
|
|
|
|
|
Usage: %s [flags] [options...]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help show this message
|
|
|
|
(no arguments) print all properties
|
|
|
|
NAME get property
|
|
|
|
NAME VALUE set property entry NAME with VALUE
|
|
|
|
--file FILE load props from FILE
|
|
|
|
--delete NAME delete property
|
|
|
|
|
|
|
|
Flags:
|
|
|
|
-v print verbose output to stderr
|
|
|
|
-n set properties without going through init
|
|
|
|
affects setprop and prop file loading
|
|
|
|
-p also access props directly from persist storage
|
|
|
|
affects getprop and delprop
|
|
|
|
|
|
|
|
)EOF", arg0);
|
2018-11-03 03:56:15 +00:00
|
|
|
exit(1);
|
2017-04-13 22:06:41 +00:00
|
|
|
}
|
2017-02-28 08:52:27 +00:00
|
|
|
|
2020-05-07 13:08:30 +00:00
|
|
|
static void read_props(const prop_info *pi, void *cb) {
|
2018-11-03 03:56:15 +00:00
|
|
|
__system_property_read_callback(
|
2019-07-01 02:09:31 +00:00
|
|
|
pi, [](auto cb, auto name, auto value, auto) {
|
2020-05-07 13:08:30 +00:00
|
|
|
reinterpret_cast<prop_cb*>(cb)->exec(name, value);
|
|
|
|
}, cb);
|
2018-04-28 12:22:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 12:22:10 +00:00
|
|
|
static int init_resetprop() {
|
2018-11-03 03:56:15 +00:00
|
|
|
use_pb = use_pb ? true : access(PERSISTENT_PROPERTY_DIR "/persistent_properties", R_OK) == 0;
|
2018-07-13 20:34:48 +00:00
|
|
|
if (__system_properties_init()) {
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGE("resetprop: Initialize error\n");
|
2017-10-09 18:04:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-04-13 22:06:41 +00:00
|
|
|
}
|
2017-02-28 08:52:27 +00:00
|
|
|
|
2018-11-04 08:38:06 +00:00
|
|
|
static void print_props(bool persist) {
|
2020-05-07 13:08:30 +00:00
|
|
|
getprops([](auto name, auto value, auto) {
|
|
|
|
printf("[%s]: [%s]\n", name, value);
|
|
|
|
}, nullptr, persist);
|
2018-04-28 12:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************
|
|
|
|
* Implementations of functions in resetprop.h (APIs)
|
|
|
|
* **************************************************/
|
|
|
|
|
2019-07-01 02:09:31 +00:00
|
|
|
#define ENSURE_INIT(ret) if (init_resetprop()) return ret
|
|
|
|
|
2020-05-07 13:08:30 +00:00
|
|
|
bool prop_exist(const char *name) {
|
|
|
|
ENSURE_INIT(false);
|
2018-11-03 03:56:15 +00:00
|
|
|
return __system_property_find(name) != nullptr;
|
2017-06-11 12:22:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
// Get prop by name, return string
|
|
|
|
string getprop(const char *name, bool persist) {
|
2019-07-01 02:09:31 +00:00
|
|
|
if (!check_legal_property_name(name))
|
2019-02-03 14:48:57 +00:00
|
|
|
return string();
|
2019-07-01 02:09:31 +00:00
|
|
|
ENSURE_INIT(string());
|
2018-07-13 20:34:48 +00:00
|
|
|
const prop_info *pi = __system_property_find(name);
|
2018-11-03 03:56:15 +00:00
|
|
|
if (pi == nullptr) {
|
2017-10-09 18:04:50 +00:00
|
|
|
if (persist && strncmp(name, "persist.", 8) == 0) {
|
2019-01-20 04:59:37 +00:00
|
|
|
auto value = persist_getprop(name);
|
|
|
|
if (value.empty())
|
2020-05-07 13:08:30 +00:00
|
|
|
goto not_found;
|
2019-01-20 04:59:37 +00:00
|
|
|
return value;
|
2018-04-28 12:22:42 +00:00
|
|
|
}
|
2020-05-07 13:08:30 +00:00
|
|
|
not_found:
|
|
|
|
LOGD("resetprop: prop [%s] does not exist\n", name);
|
2019-01-20 04:59:37 +00:00
|
|
|
return string();
|
2017-10-09 18:04:50 +00:00
|
|
|
} else {
|
2020-05-07 13:08:30 +00:00
|
|
|
char buf[PROP_VALUE_MAX];
|
|
|
|
auto reader = make_prop_cb(buf, [](auto, auto value, auto buf) { strcpy(buf, value); });
|
|
|
|
read_props(pi, &reader);
|
|
|
|
LOGD("resetprop: getprop [%s]: [%s]\n", name, buf);
|
|
|
|
return buf;
|
2017-10-09 18:04:50 +00:00
|
|
|
}
|
2017-07-18 03:56:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 13:08:30 +00:00
|
|
|
void getprops(void (*callback)(const char *, const char *, void *), void *cookie, bool persist) {
|
2019-07-01 02:09:31 +00:00
|
|
|
ENSURE_INIT();
|
2020-05-07 13:08:30 +00:00
|
|
|
prop_list list;
|
|
|
|
prop_collector collector(list);
|
|
|
|
__system_property_foreach(read_props, &collector);
|
|
|
|
if (persist)
|
|
|
|
persist_getprops(&collector);
|
|
|
|
for (auto &[key, val] : list)
|
|
|
|
callback(key.data(), val.data(), cookie);
|
|
|
|
persist_cleanup();
|
2017-07-18 03:56:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
int setprop(const char *name, const char *value, bool trigger) {
|
2018-11-03 03:56:15 +00:00
|
|
|
if (!check_legal_property_name(name))
|
2017-10-09 18:04:50 +00:00
|
|
|
return 1;
|
2019-07-01 02:09:31 +00:00
|
|
|
ENSURE_INIT(-1);
|
2018-11-03 03:56:15 +00:00
|
|
|
|
2017-10-09 18:04:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-01-20 04:59:37 +00:00
|
|
|
auto pi = (prop_info*) __system_property_find(name);
|
2018-11-03 03:56:15 +00:00
|
|
|
if (pi != nullptr) {
|
2017-10-09 18:04:50 +00:00
|
|
|
if (trigger) {
|
2020-03-09 09:05:24 +00:00
|
|
|
if (strncmp(name, "ro.", 3) == 0) delprop(name);
|
2018-07-13 20:34:48 +00:00
|
|
|
ret = __system_property_set(name, value);
|
2017-10-09 18:04:50 +00:00
|
|
|
} else {
|
2018-07-13 20:34:48 +00:00
|
|
|
ret = __system_property_update(pi, value, strlen(value));
|
2017-10-09 18:04:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGD("resetprop: New prop [%s]\n", name);
|
2017-10-09 18:04:50 +00:00
|
|
|
if (trigger) {
|
2018-07-13 20:34:48 +00:00
|
|
|
ret = __system_property_set(name, value);
|
2017-10-09 18:04:50 +00:00
|
|
|
} else {
|
2018-07-13 20:34:48 +00:00
|
|
|
ret = __system_property_add(name, strlen(name), value, strlen(value));
|
2017-10-09 18:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGD("resetprop: setprop [%s]: [%s] by %s\n", name, value,
|
2017-10-09 18:04:50 +00:00
|
|
|
trigger ? "property_service" : "modifing prop data structure");
|
|
|
|
|
|
|
|
if (ret)
|
2018-09-27 07:11:10 +00:00
|
|
|
LOGE("resetprop: setprop error\n");
|
2017-10-09 18:04:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-09 09:05:24 +00:00
|
|
|
int delprop(const char *name, bool persist) {
|
2018-11-03 03:56:15 +00:00
|
|
|
if (!check_legal_property_name(name))
|
2017-10-09 18:04:50 +00:00
|
|
|
return 1;
|
2019-07-01 02:09:31 +00:00
|
|
|
ENSURE_INIT(-1);
|
2017-10-09 18:04:50 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
path[0] = '\0';
|
2020-03-09 09:05:24 +00:00
|
|
|
LOGD("resetprop: delprop [%s]\n", name);
|
2017-10-09 18:04:50 +00:00
|
|
|
if (persist && strncmp(name, "persist.", 8) == 0)
|
2018-04-28 12:22:42 +00:00
|
|
|
persist = persist_deleteprop(name);
|
|
|
|
return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0);
|
2017-02-28 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 01:27:09 +00:00
|
|
|
void load_prop_file(const char *filename, bool trigger) {
|
2019-07-01 02:09:31 +00:00
|
|
|
ENSURE_INIT();
|
2019-01-20 20:20:34 +00:00
|
|
|
LOGD("resetprop: Parse prop file [%s]\n", filename);
|
2019-03-06 01:27:09 +00:00
|
|
|
parse_prop_file(filename, [=](auto key, auto val) -> bool {
|
|
|
|
setprop(key.data(), val.data(), trigger);
|
2019-01-20 20:20:34 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-04 22:08:53 +00:00
|
|
|
int resetprop_main(int argc, char *argv[]) {
|
2018-11-03 03:56:15 +00:00
|
|
|
log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };
|
2018-11-04 08:38:06 +00:00
|
|
|
|
|
|
|
bool trigger = true, persist = false;
|
2018-11-07 07:10:38 +00:00
|
|
|
char *argv0 = argv[0];
|
2019-01-20 04:59:37 +00:00
|
|
|
string prop;
|
2017-10-09 18:04:50 +00:00
|
|
|
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
|
|
|
|
// Parse flags and -- options
|
|
|
|
while (argc && argv[0][0] == '-') {
|
2019-01-20 04:59:37 +00:00
|
|
|
for (int idx = 1; true; ++idx) {
|
2017-10-09 18:04:50 +00:00
|
|
|
switch (argv[0][idx]) {
|
|
|
|
case '-':
|
|
|
|
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
|
2019-03-06 01:27:09 +00:00
|
|
|
load_prop_file(argv[1], trigger);
|
|
|
|
return 0;
|
2017-10-09 18:04:50 +00:00
|
|
|
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
|
2020-03-09 09:05:24 +00:00
|
|
|
return delprop(argv[1], persist);
|
2017-10-09 18:04:50 +00:00
|
|
|
} else if (strcmp(argv[0], "--help") == 0) {
|
2018-11-03 03:56:15 +00:00
|
|
|
usage(argv0);
|
2017-10-09 18:04:50 +00:00
|
|
|
}
|
|
|
|
case 'v':
|
2018-11-03 03:56:15 +00:00
|
|
|
verbose = true;
|
2017-10-09 18:04:50 +00:00
|
|
|
continue;
|
|
|
|
case 'p':
|
2019-01-20 04:59:37 +00:00
|
|
|
persist = true;
|
2017-10-09 18:04:50 +00:00
|
|
|
continue;
|
|
|
|
case 'n':
|
2019-01-20 04:59:37 +00:00
|
|
|
trigger = false;
|
2017-10-09 18:04:50 +00:00
|
|
|
continue;
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
2018-11-03 03:56:15 +00:00
|
|
|
usage(argv0);
|
2017-10-09 18:04:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
2018-04-28 12:22:42 +00:00
|
|
|
print_props(persist);
|
2017-10-09 18:04:50 +00:00
|
|
|
return 0;
|
|
|
|
case 1:
|
2019-01-20 04:59:37 +00:00
|
|
|
prop = getprop(argv[0], persist);
|
|
|
|
if (prop.empty()) return 1;
|
2018-11-07 07:10:38 +00:00
|
|
|
printf("%s\n", prop.c_str());
|
2017-10-09 18:04:50 +00:00
|
|
|
return 0;
|
|
|
|
case 2:
|
2018-11-04 08:38:06 +00:00
|
|
|
return setprop(argv[0], argv[1], trigger);
|
2017-10-09 18:04:50 +00:00
|
|
|
default:
|
|
|
|
usage(argv0);
|
|
|
|
}
|
2017-02-28 08:52:27 +00:00
|
|
|
}
|