我读了这篇文章
https://www.w3schools.com/howto/howto_css_fullscreen_video.asp。
但是我的代码在file_read
崩溃,并在系统日志中显示了一条我不理解并且不知道如何解决的跟踪。我尝试使用kernel_read
而不是file_read
,但没有帮助。请不要告诉我,我不应该在内核模块中使用文件,我只是出于研究目的而尝试这样做。那么,我该怎么做才能读取文件?这是我目前的代码:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/segment.h>
#include <linux/buffer_head.h>
// from related post
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;
}
// from related post
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;
}
static int __init myinit(void)
{
struct file* file = file_open("/home/sanyash/file.txt", O_RDONLY, 0);
char* data = vmalloc(sizeof(char) * 100);
int ret = file_read(file, 0, data, 10); // crashes here
printk(KERN_INFO "init %d %s\n", ret, data);
return 0;
}
static void __exit myexit(void)
{
printk(KERN_INFO "exit!\n");
}
module_init(myinit);
module_exit(myexit);
MODULE_LICENSE("GPL");
Ubuntu18.04,linux-headers-4.15.0-43-generic