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)
80static void hello_ll_init(
void *userdata,
struct fuse_conn_info *conn)
92 if (parent != 1 || strcmp(name, hello_name) != 0)
95 memset(&e, 0,
sizeof(e));
98 e.entry_timeout = 1.0;
99 hello_stat(e.ino, &e.attr);
110static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
114 size_t oldsize = b->size;
116 b->p = (
char *) realloc(b->p, b->size);
117 memset(&stbuf, 0,
sizeof(stbuf));
123#define min(x, y) ((x) < (y) ? (x) : (y))
125static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
126 off_t off,
size_t maxsize)
130 min(bufsize - off, maxsize));
145 memset(&b, 0,
sizeof(b));
146 dirbuf_add(req, &b,
".", 1);
147 dirbuf_add(req, &b,
"..", 1);
148 dirbuf_add(req, &b, hello_name, 2);
149 reply_buf_limited(req, b.p, b.size, off, size);
159 else if ((fi->
flags & O_ACCMODE) != O_RDONLY)
171 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
175 .
init = hello_ll_init,
176 .lookup = hello_ll_lookup,
177 .getattr = hello_ll_getattr,
178 .readdir = hello_ll_readdir,
179 .open = hello_ll_open,
180 .read = hello_ll_read,
183static int create_socket(
const char *socket_path) {
184 struct sockaddr_un addr;
186 if (strnlen(socket_path,
sizeof(addr.sun_path)) >=
187 sizeof(addr.sun_path)) {
188 printf(
"Socket path may not be longer than %zu characters\n",
189 sizeof(addr.sun_path) - 1);
193 if (remove(socket_path) == -1 && errno != ENOENT) {
194 printf(
"Could not delete previous socket file entry at %s. Error: "
195 "%s\n", socket_path, strerror(errno));
199 memset(&addr, 0,
sizeof(
struct sockaddr_un));
200 strcpy(addr.sun_path, socket_path);
202 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
204 printf(
"Could not create socket. Error: %s\n", strerror(errno));
208 addr.sun_family = AF_UNIX;
209 if (bind(sfd, (
struct sockaddr *) &addr,
210 sizeof(
struct sockaddr_un)) == -1) {
211 printf(
"Could not bind socket. Error: %s\n", strerror(errno));
215 if (listen(sfd, 1) == -1)
218 printf(
"Awaiting connection on socket at %s...\n", socket_path);
219 int cfd = accept(sfd, NULL, NULL);
221 printf(
"Could not accept connection. Error: %s\n",
225 printf(
"Accepted connection!\n");
230static ssize_t stream_writev(
int fd,
struct iovec *iov,
int count,
237 written = writev(fd, iov+cur, count-cur);
241 while (cur < count && written >= iov[cur].iov_len)
242 written -= iov[cur++].iov_len;
246 iov[cur].iov_base = (
char *)iov[cur].iov_base + written;
247 iov[cur].iov_len -= written;
253static ssize_t readall(
int fd,
void *buf,
size_t len) {
256 while (count < len) {
257 int i = read(fd, (
char *)buf + count, len - count);
269static ssize_t stream_read(
int fd,
void *buf,
size_t buf_len,
void *userdata) {
272 int res = readall(fd, buf,
sizeof(
struct fuse_in_header));
277 uint32_t packet_len = ((
struct fuse_in_header *)buf)->len;
278 if (packet_len > buf_len)
283 res = readall(fd, (
char *)buf +
sizeof(
struct fuse_in_header),
284 packet_len -
sizeof(
struct fuse_in_header));
286 return (res == -1) ? res : (res + prev_res);
289static ssize_t stream_splice_send(
int fdin, off_t *offin,
int fdout,
290 off_t *offout,
size_t len,
291 unsigned int flags,
void *userdata) {
295 while (count < len) {
296 int i = splice(fdin, offin, fdout, offout, len - count, flags);
305static void fuse_cmdline_help_uds(
void)
307 printf(
" -h --help print help\n"
308 " -V --version print version\n"
309 " -d -o debug enable debug output (implies -f)\n");
312int main(
int argc,
char *argv[])
315 struct fuse_session *se;
317 const struct fuse_custom_io io = {
318 .writev = stream_writev,
320 .splice_receive = NULL,
321 .splice_send = stream_splice_send,
326 if (fuse_parse_cmdline(&args, &opts) != 0)
328 if (opts.show_help) {
329 printf(
"usage: %s [options]\n\n", argv[0]);
330 fuse_cmdline_help_uds();
334 }
else if (opts.show_version) {
341 se = fuse_session_new(&args, &hello_ll_oper,
342 sizeof(hello_ll_oper), NULL);
349 cfd = create_socket(
"/tmp/libfuse-hello-ll.sock");
353 if (fuse_session_custom_io(se, &io, cfd) != 0)
363 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)
void fuse_lowlevel_help(void)
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(* init)(void *userdata, struct fuse_conn_info *conn)