2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <common/common.h>
28 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
30 static inline void __lttng_poll_free(void *events
)
36 * epoll(7) implementation.
39 #include <sys/epoll.h>
42 #include <common/compat/fcntl.h>
44 /* See man epoll(7) for this define path */
45 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
48 /* Polling variables compatibility for epoll */
52 LPOLLRDNORM
= EPOLLRDNORM
,
53 LPOLLRDBAND
= EPOLLRDBAND
,
54 LPOLLWRNORM
= EPOLLWRNORM
,
55 LPOLLWRBAND
= EPOLLWRBAND
,
60 LPOLLRDHUP
= EPOLLRDHUP
,
61 /* Close on exec feature of epoll */
62 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
63 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
66 * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with
67 * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor
68 * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it
69 * directly to fcntl(..) instead.
71 LTTNG_CLOEXEC
= FD_CLOEXEC
,
75 struct compat_epoll_event
{
77 uint32_t nb_fd
; /* Current number of fd in events */
78 uint32_t alloc_size
; /* Size of events array */
79 uint32_t init_size
; /* Initial size of events array */
80 struct epoll_event
*events
;
82 #define lttng_poll_event compat_epoll_event
84 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event
*events
,
85 int index
, uint32_t nb_fd
)
88 assert(index
!= nb_fd
);
90 if (index
== 0 || nb_fd
== 0) {
93 return events
->events
[index
- 1].data
.fd
;
98 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
99 * being the index of the events array.
101 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
102 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
103 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
104 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
105 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
106 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
108 /* Create the epoll set. */
109 extern int compat_epoll_create(struct lttng_poll_event
*events
,
110 int size
, int flags
);
111 #define lttng_poll_create(events, size, flags) \
112 compat_epoll_create(events, size, flags)
114 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
115 static inline int compat_glibc_epoll_create(int size
__attribute__((unused
)),
118 return epoll_create1(flags
);
121 static inline int compat_glibc_epoll_create(int size
, int flags
)
124 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
125 * epoll_create(..) also means that we lose the possibility to
126 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
129 int efd
= epoll_create(size
);
130 assert(fcntl(efd
, F_SETFD
, flags
) != -1);
136 * Wait on epoll set with the number of fd registered to the lttng_poll_event
137 * data structure (events).
139 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
141 #define lttng_poll_wait(events, timeout) \
142 compat_epoll_wait(events, timeout, false)
143 #define lttng_poll_wait_interruptible(events, timeout) \
144 compat_epoll_wait(events, timeout, true)
147 * Add a fd to the epoll set and resize the epoll_event structure if needed.
149 extern int compat_epoll_add(struct lttng_poll_event
*events
,
150 int fd
, uint32_t req_events
);
151 #define lttng_poll_add(events, fd, req_events) \
152 compat_epoll_add(events, fd, req_events)
155 * Remove a fd from the epoll set.
157 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
158 #define lttng_poll_del(events, fd) \
159 compat_epoll_del(events, fd)
162 * Modify an fd's events in the epoll set.
164 extern int compat_epoll_mod(struct lttng_poll_event
*events
,
165 int fd
, uint32_t req_events
);
166 #define lttng_poll_mod(events, fd, req_events) \
167 compat_epoll_mod(events, fd, req_events)
170 * Set up the poll set limits variable poll_max_size
172 extern int compat_epoll_set_max_size(void);
173 #define lttng_poll_set_max_size() \
174 compat_epoll_set_max_size()
177 * This function memset with zero the structure since it can be reused at each
178 * round of a main loop. Being in a loop and using a non static number of fds,
179 * this function must be called to insure coherent events with associted fds.
181 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
183 if (events
&& events
->events
) {
184 memset(events
->events
, 0,
185 events
->nb_fd
* sizeof(struct epoll_event
));
190 * Initialize an already allocated poll event data structure. For epoll(), the
191 * epfd is set to -1 to indicate that it's not usable.
193 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
195 memset(events
, 0, sizeof(struct lttng_poll_event
));
196 /* Set fd to -1 so if clean before created, we don't close 0. */
201 * Clean the events structure of a lttng_poll_event. It's the caller
202 * responsability to free the lttng_poll_event memory.
204 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
212 if (events
->epfd
>= 0) {
213 ret
= close(events
->epfd
);
219 __lttng_poll_free((void *) events
->events
);
222 #else /* HAVE_EPOLL */
224 * Fallback on poll(2) API
227 /* Needed for some poll event values */
232 /* Needed for some poll event values */
241 /* Polling variables compatibility for poll */
245 LPOLLRDNORM
= POLLRDNORM
,
246 LPOLLRDBAND
= POLLRDBAND
,
247 LPOLLWRNORM
= POLLWRNORM
,
248 LPOLLWRBAND
= POLLWRBAND
,
251 LPOLLRDHUP
= POLLRDHUP
,
252 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
256 #error "Please add support for your OS."
257 #endif /* __linux__ */
259 LPOLLHUP
= POLLHUP
| POLLNVAL
,
260 /* Close on exec feature does not exist for poll(2) */
261 LTTNG_CLOEXEC
= 0xdead,
264 struct compat_poll_event_array
{
265 uint32_t nb_fd
; /* Current number of fd in events */
266 uint32_t alloc_size
; /* Size of events array */
267 /* Initial size of the pollset. We never shrink below that. */
269 struct pollfd
*events
;
272 struct compat_poll_event
{
274 * Modified by the wait action. Updated using current fields if the
275 * need_update flag is set.
277 struct compat_poll_event_array wait
;
279 * This is modified by add/del actions being the _current_ flow of
280 * execution before a poll wait is done.
282 struct compat_poll_event_array current
;
284 /* Indicate if wait.events need to be updated from current. */
287 #define lttng_poll_event compat_poll_event
289 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event
*events
,
290 int index
, uint32_t nb_fd
)
293 assert(index
!= nb_fd
);
295 if (index
== 0 || nb_fd
== 0) {
298 return events
->current
.events
[index
- 1].fd
;
303 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
304 * being the index of the events array.
305 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
306 * current list for test compatibility purposes.
308 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
309 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
310 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
311 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
312 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
313 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
316 * Create a pollfd structure of size 'size'.
318 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
319 #define lttng_poll_create(events, size, flags) \
320 compat_poll_create(events, size)
323 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
326 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
328 #define lttng_poll_wait(events, timeout) \
329 compat_poll_wait(events, timeout, false)
330 #define lttng_poll_wait_interruptible(events, timeout) \
331 compat_poll_wait(events, timeout, true)
334 * Add the fd to the pollfd structure. Resize if needed.
336 extern int compat_poll_add(struct lttng_poll_event
*events
,
337 int fd
, uint32_t req_events
);
338 #define lttng_poll_add(events, fd, req_events) \
339 compat_poll_add(events, fd, req_events)
342 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
343 * pollfd, data is copied from the old pollfd to the new and, finally, the old
346 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
347 #define lttng_poll_del(events, fd) \
348 compat_poll_del(events, fd)
351 * Modify an fd's events in the poll set.
353 extern int compat_poll_mod(struct lttng_poll_event
*events
,
354 int fd
, uint32_t req_events
);
355 #define lttng_poll_mod(events, fd, req_events) \
356 compat_poll_mod(events, fd, req_events)
359 * Set up the poll set limits variable poll_max_size
361 extern int compat_poll_set_max_size(void);
362 #define lttng_poll_set_max_size() \
363 compat_poll_set_max_size()
366 * No need to reset a pollfd structure for poll(2)
368 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
372 * Initialize an already allocated poll event data structure.
374 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
376 memset(events
, 0, sizeof(struct lttng_poll_event
));
380 * Clean the events structure of a lttng_poll_event. It's the caller
381 * responsability to free the lttng_poll_event memory.
383 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
386 __lttng_poll_free((void *) events
->wait
.events
);
387 __lttng_poll_free((void *) events
->current
.events
);
391 #endif /* HAVE_EPOLL */
393 #endif /* _LTT_POLL_H */