X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fcompat%2Fcompat-poll.c;h=6d34a0e69cbba04d133430d11fbce0626612bad0;hp=bc79eed0aaa5321c48fcd055858698bcdc5fa375;hb=6f04ed72990f6c72d16fd08d39feac0967da732e;hpb=ac018a8b4890dcf6f0eb9d4b5dee600269e79b29 diff --git a/src/common/compat/compat-poll.c b/src/common/compat/compat-poll.c index bc79eed0a..6d34a0e69 100644 --- a/src/common/compat/compat-poll.c +++ b/src/common/compat/compat-poll.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include @@ -23,6 +24,8 @@ #include #include +#include +#include #include "poll.h" @@ -50,6 +53,11 @@ static int resize_poll_event(struct compat_poll_event_array *array, PERROR("realloc epoll add"); goto error; } + if (new_size > array->alloc_size) { + /* Zero newly allocated memory */ + memset(ptr + array->alloc_size, 0, + (new_size - array->alloc_size) * sizeof(*ptr)); + } array->events = ptr; array->alloc_size = new_size; @@ -119,7 +127,7 @@ int compat_poll_create(struct lttng_poll_event *events, int size) /* This *must* be freed by using lttng_poll_free() */ wait->events = zmalloc(size * sizeof(struct pollfd)); if (wait->events == NULL) { - perror("zmalloc struct pollfd"); + PERROR("zmalloc struct pollfd"); goto error; } @@ -127,7 +135,7 @@ int compat_poll_create(struct lttng_poll_event *events, int size) current->events = zmalloc(size * sizeof(struct pollfd)); if (current->events == NULL) { - perror("zmalloc struct current pollfd"); + PERROR("zmalloc struct current pollfd"); goto error; } @@ -168,7 +176,9 @@ int compat_poll_add(struct lttng_poll_event *events, int fd, /* Check for a needed resize of the array. */ if (current->nb_fd > current->alloc_size) { /* Expand it by a power of two of the current size. */ - new_size = current->alloc_size << 1UL; + new_size = max_t(int, + 1U << utils_get_count_order_u32(current->nb_fd), + current->alloc_size << 1UL); ret = resize_poll_event(current, new_size); if (ret < 0) { goto error; @@ -212,7 +222,9 @@ int compat_poll_del(struct lttng_poll_event *events, int fd) * Shrink if nb_fd multiplied by two is <= than the actual size and we * are above the initial size. */ - new_size = current->alloc_size >> 1UL; + new_size = max_t(int, + utils_get_count_order_u32(current->nb_fd) >> 1U, + current->alloc_size >> 1U); ret = resize_poll_event(current, new_size); if (ret < 0) { goto error; @@ -267,7 +279,7 @@ int compat_poll_wait(struct lttng_poll_event *events, int timeout) ret = poll(events->wait.events, events->wait.nb_fd, timeout); if (ret < 0) { /* At this point, every error is fatal */ - perror("poll wait"); + PERROR("poll wait"); goto error; } @@ -294,7 +306,7 @@ void compat_poll_set_max_size(void) ret = getrlimit(RLIMIT_NOFILE, &lim); if (ret < 0) { - perror("getrlimit poll RLIMIT_NOFILE"); + PERROR("getrlimit poll RLIMIT_NOFILE"); return; }