Linux 內核模塊編程(一)


實現一個模塊的編譯、加載、卸載;實現模塊內兩個文件的調用;實現兩個模塊間函數調用。

 

編譯部署spl

cd /lcf/spl
./autogen.sh
./configure --enable-debug --disable-silent-rules
make -j8
make install

 

一、模塊的編譯、加載、卸載

1、#新建文件夾,mkdir filename

#修改../module中Makefile.in,增加子目錄subdir-m += spltest;


2、在../module/spltest中用touch filename創建源文件(如helloworld.c)和Makefile.in,主要修改以下三個地方:

src = @abs_top_srcdir@/module/spltest //模塊目錄名

MODULE := spltest //模塊名(生成.ko文件)

$(MODULE)-objs += helloworld.o //源文件名

 

Makefile.in

# makefile.in for spltest kernel module

src = @abs_top_srcdir@/module/spltest
obj = @abs_builddir@

MODULE := spltest
EXTRA_CFLAGS = $(SPL_MODULE_CFLAGS) @KERNELCPPFLAGS@

# Solaris Porting LAyer Tests
obj-$(CONFIG_SPL) := $(MODULE).o

$(MODULE)-objs += modA.o

 

3、修改工程目錄下configure.ac文件,AC_CONFIG_FILES中增加module/spltest/Makefile;


4、重新編譯部署spl,執行autogen.sh、configure、make操作。

      cd /lcf/spl
      ./autogen.sh

      ./configure --enable-debug --disable-silent-rules

      make -j8

      make install

 

5、安裝、卸載模塊,打印信息

      insmod spltest.ko     //安裝模塊
      rmmod |grep spltest.ko      //卸載模塊
      dmesg       //打印message

 

二、實現模塊內兩個文件的調用。

 1、用上面一的方法新建模塊splinner,在../module/splinner中用touch filename創建源文件hello.c和hello1.c,其中hello1.c調用hello.c中的函數。

hello.c

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

int func(void)
{
printk("this is a function in hello.c \n");
return 0;
}

hello1.c

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

extern int func(void);

static int __init hello1_init(void)
{
printk("hello1 ,init \n");
func();
return 0;
}

static void __exit hello1_exit(void)
{
printk("hello1 ,exit \n");
}

module_init(hello1_init);
module_exit(hello1_exit);

 

2、加載模塊,打印信息。

      insmod splinner.ko      //安裝模塊
      rmmod |grep splinner.ko      //卸載模塊
      dmesg       //打印message

 

三、實現兩個模塊間函數調用。

1、用上面一的方法新建兩個模塊spltest和spltest2,兩個模塊中的文件分別為modA.c和modB.c,其中modB.c調用moA.c中的函數

modA.c

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

int func1(void)
{
printk("this is function1 \n");
return 0;
}

EXPORT_SYMBOL(func1);

static int __init hello_init(void)
{
printk("Module A ,init \n");
return 0;
}

static void __exit hello_exit(void)
{
printk("Module A ,exit \n");
}

module_init(hello_init);
module_exit(hello_exit);

modB.c

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

int func2(void)
{
extern int func1(void);
func1();
return 0;
}

static int __init hello_init(void)
{
printk("module B ,init \n");
func2();
return 0;
}

static void __exit hello_exit(void)
{
printk("module B ,exit \n");
}

module_init(hello_init);
module_exit(hello_exit);

 

2、加載模塊時遇到Unknown symbol in module:

     mesg | tail 查看未知模塊;

     modinfo *.ko | grep depend 查找模塊的依賴;

     modprobe xxx安裝依賴的模塊;

 

3、加載模塊,打印信息。

      insmod spltest2.ko      //安裝模塊
      rmmod |grep spltest2.ko      //卸載模塊
      dmesg       //打印message


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM