From: Mathieu Desnoyers Date: Fri, 18 May 2012 21:07:06 +0000 (-0400) Subject: Fix: don't SIGBUS when filesystem is full X-Git-Tag: v2.1.0-rc1~83 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=3a81f31dfe668b3c56207745ca698c7db4dc0890;p=lttng-ust.git Fix: don't SIGBUS when filesystem is full Write zeroes into the buffer after its allocation to ensure that writes to the shm will not trigger SIGBUS upon full filesystem situations. This scenario can occur if /dev/shm is filled, e.g. by specifying too large buffers with --enable-channel. When the write() fails, the buffer creation for this channel fails. Fixes #230 Signed-off-by: Mathieu Desnoyers --- diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index d1e50449..dbd3f348 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -31,6 +31,45 @@ #include #include #include +#include + +/* + * 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. + */ +static +int zero_file(int fd, size_t len) +{ + ssize_t retlen; + size_t written = 0; + char *zeropage; + long pagelen; + int ret; + + pagelen = sysconf(_SC_PAGESIZE); + if (pagelen < 0) + return (int) pagelen; + zeropage = calloc(pagelen, 1); + if (!zeropage) + return -ENOMEM; + + while (len > written) { + do { + retlen = write(fd, zeropage, + min_t(size_t, pagelen, len - written)); + } while (retlen == -1UL && errno == EINTR); + if (retlen < 0) { + ret = (int) retlen; + goto error; + } + written += retlen; + } + ret = 0; +error: + free(zeropage); + return ret; +} struct shm_object_table *shm_object_table_create(size_t max_nb_obj) { @@ -128,6 +167,11 @@ struct shm_object *shm_object_table_append(struct shm_object_table *table, PERROR("pthread_sigmask"); goto error_sigmask_release; } + ret = zero_file(shmfd, memory_map_size); + if (ret) { + PERROR("zero_file"); + goto error_zero_file; + } ret = ftruncate(shmfd, memory_map_size); if (ret) { PERROR("ftruncate"); @@ -152,6 +196,7 @@ struct shm_object *shm_object_table_append(struct shm_object_table *table, error_mmap: error_ftruncate: error_shm_release: +error_zero_file: error_sigmask_release: ret = close(shmfd); if (ret) {