Commit | Line | Data |
---|---|---|
5eb91c98 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
5eb91c98 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
5eb91c98 | 5 | * |
5eb91c98 DG |
6 | */ |
7 | ||
8 | #ifndef _LTT_POLL_H | |
9 | #define _LTT_POLL_H | |
10 | ||
11 | #include <string.h> | |
12 | #include <unistd.h> | |
13 | ||
990570ed | 14 | #include <common/common.h> |
5eb91c98 | 15 | |
7966af57 SM |
16 | #ifdef __cplusplus |
17 | extern "C" { | |
18 | #endif | |
19 | ||
5eb91c98 DG |
20 | /* |
21 | * Used by lttng_poll_clean to free the events structure in a lttng_poll_event. | |
22 | */ | |
23 | static inline void __lttng_poll_free(void *events) | |
24 | { | |
0e428499 | 25 | free(events); |
5eb91c98 DG |
26 | } |
27 | ||
7966af57 SM |
28 | #ifdef __cplusplus |
29 | } | |
30 | #endif | |
31 | ||
5eb91c98 DG |
32 | /* |
33 | * epoll(7) implementation. | |
34 | */ | |
35 | #ifdef HAVE_EPOLL | |
36 | #include <sys/epoll.h> | |
76d7553f | 37 | #include <stdio.h> |
f263b7fd JD |
38 | #include <features.h> |
39 | #include <common/compat/fcntl.h> | |
5eb91c98 | 40 | |
7966af57 SM |
41 | #ifdef __cplusplus |
42 | extern "C" { | |
43 | #endif | |
44 | ||
5eb91c98 | 45 | /* See man epoll(7) for this define path */ |
990570ed | 46 | #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches" |
5eb91c98 DG |
47 | |
48 | enum { | |
49 | /* Polling variables compatibility for epoll */ | |
50 | LPOLLIN = EPOLLIN, | |
51 | LPOLLPRI = EPOLLPRI, | |
52 | LPOLLOUT = EPOLLOUT, | |
53 | LPOLLRDNORM = EPOLLRDNORM, | |
54 | LPOLLRDBAND = EPOLLRDBAND, | |
55 | LPOLLWRNORM = EPOLLWRNORM, | |
56 | LPOLLWRBAND = EPOLLWRBAND, | |
57 | LPOLLMSG = EPOLLMSG, | |
58 | LPOLLERR = EPOLLERR, | |
59 | LPOLLHUP = EPOLLHUP, | |
60 | LPOLLNVAL = EPOLLHUP, | |
61 | LPOLLRDHUP = EPOLLRDHUP, | |
62 | /* Close on exec feature of epoll */ | |
062fc3d8 | 63 | #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) |
5eb91c98 | 64 | LTTNG_CLOEXEC = EPOLL_CLOEXEC, |
f263b7fd JD |
65 | #else |
66 | /* | |
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. | |
71 | */ | |
72 | LTTNG_CLOEXEC = FD_CLOEXEC, | |
73 | #endif | |
5eb91c98 DG |
74 | }; |
75 | ||
76 | struct compat_epoll_event { | |
77 | int epfd; | |
78 | uint32_t nb_fd; /* Current number of fd in events */ | |
d21b0d71 DG |
79 | uint32_t alloc_size; /* Size of events array */ |
80 | uint32_t init_size; /* Initial size of events array */ | |
5eb91c98 DG |
81 | struct epoll_event *events; |
82 | }; | |
83 | #define lttng_poll_event compat_epoll_event | |
84 | ||
beaad64c DG |
85 | static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event *events, |
86 | int index, uint32_t nb_fd) | |
87 | { | |
a0377dfe FD |
88 | LTTNG_ASSERT(events); |
89 | LTTNG_ASSERT(index != nb_fd); | |
beaad64c DG |
90 | |
91 | if (index == 0 || nb_fd == 0) { | |
92 | return -1; | |
93 | } else { | |
94 | return events->events[index - 1].data.fd; | |
95 | } | |
96 | } | |
97 | ||
5eb91c98 DG |
98 | /* |
99 | * For the following calls, consider 'e' to be a lttng_poll_event pointer and i | |
100 | * being the index of the events array. | |
101 | */ | |
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 | |
beaad64c DG |
106 | #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \ |
107 | __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd) | |
5eb91c98 | 108 | |
5a12931e | 109 | /* Create the epoll set. */ |
5eb91c98 DG |
110 | extern int compat_epoll_create(struct lttng_poll_event *events, |
111 | int size, int flags); | |
112 | #define lttng_poll_create(events, size, flags) \ | |
65b1b198 | 113 | compat_epoll_create(events, size, flags) |
5eb91c98 | 114 | |
062fc3d8 | 115 | #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) |
f263b7fd JD |
116 | static inline int compat_glibc_epoll_create(int size __attribute__((unused)), |
117 | int flags) | |
118 | { | |
119 | return epoll_create1(flags); | |
120 | } | |
121 | #else | |
122 | static inline int compat_glibc_epoll_create(int size, int flags) | |
123 | { | |
124 | /* | |
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 | |
128 | * fcntl(..). | |
129 | */ | |
130 | int efd = epoll_create(size); | |
a0377dfe | 131 | LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1); |
f263b7fd JD |
132 | return efd; |
133 | } | |
134 | #endif | |
135 | ||
5eb91c98 DG |
136 | /* |
137 | * Wait on epoll set with the number of fd registered to the lttng_poll_event | |
138 | * data structure (events). | |
139 | */ | |
9f32e9bf MD |
140 | extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout, |
141 | bool interruptible); | |
5eb91c98 | 142 | #define lttng_poll_wait(events, timeout) \ |
9f32e9bf MD |
143 | compat_epoll_wait(events, timeout, false) |
144 | #define lttng_poll_wait_interruptible(events, timeout) \ | |
145 | compat_epoll_wait(events, timeout, true) | |
5eb91c98 DG |
146 | |
147 | /* | |
148 | * Add a fd to the epoll set and resize the epoll_event structure if needed. | |
149 | */ | |
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) \ | |
65b1b198 | 153 | compat_epoll_add(events, fd, req_events) |
5eb91c98 DG |
154 | |
155 | /* | |
156 | * Remove a fd from the epoll set. | |
157 | */ | |
158 | extern int compat_epoll_del(struct lttng_poll_event *events, int fd); | |
159 | #define lttng_poll_del(events, fd) \ | |
65b1b198 | 160 | compat_epoll_del(events, fd) |
5eb91c98 | 161 | |
f057dfc3 JG |
162 | /* |
163 | * Modify an fd's events in the epoll set. | |
164 | */ | |
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) \ | |
2a85be8e | 168 | compat_epoll_mod(events, fd, req_events) |
f057dfc3 | 169 | |
5eb91c98 DG |
170 | /* |
171 | * Set up the poll set limits variable poll_max_size | |
172 | */ | |
dbe23f45 | 173 | extern int compat_epoll_set_max_size(void); |
65b1b198 MD |
174 | #define lttng_poll_set_max_size() \ |
175 | compat_epoll_set_max_size() | |
5eb91c98 DG |
176 | |
177 | /* | |
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. | |
181 | */ | |
182 | static inline void lttng_poll_reset(struct lttng_poll_event *events) | |
183 | { | |
184 | if (events && events->events) { | |
185 | memset(events->events, 0, | |
186 | events->nb_fd * sizeof(struct epoll_event)); | |
187 | } | |
188 | } | |
189 | ||
6d737ce4 DG |
190 | /* |
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. | |
193 | */ | |
194 | static inline void lttng_poll_init(struct lttng_poll_event *events) | |
195 | { | |
c5854b1c | 196 | memset(events, 0, sizeof(struct lttng_poll_event)); |
6d737ce4 DG |
197 | /* Set fd to -1 so if clean before created, we don't close 0. */ |
198 | events->epfd = -1; | |
199 | } | |
200 | ||
5eb91c98 DG |
201 | /* |
202 | * Clean the events structure of a lttng_poll_event. It's the caller | |
203 | * responsability to free the lttng_poll_event memory. | |
204 | */ | |
205 | static inline void lttng_poll_clean(struct lttng_poll_event *events) | |
206 | { | |
76d7553f MD |
207 | int ret; |
208 | ||
3cc04881 DG |
209 | if (!events) { |
210 | return; | |
211 | } | |
212 | ||
213 | if (events->epfd >= 0) { | |
76d7553f MD |
214 | ret = close(events->epfd); |
215 | if (ret) { | |
6f04ed72 | 216 | PERROR("close"); |
76d7553f | 217 | } |
5eb91c98 | 218 | } |
3cc04881 DG |
219 | |
220 | __lttng_poll_free((void *) events->events); | |
5eb91c98 DG |
221 | } |
222 | ||
7966af57 SM |
223 | #ifdef __cplusplus |
224 | } | |
225 | #endif | |
226 | ||
5eb91c98 DG |
227 | #else /* HAVE_EPOLL */ |
228 | /* | |
229 | * Fallback on poll(2) API | |
230 | */ | |
231 | ||
232 | /* Needed for some poll event values */ | |
233 | #ifndef __USE_XOPEN | |
234 | #define __USE_XOPEN | |
235 | #endif | |
236 | ||
237 | /* Needed for some poll event values */ | |
238 | #ifndef __USE_GNU | |
239 | #define __USE_GNU | |
240 | #endif | |
241 | ||
242 | #include <poll.h> | |
243 | #include <stdint.h> | |
244 | ||
7966af57 SM |
245 | #ifdef __cplusplus |
246 | extern "C" { | |
247 | #endif | |
248 | ||
5eb91c98 DG |
249 | enum { |
250 | /* Polling variables compatibility for poll */ | |
251 | LPOLLIN = POLLIN, | |
252 | LPOLLPRI = POLLPRI, | |
253 | LPOLLOUT = POLLOUT, | |
254 | LPOLLRDNORM = POLLRDNORM, | |
255 | LPOLLRDBAND = POLLRDBAND, | |
256 | LPOLLWRNORM = POLLWRNORM, | |
257 | LPOLLWRBAND = POLLWRBAND, | |
1268b9d6 | 258 | #if __linux__ |
5eb91c98 | 259 | LPOLLMSG = POLLMSG, |
1268b9d6 | 260 | LPOLLRDHUP = POLLRDHUP, |
a97dd6ce | 261 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) |
1268b9d6 DG |
262 | LPOLLMSG = 0, |
263 | LPOLLRDHUP = 0, | |
b2c3836f MD |
264 | #else |
265 | #error "Please add support for your OS." | |
1268b9d6 | 266 | #endif /* __linux__ */ |
5eb91c98 DG |
267 | LPOLLERR = POLLERR, |
268 | LPOLLHUP = POLLHUP | POLLNVAL, | |
5eb91c98 DG |
269 | /* Close on exec feature does not exist for poll(2) */ |
270 | LTTNG_CLOEXEC = 0xdead, | |
271 | }; | |
272 | ||
d21b0d71 | 273 | struct compat_poll_event_array { |
5eb91c98 | 274 | uint32_t nb_fd; /* Current number of fd in events */ |
d21b0d71 DG |
275 | uint32_t alloc_size; /* Size of events array */ |
276 | /* Initial size of the pollset. We never shrink below that. */ | |
277 | uint32_t init_size; | |
5eb91c98 DG |
278 | struct pollfd *events; |
279 | }; | |
d21b0d71 DG |
280 | |
281 | struct compat_poll_event { | |
282 | /* | |
283 | * Modified by the wait action. Updated using current fields if the | |
284 | * need_update flag is set. | |
285 | */ | |
286 | struct compat_poll_event_array wait; | |
287 | /* | |
288 | * This is modified by add/del actions being the _current_ flow of | |
289 | * execution before a poll wait is done. | |
290 | */ | |
291 | struct compat_poll_event_array current; | |
dbe23f45 | 292 | |
d21b0d71 DG |
293 | /* Indicate if wait.events need to be updated from current. */ |
294 | int need_update:1; | |
295 | }; | |
5eb91c98 DG |
296 | #define lttng_poll_event compat_poll_event |
297 | ||
beaad64c DG |
298 | static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event *events, |
299 | int index, uint32_t nb_fd) | |
300 | { | |
a0377dfe FD |
301 | LTTNG_ASSERT(events); |
302 | LTTNG_ASSERT(index != nb_fd); | |
beaad64c DG |
303 | |
304 | if (index == 0 || nb_fd == 0) { | |
305 | return -1; | |
306 | } else { | |
307 | return events->current.events[index - 1].fd; | |
308 | } | |
309 | } | |
310 | ||
5eb91c98 DG |
311 | /* |
312 | * For the following calls, consider 'e' to be a lttng_poll_event pointer and i | |
313 | * being the index of the events array. | |
7e222fa8 YL |
314 | * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the |
315 | * current list for test compatibility purposes. | |
5eb91c98 | 316 | */ |
d21b0d71 DG |
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 | |
7e222fa8 | 319 | #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd |
d21b0d71 | 320 | #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size |
beaad64c DG |
321 | #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \ |
322 | __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd) | |
5eb91c98 DG |
323 | |
324 | /* | |
325 | * Create a pollfd structure of size 'size'. | |
326 | */ | |
327 | extern int compat_poll_create(struct lttng_poll_event *events, int size); | |
328 | #define lttng_poll_create(events, size, flags) \ | |
65b1b198 | 329 | compat_poll_create(events, size) |
5eb91c98 DG |
330 | |
331 | /* | |
332 | * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data | |
333 | * structure. | |
334 | */ | |
9f32e9bf MD |
335 | extern int compat_poll_wait(struct lttng_poll_event *events, int timeout, |
336 | bool interruptible); | |
5eb91c98 | 337 | #define lttng_poll_wait(events, timeout) \ |
9f32e9bf MD |
338 | compat_poll_wait(events, timeout, false) |
339 | #define lttng_poll_wait_interruptible(events, timeout) \ | |
340 | compat_poll_wait(events, timeout, true) | |
5eb91c98 DG |
341 | |
342 | /* | |
343 | * Add the fd to the pollfd structure. Resize if needed. | |
344 | */ | |
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) \ | |
65b1b198 | 348 | compat_poll_add(events, fd, req_events) |
5eb91c98 DG |
349 | |
350 | /* | |
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 | |
353 | * one is freed(). | |
354 | */ | |
355 | extern int compat_poll_del(struct lttng_poll_event *events, int fd); | |
356 | #define lttng_poll_del(events, fd) \ | |
65b1b198 | 357 | compat_poll_del(events, fd) |
5eb91c98 | 358 | |
f057dfc3 | 359 | /* |
b14f53d4 | 360 | * Modify an fd's events in the poll set. |
f057dfc3 JG |
361 | */ |
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) \ | |
2a85be8e | 365 | compat_poll_mod(events, fd, req_events) |
f057dfc3 | 366 | |
5eb91c98 DG |
367 | /* |
368 | * Set up the poll set limits variable poll_max_size | |
369 | */ | |
dbe23f45 | 370 | extern int compat_poll_set_max_size(void); |
65b1b198 MD |
371 | #define lttng_poll_set_max_size() \ |
372 | compat_poll_set_max_size() | |
5eb91c98 DG |
373 | |
374 | /* | |
375 | * No need to reset a pollfd structure for poll(2) | |
376 | */ | |
377 | static inline void lttng_poll_reset(struct lttng_poll_event *events) | |
378 | {} | |
379 | ||
6d737ce4 DG |
380 | /* |
381 | * Initialize an already allocated poll event data structure. | |
382 | */ | |
383 | static inline void lttng_poll_init(struct lttng_poll_event *events) | |
384 | { | |
385 | memset(events, 0, sizeof(struct lttng_poll_event)); | |
386 | } | |
387 | ||
5eb91c98 DG |
388 | /* |
389 | * Clean the events structure of a lttng_poll_event. It's the caller | |
390 | * responsability to free the lttng_poll_event memory. | |
391 | */ | |
392 | static inline void lttng_poll_clean(struct lttng_poll_event *events) | |
393 | { | |
394 | if (events) { | |
d21b0d71 DG |
395 | __lttng_poll_free((void *) events->wait.events); |
396 | __lttng_poll_free((void *) events->current.events); | |
5eb91c98 DG |
397 | } |
398 | } | |
399 | ||
7966af57 SM |
400 | #ifdef __cplusplus |
401 | } | |
402 | #endif | |
403 | ||
5eb91c98 DG |
404 | #endif /* HAVE_EPOLL */ |
405 | ||
406 | #endif /* _LTT_POLL_H */ |