From d0f6cf574ef992620b09c183cb3a0ea771070ea5 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 23 Aug 2017 08:27:16 -0700 Subject: [PATCH] Fix: move fsync after ftruncate Move fsync after ftruncate to ensure we sync up all metadata after the entire initialization of the buffer. Signed-off-by: Mathieu Desnoyers --- libringbuffer/shm.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index 8ac321e3..ea946ea3 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -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: -- 2.34.1