I am reviewing the kernel module example at this page
The read_proc used in the program is as follows:
int fortune_read( char *page, char **start, off_t off,
int count, int *eof, void *data )
{
int len;
if (off > 0) {
*eof = 1;
return 0;
}
/* Wrap-around */
if (next_fortune >= cookie_index) next_fortune = 0;
len = sprintf(page, "%s\n", &cookie_pot[next_fortune]);
next_fortune += len;
return len;
}
Can someone explain why off is checked to be greater than 0. Moreover can someone explain what is the importance of the off and count arguments.
My understanding so far is that we have to write data in page and have to set eof when data has ended.
Thanks.
off is the position in the file from where data has to be read from. This is like off set of normal file. But, in the case of proc_read it is some what different. For example if you invoke a read call on the proc file to read 100 bytes of data, off and count in the proc_read will be like this:
in the first time, off = 0, count 100. Say for example in your proc_read you have returned only 10 bytes. Then the control cannot come back to the user application, your proc_read will be called by the kernel once again with off as 10 and count as 90. Again if you return 20 in the proc_read, you will again called with off 30, count 70. Like this you will be called till count reaches 0. Then the data is written into the given user buffer and your application read() call returns.
But if you don't have hundred bytes of data and want to return only a few bytes, you must set the eof to 1. Then the read() function returns immediately.
For the start, the following comment explains better than me.