115 lines
2.1 KiB
C
115 lines
2.1 KiB
C
|
// 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.");
|