I’m currently working with a custom device driver that I installed. I’m very new to this and having trouble understanding how to get the data that I write into it from the command line.
For example, I write data to the file like so:
echo -n "test" > /dev/custom
And then I merely want to get the string here, so I can use the characters within:
static ssize_t custom_write (struct file *f, const char *buf, size_t len, loff_t *offset) { printk(KERN_ALERT "Testn"); return len; }
Advertisement
Answer
The string is in buf
, but it’s in user memory. You need to use copy_from_user()
to copy to kernel memory.
static ssize_t custom_write (struct file *f, const char *buf, size_t len, loff_t *offset) { char custom_buffer[MAX_BUFFER_SIZE]; size_t custom_buffer_size = len; if (custom_buffer_size > MAX_BUFFER_SIZE) { custom_buffer_size = MAX_BUFFER_SIZE; } if (copy_from_user(custom_buffer, buf, custom_buffer_size) != 0) { return -EFAULT; } printk(KERN_ALERT "Test %.*sn", (int)custom_buffer_size, custom_buffer); return custom_buffer_size; }