libfuse: fuse-3.13.1/example/hello_ll_uds.c Source File

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
42 static const char *hello_str = "Hello World!\n";
43 static const char *hello_name = "hello";
44
45 static 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
66 static 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
80 static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
81 {
82 struct fuse_entry_param e;
83
84 if (parent != 1 || strcmp(name, hello_name) != 0)
85 fuse_reply_err(req, ENOENT);
86 else {
87 memset(&e, 0, sizeof(e));
88 e.ino = 2;
89 e.attr_timeout = 1.0;
90 e.entry_timeout = 1.0;
91 hello_stat(e.ino, &e.attr);
92
93 fuse_reply_entry(req, &e);
94 }
95 }
96
97 struct dirbuf {
98 char *p;
99 size_t size;
100 };
101
102 static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
103 fuse_ino_t ino)
104 {
105 struct stat stbuf;
106 size_t oldsize = b->size;
107 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
108 b->p = (char *) realloc(b->p, b->size);
109 memset(&stbuf, 0, sizeof(stbuf));
110 stbuf.st_ino = ino;
111 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
112 b->size);
113 }
114
115 #define min(x, y) ((x) < (y) ? (x) : (y))
116
117 static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
118 off_t off, size_t maxsize)
119 {
120 if (off < bufsize)
121 return fuse_reply_buf(req, buf + off,
122 min(bufsize - off, maxsize));
123 else
124 return fuse_reply_buf(req, NULL, 0);
125 }
126
127 static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
128 off_t off, struct fuse_file_info *fi)
129 {
130 (void) fi;
131
132 if (ino != 1)
133 fuse_reply_err(req, ENOTDIR);
134 else {
135 struct dirbuf b;
136
137 memset(&b, 0, sizeof(b));
138 dirbuf_add(req, &b, ".", 1);
139 dirbuf_add(req, &b, "..", 1);
140 dirbuf_add(req, &b, hello_name, 2);
141 reply_buf_limited(req, b.p, b.size, off, size);
142 free(b.p);
143 }
144 }
145
146 static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
147 struct fuse_file_info *fi)
148 {
149 if (ino != 2)
150 fuse_reply_err(req, EISDIR);
151 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
152 fuse_reply_err(req, EACCES);
153 else
154 fuse_reply_open(req, fi);
155 }
156
157 static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
158 off_t off, struct fuse_file_info *fi)
159 {
160 (void) fi;
161
162 assert(ino == 2);
163 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
164 }
165
166 static const struct fuse_lowlevel_ops hello_ll_oper = {
167 .lookup = hello_ll_lookup,
168 .getattr = hello_ll_getattr,
169 .readdir = hello_ll_readdir,
170 .open = hello_ll_open,
171 .read = hello_ll_read,
172 };
173
174 static int create_socket(const char *socket_path) {
175 struct sockaddr_un addr;
176
177 if (strnlen(socket_path, sizeof(addr.sun_path)) >=
178 sizeof(addr.sun_path)) {
179 printf("Socket path may not be longer than %lu characters\n",
180 sizeof(addr.sun_path) - 1);
181 return -1;
182 }
183
184 if (remove(socket_path) == -1 && errno != ENOENT) {
185 printf("Could not delete previous socket file entry at %s. Error: "
186 "%s\n", socket_path, strerror(errno));
187 return -1;
188 }
189
190 memset(&addr, 0, sizeof(struct sockaddr_un));
191 strcpy(addr.sun_path, socket_path);
192
193 int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
194 if (sfd == -1) {
195 printf("Could not create socket. Error: %s\n", strerror(errno));
196 return -1;
197 }
198
199 addr.sun_family = AF_UNIX;
200 if (bind(sfd, (struct sockaddr *) &addr,
201 sizeof(struct sockaddr_un)) == -1) {
202 printf("Could not bind socket. Error: %s\n", strerror(errno));
203 return -1;
204 }
205
206 if (listen(sfd, 1) == -1)
207 return -1;
208
209 printf("Awaiting connection on socket at %s...\n", socket_path);
210 int cfd = accept(sfd, NULL, NULL);
211 if (cfd == -1) {
212 printf("Could not accept connection. Error: %s\n",
213 strerror(errno));
214 return -1;
215 } else {
216 printf("Accepted connection!\n");
217 }
218 return cfd;
219 }
220
221 static ssize_t stream_writev(int fd, struct iovec *iov, int count,
222 void *userdata) {
223 (void)userdata;
224
225 ssize_t written = 0;
226 int cur = 0;
227 for (;;) {
228 written = writev(fd, iov+cur, count-cur);
229 if (written < 0)
230 return written;
231
232 while (cur < count && written >= iov[cur].iov_len)
233 written -= iov[cur++].iov_len;
234 if (cur == count)
235 break;
236
237 iov[cur].iov_base = (char *)iov[cur].iov_base + written;
238 iov[cur].iov_len -= written;
239 }
240 return written;
241 }
242
243
244 static ssize_t readall(int fd, void *buf, size_t len) {
245 size_t count = 0;
246
247 while (count < len) {
248 int i = read(fd, (char *)buf + count, len - count);
249 if (!i)
250 break;
251
252 if (i < 0)
253 return i;
254
255 count += i;
256 }
257 return count;
258 }
259
260 static ssize_t stream_read(int fd, void *buf, size_t buf_len, void *userdata) {
261 (void)userdata;
262
263 int res = readall(fd, buf, sizeof(struct fuse_in_header));
264 if (res == -1)
265 return res;
266
267
268 uint32_t packet_len = ((struct fuse_in_header *)buf)->len;
269 if (packet_len > buf_len)
270 return -1;
271
272 int prev_res = res;
273
274 res = readall(fd, (char *)buf + sizeof(struct fuse_in_header),
275 packet_len - sizeof(struct fuse_in_header));
276
277 return (res == -1) ? res : (res + prev_res);
278 }
279
280 static ssize_t stream_splice_send(int fdin, off_t *offin, int fdout,
281 off_t *offout, size_t len,
282 unsigned int flags, void *userdata) {
283 (void)userdata;
284
285 size_t count = 0;
286 while (count < len) {
287 int i = splice(fdin, offin, fdout, offout, len - count, flags);
288 if (i < 1)
289 return i;
290
291 count += i;
292 }
293 return count;
294 }
295
296 static void fuse_cmdline_help_uds(void)
297 {
298 printf(" -h --help print help\n"
299 " -V --version print version\n"
300 " -d -o debug enable debug output (implies -f)\n");
301 }
302
303 int main(int argc, char *argv[])
304 {
305 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
306 struct fuse_session *se;
307 struct fuse_cmdline_opts opts;
308 const struct fuse_custom_io io = {
309 .writev = stream_writev,
310 .read = stream_read,
311 .splice_receive = NULL,
312 .splice_send = stream_splice_send,
313 };
314 int cfd = -1;
315 int ret = -1;
316
317 if (fuse_parse_cmdline(&args, &opts) != 0)
318 return 1;
319 if (opts.show_help) {
320 printf("usage: %s [options]\n\n", argv[0]);
321 fuse_cmdline_help_uds();
322 fuse_lowlevel_help();
323 ret = 0;
324 goto err_out1;
325 } else if (opts.show_version) {
326 printf("FUSE library version %s\n", fuse_pkgversion());
327 fuse_lowlevel_version();
328 ret = 0;
329 goto err_out1;
330 }
331
332 se = fuse_session_new(&args, &hello_ll_oper,
333 sizeof(hello_ll_oper), NULL);
334 if (se == NULL)
335 goto err_out1;
336
337 if (fuse_set_signal_handlers(se) != 0)
338 goto err_out2;
339
340 cfd = create_socket("/tmp/libfuse-hello-ll.sock");
341 if (cfd == -1)
342 goto err_out3;
343
344 if (fuse_session_custom_io(se, &io, cfd) != 0)
345 goto err_out3;
346
347 /* Block until ctrl+c */
348 ret = fuse_session_loop(se);
349 err_out3:
350 fuse_remove_signal_handlers(se);
351 err_out2:
352 fuse_session_destroy(se);
353 err_out1:
354 free(opts.mountpoint);
355 fuse_opt_free_args(&args);
356
357 return ret ? 1 : 0;
358 }
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:62
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition: fuse.c:5106
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:79
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition: fuse_lowlevel.c:2759
fuse_reply_open
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
Definition: fuse_lowlevel.c:462
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition: fuse_lowlevel.c:319
fuse_reply_buf
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
Definition: fuse_lowlevel.c:481
fuse_req_t
struct fuse_req * fuse_req_t
Definition: fuse_lowlevel.h:49
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition: fuse_loop.c:19
fuse_reply_entry
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
Definition: fuse_lowlevel.c:410
fuse_lowlevel_help
void fuse_lowlevel_help(void)
Definition: fuse_lowlevel.c:2749
fuse_session_new
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
Definition: fuse_lowlevel.c:2960
fuse_session_custom_io
int fuse_session_custom_io(struct fuse_session *se, const struct fuse_custom_io *io, int fd)
Definition: fuse_lowlevel.c:3056
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition: fuse_lowlevel.c:2742
fuse_ino_t
uint64_t fuse_ino_t
Definition: fuse_lowlevel.h:46
fuse_add_direntry
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
Definition: fuse_lowlevel.c:274
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition: fuse_lowlevel.c:442
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition: fuse_opt.c:34
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition: fuse_opt.h:123
fuse_args
Definition: fuse_opt.h:109
fuse_args::argc
int argc
Definition: fuse_opt.h:111
fuse_args::argv
char ** argv
Definition: fuse_opt.h:114
fuse_cmdline_opts
Definition: fuse_lowlevel.h:1927
fuse_entry_param
Definition: fuse_lowlevel.h:59
fuse_file_info
Definition: fuse_common.h:44
fuse_file_info::flags
int flags
Definition: fuse_common.h:46
fuse_lowlevel_ops
Definition: fuse_lowlevel.h:192
fuse_lowlevel_ops::lookup
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)
Definition: fuse_lowlevel.h:235