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 tocat /proc/cpuinfo
Achievement effect:
For example, create a clk node under/proc
and passcat /proc/clk
Viewable content:
Code implementation:
Add the following code in the driver:
#include#include #include #include #include struct proc_dir_entry *my_proc_entry; static int proc_clk_show(struct seq_file *m, void *v) { //cat显示的内容 seq_printf(m, "pll0: %u Mhz\n" "pll1: %u Mhz\n" "pll2: %u 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 struct file_operations myops = { .owner = THIS_MODULE, .open = clk_info_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release, }; static int __init my_module_init(void) { //注册proc接口 my_proc_entry = proc_create("clk", 0644, NULL, &myops); return 0; } static void __exit my_module_exit(void) { //注销proc接口 proc_remove(my_proc_entry); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL");
The creation of the procfs interface mainly implements thestruct file_operations
structure, and then proceeds through theproc_create
function Register and unregister through theproc_remove
function.
procfs is usually used to obtain various information such as CPU, memory, process, etc., such ascat /proc/cpuinfo
,cat /proc/meminfo
, so we only Need to implement the .open member function. When using thecat
command to view the information under/proc
, the corresponding implementation function of.open
will be called.
Here we use theseq_file
interface. What needs to be remembered is thatprocfs is usually usedtogether with the seq_file interface. seq_file is a sequence file interface.When the proc data content we create is composed of a series of data sequences or it is a relatively large proc file system, it is recommended to use the seq_file interface, such ascat / proc/meminfo
will display a lot of content.
The seq_file interface mainly solves the problems existing in proc interface programming.It is recommended to use the seq_file interfacewhen programming the proc interface. In addition, the .read, .llseek, and .release member functions can also be used directlyseq_read
,seq_lseek
andseq_release
.
Note that in newer versions of the kernel,procfs
The function interface has changed.
Kernel version | |
---|---|
4.9.88 |
System | Kernel version |
---|---|
Linux | 5.10.111 |
在驱动中添加以下代码:
#include#include #include #include #include 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!