Linux C 内核编程 /proc 编程例子

2024-09-03 08:52
文章标签 linux 编程 内核 例子 proc

本文主要是介绍Linux C 内核编程 /proc 编程例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码

  • proc_demo.c
  • 内核版本: 5.4.0-150-generic
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/slab.h>struct proc_head
{struct list_head lhead;int item_count;int str_count;
};struct proc_item
{struct list_head litem;char *buf;int num;
};struct proc_head *gp_head;// linux/seq_file.h
// 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);/**
* author:  aran
* fuction: seq_operations -> start
*/
static void *my_seq_start(struct seq_file *m, loff_t *pos)
{struct proc_item *entry;if (0 == *pos){seq_printf(m, "List has %d items, total %d bytes\n", gp_head->item_count, gp_head->str_count);}// get first item++*pos;list_for_each_entry(entry, &gp_head->lhead, litem){if (*pos == entry->num){return entry;}}return NULL;
}/**
* author:  aran
* fuction: seq_operations -> next
*/
static void *my_seq_next(struct seq_file *m, void *v, loff_t *pos)
{struct proc_item *entry;// get next item++*pos;list_for_each_entry(entry, &gp_head->lhead, litem){if (*pos == entry->num){return entry;}}return NULL;
}/**
* author:  aran
* fuction: seq_operations -> stop
*/
static void my_seq_stop(struct seq_file *m, void *v)
{// clean sth.// nothing to do
}/**
* author:  aran
* fuction: seq_operations -> show
*/
static int my_seq_show(struct seq_file *m, void *v)
{struct proc_item *tmp = v;seq_printf(m, "%s", tmp->buf);return 0;
}// global var
static struct seq_operations my_seq_fops = 
{.start	= my_seq_start,.next	= my_seq_next,.stop	= my_seq_stop,.show	= my_seq_show,
};// file_operations
// int (*open) (struct inode *, struct file *)
// ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *)/**
* author:  aran
* fuction: file_operations -> open
*/
static int proc_seq_open(struct inode *inode, struct file *file)
{return seq_open(file, &my_seq_fops);
}/**
* author:  aran
* fuction: file_operations -> write
*/
static ssize_t proc_seq_write(struct file *file, const char __user *buffer, size_t count, loff_t *f_pos)
{struct proc_item *tmp;int ret;// allocate proc_itemtmp = (struct proc_item *)kzalloc(sizeof(*tmp), GFP_KERNEL);if (NULL == tmp){ret = -ENOMEM;goto err_kzalloc1;}INIT_LIST_HEAD(&tmp->litem);// allocate str buftmp->buf = (char *)kzalloc(count, GFP_KERNEL);if (NULL == tmp->buf){ret = -ENOMEM;goto err_kzalloc2;}// 从用户空间获取数据 buffer if (0 != copy_from_user(tmp->buf, buffer, count)){ret = -1;goto err_copy;}list_add(&tmp->litem, &gp_head->lhead);gp_head->item_count++;gp_head->str_count += count;tmp->num = gp_head->item_count; return count;err_copy:kfree(tmp->buf);
err_kzalloc2:kfree(tmp);
err_kzalloc1:return ret;
}// global var
static struct file_operations proc_seq_fops = 
{.owner		= THIS_MODULE,.open		= proc_seq_open,.read		= seq_read,.write		= proc_seq_write,.llseek		= seq_lseek,.release	= seq_release,
};static int __init my_init(void)
{struct proc_dir_entry *file;int ret;// allocate & init proc_headgp_head = (struct proc_head *)kzalloc(sizeof(*gp_head), GFP_KERNEL);if (NULL == gp_head){ret = -ENOMEM;goto err_kzalloc;}gp_head->item_count = 0;gp_head->str_count = 0;INIT_LIST_HEAD(&gp_head->lhead);// create "/proc/proc_seq" filefile = proc_create_data("proc_seq",	// name0666,		// modeNULL,		// parent dir_entry&proc_seq_fops,	// file_operationsNULL		// data);if (NULL == file){printk("Count not create /proc/proc_seq file!\n");ret = -1;goto err_proc_create_data;}printk("init success.\n");return 0;err_proc_create_data:kfree(gp_head);
err_kzalloc:return ret;
}static void __exit my_exit(void)
{struct proc_item *tmp1, *tmp2;remove_proc_entry("proc_seq", NULL);list_for_each_entry_safe(tmp1, tmp2, &gp_head->lhead,litem){list_del(&tmp1->litem);kfree(tmp1->buf);kfree(tmp1);}kfree(gp_head);printk("exit success.\n");
}module_init(my_init);
module_exit(my_exit);MODULE_AUTHOR("yong");
MODULE_LICENSE("GPL");
  • Makefile
obj-m:=proc_demo.o      CURRENT_PATH:=$(shell pwd)    
LINUX_KERNEL:=$(shell uname -r)  
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)  all:make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules  #编译模块clean:make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean   #清理

输入 make
在这里插入图片描述

执行 insmod proc_demo.ko 即可

end

这个代码例子涉及的东西比较多,不适合入门。但是,如果平时有写这个内核文件 /proc 的话,应该比较容易看懂。至于具体解释以后有时间再写。

这篇关于Linux C 内核编程 /proc 编程例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132558

相关文章

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Linux环境变量&&进程地址空间详解

《Linux环境变量&&进程地址空间详解》本文介绍了Linux环境变量、命令行参数、进程地址空间以及Linux内核进程调度队列的相关知识,环境变量是系统运行环境的参数,命令行参数用于传递给程序的参数,... 目录一、初步认识环境变量1.1常见的环境变量1.2环境变量的基本概念二、命令行参数2.1通过命令编程

Linux之进程状态&&进程优先级详解

《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的

Linux编译器--gcc/g++使用方式

《Linux编译器--gcc/g++使用方式》文章主要介绍了C/C++程序的编译过程,包括预编译、编译、汇编和链接四个阶段,并详细解释了每个阶段的作用和具体操作,同时,还介绍了调试和发布版本的概念... 目录一、预编译指令1.1预处理功能1.2指令1.3问题扩展二、编译(生成汇编)三、汇编(生成二进制机器语

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同

Rsnapshot怎么用? 基于Rsync的强大Linux备份工具使用指南

《Rsnapshot怎么用?基于Rsync的强大Linux备份工具使用指南》Rsnapshot不仅可以备份本地文件,还能通过SSH备份远程文件,接下来详细介绍如何安装、配置和使用Rsnaps... Rsnapshot 是一款开源的文件系统快照工具。它结合了 Rsync 和 SSH 的能力,可以帮助你在 li