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?
You should be aware that that you should avoid file I/O 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.
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
).