X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=libringbuffer%2Fshm.c;h=ea946ea3f5e2a6d2e46d3297ebe925135164272e;hb=e1e13e8cc548aec75adecc78fd2522b0f01a0812;hp=53b2b6ec3f1819ba69d2b328f83f224d1d0b220d;hpb=5ea386c3b8e3566ec0b6710c82aae433de4d944a;p=lttng-ust.git diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index 53b2b6ec..ea946ea3 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _LGPL_SOURCE #include "shm.h" #include #include @@ -32,6 +33,7 @@ #include #include #include +#include /* * Ensure we have the required amount of space available by writing 0 @@ -132,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; @@ -151,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: @@ -342,7 +354,7 @@ error_fcntl: } static -void shmp_object_destroy(struct shm_object *obj) +void shmp_object_destroy(struct shm_object *obj, int consumer) { switch (obj->type) { case SHM_OBJECT_SHM: @@ -354,20 +366,46 @@ void shmp_object_destroy(struct shm_object *obj) PERROR("umnmap"); assert(0); } + if (obj->shm_fd_ownership) { - ret = close(obj->shm_fd); - if (ret) { - PERROR("close"); - assert(0); + /* Delete FDs only if called from app (not consumer). */ + if (!consumer) { + lttng_ust_lock_fd_tracker(); + ret = close(obj->shm_fd); + if (!ret) { + lttng_ust_delete_fd_from_tracker(obj->shm_fd); + } else { + PERROR("close"); + assert(0); + } + lttng_ust_unlock_fd_tracker(); + } else { + ret = close(obj->shm_fd); + if (ret) { + PERROR("close"); + assert(0); + } } } for (i = 0; i < 2; i++) { if (obj->wait_fd[i] < 0) continue; - ret = close(obj->wait_fd[i]); - if (ret) { - PERROR("close"); - assert(0); + if (!consumer) { + lttng_ust_lock_fd_tracker(); + ret = close(obj->wait_fd[i]); + if (!ret) { + lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]); + } else { + PERROR("close"); + assert(0); + } + lttng_ust_unlock_fd_tracker(); + } else { + ret = close(obj->wait_fd[i]); + if (ret) { + PERROR("close"); + assert(0); + } } } break; @@ -379,10 +417,22 @@ void shmp_object_destroy(struct shm_object *obj) for (i = 0; i < 2; i++) { if (obj->wait_fd[i] < 0) continue; - ret = close(obj->wait_fd[i]); - if (ret) { - PERROR("close"); - assert(0); + if (!consumer) { + lttng_ust_lock_fd_tracker(); + ret = close(obj->wait_fd[i]); + if (!ret) { + lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]); + } else { + PERROR("close"); + assert(0); + } + lttng_ust_unlock_fd_tracker(); + } else { + ret = close(obj->wait_fd[i]); + if (ret) { + PERROR("close"); + assert(0); + } } } free(obj->memory_map); @@ -393,12 +443,12 @@ void shmp_object_destroy(struct shm_object *obj) } } -void shm_object_table_destroy(struct shm_object_table *table) +void shm_object_table_destroy(struct shm_object_table *table, int consumer) { int i; for (i = 0; i < table->allocated_len; i++) - shmp_object_destroy(&table->objects[i]); + shmp_object_destroy(&table->objects[i], consumer); free(table); }