From 8557de900ac3a874f781bdc769f27b432eca7786 Mon Sep 17 00:00:00 2001 From: Maryna Malakhova Date: Fri, 19 Mar 2021 14:47:20 +0200 Subject: [PATCH] BSTRUCTMODULE: Add list Add list of strings insted of int value. Add logs Signed-off-by: Maryna Malakhova --- 04_basic_struct/BBB_log.txt | 57 ++++++++++++++++++++++++++++++ 04_basic_struct/bstructmodule.c | 42 ++++++++++++++++++---- 04_basic_struct/checkpatch_log.txt | 4 +++ 3 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 04_basic_struct/BBB_log.txt create mode 100644 04_basic_struct/checkpatch_log.txt diff --git a/04_basic_struct/BBB_log.txt b/04_basic_struct/BBB_log.txt new file mode 100644 index 00000000..813fc1ad --- /dev/null +++ b/04_basic_struct/BBB_log.txt @@ -0,0 +1,57 @@ +/mnt # +/mnt # ls -la +total 16 +drwxr-xr-x 2 root root 4096 Mar 18 2021 . +drwxr-xr-x 14 root root 4096 Mar 18 2021 .. +-rw-r--r-- 1 root root 5744 Mar 19 2021 bstructmodule.ko +/mnt # +/mnt # +/mnt # dmesg -c +/mnt # +/mnt # lsmod +/mnt # +/mnt # insmod bstructmodule.ko +/mnt # +/mnt # lsmod +bstructmodule 16384 0 - Live 0xbf018000 (O) +/mnt # +/mnt # dmesg +/mnt # +/mnt # cat /sys/kernel/hello/list +/mnt # +/mnt # echo first > /sys/kernel/hello/list +[15507.266994] bstructmodule: store nodes: Added node = first +/mnt # +/mnt # echo second > /sys/kernel/hello/list +[15518.703747] bstructmodule: store nodes: Added node = second +/mnt # +/mnt # cat /sys/kernel/hello/list +second +first +/mnt # +/mnt # echo third > /sys/kernel/hello/list +[15563.359760] bstructmodule: store nodes: Added node = third +/mnt # +/mnt # cat /sys/kernel/hello/list +third +second +first +/mnt # +/mnt # rmmod bstructmodule.ko +[15586.216674] bstructmodule: remove node: third +[15586.221264] bstructmodule: remove node: second +[15586.226007] bstructmodule: remove node: first +[15586.230584] bstructmodule: module exited +/mnt # +/mnt # dmesg +[15507.266994] bstructmodule: store nodes: Added node = first +[15518.703747] bstructmodule: store nodes: Added node = second +[15563.359760] bstructmodule: store nodes: Added node = third +[15586.216674] bstructmodule: remove node: third +[15586.221264] bstructmodule: remove node: second +[15586.226007] bstructmodule: remove node: first +[15586.230584] bstructmodule: module exited +/mnt # +/mnt # lsmod +/mnt # + diff --git a/04_basic_struct/bstructmodule.c b/04_basic_struct/bstructmodule.c index 4df6e872..8f8c0826 100644 --- a/04_basic_struct/bstructmodule.c +++ b/04_basic_struct/bstructmodule.c @@ -7,22 +7,43 @@ #include #include #include +#include +#include -static int value; +struct list_node { + char *node; + struct list_head head_list; +}; + +LIST_HEAD(linkedlist); static ssize_t hello_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { - pr_info("hello_show: value = %d\n", value); - sprintf(buf, "%d\n", value); - return strlen(buf); + struct list_node *cur_node = NULL; + int offset = 0; + int res = 0; + + list_for_each_entry(cur_node, &linkedlist, head_list) { + res = scnprintf(buf+offset, PAGE_SIZE-offset, "%s", cur_node->node); + offset += res; + } + return offset; } static ssize_t hello_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { - sscanf(buf, "%d\n", &value); - pr_info("hello_store: value = %d\n", value); + struct list_node *new_node = + kmalloc(sizeof(struct list_node), GFP_KERNEL); + + if (!new_node) + return -ENOMEM; + new_node->node = kstrdup(buf, GFP_KERNEL); + if (new_node->node == NULL) + return -ENOMEM; + list_add_tail(&new_node->head_list, &linkedlist); + pr_info("store nodes: Added node = %s", buf); return count; } @@ -57,6 +78,15 @@ static int __init hello_init(void) static void __exit hello_exit(void) { + struct list_node *cur_node = NULL; + struct list_node *tmp_node = NULL; + + list_for_each_entry_safe(cur_node, tmp_node, &linkedlist, head_list) { + pr_info("remove node: %s", cur_node->node); + list_del(&cur_node->head_list); + kfree(cur_node->node); + kfree(cur_node); + } kobject_put(hello_kobj); pr_info("module exited\n"); } diff --git a/04_basic_struct/checkpatch_log.txt b/04_basic_struct/checkpatch_log.txt new file mode 100644 index 00000000..6be6199b --- /dev/null +++ b/04_basic_struct/checkpatch_log.txt @@ -0,0 +1,4 @@ +total: 0 errors, 0 warnings, 100 lines checked + +/home/marynam/Maryna/KernelProCamp21/gl-kernel-training-2021/04_basic_struct/bstructmodule.c has no obvious style problems and is ready for submission. +