libfuse
hello_ll_uds.c
Go to the documentation of this file.
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4 Copyright (C) 2022 Tofik Sonono <tofik.sonono@intel.com>
5
6 This program can be distributed under the terms of the GNU GPLv2.
7 See the file GPL2.txt.
8*/
9
22
23#define FUSE_USE_VERSION 34
24
25
26#ifndef _GNU_SOURCE
27#define _GNU_SOURCE
28#endif
29
30#include <fuse_lowlevel.h>
31#include <fuse_kernel.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <assert.h>
39#include <sys/socket.h>
40#include <sys/un.h>
41
42static const char *hello_str = "Hello World!\n";
43static const char *hello_name = "hello";
44
45#define DEFAULT_SOCKET_PATH "/tmp/libfuse-hello-ll.sock"
46
47static struct options {
48 const char *socket_path;
49} options;
50
51#define OPTION(t, p) \
52 { t, offsetof(struct options, p), 1 }
53static const struct fuse_opt option_spec[] = {
54 OPTION("--socket=%s", socket_path),
56};
57
58static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
59{
60 stbuf->st_ino = ino;
61 switch (ino) {
62 case 1:
63 stbuf->st_mode = S_IFDIR | 0755;
64 stbuf->st_nlink = 2;
65 break;
66
67 case 2:
68 stbuf->st_mode = S_IFREG | 0444;
69 stbuf->st_nlink = 1;
70 stbuf->st_size = strlen(hello_str);
71 break;
72
73 default:
74 return -1;
75 }
76 return 0;
77}
78
79static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
80 struct fuse_file_info *fi)
81{
82 struct stat stbuf;
83
84 (void) fi;
85
86 memset(&stbuf, 0, sizeof(stbuf));
87 if (hello_stat(ino, &stbuf) == -1)
88 fuse_reply_err(req, ENOENT);
89 else
90 fuse_reply_attr(req, &stbuf, 1.0);
91}
92
93static void hello_ll_init(void *userdata, struct fuse_conn_info *conn)
94{
95 (void)userdata;
96
97 /* Disable the receiving and processing of FUSE_INTERRUPT requests */
98 conn->no_interrupt = 1;
99}
100
101static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
102{
103 struct fuse_entry_param e;
104
105 if (parent != 1 || strcmp(name, hello_name) != 0)
106 fuse_reply_err(req, ENOENT);
107 else {
108 memset(&e, 0, sizeof(e));
109 e.ino = 2;
110 e.attr_timeout = 1.0;
111 e.entry_timeout = 1.0;
112 hello_stat(e.ino, &e.attr);
113
114 fuse_reply_entry(req, &e);
115 }
116}
117
118struct dirbuf {
119 char *p;
120 size_t size;
121};
122
123static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
124 fuse_ino_t ino)
125{
126 struct stat stbuf;
127 size_t oldsize = b->size;
128 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
129 b->p = (char *) realloc(b->p, b->size);
130 memset(&stbuf, 0, sizeof(stbuf));
131 stbuf.st_ino = ino;
132 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
133 b->size);
134}
135
136#define min(x, y) ((x) < (y) ? (x) : (y))
137
138static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
139 off_t off, size_t maxsize)
140{
141 if (off < bufsize)
142 return fuse_reply_buf(req, buf + off,
143 min(bufsize - off, maxsize));
144 else
145 return fuse_reply_buf(req, NULL, 0);
146}
147
148static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
149 off_t off, struct fuse_file_info *fi)
150{
151 (void) fi;
152
153 if (ino != 1)
154 fuse_reply_err(req, ENOTDIR);
155 else {
156 struct dirbuf b;
157
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);
163 free(b.p);
164 }
165}
166
167static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
168 struct fuse_file_info *fi)
169{
170 if (ino != 2)
171 fuse_reply_err(req, EISDIR);
172 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
173 fuse_reply_err(req, EACCES);
174 else
175 fuse_reply_open(req, fi);
176}
177
178static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
179 off_t off, struct fuse_file_info *fi)
180{
181 (void) fi;
182
183 assert(ino == 2);
184 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
185}
186
187static const struct fuse_lowlevel_ops hello_ll_oper = {
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,
194};
195
196static int create_socket(const char *socket_path) {
197 struct sockaddr_un addr;
198
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);
203 return -1;
204 }
205
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));
209 return -1;
210 }
211
212 memset(&addr, 0, sizeof(struct sockaddr_un));
213 strcpy(addr.sun_path, socket_path);
214
215 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
216 if (sfd == -1) {
217 printf("Could not create socket. Error: %s\n", strerror(errno));
218 return -1;
219 }
220
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));
225 return -1;
226 }
227
228 if (listen(sfd, 1) == -1)
229 return -1;
230
231 printf("Awaiting connection on socket at %s...\n", socket_path);
232 int cfd = accept(sfd, NULL, NULL);
233 if (cfd == -1) {
234 printf("Could not accept connection. Error: %s\n",
235 strerror(errno));
236 return -1;
237 } else {
238 printf("Accepted connection!\n");
239 }
240 return cfd;
241}
242
243static ssize_t stream_writev(int fd, struct iovec *iov, int count,
244 void *userdata) {
245 (void)userdata;
246
247 ssize_t written = 0;
248 int cur = 0;
249 for (;;) {
250 written = writev(fd, iov+cur, count-cur);
251 if (written < 0)
252 return written;
253
254 while (cur < count && written >= iov[cur].iov_len)
255 written -= iov[cur++].iov_len;
256 if (cur == count)
257 break;
258
259 iov[cur].iov_base = (char *)iov[cur].iov_base + written;
260 iov[cur].iov_len -= written;
261 }
262 return written;
263}
264
265
266static ssize_t readall(int fd, void *buf, size_t len) {
267 size_t count = 0;
268
269 while (count < len) {
270 int i = read(fd, (char *)buf + count, len - count);
271 if (!i)
272 break;
273
274 if (i < 0)
275 return i;
276
277 count += i;
278 }
279 return count;
280}
281
282static ssize_t stream_read(int fd, void *buf, size_t buf_len, void *userdata) {
283 (void)userdata;
284
285 int res = readall(fd, buf, sizeof(struct fuse_in_header));
286 if (res == -1)
287 return res;
288
289
290 uint32_t packet_len = ((struct fuse_in_header *)buf)->len;
291 if (packet_len > buf_len)
292 return -1;
293
294 int prev_res = res;
295
296 res = readall(fd, (char *)buf + sizeof(struct fuse_in_header),
297 packet_len - sizeof(struct fuse_in_header));
298
299 return (res == -1) ? res : (res + prev_res);
300}
301
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) {
305 (void)userdata;
306
307 size_t count = 0;
308 while (count < len) {
309 int i = splice(fdin, offin, fdout, offout, len - count, flags);
310 if (i < 1)
311 return i;
312
313 count += i;
314 }
315 return count;
316}
317
318static void fuse_cmdline_help_uds(void)
319{
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");
325}
326
327int main(int argc, char *argv[])
328{
329 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
330 struct fuse_session *se;
331 struct fuse_cmdline_opts opts;
332 const struct fuse_custom_io io = {
333 .writev = stream_writev,
334 .read = stream_read,
335 .splice_receive = NULL,
336 .splice_send = stream_splice_send,
337 };
338 int cfd = -1;
339 int ret = -1;
340
341 /* strdup: fuse_opt_parse frees the old value if --socket is given */
342 options.socket_path = strdup(DEFAULT_SOCKET_PATH);
343 if (options.socket_path == NULL)
344 return 1;
345
346 if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
347 return 1;
348 if (fuse_parse_cmdline(&args, &opts) != 0)
349 return 1;
350 if (opts.show_help) {
351 printf("usage: %s [options]\n\n", argv[0]);
352 fuse_cmdline_help_uds();
354 ret = 0;
355 goto err_out1;
356 } else if (opts.show_version) {
357 printf("FUSE library version %s\n", fuse_pkgversion());
359 ret = 0;
360 goto err_out1;
361 }
362
363 se = fuse_session_new(&args, &hello_ll_oper,
364 sizeof(hello_ll_oper), NULL);
365 if (se == NULL)
366 goto err_out1;
367
368 if (fuse_set_signal_handlers(se) != 0)
369 goto err_out2;
370
371 cfd = create_socket(options.socket_path);
372 if (cfd == -1)
373 goto err_out3;
374
375 if (fuse_session_custom_io(se, &io, cfd) != 0)
376 goto err_out3;
377
378 /* Block until ctrl+c */
379 ret = fuse_session_loop(se);
380err_out3:
382err_out2:
384err_out1:
385 free(opts.mountpoint);
386 fuse_opt_free_args(&args);
387
388 return ret ? 1 : 0;
389}
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
Definition fuse.c:5274
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)
Definition fuse_loop.c:19
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
void fuse_lowlevel_help(void)
void fuse_lowlevel_version(void)
uint64_t fuse_ino_t
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)
Definition fuse_opt.c:34
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
Definition fuse_opt.c:398
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
#define FUSE_OPT_END
Definition fuse_opt.h:104
char ** argv
Definition fuse_opt.h:114
uint32_t no_interrupt