2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: LGPL-2.1-only
11 #include <common/common.hpp>
17 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
19 static inline void __lttng_poll_free(void *events)
25 * epoll(7) implementation.
31 #include <sys/epoll.h>
33 /* See man epoll(7) for this define path */
34 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
37 /* Polling variables compatibility for epoll */
41 LPOLLRDNORM = EPOLLRDNORM,
42 LPOLLRDBAND = EPOLLRDBAND,
43 LPOLLWRNORM = EPOLLWRNORM,
44 LPOLLWRBAND = EPOLLWRBAND,
49 LPOLLRDHUP = EPOLLRDHUP,
50 /* Close on exec feature of epoll */
51 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
52 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
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.
60 LTTNG_CLOEXEC = FD_CLOEXEC,
64 struct compat_epoll_event {
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;
71 #define lttng_poll_event compat_epoll_event
74 __lttng_epoll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd)
77 LTTNG_ASSERT(index != nb_fd);
79 if (index == 0 || nb_fd == 0) {
82 return events->events[index - 1].data.fd;
87 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
88 * being the index of the events array.
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)
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)
100 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
101 static inline int compat_glibc_epoll_create(int size __attribute__((unused)), int flags)
103 return epoll_create1(flags);
106 static inline int compat_glibc_epoll_create(int size, int flags)
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
114 int efd = epoll_create(size);
115 LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1);
121 * Wait on epoll set with the number of fd registered to the lttng_poll_event
122 * data structure (events).
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)
129 * Add a fd to the epoll set and resize the epoll_event structure if needed.
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)
135 * Remove a fd from the epoll set.
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)
141 * Modify an fd's events in the epoll set.
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)
147 * Set up the poll set limits variable poll_max_size
149 extern int compat_epoll_set_max_size();
150 #define lttng_poll_set_max_size() compat_epoll_set_max_size()
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.
157 static inline void lttng_poll_reset(struct lttng_poll_event *events)
159 if (events && events->events) {
160 memset(events->events, 0, events->nb_fd * sizeof(struct epoll_event));
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.
168 static inline void lttng_poll_init(struct lttng_poll_event *events)
170 memset(events, 0, sizeof(struct lttng_poll_event));
171 /* Set fd to -1 so if clean before created, we don't close 0. */
176 * Clean the events structure of a lttng_poll_event. It's the caller
177 * responsability to free the lttng_poll_event memory.
179 static inline void lttng_poll_clean(struct lttng_poll_event *events)
187 if (events->epfd >= 0) {
188 ret = close(events->epfd);
194 __lttng_poll_free((void *) events->events);
197 #else /* HAVE_EPOLL */
199 * Fallback on poll(2) API
206 /* Polling variables compatibility for poll */
210 LPOLLRDNORM = POLLRDNORM,
211 LPOLLRDBAND = POLLRDBAND,
212 LPOLLWRNORM = POLLWRNORM,
213 LPOLLWRBAND = POLLWRBAND,
216 LPOLLRDHUP = POLLRDHUP,
220 #endif /* __USE_GNU */
222 LPOLLHUP = POLLHUP | POLLNVAL,
223 /* Close on exec feature does not exist for poll(2) */
224 LTTNG_CLOEXEC = 0xdead,
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. */
232 struct pollfd *events;
235 struct compat_poll_event {
237 * Modified by the wait action. Updated using current fields if the
238 * need_update flag is set.
240 struct compat_poll_event_array wait;
242 * This is modified by add/del actions being the _current_ flow of
243 * execution before a poll wait is done.
245 struct compat_poll_event_array current;
247 /* Indicate if wait.events need to be updated from current. */
250 #define lttng_poll_event compat_poll_event
253 __lttng_poll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd)
255 LTTNG_ASSERT(events);
256 LTTNG_ASSERT(index != nb_fd);
258 if (index == 0 || nb_fd == 0) {
261 return events->current.events[index - 1].fd;
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.
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)
278 * Create a pollfd structure of size 'size'.
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)
284 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
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)
292 * Add the fd to the pollfd structure. Resize if needed.
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)
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
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)
306 * Modify an fd's events in the poll set.
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)
312 * Set up the poll set limits variable poll_max_size
314 extern int compat_poll_set_max_size(void);
315 #define lttng_poll_set_max_size() compat_poll_set_max_size()
318 * No need to reset a pollfd structure for poll(2)
320 static inline void lttng_poll_reset(struct lttng_poll_event *events __attribute__((unused)))
325 * Initialize an already allocated poll event data structure.
327 static inline void lttng_poll_init(struct lttng_poll_event *events)
329 memset(events, 0, sizeof(struct lttng_poll_event));
333 * Clean the events structure of a lttng_poll_event. It's the caller
334 * responsability to free the lttng_poll_event memory.
336 static inline void lttng_poll_clean(struct lttng_poll_event *events)
339 __lttng_poll_free((void *) events->wait.events);
340 __lttng_poll_free((void *) events->current.events);
344 #endif /* HAVE_EPOLL */
346 #endif /* _LTT_POLL_H */