我如何在内核级别使用该目录中的文件描述符打开一个目录? [英] How do I open a directory at kernel level using the file descriptor for that directory?

查看:264
本文介绍了我如何在内核级别使用该目录中的文件描述符打开一个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我必须打开一个目录,并在内核级别读取里面的文件/目录。基本上,我试图找出如何 LS 是在内核级别实现的。

现在我已经想通了如何获得使用目录中的文件描述符 SYS_OPEN() O_DIRECTORY标志,但我不知道如何阅读,我收到了FD。如果任何人有任何提示或其他建议我最好的AP preciate它。 (请记住,这已经在内核级别上进行)。

编辑:对于一个长话短说,因为我实现文件的一所学校的项目/目录属性。在那里我storring的属性是在同一水平的文件与给定属性的隐藏文件夹。 (因此,在桌面/ MyFolder中的文件有一个属性的文件夹,名为桌面/ MyFolder中/ .filename_attr)。相信我,我不在乎周围的混乱在内核funsies。但我需要阅读的内核级目录的原因是因为它的项目规格的距离。


解决方案

要添加到咖啡馆的回答提的 vfs_readdir() ,读,从内核中写入文件被认为是不安全的(除了 / proc中,其作为对在内核的内部数据结构的接口。)

原因在这linuxjournal文章很好的描述,虽然他们也提供了一个劈访问文件。我不认为他们的方法可以很容易地修改为目录工作。更正确的做法是访问内核的文件系统的inode 的条目,这就是 vfs_readdir 一样。


  

的inode文件系统对象,如普通文件,目录,FIFO和其他
  野兽。他们住光碟上的任何(对于块设备文件系统)
  或在存储器(伪文件系统)。


注意 vfs_readdir()需要一个文件* 参数。要获得文件从用户空间的文件描述符结构的指针,你应该利用内核的文件描述符表。

借助 kernel.org文件文档上说这样做安全如下:


  

要查找的文件结构给予FD,读者
     必须使用 F确认() fcheck_files()的API。这些
     照顾因锁免费查询屏障的要求。
     举个例子:


  rcu_read_lock();
    文件= fcheck_files(文件,FD);
    如果(文件){
        //文件结构的处理是特殊的。
        //由于fd的查找(fget()/ fget_light())
        //是无锁,它是可能的查找可以与比赛
        //上的文件结构的最后放()操作。
        //这是用在atomic_long_inc_not_zero()避免 - > f_count
        如果(atomic_long_inc_not_zero(安培;文件 - > f_count))
            * fput_needed = 1;
        其他
        / *没有拿到参考,谁家释放* /
            文件= NULL;
    }
    rcu_read_unlock();
    ....
    返回文件;


  

atomic_long_inc_not_zero()检测是否引用计数已经是零或
  在增量变为零。如果确实如此,我们失败了 fget() / fget_light()


最后,来看看 filldir_t ,第二个参数的类型。

I'm working on a project where I must open a directory and read the files/directories inside at kernel level. I'm basically trying to find out how ls is implemented at kernel level.

Right now I've figured out how to get a file descriptor for a directory using sys_open() and the O_DIRECTORY flag, but I don't know how to read the fd that I receive. If anyone has any tips or other suggestions I'd appreciate it. (Keep in mind this has to be done at kernel level).

Edit:For a long story short, For a school project I am implementing file/directory attributes. Where I'm storring the attributes is a hidden folder at the same level of the file with a given attribute. (So a file in Desktop/MyFolder has an attributes folder called Desktop/MyFolder/.filename_attr). Trust me I don't care to mess around in kernel for funsies. But the reason I need to read a dir at kernel level is because it's apart of project specs.

解决方案

To add to caf's answer mentioning vfs_readdir(), reading and writing to files from within the kernel is is considered unsafe (except for /proc, which acts as an interface to internal data structures in the kernel.)

The reasons are well described in this linuxjournal article, although they also provide a hack to access files. I don't think their method could be easily modified to work for directories. A more correct approach is accessing the kernel's filesystem inode entries, which is what vfs_readdir does.

Inodes are filesystem objects such as regular files, directories, FIFOs and other beasts. They live either on the disc (for block device filesystems) or in the memory (for pseudo filesystems).

Notice that vfs_readdir() expects a file * parameter. To obtain a file structure pointer from a user space file descriptor, you should utilize the kernel's file descriptor table.

The kernel.org files documentation says the following on doing so safely:

To look up the file structure given an fd, a reader must use either fcheck() or fcheck_files() APIs. These take care of barrier requirements due to lock-free lookup. An example :

    rcu_read_lock();
    file = fcheck_files(files, fd);
    if (file) {
        // Handling of the file structures is special. 
        // Since the look-up of the fd (fget() / fget_light()) 
        // are lock-free, it is possible that look-up may race with 
        // the last put() operation on the file structure. 
        // This is avoided using atomic_long_inc_not_zero() on ->f_count
        if (atomic_long_inc_not_zero(&file->f_count))
            *fput_needed = 1;
        else
        /* Didn't get the reference, someone's freed */
            file = NULL;
    }
    rcu_read_unlock();
    ....
    return file;

atomic_long_inc_not_zero() detects if refcounts is already zero or goes to zero during increment. If it does, we fail fget() / fget_light().

Finally, take a look at filldir_t, the second parameter type.

这篇关于我如何在内核级别使用该目录中的文件描述符打开一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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