/
procfs.c
208 lines (181 loc) · 4.69 KB
/
procfs.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************
* in-kernel file reader
*
* Copyright (C) 2018 Michael D. Day II
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*******************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/file.h>
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#define assert(s) do { \
if (unlikely(!(s))) printk(KERN_DEBUG "assertion failed: " #s " at %s:%d\n", \
__FILE__, __LINE__); \
} while(0)
#define _MODULE_LICENSE "GPL v2"
#define _MODULE_AUTHOR "Michael D. Day II <ncultra@gmail.com>"
#define _MODULE_INFO "in-kernel file reader"
char *name = "/proc/cpuinfo";
module_param(name, charp, 0644);
MODULE_PARM_DESC(name, "path of file to read");
int text_file = 1;
module_param(text_file, int, 0644);
MODULE_PARM_DESC(text_file, "file to be read is utf-8");
long chunk_size = 0x400;
module_param(chunk_size, long, 0644);
MODULE_PARM_DESC(chunk_size, "chunk size for virtual files");
long max_size = (~0UL >> 1);
module_param(max_size, long, 0644);
MODULE_PARM_DESC(max_size, "largest file buffer to allocate");
int file_getattr(struct file *f, struct kstat *k)
{
int ccode = 0;
memset(k, 0x00, sizeof(struct kstat));
ccode = vfs_getattr(&f->f_path, k, 0x00000fffU, KSTAT_QUERY_FLAGS);
return ccode;
}
ssize_t write_file(char *name, void *buf, size_t count, loff_t * pos)
{
ssize_t ccode;
struct file *f;
f = filp_open(name, O_RDWR, 0);
if (f) {
ccode = __kernel_write(f, buf, count, pos);
if (ccode < 0) {
pr_err("Unable to write file: %s (%ld)", name, ccode);
}
filp_close(f, 0);
} else {
ccode = -EBADF;
pr_err("Unable to open file: %s (%ld)", name, ccode);
}
return ccode;
}
void dump_buffer(uint8_t *buffer, ssize_t size)
{
ssize_t characters = size;
uint8_t *cursor = buffer;
while (characters) {
printk(KERN_CONT "%c", *cursor);
cursor++;
characters--;
}
}
ssize_t vfs_read_file(char *name, void **buf, size_t max_count, loff_t *pos)
{
ssize_t ccode = 0;
struct file *f = NULL;
assert(buf);
*buf = NULL;
assert(pos);
*pos = 0LL;
f = filp_open(name, O_RDONLY, 0);
if (f) {
ssize_t chunk = chunk_size, allocated = 0, cursor = 0;
*buf = kzalloc(chunk, GFP_KERNEL);
if (*buf) {
allocated = chunk;
} else {
ccode = -ENOMEM;
goto out_err;
}
do {
/**
* read one chunk at a time
**/
cursor = *pos; /* initially zero, then positioned with further reads */
ccode = kernel_read(f, *buf + cursor, chunk, pos);
if (ccode < 0) {
pr_err("Unable to read file chunk: %s (%ld)", name, ccode);
goto out_err;
}
if (ccode > 0) {
*buf = krealloc(*buf, allocated + chunk, GFP_KERNEL);
if (! *buf) {
ccode = -ENOMEM;
goto out_err;
}
allocated += chunk;
}
} while (ccode && allocated <= max_count);
filp_close(f, 0);
} else {
ccode = -EBADF;
pr_err("Unable to open file: %s (%ld)", name, ccode);
}
return ccode;
out_err:
if (f) {
filp_close(f, 0);
}
if (*buf) {
kfree(*buf);
*buf = NULL;
}
return ccode;
}
ssize_t module_main(void)
{
void *buffer = NULL;
int ccode = 0;
loff_t size = 0x1000;
ccode = kernel_read_file_from_path(name,
&buffer,
&size,
max_size,
READING_MODULE);
if (ccode >= 0 && buffer != NULL && size >= 0) {
if (text_file) {
dump_buffer((uint8_t *)buffer, (ssize_t) size);
}
vfree(buffer);
} else {
size = 0LL;
buffer = NULL;
ccode = vfs_read_file(name,
&buffer,
max_size,
&size);
if (ccode >= 0 && buffer != NULL && size >= 0) {
if (text_file) {
dump_buffer((uint8_t *)buffer, (ssize_t)size);
}
kfree(buffer);
}
}
return ccode;
}
static int
__init procfs_init(void)
{
module_main();
return 0;
}
static void
__exit procfs_exit(void)
{
return;
}
module_init(procfs_init);
module_exit(procfs_exit);
MODULE_LICENSE(_MODULE_LICENSE);
MODULE_AUTHOR(_MODULE_AUTHOR);
MODULE_DESCRIPTION(_MODULE_INFO);