2013-10-23 29 views
0

我無法讀取大於緩衝區的proc文件。我看過這兩個問題,我想我錯過了一些東西。我是否正確使用buffer_location指針?無法讀取到procfile的末尾

How can I read large data from proc file?

how read to the end of /proc file

int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data) { 
    int ret; 

    //this function reads a data structure and fills the char* messages 
    read_log(); 

    if(strlen(messages) < buffer_length) { 
     //if the string is less than the size of the buffer read it all 
     memcpy(buffer, messages, strlen(messages)+1); 
     ret = strlen(messages)+1; 
     kfree(messages); 
    } 
    else { 
     //read just the buffer_length 
     memcpy(buffer, messages, buffer_length); 

     //move the messages char * up for the next read 
     char *temp = kmalloc(strlen(messages)-buffer_length+1, GFP_KERNEL); 
     strcpy(temp, messages+buffer_length); 
     kfree(messages); 
     messages = temp; 

     //from the question linked above I am putting my first lump of data 
     //into the buffer, setting *buffer_location = buffer, and returning buffer_length 
     *buffer_location = buffer; 
     ret = buffer_length; 
    } 
    return ret; 
} 

但是我procfile_read犯規再次調用,就像我想。我究竟做錯了什麼?

回答

0

你忘了設置eof返回參數。

*eof = 0; 
return ret; 

直到,當然,你有沒有更多的數據要發送,在這種情況下設置*eof = 1;