Home >Operation and Maintenance >Linux Operation and Maintenance >Linux driver | procfs interface creation
The previous article introduced the creation of the sysfs interface in the Linux driver. Today we will introduce the creation of the procfs interface.
procfs
: Can implement operations similar to cat /proc/cpuinfo
Achievement effect:
For example, create a clk node under /proc
and pass cat /proc/clk
Viewable content:
Code implementation:
Kernel version | |
---|---|
4.9.88 |
System | Kernel version |
---|---|
Linux | 5.10.111 |
在驱动中添加以下代码:
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> struct proc_dir_entry *my_proc_entry; static int proc_clk_show(struct seq_file *m, void *v) { seq_printf(m, "pll0: %lu Mhz\n" "pll1: %lu Mhz\n" "pll2: %lu Mhz\n", 100, 200, 300); return 0; } static int clk_info_open(struct inode *inode, struct file *filp) { return single_open(filp, proc_clk_show, NULL); } static const struct proc_ops clk_stat_proc_fops = { .proc_open = clk_info_open, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_release = seq_release, }; static int __init my_module_init(void) { my_proc_entry = proc_create("clk", 0, NULL, &clk_stat_proc_fops); return 0; } static void __exit my_module_exit(void) { proc_remove(my_proc_entry); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL");
新的proc
接口中,将原来的struct file_operations
换成了struct proc_ops
,其中成员函数也添加了对应的前缀proc
,但本质还是一样的,只是换了名字,更加规范了一些。
The above is the detailed content of Linux driver | procfs interface creation. For more information, please follow other related articles on the PHP Chinese website!