Magisk/native/jni/resetprop/resetprop.cpp

278 lines
7.1 KiB
C++
Raw Normal View History

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