这是我从内核写入文件的代码。(我知道直接从内核读取和写入不是一个好主意,但这只是一个测试)。
static void write_startup_file(char *startstring)
{
mm_segment_t oldfs;
struct file *f;
loff_t pos=0;
f = filp_open("/bin/startupcheck",O_WRONLY|O_CREAT, 0644);
oldfs = get_fs();
set_fs (KERNEL_DS);
if(f)
{
vfs_write(f,startstring, strlen(startstring), &pos);
set_fs(oldfs);
filp_close(f, NULL);
}
else
printk(KERN_CRIT "Unable to open startfile...\n");
}
我在另一个函数 ourVeryOwnFunction()
中称它为
if(dbval==NULL) { write_startup_file("FAILED");}
其中 dbval
是一个正在读取的值。但是当我编译内核并运行它时(我在 vmware 上运行它)。我得到的内核 panic 是 here
显然我做错了什么。寻求帮助。谢谢。
最佳答案
首先,初始化您的struct file *
指针。不要依赖于根据 NULL
简单检查返回的指针。请改用 IS_ERR
宏。老实说,一般来说,很难说还有什么问题。读这样的东西http://ytliu.info/notes/linux/file_ops_in_kernel.html或者在 SO 上引用这些问题
How to read/write files within a Linux kernel module?
File I/O in a Linux kernel module
也许它会给您一些见解或阐明一些事情。当然,不要从内核写入 fs :)
HTH.
关于linux - vfs_write 导致内核 panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014074/