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