-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailslot.c
392 lines (316 loc) · 8.79 KB
/
mailslot.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Mailslot implementation using Kernel Module */
#define EXPORT_SYMTAB
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/pid.h> /* For pid types */
#include <linux/version.h> /* For LINUX_VERSION_CODE */
#include <linux/slab.h> /* For kmalloc */
#include <asm/uaccess.h> /* For copy_to_user */
#include <linux/cdev.h> /* Modern way to handle cdevs (cdev_alloc())*/
#include <linux/mutex.h> /* Mutual exclusion */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Michele Rullo");
/* ioctl operations */
static int mailslot_open(struct inode *, struct file *);
static int mailslot_release(struct inode *, struct file *);
static ssize_t mailslot_read(struct file * filp, const char * buf, size_t, loff_t *);
static ssize_t mailslot_write(struct file * filp, const char * buf, size_t, loff_t *);
#define DEVICE_NAME "mailslot"
#define INSTANCES 256
#define MESSAGE_SIZE 256
#define MAILSLOT_STORAGE 256
#define MINOR_LOWER 0
static int Major; /* Major number assigned to broadcast device driver */
struct cdev *mailslot_cdev;
// Message Struct
static struct message
{
char *content;
size_t len;
struct message *next;
};
// Mailslot instance struct
static struct mailslot
{
int opened; // 1 if opened
struct message *head; // FIFO head
struct message *tail; // FIFO tail
int messages_count;
struct mutex mutex; // Mutual exclusion on mailslot (device)
};
/* Module facilities */
static int pushMessage(const char *buff, size_t len, int instance);
static int getMessage(const char *buff, int instance, size_t *ret_len);
static int clearMailslot(int instance);
// Mailslots
static struct mailslot* instances[INSTANCES];
static int instances_count = 0;
/* Create a new mailslot instance */
static int mailslot_open(struct inode *inode, struct file *file)
{
printk("Opening mailslot\n");
if (instances_count == INSTANCES)
{
printk("No more room to allocate new mailslot\n");
return -1;
}
// Get minor number (device)
int minor = iminor(file->f_path.dentry->d_inode);
if (!instances[minor]->opened)
{
instances_count++;
instances[minor]->opened = 1;
printk("New mailslot instance created with minor number: %d. There are %d mailslots now.\n", minor, instances_count);
return 0;
}
else
{
printk("There is already a mailslot opened associated with minor number %d.\n", minor);
return -1;
}
}
static int mailslot_release(struct inode *inode, struct file *file)
{
printk("Releasing mailslot\n");
/* This should not happen */
if (instances_count == 0)
{
printk("No mailslots no remove!\n");
return -1;
}
// Get minor number (device)
int minor = iminor(file->f_path.dentry->d_inode);
// Release mailslot if it's opened
if (instances[minor]->opened)
{
instances_count--;
instances[minor]->opened = 0;
printk("Successfully closed mailslot with minor number: %d\n",minor);
return 0;
}
else
{
printk("Mailslot %d is already closed.\n", minor);
return -1;
}
}
/* Read from desired mailslot. Each message gets removed when consumed.*/
static ssize_t mailslot_read(struct file *filp,
const char *buff,
size_t len,
loff_t *off)
{
// Avoid reading if off > 0 ("cat" reads twice!)
if (*off > 0) return 0;
printk("Mailslot read\n");
// 1. Get minor number
int minor = iminor(filp->f_path.dentry->d_inode);
// 2. Try getting the lock on current mailslot
if (mutex_lock_interruptible(&instances[minor]->mutex))
{
printk("Mailslot %d is currently busy\n",minor);
return -1;
}
// 3. Get message
size_t ret_len;
getMessage(buff,minor,&ret_len);
// 4. Unlock mutex
mutex_unlock(&instances[minor]->mutex);
printk("Message: %s\n",buff);
strcat(buff, "\n");
// 5. Change reading pos
if (*off == 0)
{
*off += ret_len+1;
return *off;
}
else
return 0;
}
/* Write on desired mailslot */
static ssize_t mailslot_write(struct file *filp,
const char *buff,
size_t len,
loff_t *off)
{
printk("Mailslot writing %d bytes\n",len);
// 1. Get minor number (device)
int minor = iminor(filp->f_path.dentry->d_inode);
// 2. Try getting the lock on current mailslot
if (mutex_lock_interruptible(&instances[minor]->mutex))
{
printk("Mailslot %d is currently busy\n",minor);
return -1;
}
// 3. Push message to mailslot
pushMessage(buff,len,minor);
// 4. Release lock
mutex_unlock(&instances[minor]->mutex);
printk("Done!\n");
return len;
}
/* Module facilities */
// Get message (FIFO order). Once a message is returned is also removed from its mailslot
static int getMessage(const char *buff, int instance, size_t *ret_len)
{
int *count = &instances[instance]->messages_count;
if (*count == 0)
{
printk("No message to read\n");
char err[] = "No message to read";
strcpy(buff,err);
*ret_len = strlen(err);
return 0;
}
*ret_len = instances[instance]->head->len;
// 1. Copy message to return struct
printk("Copying message: %s\n", instances[instance]->head->content);
printk("Message length: %d\n", instances[instance]->head->len);
//copy_to_user(buff,msg->content,msg->len);
memcpy(buff,instances[instance]->head->content,instances[instance]->head->len);
// 2. Remove message
if (*count > 1)
{
struct message *new_head = instances[instance]->head->next;
kfree(instances[instance]->head);
instances[instance]->head = new_head;
}
else if (*count == 1)
kfree(instances[instance]->head);
// 2. Decrease message counter
*count -= 1;
printk("Message successfully returned and removed\n");
return 0;
}
// Push message into mailslot (specified by "instance")
static int pushMessage(const char *buff, size_t len, int instance)
{
printk("Pushing message to mailslot: %d\n",instance);
int *count = &instances[instance]->messages_count;
// 1. Check if there's space
if (*count == MAILSLOT_STORAGE)
{
printk("Mailslot full, message discarded\n");
return -1;
}
// 2. Allocate space for message struct
struct message *msg = kmalloc(sizeof(struct message), GFP_KERNEL);
if (!msg)
{
printk("Unable to allocate space for message\n");
return -1;
}
memset(msg,0,sizeof(struct message));
// 3. Allocate space for message content
msg->content = kmalloc(MESSAGE_SIZE, GFP_KERNEL);
if (!msg->content)
{
printk("Unable to allocate space for message content\n");
return -1;
}
memset(msg->content,0,MESSAGE_SIZE);
// 4. Copy input message to allocated space
memcpy(msg->content,buff,len);
msg->len = len;
printk("Message pushed: %s\n",msg->content);
printk("Message length: %d\n", msg->len);
// 3. Link message to tail
if (*count == 0)
{
instances[instance]->head = msg;
instances[instance]->tail = msg;
}
if (*count > 0)
instances[instance]->tail->next = msg;
// 4. Increment message counter
*count += 1;
printk("There are currently %d messages in this mailslot\n",*count);
return 0;
}
// Clear mailslot (called by "release" ioctl operation)
static int clearMailslot(int instance)
{
printk("Mailslot %d cleared\n", instance);
return 0;
}
// File operations struct
static struct file_operations fops =
{
.read = mailslot_read,
.write = mailslot_write,
.open = mailslot_open,
.release = mailslot_release
};
int init_module(void)
{
// Allocate mailslot instances
int i;
for (i = 0; i < INSTANCES; i++)
{
instances[i] = kmalloc(sizeof(struct mailslot), GFP_KERNEL);
if (!instances[i])
{
printk("Allocating memory for mailslot failed\n");
return -1;
}
memset(instances[i], 0, sizeof(struct mailslot));
instances[i]->opened = 0;
instances[i]->messages_count = 0;
mutex_init(&instances[i]->mutex);
}
/* cdev setup */
// 1. Allocate dynamically a device numbers region
dev_t dev;
int err = alloc_chrdev_region(&dev, MINOR_LOWER, MINOR_LOWER+INSTANCES, DEVICE_NAME);
if (err)
{
printk("Allocating chrdev region failed\n");
return err;
}
Major = MAJOR(dev);
// 2. Allocate cdev struct
mailslot_cdev = cdev_alloc();
// 3. Init cdev
cdev_init(mailslot_cdev, &fops);
// 4. Add cdev
err = cdev_add(mailslot_cdev, dev, MINOR_LOWER+INSTANCES);
if (err)
{
printk("Adding cdev failed\n");
return err;
}
// Old way:
// Major = register_chrdev(MINOR_LOWER, DEVICE_NAME, &fops);
if (Major < 0)
{
printk("Registering mailslot device failed\n");
return Major;
}
printk(KERN_INFO "Mailslot device registered, it is assigned major number %d\n", Major);
return 0;
}
void cleanup_module(void)
{
printk("Cleaning Mailslot Module Up\n");
// De-Allocate memory for mailslots
int i;
for (i = 0; i < INSTANCES; i++)
{
int j;
struct message *msg;
for (j = 0; j < instances[i]->messages_count; j++)
{
msg = instances[i]->head->next;
kfree(instances[i]->head->content);
kfree(instances[i]->head);
instances[i]->head = msg;
}
kfree(instances[i]);
}
unregister_chrdev(Major, DEVICE_NAME);
cdev_del(mailslot_cdev);
printk(KERN_INFO "Mailslot device unregistered, it was assigned major number %d\n", Major);
}