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 COPYING.
8*/
9
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
45static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
46{
47 stbuf->st_ino = ino;
48 switch (ino) {
49 case 1:
50 stbuf->st_mode = S_IFDIR | 0755;
51 stbuf->st_nlink = 2;
52 break;
53
54 case 2:
55 stbuf->st_mode = S_IFREG | 0444;
56 stbuf->st_nlink = 1;
57 stbuf->st_size = strlen(hello_str);
58 break;
59
60 default:
61 return -1;
62 }
63 return 0;
64}
65
66static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
67 struct fuse_file_info *fi)
68{
69 struct stat stbuf;
70
71 (void) fi;
72
73 memset(&stbuf, 0, sizeof(stbuf));
74 if (hello_stat(ino, &stbuf) == -1)
75 fuse_reply_err(req, ENOENT);
76 else
77 fuse_reply_attr(req, &stbuf, 1.0);
78}
79
80static void hello_ll_init(void *userdata, struct fuse_conn_info *conn)
81{
82 (void)userdata;
83
84 /* Disable the receiving and processing of FUSE_INTERRUPT requests */
85 conn->no_interrupt = 1;
86}
87
88static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
89{
90 struct fuse_entry_param e;
91
92 if (parent != 1 || strcmp(name, hello_name) != 0)
93 fuse_reply_err(req, ENOENT);
94 else {
95 memset(&e, 0, sizeof(e));
96 e.ino = 2;
97 e.attr_timeout = 1.0;
98 e.entry_timeout = 1.0;
99 hello_stat(e.ino, &e.attr);
100
101 fuse_reply_entry(req, &e);
102 }
103}
104
105struct dirbuf {
106 char *p;
107 size_t size;
108};
109
110static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
111 fuse_ino_t ino)
112{
113 struct stat stbuf;
114 size_t oldsize = b->size;
115 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
116 b->p = (char *) realloc(b->p, b->size);
117 memset(&stbuf, 0, sizeof(stbuf));
118 stbuf.st_ino = ino;
119 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
120 b->size);
121}
122
123#define min(x, y) ((x) < (y) ? (x) : (y))
124
125static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
126 off_t off, size_t maxsize)
127{
128 if (off < bufsize)
129 return fuse_reply_buf(req, buf + off,
130 min(bufsize - off, maxsize));
131 else
132 return fuse_reply_buf(req, NULL, 0);
133}
134
135static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
136 off_t off, struct fuse_file_info *fi)
137{
138 (void) fi;
139
140 if (ino != 1)
141 fuse_reply_err(req, ENOTDIR);
142 else {
143 struct dirbuf b;
144
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);
150 free(b.p);
151 }
152}
153
154static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
155 struct fuse_file_info *fi)
156{
157 if (ino != 2)
158 fuse_reply_err(req, EISDIR);
159 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
160 fuse_reply_err(req, EACCES);
161 else
162 fuse_reply_open(req, fi);
163}
164
165static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
166 off_t off, struct fuse_file_info *fi)
167{
168 (void) fi;
169
170 assert(ino == 2);
171 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
172}
173
174static const struct fuse_lowlevel_ops hello_ll_oper = {
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,
181};
182
183static int create_socket(const char *socket_path) {
184 struct sockaddr_un addr;
185
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);
190 return -1;
191 }
192
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));
196 return -1;
197 }
198
199 memset(&addr, 0, sizeof(struct sockaddr_un));
200 strcpy(addr.sun_path, socket_path);
201
202 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
203 if (sfd == -1) {
204 printf("Could not create socket. Error: %s\n", strerror(errno));
205 return -1;
206 }
207
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));
212 return -1;
213 }
214
215 if (listen(sfd, 1) == -1)
216 return -1;
217
218 printf("Awaiting connection on socket at %s...\n", socket_path);
219 int cfd = accept(sfd, NULL, NULL);
220 if (cfd == -1) {
221 printf("Could not accept connection. Error: %s\n",
222 strerror(errno));
223 return -1;
224 } else {
225 printf("Accepted connection!\n");
226 }
227 return cfd;
228}
229
230static ssize_t stream_writev(int fd, struct iovec *iov, int count,
231 void *userdata) {
232 (void)userdata;
233
234 ssize_t written = 0;
235 int cur = 0;
236 for (;;) {
237 written = writev(fd, iov+cur, count-cur);
238 if (written < 0)
239 return written;
240
241 while (cur < count && written >= iov[cur].iov_len)
242 written -= iov[cur++].iov_len;
243 if (cur == count)
244 break;
245
246 iov[cur].iov_base = (char *)iov[cur].iov_base + written;
247 iov[cur].iov_len -= written;
248 }
249 return written;
250}
251
252
253static ssize_t readall(int fd, void *buf, size_t len) {
254 size_t count = 0;
255
256 while (count < len) {
257 int i = read(fd, (char *)buf + count, len - count);
258 if (!i)
259 break;
260
261 if (i < 0)
262 return i;
263
264 count += i;
265 }
266 return count;
267}
268
269static ssize_t stream_read(int fd, void *buf, size_t buf_len, void *userdata) {
270 (void)userdata;
271
272 int res = readall(fd, buf, sizeof(struct fuse_in_header));
273 if (res == -1)
274 return res;
275
276
277 uint32_t packet_len = ((struct fuse_in_header *)buf)->len;
278 if (packet_len > buf_len)
279 return -1;
280
281 int prev_res = res;
282
283 res = readall(fd, (char *)buf + sizeof(struct fuse_in_header),
284 packet_len - sizeof(struct fuse_in_header));
285
286 return (res == -1) ? res : (res + prev_res);
287}
288
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) {
292 (void)userdata;
293
294 size_t count = 0;
295 while (count < len) {
296 int i = splice(fdin, offin, fdout, offout, len - count, flags);
297 if (i < 1)
298 return i;
299
300 count += i;
301 }
302 return count;
303}
304
305static void fuse_cmdline_help_uds(void)
306{
307 printf(" -h --help print help\n"
308 " -V --version print version\n"
309 " -d -o debug enable debug output (implies -f)\n");
310}
311
312int main(int argc, char *argv[])
313{
314 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
315 struct fuse_session *se;
316 struct fuse_cmdline_opts opts;
317 const struct fuse_custom_io io = {
318 .writev = stream_writev,
319 .read = stream_read,
320 .splice_receive = NULL,
321 .splice_send = stream_splice_send,
322 };
323 int cfd = -1;
324 int ret = -1;
325
326 if (fuse_parse_cmdline(&args, &opts) != 0)
327 return 1;
328 if (opts.show_help) {
329 printf("usage: %s [options]\n\n", argv[0]);
330 fuse_cmdline_help_uds();
332 ret = 0;
333 goto err_out1;
334 } else if (opts.show_version) {
335 printf("FUSE library version %s\n", fuse_pkgversion());
337 ret = 0;
338 goto err_out1;
339 }
340
341 se = fuse_session_new(&args, &hello_ll_oper,
342 sizeof(hello_ll_oper), NULL);
343 if (se == NULL)
344 goto err_out1;
345
346 if (fuse_set_signal_handlers(se) != 0)
347 goto err_out2;
348
349 cfd = create_socket("/tmp/libfuse-hello-ll.sock");
350 if (cfd == -1)
351 goto err_out3;
352
353 if (fuse_session_custom_io(se, &io, cfd) != 0)
354 goto err_out3;
355
356 /* Block until ctrl+c */
357 ret = fuse_session_loop(se);
358err_out3:
360err_out2:
362err_out1:
363 free(opts.mountpoint);
364 fuse_opt_free_args(&args);
365
366 return ret ? 1 : 0;
367}
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
Definition fuse.c:5211
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
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
char ** argv
Definition fuse_opt.h:114
uint32_t no_interrupt
void(* init)(void *userdata, struct fuse_conn_info *conn)