如何从内核空间读取/写入linux/proc文件? [英] How to read/write from/to a linux /proc file from kernel space?

查看:586
本文介绍了如何从内核空间读取/写入linux/proc文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个由用户程序和内核模块组成的程序.内核模块需要收集数据,然后将其发送"到用户程序.这必须通过/proc文件来完成.现在,我创建了文件,一切都很好,花了很长时间阅读互联网以寻找答案,但仍然找不到.如何从内核空间 读/写/proc文件?提供给procfile的write_proc和read_proc用于从USER空间读取和写入数据,而我需要模块能够写入/proc文件本身.

I am writing a program consisting of user program and a kernel module. The kernel module needs to gather data that it will then "send" to the user program. This has to be done via a /proc file. Now, I create the file, everything is fine, and spent ages reading the internet for answer and still cannot find one. How do you read/write a /proc file from the kernel space ? The write_proc and read_proc supplied to the procfile are used to read and write data from USER space, whereas I need the module to be able to write the /proc file itself.

推荐答案

那不是它的工作原理.当用户空间程序打开文件时,它们是根据具体情况即时生成的.它们大多数是只读的,并且是通过通用机制生成的:

That's not how it works. When a userspace program opens the files, they are generated on the fly on a case-by-case basis. Most of them are readonly and generated by a common mechanism:

  • 使用create_proc_read_entry注册条目
  • 提供一个回调函数(按惯例称为read_proc),该函数在读取文件时被调用
  • 此回调函数应填充提供的缓冲区,并(通常)调用proc_calc_metrics以更新提供给用户空间的文件指针等.

(从内核)您没有写"到procfs文件,而是在用户空间请求它们时动态提供结果.

You (from the kernel) do not "write" to procfs files, you supply the results dynamically when userspace requests them.

这篇关于如何从内核空间读取/写入linux/proc文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆