-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_slot.c
More file actions
295 lines (241 loc) · 7.61 KB
/
Copy pathmessage_slot.c
File metadata and controls
295 lines (241 loc) · 7.61 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
#undef __KERNEL__
#define __KERNEL__
#undef MODULE
#define MODULE
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/module.h> // included for all kernel modules
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/init.h> // included for __init and __exit macros
#include "message_slot.h"
#define err_msg(msg) \
printk(KERN_ERR "message_slot: %s, line: %d\n", msg, __LINE__);
MODULE_LICENSE("GPL");
static message_slot devices[MAX_SLOTS]; // This will holds all the slots s.t devices[minor] = slot
struct chardev_info
{
spinlock_t lock;
};
static struct chardev_info device_info;
static int device_open(struct inode *inode, struct file *file)
{
int minor;
minor = iminor(inode); // Finding the minor of the inode
// Will save the address of the slot of the opened file in the private_data of the file
// If there is no slot for the opened file, then devices[minor] will hold an empty slot (slot that wasn't initialize)
file->private_data = &devices[minor];
return 0;
}
static long device_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
{
message_slot *slot = file->private_data;
channel *ch = (*slot).channels;
unsigned int channel_id = (unsigned int)ioctl_param;
if (ioctl_num != MSG_SLOT_CHANNEL)
{
err_msg("Invalid ioctl control command");
return -EINVAL;
}
// channel id must be a non-zero (and since it's unsigned it must be > 0)
if(channel_id == 0)
{
err_msg("Invalid ioctl command param (invalid channel id)");
return -EINVAL;
}
// Reach the maximum number of channels in the slot
if (slot->num_channels >= MAX_CHANNELS)
{
err_msg("Reached maximum channels");
return -1;
}
// Going through the channels of the slot and searching the desired channel
while (ch != NULL && ch->id != channel_id)
{
ch = ch->next;
}
if (ch == NULL)
{
ch = (channel *)kmalloc(sizeof(channel), GFP_KERNEL);
if (ch == NULL) // The kalloc failed
{
err_msg("kmalloc failed");
return -ENOMEM;
}
ch->id = channel_id;
ch->message_len = 0;
ch->next = slot->channels;
slot->channels = ch;
slot->num_channels++;
}
// Setting the current channel
slot->current_channel = ch;
// get_channel did the work of changing the current channel to given channel_id
return 0;
}
static ssize_t device_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
{
message_slot *slot;
channel *ch;
char write_buffer[BUF_LEN];
int i;
// There isn't a slot set to the fd
if (file->private_data == NULL)
{
err_msg("Uninitialized slot for the fd");
return -EINVAL;
}
slot = file->private_data;
// There isn't a channel set to the fd
if (slot->current_channel == NULL)
{
err_msg("Uninitialized channel for the slot");
return -EINVAL;
}
ch = slot->current_channel; // slot->current_channel is not NULL
// buffer length is inavlid
if (length == 0 || length > BUF_LEN)
{
err_msg("Invalid buffer length for write");
return -EMSGSIZE;
}
// buffer is inavlid
if (buffer == NULL)
{
err_msg("Invalid buffer for write");
return -EINVAL;
}
// Reading the message from user into write_buffer
// Writing to write_buffer so the writing will be atomic
for (i = 0; i < length; i++)
{
if (get_user(write_buffer[i], &buffer[i]) != 0) // get_user falied
{
err_msg("get_user failed");
return -EFAULT;
}
}
// Failed writing entire message
if (i != length)
{
err_msg("Failed writing whole message");
return -EAGAIN; // Try again error code
}
// Updating the channel
memcpy(ch->message, write_buffer, sizeof(ch->message));
ch->message_len = i;
return i;
}
static ssize_t device_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
{
message_slot *slot;
channel *ch;
char __user read_buffer[BUF_LEN];
int i;
// There isn't a slot set to the fd
if (file->private_data == NULL)
{
err_msg("Uninitialized slot for the fd");
return -EINVAL;
}
slot = file->private_data;
// There isn't a channel set to the fd
if ((slot->current_channel) == NULL)
{
err_msg("Uninitialized channel for the slot");
return -EINVAL;
}
ch = slot->current_channel; // slot->current_channel is not NULL
// There is no message in the channel
if (ch->message_len == 0)
{
err_msg("Trying to read an empty message");
return -EWOULDBLOCK;
}
// The buffer length is too small to hold the message
if (ch->message_len > length)
{
err_msg("Trying to read a long message");
return -ENOSPC;
}
// Copying the buffer to the read_buffer, for supporting atomic read
memcpy(read_buffer, buffer, length);
// Reading the message from channel into read_buffer
// Writing to read_buffer so the reading will be atomic
for (i = 0; i < ch->message_len; i++)
{
if (put_user((ch->message)[i], &buffer[i]) != 0) // put_user falied
{
// The read failed, so returning buffer to it's initialize state by copying read_buffer to buffer
memcpy(buffer, read_buffer, length);
err_msg("put_user failed");
return -EFAULT;
}
}
// Failed reading entire message
if (i != ch->message_len)
{
// The read failed, so returning buffer to it's initialize state by copying read_buffer to buffer
memcpy(buffer, read_buffer, length);
err_msg("Failed readig a whole message");
return -EAGAIN; // Try again error code
}
return i;
}
static int device_release(struct inode *inode, struct file *file)
{
message_slot *slot = file->private_data;
slot->current_channel = NULL; // Reset the current channel
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = device_open,
.unlocked_ioctl = device_ioctl,
.write = device_write,
.read = device_read,
.release = device_release,
};
static int __init message_slot_init(void)
{
int i, result;
message_slot slot;
memset(&device_info, 0, sizeof(struct chardev_info));
spin_lock_init(&device_info.lock);
result = register_chrdev(MAJOR_NUM, DEVICE_RANGE_NAME, &fops);
if (result < 0)
{
err_msg("Cannot register device");
return result;
}
// Initializing the slots
for (i = 0; i < MAX_SLOTS; i++)
{
slot = devices[i];
slot.channels = NULL;
slot.num_channels = 0;
slot.current_channel = NULL;
}
return 0;
}
static void __exit message_slot_cleanup(void)
{
int i;
// Going through the slots to free the channels
for (i = 0; i < MAX_SLOTS; i++)
{
channel *tmp;
channel *head = devices[i].channels;
// Freeing all the channels
while (head != NULL)
{
tmp = head;
head = head->next;
kfree(tmp);
}
}
unregister_chrdev(MAJOR_NUM, DEVICE_RANGE_NAME);
}
module_init(message_slot_init);
module_exit(message_slot_cleanup);