16881f68fSopenharmony_ci/*
26881f68fSopenharmony_ci  FUSE: Filesystem in Userspace
36881f68fSopenharmony_ci  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
46881f68fSopenharmony_ci  Copyright (C) 2022  Tofik Sonono <tofik.sonono@intel.com>
56881f68fSopenharmony_ci
66881f68fSopenharmony_ci  This program can be distributed under the terms of the GNU GPLv2.
76881f68fSopenharmony_ci  See the file COPYING.
86881f68fSopenharmony_ci*/
96881f68fSopenharmony_ci
106881f68fSopenharmony_ci/** @file
116881f68fSopenharmony_ci *
126881f68fSopenharmony_ci * minimal example filesystem using low-level API and a custom io. This custom
136881f68fSopenharmony_ci * io is implemented using UNIX domain sockets (of type SOCK_STREAM)
146881f68fSopenharmony_ci *
156881f68fSopenharmony_ci * Compile with:
166881f68fSopenharmony_ci *
176881f68fSopenharmony_ci *     gcc -Wall hello_ll_uds.c `pkg-config fuse3 --cflags --libs` -o hello_ll_uds
186881f68fSopenharmony_ci *
196881f68fSopenharmony_ci * ## Source code ##
206881f68fSopenharmony_ci * \include hello_ll.c
216881f68fSopenharmony_ci */
226881f68fSopenharmony_ci
236881f68fSopenharmony_ci#define FUSE_USE_VERSION 34
246881f68fSopenharmony_ci
256881f68fSopenharmony_ci
266881f68fSopenharmony_ci#ifndef _GNU_SOURCE
276881f68fSopenharmony_ci#define _GNU_SOURCE
286881f68fSopenharmony_ci#endif
296881f68fSopenharmony_ci
306881f68fSopenharmony_ci#include <fuse_lowlevel.h>
316881f68fSopenharmony_ci#include <fuse_kernel.h>
326881f68fSopenharmony_ci#include <stdio.h>
336881f68fSopenharmony_ci#include <stdlib.h>
346881f68fSopenharmony_ci#include <string.h>
356881f68fSopenharmony_ci#include <errno.h>
366881f68fSopenharmony_ci#include <fcntl.h>
376881f68fSopenharmony_ci#include <unistd.h>
386881f68fSopenharmony_ci#include <assert.h>
396881f68fSopenharmony_ci#include <sys/socket.h>
406881f68fSopenharmony_ci#include <sys/un.h>
416881f68fSopenharmony_ci
426881f68fSopenharmony_cistatic const char *hello_str = "Hello World!\n";
436881f68fSopenharmony_cistatic const char *hello_name = "hello";
446881f68fSopenharmony_ci
456881f68fSopenharmony_cistatic int hello_stat(fuse_ino_t ino, struct stat *stbuf)
466881f68fSopenharmony_ci{
476881f68fSopenharmony_ci	stbuf->st_ino = ino;
486881f68fSopenharmony_ci	switch (ino) {
496881f68fSopenharmony_ci	case 1:
506881f68fSopenharmony_ci		stbuf->st_mode = S_IFDIR | 0755;
516881f68fSopenharmony_ci		stbuf->st_nlink = 2;
526881f68fSopenharmony_ci		break;
536881f68fSopenharmony_ci
546881f68fSopenharmony_ci	case 2:
556881f68fSopenharmony_ci		stbuf->st_mode = S_IFREG | 0444;
566881f68fSopenharmony_ci		stbuf->st_nlink = 1;
576881f68fSopenharmony_ci		stbuf->st_size = strlen(hello_str);
586881f68fSopenharmony_ci		break;
596881f68fSopenharmony_ci
606881f68fSopenharmony_ci	default:
616881f68fSopenharmony_ci		return -1;
626881f68fSopenharmony_ci	}
636881f68fSopenharmony_ci	return 0;
646881f68fSopenharmony_ci}
656881f68fSopenharmony_ci
666881f68fSopenharmony_cistatic void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
676881f68fSopenharmony_ci			     struct fuse_file_info *fi)
686881f68fSopenharmony_ci{
696881f68fSopenharmony_ci	struct stat stbuf;
706881f68fSopenharmony_ci
716881f68fSopenharmony_ci	(void) fi;
726881f68fSopenharmony_ci
736881f68fSopenharmony_ci	memset(&stbuf, 0, sizeof(stbuf));
746881f68fSopenharmony_ci	if (hello_stat(ino, &stbuf) == -1)
756881f68fSopenharmony_ci		fuse_reply_err(req, ENOENT);
766881f68fSopenharmony_ci	else
776881f68fSopenharmony_ci		fuse_reply_attr(req, &stbuf, 1.0);
786881f68fSopenharmony_ci}
796881f68fSopenharmony_ci
806881f68fSopenharmony_cistatic void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
816881f68fSopenharmony_ci{
826881f68fSopenharmony_ci	struct fuse_entry_param e;
836881f68fSopenharmony_ci
846881f68fSopenharmony_ci	if (parent != 1 || strcmp(name, hello_name) != 0)
856881f68fSopenharmony_ci		fuse_reply_err(req, ENOENT);
866881f68fSopenharmony_ci	else {
876881f68fSopenharmony_ci		memset(&e, 0, sizeof(e));
886881f68fSopenharmony_ci		e.ino = 2;
896881f68fSopenharmony_ci		e.attr_timeout = 1.0;
906881f68fSopenharmony_ci		e.entry_timeout = 1.0;
916881f68fSopenharmony_ci		hello_stat(e.ino, &e.attr);
926881f68fSopenharmony_ci
936881f68fSopenharmony_ci		fuse_reply_entry(req, &e);
946881f68fSopenharmony_ci	}
956881f68fSopenharmony_ci}
966881f68fSopenharmony_ci
976881f68fSopenharmony_cistruct dirbuf {
986881f68fSopenharmony_ci	char *p;
996881f68fSopenharmony_ci	size_t size;
1006881f68fSopenharmony_ci};
1016881f68fSopenharmony_ci
1026881f68fSopenharmony_cistatic void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
1036881f68fSopenharmony_ci		       fuse_ino_t ino)
1046881f68fSopenharmony_ci{
1056881f68fSopenharmony_ci	struct stat stbuf;
1066881f68fSopenharmony_ci	size_t oldsize = b->size;
1076881f68fSopenharmony_ci	b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
1086881f68fSopenharmony_ci	b->p = (char *) realloc(b->p, b->size);
1096881f68fSopenharmony_ci	memset(&stbuf, 0, sizeof(stbuf));
1106881f68fSopenharmony_ci	stbuf.st_ino = ino;
1116881f68fSopenharmony_ci	fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
1126881f68fSopenharmony_ci			  b->size);
1136881f68fSopenharmony_ci}
1146881f68fSopenharmony_ci
1156881f68fSopenharmony_ci#define min(x, y) ((x) < (y) ? (x) : (y))
1166881f68fSopenharmony_ci
1176881f68fSopenharmony_cistatic int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
1186881f68fSopenharmony_ci			     off_t off, size_t maxsize)
1196881f68fSopenharmony_ci{
1206881f68fSopenharmony_ci	if (off < bufsize)
1216881f68fSopenharmony_ci		return fuse_reply_buf(req, buf + off,
1226881f68fSopenharmony_ci				      min(bufsize - off, maxsize));
1236881f68fSopenharmony_ci	else
1246881f68fSopenharmony_ci		return fuse_reply_buf(req, NULL, 0);
1256881f68fSopenharmony_ci}
1266881f68fSopenharmony_ci
1276881f68fSopenharmony_cistatic void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1286881f68fSopenharmony_ci			     off_t off, struct fuse_file_info *fi)
1296881f68fSopenharmony_ci{
1306881f68fSopenharmony_ci	(void) fi;
1316881f68fSopenharmony_ci
1326881f68fSopenharmony_ci	if (ino != 1)
1336881f68fSopenharmony_ci		fuse_reply_err(req, ENOTDIR);
1346881f68fSopenharmony_ci	else {
1356881f68fSopenharmony_ci		struct dirbuf b;
1366881f68fSopenharmony_ci
1376881f68fSopenharmony_ci		memset(&b, 0, sizeof(b));
1386881f68fSopenharmony_ci		dirbuf_add(req, &b, ".", 1);
1396881f68fSopenharmony_ci		dirbuf_add(req, &b, "..", 1);
1406881f68fSopenharmony_ci		dirbuf_add(req, &b, hello_name, 2);
1416881f68fSopenharmony_ci		reply_buf_limited(req, b.p, b.size, off, size);
1426881f68fSopenharmony_ci		free(b.p);
1436881f68fSopenharmony_ci	}
1446881f68fSopenharmony_ci}
1456881f68fSopenharmony_ci
1466881f68fSopenharmony_cistatic void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
1476881f68fSopenharmony_ci			  struct fuse_file_info *fi)
1486881f68fSopenharmony_ci{
1496881f68fSopenharmony_ci	if (ino != 2)
1506881f68fSopenharmony_ci		fuse_reply_err(req, EISDIR);
1516881f68fSopenharmony_ci	else if ((fi->flags & O_ACCMODE) != O_RDONLY)
1526881f68fSopenharmony_ci		fuse_reply_err(req, EACCES);
1536881f68fSopenharmony_ci	else
1546881f68fSopenharmony_ci		fuse_reply_open(req, fi);
1556881f68fSopenharmony_ci}
1566881f68fSopenharmony_ci
1576881f68fSopenharmony_cistatic void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
1586881f68fSopenharmony_ci			  off_t off, struct fuse_file_info *fi)
1596881f68fSopenharmony_ci{
1606881f68fSopenharmony_ci	(void) fi;
1616881f68fSopenharmony_ci
1626881f68fSopenharmony_ci	assert(ino == 2);
1636881f68fSopenharmony_ci	reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
1646881f68fSopenharmony_ci}
1656881f68fSopenharmony_ci
1666881f68fSopenharmony_cistatic const struct fuse_lowlevel_ops hello_ll_oper = {
1676881f68fSopenharmony_ci	.lookup		= hello_ll_lookup,
1686881f68fSopenharmony_ci	.getattr	= hello_ll_getattr,
1696881f68fSopenharmony_ci	.readdir	= hello_ll_readdir,
1706881f68fSopenharmony_ci	.open		= hello_ll_open,
1716881f68fSopenharmony_ci	.read		= hello_ll_read,
1726881f68fSopenharmony_ci};
1736881f68fSopenharmony_ci
1746881f68fSopenharmony_cistatic int create_socket(const char *socket_path) {
1756881f68fSopenharmony_ci	struct sockaddr_un addr;
1766881f68fSopenharmony_ci
1776881f68fSopenharmony_ci	if (strnlen(socket_path, sizeof(addr.sun_path)) >=
1786881f68fSopenharmony_ci		sizeof(addr.sun_path)) {
1796881f68fSopenharmony_ci		printf("Socket path may not be longer than %lu characters\n",
1806881f68fSopenharmony_ci			 sizeof(addr.sun_path) - 1);
1816881f68fSopenharmony_ci		return -1;
1826881f68fSopenharmony_ci	}
1836881f68fSopenharmony_ci
1846881f68fSopenharmony_ci	if (remove(socket_path) == -1 && errno != ENOENT) {
1856881f68fSopenharmony_ci		printf("Could not delete previous socket file entry at %s. Error: "
1866881f68fSopenharmony_ci			 "%s\n", socket_path, strerror(errno));
1876881f68fSopenharmony_ci		return -1;
1886881f68fSopenharmony_ci	}
1896881f68fSopenharmony_ci
1906881f68fSopenharmony_ci	memset(&addr, 0, sizeof(struct sockaddr_un));
1916881f68fSopenharmony_ci	strcpy(addr.sun_path, socket_path);
1926881f68fSopenharmony_ci
1936881f68fSopenharmony_ci	int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
1946881f68fSopenharmony_ci	if (sfd == -1) {
1956881f68fSopenharmony_ci		printf("Could not create socket. Error: %s\n", strerror(errno));
1966881f68fSopenharmony_ci		return -1;
1976881f68fSopenharmony_ci	}
1986881f68fSopenharmony_ci
1996881f68fSopenharmony_ci	addr.sun_family = AF_UNIX;
2006881f68fSopenharmony_ci	if (bind(sfd, (struct sockaddr *) &addr,
2016881f68fSopenharmony_ci		   sizeof(struct sockaddr_un)) == -1) {
2026881f68fSopenharmony_ci		printf("Could not bind socket. Error: %s\n", strerror(errno));
2036881f68fSopenharmony_ci		return -1;
2046881f68fSopenharmony_ci	}
2056881f68fSopenharmony_ci
2066881f68fSopenharmony_ci	if (listen(sfd, 1) == -1)
2076881f68fSopenharmony_ci		return -1;
2086881f68fSopenharmony_ci
2096881f68fSopenharmony_ci	printf("Awaiting connection on socket at %s...\n", socket_path);
2106881f68fSopenharmony_ci	int cfd = accept(sfd, NULL, NULL);
2116881f68fSopenharmony_ci	if (cfd == -1) {
2126881f68fSopenharmony_ci		printf("Could not accept connection. Error: %s\n",
2136881f68fSopenharmony_ci			 strerror(errno));
2146881f68fSopenharmony_ci		return -1;
2156881f68fSopenharmony_ci	} else {
2166881f68fSopenharmony_ci		printf("Accepted connection!\n");
2176881f68fSopenharmony_ci	}
2186881f68fSopenharmony_ci	return cfd;
2196881f68fSopenharmony_ci}
2206881f68fSopenharmony_ci
2216881f68fSopenharmony_cistatic ssize_t stream_writev(int fd, struct iovec *iov, int count,
2226881f68fSopenharmony_ci                             void *userdata) {
2236881f68fSopenharmony_ci	(void)userdata;
2246881f68fSopenharmony_ci
2256881f68fSopenharmony_ci	ssize_t written = 0;
2266881f68fSopenharmony_ci	int cur = 0;
2276881f68fSopenharmony_ci	for (;;) {
2286881f68fSopenharmony_ci		written = writev(fd, iov+cur, count-cur);
2296881f68fSopenharmony_ci		if (written < 0)
2306881f68fSopenharmony_ci			return written;
2316881f68fSopenharmony_ci
2326881f68fSopenharmony_ci		while (cur < count && written >= iov[cur].iov_len)
2336881f68fSopenharmony_ci			written -= iov[cur++].iov_len;
2346881f68fSopenharmony_ci		if (cur == count)
2356881f68fSopenharmony_ci			break;
2366881f68fSopenharmony_ci
2376881f68fSopenharmony_ci		iov[cur].iov_base = (char *)iov[cur].iov_base + written;
2386881f68fSopenharmony_ci		iov[cur].iov_len -= written;
2396881f68fSopenharmony_ci	}
2406881f68fSopenharmony_ci	return written;
2416881f68fSopenharmony_ci}
2426881f68fSopenharmony_ci
2436881f68fSopenharmony_ci
2446881f68fSopenharmony_cistatic ssize_t readall(int fd, void *buf, size_t len) {
2456881f68fSopenharmony_ci	size_t count = 0;
2466881f68fSopenharmony_ci
2476881f68fSopenharmony_ci	while (count < len) {
2486881f68fSopenharmony_ci		int i = read(fd, (char *)buf + count, len - count);
2496881f68fSopenharmony_ci		if (!i)
2506881f68fSopenharmony_ci			break;
2516881f68fSopenharmony_ci
2526881f68fSopenharmony_ci		if (i < 0)
2536881f68fSopenharmony_ci			return i;
2546881f68fSopenharmony_ci
2556881f68fSopenharmony_ci		count += i;
2566881f68fSopenharmony_ci	}
2576881f68fSopenharmony_ci	return count;
2586881f68fSopenharmony_ci}
2596881f68fSopenharmony_ci
2606881f68fSopenharmony_cistatic ssize_t stream_read(int fd, void *buf, size_t buf_len, void *userdata) {
2616881f68fSopenharmony_ci    (void)userdata;
2626881f68fSopenharmony_ci
2636881f68fSopenharmony_ci	int res = readall(fd, buf, sizeof(struct fuse_in_header));
2646881f68fSopenharmony_ci	if (res == -1)
2656881f68fSopenharmony_ci    	return res;
2666881f68fSopenharmony_ci
2676881f68fSopenharmony_ci
2686881f68fSopenharmony_ci    uint32_t packet_len = ((struct fuse_in_header *)buf)->len;
2696881f68fSopenharmony_ci    if (packet_len > buf_len)
2706881f68fSopenharmony_ci    	return -1;
2716881f68fSopenharmony_ci
2726881f68fSopenharmony_ci    int prev_res = res;
2736881f68fSopenharmony_ci
2746881f68fSopenharmony_ci    res = readall(fd, (char *)buf + sizeof(struct fuse_in_header),
2756881f68fSopenharmony_ci                  packet_len - sizeof(struct fuse_in_header));
2766881f68fSopenharmony_ci
2776881f68fSopenharmony_ci    return  (res == -1) ? res : (res + prev_res);
2786881f68fSopenharmony_ci}
2796881f68fSopenharmony_ci
2806881f68fSopenharmony_cistatic ssize_t stream_splice_send(int fdin, off_t *offin, int fdout,
2816881f68fSopenharmony_ci					    off_t *offout, size_t len,
2826881f68fSopenharmony_ci                                  unsigned int flags, void *userdata) {
2836881f68fSopenharmony_ci	(void)userdata;
2846881f68fSopenharmony_ci
2856881f68fSopenharmony_ci	size_t count = 0;
2866881f68fSopenharmony_ci	while (count < len) {
2876881f68fSopenharmony_ci		int i = splice(fdin, offin, fdout, offout, len - count, flags);
2886881f68fSopenharmony_ci		if (i < 1)
2896881f68fSopenharmony_ci			return i;
2906881f68fSopenharmony_ci
2916881f68fSopenharmony_ci		count += i;
2926881f68fSopenharmony_ci	}
2936881f68fSopenharmony_ci	return count;
2946881f68fSopenharmony_ci}
2956881f68fSopenharmony_ci
2966881f68fSopenharmony_cistatic void fuse_cmdline_help_uds(void)
2976881f68fSopenharmony_ci{
2986881f68fSopenharmony_ci	printf("    -h   --help            print help\n"
2996881f68fSopenharmony_ci	       "    -V   --version         print version\n"
3006881f68fSopenharmony_ci	       "    -d   -o debug          enable debug output (implies -f)\n");
3016881f68fSopenharmony_ci}
3026881f68fSopenharmony_ci
3036881f68fSopenharmony_ciint main(int argc, char *argv[])
3046881f68fSopenharmony_ci{
3056881f68fSopenharmony_ci	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
3066881f68fSopenharmony_ci	struct fuse_session *se;
3076881f68fSopenharmony_ci	struct fuse_cmdline_opts opts;
3086881f68fSopenharmony_ci	const struct fuse_custom_io io = {
3096881f68fSopenharmony_ci		.writev = stream_writev,
3106881f68fSopenharmony_ci		.read = stream_read,
3116881f68fSopenharmony_ci		.splice_receive = NULL,
3126881f68fSopenharmony_ci		.splice_send = stream_splice_send,
3136881f68fSopenharmony_ci	};
3146881f68fSopenharmony_ci	int cfd = -1;
3156881f68fSopenharmony_ci	int ret = -1;
3166881f68fSopenharmony_ci
3176881f68fSopenharmony_ci	if (fuse_parse_cmdline(&args, &opts) != 0)
3186881f68fSopenharmony_ci		return 1;
3196881f68fSopenharmony_ci	if (opts.show_help) {
3206881f68fSopenharmony_ci		printf("usage: %s [options]\n\n", argv[0]);
3216881f68fSopenharmony_ci		fuse_cmdline_help_uds();
3226881f68fSopenharmony_ci		fuse_lowlevel_help();
3236881f68fSopenharmony_ci		ret = 0;
3246881f68fSopenharmony_ci		goto err_out1;
3256881f68fSopenharmony_ci	} else if (opts.show_version) {
3266881f68fSopenharmony_ci		printf("FUSE library version %s\n", fuse_pkgversion());
3276881f68fSopenharmony_ci		fuse_lowlevel_version();
3286881f68fSopenharmony_ci		ret = 0;
3296881f68fSopenharmony_ci		goto err_out1;
3306881f68fSopenharmony_ci	}
3316881f68fSopenharmony_ci
3326881f68fSopenharmony_ci	se = fuse_session_new(&args, &hello_ll_oper,
3336881f68fSopenharmony_ci			      sizeof(hello_ll_oper), NULL);
3346881f68fSopenharmony_ci	if (se == NULL)
3356881f68fSopenharmony_ci	    goto err_out1;
3366881f68fSopenharmony_ci
3376881f68fSopenharmony_ci	if (fuse_set_signal_handlers(se) != 0)
3386881f68fSopenharmony_ci	    goto err_out2;
3396881f68fSopenharmony_ci
3406881f68fSopenharmony_ci	cfd = create_socket("/tmp/libfuse-hello-ll.sock");
3416881f68fSopenharmony_ci	if (cfd == -1)
3426881f68fSopenharmony_ci		goto err_out3;
3436881f68fSopenharmony_ci
3446881f68fSopenharmony_ci	if (fuse_session_custom_io(se, &io, cfd) != 0)
3456881f68fSopenharmony_ci		goto err_out3;
3466881f68fSopenharmony_ci
3476881f68fSopenharmony_ci	/* Block until ctrl+c */
3486881f68fSopenharmony_ci	ret = fuse_session_loop(se);
3496881f68fSopenharmony_cierr_out3:
3506881f68fSopenharmony_ci	fuse_remove_signal_handlers(se);
3516881f68fSopenharmony_cierr_out2:
3526881f68fSopenharmony_ci	fuse_session_destroy(se);
3536881f68fSopenharmony_cierr_out1:
3546881f68fSopenharmony_ci	free(opts.mountpoint);
3556881f68fSopenharmony_ci	fuse_opt_free_args(&args);
3566881f68fSopenharmony_ci
3576881f68fSopenharmony_ci	return ret ? 1 : 0;
3586881f68fSopenharmony_ci}
359