Skip to content

Commit

Permalink
BSTRUCTMODULE: Add list
Browse files Browse the repository at this point in the history
Add list of strings insted of int value. Add logs

Signed-off-by: Maryna Malakhova <[email protected]>
  • Loading branch information
marynamalakhova committed Apr 5, 2021
1 parent 1f808a8 commit 8557de9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
57 changes: 57 additions & 0 deletions 04_basic_struct/BBB_log.txt
Original file line number Diff line number Diff line change
@@ -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 #

42 changes: 36 additions & 6 deletions 04_basic_struct/bstructmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,43 @@
#include <linux/err.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/list.h>
#include <linux/slab.h>

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;
}

Expand Down Expand Up @@ -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");
}
Expand Down
4 changes: 4 additions & 0 deletions 04_basic_struct/checkpatch_log.txt
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 8557de9

Please sign in to comment.