Clean-up: run format-cpp on the tree
[lttng-tools.git] / src / common / compat / poll.hpp
CommitLineData
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
28f23191
JG
11#include <common/common.hpp>
12
5eb91c98
DG
13#include <string.h>
14#include <unistd.h>
15
5eb91c98
DG
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{
0e428499 21 free(events);
5eb91c98
DG
22}
23
24/*
25 * epoll(7) implementation.
26 */
27#ifdef HAVE_EPOLL
c9e313bc 28#include <common/compat/fcntl.hpp>
5eb91c98 29
28f23191
JG
30#include <features.h>
31#include <stdio.h>
32#include <sys/epoll.h>
33
5eb91c98 34/* See man epoll(7) for this define path */
990570ed 35#define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
5eb91c98
DG
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,
28f23191 51/* Close on exec feature of epoll */
062fc3d8 52#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
5eb91c98 53 LTTNG_CLOEXEC = EPOLL_CLOEXEC,
f263b7fd
JD
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
5eb91c98
DG
63};
64
65struct compat_epoll_event {
66 int epfd;
28f23191 67 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71 68 uint32_t alloc_size; /* Size of events array */
28f23191 69 uint32_t init_size; /* Initial size of events array */
5eb91c98
DG
70 struct epoll_event *events;
71};
72#define lttng_poll_event compat_epoll_event
73
28f23191
JG
74static inline int
75__lttng_epoll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd)
beaad64c 76{
a0377dfe
FD
77 LTTNG_ASSERT(events);
78 LTTNG_ASSERT(index != nb_fd);
beaad64c
DG
79
80 if (index == 0 || nb_fd == 0) {
81 return -1;
82 } else {
83 return events->events[index - 1].data.fd;
84 }
85}
86
5eb91c98
DG
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 */
28f23191
JG
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)
5eb91c98 96
5a12931e 97/* Create the epoll set. */
28f23191
JG
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)
5eb91c98 100
062fc3d8 101#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
28f23191 102static inline int compat_glibc_epoll_create(int size __attribute__((unused)), int flags)
f263b7fd
JD
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);
a0377dfe 116 LTTNG_ASSERT(fcntl(efd, F_SETFD, flags) != -1);
f263b7fd
JD
117 return efd;
118}
119#endif
120
5eb91c98
DG
121/*
122 * Wait on epoll set with the number of fd registered to the lttng_poll_event
123 * data structure (events).
124 */
28f23191
JG
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)
5eb91c98
DG
128
129/*
130 * Add a fd to the epoll set and resize the epoll_event structure if needed.
131 */
28f23191
JG
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)
5eb91c98
DG
134
135/*
136 * Remove a fd from the epoll set.
137 */
138extern int compat_epoll_del(struct lttng_poll_event *events, int fd);
28f23191 139#define lttng_poll_del(events, fd) compat_epoll_del(events, fd)
5eb91c98 140
f057dfc3
JG
141/*
142 * Modify an fd's events in the epoll set.
143 */
28f23191
JG
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)
f057dfc3 146
5eb91c98
DG
147/*
148 * Set up the poll set limits variable poll_max_size
149 */
cd9adb8b 150extern int compat_epoll_set_max_size();
28f23191 151#define lttng_poll_set_max_size() compat_epoll_set_max_size()
5eb91c98
DG
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) {
28f23191 161 memset(events->events, 0, events->nb_fd * sizeof(struct epoll_event));
5eb91c98
DG
162 }
163}
164
6d737ce4
DG
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{
c5854b1c 171 memset(events, 0, sizeof(struct lttng_poll_event));
6d737ce4
DG
172 /* Set fd to -1 so if clean before created, we don't close 0. */
173 events->epfd = -1;
174}
175
5eb91c98
DG
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{
76d7553f
MD
182 int ret;
183
3cc04881
DG
184 if (!events) {
185 return;
186 }
187
188 if (events->epfd >= 0) {
76d7553f
MD
189 ret = close(events->epfd);
190 if (ret) {
6f04ed72 191 PERROR("close");
76d7553f 192 }
5eb91c98 193 }
3cc04881
DG
194
195 __lttng_poll_free((void *) events->events);
5eb91c98
DG
196}
197
28f23191 198#else /* HAVE_EPOLL */
5eb91c98
DG
199/*
200 * Fallback on poll(2) API
201 */
202
5eb91c98
DG
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,
5f53c4bb 215#ifdef __USE_GNU
5eb91c98 216 LPOLLMSG = POLLMSG,
1268b9d6 217 LPOLLRDHUP = POLLRDHUP,
5f53c4bb 218#else
1268b9d6
DG
219 LPOLLMSG = 0,
220 LPOLLRDHUP = 0,
5f53c4bb 221#endif /* __USE_GNU */
5eb91c98
DG
222 LPOLLERR = POLLERR,
223 LPOLLHUP = POLLHUP | POLLNVAL,
5eb91c98
DG
224 /* Close on exec feature does not exist for poll(2) */
225 LTTNG_CLOEXEC = 0xdead,
226};
227
d21b0d71 228struct compat_poll_event_array {
28f23191 229 uint32_t nb_fd; /* Current number of fd in events */
d21b0d71
DG
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;
5eb91c98
DG
233 struct pollfd *events;
234};
d21b0d71
DG
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;
dbe23f45 247
d21b0d71
DG
248 /* Indicate if wait.events need to be updated from current. */
249 int need_update:1;
250};
28f23191 251#define lttng_poll_event compat_poll_event
5eb91c98 252
28f23191
JG
253static inline int
254__lttng_poll_get_prev_fd(struct lttng_poll_event *events, int index, uint32_t nb_fd)
beaad64c 255{
a0377dfe
FD
256 LTTNG_ASSERT(events);
257 LTTNG_ASSERT(index != nb_fd);
beaad64c
DG
258
259 if (index == 0 || nb_fd == 0) {
260 return -1;
261 } else {
262 return events->current.events[index - 1].fd;
263 }
264}
265
5eb91c98
DG
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.
7e222fa8
YL
269 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
270 * current list for test compatibility purposes.
5eb91c98 271 */
28f23191
JG
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)
5eb91c98
DG
277
278/*
279 * Create a pollfd structure of size 'size'.
280 */
281extern int compat_poll_create(struct lttng_poll_event *events, int size);
28f23191 282#define lttng_poll_create(events, size, flags) compat_poll_create(events, size)
5eb91c98
DG
283
284/*
285 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
286 * structure.
287 */
28f23191
JG
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)
5eb91c98
DG
291
292/*
293 * Add the fd to the pollfd structure. Resize if needed.
294 */
28f23191
JG
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)
5eb91c98
DG
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);
28f23191 304#define lttng_poll_del(events, fd) compat_poll_del(events, fd)
5eb91c98 305
f057dfc3 306/*
b14f53d4 307 * Modify an fd's events in the poll set.
f057dfc3 308 */
28f23191
JG
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)
f057dfc3 311
5eb91c98
DG
312/*
313 * Set up the poll set limits variable poll_max_size
314 */
dbe23f45 315extern int compat_poll_set_max_size(void);
28f23191 316#define lttng_poll_set_max_size() compat_poll_set_max_size()
5eb91c98
DG
317
318/*
319 * No need to reset a pollfd structure for poll(2)
320 */
28f23191
JG
321static inline void lttng_poll_reset(struct lttng_poll_event *events __attribute__((unused)))
322{
323}
5eb91c98 324
6d737ce4
DG
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
5eb91c98
DG
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) {
d21b0d71
DG
340 __lttng_poll_free((void *) events->wait.events);
341 __lttng_poll_free((void *) events->current.events);
5eb91c98
DG
342 }
343}
344
345#endif /* HAVE_EPOLL */
346
347#endif /* _LTT_POLL_H */
This page took 0.093185 seconds and 4 git commands to generate.