Commit 668601ca authored by topjohnwu's avatar topjohnwu

Separate all binaries and makefile

parent 99406f20
LOCAL_PATH := $(call my-dir) LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) include jni/bootimgtools/Android.mk
LOCAL_MODULE := magiskhide include jni/magiskhide/Android.mk
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := magiskhide.c
LOCAL_CFLAGS += -std=gnu11
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_MODULE := bootimgtools
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := bootimgtools/main.c bootimgtools/extract.c bootimgtools/repack.c bootimgtools/hexpatch.c
LOCAL_CFLAGS += -std=gnu11
include $(BUILD_EXECUTABLE)
include jni/resetprop/Android.mk include jni/resetprop/Android.mk
include jni/sepolicy-inject/Android.mk include jni/sepolicy-inject/Android.mk
include jni/su/Android.mk include jni/su/Android.mk
......
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bootimgtools
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := main.c extract.c repack.c hexpatch.c
LOCAL_CFLAGS += -std=gnu11
include $(BUILD_EXECUTABLE)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := magiskhide
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := main.c hide.c list_monitor.c proc_monitor.c util.c
LOCAL_CFLAGS += -std=gnu11
include $(BUILD_EXECUTABLE)
\ No newline at end of file
#define _GNU_SOURCE #include "magiskhide.h"
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mount.h>
#include <sys/inotify.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#define LOGFILE "/cache/magisk.log"
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
#define DUMMYPATH "/dev/magisk/dummy"
FILE *logfile;
int i, list_size, pipefd[2];
char **hide_list = NULL, buffer[512];
pthread_t list_monitor;
pthread_mutex_t mutex;
char **file_to_str_arr(FILE *fp, int *size) {
int allocated = 16;
char *line = NULL, **array;
size_t len = 0;
ssize_t read;
array = (char **) malloc(sizeof(char*) * allocated);
*size = 0;
while ((read = getline(&line, &len, fp)) != -1) {
if (*size >= allocated) {
// Double our allocation and re-allocate
allocated *= 2;
array = (char **) realloc(array, sizeof(char*) * allocated);
}
// Remove end newline
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
}
array[*size] = line;
line = NULL;
++(*size);
}
return array;
}
void read_namespace(const int pid, char* target, const size_t size) {
char path[32];
snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
ssize_t len = readlink(path, target, size);
target[len] = '\0';
}
void lazy_unmount(const char* mountpoint) {
if (umount2(mountpoint, MNT_DETACH) != -1)
fprintf(logfile, "MagiskHide: Unmounted (%s)\n", mountpoint);
else
fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint);
}
int hideMagisk() { int hideMagisk() {
close(pipefd[1]); close(pipefd[1]);
...@@ -175,153 +110,3 @@ int hideMagisk() { ...@@ -175,153 +110,3 @@ int hideMagisk() {
// Should never go here // Should never go here
return 1; return 1;
} }
void update_list(const char *listpath) {
FILE *hide_fp = fopen(listpath, "r");
if (hide_fp == NULL) {
fprintf(logfile, "MagiskHide: Error opening hide list\n");
exit(1);
}
pthread_mutex_lock(&mutex);
if (hide_list) {
// Free memory
for(i = 0; i < list_size; ++i)
free(hide_list[i]);
free(hide_list);
}
hide_list = file_to_str_arr(hide_fp, &list_size);
pthread_mutex_unlock(&mutex);
fclose(hide_fp);
if (list_size) fprintf(logfile, "MagiskHide: Update process/package list:\n");
for(i = 0; i < list_size; i++)
fprintf(logfile, "MagiskHide: [%s]\n", hide_list[i]);
}
void quit_pthread(int sig) {
// Free memory
for(i = 0; i < list_size; ++i)
free(hide_list[i]);
free(hide_list);
pthread_exit(NULL);
}
void *monitor_list(void *path) {
char* listpath = (char*) path;
signal(SIGQUIT, quit_pthread);
int inotifyFd = -1;
char str[512];
while(1) {
if (inotifyFd == -1 || read(inotifyFd, str, sizeof(str)) == -1) {
close(inotifyFd);
inotifyFd = inotify_init();
if (inotifyFd == -1) {
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
exit(1);
}
if (inotify_add_watch(inotifyFd, listpath, IN_MODIFY) == -1) {
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
exit(1);
}
}
update_list(listpath);
}
return NULL;
}
void terminate(int sig) {
// Close the config list monitor
pthread_kill(list_monitor, SIGQUIT);
pthread_mutex_destroy(&mutex);
// Terminate our children
i = -1;
write(pipefd[1], &i, sizeof(i));
}
void run_as_daemon() {
switch(fork()) {
case -1:
exit(-1);
case 0:
if (setsid() < 0)
exit(-1);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
logfile = fopen(LOGFILE, "a+");
setbuf(logfile, NULL);
break;
default:
exit(0);
}
}
int main(int argc, char **argv, char **envp) {
run_as_daemon();
// Handle all killing signals
signal(SIGINT, terminate);
signal(SIGKILL, terminate);
signal(SIGTERM, terminate);
// Fork a child to handle namespace switches and unmounts
pipe(pipefd);
switch(fork()) {
case -1:
exit(-1);
case 0:
return hideMagisk();
default:
break;
}
close(pipefd[0]);
// Start a thread to constantly check the hide list
pthread_mutex_init(&mutex, NULL);
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
// Monitor am_proc_start in main thread
FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
while(!feof(p)) {
//Format of am_proc_start is (as of Android 5.1 and 6.0)
//UserID, pid, unix uid, processName, hostingType, hostingName
fgets(buffer, sizeof(buffer), p);
char *pos = buffer;
while(1) {
pos = strchr(pos, ',');
if(pos == NULL)
break;
pos[0] = ' ';
}
int pid;
char processName[256];
int ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName);
if(ret != 2)
continue;
for (i = 0; i < list_size; ++i) {
if(strcmp(processName, hide_list[i]) == 0) {
// Check PID exist
if (kill(pid, 0) == -1) continue;
fprintf(logfile, "MagiskHide: %s(PID=%d ", processName, pid);
write(pipefd[1], &pid, sizeof(pid));
}
}
}
// Close the logcat monitor
pclose(p);
terminate(0);
fprintf(logfile, "MagiskHide: Cannot read from logcat, abort...\n");
fclose(logfile);
return 1;
}
#include "magiskhide.h"
void *monitor_list(void *path) {
char* listpath = (char*) path;
signal(SIGQUIT, quit_pthread);
int inotifyFd = -1;
char str[512];
while(1) {
if (inotifyFd == -1 || read(inotifyFd, str, sizeof(str)) == -1) {
close(inotifyFd);
inotifyFd = inotify_init();
if (inotifyFd == -1) {
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
exit(1);
}
if (inotify_add_watch(inotifyFd, listpath, IN_MODIFY) == -1) {
fprintf(logfile, "MagiskHide: Unable to watch %s\n", listpath);
exit(1);
}
}
update_list(listpath);
}
return NULL;
}
void update_list(const char *listpath) {
FILE *hide_fp = fopen(listpath, "r");
if (hide_fp == NULL) {
fprintf(logfile, "MagiskHide: Error opening hide list\n");
exit(1);
}
pthread_mutex_lock(&mutex);
if (hide_list) {
// Free memory
for(i = 0; i < list_size; ++i)
free(hide_list[i]);
free(hide_list);
}
hide_list = file_to_str_arr(hide_fp, &list_size);
pthread_mutex_unlock(&mutex);
fclose(hide_fp);
if (list_size) fprintf(logfile, "MagiskHide: Update process/package list:\n");
for(i = 0; i < list_size; i++)
fprintf(logfile, "MagiskHide: [%s]\n", hide_list[i]);
}
void quit_pthread(int sig) {
// Free memory
for(i = 0; i < list_size; ++i)
free(hide_list[i]);
free(hide_list);
pthread_exit(NULL);
}
#ifndef MAGISK_HIDE_H
#define MAGISK_HIDE_H
#define _GNU_SOURCE
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mount.h>
#include <sys/inotify.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#define LOGFILE "/cache/magisk.log"
#define HIDELIST "/magisk/.core/magiskhide/hidelist"
#define DUMMYPATH "/dev/magisk/dummy"
// Main thread
void monitor_proc();
// Forked process for namespace setting
int hideMagisk();
// List monitor thread
void update_list(const char *listpath);
void quit_pthread(int sig);
void *monitor_list(void *path);
// Util functions
char **file_to_str_arr(FILE *fp, int *size);
void read_namespace(const int pid, char* target, const size_t size);
void lazy_unmount(const char* mountpoint);
void run_as_daemon();
// Global variable sharing through process/threads
extern FILE *logfile;
extern int i, list_size, pipefd[2];
extern char **hide_list, buffer[512];
extern pthread_t list_monitor;
extern pthread_mutex_t mutex;
#endif
#include "magiskhide.h"
FILE *logfile;
int i, list_size, pipefd[2];
char **hide_list = NULL, buffer[512];
pthread_t list_monitor;
pthread_mutex_t mutex;
static void terminate(int sig) {
// Close the config list monitor
pthread_kill(list_monitor, SIGQUIT);
pthread_mutex_destroy(&mutex);
// Terminate our children
i = -1;
write(pipefd[1], &i, sizeof(i));
}
int main(int argc, char **argv, char **envp) {
run_as_daemon();
// Handle all killing signals
signal(SIGINT, terminate);
signal(SIGKILL, terminate);
signal(SIGTERM, terminate);
// Fork a child to handle namespace switches and unmounts
pipe(pipefd);
switch(fork()) {
case -1:
exit(-1);
case 0:
return hideMagisk();
default:
break;
}
close(pipefd[0]);
// Start a thread to constantly check the hide list
pthread_mutex_init(&mutex, NULL);
pthread_create(&list_monitor, NULL, monitor_list, HIDELIST);
monitor_proc();
terminate(0);
fprintf(logfile, "MagiskHide: Cannot monitor am_proc_start, abort...\n");
fclose(logfile);
return 1;
}
\ No newline at end of file
#include "magiskhide.h"
void monitor_proc() {
// Monitor am_proc_start in main thread
FILE *p = popen("while true; do logcat -b events -v raw -s am_proc_start; sleep 1; done", "r");
while(!feof(p)) {
//Format of am_proc_start is (as of Android 5.1 and 6.0)
//UserID, pid, unix uid, processName, hostingType, hostingName
fgets(buffer, sizeof(buffer), p);
char *pos = buffer;
while(1) {
pos = strchr(pos, ',');
if(pos == NULL)
break;
pos[0] = ' ';
}
int pid;
char processName[256];
int ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName);
if(ret != 2)
continue;
for (i = 0; i < list_size; ++i) {
if(strcmp(processName, hide_list[i]) == 0) {
// Check PID exist
if (kill(pid, 0) == -1) continue;
fprintf(logfile, "MagiskHide: %s(PID=%d ", processName, pid);
write(pipefd[1], &pid, sizeof(pid));
}
}
}
// Close the logcat monitor
pclose(p);
}
\ No newline at end of file
#include "magiskhide.h"
char **file_to_str_arr(FILE *fp, int *size) {
int allocated = 16;
char *line = NULL, **array;
size_t len = 0;
ssize_t read;
array = (char **) malloc(sizeof(char*) * allocated);
*size = 0;
while ((read = getline(&line, &len, fp)) != -1) {
if (*size >= allocated) {
// Double our allocation and re-allocate
allocated *= 2;
array = (char **) realloc(array, sizeof(char*) * allocated);
}
// Remove end newline
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
}
array[*size] = line;
line = NULL;
++(*size);
}
return array;
}
void read_namespace(const int pid, char* target, const size_t size) {
char path[32];
snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
ssize_t len = readlink(path, target, size);
target[len] = '\0';
}
void lazy_unmount(const char* mountpoint) {
if (umount2(mountpoint, MNT_DETACH) != -1)
fprintf(logfile, "MagiskHide: Unmounted (%s)\n", mountpoint);
else
fprintf(logfile, "MagiskHide: Unmount Failed (%s)\n", mountpoint);
}
void run_as_daemon() {
switch(fork()) {
case -1:
exit(-1);
case 0:
if (setsid() < 0)
exit(-1);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
logfile = fopen(LOGFILE, "a+");
setbuf(logfile, NULL);
break;
default:
exit(0);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment