-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcachefilesd2.c
86 lines (70 loc) · 1.58 KB
/
cachefilesd2.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
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <stdint.h>
#include "internal.h"
static int process_one_req(int devfd)
{
char buf[CACHEFILES_MSG_MAX_SIZE];
struct cachefiles_msg *msg;
size_t len;
int ret;
memset(buf, 0, sizeof(buf));
ret = read(devfd, buf, sizeof(buf));
if (ret < 0)
printf("read devnode failed\n");
if (ret <= 0)
return -1;
msg = (void *)buf;
if (ret != msg->len) {
printf("invalid message length %d (readed %d)\n", msg->len, ret);
return -1;
}
printf("[HEADER] id %u, opcode %d\t", msg->msg_id, msg->opcode);
switch (msg->opcode) {
case CACHEFILES_OP_OPEN:
return process_open_req(devfd, msg);
case CACHEFILES_OP_CLOSE:
return process_close_req(devfd, msg);
case CACHEFILES_OP_READ:
return process_read_req(devfd, msg);
default:
printf("invalid opcode %d\n", msg->opcode);
return -1;
}
}
int main(int argc, char *argv[])
{
struct pollfd pollfd;
char *fscachedir;
int fd, ret;
if (argc != 2) {
printf("Using example: cachefilesd2 <fscachedir>\n");
return -1;
}
fscachedir = argv[1];
fd = daemon_get_devfd(fscachedir, "test");
if (fd < 0)
return -1;
pollfd.fd = fd;
pollfd.events = POLLIN;
while (1) {
ret = poll(&pollfd, 1, -1);
if (ret < 0) {
printf("poll failed\n");
return -1;
}
if (ret == 0 || !(pollfd.revents & POLLIN)) {
printf("poll returned %d (%x)\n", ret, pollfd.revents);
continue;
}
/* process all pending read requests */
while (!process_one_req(fd)) {}
}
return 0;
}