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>
|
2017-04-09 07:25:10 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
2017-04-07 07:50:02 +08:00
|
|
|
#include <sys/types.h>
|
2019-06-04 22:27:19 -07:00
|
|
|
#include <sys/mount.h>
|
2016-12-31 02:44:24 +08:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include <daemon.hpp>
|
|
|
|
#include <utils.hpp>
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <flags.h>
|
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "magiskhide.hpp"
|
2016-12-31 02:44:24 +08:00
|
|
|
|
2019-05-25 16:08:53 -07:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2018-11-01 14:08:33 -04:00
|
|
|
[[noreturn]] static void usage(char *arg0) {
|
2017-04-09 07:25:10 +08:00
|
|
|
fprintf(stderr,
|
2020-03-23 01:17:13 -07:00
|
|
|
NAME_WITH_VER(MagiskHide) "\n\n"
|
2019-05-26 02:59:38 -07: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"
|
2019-06-04 22:27:19 -07:00
|
|
|
" exec CMDs... Execute commands in isolated mount\n"
|
|
|
|
" namespace and do all hide unmounts\n"
|
2019-05-26 02:53:28 -07:00
|
|
|
#ifdef MAGISK_DEBUG
|
2019-05-26 02:59:38 -07:00
|
|
|
" test Run process monitor test\n"
|
2019-05-26 02:53:28 -07:00
|
|
|
#endif
|
2017-04-09 07:25:10 +08:00
|
|
|
, arg0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:08:33 -04:00
|
|
|
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:
|
2020-05-16 23:31:30 -07:00
|
|
|
if (!hide_enabled()) {
|
2018-11-01 14:08:33 -04:00
|
|
|
write_int(client, HIDE_NOT_ENABLED);
|
|
|
|
close(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (req) {
|
|
|
|
case LAUNCH_MAGISKHIDE:
|
2020-05-17 14:45:08 -07:00
|
|
|
res = launch_magiskhide();
|
|
|
|
break;
|
2018-11-01 14:08:33 -04:00
|
|
|
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);
|
2020-05-17 14:45:08 -07:00
|
|
|
return;
|
2018-11-16 00:37:41 -05:00
|
|
|
case HIDE_STATUS:
|
2020-05-16 23:31:30 -07:00
|
|
|
res = hide_enabled() ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED;
|
2018-11-16 00:37:41 -05:00
|
|
|
break;
|
2018-11-01 14:08:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
write_int(client, res);
|
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[]) {
|
2018-11-16 00:37:41 -05:00
|
|
|
if (argc < 2)
|
2017-04-09 07:25:10 +08:00
|
|
|
usage(argv[0]);
|
2018-11-16 00:37:41 -05:00
|
|
|
|
2019-05-26 02:59:38 -07:00
|
|
|
// CLI backwards compatibility
|
|
|
|
const char *opt = argv[1];
|
|
|
|
if (opt[0] == '-' && opt[1] == '-')
|
|
|
|
opt += 2;
|
|
|
|
|
2018-11-01 14:08:33 -04:00
|
|
|
int req;
|
2019-05-26 02:59:38 -07:00
|
|
|
if (opt == "enable"sv)
|
2017-04-09 07:25:10 +08:00
|
|
|
req = LAUNCH_MAGISKHIDE;
|
2019-05-26 02:59:38 -07:00
|
|
|
else if (opt == "disable"sv)
|
2017-04-09 07:25:10 +08:00
|
|
|
req = STOP_MAGISKHIDE;
|
2019-05-26 02:59:38 -07:00
|
|
|
else if (opt == "add"sv)
|
2017-04-09 07:25:10 +08:00
|
|
|
req = ADD_HIDELIST;
|
2019-05-26 02:59:38 -07:00
|
|
|
else if (opt == "rm"sv)
|
2017-04-09 07:25:10 +08:00
|
|
|
req = RM_HIDELIST;
|
2019-05-26 02:59:38 -07:00
|
|
|
else if (opt == "ls"sv)
|
2017-09-06 02:25:40 +08:00
|
|
|
req = LS_HIDELIST;
|
2019-05-26 02:59:38 -07:00
|
|
|
else if (opt == "status"sv)
|
2018-11-16 00:37:41 -05:00
|
|
|
req = HIDE_STATUS;
|
2019-06-04 22:27:19 -07:00
|
|
|
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);
|
|
|
|
}
|
2019-05-26 02:53:28 -07:00
|
|
|
#ifdef MAGISK_DEBUG
|
2019-05-26 02:59:38 -07:00
|
|
|
else if (opt == "test"sv)
|
2019-05-25 16:08:53 -07:00
|
|
|
test_proc_monitor();
|
2019-05-26 02:53:28 -07:00
|
|
|
#endif
|
2018-11-16 00:37:41 -05:00
|
|
|
else
|
2017-10-10 19:49:15 +08:00
|
|
|
usage(argv[0]);
|
2018-11-01 14:08:33 -04:00
|
|
|
|
2018-11-16 00:37:41 -05:00
|
|
|
// Send request
|
2018-06-17 01:28:29 +08:00
|
|
|
int fd = connect_daemon();
|
2018-11-01 14:08:33 -04:00
|
|
|
write_int(fd, MAGISKHIDE);
|
2017-04-09 07:25:10 +08:00
|
|
|
write_int(fd, req);
|
2019-03-01 17:08:08 -05:00
|
|
|
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
|
2017-04-09 07:25:10 +08:00
|
|
|
write_string(fd, argv[2]);
|
2019-03-01 17:08:08 -05:00
|
|
|
write_string(fd, argv[3] ? argv[3] : "");
|
|
|
|
}
|
2018-11-16 01:49:15 -05:00
|
|
|
if (req == LS_HIDELIST)
|
|
|
|
send_fd(fd, STDOUT_FILENO);
|
2018-11-16 00:37:41 -05:00
|
|
|
|
|
|
|
// Get response
|
2018-02-11 17:23:36 +08:00
|
|
|
int code = read_int(fd);
|
2017-04-22 01:40:07 +08:00
|
|
|
switch (code) {
|
2017-05-05 16:13:26 +08:00
|
|
|
case DAEMON_SUCCESS:
|
|
|
|
break;
|
2017-04-22 01:40:07 +08:00
|
|
|
case HIDE_NOT_ENABLED:
|
2018-11-16 00:37:41 -05:00
|
|
|
fprintf(stderr, "MagiskHide is not enabled\n");
|
|
|
|
break;
|
2017-04-22 01:40:07 +08:00
|
|
|
case HIDE_IS_ENABLED:
|
2018-11-16 00:37:41 -05:00
|
|
|
fprintf(stderr, "MagiskHide is enabled\n");
|
|
|
|
break;
|
2017-04-22 01:40:07 +08:00
|
|
|
case HIDE_ITEM_EXIST:
|
2019-03-01 17:08:08 -05:00
|
|
|
fprintf(stderr, "Target already exists in hide list\n");
|
2018-11-16 00:37:41 -05:00
|
|
|
break;
|
2017-04-22 01:40:07 +08:00
|
|
|
case HIDE_ITEM_NOT_EXIST:
|
2019-03-01 17:08:08 -05:00
|
|
|
fprintf(stderr, "Target does not exist in hide list\n");
|
2018-11-16 00:37:41 -05:00
|
|
|
break;
|
2019-02-14 04:08:05 -05:00
|
|
|
case HIDE_NO_NS:
|
|
|
|
fprintf(stderr, "Your kernel doesn't support mount namespace\n");
|
|
|
|
break;
|
2019-09-01 14:16:12 +08:00
|
|
|
case HIDE_INVALID_PKG:
|
|
|
|
fprintf(stderr, "Invalid package / process name\n");
|
|
|
|
break;
|
2018-11-16 00:37:41 -05:00
|
|
|
case ROOT_REQUIRED:
|
|
|
|
fprintf(stderr, "Root is required for this operation\n");
|
|
|
|
break;
|
2018-02-11 17:23:36 +08:00
|
|
|
case DAEMON_ERROR:
|
|
|
|
default:
|
2019-02-14 04:08:05 -05:00
|
|
|
fprintf(stderr, "Daemon error\n");
|
2018-11-16 00:37:41 -05:00
|
|
|
return DAEMON_ERROR;
|
2017-09-06 02:25:40 +08:00
|
|
|
}
|
|
|
|
|
2018-11-16 00:37:41 -05:00
|
|
|
return req == HIDE_STATUS ? (code == HIDE_IS_ENABLED ? 0 : 1) : code != DAEMON_SUCCESS;
|
2017-04-06 06:12:29 +08:00
|
|
|
}
|