我正在审核此page
的内核模块示例程序中使用的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 */
if (next_fortune >= cookie_index) next_fortune = 0;
len = sprintf(page, "%s\n", &cookie_pot[next_fortune]);
next_fortune += len;
return len;
}
有人可以解释为什么检查off大于0.此外,有人可以解释off和count参数的重要性。
到目前为止,我的理解是我们必须在页面中写入数据,并且必须在数据结束时设置eof。
感谢。
答案 0 :(得分:3)
off是文件中必须从中读取数据的位置。这就像关闭普通文件一样。但是,在proc_read的情况下,它有些不同。例如,如果你在proc文件上调用read调用来读取100个字节的数据,那么proc_read中的off和count将是这样的:
在第一次,off = 0,计数100.例如,在你的proc_read中,你只返回了10个字节。然后控件无法返回到用户应用程序,你的proc_read将再次被内核调用,off为10并计为90.再次如果你在proc_read中返回20,你将再次调用off 30,计数70。像这样,你将被调用直到count达到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.
*/