-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifoproc.c
More file actions
297 lines (234 loc) · 5.83 KB
/
fifoproc.c
File metadata and controls
297 lines (234 loc) · 5.83 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
296
297
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/vmalloc.h>
#include <asm-generic/uaccess.h>
#include <asm-generic/errno.h>
#include <linux/semaphore.h>
#include "cbuffer.h"
#define MAX_ITEMS_CBUFFER 50
#define MAX_CHARS_KBUF 50
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Implementation of a FIFO using a /proc entry");
MODULE_AUTHOR("Carlos Martinez & Sergio Pino");
static struct proc_dir_entry *proc_entry;
static cbuffer_t* cbuffer;
int prod_count = 0; /* Number of processes who opened /proc entry for writing (producers) */
int cons_count = 0; /* Number of processes who opened /proc entry for reading (consumers) */
struct semaphore mtx; /* This guarantees mutual exclusion */
struct semaphore sem_prod; /* Queue for producers */
struct semaphore sem_cons; /* Queue for consumers */
int nr_prod_waiting = 0; /* Number of producers waiting */
int nr_cons_waiting = 0; /* Number of consumers waiting */
static int fifoproc_open(struct inode *inode, struct file *file) {
if (down_interruptible(&mtx)) {
return -EINTR;
}
if (file->f_mode & FMODE_READ) {
/* A consumer opens FIFO */
cons_count++;
while (nr_prod_waiting) {
nr_prod_waiting--;
up(&sem_prod);
}
while (!prod_count) {
nr_cons_waiting++;
up(&mtx);
if (down_interruptible(&sem_cons)) {
down(&mtx);
nr_cons_waiting--;
up(&mtx);
down(&mtx);
cons_count--;
up(&mtx);
return -EINTR;
}
if (down_interruptible(&mtx)) {
down(&mtx);
cons_count--;
up(&mtx);
}
}
} else {
/* A producer opens FIFO */
prod_count++;
while (nr_cons_waiting) {
nr_cons_waiting--;
up(&sem_cons);
}
while (!cons_count) {
nr_prod_waiting++;
up(&mtx);
if (down_interruptible(&sem_prod)) {
down(&mtx);
nr_prod_waiting--;
up(&mtx);
down(&mtx);
prod_count--;
up(&mtx);
return -EINTR;
}
if (down_interruptible(&mtx)) {
down(&mtx);
prod_count--;
up(&mtx);
}
}
}
up(&mtx);
return 0;
}
static int fifoproc_release(struct inode *inode, struct file *file) {
if (down_interruptible(&mtx)) {
return -EINTR;
}
if (file->f_mode & FMODE_READ) {
cons_count--;
if (cons_count == 0) {
while (nr_prod_waiting) {
nr_prod_waiting--;
up(&sem_prod);
}
}
} else {
prod_count--;
}
if (!(cons_count || prod_count)) {
clear_cbuffer_t(cbuffer);
}
up(&mtx);
return 0;
}
static ssize_t fifoproc_read(struct file *filp, char __user *buf, size_t len, loff_t *off) {
char kbuf[MAX_CHARS_KBUF];
if((*off) > 0) { return 0; }
if(len > MAX_CHARS_KBUF) { return -ENOSPC; }
/* Block mutex */
if(down_interruptible(&mtx)) {
return -EINTR;
}
/* If there isn't producers and pipe is empty, FIFO must be closed */
if (prod_count == 0 && is_empty_cbuffer_t(cbuffer)) {
up(&mtx);
return 0;
}
/* Block while buffer is empty */
while (size_cbuffer_t(cbuffer) < len) {
/* Increments consumers waiting */
nr_cons_waiting++;
/* Releases the mutex before blocking it */
up(&mtx);
/* Blocking queue */
if (down_interruptible(&sem_cons)) {
down(&mtx);
nr_cons_waiting--;
up(&mtx);
return -EINTR;
}
if (prod_count == 0 && is_empty_cbuffer_t(cbuffer)) {
up(&mtx);
return 0;
}
/* Acquires the mutex before entering the critical section */
if (down_interruptible(&mtx)) {
return -EINTR;
}
}
/* Obtains cbuffer data */
remove_items_cbuffer_t(cbuffer, kbuf, len);
/* Wake up to potential producers */
while (nr_prod_waiting) {
up(&sem_prod);
nr_prod_waiting--;
}
/* Unblock mutex */
up(&mtx);
len -= copy_to_user(buf, kbuf, len);
return len;
}
static ssize_t fifoproc_write(struct file *filp, const char __user *buf, size_t len, loff_t *off) {
char kbuf[MAX_CHARS_KBUF];
if((*off) > 0) { return 0; }
if(len > MAX_CHARS_KBUF) { return -ENOSPC; }
len -= copy_from_user(kbuf, buf, len);
/* Block mutex */
if (down_interruptible(&mtx)) { return -EINTR; }
/* If there isn't consumers, FIFO must be closed */
if (cons_count == 0) {
up(&mtx);
return -EPIPE;
}
/* Producer is blocked if no space */
while (nr_gaps_cbuffer_t(cbuffer) < len && cons_count > 0) {
/* Increase producers waiting */
nr_prod_waiting++;
/* Releases the mutex before blocking it */
up(&mtx);
/* Blocking queue */
if (down_interruptible(&sem_prod)) {
down(&mtx);
nr_prod_waiting--;
up(&mtx);
return -EINTR;
}
if (cons_count == 0) {
up(&mtx);
return -EPIPE;
}
/* Acquires the mutex before entering the critical section */
if (down_interruptible(&mtx)) {
return -EINTR;
}
}
/* Insert data received in cbuffer */
insert_items_cbuffer_t(cbuffer, kbuf, len);
/* Wake up to potential consumers */
while (nr_cons_waiting) {
up(&sem_cons);
nr_cons_waiting--;
}
/* Unlock mutex */
up(&mtx);
return len;
}
/*
* Available operations module
*/
static const struct file_operations proc_entry_fops = {
.open = fifoproc_open,
.release = fifoproc_release,
.read = fifoproc_read,
.write = fifoproc_write,
};
/*
* Module initialization.
*/
int init_fifoproc_module(void) {
cbuffer = create_cbuffer_t(MAX_ITEMS_CBUFFER);
if(!cbuffer) {
return -ENOMEM;
}
sema_init(&sem_prod, 0);
sema_init(&sem_cons, 0);
sema_init(&mtx, 1);
proc_entry = proc_create_data("fifoproc", 0666, NULL, &proc_entry_fops, NULL);
if(proc_entry == NULL) {
destroy_cbuffer_t(cbuffer);
printk(KERN_INFO "Fifoproc: Couldn't create /proc entry.\n");
return -ENOMEM;
}
printk(KERN_INFO "Fifoproc: Module loaded.\n");
return 0;
}
/*
* Unload module
*/
void exit_fifoproc_module(void) {
remove_proc_entry("fifoproc", NULL);
destroy_cbuffer_t(cbuffer);
printk(KERN_INFO "Fifoproc: Module unloaded.\n");
}
module_init( init_fifoproc_module );
module_exit( exit_fifoproc_module );