頭檔案linux/seq_file.h
seq相關函數的實作在fs/seq_file.c
struct seq_file {
char *buf;
size_t size;
size_t from;
size_t count;
loff_t index;
loff_t read_pos;
u64 version;
struct mutex lock;
const struct seq_operations *op;
int poll_event;
void *private;
};
struct seq_operations {
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);
};
start實作初始化工作,在周遊一個連結對象開始時調用
stop當所有連結對象周遊結束時調用,主要完成一些清理工作
next用來在周遊中尋找下一個連結對象,傳回下一個對象或者NULL
show對周遊對象進行操作的函數主要是調用seq_printf, seq_puts之類的函數,列印出這個對象節點的資訊。
//seq操作包括以下一系列函數
int seq_open(struct file *, const struct seq_operations *);
ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
loff_t seq_lseek(struct file *, loff_t, int);
int seq_release(struct inode *, struct file *);
實作例子
/*
* Documentation/filesystem/seq_file.txt
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
//#define DEBUG_SEQ
#ifdef DEBUG_SEQ
#define log_seq(...) printk(__VA_ARGS__)
#else
#define log_seq(...)
#endif
static void *ct_seq_start(struct seq_file *s, loff_t *pos)
{
int *count = s->private;
log_seq("%s\n", __func__);
if ((long long)*pos < *count) {
printk("start pos %lld\n", (long long)*pos);
return pos;
}
return NULL;
}
static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
int *count = s->private;
log_seq("%s\n", __func__);
++*pos;
if ((long long)*pos < *count) {
printk("next pos %lld\n", (long long)*pos);
return pos;
}
return NULL;
}
static void ct_seq_stop(struct seq_file *s, void *v)
{
log_seq("%s\n", __func__);
}
static int ct_seq_show(struct seq_file *s, void *v)
{
loff_t *pos = v;
log_seq("%s\n", __func__);
seq_printf(s, "%lld\n", (long long)*pos);
return 0;
}
static const struct seq_operations ct_seq_ops = {
.start = ct_seq_start,
.next = ct_seq_next,
.stop = ct_seq_stop,
.show = ct_seq_show
};
static int ct_open(struct inode *inode, struct file *file)
{
int ret;
struct seq_file *s;
ret = seq_open(file, &ct_seq_ops);
s = file->private_data;
s->private = (void *)kmalloc(sizeof(int), GFP_KERNEL);
*((int *)s->private) = 5;
return ret;
}
static int ct_close(struct inode *inode, struct file *file)
{
struct seq_file *s = file->private_data;
kfree(s->private);
return seq_release(inode, file);
}
static const struct file_operations ct_file_ops = {
.owner = THIS_MODULE,
.open = ct_open,
.read = seq_read,
// .write = seq_write,
.llseek = seq_lseek,
.release = ct_close
};
static int __init ct_init(void)
{
struct proc_dir_entry *entry;
entry = proc_create("sequence", 0, NULL, &ct_file_ops);
return 0;
}
static void __exit ct_exit(void)
{
remove_proc_entry("sequence", NULL);
}
module_init(ct_init);
module_exit(ct_exit);
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAnYldHL0FWby9mZvwFN4ETMfdHLkVGepZ2XtxSZ6l2clJ3LcV2Zh1Wa9M3clN2byBXLzN3btgHL9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsQTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5yM5QTNyQDMzUDZ1cjM0AjNzYzXzMzN0UDMxAzLcFTMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
參考文章
- 讀取proc檔案之seq_file
參考資源
-
實作核心seq操作的例子 其他可以參考的文章
http://blog.chinaunix.net/uid-28253945-id-3382865.html
- http://blog.chinaunix.net/uid-28253945-id-3382866.html