2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/common.h>
18 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
20 static inline void __lttng_poll_free(void *events
)
26 * epoll(7) implementation.
29 #include <sys/epoll.h>
32 #include <common/compat/fcntl.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
74 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event
*events
,
75 int index
, uint32_t nb_fd
)
78 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) \
96 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
98 /* Create the epoll set. */
99 extern int compat_epoll_create(struct lttng_poll_event
*events
,
100 int size
, int flags
);
101 #define lttng_poll_create(events, size, flags) \
102 compat_epoll_create(events, size, flags)
104 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
105 static inline int compat_glibc_epoll_create(int size
__attribute__((unused
)),
108 return epoll_create1(flags
);
111 static inline int compat_glibc_epoll_create(int size
, int flags
)
114 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
115 * epoll_create(..) also means that we lose the possibility to
116 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
119 int efd
= epoll_create(size
);
120 assert(fcntl(efd
, F_SETFD
, flags
) != -1);
126 * Wait on epoll set with the number of fd registered to the lttng_poll_event
127 * data structure (events).
129 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
131 #define lttng_poll_wait(events, timeout) \
132 compat_epoll_wait(events, timeout, false)
133 #define lttng_poll_wait_interruptible(events, timeout) \
134 compat_epoll_wait(events, timeout, true)
137 * Add a fd to the epoll set and resize the epoll_event structure if needed.
139 extern int compat_epoll_add(struct lttng_poll_event
*events
,
140 int fd
, uint32_t req_events
);
141 #define lttng_poll_add(events, fd, req_events) \
142 compat_epoll_add(events, fd, req_events)
145 * Remove a fd from the epoll set.
147 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
148 #define lttng_poll_del(events, fd) \
149 compat_epoll_del(events, fd)
152 * Modify an fd's events in the epoll set.
154 extern int compat_epoll_mod(struct lttng_poll_event
*events
,
155 int fd
, uint32_t req_events
);
156 #define lttng_poll_mod(events, fd, req_events) \
157 compat_epoll_mod(events, fd, req_events)
160 * Set up the poll set limits variable poll_max_size
162 extern int compat_epoll_set_max_size(void);
163 #define lttng_poll_set_max_size() \
164 compat_epoll_set_max_size()
167 * This function memset with zero the structure since it can be reused at each
168 * round of a main loop. Being in a loop and using a non static number of fds,
169 * this function must be called to insure coherent events with associted fds.
171 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
173 if (events
&& events
->events
) {
174 memset(events
->events
, 0,
175 events
->nb_fd
* sizeof(struct epoll_event
));
180 * Initialize an already allocated poll event data structure. For epoll(), the
181 * epfd is set to -1 to indicate that it's not usable.
183 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
185 memset(events
, 0, sizeof(struct lttng_poll_event
));
186 /* Set fd to -1 so if clean before created, we don't close 0. */
191 * Clean the events structure of a lttng_poll_event. It's the caller
192 * responsability to free the lttng_poll_event memory.
194 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
202 if (events
->epfd
>= 0) {
203 ret
= close(events
->epfd
);
209 __lttng_poll_free((void *) events
->events
);
212 #else /* HAVE_EPOLL */
214 * Fallback on poll(2) API
217 /* Needed for some poll event values */
222 /* Needed for some poll event values */
231 /* Polling variables compatibility for poll */
235 LPOLLRDNORM
= POLLRDNORM
,
236 LPOLLRDBAND
= POLLRDBAND
,
237 LPOLLWRNORM
= POLLWRNORM
,
238 LPOLLWRBAND
= POLLWRBAND
,
241 LPOLLRDHUP
= POLLRDHUP
,
242 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
246 #error "Please add support for your OS."
247 #endif /* __linux__ */
249 LPOLLHUP
= POLLHUP
| POLLNVAL
,
250 /* Close on exec feature does not exist for poll(2) */
251 LTTNG_CLOEXEC
= 0xdead,
254 struct compat_poll_event_array
{
255 uint32_t nb_fd
; /* Current number of fd in events */
256 uint32_t alloc_size
; /* Size of events array */
257 /* Initial size of the pollset. We never shrink below that. */
259 struct pollfd
*events
;
262 struct compat_poll_event
{
264 * Modified by the wait action. Updated using current fields if the
265 * need_update flag is set.
267 struct compat_poll_event_array wait
;
269 * This is modified by add/del actions being the _current_ flow of
270 * execution before a poll wait is done.
272 struct compat_poll_event_array current
;
274 /* Indicate if wait.events need to be updated from current. */
277 #define lttng_poll_event compat_poll_event
279 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event
*events
,
280 int index
, uint32_t nb_fd
)
283 assert(index
!= nb_fd
);
285 if (index
== 0 || nb_fd
== 0) {
288 return events
->current
.events
[index
- 1].fd
;
293 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
294 * being the index of the events array.
295 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
296 * current list for test compatibility purposes.
298 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
299 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
300 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
301 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
302 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
303 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
306 * Create a pollfd structure of size 'size'.
308 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
309 #define lttng_poll_create(events, size, flags) \
310 compat_poll_create(events, size)
313 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
316 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
318 #define lttng_poll_wait(events, timeout) \
319 compat_poll_wait(events, timeout, false)
320 #define lttng_poll_wait_interruptible(events, timeout) \
321 compat_poll_wait(events, timeout, true)
324 * Add the fd to the pollfd structure. Resize if needed.
326 extern int compat_poll_add(struct lttng_poll_event
*events
,
327 int fd
, uint32_t req_events
);
328 #define lttng_poll_add(events, fd, req_events) \
329 compat_poll_add(events, fd, req_events)
332 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
333 * pollfd, data is copied from the old pollfd to the new and, finally, the old
336 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
337 #define lttng_poll_del(events, fd) \
338 compat_poll_del(events, fd)
341 * Modify an fd's events in the poll set.
343 extern int compat_poll_mod(struct lttng_poll_event
*events
,
344 int fd
, uint32_t req_events
);
345 #define lttng_poll_mod(events, fd, req_events) \
346 compat_poll_mod(events, fd, req_events)
349 * Set up the poll set limits variable poll_max_size
351 extern int compat_poll_set_max_size(void);
352 #define lttng_poll_set_max_size() \
353 compat_poll_set_max_size()
356 * No need to reset a pollfd structure for poll(2)
358 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
362 * Initialize an already allocated poll event data structure.
364 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
366 memset(events
, 0, sizeof(struct lttng_poll_event
));
370 * Clean the events structure of a lttng_poll_event. It's the caller
371 * responsability to free the lttng_poll_event memory.
373 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
376 __lttng_poll_free((void *) events
->wait
.events
);
377 __lttng_poll_free((void *) events
->current
.events
);
381 #endif /* HAVE_EPOLL */
383 #endif /* _LTT_POLL_H */