5.4. 使用 seq_file 管理 /proc 文件

正如我们所见,编写 /proc 文件可能相当“复杂”。因此,为了帮助人们编写 /proc 文件,有一个名为 seq_file 的 API 可以帮助格式化 /proc 文件以进行输出。它基于序列,序列由 3 个函数组成:start()、next() 和 stop()。当用户读取 /proc 文件时,seq_file API 启动一个序列。

序列以调用 start() 函数开始。如果返回值是非 NULL 值,则调用 next() 函数。此函数是一个迭代器,目标是遍历所有数据。每次调用 next() 时,也会调用 show() 函数。它将数据值写入用户读取的缓冲区中。next() 函数被调用直到它返回 NULL。当 next() 返回 NULL 时,序列结束,然后调用 stop() 函数。

请注意:当一个序列结束后,另一个序列会开始。这意味着在 stop() 函数结束时,start() 函数会再次被调用。当 start() 函数返回 NULL 时,此循环结束。您可以在“seq_file 工作原理”图中看到这一点。

图 5-1. seq_file 工作原理

Seq_file 为 file_operations 提供了基本函数,如 seq_read、seq_lseek 和其他一些函数。但没有用于写入 /proc 文件的函数。当然,您仍然可以使用与前面示例中相同的方法。

示例 5-4. procfs4.c

/**
 *  procfs4.c -  create a "file" in /proc
 * 	This program uses the seq_file library to manage the /proc file.
 *
 */

#include <linux/kernel.h>	/* We're doing kernel work */
#include <linux/module.h>	/* Specifically, a module */
#include <linux/proc_fs.h>	/* Necessary because we use proc fs */
#include <linux/seq_file.h>	/* for seq_file */

#define PROC_NAME	"iter"

MODULE_AUTHOR("Philippe Reynes");
MODULE_LICENSE("GPL");

/**
 * This function is called at the beginning of a sequence.
 * ie, when:
 *	- the /proc file is read (first time)
 *	- after the function stop (end of sequence)
 *
 */
static void *my_seq_start(struct seq_file *s, loff_t *pos)
{
	static unsigned long counter = 0;

	/* beginning a new sequence ? */	
	if ( *pos == 0 )
	{	
		/* yes => return a non null value to begin the sequence */
		return &counter;
	}
	else
	{
		/* no => it's the end of the sequence, return end to stop reading */
		*pos = 0;
		return NULL;
	}
}

/**
 * This function is called after the beginning of a sequence.
 * It's called untill the return is NULL (this ends the sequence).
 *
 */
static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	unsigned long *tmp_v = (unsigned long *)v;
	(*tmp_v)++;
	(*pos)++;
	return NULL;
}

/**
 * This function is called at the end of a sequence
 * 
 */
static void my_seq_stop(struct seq_file *s, void *v)
{
	/* nothing to do, we use a static value in start() */
}

/**
 * This function is called for each "step" of a sequence
 *
 */
static int my_seq_show(struct seq_file *s, void *v)
{
	loff_t *spos = (loff_t *) v;
	
	seq_printf(s, "%Ld\n", *spos);
	return 0;
}

/**
 * This structure gather "function" to manage the sequence
 *
 */
static struct seq_operations my_seq_ops = {
	.start = my_seq_start,
	.next  = my_seq_next,
	.stop  = my_seq_stop,
	.show  = my_seq_show
};

/**
 * This function is called when the /proc file is open.
 *
 */
static int my_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &my_seq_ops);
};

/**
 * This structure gather "function" that manage the /proc file
 *
 */
static struct file_operations my_file_ops = {
	.owner   = THIS_MODULE,
	.open    = my_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};
	
	
/**
 * This function is called when the module is loaded
 *
 */
int init_module(void)
{
	struct proc_dir_entry *entry;

	entry = create_proc_entry(PROC_NAME, 0, NULL);
	if (entry) {
		entry->proc_fops = &my_file_ops;
	}
	
	return 0;
}

/**
 * This function is called when the module is unloaded.
 *
 */
void cleanup_module(void)
{
	remove_proc_entry(PROC_NAME, NULL);
}

如果您想了解更多信息,您可以阅读此网页

您也可以阅读 linux 内核中 fs/seq_file.c 的代码。