[Linux] 核心模組相關

  • 查看當前核心模組
lsmod
  • 安裝模組/卸載模組
sudo insmod modulename.ko
sudo rmmod modulename
  • Kernel List
#include<linux/list.h>
#include<linux/types.h>
#include<linux/slab.h>

struct birthday
{
 int day;
 int month;
 int year;
 struct list_head list;
};

//init list
static LIST_HEAD(birthday_list);

//add element
struct birthday *person;

person = kmalloc(sizeof(*person),GFP_KERNEL);
person->day = 18;
person->month = 10;
person->year = 1996;

INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list,&birthday_list);

//traverse
struct birthday *ptr;
list_for_each_entry(ptr, &birthday_list, list){
 printk("Birth: %d/%d/%d\n", ptr->year, ptr->month, ptr->day);
}

//delete
struct birthday *ptr, *next;
list_for_each_entry_safe(ptr, next, &birthday_list, list){
 printk("Delete element %d\n",i);
 i++;
 list_del(&ptr->list);
 kfree(ptr);
}
  • Kernel Module Sample Code
#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("Hubert");

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *