mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 16:51:23 +00:00
Reduce C++ wizardry
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
#include <selinux.hpp>
|
||||
|
||||
#include "core.hpp"
|
||||
#include "zygisk/deny/deny.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -264,6 +263,7 @@ static bool check_key_combo() {
|
||||
***********************/
|
||||
|
||||
static pthread_mutex_t stage_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
extern int disable_deny();
|
||||
|
||||
void post_fs_data(int client) {
|
||||
// ack
|
||||
|
@@ -133,34 +133,33 @@ static void poll_ctrl_handler(pollfd *pfd) {
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_request_async(int client, DaemonRequest code, const sock_cred &cred) {
|
||||
// using enum DAEMON_REQUEST;
|
||||
static void handle_request_async(int client, int code, const sock_cred &cred) {
|
||||
switch (code) {
|
||||
case DaemonRequest::DENYLIST:
|
||||
case MainRequest::DENYLIST:
|
||||
denylist_handler(client, &cred);
|
||||
break;
|
||||
case DaemonRequest::SUPERUSER:
|
||||
case MainRequest::SUPERUSER:
|
||||
su_daemon_handler(client, &cred);
|
||||
break;
|
||||
case DaemonRequest::POST_FS_DATA:
|
||||
case MainRequest::POST_FS_DATA:
|
||||
post_fs_data(client);
|
||||
break;
|
||||
case DaemonRequest::LATE_START:
|
||||
case MainRequest::LATE_START:
|
||||
late_start(client);
|
||||
break;
|
||||
case DaemonRequest::BOOT_COMPLETE:
|
||||
case MainRequest::BOOT_COMPLETE:
|
||||
boot_complete(client);
|
||||
break;
|
||||
case DaemonRequest::SQLITE_CMD:
|
||||
case MainRequest::SQLITE_CMD:
|
||||
exec_sql(client);
|
||||
break;
|
||||
case DaemonRequest::REMOVE_MODULES:
|
||||
case MainRequest::REMOVE_MODULES:
|
||||
remove_modules();
|
||||
write_int(client, 0);
|
||||
close(client);
|
||||
reboot();
|
||||
break;
|
||||
case DaemonRequest::ZYGISK_REQUEST:
|
||||
case MainRequest::ZYGISK:
|
||||
zygisk_handler(client, &cred);
|
||||
break;
|
||||
default:
|
||||
@@ -168,22 +167,21 @@ static void handle_request_async(int client, DaemonRequest code, const sock_cred
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_request_sync(int client, DaemonRequest code) {
|
||||
// using enum DAEMON_REQUEST;
|
||||
static void handle_request_sync(int client, int code) {
|
||||
switch (code) {
|
||||
case DaemonRequest::CHECK_VERSION:
|
||||
case MainRequest::CHECK_VERSION:
|
||||
write_string(client, MAGISK_VERSION ":MAGISK");
|
||||
break;
|
||||
case DaemonRequest::CHECK_VERSION_CODE:
|
||||
case MainRequest::CHECK_VERSION_CODE:
|
||||
write_int(client, MAGISK_VER_CODE);
|
||||
break;
|
||||
case DaemonRequest::GET_PATH:
|
||||
case MainRequest::GET_PATH:
|
||||
write_string(client, MAGISKTMP.data());
|
||||
break;
|
||||
case DaemonRequest::START_DAEMON:
|
||||
case MainRequest::START_DAEMON:
|
||||
setup_logfile(true);
|
||||
break;
|
||||
case DaemonRequest::STOP_DAEMON:
|
||||
case MainRequest::STOP_DAEMON:
|
||||
denylist_handler(-1, nullptr);
|
||||
write_int(client, 0);
|
||||
// Terminate the daemon!
|
||||
@@ -201,13 +199,7 @@ static bool is_client(pid_t pid) {
|
||||
return !(stat(path, &st) || st.st_dev != self_st.st_dev || st.st_ino != self_st.st_ino);
|
||||
}
|
||||
|
||||
inline static void write_response(int client, DaemonResponse res) {
|
||||
write_int(client, static_cast<std::underlying_type_t<DaemonResponse>>(res));
|
||||
}
|
||||
|
||||
static void handle_request(pollfd *pfd) {
|
||||
// using enum DAEMON_REQUEST;
|
||||
// using enum DAEMON_RESPONSE;
|
||||
int client = xaccept4(pfd->fd, nullptr, nullptr, SOCK_CLOEXEC);
|
||||
|
||||
// Verify client credentials
|
||||
@@ -215,7 +207,6 @@ static void handle_request(pollfd *pfd) {
|
||||
bool is_root;
|
||||
bool is_zygote;
|
||||
int code;
|
||||
DaemonRequest req;
|
||||
|
||||
if (!get_client_cred(client, &cred))
|
||||
goto done;
|
||||
@@ -226,30 +217,27 @@ static void handle_request(pollfd *pfd) {
|
||||
goto done;
|
||||
|
||||
code = read_int(client);
|
||||
static_assert(is_scoped_enum_v<DaemonRequest>);
|
||||
|
||||
if (code < 0 || code >= DaemonRequest::END || code == DaemonRequest::_SYNC_BARRIER_)
|
||||
if (code < 0 || code >= MainRequest::END || code == MainRequest::_SYNC_BARRIER_)
|
||||
goto done;
|
||||
|
||||
req = static_cast<DaemonRequest>(code);
|
||||
|
||||
// Check client permissions
|
||||
switch (req) {
|
||||
case DaemonRequest::POST_FS_DATA:
|
||||
case DaemonRequest::LATE_START:
|
||||
case DaemonRequest::BOOT_COMPLETE:
|
||||
case DaemonRequest::SQLITE_CMD:
|
||||
case DaemonRequest::GET_PATH:
|
||||
case DaemonRequest::DENYLIST:
|
||||
case DaemonRequest::STOP_DAEMON:
|
||||
switch (code) {
|
||||
case MainRequest::POST_FS_DATA:
|
||||
case MainRequest::LATE_START:
|
||||
case MainRequest::BOOT_COMPLETE:
|
||||
case MainRequest::SQLITE_CMD:
|
||||
case MainRequest::GET_PATH:
|
||||
case MainRequest::DENYLIST:
|
||||
case MainRequest::STOP_DAEMON:
|
||||
if (!is_root) {
|
||||
write_response(client, DaemonResponse::ROOT_REQUIRED);
|
||||
write_int(client, MainResponse::ROOT_REQUIRED);
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case DaemonRequest::REMOVE_MODULES:
|
||||
case MainRequest::REMOVE_MODULES:
|
||||
if (!is_root && cred.uid != UID_SHELL) {
|
||||
write_response(client, DaemonResponse::ROOT_REQUIRED);
|
||||
write_int(client, MainResponse::ROOT_REQUIRED);
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
@@ -257,15 +245,15 @@ static void handle_request(pollfd *pfd) {
|
||||
break;
|
||||
}
|
||||
|
||||
write_response(client, DaemonResponse::OK);
|
||||
write_int(client, MainResponse::OK);
|
||||
|
||||
if (req < DaemonRequest::_SYNC_BARRIER_) {
|
||||
handle_request_sync(client, req);
|
||||
if (code < MainRequest::_SYNC_BARRIER_) {
|
||||
handle_request_sync(client, code);
|
||||
goto done;
|
||||
}
|
||||
|
||||
// Handle complex requests in another thread
|
||||
exec_task([=] { handle_request_async(client, req, cred); });
|
||||
exec_task([=] { handle_request_async(client, code, cred); });
|
||||
return;
|
||||
|
||||
done:
|
||||
@@ -402,8 +390,7 @@ static void daemon_entry() {
|
||||
poll_loop();
|
||||
}
|
||||
|
||||
int connect_daemon(DaemonRequest req, bool create) {
|
||||
// using enum DAEMON_RESPONSE;
|
||||
int connect_daemon(int req, bool create) {
|
||||
sockaddr_un sun{};
|
||||
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET);
|
||||
int fd = xsocket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
@@ -422,23 +409,23 @@ int connect_daemon(DaemonRequest req, bool create) {
|
||||
while (connect(fd, (struct sockaddr *) &sun, len))
|
||||
usleep(10000);
|
||||
}
|
||||
write_int(fd, static_cast<int>(req));
|
||||
int ret = read_int(fd);
|
||||
auto res = (ret < DaemonResponse::ERROR || ret >= DaemonResponse::END) ? DaemonResponse::ERROR
|
||||
: static_cast<DaemonResponse>(ret);
|
||||
write_int(fd, req);
|
||||
int res = read_int(fd);
|
||||
if (res < MainResponse::ERROR || res >= MainResponse::END)
|
||||
res = MainResponse::ERROR;
|
||||
switch (res) {
|
||||
case DaemonResponse::OK:
|
||||
case MainResponse::OK:
|
||||
break;
|
||||
case DaemonResponse::ERROR:
|
||||
case MainResponse::ERROR:
|
||||
LOGE("Daemon error\n");
|
||||
exit(-1);
|
||||
case DaemonResponse::ROOT_REQUIRED:
|
||||
case MainResponse::ROOT_REQUIRED:
|
||||
LOGE("Root is required for this operation\n");
|
||||
exit(-1);
|
||||
case DaemonResponse::INVALID_REQUEST:
|
||||
case MainResponse::INVALID_REQUEST:
|
||||
LOGE("Invalid request\n");
|
||||
exit(-1);
|
||||
case DaemonResponse::END:
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
return fd;
|
||||
|
@@ -50,19 +50,18 @@ Available applets:
|
||||
}
|
||||
|
||||
int magisk_main(int argc, char *argv[]) {
|
||||
// using enum DAEMON_REQUEST;
|
||||
if (argc < 2)
|
||||
usage();
|
||||
if (argv[1] == "-c"sv) {
|
||||
printf(MAGISK_VERSION ":MAGISK (" str(MAGISK_VER_CODE) ")\n");
|
||||
return 0;
|
||||
} else if (argv[1] == "-v"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::CHECK_VERSION);
|
||||
int fd = connect_daemon(MainRequest::CHECK_VERSION);
|
||||
string v = read_string(fd);
|
||||
printf("%s\n", v.data());
|
||||
return 0;
|
||||
} else if (argv[1] == "-V"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::CHECK_VERSION_CODE);
|
||||
int fd = connect_daemon(MainRequest::CHECK_VERSION_CODE);
|
||||
printf("%d\n", read_int(fd));
|
||||
return 0;
|
||||
} else if (argv[1] == "--list"sv) {
|
||||
@@ -82,25 +81,25 @@ int magisk_main(int argc, char *argv[]) {
|
||||
cp_afc(argv[2], argv[3]);
|
||||
return 0;
|
||||
} else if (argv[1] == "--daemon"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::START_DAEMON, true);
|
||||
int fd = connect_daemon(MainRequest::START_DAEMON, true);
|
||||
close(fd);
|
||||
return 0;
|
||||
} else if (argv[1] == "--stop"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::STOP_DAEMON);
|
||||
int fd = connect_daemon(MainRequest::STOP_DAEMON);
|
||||
return read_int(fd);
|
||||
} else if (argv[1] == "--post-fs-data"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::POST_FS_DATA, true);
|
||||
int fd = connect_daemon(MainRequest::POST_FS_DATA, true);
|
||||
return read_int(fd);
|
||||
} else if (argv[1] == "--service"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::LATE_START, true);
|
||||
int fd = connect_daemon(MainRequest::LATE_START, true);
|
||||
return read_int(fd);
|
||||
} else if (argv[1] == "--boot-complete"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::BOOT_COMPLETE, true);
|
||||
int fd = connect_daemon(MainRequest::BOOT_COMPLETE, true);
|
||||
return read_int(fd);
|
||||
} else if (argv[1] == "--denylist"sv) {
|
||||
return denylist_cli(argc - 1, argv + 1);
|
||||
} else if (argc >= 3 && argv[1] == "--sqlite"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::SQLITE_CMD);
|
||||
int fd = connect_daemon(MainRequest::SQLITE_CMD);
|
||||
write_string(fd, argv[2]);
|
||||
string res;
|
||||
for (;;) {
|
||||
@@ -110,10 +109,10 @@ int magisk_main(int argc, char *argv[]) {
|
||||
printf("%s\n", res.data());
|
||||
}
|
||||
} else if (argv[1] == "--remove-modules"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::REMOVE_MODULES);
|
||||
int fd = connect_daemon(MainRequest::REMOVE_MODULES);
|
||||
return read_int(fd);
|
||||
} else if (argv[1] == "--path"sv) {
|
||||
int fd = connect_daemon(DaemonRequest::GET_PATH);
|
||||
int fd = connect_daemon(MainRequest::GET_PATH);
|
||||
string path = read_string(fd);
|
||||
printf("%s\n", path.data());
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user