Magisk/native/jni/resetprop/resetprop.c

360 lines
8.9 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-02-28 16:52:27 +08:00
*/
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>
2017-10-10 02:04:50 +08:00
#include <dirent.h>
2017-04-14 06:06:41 +08:00
#include <sys/types.h>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
2018-07-14 04:34:48 +08:00
#include "private/_system_properties.h"
#include "private/system_properties.h"
2017-10-10 02:04:50 +08:00
2017-04-14 06:06:41 +08:00
#include "magisk.h"
2017-07-13 23:42:01 +08:00
#include "resetprop.h"
2018-04-28 20:22:42 +08:00
#include "_resetprop.h"
2017-10-10 02:04:50 +08:00
#include "vector.h"
2018-04-28 20:22:42 +08:00
#include "utils.h"
2017-04-28 01:45:57 +08:00
2018-04-28 20:22:42 +08:00
int prop_verbose = 0;
2017-04-14 06:06:41 +08:00
2017-10-10 02:04:50 +08:00
static int check_legal_property_name(const char *name) {
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;
}
return 0;
illegal:
2018-09-27 03:11:10 -04:00
LOGE("Illegal property name: [%s]\n", name);
2017-10-10 02:04:50 +08:00
return 1;
2017-02-28 16:52:27 +08:00
}
2017-04-14 06:06:41 +08:00
static int usage(char* arg0) {
2017-10-10 02:04:50 +08: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);
return 1;
2017-04-14 06:06:41 +08:00
}
2017-02-28 16:52:27 +08:00
2018-07-14 04:34:48 +08:00
// The callback passes to __system_property_read_callback, actually runs the callback in read_cb
2018-04-28 20:22:42 +08:00
static void callback_wrapper(void *read_cb, const char *name, const char *value, uint32_t serial) {
((struct read_cb_t *) read_cb)->func(name, value, ((struct read_cb_t *) read_cb)->cookie);
}
/* **********************************
* Callback functions for read_cb_t
* **********************************/
void collect_props(const char *name, const char *value, void *prop_list) {
struct prop_t *p = (struct prop_t *) xmalloc(sizeof(*p));
p->name = strdup(name);
strcpy(p->value, value);
vec_push_back(prop_list, p);
}
static void collect_unique_props(const char *name, const char *value, void *prop_list) {
struct vector *v = prop_list;
struct prop_t *p;
bool uniq = true;
vec_for_each(v, p) {
if (strcmp(name, p->name) == 0) {
uniq = 0;
break;
}
}
if (uniq)
collect_props(name, value, prop_list);
}
static void store_prop_value(const char *name, const char *value, void *dst) {
strcpy(dst, value);
}
static void prop_foreach_cb(const prop_info* pi, void* read_cb) {
2018-07-14 04:34:48 +08:00
__system_property_read_callback(pi, callback_wrapper, read_cb);
2018-04-28 20:22:42 +08:00
}
// Comparision function used to sort prop vectors
static int prop_cmp(const void *p1, const void *p2) {
return strcmp(((struct prop_t *) p1)->name, ((struct prop_t *) p2)->name);
}
2017-06-11 20:22:10 +08:00
static int init_resetprop() {
2018-07-14 04:34:48 +08:00
if (__system_properties_init()) {
2018-09-27 03:11:10 -04:00
LOGE("resetprop: Initialize error\n");
2017-10-10 02:04:50 +08:00
return -1;
}
return 0;
2017-04-14 06:06:41 +08:00
}
2017-02-28 16:52:27 +08:00
2018-04-28 20:22:42 +08:00
static void print_props(int persist) {
struct prop_t *p;
struct vector prop_list;
vec_init(&prop_list);
getprop_all(collect_props, &prop_list);
if (persist) {
struct read_cb_t read_cb = {
.func = collect_unique_props,
.cookie = &prop_list
};
persist_getprop_all(&read_cb);
}
vec_sort(&prop_list, prop_cmp);
vec_for_each(&prop_list, p) {
printf("[%s]: [%s]\n", p->name, p->value);
free(p->name);
free(p);
}
vec_destroy(&prop_list);
}
/* **************************************************
* Implementations of functions in resetprop.h (APIs)
* **************************************************/
2017-06-11 20:22:10 +08:00
int prop_exist(const char *name) {
2017-10-10 02:04:50 +08:00
if (init_resetprop()) return 0;
2018-07-14 04:34:48 +08:00
return __system_property_find(name) != NULL;
2017-06-11 20:22:10 +08:00
}
2017-04-14 06:06:41 +08:00
char *getprop(const char *name) {
2017-10-10 02:04:50 +08:00
return getprop2(name, 0);
2017-04-14 06:06:41 +08:00
}
2017-02-28 16:52:27 +08:00
2017-10-10 02:04:50 +08:00
// Get prop by name, return string (should free manually!)
char *getprop2(const char *name, int persist) {
if (check_legal_property_name(name))
return NULL;
if (init_resetprop()) return NULL;
2018-07-14 04:34:48 +08:00
const prop_info *pi = __system_property_find(name);
2017-10-10 02:04:50 +08:00
if (pi == NULL) {
if (persist && strncmp(name, "persist.", 8) == 0) {
2018-04-28 20:22:42 +08:00
char *value = persist_getprop(name);
if (value)
return value;
}
2018-09-27 03:11:10 -04:00
LOGD("resetprop: prop [%s] does not exist\n", name);
2017-10-10 02:04:50 +08:00
return NULL;
} else {
2018-04-28 20:22:42 +08:00
char value[PROP_VALUE_MAX];
struct read_cb_t read_cb = {
.func = store_prop_value,
.cookie = value
};
2018-07-14 04:34:48 +08:00
__system_property_read_callback(pi, callback_wrapper, &read_cb);
2018-09-27 03:11:10 -04:00
LOGD("resetprop: getprop [%s]: [%s]\n", name, value);
2018-04-28 20:22:42 +08:00
return strdup(value);
2017-10-10 02:04:50 +08:00
}
2017-07-18 11:56:57 +08:00
}
2018-04-28 20:22:42 +08:00
void getprop_all(void (*callback)(const char *, const char *, void *), void *cookie) {
2017-10-10 02:04:50 +08:00
if (init_resetprop()) return;
2018-04-28 20:22:42 +08:00
struct read_cb_t read_cb = {
.func = callback,
.cookie = cookie
2017-10-10 02:04:50 +08:00
};
2018-07-14 04:34:48 +08:00
__system_property_foreach(prop_foreach_cb, &read_cb);
2017-07-18 11:56:57 +08:00
}
2017-04-14 06:06:41 +08:00
int setprop(const char *name, const char *value) {
2017-10-10 02:04:50 +08:00
return setprop2(name, value, 1);
2017-04-14 06:06:41 +08:00
}
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-10-10 02:04:50 +08:00
if (check_legal_property_name(name))
return 1;
if (init_resetprop()) return -1;
int ret;
2018-07-14 04:34:48 +08:00
prop_info *pi = (prop_info*) __system_property_find(name);
2017-10-10 02:04:50 +08:00
if (pi != NULL) {
if (trigger) {
if (strncmp(name, "ro.", 3) == 0) deleteprop(name);
2018-07-14 04:34:48 +08:00
ret = __system_property_set(name, value);
2017-10-10 02:04:50 +08:00
} else {
2018-07-14 04:34:48 +08:00
ret = __system_property_update(pi, value, strlen(value));
2017-10-10 02:04:50 +08:00
}
} else {
2018-09-27 03:11:10 -04:00
LOGD("resetprop: New prop [%s]\n", name);
2017-10-10 02:04:50 +08:00
if (trigger) {
2018-07-14 04:34:48 +08:00
ret = __system_property_set(name, value);
2017-10-10 02:04:50 +08:00
} else {
2018-07-14 04:34:48 +08:00
ret = __system_property_add(name, strlen(name), value, strlen(value));
2017-10-10 02:04:50 +08:00
}
}
2018-09-27 03:11:10 -04:00
LOGD("resetprop: setprop [%s]: [%s] by %s\n", name, value,
2017-10-10 02:04:50 +08:00
trigger ? "property_service" : "modifing prop data structure");
if (ret)
2018-09-27 03:11:10 -04:00
LOGE("resetprop: setprop error\n");
2017-10-10 02:04:50 +08:00
return ret;
}
int deleteprop(const char *name) {
return deleteprop2(name, 1);
2017-04-14 06:06:41 +08:00
}
2018-04-28 20:22:42 +08:00
int deleteprop2(const char *name, int persist) {
2017-10-10 02:04:50 +08:00
if (check_legal_property_name(name))
return 1;
if (init_resetprop()) return -1;
char path[PATH_MAX];
path[0] = '\0';
2018-09-27 03:11:10 -04:00
LOGD("resetprop: deleteprop [%s]\n", name);
2017-10-10 02:04:50 +08:00
if (persist && strncmp(name, "persist.", 8) == 0)
2018-04-28 20:22:42 +08:00
persist = persist_deleteprop(name);
return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0);
2017-02-28 16:52:27 +08:00
}
2017-05-01 01:58:52 +08:00
int read_prop_file(const char* filename, const int trigger) {
2017-10-10 02:04:50 +08:00
if (init_resetprop()) return -1;
2018-09-27 03:11:10 -04:00
LOGD("resetprop: Load prop file [%s]\n", filename);
2017-10-10 02:04:50 +08:00
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
2018-09-27 03:11:10 -04:00
LOGE("Cannot open [%s]\n", filename);
2017-10-10 02:04:50 +08:00
return 1;
}
char *line = NULL, *pch;
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 20:22:42 +08:00
// Ignore invalid formats
2017-10-10 02:04:50 +08:00
if ( ((pch == NULL) || (i >= (pch - line))) || (pch >= line + read - 1) ) continue;
// Separate the string
*pch = '\0';
setprop2(line + i, pch + 1, trigger);
}
free(line);
fclose(fp);
return 0;
2017-02-28 16:52:27 +08:00
}
2018-09-27 03:11:10 -04:00
static int verbose_logging(const char *fmt, va_list ap) {
return prop_verbose ? vfprintf(stderr, fmt, ap) : 0;
}
int resetprop_main(int argc, char *argv[]) {
2018-09-27 03:11:10 -04:00
cmdline_logging();
log_cb.d = verbose_logging;
2017-10-10 02:04:50 +08:00
int trigger = 1, persist = 0;
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) {
return read_prop_file(argv[1], trigger);
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
return deleteprop2(argv[1], persist);
} else if (strcmp(argv[0], "--help") == 0) {
goto usage;
}
case 'v':
2018-04-28 20:22:42 +08:00
prop_verbose = 1;
2017-10-10 02:04:50 +08:00
continue;
case 'p':
persist = 1;
continue;
case 'n':
trigger = 0;
continue;
case '\0':
break;
case 'h':
default:
usage:
return usage(argv0);
}
break;
}
--argc;
++argv;
}
switch (argc) {
case 0:
2018-04-28 20:22:42 +08:00
print_props(persist);
2017-10-10 02:04:50 +08:00
return 0;
case 1:
prop = getprop2(argv[0], persist);
if (prop == NULL) return 1;
printf("%s\n", prop);
free(prop);
return 0;
case 2:
return setprop2(argv[0], argv[1], trigger);
default:
usage(argv0);
return 1;
}
2017-02-28 16:52:27 +08:00
}