当前位置: 首页 > news >正文

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 的话,应该比较容易看懂。至于具体解释以后有时间再写。


http://www.mrgr.cn/news/18425.html

相关文章:

  • 分体设计不入耳,舒适佩戴的AI耳机,塞那G6S体验
  • 备忘录1【java环境变量手动更改】
  • WordPress 集网址、资源、资讯于一体的导航类主题开心版
  • C++ STL find_first_of 用法
  • 智能化浪潮赋能工业制造与报废拆解,基于高精度YOLOv8全系列参数【n/s/m/l/x】模型开发构建工业生产场景下车辆不同部位智能化分割检测识别分系统
  • echarts graphChart关系图简单逻辑实现
  • Git原理与使用
  • 高度集成:硬件和软件通常集成在一起是什么意思
  • 力扣SQL仅数据库(1068~1084)
  • [大数据]Debug:常见错误集合
  • RS-FS-N01风速变送器简明教程(485通信类型变送器)
  • Open3D mesh 模型精细化处理--中点剖分
  • 梧桐数据库(WuTongDB):B+树索引的原理、实现方式及与B树索引的区别
  • C++篇:C向C++迈进(下)
  • reduce使用场景
  • C++string类(1)
  • nestjs目录命名导致的循环引用
  • C语言操作符详解1(含进制转换,原反补码)
  • 自控原理-传递函数(闭环 扰动 偏差 前馈 复合 顺馈)
  • Linux | 匿名管道和命名管道:进程间通信数据流的桥梁