指针变量whom指向常量hello
w@w:~/linux_kernel/param$ cat hello.c
#include
#include
static char *whom = "hello \n";
static int var = 1;
static int hello_init(void)
{
printk("hello_init %s \n",whom);
return 0;
}
static void hello_exit(void)
{
printk("hello_exit %d\n",var);
return;
}
MODULE_LICENSE("GPL");
module_param(whom,charp,0644);
module_param_named(var_out,var,int,0644);
module_init(hello_init);
module_exit(hello_exit);
w@w:~/linux_kernel/param$ cat Makefile
ifneq ($(KERNELRELEASE),)
$(info "2nd")
obj-m:=hello.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
w@w:~/linux_kernel/param$ sudo make
"1st"
make -C /lib/modules/5.4.0-104-generic/build M=/home/w/linux_kernel/param modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-104-generic'
"2nd"
CC [M] /home/w/linux_kernel/param/hello.o
"2nd"
Building modules, stage 2.
MODPOST 1 modules
CC [M] /home/w/linux_kernel/param/hello.mod.o
LD [M] /home/w/linux_kernel/param/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-104-generic'
w@w:~/linux_kernel/param$ sudo insmod hello.ko
w@w:~/linux_kernel/param$ sudo dmesg -c
[13425.220292] helloa: module license 'unspecified' taints kernel.
[13425.220294] Disabling lock debugging due to kernel taint
[13425.220586] hello_init
[13630.617249] hello_init 100
[13630.617250] aaaa: num =100
[14299.890225] hello_exit
[14312.594776] hello_exit
[16236.730997] hello_init hello
w@w:~/linux_kernel/param$ sudo rmmod hello.ko
w@w:~/linux_kernel/param$ dmesg
[16271.042902] hello_exit 1
w@w:~/linux_kernel/param$ sudo insmod hello.ko var_out=5 whom="Jack"
w@w:~/linux_kernel/param$ dmesg
[16271.042902] hello_exit 1
[16475.521375] hello_init Jack
w@w:~/linux_kernel/param$ sudo rmmod hello.ko
w@w:~/linux_kernel/param$ dmesg
[16271.042902] hello_exit 1
[16475.521375] hello_init Jack
[16492.779499] hello_exit 5
每加载一个内核模块,Linux系统就会在/sys/module下面为该模块建立一个文件夹。 hello.ko对应的就是/sys/module下面的hello 文件夹。 卸载模块时,该文件夹同时消失