linux - 读取过程未正确显示数据

标签 linux linux-kernel linux-device-driver

我正在审查 linux 内核模块中的以下代码:

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)
   {

它从上面的代码切换到另一个具有如下长度检查的函数

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 read 无法正常工作:

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

有什么建议可以消除上述错误吗?

最佳答案

根据您的评论,我建议您查看linux/seq_file.h
它导出的 API 允许您创建多行/proc 条目,其大小不受限制。
您需要提供一个返回一行数据的函数,I/S 会重复调用它,每次都有一个新的缓冲区。如果每行不超过 3K(如果超过 3K,将非常难以阅读),应该没问题。

关于linux - 读取过程未正确显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10053497/

相关文章:

file - 无法从 Linux 内核版本 4.2.3 上的内核模块打开/读取文本文件

c - 如何显示 st_atime 和 st_mtime

c++ - 为什么 Go 套接字比 C++ 套接字慢?

linux-kernel - 从 S3C6410 板启动时无法在未知 block (0,0) 上挂载根 fs

linux-kernel - pci_enable_device 和 pcim_enable_device 有什么区别?

c - ioctl 参数大小限制

linux - 在 TI OMAP 3530 上插入适用于旧文件系统的 Linux 模块

c++ - 使用 C++ 的 SQL - 瓶颈在哪里?

c++ - qdbusxml2cpp 的最大参数数

linux-kernel - 如何使用 printk 打印物理地址(又名 phys_addr_t)?