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";
45#define DEFAULT_SOCKET_PATH "/tmp/libfuse-hello-ll.sock"
47static struct options {
48 const char *socket_path;
52 { t, offsetof(struct options, p), 1 }
53static const struct fuse_opt option_spec[] = {
54 OPTION(
"--socket=%s", socket_path),
58static int hello_stat(
fuse_ino_t ino,
struct stat *stbuf)
63 stbuf->st_mode = S_IFDIR | 0755;
68 stbuf->st_mode = S_IFREG | 0444;
70 stbuf->st_size = strlen(hello_str);
86 memset(&stbuf, 0,
sizeof(stbuf));
87 if (hello_stat(ino, &stbuf) == -1)
93static void hello_ll_init(
void *userdata,
struct fuse_conn_info *conn)
105 if (parent != 1 || strcmp(name, hello_name) != 0)
108 memset(&e, 0,
sizeof(e));
110 e.attr_timeout = 1.0;
111 e.entry_timeout = 1.0;
112 hello_stat(e.ino, &e.attr);
123static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
127 size_t oldsize = b->size;
129 b->p = (
char *) realloc(b->p, b->size);
130 memset(&stbuf, 0,
sizeof(stbuf));
136#define min(x, y) ((x) < (y) ? (x) : (y))
138static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
139 off_t off,
size_t maxsize)
143 min(bufsize - off, maxsize));
158 memset(&b, 0,
sizeof(b));
159 dirbuf_add(req, &b,
".", 1);
160 dirbuf_add(req, &b,
"..", 1);
161 dirbuf_add(req, &b, hello_name, 2);
162 reply_buf_limited(req, b.p, b.size, off, size);
172 else if ((fi->
flags & O_ACCMODE) != O_RDONLY)
184 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
188 .init = hello_ll_init,
189 .lookup = hello_ll_lookup,
190 .getattr = hello_ll_getattr,
191 .readdir = hello_ll_readdir,
192 .open = hello_ll_open,
193 .read = hello_ll_read,
196static int create_socket(
const char *socket_path) {
197 struct sockaddr_un addr;
199 if (strnlen(socket_path,
sizeof(addr.sun_path)) >=
200 sizeof(addr.sun_path)) {
201 printf(
"Socket path may not be longer than %zu characters\n",
202 sizeof(addr.sun_path) - 1);
206 if (remove(socket_path) == -1 && errno != ENOENT) {
207 printf(
"Could not delete previous socket file entry at %s. Error: "
208 "%s\n", socket_path, strerror(errno));
212 memset(&addr, 0,
sizeof(
struct sockaddr_un));
213 strcpy(addr.sun_path, socket_path);
215 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
217 printf(
"Could not create socket. Error: %s\n", strerror(errno));
221 addr.sun_family = AF_UNIX;
222 if (bind(sfd, (
struct sockaddr *) &addr,
223 sizeof(
struct sockaddr_un)) == -1) {
224 printf(
"Could not bind socket. Error: %s\n", strerror(errno));
228 if (listen(sfd, 1) == -1)
231 printf(
"Awaiting connection on socket at %s...\n", socket_path);
232 int cfd = accept(sfd, NULL, NULL);
234 printf(
"Could not accept connection. Error: %s\n",
238 printf(
"Accepted connection!\n");
243static ssize_t stream_writev(
int fd,
struct iovec *iov,
int count,
250 written = writev(fd, iov+cur, count-cur);
254 while (cur < count && written >= iov[cur].iov_len)
255 written -= iov[cur++].iov_len;
259 iov[cur].iov_base = (
char *)iov[cur].iov_base + written;
260 iov[cur].iov_len -= written;
266static ssize_t readall(
int fd,
void *buf,
size_t len) {
269 while (count < len) {
270 int i = read(fd, (
char *)buf + count, len - count);
282static ssize_t stream_read(
int fd,
void *buf,
size_t buf_len,
void *userdata) {
285 int res = readall(fd, buf,
sizeof(
struct fuse_in_header));
290 uint32_t packet_len = ((
struct fuse_in_header *)buf)->len;
291 if (packet_len > buf_len)
296 res = readall(fd, (
char *)buf +
sizeof(
struct fuse_in_header),
297 packet_len -
sizeof(
struct fuse_in_header));
299 return (res == -1) ? res : (res + prev_res);
302static ssize_t stream_splice_send(
int fdin, off_t *offin,
int fdout,
303 off_t *offout,
size_t len,
304 unsigned int flags,
void *userdata) {
308 while (count < len) {
309 int i = splice(fdin, offin, fdout, offout, len - count, flags);
318static void fuse_cmdline_help_uds(
void)
320 printf(
" -h --help print help\n"
321 " -V --version print version\n"
322 " -d -o debug enable debug output (implies -f)\n"
323 " --socket=PATH serve on PATH\n"
324 " (default: " DEFAULT_SOCKET_PATH
")\n");
327int main(
int argc,
char *argv[])
330 struct fuse_session *se;
332 const struct fuse_custom_io io = {
333 .writev = stream_writev,
335 .splice_receive = NULL,
336 .splice_send = stream_splice_send,
342 options.socket_path = strdup(DEFAULT_SOCKET_PATH);
343 if (options.socket_path == NULL)
348 if (fuse_parse_cmdline(&args, &opts) != 0)
350 if (opts.show_help) {
351 printf(
"usage: %s [options]\n\n", argv[0]);
352 fuse_cmdline_help_uds();
356 }
else if (opts.show_version) {
363 se = fuse_session_new(&args, &hello_ll_oper,
364 sizeof(hello_ll_oper), NULL);
371 cfd = create_socket(options.socket_path);
375 if (fuse_session_custom_io(se, &io, cfd) != 0)
385 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)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
#define FUSE_ARGS_INIT(argc, argv)