author | Tero Marttila <terom@fixme.fi> |
Fri, 26 Sep 2008 20:11:22 +0300 | |
changeset 6 | d2036d7799fd |
parent 3 | 10b53719659c |
child 27 | 461be4cd34a3 |
permissions | -rw-r--r-- |
1 | 1 |
|
2 |
#include <errno.h> |
|
3 |
#include <string.h> |
|
4 |
#include <stdlib.h> |
|
0 | 5 |
|
6 |
#include "evfuse.h" |
|
6
d2036d7799fd
new 'simple' module, plus hello_simple
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
7 |
#include "lib/log.h" |
0 | 8 |
|
9 |
struct evfuse { |
|
3 | 10 |
// our mountpoint |
11 |
char *mountpoint; |
|
12 |
||
0 | 13 |
// the /dev/fuse fd/channel that we get from fuse_mount |
14 |
struct fuse_chan *chan; |
|
15 |
||
16 |
// the session that we use to process the fuse stuff |
|
17 |
struct fuse_session *session; |
|
18 |
||
19 |
// the event that we use to receive requests |
|
20 |
struct event *ev; |
|
21 |
||
22 |
// what our receive-message length is |
|
23 |
size_t recv_size; |
|
24 |
||
25 |
// the buffer that we use to receive events |
|
26 |
char *recv_buf; |
|
27 |
}; |
|
28 |
||
29 |
static void _evfuse_ev_read (evutil_socket_t fd, short what, void *arg) { |
|
30 |
struct evfuse *ctx = arg; |
|
1 | 31 |
struct fuse_chan *ch = ctx->chan; |
0 | 32 |
int res; |
33 |
||
34 |
// loop until we complete a recv |
|
35 |
do { |
|
36 |
// a new fuse_req is available |
|
37 |
res = fuse_chan_recv(&ch, ctx->recv_buf, ctx->recv_size); |
|
38 |
} while (res == -EINTR); |
|
39 |
||
40 |
if (res == 0) |
|
41 |
ERROR("fuse_chan_recv gave EOF"); |
|
42 |
||
43 |
if (res < 0 && res != -EAGAIN) |
|
44 |
ERROR("fuse_chan_recv failed: %s", strerror(-res)); |
|
45 |
||
46 |
if (res > 0) { |
|
1 | 47 |
INFO("[evfuse] got %d bytes from /dev/fuse", res); |
48 |
||
0 | 49 |
// received a fuse_req, so process it |
50 |
fuse_session_process(ctx->session, ctx->recv_buf, res, ch); |
|
51 |
} |
|
52 |
||
53 |
// reschedule |
|
54 |
if (event_add(ctx->ev, NULL)) |
|
55 |
PERROR("event_add"); |
|
56 |
||
57 |
// ok, wait for the next event |
|
58 |
return; |
|
59 |
||
60 |
error: |
|
6
d2036d7799fd
new 'simple' module, plus hello_simple
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
61 |
// fail |
d2036d7799fd
new 'simple' module, plus hello_simple
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
62 |
evfuse_close(ctx); |
0 | 63 |
} |
64 |
||
65 |
struct evfuse *evfuse_new (struct event_base *evbase, struct fuse_args *args, struct fuse_lowlevel_ops *llops, void *cb_data) { |
|
66 |
struct evfuse *ctx = NULL; |
|
67 |
int multithreaded, foreground; |
|
68 |
||
69 |
// allocate our context |
|
1 | 70 |
if ((ctx = calloc(1, sizeof(*ctx))) == NULL) |
0 | 71 |
ERROR("calloc"); |
72 |
||
73 |
// parse the commandline for the mountpoint |
|
3 | 74 |
if (fuse_parse_cmdline(args, &ctx->mountpoint, &multithreaded, &foreground) == -1) |
0 | 75 |
ERROR("fuse_parse_cmdline"); |
76 |
||
77 |
// mount it |
|
3 | 78 |
if ((ctx->chan = fuse_mount(ctx->mountpoint, args)) == NULL) |
0 | 79 |
PERROR("fuse_mount_common"); |
80 |
||
81 |
// the receive buffer stufff |
|
82 |
ctx->recv_size = fuse_chan_bufsize(ctx->chan); |
|
83 |
||
84 |
// allocate the recv buffer |
|
85 |
if ((ctx->recv_buf = malloc(ctx->recv_size)) == NULL) |
|
86 |
ERROR("malloc"); |
|
87 |
||
88 |
// allocate a low-level session |
|
89 |
if ((ctx->session = fuse_lowlevel_new(args, llops, sizeof(*llops), cb_data)) == NULL) |
|
90 |
PERROR("fuse_lowlevel_new"); |
|
91 |
||
92 |
// add the channel to the session |
|
93 |
// this isn't strictly necessary, but let's do it anyways |
|
94 |
fuse_session_add_chan(ctx->session, ctx->chan); |
|
95 |
||
96 |
// now, we can start listening for events on the channel |
|
97 |
if ((ctx->ev = event_new(evbase, fuse_chan_fd(ctx->chan), EV_READ, &_evfuse_ev_read, ctx)) == NULL) |
|
98 |
ERROR("event_new"); |
|
99 |
||
100 |
if (event_add(ctx->ev, NULL)) |
|
101 |
PERROR("event_add"); |
|
102 |
||
103 |
// and then we wait |
|
1 | 104 |
return ctx; |
0 | 105 |
|
106 |
error: |
|
107 |
free(ctx); |
|
108 |
||
109 |
return NULL; |
|
110 |
} |
|
3 | 111 |
|
112 |
void evfuse_close (struct evfuse *ctx) { |
|
113 |
// remove our event |
|
114 |
if (event_del(ctx->ev)) |
|
115 |
PWARNING("event_del"); |
|
116 |
||
117 |
// remove the chan |
|
118 |
fuse_session_remove_chan(ctx->chan); |
|
119 |
||
120 |
// destroy the session |
|
121 |
fuse_session_destroy(ctx->session); |
|
122 |
||
123 |
// unmount |
|
124 |
fuse_unmount(ctx->mountpoint, ctx->chan); |
|
125 |
||
126 |
// free |
|
127 |
free(ctx->recv_buf); |
|
128 |
free(ctx->mountpoint); |
|
129 |
free(ctx); |
|
130 |
} |
|
131 |