I am new to linux and was making a module in which user can read as well as write to a device file.the following is my code where read is working fine but not the write function.
MODULE_LICENSE("DUAL BSD/GPL");
char message[80];
char *msg_ptr;
int dev_major = 0;
int dev_minor = 0;
struct cdev *cdev;
ssize_t dev_read(struct file *filp,char __user *buf,size_t count,loff_t *offset)
{
int i;
i=copy_to_user(buf,msg_ptr,count);
printk(KERN_ALERT"buff:%s",buf);
return 0;
}
ssize_t dev_write(struct file *filp,const char __user *buf,size_t count,loff_t *offset)
{
int j;
msg_ptr = kmalloc(sizeof(*buf),GFP_KERNEL);
copy_from_user(msg_ptr,buf,sizeof(*buf));
//printk(KERN_ALERT"msg_ptr:%s",msg_ptr);
return 0;
}
when i make a char node and then use echo hi >/dev/my_dev then it prints hi but writes infintely as seen in /var/log/messages.
dev_writeshould return the number of bytes written.When you return 0, Linux understand you wrote 0 bytes, and calls you again to write the rest. And again…
Same for
dev_read.