Magisk/jni/magiskhide/magiskhide.c

168 lines
3.6 KiB
C
Raw Permalink Normal View History

2017-04-06 06:12:29 +08:00
/* magiskhide.c - initialize the environment for Magiskhide
*/
2016-12-31 02:44:24 +08:00
2017-04-06 06:12:29 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
2017-04-07 07:50:02 +08:00
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
2017-04-07 07:50:02 +08:00
#include <sys/types.h>
2016-12-31 02:44:24 +08:00
2017-04-06 06:12:29 +08:00
#include "magisk.h"
#include "utils.h"
#include "magiskhide.h"
#include "daemon.h"
2017-04-20 22:45:56 +08:00
#include "resetprop.h"
2016-12-31 02:44:24 +08:00
2017-04-20 22:45:56 +08:00
struct vector *hide_list = NULL;
2016-12-31 02:44:24 +08:00
2017-04-20 22:45:56 +08:00
int hideEnabled = 0;
2017-04-06 06:12:29 +08:00
static pthread_t proc_monitor_thread;
2017-05-04 02:58:37 +08:00
pthread_mutex_t hide_lock, file_lock;
2017-01-01 18:54:13 +08:00
void kill_proc(int pid) {
2017-04-07 07:50:02 +08:00
kill(pid, SIGTERM);
}
static void usage(char *arg0) {
fprintf(stderr,
"MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"
"%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) {
2017-04-24 21:43:30 +08:00
// We manually handle crashes
err_handler = do_nothing;
2017-04-22 00:54:08 +08:00
if (hideEnabled) {
2017-07-02 23:04:57 +08:00
if (client > 0) {
write_int(client, HIDE_IS_ENABLED);
close(client);
}
2017-04-22 00:54:08 +08:00
return;
}
2016-12-31 02:44:24 +08:00
2017-04-20 22:45:56 +08:00
hideEnabled = 1;
2017-07-02 23:04:57 +08:00
LOGI("* Starting MagiskHide\n");
2017-04-24 21:43:30 +08:00
2017-07-02 23:04:57 +08:00
if (client > 0) {
2017-05-26 22:05:04 +08:00
if (setprop(MAGISKHIDE_PROP, "1"))
2017-05-01 01:58:52 +08:00
goto error;
}
2017-04-20 22:45:56 +08:00
2017-04-17 16:36:49 +08:00
hide_sensitive_props();
2017-05-12 15:28:15 +08:00
// Initialize the mutex lock
pthread_mutex_init(&hide_lock, NULL);
pthread_mutex_init(&file_lock, NULL);
2017-04-06 06:12:29 +08:00
// Initialize the hide list
2017-04-20 22:45:56 +08:00
if (init_list())
goto error;
2017-04-20 22:45:56 +08:00
// Add SafetyNet by default
add_list(strdup("com.google.android.gms.unstable"));
2016-12-31 02:44:24 +08:00
if (client > 0) {
write_int(client, DAEMON_SUCCESS);
close(client);
}
2017-04-22 00:54:08 +08:00
// Get thread reference
proc_monitor_thread = pthread_self();
// Start monitoring
proc_monitor();
return;
2017-04-22 00:54:08 +08:00
error:
2017-04-20 22:45:56 +08:00
hideEnabled = 0;
if (client > 0) {
write_int(client, DAEMON_ERROR);
close(client);
}
return;
2017-04-06 06:12:29 +08:00
}
2016-12-31 02:44:24 +08:00
void stop_magiskhide(int client) {
2017-04-22 00:54:08 +08:00
if (!hideEnabled) {
2017-04-22 01:40:07 +08:00
write_int(client, HIDE_NOT_ENABLED);
2017-04-22 00:54:08 +08:00
close(client);
return;
2017-04-22 00:54:08 +08:00
}
LOGI("* Stopping MagiskHide\n");
2017-04-20 22:45:56 +08:00
hideEnabled = 0;
2017-05-26 22:05:04 +08:00
setprop(MAGISKHIDE_PROP, "0");
2017-04-22 00:54:08 +08:00
pthread_kill(proc_monitor_thread, SIGUSR1);
2017-05-05 16:13:26 +08:00
write_int(client, DAEMON_SUCCESS);
2017-04-15 03:23:09 +08:00
close(client);
2017-04-06 06:12:29 +08:00
}
2016-12-31 02:44:24 +08:00
2017-04-06 06:12:29 +08:00
int magiskhide_main(int argc, char *argv[]) {
if (argc < 2) {
usage(argv[0]);
}
2017-05-05 16:13:26 +08:00
client_request req = DO_NOTHING;
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) {
2017-06-03 05:52:49 +08:00
FILE *fp = fopen(HIDELIST, "r");
if (fp == NULL)
return 1;
2017-04-15 03:23:09 +08:00
char buffer[512];
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
fclose(fp);
return 0;
}
int fd = connect_daemon();
write_int(fd, req);
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
write_string(fd, argv[2]);
}
2017-05-05 16:13:26 +08:00
daemon_response code = read_int(fd);
close(fd);
2017-04-22 01:40:07 +08:00
switch (code) {
2017-05-05 16:13:26 +08:00
case DAEMON_ERROR:
2017-04-22 01:40:07 +08:00
fprintf(stderr, "Error occured in daemon...\n");
break;
2017-05-05 16:13:26 +08:00
case DAEMON_SUCCESS:
break;
case ROOT_REQUIRED:
fprintf(stderr, "Root is required for this operation\n");
2017-04-22 01:40:07 +08:00
break;
case HIDE_NOT_ENABLED:
fprintf(stderr, "Magisk hide is not enabled yet\n");
break;
case HIDE_IS_ENABLED:
fprintf(stderr, "Magisk hide is already enabled\n");
break;
case HIDE_ITEM_EXIST:
fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]);
break;
case HIDE_ITEM_NOT_EXIST:
fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]);
break;
}
return code;
2017-04-06 06:12:29 +08:00
}