read proc无法正确显示数据

时间:2012-04-07 10:02:37

标签: linux linux-kernel linux-device-driver

我正在审核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读取不正常:

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

有关删除上述错误的建议吗?

1 个答案:

答案 0 :(得分:1)

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