Magisk/native/jni/resetprop/resetprop.cpp

311 lines
8.0 KiB
C++
Raw Normal View History

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>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
2018-07-13 20:34:48 +00:00
#include "private/_system_properties.h"
#include "private/system_properties.h"
2017-10-09 18:04:50 +00:00
2017-04-13 22:06:41 +00:00
#include "magisk.h"
2017-07-13 15:42:01 +00:00
#include "resetprop.h"
2018-04-28 12:22:42 +00:00
#include "_resetprop.h"
#include "utils.h"
2018-11-03 03:56:15 +00:00
#include "array.h"
#include "flags.h"
2017-04-27 17:45:57 +00:00
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,
"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 03:56:15 +00:00
exit(1);
2017-04-13 22:06:41 +00:00
}
2017-02-28 08:52:27 +00:00
2018-11-03 03:56:15 +00:00
// Define the way to sort prop_t
template<>
int(*Array<prop_t>::_cmp)(prop_t&, prop_t&) = [](auto a, auto b) -> int {
return strcmp(a.name, b.name);
};
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 12:22:42 +00:00
}
2018-11-03 03:56:15 +00:00
void collect_props(const char *name, const char *value, void *v_plist) {
Array<prop_t> &prop_list = *static_cast<Array<prop_t> *>(v_plist);
prop_list.push_back(prop_t(name, value));
2018-04-28 12:22:42 +00:00
}
2018-11-03 03:56:15 +00:00
static void collect_unique_props(const char *name, const char *value, void *v_plist) {
Array<prop_t> &prop_list = *static_cast<Array<prop_t> *>(v_plist);
for (auto &prop : prop_list) {
if (strcmp(name, prop.name) == 0)
return;
2018-04-28 12:22:42 +00:00
}
2018-11-03 03:56:15 +00:00
collect_props(name, value, v_plist);
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) {
2018-11-05 19:37:47 +00:00
Array<prop_t> prop_list;
2018-11-04 08:38:06 +00:00
getprop(collect_props, &prop_list, persist);
2018-11-03 03:56:15 +00:00
prop_list.sort();
for (auto &prop : prop_list)
printf("[%s]: [%s]\n", prop.name, prop.value);
2018-04-28 12:22:42 +00:00
}
/* **************************************************
* Implementations of functions in resetprop.h (APIs)
* **************************************************/
2017-06-11 12:22:10 +00:00
int prop_exist(const char *name) {
2017-10-09 18:04:50 +00:00
if (init_resetprop()) return 0;
2018-11-03 03:56:15 +00:00
return __system_property_find(name) != nullptr;
2017-06-11 12:22:10 +00:00
}
2017-10-09 18:04:50 +00:00
// Get prop by name, return string (should free manually!)
2018-11-04 08:38:06 +00:00
char *getprop(const char *name, bool persist) {
2018-11-03 03:56:15 +00:00
if (!check_legal_property_name(name) || init_resetprop())
return nullptr;
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) {
2018-04-28 12:22:42 +00:00
char *value = persist_getprop(name);
if (value)
return value;
}
2018-09-27 07:11:10 +00:00
LOGD("resetprop: prop [%s] does not exist\n", name);
2018-11-03 03:56:15 +00:00
return nullptr;
2017-10-09 18:04:50 +00:00
} else {
2018-04-28 12:22:42 +00:00
char value[PROP_VALUE_MAX];
2018-11-03 03:56:15 +00: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 07:11:10 +00:00
LOGD("resetprop: getprop [%s]: [%s]\n", name, value);
2018-04-28 12:22:42 +00:00
return strdup(value);
2017-10-09 18:04:50 +00:00
}
2017-07-18 03:56:57 +00:00
}
2018-11-04 08:38:06 +00:00
void getprop(void (*callback)(const char *, const char *, void *), void *cookie, bool persist) {
2017-10-09 18:04:50 +00:00
if (init_resetprop()) return;
2018-11-03 03:56:15 +00:00
read_cb_t read_cb(callback, cookie);
__system_property_foreach(read_props, &read_cb);
2018-11-03 04:15:21 +00:00
if (persist) {
read_cb.cb = collect_unique_props;
2018-11-05 19:37:47 +00:00
persist_getprop(&read_cb);
2018-11-03 04:15:21 +00:00
}
2017-07-18 03:56:57 +00:00
}
2018-11-04 08:38:06 +00:00
int setprop(const char *name, const char *value, const 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;
2018-11-03 03:56:15 +00:00
if (init_resetprop())
return -1;
2017-10-09 18:04:50 +00:00
int ret;
2018-07-13 20:34:48 +00:00
prop_info *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) {
if (strncmp(name, "ro.", 3) == 0) deleteprop(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;
}
2018-11-04 08:38:06 +00:00
int deleteprop(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;
if (init_resetprop()) return -1;
char path[PATH_MAX];
path[0] = '\0';
2018-09-27 07:11:10 +00:00
LOGD("resetprop: deleteprop [%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
}
2018-11-04 08:38:06 +00:00
int load_prop_file(const char *filename, const bool trigger) {
2017-10-09 18:04:50 +00:00
if (init_resetprop()) return -1;
2018-09-27 07:11:10 +00:00
LOGD("resetprop: Load prop file [%s]\n", filename);
2017-10-09 18:04:50 +00:00
FILE *fp = fopen(filename, "r");
2018-11-03 03:56:15 +00:00
if (fp == nullptr) {
2018-09-27 07:11:10 +00:00
LOGE("Cannot open [%s]\n", filename);
2017-10-09 18:04:50 +00:00
return 1;
}
2018-11-03 03:56:15 +00:00
char *line = nullptr, *pch;
2017-10-09 18:04:50 +00:00
size_t len;
ssize_t read;
int comment = 0, i;
while ((read = getline(&line, &len, fp)) != -1) {
// Remove the trailing newline
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
--read;
}
comment = 0;
for (i = 0; i < read; ++i) {
// Ignore starting spaces
if (line[i] == ' ') continue;
else {
// A line starting with # is ignored
if (line[i] == '#') comment = 1;
break;
}
}
if (comment) continue;
pch = strchr(line, '=');
2018-04-28 12:22:42 +00:00
// Ignore invalid formats
2018-11-03 03:56:15 +00:00
if ( ((pch == nullptr) || (i >= (pch - line))) || (pch >= line + read - 1) ) continue;
2017-10-09 18:04:50 +00:00
// Separate the string
*pch = '\0';
2018-11-04 08:38:06 +00:00
setprop(line + i, pch + 1, trigger);
2017-10-09 18:04:50 +00:00
}
free(line);
fclose(fp);
return 0;
2017-02-28 08:52:27 +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;
2017-10-09 18:04:50 +00:00
char *argv0 = argv[0], *prop;
--argc;
++argv;
// Parse flags and -- options
while (argc && argv[0][0] == '-') {
for (int idx = 1; 1; ++idx) {
switch (argv[0][idx]) {
case '-':
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
2018-11-04 08:38:06 +00:00
return load_prop_file(argv[1], trigger);
2017-10-09 18:04:50 +00:00
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
2018-11-04 08:38:06 +00:00
return deleteprop(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':
persist = 1;
continue;
case 'n':
trigger = 0;
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:
2018-11-04 08:38:06 +00:00
prop = getprop(argv[0], persist);
2018-11-03 03:56:15 +00:00
if (prop == nullptr) return 1;
2017-10-09 18:04:50 +00:00
printf("%s\n", prop);
free(prop);
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
}