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