2017-04-06 00:12:29 +02:00
|
|
|
/* magiskhide.c - initialize the environment for Magiskhide
|
|
|
|
*/
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
// TODO: Functions to modify hiding list
|
2017-04-05 00:08:53 +02:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
2017-04-07 01:50:02 +02:00
|
|
|
#include <signal.h>
|
2017-04-09 01:25:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
2017-04-07 01:50:02 +02:00
|
|
|
#include <sys/types.h>
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
#include "magisk.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "magiskhide.h"
|
2017-04-09 01:25:10 +02:00
|
|
|
#include "daemon.h"
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-15 20:43:19 +02:00
|
|
|
int sv[2], hide_pid = -1;
|
2017-04-06 00:12:29 +02:00
|
|
|
struct vector *hide_list, *new_list;
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
int isEnabled = 0;
|
2017-04-06 00:12:29 +02:00
|
|
|
static pthread_t proc_monitor_thread;
|
2017-01-01 11:54:13 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
void kill_proc(int pid) {
|
2017-04-07 01:50:02 +02:00
|
|
|
kill(pid, SIGTERM);
|
|
|
|
}
|
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
static void usage(char *arg0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s [--options [arguments...] ]\n\n"
|
|
|
|
"Options:\n"
|
|
|
|
" --enable: Start the magiskhide daemon\n"
|
|
|
|
" --disable: Stop the magiskhide daemon\n"
|
|
|
|
" --add <process name>: Add <process name> to the list\n"
|
|
|
|
" --rm <process name>: Remove <process name> from the list\n"
|
|
|
|
" --ls: Print out the current hide list\n"
|
|
|
|
, arg0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void launch_magiskhide(int client) {
|
|
|
|
if (isEnabled) {
|
|
|
|
write_int(client, 0);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-06 00:12:29 +02:00
|
|
|
/*
|
|
|
|
* The setns system call do not support multithread processes
|
|
|
|
* We have to fork a new process, and communicate with pipe
|
|
|
|
*/
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
LOGI("* Starting MagiskHide\n");
|
|
|
|
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1)
|
|
|
|
goto error;
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
// Launch the hide daemon
|
2017-04-09 01:25:10 +02:00
|
|
|
if (hide_daemon())
|
|
|
|
goto error;
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
close(sv[1]);
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
// Initialize the hide list
|
2017-04-09 01:25:10 +02:00
|
|
|
hide_list = new_list = xmalloc(sizeof(*hide_list));
|
|
|
|
if (hide_list == NULL)
|
|
|
|
goto error;
|
2017-04-06 00:12:29 +02:00
|
|
|
vec_init(hide_list);
|
|
|
|
FILE *fp = xfopen(HIDELIST, "r");
|
2017-04-09 01:25:10 +02:00
|
|
|
if (fp == NULL)
|
|
|
|
goto error;
|
2017-04-06 00:12:29 +02:00
|
|
|
file_to_vector(hide_list, fp);
|
|
|
|
fclose(fp);
|
2017-04-07 01:50:02 +02:00
|
|
|
char *line;
|
|
|
|
vec_for_each(hide_list, line) {
|
|
|
|
LOGI("hide_list: [%s]\n", line);
|
|
|
|
ps_filter_proc_name(line, kill_proc);
|
|
|
|
}
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
// Start a new thread to monitor processes
|
2017-04-09 01:25:10 +02:00
|
|
|
if (xpthread_create(&proc_monitor_thread, NULL, proc_monitor, NULL))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
isEnabled = 1;
|
|
|
|
write_int(client, 0);
|
2017-04-14 21:23:09 +02:00
|
|
|
close(client);
|
2017-04-09 01:25:10 +02:00
|
|
|
return;
|
|
|
|
error:
|
|
|
|
write_int(client, 1);
|
2017-04-14 21:23:09 +02:00
|
|
|
close(client);
|
2017-04-15 20:43:19 +02:00
|
|
|
if (hide_pid != -1) {
|
|
|
|
int kill = -1;
|
|
|
|
// Kill hide daemon
|
|
|
|
write(sv[0], &kill, sizeof(kill));
|
|
|
|
close(sv[0]);
|
|
|
|
waitpid(hide_pid, NULL, 0);
|
|
|
|
}
|
2017-04-09 01:25:10 +02:00
|
|
|
return;
|
2017-04-06 00:12:29 +02:00
|
|
|
}
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-09 01:25:10 +02:00
|
|
|
void stop_magiskhide(int client) {
|
|
|
|
if (!isEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOGI("* Stopping MagiskHide\n");
|
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
int kill = -1;
|
|
|
|
// Terminate hide daemon
|
2017-04-09 01:25:10 +02:00
|
|
|
write(sv[0], &kill, sizeof(kill));
|
|
|
|
close(sv[0]);
|
|
|
|
waitpid(hide_pid, NULL, 0);
|
2017-04-06 00:12:29 +02:00
|
|
|
// Stop process monitor
|
|
|
|
pthread_kill(proc_monitor_thread, SIGUSR1);
|
|
|
|
pthread_join(proc_monitor_thread, NULL);
|
2017-04-09 01:25:10 +02:00
|
|
|
|
|
|
|
isEnabled = 0;
|
|
|
|
write_int(client, 0);
|
2017-04-14 21:23:09 +02:00
|
|
|
close(client);
|
2017-04-06 00:12:29 +02:00
|
|
|
}
|
2016-12-30 19:44:24 +01:00
|
|
|
|
2017-04-06 00:12:29 +02:00
|
|
|
int magiskhide_main(int argc, char *argv[]) {
|
2017-04-09 01:25:10 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
client_request req;
|
|
|
|
if (strcmp(argv[1], "--enable") == 0) {
|
|
|
|
req = LAUNCH_MAGISKHIDE;
|
|
|
|
} else if (strcmp(argv[1], "--disable") == 0) {
|
|
|
|
req = STOP_MAGISKHIDE;
|
|
|
|
} else if (strcmp(argv[1], "--add") == 0 && argc > 2) {
|
|
|
|
req = ADD_HIDELIST;
|
|
|
|
} else if (strcmp(argv[1], "--rm") == 0 && argc > 2) {
|
|
|
|
req = RM_HIDELIST;
|
|
|
|
} else if (strcmp(argv[1], "--ls") == 0) {
|
|
|
|
FILE *fp = xfopen(HIDELIST, "r");
|
2017-04-14 21:23:09 +02:00
|
|
|
char buffer[512];
|
|
|
|
while (fgets(buffer, sizeof(buffer), fp)) {
|
|
|
|
printf("%s", buffer);
|
2017-04-09 01:25:10 +02:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int fd = connect_daemon();
|
|
|
|
write_int(fd, req);
|
|
|
|
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
|
|
|
|
write_string(fd, argv[2]);
|
|
|
|
}
|
|
|
|
int code = read_int(fd);
|
|
|
|
close(fd);
|
|
|
|
if (code) {
|
|
|
|
fprintf(stderr, "Error occured in MagiskHide daemon\n");
|
|
|
|
}
|
|
|
|
return code;
|
2017-04-06 00:12:29 +02:00
|
|
|
}
|