X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fcompat%2Fcompat-poll.c;h=11149f4d46f04b9b55cee2f87c41dd530138ab56;hp=79fcb7d652b87230815c4d7b6d7146cf8546a9c2;hb=refs%2Fheads%2FC009-19;hpb=cfa9a5a2b4a96e0d6a9eeddd2622a6d7c173b7ac diff --git a/src/common/compat/compat-poll.c b/src/common/compat/compat-poll.c index 79fcb7d65..11149f4d4 100644 --- a/src/common/compat/compat-poll.c +++ b/src/common/compat/compat-poll.c @@ -15,11 +15,12 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include #include +#include #include #include @@ -52,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; @@ -75,7 +81,7 @@ static int update_current_events(struct lttng_poll_event *events) wait = &events->wait; wait->nb_fd = current->nb_fd; - if (events->need_realloc) { + if (current->alloc_size != wait->alloc_size) { ret = resize_poll_event(wait, current->alloc_size); if (ret < 0) { goto error; @@ -84,9 +90,8 @@ static int update_current_events(struct lttng_poll_event *events) memcpy(wait->events, current->events, current->nb_fd * sizeof(*current->events)); - /* Update is done and realloc as well. */ + /* Update is done. */ events->need_update = 0; - events->need_realloc = 0; return 0; @@ -97,6 +102,7 @@ error: /* * Create pollfd data structure. */ +LTTNG_HIDDEN int compat_poll_create(struct lttng_poll_event *events, int size) { struct compat_poll_event_array *current, *wait; @@ -106,6 +112,12 @@ int compat_poll_create(struct lttng_poll_event *events, int size) goto error; } + if (!poll_max_size) { + if (lttng_poll_set_max_size()) { + goto error; + } + } + /* Don't bust the limit here */ if (size > poll_max_size) { size = poll_max_size; @@ -114,14 +126,13 @@ int compat_poll_create(struct lttng_poll_event *events, int size) /* Reset everything before begining the allocation. */ memset(events, 0, sizeof(struct lttng_poll_event)); - /* Ease our life a bit. */ current = &events->current; wait = &events->wait; /* 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; } @@ -129,7 +140,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; } @@ -155,29 +166,23 @@ int compat_poll_add(struct lttng_poll_event *events, int fd, goto error; } - /* Ease our life a bit. */ current = &events->current; /* Check if fd we are trying to add is already there. */ for (i = 0; i < current->nb_fd; i++) { - /* Don't put back the fd we want to delete */ if (current->events[i].fd == fd) { errno = EEXIST; goto error; } } - /* 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 = max_t(int, - 1U << utils_get_count_order_u32(current->nb_fd), - current->alloc_size << 1UL); + /* Resize array if needed. */ + new_size = 1U << utils_get_count_order_u32(current->nb_fd + 1); + if (new_size != current->alloc_size && new_size >= current->init_size) { ret = resize_poll_event(current, new_size); if (ret < 0) { goto error; } - events->need_realloc = 1; } current->events[current->nb_fd].fd = fd; @@ -193,9 +198,47 @@ error: return -1; } +/* + * Modify an fd's events.. + */ +LTTNG_HIDDEN +int compat_poll_mod(struct lttng_poll_event *events, int fd, + uint32_t req_events) +{ + int i; + bool fd_found = false; + struct compat_poll_event_array *current; + + if (events == NULL || events->current.events == NULL || fd < 0) { + ERR("Bad compat poll mod arguments"); + goto error; + } + + current = &events->current; + + for (i = 0; i < current->nb_fd; i++) { + if (current->events[i].fd == fd) { + fd_found = true; + current->events[i].events = req_events; + events->need_update = 1; + break; + } + } + + if (!fd_found) { + goto error; + } + + return 0; + +error: + return -1; +} + /* * Remove a fd from the pollfd structure. */ +LTTNG_HIDDEN int compat_poll_del(struct lttng_poll_event *events, int fd) { int new_size, i, count = 0, ret; @@ -209,23 +252,6 @@ int compat_poll_del(struct lttng_poll_event *events, int fd) /* Ease our life a bit. */ current = &events->current; - /* Check if we need to shrink it down. */ - if ((current->nb_fd << 1UL) <= current->alloc_size && - current->nb_fd >= current->init_size) { - /* - * Shrink if nb_fd multiplied by two is <= than the actual size and we - * are above the initial size. - */ - 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; - } - events->need_realloc = 1; - } - for (i = 0; i < current->nb_fd; i++) { /* Don't put back the fd we want to delete */ if (current->events[i].fd != fd) { @@ -234,8 +260,19 @@ int compat_poll_del(struct lttng_poll_event *events, int fd) count++; } } + /* No fd duplicate should be ever added into array. */ + assert(current->nb_fd - 1 == count); + current->nb_fd = count; + + /* Resize array if needed. */ + new_size = 1U << utils_get_count_order_u32(current->nb_fd); + if (new_size != current->alloc_size && new_size >= current->init_size) { + ret = resize_poll_event(current, new_size); + if (ret < 0) { + goto error; + } + } - current->nb_fd--; events->need_update = 1; return 0; @@ -247,6 +284,7 @@ error: /* * Wait on poll() with timeout. Blocking call. */ +LTTNG_HIDDEN int compat_poll_wait(struct lttng_poll_event *events, int timeout) { int ret; @@ -255,10 +293,12 @@ int compat_poll_wait(struct lttng_poll_event *events, int timeout) ERR("poll wait arguments error"); goto error; } + assert(events->current.nb_fd >= 0); if (events->current.nb_fd == 0) { /* Return an invalid error to be consistent with epoll. */ errno = EINVAL; + events->wait.nb_fd = 0; goto error; } @@ -270,10 +310,12 @@ int compat_poll_wait(struct lttng_poll_event *events, int timeout) } } - ret = poll(events->wait.events, events->wait.nb_fd, timeout); + do { + ret = poll(events->wait.events, events->wait.nb_fd, timeout); + } while (ret == -1 && errno == EINTR); if (ret < 0) { /* At this point, every error is fatal */ - perror("poll wait"); + PERROR("poll wait"); goto error; } @@ -290,25 +332,24 @@ error: /* * Setup poll set maximum size. */ -void compat_poll_set_max_size(void) +LTTNG_HIDDEN +int compat_poll_set_max_size(void) { - int ret; + int ret, retval = 0; struct rlimit lim; - /* Default value */ - poll_max_size = DEFAULT_POLL_SIZE; - ret = getrlimit(RLIMIT_NOFILE, &lim); if (ret < 0) { - perror("getrlimit poll RLIMIT_NOFILE"); - return; + PERROR("getrlimit poll RLIMIT_NOFILE"); + retval = -1; + goto end; } poll_max_size = lim.rlim_cur; +end: if (poll_max_size == 0) { - /* Extra precaution */ poll_max_size = DEFAULT_POLL_SIZE; } - DBG("poll set max size set to %u", poll_max_size); + return retval; }