libfuse: fuse-3.18.2/example/notify_store_retrieve.c Source File

libfuse

notify_store_retrieve.c
Go to the documentation of this file.
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2016 Nikolaus Rath <Nikolaus@rath.org>
4
5 This program can be distributed under the terms of the GNU GPLv2.
6 See the file GPL2.txt.
7*/
8
61#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
62
63#include <fuse_lowlevel.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <errno.h>
68#include <fcntl.h>
69#include <assert.h>
70#include <stddef.h>
71#include <unistd.h>
72#include <pthread.h>
73#include <stdbool.h>
74
75/* We can't actually tell the kernel that there is no
76 timeout, so we just send a big value */
77#define NO_TIMEOUT 500000
78
79#define MAX_STR_LEN 128
80#define FILE_INO 2
81#define FILE_NAME "current_time"
82static char file_contents[MAX_STR_LEN];
83static int lookup_cnt = 0;
84static int open_cnt = 0;
85static size_t file_size;
86static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
87
88/* Keep track if we ever stored data (==1), and
89 received it back correctly (==2) */
90static int retrieve_status = 0;
91
92static bool is_umount = false;
93
94/* updater thread tid */
95static pthread_t updater;
96
97
98/* Command line parsing */
99struct options {
100 int no_notify;
101 int update_interval;
102};
103static struct options options = {
104 .no_notify = 0,
105 .update_interval = 1,
106};
107
108#define OPTION(t, p) \
109 { t, offsetof(struct options, p), 1 }
110static const struct fuse_opt option_spec[] = {
111 OPTION("--no-notify", no_notify),
112 OPTION("--update-interval=%d", update_interval),
113 FUSE_OPT_END
114};
115
116static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) {
117 stbuf->st_ino = ino;
118 if (ino == FUSE_ROOT_ID) {
119 stbuf->st_mode = S_IFDIR | 0755;
120 stbuf->st_nlink = 1;
121 }
122
123 else if (ino == FILE_INO) {
124 stbuf->st_mode = S_IFREG | 0444;
125 stbuf->st_nlink = 1;
126 stbuf->st_size = file_size;
127 }
128
129 else
130 return -1;
131
132 return 0;
133}
134
135static void tfs_init(void *userdata, struct fuse_conn_info *conn) {
136 (void)userdata;
137
138 /* Disable the receiving and processing of FUSE_INTERRUPT requests */
139 conn->no_interrupt = 1;
140}
141
142static void tfs_lookup(fuse_req_t req, fuse_ino_t parent,
143 const char *name) {
144 struct fuse_entry_param e;
145 memset(&e, 0, sizeof(e));
146
147 if (parent != FUSE_ROOT_ID)
148 goto err_out;
149 else if (strcmp(name, FILE_NAME) == 0) {
150 e.ino = FILE_INO;
151 } else
152 goto err_out;
153
154 e.attr_timeout = NO_TIMEOUT;
155 e.entry_timeout = NO_TIMEOUT;
156 if (tfs_stat(e.ino, &e.attr) != 0)
157 goto err_out;
158 fuse_reply_entry(req, &e);
159
160 /*
161 * must only be set when the kernel knows about the entry,
162 * otherwise update_fs_loop() might see a positive count, but kernel
163 * would not have the entry yet
164 */
165 if (e.ino == FILE_INO) {
166 pthread_mutex_lock(&lock);
167 lookup_cnt++;
168 pthread_mutex_unlock(&lock);
169 }
170
171 return;
172
173err_out:
174 fuse_reply_err(req, ENOENT);
175}
176
177static void tfs_forget (fuse_req_t req, fuse_ino_t ino,
178 uint64_t nlookup) {
179 (void) req;
180 if(ino == FILE_INO) {
181 pthread_mutex_lock(&lock);
182 lookup_cnt -= nlookup;
183 pthread_mutex_unlock(&lock);
184 } else
185 assert(ino == FUSE_ROOT_ID);
186 fuse_reply_none(req);
187}
188
189static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
190 struct fuse_file_info *fi) {
191 struct stat stbuf;
192
193 (void) fi;
194
195 memset(&stbuf, 0, sizeof(stbuf));
196 if (tfs_stat(ino, &stbuf) != 0)
197 fuse_reply_err(req, ENOENT);
198 else
199 fuse_reply_attr(req, &stbuf, NO_TIMEOUT);
200}
201
202struct dirbuf {
203 char *p;
204 size_t size;
205};
206
207static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
208 fuse_ino_t ino) {
209 struct stat stbuf;
210 size_t oldsize = b->size;
211 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
212 b->p = (char *) realloc(b->p, b->size);
213 memset(&stbuf, 0, sizeof(stbuf));
214 stbuf.st_ino = ino;
215 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
216 b->size);
217}
218
219#define min(x, y) ((x) < (y) ? (x) : (y))
220
221static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
222 off_t off, size_t maxsize) {
223 if (off < bufsize)
224 return fuse_reply_buf(req, buf + off,
225 min(bufsize - off, maxsize));
226 else
227 return fuse_reply_buf(req, NULL, 0);
228}
229
230static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
231 off_t off, struct fuse_file_info *fi) {
232 (void) fi;
233
234 if (ino != FUSE_ROOT_ID)
235 fuse_reply_err(req, ENOTDIR);
236 else {
237 struct dirbuf b;
238
239 memset(&b, 0, sizeof(b));
240 dirbuf_add(req, &b, FILE_NAME, FILE_INO);
241 reply_buf_limited(req, b.p, b.size, off, size);
242 free(b.p);
243 }
244}
245
246static void tfs_open(fuse_req_t req, fuse_ino_t ino,
247 struct fuse_file_info *fi) {
248
249 /* Make cache persistent even if file is closed,
250 this makes it easier to see the effects */
251 fi->keep_cache = 1;
252
253 if (ino == FUSE_ROOT_ID)
254 fuse_reply_err(req, EISDIR);
255 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
256 fuse_reply_err(req, EACCES);
257 else if (ino == FILE_INO) {
258 fuse_reply_open(req, fi);
259 pthread_mutex_lock(&lock);
260 open_cnt++;
261 pthread_mutex_unlock(&lock);
262 } else {
263 // This should not happen
264 fprintf(stderr, "Got open for non-existing inode!\n");
265 fuse_reply_err(req, ENOENT);
266 }
267}
268
269static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size,
270 off_t off, struct fuse_file_info *fi) {
271 (void) fi;
272
273 assert(ino == FILE_INO);
274 reply_buf_limited(req, file_contents, file_size, off, size);
275}
276
277static void tfs_retrieve_reply(fuse_req_t req, void *cookie, fuse_ino_t ino,
278 off_t offset, struct fuse_bufvec *data) {
279 struct fuse_bufvec bufv;
280 char buf[MAX_STR_LEN];
281 char *expected;
282 ssize_t ret;
283
284 assert(ino == FILE_INO);
285 assert(offset == 0);
286 expected = (char*) cookie;
287
288 bufv.count = 1;
289 bufv.idx = 0;
290 bufv.off = 0;
291 bufv.buf[0].size = MAX_STR_LEN;
292 bufv.buf[0].mem = buf;
293 bufv.buf[0].flags = 0;
294
295 ret = fuse_buf_copy(&bufv, data, 0);
296 assert(ret > 0);
297 assert(strncmp(buf, expected, ret) == 0);
298 free(expected);
299 retrieve_status = 2;
300 fuse_reply_none(req);
301}
302
303static void tfs_destroy(void *userdata)
304{
305 (void)userdata;
306
307 is_umount = true;
308
309 pthread_join(updater, NULL);
310}
311
312
313static const struct fuse_lowlevel_ops tfs_oper = {
314 .init = tfs_init,
315 .lookup = tfs_lookup,
316 .getattr = tfs_getattr,
317 .readdir = tfs_readdir,
318 .open = tfs_open,
319 .read = tfs_read,
320 .forget = tfs_forget,
321 .retrieve_reply = tfs_retrieve_reply,
322 .destroy = tfs_destroy,
323};
324
325static void update_fs(void) {
326 struct tm *now;
327 time_t t;
328 t = time(NULL);
329 now = localtime(&t);
330 assert(now != NULL);
331
332 file_size = strftime(file_contents, MAX_STR_LEN,
333 "The current time is %H:%M:%S\n", now);
334 assert(file_size != 0);
335}
336
337static void* update_fs_loop(void *data) {
338 struct fuse_session *se = (struct fuse_session*) data;
339 struct fuse_bufvec bufv;
340 int ret;
341
342 while(!is_umount) {
343 update_fs();
344 pthread_mutex_lock(&lock);
345 if (!options.no_notify && open_cnt && lookup_cnt) {
346 /* Only send notification if the kernel
347 is aware of the inode */
348 bufv.count = 1;
349 bufv.idx = 0;
350 bufv.off = 0;
351 bufv.buf[0].size = file_size;
352 bufv.buf[0].mem = file_contents;
353 bufv.buf[0].flags = 0;
354
355 /*
356 * Some errors (ENOENT, EBADF, ENODEV) have to be accepted as they
357 * might come up during umount, when kernel side already releases
358 * all inodes, but does not send FUSE_DESTROY yet.
359 */
360
361 ret = fuse_lowlevel_notify_store(se, FILE_INO, 0, &bufv, 0);
362 if ((ret != 0 && !is_umount) &&
363 ret != -ENOENT && ret != -EBADF && ret != -ENODEV) {
364 fprintf(stderr,
365 "ERROR: fuse_lowlevel_notify_store() failed with %s (%d)\n",
366 strerror(-ret), -ret);
367 abort();
368 }
369
370 /* To make sure that everything worked correctly, ask the
371 kernel to send us back the stored data */
372 ret = fuse_lowlevel_notify_retrieve(se, FILE_INO, MAX_STR_LEN,
373 0, (void*) strdup(file_contents));
374 assert((ret == 0 || is_umount) || ret == -ENOENT || ret == -EBADF ||
375 ret != -ENODEV);
376 if(retrieve_status == 0)
377 retrieve_status = 1;
378 }
379 pthread_mutex_unlock(&lock);
380 sleep(options.update_interval);
381 }
382 return NULL;
383}
384
385static void show_help(const char *progname)
386{
387 printf("usage: %s [options] <mountpoint>\n\n", progname);
388 printf("File-system specific options:\n"
389 " --update-interval=<secs> Update-rate of file system contents\n"
390 " --no-notify Disable kernel notifications\n"
391 "\n");
392}
393
394int main(int argc, char *argv[]) {
395 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
396 struct fuse_session *se;
397 struct fuse_cmdline_opts opts;
398 struct fuse_loop_config *config;
399 int ret = -1;
400
401 if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
402 return 1;
403
404 if (fuse_parse_cmdline(&args, &opts) != 0)
405 return 1;
406 if (opts.show_help) {
407 show_help(argv[0]);
408 fuse_cmdline_help();
409 fuse_lowlevel_help();
410 ret = 0;
411 goto err_out1;
412 } else if (opts.show_version) {
413 printf("FUSE library version %s\n", fuse_pkgversion());
414 fuse_lowlevel_version();
415 ret = 0;
416 goto err_out1;
417 }
418
419 /* Initial contents */
420 update_fs();
421
422 se = fuse_session_new(&args, &tfs_oper,
423 sizeof(tfs_oper), NULL);
424 if (se == NULL)
425 goto err_out1;
426
427 if (fuse_set_signal_handlers(se) != 0)
428 goto err_out2;
429
430 if (fuse_session_mount(se, opts.mountpoint) != 0)
431 goto err_out3;
432
433 fuse_daemonize(opts.foreground);
434
435 /* Start thread to update file contents */
436 ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
437 if (ret != 0) {
438 fprintf(stderr, "pthread_create failed with %s\n",
439 strerror(ret));
440 goto err_out3;
441 }
442
443 /* Block until ctrl+c or fusermount -u */
444 if (opts.singlethread)
445 ret = fuse_session_loop(se);
446 else {
447 config = fuse_loop_cfg_create();
448 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
449 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
450 ret = fuse_session_loop_mt(se, config);
451 fuse_loop_cfg_destroy(config);
452 config = NULL;
453 }
454
455 assert(retrieve_status != 1);
456 fuse_session_unmount(se);
457err_out3:
458 fuse_remove_signal_handlers(se);
459err_out2:
460 fuse_session_destroy(se);
461err_out1:
462 free(opts.mountpoint);
463 fuse_opt_free_args(&args);
464
465 return ret ? 1 : 0;
466}
467
468
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:140
fuse_buf_copy
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
Definition buffer.c:284
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition fuse.c:5211
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:187
fuse_daemonize
int fuse_daemonize(int foreground)
Definition helper.c:253
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition fuse_lowlevel.c:3010
fuse_reply_open
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
Definition fuse_lowlevel.c:505
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition fuse_lowlevel.c:331
fuse_reply_buf
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
Definition fuse_lowlevel.c:524
fuse_req_t
struct fuse_req * fuse_req_t
Definition fuse_lowlevel.h:50
fuse_lowlevel_notify_retrieve
int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino, size_t size, off_t offset, void *cookie)
Definition fuse_lowlevel.c:2668
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:428
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition fuse_lowlevel.c:3539
fuse_cmdline_help
void fuse_cmdline_help(void)
Definition helper.c:130
fuse_reply_none
void fuse_reply_none(fuse_req_t req)
Definition fuse_lowlevel.c:336
fuse_lowlevel_help
void fuse_lowlevel_help(void)
Definition fuse_lowlevel.c:3000
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition fuse_lowlevel.c:3473
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition fuse_lowlevel.c:2993
fuse_ino_t
uint64_t fuse_ino_t
Definition fuse_lowlevel.h:47
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:286
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition fuse_lowlevel.c:460
fuse_lowlevel_notify_store
int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino, off_t offset, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
Definition fuse_lowlevel.c:2588
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
fuse_opt_parse
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
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
FUSE_OPT_END
#define FUSE_OPT_END
Definition fuse_opt.h:104
FUSE_ROOT_ID
#define FUSE_ROOT_ID
Definition fuse_lowlevel.h:44
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_buf::flags
enum fuse_buf_flags flags
Definition fuse_common.h:883
fuse_bufvec
Definition fuse_common.h:923
fuse_bufvec::buf
struct fuse_buf buf[1]
Definition fuse_common.h:942
fuse_cmdline_opts
Definition fuse_lowlevel.h:2003
fuse_conn_info
Definition fuse_common.h:538
fuse_conn_info::no_interrupt
uint32_t no_interrupt
Definition fuse_common.h:677
fuse_entry_param
Definition fuse_lowlevel.h:60
fuse_entry_param::ino
fuse_ino_t ino
Definition fuse_lowlevel.h:68
fuse_file_info
Definition fuse_common.h:50
fuse_file_info::keep_cache
uint32_t keep_cache
Definition fuse_common.h:69
fuse_file_info::flags
int32_t flags
Definition fuse_common.h:52
fuse_loop_config
Definition fuse_common.h:139
fuse_lowlevel_ops
Definition fuse_lowlevel.h:206
fuse_lowlevel_ops::init
void(* init)(void *userdata, struct fuse_conn_info *conn)
Definition fuse_lowlevel.h:223
fuse_opt
Definition fuse_opt.h:77