使用系统调用打印文件系统上的所有文件
我正在内核中工作,我正在尝试进行一个系统调用,该调用将分区作为输入(即/dev/sda1
),然后打印文件系统上的每个文件使用printk()
。 我输入一个分区(即 /dev/sda1
),并将 printk()
放入此系统调用中进行打印。
首先,我尝试使用进程来执行此操作,因为如果我是对的,每个进程都由 task_struct
表示,并且我尝试使用 files_struct
访问文件。但问题是我只有打开文件的文件描述符,而不是所有文件。
所以,我想做的是传递分区的名称和 printk()
所有文件的名称。
例如:
我输入路径 /dev/sda1
作为参数,假设我里面有文件 a.txt
和 b.txt
这个分区,因此系统调用应该打印 a.txt
和 b.txt
。
签名将是这样的:
asmlinkage long sys_acall(char *partition_name);
I am working in the kernel and I am trying to make a system call that takes a partition as input (i.e. /dev/sda1
) and then prints every file on the filesystem using printk()
.
I enter a partition (i.e. /dev/sda1
) and I put a printk()
inside this system call to print.
First, I tried to do this with a process, because if I am right each process is represented by a task_struct
and I tried to access the files with the files_struct
. But the problem is that I only have the file descriptors of the opened files and not all the files.
So, what I want to do is that I pass the name of the partition and I printk()
the names of all the files.
For example:
I enter the path /dev/sda1
as an argument and let's suppose I have the file a.txt
and b.txt
inside this partition , so the system call should print a.txt
and b.txt
.
The signature will be like this:
asmlinkage long sys_acall(char *partition_name);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几件事需要讨论。
系统调用的partition_name参数应具有__user标记。
严格来说,如果您想从分区读取文件,您将必须实现文件系统识别(该分区是 ext3、reiserfs、ntfs,...?),然后实现该类型文件系统的驱动程序。正如 Christ 指出的那样,分区不包含文件,但文件系统包含文件。另一种选择是使用已为该分区上的文件系统实现的驱动程序。这个选项太可怕了。
如果您想从文件系统读取文件,您的工作会变得更容易,您可以使用 VFS 接口来访问它,但您需要安装该文件系统(不过您可以即时执行)。< /p>
我的最终意见是,我会将“实现打印分区中每个文件的系统调用”更改为“实现打印目录中每个文件的系统调用”。该系统调用的签名为:
asmlinkage long sys_crazyness(__user const char *dir);
我们不关心传递的目录是文件系统的根目录还是只是文件系统任何深度级别的文件夹。
如果你可以将你的问题改为这个问题,那就容易多了;)
There is a few things that needs to be discussed.
The partition_name parameter of your syscall should have the __user tag.
If you want to, strictly speaking, read files from a partition you will have to implement filesystem recognition (is that partition ext3, reiserfs, ntfs, ...?) and then implement the driver for that kind of filesystem. As Christ pointed out, partitions doesn't contain files but filesystems does. Another option is use the drivers already implemented for the filesystem on that partition. This option is just horrible.
If you want to read files from a filesystem your work gets easier, you can use the VFS interface to access it, but you will need that filesystem to be mounted (you can do it on-the-fly though).
My final opinion, I would change "implement a system call that prints every file in a partition" for "implement a system call that prints every file in a directory". The signature for that system call would be:
asmlinkage long sys_crazyness(__user const char *dir);
We don't care if the directory passed is the root of a filesystem or just a folder in any depth-level of a filesystem.
If you can change your problem to this one it would be much easier ;)