From 27611f79695e9167ca82099054f21874d54873d2 Mon Sep 17 00:00:00 2001 From: PrismRealm Date: Tue, 5 Mar 2019 16:20:48 +0800 Subject: [PATCH] Add sample.c --- sample.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 sample.c diff --git a/sample.c b/sample.c new file mode 100644 index 0000000..7bcb34d --- /dev/null +++ b/sample.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Sample"); +MODULE_AUTHOR("SGG"); + +static LIST_HEAD(birthday_list); + +struct birthday { + int day; + int month; + int year; + struct list_head list; +}; + +int sample_init(void) +{ + printk(KERN_INFO "Loading Module...\n"); + struct birthday *person; + int i; + for(i = 0; i < 5 ;i++) { + person = kmalloc(sizeof(*person), GFP_KERNEL); + person->day = 2+i; + person->month = 8; + person->year = 1995; + INIT_LIST_HEAD(&person->list); + list_add_tail(&person->list, &birthday_list); + } + printk(KERN_INFO "Loading Module\n"); + printk(KERN_INFO "This list be constructed\n"); + list_for_each_entry(person, &birthday_list, list) { + printk(KERN_INFO "Day: %d Month: %d Year: %d\n", person->day, person->month, person->year); + } + printk(KERN_INFO "Module Loaded.\n"); + return 0; +} + +void sample_exit(void) +{ + printk(KERN_INFO "Removing Module...\n"); + struct birthday *person, *next; + list_for_each_entry_safe(person, next, &birthday_list, list) { + printk(KERN_INFO "Freeing node"); + list_del(&person->list); + kfree(person); + } + printk(KERN_INFO "Removing Module\n"); +} + +module_init(birthday_list_init); +module_exit(birthday_list_exit);