国产无码免费,人妻口爆,国产V在线,99中文精品7,国产成人无码AA精品一,制度丝袜诱惑av,久久99免费麻辣视频,蜜臀久久99精品久久久久久酒店
        訂閱
        糾錯
        加入自媒體

        如何在Linux內核中操作某個文件?

        2021-03-09 10:51
        一口Linux
        關注

        一、問題描述

        如何在內核中操作某個文件?

        問題

        二、操作函數 

        1. 分析

        在用戶態,讀寫文件可以通過read和write這兩個系統調用來完成(C庫函數實際上是對系統調用的封裝)。但是,在內核態沒有這樣的系統調用,我們又該如何讀寫文件呢?

        閱讀Linux內核源碼,可以知道陷入內核執行的是實際執行的是sys_read和sys_write這兩個函數,但是這兩個函數沒有使用EXPORT_SYMBOL導出,也就是說其他模塊不能使用。

        在fs/open.c中系統調用具體實現如下(內核版本3.14):

        SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)

        if (force_o_largefile())
         flags |= O_LARGEFILE;
        return do_sys_open(AT_FDCWD, filename, flags, mode);

        跟蹤do_sys_open()函數,

        long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)

        struct open_flags op;
        int fd = build_open_flags(flags, mode, &op);
        struct filename *tmp;
        if (fd)
         return fd;
        tmp = getname(filename);
        if (IS_ERR(tmp))
         return PTR_ERR(tmp);
        fd = get_unused_fd_flags(flags);
        if (fd >= 0) {
         struct file *f = do_filp_open(dfd, tmp, &op);
         if (IS_ERR(f)) {
          put_unused_fd(fd);
          fd = PTR_ERR(f);
         } else {
          fsnotify_open(f);
          fd_install(fd, f);
         }

        putname(tmp);
        return fd;

        就會發現它主要使用了do_filp_open()函數該函數在fs/namei.c中,

        struct file *do_filp_open(int dfd, struct filename *pathname,
         const struct open_flags *op)

        struct nameidata nd;
        int flags = op->lookup_flags;
        struct file *filp;
        filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
        if (unlikely(filp == ERR_PTR(-ECHILD)))
         filp = path_openat(dfd, pathname, &nd, op, flags);
        if (unlikely(filp == ERR_PTR(-ESTALE)))
         filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
        return filp;

        該函數最終打開了文件,并返回file類型指針。所以我們只需要找到其他調用了do_filp_open()函數的地方,就可找到我們需要的文件操作函數。

        而在文件fs/open.c中,filp_open函數也是調用了file_open_name函數,


        * filp_open - open file and return file pointer

        * @filename: path to open
        * @flags: open flags as per the open(2) second argument
        * @mode: mode for the new file if O_CREAT is set, else ignored

        * This is the helper to open a file from kernelspace if you really
        * have to.  But in generally you should not do this, so please move
        * along, nothing to see here..

        struct file *filp_open(const char *filename, int flags, umode_t mode)

        struct filename name = {.name = filename};
        return file_open_name(&name, flags, mode);

        EXPORT_SYMBOL(filp_open);

        函數file_open_name調用了do_filp_open,并且接口和sys_open函數極為相似,調用參數也和sys_open一樣,并且使用EXPORT_SYMBOL導出了,所以在內核中可以使用該函數打開文件,功能非常類似于應用層的open。


        * file_open_name - open file and return file pointer

        * @name: struct filename containing path to open
        * @flags: open flags as per the open(2) second argument
        * @mode: mode for the new file if O_CREAT is set, else ignored

        * This is the helper to open a file from kernelspace if you really
        * have to.  But in generally you should not do this, so please move
        * along, nothing to see here..

        struct file *file_open_name(struct filename *name, int flags, umode_t mode)

        struct open_flags op;
        int err = build_open_flags(flags, mode, &op);
        return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);

        2. 所有操作函數

        使用同樣的方法,找出了一組在內核操作文件的函數,如下:

        功能函數原型打開文件struct file *filp_open(const char *filename, int flags, int mode)讀文件ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)寫文件ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)關閉文件int filp_close(struct file *filp, fl_owner_t id)

        這些函數的參數非常類似于應用層文件IO函數,open、read、write、close。

        3. 用戶空間地址

        雖然我們找到了這些函數,但是我們還不能直接使用。

        因為在vfs_read和vfs_write函數中,其參數buf指向的用戶空間的內存地址,如果我們直接使用內核空間的指針,則會返回-EFALUT。

        這是因為使用的緩沖區超過了用戶空間的地址范圍。一般系統調用會要求你使用的緩沖區不能在內核區。這個可以用set_fs()、get_fs()來解決。

        在include/asm/uaccess.h中,有如下定義:

        #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
        #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
        #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
        #define get_ds() (KERNEL_DS)
        #define get_fs() (current->addr_limit)
        #define set_fs(x) (current->addr_limit = (x))

        如果使用,可以按照如下順序執行:

        mm_segment_t fs = get_fs();
        set_fs(KERNEL_FS);
        //vfs_write();
        //vfs_read();
        set_fs(fs);

        詳解:系統調用本來是提供給用戶空間的程序訪問的,所以,對傳遞給它的參數(比如上面的buf),它默認會認為來自用戶空間,在read或write()函數中,為了保護內核空間,一般會用get_fs()得到的值來和USER_DS進行比較,從而防止用戶空間程序“蓄意”破壞內核空間。

        而現在要在內核空間使用系統調用,此時傳遞給read或write()的參數地址就是內核空間的地址了,在USER_DS之上(USER_DS ~ KERNEL_DS),如果不做任何其它處理,在write()函數中,會認為該地址超過了USER_DS范圍,所以會認為是用戶空間的“蓄意破壞”,從而不允許進一步的執行。

        為了解決這個問題, set_fs(KERNEL_DS),將其能訪問的空間限制擴大到KERNEL_DS,這樣就可以在內核順利使用系統調用了!

        在VFS的支持下,用戶態進程讀寫任何類型的文件系統都可以使用read和write這兩個系統調用,但是在linux內核中沒有這樣的系統調用我們如何操作文件呢?

        我們知道read和write在進入內核態之后,實際執行的是sys_read和sys_write,但是查看內核源代碼,發現這些操作文件的函數都沒有導出(使用EXPORT_SYMBOL導出),也就是說在內核模塊中是不能使用的,那如何是好?

        通過查看sys_open的源碼我們發現,其主要使用了do_filp_open()函數,該函數在fs/namei.c中,而在改文件中,filp_open函數也是間接調用了do_filp_open函數,并且接口和sys_open函數極為相似,調用參數也和sys_open一樣,并且使用EXPORT_SYMBOL導出了,所以我們猜想該函數可以打開文件,功能和open一樣。

        三、實例

        Makefile

        ifneq ($(KERNELRELEASE),)
        obj-m:=sysopen.o
        else
        KDIR :=/lib/modules/$(shell uname -r)/build
        PWD  :=$(shell pwd)
        all:
        $(info "1st")
        make -C $(KDIR) M=$(PWD) modules
        clean:
        rm -f *.ko *.o *.mod.o *.symvers *.cmd  *.mod.c *.order
        endif

        sysopen.c

        #include <linux/module.h>
        #include <linux/syscalls.h>
        #include <linux/file.h>
        #include <linux/fcntl.h>
        #include <linux/delay.h>
        #include <linux/slab.h>
        #include <linux/uaccess.h>
        MODULE_LICENSE("GPL");
        MODULE_AUTHOR("yikoulinux");
        void test(void)

        struct file *file = NULL;
        mm_segment_t old_fs;
        loff_t  pos;
        char buf[64]="yikoulinux";
        printk("test()");
        file = filp_open("/home/peng/open/test.txt",O_RDWR|O_APPEND|O_CREAT,0644);
        if(IS_ERR(file)){
         return ;

        old_fs = get_fs();
        set_fs(KERNEL_DS);
        pos = 0;
        vfs_write(file,buf,sizeof(buf),&pos);
        pos =0;
        vfs_read(file, buf, sizeof(buf), &pos);
        printk("buf:%s",buf);

        filp_close(file,NULL);
        set_fs(old_fs);
        return;

        static int hello_init(void)

        printk("hello_init ");
        test();
        return 0;

        static void hello_exit(void)

        printk("hello_exit ");
        return;

        module_init(hello_init);
        module_exit(hello_exit);

        編譯:

        安裝模塊:

        查看操作的文件:

        查看文件內容:

        可見在內核模塊中成功操作了文件。


        聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權或其他問題,請聯系舉報。

        發表評論

        0條評論,0人參與

        請輸入評論內容...

        請輸入評論/評論長度6~500個字

        您提交的評論過于頻繁,請輸入驗證碼繼續

        暫無評論

        暫無評論

          掃碼關注公眾號
          OFweek人工智能網
          獲取更多精彩內容
          文章糾錯
          x
          *文字標題:
          *糾錯內容:
          聯系郵箱:
          *驗 證 碼:

          粵公網安備 44030502002758號

          主站蜘蛛池模板: 拍拍拍免费视频| 亚洲欧美色欲天天| 福利导航在线播放| jizz日本版| 正阳县| 成人亚洲精品一区二区三区嫩花| 99黄色| AV天堂免费观看| 亚洲精品国产第一页第二页_久久精品国产亚洲a片无码_国产精品乱码一区二区三 | 不卡的无码AV| 蒙山县| 久久55| 熟女中文字幕在线| 武穴市| 错那县| 亚洲日本中文字幕| 久久99精品国产| 无码社区| 柞水县| 黄色不卡| 5月丁香,6月综合| 人妻久久久| 国产丝袜AV| 精品va在线观看| 国产无码免费| 亚洲天堂在线播放| 超碰人人插| 夏河县| 国产精品96久久久久久| A片A级毛片| 武夷山市| 广南县| 扶风县| 欧美音影成人在线一级片| 亚洲国内自拍| 国产精品A片| 91探花视频在线观看| 伊人久久精品| 国产?成人?无码| 开江县| 江陵县|