Magisk/jni/resetprop/resetprop.cpp

309 lines
9.0 KiB
C++
Raw Normal View History

2017-04-14 06:06:41 +08:00
/* resetprop.cpp - Manipulate any system props
2017-08-15 01:25:54 +08:00
*
2017-02-28 16:52:27 +08:00
* Copyright 2016 nkk71 <nkk71x@gmail.com>
* Copyright 2016 topjohnwu <topjohnwu@gmail.com>
2017-08-15 01:25:54 +08:00
*
2017-04-14 06:06:41 +08:00
* Info:
2017-08-15 01:25:54 +08:00
*
2017-02-28 16:52:27 +08:00
* all changes are in
*
* bionic/libc/bionic/system_properties.cpp
*
* Functions that need to be patched/added in system_properties.cpp
*
* int __system_properties_init2()
2017-02-28 16:52:27 +08:00
* on android 7, first tear down the everything then let it initialize again:
* if (initialized) {
* //list_foreach(contexts, [](context_node* l) { l->reset_access(); });
* //return 0;
* free_and_unmap_contexts();
* initialized = false;
* }
*
*
* static prop_area* map_prop_area(const char* filename, bool is_legacy)
* we dont want this read only so change: 'O_RDONLY' to 'O_RDWR'
*
* static prop_area* map_fd_ro(const int fd)
* we dont want this read only so change: 'PROT_READ' to 'PROT_READ | PROT_WRITE'
2017-08-15 01:25:54 +08:00
*
2017-02-28 16:52:27 +08:00
*
* Copy the code of prop_info *prop_area::find_property, and modify to delete props
* const prop_info *prop_area::find_property_and_del(prop_bt *const trie, const char *name)
* {
2017-08-15 01:25:54 +08:00
* ...
2017-02-28 16:52:27 +08:00
* ... Do not alloc a new prop_bt here, remove all code involve alloc_if_needed
* ...
2017-08-15 01:25:54 +08:00
*
2017-02-28 16:52:27 +08:00
* if (prop_offset != 0) {
* atomic_store_explicit(&current->prop, 0, memory_order_release); // Add this line to nullify the prop entry
* return to_prop_info(&current->prop);
* } else {
2017-08-15 01:25:54 +08:00
*
2017-02-28 16:52:27 +08:00
* ....
* }
2017-08-15 01:25:54 +08:00
*
2017-02-28 16:52:27 +08:00
*
* by patching just those functions directly, all other functions should be ok
* as is.
*
*
*/
2017-04-14 06:06:41 +08:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include "_system_properties.h"
2017-07-13 23:42:01 +08:00
#include "system_properties.h"
2017-04-14 06:06:41 +08:00
#include "magisk.h"
2017-07-13 23:42:01 +08:00
#include "resetprop.h"
2017-04-28 01:45:57 +08:00
#define PRINT_D(...) { LOGD(__VA_ARGS__); if (verbose) printf(__VA_ARGS__); }
#define PRINT_E(...) { LOGE(__VA_ARGS__); fprintf(stderr, __VA_ARGS__); }
2017-07-18 11:56:57 +08:00
#define PERSISTENT_PROPERTY_DIR "/data/property"
2017-04-28 01:45:57 +08:00
2017-04-14 06:06:41 +08:00
static int verbose = 0;
2017-07-18 11:56:57 +08:00
static bool is_legal_property_name(const char *name, size_t namelen) {
2017-02-28 16:52:27 +08:00
if (namelen < 1) return false;
if (name[0] == '.') return false;
if (name[namelen - 1] == '.') return false;
2017-07-18 11:56:57 +08:00
/* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
2017-02-28 16:52:27 +08:00
/* Don't allow ".." to appear in a property name */
2017-04-14 06:06:41 +08:00
for (size_t i = 0; i < namelen; i++) {
2017-02-28 16:52:27 +08:00
if (name[i] == '.') {
// i=0 is guaranteed to never have a dot. See above.
2017-07-18 11:56:57 +08:00
if (name[i-1] == '.') return false;
2017-02-28 16:52:27 +08:00
continue;
}
2017-07-18 11:56:57 +08:00
if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
2017-02-28 16:52:27 +08:00
if (name[i] >= 'a' && name[i] <= 'z') continue;
if (name[i] >= 'A' && name[i] <= 'Z') continue;
if (name[i] >= '0' && name[i] <= '9') continue;
return false;
}
return true;
}
2017-04-14 06:06:41 +08:00
static int usage(char* arg0) {
fprintf(stderr,
"resetprop v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu & nkk71) - System Props Modification Tool\n\n"
2017-04-14 06:06:41 +08:00
"Usage: %s [options] [args...]\n"
"\n"
"Options:\n"
2017-08-15 01:25:54 +08:00
" -v show verbose output\n"
" -n only modify property in memory\n"
"\n"
"%s NAME VALUE set property entry NAME with VALUE\n"
"%s --file FILE load props from FILE\n"
"%s --delete NAME remove prop entry NAME\n"
"\n"
2017-04-14 06:06:41 +08:00
, arg0, arg0, arg0, arg0);
return 1;
}
2017-02-28 16:52:27 +08:00
2017-06-11 20:22:10 +08:00
static int init_resetprop() {
if (__system_properties_init2()) {
2017-04-28 01:45:57 +08:00
PRINT_E("resetprop: Initialize error\n");
2017-04-14 06:06:41 +08:00
return -1;
}
return 0;
}
2017-02-28 16:52:27 +08:00
2017-06-11 20:22:10 +08:00
int prop_exist(const char *name) {
if (init_resetprop()) return 0;
return __system_property_find2(name) != NULL;
}
2017-04-17 00:35:34 +08:00
static void read_prop_info(void* cookie, const char *name, const char *value, uint32_t serial) {
strcpy((char *) cookie, value);
}
2017-04-14 06:06:41 +08:00
// Get prop by name, return string (should free manually!)
char *getprop(const char *name) {
2017-06-11 20:22:10 +08:00
if (init_resetprop()) return NULL;
const prop_info *pi = __system_property_find2(name);
2017-04-17 00:35:34 +08:00
if (pi == NULL) {
2017-06-11 20:22:10 +08:00
PRINT_D("resetprop: prop [%s] does not exist\n", name);
2017-04-14 06:06:41 +08:00
return NULL;
2017-02-28 16:52:27 +08:00
}
2017-04-17 00:35:34 +08:00
char value[PROP_VALUE_MAX];
__system_property_read_callback2(pi, read_prop_info, value);
2017-04-28 01:45:57 +08:00
PRINT_D("resetprop: getprop [%s]: [%s]\n", name, value);
2017-04-14 06:06:41 +08:00
return strdup(value);
}
2017-02-28 16:52:27 +08:00
2017-07-18 11:56:57 +08:00
static void (*cb)(const char *);
static void run_actual_cb(void* cookie, const char *name, const char *value, uint32_t serial) {
cb(name);
}
static void prop_foreach_cb(const prop_info* pi, void* cookie) {
__system_property_read_callback2(pi, run_actual_cb, NULL);
}
void getprop_all(void (*cbk)(const char *name)) {
if (init_resetprop()) return;
cb = cbk;
__system_property_foreach2(prop_foreach_cb, NULL);
}
2017-04-14 06:06:41 +08:00
int setprop(const char *name, const char *value) {
return setprop2(name, value, 1);
}
2017-02-28 16:52:27 +08:00
2017-05-01 01:58:52 +08:00
int setprop2(const char *name, const char *value, const int trigger) {
2017-06-11 20:22:10 +08:00
if (init_resetprop()) return -1;
2017-04-14 06:06:41 +08:00
int ret;
2017-08-15 01:25:54 +08:00
2017-06-11 20:22:10 +08:00
prop_info *pi = (prop_info*) __system_property_find2(name);
if (pi != NULL) {
2017-02-28 16:52:27 +08:00
if (trigger) {
2017-07-18 11:56:57 +08:00
if (!strncmp(name, "ro.", 3)) deleteprop(name, trigger);
ret = __system_property_set2(name, value);
2017-02-28 16:52:27 +08:00
} else {
2017-06-11 20:22:10 +08:00
ret = __system_property_update2(pi, value, strlen(value));
2017-02-28 16:52:27 +08:00
}
} else {
2017-04-28 01:45:57 +08:00
PRINT_D("resetprop: New prop [%s]\n", name);
2017-02-28 16:52:27 +08:00
if (trigger) {
ret = __system_property_set2(name, value);
2017-02-28 16:52:27 +08:00
} else {
ret = __system_property_add2(name, strlen(name), value, strlen(value));
2017-02-28 16:52:27 +08:00
}
}
2017-04-28 01:45:57 +08:00
PRINT_D("resetprop: setprop [%s]: [%s] by %s\n", name, value,
2017-04-14 06:06:41 +08:00
trigger ? "property_service" : "modifing prop data structure");
2017-02-28 16:52:27 +08:00
2017-04-14 06:06:41 +08:00
if (ret)
2017-04-28 01:45:57 +08:00
PRINT_E("resetprop: setprop error\n");
2017-02-28 16:52:27 +08:00
2017-04-14 06:06:41 +08:00
return ret;
}
2017-07-18 11:56:57 +08:00
int deleteprop(const char *name, const int trigger) {
2017-06-11 20:22:10 +08:00
if (init_resetprop()) return -1;
2017-04-28 01:45:57 +08:00
PRINT_D("resetprop: deleteprop [%s]\n", name);
2017-04-14 06:06:41 +08:00
if (__system_property_del(name)) {
2017-07-18 11:56:57 +08:00
PRINT_D("resetprop: delete prop: [%s] error\n", name);
2017-04-14 06:06:41 +08:00
return -1;
}
2017-07-18 11:56:57 +08:00
if (trigger && strstr(name, "persist.")) {
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
unlink(buffer);
}
2017-02-28 16:52:27 +08:00
return 0;
}
2017-05-01 01:58:52 +08:00
int read_prop_file(const char* filename, const int trigger) {
2017-06-11 20:22:10 +08:00
if (init_resetprop()) return -1;
2017-04-28 01:45:57 +08:00
PRINT_D("resetprop: Load prop file [%s]\n", filename);
2017-02-28 16:52:27 +08:00
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
2017-04-28 01:45:57 +08:00
PRINT_E("Cannot open [%s]\n", filename);
2017-02-28 16:52:27 +08:00
return 1;
}
2017-03-18 16:57:04 +08:00
char *line = NULL, *pch;
2017-02-28 16:52:27 +08: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, '=');
// Ignore ivalid formats
if ( ((pch == NULL) || (i >= (pch - line))) || (pch >= line + read - 1) ) continue;
// Separate the string
*pch = '\0';
2017-05-01 01:58:52 +08:00
setprop2(line + i, pch + 1, trigger);
2017-02-28 16:52:27 +08:00
}
free(line);
fclose(fp);
return 0;
}
int resetprop_main(int argc, char *argv[]) {
2017-04-14 06:06:41 +08:00
int del = 0, file = 0, trigger = 1;
2017-08-15 01:25:54 +08:00
2017-05-01 01:58:52 +08:00
int exp_arg = 2;
2017-02-28 16:52:27 +08:00
char *name, *value, *filename;
if (argc < 3) {
return usage(argv[0]);
}
for (int i = 1; i < argc; ++i) {
if (!strcmp("-v", argv[i])) {
verbose = 1;
} else if (!strcmp("-n", argv[i])) {
trigger = 0;
} else if (!strcmp("--file", argv[i])) {
file = 1;
exp_arg = 1;
} else if (!strcmp("--delete", argv[i])) {
del = 1;
exp_arg = 1;
} else {
if (i + exp_arg > argc) {
return usage(argv[0]);
}
if (file) {
filename = argv[i];
break;
} else {
if(!is_legal_property_name(argv[i], strlen(argv[i]))) {
2017-04-28 01:45:57 +08:00
PRINT_E("Illegal property name: [%s]\n", argv[i]);
2017-02-28 16:52:27 +08:00
return 1;
}
name = argv[i];
if (exp_arg > 1) {
if (strlen(argv[i + 1]) >= PROP_VALUE_MAX) {
2017-04-28 01:45:57 +08:00
PRINT_E("Value too long: [%s]\n", argv[i + 1]);
2017-02-28 16:52:27 +08:00
return 1;
}
value = argv[i + 1];
}
break;
}
}
}
if (file) {
2017-05-01 01:58:52 +08:00
return read_prop_file(filename, trigger);
2017-02-28 16:52:27 +08:00
} else if (del) {
2017-07-18 11:56:57 +08:00
return deleteprop(name, trigger);
2017-02-28 16:52:27 +08:00
} else {
2017-04-14 06:06:41 +08:00
return setprop2(name, value, trigger);
2017-02-28 16:52:27 +08:00
}
2017-04-14 06:06:41 +08:00
return 0;
2017-02-28 16:52:27 +08:00
}