-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.c
80 lines (62 loc) · 2.47 KB
/
hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/ktime.h>
MODULE_AUTHOR("Gontar Daniil <[email protected]>");
MODULE_DESCRIPTION("Hello, world in Linux Kernel Training");
MODULE_LICENSE("Dual BSD/GPL");
// Оголосити структуру для елемента списку
struct hello_entry {
struct list_head list;
ktime_t time;
};
// Створити статичну змінну голови списку
static LIST_HEAD(hello_list);
// Оголосити параметр
static unsigned int amount = 1;
module_param(amount, uint, S_IRUGO);
MODULE_PARM_DESC(amount, "Number of times to print 'Hello, world!' (default=1)");
static int __init hello_init(void)
{
if (amount == 0 || (amount > 5 && amount < 10)) {
// Надрукувати попередження та продовжити роботу
pr_warn("Print count is 0 or between 5 and 10. Defaulting to 1.\n");
} else if (amount > 10) {
// Надрукувати повідомлення про помилку та повернути -EINVAL
pr_err("Print count is greater than 10. Module cannot be loaded.\n");
return -EINVAL;
}
// Виділити пам'ять та додати елементи до списку відповідно до значень amount
while (amount > 0) {
struct hello_entry *entry = kmalloc(sizeof(*entry), GFP_KERNEL);
if (!entry) {
pr_err("Failed to allocate memory for hello_entry\n");
return -ENOMEM;
}
// Зберегти поточний час ядра
entry->time = ktime_get();
// Додати елемент до списку
list_add(&entry->list, &hello_list);
// Надрукувати привітання
pr_emerg("Hello, world!\n");
amount--;
}
return 0;
}
static void __exit hello_exit(void)
{
struct hello_entry *entry, *temp;
// Пройти по списку
list_for_each_entry_safe(entry, temp, &hello_list, list) {
// Надрукувати час події в наносекундах
pr_emerg("Time: %lld ns\n", ktime_to_ns(entry->time));
// Вилучити елемент зі списку
list_del(&entry->list);
// Звільнити виділену пам'ять
kfree(entry);
}
}
module_init(hello_init);
module_exit(hello_exit);