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.
28 #include <common/compat/fcntl.hpp>
32 #include <sys/epoll.h>
34 /* See man epoll(7) for this define path */
35 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
38 /* Polling variables compatibility for epoll */
42 LPOLLRDNORM = EPOLLRDNORM,
43 LPOLLRDBAND = EPOLLRDBAND,
44 LPOLLWRNORM = EPOLLWRNORM,
45 LPOLLWRBAND = EPOLLWRBAND,
50 LPOLLRDHUP = EPOLLRDHUP,
51 /* Close on exec feature of epoll */
52 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
53 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
56 * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with
57 * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor
58 * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it
59 * directly to fcntl(..) instead.
61 LTTNG_CLOEXEC = FD_CLOEXEC,
65 struct compat_epoll_event {
67 uint32_t nb_fd; /* Current number of fd in events */
68 uint32_t alloc_size; /* Size of events array */
69 uint32_t init_size; /* Initial size of events array */
70 struct epoll_event *events;
72 #define lttng_poll_event compat_epoll_event
75 __lttng_epoll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd)
78 LTTNG_ASSERT(index != nb_fd);
80 if (index == 0 || nb_fd == 0) {
83 return events->events[index - 1].data.fd;
88 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
89 * being the index of the events array.
91 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
92 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
93 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
94 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
95 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
97 /* Create the epoll set. */
98 extern int compat_epoll_create(struct lttng_poll_event *events, int size, int flags);
99 #define lttng_poll_create(events, size, flags) compat_epoll_create(events, size, flags)
101 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
102 static inline int compat_glibc_epoll_create(int size __attribute__((unused)), int flags)
104 return epoll_create1(flags);
107 static inline int compat_glibc_epoll_create(int size, int flags)
110 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
111 * epoll_create(..) also means that we lose the possibility to
112 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
115 int efd = epoll_create(size);
116 LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1);
122 * Wait on epoll set with the number of fd registered to the lttng_poll_event
123 * data structure (events).
125 extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout, bool interruptible);
126 #define lttng_poll_wait(events, timeout) compat_epoll_wait(events, timeout, false)
127 #define lttng_poll_wait_interruptible(events, timeout) compat_epoll_wait(events, timeout, true)
130 * Add a fd to the epoll set and resize the epoll_event structure if needed.
132 extern int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events);
133 #define lttng_poll_add(events, fd, req_events) compat_epoll_add(events, fd, req_events)
136 * Remove a fd from the epoll set.
138 extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
139 #define lttng_poll_del(events, fd) compat_epoll_del(events, fd)
142 * Modify an fd's events in the epoll set.
144 extern int compat_epoll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events);
145 #define lttng_poll_mod(events, fd, req_events) compat_epoll_mod(events, fd, req_events)
148 * Set up the poll set limits variable poll_max_size
150 extern int compat_epoll_set_max_size();
151 #define lttng_poll_set_max_size() compat_epoll_set_max_size()
154 * This function memset with zero the structure since it can be reused at each
155 * round of a main loop. Being in a loop and using a non static number of fds,
156 * this function must be called to insure coherent events with associted fds.
158 static inline void lttng_poll_reset(struct lttng_poll_event *events)
160 if (events && events->events) {
161 memset(events->events, 0, events->nb_fd * sizeof(struct epoll_event));
166 * Initialize an already allocated poll event data structure. For epoll(), the
167 * epfd is set to -1 to indicate that it's not usable.
169 static inline void lttng_poll_init(struct lttng_poll_event *events)
171 memset(events, 0, sizeof(struct lttng_poll_event));
172 /* Set fd to -1 so if clean before created, we don't close 0. */
177 * Clean the events structure of a lttng_poll_event. It's the caller
178 * responsability to free the lttng_poll_event memory.
180 static inline void lttng_poll_clean(struct lttng_poll_event *events)
188 if (events->epfd >= 0) {
189 ret = close(events->epfd);
195 __lttng_poll_free((void *) events->events);
198 #else /* HAVE_EPOLL */
200 * Fallback on poll(2) API
207 /* Polling variables compatibility for poll */
211 LPOLLRDNORM = POLLRDNORM,
212 LPOLLRDBAND = POLLRDBAND,
213 LPOLLWRNORM = POLLWRNORM,
214 LPOLLWRBAND = POLLWRBAND,
217 LPOLLRDHUP = POLLRDHUP,
221 #endif /* __USE_GNU */
223 LPOLLHUP = POLLHUP | POLLNVAL,
224 /* Close on exec feature does not exist for poll(2) */
225 LTTNG_CLOEXEC = 0xdead,
228 struct compat_poll_event_array {
229 uint32_t nb_fd; /* Current number of fd in events */
230 uint32_t alloc_size; /* Size of events array */
231 /* Initial size of the pollset. We never shrink below that. */
233 struct pollfd *events;
236 struct compat_poll_event {
238 * Modified by the wait action. Updated using current fields if the
239 * need_update flag is set.
241 struct compat_poll_event_array wait;
243 * This is modified by add/del actions being the _current_ flow of
244 * execution before a poll wait is done.
246 struct compat_poll_event_array current;
248 /* Indicate if wait.events need to be updated from current. */
251 #define lttng_poll_event compat_poll_event
254 __lttng_poll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd)
256 LTTNG_ASSERT(events);
257 LTTNG_ASSERT(index != nb_fd);
259 if (index == 0 || nb_fd == 0) {
262 return events->current.events[index - 1].fd;
267 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
268 * being the index of the events array.
269 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
270 * current list for test compatibility purposes.
272 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
273 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
274 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
275 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
276 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
279 * Create a pollfd structure of size 'size'.
281 extern int compat_poll_create(struct lttng_poll_event *events, int size);
282 #define lttng_poll_create(events, size, flags) compat_poll_create(events, size)
285 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
288 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout, bool interruptible);
289 #define lttng_poll_wait(events, timeout) compat_poll_wait(events, timeout, false)
290 #define lttng_poll_wait_interruptible(events, timeout) compat_poll_wait(events, timeout, true)
293 * Add the fd to the pollfd structure. Resize if needed.
295 extern int compat_poll_add(struct lttng_poll_event *events, int fd, uint32_t req_events);
296 #define lttng_poll_add(events, fd, req_events) compat_poll_add(events, fd, req_events)
299 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
300 * pollfd, data is copied from the old pollfd to the new and, finally, the old
303 extern int compat_poll_del(struct lttng_poll_event *events, int fd);
304 #define lttng_poll_del(events, fd) compat_poll_del(events, fd)
307 * Modify an fd's events in the poll set.
309 extern int compat_poll_mod(struct lttng_poll_event *events, int fd, uint32_t req_events);
310 #define lttng_poll_mod(events, fd, req_events) compat_poll_mod(events, fd, req_events)
313 * Set up the poll set limits variable poll_max_size
315 extern int compat_poll_set_max_size(void);
316 #define lttng_poll_set_max_size() compat_poll_set_max_size()
319 * No need to reset a pollfd structure for poll(2)
321 static inline void lttng_poll_reset(struct lttng_poll_event *events __attribute__((unused)))
326 * Initialize an already allocated poll event data structure.
328 static inline void lttng_poll_init(struct lttng_poll_event *events)
330 memset(events, 0, sizeof(struct lttng_poll_event));
334 * Clean the events structure of a lttng_poll_event. It's the caller
335 * responsability to free the lttng_poll_event memory.
337 static inline void lttng_poll_clean(struct lttng_poll_event *events)
340 __lttng_poll_free((void *) events->wait.events);
341 __lttng_poll_free((void *) events->current.events);
345 #endif /* HAVE_EPOLL */
347 #endif /* _LTT_POLL_H */