Linux driver | procfs interface creation

Release: 2023-08-01 15:26:10
forward
1181 people have browsed it

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

procfs interface creation

Achievement effect:

For example, create a clk node under/procand passcat /proc/clkViewable content:

Linux driver | procfs interface creation

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");
Copy after login

The creation of the procfs interface mainly implements thestruct file_operationsstructure, and then proceeds through theproc_createfunction Register and unregister through theproc_removefunction.

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 thecatcommand to view the information under/proc, the corresponding implementation function of.openwill be called.

Here we use theseq_fileinterface. 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/meminfowill 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_lseekandseq_release.

proc new interface

Note that in newer versions of the kernel,procfsThe function interface has changed.

##System Kernel version Linux 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");
Copy after login

新的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!

Related labels:
source:嵌入式Linux充电站
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!