magic_mount: tell ksu about our mounts

This commit is contained in:
backslashxx 2025-11-21 21:01:37 +08:00
commit ae87f30680
2 changed files with 42 additions and 1 deletions

View file

@ -14,6 +14,8 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
MountStats g_stats = { 0 };
@ -26,6 +28,25 @@ int g_failed_modules_count = 0;
char **g_extra_parts = NULL;
int g_extra_parts_count = 0;
int global_fd = 0;
static void grab_fd(void) { syscall(SYS_reboot, KSU_INSTALL_MAGIC1, KSU_INSTALL_MAGIC2, 0, (void *)&global_fd); }
static void send_unmountable(const char *mntpoint)
{
struct ksu_add_try_umount_cmd cmd = {0};
if (!global_fd)
return;
cmd.arg = (uint64_t)mntpoint;
cmd.flags = 0x2;
cmd.mode = 1;
ioctl(global_fd, KSU_IOCTL_ADD_TRY_UMOUNT, &cmd);
}
static void register_module_failure(const char *module_name)
{
if (!module_name)
@ -496,7 +517,8 @@ static int do_magic(const char *base, const char *wbase, Node *node,
LOGE("bind %s->%s: %s", node->module_path, target,
strerror(errno));
return -1;
}
} else
send_unmountable(target); // tell ksu about this mount
(void)mount(NULL, target, NULL,
MS_REMOUNT | MS_BIND | MS_RDONLY, NULL);
@ -704,6 +726,10 @@ static int do_magic(const char *base, const char *wbase, Node *node,
LOGI("move mountpoint success: %s -> %s", wpath, path);
(void)mount(NULL, path, NULL, MS_REC | MS_PRIVATE, NULL);
// tell ksu about this one too
send_unmountable(path);
}
g_stats.nodes_mounted++;
@ -800,6 +826,8 @@ int magic_mount(const char *tmp_root)
node_free(root);
return -1;
}
grab_fd();
LOGI("starting magic_mount core logic: tmpfs_source=%s tmp_dir=%s",
g_mount_source, tmp_dir);

View file

@ -2,6 +2,7 @@
#define MAGIC_MOUNT_H
#include <stdio.h>
#include <stdint.h>
#define DISABLE_FILE_NAME "disable"
#define REMOVE_FILE_NAME "remove"
@ -36,4 +37,16 @@ int magic_mount(const char *tmp_root);
void add_extra_part_token(const char *start, size_t len);
// kernelsu api, atleast ones we care about
#define KSU_INSTALL_MAGIC1 0xDEADBEEF
#define KSU_INSTALL_MAGIC2 0xCAFEBABE
struct ksu_add_try_umount_cmd {
uint64_t arg; // char ptr, this is the mountpoint
uint32_t flags; // this is the flag we use for it
uint8_t mode; // denotes what to do with it 0:wipe_list 1:add_to_list 2:delete_entry
};
#define KSU_IOCTL_ADD_TRY_UMOUNT _IOC(_IOC_WRITE, 'K', 18, 0)
#endif /* MAGIC_MOUNT_H */