Crash system when the module is running

Viewed 34

I need to write a module that creates a file and outputs an inscription with a certain frequency. I implemented it. But when this module is running, at some point the system crashes and no longer turns on.

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/timer.h>

MODULE_LICENSE("GPL");

#define BUF_LEN 255
#define TEXT "Hello from kernel mod\n"

int g_timer_interval = 10000;

static struct file *i_fp;
struct timer_list g_timer;

loff_t offset = 0;
char buff[BUF_LEN + 1] = TEXT;

void timer_rest(struct timer_list *timer)

{
    mod_timer(&g_timer, jiffies + msecs_to_jiffies(g_timer_interval));
    i_fp = filp_open("/home/hajol/Test.txt", O_RDWR | O_CREAT, 0644);
    kernel_write(i_fp, buff, strlen(buff), &offset);
    filp_close(i_fp, NULL);
}
  
static int __init kernel_init(void)
{
    timer_setup(&g_timer, timer_rest, 0);
    mod_timer(&g_timer, jiffies + msecs_to_jiffies(g_timer_interval));
    return 0;
}

static void __exit kernel_exit(void)
{
    pr_info("Ending");
    del_timer(&g_timer);
}

module_init(kernel_init);
module_exit(kernel_exit);
1 Answers

When the system crashes, you should get a very detailed error message from the kernel, letting you know where and why this happened (the "oops" message):

  1. Read that error message
  2. Read it again
  3. Understand what it means (this often requires starting over from step 1 a couple of times :-) )

One thing that jumps out at me is that you're not going any error checking on the return value of filp_open. So you could very well be feeding a NULL pointer (or error pointer) into kernel_write.

Related