0

我正在查看 linux 内核模块中的以下代码:

static int proc_read_kernel(char *buffer, char **start, off_t offset, int length,int *eof, void *data)
 {
   int len=0;
   struct mem_rw_t *mem= (struct mem_rw_t *)data;

   switch(mem->flag)
   {

从上面的代码它切换到另一个具有长度检查的函数,如下所示

static int func1(char *buffer, char **start, off_t offset, int length)
{
    printk ("The value for len is %d\n\r",len);
    printk ("The value for length is %d\n\r",length);

    if(len > length)
         goto list_finished;

上述代码的输出如下所示。看起来 len 的长度大于最后一个值的长度,并且 proc read 无法正常工作:

The value for len is 0
The value for length is 3072
The value for len is 398
The value for length is 3072
The value for len is 796
The value for length is 3072
The value for len is 796
The value for length is 3072
The value for len is 1537
The value for length is 3072
The value for len is 1777
The value for length is 3072
The value for len is 1777
The value for length is 3072
The value for len is 2029
The value for length is 3072
The value for len is 2427
The value for length is 3072
The value for len is 3120
The value for length is 3072
<4>proc_file_read: Read count exceeded

有什么建议可以消除上述错误吗?

4

1 回答 1

1

根据您的评论,我建议您查看linux/seq_file.h.
它导出的 API 允许您创建不受大小限制的多行 /proc 条目。
您需要提供一个返回一行数据的函数,它会被 I/S 重复调用,每次都有一个新的缓冲区。如果每行不超过 3K(如果超过了,那将是非常不可读的),它应该没问题。

于 2012-04-09T09:54:33.363 回答