linux 内核模块 proc,无法理解Linux内核模块中read_proc的工作

我正在此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.

谢谢.

解决方法:

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()调用返回.

但是,如果您没有数百个字节的数据并且只想返回几个字节,则必须将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.

*/

标签:kernel,linux-kernel,kernel-module,c-3,linux

来源: https://codeday.me/bug/20191201/2083028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值