/proc 文件系统是 GNU/Linux 特有的。它是一个虚拟的文件系统,因此在该目录中的所有文件都不会消耗磁盘空间。通过它能够非常简便地了解系统信息,尤其是其中的大部分文件是人类可阅读的(不过还是需要一些帮助)。许多程序实际上只是从 /proc 的文件中收集信息,然后按照它们自己的格式组织后显示出来。有一些显示进程信息的程序(top、ps 等)就是这么作的。/proc 还是了解您系统硬件的好去处。就象那些显示进程信息的程序一样,不少程序只是提供了获取 /proc 中信息的接口。

最初开发 /proc 文件系统是为了提供有关系统中进程的信息。但是由于这个文件系统非常有用并且使用简单,因此内核中的很多元素也开始使用它来报告信息,或启用动态运行时配置。如在printk一章中说的在用户态控制内核printk打印级别以及打印速率。

通过/proc文件系统调试内核模块非常简单,内核已经实现好了框架,我们只需要实现一到两个回调函数即可。常用的方法有以下几个:

1,创建proc入口文件

struct proc_dir_entry *create_proc_entry(const char *name,
mode_t mode, struct proc_dir_entry *parent);

系统中为了方便还提供了一个创建只读文件的接口create_proc_read_entry,其实质还是调用了create_proc_entry函数,代码如下:

static inline struct proc_dir_entry *create_proc_read_entry(const char *name,
	mode_t mode, struct proc_dir_entry *base, 
	read_proc_t *read_proc, void * data)
{
	struct proc_dir_entry *res=create_proc_entry(name,mode,base);
	if (res) {
		res->read_proc=read_proc;
		res->data=data;
	}
	return res;
}

2,删除proc入口文件

void remove_proc_entry(const char *name, struct proc_dir_entry *parent);

注意:入口项不存在关联的所有者,也即对proc文件的使用并不会作用到模块的引用计数上,因此proc文件正在被使用时调用了此删除函数就会有问题。另一方面,如果模块卸载时不调用此函数删除入口项,有可能下次插入模块时会在同一路径创建一个同名文件,是不是很吃惊?但是在proc文件中就有可能发生,所以一定要在卸载模块时调用此方法。

3,proc文件读回调函数

static int (*proc_read)(char *page, char **start, 
   off_t off, int count,  int *eof, void *data);

4,proc文件写回调函数

static int proc_write_foobar(struct file *file,  const char *buffer, 
    unsigned long count,  void *data);

5,将读、写回调函数与create_proc_entry返回的对象联系起来就OK了,内核代码中有一个很好的例子(见文章的最后面),这里就不再自己写了。

6,另外通过proc文件我们也可以实现一个开关文件,我们在用户态只给proc文件中写入Y/N,0/1之类的值,在内核中通过判断proc文件的值来打开或者关闭模块的某一功能,其实内核中有一个更简单的实现--debugfs,以后再详细分析。


写到这里其实有一点怪异的地方,对文件系统稍微了解一些就会知道在linux中一切都是文件,对文件的操作都是需要实现一个叫file_operations的结构体,proc也是一个文件系统,其下的也都是文件,为什么上面几个步骤中却没有看到file_operations结构体的影子呢?

这个其实可以猜测一下,内核中应该是实现了一个默认的file_operations结构体,其读/写函数实现正好使用了我们上面说的读、写回调函数,我们通过内核代码来验证一下这个猜测是否正确。

1,create_proc_entry函数返回了一个struct proc_dir_entry结构体,看实现可以发现有file_operations结构体指针变量

struct proc_dir_entry {
	......
	const struct file_operations *proc_fops;    <==文件操作结构体
	struct proc_dir_entry *next, *parent, *subdir;
	void *data;
	read_proc_t *read_proc;                    <==读回调
	write_proc_t *write_proc;                  <==写回调
	......
};

2,create_proc_entry函数是先通过__proc_create创建一个proc_dir_entry结构体ent,然后通过调用proc_register来实现注册

struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
					 struct proc_dir_entry *parent)
{
	struct proc_dir_entry *ent;
	nlink_t nlink;

	......

	ent = __proc_create(&parent, name, mode, nlink);
	if (ent) {
		if (proc_register(parent, ent) < 0) {
			kfree(ent);
			ent = NULL;
		}
	}
	return ent;
}


3,proc_register注册时会判断ent中的file_operations变量是否为空,如果为空则赋一个值

static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
{
	unsigned int i;
	struct proc_dir_entry *tmp;
	.......

	if (S_ISDIR(dp->mode)) {
		......
	} else if (S_ISLNK(dp->mode)) {
		......
	} else if (S_ISREG(dp->mode)) {
		if (dp->proc_fops == NULL)
			dp->proc_fops = &proc_file_operations;
		if (dp->proc_iops == NULL)
			dp->proc_iops = &proc_file_inode_operations;
	}
<span style="white-space:pre">	</span>......
	return 0;
}

4,系统中默认的file_operations变量实现了read和write方法,定义如下

static const struct file_operations proc_file_operations = {
	.llseek		= proc_file_lseek,
	.read		= proc_file_read,
	.write		= proc_file_write,
};


5,proc_file_read通过__proc_file_read实现读操作

static ssize_t
proc_file_read(struct file *file, char __user *buf, size_t nbytes,
	       loff_t *ppos)
{
	......

	rv = __proc_file_read(file, buf, nbytes, ppos);

	pde_users_dec(pde);
	return rv;
}

6, __ proc_file_read代码中调用了我们上面所说的read回调函数来实现真正的读操作,这就印证了我们前面的猜测是正确的。

static ssize_t
__proc_file_read(struct file *file, char __user *buf, size_t nbytes,
	       loff_t *ppos)
{
	......
	while ((nbytes > 0) && !eof) {
		count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);

		start = NULL;
		if (dp->read_proc) {
			
			n = dp->read_proc(page, &start, *ppos,
					  count, &eof, dp->data);
		} else
			break;

		......

		*ppos += start < page ? (unsigned long)start : n;
		nbytes -= n;
		buf += n;
		retval += n;
	}
	free_page((unsigned long) page);
	return retval;
}


注意:

在新内核系统中已经去掉了read_proc和write_proc两个回调函数,而是直接实现file_operations中的read和write的方法,个人觉得这样挺好的,与其他文件操作做到了一致,至少不至于看实现步骤总觉得怪怪的。

procfs_example.c

/*
 * procfs_example.c: an example proc interface
 *
 * Copyright (C) 2001, Erik Mouw (mouw@nl.linux.org)
 *
 * This file accompanies the procfs-guide in the Linux kernel
 * source. Its main use is to demonstrate the concepts and
 * functions described in the guide.
 *
 * This software has been developed while working on the LART
 * computing board (http://www.lartmaker.nl), which was sponsored
 * by the Delt University of Technology projects Mobile Multi-media
 * Communications and Ubiquitous Communications.
 *
 * This program is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/jiffies.h>
#include <asm/uaccess.h>


#define MODULE_VERS "1.0"
#define MODULE_NAME "procfs_example"

#define FOOBAR_LEN 8

struct fb_data_t {
	char name[FOOBAR_LEN + 1];
	char value[FOOBAR_LEN + 1];
};


static struct proc_dir_entry *example_dir, *foo_file,
	*bar_file, *jiffies_file, *symlink;


struct fb_data_t foo_data, bar_data;


static int proc_read_jiffies(char *page, char **start,
			     off_t off, int count,
			     int *eof, void *data)
{
	int len;

	len = sprintf(page, "jiffies = %ld\n",
                      jiffies);

	return len;
}


static int proc_read_foobar(char *page, char **start,
			    off_t off, int count, 
			    int *eof, void *data)
{
	int len;
	struct fb_data_t *fb_data = (struct fb_data_t *)data;

	/* DON'T DO THAT - buffer overruns are bad */
	len = sprintf(page, "%s = '%s'\n", 
		      fb_data->name, fb_data->value);

	return len;
}


static int proc_write_foobar(struct file *file,
			     const char *buffer,
			     unsigned long count, 
			     void *data)
{
	int len;
	struct fb_data_t *fb_data = (struct fb_data_t *)data;

	if(count > FOOBAR_LEN)
		len = FOOBAR_LEN;
	else
		len = count;

	if(copy_from_user(fb_data->value, buffer, len))
		return -EFAULT;

	fb_data->value[len] = '\0';

	return len;
}


static int __init init_procfs_example(void)
{
	int rv = 0;

	/* create directory */
	example_dir = proc_mkdir(MODULE_NAME, NULL);
	if(example_dir == NULL) {
		rv = -ENOMEM;
		goto out;
	}
	/* create jiffies using convenience function */
	jiffies_file = create_proc_read_entry("jiffies", 
					      0444, example_dir, 
					      proc_read_jiffies,
					      NULL);
	if(jiffies_file == NULL) {
		rv  = -ENOMEM;
		goto no_jiffies;
	}

	/* create foo and bar files using same callback
	 * functions 
	 */
	foo_file = create_proc_entry("foo", 0644, example_dir);
	if(foo_file == NULL) {
		rv = -ENOMEM;
		goto no_foo;
	}

	strcpy(foo_data.name, "foo");
	strcpy(foo_data.value, "foo");
	foo_file->data = &foo_data;
	foo_file->read_proc = proc_read_foobar;
	foo_file->write_proc = proc_write_foobar;
		
	bar_file = create_proc_entry("bar", 0644, example_dir);
	if(bar_file == NULL) {
		rv = -ENOMEM;
		goto no_bar;
	}

	strcpy(bar_data.name, "bar");
	strcpy(bar_data.value, "bar");
	bar_file->data = &bar_data;
	bar_file->read_proc = proc_read_foobar;
	bar_file->write_proc = proc_write_foobar;
		
	/* create symlink */
	symlink = proc_symlink("jiffies_too", example_dir, 
			       "jiffies");
	if(symlink == NULL) {
		rv = -ENOMEM;
		goto no_symlink;
	}

	/* everything OK */
	printk(KERN_INFO "%s %s initialised\n",
	       MODULE_NAME, MODULE_VERS);
	return 0;

no_symlink:
	remove_proc_entry("bar", example_dir);
no_bar:
	remove_proc_entry("foo", example_dir);
no_foo:
	remove_proc_entry("jiffies", example_dir);
no_jiffies:			      
	remove_proc_entry(MODULE_NAME, NULL);
out:
	return rv;
}


static void __exit cleanup_procfs_example(void)
{
	remove_proc_entry("jiffies_too", example_dir);
	remove_proc_entry("bar", example_dir);
	remove_proc_entry("foo", example_dir);
	remove_proc_entry("jiffies", example_dir);
	remove_proc_entry(MODULE_NAME, NULL);

	printk(KERN_INFO "%s %s removed\n",
	       MODULE_NAME, MODULE_VERS);
}


module_init(init_procfs_example);
module_exit(cleanup_procfs_example);

MODULE_AUTHOR("Erik Mouw");
MODULE_DESCRIPTION("procfs examples");
MODULE_LICENSE("GPL");



GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:23 天前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐