基本命令
lsmod // 查看当前已加载的模块
insmod testmodule.ko // 加载testmodule模块
rmmod testmodule // 卸载testmodule模块
dmesg // dmesg用来显示内核环缓冲区内容,内核将各种消息存放在这里
简单实例
模块源代码testmodule1.c如下
// 源码来自《操作系统概念-第九版》
// 《operating system concepts, 9th edition》
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* This function is called when the module is loaded. */
int simple init(void)
{
printk(KERN INFO "Loading Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple exit(void)
{
printk(KERN INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module init(simple init);
module exit(simple exit);
MODULE LICENSE("GPL");
MODULE DESCRIPTION("Simple Module");
MODULE AUTHOR("SGG");
testmodule1.c的Makefile如下
ifneq ($(KERNELRELEASE),)
obj-m:=testmodule1.o
else
PWD:=$(shell pwd)
KDIR:=/lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
endif
Linux内核链表操作
《operating system concepts,9th edition》p96习题。
源代码testmodule2.c如下:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/slab.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
// declare a list_head object by using LIST_HEAD macro
static LIST_HEAD(birthday_list);
struct birthday
{
int day;
int month;
int year;
struct list_head list;
};
struct birthday *ptr, *next;
struct birthday *Bob;
struct birthday *Alice;
struct birthday *youngseaz;
Bob = kmalloc(sizeof(*Bob), GFP_KERNEL);
Bob -> day = 1;
Bob -> month = 1;
Bob -> year = 1990;
// initializes the list member in struct birthday
INIT_LIST_HEAD(&Bob -> list);
Alice = kmalloc(sizeof(*Alice), GFP_KERNEL);
Alice -> day = 2;
Alice -> month = 2;
Alice -> year = 1991;
INIT_LIST_HEAD(&Alice -> list);
youngseaz = kmalloc(sizeof(*youngseaz), GFP_KERNEL);
youngseaz -> day = 1;
youngseaz -> month = 1;
youngseaz -> year = 2000;
INIT_LIST_HEAD(&youngseaz -> list);
//add this instance to the end of the linked list using the list add tail() macro
list_add_tail(&Bob -> list, &birthday_list);
list_add_tail(&Alice -> list, &birthday_list);
list_add_tail(&youngseaz -> list, &birthday_list);
list_for_each_entry(ptr, &birthday_list, list)
{
printk(KERN_INFO "%d/%d/%d \n", ptr -> month, ptr -> day, ptr -> year);
}
//removing all elements from a linked list is
list_for_each_entry_safe(ptr, next, &birthday_list, list)
{
/* Removing elements from the list involves using the list_del() macro,
which is passed a pointer to struct list head */
list_del(&ptr->list);
kfree(ptr);
}
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void)
{
printk(KERN_INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
testmodule2.c的Makefile如下
ifneq ($(KERNELRELEASE),)
obj-m:=testmodule2.o
else
PWD:=$(shell pwd)
KDIR:=/lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
endif