-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmm.c
143 lines (113 loc) · 2.69 KB
/
kmm.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
#include "kmm.h"
#include "printm.h"
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/device.h>
#include "rbuf.h"
//frequently printm call
#define CLASS_NAME "kmm_class"
#define DEVICE_NAME "mod_test"
#define DRIVER_NAME "kernel_monitor"
struct cdev *vcdev = NULL;
dev_t vdev;
struct class *myclass = NULL;
struct file_operations fops = {
.read=device_read,
.write=device_write,
.open=device_open,
.release=device_release
};
struct device *mydevice = NULL;
int kmm_init(void) {
int result = 0;
if(alloc_chrdev_region(&vdev, 0, 1, DRIVER_NAME)) {
printk("alloc_chrdev_region failed\n");
return -EIO;
}
printk("KMM: Dynamic device number assignment: DN=%d MAJOR=%d MINOR=%d\n", vdev, MAJOR(vdev),MINOR(vdev));
vcdev = cdev_alloc();
if(!vcdev) {
return -EIO;
}
//init stuff
if(init_rbuf() < 0) {
printk("init_rbuf failed\n");
return -EIO;
}
init_printm();
cdev_init(vcdev, &fops);
kobject_set_name(&vcdev->kobj, DRIVER_NAME);
vcdev->owner = THIS_MODULE;
result = cdev_add(vcdev, vdev, 1);
if(result < 0) {
printk("adding device failed\n");
goto fail_device;
}
myclass = class_create(vcdev->owner, CLASS_NAME);
if(!myclass)
goto fail_class;
mydevice = device_create(myclass, NULL, vdev, NULL, DEVICE_NAME);
if(!mydevice)
goto fail_device;
return 1;
fail_device:
if(myclass) {
class_unregister(myclass);
class_destroy(myclass);
myclass = NULL;
}
fail_class:
if(vcdev) {
cdev_del(vcdev);
vcdev = NULL;
}
return -EIO;
}
void kmm_exit(void) {
exit_rbuf();
if(mydevice && myclass) {
device_destroy(myclass, vcdev->dev);
}
if(vcdev)
cdev_del(vcdev);
unregister_chrdev_region(MAJOR(vcdev->dev), 1);
if(myclass) {
class_unregister(myclass);
class_destroy(myclass);
myclass = NULL;
}
}
void print_debug(void) {
printk("dev->%i\n", MAJOR(vcdev->dev));
}
int device_open(struct inode *inode, struct file *file) {
register_listener((unsigned long) file);
printk("device_open %lu\n", (unsigned long) file);
return 0;
}
int device_release(struct inode *inode, struct file *file) {
unregister_listener((unsigned long) file);
printk("device_release\n");
return 0;
}
ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset) {
size_t written = 0;
check_listeners();
written = read_next_entry((unsigned long) filp, buffer, length);
printk("written %zu\n", written);
return written;
}
ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
char tmp_buf[1000];
int i = 0;
for(i = 0; i < len; i++) {
printk("adding %c from %i\n", buff[i], i);
tmp_buf[i] = buff[i];
}
if(len) {
tmp_buf[len] = '\0';
}
return printm(tmp_buf);
}