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