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线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

Linux之platform平台设备驱动详解

《Linux之platform平台设备驱动详解》Linux设备驱动模型中,Platform总线作为虚拟总线统一管理无物理总线依赖的嵌入式设备,通过platform_driver和platform_de... 目录platform驱动注册platform设备注册设备树Platform驱动和设备的关系总结在 l

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st