common: compile libcompat as C++
[lttng-tools.git] / src / common / compat / poll.h
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _LTT_POLL_H
9 #define _LTT_POLL_H
10
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <common/common.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
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 {
25 free(events);
26 }
27
28 #ifdef __cplusplus
29 }
30 #endif
31
32 /*
33 * epoll(7) implementation.
34 */
35 #ifdef HAVE_EPOLL
36 #include <sys/epoll.h>
37 #include <stdio.h>
38 #include <features.h>
39 #include <common/compat/fcntl.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /* See man epoll(7) for this define path */
46 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
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 */
63 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
64 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
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
74 };
75
76 struct compat_epoll_event {
77 int epfd;
78 uint32_t nb_fd; /* Current number of fd in events */
79 uint32_t alloc_size; /* Size of events array */
80 uint32_t init_size; /* Initial size of events array */
81 struct epoll_event *events;
82 };
83 #define lttng_poll_event compat_epoll_event
84
85 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event *events,
86 int index, uint32_t nb_fd)
87 {
88 LTTNG_ASSERT(events);
89 LTTNG_ASSERT(index != nb_fd);
90
91 if (index == 0 || nb_fd == 0) {
92 return -1;
93 } else {
94 return events->events[index - 1].data.fd;
95 }
96 }
97
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
106 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
107 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
108
109 /* Create the epoll set. */
110 extern int compat_epoll_create(struct lttng_poll_event *events,
111 int size, int flags);
112 #define lttng_poll_create(events, size, flags) \
113 compat_epoll_create(events, size, flags)
114
115 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
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);
131 LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1);
132 return efd;
133 }
134 #endif
135
136 /*
137 * Wait on epoll set with the number of fd registered to the lttng_poll_event
138 * data structure (events).
139 */
140 extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout,
141 bool interruptible);
142 #define lttng_poll_wait(events, timeout) \
143 compat_epoll_wait(events, timeout, false)
144 #define lttng_poll_wait_interruptible(events, timeout) \
145 compat_epoll_wait(events, timeout, true)
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) \
153 compat_epoll_add(events, fd, req_events)
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) \
160 compat_epoll_del(events, fd)
161
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) \
168 compat_epoll_mod(events, fd, req_events)
169
170 /*
171 * Set up the poll set limits variable poll_max_size
172 */
173 extern int compat_epoll_set_max_size(void);
174 #define lttng_poll_set_max_size() \
175 compat_epoll_set_max_size()
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
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 {
196 memset(events, 0, sizeof(struct lttng_poll_event));
197 /* Set fd to -1 so if clean before created, we don't close 0. */
198 events->epfd = -1;
199 }
200
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 {
207 int ret;
208
209 if (!events) {
210 return;
211 }
212
213 if (events->epfd >= 0) {
214 ret = close(events->epfd);
215 if (ret) {
216 PERROR("close");
217 }
218 }
219
220 __lttng_poll_free((void *) events->events);
221 }
222
223 #ifdef __cplusplus
224 }
225 #endif
226
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
245 #ifdef __cplusplus
246 extern "C" {
247 #endif
248
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,
258 #if __linux__
259 LPOLLMSG = POLLMSG,
260 LPOLLRDHUP = POLLRDHUP,
261 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
262 LPOLLMSG = 0,
263 LPOLLRDHUP = 0,
264 #else
265 #error "Please add support for your OS."
266 #endif /* __linux__ */
267 LPOLLERR = POLLERR,
268 LPOLLHUP = POLLHUP | POLLNVAL,
269 /* Close on exec feature does not exist for poll(2) */
270 LTTNG_CLOEXEC = 0xdead,
271 };
272
273 struct compat_poll_event_array {
274 uint32_t nb_fd; /* Current number of fd in events */
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;
278 struct pollfd *events;
279 };
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;
292
293 /* Indicate if wait.events need to be updated from current. */
294 int need_update:1;
295 };
296 #define lttng_poll_event compat_poll_event
297
298 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event *events,
299 int index, uint32_t nb_fd)
300 {
301 LTTNG_ASSERT(events);
302 LTTNG_ASSERT(index != nb_fd);
303
304 if (index == 0 || nb_fd == 0) {
305 return -1;
306 } else {
307 return events->current.events[index - 1].fd;
308 }
309 }
310
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.
314 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
315 * current list for test compatibility purposes.
316 */
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
319 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
320 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
321 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
322 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
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) \
329 compat_poll_create(events, size)
330
331 /*
332 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
333 * structure.
334 */
335 extern int compat_poll_wait(struct lttng_poll_event *events, int timeout,
336 bool interruptible);
337 #define lttng_poll_wait(events, timeout) \
338 compat_poll_wait(events, timeout, false)
339 #define lttng_poll_wait_interruptible(events, timeout) \
340 compat_poll_wait(events, timeout, true)
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) \
348 compat_poll_add(events, fd, req_events)
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) \
357 compat_poll_del(events, fd)
358
359 /*
360 * Modify an fd's events in the poll set.
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) \
365 compat_poll_mod(events, fd, req_events)
366
367 /*
368 * Set up the poll set limits variable poll_max_size
369 */
370 extern int compat_poll_set_max_size(void);
371 #define lttng_poll_set_max_size() \
372 compat_poll_set_max_size()
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
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
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) {
395 __lttng_poll_free((void *) events->wait.events);
396 __lttng_poll_free((void *) events->current.events);
397 }
398 }
399
400 #ifdef __cplusplus
401 }
402 #endif
403
404 #endif /* HAVE_EPOLL */
405
406 #endif /* _LTT_POLL_H */
This page took 0.037883 seconds and 4 git commands to generate.