开发中proc文件系统的简单使用

这篇具有很好参考价值的文章主要介绍了开发中proc文件系统的简单使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用proc文件系统



在Linux系统中, “/proc”文件系统十分有用, 它被内核用于向用户导出信息。 “/proc”文件系统是一个虚拟文件系统, 通过它可以在Linux内核空间和用户空间之间进行通信。 在/proc文件系统中, 我们可以将对虚拟文件的读写作为与内核中实体进行通信的一种手段, 与普通文件不同的是, 这些虚拟文件的内容都是动态创建的。

“/proc”下的绝大多数文件是只读的, 以显示内核信息为主。 但是“/proc”下的文件也并不是完全只读的, 若节点可写, 还可用于一定的控制或配置目的, 例如前面介绍的写/proc/sys/kernel/printk可以改变printk() 的打印级别。

Linux系统的许多命令本身都是通过分析“/proc”下的文件来完成的, 如ps、 top、 uptime和free等。 例如, free命令通过分析/proc/meminfo文件得到可用内存信息, 下面显示了对应的meminfo文件和free命令的结果。

1.meminfo文件

开发中proc文件系统的简单使用,Linux驱动,驱动开发,linux,arm开发,驱动开发,嵌入式,调试

2. free命令

开发中proc文件系统的简单使用,Linux驱动,驱动开发,linux,arm开发,驱动开发,嵌入式,调试

3、创建 /proc 节点

  • struct proc_dir_entry
    内核使用
    proc_dir_entry 结构体来描述
    /proc 文件系统中的一个目录或者文件节点
/* 进程目录项结构体 /
struct proc_dir_entry {
    unsigned int low_ino; / 低位 inode 编号 /
    umode_t mode; / 文件访问权限 /
    nlink_t nlink; / 链接计数 /
    kuid_t uid; / 用户 ID /
    kgid_t gid; / 组 ID /
    loff_t size; / 文件大小 */
    const struct inode_operations proc_iops; / inode 操作函数集指针 */
    const struct file_operations proc_fops; / 文件操作函数集指针 */
    struct proc_dir_entry *next, *parent, subdir; / 目录项指针 */
    void data; / 自定义数据指针 /
    atomic_t count; / 使用计数 /
    atomic_t in_use; / 调用模块的进程数;负数表示正在卸载 */
    struct completion pde_unload_completion; / 卸载完成信号量指针 /
    struct list_head pde_openers; / 打开文件的进程链表头 /
    spinlock_t pde_unload_lock; / 卸载锁 /
    u8 namelen; / 名称长度 /
    char name[]; / 名称 */
};
  • proc_create
    内核提供了一套
    API用于在
    proc 文件系统中创建 文件节点
    /目录:
static struct proc_dir_entry *proc_create(  const char *name, umode_t mode,
                                            struct proc_dir_entry *parent, 
                const struct file_operations *proc_fops);    
                                            
@ name: 节点名
@ mode:    权限位
@ parent:父目录
@ proc_fops: 文件操作结构体

    struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);

@ name: 目录名
@ parent:父目录
  • remove_proc_entry()
    内核提供
    remove_proc_entry 来移除
    proc 文件系统中的 文件
    /目录 项:
void remove_proc_entry(const char *name, struct proc_dir_entry *parent);

@ name: 文件/目录 名
@ parent:父目录

4、使用 file_operations 实现 proc 文件读写 导向内核信息

在调用 proc_create() 创建文件项时, 提供了一个 file_operations结构体, 实现里面的read, write方法即可实现proc 文件读写,demo 程序如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm/stat.h>
#include <linux/proc_fs.h>

MODULE_LICENSE("GPL");

#define LOG_TAG    "my_test1: "

static char my_buf[256];

//====================================================================================
static ssize_t my_proc_read(struct file *fp, char __user *buf, size_t size, loff_t *pos)
{
    int len = strlen(my_buf);

    pr_info(LOG_TAG "enter %s()\n", __func__);

    if( 0 == *pos )    {
        strncpy(buf, my_buf, sizeof(my_buf));
        *pos += len;
        return len;
    } else
        return 0;
}

static ssize_t my_proc_write(struct file *file,  const char __user *buf, size_t size, loff_t *pos)
{
    int len = size < sizeof(my_buf) ? size : sizeof(my_buf);

    pr_info(LOG_TAG "enter %s()\n", __func__);

    if( 0 == *pos)    {
        memset(my_buf, '\0', sizeof(my_buf));
        len = copy_from_user(my_buf, buf, len);
        *pos += len;
        my_buf[len - 1] = '\0';
        return size;
    }else 
        return 0;
}


static struct file_operations my_file_ops = {
    .owner     = THIS_MODULE,
    .read     = my_proc_read,
    .write     = my_proc_write,
};

//====================================================================================
static int __init my_module_init(void)
{
    struct proc_dir_entry * my_proc_node = NULL;

    pr_info(LOG_TAG "enter %s()\n", __func__);

    my_proc_node = proc_create("my_test1", S_IRUGO | S_IWUGO, NULL, &my_file_ops);
    if( NULL == my_proc_node )    {
        pr_err(LOG_TAG "creat /proc/my_test1 failed\n");
        remove_proc_entry("my_test1", NULL);
        return -1;
    }

    pr_info(LOG_TAG "creat /proc/my_test1 success\n");
    return 0;
}

static void __exit my_module_exit(void)
{
    pr_info(LOG_TAG "enter %s()\n", __func__);

    pr_info(LOG_TAG "remove /proc/my_test1\n");
    remove_proc_entry("my_test1", NULL);
}

module_init(my_module_init);
module_exit(my_module_exit);

5、使用 seq_file 实现 proc 文件的读取

seq_file只是在普通的文件read中加入了内核缓冲的功能,从而实现顺序多次遍历,读取大数据量的简单接口。内核使用 seq_file 结构体来描述seq_file:

/* 序列文件结构体 */
struct seq_file {
    char buf; / 缓冲区指针 /
    size_t size; / 缓冲区大小 /
    size_t from; / 开始位置 /
    size_t count; / 计数器 /
    loff_t index; / 索引 /
    loff_t read_pos; / 读取位置 /
    u64 version; / 版本号 /
    struct mutex lock; / 互斥锁 */
    const struct seq_operations op; / 序列操作函数集指针 /
    int poll_event; / poll() 事件 */
    #ifdef CONFIG_USER_NS
    struct user_namespace user_ns; / 用户命名空间 */
    #endifvoid private; / 私有数据指针 */
};

seq_file一般只提供只读接口,在使用seq_read时,主要靠下述四个操作来完成内核自定义缓冲区的遍历的输出操作,其中pos作为遍历的iterator,在seq_read函数中被多次使用,用以定位当前从内核自定义链表(不止是链表, 还可以是哈希表, 数组, 红黑树)中读取的当前位置,当多次读取时,pos非常重要,且pos总是遵循从0,1,2…end+1遍历的次序,其即必须作为遍历内核自定义链表的下标,也可以作为返回内容的标识。但是我在使用中仅仅将其作为返回内容的标示,并没有将其作为遍历链表的下标,从而导致返回数据量大时造成莫名奇妙的错误,注意:start返回的void*v如果非0,被show输出后,在作为参数传递给next函数,next可以对其修改,也可以忽略;当next或者start返回NULL时,在seq_open中控制路径到达seq_end。

struct seq_operations {
    void * (*start) (struct seq_file *m, loff_t *pos);
    void (*stop) (struct seq_file *m, void *v);
    void * (*next) (struct seq_file *m, void *v, loff_t *pos);
    int (*show) (struct seq_file *m, void *v);
};

在读取 proc file 时:

  • 如果只有一条记录, 调用过程是:
    start->show->stop,
    start->stop.
  • 如果有多条记录并且能通过缓冲区一次传第完, 调用过程是,继续
    start->show->next->show->next-> … ->nex->show->stop,
    start->stop .
  • 如果有多条记录, 比且不能一次通过缓冲区传送完成, 调用过程是:
    start->show->next->show->…->show->stop,
    start->show->next->show->…->show->stop, … , start->stop.

seq_file 的缓冲区通常为1K, 但是会动态增长以便能容纳至少一条记录(当单条记录的大小大于1页的时候,这个就很有用), 通常, 应该在start中进行必要的加锁, 在stop中进行必要的解锁一边避免竟态。
当使用seq_read时, 需要实现seq_operations, 并且将file和seq_file关联起来(通常在open操作中进行), 同时, seq_file也提供了游离的 seq_lseek, seq_release用于支持llseek和release操作。
使用seq_file 的demo如下:文章来源地址https://www.toymoban.com/news/detail-793349.html

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm/stat.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

#define LOG_TAG    "my_test2: "

MODULE_LICENSE("GPL");

static char my_buf[][32] = {    {"i am the 0th record"},    //buf[0]
                {"i am the 1th record"},    //buf[1]
                {"i am the 2th record"},    //buf[2]
                {"i am the 3th record"},    //buf[3]
                {"i am the 4th record"},    //buf[4]
};

//==========================================================================
static void* my_seq_start (struct seq_file *m, loff_t *pos)
{
    pr_info(LOG_TAG "enter %s()\n", __func__);

    if( *pos < 0 || *pos > 4 /*my_buf[][32] index*/)    {
        pr_err(LOG_TAG "invalid pos %ld\n", (long)*pos);
        return NULL;
    }else    
        return my_buf[*pos];
}

static void my_seq_stop (struct seq_file *m, void *v)
{
    pr_info(LOG_TAG "enter %s()\n", __func__);
    pr_info(LOG_TAG "do nothing\n");
}

static void* my_seq_next (struct seq_file *m, void *v, loff_t *pos)
{
    pr_info(LOG_TAG "enter %s()\n", __func__);

    if( ++(*pos) < 0  || *pos > 4 /*my_buf[][32] index*/)    {
        pr_info(LOG_TAG "end\n");
        return NULL;
    }else
        return my_buf[*pos];
}

static int my_seq_show (struct seq_file *m, void *v)
{
    seq_printf(m, "%s\n", (char*)v);
    return 0;    
}

static struct seq_operations my_seq_ops = {
    .start    =    my_seq_start,
    .stop    =    my_seq_stop,
    .next    =    my_seq_next,
    .show    =    my_seq_show,
      };
//==========================================================================

static int my_proc_open(struct inode * inode , struct file * file) 
{ 
    return seq_open(file, &my_seq_ops ); 
}

static struct file_operations my_file_ops = {
    .owner      = THIS_MODULE,
    .open     = my_proc_open,
    .read      = seq_read,
    .llseek  = seq_lseek,
    .release = seq_release,
};

//==========================================================================
static int __init my_module_init(void)
{
    struct proc_dir_entry * my_proc_node = NULL;

    pr_info(LOG_TAG "enter %s()\n", __func__);

    my_proc_node = proc_create("my_test2", S_IRUGO, NULL, &my_file_ops);
    if( NULL == my_proc_node )    {
        pr_err(LOG_TAG "creat /proc/my_test2 failed\n");
        remove_proc_entry("my_test2", NULL);
        return -1;
    }

    pr_info(LOG_TAG "creat /proc/my_test2 success\n");
    return 0;
}

static void __exit my_module_exit(void)
{
    pr_info(LOG_TAG "enter %s()\n", __func__);

    pr_info(LOG_TAG "remove /proc/my_test2\n");
    remove_proc_entry("my_test2", NULL);
}

module_init(my_module_init);
module_exit(my_module_exit);    

到了这里,关于开发中proc文件系统的简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Linux】linux5.6引入struct proc_ops,用以替代struct file_operations在/proc下进行文件操作

    linux5.10生成在/proc目录下的文件时,利用cat读取文件,提示: 该报错是错误码:EPERM,不允许操作 发现是在移植内核代码时,未对proc接口进行适配。 linux-5.6引入结构体struct proc_ops,用以替代struct file_operations在/proc下进行文件操作。 proc_create中的proc_ops结构体类型定义改变,导

    2024年02月08日
    浏览(24)
  • [驱动开发]Linux内核定时器与中断的简单应用

    首先介绍一下定时器原理。 在linux系统中定时器有分为软定时和硬件定时器。 以海思某款芯片为例,定时器模块又称为Timer模块,主要实现定时、计数功能。 Timer 具有以下特点: 带可编程 8 位预分频器的 32bit/16bit 减法定时器/计数器。 Timer 的计数时钟为 3MHz 时钟。 支持 3 种

    2024年02月20日
    浏览(28)
  • 2023-07-10 linux IIO子系统使用学习,在TI 的ads1015驱动里面看到相关使用,故花点时间进行简单的学习,入门级别,纪录点滴。

    一、Linux IIO(Industrial I/O)架构是Linux内核提供的一种用于支持各种类型传感器和数据采集设备的子系统,包括温度、压力、湿度、加速度、光度等多种传感器。 二、这个就是ads1015的驱动,里面用到iio子系统。 ti-ads1015.c « adc « iio « drivers - kernel/git/torvalds/linux.git - Linux kerne

    2024年02月17日
    浏览(32)
  • linux i2c驱动开发之最简单入门:设备树与驱动的匹配

    1在设备树中声明我们的模拟I2C设备: i2c的设备加入是必须放在某个i2c的总线下面的,我们这里是放在 i2c7下面。 可以直接在设备树搜索i2c7,然后把status 修改为okay,然后再加入设备信息. 这里我们指定和驱动匹配的字符串是 myi2c-dev,在bus segment 上分配的地址是 0x70 。 当然这

    2024年02月16日
    浏览(34)
  • <Linux开发>驱动开发 -之-Linux INPUT 子系统

    <Linux开发>驱动开发 -之-Linux INPUT 子系统 交叉编译环境搭建: <Linux开发> linux开发工具-之-交叉编译环境搭建 uboot移植可参考以下: <Linux开发> -之-系统移植 uboot移植过程详细记录(第一部分) <Linux开发> -之-系统移植 uboot移植过程详细记录(第二部分) <Linux开发

    2024年02月09日
    浏览(36)
  • Linux驱动开发:SPI子系统

    MISO:主设备数据输入,从设备数据输出。 MOSI:主设备数据输出,从设备数据输入。 SCLK:时钟信号,由主设备产生。 CS:    从设备片选信号,由主设备控制。 CPOL(时钟极性) :   0:时钟起始位低电平      1:时钟起始为高电平   CPHA(时钟相位) :0:第一个时钟周期采样   1

    2024年02月06日
    浏览(37)
  • Linux驱动开发:gpio子系统

    目录 1、GPIO配置流程 2、GPIO子系统API 2.1 of_find_node_by_path 2.2 of_get_named_gpio 2.3 gpio_request 与 gpiod_get 与 gpiod_get_index 2.4 gpio_direction_input 与 gpiod_direction_input 2.5 gpio_direction_output 与 gpiod_direction_output 2.6 gpio_get_value 与 gpiod_get_value 2.7 gpio_set_value 与 gpiod_set_value 2.8  gpiod_get_from

    2024年02月12日
    浏览(36)
  • 嵌入式Linux驱动开发系列五:Linux系统和HelloWorld

    三个问题 了解Hello World程序的执行过程有什么用? 编译和执行:Hello World程序的执行分为两个主要步骤:编译和执行。编译器将源代码转换为可执行文件,然后计算机执行该文件并输出相应的结果。了解这个过程可以帮助我们理解如何将代码转化为可运行的程序。 语法和语义

    2024年02月13日
    浏览(46)
  • 【Linux驱动开发】010 pinctrl子系统

    上一章我们编写了基于设备树的 LED 驱动,但是驱动的本质还是没变,都是配置 LED 灯所使用的 GPIO 寄存器,驱动开发方式和裸机基本没啥区别。本章我们就来学习一下如何借助 pinctrl 和 gpio 子系统来简化 GPIO 驱动开发。   Linux 内核针对 PIN 的配置推出了 pinctrl 子系统,对于

    2024年02月14日
    浏览(29)
  • 【Linux驱动开发】011 gpio子系统

    前面我们编写了基于设备树的 LED 驱动,但是驱动的本质还是没变,都是配置 LED 灯所使用的 GPIO 寄存器,驱动开发方式和裸机基本没啥区别。本章我们就来学习一下如何借助 pinctrl 和 gpio 子系统来简化 GPIO 驱动开发。   Linux 内核针对 PIN 的配置推出了 pinctrl 子系统,对于

    2024年02月03日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包