-
Notifications
You must be signed in to change notification settings - Fork 1
/
devtilde.c
225 lines (194 loc) · 6.23 KB
/
devtilde.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Shamelessly based on the lkmpg's char.c */
#include <linux/init.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("fauxm");
MODULE_DESCRIPTION("~");
MODULE_VERSION("~.2.0");
#define SUCCESS 0
#define FAILURE 1
#define CLASS_NAME "tilde"
static struct class* tildeC = NULL;
// Normal tilde
#define TILDEVICE_NAME "tilde"
#define MSG_LEN 1
const static char msg = '~';
static int TildeMajor;
static struct device* tildeD = NULL;
static int tilde_open(struct inode*, struct file*);
static int tilde_release(struct inode*, struct file*);
static ssize_t tilde_read(struct file*, char*, size_t, loff_t*);
static ssize_t tilde_write(struct file*, const char*, size_t, loff_t*);
// Wide tilde
#define WTILDEVICE_NAME "widetilde"
#define WIDEMSG_LEN 3
const static int widemsg = 0x9EBDEF;
static int WTildeMajor;
static struct device* wtildeD = NULL;
#define wtilde_open tilde_open
#define wtilde_release tilde_release
static ssize_t wtilde_read(struct file*, char*, size_t, loff_t*);
#define wtilde_write tilde_write
// Big tilde
#define BTILDEVICE_NAME "bigtilde"
#define BIGMSG_LEN 91
const static char* bigmsg = " ~~~~\n"
" ~~~~ ~~~~ ~~~~\n"
"~~~~ ~~~~ ~~~~\n"
" ~~~~\n\n\n\n";
static int BTildeMajor;
static struct device* btildeD = NULL;
#define btilde_open tilde_open
#define btilde_release tilde_release
static ssize_t btilde_read(struct file*, char*, size_t, loff_t*);
#define btilde_write tilde_write
static struct file_operations tilde_fops = {
.read = tilde_read,
.write = tilde_write,
.open = tilde_open,
.release = tilde_release
};
static struct file_operations wtilde_fops = {
.read = wtilde_read,
.write = wtilde_write,
.open = wtilde_open,
.release = wtilde_release
};
static struct file_operations btilde_fops = {
.read = btilde_read,
.write = btilde_write,
.open = btilde_open,
.release = btilde_release
};
// Helper function for dev_init() to call; registers and creates the devices.
static int reg_and_create_devices(void)
{
TildeMajor = register_chrdev(0, TILDEVICE_NAME, &tilde_fops);
WTildeMajor = register_chrdev(0, WTILDEVICE_NAME, &wtilde_fops);
BTildeMajor = register_chrdev(0, BTILDEVICE_NAME, &btilde_fops);
tildeC = class_create(THIS_MODULE, CLASS_NAME);
if(IS_ERR(tildeC)) {
unregister_chrdev(TildeMajor, TILDEVICE_NAME);
unregister_chrdev(WTildeMajor, WTILDEVICE_NAME);
unregister_chrdev(BTildeMajor, BTILDEVICE_NAME);
printk(KERN_ALERT "~: Failed to register device class\n");
return PTR_ERR(tildeC);
}
if(TildeMajor < 0) {
printk(KERN_ALERT "~: Registering %s failed with %d\n", TILDEVICE_NAME, TildeMajor);
} else {
tildeD = device_create(tildeC, NULL, MKDEV(TildeMajor, 0), NULL, TILDEVICE_NAME);
if(IS_ERR(tildeD)) {
class_destroy(tildeC);
unregister_chrdev(TildeMajor, TILDEVICE_NAME);
printk(KERN_ALERT "~: Failed to create %s. Aborting.\n", TILDEVICE_NAME);
return PTR_ERR(tildeD);
}
}
if(WTildeMajor < 0) {
printk(KERN_ALERT "~: Registering %s failed with %d\n", WTILDEVICE_NAME, WTildeMajor);
} else {
wtildeD = device_create(tildeC, NULL, MKDEV(WTildeMajor, 0), NULL, WTILDEVICE_NAME);
if(IS_ERR(wtildeD)) {
class_destroy(tildeC);
unregister_chrdev(WTildeMajor, WTILDEVICE_NAME);
printk(KERN_ALERT "~: Failed to create %s. Aborting.\n", WTILDEVICE_NAME);
return PTR_ERR(wtildeD);
}
}
if(BTildeMajor < 0) {
printk(KERN_ALERT "~: Registering %s failed with %d\n", BTILDEVICE_NAME, BTildeMajor);
} else {
btildeD = device_create(tildeC, NULL, MKDEV(BTildeMajor, 0), NULL, BTILDEVICE_NAME);
if(IS_ERR(btildeD)) {
class_destroy(tildeC);
unregister_chrdev(BTildeMajor, BTILDEVICE_NAME);
printk(KERN_ALERT "~: Failed to create %s. Aborting.\n", BTILDEVICE_NAME);
return PTR_ERR(btildeD);
}
}
return SUCCESS;
}
static int __init dev_init(void)
{
if(reg_and_create_devices() == 0) {
printk(KERN_INFO "~: Module initiallized successfully~!\n");
return SUCCESS;
} else {
printk(KERN_ALERT "~: Oopsie daisy, we made a fucky-wucky~!\n");
return FAILURE;
}
}
static void __exit dev_exit(void)
{
device_destroy(tildeC, MKDEV(TildeMajor, 0));
device_destroy(tildeC, MKDEV(WTildeMajor, 0));
device_destroy(tildeC, MKDEV(BTildeMajor, 0));
class_unregister(tildeC);
class_destroy(tildeC);
unregister_chrdev(TildeMajor, TILDEVICE_NAME);
unregister_chrdev(WTildeMajor, WTILDEVICE_NAME);
unregister_chrdev(BTildeMajor, BTILDEVICE_NAME);
printk(KERN_INFO "~: Module cleanup successful~!\n");
}
static int tilde_open(struct inode *inode, struct file *file)
{
return SUCCESS;
}
static int tilde_release(struct inode *inode, struct file *file)
{
return SUCCESS;
}
static ssize_t tilde_read(struct file *filep,
char *buf,
size_t len,
loff_t *off)
{
int errno = 0;
errno = copy_to_user(buf, &msg, MSG_LEN);
if (errno != 0){
printk(KERN_INFO "~: lol something happened: %d\n", errno);
return -EFAULT;
}
return MSG_LEN;
}
static ssize_t wtilde_read(struct file *filep,
char *buf,
size_t len,
loff_t *off)
{
int errno = 0;
errno = copy_to_user(buf, &widemsg, WIDEMSG_LEN);
if (errno != 0){
printk(KERN_INFO "~: lol something happened: %d\n", errno);
return -EFAULT;
}
return WIDEMSG_LEN;
}
static ssize_t btilde_read(struct file *filep,
char *buf,
size_t len,
loff_t *off)
{
int errno = 0;
errno = copy_to_user(buf, bigmsg, BIGMSG_LEN);
if (errno != 0){
printk(KERN_INFO "~: lol something happened: %d\n", errno);
return -EFAULT;
}
return BIGMSG_LEN;
}
static ssize_t tilde_write(struct file *filep,
const char *buf,
size_t len,
loff_t *off)
{
printk(KERN_INFO "~: Writing is not supported, silly~!\n");
return -EINVAL;
}
module_init(dev_init);
module_exit(dev_exit);