一、proc文件系统
proc文件系统是一种无存储的文件系统,当读其中的文件时,其内容动态生成,当写文件时,文件所关联的写函数被调用。上面说文件关联的读写函数,其实就是在创建对应proc 文件的驱动中实现的。当proc文件时,就相当于调用了,对应驱动的read函数。这里我们不研究proc的实现原理,我也没那么多时间,这里就先会用,后面在来研究它的实现原理吧。感兴趣的话,可以参考博文。
二、几个重要的API
我们现在注重的是编码。针对编码,我们需要熟练掌握的的API有3个,如下所示,如果后面遇到常用的,会在博客上更新。
1.proc_create()
作用:这个函数的作用就是创建proc文件,并将proc文件和fops绑定。
static inline struct proc_dir_entry *proc_create(
const char *name, //创建proc文件的名字
umode_t mode, //读写权限,例如:读,写,执行
struct proc_dir_entry *parent, //父目录,如果为NULL,会在/proc创建proc文件。
const struct file_operations *proc_fops ) //文件的操作集合,具体可参考例子。
返回值:返回当前proc文件的入口结构体
2.proc_mkdir()
作用:只是在proc下创建文件目录
struct proc_dir_entry *proc_mkdir(
const char *, //创建proc目录的名称
struct proc_dir_entry *); //父目录
返回值:返回当前目录的入口结构体
3.proc_remove()
作用:见名知意,删除对应的proc文件。
void proc_remove(
struct proc_dir_entry *de //要删除结构的入口结构体指针
)
返回值:无
三、例子show time
下面这个例子,我在本地已经验证过。大家要把这个流程熟记于心。先要会写,后在理解。
/*************************************************************************
> File Name: proc.c
> Author: armwind
> Mail: armwind@163.com
> Created Time: Tue 16 Aug 2016 19:30:38 CST
************************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/proc_fs.h> //必须要包含的头文件
#include <linux/seq_file.h> //必须要包含的头文件
static DEFINE_MUTEX(registration_lock);
#define TEST_PROC_FS //这里我们可以在对应驱动中添加菜单来控制。
#ifdef TEST_PROC_FS
static int test_proc_show(struct seq_file *m, void *v)
{
#if 0 //其实这一段我也是从其它驱动中拿过来的,这里保留原始的code,大家看看它都做了什么。
struct saa7164_dev *dev;
struct tmComResBusInfo *b;
struct list_head *list;
int i, c;
if (saa7164_devcount == 0)
return 0;
list_for_each(list, &saa7164_devlist) {
dev = list_entry(list, struct saa7164_dev, devlist);
seq_printf(m, "%s = %p\n", dev->name, dev);
/* Lock the bus from any other access */
b = &dev->bus;
mutex_lock(&b->lock);
seq_printf(m, " .m_pdwSetWritePos = 0x%x (0x%08x)\n", //下面这个可以做很多打印工作,在实际的开发中,我们可以打印任何我们驱动中的信息。
b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
seq_printf(m, " .m_pdwSetReadPos = 0x%x (0x%08x)\n",
b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
seq_printf(m, " .m_pdwGetWritePos = 0x%x (0x%08x)\n",
b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
seq_printf(m, " .m_pdwGetReadPos = 0x%x (0x%08x)\n",
b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
c = 0;
seq_printf(m, "\n Set Ring:\n");
seq_printf(m, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (i = 0; i < b->m_dwSizeSetRing; i++) {
if (c == 0)
seq_printf(m, " %04x:", i);
seq_printf(m, " %02x", *(b->m_pdwSetRing + i));
if (++c == 16) {
seq_printf(m, "\n");
c = 0;
}
}
c = 0;
seq_printf(m, "\n Get Ring:\n");
seq_printf(m, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (i = 0; i < b->m_dwSizeGetRing; i++) {
if (c == 0)
seq_printf(m, " %04x:", i);
seq_printf(m, " %02x", *(b->m_pdwGetRing + i));
if (++c == 16) {
seq_printf(m, "\n");
c = 0;
}
}
mutex_unlock(&b->lock);
}
#endif
seq_printf(m, "you can read something here\n");
seq_printf(m, "you can read something many times\n");
return 0;
}
static int test_proc_open(struct inode *inode, struct file *filp)
{
return single_open(filp, test_proc_show, NULL); //open函数中包含了single_open(),先按着别人这样写就行。
}
static const struct file_operations test_proc_fops = {
.open = test_proc_open,
.read = seq_read, //这个是系统默认的
.llseek = seq_lseek, //同样是系统默认的
.release = single_release, //因为在proc_open的时候使用的是single_open(),所以这里必须使用single_release.
};
struct proc_dir_entry *entry = NULL;
static int test_proc_create(void)
{
entry = proc_create("read_one_time", S_IRUGO, NULL, &test_proc_fops);//将对应proc文件read_one_time和test_proc_fops捆绑
if (!entry)
return -ENOMEM;
return 0;
}
#endif
int hello_init(void)
{
if(test_proc_create())
printk(KERN_ALERT "proc create failed\n");
printk(KERN_ALERT "proc create success\n");
return 0;
}
void hello_exit(void)
{
proc_remove(entry);
printk(KERN_ALERT "Hello Exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL"); //如果没这个,加载模块时,它会提示找不到若干symbol。
2.proc文件创建效果图
3.使用cat命令查看
4.使用vim查看
总结:上面可以看出,不管你是使用哪一种读方式,最终都会调用static int test_proc_show()函数,不管过程怎么样,结果都是一样的