diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index f3fa51a9b..9c57c776e 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -449,6 +449,33 @@ static void prepare_modules() { chmod(SECURE_DIR, 0700); } +static void reboot() { + if (RECOVERY_MODE) + exec_command_sync("/system/bin/reboot", "recovery"); + else + exec_command_sync("/system/bin/reboot"); +} + +void remove_modules() { + LOGI("* Remove all modules and reboot"); + chdir(MODULEROOT); + rm_rf("lost+found"); + DIR *dir = xopendir("."); + struct dirent *entry; + while ((entry = xreaddir(dir))) { + if (entry->d_type == DT_DIR) { + if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv) + continue; + chdir(entry->d_name); + close(creat("remove", 0644)); + chdir(".."); + } + } + closedir(dir); + chdir("/"); + reboot(); +} + static void collect_modules() { chdir(MODULEROOT); rm_rf("lost+found"); diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 0964b23d6..2948b1497 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -31,13 +31,6 @@ static void verify_client(int client, pid_t pid) { } } -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 = reinterpret_cast(args); @@ -179,27 +172,6 @@ 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); - if (access(mnt, R_OK) == -1) return 1; // Maybe process died.. - - int fd, ret; - fd = xopen(mnt, O_RDONLY); - if (fd < 0) return 1; - // Switch to its namespace - ret = xsetns(fd, 0); - close(fd); - return ret; -} - int connect_daemon(bool create) { struct sockaddr_un sun; socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET); diff --git a/native/jni/include/daemon.h b/native/jni/include/daemon.h index 0f6956b0e..6af462f43 100644 --- a/native/jni/include/daemon.h +++ b/native/jni/include/daemon.h @@ -30,13 +30,11 @@ enum { DAEMON_LAST }; -// daemon.c +// daemon.cpp int connect_daemon(bool create = false); -int switch_mnt_ns(int pid); -void reboot(); -// socket.c +// socket.cpp socklen_t setup_sockaddr(struct sockaddr_un *sun, const char *name); int create_rand_socket(struct sockaddr_un *sun); @@ -63,6 +61,7 @@ void unlock_blocks(); void post_fs_data(int client); void late_start(int client); void boot_complete(int client); +void remove_modules(); /************* * Scripting * diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index b01da66b1..4678fb41e 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -198,3 +198,17 @@ uint32_t binary_gcd(uint32_t u, uint32_t v) { } while (v != 0); return u << shift; } + +int switch_mnt_ns(int pid) { + char mnt[32]; + snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid); + if (access(mnt, R_OK) == -1) return 1; // Maybe process died.. + + int fd, ret; + fd = xopen(mnt, O_RDONLY); + if (fd < 0) return 1; + // Switch to its namespace + ret = xsetns(fd, 0); + close(fd); + return ret; +} diff --git a/native/jni/utils/misc.h b/native/jni/utils/misc.h index a55516f30..c5addb0fa 100644 --- a/native/jni/utils/misc.h +++ b/native/jni/utils/misc.h @@ -15,6 +15,7 @@ void init_argv0(int argc, char **argv); void set_nice_name(const char *name); int parse_int(const char *s); uint32_t binary_gcd(uint32_t u, uint32_t v); +int switch_mnt_ns(int pid); #ifdef __cplusplus } @@ -96,4 +97,4 @@ int exec_command_sync(Args &&...args) { bool ends_with(const std::string_view &s1, const std::string_view &s2); -#endif \ No newline at end of file +#endif