-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathker.c
More file actions
313 lines (259 loc) · 5.8 KB
/
ker.c
File metadata and controls
313 lines (259 loc) · 5.8 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/moduleparam.h>
#include <linux/seq_file.h>
#include <linux/jiffies.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#define BUFFER_MAX_SIZE 100
#define SUCCESS 0
#define PLUS 0
#define MINUS 1
#define MULTIPLY 2
#define DIVIDE 3
MODULE_AUTHOR("Glebik Stas!!!");
struct proc_dir_entry *result_proc;
struct proc_dir_entry *first_op_proc;
struct proc_dir_entry *second_op_proc;
struct proc_dir_entry *oper_proc;
char buff[BUFFER_MAX_SIZE];
static char * result = "result";
module_param(result, charp, 0);
static char * first_operand = "first operand";
module_param(first_operand, charp, 0);
static char * second_operand = "second operand";
module_param(second_operand, charp, 0);
static char * operation = "operation";
module_param(operation, charp, 0);
static int oper_type = PLUS;
static char first_oper[BUFFER_MAX_SIZE];
static char second_oper [BUFFER_MAX_SIZE];
static int atoi(char * a)
{
int res = 0;
int i = 0;
for (i = 0; a[i] != 0; ++i) {
if (a[i] >= '0' && a[i] <= '9') {
res = res * 10 + a[i] - '0';
}
}
return res;
}
static int
read_proc(struct seq_file *m, void *v) {
switch(oper_type) {
case PLUS:
seq_printf(m, "%d\n", atoi(first_oper) + atoi(second_oper));
break;
case MINUS:
seq_printf(m, "%d\n", atoi(first_oper) - atoi(second_oper));
break;
case MULTIPLY:
seq_printf(m, "%d\n", atoi(first_oper) * atoi(second_oper));
break;
case DIVIDE:
if (atoi(second_oper) == 0) {
seq_printf(m, "Disivion by zero!\n");
}
else {
seq_printf(m, "%d\n", atoi(first_oper) / atoi(second_oper));
}
break;
}
return 0;
}
static int
jif_open(struct inode *inode, struct file *file)
{
return single_open(file, read_proc, NULL);
}
static const struct file_operations proc_file_fops = {
.owner = THIS_MODULE,
.open = jif_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static ssize_t
procfs_read_oper(struct file *filp,
char *buffer,
size_t length,
loff_t *offset)
{
static int finished = 0;
char sign;
if (finished) {
finished = 0;
return 0;
}
finished = 1;
switch(oper_type) {
case PLUS:
sign = '+';
break;
case MINUS:
sign = '-';
break;
case DIVIDE:
sign = '/';
break;
default:
sign = '*';
break;
}
if (copy_to_user(buffer, &sign, 1)) {
return -EFAULT;
}
return 1;
}
static ssize_t
procfs_read_second(struct file *filp,
char *buffer,
size_t length,
loff_t *offset)
{
static int finished = 0;
if (finished) {
finished = 0;
return 0;
}
finished = 1;
if (copy_to_user(buffer, second_oper, strlen(second_oper))) {
return -EFAULT;
}
return strlen(second_oper);
}
static ssize_t
procfs_read_first(struct file *filp,
char *buffer,
size_t length,
loff_t *offset)
{
static int finished = 0;
if (finished) {
finished = 0;
return 0;
}
finished = 1;
if (copy_to_user(buffer, first_oper, strlen(first_oper))) {
return -EFAULT;
}
return strlen(first_oper);
}
int operation_write(struct file *file, const char * buffer, size_t count,
loff_t *data)
{
if (count > BUFFER_MAX_SIZE)
{
count = BUFFER_MAX_SIZE-1;
}
if (copy_from_user(buff, buffer, count)) {
return -EFAULT;
}
if (count > 0) {
switch (buff[0]) {
case '+':
oper_type = PLUS;
break;
case '-':
oper_type = MINUS;
break;
case '*':
oper_type = MULTIPLY;
break;
case '/':
oper_type = DIVIDE;
break;
}
}
else {
oper_type = PLUS;
}
return count;
}
int second_operand_write(struct file *file, const char * buffer, size_t count,
loff_t *data)
{
if (count >= BUFFER_MAX_SIZE)
{
count = BUFFER_MAX_SIZE-1;
}
if (copy_from_user(second_oper, buffer, count)) {
return -EFAULT;
}
return count;
}
int first_operand_write(struct file *file, const char * buffer, size_t count,
loff_t *data)
{
if (count >= BUFFER_MAX_SIZE)
{
count = BUFFER_MAX_SIZE-1;
}
if (copy_from_user(first_oper, buffer, count)) {
return -EFAULT;
}
return count;
}
static ssize_t
procfs_open(struct inode *inode, struct file *file)
{
try_module_get(THIS_MODULE);
return SUCCESS;
}
static int procfs_release(struct inode *inode, struct file *file)
{
module_put(THIS_MODULE);
return SUCCESS;
}
static const struct file_operations operation_write_fops = {
.owner = THIS_MODULE,
.open = procfs_open,
.release = procfs_release,
.write = operation_write,
.read = procfs_read_oper,
};
static const struct file_operations first_operand_write_fops = {
.owner = THIS_MODULE,
.open = procfs_open,
.release = procfs_release,
.write = first_operand_write,
.read = procfs_read_first,
};
static const struct file_operations second_operand_write_fops = {
.owner = THIS_MODULE,
.open = procfs_open,
.release = procfs_release,
.write = second_operand_write,
.read = procfs_read_second,
};
static int __init test_init( void )
{
printk( KERN_ALERT "ker module loaded\n");
result_proc = proc_create(result, 0, NULL, &proc_file_fops);
first_op_proc = proc_create(first_operand, 0666, NULL, &first_operand_write_fops);
second_op_proc = proc_create(second_operand, 0666, NULL, &second_operand_write_fops);
oper_proc = proc_create(operation, 0666, NULL, &operation_write_fops);
strcpy(first_oper, "0");
strcpy(second_oper, "0");
if (result_proc == NULL || first_op_proc == NULL
|| second_op_proc == NULL || oper_proc == NULL) {
printk(KERN_INFO "Error: Could not initialize /proc/ker\n");
return -ENOMEM;
} else {
printk(KERN_INFO "Success!\n");
}
return 0;
}
static void __exit test_exit( void )
{
remove_proc_entry(result, NULL);
remove_proc_entry(first_operand, NULL);
remove_proc_entry(second_operand, NULL);
remove_proc_entry(operation, NULL);
printk(KERN_ALERT "ker module is unloaded\n");
}
module_init( test_init );
module_exit( test_exit );