在 Linux 内核模块中读/写文件
我知道所有关于为什么不应该从内核读取/写入文件的讨论,而是如何使用 /proc 或 netlink 来做到这一点。 无论如何我想读/写。 我也读过 让我发疯 - 你永远不应该在内核中做的事情。
然而,问题是 2.6.30 没有导出 sys_read() 。 相反,它被封装在 SYSCALL_DEFINE3
中。 因此,如果我在模块中使用它,我会收到以下警告:
WARNING: "sys_read" [xxx.ko] undefined!
WARNING: "sys_open" [xxx.ko] undefined!
显然 insmod
无法加载模块,因为链接未正确发生。
问题:
- 2.6.22 之后如何在内核中读/写(其中
sys_read()
/sys_open()
未导出)? - 一般来说,如何从内核内部使用宏
SYSCALL_DEFINEn()
中包装的系统调用?
I know all the discussions about why one should not read/write files from kernel, instead how to use /proc or netlink to do that. I want to read/write anyway. I have also read
Driving Me Nuts - Things You Never Should Do in the Kernel.
However, the problem is that 2.6.30 does not export sys_read()
. Rather it's wrapped in SYSCALL_DEFINE3
. So if I use it in my module, I get the following warnings:
WARNING: "sys_read" [xxx.ko] undefined!
WARNING: "sys_open" [xxx.ko] undefined!
Obviously insmod
cannot load the module because linking does not happen correctly.
Questions:
- How to read/write within kernel after 2.6.22 (where
sys_read()
/sys_open()
are not exported)? - In general, how to use system calls wrapped in macro
SYSCALL_DEFINEn()
from within the kernel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该意识到,您应该尽可能避免从 Linux 内核内部进行文件 I/O。 主要思想是“更深一层”并调用VFS级别函数而不是系统调用处理程序直接:
包括:
打开文件(类似于open):
关闭文件(类似于close):
从文件中读取数据(类似于pread):
将数据写入文件(类似于pwrite):
同步更改文件(类似于 pwrite) to fsync):
[编辑] 最初,我建议使用 file_fsync,它在较新的内核版本中已消失。 感谢那个提出改变的可怜人,但他的改变被拒绝了。 在我审阅之前,编辑被拒绝了。
You should be aware that you should avoid file I/O from within Linux kernel when possible. The main idea is to go "one level deeper" and call VFS level functions instead of the syscall handler directly:
Includes:
Opening a file (similar to open):
Close a file (similar to close):
Reading data from a file (similar to pread):
Writing data to a file (similar to pwrite):
Syncing changes a file (similar to fsync):
[Edit] Originally, I proposed using file_fsync, which is gone in newer kernel versions. Thanks to the poor guy suggesting the change, but whose change was rejected. The edit was rejected before I could review it.
从 Linux 内核 4.14 版开始,
vfs_read
和vfs_write
函数不再导出以供在模块中使用。 相反,提供了专门用于内核文件访问的函数:此外,filp_open 不再接受用户空间字符串,因此它可以直接用于内核访问(无需与 <代码>set_fs)。
Since version 4.14 of Linux kernel,
vfs_read
andvfs_write
functions are no longer exported for use in modules. Instead, functions exclusively for kernel's file access are provided:Also,
filp_open
no longer accepts user-space string, so it can be used for kernel access directly (without dance withset_fs
).