Home >Operation and Maintenance >Linux Operation and Maintenance >Linux driver | procfs interface creation

Linux driver | procfs interface creation

嵌入式Linux充电站
嵌入式Linux充电站forward
2023-08-01 15:26:101499browse

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

procfs interface creation

Achievement effect:

For example, create a clk node under /proc and pass cat /proc/clkViewable content:

Linux driver | procfs interface creation

Code implementation:

##SystemKernel version Linux4.9.88

Add the following code in the driver:

#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)
{
    //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 the struct file_operations structure, and then proceeds through the proc_create function Register and unregister through the proc_remove function.

procfs is usually used to obtain various information such as CPU, memory, process, etc., such as cat /proc/cpuinfo, cat /proc/meminfo, so we only Need to implement the .open member function. When using the cat command to view the information under /proc, the corresponding implementation function of .open will be called.

Here we use the seq_file interface. What needs to be remembered is that procfs is usually used together 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 as cat / 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 interface when programming the proc interface. In addition, the .read, .llseek, and .release member functions can also be used directly seq_read , seq_lseek and seq_release.

proc new interface

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

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!

Statement:
This article is reproduced at:嵌入式Linux充电站. If there is any infringement, please contact admin@php.cn delete