Magisk/native/jni/resetprop/resetprop.cpp

314 lines
8.0 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
#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"
2017-10-09 20:04:50 +02:00
2017-04-14 00:06:41 +02:00
#include "magisk.h"
2017-07-13 17:42:01 +02:00
#include "resetprop.h"
2018-04-28 14:22:42 +02:00
#include "_resetprop.h"
#include "utils.h"
#include "flags.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,
"resetprop v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu & nkk71) - System Props Modification Tool\n\n"
"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(
pi, [](auto cb, auto name, auto value, auto) -> void
{
((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-01-20 05:59:37 +01: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)
* **************************************************/
2017-06-11 14:22:10 +02:00
int prop_exist(const char *name) {
2017-10-09 20:04:50 +02:00
if (init_resetprop()) return 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) {
2018-11-03 04:56:15 +01:00
if (!check_legal_property_name(name) || init_resetprop())
return nullptr;
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) {
2017-10-09 20:04:50 +02:00
if (init_resetprop()) return;
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;
2018-11-03 04:56:15 +01:00
if (init_resetprop())
return -1;
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;
if (init_resetprop()) return -1;
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-01-20 21:20:34 +01:00
int parse_prop_file(const char *filename, const function<bool (const char *, const char *)> &cb) {
LOGD("resetprop: Parse prop file [%s]\n", filename);
2018-11-24 03:15:44 +01:00
FILE *fp = xfopen(filename, "re");
2018-11-03 04:56:15 +01:00
if (fp == nullptr) {
2018-09-27 09:11:10 +02:00
LOGE("Cannot open [%s]\n", filename);
2017-10-09 20:04:50 +02:00
return 1;
}
2019-01-20 21:20:34 +01:00
char *line = nullptr, *eql;
2017-10-09 20:04:50 +02:00
size_t len;
ssize_t read;
2019-01-20 21:20:34 +01:00
int i;
2017-10-09 20:04:50 +02:00
while ((read = getline(&line, &len, fp)) != -1) {
// Remove the trailing newline
if (line[read - 1] == '\n') {
2019-01-20 21:20:34 +01:00
line[--read] = '\0';
2017-10-09 20:04:50 +02:00
}
2019-01-20 21:20:34 +01:00
bool comment = false;
2017-10-09 20:04:50 +02:00
for (i = 0; i < read; ++i) {
// Ignore starting spaces
if (line[i] == ' ') continue;
2019-01-20 21:20:34 +01:00
// A line starting with # is ignored
if (line[i] == '#') comment = true;
break;
2017-10-09 20:04:50 +02:00
}
if (comment) continue;
2019-01-20 21:20:34 +01:00
eql = strchr(line, '=');
2018-04-28 14:22:42 +02:00
// Ignore invalid formats
2019-01-20 21:20:34 +01:00
if ( ((eql == nullptr) || (i >= (eql - line))) || (eql >= line + read - 1) )
continue;
2017-10-09 20:04:50 +02:00
// Separate the string
2019-01-20 21:20:34 +01:00
*eql = '\0';
if (!cb(line + i, eql + 1))
break;
2017-10-09 20:04:50 +02:00
}
free(line);
fclose(fp);
return 0;
2017-02-28 09:52:27 +01:00
}
2019-01-20 21:20:34 +01:00
int load_prop_file(const char *filename, bool trigger) {
if (init_resetprop()) return -1;
return parse_prop_file(filename, [=](auto key, auto val) -> bool {
setprop(key, val, trigger);
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) {
2018-11-04 09:38:06 +01:00
return load_prop_file(argv[1], trigger);
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
}