terom@1: terom@1: #include terom@1: #include terom@1: #include terom@0: terom@0: #include "evfuse.h" terom@6: #include "lib/log.h" terom@0: terom@0: struct evfuse { terom@3: // our mountpoint terom@3: char *mountpoint; terom@3: terom@0: // the /dev/fuse fd/channel that we get from fuse_mount terom@0: struct fuse_chan *chan; terom@0: terom@0: // the session that we use to process the fuse stuff terom@0: struct fuse_session *session; terom@0: terom@0: // the event that we use to receive requests terom@0: struct event *ev; terom@0: terom@0: // what our receive-message length is terom@0: size_t recv_size; terom@0: terom@0: // the buffer that we use to receive events terom@0: char *recv_buf; terom@0: }; terom@0: terom@0: static void _evfuse_ev_read (evutil_socket_t fd, short what, void *arg) { terom@0: struct evfuse *ctx = arg; terom@1: struct fuse_chan *ch = ctx->chan; terom@0: int res; terom@0: terom@0: // loop until we complete a recv terom@0: do { terom@0: // a new fuse_req is available terom@0: res = fuse_chan_recv(&ch, ctx->recv_buf, ctx->recv_size); terom@0: } while (res == -EINTR); terom@0: terom@0: if (res == 0) terom@0: ERROR("fuse_chan_recv gave EOF"); terom@0: terom@0: if (res < 0 && res != -EAGAIN) terom@0: ERROR("fuse_chan_recv failed: %s", strerror(-res)); terom@0: terom@0: if (res > 0) { terom@1: INFO("[evfuse] got %d bytes from /dev/fuse", res); terom@1: terom@0: // received a fuse_req, so process it terom@0: fuse_session_process(ctx->session, ctx->recv_buf, res, ch); terom@0: } terom@0: terom@0: // reschedule terom@0: if (event_add(ctx->ev, NULL)) terom@0: PERROR("event_add"); terom@0: terom@0: // ok, wait for the next event terom@0: return; terom@0: terom@0: error: terom@6: // fail terom@6: evfuse_close(ctx); terom@0: } terom@0: terom@0: struct evfuse *evfuse_new (struct event_base *evbase, struct fuse_args *args, struct fuse_lowlevel_ops *llops, void *cb_data) { terom@0: struct evfuse *ctx = NULL; terom@0: int multithreaded, foreground; terom@0: terom@0: // allocate our context terom@1: if ((ctx = calloc(1, sizeof(*ctx))) == NULL) terom@0: ERROR("calloc"); terom@0: terom@0: // parse the commandline for the mountpoint terom@3: if (fuse_parse_cmdline(args, &ctx->mountpoint, &multithreaded, &foreground) == -1) terom@0: ERROR("fuse_parse_cmdline"); terom@0: terom@0: // mount it terom@3: if ((ctx->chan = fuse_mount(ctx->mountpoint, args)) == NULL) terom@0: PERROR("fuse_mount_common"); terom@0: terom@0: // the receive buffer stufff terom@0: ctx->recv_size = fuse_chan_bufsize(ctx->chan); terom@0: terom@0: // allocate the recv buffer terom@0: if ((ctx->recv_buf = malloc(ctx->recv_size)) == NULL) terom@0: ERROR("malloc"); terom@0: terom@0: // allocate a low-level session terom@0: if ((ctx->session = fuse_lowlevel_new(args, llops, sizeof(*llops), cb_data)) == NULL) terom@0: PERROR("fuse_lowlevel_new"); terom@0: terom@0: // add the channel to the session terom@0: // this isn't strictly necessary, but let's do it anyways terom@0: fuse_session_add_chan(ctx->session, ctx->chan); terom@0: terom@0: // now, we can start listening for events on the channel terom@0: if ((ctx->ev = event_new(evbase, fuse_chan_fd(ctx->chan), EV_READ, &_evfuse_ev_read, ctx)) == NULL) terom@0: ERROR("event_new"); terom@0: terom@0: if (event_add(ctx->ev, NULL)) terom@0: PERROR("event_add"); terom@0: terom@0: // and then we wait terom@1: return ctx; terom@0: terom@0: error: terom@0: free(ctx); terom@0: terom@0: return NULL; terom@0: } terom@3: terom@3: void evfuse_close (struct evfuse *ctx) { terom@3: // remove our event terom@3: if (event_del(ctx->ev)) terom@3: PWARNING("event_del"); terom@3: terom@3: // remove the chan terom@3: fuse_session_remove_chan(ctx->chan); terom@3: terom@3: // destroy the session terom@3: fuse_session_destroy(ctx->session); terom@3: terom@3: // unmount terom@3: fuse_unmount(ctx->mountpoint, ctx->chan); terom@3: terom@3: // free terom@3: free(ctx->recv_buf); terom@3: free(ctx->mountpoint); terom@3: free(ctx); terom@3: } terom@3: