2012-04-07 54 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(如果它確實不可讀),那應該沒問題。