/
lkm_mem.c
128 lines (105 loc) · 3.64 KB
/
lkm_mem.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* LKM Sandbox::Memory, Swap and /proc
* <https://github.com/tpiekarski/lkm-sandbox>
* ---
* Copyright 2020 Thomas Piekarski <t.piekarski@deloquencia.de>
*
* This file is part of LKM Sandbox.
*
* LKM Sandbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* LKM Sandbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LKM Sandbox. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Piekarski");
MODULE_DESCRIPTION("Exposing separated memory and swap statistics to /proc");
MODULE_VERSION("0.1");
#define LKM_PROC_PERMISSION 0444
#define LKM_PROC_PARENT "lkm"
#define LKM_MEM_PROC_PARENT "mem"
#define LKM_MEM_PROC_TOTAL_ENTRY "total"
#define LKM_MEM_PROC_FREE_ENTRY "free"
struct sysinfo si;
struct proc_dir_entry *lkm_proc_parent;
struct proc_dir_entry *mem_proc_parent;
struct proc_dir_entry *mem_proc_total_entry;
struct proc_dir_entry *mem_proc_free_entry;
static int lkm_value_show(struct seq_file *seq, void *v)
{
seq_put_decimal_ull(seq, "", *(unsigned long *)seq->private);
seq_putc(seq, '\n');
return 0;
}
void lkm_proc_create_single_data(struct proc_dir_entry *entry,
unsigned long *value, const char *name)
{
entry = proc_create_single_data(name, LKM_PROC_PERMISSION,
mem_proc_parent, lkm_value_show, value);
if (entry == NULL) {
printk(KERN_ERR "lkm_mem: Failed to create /proc/%s/%s/%s.\n",
LKM_PROC_PARENT, LKM_MEM_PROC_PARENT, name);
// todo: What and how to return error-code without having
// ifs in calling function all over the place? Loading of
// module should be aborted in some way and a clean unload
// (if possible) initiated.
}
}
void lkm_proc_mkdir(struct proc_dir_entry *entry, const char *name,
struct proc_dir_entry *parent)
{
entry = proc_mkdir(name, parent);
if (entry == NULL) {
printk(KERN_ERR
"lkm_mem: Failed to create parent %s in proc.\n",
name);
// todo: How to abort loading module?
// For details see lkm_proc_create_single_data.
}
}
void lkm_remove_proc_entry(struct proc_dir_entry *entry, const char *name,
struct proc_dir_entry *parent)
{
// todo: check if name is stored in proc_dir_entry and remove one arg.
if (entry != NULL) {
remove_proc_entry(name, parent);
}
}
static int __init lkm_mem_init(void)
{
lkm_proc_mkdir(lkm_proc_parent, LKM_PROC_PARENT, NULL);
lkm_proc_mkdir(mem_proc_parent, LKM_MEM_PROC_PARENT, lkm_proc_parent);
si_meminfo(&si);
lkm_proc_create_single_data(mem_proc_total_entry, &si.totalram,
LKM_MEM_PROC_TOTAL_ENTRY);
lkm_proc_create_single_data(mem_proc_free_entry, &si.freeram,
LKM_MEM_PROC_FREE_ENTRY);
return 0;
}
static void __exit lkm_mem_exit(void)
{
lkm_remove_proc_entry(mem_proc_total_entry, LKM_MEM_PROC_TOTAL_ENTRY,
mem_proc_parent);
lkm_remove_proc_entry(mem_proc_free_entry, LKM_MEM_PROC_FREE_ENTRY,
mem_proc_parent);
lkm_remove_proc_entry(mem_proc_parent, LKM_MEM_PROC_PARENT,
lkm_proc_parent);
lkm_remove_proc_entry(lkm_proc_parent, LKM_PROC_PARENT, NULL);
}
module_init(lkm_mem_init);
module_exit(lkm_mem_exit);