Magisk/native/jni/magiskhide/magiskhide.cpp

164 lines
3.6 KiB
C++
Raw Normal View History

2017-04-05 22:12:29 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
2017-04-06 23:50:02 +00:00
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
2017-04-06 23:50:02 +00:00
#include <sys/types.h>
#include <sys/mount.h>
2016-12-30 18:44:24 +00:00
2020-03-09 08:50:30 +00:00
#include <daemon.hpp>
#include <utils.hpp>
2019-02-10 08:57:51 +00:00
#include <flags.h>
2020-03-09 08:50:30 +00:00
#include "magiskhide.hpp"
2016-12-30 18:44:24 +00:00
using namespace std::literals;
2018-11-16 05:37:41 +00:00
bool hide_enabled = false;
2017-01-01 10:54:13 +00:00
[[noreturn]] static void usage(char *arg0) {
fprintf(stderr,
2019-02-12 10:17:02 +00:00
FULL_VER(MagiskHide) "\n\n"
2019-05-26 09:59:38 +00:00
"Usage: %s [action [arguments...] ]\n\n"
"Actions:\n"
" status Return the status of magiskhide\n"
" enable Start magiskhide\n"
" disable Stop magiskhide\n"
" add PKG [PROC] Add a new target to the hide list\n"
" rm PKG [PROC] Remove target(s) from the hide list\n"
" ls Print the current hide list\n"
" exec CMDs... Execute commands in isolated mount\n"
" namespace and do all hide unmounts\n"
#ifdef MAGISK_DEBUG
2019-05-26 09:59:38 +00:00
" test Run process monitor test\n"
#endif
, arg0);
exit(1);
}
void magiskhide_handler(int client) {
int req = read_int(client);
int res = DAEMON_ERROR;
switch (req) {
case STOP_MAGISKHIDE:
case ADD_HIDELIST:
case RM_HIDELIST:
case LS_HIDELIST:
if (!hide_enabled) {
write_int(client, HIDE_NOT_ENABLED);
close(client);
return;
}
}
switch (req) {
case LAUNCH_MAGISKHIDE:
launch_magiskhide(client);
return;
case STOP_MAGISKHIDE:
res = stop_magiskhide();
break;
case ADD_HIDELIST:
res = add_list(client);
break;
case RM_HIDELIST:
res = rm_list(client);
break;
case LS_HIDELIST:
ls_list(client);
client = -1;
break;
2018-11-16 05:37:41 +00:00
case HIDE_STATUS:
res = hide_enabled ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED;
break;
}
write_int(client, res);
2017-04-14 19:23:09 +00:00
close(client);
2017-04-05 22:12:29 +00:00
}
2016-12-30 18:44:24 +00:00
2017-04-05 22:12:29 +00:00
int magiskhide_main(int argc, char *argv[]) {
2018-11-16 05:37:41 +00:00
if (argc < 2)
usage(argv[0]);
2018-11-16 05:37:41 +00:00
2019-05-26 09:59:38 +00:00
// CLI backwards compatibility
const char *opt = argv[1];
if (opt[0] == '-' && opt[1] == '-')
opt += 2;
int req;
2019-05-26 09:59:38 +00:00
if (opt == "enable"sv)
req = LAUNCH_MAGISKHIDE;
2019-05-26 09:59:38 +00:00
else if (opt == "disable"sv)
req = STOP_MAGISKHIDE;
2019-05-26 09:59:38 +00:00
else if (opt == "add"sv)
req = ADD_HIDELIST;
2019-05-26 09:59:38 +00:00
else if (opt == "rm"sv)
req = RM_HIDELIST;
2019-05-26 09:59:38 +00:00
else if (opt == "ls"sv)
req = LS_HIDELIST;
2019-05-26 09:59:38 +00:00
else if (opt == "status"sv)
2018-11-16 05:37:41 +00:00
req = HIDE_STATUS;
else if (opt == "exec"sv && argc > 2) {
xunshare(CLONE_NEWNS);
xmount(nullptr, "/", nullptr, MS_PRIVATE | MS_REC, nullptr);
hide_unmount();
execvp(argv[2], argv + 2);
exit(1);
}
#ifdef MAGISK_DEBUG
2019-05-26 09:59:38 +00:00
else if (opt == "test"sv)
test_proc_monitor();
#endif
2018-11-16 05:37:41 +00:00
else
2017-10-10 11:49:15 +00:00
usage(argv[0]);
2018-11-16 05:37:41 +00:00
// Send request
2018-06-16 17:28:29 +00:00
int fd = connect_daemon();
write_int(fd, MAGISKHIDE);
write_int(fd, req);
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
write_string(fd, argv[2]);
write_string(fd, argv[3] ? argv[3] : "");
}
2018-11-16 06:49:15 +00:00
if (req == LS_HIDELIST)
send_fd(fd, STDOUT_FILENO);
2018-11-16 05:37:41 +00:00
// Get response
2018-02-11 09:23:36 +00:00
int code = read_int(fd);
2017-04-21 17:40:07 +00:00
switch (code) {
2017-05-05 08:13:26 +00:00
case DAEMON_SUCCESS:
break;
2017-04-21 17:40:07 +00:00
case HIDE_NOT_ENABLED:
2018-11-16 05:37:41 +00:00
fprintf(stderr, "MagiskHide is not enabled\n");
break;
2017-04-21 17:40:07 +00:00
case HIDE_IS_ENABLED:
2018-11-16 05:37:41 +00:00
fprintf(stderr, "MagiskHide is enabled\n");
break;
2017-04-21 17:40:07 +00:00
case HIDE_ITEM_EXIST:
fprintf(stderr, "Target already exists in hide list\n");
2018-11-16 05:37:41 +00:00
break;
2017-04-21 17:40:07 +00:00
case HIDE_ITEM_NOT_EXIST:
fprintf(stderr, "Target does not exist in hide list\n");
2018-11-16 05:37:41 +00:00
break;
case HIDE_NO_NS:
fprintf(stderr, "Your kernel doesn't support mount namespace\n");
break;
2019-09-01 06:16:12 +00:00
case HIDE_INVALID_PKG:
fprintf(stderr, "Invalid package / process name\n");
break;
2018-11-16 05:37:41 +00:00
case ROOT_REQUIRED:
fprintf(stderr, "Root is required for this operation\n");
break;
2018-02-11 09:23:36 +00:00
case DAEMON_ERROR:
default:
fprintf(stderr, "Daemon error\n");
2018-11-16 05:37:41 +00:00
return DAEMON_ERROR;
}
2018-11-16 05:37:41 +00:00
return req == HIDE_STATUS ? (code == HIDE_IS_ENABLED ? 0 : 1) : code != DAEMON_SUCCESS;
2017-04-05 22:12:29 +00:00
}