-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathreverse.c
More file actions
235 lines (186 loc) · 4.76 KB
/
reverse.c
File metadata and controls
235 lines (186 loc) · 4.76 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
#include <linux/init.h> /* __init and __exit macroses */
#include <linux/kernel.h> /* KERN_INFO macros */
#include <linux/module.h> /* required for all kernel modules */
#include <linux/moduleparam.h> /* module_param() and MODULE_PARM_DESC() */
#include <linux/fs.h> /* struct file_operations, struct file */
#include <linux/miscdevice.h> /* struct miscdevice and misc_[de]register() */
#include <linux/mutex.h> /* mutexes */
#include <linux/string.h> /* memchr() function */
#include <linux/slab.h> /* kzalloc() function */
#include <linux/sched.h> /* wait queues */
#include <linux/uaccess.h> /* copy_{to,from}_user() */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Valentine Sinitsyn <valentine.sinitsyn@gmail.com>");
MODULE_DESCRIPTION("In-kernel phrase reverser");
static unsigned long buffer_size = 8192;
module_param(buffer_size, ulong, (S_IRUSR | S_IRGRP | S_IROTH));
MODULE_PARM_DESC(buffer_size, "Internal buffer size");
struct buffer {
wait_queue_head_t read_queue;
struct mutex lock;
char *data, *end;
char *read_ptr;
unsigned long size;
};
static struct buffer *buffer_alloc(unsigned long size)
{
struct buffer *buf = NULL;
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (unlikely(!buf))
goto out;
buf->data = kzalloc(size, GFP_KERNEL);
if (unlikely(!buf->data))
goto out_free;
init_waitqueue_head(&buf->read_queue);
mutex_init(&buf->lock);
/* It's unused for now, but may appear useful later */
buf->size = size;
out:
return buf;
out_free:
kfree(buf);
return NULL;
}
static void buffer_free(struct buffer *buffer)
{
kfree(buffer->data);
kfree(buffer);
}
static inline char *reverse_word(char *start, char *end)
{
char *orig_start = start, tmp;
for (; start < end; start++, end--) {
tmp = *start;
*start = *end;
*end = tmp;
}
return orig_start;
}
static char *reverse_phrase(char *start, char *end)
{
char *word_start = start, *word_end = NULL;
while ((word_end = memchr(word_start, ' ', end - word_start)) != NULL) {
reverse_word(word_start, word_end - 1);
word_start = word_end + 1;
}
reverse_word(word_start, end);
return reverse_word(start, end);
}
static int reverse_open(struct inode *inode, struct file *file)
{
struct buffer *buf;
int err = 0;
/*
* Real code can use inode to get pointer to the private
* device state.
*/
buf = buffer_alloc(buffer_size);
if (unlikely(!buf)) {
err = -ENOMEM;
goto out;
}
file->private_data = buf;
out:
return err;
}
static ssize_t reverse_read(struct file *file, char __user * out,
size_t size, loff_t * off)
{
struct buffer *buf = file->private_data;
ssize_t result;
if (mutex_lock_interruptible(&buf->lock)) {
result = -ERESTARTSYS;
goto out;
}
while (buf->read_ptr == buf->end) {
mutex_unlock(&buf->lock);
if (file->f_flags & O_NONBLOCK) {
result = -EAGAIN;
goto out;
}
if (wait_event_interruptible
(buf->read_queue, buf->read_ptr != buf->end)) {
result = -ERESTARTSYS;
goto out;
}
if (mutex_lock_interruptible(&buf->lock)) {
result = -ERESTARTSYS;
goto out;
}
}
size = min(size, (size_t) (buf->end - buf->read_ptr));
if (copy_to_user(out, buf->read_ptr, size)) {
result = -EFAULT;
goto out_unlock;
}
buf->read_ptr += size;
result = size;
out_unlock:
mutex_unlock(&buf->lock);
out:
return result;
}
static ssize_t reverse_write(struct file *file, const char __user * in,
size_t size, loff_t * off)
{
struct buffer *buf = file->private_data;
ssize_t result;
if (size > buffer_size) {
result = -EFBIG;
goto out;
}
if (mutex_lock_interruptible(&buf->lock)) {
result = -ERESTARTSYS;
goto out;
}
if (copy_from_user(buf->data, in, size)) {
result = -EFAULT;
goto out_unlock;
}
buf->end = buf->data + size;
buf->read_ptr = buf->data;
if (buf->end > buf->data)
reverse_phrase(buf->data, buf->end - 1);
wake_up_interruptible(&buf->read_queue);
result = size;
out_unlock:
mutex_unlock(&buf->lock);
out:
return result;
}
static int reverse_close(struct inode *inode, struct file *file)
{
struct buffer *buf = file->private_data;
buffer_free(buf);
return 0;
}
static struct file_operations reverse_fops = {
.owner = THIS_MODULE,
.open = reverse_open,
.read = reverse_read,
.write = reverse_write,
.release = reverse_close,
.llseek = noop_llseek
};
static struct miscdevice reverse_misc_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "reverse",
.fops = &reverse_fops
};
static int __init reverse_init(void)
{
if (!buffer_size)
return -1;
misc_register(&reverse_misc_device);
printk(KERN_INFO
"reverse device has been registered, buffer size is %lu bytes\n",
buffer_size);
return 0;
}
static void __exit reverse_exit(void)
{
misc_deregister(&reverse_misc_device);
printk(KERN_INFO "reverse device has been unregistered\n");
}
module_init(reverse_init);
module_exit(reverse_exit);