2012-04-07 69 views
0

我正在审查一个Linux内核模块在以下代码:读PROC不显示数据正确

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) 
    { 

从上面的代码它swiches到具有长度检查如下

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 

任何建议,以去除上面的错误?

+0

什么是'len'和'length'?你从哪里得到它们? 'func1'没有名为'len'的变量。 – ugoren 2012-04-08 10:21:02

+0

长度是我们传递给proc_read的长度,len(它是内部计算的)是使用PROC接口读取的数据的总长度,该接口根据打印超出PROC_BLOCK_SIZE(3072); proc/base.c中的代码表示最后1K保留给Kernel。现在的PROC基础设施本身可以解决这个问题吗(意思是让proc读取超过3K)?谢谢。 – lxusr 2012-04-09 05:16:41

+0

问题只是不清楚。代码示例应该更完整,问题应该更加准确。您的评论解释了一些问题,但编辑问题会更好。 – ugoren 2012-04-09 09:52:35

回答

1

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