(转)Linux 文件系统:procfs, sysfs, debugfs 用法简介
网址:http://www.tinylab.org/show-the-usage-of-procfs-sysfs-debugfs/
1 前言
内核中有三个常用的伪文件系统:procfs,debugfs和sysfs。
- procfs — The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures.
- sysfs — The filesystem for exporting kernel objects.
- debugfs — Debugfs exists as a simple way for kernel developers to make information available to user space.
它们都用于Linux内核和用户空间的数据交换,但是适用的场景有所差异:
- procfs 历史最早,最初就是用来跟内核交互的唯一方式,用来获取处理器、内存、设备驱动、进程等各种信息。
- sysfs 跟 kobject 框架紧密联系,而 kobject 是为设备驱动模型而存在的,所以 sysfs 是为设备驱动服务的。
- debugfs 从名字来看就是为debug而生,所以更加灵活。
它们仨的挂载方式类似,做个实验:
$ sudo mkdir /tmp/{proc,sys,debug}$ sudo mount -t proc nondev /tmp/proc/$ sudo mount -t sys nondev /tmp/sys/$ sudo mount -t debugfs nondev /tmp/debug/
不过,默认情况下,它们分别挂载在/proc,/sys/,/sys/kernel/debug/。
下面简单介绍这三个文件系统的用法。在介绍之前,请记下他们的官方文档:
- procfs — Documentation/filesystems/proc.txt
- sysfs — Documentation/filesystems/sysfs.txt
- debugfs — Documentation/filesystems/debugfs.txt
2 debugfs
API说明
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)struct dentry *debugfs_create_file(const char *name, umode_t mode,struct dentry *parent, void *data,const struct file_operations *fops)
参考实例
drivers/base/power/wakeup.c:
/*** wakeup_sources_stats_show - Print wakeup sources statistics information.* @m: seq_file to print the statistics into.*/static int wakeup_sources_stats_show(struct seq_file *m, void *unused){struct wakeup_source *ws;seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t""expire_count\tactive_since\ttotal_time\tmax_time\t""last_change\tprevent_suspend_time\n");rcu_read_lock();list_for_each_entry_rcu(ws, &wakeup_sources, entry)print_wakeup_source_stats(m, ws);rcu_read_unlock();return 0;}static int wakeup_sources_stats_open(struct inode *inode, struct file *file){return single_open(file, wakeup_sources_stats_show, NULL);}static const struct file_operations wakeup_sources_stats_fops = {.owner = THIS_MODULE,.open = wakeup_sources_stats_open,.read = seq_read,.llseek = seq_lseek,.release = single_release,};static int __init wakeup_sources_debugfs_init(void){wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);return 0;}
创建完的接口
/sys/kernel/debug/wakup_sources
给接口添加多级目录
上述接口直接创建在
debugfs根目录(/sys/kernel/debug)下,所以debugfs_create_file的parent参数被设置成了NULL,如果要加一级目录,则可以先用debugfs_create_dir创建一级目录,例如,要创建:/sys/kernel/debug/power/wakeup_sources的话,则需要:struct dentry *power;int err = -ENOMEM;power = debugfs_create_dir("power", NULL);if (!power)return err;wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",S_IRUGO, power, NULL, &wakeup_sources_stats_fops);
3 procfs
API说明
static inline struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent)static inline struct proc_dir_entry *proc_create(const char *name, umode_t mode,struct proc_dir_entry *parent, const struct file_operations *proc_fops)
参考实例
在上面例子的基础上,可以添加如下语句:
static int __init wakeup_sources_debugfs_init(void){proc_create("wakelocks", S_IFREG | S_IRUGO, NULL, &wakeup_sources_stats_fops);return 0;}
创建后的接口
/proc/wakelocks
给接口添加多级目录
这样创建的接口用起来跟
/sys/kernel/debug/wakeup_sources没有任何差异,类似地,如果要加一级目录,例如/proc/power/wakelocks,则可以:struct proc_dir_entry *power;int err = -ENOMEM;power = proc_mkdir("power", NULL);if (!power)return err;proc_create("wakelocks", S_IFREG | S_IRUGO, power, &wakeup_sources_stats_fops);
proc_mkdir用法跟debugfs_create_dir几无差异。
4 sysfs
API说明
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)static inline int sysfs_create_link(struct kobject *kobj, struct kobject *target, const char *name)int device_create_file(struct device *dev, const struct device_attribute *attr)
参考实例
在
/sys/power下创建一个 wakelocks 节点,用于读/写一个字符串。static char test_str[11];static ssize_t show_wakelocks(struct kobject *kobj, struct attribute *attr, char *buf){int ret;ret = snprintf(buf, 10, "%s\n", test_str);return ret;}static ssize_t store_wakelocks(struct kobject *kobj, struct attribute *attr,const char *buf, size_t count){int tmp;ret = sscanf(buf, "%10s", test_str);if (ret != 1)return -EINVAL;return count;}define_one_global_rw(wakelocks);static int __init wakelocks_init(void){int ret;ret = sysfs_create_file(power_kobj, &wakelocks.attr);}
创建后的节点
/sys/power/test_node
给接口添加多级目录
咱们上面其实已经把 test_node 创建在
/sys/power目录下,而非根目录/sys下,而参数 power_kobj 为内核已经在kernel/power/main.c创建的kobject对象。struct kobject *power_kobj;power_kobj = kobject_create_and_add("power", NULL);if (!power_kobj)return -ENOMEM;
在
sysfs中,有另外一个常见用法,那就是在一个 kobject 对应的目录下创建一个符号(属性文件)指向另外一个 kobject 对应的目录,通常这个是为了方便记忆和访问。这个API是sysfs_create_link。这种创建符号链接方法其实有一个很特殊的实例,那就是在驱动模型里头,有一个
class的概念,它把挂在不同总线上,但是实现类似功能的设备进行归类,比如说input类,backlight类等。如果设备属于一个现存的类,比如
backlight,那么可以用backlight_device_register创建,如果是I2C设备,会先在I2C下创建sysfs访问节点,并创建一个符号链接到backlight类所属的目录下。当然,如果没有找到设备能挂的直观的类,也可以用
class_create创建类,设备类通常会有一组默认的设备操作接口,例如backlight类有bl_device_attributes,如果要创建更多的设备特定的节点,可以用device_create_file或者device_add_groups创建节点或者节点群。
5 小结
通过比较发现,上述三个文件系统的 API 用法类似,而其中 debugfs 和 procfs 几乎有相同的参数,用的主要结构体是 struct file_operations,蛮多操作可以用 seq_* 家族的函数来实现。而 sysfs 则用到比较简单一些的 struct global_attr 结构体。对于提供给用户空间的节点,都可以轻松实现读写操作。
在创建目录方面,debugfs 和 procfs 类似,且比较简单。而 sysfs 要创建一级目录,需要先创建一个 kobject 对象。
为了简化设备模型依据总线创建的访问节点路径,sysfs 提供了API用于创建更简易的符号链接,可以创建到自己指定的目录下,也可以用设备类(Class)提供的API创建到设备类所属的目录下。
对于 sysfs,由于 kobject 与 device 的一对一依存关系,也可以直接用 device_create_file 来创建节点。
(转)Linux 文件系统:procfs, sysfs, debugfs 用法简介的更多相关文章
- Linux驱动开发3——devfs udev procfs sysfs debugfs傻傻地分不清楚
Linux调试文件系统 1.1.procfs 早期的Linux内核中,内核通过procfs输出调试信息,可以在用户态通过读写procfs节点与内核进行交互,用来获取处理器.内存.设备驱动.进程等各种信 ...
- [转帖]linux lsof 用法简介
linux lsof 用法简介 https://www.cnblogs.com/saneri/p/5333333.html 1.简介: lsof(list open files)是一个列出当前系统打开 ...
- linux 文件系统简介
linux文件系统简介 文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载基 ...
- linux文件系统简介
文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载基本原理.文件存储结构.软链接 ...
- Linux文件系统简介及常用命令
在linux系统中一切皆是文件,下面简要总结了一下linux文件系统中分区类型.文件系统类型以及常用命令. 一.分区类型1.主分区:最多只能有四个2.扩展分区:只能有一个,也可以看做是主分区的一种.即 ...
- Linux文件系统简介----转载
原文地址:Linux文件系统 文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载 ...
- 新一代 Linux 文件系统 btrfs 简介
https://www.ibm.com/developerworks/cn/linux/l-cn-btrfs/ Btrfs 简介 文件系统似乎是内核中比较稳定的部分,多年来,人们一直使用 ext2/3 ...
- 【转载】linux文件系统简介
文件系统是linux的一个十分基础的知识,同时也是学习linux的必备知识. 本文将站在一个较高的视图来了解linux的文件系统,主要包括了linux磁盘分区和目录.挂载基本原理.文件存储结构.软链接 ...
- 存储那些事儿(二): 下一代Linux文件系统BTRFS简介
BTRFS,通常念成 Butter FS,Better FS 或B-tree FS.下一代的Linux文件系统. 它基于写时拷贝(copy-on-write),支持高效的snapshot和clone. ...
随机推荐
- UseAdaptiveSizePolicy与CMS垃圾回收同时使用导致的JVM报错
系统在灰度环境上变更时发现JVM启动报错,详细检查JVM配置参数,发现新境了如下配置: -XX:+UseAdaptiveSizePolicy和-XX:+UseConcMarkSweepGC 初步猜想是 ...
- 如何通过wifi在android手机上安装调试应用
如何通过wifi在android手机上安装调试应用 1. 首先还是要打开手机的usb调试选项,并通过usb线连接手机.2. 然后执行“adb tcpip 5555”,把adb从usb模式切换到tcpi ...
- 用Activator.CreateInstance代替new实现类的实例化(转)
一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...
- (浅谈).Net控件GridView绑定数据
前台GridView属性设置 <td> <asp:GridView ID="GridView" runat="server" AutoGene ...
- PowerShell 解锁使用浏览器下载的文件
1 dir *.* -Recurse | Unblock-File
- 关于Could not parse configuration: /hibernate.cfg.xml的问题
第一次在eclipse上配置hibernate,问题百出啊,比如下面的org.hibernate.HibernateException: Could not parse configuration: ...
- Nginx - HTTP Configuration, the Location Block
Nginx offers you the possibility to fine-tune your configuration down to three levels — at the proto ...
- (干货)Linux学习资源推荐
源地址 国内的专业Linux网站(GB) ChinaUnix Linux中国 实验楼: 免费提供了Linux在线实验环境,不用在自己机子上装系统也可以学习Linux,超方便实用!. 国内的专业Linu ...
- asp.net 文件操作小例子(创建文件夹,读,写,删)
静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作 一.创建文件夹 using System.IO; string name = "aa"; strin ...
- vJine 第三波 之 Lua 来袭 vJine.Lua
vJine.Lua vJine.Lua是Lua语言的C#封装库,可实现通过C#直接运行Lua脚本并与Lua脚本交互的功能. 1. 授权: MPL2.0 相关资源: nuget:(https://www ...