forked from embedded2013/rtenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_server.c
318 lines (276 loc) · 7.83 KB
/
path_server.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
#include "path_server.h"
#include "str_util.h"
#include "syscall.h"
/*
* pathserver assumes that all files are FIFOs that were registered
* with mkfifo. It also assumes a global tables of FDs shared by all
* processes. It would have to get much smarter to be generally useful.
*
* The first TASK_LIMIT FDs are reserved for use by their respective tasks.
* 0-2 are reserved FDs and are skipped.
* The server registers itself at /sys/pathserver
*/
void pathserver_task()
{
char paths[PIPE_LIMIT - TASK_LIMIT - 3][PATH_MAX];
int npaths = 0;
int i = 0;
unsigned int str_len = 0;
unsigned int replyfd = 0;
char path[PATH_MAX];
memcpy(paths[npaths++], PATH_SERVER_NAME, sizeof(PATH_SERVER_NAME));
while (1) {
read(PATHSERVER_FD, &replyfd, 4);
read(PATHSERVER_FD, &str_len, 4);
read(PATHSERVER_FD, path, str_len);
if (!replyfd) { /* mkfifo */
int dev;
read(PATHSERVER_FD, &dev, 4);
memcpy(paths[npaths], path, str_len);
mknod(npaths + 3 + TASK_LIMIT, 0, dev);
npaths++;
}
else { /* open */
/* Search for path */
for (i = 0; i < npaths; i++) {
if (*paths[i] && strcmp(path, paths[i]) == 0) {
i += 3; /* 0-2 are reserved */
i += TASK_LIMIT; /* FDs reserved for tasks */
write(replyfd, &i, 4);
i = 0;
break;
}
}
if (i >= npaths) {
i = -1; /* Error: not found */
write(replyfd, &i, 4);
}
}
}
}
int mkfile(const char *pathname, int mode, int dev)
{
size_t str_len = strlen(pathname)+1;
char buf[4+4+PATH_MAX+4];
(void) mode;
*((unsigned int *)buf) = 0;
*((unsigned int *)(buf + 4)) = str_len;
memcpy(buf + 4 + 4, pathname, str_len);
*((int *)(buf + 4 + 4 + str_len)) = dev;
write(PATHSERVER_FD, buf, 4 + 4 + str_len + 4);
return 0;
}
int mkfifo(const char *pathname, int mode)
{
mkfile(pathname, mode, S_IFIFO);
return 0;
}
int open(const char *pathname, int flags)
{
unsigned int replyfd = gettid() + 3;
size_t str_len = strlen(pathname) + 1;
unsigned int fd = -1;
char buf[4 + 4 + PATH_MAX];
(void) flags;
*((unsigned int *)buf) = replyfd;
*((unsigned int *)(buf + 4)) = str_len;
memcpy(buf + 4 + 4, pathname, str_len);
write(PATHSERVER_FD, buf, 4 + 4 + str_len);
read(replyfd, &fd, 4);
return fd;
}
int mq_open(const char *name, int oflag)
{
if (oflag & O_CREAT)
mkfile(name, 0, S_IMSGQ);
return open(name, 0);
}
void _read(struct task_control_block *task, struct task_control_block *tasks, struct pipe_ringbuffer *pipes)
{
task->status = TASK_READY;
/* If the fd is invalid */
if (task->stack->r0 > PIPE_LIMIT) {
task->stack->r0 = -1;
}
else {
struct pipe_ringbuffer *pipe = &pipes[task->stack->r0];
if (pipe->readable(pipe, task)) {
size_t i;
pipe->read(pipe, task);
/* Unblock any waiting writes */
for (i = 0; i < TASK_LIMIT; i++) {
if(tasks[i].status != TASK_IS_EMPTY) {
if (tasks[i].status == TASK_WAIT_WRITE)
_write(&tasks[i], tasks, pipes);
}
}
}
}
}
void _write(struct task_control_block *task, struct task_control_block *tasks, struct pipe_ringbuffer *pipes)
{
task->status = TASK_READY;
/* If the fd is invalid */
if (task->stack->r0 > PIPE_LIMIT) {
task->stack->r0 = -1;
}
else {
struct pipe_ringbuffer *pipe = &pipes[task->stack->r0];
if (pipe->writable(pipe, task)) {
size_t i;
pipe->write(pipe, task);
/* Unblock any waiting reads */
for (i = 0; i < TASK_LIMIT; i++) {
if(tasks[i].status != TASK_IS_EMPTY) {
if (tasks[i].status == TASK_WAIT_READ)
_read(&tasks[i], tasks, pipes);
}
}
}
}
}
int
fifo_readable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
/* Trying to read too much */
if (task->stack->r2 > PIPE_BUF) {
task->stack->r0 = -1;
return 0;
}
if ((size_t)PIPE_LEN(*pipe) < task->stack->r2) {
/* Trying to read more than there is: block */
task->status = TASK_WAIT_READ;
return 0;
}
return 1;
}
int
mq_readable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t msg_len;
/* Trying to read too much */
if ((size_t)PIPE_LEN(*pipe) < sizeof(size_t)) {
/* Nothing to read */
task->status = TASK_WAIT_READ;
return 0;
}
PIPE_PEEK(*pipe, msg_len, 4);
if (msg_len > task->stack->r2) {
/* Trying to read more than buffer size */
task->stack->r0 = -1;
return 0;
}
return 1;
}
int
fifo_read (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t i;
char *buf = (char*)task->stack->r1;
/* Copy data into buf */
for (i = 0; i < task->stack->r2; i++) {
PIPE_POP(*pipe, buf[i]);
}
return task->stack->r2;
}
int
mq_read (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t msg_len;
size_t i;
char *buf = (char*)task->stack->r1;
/* Get length */
for (i = 0; i < 4; i++) {
PIPE_POP(*pipe, *(((char*)&msg_len)+i));
}
/* Copy data into buf */
for (i = 0; i < msg_len; i++) {
PIPE_POP(*pipe, buf[i]);
}
return msg_len;
}
int
fifo_writable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
/* If the write would be non-atomic */
if (task->stack->r2 > PIPE_BUF) {
task->stack->r0 = -1;
return 0;
}
/* Preserve 1 byte to distiguish empty or full */
if ((size_t)PIPE_BUF - PIPE_LEN(*pipe) - 1 < task->stack->r2) {
/* Trying to write more than we have space for: block */
task->status = TASK_WAIT_WRITE;
return 0;
}
return 1;
}
int
mq_writable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t total_len = sizeof(size_t) + task->stack->r2;
/* If the write would be non-atomic */
if (total_len > PIPE_BUF) {
task->stack->r0 = -1;
return 0;
}
/* Preserve 1 byte to distiguish empty or full */
if ((size_t)PIPE_BUF - PIPE_LEN(*pipe) - 1 < total_len) {
/* Trying to write more than we have space for: block */
task->status = TASK_WAIT_WRITE;
return 0;
}
return 1;
}
int
fifo_write (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t i;
const char *buf = (const char*)task->stack->r1;
/* Copy data into pipe */
for (i = 0; i < task->stack->r2; i++)
PIPE_PUSH(*pipe,buf[i]);
return task->stack->r2;
}
int
mq_write (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t i;
const char *buf = (const char*)task->stack->r1;
/* Copy count into pipe */
for (i = 0; i < sizeof(size_t); i++)
PIPE_PUSH(*pipe, *(((char*)&task->stack->r2) + i));
/* Copy data into pipe */
for (i = 0; i < task->stack->r2; i++)
PIPE_PUSH(*pipe, buf[i]);
return task->stack->r2;
}
int
_mknod(struct pipe_ringbuffer *pipe, int dev)
{
switch(dev) {
case S_IFIFO:
pipe->readable = fifo_readable;
pipe->writable = fifo_writable;
pipe->read = fifo_read;
pipe->write = fifo_write;
break;
case S_IMSGQ:
pipe->readable = mq_readable;
pipe->writable = mq_writable;
pipe->read = mq_read;
pipe->write = mq_write;
break;
default:
return 1;
}
return 0;
}