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