| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTT_POLL_H |
| 9 | #define _LTT_POLL_H |
| 10 | |
| 11 | #include <common/common.hpp> |
| 12 | |
| 13 | #include <string.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | /* |
| 17 | * Used by lttng_poll_clean to free the events structure in a lttng_poll_event. |
| 18 | */ |
| 19 | static inline void __lttng_poll_free(void *events) |
| 20 | { |
| 21 | free(events); |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * epoll(7) implementation. |
| 26 | */ |
| 27 | #ifdef HAVE_EPOLL |
| 28 | |
| 29 | #include <features.h> |
| 30 | #include <stdio.h> |
| 31 | #include <sys/epoll.h> |
| 32 | |
| 33 | /* See man epoll(7) for this define path */ |
| 34 | #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches" |
| 35 | |
| 36 | enum { |
| 37 | /* Polling variables compatibility for epoll */ |
| 38 | LPOLLIN = EPOLLIN, |
| 39 | LPOLLPRI = EPOLLPRI, |
| 40 | LPOLLOUT = EPOLLOUT, |
| 41 | LPOLLRDNORM = EPOLLRDNORM, |
| 42 | LPOLLRDBAND = EPOLLRDBAND, |
| 43 | LPOLLWRNORM = EPOLLWRNORM, |
| 44 | LPOLLWRBAND = EPOLLWRBAND, |
| 45 | LPOLLMSG = EPOLLMSG, |
| 46 | LPOLLERR = EPOLLERR, |
| 47 | LPOLLHUP = EPOLLHUP, |
| 48 | LPOLLNVAL = EPOLLHUP, |
| 49 | LPOLLRDHUP = EPOLLRDHUP, |
| 50 | /* Close on exec feature of epoll */ |
| 51 | #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) |
| 52 | LTTNG_CLOEXEC = EPOLL_CLOEXEC, |
| 53 | #else |
| 54 | /* |
| 55 | * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with |
| 56 | * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor |
| 57 | * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it |
| 58 | * directly to fcntl(..) instead. |
| 59 | */ |
| 60 | LTTNG_CLOEXEC = FD_CLOEXEC, |
| 61 | #endif |
| 62 | }; |
| 63 | |
| 64 | struct compat_epoll_event { |
| 65 | int epfd; |
| 66 | uint32_t nb_fd; /* Current number of fd in events */ |
| 67 | uint32_t alloc_size; /* Size of events array */ |
| 68 | uint32_t init_size; /* Initial size of events array */ |
| 69 | struct epoll_event *events; |
| 70 | }; |
| 71 | #define lttng_poll_event compat_epoll_event |
| 72 | |
| 73 | static inline int |
| 74 | __lttng_epoll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd) |
| 75 | { |
| 76 | LTTNG_ASSERT(events); |
| 77 | LTTNG_ASSERT(index != nb_fd); |
| 78 | |
| 79 | if (index == 0 || nb_fd == 0) { |
| 80 | return -1; |
| 81 | } else { |
| 82 | return events->events[index - 1].data.fd; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * For the following calls, consider 'e' to be a lttng_poll_event pointer and i |
| 88 | * being the index of the events array. |
| 89 | */ |
| 90 | #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd |
| 91 | #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events |
| 92 | #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd |
| 93 | #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size |
| 94 | #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd) |
| 95 | |
| 96 | /* Create the epoll set. */ |
| 97 | extern int compat_epoll_create(struct lttng_poll_event *events, int size, int flags); |
| 98 | #define lttng_poll_create(events, size, flags) compat_epoll_create(events, size, flags) |
| 99 | |
| 100 | #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) |
| 101 | static inline int compat_glibc_epoll_create(int size __attribute__((unused)), int flags) |
| 102 | { |
| 103 | return epoll_create1(flags); |
| 104 | } |
| 105 | #else |
| 106 | static inline int compat_glibc_epoll_create(int size, int flags) |
| 107 | { |
| 108 | /* |
| 109 | * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to |
| 110 | * epoll_create(..) also means that we lose the possibility to |
| 111 | * directly set the EPOLL_CLOEXEC, so try and do it anyway but through |
| 112 | * fcntl(..). |
| 113 | */ |
| 114 | int efd = epoll_create(size); |
| 115 | LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1); |
| 116 | return efd; |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | /* |
| 121 | * Wait on epoll set with the number of fd registered to the lttng_poll_event |
| 122 | * data structure (events). |
| 123 | */ |
| 124 | extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout, bool interruptible); |
| 125 | #define lttng_poll_wait(events, timeout) compat_epoll_wait(events, timeout, false) |
| 126 | #define lttng_poll_wait_interruptible(events, timeout) compat_epoll_wait(events, timeout, true) |
| 127 | |
| 128 | /* |
| 129 | * Add a fd to the epoll set and resize the epoll_event structure if needed. |
| 130 | */ |
| 131 | extern int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events); |
| 132 | #define lttng_poll_add(events, fd, req_events) compat_epoll_add(events, fd, req_events) |
| 133 | |
| 134 | /* |
| 135 | * Remove a fd from the epoll set. |
| 136 | */ |
| 137 | extern int compat_epoll_del(struct lttng_poll_event *events, int fd); |
| 138 | #define lttng_poll_del(events, fd) compat_epoll_del(events, fd) |
| 139 | |
| 140 | /* |
| 141 | * Modify an fd's events in the epoll set. |
| 142 | */ |
| 143 | extern int compat_epoll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events); |
| 144 | #define lttng_poll_mod(events, fd, req_events) compat_epoll_mod(events, fd, req_events) |
| 145 | |
| 146 | /* |
| 147 | * Set up the poll set limits variable poll_max_size |
| 148 | */ |
| 149 | extern int compat_epoll_set_max_size(); |
| 150 | #define lttng_poll_set_max_size() compat_epoll_set_max_size() |
| 151 | |
| 152 | /* |
| 153 | * This function memset with zero the structure since it can be reused at each |
| 154 | * round of a main loop. Being in a loop and using a non static number of fds, |
| 155 | * this function must be called to insure coherent events with associted fds. |
| 156 | */ |
| 157 | static inline void lttng_poll_reset(struct lttng_poll_event *events) |
| 158 | { |
| 159 | if (events && events->events) { |
| 160 | memset(events->events, 0, events->nb_fd * sizeof(struct epoll_event)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Initialize an already allocated poll event data structure. For epoll(), the |
| 166 | * epfd is set to -1 to indicate that it's not usable. |
| 167 | */ |
| 168 | static inline void lttng_poll_init(struct lttng_poll_event *events) |
| 169 | { |
| 170 | memset(events, 0, sizeof(struct lttng_poll_event)); |
| 171 | /* Set fd to -1 so if clean before created, we don't close 0. */ |
| 172 | events->epfd = -1; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Clean the events structure of a lttng_poll_event. It's the caller |
| 177 | * responsability to free the lttng_poll_event memory. |
| 178 | */ |
| 179 | static inline void lttng_poll_clean(struct lttng_poll_event *events) |
| 180 | { |
| 181 | int ret; |
| 182 | |
| 183 | if (!events) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (events->epfd >= 0) { |
| 188 | ret = close(events->epfd); |
| 189 | if (ret) { |
| 190 | PERROR("close"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | __lttng_poll_free((void *) events->events); |
| 195 | } |
| 196 | |
| 197 | #else /* HAVE_EPOLL */ |
| 198 | /* |
| 199 | * Fallback on poll(2) API |
| 200 | */ |
| 201 | |
| 202 | #include <poll.h> |
| 203 | #include <stdint.h> |
| 204 | |
| 205 | enum { |
| 206 | /* Polling variables compatibility for poll */ |
| 207 | LPOLLIN = POLLIN, |
| 208 | LPOLLPRI = POLLPRI, |
| 209 | LPOLLOUT = POLLOUT, |
| 210 | LPOLLRDNORM = POLLRDNORM, |
| 211 | LPOLLRDBAND = POLLRDBAND, |
| 212 | LPOLLWRNORM = POLLWRNORM, |
| 213 | LPOLLWRBAND = POLLWRBAND, |
| 214 | #ifdef __USE_GNU |
| 215 | LPOLLMSG = POLLMSG, |
| 216 | LPOLLRDHUP = POLLRDHUP, |
| 217 | #else |
| 218 | LPOLLMSG = 0, |
| 219 | LPOLLRDHUP = 0, |
| 220 | #endif /* __USE_GNU */ |
| 221 | LPOLLERR = POLLERR, |
| 222 | LPOLLHUP = POLLHUP | POLLNVAL, |
| 223 | /* Close on exec feature does not exist for poll(2) */ |
| 224 | LTTNG_CLOEXEC = 0xdead, |
| 225 | }; |
| 226 | |
| 227 | struct compat_poll_event_array { |
| 228 | uint32_t nb_fd; /* Current number of fd in events */ |
| 229 | uint32_t alloc_size; /* Size of events array */ |
| 230 | /* Initial size of the pollset. We never shrink below that. */ |
| 231 | uint32_t init_size; |
| 232 | struct pollfd *events; |
| 233 | }; |
| 234 | |
| 235 | struct compat_poll_event { |
| 236 | /* |
| 237 | * Modified by the wait action. Updated using current fields if the |
| 238 | * need_update flag is set. |
| 239 | */ |
| 240 | struct compat_poll_event_array wait; |
| 241 | /* |
| 242 | * This is modified by add/del actions being the _current_ flow of |
| 243 | * execution before a poll wait is done. |
| 244 | */ |
| 245 | struct compat_poll_event_array current; |
| 246 | |
| 247 | /* Indicate if wait.events need to be updated from current. */ |
| 248 | int need_update:1; |
| 249 | }; |
| 250 | #define lttng_poll_event compat_poll_event |
| 251 | |
| 252 | static inline int |
| 253 | __lttng_poll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd) |
| 254 | { |
| 255 | LTTNG_ASSERT(events); |
| 256 | LTTNG_ASSERT(index != nb_fd); |
| 257 | |
| 258 | if (index == 0 || nb_fd == 0) { |
| 259 | return -1; |
| 260 | } else { |
| 261 | return events->current.events[index - 1].fd; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * For the following calls, consider 'e' to be a lttng_poll_event pointer and i |
| 267 | * being the index of the events array. |
| 268 | * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the |
| 269 | * current list for test compatibility purposes. |
| 270 | */ |
| 271 | #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd |
| 272 | #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents |
| 273 | #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd |
| 274 | #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size |
| 275 | #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd) |
| 276 | |
| 277 | /* |
| 278 | * Create a pollfd structure of size 'size'. |
| 279 | */ |
| 280 | extern int compat_poll_create(struct lttng_poll_event *events, int size); |
| 281 | #define lttng_poll_create(events, size, flags) compat_poll_create(events, size) |
| 282 | |
| 283 | /* |
| 284 | * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data |
| 285 | * structure. |
| 286 | */ |
| 287 | extern int compat_poll_wait(struct lttng_poll_event *events, int timeout, bool interruptible); |
| 288 | #define lttng_poll_wait(events, timeout) compat_poll_wait(events, timeout, false) |
| 289 | #define lttng_poll_wait_interruptible(events, timeout) compat_poll_wait(events, timeout, true) |
| 290 | |
| 291 | /* |
| 292 | * Add the fd to the pollfd structure. Resize if needed. |
| 293 | */ |
| 294 | extern int compat_poll_add(struct lttng_poll_event *events, int fd, uint32_t req_events); |
| 295 | #define lttng_poll_add(events, fd, req_events) compat_poll_add(events, fd, req_events) |
| 296 | |
| 297 | /* |
| 298 | * Remove the fd from the pollfd. Memory allocation is done to recreate a new |
| 299 | * pollfd, data is copied from the old pollfd to the new and, finally, the old |
| 300 | * one is freed(). |
| 301 | */ |
| 302 | extern int compat_poll_del(struct lttng_poll_event *events, int fd); |
| 303 | #define lttng_poll_del(events, fd) compat_poll_del(events, fd) |
| 304 | |
| 305 | /* |
| 306 | * Modify an fd's events in the poll set. |
| 307 | */ |
| 308 | extern int compat_poll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events); |
| 309 | #define lttng_poll_mod(events, fd, req_events) compat_poll_mod(events, fd, req_events) |
| 310 | |
| 311 | /* |
| 312 | * Set up the poll set limits variable poll_max_size |
| 313 | */ |
| 314 | extern int compat_poll_set_max_size(void); |
| 315 | #define lttng_poll_set_max_size() compat_poll_set_max_size() |
| 316 | |
| 317 | /* |
| 318 | * No need to reset a pollfd structure for poll(2) |
| 319 | */ |
| 320 | static inline void lttng_poll_reset(struct lttng_poll_event *events __attribute__((unused))) |
| 321 | { |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Initialize an already allocated poll event data structure. |
| 326 | */ |
| 327 | static inline void lttng_poll_init(struct lttng_poll_event *events) |
| 328 | { |
| 329 | memset(events, 0, sizeof(struct lttng_poll_event)); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Clean the events structure of a lttng_poll_event. It's the caller |
| 334 | * responsability to free the lttng_poll_event memory. |
| 335 | */ |
| 336 | static inline void lttng_poll_clean(struct lttng_poll_event *events) |
| 337 | { |
| 338 | if (events) { |
| 339 | __lttng_poll_free((void *) events->wait.events); |
| 340 | __lttng_poll_free((void *) events->current.events); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | #endif /* HAVE_EPOLL */ |
| 345 | |
| 346 | #endif /* _LTT_POLL_H */ |