内核交互--procfs
文档介绍:http://lxr.linux.no/linux+v2.6.37/Documentation/filesystems/proc.txt
以下内容抄录linux设备驱动开发详解-宋宝华
在/proc文件系统中,我们可以将对虚拟文件的读写作为与内核中实体进行通信的一种手段。/proc被内核用于向用户导出信息。linux系统的许多命令本身都是通过分析/proc下的文件来完成的,如ps、top、uptime和free等。例如free命令通过分析/proc/meminfo文件得到可用内存信息。
在linux3.9及以前的内核版本中,proc应用
可用如下函数创建/proc节点:
struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent);
static inline struct proc_dir_entry *create_proc_read_entry(const char *name, mode_t mode, struct proc_dir_entry *base, read_proc_t *read_proc, void * data);
create_proc_entry()用于创建/proc节点,create_proc_read_entry()用于创建只读的/proc节点。参数name为/proc节点名字,parent或base为父目录的节点,若为NULL,则指/proc目录,read_proc为节点的读函数指针。
创建/proc目录,
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);
proc_mkdir()和create_proc_entry()使用范例:
struct proc_dir_entry *example_dir = proc_mkdir(“proc_example”, NULL);
if(example_dir == NULL){
rv = -ENOMEM;
goto out;
} example_dir->owner = THIS_MODULE;
example_file = create_proc_entry(“example_file”, , example_dir);
if( example_file == NULL){
rv = -ENOMEM;
goto out;
} example_file->owner = THIS_MODULE;
example_file->read_proc = example_file_read;
example_file->write_proc = example_file_write;
proc节点的读写函数的类型分别为:
typedef int (read_proc_t)(char *page, char **start, off_t off, int count, int *eof, void *data);
typedef int (write_proc_t)(struct file *file, const char __user *buffer, unsigned long count, void *data);
page指向用于写入数据的缓冲区,start用户返回实际的数据并写到内存页的位置,eof用于返回读结束标志,offset是读的偏移,count是要读的数据长度。start参数比较复杂,对于/proc只包含简单数据的情况,通常不需要再读函数中设置*start,这意味着内核将认为数据保存在内存页偏移0的地方。
写函数与file_operation中的write()成员函数类似,需要一次从用户缓冲区到内存空间的复制过程。
data是struct proc_dir_entry的私有数据,实际读写就是通过page或buf将数据读取或保存到data所指数据区中。
void remove_proc_entry(const char *name, struct proc_dir_entry *parent);
在linux系统中已经定义好的可使用的/proc节点宏包括:proc_root_fs(/proc),proc_net(/proc/net),proc_bus(/proc/bus),proc_root_driver(proc_driver)等,proc_root_fs实际就是NULL。
linux3.10及以后版本中proc应用
proc的内核API和实现架构变更较大,create_proc_entry(),create_proc_read_entry()之类的API都被删除了,取而代之的是直接使用proc_create()、proc_create_date()API。同时,也不存在read_proc()、write_proc()之类的针对proc_dir_entry的成员函数了,而是直接把file_operations结构体的指针传入proc_create()或者proc_create_date()函数中。
static inline struct proc_dir_entry *proc_create(const char *name, mode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops)
static inline struct proc_dir_entry *proc_create_data(const char *name, mode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops, void *data)
代码中通过宏定义区分:
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
…
#else
…
#endif
内核交互--procfs的更多相关文章
- ifconfig源码分析之与内核交互数据
<ifconfig源码分析之与内核交互数据>本文档的Copyleft归rosetta所有,使用GPL发布,可以自由拷贝.转载,转载时请保持文档的完整性.参考资料:<Linux设备驱动 ...
- 内核交互--sysfs
文档介绍:http://lxr.linux.no/linux+v2.6.37/Documentation/filesystems/sysfs.txt The sysfs Filesystem Sysf ...
- 内核交互--debugfs_转
转载:Linux内核里的DebugFS DebugFS,顾名思义,是一种用于内核调试的虚拟文件系统,内核开发者通过debugfs和用户空间交换数据.类似的虚拟文件系统还有procfs和sysfs等,这 ...
- Python IO内核交互了解
注:Unix \ Linux 环境下的network IO 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系 ...
- linux内核交互,设备驱动控制管理接口
1,ioctl preface--starting point ,format,mount volume,in addition to the above file system -- allows ...
- 深入理解Linux网络技术内幕——用户空间与内核空间交互
概述: 内核空间与用户空间经常需要进行交互.举个例子:当用户空间使用一些配置命令如ifconfig或route时,内核处理程序就要响应这些处理请求. 用户空间与内核有多种交互方式,最常 ...
- Linux 用户态与内核态的交互【转载】
Linux 用户态与内核态的交互 在 Linux 2.4 版以后版本的内核中,几乎全部的中断过程与用户态进程的通信都是使用 netlink 套接字实现的,例如iprote2网络管理工具,它与内核的交 ...
- (转)Linux 文件系统:procfs, sysfs, debugfs 用法简介
网址:http://www.tinylab.org/show-the-usage-of-procfs-sysfs-debugfs/ 1 前言 内核中有三个常用的伪文件系统:procfs,debugfs ...
- quagga源码分析--内核通信netlink
Linux操作系统中当CPU处于内核状态时,可以分为有用户上下文的状态和执行硬件.软件中断两种.其中当处于有用户上下文时,由于内核态和用户态的内 存映射机制不同,不可直接将本地变量传给用户态的内存区: ...
随机推荐
- [转] matlab获取时间日期
原文:EmanLee, Eman Lee's Space (blog, website) 在MATLAB中得到系统当前日期.时间也是经常用到的内容,由以下函数实现. 1.生成指定格式日期和时间 dat ...
- mormot支持websocket
mormot支持websocket 根据定义,HTTP连接是无状态的和单向的,也就是说,客户机向服务器发送一个请求,该服务器通过一个应答回复.没有客户端的预先请求,就没有办法让服务器发送消息给客户机. ...
- php实现简单视图模板(视图引擎)
视图 视图,你所看见的部分. <?php echo 'hello, world'; 从简单开始理解 这就是个视图文件中的代码,没错就这么简单.视图,实际上是在 MVC 这种架构上提出的.MVC ...
- http://blog.csdn.net/hahalzb/article/details/5889545
http://blog.csdn.net/hahalzb/article/details/5889545
- AutoConfig工具使用指南
转载:http://blog.csdn.net/fighterandknight/article/details/70245905 13.1. 需求分析 13.1.1. 解决方案 13.2. Auto ...
- Git——版本管理工具(一)
Git 是一个分布式版本控制工具,它的作者 Linus Torvalds 是这样给我们介绍 Git —— The stupid content tracker(傻瓜式的内容跟踪器) 1. Git 背 ...
- 查看tomcat启动文件都干点啥---catalina.bat
在上一次查看tomcat启动文件都干点啥一文中,我们总结出,startup.bat文件的作用就是找到catalina.bat文件,然后把参数传递给它,在startup.bat中,调用catalina. ...
- RAID详解[RAID0/RAID1/RAID10/RAID5] (转)
一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...
- Timus Online Judge 1057. Amount of Degrees(数位dp)
1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...
- The fundamental differences between "GET" and "POST"
The HTML specifications technically define the difference between "GET" and "POST&quo ...