簡(jiǎn)介
procfs文件系統(tǒng)是內(nèi)核中的一個(gè)特殊文件系統(tǒng)。它是一個(gè)虛擬文件系統(tǒng): 它不是實(shí)際的存儲(chǔ)設(shè)備中的文件,而是存在于內(nèi)存中。procfs中的文件是用來允許用戶空間的程序訪問內(nèi)核中的某些信息(比如進(jìn)程信息在?/proc/[0-9]+/中),或者用來做調(diào)試用途(/proc/ksyms,這個(gè)文件列出了已經(jīng)登記的內(nèi)核符號(hào),這些符號(hào)給出了變量或函數(shù)的地址。每行給出一個(gè)符號(hào)的地址,符號(hào)名稱以及登記這個(gè)符號(hào)的模塊。程序ksyms、insmod和kmod使用這個(gè)文件。它還列出了正在運(yùn)行的任務(wù)數(shù),總?cè)蝿?wù)數(shù)和最后分配的PID。)
這個(gè)文檔描述了內(nèi)核中procfs文件系統(tǒng)的使用。它以介紹所有和管理文件系統(tǒng)相關(guān)的函數(shù)開始。在函數(shù)介紹后,它還展示了怎么和用戶空間通信,和一些小技巧。在文檔的最后,還給出了一個(gè)完整的例子。
注意/proc/sys中的文件屬于sysctl文件,它們不屬于procfs文件系統(tǒng),被另外一套完全不同的api管理。
seq_file
procfs在處理大文件時(shí)有點(diǎn)笨拙。為了清理procfs文件系統(tǒng)并且使內(nèi)核編程簡(jiǎn)單些,引入了seq_file機(jī)制。seq_file機(jī)制提供了大量簡(jiǎn)單的接口去實(shí)現(xiàn)大內(nèi)核虛擬文件。
seq_file機(jī)制適用于你利用結(jié)構(gòu)序列去創(chuàng)建一個(gè)返回給用戶空間的虛擬文件。要使用seq_file機(jī)制,你必須創(chuàng)建一個(gè)”iterator”對(duì)象,這個(gè)對(duì)象指向這個(gè)序列,并且能逐個(gè)指向這個(gè)序列中的對(duì)象,此外還要能輸出這個(gè)序列中的任一個(gè)對(duì)象。它聽起來復(fù)雜,實(shí)際上,操作過程相當(dāng)簡(jiǎn)單。接下來將用實(shí)際的例子展示到底怎么做。
首先,你必須包含頭文件。接下來,你必須創(chuàng)建迭代器方法:start, next, stop, and show。
start方法通常被首先調(diào)用。這個(gè)方法的函數(shù)原型是:
void *start(struct seq_file *sfile, loff_t *pos);
sfile沒什么作用,通常被忽略。pos參數(shù)是一個(gè)整型,表示從哪個(gè)位置開始讀。關(guān)于位置的定義完全取決于函數(shù)實(shí)現(xiàn);它不一定要是結(jié)果文件中的一個(gè)字節(jié)位置。 由于seq_file機(jī)制通常是利用一個(gè)特定的結(jié)構(gòu)序列實(shí)現(xiàn)的,所以位置通常是一個(gè)指向序列中下一個(gè)結(jié)構(gòu)體的指針。在wing驅(qū)動(dòng)中,每一個(gè)設(shè)備表示序列中的一個(gè)結(jié)構(gòu),所以,入?yún)os代表g_pstWingDevices數(shù)組的索引。因此,在wing驅(qū)動(dòng)中start方法的實(shí)現(xiàn)為:
static void *wing_seq_start(struct seq_file *s, loff_t *pos){ if (*pos >= g_iWingDevicesNum) return NULL; /* No more to read */ return g_pstWingDevices + *pos;}
返回值如果不為NULL,代表一個(gè)可以被迭代器使用的私有數(shù)據(jù)。
next函數(shù)應(yīng)該移動(dòng)迭代器到下一個(gè)位置,如果序列中沒有數(shù)據(jù),返回NULL。這個(gè)方法的函數(shù)原型為:
void *next(struct seq_file *sfile, void *v, loff_t *pos);
這里,參數(shù)v代表上一個(gè)函數(shù)調(diào)用(可能是start函數(shù),或者是next函數(shù))返回的迭代器,, 參數(shù)pos是文件中的當(dāng)前位置。next函數(shù)應(yīng)該改變pos的指向,具體是逐步改變還是跳躍改變?nèi)Q于迭代器的工作機(jī)制。next函數(shù)在wing驅(qū)動(dòng)中的實(shí)現(xiàn)為:
static void* wing_seq_next(struct seq_file *s, void *v, loff_t *pos){ (*pos)++; if (*pos >= g_iWingDevicesNum) return NULL; return g_pstWingDevices + *pos;}
當(dāng)內(nèi)核停止了迭代器的工作,它調(diào)用stop函數(shù)清理現(xiàn)場(chǎng):
void stop(struct seq_file *sfile, void *v);
wing驅(qū)動(dòng)沒有清理工作要做,所以stop函數(shù)為空。
void wing_seq_stop(struct seq_file *sfile, void *v){}
要是seq_file代碼在調(diào)用start和stop時(shí)不執(zhí)行睡眠或是非原子的操作,那么這種機(jī)制將毫無意義。你要保證從start函數(shù)調(diào)用到stop函數(shù)調(diào)用是很短暫的。因此,在開始函數(shù)中獲得一個(gè)信號(hào)量或者自旋鎖是比較安全的做法。要是seq_file其他方法是原子的,整個(gè)調(diào)用鏈必須是原子的。
在這些函數(shù)調(diào)用中,內(nèi)核調(diào)用call函數(shù)向內(nèi)核空間輸出特性的信息。這個(gè)函數(shù)的函數(shù)原型是:
int show(struct seq_file *sfile, void *v);
這個(gè)方法應(yīng)該創(chuàng)建序列中由指示器v指定項(xiàng)的輸出。不能使用printk,而是使用以下這些特定函數(shù):
int seq_printf(struct seq_file *sfile, const char *fmt, ...)?
這個(gè)函數(shù)是seq_file機(jī)制中類似于printf的實(shí)現(xiàn);它使用通常的格式字符串和參數(shù)組成輸出字符串。你必須把show函數(shù)中的seq_file結(jié)構(gòu)體傳給這個(gè)函數(shù)。如果它返回一個(gè)非零的值,表示buffer已經(jīng)填充好,輸出被丟出去了。在大多數(shù)實(shí)現(xiàn)中,都選擇忽略返回值。
int seq_putc(struct seq_file *sfile, char c);
int seq_puts(struct seq_file *sfile, const char *s);?
這兩個(gè)函數(shù)相當(dāng)于用戶層的putc和puts。
int seq_escape(struct seq_file *m, const char *s, const char *esc);?
這個(gè)函數(shù)是 seq_puts 的對(duì)等體, 除了 s 中的任何也在 esc 中出現(xiàn)的字符以八進(jìn)制格式打印. esc 的一個(gè)通用值是”
”, 它使內(nèi)嵌的空格不會(huì)搞亂輸出和可能搞亂 shell 腳本.
int seq_path(struct seq_file *sfile, struct vfsmount *m, struct dentry *dentry, char *esc);?
這個(gè)函數(shù)能夠用來輸出和給定命令項(xiàng)關(guān)聯(lián)的文件名子. 它在設(shè)備驅(qū)動(dòng)中不可能有用;我們是為了完整在此包含它.
wing設(shè)備中的show函數(shù)例子:
static int wing_seq_show(struct seq_file *s, void *v){ ST_Wing_Dev_Type* pDev = (ST_Wing_Dev_Type* ) v; seq_printf(s, " This Device is %i ", pDev->iData); return 0;}
在我的例子中,我將一個(gè)ST_Wing_Dev_Type結(jié)構(gòu)體表示為迭代器。
上面就是完整的迭代器操作,wing驅(qū)動(dòng)必須將它們打包到一起好連接到procfs文件系統(tǒng)。首先要做的就是利用它們組成一個(gè)seq_operations結(jié)構(gòu)體:
static struct seq_operations s_stWingSeqOps = { .start = wing_seq_start, .next = wing_seq_next, .stop = wing_seq_stop, .show = wing_seq_show};
有了這個(gè)結(jié)構(gòu),我們必須創(chuàng)建一個(gè)內(nèi)核能理解的文件實(shí)現(xiàn)。我們不使用前面說過的read_proc方法;在使用seq_file時(shí), 最好在一個(gè)稍低的級(jí)別上連接到procfs。這意味著創(chuàng)建一個(gè)file_operations(和字符設(shè)備一樣的結(jié)構(gòu)),這個(gè)結(jié)構(gòu)實(shí)現(xiàn)了內(nèi)核對(duì)文件的reads和seeks操作。幸運(yùn)的是,這個(gè)操做很簡(jiǎn)單。首先創(chuàng)建一個(gè)把文件和seq_file方法聯(lián)接起來的open方法:
static int wing_proc_open(struct inode *inode, struct file *file){ return seq_open(file, &s_stWingSeqOps);}
調(diào)用seq_open函數(shù)的時(shí)候?qū)⑽募蜕厦娑x的序列操作關(guān)聯(lián)到一起。open是唯一要我們實(shí)現(xiàn)的函數(shù)接口,所以我們的file_operations結(jié)構(gòu)體是:
static struct file_operations s_stWingProcFops = { .owner = THIS_MODULE, .open = wing_proc_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release};
最后我們要在procfs文件系統(tǒng)中創(chuàng)建文件:
proc_create("wingdevices", 0644, NULL, &s_stWingProcFops);
關(guān)鍵結(jié)構(gòu)體
struct proc_dir_entry代表的是/proc目錄下的一個(gè)目錄或者文件,他是procfs文件系統(tǒng)的主要結(jié)構(gòu)體,它的定義在/fs/internal.h中:
/* * This is not completely implemented yet. The idea is to * create an in-memory tree (like the actual /proc filesystem * tree) of these proc_dir_entries, so that we can dynamically * add new files to /proc. * * The "next" pointer creates a linked list of one /proc directory, * while parent/subdir create the directory structure (every * /proc file has a parent, but "subdir" is NULL for all * non-directory entries). */struct proc_dir_entry { unsigned int low_ino; umode_t mode; nlink_t nlink; kuid_t uid; kgid_t gid; loff_t size; const struct inode_operations *proc_iops; const struct file_operations *proc_fops; struct proc_dir_entry *next, *parent, *subdir; void *data; atomic_t count; /* use count */ atomic_t in_use; /* number of callers into module in progress; */ /* negative -> it's going away RSN */ struct completion *pde_unload_completion; struct list_head pde_openers; /* who did ->open, but not ->release */ spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */ u8 namelen; char name[];};
主要接口
procfs應(yīng)該包含的頭文件。
在3.x內(nèi)核中procfs主要接口有:
proc_symlink
proc_mkdir
proc_mkdir_data
proc_mkdir_mode
proc_create_data
proc_create
proc_set_size
proc_set_user
PDE_DATA
proc_get_parent_data
proc_remove
remove_proc_entry
remove_proc_subtree
proc_mkdir
說明:在/proc下創(chuàng)建目錄
函數(shù)原型:
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent)
參數(shù):
name?
要?jiǎng)?chuàng)建的目錄名稱
parent?
父目錄,如果為NULL,表示直接在/proc下面創(chuàng)建目錄。
proc_mkdir_data
說明:在/proc下創(chuàng)建目錄
函數(shù)原型:
struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, struct proc_dir_entry *parent, void *data)
參數(shù):
name?
要?jiǎng)?chuàng)建的目錄名稱
mode?
指定要?jiǎng)?chuàng)建目錄的權(quán)限
parent?
父目錄,如果為NULL,表示直接在/proc下面創(chuàng)建目錄。
data
proc_create_data
說明:創(chuàng)建proc虛擬文件系統(tǒng)文件?
函數(shù)原型:
struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops, void *data)
參數(shù):
name?
你要?jiǎng)?chuàng)建的文件名。
mode?
為創(chuàng)建的文件指定權(quán)限
parent?
為你要在哪個(gè)文件夾下建立名字為name的文件,如:init_net.proc_net是要在/proc/net/下建立文件。
proc_fops?
為struct file_operations
data?
保存私有數(shù)據(jù)的指針,如不要為NULL。
例子:
////////////////////////test.c////////////////////////////////////////#include #include #include #include #include #include #include #include #include MODULE_LICENSE("GPL"); typedef struct { int data1; int data2; }ST_Data_Info_Type; static ST_Data_Info_Type g_astDataInfo[2]; static int test_proc_show(struct seq_file *m, void *v){ ST_Data_Info_Type* pInfo = (ST_Data_Info_Type*)m->private; if(pInfo != NULL) { seq_printf(m, "%d----%d ", pInfo->data1, pInfo->data2); } return 0;}static int test_proc_open(struct inode *inode, struct file *file) { return single_open(file, test_proc_show, PDE_DATA(inode));} static const struct file_operations dl_file_ops = { .owner = THIS_MODULE, .open = test_proc_open, .read = seq_read, .llseek = seq_lseek, .release = single_release,}; static struct proc_dir_entry *s_pstRootTestDir;void init_mem(void) { /* create /proc/test */ s_pstRootTestDir = proc_mkdir("test", NULL); if (!s_pstRootTestDir) return; g_astDataInfo[0].data1=1; g_astDataInfo[0].data2=2; proc_create_data("proc_test1", 0644, s_pstRootTestDir, &dl_file_ops, &g_astDataInfo[0]); g_astDataInfo[1].data1=3; g_astDataInfo[1].data2=4; proc_create_data("proc_test2", 0644, s_pstRootTestDir, &dl_file_ops, &g_astDataInfo[1]); } static int __init test_module_init(void) { printk("[test]: module init "); init_mem(); return 0; } static void __exit test_module_exit(void) { printk("[test]: module exit "); remove_proc_entry("proc_test1", s_pstRootTestDir); remove_proc_entry("proc_test2", s_pstRootTestDir); remove_proc_entry("test", NULL);} module_init(test_module_init); module_exit(test_module_exit);
proc_create
說明:創(chuàng)建proc虛擬文件系統(tǒng)文件
函數(shù)原型:
struct proc_dir_entry *proc_create(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops)
參數(shù):
name?
你要?jiǎng)?chuàng)建的文件名。
mode?
為創(chuàng)建的文件指定權(quán)限
parent?
為你要在哪個(gè)文件夾下建立名字為name的文件,如:init_net.proc_net是要在/proc/net/下建立文件。
proc_fops?
為struct file_operations
注意:這個(gè)接口和proc_create_data的區(qū)別在于他不能保存私有數(shù)據(jù)指針。
PDE_DATA
獲取proc_create_data傳入的私有數(shù)據(jù)。
proc_symlink
說明:這個(gè)函數(shù)在procfs目錄下創(chuàng)建一個(gè)從name指向dest的符號(hào)鏈接. 它在用戶空間等效為ln -s dest name。
函數(shù)原型:
struct proc_dir_entry *proc_symlink(const char *name, struct proc_dir_entry *parent, const char *dest)
參數(shù):
name?
原始符號(hào)。
parent?
符號(hào)所在的目錄。
dest?
所要?jiǎng)?chuàng)建的符號(hào)鏈接名字。
remove_proc_entry
說明:刪除procfs文件系統(tǒng)中的文件或者目錄。
函數(shù)原型:
void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
參數(shù):
name?
要?jiǎng)h除的文件或者目錄名。
parent?
符號(hào)所在的目錄,如果為NULL,表示在/proc目錄下。
?
評(píng)論