本文介绍了从文件描述符获取文件名/路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Linux内核模块是否有一种方式来获得从文件名/路径unsigned int类型FD

In a linux kernel module is there a way to get a file name/path from an unsigned int fd?

我知道这个答案:How我可以从一个内核模块内部的文件描述符的文件名?但如果我理解code吧,我需要一个结构files_struct 了。

I'm aware of this answer: How can I get a filename from a file descriptor inside a kernel module? but if I understand the code right, I need a struct files_struct too.

编辑:

作为重复,因为它是不请停止投票。我问了一个办法从内核模块获得纯C文件的名称/路径,不使用的系统工具。
说另一种方法:在运行上的readlink / procself / FD / IS的的一个很好的答案

Please stop voting as duplicated as it isn't. I'm asking for a way to get file's name/path in plain C from a kernel module, not using system tools.Said in another way: running readlink on /procself/fd/ is not a good answer.

编辑2:

内核的系统调用读 ssize_t供读取(INT FD,无效* buf中,为size_t计数); 3个参数,其中之一是一个FD。很明显,在某种程度上能够从一个单一的文件(而不是所有的文件索引节点内)来读取。问题是如何。

Kernel's syscall read ssize_t read(int fd, void *buf, size_t count); takes 3 arguments, one of them being a fd. It's obvious that somehow read is able to read from a single file (instead of all files inside an inode). The question is how.

推荐答案

在回答您引用问题上的code是你需要做什么。是的,一个结构files_struct 从任务是必须的,因为文件描述符只是一个背景下有意义的 files_struct (通常,存在的这些每个进程之一)。他们不是全球唯一的,只是一个单独的打开文件表中的索引。

The code in the answer to the question that you reference is what you need to do. And yes, a struct files_struct from a task is needed, because a file descriptor is only meaningful in the context of a files_struct (usually, there is one of these per process). They aren't globally unique, just an index within an individual open file table.

如果您code是进程上下文中运行(例如,通过系统调用调用。),那么你可以使用电流 - >文件当前任务的 files_struct 。这是阅读()一样。

If your code is running in process-context (eg. invoked through a syscall) then you can use current->files for the current task's files_struct. This is what read() does.

这篇关于从文件描述符获取文件名/路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:36
查看更多