繁体   English   中英

在没有open()的情况下获取内核空间中的文件描述符和细节

[英]Getting file descriptors and details within kernel space without open()

任何人都可以提供代码来克服这个问题吗?

有效地,如果给定文件/dev/driver1我们如何从内核级获取struct inode*

在用户空间中给出:

int fd;
fd = open("/dev/driver1", O_RDWR | O_SYNC);

在内核空间:

static long dev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
struct dev_handle *handle;
handle = file->private_data;    

假设,我们不走那条路,

我们如何通过例如内核本身获得。 硬编码file->private_data给予处理?

您正在寻找filp_open函数。 从文件include/linux/fs.h

struct file *filp_open(const char *filename, int flags, umode_t mode);

以下是功能源和文档的链接: http//lxr.free-electrons.com/source/fs/open.c#L937

如果你真的需要FD,你可以使用sys_open (不在较新的内核中导出):

long sys_open(const char __user *filename, int flags, int mode);

您可以在类似问题上找到一个非常好的答案: 如何在Linux内核模块中读/写文件?

编辑(如何获取inode ):

您可以从struct file获取缓存的inode

struct file *file = ...;
struct inode *inode = file->inode;

如果你想要锁定:这是一个背景: Documentation/filesystems/path-lookup.txt

遍历的起点是current->fs->root 内核中有许多功能,已经完成了工作,你可以在fs/namei.c源文件中找到它们。

有一个函数: kern_path

int error;
struct inode *inode;
struct path path;

error = kern_path(pathname, LOOKUP_FOLLOW, &path);
if (error) ...;

inode = path.dentry->d_inode;

你的代码在dev_ioctl函数中? 如果是的话,那么

static long dev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
    struct dev_handle *handle;
    struct inode *inode;
    handle = file->private_data;    
    inode = file->f_inode;

似乎没有关于锁定要求的合理文档,因此您应该尝试挖掘类似的代码并查看它如何在f_inode成员上运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM