如何从内核模块内的文件描述符获取文件名?
- 2024-11-11 08:26:00
- admin 原创
- 170
问题描述:
我需要从给定的文件描述符中获取文件的名称,在我编写的一个小型 Linux 内核模块中。我尝试了在 C 中从文件描述符获取文件名中给出的解决方案,但由于某种原因,它会打印出垃圾值(使用解决方案中提到的readlink
on时/proc/self/fd/NNN
)。我该怎么做?
解决方案 1:
不要调用- 使用与读取其中一个链接时SYS_readlink
相同的方法。从和中的代码开始。procfs
`proc_pid_readlink()proc_fd_link()
fs/proc/base.c`
广义上讲,给定您感兴趣的任务(您已经参考过)的int fd
和,您想要执行的操作:struct files_struct *files
char *tmp;
char *pathname;
struct file *file;
struct path *path;
spin_lock(&files->file_lock);
file = fcheck_files(files, fd);
if (!file) {
spin_unlock(&files->file_lock);
return -ENOENT;
}
path = &file->f_path;
path_get(path);
spin_unlock(&files->file_lock);
tmp = (char *)__get_free_page(GFP_KERNEL);
if (!tmp) {
path_put(path);
return -ENOMEM;
}
pathname = d_path(path, tmp, PAGE_SIZE);
path_put(path);
if (IS_ERR(pathname)) {
free_page((unsigned long)tmp);
return PTR_ERR(pathname);
}
/* do something here with pathname */
free_page((unsigned long)tmp);
如果您的代码在进程上下文中运行(例如通过系统调用调用)并且文件描述符来自当前进程,那么您可以将它current->files
用于当前任务的struct files_struct *
。
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD