add task 12

This commit is contained in:
rick 2024-02-04 15:41:23 +01:00
parent aa602678c0
commit d595cdf771
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
3 changed files with 138 additions and 0 deletions

View file

@ -287,6 +287,21 @@ ls -l /sys/eudyptula/
`kobject`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/kobject) `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/) * [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 ## Informations diverses
Je liste dans cette section quelques informations que j'ai pu découvrir en Je liste dans cette section quelques informations que j'ai pu découvrir en

9
task_12/Makefile Normal file
View file

@ -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

114
task_12/list.c Normal file
View file

@ -0,0 +1,114 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/string.h>
#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 <rick@gnous.eu>");
MODULE_DESCRIPTION("Simple liste chainée.");