加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux-kernel – 如何在驱动程序模块中使用seq_file实现可写proc

发布时间:2020-12-14 01:02:17 所属栏目:Linux 来源:网络整理
导读:在 Linux Device Driver 3rd ed一书中,/ proc文件系统用作输出方法来导出某个设备驱动程序的运行状态. 但是,在某些情况下,/ proc文件系统用作一个接口来更改驱动程序模块的内部参数. 我google了很多,发现互联网上的一些实现太旧了,他们使用create_proc_entry
在 Linux Device Driver 3rd ed一书中,/ proc文件系统用作输出方法来导出某个设备驱动程序的运行状态.

但是,在某些情况下,/ proc文件系统用作一个接口来更改驱动程序模块的内部参数.

我google了很多,发现互联网上的一些实现太旧了,他们使用create_proc_entry()而不是proc_create().

更重要的是,我更喜欢通过seq_file实现它(实际上,我不确定是否可能).我检查了seq_write()函数,什么也没得到.

谁能告诉我一个完成这项任务的例子? seq_file实现是更优选的.

解决方法

seq_file仅为读取文件提供帮助程序.写入没有类似的帮助程序,但是用手实现.write回调可迭代数据并不困难:

与读取不同,您可以在.write回调中删除文件的位置处理,假设用户始终写入开头,或者可选地,写入结尾(使用O_APPEND文件的控制标志).
其次,与阅读不同,您可以假设用户立即写入1,2或更多元素的内容,但不是半元素.

最简单的方法是允许单个元素写:

size_t write(struct file* file,const char __user* buf,size_t size,loff_t* pos)
{
    copy_from_user(str,buf,size); // Copy string from user space
    my_elem = my_parse(str,size); // Parse string
    if(file->f_flags & O_APPEND) {
         list_add_tail(my_elem,&my_list);//Append element to the end of list
    }
    else {
         clear_my_list(); // Clear old content of the list
         list_add_tail(my_elem,&my_list);// Add single element to it.
    }

    (void)pos; //Do not use file position at all
    return count; // Return number of bytes passed by the user
}

如果用户想要写几个元素,例如,从硬盘上的文件中写入,则任何shell都能够通过例如新行分割该文件,并且逐行地将行提供给你的proc文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读