I am trying to implement a one-byte char device driver for the linux kernel, as part of my operating system module assignment. To implement the device driver, I have to define the read and write function. Below is my device write function
ssize_t onebyte_write(struct file *filep, const char *buf, size_t count, loff_t *f_pos)
{
if (get_user(*onebyte_data, buf) != 0)
return 0;
if (count > 1)
printk(KERN_ALERT "Error: No space left on device\n");
return 1;
}
'onebyte_data' is a char pointer to a dynamically allocated one byte memory in the kernel space. From my understanding, 'get_user' is supposed to copy a simple variable (int or char) from user space to kernel space. But when I execute the following set of command
printf abc > /dev/onebyte
cat /dev/onebyte
The result is a 'c' instead of a 'a', meaning get_user has read the whole input string instead of just the first character, and store the last character 'c' instead. Is this the expected behaviour? What should I do if I just want to store the first character of the input in onebyte_data?