| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2019 - Yannick Lamarre <ylamarre@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/resource.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <stdbool.h> |
| 25 | |
| 26 | #include <common/defaults.h> |
| 27 | #include <common/error.h> |
| 28 | #include <common/macros.h> |
| 29 | #include <common/utils.h> |
| 30 | |
| 31 | #include "poll.h" |
| 32 | |
| 33 | unsigned int poll_max_size; |
| 34 | |
| 35 | /* |
| 36 | * Resize the epoll events structure of the new size. |
| 37 | * |
| 38 | * Return 0 on success or else -1 with the current events pointer untouched. |
| 39 | */ |
| 40 | static int resize_poll_event(struct compat_poll_event_array *array, |
| 41 | uint32_t new_size) |
| 42 | { |
| 43 | struct pollfd *ptr; |
| 44 | |
| 45 | assert(array); |
| 46 | |
| 47 | /* Refuse to resize the array more than the max size. */ |
| 48 | if (new_size > poll_max_size) { |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | ptr = realloc(array->events, new_size * sizeof(*ptr)); |
| 53 | if (ptr == NULL) { |
| 54 | PERROR("realloc epoll add"); |
| 55 | goto error; |
| 56 | } |
| 57 | if (new_size > array->alloc_size) { |
| 58 | /* Zero newly allocated memory */ |
| 59 | memset(ptr + array->alloc_size, 0, |
| 60 | (new_size - array->alloc_size) * sizeof(*ptr)); |
| 61 | } |
| 62 | array->events = ptr; |
| 63 | array->alloc_size = new_size; |
| 64 | |
| 65 | return 0; |
| 66 | |
| 67 | error: |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Update events with the current events object. |
| 73 | */ |
| 74 | static int update_current_events(struct lttng_poll_event *events) |
| 75 | { |
| 76 | int ret; |
| 77 | struct compat_poll_event_array *current, *wait; |
| 78 | |
| 79 | assert(events); |
| 80 | |
| 81 | current = &events->current; |
| 82 | wait = &events->wait; |
| 83 | |
| 84 | wait->nb_fd = current->nb_fd; |
| 85 | if (current->alloc_size != wait->alloc_size) { |
| 86 | ret = resize_poll_event(wait, current->alloc_size); |
| 87 | if (ret < 0) { |
| 88 | goto error; |
| 89 | } |
| 90 | } |
| 91 | memcpy(wait->events, current->events, |
| 92 | current->nb_fd * sizeof(*current->events)); |
| 93 | |
| 94 | /* Update is done. */ |
| 95 | events->need_update = 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 | if (!poll_max_size) { |
| 116 | if (lttng_poll_set_max_size()) { |
| 117 | goto error; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* Don't bust the limit here */ |
| 122 | if (size > poll_max_size) { |
| 123 | size = poll_max_size; |
| 124 | } |
| 125 | |
| 126 | /* Reset everything before begining the allocation. */ |
| 127 | memset(events, 0, sizeof(struct lttng_poll_event)); |
| 128 | |
| 129 | current = &events->current; |
| 130 | wait = &events->wait; |
| 131 | |
| 132 | /* This *must* be freed by using lttng_poll_free() */ |
| 133 | wait->events = zmalloc(size * sizeof(struct pollfd)); |
| 134 | if (wait->events == NULL) { |
| 135 | PERROR("zmalloc struct pollfd"); |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | wait->alloc_size = wait->init_size = size; |
| 140 | |
| 141 | current->events = zmalloc(size * sizeof(struct pollfd)); |
| 142 | if (current->events == NULL) { |
| 143 | PERROR("zmalloc struct current pollfd"); |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | current->alloc_size = current->init_size = size; |
| 148 | |
| 149 | return 0; |
| 150 | |
| 151 | error: |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Add fd to pollfd data structure with requested events. |
| 157 | */ |
| 158 | int compat_poll_add(struct lttng_poll_event *events, int fd, |
| 159 | uint32_t req_events) |
| 160 | { |
| 161 | int new_size, ret, i; |
| 162 | struct compat_poll_event_array *current; |
| 163 | |
| 164 | if (events == NULL || events->current.events == NULL || fd < 0) { |
| 165 | ERR("Bad compat poll add arguments"); |
| 166 | goto error; |
| 167 | } |
| 168 | |
| 169 | current = &events->current; |
| 170 | |
| 171 | /* Check if fd we are trying to add is already there. */ |
| 172 | for (i = 0; i < current->nb_fd; i++) { |
| 173 | if (current->events[i].fd == fd) { |
| 174 | errno = EEXIST; |
| 175 | goto error; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* Resize array if needed. */ |
| 180 | new_size = 1U << utils_get_count_order_u32(current->nb_fd + 1); |
| 181 | if (new_size != current->alloc_size && new_size >= current->init_size) { |
| 182 | ret = resize_poll_event(current, new_size); |
| 183 | if (ret < 0) { |
| 184 | goto error; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | current->events[current->nb_fd].fd = fd; |
| 189 | current->events[current->nb_fd].events = req_events; |
| 190 | current->nb_fd++; |
| 191 | events->need_update = 1; |
| 192 | |
| 193 | DBG("fd %d of %d added to pollfd", fd, current->nb_fd); |
| 194 | |
| 195 | return 0; |
| 196 | |
| 197 | error: |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Modify an fd's events.. |
| 203 | */ |
| 204 | int compat_poll_mod(struct lttng_poll_event *events, int fd, |
| 205 | uint32_t req_events) |
| 206 | { |
| 207 | int i; |
| 208 | struct compat_poll_event_array *current; |
| 209 | |
| 210 | if (events == NULL || events->current.nb_fd == 0 || |
| 211 | events->current.events == NULL || fd < 0) { |
| 212 | ERR("Bad compat poll mod arguments"); |
| 213 | goto error; |
| 214 | } |
| 215 | |
| 216 | current = &events->current; |
| 217 | |
| 218 | for (i = 0; i < current->nb_fd; i++) { |
| 219 | if (current->events[i].fd == fd) { |
| 220 | current->events[i].events = req_events; |
| 221 | events->need_update = 1; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * The epoll flavor doesn't flag modifying a non-included FD as an |
| 228 | * error. |
| 229 | */ |
| 230 | |
| 231 | return 0; |
| 232 | |
| 233 | error: |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Remove a fd from the pollfd structure. |
| 239 | */ |
| 240 | int compat_poll_del(struct lttng_poll_event *events, int fd) |
| 241 | { |
| 242 | int i, count = 0, ret; |
| 243 | uint32_t new_size; |
| 244 | struct compat_poll_event_array *current; |
| 245 | |
| 246 | if (events == NULL || events->current.nb_fd == 0 || |
| 247 | events->current.events == NULL || fd < 0) { |
| 248 | goto error; |
| 249 | } |
| 250 | |
| 251 | /* Ease our life a bit. */ |
| 252 | current = &events->current; |
| 253 | |
| 254 | for (i = 0; i < current->nb_fd; i++) { |
| 255 | /* Don't put back the fd we want to delete */ |
| 256 | if (current->events[i].fd != fd) { |
| 257 | current->events[count].fd = current->events[i].fd; |
| 258 | current->events[count].events = current->events[i].events; |
| 259 | count++; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* The fd was not in our set, return no error as with epoll. */ |
| 264 | if (current->nb_fd == count) { |
| 265 | goto end; |
| 266 | } |
| 267 | |
| 268 | /* No fd duplicate should be ever added into array. */ |
| 269 | assert(current->nb_fd - 1 == count); |
| 270 | current->nb_fd = count; |
| 271 | |
| 272 | /* Resize array if needed. */ |
| 273 | new_size = 1U << utils_get_count_order_u32(current->nb_fd); |
| 274 | if (new_size != current->alloc_size && new_size >= current->init_size |
| 275 | && current->nb_fd != 0) { |
| 276 | ret = resize_poll_event(current, new_size); |
| 277 | if (ret < 0) { |
| 278 | goto error; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | events->need_update = 1; |
| 283 | |
| 284 | end: |
| 285 | return 0; |
| 286 | |
| 287 | error: |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Wait on poll() with timeout. Blocking call. |
| 293 | */ |
| 294 | int compat_poll_wait(struct lttng_poll_event *events, int timeout) |
| 295 | { |
| 296 | int ret, active_fd_count; |
| 297 | int idle_pfd_index = 0; |
| 298 | size_t i; |
| 299 | |
| 300 | if (events == NULL || events->current.events == NULL) { |
| 301 | ERR("poll wait arguments error"); |
| 302 | goto error; |
| 303 | } |
| 304 | assert(events->current.nb_fd >= 0); |
| 305 | |
| 306 | if (events->current.nb_fd == 0) { |
| 307 | /* Return an invalid error to be consistent with epoll. */ |
| 308 | errno = EINVAL; |
| 309 | events->wait.nb_fd = 0; |
| 310 | goto error; |
| 311 | } |
| 312 | |
| 313 | if (events->need_update) { |
| 314 | ret = update_current_events(events); |
| 315 | if (ret < 0) { |
| 316 | errno = ENOMEM; |
| 317 | goto error; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | do { |
| 322 | ret = poll(events->wait.events, events->wait.nb_fd, timeout); |
| 323 | } while (ret == -1 && errno == EINTR); |
| 324 | if (ret < 0) { |
| 325 | /* At this point, every error is fatal */ |
| 326 | PERROR("poll wait"); |
| 327 | goto error; |
| 328 | } |
| 329 | |
| 330 | active_fd_count = ret; |
| 331 | |
| 332 | /* |
| 333 | * Swap all active pollfd structs to the beginning of the |
| 334 | * array to emulate compat-epoll behaviour. This algorithm takes |
| 335 | * advantage of poll's returned value and the burst nature of active |
| 336 | * events on the file descriptors. The while loop guarantees that |
| 337 | * idle_pfd will always point to an idle fd. |
| 338 | */ |
| 339 | if (active_fd_count == events->wait.nb_fd) { |
| 340 | goto end; |
| 341 | } |
| 342 | while (idle_pfd_index < active_fd_count && |
| 343 | events->wait.events[idle_pfd_index].revents != 0) { |
| 344 | idle_pfd_index++; |
| 345 | } |
| 346 | |
| 347 | for (i = idle_pfd_index + 1; idle_pfd_index < active_fd_count; |
| 348 | i++) { |
| 349 | struct pollfd swap_pfd; |
| 350 | struct pollfd *idle_pfd = &events->wait.events[idle_pfd_index]; |
| 351 | struct pollfd *current_pfd = &events->wait.events[i]; |
| 352 | |
| 353 | if (idle_pfd->revents != 0) { |
| 354 | swap_pfd = *current_pfd; |
| 355 | *current_pfd = *idle_pfd; |
| 356 | *idle_pfd = swap_pfd; |
| 357 | idle_pfd_index++; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | end: |
| 362 | return ret; |
| 363 | |
| 364 | error: |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Setup poll set maximum size. |
| 370 | */ |
| 371 | int compat_poll_set_max_size(void) |
| 372 | { |
| 373 | int ret, retval = 0; |
| 374 | struct rlimit lim; |
| 375 | |
| 376 | ret = getrlimit(RLIMIT_NOFILE, &lim); |
| 377 | if (ret < 0) { |
| 378 | PERROR("getrlimit poll RLIMIT_NOFILE"); |
| 379 | retval = -1; |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | poll_max_size = lim.rlim_cur; |
| 384 | end: |
| 385 | if (poll_max_size == 0) { |
| 386 | poll_max_size = DEFAULT_POLL_SIZE; |
| 387 | } |
| 388 | DBG("poll set max size set to %u", poll_max_size); |
| 389 | return retval; |
| 390 | } |