| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/resource.h> |
| 23 | #include <sys/time.h> |
| 24 | |
| 25 | #include <common/defaults.h> |
| 26 | #include <common/error.h> |
| 27 | #include <common/macros.h> |
| 28 | #include <common/utils.h> |
| 29 | |
| 30 | #include "poll.h" |
| 31 | |
| 32 | unsigned int poll_max_size; |
| 33 | |
| 34 | /* |
| 35 | * Resize the epoll events structure of the new size. |
| 36 | * |
| 37 | * Return 0 on success or else -1 with the current events pointer untouched. |
| 38 | */ |
| 39 | static int resize_poll_event(struct compat_poll_event_array *array, |
| 40 | uint32_t new_size) |
| 41 | { |
| 42 | struct pollfd *ptr; |
| 43 | |
| 44 | assert(array); |
| 45 | |
| 46 | /* Refuse to resize the array more than the max size. */ |
| 47 | if (new_size > poll_max_size) { |
| 48 | goto error; |
| 49 | } |
| 50 | |
| 51 | ptr = realloc(array->events, new_size * sizeof(*ptr)); |
| 52 | if (ptr == NULL) { |
| 53 | PERROR("realloc epoll add"); |
| 54 | goto error; |
| 55 | } |
| 56 | if (new_size > array->alloc_size) { |
| 57 | /* Zero newly allocated memory */ |
| 58 | memset(ptr + array->alloc_size, 0, |
| 59 | (new_size - array->alloc_size) * sizeof(*ptr)); |
| 60 | } |
| 61 | array->events = ptr; |
| 62 | array->alloc_size = new_size; |
| 63 | |
| 64 | return 0; |
| 65 | |
| 66 | error: |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Update events with the current events object. |
| 72 | */ |
| 73 | static int update_current_events(struct lttng_poll_event *events) |
| 74 | { |
| 75 | int ret; |
| 76 | struct compat_poll_event_array *current, *wait; |
| 77 | |
| 78 | assert(events); |
| 79 | |
| 80 | current = &events->current; |
| 81 | wait = &events->wait; |
| 82 | |
| 83 | wait->nb_fd = current->nb_fd; |
| 84 | if (events->need_realloc) { |
| 85 | ret = resize_poll_event(wait, current->alloc_size); |
| 86 | if (ret < 0) { |
| 87 | goto error; |
| 88 | } |
| 89 | } |
| 90 | memcpy(wait->events, current->events, |
| 91 | current->nb_fd * sizeof(*current->events)); |
| 92 | |
| 93 | /* Update is done and realloc as well. */ |
| 94 | events->need_update = 0; |
| 95 | events->need_realloc = 0; |
| 96 | |
| 97 | return 0; |
| 98 | |
| 99 | error: |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Create pollfd data structure. |
| 105 | */ |
| 106 | int compat_poll_create(struct lttng_poll_event *events, int size) |
| 107 | { |
| 108 | struct compat_poll_event_array *current, *wait; |
| 109 | |
| 110 | if (events == NULL || size <= 0) { |
| 111 | ERR("Wrong arguments for poll create"); |
| 112 | goto error; |
| 113 | } |
| 114 | |
| 115 | /* Don't bust the limit here */ |
| 116 | if (size > poll_max_size) { |
| 117 | size = poll_max_size; |
| 118 | } |
| 119 | |
| 120 | /* Reset everything before begining the allocation. */ |
| 121 | memset(events, 0, sizeof(struct lttng_poll_event)); |
| 122 | |
| 123 | /* Ease our life a bit. */ |
| 124 | current = &events->current; |
| 125 | wait = &events->wait; |
| 126 | |
| 127 | /* This *must* be freed by using lttng_poll_free() */ |
| 128 | wait->events = zmalloc(size * sizeof(struct pollfd)); |
| 129 | if (wait->events == NULL) { |
| 130 | perror("zmalloc struct pollfd"); |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | wait->alloc_size = wait->init_size = size; |
| 135 | |
| 136 | current->events = zmalloc(size * sizeof(struct pollfd)); |
| 137 | if (current->events == NULL) { |
| 138 | perror("zmalloc struct current pollfd"); |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | current->alloc_size = current->init_size = size; |
| 143 | |
| 144 | return 0; |
| 145 | |
| 146 | error: |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Add fd to pollfd data structure with requested events. |
| 152 | */ |
| 153 | int compat_poll_add(struct lttng_poll_event *events, int fd, |
| 154 | uint32_t req_events) |
| 155 | { |
| 156 | int new_size, ret, i; |
| 157 | struct compat_poll_event_array *current; |
| 158 | |
| 159 | if (events == NULL || events->current.events == NULL || fd < 0) { |
| 160 | ERR("Bad compat poll add arguments"); |
| 161 | goto error; |
| 162 | } |
| 163 | |
| 164 | /* Ease our life a bit. */ |
| 165 | current = &events->current; |
| 166 | |
| 167 | /* Check if fd we are trying to add is already there. */ |
| 168 | for (i = 0; i < current->nb_fd; i++) { |
| 169 | /* Don't put back the fd we want to delete */ |
| 170 | if (current->events[i].fd == fd) { |
| 171 | errno = EEXIST; |
| 172 | goto error; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* Check for a needed resize of the array. */ |
| 177 | if (current->nb_fd > current->alloc_size) { |
| 178 | /* Expand it by a power of two of the current size. */ |
| 179 | new_size = max_t(int, |
| 180 | 1U << utils_get_count_order_u32(current->nb_fd), |
| 181 | current->alloc_size << 1UL); |
| 182 | ret = resize_poll_event(current, new_size); |
| 183 | if (ret < 0) { |
| 184 | goto error; |
| 185 | } |
| 186 | events->need_realloc = 1; |
| 187 | } |
| 188 | |
| 189 | current->events[current->nb_fd].fd = fd; |
| 190 | current->events[current->nb_fd].events = req_events; |
| 191 | current->nb_fd++; |
| 192 | events->need_update = 1; |
| 193 | |
| 194 | DBG("fd %d of %d added to pollfd", fd, current->nb_fd); |
| 195 | |
| 196 | return 0; |
| 197 | |
| 198 | error: |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Remove a fd from the pollfd structure. |
| 204 | */ |
| 205 | int compat_poll_del(struct lttng_poll_event *events, int fd) |
| 206 | { |
| 207 | int new_size, i, count = 0, ret; |
| 208 | struct compat_poll_event_array *current; |
| 209 | |
| 210 | if (events == NULL || events->current.events == NULL || fd < 0) { |
| 211 | ERR("Wrong arguments for poll del"); |
| 212 | goto error; |
| 213 | } |
| 214 | |
| 215 | /* Ease our life a bit. */ |
| 216 | current = &events->current; |
| 217 | |
| 218 | /* Check if we need to shrink it down. */ |
| 219 | if ((current->nb_fd << 1UL) <= current->alloc_size && |
| 220 | current->nb_fd >= current->init_size) { |
| 221 | /* |
| 222 | * Shrink if nb_fd multiplied by two is <= than the actual size and we |
| 223 | * are above the initial size. |
| 224 | */ |
| 225 | new_size = max_t(int, |
| 226 | utils_get_count_order_u32(current->nb_fd) >> 1U, |
| 227 | current->alloc_size >> 1U); |
| 228 | ret = resize_poll_event(current, new_size); |
| 229 | if (ret < 0) { |
| 230 | goto error; |
| 231 | } |
| 232 | events->need_realloc = 1; |
| 233 | } |
| 234 | |
| 235 | for (i = 0; i < current->nb_fd; i++) { |
| 236 | /* Don't put back the fd we want to delete */ |
| 237 | if (current->events[i].fd != fd) { |
| 238 | current->events[count].fd = current->events[i].fd; |
| 239 | current->events[count].events = current->events[i].events; |
| 240 | count++; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | current->nb_fd--; |
| 245 | events->need_update = 1; |
| 246 | |
| 247 | return 0; |
| 248 | |
| 249 | error: |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Wait on poll() with timeout. Blocking call. |
| 255 | */ |
| 256 | int compat_poll_wait(struct lttng_poll_event *events, int timeout) |
| 257 | { |
| 258 | int ret; |
| 259 | |
| 260 | if (events == NULL || events->current.events == NULL) { |
| 261 | ERR("poll wait arguments error"); |
| 262 | goto error; |
| 263 | } |
| 264 | |
| 265 | if (events->current.nb_fd == 0) { |
| 266 | /* Return an invalid error to be consistent with epoll. */ |
| 267 | errno = EINVAL; |
| 268 | goto error; |
| 269 | } |
| 270 | |
| 271 | if (events->need_update) { |
| 272 | ret = update_current_events(events); |
| 273 | if (ret < 0) { |
| 274 | errno = ENOMEM; |
| 275 | goto error; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | ret = poll(events->wait.events, events->wait.nb_fd, timeout); |
| 280 | if (ret < 0) { |
| 281 | /* At this point, every error is fatal */ |
| 282 | perror("poll wait"); |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * poll() should always iterate on all FDs since we handle the pollset in |
| 288 | * user space and after poll returns, we have to try every fd for a match. |
| 289 | */ |
| 290 | return events->wait.nb_fd; |
| 291 | |
| 292 | error: |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Setup poll set maximum size. |
| 298 | */ |
| 299 | void compat_poll_set_max_size(void) |
| 300 | { |
| 301 | int ret; |
| 302 | struct rlimit lim; |
| 303 | |
| 304 | /* Default value */ |
| 305 | poll_max_size = DEFAULT_POLL_SIZE; |
| 306 | |
| 307 | ret = getrlimit(RLIMIT_NOFILE, &lim); |
| 308 | if (ret < 0) { |
| 309 | perror("getrlimit poll RLIMIT_NOFILE"); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | poll_max_size = lim.rlim_cur; |
| 314 | if (poll_max_size == 0) { |
| 315 | /* Extra precaution */ |
| 316 | poll_max_size = DEFAULT_POLL_SIZE; |
| 317 | } |
| 318 | |
| 319 | DBG("poll set max size set to %u", poll_max_size); |
| 320 | } |