上篇介紹了Linux驅(qū)動(dòng)中sysfs接口的創(chuàng)建,今天介紹procfs接口的創(chuàng)建。
【資料圖】
procfs
:可實(shí)現(xiàn)類似cat /proc/cpuinfo
的操作
實(shí)現(xiàn)效果:
例如, 在/proc
下創(chuàng)建一個(gè)clk節(jié)點(diǎn),通過(guò)cat /proc/clk
可查看內(nèi)容:
代碼實(shí)現(xiàn):
系統(tǒng) | 內(nèi)核版本 |
---|---|
Linux | 4.9.88 |
在驅(qū)動(dòng)中添加以下代碼:
#include #include #include #include #include struct proc_dir_entry *my_proc_entry;static int proc_clk_show(struct seq_file *m, void *v){ //cat顯示的內(nèi)容 seq_printf(m, "pll0: %u Mhz\\n" "pll1: %u Mhz\\n" "pll2: %u Mhz\\n", 100, 200, 300); return 0;}static int clk_info_open(struct inode *inode, struct file *filp){ return single_open(filp, proc_clk_show, NULL);}static struct file_operations myops = { .owner = THIS_MODULE, .open = clk_info_open, .read= seq_read, .llseek = seq_lseek, .release = seq_release,};static int __init my_module_init(void){ //注冊(cè)proc接口 my_proc_entry = proc_create("clk", 0644, NULL, &myops); return 0;}static void __exit my_module_exit(void){ //注銷proc接口 proc_remove(my_proc_entry);}module_init(my_module_init);module_exit(my_module_exit);MODULE_LICENSE("GPL");
procfs接口的創(chuàng)建,主要是實(shí)現(xiàn)struct file_operations
結(jié)構(gòu)體,然后通過(guò)proc_create
函數(shù)進(jìn)行注冊(cè),通過(guò)proc_remove
函數(shù)進(jìn)行注銷。
procfs通常是用來(lái)獲取CPU、內(nèi)存、進(jìn)程等各種信息,例如cat /proc/cpuinfo
、cat /proc/meminfo
,所以我們只需要實(shí)現(xiàn).open成員函數(shù)。 當(dāng)使用cat
命令查看/proc
下的信息時(shí),會(huì)調(diào)用到.open
對(duì)應(yīng)的實(shí)現(xiàn)函數(shù)。
這里我們使用了seq_file
接口,需要記住的是, procfs通常會(huì)和seq_file接口一起使用。 seq_file是一個(gè)序列文件接口, 當(dāng)我們創(chuàng)建的proc數(shù)據(jù)內(nèi)容由一系列數(shù)據(jù)順序組合而成或者是比較大的proc文件系統(tǒng)時(shí),都建議使用seq_file接口,例如cat /proc/meminfo
就會(huì)顯示很多內(nèi)容。
seq_file接口主要就是解決proc接口編程存在的問(wèn)題, 推薦在proc接口編程時(shí)使用seq_file接口,另外.read、.llseek、.release成員函數(shù)也可以直接用seq_read
、seq_lseek
和seq_release
。
注意,在較新版本的內(nèi)核中,procfs
的函數(shù)接口有所變化。
系統(tǒng) | 內(nèi)核版本 |
---|---|
Linux | 5.10.111 |
在驅(qū)動(dòng)中添加以下代碼:
#include #include #include #include #include struct proc_dir_entry *my_proc_entry;static int proc_clk_show(struct seq_file *m, void *v){ seq_printf(m, "pll0: %lu Mhz\\n" "pll1: %lu Mhz\\n" "pll2: %lu Mhz\\n", 100, 200, 300); return 0;}static int clk_info_open(struct inode *inode, struct file *filp){ return single_open(filp, proc_clk_show, NULL);}static const struct proc_ops clk_stat_proc_fops = { .proc_open = clk_info_open, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_release = seq_release,};static int __init my_module_init(void){ my_proc_entry = proc_create("clk", 0, NULL, &clk_stat_proc_fops); return 0;}static void __exit my_module_exit(void){ proc_remove(my_proc_entry);}module_init(my_module_init);module_exit(my_module_exit);MODULE_LICENSE("GPL");
新的proc
接口中,將原來(lái)的struct file_operations
換成了struct proc_ops
,其中成員函數(shù)也添加了對(duì)應(yīng)的前綴proc
,但本質(zhì)還是一樣的,只是換了名字,更加規(guī)范了一些。
標(biāo)簽: