繁体   English   中英

无法通过Linux内核模块中的/ proc文件系统读取链接列表

[英]Cannot read linked list via /proc file system in Linux kernel module

我想通过/ proc文件系统读取由内核模块创建的链表。 我的用户空间程序将包含一个fopen()调用,以打开/ proc / file1进行读取,并在每个循环中使用while循环执行fread()来从链接列表中读取节点。

用户空间程序包含:

 char buffer[100];
 FILE* fp = fopen("/proc/file1","r");
 while(fread(buffer,sizeof(char),100,fp)){
      printf("%s",buffer);
      // buffer is cleared before next iteration
 }
 fclose(fp);

内核模块创建一个链表,其中每个节点的类型

 struct node{
     int data;
     struct node* next;
 }

链接列表的起始节点地址存储在一个名为LIST的变量中。

我在内核模块中为read_proc编写了以下代码:

  int read_func(char *page,char **start,off_t off,int count,int *eof, void* data)
  {
        static struct node* ptr = NULL;
        static int flag = 0;
        int len;

        if(flag == 0){
               flag = 1;
               ptr = LIST;
        }

        if(ptr == NULL){
               // empty linked list or end of traversal
               *eof = 1;
               flag = 0;
               return 0;
        }

        if(ptr != NULL){
               len = sprintf(page, "%d", ptr->data);
               ptr = ptr->next;
        }
        return len;
  }

在执行用户空间程序时,当链表包含两个或更多节点时,仅读取一个节点。

谁能帮忙。

谢谢。

您正在请求读取100个字节的数据,并且在read_func()中返回的数据少于100个。正如我在上一篇文章中所说, 无法理解read_proc在Linux内核模块中的工作

内核将一次又一次调用此函数,直到count达到0,即读取100字节的数据。

基本上您想返回记录之类的数据,因此您需要在返回read_func()之前将eof设置为1。请注意必须按照上一篇文章中的说明设置start。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM