如何从proc文件中读取大数据?
我正在尝试编写一个内核模块,将一些数据写入 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;
}
这是我的代码。提前致谢。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正是遇到这个问题。
原始帖子中的一个问题是,
if (offset>0)
在小 proc 文件的示例中多次使用。多次调用 read 直到我们返回 0 表示没有更多数据。因此,if (offset>0)
意味着我们返回(缓冲区的长度)为 0。此函数有 3 种返回数据的方法。查看源代码注释,第 75 行开始:
对于大型文件(注释中的方法 2),我执行了以下操作:-
最后,所有数据都将被写入,并且您返回 0。
这对我来说适用于几兆数据。
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 theif (offset>0)
means we return (length of the buffer) as 0.There are 3 ways to return data with this function. Look at the source code comments, line 75 onwards :
For large files (method 2 from comments), I did the following :-
Finally, all data will be written and you return 0.
This worked for me with several Meg of data.
我不是内核专家,但在 linux-3.1.6/fs/proc/task_mmu.c 中我看到一些类似的代码
,这表明您可能想要使用 seq_printf 不是
sprintf
....m
是struct seq_file *
指针。一般来说,通过研究您正在扩展的自由软件源代码,您将学到很多东西。就您而言,它是 Linux 内核源代码
I am not a kernel expert, but in
linux-3.1.6/fs/proc/task_mmu.c
I see some code likeso this suggests that you might want to use
seq_printf
notsprintf
.... Them
is astruct seq_file *
pointer.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