溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

linux中引入模塊機(jī)制的好處是什么

發(fā)布時(shí)間:2023-04-19 10:59:43 來(lái)源:億速云 閱讀:139 作者:iii 欄目:建站服務(wù)器

本文小編為大家詳細(xì)介紹“l(fā)inux中引入模塊機(jī)制的好處是什么”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“l(fā)inux中引入模塊機(jī)制的好處是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

linux中引入模塊機(jī)制的好處:1、應(yīng)用程序在退出時(shí),可以不管資源的釋放或者其他的清除工作,但是模塊的退出函數(shù)卻必須仔細(xì)此撤銷(xiāo)初始化函數(shù)所作的一切;2、該機(jī)制有助于縮短模塊的開(kāi)發(fā)周期,即注冊(cè)和卸載都很靈活方便。

Linux中引入模塊機(jī)制有什么好處?

首先,模塊是預(yù)先注冊(cè)自己以便服務(wù)于將來(lái)的某個(gè)請(qǐng)求,然后他的初始化函數(shù)就立即結(jié)束。換句話說(shuō),模塊初始化函數(shù)的任務(wù)就是為以后調(diào)用函數(shù)預(yù)先作準(zhǔn)備。

好處:

  • 1) 應(yīng)用程序在退出時(shí),可以不管資源的釋放或者其他的清除工作,但是模塊的退出函數(shù)卻必須仔細(xì)此撤銷(xiāo)初始化函數(shù)所作的一切。

  • 2) 該機(jī)制有助于縮短模塊的開(kāi)發(fā)周期。即:注冊(cè)和卸載都很靈活方便。

Linux模塊機(jī)制淺析

Linux允許用戶通過(guò)插入模塊,實(shí)現(xiàn)干預(yù)內(nèi)核的目的。一直以來(lái),對(duì)linux的模塊機(jī)制都不夠清晰,因此本文對(duì)內(nèi)核模塊的加載機(jī)制進(jìn)行簡(jiǎn)單地分析。

模塊的Hello World!

我們通過(guò)創(chuàng)建一個(gè)簡(jiǎn)單的模塊進(jìn)行測(cè)試。首先是源文件main.c和Makefile。

florian@florian-pc:~/module$ cat main.c

#include<linux/module.h>
#include<linux/init.h>

static int __init init(void)
{
   printk("Hi module!\n");
   return 0;
}

static void __exit exit(void)
{
   printk("Bye module!\n");
}

module_init(init);
module_exit(exit);

其中init為模塊入口函數(shù),在模塊加載時(shí)被調(diào)用執(zhí)行,exit為模塊出口函數(shù),在模塊卸載被調(diào)用執(zhí)行。

florian@florian-pc:~/module$ cat Makefile

obj-m += main.o
#generate the path
CURRENT_PATH:=$(shell pwd)
#the current kernel version number
LINUX_KERNEL:=$(shell uname -r)
#the absolute path
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
#complie object
all:
   make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
#clean
clean:
   make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean

其中,obj-m指定了目標(biāo)文件的名稱(chēng),文件名需要和源文件名相同(擴(kuò)展名除外),以便于make自動(dòng)推導(dǎo)。

然后使用make命令編譯模塊,得到模塊文件main.ko。

florian@florian-pc:~/module$ make

make -C /usr/src/linux-headers-2.6.35-22-generic M=/home/florian/module modules
make[1]: 正在進(jìn)入目錄 `/usr/src/linux-headers-2.6.35-22-generic'
 Building modules, stage 2.
 MODPOST 1 modules
make[1]:正在離開(kāi)目錄 `/usr/src/linux-headers-2.6.35-22-generic'

使用insmod和rmmod命令對(duì)模塊進(jìn)行加載和卸載操作,并使用dmesg打印內(nèi)核日志。

florian@florian-pc:~/module$ sudo insmod main.ko;dmesg | tail -1
[31077.810049] Hi module!
florian@florian-pc:~/module$ sudo rmmod main.ko;dmesg | tail -1
[31078.960442] Bye module!

通過(guò)內(nèi)核日志信息,可以看出模塊的入口函數(shù)和出口函數(shù)都被正確調(diào)用執(zhí)行。

模塊文件

使用readelf命令查看一下模塊文件main.ko的信息。

florian@florian-pc:~/module$ readelf -h main.ko

ELF Header:
 Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
 Class:                             ELF32
 Data:                              2's complement, little endian
 Version:                           1 (current)
 OS/ABI:                            UNIX - System V
 ABI Version:                       0
 Type:                              REL (Relocatable file)
 Machine:                           Intel 80386
 Version:                           0x1
 Entry point address:               0x0
 Start of program headers:          0 (bytes into file)
 Start of section headers:          1120 (bytes into file)
 Flags:                             0x0
 Size of this header:               52 (bytes)
 Size of program headers:           0 (bytes)
 Number of program headers:         0
 Size of section headers:           40 (bytes)
 Number of section headers:         19
 Section header string table index: 16

我們發(fā)現(xiàn)main.ko的文件類(lèi)型為可重定位目標(biāo)文件,這和一般的目標(biāo)文件格式?jīng)]有任何區(qū)別。我們知道,目標(biāo)文件是不能直接執(zhí)行的,它需要經(jīng)過(guò)鏈接器的地址空間分配、符號(hào)解析和重定位的過(guò)程,轉(zhuǎn)化為可執(zhí)行文件才能執(zhí)行。

那么,內(nèi)核將main.ko加載后,是否對(duì)其進(jìn)行了鏈接呢?

模塊數(shù)據(jù)結(jié)構(gòu)

首先,我們了解一下模塊的內(nèi)核數(shù)據(jù)結(jié)構(gòu)。

linux3.5.2/kernel/module.h:220

struct module
{
   ……
   /* Startup function. */
   int (*init)(void);
   ……
   /* Destruction function. */
   void (*exit)(void);
   ……
};

模塊數(shù)據(jù)結(jié)構(gòu)的init和exit函數(shù)指針記錄了我們定義的模塊入口函數(shù)和出口函數(shù)。

模塊加載

模塊加載由內(nèi)核的系統(tǒng)調(diào)用init_module完成。

linux3.5.2/kernel/module.c:3009

/* This is where the real work happens */
SYSCALL_DEFINE3(init_module, void __user *, umod,
      unsigned long, len, const char __user *, uargs)
{
   struct module *mod;
   int ret = 0;
   ……
   /* Do all the hard work */
   mod = load_module(umod, len, uargs);//模塊加載
   ……
   /* Start the module */
   if (mod->init != NULL)
      ret = do_one_initcall(mod->init);//模塊init函數(shù)調(diào)用
   ……
   return 0;
}

系統(tǒng)調(diào)用init_module由SYSCALL_DEFINE3(init_module...)實(shí)現(xiàn),其中有兩個(gè)關(guān)鍵的函數(shù)調(diào)用。load_module用于模塊加載,do_one_initcall用于回調(diào)模塊的init函數(shù)。

函數(shù)load_module的實(shí)現(xiàn)為。

linux3.5.2/kernel/module.c:2864

/* Allocate and load the module: note that size of section 0 is always
  zero, and we rely on this for optional sections. */
static struct module *load_module(void __user *umod,
               unsigned long len,
               const char __user *uargs)
{
   struct load_info info = { NULL, };
   struct module *mod;
   long err;
   ……
   /* Copy in the blobs from userspace, check they are vaguely sane. */
   err = copy_and_check(&info, umod, len, uargs);//拷貝到內(nèi)核
   if (err)
      return ERR_PTR(err);
   /* Figure out module layout, and allocate all the memory. */
   mod = layout_and_allocate(&info);//地址空間分配
   if (IS_ERR(mod)) {
      err = PTR_ERR(mod);
      goto free_copy;
   }
   ……
   /* Fix up syms, so that st_value is a pointer to location. */
   err = simplify_symbols(mod, &info);//符號(hào)解析
   if (err < 0)
      goto free_modinfo;
   err = apply_relocations(mod, &info);//重定位
   if (err < 0)
      goto free_modinfo;
   ……
}

函數(shù)load_module內(nèi)有四個(gè)關(guān)鍵的函數(shù)調(diào)用。copy_and_check將模塊從用戶空間拷貝到內(nèi)核空間,layout_and_allocate為模塊進(jìn)行地址空間分配,simplify_symbols為模塊進(jìn)行符號(hào)解析,apply_relocations為模塊進(jìn)行重定位。

由此可見(jiàn),模塊加載時(shí),內(nèi)核為模塊文件main.ko進(jìn)行了鏈接的過(guò)程!

至于函數(shù)do_one_initcall的實(shí)現(xiàn)就比較簡(jiǎn)單了。

linux3.5.2/kernel/init.c:673

int __init_or_module do_one_initcall(initcall_t fn)
{
   int count = preempt_count();
   int ret;
   if (initcall_debug)
      ret = do_one_initcall_debug(fn);
   else
      ret = fn();//調(diào)用init module
   ……
   return ret;
}

即調(diào)用了模塊的入口函數(shù)init。

模塊卸載

模塊卸載由內(nèi)核的系統(tǒng)調(diào)用delete_module完成。

linux3.5.2/kernel/module.c:768

SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
       unsigned int, flags)
{
   struct module *mod;
   char name[MODULE_NAME_LEN];
   int ret, forced = 0;
   ……
   /* Final destruction now no one is using it. */
   if (mod->exit != NULL)
      mod->exit();//調(diào)用exit module
   ……
   free_module(mod);//卸載模塊
   ……
}

通過(guò)回調(diào)exit完成模塊的出口函數(shù)功能,最后調(diào)用free_module將模塊卸載。

讀到這里,這篇“l(fā)inux中引入模塊機(jī)制的好處是什么”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI