ubuntu/linux mint 创建proc文件的三种方法(四)
在做内核驱动开发的时候,能够使用/proc下的文件,获取对应的信息,以便调试。
大多数/proc下的文件是仅仅读的,但为了演示样例的完整性,都提供了写方法。
方法一:使用create_proc_entry创建proc文件(简单,但写操作有缓冲区溢出的危急);
方法二:使用proc_create和seq_file创建proc文件(较方法三简洁);
方法三:使用proc_create_data和seq_file创建proc文件(较麻烦,但比較完整);
演示样例四:在proc文件里使用内核链表的一个演示样例(用的方法三)。
--------------------------------------------------------------------------------------------------------------------
四、
proc_test04.c 源代码
#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_item
tmp = (struct proc_item *)kzalloc(sizeof(*tmp), GFP_KERNEL);
if (NULL == tmp)
{
ret = -ENOMEM;
goto err_kzalloc1;
}
INIT_LIST_HEAD(&tmp->litem); // allocate str buf
tmp->buf = (char *)kzalloc(count, GFP_KERNEL);
if (NULL == tmp->buf)
{
ret = -ENOMEM;
goto err_kzalloc2;
} 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_head
gp_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" file
file = proc_create_data(
"proc_seq", // name
0666, // mode
NULL, // parent dir_entry
&proc_seq_fops, // file_operations
NULL // data
);
if (NULL == file)
{
printk("Count not create /proc/proc_seq file!\n");
ret = -1;
goto err_proc_create_data;
} 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);
} module_init(my_init);
module_exit(my_exit); MODULE_AUTHOR("aran");
MODULE_LICENSE("GPL");
Makefile文件:
obj-m := proc_test04.o
KERNEL := /lib/modules/`uname -r`/build #for mint/ubuntu
#KERNEL := /lib/modules/`uname -r`/source #for redhat all:
make -C $(KERNEL) M=`pwd` modules install:
make -C $(KERNEL) M=`pwd` modules_install
depmod -A clean:
make -C $(KERNEL) M=`pwd` clean
測试结果:
ubuntu/linux mint 创建proc文件的三种方法(四)的更多相关文章
- ubuntu/linux mint 创建proc文件的三种方法(两)
在这样做的内核驱动程序的开发时间.可以使用/proc下档.获取相应的信息.对于调试. 大多数/proc下的文件是仅仅读的.但为了演示样例的完整性.都提供了写方法. 方法一:使用create_proc_ ...
- Linux中创建Daemon进程的三种方法
什么是daemon进程? Unix/Linux中的daemon进程类似于Windows中的后台服务进程,一直在后台运行运行,例如http服务进程nginx,ssh服务进程sshd等.注意,其英文拼写为 ...
- Linux下查看磁盘挂载的三种方法
Linux下查看磁盘挂载的三种方法 2009-06-05 23:17 好久没有更新日志了,呵呵.不是没有要写的东东.实在抽不出时间来写,要准备公司的考试呢,C++考试.已经有七个月没有写C++代码了, ...
- linux 环境变量PATH路径的三种方法
转:http://www.jb51.net/LINUXjishu/150167.html 总结:修改1.#PATH=$PATH:/etc/apache/bin 或者#vi /etc/profile ...
- Linux 添加开机启动项的三种方法
linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh## This scri ...
- .net中创建xml文件的两种方法
.net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...
- VC中加载LIB库文件的三种方法
VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...
- 两台Linux系统之间传输文件的几种方法
两台Linux系统之间传输文件的几种方法:参考https://www.cnblogs.com/bignode/articles/9241333.html
- Linux CentOS7 VMware 安装软件包的三种方法、rpm包介绍、rpm工具用法、yum工具用法、yum搭建本地仓库
一.安装软件包的三种方法 Linux下游三种安装方法,rpm工具.yum工具.源码包.rpm按装一个程序包时,有可能因为该程序包依赖另一个程序包而无法安装:yum工具,可以连同依赖的程序包一起安装. ...
随机推荐
- int 转换成 CString(VC2008里有这个问题)
int s = 123; CString str; str.Format("%d",s); 这样就可以了,但是有的会提示这个错误 如果出现这个错误,就改成下面这个就OK了: st ...
- 【milonga】什么意思_英语milonga在线翻译_有道词典
[milonga]什么意思_英语milonga在线翻译_有道词典 milonga 网络释义英英释义 米隆加 本届探戈艺术节表演最受观众欢迎的是热情欢快的米隆加(Milonga)舞曲探戈,为了吸引年 ...
- Android面试题收集(有具体答案)
Android面试题目及其答案 1.Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每个Android应用程序都在它自己的进程中执行,都 ...
- Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换
public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ publ ...
- adb logcat 命令行用法
作者 :万境绝尘 转载请著名出处 eclipse 自带的 LogCat 工具太垃圾了, 开始用 adb logcat 在终端查看日志; 1. 解析 adb logcat 的帮助信息 在命令行中输入 ...
- C++ 需要返回值的函数却没有返回值的情况 单例模式
昨天在看前些天写的代码,发现一个错误. #include <iostream> using namespace std; class singleton { public: static ...
- git-daemon的快捷搭建
使用git-daemon进行git服务器搭建 1.安装git-daemon 前提是已经安装git sudo apt-get install git git-core 然后安装git-daemon su ...
- python获取实时股票信息
Python3获取股票行情数据(中国个股/中国指数/全球指数) #!/usr/local/bin/python3 #coding=utf-8 #source http://www.cnblogs.co ...
- 52. 模版和设计元素——Lotus Notes的代码重用
不论是理论上还是实用上,代码重用都是编程的一个重要议题.可以从两个角度来讨论代码重用. 一是逻辑上代码以怎样的方式被重用.既可以通过面向对象的思想普及以来耳熟能详的继承的方式.比如先建了一个车的基类, ...
- AFNetwork学习(二)——GET/POST请求
为了学习AFNetwork,自己搭建整理了一下AFNetwork向后台发送请求和后台返回json数据的整个处理过程.利用Struts2搭建了一个后台,提供Action并返回json数据 环境:Xcod ...