aboutsummaryrefslogtreecommitdiff
path: root/example/notify_store_retrieve.c
diff options
context:
space:
mode:
authorGravatar Nikolaus Rath <Nikolaus@rath.org>2016-10-10 11:18:00 -0700
committerGravatar Nikolaus Rath <Nikolaus@rath.org>2016-10-10 11:47:29 -0700
commit2044df660f193d6eb26e2e5121fb33d2f1dcc145 (patch)
treee7f32d8a75448f382fdca47e05f738fc4ab70171 /example/notify_store_retrieve.c
parent71064a41f823d2c857813330e8a3bce876e43553 (diff)
Fix race condition in notify_* examples
The fix in commit cf4159156b was incomplete. While some false positives are caused by sleep() in the file system taking longer than expected, there was also a race condition where the file system would run before the contents are initialized properly.
Diffstat (limited to 'example/notify_store_retrieve.c')
-rw-r--r--example/notify_store_retrieve.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/example/notify_store_retrieve.c b/example/notify_store_retrieve.c
index cc7e865..76f3291 100644
--- a/example/notify_store_retrieve.c
+++ b/example/notify_store_retrieve.c
@@ -286,20 +286,24 @@ static struct fuse_lowlevel_ops tfs_oper = {
.retrieve_reply = tfs_retrieve_reply,
};
-static void* update_fs(void *data) {
- struct fuse_session *se = (struct fuse_session*) data;
+static void update_fs(void) {
struct tm *now;
time_t t;
+ t = time(NULL);
+ now = localtime(&t);
+ assert(now != NULL);
+
+ file_size = strftime(file_contents, MAX_STR_LEN,
+ "The current time is %H:%M:%S\n", now);
+ assert(file_size != 0);
+}
+
+static void* update_fs_loop(void *data) {
+ struct fuse_session *se = (struct fuse_session*) data;
struct fuse_bufvec bufv;
while(1) {
- t = time(NULL);
- now = localtime(&t);
- assert(now != NULL);
-
- file_size = strftime(file_contents, MAX_STR_LEN,
- "The current time is %H:%M:%S\n", now);
- assert(file_size != 0);
+ update_fs();
if (!options.no_notify && lookup_cnt) {
/* Only send notification if the kernel
is aware of the inode */
@@ -361,6 +365,9 @@ int main(int argc, char *argv[]) {
goto err_out1;
}
+ /* Initial contents */
+ update_fs();
+
se = fuse_session_new(&args, &tfs_oper,
sizeof(tfs_oper), NULL);
if (se == NULL)
@@ -375,7 +382,7 @@ int main(int argc, char *argv[]) {
fuse_daemonize(opts.foreground);
/* Start thread to update file contents */
- ret = pthread_create(&updater, NULL, update_fs, (void *)se);
+ ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
if (ret != 0) {
fprintf(stderr, "pthread_create failed with %s\n",
strerror(ret));