src/evfuse.c
changeset 56 9dfc861273e5
parent 42 40a3b13ffc9d
child 57 527d23bf6441
equal deleted inserted replaced
42:40a3b13ffc9d 56:9dfc861273e5
     1 
       
     2 #include <errno.h>
       
     3 #include <string.h>
       
     4 #include <stdlib.h>
       
     5 
       
     6 #include "evfuse.h"
       
     7 #include "lib/log.h"
       
     8 
       
     9 struct evfuse {
       
    10     // our mountpoint
       
    11     char *mountpoint;
       
    12 
       
    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 // prototypes
       
    30 void evfuse_close (struct evfuse *ctx);
       
    31 
       
    32 static void _evfuse_ev_read (evutil_socket_t fd, short what, void *arg) {
       
    33     struct evfuse *ctx = arg;
       
    34     struct fuse_chan *ch = ctx->chan;
       
    35     int res;
       
    36     
       
    37     // loop until we complete a recv
       
    38     do {
       
    39         // a new fuse_req is available
       
    40         res = fuse_chan_recv(&ch, ctx->recv_buf, ctx->recv_size);
       
    41     } while (res == -EINTR);
       
    42 
       
    43     if (res == 0)
       
    44         ERROR("fuse_chan_recv gave EOF");
       
    45 
       
    46     if (res < 0 && res != -EAGAIN)
       
    47         ERROR("fuse_chan_recv failed: %s", strerror(-res));
       
    48     
       
    49     if (res > 0) {
       
    50         DEBUG("got %d bytes from /dev/fuse", res);
       
    51 
       
    52         // received a fuse_req, so process it
       
    53         fuse_session_process(ctx->session, ctx->recv_buf, res, ch);
       
    54     }
       
    55     
       
    56     // reschedule
       
    57     if (event_add(ctx->ev, NULL))
       
    58         PERROR("event_add");
       
    59 
       
    60     // ok, wait for the next event
       
    61     return;
       
    62 
       
    63 error:
       
    64     // close, but don't free
       
    65     evfuse_close(ctx);
       
    66 }
       
    67 
       
    68 struct evfuse *evfuse_new (struct event_base *evbase, struct fuse_args *args, struct fuse_lowlevel_ops *llops, void *cb_data) {
       
    69     struct evfuse *ctx = NULL;
       
    70     int multithreaded, foreground;
       
    71     
       
    72     // allocate our context
       
    73     if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
       
    74         ERROR("calloc");
       
    75 
       
    76     // parse the commandline for the mountpoint
       
    77     if (fuse_parse_cmdline(args, &ctx->mountpoint, &multithreaded, &foreground) == -1)
       
    78         ERROR("fuse_parse_cmdline");
       
    79 
       
    80     // mount it
       
    81     if ((ctx->chan = fuse_mount(ctx->mountpoint, args)) == NULL)
       
    82         PERROR("fuse_mount_common");
       
    83 
       
    84     // the receive buffer stufff
       
    85     ctx->recv_size = fuse_chan_bufsize(ctx->chan);
       
    86 
       
    87     // allocate the recv buffer
       
    88     if ((ctx->recv_buf = malloc(ctx->recv_size)) == NULL)
       
    89         ERROR("malloc");
       
    90     
       
    91     // allocate a low-level session
       
    92     if ((ctx->session = fuse_lowlevel_new(args, llops, sizeof(*llops), cb_data)) == NULL)
       
    93         PERROR("fuse_lowlevel_new");
       
    94     
       
    95     // add the channel to the session
       
    96     // this isn't strictly necessary, but let's do it anyways
       
    97     fuse_session_add_chan(ctx->session, ctx->chan);
       
    98     
       
    99     // now, we can start listening for events on the channel
       
   100     if ((ctx->ev = event_new(evbase, fuse_chan_fd(ctx->chan), EV_READ, &_evfuse_ev_read, ctx)) == NULL)
       
   101         ERROR("event_new");
       
   102 
       
   103     if (event_add(ctx->ev, NULL))
       
   104         PERROR("event_add");
       
   105 
       
   106     // and then we wait
       
   107     return ctx;
       
   108 
       
   109 error:
       
   110     evfuse_free(ctx);
       
   111 
       
   112     return NULL;
       
   113 }
       
   114 
       
   115 void evfuse_close (struct evfuse *ctx) {
       
   116     if (ctx->ev) {
       
   117         // remove our event
       
   118         if (event_del(ctx->ev))
       
   119             PWARNING("event_del");
       
   120 
       
   121         ctx->ev = NULL;
       
   122     }
       
   123 
       
   124     if (ctx->session) {
       
   125         // remove the chan
       
   126         fuse_session_remove_chan(ctx->chan);
       
   127 
       
   128         // destroy the session
       
   129         fuse_session_destroy(ctx->session);
       
   130 
       
   131         ctx->session = NULL;
       
   132     }
       
   133 
       
   134     if (ctx->chan) {
       
   135         // unmount
       
   136         fuse_unmount(ctx->mountpoint, ctx->chan);
       
   137 
       
   138         ctx->chan = NULL;
       
   139     }
       
   140 
       
   141     // free
       
   142     free(ctx->recv_buf); ctx->recv_buf = NULL;
       
   143     free(ctx->mountpoint); ctx->mountpoint = NULL;
       
   144 }
       
   145 
       
   146 void evfuse_free (struct evfuse *ctx) {
       
   147     if (ctx) {
       
   148         evfuse_close(ctx);
       
   149 
       
   150         free(ctx);
       
   151     }
       
   152 }
       
   153