I am trying following program.
Code:
#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;
struct rw_semaphore sem;
char *msg;
size_t buffer_size= 0;
int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp )
{
down_read(&sem);
//copy_to_user(buffer1,"In reader", 1024);
if(count > 1024)
{
buffer_size = 1024;
}
else
{
buffer_size = count;
}
copy_to_user(buf,msg,buffer_size);
up_read(&sem);
return 0;
}
int write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
{
down_write(&sem);
/*if(count > 1024)
{
buffer_size = 1024;
}
else
{
buffer_size = count;
}*/
//copy_to_user(buffer2,"In writer", 1024);
copy_from_user(msg,buf,buffer_size);
up_write(&sem);
return 0;
}
struct file_operations proc_fops =
{
read: read_proc,
write: write_proc
};
void create_new_proc_entry()
{
proc_create("hello1",0,NULL,&proc_fops);
msg=kmalloc(GFP_KERNEL,1024*sizeof(char));
//*msg = "PICT";
}
int proc_init (void)
{
create_new_proc_entry();
init_rwsem(&sem);
*msg = "PICT";
printk(KERN_INFO "msg :- %s \n", *msg);
return 0;
}
void proc_cleanup(void)
{
remove_proc_entry("hello1",NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);
After successfull make, when i try running cat /proc/hello1 command, it does not give any output and when i try running echo "hi" >> /proc/hello1 command, it goes into infinite loop and system gets dump.
Please tell me my mistake or how I should modify it to make it work.?