您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从内核模块中的文件描述符中获取文件名?

如何从内核模块中的文件描述符中获取文件名?

不要调用SYS_readlink-使用与procfs读取其中一个链接时相同的方法。开始与代码proc_pid_readlink()proc_fd_link()fs/proc/base.c

从广义上讲,给予int fdstruct 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);

如果您的代码在进程上下文中运行(例如,通过syscall调用),并且文件描述符来自当前进程,则可以将其current->files用于当前任务struct files_struct *

其他 2022/1/1 18:21:14 有486人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶