我怎样才能得到一个内核模块内部的文件描述符的文件名? [英] How can I get a filename from a file descriptor inside a kernel module?
问题描述
我需要从一个给定的文件描述符获取文件的名字,我写了一个小的Linux内核模块中。我想在给出获取文件名从文件描述符使用C 的解决方案,但由于某些原因,它打印出垃圾值(使用的readlink
在的/ proc /自/ FD / NNN
作为在溶液中提到的)。我该怎么办呢?
I need to get the name of a file from a given file descriptor, inside a small linux kernel module that I wrote. I tried the solution given at Getting Filename from file descriptor in C, but for some reason, it prints out garbage values (on using readlink
on /proc/self/fd/NNN
as mentioned in the solution). How can I do it?
推荐答案
不要叫 SYS_readlink
- 使用同样的方法PROCFS 当读取这些环节之一code>一样。开始在
的code proc_pid_readlink()
和 proc_fd_link()
在 FS / PROC / base.c
。
Don't call SYS_readlink
- use the same method that procfs
does when one of those links is read. Start with the code in proc_pid_readlink()
and proc_fd_link()
in fs/proc/base.c
.
从广义上讲,给予 INT FD
和结构files_struct *您感兴趣的任务文件
(你所采取的引用),你想要做的:
Broadly, given an int fd
and a struct files_struct *files
from the task you're interested in (which you have taken a reference to), you want to do:
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_TEMPORARY);
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);
这篇关于我怎样才能得到一个内核模块内部的文件描述符的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!