Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / compat / poll.hpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-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.hpp>
15
16/*
17 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
18 */
19static inline void __lttng_poll_free(void *events)
20{
21 free(events);
22}
23
24/*
25 * epoll(7) implementation.
26 */
27#ifdef HAVE_EPOLL
28#include <sys/epoll.h>
29#include <stdio.h>
30#include <features.h>
31#include <common/compat/fcntl.hpp>
32
33/* See man epoll(7) for this define path */
34#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
35
36enum {
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 */
51#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
52 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
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
62};
63
64struct compat_epoll_event {
65 int epfd;
66 uint32_t nb_fd; /* Current number of fd in events */
67 uint32_t alloc_size; /* Size of events array */
68 uint32_t init_size; /* Initial size of events array */
69 struct epoll_event *events;
70};
71#define lttng_poll_event compat_epoll_event
72
73static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event *events,
74 int index, uint32_t nb_fd)
75{
76 LTTNG_ASSERT(events);
77 LTTNG_ASSERT(index != nb_fd);
78
79 if (index == 0 || nb_fd == 0) {
80 return -1;
81 } else {
82 return events->events[index - 1].data.fd;
83 }
84}
85
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
94#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
95 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
96
97/* Create the epoll set. */
98extern int compat_epoll_create(struct lttng_poll_event *events,
99 int size, int flags);
100#define lttng_poll_create(events, size, flags) \
101 compat_epoll_create(events, size, flags)
102
103#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
104static inline int compat_glibc_epoll_create(int size __attribute__((unused)),
105 int flags)
106{
107 return epoll_create1(flags);
108}
109#else
110static 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);
119 LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1);
120 return efd;
121}
122#endif
123
124/*
125 * Wait on epoll set with the number of fd registered to the lttng_poll_event
126 * data structure (events).
127 */
128extern int compat_epoll_wait(struct lttng_poll_event *events, int timeout,
129 bool interruptible);
130#define lttng_poll_wait(events, timeout) \
131 compat_epoll_wait(events, timeout, false)
132#define lttng_poll_wait_interruptible(events, timeout) \
133 compat_epoll_wait(events, timeout, true)
134
135/*
136 * Add a fd to the epoll set and resize the epoll_event structure if needed.
137 */
138extern 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) \
141 compat_epoll_add(events, fd, req_events)
142
143/*
144 * Remove a fd from the epoll set.
145 */
146extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
147#define lttng_poll_del(events, fd) \
148 compat_epoll_del(events, fd)
149
150/*
151 * Modify an fd's events in the epoll set.
152 */
153extern 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) \
156 compat_epoll_mod(events, fd, req_events)
157
158/*
159 * Set up the poll set limits variable poll_max_size
160 */
161extern int compat_epoll_set_max_size();
162#define lttng_poll_set_max_size() \
163 compat_epoll_set_max_size()
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 */
170static 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
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 */
182static inline void lttng_poll_init(struct lttng_poll_event *events)
183{
184 memset(events, 0, sizeof(struct lttng_poll_event));
185 /* Set fd to -1 so if clean before created, we don't close 0. */
186 events->epfd = -1;
187}
188
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 */
193static inline void lttng_poll_clean(struct lttng_poll_event *events)
194{
195 int ret;
196
197 if (!events) {
198 return;
199 }
200
201 if (events->epfd >= 0) {
202 ret = close(events->epfd);
203 if (ret) {
204 PERROR("close");
205 }
206 }
207
208 __lttng_poll_free((void *) events->events);
209}
210
211#else /* HAVE_EPOLL */
212/*
213 * Fallback on poll(2) API
214 */
215
216#include <poll.h>
217#include <stdint.h>
218
219enum {
220 /* Polling variables compatibility for poll */
221 LPOLLIN = POLLIN,
222 LPOLLPRI = POLLPRI,
223 LPOLLOUT = POLLOUT,
224 LPOLLRDNORM = POLLRDNORM,
225 LPOLLRDBAND = POLLRDBAND,
226 LPOLLWRNORM = POLLWRNORM,
227 LPOLLWRBAND = POLLWRBAND,
228#ifdef __USE_GNU
229 LPOLLMSG = POLLMSG,
230 LPOLLRDHUP = POLLRDHUP,
231#else
232 LPOLLMSG = 0,
233 LPOLLRDHUP = 0,
234#endif /* __USE_GNU */
235 LPOLLERR = POLLERR,
236 LPOLLHUP = POLLHUP | POLLNVAL,
237 /* Close on exec feature does not exist for poll(2) */
238 LTTNG_CLOEXEC = 0xdead,
239};
240
241struct compat_poll_event_array {
242 uint32_t nb_fd; /* Current number of fd in events */
243 uint32_t alloc_size; /* Size of events array */
244 /* Initial size of the pollset. We never shrink below that. */
245 uint32_t init_size;
246 struct pollfd *events;
247};
248
249struct compat_poll_event {
250 /*
251 * Modified by the wait action. Updated using current fields if the
252 * need_update flag is set.
253 */
254 struct compat_poll_event_array wait;
255 /*
256 * This is modified by add/del actions being the _current_ flow of
257 * execution before a poll wait is done.
258 */
259 struct compat_poll_event_array current;
260
261 /* Indicate if wait.events need to be updated from current. */
262 int need_update:1;
263};
264#define lttng_poll_event compat_poll_event
265
266static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event *events,
267 int index, uint32_t nb_fd)
268{
269 LTTNG_ASSERT(events);
270 LTTNG_ASSERT(index != nb_fd);
271
272 if (index == 0 || nb_fd == 0) {
273 return -1;
274 } else {
275 return events->current.events[index - 1].fd;
276 }
277}
278
279/*
280 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
281 * being the index of the events array.
282 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
283 * current list for test compatibility purposes.
284 */
285#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
286#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
287#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
288#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
289#define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
290 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
291
292/*
293 * Create a pollfd structure of size 'size'.
294 */
295extern int compat_poll_create(struct lttng_poll_event *events, int size);
296#define lttng_poll_create(events, size, flags) \
297 compat_poll_create(events, size)
298
299/*
300 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
301 * structure.
302 */
303extern int compat_poll_wait(struct lttng_poll_event *events, int timeout,
304 bool interruptible);
305#define lttng_poll_wait(events, timeout) \
306 compat_poll_wait(events, timeout, false)
307#define lttng_poll_wait_interruptible(events, timeout) \
308 compat_poll_wait(events, timeout, true)
309
310/*
311 * Add the fd to the pollfd structure. Resize if needed.
312 */
313extern int compat_poll_add(struct lttng_poll_event *events,
314 int fd, uint32_t req_events);
315#define lttng_poll_add(events, fd, req_events) \
316 compat_poll_add(events, fd, req_events)
317
318/*
319 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
320 * pollfd, data is copied from the old pollfd to the new and, finally, the old
321 * one is freed().
322 */
323extern int compat_poll_del(struct lttng_poll_event *events, int fd);
324#define lttng_poll_del(events, fd) \
325 compat_poll_del(events, fd)
326
327/*
328 * Modify an fd's events in the poll set.
329 */
330extern int compat_poll_mod(struct lttng_poll_event *events,
331 int fd, uint32_t req_events);
332#define lttng_poll_mod(events, fd, req_events) \
333 compat_poll_mod(events, fd, req_events)
334
335/*
336 * Set up the poll set limits variable poll_max_size
337 */
338extern int compat_poll_set_max_size(void);
339#define lttng_poll_set_max_size() \
340 compat_poll_set_max_size()
341
342/*
343 * No need to reset a pollfd structure for poll(2)
344 */
345static inline void lttng_poll_reset(
346 struct lttng_poll_event *events __attribute__((unused)))
347{}
348
349/*
350 * Initialize an already allocated poll event data structure.
351 */
352static inline void lttng_poll_init(struct lttng_poll_event *events)
353{
354 memset(events, 0, sizeof(struct lttng_poll_event));
355}
356
357/*
358 * Clean the events structure of a lttng_poll_event. It's the caller
359 * responsability to free the lttng_poll_event memory.
360 */
361static inline void lttng_poll_clean(struct lttng_poll_event *events)
362{
363 if (events) {
364 __lttng_poll_free((void *) events->wait.events);
365 __lttng_poll_free((void *) events->current.events);
366 }
367}
368
369#endif /* HAVE_EPOLL */
370
371#endif /* _LTT_POLL_H */
This page took 0.022819 seconds and 4 git commands to generate.