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()从内核里?
3 回答
慕森王
TA贡献1777条经验 获得超3个赞
#include <linux/fs.h>#include <asm/segment.h>#include <asm/uaccess.h>#include <linux/buffer_head.h>
struct file *file_open(const char *path, int flags, int rights) { struct file *filp = NULL; mm_segment_t oldfs; int err = 0; oldfs = get_fs(); set_fs(get_ds()); filp = filp_open(path, flags, rights); set_fs(oldfs); if (IS_ERR(filp)) { err = PTR_ERR(filp); return NULL; } return filp;}
void file_close(struct file *file) { filp_close(file, NULL);}
int file_read(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) { mm_segment_t oldfs; int ret; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_read(file, data, size, &offset); set_fs(oldfs); return ret;}
int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) { mm_segment_t oldfs; int ret; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_write(file, data, size, &offset); set_fs(oldfs); return ret;}
int file_sync(struct file *file) { vfs_fsync(file, 0); return 0;}
侃侃无极
TA贡献2051条经验 获得超10个赞
vfs_read
vfs_write
# Read the file from the kernel space.ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos); # Write the file from the kernel space.ssize_t kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos);
filp_open
set_fs
).
- 3 回答
- 0 关注
- 1645 浏览
添加回答
举报
0/150
提交
取消