| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <assert.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <sys/resource.h> |
| 13 | #include <sys/time.h> |
| 14 | #include <stdbool.h> |
| 15 | |
| 16 | #include <common/defaults.h> |
| 17 | #include <common/error.h> |
| 18 | #include <common/macros.h> |
| 19 | #include <common/utils.h> |
| 20 | |
| 21 | #include "poll.h" |
| 22 | |
| 23 | |
| 24 | /* |
| 25 | * Maximum number of fd we can monitor. |
| 26 | * |
| 27 | * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by |
| 28 | * getrlimit(2). |
| 29 | */ |
| 30 | static unsigned int poll_max_size; |
| 31 | |
| 32 | /* |
| 33 | * Resize the epoll events structure of the new size. |
| 34 | * |
| 35 | * Return 0 on success or else -1 with the current events pointer untouched. |
| 36 | */ |
| 37 | static int resize_poll_event(struct compat_poll_event_array *array, |
| 38 | uint32_t new_size) |
| 39 | { |
| 40 | struct pollfd *ptr; |
| 41 | |
| 42 | assert(array); |
| 43 | |
| 44 | /* Refuse to resize the array more than the max size. */ |
| 45 | if (new_size > poll_max_size) { |
| 46 | goto error; |
| 47 | } |
| 48 | |
| 49 | ptr = realloc(array->events, new_size * sizeof(*ptr)); |
| 50 | if (ptr == NULL) { |
| 51 | PERROR("realloc epoll add"); |
| 52 | goto error; |
| 53 | } |
| 54 | if (new_size > array->alloc_size) { |
| 55 | /* Zero newly allocated memory */ |
| 56 | memset(ptr + array->alloc_size, 0, |
| 57 | (new_size - array->alloc_size) * sizeof(*ptr)); |
| 58 | } |
| 59 | array->events = ptr; |
| 60 | array->alloc_size = new_size; |
| 61 | |
| 62 | return 0; |
| 63 | |
| 64 | error: |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Update events with the current events object. |
| 70 | */ |
| 71 | static int update_current_events(struct lttng_poll_event *events) |
| 72 | { |
| 73 | int ret; |
| 74 | struct compat_poll_event_array *current, *wait; |
| 75 | |
| 76 | assert(events); |
| 77 | |
| 78 | current = &events->current; |
| 79 | wait = &events->wait; |
| 80 | |
| 81 | wait->nb_fd = current->nb_fd; |
| 82 | if (current->alloc_size != wait->alloc_size) { |
| 83 | ret = resize_poll_event(wait, current->alloc_size); |
| 84 | if (ret < 0) { |
| 85 | goto error; |
| 86 | } |
| 87 | } |
| 88 | memcpy(wait->events, current->events, |
| 89 | current->nb_fd * sizeof(*current->events)); |
| 90 | |
| 91 | /* Update is done. */ |
| 92 | events->need_update = 0; |
| 93 | |
| 94 | return 0; |
| 95 | |
| 96 | error: |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Create pollfd data structure. |
| 102 | */ |
| 103 | LTTNG_HIDDEN |
| 104 | int compat_poll_create(struct lttng_poll_event *events, int size) |
| 105 | { |
| 106 | struct compat_poll_event_array *current, *wait; |
| 107 | |
| 108 | if (events == NULL || size <= 0) { |
| 109 | ERR("Wrong arguments for poll create"); |
| 110 | goto error; |
| 111 | } |
| 112 | |
| 113 | if (!poll_max_size) { |
| 114 | if (lttng_poll_set_max_size()) { |
| 115 | goto error; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* Don't bust the limit here */ |
| 120 | if (size > poll_max_size) { |
| 121 | size = poll_max_size; |
| 122 | } |
| 123 | |
| 124 | /* Reset everything before begining the allocation. */ |
| 125 | memset(events, 0, sizeof(struct lttng_poll_event)); |
| 126 | |
| 127 | current = &events->current; |
| 128 | wait = &events->wait; |
| 129 | |
| 130 | /* This *must* be freed by using lttng_poll_free() */ |
| 131 | wait->events = zmalloc(size * sizeof(struct pollfd)); |
| 132 | if (wait->events == NULL) { |
| 133 | PERROR("zmalloc struct pollfd"); |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | wait->alloc_size = wait->init_size = size; |
| 138 | |
| 139 | current->events = zmalloc(size * sizeof(struct pollfd)); |
| 140 | if (current->events == NULL) { |
| 141 | PERROR("zmalloc struct current pollfd"); |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | current->alloc_size = current->init_size = size; |
| 146 | |
| 147 | return 0; |
| 148 | |
| 149 | error: |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Add fd to pollfd data structure with requested events. |
| 155 | */ |
| 156 | LTTNG_HIDDEN |
| 157 | int compat_poll_add(struct lttng_poll_event *events, int fd, |
| 158 | uint32_t req_events) |
| 159 | { |
| 160 | int new_size, ret, i; |
| 161 | struct compat_poll_event_array *current; |
| 162 | |
| 163 | if (events == NULL || events->current.events == NULL || fd < 0) { |
| 164 | ERR("Bad compat poll add arguments"); |
| 165 | goto error; |
| 166 | } |
| 167 | |
| 168 | current = &events->current; |
| 169 | |
| 170 | /* Check if fd we are trying to add is already there. */ |
| 171 | for (i = 0; i < current->nb_fd; i++) { |
| 172 | if (current->events[i].fd == fd) { |
| 173 | errno = EEXIST; |
| 174 | goto error; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* Resize array if needed. */ |
| 179 | new_size = 1U << utils_get_count_order_u32(current->nb_fd + 1); |
| 180 | if (new_size != current->alloc_size && new_size >= current->init_size) { |
| 181 | ret = resize_poll_event(current, new_size); |
| 182 | if (ret < 0) { |
| 183 | goto error; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | current->events[current->nb_fd].fd = fd; |
| 188 | current->events[current->nb_fd].events = req_events; |
| 189 | current->nb_fd++; |
| 190 | events->need_update = 1; |
| 191 | |
| 192 | DBG("fd %d of %d added to pollfd", fd, current->nb_fd); |
| 193 | |
| 194 | return 0; |
| 195 | |
| 196 | error: |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Modify an fd's events.. |
| 202 | */ |
| 203 | LTTNG_HIDDEN |
| 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 | LTTNG_HIDDEN |
| 241 | int compat_poll_del(struct lttng_poll_event *events, int fd) |
| 242 | { |
| 243 | int i, count = 0, ret; |
| 244 | uint32_t new_size; |
| 245 | struct compat_poll_event_array *current; |
| 246 | |
| 247 | if (events == NULL || events->current.nb_fd == 0 || |
| 248 | events->current.events == NULL || fd < 0) { |
| 249 | goto error; |
| 250 | } |
| 251 | |
| 252 | /* Ease our life a bit. */ |
| 253 | current = &events->current; |
| 254 | |
| 255 | for (i = 0; i < current->nb_fd; i++) { |
| 256 | /* Don't put back the fd we want to delete */ |
| 257 | if (current->events[i].fd != fd) { |
| 258 | current->events[count].fd = current->events[i].fd; |
| 259 | current->events[count].events = current->events[i].events; |
| 260 | count++; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* The fd was not in our set, return no error as with epoll. */ |
| 265 | if (current->nb_fd == count) { |
| 266 | goto end; |
| 267 | } |
| 268 | |
| 269 | /* No fd duplicate should be ever added into array. */ |
| 270 | assert(current->nb_fd - 1 == count); |
| 271 | current->nb_fd = count; |
| 272 | |
| 273 | /* Resize array if needed. */ |
| 274 | new_size = 1U << utils_get_count_order_u32(current->nb_fd); |
| 275 | if (new_size != current->alloc_size && new_size >= current->init_size |
| 276 | && current->nb_fd != 0) { |
| 277 | ret = resize_poll_event(current, new_size); |
| 278 | if (ret < 0) { |
| 279 | goto error; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | events->need_update = 1; |
| 284 | |
| 285 | end: |
| 286 | return 0; |
| 287 | |
| 288 | error: |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Wait on poll() with timeout. Blocking call. |
| 294 | */ |
| 295 | LTTNG_HIDDEN |
| 296 | int compat_poll_wait(struct lttng_poll_event *events, int timeout, |
| 297 | bool interruptible) |
| 298 | { |
| 299 | int ret, active_fd_count; |
| 300 | int idle_pfd_index = 0; |
| 301 | size_t i; |
| 302 | |
| 303 | if (events == NULL || events->current.events == NULL) { |
| 304 | ERR("poll wait arguments error"); |
| 305 | goto error; |
| 306 | } |
| 307 | |
| 308 | if (events->current.nb_fd == 0) { |
| 309 | /* Return an invalid error to be consistent with epoll. */ |
| 310 | errno = EINVAL; |
| 311 | events->wait.nb_fd = 0; |
| 312 | goto error; |
| 313 | } |
| 314 | |
| 315 | if (events->need_update) { |
| 316 | ret = update_current_events(events); |
| 317 | if (ret < 0) { |
| 318 | errno = ENOMEM; |
| 319 | goto error; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | do { |
| 324 | ret = poll(events->wait.events, events->wait.nb_fd, timeout); |
| 325 | } while (!interruptible && ret == -1 && errno == EINTR); |
| 326 | if (ret < 0) { |
| 327 | if (errno != EINTR) { |
| 328 | PERROR("poll wait"); |
| 329 | } |
| 330 | goto error; |
| 331 | } |
| 332 | |
| 333 | active_fd_count = ret; |
| 334 | |
| 335 | /* |
| 336 | * Swap all active pollfd structs to the beginning of the |
| 337 | * array to emulate compat-epoll behaviour. This algorithm takes |
| 338 | * advantage of poll's returned value and the burst nature of active |
| 339 | * events on the file descriptors. The while loop guarantees that |
| 340 | * idle_pfd will always point to an idle fd. |
| 341 | */ |
| 342 | if (active_fd_count == events->wait.nb_fd) { |
| 343 | goto end; |
| 344 | } |
| 345 | while (idle_pfd_index < active_fd_count && |
| 346 | events->wait.events[idle_pfd_index].revents != 0) { |
| 347 | idle_pfd_index++; |
| 348 | } |
| 349 | |
| 350 | for (i = idle_pfd_index + 1; idle_pfd_index < active_fd_count; |
| 351 | i++) { |
| 352 | struct pollfd swap_pfd; |
| 353 | struct pollfd *idle_pfd = &events->wait.events[idle_pfd_index]; |
| 354 | struct pollfd *current_pfd = &events->wait.events[i]; |
| 355 | |
| 356 | if (idle_pfd->revents != 0) { |
| 357 | swap_pfd = *current_pfd; |
| 358 | *current_pfd = *idle_pfd; |
| 359 | *idle_pfd = swap_pfd; |
| 360 | idle_pfd_index++; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | end: |
| 365 | return ret; |
| 366 | |
| 367 | error: |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Setup poll set maximum size. |
| 373 | */ |
| 374 | LTTNG_HIDDEN |
| 375 | int compat_poll_set_max_size(void) |
| 376 | { |
| 377 | int ret, retval = 0; |
| 378 | struct rlimit lim; |
| 379 | |
| 380 | ret = getrlimit(RLIMIT_NOFILE, &lim); |
| 381 | if (ret < 0) { |
| 382 | PERROR("getrlimit poll RLIMIT_NOFILE"); |
| 383 | retval = -1; |
| 384 | goto end; |
| 385 | } |
| 386 | |
| 387 | poll_max_size = lim.rlim_cur; |
| 388 | end: |
| 389 | if (poll_max_size == 0) { |
| 390 | poll_max_size = DEFAULT_POLL_SIZE; |
| 391 | } |
| 392 | DBG("poll set max size set to %u", poll_max_size); |
| 393 | return retval; |
| 394 | } |