在没有open()的情况下获取内核空间中的文件描述符和详细信息 [英] Getting file descriptors and details within kernel space without open()
问题描述
任何人都可以提供代码来解决此问题吗?
Could anyone provide the code to overcome this problem?
在给定文件/dev/driver1
的情况下,我们如何有效地从内核级别获取struct inode*
?
Effectively how do we obtain the struct inode*
from kernel level given the file /dev/driver1
?
在用户空间中给出以下信息:
Given in user space that:
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;
假设,我们不会走这条路,
Assuming that, we do not go by that path,
我们如何在内核本身中获取数据,例如硬编码要处理的file->private_data
?
How do we obtain within the kernel itself, by eg. hard coding the file->private_data
to be given to handle?
推荐答案
您正在寻找filp_open
函数.从文件include/linux/fs.h
:
You are looking for filp_open
function. From file 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
Here is a link to function source and documentation: http://lxr.free-electrons.com/source/fs/open.c#L937
如果您确实需要FD,则可以使用sys_open
(在新内核中不导出):
If you really need FD, you can use sys_open
(not exported in newer kernels):
long sys_open(const char __user *filename, int flags, int mode);
您可以在类似的问题上找到一个很好的答案: 如何在Linux内核模块中读取/写入文件?
You can find a very good answer on a similar question: How to read/write files within a Linux kernel module?
编辑(如何获取inode
):
您可以从struct file
缓存inode
:
struct file *file = ...;
struct inode *inode = file->inode;
如果要使用锁定功能:这是背景:Documentation/filesystems/path-lookup.txt
If you want it with locking: here is a background: Documentation/filesystems/path-lookup.txt
遍历的起点是current->fs->root
.内核中有许多功能已经完成工作,您可以在fs/namei.c
源文件中找到它们.
The starting point for traversing is current->fs->root
. There is a number of function in kernel, that already do the work, you can find them in fs/namei.c
source file.
有一个功能:kern_path
:
int error;
struct inode *inode;
struct path path;
error = kern_path(pathname, LOOKUP_FOLLOW, &path);
if (error) ...;
inode = path.dentry->d_inode;
这篇关于在没有open()的情况下获取内核空间中的文件描述符和详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!