0

我无法读取大于缓冲区的 proc 文件。我看过这两个问题,我想我遗漏了一些东西。我是否错误地使用了 buffer_location 指针?

如何从 proc 文件中读取大数据?

如何读到 /proc 文件的末尾

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 不会再被调用,就像我想的那样。我究竟做错了什么?

4

1 回答 1

0

您忘记设置 eof 返回参数。

*eof = 0;
return ret;

当然,直到您没有更多数据要发送,在这种情况下设置*eof = 1;

于 2013-10-23T18:08:47.437 回答