序列檔案 seq file 介面

2021-05-06 17:43:19 字數 4164 閱讀 3664

內容簡介:

本文主要講述序列檔案(seq_file)介面的核心實現,如何使用它將linux核心裡面常用的資料結構通過檔案(主要關注proc檔案)匯出到使用者空間,最後定義了一些巨集以便於程式設計,減少重複**。在分析序列檔案介面實現的過程中,還連帶涉及到一些應用陷阱和避免手段。

序列檔案介面:

unix的世界裡,檔案是最普通的概念,所以用檔案來作為核心和使用者空間傳遞資料的介面也是再普通不過的事情,並且這樣的介面對於shell也是相當友好的,方便管理員通過shell直接管理系統。由於偽檔案系統proc檔案系統在處理大資料結構(大於一頁的資料)方面有比較大的侷限性,使得在那種情況下進行程式設計特別彆扭,很容易導致bug,所以序列檔案介面被發明出來,它提供了更加友好的介面,以方便程式設計師。之所以選擇序列檔案介面這個名字,應該是因為它主要用來匯出一條條的記錄資料。

#include

#include

#include

#include

#include

static struct mutex lock;

static struct list_head head;

struct my_data ;

static void add_one(void)

static ssize_t _seq_write(struct file *file, const char __user * buffer,

size_t count, loff_t *ppos)

static int _seq_show(struct seq_file *m, void *p)

static void *_seq_start(struct seq_file *m, loff_t *pos)

static void *_seq_next(struct seq_file *m, void *p, loff_t *pos)

static void _seq_stop(struct seq_file *m, void *p)

static struct seq_operations _seq_ops = ;

static int _seq_open(struct inode *inode, struct file *file)

static struct file_operations _seq_fops = ;

static void clean_all(struct list_head *head)

}static int __init init(void)

entry->proc_fops = &_seq_fops;

return 0;

}static void __exit fini(void)

module_init(init);

module_exit(fini);

seq_file file

system

針對proc檔案的不足而誕生了seq_file。

seq_file的實現基於proc檔案。使用seq_file,使用者必須抽象出乙個鏈結物件,然後可以依次遍歷這個鏈結物件。這個鏈結物件可以是鍊錶,陣列,雜湊表等等。

程式設計介面

seq_file必須實現四個操作函式:start(

), next(

), show(

), stop(

)。struct seq_operations

;start(

):主要實現初始化工作,在遍歷乙個鏈結物件開始時,呼叫。返回乙個鏈結物件的偏移或者seq_start_token(表徵這是所有迴圈的開始)。出錯返回err_ptr。

stop():

當所有鏈結物件遍歷結束時呼叫。主要完成一些清理工作。

next():

):對遍歷物件進行操作的函式。主要是呼叫seq_printf(

), seq_puts(

)之類的函式,列印出這個物件節點的資訊。

下圖描述了seq_file函式對乙個鍊錶的遍歷。

2、重要的資料結構

除了struct seq_operations以外,另乙個最重要的資料結構是struct seq_file:

struct seq_file

;該結構會在seq_open函式呼叫中分配,然後作為引數傳遞給每個seq_file的操作函式。privat變數可以用來在各個操作函式之間傳遞引數。

seq_hello.c

#include

#include

#include

#include

#include

#define proc_name "test_proc"

#define max_lines 10

typedef

struct item

user_item;

user_item items[4]

;module_author(

"zhang jie:[email protected]");

module_license(

"gpl");

static

void

*my_seq_start(

struct seq_file *s, loff_t *pos)

else

}static

void

*my_seq_next(

struct seq_file *s,

void

*v, loff_t *pos)

else

}static

void my_seq_stop(

struct seq_file *s,

void

*v)static

int my_seq_show(

struct seq_file *s,

void

*v)items[0]

.value =

'0';

items[1]

.value =

'1';

items[2]

.value =

'2';

items[3]

.value =

'3';

seq_printf(s,

"%ld=%c,%ld=%c,%ld=%c,%ld=%c;/n"

, items[0]

.key,

items[0]

.value, items[1]

.key, items[1]

.value, items[2]

.key,

items[2]

.value, items[3]

.key, items[3]

.value)

;return 0;

}static

struct seq_operations my_seq_ops =

;static

int my_open(

struct inode *inode,

struct

file

*file);

static

struct file_operations my_file_ops =

;int init_module(

void

)printk(kern_info"initialze /proc/net/test_proc success!/n");

return 0;

}void cleanup_module(

void

)makefile

obj-m :

= seq_hello.o

kdir :

=/lib/modules/

$(shell uname -r)

/build

pwd :

= $(shell pwd)

default

:$(make)

-c $(kdir) m=

$(pwd) modules

clean:

$(rm)

*.o *

.mod.c *

.ko *

.symvers *

.markers *

.order

[root@zj:

~/desktop/net/seq]

# cat /proc/net/test_proc

序列檔案 seq file 介面

內容簡介 本文主要講述序列檔案 seq file 介面的核心實現,如何使用它將linux核心裡面常用的資料結構通過檔案 主要關注proc檔案 匯出到使用者空間,最後定義了一些巨集以便於程式設計,減少重複 在分析序列檔案介面實現的過程中,還連帶涉及到一些應用陷阱和避免手段。序列檔案介面 unix的世界...

序列檔案 seq file 介面

內容簡介 本文主要講述序列檔案 seq file 介面的核心實現,如何使用它將linux核心裡面常用的資料結構通過檔案 主要關注proc檔案 匯出到 使用者空間,最後定義了一些巨集以便於程式設計,減少重複 在分析序列檔案介面實現的過程中,還連帶涉及到一些應用陷阱和避免手段。序列檔案介面 unix的世...

FreeStyler 序列檔案分析

分析過的軟體版本 3.5.8 和3.4.6 檔案型別 chb 控制器型別 udmx chb序列檔案位元組數為 檔案儲存形式為 小端 6 6150 2 52 fixturesnum stepnum 2 2 fixturesnum 6 stepnum 1 c1 c2 c3 c4 檔案組成順序為 c1 c...