Problem with reading (in userspace) from a kernel module
I have an issue with a kernel module I'm trying to write.
The only purpose of the module is to be a buffer between 2 userspace programs (each reading and writing to the buffer).
The function I currently use to read from userspace:
has the following problem:
/** This funtion is called when the /proc file is read */
static ssize_t module_read(struct file *file, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
return length; /* Return the number of bytes "read" */
every time it runs, it always receives 1024 in the 'length' var.
No matter how much bytes I really try to read in userspace.
btw, the userspace code I use for reading is:
Where 'sizeToRead' is an integer representing the amount of bytes I want to read, 'fd' is the file descriptor of the kernel module, and 'buffer' is the string I try to write.
bytesRead = fread(buffer,sizeof(char), sizeToRead, fd)
Moreover, and even more strangely:
When the kernel module receives the request to get 1024 bytes, it sends to userspace 1024 bytes of data, and returns '1024' as number of bytes read (length variable).
But in userspace, the number I get in 'bytesRead' (how much bytes I read) is identical to 'sizeToRead' and not the real amount of data that was sent from the kernel module.
btw, I don't have this problem when writing to the kernel module:
Thanks to all the helpers,
bytesSent = fprintf(fd,"%s\n", buffer)
character I/O or block I/O
I didn't read your message carefully, but I suspect what is going on is character I/O versus block I/O. When reading from or writing to a terminal/tty/keyboard, the I/O is done character by character. When reading from or writing to disk, the I/O is done in blocks of characters.
Is there a reason why you aren't using a socketpair()? Is this some form of Inter-Process Communication that could be better done with shared memory (mmap or SysV) SysV messages (relatively small messages), or semaphores?
What happens if you read or write more than 1024 characters?