无法理解Linux内核模块中read_proc的工作情况
我在这里回顾内核模块示例 程序中使用的read_proc如下所示:无法理解Linux内核模块中read_proc的工作情况,c,linux,linux-kernel,kernel,kernel-module,C,Linux,Linux Kernel,Kernel,Kernel Module,我在这里回顾内核模块示例 程序中使用的read_proc如下所示: 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 */
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;
}
有人能解释为什么off被检查为大于0吗。此外,是否有人能解释一下“停止”和“计数”论点的重要性
到目前为止,我的理解是我们必须在页面中写入数据,并且必须在数据结束时设置eof
谢谢。关闭是文件中必须从中读取数据的位置。这类似于正常文件的偏移。但是,在proc_read的例子中,它有一些不同之处。例如,如果调用proc文件上的read调用来读取100字节的数据,proc_read中的off和count将如下所示: 第一次,off=0,计数100。例如,在proc_read中,您只返回了10个字节。然后控件无法返回到用户应用程序,内核将再次调用proc_read,off为10,计数为90。同样,如果您在程序读取中返回20,您将再次调用off 30,计数70。像这样,你将被呼叫,直到计数达到0。然后数据被写入给定的用户缓冲区,应用程序read()调用返回 但是如果您没有100字节的数据,并且只想返回几个字节,那么必须将eof设置为1。然后read()函数立即返回 首先,下面的评论解释得比我好
/*
* How to be a proc read function
* ------------------------------
* Prototype:
* int f(char *buffer, char **start, off_t offset,
* int count, int *peof, void *dat)
*
* Assume that the buffer is "count" bytes in size.
*
* If you know you have supplied all the data you
* have, set *peof.
*
* You have three ways to return data:
* 0) Leave *start = NULL. (This is the default.)
* Put the data of the requested offset at that
* offset within the buffer. Return the number (n)
* of bytes there are from the beginning of the
* buffer up to the last byte of data. If the
* number of supplied bytes (= n - offset) is
* greater than zero and you didn't signal eof
* and the reader is prepared to take more data
* you will be called again with the requested
* offset advanced by the number of bytes
* absorbed. This interface is useful for files
* no larger than the buffer.
* 1) Set *start = an unsigned long value less than
* the buffer address but greater than zero.
* Put the data of the requested offset at the
* beginning of the buffer. Return the number of
* bytes of data placed there. If this number is
* greater than zero and you didn't signal eof
* and the reader is prepared to take more data
* you will be called again with the requested
* offset advanced by *start. This interface is
* useful when you have a large file consisting
* of a series of blocks which you want to count
* and return as wholes.
* (Hack by Paul.Russell@rustcorp.com.au)
* 2) Set *start = an address within the buffer.
* Put the data of the requested offset at *start.
* Return the number of bytes of data placed there.
* If this number is greater than zero and you
* didn't signal eof and the reader is prepared to
* take more data you will be called again with the
* requested offset advanced by the number of bytes
* absorbed.
*/
off
(文件内的偏移量)的值大于零意味着调用fourtune\u read
函数不是第一次。在这种情况下,不需要向用户写入任何内容,因为在第一次调用.AFAIK时有一个数据,因为linux 3.10的机制已经改变,所以您不需要该签名的功能。看见