I'm trying to read and write to a proc file through kernel module
But when I run this command :
echo "hello" >> /proc/hello && cat /proc/hello
It doesn't print anything and when i open the file through text editor. I found mysterious symbols like this
^@^@^@^@^@^@^@^@^@^@
Any help will be appreciated thanks in advance
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include<linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
int len,temp;
char *msg;
int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ){
if(count>temp){count=temp;}
temp=temp-count;
copy_to_user(buf,msg, count);
if(count==0)temp=len;
return count;
}
int write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp){
copy_from_user(msg,buf,count);
len=count;
temp=len;
return count;
}
struct file_operations proc_fops = {
read: read_proc,
write: write_proc
};
void create_new_proc_entry(void){
proc_create("hello",0,NULL,&proc_fops);
msg=kmalloc(GFP_KERNEL,10*sizeof(char));
}
int proc_init (void){
create_new_proc_entry();
return 0;
}
void proc_cleanup(void){
remove_proc_entry("hello",NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);
msg, butmsghas a size of 10 characters => inwrite_procbe sure to not allowedcountmore than 10, otherwise you will have a buffer overflow!