Rewrite magiskhide

This commit is contained in:
topjohnwu
2017-04-06 06:12:29 +08:00
parent f476daa041
commit 8f6b33d790
17 changed files with 447 additions and 427 deletions

View File

@ -8,6 +8,7 @@
#include <stdio.h>
#include <pthread.h>
#include "magisk.h"
#include "utils.h"
static void *logger_thread(void *args) {
@ -23,10 +24,8 @@ static void *logger_thread(void *args) {
return NULL;
}
/* Start a new thread to monitor logcat and dump to file */
/* Start a new thread to monitor logcat and dump to logfile */
void monitor_logs() {
pthread_t log_monitor;
pthread_create(&log_monitor, NULL, logger_thread, NULL);
printf("Hello :)\n");
pthread_join(log_monitor, NULL);
}

36
jni/utils/misc.c Normal file
View File

@ -0,0 +1,36 @@
/* misc.c - Store all functions that are unable to be catagorized clearly
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
int check_data() {
FILE *fp = xfopen("/proc/mounts", "r");
while (fgets(magiskbuf, BUF_SIZE, fp)) {
if (strstr(magiskbuf, " /data ")) {
if (strstr(magiskbuf, "tmpfs"))
return 0;
else
return 1;
}
}
return 0;
}
/* All the string should be freed manually!! */
void file_to_vector(struct vector *v, FILE *fp) {
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, fp)) != -1) {
// Remove end newline
if (line[read - 1] == '\n')
line[read - 1] = '\0';
vec_push_back(v, line);
line = NULL;
}
}

View File

@ -9,6 +9,12 @@
// xwrap.c
FILE *xfopen(const char *pathname, const char *mode);
int xopen(const char *pathname, int flags);
ssize_t xwrite(int fd, const void *buf, size_t count);
ssize_t xread(int fd, void *buf, size_t count);
ssize_t xxread(int fd, void *buf, size_t count);
int xpipe(int pipefd[2]);
int xsetns(int fd, int nstype);
// vector.c
@ -18,4 +24,9 @@ FILE *xfopen(const char *pathname, const char *mode);
void monitor_logs();
// misc.c
int check_data();
void file_to_vector(struct vector *v, FILE *fp);
#endif

View File

@ -25,10 +25,23 @@ void vec_sort(struct vector *v, int (*compar)(const void *, const void *)) {
qsort(vec_entry(v), vec_size(v), sizeof(void*), compar);
}
/* Will cleanup only the vector itself
* use in cases when each element requires special cleanup
*/
void vec_destroy(struct vector *v) {
// Will not free each entry!
// Manually free each entry, then call this function
vec_size(v) = 0;
vec_cap(v) = 0;
free(v->data);
}
free(vec_entry(v));
vec_entry(v) = NULL; // Prevent double destroy segfault
}
/* Will cleanup each element AND the vector itself
* Shall be the general case
*/
void vec_deep_destroy(struct vector *v) {
void *e;
vec_for_each(v, e) {
free(e);
}
vec_destroy(v);
}

View File

@ -15,6 +15,7 @@ void vec_init(struct vector *v);
void vec_push_back(struct vector *v, void *p);
void vec_sort(struct vector *v, int (*compar)(const void *, const void *));
void vec_destroy(struct vector *v);
void vec_deep_destroy(struct vector *v);
#define vec_size(v) (v)->size
#define vec_cap(v) (v)->cap
#define vec_entry(v) (v)->data
@ -23,4 +24,8 @@ void vec_destroy(struct vector *v);
e = (v)->data[0]; \
for (size_t _ = 0; _ < (v)->size; ++_, e = (v)->data[_])
#define vec_for_each_r(v, e) \
e = (v)->data[(v)->size - 1]; \
for (size_t _ = (v)->size - 1; _ >= 0; --_, e = (v)->data[_])
#endif

View File

@ -6,8 +6,14 @@
*
*/
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "utils.h"
@ -15,7 +21,54 @@ FILE *xfopen(const char *pathname, const char *mode) {
FILE *fp = fopen(pathname, mode);
if (fp == NULL) {
PLOGE("fopen");
exit(1);
}
return fp;
}
}
int xopen(const char *pathname, int flags) {
int fd = open(pathname, flags);
if (fd < 0) {
PLOGE("open");
}
return fd;
}
ssize_t xwrite(int fd, const void *buf, size_t count) {
if (count != write(fd, buf, count)) {
PLOGE("write");
}
return count;
}
// Read error other than EOF
ssize_t xread(int fd, void *buf, size_t count) {
int ret = read(fd, buf, count);
if (ret < 0) {
PLOGE("read");
}
return ret;
}
// Read exact same size as count
ssize_t xxread(int fd, void *buf, size_t count) {
if (count != read(fd, buf, count)) {
PLOGE("read");
}
return count;
}
int xpipe(int pipefd[2]) {
if (pipe(pipefd) == -1) {
PLOGE("pipe");
exit(1);
}
return 0;
}
int xsetns(int fd, int nstype) {
if (setns(fd, nstype) == -1) {
PLOGE("setns");
}
return 0;
}