From 934a52218609be7ceef8dacccb09065af2bbb787 Mon Sep 17 00:00:00 2001 From: Oleksandr Posukhov Date: Tue, 23 Nov 2021 20:10:08 +0200 Subject: [PATCH 1/3] Task04: Add report Log_struct.txt Add report for home task4 for lection Basic Data structures Signed-off-by: Oleksandr Posukhov Signed-off-by: SyedinSN --- 04_basic_struct/Log_struct.txt | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 04_basic_struct/Log_struct.txt diff --git a/04_basic_struct/Log_struct.txt b/04_basic_struct/Log_struct.txt new file mode 100644 index 0000000..b9de77c --- /dev/null +++ b/04_basic_struct/Log_struct.txt @@ -0,0 +1,29 @@ +# insmod struct4.ko +struct4: loading out-of-tree module taints kernel. +struct4: module load +# echo '1' > /sys/kernel/hello/list +struct4: adding report size: 2 to the list: '1 +' +# echo -n "2" > /sys/kernel/hello/list +struct4: adding report size: 1 to the list: '2' +# echo -n "45" > /sys/kernel/hello/list +struct4: adding report size: 2 to the list: '45' +# echo -n "27" > /sys/kernel/hello/list +struct4: adding report size: 2 to the list: '27' +# cat /sys/kernel/hello/list +struct4: report #0 size: 2 '1 +' +struct4: report #1 size: 1 '2' +struct4: report #2 size: 2 '45' +struct4: report #3 size: 2 '27' +1 +24527# +# +# rmmod struct4.ko +struct4: freeing log size: 2 from the list: '27' +struct4: freeing log size: 2 from the list: '45' +struct4: freeing log size: 1 from the list: '2' +struct4: freeing log size: 2 from the list: '1 +' +struct4: list is empty: 1 +struct4: module exited From d5e7fccce09b888ee6bb5a73f13b0e93e5b10183 Mon Sep 17 00:00:00 2001 From: SyedinSN Date: Fri, 10 Dec 2021 12:42:49 +0200 Subject: [PATCH 2/3] Task4: Add new Makefile for this task Added a file to build the project within the current task Signed-off-by: SyedinSN --- 04_basic_struct/Makefile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 04_basic_struct/Makefile diff --git a/04_basic_struct/Makefile b/04_basic_struct/Makefile new file mode 100644 index 0000000..1db171f --- /dev/null +++ b/04_basic_struct/Makefile @@ -0,0 +1,10 @@ +KERNELDIR ?= ../output/build/linux-5.10.7/ #WARNING relative path + +obj-m := struct4.o + + +all: + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules + +clean: + $(MAKE) -C $(KERNELDIR) M=$(PWD) clean From c756f8768ddd13a68b636a96d0dc1d5a39e5737b Mon Sep 17 00:00:00 2001 From: SyedinSN Date: Fri, 10 Dec 2021 12:46:39 +0200 Subject: [PATCH 3/3] Task4: Add new file struct4.c Added file with code for current task Signed-off-by: SyedinSN --- 04_basic_struct/struct4.c | 116 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 04_basic_struct/struct4.c diff --git a/04_basic_struct/struct4.c b/04_basic_struct/struct4.c new file mode 100644 index 0000000..e8c97fa --- /dev/null +++ b/04_basic_struct/struct4.c @@ -0,0 +1,116 @@ +#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME + +#include // Core header for loading LKMs into the kernel +#include +#include +#include +#include +#include +#include + + +struct m_item { + struct list_head list; + char *m; + size_t m_len; +}; +static LIST_HEAD(m_list_head); + +static ssize_t hello_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) +{ + struct m_item *pos = NULL; + size_t item_number = 0; + size_t count = 0; + char *p_buff = buf; + + list_for_each_entry_reverse(pos, &m_list_head, list) + { + pr_info("report #%lu size: %lu '%.*s'\n", + item_number, pos->m_len, (int)pos->m_len, (char *)pos->m); + + memcpy(p_buff, (char *)pos->m, pos->m_len); + p_buff += pos->m_len; + count += pos->m_len; + item_number += 1; + } + return count; +} + +static ssize_t hello_store(struct kobject *kobj, struct kobj_attribute *attr, + const char *buf, size_t count) +{ + char *p_msg = NULL; + struct m_item *p_item = NULL; + + p_msg = kmalloc(count, GFP_KERNEL); + if (!p_msg) + { + pr_err("could not allocate report: %lu\n", count); + return -ENOMEM; + } + p_item = kmalloc(sizeof(struct m_item), GFP_KERNEL); + if (!p_item) + { + pr_err("could not allocate m_item\n"); + return -ENOMEM; + } + memcpy(p_msg, buf, count); + p_item->m = p_msg; + p_item->m_len = count; + + pr_info("adding report size: %lu to the list: '%.*s'\n", + p_item->m_len, (int)p_item->m_len, (char *)p_item->m); + list_add(&p_item->list, &m_list_head); + return count; +} + +static struct kobj_attribute list_attribute = + __ATTR(list, 0664, hello_show, hello_store); + +static struct kobject *hello_kobj; + + +static int hello_init(void) +{ + int res = 0; + + pr_info("module load\n"); + + hello_kobj = kobject_create_and_add("hello", kernel_kobj); + + if (!hello_kobj) + return -ENOMEM; + res = sysfs_create_file(hello_kobj, &list_attribute.attr); + + if (res) + kobject_put(hello_kobj); + return res; +} + +static void hello_exit(void) +{ + struct list_head *pos = NULL; + struct list_head *n = NULL; + struct m_item *p_item = NULL; + + list_for_each_safe(pos, n, &m_list_head) + { + p_item = list_entry(pos, struct m_item, list); + pr_info("freeing log size: %lu from the list: '%.*s'\n", + p_item->m_len, (int)p_item->m_len, (char *)p_item->m); + list_del(pos); + kfree(p_item->m); + kfree(p_item); + } + pr_info("list is empty: %d\n", list_empty_careful(&m_list_head)); + kobject_put(hello_kobj); + pr_info("module exited\n"); +} + +module_init(hello_init); +module_exit(hello_exit); + +MODULE_AUTHOR("Serhii Siedin "); +MODULE_DESCRIPTION("Basic data structures module Homework"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.1"); \ No newline at end of file