2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <common/common.h>
21 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
23 static inline void __lttng_poll_free(void *events
)
33 * epoll(7) implementation.
36 #include <sys/epoll.h>
39 #include <common/compat/fcntl.h>
45 /* See man epoll(7) for this define path */
46 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
49 /* Polling variables compatibility for epoll */
53 LPOLLRDNORM
= EPOLLRDNORM
,
54 LPOLLRDBAND
= EPOLLRDBAND
,
55 LPOLLWRNORM
= EPOLLWRNORM
,
56 LPOLLWRBAND
= EPOLLWRBAND
,
61 LPOLLRDHUP
= EPOLLRDHUP
,
62 /* Close on exec feature of epoll */
63 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
64 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
67 * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with
68 * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor
69 * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it
70 * directly to fcntl(..) instead.
72 LTTNG_CLOEXEC
= FD_CLOEXEC
,
76 struct compat_epoll_event
{
78 uint32_t nb_fd
; /* Current number of fd in events */
79 uint32_t alloc_size
; /* Size of events array */
80 uint32_t init_size
; /* Initial size of events array */
81 struct epoll_event
*events
;
83 #define lttng_poll_event compat_epoll_event
85 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event
*events
,
86 int index
, uint32_t nb_fd
)
89 LTTNG_ASSERT(index
!= nb_fd
);
91 if (index
== 0 || nb_fd
== 0) {
94 return events
->events
[index
- 1].data
.fd
;
99 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
100 * being the index of the events array.
102 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
103 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
104 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
105 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
106 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
107 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
109 /* Create the epoll set. */
110 extern int compat_epoll_create(struct lttng_poll_event
*events
,
111 int size
, int flags
);
112 #define lttng_poll_create(events, size, flags) \
113 compat_epoll_create(events, size, flags)
115 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
116 static inline int compat_glibc_epoll_create(int size
__attribute__((unused
)),
119 return epoll_create1(flags
);
122 static inline int compat_glibc_epoll_create(int size
, int flags
)
125 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
126 * epoll_create(..) also means that we lose the possibility to
127 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
130 int efd
= epoll_create(size
);
131 LTTNG_ASSERT(fcntl(efd
, F_SETFD
, flags
) != -1);
137 * Wait on epoll set with the number of fd registered to the lttng_poll_event
138 * data structure (events).
140 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
142 #define lttng_poll_wait(events, timeout) \
143 compat_epoll_wait(events, timeout, false)
144 #define lttng_poll_wait_interruptible(events, timeout) \
145 compat_epoll_wait(events, timeout, true)
148 * Add a fd to the epoll set and resize the epoll_event structure if needed.
150 extern int compat_epoll_add(struct lttng_poll_event
*events
,
151 int fd
, uint32_t req_events
);
152 #define lttng_poll_add(events, fd, req_events) \
153 compat_epoll_add(events, fd, req_events)
156 * Remove a fd from the epoll set.
158 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
159 #define lttng_poll_del(events, fd) \
160 compat_epoll_del(events, fd)
163 * Modify an fd's events in the epoll set.
165 extern int compat_epoll_mod(struct lttng_poll_event
*events
,
166 int fd
, uint32_t req_events
);
167 #define lttng_poll_mod(events, fd, req_events) \
168 compat_epoll_mod(events, fd, req_events)
171 * Set up the poll set limits variable poll_max_size
173 extern int compat_epoll_set_max_size(void);
174 #define lttng_poll_set_max_size() \
175 compat_epoll_set_max_size()
178 * This function memset with zero the structure since it can be reused at each
179 * round of a main loop. Being in a loop and using a non static number of fds,
180 * this function must be called to insure coherent events with associted fds.
182 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
184 if (events
&& events
->events
) {
185 memset(events
->events
, 0,
186 events
->nb_fd
* sizeof(struct epoll_event
));
191 * Initialize an already allocated poll event data structure. For epoll(), the
192 * epfd is set to -1 to indicate that it's not usable.
194 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
196 memset(events
, 0, sizeof(struct lttng_poll_event
));
197 /* Set fd to -1 so if clean before created, we don't close 0. */
202 * Clean the events structure of a lttng_poll_event. It's the caller
203 * responsability to free the lttng_poll_event memory.
205 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
213 if (events
->epfd
>= 0) {
214 ret
= close(events
->epfd
);
220 __lttng_poll_free((void *) events
->events
);
227 #else /* HAVE_EPOLL */
229 * Fallback on poll(2) API
232 /* Needed for some poll event values */
237 /* Needed for some poll event values */
250 /* Polling variables compatibility for poll */
254 LPOLLRDNORM
= POLLRDNORM
,
255 LPOLLRDBAND
= POLLRDBAND
,
256 LPOLLWRNORM
= POLLWRNORM
,
257 LPOLLWRBAND
= POLLWRBAND
,
260 LPOLLRDHUP
= POLLRDHUP
,
261 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
265 #error "Please add support for your OS."
266 #endif /* __linux__ */
268 LPOLLHUP
= POLLHUP
| POLLNVAL
,
269 /* Close on exec feature does not exist for poll(2) */
270 LTTNG_CLOEXEC
= 0xdead,
273 struct compat_poll_event_array
{
274 uint32_t nb_fd
; /* Current number of fd in events */
275 uint32_t alloc_size
; /* Size of events array */
276 /* Initial size of the pollset. We never shrink below that. */
278 struct pollfd
*events
;
281 struct compat_poll_event
{
283 * Modified by the wait action. Updated using current fields if the
284 * need_update flag is set.
286 struct compat_poll_event_array wait
;
288 * This is modified by add/del actions being the _current_ flow of
289 * execution before a poll wait is done.
291 struct compat_poll_event_array current
;
293 /* Indicate if wait.events need to be updated from current. */
296 #define lttng_poll_event compat_poll_event
298 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event
*events
,
299 int index
, uint32_t nb_fd
)
301 LTTNG_ASSERT(events
);
302 LTTNG_ASSERT(index
!= nb_fd
);
304 if (index
== 0 || nb_fd
== 0) {
307 return events
->current
.events
[index
- 1].fd
;
312 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
313 * being the index of the events array.
314 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
315 * current list for test compatibility purposes.
317 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
318 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
319 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
320 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
321 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
322 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
325 * Create a pollfd structure of size 'size'.
327 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
328 #define lttng_poll_create(events, size, flags) \
329 compat_poll_create(events, size)
332 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
335 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
337 #define lttng_poll_wait(events, timeout) \
338 compat_poll_wait(events, timeout, false)
339 #define lttng_poll_wait_interruptible(events, timeout) \
340 compat_poll_wait(events, timeout, true)
343 * Add the fd to the pollfd structure. Resize if needed.
345 extern int compat_poll_add(struct lttng_poll_event
*events
,
346 int fd
, uint32_t req_events
);
347 #define lttng_poll_add(events, fd, req_events) \
348 compat_poll_add(events, fd, req_events)
351 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
352 * pollfd, data is copied from the old pollfd to the new and, finally, the old
355 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
356 #define lttng_poll_del(events, fd) \
357 compat_poll_del(events, fd)
360 * Modify an fd's events in the poll set.
362 extern int compat_poll_mod(struct lttng_poll_event
*events
,
363 int fd
, uint32_t req_events
);
364 #define lttng_poll_mod(events, fd, req_events) \
365 compat_poll_mod(events, fd, req_events)
368 * Set up the poll set limits variable poll_max_size
370 extern int compat_poll_set_max_size(void);
371 #define lttng_poll_set_max_size() \
372 compat_poll_set_max_size()
375 * No need to reset a pollfd structure for poll(2)
377 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
381 * Initialize an already allocated poll event data structure.
383 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
385 memset(events
, 0, sizeof(struct lttng_poll_event
));
389 * Clean the events structure of a lttng_poll_event. It's the caller
390 * responsability to free the lttng_poll_event memory.
392 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
395 __lttng_poll_free((void *) events
->wait
.events
);
396 __lttng_poll_free((void *) events
->current
.events
);
404 #endif /* HAVE_EPOLL */
406 #endif /* _LTT_POLL_H */