23#define FUSE_USE_VERSION 34
31#include <fuse_kernel.h>
39#include <sys/socket.h>
42static const char *hello_str =
"Hello World!\n";
43static const char *hello_name =
"hello";
45static int hello_stat(
fuse_ino_t ino,
struct stat *stbuf)
50 stbuf->st_mode = S_IFDIR | 0755;
55 stbuf->st_mode = S_IFREG | 0444;
57 stbuf->st_size = strlen(hello_str);
73 memset(&stbuf, 0,
sizeof(stbuf));
74 if (hello_stat(ino, &stbuf) == -1)
84 if (parent != 1 || strcmp(name, hello_name) != 0)
87 memset(&e, 0,
sizeof(e));
90 e.entry_timeout = 1.0;
91 hello_stat(e.ino, &e.attr);
102static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
106 size_t oldsize = b->size;
108 b->p = (
char *) realloc(b->p, b->size);
109 memset(&stbuf, 0,
sizeof(stbuf));
115#define min(x, y) ((x) < (y) ? (x) : (y))
117static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
118 off_t off,
size_t maxsize)
122 min(bufsize - off, maxsize));
137 memset(&b, 0,
sizeof(b));
138 dirbuf_add(req, &b,
".", 1);
139 dirbuf_add(req, &b,
"..", 1);
140 dirbuf_add(req, &b, hello_name, 2);
141 reply_buf_limited(req, b.p, b.size, off, size);
151 else if ((fi->
flags & O_ACCMODE) != O_RDONLY)
163 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
167 .
lookup = hello_ll_lookup,
168 .getattr = hello_ll_getattr,
169 .readdir = hello_ll_readdir,
170 .open = hello_ll_open,
171 .read = hello_ll_read,
174static int create_socket(
const char *socket_path) {
175 struct sockaddr_un addr;
177 if (strnlen(socket_path,
sizeof(addr.sun_path)) >=
178 sizeof(addr.sun_path)) {
179 printf(
"Socket path may not be longer than %lu characters\n",
180 sizeof(addr.sun_path) - 1);
184 if (remove(socket_path) == -1 && errno != ENOENT) {
185 printf(
"Could not delete previous socket file entry at %s. Error: "
186 "%s\n", socket_path, strerror(errno));
190 memset(&addr, 0,
sizeof(
struct sockaddr_un));
191 strcpy(addr.sun_path, socket_path);
193 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
195 printf(
"Could not create socket. Error: %s\n", strerror(errno));
199 addr.sun_family = AF_UNIX;
200 if (bind(sfd, (
struct sockaddr *) &addr,
201 sizeof(
struct sockaddr_un)) == -1) {
202 printf(
"Could not bind socket. Error: %s\n", strerror(errno));
206 if (listen(sfd, 1) == -1)
209 printf(
"Awaiting connection on socket at %s...\n", socket_path);
210 int cfd = accept(sfd, NULL, NULL);
212 printf(
"Could not accept connection. Error: %s\n",
216 printf(
"Accepted connection!\n");
221static ssize_t stream_writev(
int fd,
struct iovec *iov,
int count,
228 written = writev(fd, iov+cur, count-cur);
232 while (cur < count && written >= iov[cur].iov_len)
233 written -= iov[cur++].iov_len;
237 iov[cur].iov_base = (
char *)iov[cur].iov_base + written;
238 iov[cur].iov_len -= written;
244static ssize_t readall(
int fd,
void *buf,
size_t len) {
247 while (count < len) {
248 int i = read(fd, (
char *)buf + count, len - count);
260static ssize_t stream_read(
int fd,
void *buf,
size_t buf_len,
void *userdata) {
263 int res = readall(fd, buf,
sizeof(
struct fuse_in_header));
268 uint32_t packet_len = ((
struct fuse_in_header *)buf)->len;
269 if (packet_len > buf_len)
274 res = readall(fd, (
char *)buf +
sizeof(
struct fuse_in_header),
275 packet_len -
sizeof(
struct fuse_in_header));
277 return (res == -1) ? res : (res + prev_res);
280static ssize_t stream_splice_send(
int fdin, off_t *offin,
int fdout,
281 off_t *offout,
size_t len,
282 unsigned int flags,
void *userdata) {
286 while (count < len) {
287 int i = splice(fdin, offin, fdout, offout, len - count, flags);
296static void fuse_cmdline_help_uds(
void)
298 printf(
" -h --help print help\n"
299 " -V --version print version\n"
300 " -d -o debug enable debug output (implies -f)\n");
303int main(
int argc,
char *argv[])
306 struct fuse_session *se;
308 const struct fuse_custom_io io = {
309 .writev = stream_writev,
311 .splice_receive = NULL,
312 .splice_send = stream_splice_send,
317 if (fuse_parse_cmdline(&args, &opts) != 0)
319 if (opts.show_help) {
320 printf(
"usage: %s [options]\n\n", argv[0]);
321 fuse_cmdline_help_uds();
325 }
else if (opts.show_version) {
333 sizeof(hello_ll_oper), NULL);
340 cfd = create_socket(
"/tmp/libfuse-hello-ll.sock");
354 free(opts.mountpoint);
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
void fuse_remove_signal_handlers(struct fuse_session *se)
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
int fuse_reply_err(fuse_req_t req, int err)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
struct fuse_req * fuse_req_t
int fuse_session_loop(struct fuse_session *se)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
void fuse_lowlevel_help(void)
int fuse_session_custom_io(struct fuse_session *se, const struct fuse_custom_io *io, int fd)
void fuse_lowlevel_version(void)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
void fuse_opt_free_args(struct fuse_args *args)
#define FUSE_ARGS_INIT(argc, argv)
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)