From d595cdf771c49137d3063b1396bd548166d3f346 Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 4 Feb 2024 15:41:23 +0100 Subject: [PATCH] add task 12 --- README.md | 15 +++++++ task_12/Makefile | 9 ++++ task_12/list.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 task_12/Makefile create mode 100644 task_12/list.c diff --git a/README.md b/README.md index 7d2c047..501aa39 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,21 @@ ls -l /sys/eudyptula/ `kobject`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/kobject) * [Chapitre 14, Linux Device Drivers, 3rd Edition](https://lwn.net/Kernel/LDD3/) +### Tache 12 + +Il suffit de charger le menu et de lire les messages noyaux pour voir le bon +fonctionnement. J'ai repris le `main` donné dans l'ennoncé sans le modifier +plus que ça : + +``` +[1220142.976299] Coucou le gens !!!! +[1220142.976340] id 3 = Dave +[1220142.976346] id 42 not found +[1220148.124624] Tschuss !!! +``` + + * [Chapitre 11, Linux Device Drivers, 3rd Edition](https://lwn.net/Kernel/LDD3/) + ## Informations diverses Je liste dans cette section quelques informations que j'ai pu découvrir en diff --git a/task_12/Makefile b/task_12/Makefile new file mode 100644 index 0000000..98f3242 --- /dev/null +++ b/task_12/Makefile @@ -0,0 +1,9 @@ +obj-m += list.o + +PWD := $(CURDIR) + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/task_12/list.c b/task_12/list.c new file mode 100644 index 0000000..b5fa8fa --- /dev/null +++ b/task_12/list.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include + +#define NAME_SIZE 20 + +struct identity { + struct list_head list; + char name[NAME_SIZE]; + int id; + bool busy; +}; + +static struct list_head identity_list; + +static int identity_create(char *name, int id) +{ + int ret = 0; + struct identity *new = kmalloc(sizeof(*new), GFP_KERNEL); + + if (!new) { + ret = -ENOMEM; + } else { + ssize_t name_len = strlen(name) + 1; + + if (name_len > NAME_SIZE) { + ret = -EINVAL; + } else { + strscpy(new->name, name, name_len); + new->id = id; + new->busy = false; + list_add_tail(&new->list, &identity_list); + } + } + + return ret; +} + +static struct identity *identity_find(int id) +{ + struct identity *ret = NULL; + struct identity *tmp = NULL; + struct list_head *list = &identity_list; + + do { + tmp = list_entry(list, struct identity, list); + if (tmp->id == id) + ret = tmp; + else + list = tmp->list.next; + } while (list != &identity_list && !ret); + + return ret; +} + +static void identity_destroy(int id) +{ + struct identity *tmp = identity_find(id); + + if (tmp) { + list_del(&tmp->list); + kfree(tmp); + } +} + +static int __init my_init(void) +{ + struct identity *temp; + + pr_info("Coucou le gens !!!!\n"); + INIT_LIST_HEAD(&identity_list); + + if (identity_create("Alice", 1)) + pr_info("Alice n'a pas été créée."); + if (identity_create("Bob", 2)) + pr_info("Bob n'a pas été créée."); + if (identity_create("Dave", 3)) + pr_info("Dave n'a pas été créée."); + if (identity_create("Gena", 10)) + pr_info("Gena n'a pas été créée."); + + temp = identity_find(3); + if (!temp) + pr_info("id 3 not found\n"); + else + pr_info("id 3 = %s\n", temp->name); + + temp = identity_find(42); + if (!temp) + pr_info("id 42 not found\n"); + + identity_destroy(2); + identity_destroy(1); + identity_destroy(10); + identity_destroy(42); + identity_destroy(3); + return 0; +} + +static void __exit my_exit(void) +{ + pr_info("Tschuss !!!\n"); +} + +module_init(my_init); +module_exit(my_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("rick "); +MODULE_DESCRIPTION("Simple liste chainée.");