system calls in kernel timer callback functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I have developped a small kernel module. It schedules a regular timer calling the timeout function.
In this timeout function I would like to open the file /proc/net/dev from proc-filesystem. If I do,
the kernel crashes. Please see the program code below.

How do I make system calls in timer callback function.

Please can somebody give me a glue.

Regards,
Markus

------------------------------------------------------------------------------------
/*  hello.c - Demonstrating the module_init() and module_exit() macros.  This is the 
 *     preferred over using init_module() and cleanup_module().
 */
#include <linux/module.h>   // Needed by all modules
#include <linux/kernel.h>   // Needed for KERN_ALERT
#include <linux/init.h>     // Needed for the macros

#include <linux/timer.h>
#include <linux/sched.h>
#include <linux/syscall.h>
#include <linux/fcntl.h>

static struct timer_list list;
static unsigned long last_call;
static unsigned long num;
static long fd;
#define MAX_BUF 512
static char buf[MAX_BUF];

static void hello_timeout(unsigned long ptr) {
   printk(KERN_ALERT "Timeout %u\n",num);
   if (last_call) del_timer(&list);
   else {
     num++;
     init_timer(&list);
     list.function = hello_timeout;
     list.data = 0;
     list.expires = jiffies + HZ;
     add_timer(&list);
     if ((fd=sys_open("/proc/net/dev",O_RDONLY, 0x444)) == 0) {
        last_call = 0x1;
     } else {
        sys_read(fd, buf, MAX_BUF - 1);
        buf[MAX_BUF-1] = '\0';
        printk (KERN_ALERT "Buf: %s\n",buf);
        sys_close(fd);
     }
   }
}

static int hello_init(void)
{
   printk(KERN_ALERT "Hello, world\n");
   init_timer(&list);
   list.function = hello_timeout;
   list.data = 0;
   list.expires = jiffies + HZ;
   add_timer(&list);
   return 0;
}


static void hello_exit(void)
{
   printk(KERN_ALERT "Goodbye, world\n");
   del_timer(&list);
   last_call = 0x1;
}


module_init(hello_init);
module_exit(hello_exit);


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux