commit ef25eb0e44f29e89356fab353691492faa4f9696 Author: omgohmygod Date: Thu Mar 7 04:27:08 2019 -0800 HW1 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bfc969f --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +obj-m := sample.o +PWD := $(shell pwd) +KERNEL_VERSION := $(shell uname -r) +KERNEL_DIR := /usr/src/linux-headers-$(KERNEL_VERSION)/ +all: + make -C $(KERNEL_DIR) M=$(PWD) modules + +clean: + make -C $(KERNEL_DIR) M=$(PWD) clean \ No newline at end of file diff --git a/sample.c b/sample.c new file mode 100644 index 0000000..2909db4 --- /dev/null +++ b/sample.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +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"); \ No newline at end of file