- 論壇徽章:
- 3
|
Proc file system
dreamice.jiang@gmail.com
1、重要的數(shù)據(jù)結(jié)構(gòu):
struct proc_dir_entry {
unsigned int low_ino;
unsigned short namelen;
const char *name;
mode_t mode;
nlink_t nlink;
uid_t uid;
gid_t gid;
loff_t size;
const struct inode_operations *proc_iops;
/*
* NULL ->proc_fops means "PDE is going away RSN" or
* "PDE is just created". In either case, e.g. ->read_proc won't be
* called because it's too late or too early, respectively.
*
* If you're allocating ->proc_fops dynamically, save a pointer
* somewhere.
*/
const struct file_operations *proc_fops;
get_info_t *get_info;
struct module *owner;
struct proc_dir_entry *next, *parent, *subdir;
void *data;
read_proc_t *read_proc;
write_proc_t *write_proc;
atomic_t count; /* use count */
int pde_users; /* number of callers into module in progress */
spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
struct completion *pde_unload_completion;
shadow_proc_t *shadow_proc;
};
2、創(chuàng)建函數(shù):
struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
struct proc_dir_entry *parent);
name: 要?jiǎng)?chuàng)建的文件名稱;
mode: 該文件的保護(hù)掩碼;
parent: 確定文件所在目錄,如果置NULL,則位置為/proc下。
3、讀proc:
int read_proc(char *page, char **start, off_t off,
int count, int *eof, void *data);
page:指示用來(lái)寫(xiě)入數(shù)據(jù)的緩沖區(qū);
off和count:與read函數(shù)對(duì)應(yīng)的參數(shù)相同;
start和eof:用于讀取大于1個(gè)page數(shù)據(jù)時(shí)實(shí)現(xiàn)。
4、寫(xiě)proc:
int write_proc(struct file *file, const char __user *buffer,
unsigned long count, void *data);
filp 參數(shù)實(shí)際上是一個(gè)打開(kāi)文件結(jié)構(gòu)(可以忽略這個(gè)參數(shù))。Buffer 參數(shù)是用戶空間要寫(xiě)入的數(shù)據(jù)。緩沖區(qū)地址實(shí)際上是一個(gè)用戶空間的緩沖區(qū),不能直接讀取它。len 參數(shù)定義了在 buffer 中有多少數(shù)據(jù)要被寫(xiě)入。data 參數(shù)是一個(gè)指向私有數(shù)據(jù)的指針。
5、實(shí)現(xiàn)一個(gè)proc文件
(1)調(diào)用create_proc_entry創(chuàng)建一個(gè)struct proc_dir_entry,作為一個(gè)全局量。
(2)對(duì)創(chuàng)建的struct proc_dir_entry進(jìn)行賦值:read_proc,mode,owner,size,write_proc等等。
示例:
proc_hello.c
#include <linux/module.h> /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#define procfs_name "helloworld"
struct proc_dir_entry *Our_Proc_File;
int
procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);
/*
* We give all of our information in one go, so if the
* user asks us if we have more information the
* answer should always be no.
*
* This is important because the standard read
* function from the library would continue to issue
* the read system call until the kernel replies
* that it has no more information, or until its
* buffer is filled.
*/
if (offset > 0) {
printk(KERN_INFO "offset > 0\n");
/* we have finished to read, return 0 */
ret = 0;
} else {
/* fill the buffer, return the buffer size */
printk(KERN_INFO "offset <= 0\n");
ret = sprintf(buffer, "HelloWorld!\n");
}
return ret;
}
int init_module()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, &proc_root);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk(KERN_INFO "/proc/%s created\n", procfs_name);
return 0; /* everything is ok */
}
void cleanup_module()
{
remove_proc_entry(procfs_name, &proc_root);
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
Makefile:
obj-m := proc_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
本示例只實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的讀proc操作。
編譯后,執(zhí)行:
# insmod proc_hello.ko
# dmesg
/proc/helloworld created
# cat /proc/ helloworld
HelloWorld!
# dmesg
procfile_read (/proc/helloworld) called
offset <= 0
procfile_read (/proc/helloworld) called
offset > 0
procfile_read (/proc/helloworld) called
offset > 0
# rmmod proc_hello
/proc/helloworld
為什么在調(diào)用cat /proc/ helloworld后,會(huì)看到procfile_read被執(zhí)行了三次,且只有第一次讀到了數(shù)據(jù)?
因?yàn)橥ǔat,dd等,都是通過(guò)標(biāo)準(zhǔn)庫(kù)函數(shù)來(lái)實(shí)現(xiàn)的,標(biāo)準(zhǔn)庫(kù)函數(shù)往往會(huì)多次調(diào)用系統(tǒng)調(diào)用來(lái)讀寫(xiě)數(shù)據(jù),所以我們看到調(diào)用了3次。具體cat以及dd等調(diào)用一次,讀取或?qū)憯?shù)據(jù)大小,這個(gè)需要具體查證一下。
我寫(xiě)了一個(gè)test程序,來(lái)驗(yàn)證這個(gè)多次讀操作不是在系統(tǒng)調(diào)用里面實(shí)現(xiàn)的:
test.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PROC_FILE "/proc/helloworld"
int main()
{
char buf[50];
int fd = open(PROC_FILE, O_RDONLY);
if (fd == -1) {
printf("open err\n");
return -1;
}
if (read(fd, buf, sizeof("HelloWorld")) > 0) {
printf("read: %s\n", buf);
}
close(fd);
}
編譯:
# gcc test.c –o test
加載模塊后,執(zhí)行
# ./test
然后,執(zhí)行:
# dmesg
procfile_read (/proc/helloworld) called
offset <= 0
確實(shí)只執(zhí)行了一次,這說(shuō)明多次調(diào)用procfile_read是在系統(tǒng)之上做的。
6、注意點(diǎn)
(1)切記不能訪問(wèn)已經(jīng)卸載的一個(gè)proc文件,否則會(huì)導(dǎo)致kernel panic;
(2)注意不要?jiǎng)h除正在調(diào)用的問(wèn)proc文件。因?yàn)槲募娜肟陧?xiàng)不存在關(guān)聯(lián)的作者,文件的調(diào)用也并沒(méi)有作用到模塊的引用計(jì)數(shù)上;
(3)勿注冊(cè)兩個(gè)同名的proc文件,這樣將導(dǎo)致入口項(xiàng)無(wú)法區(qū)分。
以上這些都是proc文件的缺點(diǎn),也是使用時(shí)必須注意的地方。所以,現(xiàn)在推薦使用seq_file 和sys_fs。 |
-
-
proc.pdf
2008-11-12 15:44 上傳
點(diǎn)擊文件名下載附件
73.55 KB, 下載次數(shù): 1456
評(píng)分
-
查看全部評(píng)分
|