亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
123456下一頁(yè)
最近訪問(wèn)板塊 發(fā)新帖
查看: 172433 | 回復(fù): 56
打印 上一主題 下一主題

proc文件系統(tǒng)剖析 [復(fù)制鏈接]

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-11-12 15:44 |只看該作者 |倒序?yàn)g覽
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

73.55 KB, 下載次數(shù): 1456

評(píng)分

參與人數(shù) 1可用積分 +15 收起 理由
Godbach + 15 我很贊同

查看全部評(píng)分

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [報(bào)告]
發(fā)表于 2008-11-12 15:46 |只看該作者
具體proc的介紹我就不說(shuō)了,到處都可以查到。
上面我也說(shuō)到了與歌德巴赫兄討論的系統(tǒng)調(diào)用與標(biāo)準(zhǔn)庫(kù)函數(shù)實(shí)現(xiàn)之間的差異。

論壇徽章:
36
IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
3 [報(bào)告]
發(fā)表于 2008-11-12 15:55 |只看該作者
剖析的很好啊。現(xiàn)在很多時(shí)候做模塊開(kāi)發(fā)還是離不開(kāi)proc文件的。

論壇徽章:
36
IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
4 [報(bào)告]
發(fā)表于 2008-11-12 15:57 |只看該作者
dreamice兄什么時(shí)候也研究一下sysfs,寫(xiě)個(gè)例程讓大家學(xué)習(xí)一下啊。LDD3中比較推薦使用這個(gè)。

論壇徽章:
36
IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
5 [報(bào)告]
發(fā)表于 2008-11-12 16:01 |只看該作者
dreamice兄是不是以前學(xué)習(xí)的過(guò)程中都做了很多總結(jié),寫(xiě)成文章了啊。最近一下子這么多精華的文章,真是讓兄弟應(yīng)接不暇啊。

贊一個(gè)!

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2008-11-12 16:08 |只看該作者
awesome

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
7 [報(bào)告]
發(fā)表于 2008-11-12 16:13 |只看該作者
原帖由 Godbach 于 2008-11-12 16:01 發(fā)表
dreamice兄是不是以前學(xué)習(xí)的過(guò)程中都做了很多總結(jié),寫(xiě)成文章了啊。最近一下子這么多精華的文章,真是讓兄弟應(yīng)接不暇啊。

贊一個(gè)!


沒(méi)有,今天剛寫(xiě)好就拿出來(lái)分享了,我這個(gè)人是藏不住東西的,呵呵。
爭(zhēng)取明天分析個(gè)seq_file,sysfs稍后再說(shuō),呵呵。多謝支持。

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
8 [報(bào)告]
發(fā)表于 2008-11-12 16:17 |只看該作者
呵呵,你可以給我加分啊,權(quán)限真大,哈哈

論壇徽章:
36
IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-10 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-16 06:20:0015-16賽季CBA聯(lián)賽之廣東
日期:2016-04-16 19:59:32IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-19 06:20:00每日論壇發(fā)貼之星
日期:2016-04-19 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-25 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-06 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-08 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-13 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-28 06:20:00每日論壇發(fā)貼之星
日期:2016-05-28 06:20:00
9 [報(bào)告]
發(fā)表于 2008-11-12 17:32 |只看該作者
原帖由 dreamice 于 2008-11-12 16:17 發(fā)表
呵呵,你可以給我加分啊,權(quán)限真大,哈哈


呵呵,好像是積分達(dá)到一定程度就可以給被人加分減分。我頭一次使用,呵呵。

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
10 [報(bào)告]
發(fā)表于 2008-11-12 17:50 |只看該作者
原帖由 Godbach 于 2008-11-12 17:32 發(fā)表


呵呵,好像是積分達(dá)到一定程度就可以給被人加分減分。我頭一次使用,呵呵。




你的處女分都給我了啊
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP