在 Linux 下使用者空間與核心空間資料交換的方式,第 2 部分:__Linux

來源:互聯網
上載者:User
http://www.ibm.com/developerworks/cn/linux/l-kerns-usrs/
http://www.ibm.com/developerworks/cn/linux/l-kerns-usrs2/ 在 Linux 下使用者空間與核心空間資料交換的方式,第 2 部分: procfs、seq_file、debugfs和relayfs

本系列文章包括兩篇,它們文詳細地地介紹了Linux系統下使用者空間與核心空間資料交換的九種方式,包括核心啟動參數、模組參數與sysfs、sysctl、系統調用、netlink、procfs、seq_file、debugfs和relayfs,並給出具體的例子協助讀者掌握這些技術的使用。
本文是該系列文章的第二篇,它介紹了procfs、seq_file、debugfs和relayfs,並結合給出的例子程式詳細地說明了它們如何使用。

1 評論

燚 楊 (yang.y.yi@gmail.com), 電腦科學碩士

2006 年 2 月 16 日 內容

在 IBM Bluemix 雲平台上開發並部署您的下一個應用。

開始您的試用 一、procfs

procfs是比較老的一種使用者態與核心態的資料交換方式,核心的很多資料都是通過這種方式出口給使用者的,核心的很多參數也是通過這種方式來讓使用者方便設定的。除了sysctl出口到/proc下的參數,procfs提供的大部分核心參數是唯讀。實際上,很多應用嚴重地依賴於procfs,因此它幾乎是必不可少的組件。前面部分的幾個例子實際上已經使用它來出口核心資料,但是並沒有講解如何使用,本節將講解如何使用procfs。

Procfs提供了如下API:

struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,                                          struct proc_dir_entry *parent)

該函數用於建立一個正常的proc條目,參數name給出要建立的proc條目的名稱,參數mode給出了建立的該proc條目的存取權限,參數parent指定建立的proc條目所在的目錄。如果要在/proc下建立proc條目,parent應當為NULL。否則它應當為proc_mkdir返回的struct proc_dir_entry結構的指標。

extern void remove_proc_entry(const char *name, struct proc_dir_entry *parent)

該函數用於刪除上面函數建立的proc條目,參數name給出要刪除的proc條目的名稱,參數parent指定建立的proc條目所在的目錄。

struct proc_dir_entry *proc_mkdir(const char * name, struct proc_dir_entry *parent)

該函數用於建立一個proc目錄,參數name指定要建立的proc目錄的名稱,參數parent為該proc目錄所在的目錄。

extern struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,                        struct proc_dir_entry *parent);struct proc_dir_entry *proc_symlink(const char * name,                struct proc_dir_entry * parent, const char * dest)

該函數用於建立一個proc條目的符號連結,參數name給出要建立的符號連結proc條目的名稱,參數parent指定符號串連所在的目錄,參數dest指定連結到的proc條目名稱。

struct proc_dir_entry *create_proc_read_entry(const char *name,        mode_t mode, struct proc_dir_entry *base,        read_proc_t *read_proc, void * data)

該函數用於建立一個規則的唯讀proc條目,參數name給出要建立的proc條目的名稱,參數mode給出了建立的該proc條目的存取權限,參數base指定建立的proc條目所在的目錄,參數read_proc給出讀去該proc條目的操作函數,參數data為該proc條目的專用資料,它將儲存在該proc條目對應的struct file結構的private_data欄位中。

struct proc_dir_entry *create_proc_info_entry(const char *name,        mode_t mode, struct proc_dir_entry *base, get_info_t *get_info)

該函數用於建立一個info型的proc條目,參數name給出要建立的proc條目的名稱,參數mode給出了建立的該proc條目的存取權限,參數base指定建立的proc條目所在的目錄,參數get_info指定該proc條目的get_info操作函數。實際上get_info等同於read_proc,如果proc條目沒有定義個read_proc,對該proc條目的read操作將使用get_info取代,因此它在功能上非常類似於函數create_proc_read_entry。

struct proc_dir_entry *proc_net_create(const char *name,        mode_t mode, get_info_t *get_info)

該函數用於在/proc/net目錄下建立一個proc條目,參數name給出要建立的proc條目的名稱,參數mode給出了建立的該proc條目的存取權限,參數get_info指定該proc條目的get_info操作函數。

struct proc_dir_entry *proc_net_fops_create(const char *name,        mode_t mode, struct file_operations *fops)

該函數也用於在/proc/net下建立proc條目,但是它也同時指定了對該proc條目的檔案操作函數。

void proc_net_remove(const char *name)

該函數用於刪除前面兩個函數在/proc/net目錄下建立的proc條目。參數name指定要刪除的proc名稱。

除了這些函數,值得一提的是結構struct proc_dir_entry,為了建立一了可寫的proc條目並指定該proc條目的寫操作函數,必須設定上面的這些建立proc條目的函數返回的指標指向的struct proc_dir_entry結構的write_proc欄位,並指定該proc條目的存取權限有寫入權限。

為了使用這些介面函數以及結構struct proc_dir_entry,使用者必須在模組中包含標頭檔linux/proc_fs.h。

在原始碼包中給出了procfs樣本程式procfs_exam.c,它定義了三個proc檔案條目和一個proc目錄條目,讀者在插入該模組後應當看到如下結構:

$ ls /proc/myproctestaintastringbigprocfile$

讀者可以通過cat和echo等檔案操作函數來查看和設定這些proc檔案。特別需要指出,bigprocfile是一個大檔案(超過一個記憶體頁),對於這種大檔案,procfs有一些限制,因為它提供的緩衝,只有一個頁,因此必須特別小心,並對超過頁的部分做特別的考慮,處理起來比較複雜並且很容易出錯,所有procfs並不適合於大資料量的輸入輸出,後面一節seq_file就是因為這一缺陷而設計的,當然seq_file依賴於procfs的一些基礎功能。

回頁首 二、seq_file

一般地,核心通過在procfs檔案系統下建立檔案來向使用者空間提供輸出資訊,使用者空間可以通過任何文本閱讀應用查看該檔案資訊,但是procfs有一個缺陷,如果輸出內容大於1個記憶體頁,需要多次讀,因此處理起來很難,另外,如果輸出太大,速度比較慢,有時會出現一些意想不到的情況,Alexander Viro實現了一套新的功能,使得核心輸出大檔案資訊更容易,該功能出現在2.4.15(包括2.4.15)以後的所有2.4核心以及2.6核心中,尤其是在2.6核心中,已經大量地使用了該功能。

要想使用seq_file功能,開發人員需要包含標頭檔linux/seq_file.h,並定義與設定一個seq_operations結構(類似於file_operations結構):

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函數用於指定seq_file檔案的讀開始位置,返回實際讀開始位置,如果指定的位置超過檔案末尾,應當返回NULL,start函數可以有一個特殊的返回SEQ_START_TOKEN,它用於讓show函數輸出檔案頭,但這隻能在pos為0時使用,next函數用於把seq_file檔案的當前讀位置移動到下一個讀位置,返回實際的下一個讀位置,如果已經到達檔案末尾,返回NULL,stop函數用於在讀完seq_file檔案後調用,它類似於檔案操作close,用於做一些必要的清理,如釋放記憶體等,show函數用于格式化輸出,如果成功返回0,否則返回出錯碼。

Seq_file也定義了一些輔助函數用于格式化輸出:

int seq_putc(struct seq_file *m, char c);

函數seq_putc用於把一個字元輸出到seq_file檔案。

int seq_puts(struct seq_file *m, const char *s);

函數seq_puts則用於把一個字串輸出到seq_file檔案。

int seq_escape(struct seq_file *, const char *, const char *);

函數seq_escape類似於seq_puts,只是,它將把第一個字串參數中出現的包含在第二個字串參數中的字元按照八進位形式輸出,也即對這些字元進行轉義處理。

int seq_printf(struct seq_file *, const char *, ...)        __attribute__ ((format (printf,2,3)));

函數seq_printf是最常用的輸出函數,它用於把給定參數按照給定的格式輸出到seq_file檔案。

int seq_path(struct seq_file *, struct vfsmount *, struct dentry *, char *);

函數seq_path則用於輸出檔案名,字串參數提供需要轉義的檔案名稱字元,它主要供檔案系統使用。

在定義了結構struct seq_operations之後,使用者還需要把開啟seq_file檔案的open函數,以便該結構與對應於seq_file檔案的struct file結構關聯起來,例如,struct seq_operations定義為:

struct seq_operations exam_seq_ops = {.start = exam_seq_start,   .stop = exam_seq_stop,   .next = exam_seq_next,   .show = exam_seq_show};

那麼,open函數應該如下定義:

static int exam_seq_open(struct inode *inode, struct file *file){        return seq_open(file, &exam_seq_ops);};

注意,函數seq_open是seq_file提供的函數,它用於把struct seq_operations結構與seq_file檔案關聯起來。 最後,使用者需要如下設定struct file_operations結構:

struct file_operations exam_seq_file_ops = {        .owner   = THIS_MODULE,        .open    = exm_seq_open,        .read    = seq_read,        .llseek  = seq_lseek,        .release = seq_release};

注意,使用者僅需要設定open函數,其它的都是seq_file提供的函數。

然後,使用者建立一個/proc檔案並把它的檔案操作設定為exam_seq_file_ops即可:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.