Allow ADB shell to remove modules and reboot

This commit is contained in:
topjohnwu 2019-09-13 03:14:21 -04:00
parent 86bfb22d4c
commit e31e687602
4 changed files with 60 additions and 45 deletions

View File

@ -1,10 +1,3 @@
/* bootstages.c - Core bootstage operations
*
* All bootstage operations, including simple mount in post-fs,
* magisk mount in post-fs-data, various image handling, script
* execution, load modules, install Magisk Manager etc.
*/
#include <sys/mount.h>
#include <sys/wait.h>
#include <stdlib.h>
@ -718,10 +711,7 @@ void late_start(int client) {
if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700);
// And reboot to make proper setup possible
if (RECOVERY_MODE)
exec_command_sync("/system/bin/reboot", "recovery");
else
exec_command_sync("/system/bin/reboot");
reboot();
}
if (access(BBPATH, F_OK) != 0){

View File

@ -1,9 +1,3 @@
/* daemon.c - Magisk Daemon
*
* Start the daemon and wait for requests
* Connect the daemon and send requests through sockets
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
@ -30,14 +24,20 @@ static void verify_client(int client, pid_t pid) {
// Verify caller is the same as server
char path[32];
sprintf(path, "/proc/%d/exe", pid);
struct stat st{};
stat(path, &st);
if (st.st_dev != SERVER_STAT.st_dev || st.st_ino != SERVER_STAT.st_ino) {
struct stat st;
if (stat(path, &st) || st.st_dev != SERVER_STAT.st_dev || st.st_ino != SERVER_STAT.st_ino) {
close(client);
pthread_exit(nullptr);
}
}
static void remove_modules() {
LOGI("* Remove all modules and reboot");
rm_rf(MODULEROOT);
rm_rf(MODULEUPGRADE);
reboot();
}
static void *request_handler(void *args) {
int client = *((int *) args);
delete (int *) args;
@ -95,6 +95,16 @@ static void *request_handler(void *args) {
LOGD("* Use broadcasts for su logging and notify\n");
CONNECT_BROADCAST = true;
close(client);
break;
case REMOVE_MODULES:
if (credential.uid == UID_SHELL || credential.uid == UID_ROOT) {
remove_modules();
write_int(client, 0);
} else {
write_int(client, 1);
}
close(client);
break;
default:
close(client);
break;
@ -169,6 +179,13 @@ static void main_daemon() {
}
}
void reboot() {
if (RECOVERY_MODE)
exec_command_sync("/system/bin/reboot", "recovery");
else
exec_command_sync("/system/bin/reboot");
}
int switch_mnt_ns(int pid) {
char mnt[32];
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);

View File

@ -16,31 +16,33 @@ using namespace std::literals;
[[noreturn]] static void usage() {
fprintf(stderr,
FULL_VER(Magisk) " multi-call binary\n"
"\n"
"Usage: magisk [applet [arguments]...]\n"
" or: magisk [options]...\n"
"\n"
"Options:\n"
" -c print current binary version\n"
" -v print running daemon version\n"
" -V print running daemon version code\n"
" --list list all available applets\n"
" --daemon manually start magisk daemon\n"
" --[init trigger] start service for init trigger\n"
"\n"
"Advanced Options (Internal APIs):\n"
" --unlock-blocks set BLKROSET flag to OFF for all block devices\n"
" --restorecon restore selinux context on Magisk files\n"
" --clone-attr SRC DEST clone permission, owner, and selinux context\n"
" --clone SRC DEST clone SRC to DEST\n"
" --sqlite SQL exec SQL to Magisk database\n"
" --use-broadcast use broadcast for su logging and notify\n"
"\n"
"Supported init triggers:\n"
" post-fs-data, service, boot-complete\n"
"\n"
"Supported applets:\n");
FULL_VER(Magisk) R"EOF( multi-call binary
Usage: magisk [applet [arguments]...]
or: magisk [options]...
Options:
-c print current binary version
-v print running daemon version
-V print running daemon version code
--list list all available applets
--daemon manually start magisk daemon
--remove-modules remove all modules and reboot
--[init trigger] start service for init trigger
Advanced Options (Internal APIs):
--unlock-blocks set BLKROSET flag to OFF for all block devices
--restorecon restore selinux context on Magisk files
--clone-attr SRC DEST clone permission, owner, and selinux context
--clone SRC DEST clone SRC to DEST
--sqlite SQL exec SQL commands to Magisk database
--use-broadcast use broadcast for su logging and notify
Supported init triggers:
post-fs-data, service, boot-complete
Supported applets:
)EOF");
for (int i = 0; applet_names[i]; ++i)
fprintf(stderr, i ? ", %s" : " %s", applet_names[i]);
@ -117,6 +119,10 @@ int magisk_main(int argc, char *argv[]) {
int fd = connect_daemon();
write_int(fd, BROADCAST_ACK);
return 0;
} else if (argv[1] == "--remove-modules"sv) {
int fd = connect_daemon();
write_int(fd, REMOVE_MODULES);
return read_int(fd);
}
#if 0
/* Entry point for testing stuffs */

View File

@ -18,6 +18,7 @@ enum {
MAGISKHIDE,
SQLITE_CMD,
BROADCAST_ACK,
REMOVE_MODULES,
};
// Return codes for daemon
@ -32,6 +33,7 @@ enum {
int connect_daemon(bool create = false);
int switch_mnt_ns(int pid);
void reboot();
// socket.c