-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailslot.z~
333 lines (269 loc) · 7.49 KB
/
mailslot.z~
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
/* 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())*/
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;
int len;
};
// Mailslot instance struct
static struct mailslot
{
int opened; // 1 if opened
struct message *messages[MAILSLOT_STORAGE];
int messages_count;
};
/* Module facilities */
static int pushMessage(const char *buff, int len, int instance);
static int getMessage(const char *buff, int instance);
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)
{
printk("Mailslot read\n");
// 1. Get minor number
int minor = iminor(filp->f_path.dentry->d_inode);
// 2. Get message
char *ret_buff;
getMessage(buff,minor);
printk("Message: %s\n",buff);
// Change reading pos
if (*off == 0)
{
*off += 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. Push message to mailslot
pushMessage(buff,len,minor);
printk("Done!\n");
return len;
}
/* Module facilities */
// Push message into mailslot (specified by "instance")
static int pushMessage(const char *buff, int len, int instance)
{
printk("Message content: %s\n", buff);
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. Copy input message to allocated space
strcpy(instances[instance]->messages[*count]->content,buff);
instances[instance]->messages[*count]->len = len;
printk("Message pushed: %s\n",instances[instance]->messages[*count]->content);
printk("Message length: %d\n", instances[instance]->messages[*count]->len);
// 3. Increment message counter
*count += 1;
printk("There are currently %d messages in this mailslot\n",*count);
return 0;
}
// Get message (FIFO order). Once a message is returned is also removed from its mailslot
static int getMessage(const char *buff, int instance)
{
int *count = &instances[instance]->messages_count;
if (*count == 0)
{
printk("No message to read\n");
return 0;
}
struct message *msg = instances[instance]->messages[*count-1];
// 1. Copy message to return struct
printk("Copying message: %s\n", msg->content);
printk("Message length: %d\n", msg->len);
//copy_to_user(buff,msg->content,msg->len);
strcpy(buff,msg->content);
// 2. Decrease message counter
*count--;
printk("Message successfully returned and removed\n");
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)
{
// 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);
// 5. 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;
// 6. Allocate space for messages
int j;
for (j = 0; j < MAILSLOT_STORAGE; j++)
{
instances[i]->messages[j] = kmalloc(sizeof(struct message), GFP_KERNEL);
if (!instances[i]->messages[j])
{
printk("Allocating memory for message failed\n");
return -1;
}
instances[i]->messages[j]->content = kmalloc(sizeof(char)*MESSAGE_SIZE,GFP_KERNEL);
if (!instances[i]->messages[j]->content)
{
printk("Allocating memory for message content failed\n");
}
}
}
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;
for (j = 0; j < MAILSLOT_STORAGE; j++)
{
kfree(instances[i]->messages[j]->content);
kfree(instances[i]->messages[j]);
}
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);
}