获取内核模块中的所有安装点 [英] Get all mount points in kernel module

查看:97
本文介绍了获取内核模块中的所有安装点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取内核模块中的所有挂载点.以下是我想出的.由于strcat而导致段错误.这是获取挂载点的正确方法吗?这会工作吗?如果是的话,我该如何修复段错误?如果没有,如何在Linux内核模块中获取挂载点?

I'm trying to get all the mount points in a kernel module. Below is what I've come up with. It segfaults because of the strcat. Is this the correct way to get the mount points? Will this work? if so how do i fix the segfault? If not, how does one go about getting the mount points in a linux kernel module?

我已经尝试过循环整个命名空间,以查找挂载点根匹配,但是从2003年开始,内核发生了很大变化,因此基本上没有用.还尝试了在内核模块中获取文件系统挂载点,但是从2012年起再次使用它,因此它已经过时了.

I've tried cycle the whole namespace looking for mountpoint roots that match but its from 2003 and the kernel has changed so much so its basically useless. Also tried get filesystem mount point in kernel module but again its from 2012 so it to is outdated.

static int __init misc_init(void)
{
    struct path path;
    struct dentry *thedentry;
    struct dentry *curdentry;

    kern_path("/", LOOKUP_FOLLOW, &path);
    thedentry = path.dentry;
    list_for_each_entry(curdentry, &thedentry->d_subdirs, d_child)
    {
        kern_path(strncat("/", curdentry->d_name.name, strlen(curdentry->d_name.name)), LOOKUP_FOLLOW, &path);
        if (path_is_mountpoint(&path))
        {
            printk("%s: is a mountpoint", curdentry->d_name.name);
        }
        else
            printk("%s: is not a mountpoint", curdentry->d_name.name);
    }
    return 0;
}

推荐答案

在dentry结构中有相应的标志. d_flags.并有一个标志DCACHED_MOUNTED.获取当前指针.那里的fs_struct.然后是根.这为您提供了当前文件系统的根目录.从那里循环所有子目录,如果d_flags&通过DCACHE_MOUNTED便是安装点.

There are flags for it in the dentry struct. d_flags. and there is a flag DCACHED_MOUNTED. Get the current pointer. The fs_struct in there. then the root. this gives you the root of the current filesystem. from there loop though all the subdirs and if d_flags & DCACHE_MOUNTED passes then it is a mount point.

ssize_t read_proc(struct file *filp, char *buf, size_t len, loff_t *offp )
{
    struct dentry *curdentry;

    list_for_each_entry(curdentry, &current->fs->root.mnt->mnt_root->d_subdirs, d_child)
    {
        if ( curdentry->d_flags & DCACHE_MOUNTED)
            printk("%s is mounted", curdentry->d_name.name);
    }
    return 0;
}

这篇关于获取内核模块中的所有安装点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆