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

时间:2023-01-16 15:43:26

I'm trying to write a kernel module which writes some data to a proc file. I'm trying to write something like 5000 characters but when I say $>cat /proc/myentry I can read only 1000 characters.

我正在尝试编写一个内核模块,它将一些数据写入proc文件。我正在尝试写5000个字符,但当我说$> cat / proc / myentry时我只能读1000个字符。

int procfile_read(char *buffer,  char **buffer_location,  off_t offset, int buffer_length, int *eof, void *data){
int ret;
static char my_buffer[4096];

if (offset > 0) {

    ret  = 0;
} else {

    ret = sprintf(my_buffer, LARGE STRING HERE);
}

*buffer_location=my_buffer;
return ret;
}

This is my code. Thanks in advance.

这是我的代码。提前致谢。

2 个解决方案

#1


3  

I had exactly this problem.

我有这个问题。

One issue in the original post, the if (offset>0) is used many times in examples of small proc files. The read is called multiple times until we return a 0 to indicate that there is no more data. So the if (offset>0) means we return (length of the buffer) as 0.

原始帖子中的一个问题是if(offset> 0)在小proc文件的示例中多次使用。多次调用读取,直到我们返回0表示没有更多数据。所以if(offset> 0)意味着我们返回(缓冲区的长度)为0。

There are 3 ways to return data with this function. Look at the source code comments, line 75 onwards :

使用此功能有3种方法可以返回数据。查看源代码注释,第75行:

For large files (method 2 from comments), I did the following :-

对于大文件(评论中的方法2),我做了以下内容: -

  • For each lump of your large data, copy 'buffer_length' of data to 'buffer'.
  • 对于每个大数据块,将'buffer_length'数据复制到'buffer'。

  • Set '*start' (or in your case *buffer_location) to 'buffer'.
  • 将'* start'(或在您的情况下为* buffer_location)设置为'buffer'。

  • return the amount of data you wrote (typically 'buffer_length')
  • 返回你写的数据量(通常是'buffer_length')

Finally, all data will be written and you return 0.

最后,所有数据都将被写入并返回0。

This worked for me with several Meg of data.

这对我来说有几个Meg数据。

#2


3  

I am not a kernel expert, but in linux-3.1.6/fs/proc/task_mmu.c I see some code like

我不是内核专家,但在linux-3.1.6 / fs / proc / task_mmu.c中我看到了一些代码

    seq_printf(m,
            "VmPeak:\t%8lu kB\n"
            "VmSize:\t%8lu kB\n"
            "VmLck:\t%8lu kB\n"
            "VmHWM:\t%8lu kB\n"
            "VmRSS:\t%8lu kB\n"
            "VmData:\t%8lu kB\n"
            "VmStk:\t%8lu kB\n"

so this suggests that you might want to use seq_printf not sprintf .... The m is a struct seq_file * pointer.

所以这表明你可能想要使用seq_printf而不是sprintf .... m是一个struct seq_file *指针。

As a general rule, you'll learn a lot by studying the free software source code which you are extending. In your case, it is the Linux kernel source code

作为一般规则,通过研究您正在扩展的免费软件源代码,您将学到很多东西。在您的情况下,它是Linux内核源代码

#1


3  

I had exactly this problem.

我有这个问题。

One issue in the original post, the if (offset>0) is used many times in examples of small proc files. The read is called multiple times until we return a 0 to indicate that there is no more data. So the if (offset>0) means we return (length of the buffer) as 0.

原始帖子中的一个问题是if(offset> 0)在小proc文件的示例中多次使用。多次调用读取,直到我们返回0表示没有更多数据。所以if(offset> 0)意味着我们返回(缓冲区的长度)为0。

There are 3 ways to return data with this function. Look at the source code comments, line 75 onwards :

使用此功能有3种方法可以返回数据。查看源代码注释,第75行:

For large files (method 2 from comments), I did the following :-

对于大文件(评论中的方法2),我做了以下内容: -

  • For each lump of your large data, copy 'buffer_length' of data to 'buffer'.
  • 对于每个大数据块,将'buffer_length'数据复制到'buffer'。

  • Set '*start' (or in your case *buffer_location) to 'buffer'.
  • 将'* start'(或在您的情况下为* buffer_location)设置为'buffer'。

  • return the amount of data you wrote (typically 'buffer_length')
  • 返回你写的数据量(通常是'buffer_length')

Finally, all data will be written and you return 0.

最后,所有数据都将被写入并返回0。

This worked for me with several Meg of data.

这对我来说有几个Meg数据。

#2


3  

I am not a kernel expert, but in linux-3.1.6/fs/proc/task_mmu.c I see some code like

我不是内核专家,但在linux-3.1.6 / fs / proc / task_mmu.c中我看到了一些代码

    seq_printf(m,
            "VmPeak:\t%8lu kB\n"
            "VmSize:\t%8lu kB\n"
            "VmLck:\t%8lu kB\n"
            "VmHWM:\t%8lu kB\n"
            "VmRSS:\t%8lu kB\n"
            "VmData:\t%8lu kB\n"
            "VmStk:\t%8lu kB\n"

so this suggests that you might want to use seq_printf not sprintf .... The m is a struct seq_file * pointer.

所以这表明你可能想要使用seq_printf而不是sprintf .... m是一个struct seq_file *指针。

As a general rule, you'll learn a lot by studying the free software source code which you are extending. In your case, it is the Linux kernel source code

作为一般规则,通过研究您正在扩展的免费软件源代码,您将学到很多东西。在您的情况下,它是Linux内核源代码