I am trying to read/Write a variable in the linux kernel module using /proc file entry facility. Kernel module compiles successfully but when tried writing it via
echo 1 > My_file
This operation didn't finish.
Also, dmesg console is continuously flooded with some random value.
[ 1171.481231] proc_write_flag New_Flag 1124646486
[ 1171.481245] proc_write_flag New_Flag 1124646486
[ 1171.481259] proc_write_flag New_Flag 1124646486
[ 1171.481271] proc_write_flag New_Flag 1124646486
[ 1171.481473] ^C
I am new to linux device drivers and trying to use /proc
facility provided by the linux kernel. I tried removing the this kernel module, but again, the operation didn't finish.
what is causing this behaviour and how can i rectify it?
Here is the code:
int my_flag;
static struct proc_dir_entry *pdir = NULL;
MODULE_LICENSE("GPL");
MODULE_AUTHOR("GPL");
static ssize_t proc_read_flag(struct file* page,char __user * data, size_t count, loff_t *offset);
static ssize_t proc_write_flag(struct file *file, const char __user* ubuf, size_t count, loff_t* offset);
static struct file_operations myops =
{
.owner = THIS_MODULE,
.read = proc_read_flag,
.write= proc_write_flag,
};
//ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
static ssize_t proc_read_flag(struct file* page,char __user * data, size_t count,loff_t *offset)
{
int len=0;
printk("%s Flag %d\n",__FUNCTION__,my_flag);
//len = sprintf(,"%d\n",my_flag);
return len;
}
//ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
static ssize_t proc_write_flag(struct file *file, const char __user* ubuf, size_t count, loff_t* offset)
{
int len= 0, value= 0, i=0, ret = 0;
char buf[WRITE_BUF_SIZE] ;
printk("%s Cur_Flag %d\n",__FUNCTION__,my_flag);
if( copy_from_user(buf,ubuf,1) ){
return -EFAULT;
}
my_flag = (int)buf;
printk("%s New_Flag %d\n",__FUNCTION__,my_flag);
return ret;
}
int init_module(void)
{
struct proc_dir_entry *pfile = NULL;
pdir = proc_mkdir("My_dir",NULL);
if(!pdir){
return -ENOMEM;
}
pfile = proc_create("My_file", 0666, pdir, &myops);
if(!pfile)
return -ENOMEM;
printk("Proc_entry Created Successfully, Module initialized\n");
return 0;
}
void cleanup_function(void)
{
remove_proc_entry("My_file", pdir);
remove_proc_entry("My_dir", NULL);
printk("Removing Proc_entry!!!");
}