Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 04_basic_struct/Log_struct.txt
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions 04_basic_struct/Makefile
Original file line number Diff line number Diff line change
@@ -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
116 changes: 116 additions & 0 deletions 04_basic_struct/struct4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME

#include <linux/module.h> // Core header for loading LKMs into the kernel
#include <linux/kobject.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/list.h>
#include <linux/slab.h>


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 <serhii.siedin@globallogic.com>");
MODULE_DESCRIPTION("Basic data structures module Homework");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");