我试图以 200 毫秒的周期读取我通过内核模块创建的环回设备,但是当我尝试插入它时,它会使内核崩溃。
我认为我的读取模块有问题,但是没有计时器它可以正常工作。
我是内核编程的新手,请帮助。提前谢谢你:D
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>
#include<linux/fs.h>
#include <linux/init.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/buffer_head.h>
static struct timer_list my_timer;
static void read_file(char *filename)
{
struct file *fd;
char buf[1];
unsigned long long offset=0;
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
fd = filp_open(filename, O_RDONLY, 0);
if (fd >= 0) {
printk(KERN_DEBUG);
while (vfs_read(fd, buf, 1,&offset) == 1)
{
if((0 <= buf[0]) && (buf[0] <=255))
printk("%c", buf[0]);
}
printk(KERN_ALERT "Loop Ran\n");
filp_close(fd,NULL);
}
set_fs(old_fs);
}
void my_timer_callback( unsigned long data )
{
int ret;
printk( "my_timer_callback called (%ld).\n", jiffies );
printk( "Starting timer to fire in 200ms (%ld)\n", jiffies );
read_file("/dev/loop0");
ret = mod_timer( &my_timer, jiffies + msecs_to_jiffies(3000) );
if(ret)
printk("Error in mod_timer\n");
}
int init_module( void )
{
int ret;
printk("Timer module installing\n");
setup_timer( &my_timer, my_timer_callback, 0 );
printk( "Starting timer to fire in 200ms (%ld)\n", jiffies );
ret = mod_timer( &my_timer, jiffies + msecs_to_jiffies(200) );
if(ret)
printk("Error in mod_timer\n");
return 0;
}
void cleanup_module( void )
{
int ret;
ret = del_timer( &my_timer );
if(ret)
printk("The timer is still in use...\n");
printk("Timer module uninstalling\n");
return;
}`enter code here`
MODULE_LICENSE("GPL");
我的制作文件:
obj-m := timer2.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean