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中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变