Files
OS/sample.c
2019-03-07 04:27:08 -08:00

68 lines
1.4 KiB
C

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/slab.h>
struct birthday
{
int day;
int month;
int year;
struct list_head list;
};
static LIST_HEAD(birthday_list);
/* This function is called when the module is loaded. */
int sample_init(void)
{
printk(KERN_INFO "Loading Module\n");
struct birthday *person;
int i = 0;
for (i = 0; i < 5; i++)
{
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->day = 2;
person->month = 8 + i ;
person->year = 1995 + i;
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
}
list_for_each_entry(person, &birthday_list, list)
{
printk(KERN_INFO "Birthday: %1d/%1d/%1d\n", person->year, person->month, person->day);
}
printk(KERN_INFO "Module Loaded\n");
return 0;
}
/* This function is called when the module is removed. */
void sample_exit(void)
{
printk(KERN_INFO "Removing Module\n");
struct birthday *person, *next;
list_for_each_entry_safe(person, next, &birthday_list, list)
{
list_del(&person->list);
kfree(person);
}
printk(KERN_INFO "Module Removed\n");
}
/* Macros for registering module entry and exit points. */
module_init(sample_init);
module_exit(sample_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Sample Module");
MODULE_AUTHOR("SGG");