Fix: move fsync after ftruncate
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 23 Aug 2017 15:27:16 +0000 (08:27 -0700)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 23 Aug 2017 15:28:26 +0000 (08:28 -0700)
Move fsync after ftruncate to ensure we sync up all metadata after the
entire initialization of the buffer.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
libringbuffer/shm.c

index 8ac321e3931906888f79dd9e2419a7dbca8225de..ea946ea3f5e2a6d2e46d3297ebe925135164272e 100644 (file)
@@ -39,9 +39,6 @@
  * Ensure we have the required amount of space available by writing 0
  * into the entire buffer. Not doing so can trigger SIGBUS when going
  * beyond the available shm space.
- *
- * Also ensure the file metadata is synced with the storage by using
- * fsync(2).
  */
 static
 int zero_file(int fd, size_t len)
@@ -70,11 +67,6 @@ int zero_file(int fd, size_t len)
                }
                written += retlen;
        }
-       ret = fsync(fd);
-       if (ret) {
-               ret = (int) -errno;
-               goto error;
-       }
        ret = 0;
 error:
        free(zeropage);
@@ -142,6 +134,15 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
                PERROR("ftruncate");
                goto error_ftruncate;
        }
+       /*
+        * Also ensure the file metadata is synced with the storage by using
+        * fsync(2).
+        */
+       ret = fsync(shmfd);
+       if (ret) {
+               PERROR("fsync");
+               goto error_fsync;
+       }
        obj->shm_fd_ownership = 0;
        obj->shm_fd = shmfd;
 
@@ -161,6 +162,7 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table,
        return obj;
 
 error_mmap:
+error_fsync:
 error_ftruncate:
 error_zero_file:
 error_fcntl:
This page took 0.025268 seconds and 4 git commands to generate.